xref: /freebsd/contrib/llvm-project/llvm/include/llvm/MC/MCSymbol.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
16 #include "llvm/ADT/StringMapEntry.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCSection.h"
20 #include "llvm/MC/MCSymbolTableEntry.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MathExtras.h"
24 #include <cassert>
25 #include <cstddef>
26 #include <cstdint>
27 
28 namespace llvm {
29 
30 class MCAsmInfo;
31 class MCContext;
32 class MCSection;
33 class raw_ostream;
34 
35 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
36 /// and MCSymbols are created and uniqued by the MCContext class.  MCSymbols
37 /// should only be constructed with valid names for the object file.
38 ///
39 /// If the symbol is defined/emitted into the current translation unit, the
40 /// Section member is set to indicate what section it lives in.  Otherwise, if
41 /// it is a reference to an external entity, it has a null section.
42 class MCSymbol {
43 protected:
44   /// The kind of the symbol.  If it is any value other than unset then this
45   /// class is actually one of the appropriate subclasses of MCSymbol.
46   enum SymbolKind {
47     SymbolKindUnset,
48     SymbolKindCOFF,
49     SymbolKindELF,
50     SymbolKindGOFF,
51     SymbolKindMachO,
52     SymbolKindWasm,
53     SymbolKindXCOFF,
54   };
55 
56   /// A symbol can contain an Offset, or Value, or be Common, but never more
57   /// than one of these.
58   enum Contents : uint8_t {
59     SymContentsUnset,
60     SymContentsOffset,
61     SymContentsVariable,
62     SymContentsCommon,
63     SymContentsTargetCommon, // Index stores the section index
64   };
65 
66   // Special sentinel value for the absolute pseudo fragment.
67   LLVM_ABI static MCFragment *AbsolutePseudoFragment;
68 
69   /// If a symbol has a Fragment, the section is implied, so we only need
70   /// one pointer.
71   /// The special AbsolutePseudoFragment value is for absolute symbols.
72   /// If this is a variable symbol, this caches the variable value's fragment.
73   /// FIXME: We might be able to simplify this by having the asm streamer create
74   /// dummy fragments.
75   /// If this is a section, then it gives the symbol is defined in. This is null
76   /// for undefined symbols.
77   ///
78   /// If this is a fragment, then it gives the fragment this symbol's value is
79   /// relative to, if any.
80   mutable MCFragment *Fragment = nullptr;
81 
82   /// True if this symbol is named.  A named symbol will have a pointer to the
83   /// name allocated in the bytes immediately prior to the MCSymbol.
84   unsigned HasName : 1;
85 
86   /// IsTemporary - True if this is an assembler temporary label, which
87   /// typically does not survive in the .o file's symbol table.  Usually
88   /// "Lfoo" or ".foo".
89   unsigned IsTemporary : 1;
90 
91   /// True if this symbol can be redefined.
92   unsigned IsRedefinable : 1;
93 
94   mutable unsigned IsRegistered : 1;
95 
96   /// True if this symbol is visible outside this translation unit. Note: ELF
97   /// uses binding instead of this bit.
98   mutable unsigned IsExternal : 1;
99 
100   /// Mach-O specific: This symbol is private extern.
101   mutable unsigned IsPrivateExtern : 1;
102 
103   /// This symbol is weak external.
104   mutable unsigned IsWeakExternal : 1;
105 
106   /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
107   /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
108   unsigned Kind : 3;
109 
110   /// True if we have created a relocation that uses this symbol.
111   mutable unsigned IsUsedInReloc : 1;
112 
113   /// Used to detect cyclic dependency like `a = a + 1` and `a = b; b = a`.
114   unsigned IsResolving : 1;
115 
116   /// This is actually a Contents enumerator, but is unsigned to avoid sign
117   /// extension and achieve better bitpacking with MSVC.
118   unsigned SymbolContents : 3;
119 
120   /// The alignment of the symbol if it is 'common'.
121   ///
122   /// Internally, this is stored as log2(align) + 1.
123   /// We reserve 5 bits to encode this value which allows the following values
124   /// 0b00000 -> unset
125   /// 0b00001 -> 1ULL <<  0 = 1
126   /// 0b00010 -> 1ULL <<  1 = 2
127   /// 0b00011 -> 1ULL <<  2 = 4
128   /// ...
129   /// 0b11111 -> 1ULL << 30 = 1 GiB
130   enum : unsigned { NumCommonAlignmentBits = 5 };
131   unsigned CommonAlignLog2 : NumCommonAlignmentBits;
132 
133   /// The Flags field is used by object file implementations to store
134   /// additional per symbol information which is not easily classified.
135   enum : unsigned { NumFlagsBits = 16 };
136   mutable uint32_t Flags : NumFlagsBits;
137 
138   /// Index field, for use by the object file implementation.
139   mutable uint32_t Index = 0;
140 
141   union {
142     /// The offset to apply to the fragment address to form this symbol's value.
143     uint64_t Offset;
144 
145     /// The size of the symbol, if it is 'common'.
146     uint64_t CommonSize;
147 
148     /// If non-null, the value for a variable symbol.
149     const MCExpr *Value;
150   };
151 
152   // MCContext creates and uniques these.
153   friend class MCExpr;
154   friend class MCContext;
155 
156   /// The name for a symbol.
157   /// MCSymbol contains a uint64_t so is probably aligned to 8.  On a 32-bit
158   /// system, the name is a pointer so isn't going to satisfy the 8 byte
159   /// alignment of uint64_t.  Account for that here.
160   using NameEntryStorageTy = union {
161     const MCSymbolTableEntry *NameEntry;
162     uint64_t AlignmentPadding;
163   };
164 
MCSymbol(SymbolKind Kind,const MCSymbolTableEntry * Name,bool isTemporary)165   MCSymbol(SymbolKind Kind, const MCSymbolTableEntry *Name, bool isTemporary)
166       : IsTemporary(isTemporary), IsRedefinable(false), IsRegistered(false),
167         IsExternal(false), IsPrivateExtern(false), IsWeakExternal(false),
168         Kind(Kind), IsUsedInReloc(false), IsResolving(0),
169         SymbolContents(SymContentsUnset), CommonAlignLog2(0), Flags(0) {
170     Offset = 0;
171     HasName = !!Name;
172     if (Name)
173       getNameEntryPtr() = Name;
174   }
175 
176   MCSymbol(const MCSymbol &) = default;
177   MCSymbol &operator=(const MCSymbol &) = delete;
178 
179   // Provide custom new/delete as we will only allocate space for a name
180   // if we need one.
181   LLVM_ABI void *operator new(size_t s, const MCSymbolTableEntry *Name,
182                               MCContext &Ctx);
183 
184 private:
185   void operator delete(void *);
186   /// Placement delete - required by std, but never called.
delete(void *,unsigned)187   void operator delete(void*, unsigned) {
188     llvm_unreachable("Constructor throws?");
189   }
190   /// Placement delete - required by std, but never called.
delete(void *,unsigned,bool)191   void operator delete(void*, unsigned, bool) {
192     llvm_unreachable("Constructor throws?");
193   }
194 
195   /// Get a reference to the name field.  Requires that we have a name
getNameEntryPtr()196   const MCSymbolTableEntry *&getNameEntryPtr() {
197     assert(HasName && "Name is required");
198     NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
199     return (*(Name - 1)).NameEntry;
200   }
getNameEntryPtr()201   const MCSymbolTableEntry *&getNameEntryPtr() const {
202     return const_cast<MCSymbol*>(this)->getNameEntryPtr();
203   }
204 
205 public:
206   /// getName - Get the symbol name.
getName()207   StringRef getName() const {
208     if (!HasName)
209       return StringRef();
210 
211     return getNameEntryPtr()->first();
212   }
213 
isRegistered()214   bool isRegistered() const { return IsRegistered; }
setIsRegistered(bool Value)215   void setIsRegistered(bool Value) const { IsRegistered = Value; }
216 
setUsedInReloc()217   void setUsedInReloc() const { IsUsedInReloc = true; }
isUsedInReloc()218   bool isUsedInReloc() const { return IsUsedInReloc; }
219 
220   /// \name Accessors
221   /// @{
222 
223   /// isTemporary - Check if this is an assembler temporary symbol.
isTemporary()224   bool isTemporary() const { return IsTemporary; }
225 
226   /// Check if this symbol is redefinable.
isRedefinable()227   bool isRedefinable() const { return IsRedefinable; }
228   /// Mark this symbol as redefinable.
setRedefinable(bool Value)229   void setRedefinable(bool Value) { IsRedefinable = Value; }
230   /// Prepare this symbol to be redefined.
redefineIfPossible()231   void redefineIfPossible() {
232     if (IsRedefinable) {
233       if (SymbolContents == SymContentsVariable) {
234         Value = nullptr;
235         SymbolContents = SymContentsUnset;
236       }
237       setUndefined();
238       IsRedefinable = false;
239     }
240   }
241 
isResolving()242   bool isResolving() const { return IsResolving; }
setIsResolving(bool V)243   void setIsResolving(bool V) { IsResolving = V; }
244 
245   /// @}
246   /// \name Associated Sections
247   /// @{
248 
249   /// isDefined - Check if this symbol is defined (i.e., it has an address).
250   ///
251   /// Defined symbols are either absolute or in some section.
isDefined()252   bool isDefined() const { return !isUndefined(); }
253 
254   /// isInSection - Check if this symbol is defined in some section (i.e., it
255   /// is defined but not absolute).
isInSection()256   bool isInSection() const {
257     auto *F = getFragment();
258     return F && F != AbsolutePseudoFragment;
259   }
260 
261   /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
isUndefined()262   bool isUndefined() const { return getFragment() == nullptr; }
263 
264   /// isAbsolute - Check if this is an absolute symbol.
isAbsolute()265   bool isAbsolute() const {
266     return getFragment() == AbsolutePseudoFragment;
267   }
268 
269   /// Get the section associated with a defined, non-absolute symbol.
getSection()270   MCSection &getSection() const {
271     assert(isInSection() && "Invalid accessor!");
272     return *getFragment()->getParent();
273   }
274 
275   /// Mark the symbol as defined in the fragment \p F.
setFragment(MCFragment * F)276   void setFragment(MCFragment *F) const {
277     assert(!isVariable() && "Cannot set fragment of variable");
278     Fragment = F;
279   }
280 
281   /// Mark the symbol as undefined.
setUndefined()282   void setUndefined() { Fragment = nullptr; }
283 
isELF()284   bool isELF() const { return Kind == SymbolKindELF; }
285 
isCOFF()286   bool isCOFF() const { return Kind == SymbolKindCOFF; }
287 
isGOFF()288   bool isGOFF() const { return Kind == SymbolKindGOFF; }
289 
isMachO()290   bool isMachO() const { return Kind == SymbolKindMachO; }
291 
isWasm()292   bool isWasm() const { return Kind == SymbolKindWasm; }
293 
isXCOFF()294   bool isXCOFF() const { return Kind == SymbolKindXCOFF; }
295 
296   /// @}
297   /// \name Variable Symbols
298   /// @{
299 
300   /// isVariable - Check if this is a variable symbol.
isVariable()301   bool isVariable() const {
302     return SymbolContents == SymContentsVariable;
303   }
304 
305   /// Get the expression of the variable symbol.
getVariableValue()306   const MCExpr *getVariableValue() const {
307     assert(isVariable() && "Invalid accessor!");
308     return Value;
309   }
310 
311   LLVM_ABI void setVariableValue(const MCExpr *Value);
312 
313   /// @}
314 
315   /// Get the (implementation defined) index.
getIndex()316   uint32_t getIndex() const {
317     return Index;
318   }
319 
320   /// Set the (implementation defined) index.
setIndex(uint32_t Value)321   void setIndex(uint32_t Value) const {
322     Index = Value;
323   }
324 
isUnset()325   bool isUnset() const { return SymbolContents == SymContentsUnset; }
326 
getOffset()327   uint64_t getOffset() const {
328     assert((SymbolContents == SymContentsUnset ||
329             SymbolContents == SymContentsOffset) &&
330            "Cannot get offset for a common/variable symbol");
331     return Offset;
332   }
setOffset(uint64_t Value)333   void setOffset(uint64_t Value) {
334     assert((SymbolContents == SymContentsUnset ||
335             SymbolContents == SymContentsOffset) &&
336            "Cannot set offset for a common/variable symbol");
337     Offset = Value;
338     SymbolContents = SymContentsOffset;
339   }
340 
341   /// Return the size of a 'common' symbol.
getCommonSize()342   uint64_t getCommonSize() const {
343     assert(isCommon() && "Not a 'common' symbol!");
344     return CommonSize;
345   }
346 
347   /// Mark this symbol as being 'common'.
348   ///
349   /// \param Size - The size of the symbol.
350   /// \param Alignment - The alignment of the symbol.
351   /// \param Target - Is the symbol a target-specific common-like symbol.
352   void setCommon(uint64_t Size, Align Alignment, bool Target = false) {
353     assert(getOffset() == 0);
354     CommonSize = Size;
355     SymbolContents = Target ? SymContentsTargetCommon : SymContentsCommon;
356 
357     unsigned Log2Align = encode(Alignment);
358     assert(Log2Align < (1U << NumCommonAlignmentBits) &&
359            "Out of range alignment");
360     CommonAlignLog2 = Log2Align;
361   }
362 
363   ///  Return the alignment of a 'common' symbol.
getCommonAlignment()364   MaybeAlign getCommonAlignment() const {
365     assert(isCommon() && "Not a 'common' symbol!");
366     return decodeMaybeAlign(CommonAlignLog2);
367   }
368 
369   /// Declare this symbol as being 'common'.
370   ///
371   /// \param Size - The size of the symbol.
372   /// \param Alignment - The alignment of the symbol.
373   /// \param Target - Is the symbol a target-specific common-like symbol.
374   /// \return True if symbol was already declared as a different type
375   bool declareCommon(uint64_t Size, Align Alignment, bool Target = false) {
376     assert(isCommon() || getOffset() == 0);
377     if(isCommon()) {
378       if (CommonSize != Size || getCommonAlignment() != Alignment ||
379           isTargetCommon() != Target)
380         return true;
381     } else
382       setCommon(Size, Alignment, Target);
383     return false;
384   }
385 
386   /// Is this a 'common' symbol.
isCommon()387   bool isCommon() const {
388     return SymbolContents == SymContentsCommon ||
389            SymbolContents == SymContentsTargetCommon;
390   }
391 
392   /// Is this a target-specific common-like symbol.
isTargetCommon()393   bool isTargetCommon() const {
394     return SymbolContents == SymContentsTargetCommon;
395   }
396 
getFragment()397   MCFragment *getFragment() const {
398     if (Fragment || !isVariable() || isWeakExternal())
399       return Fragment;
400     // If the symbol is a non-weak alias, get information about
401     // the aliasee. (Don't try to resolve weak aliases.)
402     Fragment = getVariableValue()->findAssociatedFragment();
403     return Fragment;
404   }
405 
406   // For ELF, use MCSymbolELF::setBinding instead.
isExternal()407   bool isExternal() const { return IsExternal; }
setExternal(bool Value)408   void setExternal(bool Value) const { IsExternal = Value; }
409 
410   // COFF-specific
isWeakExternal()411   bool isWeakExternal() const { return IsWeakExternal; }
412 
413   /// print - Print the value to the stream \p OS.
414   LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
415 
416   /// dump - Print the value to stderr.
417   LLVM_ABI void dump() const;
418 
419 protected:
420   /// Get the (implementation defined) symbol flags.
getFlags()421   uint32_t getFlags() const { return Flags; }
422 
423   /// Set the (implementation defined) symbol flags.
setFlags(uint32_t Value)424   void setFlags(uint32_t Value) const {
425     assert(Value < (1U << NumFlagsBits) && "Out of range flags");
426     Flags = Value;
427   }
428 
429   /// Modify the flags via a mask
modifyFlags(uint32_t Value,uint32_t Mask)430   void modifyFlags(uint32_t Value, uint32_t Mask) const {
431     assert(Value < (1U << NumFlagsBits) && "Out of range flags");
432     Flags = (Flags & ~Mask) | Value;
433   }
434 };
435 
436 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
437   Sym.print(OS, nullptr);
438   return OS;
439 }
440 
441 } // end namespace llvm
442 
443 #endif // LLVM_MC_MCSYMBOL_H
444