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