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/PointerUnion.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/Support/Compiler.h"
31 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
32 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
33 /// should only be constructed with valid names for the object file.
35 /// If the symbol is defined/emitted into the current translation unit, the
36 /// Section member is set to indicate what section it lives in. Otherwise, if
37 /// it is a reference to an external entity, it has a null section.
40 /// The kind of the symbol. If it is any value other than unset then this
41 /// class is actually one of the appropriate subclasses of MCSymbol.
49 // Special sentinal value for the absolute pseudo section.
51 // FIXME: Use a PointerInt wrapper for this?
52 static MCSection *AbsolutePseudoSection;
54 /// If a symbol has a Fragment, the section is implied, so we only need
56 /// FIXME: We might be able to simplify this by having the asm streamer create
58 /// If this is a section, then it gives the symbol is defined in. This is null
59 /// for undefined symbols, and the special AbsolutePseudoSection value for
60 /// absolute symbols. If this is a variable symbol, this caches the variable
63 /// If this is a fragment, then it gives the fragment this symbol's value is
64 /// relative to, if any.
65 mutable PointerUnion<MCSection *, MCFragment *> SectionOrFragment;
67 /// Value - If non-null, the value for a variable symbol.
70 /// IsTemporary - True if this is an assembler temporary label, which
71 /// typically does not survive in the .o file's symbol table. Usually
73 unsigned IsTemporary : 1;
75 /// \brief True if this symbol can be redefined.
76 unsigned IsRedefinable : 1;
78 /// IsUsed - True if this symbol has been used.
79 mutable unsigned IsUsed : 1;
81 mutable bool IsRegistered : 1;
83 /// This symbol is visible outside this translation unit.
84 mutable unsigned IsExternal : 1;
86 /// This symbol is private extern.
87 mutable unsigned IsPrivateExtern : 1;
89 /// True if this symbol is named.
90 /// A named symbol will have a pointer to the name allocated in the bytes
91 /// immediately prior to the MCSymbol.
94 /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
95 /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
98 /// True if we have created a relocation that uses this symbol.
99 mutable unsigned IsUsedInReloc : 1;
101 /// Index field, for use by the object file implementation.
102 mutable uint32_t Index = 0;
105 /// The offset to apply to the fragment address to form this symbol's value.
108 /// The size of the symbol, if it is 'common'.
112 /// The alignment of the symbol, if it is 'common', or -1.
114 // FIXME: Pack this in with other fields?
115 unsigned CommonAlign = -1U;
117 /// The Flags field is used by object file implementations to store
118 /// additional per symbol information which is not easily classified.
119 mutable uint32_t Flags = 0;
121 protected: // MCContext creates and uniques these.
123 friend class MCContext;
125 /// \brief The name for a symbol.
126 /// MCSymbol contains a uint64_t so is probably aligned to 8. On a 32-bit
127 /// system, the name is a pointer so isn't going to satisfy the 8 byte
128 /// alignment of uint64_t. Account for that here.
130 const StringMapEntry<bool> *NameEntry;
131 uint64_t AlignmentPadding;
132 } NameEntryStorageTy;
134 MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
135 : Value(nullptr), IsTemporary(isTemporary), IsRedefinable(false),
136 IsUsed(false), IsRegistered(false), IsExternal(false),
137 IsPrivateExtern(false), HasName(!!Name), Kind(Kind),
138 IsUsedInReloc(false) {
141 getNameEntryPtr() = Name;
144 // Provide custom new/delete as we will only allocate space for a name
146 void *operator new(size_t s, const StringMapEntry<bool> *Name,
151 void operator delete(void *);
152 /// \brief Placement delete - required by std, but never called.
153 void operator delete(void*, unsigned) {
154 llvm_unreachable("Constructor throws?");
156 /// \brief Placement delete - required by std, but never called.
157 void operator delete(void*, unsigned, bool) {
158 llvm_unreachable("Constructor throws?");
161 MCSymbol(const MCSymbol &) = delete;
162 void operator=(const MCSymbol &) = delete;
163 MCSection *getSectionPtr() const {
164 if (MCFragment *F = getFragment())
165 return F->getParent();
166 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
167 MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
168 if (Section || !Value)
170 return Section = Value->findAssociatedSection();
173 /// \brief Get a reference to the name field. Requires that we have a name
174 const StringMapEntry<bool> *&getNameEntryPtr() {
175 assert(HasName && "Name is required");
176 NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
177 return (*(Name - 1)).NameEntry;
179 const StringMapEntry<bool> *&getNameEntryPtr() const {
180 return const_cast<MCSymbol*>(this)->getNameEntryPtr();
184 /// getName - Get the symbol name.
185 StringRef getName() const {
189 return getNameEntryPtr()->first();
192 bool isRegistered() const { return IsRegistered; }
193 void setIsRegistered(bool Value) const { IsRegistered = Value; }
195 void setUsedInReloc() const { IsUsedInReloc = true; }
196 bool isUsedInReloc() const { return IsUsedInReloc; }
201 /// isTemporary - Check if this is an assembler temporary symbol.
202 bool isTemporary() const { return IsTemporary; }
204 /// isUsed - Check if this is used.
205 bool isUsed() const { return IsUsed; }
206 void setUsed(bool Value) const { IsUsed = Value; }
208 /// \brief Check if this symbol is redefinable.
209 bool isRedefinable() const { return IsRedefinable; }
210 /// \brief Mark this symbol as redefinable.
211 void setRedefinable(bool Value) { IsRedefinable = Value; }
212 /// \brief Prepare this symbol to be redefined.
213 void redefineIfPossible() {
216 SectionOrFragment = nullptr;
217 IsRedefinable = false;
222 /// \name Associated Sections
225 /// isDefined - Check if this symbol is defined (i.e., it has an address).
227 /// Defined symbols are either absolute or in some section.
228 bool isDefined() const { return getSectionPtr() != nullptr; }
230 /// isInSection - Check if this symbol is defined in some section (i.e., it
231 /// is defined but not absolute).
232 bool isInSection() const { return isDefined() && !isAbsolute(); }
234 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
235 bool isUndefined() const { return !isDefined(); }
237 /// isAbsolute - Check if this is an absolute symbol.
238 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
240 /// Get the section associated with a defined, non-absolute symbol.
241 MCSection &getSection() const {
242 assert(isInSection() && "Invalid accessor!");
243 return *getSectionPtr();
246 /// Mark the symbol as defined in the section \p S.
247 void setSection(MCSection &S) {
248 assert(!isVariable() && "Cannot set section of variable");
249 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
250 SectionOrFragment = &S;
253 /// Mark the symbol as undefined.
254 void setUndefined() {
255 SectionOrFragment = nullptr;
258 bool isELF() const { return Kind == SymbolKindELF; }
260 bool isCOFF() const { return Kind == SymbolKindCOFF; }
262 bool isMachO() const { return Kind == SymbolKindMachO; }
265 /// \name Variable Symbols
268 /// isVariable - Check if this is a variable symbol.
269 bool isVariable() const { return Value != nullptr; }
271 /// getVariableValue() - Get the value for variable symbols.
272 const MCExpr *getVariableValue() const {
273 assert(isVariable() && "Invalid accessor!");
278 void setVariableValue(const MCExpr *Value);
282 /// Get the (implementation defined) index.
283 uint32_t getIndex() const {
287 /// Set the (implementation defined) index.
288 void setIndex(uint32_t Value) const {
292 uint64_t getOffset() const {
296 void setOffset(uint64_t Value) {
301 /// Return the size of a 'common' symbol.
302 uint64_t getCommonSize() const {
303 assert(isCommon() && "Not a 'common' symbol!");
307 /// Mark this symbol as being 'common'.
309 /// \param Size - The size of the symbol.
310 /// \param Align - The alignment of the symbol.
311 void setCommon(uint64_t Size, unsigned Align) {
312 assert(getOffset() == 0);
317 /// Return the alignment of a 'common' symbol.
318 unsigned getCommonAlignment() const {
319 assert(isCommon() && "Not a 'common' symbol!");
323 /// Declare this symbol as being 'common'.
325 /// \param Size - The size of the symbol.
326 /// \param Align - The alignment of the symbol.
327 /// \return True if symbol was already declared as a different type
328 bool declareCommon(uint64_t Size, unsigned Align) {
329 assert(isCommon() || getOffset() == 0);
331 if(CommonSize != Size || CommonAlign != Align)
334 setCommon(Size, Align);
338 /// Is this a 'common' symbol.
339 bool isCommon() const { return CommonAlign != -1U; }
341 MCFragment *getFragment() const {
342 return SectionOrFragment.dyn_cast<MCFragment *>();
344 void setFragment(MCFragment *Value) const {
345 SectionOrFragment = Value;
348 bool isExternal() const { return IsExternal; }
349 void setExternal(bool Value) const { IsExternal = Value; }
351 bool isPrivateExtern() const { return IsPrivateExtern; }
352 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
354 /// print - Print the value to the stream \p OS.
355 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
357 /// dump - Print the value to stderr.
361 /// Get the (implementation defined) symbol flags.
362 uint32_t getFlags() const { return Flags; }
364 /// Set the (implementation defined) symbol flags.
365 void setFlags(uint32_t Value) const { Flags = Value; }
367 /// Modify the flags via a mask
368 void modifyFlags(uint32_t Value, uint32_t Mask) const {
369 Flags = (Flags & ~Mask) | Value;
373 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
374 Sym.print(OS, nullptr);
377 } // end namespace llvm