LLVM  10.0.0
MCSymbol.h
Go to the documentation of this file.
1 //===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the declaration of the MCSymbol class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_MC_MCSYMBOL_H
14 #define LLVM_MC_MCSYMBOL_H
15 
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/MC/MCFragment.h"
22 #include <cassert>
23 #include <cstddef>
24 #include <cstdint>
25 
26 namespace llvm {
27 
28 class MCAsmInfo;
29 class MCContext;
30 class MCExpr;
31 class MCSection;
32 class raw_ostream;
33 
34 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
35 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
36 /// should only be constructed with valid names for the object file.
37 ///
38 /// If the symbol is defined/emitted into the current translation unit, the
39 /// Section member is set to indicate what section it lives in. Otherwise, if
40 /// it is a reference to an external entity, it has a null section.
41 class MCSymbol {
42 protected:
43  /// The kind of the symbol. If it is any value other than unset then this
44  /// class is actually one of the appropriate subclasses of MCSymbol.
45  enum SymbolKind {
52  };
53 
54  /// A symbol can contain an Offset, or Value, or be Common, but never more
55  /// than one of these.
56  enum Contents : uint8_t {
61  SymContentsTargetCommon, // Index stores the section index
62  };
63 
64  // Special sentinal value for the absolute pseudo fragment.
66 
67  /// If a symbol has a Fragment, the section is implied, so we only need
68  /// one pointer.
69  /// The special AbsolutePseudoFragment value is for absolute symbols.
70  /// If this is a variable symbol, this caches the variable value's fragment.
71  /// FIXME: We might be able to simplify this by having the asm streamer create
72  /// dummy fragments.
73  /// If this is a section, then it gives the symbol is defined in. This is null
74  /// for undefined symbols.
75  ///
76  /// If this is a fragment, then it gives the fragment this symbol's value is
77  /// relative to, if any.
78  ///
79  /// For the 'HasName' integer, this is true if this symbol is named.
80  /// A named symbol will have a pointer to the name allocated in the bytes
81  /// immediately prior to the MCSymbol.
83 
84  /// IsTemporary - True if this is an assembler temporary label, which
85  /// typically does not survive in the .o file's symbol table. Usually
86  /// "Lfoo" or ".foo".
87  unsigned IsTemporary : 1;
88 
89  /// True if this symbol can be redefined.
90  unsigned IsRedefinable : 1;
91 
92  /// IsUsed - True if this symbol has been used.
93  mutable unsigned IsUsed : 1;
94 
95  mutable unsigned IsRegistered : 1;
96 
97  /// This symbol is visible outside this translation unit.
98  mutable unsigned IsExternal : 1;
99 
100  /// This symbol is private extern.
101  mutable unsigned IsPrivateExtern : 1;
102 
103  /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
104  /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
105  unsigned Kind : 3;
106 
107  /// True if we have created a relocation that uses this symbol.
108  mutable unsigned IsUsedInReloc : 1;
109 
110  /// This is actually a Contents enumerator, but is unsigned to avoid sign
111  /// extension and achieve better bitpacking with MSVC.
112  unsigned SymbolContents : 3;
113 
114  /// The alignment of the symbol, if it is 'common', or -1.
115  ///
116  /// The alignment is stored as log2(align) + 1. This allows all values from
117  /// 0 to 2^31 to be stored which is every power of 2 representable by an
118  /// unsigned.
119  enum : unsigned { NumCommonAlignmentBits = 5 };
121 
122  /// The Flags field is used by object file implementations to store
123  /// additional per symbol information which is not easily classified.
124  enum : unsigned { NumFlagsBits = 16 };
126 
127  /// Index field, for use by the object file implementation.
128  mutable uint32_t Index = 0;
129 
130  union {
131  /// The offset to apply to the fragment address to form this symbol's value.
132  uint64_t Offset;
133 
134  /// The size of the symbol, if it is 'common'.
135  uint64_t CommonSize;
136 
137  /// If non-null, the value for a variable symbol.
138  const MCExpr *Value;
139  };
140 
141  // MCContext creates and uniques these.
142  friend class MCExpr;
143  friend class MCContext;
144 
145  /// The name for a symbol.
146  /// MCSymbol contains a uint64_t so is probably aligned to 8. On a 32-bit
147  /// system, the name is a pointer so isn't going to satisfy the 8 byte
148  /// alignment of uint64_t. Account for that here.
149  using NameEntryStorageTy = union {
150  const StringMapEntry<bool> *NameEntry;
152  };
153 
155  : IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
158  CommonAlignLog2(0), Flags(0) {
159  Offset = 0;
160  FragmentAndHasName.setInt(!!Name);
161  if (Name)
162  getNameEntryPtr() = Name;
163  }
164 
165  // Provide custom new/delete as we will only allocate space for a name
166  // if we need one.
167  void *operator new(size_t s, const StringMapEntry<bool> *Name,
168  MCContext &Ctx);
169 
170 private:
171  void operator delete(void *);
172  /// Placement delete - required by std, but never called.
173  void operator delete(void*, unsigned) {
174  llvm_unreachable("Constructor throws?");
175  }
176  /// Placement delete - required by std, but never called.
177  void operator delete(void*, unsigned, bool) {
178  llvm_unreachable("Constructor throws?");
179  }
180 
181  /// Get a reference to the name field. Requires that we have a name
182  const StringMapEntry<bool> *&getNameEntryPtr() {
183  assert(FragmentAndHasName.getInt() && "Name is required");
184  NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
185  return (*(Name - 1)).NameEntry;
186  }
187  const StringMapEntry<bool> *&getNameEntryPtr() const {
188  return const_cast<MCSymbol*>(this)->getNameEntryPtr();
189  }
190 
191 public:
192  MCSymbol(const MCSymbol &) = delete;
193  MCSymbol &operator=(const MCSymbol &) = delete;
194 
195  /// getName - Get the symbol name.
196  StringRef getName() const {
197  if (!FragmentAndHasName.getInt())
198  return StringRef();
199 
200  return getNameEntryPtr()->first();
201  }
202 
203  bool isRegistered() const { return IsRegistered; }
204  void setIsRegistered(bool Value) const { IsRegistered = Value; }
205 
206  void setUsedInReloc() const { IsUsedInReloc = true; }
207  bool isUsedInReloc() const { return IsUsedInReloc; }
208 
209  /// \name Accessors
210  /// @{
211 
212  /// isTemporary - Check if this is an assembler temporary symbol.
213  bool isTemporary() const { return IsTemporary; }
214 
215  /// isUsed - Check if this is used.
216  bool isUsed() const { return IsUsed; }
217 
218  /// Check if this symbol is redefinable.
219  bool isRedefinable() const { return IsRedefinable; }
220  /// Mark this symbol as redefinable.
222  /// Prepare this symbol to be redefined.
224  if (IsRedefinable) {
226  Value = nullptr;
228  }
229  setUndefined();
230  IsRedefinable = false;
231  }
232  }
233 
234  /// @}
235  /// \name Associated Sections
236  /// @{
237 
238  /// isDefined - Check if this symbol is defined (i.e., it has an address).
239  ///
240  /// Defined symbols are either absolute or in some section.
241  bool isDefined() const { return !isUndefined(); }
242 
243  /// isInSection - Check if this symbol is defined in some section (i.e., it
244  /// is defined but not absolute).
245  bool isInSection() const {
246  return isDefined() && !isAbsolute();
247  }
248 
249  /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
250  bool isUndefined(bool SetUsed = true) const {
251  return getFragment(SetUsed) == nullptr;
252  }
253 
254  /// isAbsolute - Check if this is an absolute symbol.
255  bool isAbsolute() const {
257  }
258 
259  /// Get the section associated with a defined, non-absolute symbol.
261  assert(isInSection() && "Invalid accessor!");
262  return *getFragment()->getParent();
263  }
264 
265  /// Mark the symbol as defined in the fragment \p F.
266  void setFragment(MCFragment *F) const {
267  assert(!isVariable() && "Cannot set fragment of variable");
268  FragmentAndHasName.setPointer(F);
269  }
270 
271  /// Mark the symbol as undefined.
272  void setUndefined() { FragmentAndHasName.setPointer(nullptr); }
273 
274  bool isELF() const { return Kind == SymbolKindELF; }
275 
276  bool isCOFF() const { return Kind == SymbolKindCOFF; }
277 
278  bool isMachO() const { return Kind == SymbolKindMachO; }
279 
280  bool isWasm() const { return Kind == SymbolKindWasm; }
281 
282  bool isXCOFF() const { return Kind == SymbolKindXCOFF; }
283 
284  /// @}
285  /// \name Variable Symbols
286  /// @{
287 
288  /// isVariable - Check if this is a variable symbol.
289  bool isVariable() const {
291  }
292 
293  /// getVariableValue - Get the value for variable symbols.
294  const MCExpr *getVariableValue(bool SetUsed = true) const {
295  assert(isVariable() && "Invalid accessor!");
296  IsUsed |= SetUsed;
297  return Value;
298  }
299 
300  void setVariableValue(const MCExpr *Value);
301 
302  /// @}
303 
304  /// Get the (implementation defined) index.
305  uint32_t getIndex() const {
306  return Index;
307  }
308 
309  /// Set the (implementation defined) index.
310  void setIndex(uint32_t Value) const {
311  Index = Value;
312  }
313 
314  bool isUnset() const { return SymbolContents == SymContentsUnset; }
315 
316  uint64_t getOffset() const {
319  "Cannot get offset for a common/variable symbol");
320  return Offset;
321  }
322  void setOffset(uint64_t Value) {
325  "Cannot set offset for a common/variable symbol");
326  Offset = Value;
328  }
329 
330  /// Return the size of a 'common' symbol.
331  uint64_t getCommonSize() const {
332  assert(isCommon() && "Not a 'common' symbol!");
333  return CommonSize;
334  }
335 
336  /// Mark this symbol as being 'common'.
337  ///
338  /// \param Size - The size of the symbol.
339  /// \param Align - The alignment of the symbol.
340  /// \param Target - Is the symbol a target-specific common-like symbol.
341  void setCommon(uint64_t Size, unsigned Align, bool Target = false) {
342  assert(getOffset() == 0);
343  CommonSize = Size;
345 
346  assert((!Align || isPowerOf2_32(Align)) &&
347  "Alignment must be a power of 2");
348  unsigned Log2Align = Log2_32(Align) + 1;
349  assert(Log2Align < (1U << NumCommonAlignmentBits) &&
350  "Out of range alignment");
351  CommonAlignLog2 = Log2Align;
352  }
353 
354  /// Return the alignment of a 'common' symbol.
355  unsigned getCommonAlignment() const {
356  assert(isCommon() && "Not a 'common' symbol!");
357  return CommonAlignLog2 ? (1U << (CommonAlignLog2 - 1)) : 0;
358  }
359 
360  /// Declare this symbol as being 'common'.
361  ///
362  /// \param Size - The size of the symbol.
363  /// \param Align - The alignment of the symbol.
364  /// \param Target - Is the symbol a target-specific common-like symbol.
365  /// \return True if symbol was already declared as a different type
366  bool declareCommon(uint64_t Size, unsigned Align, bool Target = false) {
367  assert(isCommon() || getOffset() == 0);
368  if(isCommon()) {
369  if (CommonSize != Size || getCommonAlignment() != Align ||
370  isTargetCommon() != Target)
371  return true;
372  } else
373  setCommon(Size, Align, Target);
374  return false;
375  }
376 
377  /// Is this a 'common' symbol.
378  bool isCommon() const {
379  return SymbolContents == SymContentsCommon ||
381  }
382 
383  /// Is this a target-specific common-like symbol.
384  bool isTargetCommon() const {
386  }
387 
388  MCFragment *getFragment(bool SetUsed = true) const {
389  MCFragment *Fragment = FragmentAndHasName.getPointer();
390  if (Fragment || !isVariable())
391  return Fragment;
392  Fragment = getVariableValue(SetUsed)->findAssociatedFragment();
393  FragmentAndHasName.setPointer(Fragment);
394  return Fragment;
395  }
396 
397  bool isExternal() const { return IsExternal; }
398  void setExternal(bool Value) const { IsExternal = Value; }
399 
400  bool isPrivateExtern() const { return IsPrivateExtern; }
401  void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
402 
403  /// print - Print the value to the stream \p OS.
404  void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
405 
406  /// dump - Print the value to stderr.
407  void dump() const;
408 
409 protected:
410  /// Get the (implementation defined) symbol flags.
411  uint32_t getFlags() const { return Flags; }
412 
413  /// Set the (implementation defined) symbol flags.
414  void setFlags(uint32_t Value) const {
415  assert(Value < (1U << NumFlagsBits) && "Out of range flags");
416  Flags = Value;
417  }
418 
419  /// Modify the flags via a mask
420  void modifyFlags(uint32_t Value, uint32_t Mask) const {
421  assert(Value < (1U << NumFlagsBits) && "Out of range flags");
422  Flags = (Flags & ~Mask) | Value;
423  }
424 };
425 
426 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
427  Sym.print(OS, nullptr);
428  return OS;
429 }
430 
431 } // end namespace llvm
432 
433 #endif // LLVM_MC_MCSYMBOL_H
bool isCOFF() const
Definition: MCSymbol.h:276
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
union { const StringMapEntry< bool > *NameEntry NameEntryStorageTy
The name for a symbol.
Definition: MCSymbol.h:150
void setInt(IntType IntVal) LLVM_LVALUE_FUNCTION
void setPrivateExtern(bool Value)
Definition: MCSymbol.h:401
uint32_t getIndex() const
Get the (implementation defined) index.
Definition: MCSymbol.h:305
void setUsedInReloc() const
Definition: MCSymbol.h:206
unsigned IsRegistered
Definition: MCSymbol.h:95
This class represents lattice values for constants.
Definition: AllocatorList.h:23
PointerTy getPointer() const
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:289
void setCommon(uint64_t Size, unsigned Align, bool Target=false)
Mark this symbol as being &#39;common&#39;.
Definition: MCSymbol.h:341
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:158
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void redefineIfPossible()
Prepare this symbol to be redefined.
Definition: MCSymbol.h:223
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
bool isWasm() const
Definition: MCSymbol.h:280
bool declareCommon(uint64_t Size, unsigned Align, bool Target=false)
Declare this symbol as being &#39;common&#39;.
Definition: MCSymbol.h:366
unsigned getCommonAlignment() const
Return the alignment of a &#39;common&#39; symbol.
Definition: MCSymbol.h:355
SymbolKind
The kind of the symbol.
Definition: MCSymbol.h:45
F(f)
uint64_t CommonSize
The size of the symbol, if it is &#39;common&#39;.
Definition: MCSymbol.h:135
bool isCommon() const
Is this a &#39;common&#39; symbol.
Definition: MCSymbol.h:378
unsigned IsPrivateExtern
This symbol is private extern.
Definition: MCSymbol.h:101
bool isRedefinable() const
Check if this symbol is redefinable.
Definition: MCSymbol.h:219
void setExternal(bool Value) const
Definition: MCSymbol.h:398
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:245
void setFragment(MCFragment *F) const
Mark the symbol as defined in the fragment F.
Definition: MCSymbol.h:266
Context object for machine code objects.
Definition: MCContext.h:65
unsigned IsUsedInReloc
True if we have created a relocation that uses this symbol.
Definition: MCSymbol.h:108
IntType getInt() const
uint64_t Offset
The offset to apply to the fragment address to form this symbol&#39;s value.
Definition: MCSymbol.h:132
void setPointer(PointerTy PtrVal) LLVM_LVALUE_FUNCTION
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
bool isXCOFF() const
Definition: MCSymbol.h:282
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:465
PointerIntPair - This class implements a pair of a pointer and small integer.
void setRedefinable(bool Value)
Mark this symbol as redefinable.
Definition: MCSymbol.h:221
uint32_t Index
Index field, for use by the object file implementation.
Definition: MCSymbol.h:128
unsigned Kind
LLVM RTTI discriminator.
Definition: MCSymbol.h:105
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:213
uint64_t getCommonSize() const
Return the size of a &#39;common&#39; symbol.
Definition: MCSymbol.h:331
uint64_t getOffset() const
Definition: MCSymbol.h:316
void setOffset(uint64_t Value)
Definition: MCSymbol.h:322
bool isExternal() const
Definition: MCSymbol.h:397
void setFlags(uint32_t Value) const
Set the (implementation defined) symbol flags.
Definition: MCSymbol.h:414
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
static MCFragment * AbsolutePseudoFragment
Definition: MCSymbol.h:65
void setUndefined()
Mark the symbol as undefined.
Definition: MCSymbol.h:272
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:250
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:388
bool isRegistered() const
Definition: MCSymbol.h:203
unsigned IsUsed
IsUsed - True if this symbol has been used.
Definition: MCSymbol.h:93
uint32_t Flags
Definition: MCSymbol.h:125
unsigned IsExternal
This symbol is visible outside this translation unit.
Definition: MCSymbol.h:98
bool isUsed() const
isUsed - Check if this is used.
Definition: MCSymbol.h:216
unsigned SymbolContents
This is actually a Contents enumerator, but is unsigned to avoid sign extension and achieve better bi...
Definition: MCSymbol.h:112
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:48
void dump() const
dump - Print the value to stderr.
Definition: MCSymbol.cpp:85
MCFragment * findAssociatedFragment() const
Find the "associated section" for this expression, which is currently defined as the absolute section...
Definition: MCExpr.cpp:889
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:585
bool isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
Definition: MCSymbol.h:241
Target - Wrapper for Target specific information.
bool isMachO() const
Definition: MCSymbol.h:278
MCSection * getParent() const
Definition: MCFragment.h:87
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:260
bool isAbsolute() const
isAbsolute - Check if this is an absolute symbol.
Definition: MCSymbol.h:255
void setIsRegistered(bool Value) const
Definition: MCSymbol.h:204
bool isUsedInReloc() const
Definition: MCSymbol.h:207
bool isUnset() const
Definition: MCSymbol.h:314
unsigned CommonAlignLog2
Definition: MCSymbol.h:120
unsigned IsRedefinable
True if this symbol can be redefined.
Definition: MCSymbol.h:90
uint32_t Size
Definition: Profile.cpp:46
bool isTargetCommon() const
Is this a target-specific common-like symbol.
Definition: MCSymbol.h:384
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2096
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:196
uint64_t AlignmentPadding
Definition: MCSymbol.h:151
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
Definition: MCSymbol.h:294
MCSymbol & operator=(const MCSymbol &)=delete
LLVM Value Representation.
Definition: Value.h:74
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
const MCExpr * Value
If non-null, the value for a variable symbol.
Definition: MCSymbol.h:138
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
MCSymbol(SymbolKind Kind, const StringMapEntry< bool > *Name, bool isTemporary)
Definition: MCSymbol.h:154
bool isELF() const
Definition: MCSymbol.h:274
PointerIntPair< MCFragment *, 1 > FragmentAndHasName
If a symbol has a Fragment, the section is implied, so we only need one pointer.
Definition: MCSymbol.h:82
Contents
A symbol can contain an Offset, or Value, or be Common, but never more than one of these...
Definition: MCSymbol.h:56
bool isPrivateExtern() const
Definition: MCSymbol.h:400
void setIndex(uint32_t Value) const
Set the (implementation defined) index.
Definition: MCSymbol.h:310
void modifyFlags(uint32_t Value, uint32_t Mask) const
Modify the flags via a mask.
Definition: MCSymbol.h:420
uint32_t getFlags() const
Get the (implementation defined) symbol flags.
Definition: MCSymbol.h:411
unsigned IsTemporary
IsTemporary - True if this is an assembler temporary label, which typically does not survive in the ...
Definition: MCSymbol.h:87
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59