1 //===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the MCSymbol class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_MC_MCSYMBOL_H
15 #define LLVM_MC_MCSYMBOL_H
17 #include "llvm/ADT/PointerIntPair.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/Support/Compiler.h"
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.
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.
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.
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 {
59 // Special sentinal value for the absolute pseudo section.
61 // FIXME: Use a PointerInt wrapper for this?
62 static MCSection *AbsolutePseudoSection;
64 /// If a symbol has a Fragment, the section is implied, so we only need
66 /// FIXME: We might be able to simplify this by having the asm streamer create
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
73 /// If this is a fragment, then it gives the fragment this symbol's value is
74 /// relative to, if any.
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.
79 mutable PointerIntPair<PointerUnion<MCSection *, MCFragment *>, 1>
80 SectionOrFragmentAndHasName;
82 /// IsTemporary - True if this is an assembler temporary label, which
83 /// typically does not survive in the .o file's symbol table. Usually
85 unsigned IsTemporary : 1;
87 /// \brief True if this symbol can be redefined.
88 unsigned IsRedefinable : 1;
90 /// IsUsed - True if this symbol has been used.
91 mutable unsigned IsUsed : 1;
93 mutable bool IsRegistered : 1;
95 /// This symbol is visible outside this translation unit.
96 mutable unsigned IsExternal : 1;
98 /// This symbol is private extern.
99 mutable unsigned IsPrivateExtern : 1;
101 /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
102 /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
105 /// True if we have created a relocation that uses this symbol.
106 mutable unsigned IsUsedInReloc : 1;
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;
112 /// The alignment of the symbol, if it is 'common', or -1.
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
117 enum : unsigned { NumCommonAlignmentBits = 5 };
118 unsigned CommonAlignLog2 : NumCommonAlignmentBits;
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;
125 /// Index field, for use by the object file implementation.
126 mutable uint32_t Index = 0;
129 /// The offset to apply to the fragment address to form this symbol's value.
132 /// The size of the symbol, if it is 'common'.
135 /// If non-null, the value for a variable symbol.
139 protected: // MCContext creates and uniques these.
141 friend class MCContext;
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.
148 const StringMapEntry<bool> *NameEntry;
149 uint64_t AlignmentPadding;
150 } NameEntryStorageTy;
152 MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
153 : IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
154 IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
155 Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset),
156 CommonAlignLog2(0), Flags(0) {
158 SectionOrFragmentAndHasName.setInt(!!Name);
160 getNameEntryPtr() = Name;
163 // Provide custom new/delete as we will only allocate space for a name
165 void *operator new(size_t s, const StringMapEntry<bool> *Name,
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?");
175 /// \brief Placement delete - required by std, but never called.
176 void operator delete(void*, unsigned, bool) {
177 llvm_unreachable("Constructor throws?");
180 MCSymbol(const MCSymbol &) = delete;
181 void operator=(const MCSymbol &) = delete;
182 MCSection *getSectionPtr(bool SetUsed = true) 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())
190 return Section = getVariableValue(SetUsed)->findAssociatedSection();
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;
199 const StringMapEntry<bool> *&getNameEntryPtr() const {
200 return const_cast<MCSymbol*>(this)->getNameEntryPtr();
204 /// getName - Get the symbol name.
205 StringRef getName() const {
206 if (!SectionOrFragmentAndHasName.getInt())
209 return getNameEntryPtr()->first();
212 bool isRegistered() const { return IsRegistered; }
213 void setIsRegistered(bool Value) const { IsRegistered = Value; }
215 void setUsedInReloc() const { IsUsedInReloc = true; }
216 bool isUsedInReloc() const { return IsUsedInReloc; }
221 /// isTemporary - Check if this is an assembler temporary symbol.
222 bool isTemporary() const { return IsTemporary; }
224 /// isUsed - Check if this is used.
225 bool isUsed() const { return IsUsed; }
226 void setUsed(bool Value) const { IsUsed = Value; }
228 /// \brief Check if this symbol is redefinable.
229 bool isRedefinable() const { return IsRedefinable; }
230 /// \brief Mark this symbol as redefinable.
231 void setRedefinable(bool Value) { IsRedefinable = Value; }
232 /// \brief Prepare this symbol to be redefined.
233 void redefineIfPossible() {
235 if (SymbolContents == SymContentsVariable) {
237 SymbolContents = SymContentsUnset;
240 IsRedefinable = false;
245 /// \name Associated Sections
248 /// isDefined - Check if this symbol is defined (i.e., it has an address).
250 /// Defined symbols are either absolute or in some section.
251 bool isDefined(bool SetUsed = true) const {
252 return getSectionPtr(SetUsed) != nullptr;
255 /// isInSection - Check if this symbol is defined in some section (i.e., it
256 /// is defined but not absolute).
257 bool isInSection(bool SetUsed = true) const {
258 return isDefined(SetUsed) && !isAbsolute(SetUsed);
261 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
262 bool isUndefined(bool SetUsed = true) const { return !isDefined(SetUsed); }
264 /// isAbsolute - Check if this is an absolute symbol.
265 bool isAbsolute(bool SetUsed = true) const {
266 return getSectionPtr(SetUsed) == AbsolutePseudoSection;
269 /// Get the section associated with a defined, non-absolute symbol.
270 MCSection &getSection(bool SetUsed = true) const {
271 assert(isInSection(SetUsed) && "Invalid accessor!");
272 return *getSectionPtr(SetUsed);
275 /// Mark the symbol as defined in the section \p S.
276 void setSection(MCSection &S) {
277 assert(!isVariable() && "Cannot set section of variable");
278 assert(!SectionOrFragmentAndHasName.getPointer().is<MCFragment *>() &&
279 "Section or null expected");
280 SectionOrFragmentAndHasName.setPointer(&S);
283 /// Mark the symbol as undefined.
284 void setUndefined() {
285 SectionOrFragmentAndHasName.setPointer(
286 PointerUnion<MCSection *, MCFragment *>());
289 bool isELF() const { return Kind == SymbolKindELF; }
291 bool isCOFF() const { return Kind == SymbolKindCOFF; }
293 bool isMachO() const { return Kind == SymbolKindMachO; }
296 /// \name Variable Symbols
299 /// isVariable - Check if this is a variable symbol.
300 bool isVariable() const {
301 return SymbolContents == SymContentsVariable;
304 /// getVariableValue - Get the value for variable symbols.
305 const MCExpr *getVariableValue(bool SetUsed = true) const {
306 assert(isVariable() && "Invalid accessor!");
311 void setVariableValue(const MCExpr *Value);
315 /// Get the (implementation defined) index.
316 uint32_t getIndex() const {
320 /// Set the (implementation defined) index.
321 void setIndex(uint32_t Value) const {
325 uint64_t getOffset() const {
326 assert((SymbolContents == SymContentsUnset ||
327 SymbolContents == SymContentsOffset) &&
328 "Cannot get offset for a common/variable symbol");
331 void setOffset(uint64_t Value) {
332 assert((SymbolContents == SymContentsUnset ||
333 SymbolContents == SymContentsOffset) &&
334 "Cannot set offset for a common/variable symbol");
336 SymbolContents = SymContentsOffset;
339 /// Return the size of a 'common' symbol.
340 uint64_t getCommonSize() const {
341 assert(isCommon() && "Not a 'common' symbol!");
345 /// Mark this symbol as being 'common'.
347 /// \param Size - The size of the symbol.
348 /// \param Align - The alignment of the symbol.
349 void setCommon(uint64_t Size, unsigned Align) {
350 assert(getOffset() == 0);
352 SymbolContents = 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 /// \return True if symbol was already declared as a different type
373 bool declareCommon(uint64_t Size, unsigned Align) {
374 assert(isCommon() || getOffset() == 0);
376 if(CommonSize != Size || getCommonAlignment() != Align)
379 setCommon(Size, Align);
383 /// Is this a 'common' symbol.
384 bool isCommon() const {
385 return SymbolContents == SymContentsCommon;
388 MCFragment *getFragment() const {
389 return SectionOrFragmentAndHasName.getPointer().dyn_cast<MCFragment *>();
391 void setFragment(MCFragment *Value) const {
392 SectionOrFragmentAndHasName.setPointer(Value);
395 bool isExternal() const { return IsExternal; }
396 void setExternal(bool Value) const { IsExternal = Value; }
398 bool isPrivateExtern() const { return IsPrivateExtern; }
399 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
401 /// print - Print the value to the stream \p OS.
402 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
404 /// dump - Print the value to stderr.
408 /// Get the (implementation defined) symbol flags.
409 uint32_t getFlags() const { return Flags; }
411 /// Set the (implementation defined) symbol flags.
412 void setFlags(uint32_t Value) const {
413 assert(Value < (1U << NumFlagsBits) && "Out of range flags");
417 /// Modify the flags via a mask
418 void modifyFlags(uint32_t Value, uint32_t Mask) const {
419 assert(Value < (1U << NumFlagsBits) && "Out of range flags");
420 Flags = (Flags & ~Mask) | Value;
424 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
425 Sym.print(OS, nullptr);
428 } // end namespace llvm