LLVM 22.0.0git
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/StringRef.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCSection.h"
24#include <cassert>
25#include <cstddef>
26#include <cstdint>
27
28namespace llvm {
29
30class MCAsmInfo;
31class MCContext;
32class MCSection;
33class 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.
42class MCSymbol {
43protected:
44 // A symbol can be regular, equated to an expression, or a common symbol.
45 enum Kind : uint8_t {
49 };
50
51 // Special sentinel value for the absolute pseudo fragment.
53
54 /// If a symbol has a Fragment, the section is implied, so we only need
55 /// one pointer.
56 /// The special AbsolutePseudoFragment value is for absolute symbols.
57 /// If this is a variable symbol, this caches the variable value's fragment.
58 /// FIXME: We might be able to simplify this by having the asm streamer create
59 /// dummy fragments.
60 /// If this is a section, then it gives the symbol is defined in. This is null
61 /// for undefined symbols.
62 ///
63 /// If this is a fragment, then it gives the fragment this symbol's value is
64 /// relative to, if any.
65 mutable MCFragment *Fragment = nullptr;
66
67 /// The symbol kind. Use an unsigned bitfield to achieve better bitpacking
68 /// with MSVC.
69 unsigned kind : 2;
70
71 /// True if this symbol is named. A named symbol will have a pointer to the
72 /// name allocated in the bytes immediately prior to the MCSymbol.
73 unsigned HasName : 1;
74
75 /// IsTemporary - True if this is an assembler temporary label, which
76 /// typically does not survive in the .o file's symbol table. Usually
77 /// "Lfoo" or ".foo".
78 unsigned IsTemporary : 1;
79
80 /// True if this symbol can be redefined.
81 unsigned IsRedefinable : 1;
82
83 mutable unsigned IsRegistered : 1;
84
85 /// True if this symbol is visible outside this translation unit. Note: ELF
86 /// uses binding instead of this bit.
87 mutable unsigned IsExternal : 1;
88
89 /// Mach-O specific: This symbol is private extern.
90 mutable unsigned IsPrivateExtern : 1;
91
92 /// This symbol is weak external.
93 mutable unsigned IsWeakExternal : 1;
94
95 /// True if we have created a relocation that uses this symbol.
96 mutable unsigned IsUsedInReloc : 1;
97
98 /// Used to detect cyclic dependency like `a = a + 1` and `a = b; b = a`.
99 unsigned IsResolving : 1;
100
101 /// The alignment of the symbol if it is 'common'.
102 ///
103 /// Internally, this is stored as log2(align) + 1.
104 /// We reserve 5 bits to encode this value which allows the following values
105 /// 0b00000 -> unset
106 /// 0b00001 -> 1ULL << 0 = 1
107 /// 0b00010 -> 1ULL << 1 = 2
108 /// 0b00011 -> 1ULL << 2 = 4
109 /// ...
110 /// 0b11111 -> 1ULL << 30 = 1 GiB
111 enum : unsigned { NumCommonAlignmentBits = 5 };
113
114 /// The Flags field is used by object file implementations to store
115 /// additional per symbol information which is not easily classified.
116 enum : unsigned { NumFlagsBits = 16 };
118
119 /// Index field, for use by the object file implementation.
120 mutable uint32_t Index = 0;
121
122 union {
123 /// The offset to apply to the fragment address to form this symbol's value.
125
126 /// The size of the symbol, if it is 'common'.
128
129 /// If non-null, the value for a variable symbol.
130 const MCExpr *Value;
131 };
132
133 // MCContext creates and uniques these.
134 friend class MCExpr;
135 friend class MCContext;
136
137 /// The name for a symbol.
138 /// MCSymbol contains a uint64_t so is probably aligned to 8. On a 32-bit
139 /// system, the name is a pointer so isn't going to satisfy the 8 byte
140 /// alignment of uint64_t. Account for that here.
141 using NameEntryStorageTy = union {
142 const MCSymbolTableEntry *NameEntry;
144 };
145
150 CommonAlignLog2(0), Flags(0) {
151 Offset = 0;
152 HasName = !!Name;
153 if (Name)
154 getNameEntryPtr() = Name;
155 }
156
157 MCSymbol(const MCSymbol &) = default;
158 MCSymbol &operator=(const MCSymbol &) = delete;
159
160 // Provide custom new/delete as we will only allocate space for a name
161 // if we need one.
162 LLVM_ABI void *operator new(size_t s, const MCSymbolTableEntry *Name,
163 MCContext &Ctx);
164
165private:
166 void operator delete(void *);
167 /// Placement delete - required by std, but never called.
168 void operator delete(void*, unsigned) {
169 llvm_unreachable("Constructor throws?");
170 }
171 /// Placement delete - required by std, but never called.
172 void operator delete(void*, unsigned, bool) {
173 llvm_unreachable("Constructor throws?");
174 }
175
176 /// Get a reference to the name field. Requires that we have a name
177 const MCSymbolTableEntry *&getNameEntryPtr() {
178 assert(HasName && "Name is required");
179 NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
180 return (*(Name - 1)).NameEntry;
181 }
182 const MCSymbolTableEntry *&getNameEntryPtr() const {
183 return const_cast<MCSymbol*>(this)->getNameEntryPtr();
184 }
185
186public:
187 /// getName - Get the symbol name.
189 if (!HasName)
190 return StringRef();
191
192 return getNameEntryPtr()->first();
193 }
194
195 bool isRegistered() const { return IsRegistered; }
196 void setIsRegistered(bool Value) const { IsRegistered = Value; }
197
198 void setUsedInReloc() const { IsUsedInReloc = true; }
199 bool isUsedInReloc() const { return IsUsedInReloc; }
200
201 /// \name Accessors
202 /// @{
203
204 /// isTemporary - Check if this is an assembler temporary symbol.
205 bool isTemporary() const { return IsTemporary; }
206
207 /// Check if this symbol is redefinable.
208 bool isRedefinable() const { return IsRedefinable; }
209 /// Mark this symbol as redefinable.
211 /// Prepare this symbol to be redefined.
213 if (IsRedefinable) {
214 if (kind == Kind::Equated) {
215 Value = nullptr;
217 }
218 Fragment = nullptr;
219 IsRedefinable = false;
220 }
221 }
222
223 bool isResolving() const { return IsResolving; }
224 void setIsResolving(bool V) { IsResolving = V; }
225
226 /// @}
227 /// \name Associated Sections
228 /// @{
229
230 /// isDefined - Check if this symbol is defined (i.e., it has an address).
231 ///
232 /// Defined symbols are either absolute or in some section.
233 bool isDefined() const { return !isUndefined(); }
234
235 /// isInSection - Check if this symbol is defined in some section (i.e., it
236 /// is defined but not absolute).
237 bool isInSection() const {
238 auto *F = getFragment();
239 return F && F != AbsolutePseudoFragment;
240 }
241
242 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
243 bool isUndefined() const { return getFragment() == nullptr; }
244
245 /// isAbsolute - Check if this is an absolute symbol.
246 bool isAbsolute() const {
248 }
249
250 /// Get the section associated with a defined, non-absolute symbol.
252 assert(isInSection() && "Invalid accessor!");
253 return *getFragment()->getParent();
254 }
255
256 /// Mark the symbol as defined in the fragment \p F.
257 void setFragment(MCFragment *F) const {
258 assert(!isVariable() && "Cannot set fragment of variable");
259 Fragment = F;
260 }
261
262 /// @}
263 /// \name Variable Symbols
264 /// @{
265
266 /// isVariable - Check if this is a variable symbol.
267 bool isVariable() const { return kind == Equated; }
268
269 /// Get the expression of the variable symbol.
270 const MCExpr *getVariableValue() const {
271 assert(isVariable() && "Invalid accessor!");
272 return Value;
273 }
274
276
277 /// @}
278
279 /// Get the (implementation defined) index.
281 return Index;
282 }
283
284 /// Set the (implementation defined) index.
285 void setIndex(uint32_t Value) const {
286 Index = Value;
287 }
288
291 "Cannot get offset for a common/variable symbol");
292 return Offset;
293 }
296 "Cannot set offset for a common/variable symbol");
297 Offset = Value;
298 }
299
300 /// Return the size of a 'common' symbol.
302 assert(isCommon() && "Not a 'common' symbol!");
303 return CommonSize;
304 }
305
306 /// Mark this symbol as being 'common'.
307 ///
308 /// \param Size - The size of the symbol.
309 /// \param Alignment - The alignment of the symbol.
310 /// \param Target - Is the symbol a target-specific common-like symbol.
311 void setCommon(uint64_t Size, Align Alignment) {
312 assert(getOffset() == 0);
315
316 unsigned Log2Align = encode(Alignment);
317 assert(Log2Align < (1U << NumCommonAlignmentBits) &&
318 "Out of range alignment");
319 CommonAlignLog2 = Log2Align;
320 }
321
322 /// Return the alignment of a 'common' symbol.
324 assert(isCommon() && "Not a 'common' symbol!");
326 }
327
328 /// Declare this symbol as being 'common'.
329 ///
330 /// \param Size - The size of the symbol.
331 /// \param Alignment - The alignment of the symbol.
332 /// \return True if symbol was already declared as a different type
333 bool declareCommon(uint64_t Size, Align Alignment) {
334 assert(isCommon() || getOffset() == 0);
335 if(isCommon()) {
336 if (CommonSize != Size || getCommonAlignment() != Alignment)
337 return true;
338 } else
339 setCommon(Size, Alignment);
340 return false;
341 }
342
343 /// Is this a 'common' symbol.
344 bool isCommon() const { return kind == Kind::Common; }
345
347 if (Fragment || !isVariable() || isWeakExternal())
348 return Fragment;
349 // If the symbol is a non-weak alias, get information about
350 // the aliasee. (Don't try to resolve weak aliases.)
352 return Fragment;
353 }
354
355 // COFF-specific
356 bool isWeakExternal() const { return IsWeakExternal; }
357
358 /// print - Print the value to the stream \p OS.
359 LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
360
361 /// dump - Print the value to stderr.
362 LLVM_ABI void dump() const;
363
364protected:
365 /// Get the (implementation defined) symbol flags.
366 uint32_t getFlags() const { return Flags; }
367
368 /// Set the (implementation defined) symbol flags.
369 void setFlags(uint32_t Value) const {
370 assert(Value < (1U << NumFlagsBits) && "Out of range flags");
371 Flags = Value;
372 }
373
374 /// Modify the flags via a mask
375 void modifyFlags(uint32_t Value, uint32_t Mask) const {
376 assert(Value < (1U << NumFlagsBits) && "Out of range flags");
377 Flags = (Flags & ~Mask) | Value;
378 }
379};
380
382 Sym.print(OS, nullptr);
383 return OS;
384}
385
386} // end namespace llvm
387
388#endif // LLVM_MC_MCSYMBOL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMapEntry class - it is intended to be a low dependency implementation det...
#define LLVM_ABI
Definition: Compiler.h:213
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
raw_pwrite_stream & OS
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:64
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
LLVM_ABI MCFragment * findAssociatedFragment() const
Find the "associated section" for this expression, which is currently defined as the absolute section...
Definition: MCExpr.cpp:692
MCSection * getParent() const
Definition: MCSection.h:158
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
bool isAbsolute() const
isAbsolute - Check if this is an absolute symbol.
Definition: MCSymbol.h:246
unsigned IsWeakExternal
This symbol is weak external.
Definition: MCSymbol.h:93
@ NumCommonAlignmentBits
Definition: MCSymbol.h:111
MCSymbol(const MCSymbolTableEntry *Name, bool isTemporary)
Definition: MCSymbol.h:146
void setCommon(uint64_t Size, Align Alignment)
Mark this symbol as being 'common'.
Definition: MCSymbol.h:311
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59
bool isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
Definition: MCSymbol.h:233
void setIsResolving(bool V)
Definition: MCSymbol.h:224
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition: MCSymbol.h:237
bool isUndefined() const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:243
bool isResolving() const
Definition: MCSymbol.h:223
bool isCommon() const
Is this a 'common' symbol.
Definition: MCSymbol.h:344
void modifyFlags(uint32_t Value, uint32_t Mask) const
Modify the flags via a mask.
Definition: MCSymbol.h:375
unsigned IsPrivateExtern
Mach-O specific: This symbol is private extern.
Definition: MCSymbol.h:90
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:188
void setFragment(MCFragment *F) const
Mark the symbol as defined in the fragment F.
Definition: MCSymbol.h:257
unsigned IsRegistered
Definition: MCSymbol.h:83
unsigned IsUsedInReloc
True if we have created a relocation that uses this symbol.
Definition: MCSymbol.h:96
bool isWeakExternal() const
Definition: MCSymbol.h:356
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:267
uint32_t Flags
Definition: MCSymbol.h:117
unsigned IsRedefinable
True if this symbol can be redefined.
Definition: MCSymbol.h:81
union { const MCSymbolTableEntry *NameEntry NameEntryStorageTy
The name for a symbol.
Definition: MCSymbol.h:142
void setFlags(uint32_t Value) const
Set the (implementation defined) symbol flags.
Definition: MCSymbol.h:369
uint32_t getFlags() const
Get the (implementation defined) symbol flags.
Definition: MCSymbol.h:366
unsigned IsExternal
True if this symbol is visible outside this translation unit.
Definition: MCSymbol.h:87
bool isRedefinable() const
Check if this symbol is redefinable.
Definition: MCSymbol.h:208
LLVM_ABI void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:50
MaybeAlign getCommonAlignment() const
Return the alignment of a 'common' symbol.
Definition: MCSymbol.h:323
unsigned CommonAlignLog2
Definition: MCSymbol.h:112
uint32_t Index
Index field, for use by the object file implementation.
Definition: MCSymbol.h:120
uint64_t AlignmentPadding
Definition: MCSymbol.h:143
unsigned HasName
True if this symbol is named.
Definition: MCSymbol.h:73
bool isRegistered() const
Definition: MCSymbol.h:195
void setIndex(uint32_t Value) const
Set the (implementation defined) index.
Definition: MCSymbol.h:285
void setRedefinable(bool Value)
Mark this symbol as redefinable.
Definition: MCSymbol.h:210
bool declareCommon(uint64_t Size, Align Alignment)
Declare this symbol as being 'common'.
Definition: MCSymbol.h:333
void setUsedInReloc() const
Definition: MCSymbol.h:198
uint32_t getIndex() const
Get the (implementation defined) index.
Definition: MCSymbol.h:280
MCFragment * Fragment
If a symbol has a Fragment, the section is implied, so we only need one pointer.
Definition: MCSymbol.h:65
unsigned IsResolving
Used to detect cyclic dependency like a = a + 1 and a = b; b = a.
Definition: MCSymbol.h:99
MCSymbol(const MCSymbol &)=default
unsigned kind
The symbol kind.
Definition: MCSymbol.h:69
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:251
static LLVM_ABI MCFragment * AbsolutePseudoFragment
Definition: MCSymbol.h:52
void setOffset(uint64_t Value)
Definition: MCSymbol.h:294
void redefineIfPossible()
Prepare this symbol to be redefined.
Definition: MCSymbol.h:212
uint64_t getCommonSize() const
Return the size of a 'common' symbol.
Definition: MCSymbol.h:301
const MCExpr * getVariableValue() const
Get the expression of the variable symbol.
Definition: MCSymbol.h:270
unsigned IsTemporary
IsTemporary - True if this is an assembler temporary label, which typically does not survive in the ....
Definition: MCSymbol.h:78
LLVM_ABI void dump() const
dump - Print the value to stderr.
Definition: MCSymbol.cpp:87
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:205
const MCExpr * Value
If non-null, the value for a variable symbol.
Definition: MCSymbol.h:130
void setIsRegistered(bool Value) const
Definition: MCSymbol.h:196
uint64_t CommonSize
The size of the symbol, if it is 'common'.
Definition: MCSymbol.h:127
uint64_t Offset
The offset to apply to the fragment address to form this symbol's value.
Definition: MCSymbol.h:124
MCFragment * getFragment() const
Definition: MCSymbol.h:346
uint64_t getOffset() const
Definition: MCSymbol.h:289
bool isUsedInReloc() const
Definition: MCSymbol.h:199
MCSymbol & operator=(const MCSymbol &)=delete
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringRef first() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
StringMapEntry< MCSymbolTableValue > MCSymbolTableEntry
MCContext stores MCSymbolTableValue in a string map (see MCSymbol::operator new).
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition: Alignment.h:217
MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition: Alignment.h:220
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117