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 /// A symbol can contain an Offset, or Value, or be Common, but never more
50 /// than one of these.
51 enum Contents : uint8_t {
58 // Special sentinal value for the absolute pseudo section.
60 // FIXME: Use a PointerInt wrapper for this?
61 static MCSection *AbsolutePseudoSection;
63 /// If a symbol has a Fragment, the section is implied, so we only need
65 /// FIXME: We might be able to simplify this by having the asm streamer create
67 /// If this is a section, then it gives the symbol is defined in. This is null
68 /// for undefined symbols, and the special AbsolutePseudoSection value for
69 /// absolute symbols. If this is a variable symbol, this caches the variable
72 /// If this is a fragment, then it gives the fragment this symbol's value is
73 /// relative to, if any.
74 mutable PointerUnion<MCSection *, MCFragment *> SectionOrFragment;
76 /// IsTemporary - True if this is an assembler temporary label, which
77 /// typically does not survive in the .o file's symbol table. Usually
79 unsigned IsTemporary : 1;
81 /// \brief True if this symbol can be redefined.
82 unsigned IsRedefinable : 1;
84 /// IsUsed - True if this symbol has been used.
85 mutable unsigned IsUsed : 1;
87 mutable bool IsRegistered : 1;
89 /// This symbol is visible outside this translation unit.
90 mutable unsigned IsExternal : 1;
92 /// This symbol is private extern.
93 mutable unsigned IsPrivateExtern : 1;
95 /// True if this symbol is named.
96 /// A named symbol will have a pointer to the name allocated in the bytes
97 /// immediately prior to the MCSymbol.
100 /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
101 /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
104 /// True if we have created a relocation that uses this symbol.
105 mutable unsigned IsUsedInReloc : 1;
107 /// This is actually a Contents enumerator, but is unsigned to avoid sign
108 /// extension and achieve better bitpacking with MSVC.
109 unsigned SymbolContents : 2;
111 /// Index field, for use by the object file implementation.
112 mutable uint32_t Index = 0;
115 /// The offset to apply to the fragment address to form this symbol's value.
118 /// The size of the symbol, if it is 'common'.
121 /// If non-null, the value for a variable symbol.
125 /// The alignment of the symbol, if it is 'common', or -1.
127 // FIXME: Pack this in with other fields?
128 unsigned CommonAlign = -1U;
130 /// The Flags field is used by object file implementations to store
131 /// additional per symbol information which is not easily classified.
132 mutable uint32_t Flags = 0;
134 protected: // MCContext creates and uniques these.
136 friend class MCContext;
138 /// \brief The name for a symbol.
139 /// MCSymbol contains a uint64_t so is probably aligned to 8. On a 32-bit
140 /// system, the name is a pointer so isn't going to satisfy the 8 byte
141 /// alignment of uint64_t. Account for that here.
143 const StringMapEntry<bool> *NameEntry;
144 uint64_t AlignmentPadding;
145 } NameEntryStorageTy;
147 MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
148 : IsTemporary(isTemporary), IsRedefinable(false),
149 IsUsed(false), IsRegistered(false), IsExternal(false),
150 IsPrivateExtern(false), HasName(!!Name), Kind(Kind),
151 IsUsedInReloc(false), SymbolContents(SymContentsUnset) {
154 getNameEntryPtr() = Name;
157 // Provide custom new/delete as we will only allocate space for a name
159 void *operator new(size_t s, const StringMapEntry<bool> *Name,
164 void operator delete(void *);
165 /// \brief Placement delete - required by std, but never called.
166 void operator delete(void*, unsigned) {
167 llvm_unreachable("Constructor throws?");
169 /// \brief Placement delete - required by std, but never called.
170 void operator delete(void*, unsigned, bool) {
171 llvm_unreachable("Constructor throws?");
174 MCSymbol(const MCSymbol &) = delete;
175 void operator=(const MCSymbol &) = delete;
176 MCSection *getSectionPtr() const {
177 if (MCFragment *F = getFragment())
178 return F->getParent();
179 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
180 MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
181 if (Section || !isVariable())
183 return Section = getVariableValue()->findAssociatedSection();
186 /// \brief Get a reference to the name field. Requires that we have a name
187 const StringMapEntry<bool> *&getNameEntryPtr() {
188 assert(HasName && "Name is required");
189 NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
190 return (*(Name - 1)).NameEntry;
192 const StringMapEntry<bool> *&getNameEntryPtr() const {
193 return const_cast<MCSymbol*>(this)->getNameEntryPtr();
197 /// getName - Get the symbol name.
198 StringRef getName() const {
202 return getNameEntryPtr()->first();
205 bool isRegistered() const { return IsRegistered; }
206 void setIsRegistered(bool Value) const { IsRegistered = Value; }
208 void setUsedInReloc() const { IsUsedInReloc = true; }
209 bool isUsedInReloc() const { return IsUsedInReloc; }
214 /// isTemporary - Check if this is an assembler temporary symbol.
215 bool isTemporary() const { return IsTemporary; }
217 /// isUsed - Check if this is used.
218 bool isUsed() const { return IsUsed; }
219 void setUsed(bool Value) const { IsUsed = Value; }
221 /// \brief Check if this symbol is redefinable.
222 bool isRedefinable() const { return IsRedefinable; }
223 /// \brief Mark this symbol as redefinable.
224 void setRedefinable(bool Value) { IsRedefinable = Value; }
225 /// \brief Prepare this symbol to be redefined.
226 void redefineIfPossible() {
228 if (SymbolContents == SymContentsVariable) {
230 SymbolContents = SymContentsUnset;
232 SectionOrFragment = nullptr;
233 IsRedefinable = false;
238 /// \name Associated Sections
241 /// isDefined - Check if this symbol is defined (i.e., it has an address).
243 /// Defined symbols are either absolute or in some section.
244 bool isDefined() const { return getSectionPtr() != nullptr; }
246 /// isInSection - Check if this symbol is defined in some section (i.e., it
247 /// is defined but not absolute).
248 bool isInSection() const { return isDefined() && !isAbsolute(); }
250 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
251 bool isUndefined() const { return !isDefined(); }
253 /// isAbsolute - Check if this is an absolute symbol.
254 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
256 /// Get the section associated with a defined, non-absolute symbol.
257 MCSection &getSection() const {
258 assert(isInSection() && "Invalid accessor!");
259 return *getSectionPtr();
262 /// Mark the symbol as defined in the section \p S.
263 void setSection(MCSection &S) {
264 assert(!isVariable() && "Cannot set section of variable");
265 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
266 SectionOrFragment = &S;
269 /// Mark the symbol as undefined.
270 void setUndefined() {
271 SectionOrFragment = nullptr;
274 bool isELF() const { return Kind == SymbolKindELF; }
276 bool isCOFF() const { return Kind == SymbolKindCOFF; }
278 bool isMachO() const { return Kind == SymbolKindMachO; }
281 /// \name Variable Symbols
284 /// isVariable - Check if this is a variable symbol.
285 bool isVariable() const {
286 return SymbolContents == SymContentsVariable;
289 /// getVariableValue() - Get the value for variable symbols.
290 const MCExpr *getVariableValue() const {
291 assert(isVariable() && "Invalid accessor!");
296 void setVariableValue(const MCExpr *Value);
300 /// Get the (implementation defined) index.
301 uint32_t getIndex() const {
305 /// Set the (implementation defined) index.
306 void setIndex(uint32_t Value) const {
310 uint64_t getOffset() const {
311 assert((SymbolContents == SymContentsUnset ||
312 SymbolContents == SymContentsOffset) &&
313 "Cannot get offset for a common/variable symbol");
316 void setOffset(uint64_t Value) {
317 assert((SymbolContents == SymContentsUnset ||
318 SymbolContents == SymContentsOffset) &&
319 "Cannot set offset for a common/variable symbol");
321 SymbolContents = SymContentsOffset;
324 /// Return the size of a 'common' symbol.
325 uint64_t getCommonSize() const {
326 assert(isCommon() && "Not a 'common' symbol!");
330 /// Mark this symbol as being 'common'.
332 /// \param Size - The size of the symbol.
333 /// \param Align - The alignment of the symbol.
334 void setCommon(uint64_t Size, unsigned Align) {
335 assert(getOffset() == 0);
338 SymbolContents = SymContentsCommon;
341 /// Return the alignment of a 'common' symbol.
342 unsigned getCommonAlignment() const {
343 assert(isCommon() && "Not a 'common' symbol!");
347 /// Declare this symbol as being 'common'.
349 /// \param Size - The size of the symbol.
350 /// \param Align - The alignment of the symbol.
351 /// \return True if symbol was already declared as a different type
352 bool declareCommon(uint64_t Size, unsigned Align) {
353 assert(isCommon() || getOffset() == 0);
355 if(CommonSize != Size || CommonAlign != Align)
358 setCommon(Size, Align);
362 /// Is this a 'common' symbol.
363 bool isCommon() const {
364 return SymbolContents == SymContentsCommon;
367 MCFragment *getFragment() const {
368 return SectionOrFragment.dyn_cast<MCFragment *>();
370 void setFragment(MCFragment *Value) const {
371 SectionOrFragment = Value;
374 bool isExternal() const { return IsExternal; }
375 void setExternal(bool Value) const { IsExternal = Value; }
377 bool isPrivateExtern() const { return IsPrivateExtern; }
378 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
380 /// print - Print the value to the stream \p OS.
381 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
383 /// dump - Print the value to stderr.
387 /// Get the (implementation defined) symbol flags.
388 uint32_t getFlags() const { return Flags; }
390 /// Set the (implementation defined) symbol flags.
391 void setFlags(uint32_t Value) const { Flags = Value; }
393 /// Modify the flags via a mask
394 void modifyFlags(uint32_t Value, uint32_t Mask) const {
395 Flags = (Flags & ~Mask) | Value;
399 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
400 Sym.print(OS, nullptr);
403 } // end namespace llvm