1 //===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the declaration of the MCSymbol class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_MC_MCSYMBOL_H
14 #define LLVM_MC_MCSYMBOL_H
16 #include "llvm/ADT/PointerIntPair.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/MC/MCFragment.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MathExtras.h"
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.
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.
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.
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
64 // Special sentinal value for the absolute pseudo fragment.
65 static MCFragment
*AbsolutePseudoFragment
;
67 /// If a symbol has a Fragment, the section is implied, so we only need
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
73 /// If this is a section, then it gives the symbol is defined in. This is null
74 /// for undefined symbols.
76 /// If this is a fragment, then it gives the fragment this symbol's value is
77 /// relative to, if any.
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.
82 mutable PointerIntPair
<MCFragment
*, 1> FragmentAndHasName
;
84 /// IsTemporary - True if this is an assembler temporary label, which
85 /// typically does not survive in the .o file's symbol table. Usually
87 unsigned IsTemporary
: 1;
89 /// True if this symbol can be redefined.
90 unsigned IsRedefinable
: 1;
92 /// IsUsed - True if this symbol has been used.
93 mutable unsigned IsUsed
: 1;
95 mutable unsigned IsRegistered
: 1;
97 /// This symbol is visible outside this translation unit.
98 mutable unsigned IsExternal
: 1;
100 /// This symbol is private extern.
101 mutable unsigned IsPrivateExtern
: 1;
103 /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
104 /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
107 /// True if we have created a relocation that uses this symbol.
108 mutable unsigned IsUsedInReloc
: 1;
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;
114 /// The alignment of the symbol, if it is 'common', or -1.
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
119 enum : unsigned { NumCommonAlignmentBits
= 5 };
120 unsigned CommonAlignLog2
: NumCommonAlignmentBits
;
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 };
125 mutable uint32_t Flags
: NumFlagsBits
;
127 /// Index field, for use by the object file implementation.
128 mutable uint32_t Index
= 0;
131 /// The offset to apply to the fragment address to form this symbol's value.
134 /// The size of the symbol, if it is 'common'.
137 /// If non-null, the value for a variable symbol.
141 // MCContext creates and uniques these.
143 friend class MCContext
;
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
;
151 uint64_t AlignmentPadding
;
154 MCSymbol(SymbolKind Kind
, const StringMapEntry
<bool> *Name
, bool isTemporary
)
155 : IsTemporary(isTemporary
), IsRedefinable(false), IsUsed(false),
156 IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
157 Kind(Kind
), IsUsedInReloc(false), SymbolContents(SymContentsUnset
),
158 CommonAlignLog2(0), Flags(0) {
160 FragmentAndHasName
.setInt(!!Name
);
162 getNameEntryPtr() = Name
;
165 // Provide custom new/delete as we will only allocate space for a name
167 void *operator new(size_t s
, const StringMapEntry
<bool> *Name
,
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?");
176 /// Placement delete - required by std, but never called.
177 void operator delete(void*, unsigned, bool) {
178 llvm_unreachable("Constructor throws?");
181 MCSection
*getSectionPtr() const {
182 if (MCFragment
*F
= getFragment()) {
183 assert(F
!= AbsolutePseudoFragment
);
184 return F
->getParent();
189 /// Get a reference to the name field. Requires that we have a name
190 const StringMapEntry
<bool> *&getNameEntryPtr() {
191 assert(FragmentAndHasName
.getInt() && "Name is required");
192 NameEntryStorageTy
*Name
= reinterpret_cast<NameEntryStorageTy
*>(this);
193 return (*(Name
- 1)).NameEntry
;
195 const StringMapEntry
<bool> *&getNameEntryPtr() const {
196 return const_cast<MCSymbol
*>(this)->getNameEntryPtr();
200 MCSymbol(const MCSymbol
&) = delete;
201 MCSymbol
&operator=(const MCSymbol
&) = delete;
203 /// getName - Get the symbol name.
204 StringRef
getName() const {
205 if (!FragmentAndHasName
.getInt())
208 return getNameEntryPtr()->first();
211 bool isRegistered() const { return IsRegistered
; }
212 void setIsRegistered(bool Value
) const { IsRegistered
= Value
; }
214 void setUsedInReloc() const { IsUsedInReloc
= true; }
215 bool isUsedInReloc() const { return IsUsedInReloc
; }
220 /// isTemporary - Check if this is an assembler temporary symbol.
221 bool isTemporary() const { return IsTemporary
; }
223 /// isUsed - Check if this is used.
224 bool isUsed() const { return IsUsed
; }
226 /// Check if this symbol is redefinable.
227 bool isRedefinable() const { return IsRedefinable
; }
228 /// Mark this symbol as redefinable.
229 void setRedefinable(bool Value
) { IsRedefinable
= Value
; }
230 /// Prepare this symbol to be redefined.
231 void redefineIfPossible() {
233 if (SymbolContents
== SymContentsVariable
) {
235 SymbolContents
= SymContentsUnset
;
238 IsRedefinable
= false;
243 /// \name Associated Sections
246 /// isDefined - Check if this symbol is defined (i.e., it has an address).
248 /// Defined symbols are either absolute or in some section.
249 bool isDefined() const { return !isUndefined(); }
251 /// isInSection - Check if this symbol is defined in some section (i.e., it
252 /// is defined but not absolute).
253 bool isInSection() const {
254 return isDefined() && !isAbsolute();
257 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
258 bool isUndefined(bool SetUsed
= true) const {
259 return getFragment(SetUsed
) == nullptr;
262 /// isAbsolute - Check if this is an absolute symbol.
263 bool isAbsolute() const {
264 return getFragment() == AbsolutePseudoFragment
;
267 /// Get the section associated with a defined, non-absolute symbol.
268 MCSection
&getSection() const {
269 assert(isInSection() && "Invalid accessor!");
270 return *getSectionPtr();
273 /// Mark the symbol as defined in the fragment \p F.
274 void setFragment(MCFragment
*F
) const {
275 assert(!isVariable() && "Cannot set fragment of variable");
276 FragmentAndHasName
.setPointer(F
);
279 /// Mark the symbol as undefined.
280 void setUndefined() { FragmentAndHasName
.setPointer(nullptr); }
282 bool isELF() const { return Kind
== SymbolKindELF
; }
284 bool isCOFF() const { return Kind
== SymbolKindCOFF
; }
286 bool isMachO() const { return Kind
== SymbolKindMachO
; }
288 bool isWasm() const { return Kind
== SymbolKindWasm
; }
290 bool isXCOFF() const { return Kind
== SymbolKindXCOFF
; }
293 /// \name Variable Symbols
296 /// isVariable - Check if this is a variable symbol.
297 bool isVariable() const {
298 return SymbolContents
== SymContentsVariable
;
301 /// getVariableValue - Get the value for variable symbols.
302 const MCExpr
*getVariableValue(bool SetUsed
= true) const {
303 assert(isVariable() && "Invalid accessor!");
308 void setVariableValue(const MCExpr
*Value
);
312 /// Get the (implementation defined) index.
313 uint32_t getIndex() const {
317 /// Set the (implementation defined) index.
318 void setIndex(uint32_t Value
) const {
322 bool isUnset() const { return SymbolContents
== SymContentsUnset
; }
324 uint64_t getOffset() const {
325 assert((SymbolContents
== SymContentsUnset
||
326 SymbolContents
== SymContentsOffset
) &&
327 "Cannot get offset for a common/variable symbol");
330 void setOffset(uint64_t Value
) {
331 assert((SymbolContents
== SymContentsUnset
||
332 SymbolContents
== SymContentsOffset
) &&
333 "Cannot set offset for a common/variable symbol");
335 SymbolContents
= SymContentsOffset
;
338 /// Return the size of a 'common' symbol.
339 uint64_t getCommonSize() const {
340 assert(isCommon() && "Not a 'common' symbol!");
344 /// Mark this symbol as being 'common'.
346 /// \param Size - The size of the symbol.
347 /// \param Align - The alignment of the symbol.
348 /// \param Target - Is the symbol a target-specific common-like symbol.
349 void setCommon(uint64_t Size
, unsigned Align
, bool Target
= false) {
350 assert(getOffset() == 0);
352 SymbolContents
= Target
? SymContentsTargetCommon
: SymContentsCommon
;
354 assert((!Align
|| isPowerOf2_32(Align
)) &&
355 "Alignment must be a power of 2");
356 unsigned Log2Align
= Log2_32(Align
) + 1;
357 assert(Log2Align
< (1U << NumCommonAlignmentBits
) &&
358 "Out of range alignment");
359 CommonAlignLog2
= Log2Align
;
362 /// Return the alignment of a 'common' symbol.
363 unsigned getCommonAlignment() const {
364 assert(isCommon() && "Not a 'common' symbol!");
365 return CommonAlignLog2
? (1U << (CommonAlignLog2
- 1)) : 0;
368 /// Declare this symbol as being 'common'.
370 /// \param Size - The size of the symbol.
371 /// \param Align - The alignment of the symbol.
372 /// \param Target - Is the symbol a target-specific common-like symbol.
373 /// \return True if symbol was already declared as a different type
374 bool declareCommon(uint64_t Size
, unsigned Align
, bool Target
= false) {
375 assert(isCommon() || getOffset() == 0);
377 if (CommonSize
!= Size
|| getCommonAlignment() != Align
||
378 isTargetCommon() != Target
)
381 setCommon(Size
, Align
, Target
);
385 /// Is this a 'common' symbol.
386 bool isCommon() const {
387 return SymbolContents
== SymContentsCommon
||
388 SymbolContents
== SymContentsTargetCommon
;
391 /// Is this a target-specific common-like symbol.
392 bool isTargetCommon() const {
393 return SymbolContents
== SymContentsTargetCommon
;
396 MCFragment
*getFragment(bool SetUsed
= true) const {
397 MCFragment
*Fragment
= FragmentAndHasName
.getPointer();
398 if (Fragment
|| !isVariable())
400 Fragment
= getVariableValue(SetUsed
)->findAssociatedFragment();
401 FragmentAndHasName
.setPointer(Fragment
);
405 bool isExternal() const { return IsExternal
; }
406 void setExternal(bool Value
) const { IsExternal
= Value
; }
408 bool isPrivateExtern() const { return IsPrivateExtern
; }
409 void setPrivateExtern(bool Value
) { IsPrivateExtern
= Value
; }
411 /// print - Print the value to the stream \p OS.
412 void print(raw_ostream
&OS
, const MCAsmInfo
*MAI
) const;
414 /// dump - Print the value to stderr.
418 /// Get the (implementation defined) symbol flags.
419 uint32_t getFlags() const { return Flags
; }
421 /// Set the (implementation defined) symbol flags.
422 void setFlags(uint32_t Value
) const {
423 assert(Value
< (1U << NumFlagsBits
) && "Out of range flags");
427 /// Modify the flags via a mask
428 void modifyFlags(uint32_t Value
, uint32_t Mask
) const {
429 assert(Value
< (1U << NumFlagsBits
) && "Out of range flags");
430 Flags
= (Flags
& ~Mask
) | Value
;
434 inline raw_ostream
&operator<<(raw_ostream
&OS
, const MCSymbol
&Sym
) {
435 Sym
.print(OS
, nullptr);
439 } // end namespace llvm
441 #endif // LLVM_MC_MCSYMBOL_H