MCSymbol.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains the declaration of the MCSymbol class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCSYMBOL_H
  14. #define LLVM_MC_MCSYMBOL_H
  15. #include "llvm/ADT/PointerIntPair.h"
  16. #include "llvm/ADT/PointerUnion.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/MC/MCAssembler.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace llvm {
  21. class MCAsmInfo;
  22. class MCExpr;
  23. class MCSymbol;
  24. class MCFragment;
  25. class MCSection;
  26. class MCContext;
  27. class raw_ostream;
  28. /// MCSymbol - Instances of this class represent a symbol name in the MC file,
  29. /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
  30. /// should only be constructed with valid names for the object file.
  31. ///
  32. /// If the symbol is defined/emitted into the current translation unit, the
  33. /// Section member is set to indicate what section it lives in. Otherwise, if
  34. /// it is a reference to an external entity, it has a null section.
  35. class MCSymbol {
  36. protected:
  37. /// The kind of the symbol. If it is any value other than unset then this
  38. /// class is actually one of the appropriate subclasses of MCSymbol.
  39. enum SymbolKind {
  40. SymbolKindUnset,
  41. SymbolKindCOFF,
  42. SymbolKindELF,
  43. SymbolKindMachO,
  44. };
  45. /// A symbol can contain an Offset, or Value, or be Common, but never more
  46. /// than one of these.
  47. enum Contents : uint8_t {
  48. SymContentsUnset,
  49. SymContentsOffset,
  50. SymContentsVariable,
  51. SymContentsCommon,
  52. };
  53. // Special sentinal value for the absolute pseudo section.
  54. //
  55. // FIXME: Use a PointerInt wrapper for this?
  56. static MCSection *AbsolutePseudoSection;
  57. /// If a symbol has a Fragment, the section is implied, so we only need
  58. /// one pointer.
  59. /// FIXME: We might be able to simplify this by having the asm streamer create
  60. /// dummy fragments.
  61. /// If this is a section, then it gives the symbol is defined in. This is null
  62. /// for undefined symbols, and the special AbsolutePseudoSection value for
  63. /// absolute symbols. If this is a variable symbol, this caches the variable
  64. /// value's section.
  65. ///
  66. /// If this is a fragment, then it gives the fragment this symbol's value is
  67. /// relative to, if any.
  68. ///
  69. /// For the 'HasName' integer, this is true if this symbol is named.
  70. /// A named symbol will have a pointer to the name allocated in the bytes
  71. /// immediately prior to the MCSymbol.
  72. mutable PointerIntPair<PointerUnion<MCSection *, MCFragment *>, 1>
  73. SectionOrFragmentAndHasName;
  74. /// IsTemporary - True if this is an assembler temporary label, which
  75. /// typically does not survive in the .o file's symbol table. Usually
  76. /// "Lfoo" or ".foo".
  77. unsigned IsTemporary : 1;
  78. /// \brief True if this symbol can be redefined.
  79. unsigned IsRedefinable : 1;
  80. /// IsUsed - True if this symbol has been used.
  81. mutable unsigned IsUsed : 1;
  82. mutable bool IsRegistered : 1;
  83. /// This symbol is visible outside this translation unit.
  84. mutable unsigned IsExternal : 1;
  85. /// This symbol is private extern.
  86. mutable unsigned IsPrivateExtern : 1;
  87. /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
  88. /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
  89. unsigned Kind : 2;
  90. /// True if we have created a relocation that uses this symbol.
  91. mutable unsigned IsUsedInReloc : 1;
  92. /// This is actually a Contents enumerator, but is unsigned to avoid sign
  93. /// extension and achieve better bitpacking with MSVC.
  94. unsigned SymbolContents : 2;
  95. /// The alignment of the symbol, if it is 'common', or -1.
  96. ///
  97. /// The alignment is stored as log2(align) + 1. This allows all values from
  98. /// 0 to 2^31 to be stored which is every power of 2 representable by an
  99. /// unsigned.
  100. enum : unsigned { NumCommonAlignmentBits = 5 };
  101. unsigned CommonAlignLog2 : NumCommonAlignmentBits;
  102. /// The Flags field is used by object file implementations to store
  103. /// additional per symbol information which is not easily classified.
  104. enum : unsigned { NumFlagsBits = 16 };
  105. mutable uint32_t Flags : NumFlagsBits;
  106. /// Index field, for use by the object file implementation.
  107. mutable uint32_t Index = 0;
  108. union {
  109. /// The offset to apply to the fragment address to form this symbol's value.
  110. uint64_t Offset;
  111. /// The size of the symbol, if it is 'common'.
  112. uint64_t CommonSize;
  113. /// If non-null, the value for a variable symbol.
  114. const MCExpr *Value;
  115. };
  116. protected: // MCContext creates and uniques these.
  117. friend class MCExpr;
  118. friend class MCContext;
  119. /// \brief The name for a symbol.
  120. /// MCSymbol contains a uint64_t so is probably aligned to 8. On a 32-bit
  121. /// system, the name is a pointer so isn't going to satisfy the 8 byte
  122. /// alignment of uint64_t. Account for that here.
  123. typedef union {
  124. const StringMapEntry<bool> *NameEntry;
  125. uint64_t AlignmentPadding;
  126. } NameEntryStorageTy;
  127. MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
  128. : IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
  129. IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
  130. Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset),
  131. CommonAlignLog2(0), Flags(0) {
  132. Offset = 0;
  133. SectionOrFragmentAndHasName.setInt(!!Name);
  134. if (Name)
  135. getNameEntryPtr() = Name;
  136. }
  137. // Provide custom new/delete as we will only allocate space for a name
  138. // if we need one.
  139. void *operator new(size_t s, const StringMapEntry<bool> *Name,
  140. MCContext &Ctx);
  141. private:
  142. void operator delete(void *);
  143. /// \brief Placement delete - required by std, but never called.
  144. void operator delete(void*, unsigned) {
  145. llvm_unreachable("Constructor throws?");
  146. }
  147. /// \brief Placement delete - required by std, but never called.
  148. void operator delete(void*, unsigned, bool) {
  149. llvm_unreachable("Constructor throws?");
  150. }
  151. MCSymbol(const MCSymbol &) = delete;
  152. void operator=(const MCSymbol &) = delete;
  153. MCSection *getSectionPtr() const {
  154. if (MCFragment *F = getFragment())
  155. return F->getParent();
  156. const auto &SectionOrFragment = SectionOrFragmentAndHasName.getPointer();
  157. assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
  158. MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
  159. if (Section || !isVariable())
  160. return Section;
  161. return Section = getVariableValue()->findAssociatedSection();
  162. }
  163. /// \brief Get a reference to the name field. Requires that we have a name
  164. const StringMapEntry<bool> *&getNameEntryPtr() {
  165. assert(SectionOrFragmentAndHasName.getInt() && "Name is required");
  166. NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
  167. return (*(Name - 1)).NameEntry;
  168. }
  169. const StringMapEntry<bool> *&getNameEntryPtr() const {
  170. return const_cast<MCSymbol*>(this)->getNameEntryPtr();
  171. }
  172. public:
  173. /// getName - Get the symbol name.
  174. StringRef getName() const {
  175. if (!SectionOrFragmentAndHasName.getInt())
  176. return StringRef();
  177. return getNameEntryPtr()->first();
  178. }
  179. bool isRegistered() const { return IsRegistered; }
  180. void setIsRegistered(bool Value) const { IsRegistered = Value; }
  181. void setUsedInReloc() const { IsUsedInReloc = true; }
  182. bool isUsedInReloc() const { return IsUsedInReloc; }
  183. /// \name Accessors
  184. /// @{
  185. /// isTemporary - Check if this is an assembler temporary symbol.
  186. bool isTemporary() const { return IsTemporary; }
  187. /// isUsed - Check if this is used.
  188. bool isUsed() const { return IsUsed; }
  189. void setUsed(bool Value) const { IsUsed = Value; }
  190. /// \brief Check if this symbol is redefinable.
  191. bool isRedefinable() const { return IsRedefinable; }
  192. /// \brief Mark this symbol as redefinable.
  193. void setRedefinable(bool Value) { IsRedefinable = Value; }
  194. /// \brief Prepare this symbol to be redefined.
  195. void redefineIfPossible() {
  196. if (IsRedefinable) {
  197. if (SymbolContents == SymContentsVariable) {
  198. Value = nullptr;
  199. SymbolContents = SymContentsUnset;
  200. }
  201. setUndefined();
  202. IsRedefinable = false;
  203. }
  204. }
  205. /// @}
  206. /// \name Associated Sections
  207. /// @{
  208. /// isDefined - Check if this symbol is defined (i.e., it has an address).
  209. ///
  210. /// Defined symbols are either absolute or in some section.
  211. bool isDefined() const { return getSectionPtr() != nullptr; }
  212. /// isInSection - Check if this symbol is defined in some section (i.e., it
  213. /// is defined but not absolute).
  214. bool isInSection() const { return isDefined() && !isAbsolute(); }
  215. /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
  216. bool isUndefined() const { return !isDefined(); }
  217. /// isAbsolute - Check if this is an absolute symbol.
  218. bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
  219. /// Get the section associated with a defined, non-absolute symbol.
  220. MCSection &getSection() const {
  221. assert(isInSection() && "Invalid accessor!");
  222. return *getSectionPtr();
  223. }
  224. /// Mark the symbol as defined in the section \p S.
  225. void setSection(MCSection &S) {
  226. assert(!isVariable() && "Cannot set section of variable");
  227. assert(!SectionOrFragmentAndHasName.getPointer().is<MCFragment *>() &&
  228. "Section or null expected");
  229. SectionOrFragmentAndHasName.setPointer(&S);
  230. }
  231. /// Mark the symbol as undefined.
  232. void setUndefined() {
  233. SectionOrFragmentAndHasName.setPointer(
  234. PointerUnion<MCSection *, MCFragment *>());
  235. }
  236. bool isELF() const { return Kind == SymbolKindELF; }
  237. bool isCOFF() const { return Kind == SymbolKindCOFF; }
  238. bool isMachO() const { return Kind == SymbolKindMachO; }
  239. /// @}
  240. /// \name Variable Symbols
  241. /// @{
  242. /// isVariable - Check if this is a variable symbol.
  243. bool isVariable() const {
  244. return SymbolContents == SymContentsVariable;
  245. }
  246. /// getVariableValue() - Get the value for variable symbols.
  247. const MCExpr *getVariableValue() const {
  248. assert(isVariable() && "Invalid accessor!");
  249. IsUsed = true;
  250. return Value;
  251. }
  252. void setVariableValue(const MCExpr *Value);
  253. /// @}
  254. /// Get the (implementation defined) index.
  255. uint32_t getIndex() const {
  256. return Index;
  257. }
  258. /// Set the (implementation defined) index.
  259. void setIndex(uint32_t Value) const {
  260. Index = Value;
  261. }
  262. uint64_t getOffset() const {
  263. assert((SymbolContents == SymContentsUnset ||
  264. SymbolContents == SymContentsOffset) &&
  265. "Cannot get offset for a common/variable symbol");
  266. return Offset;
  267. }
  268. void setOffset(uint64_t Value) {
  269. assert((SymbolContents == SymContentsUnset ||
  270. SymbolContents == SymContentsOffset) &&
  271. "Cannot set offset for a common/variable symbol");
  272. Offset = Value;
  273. SymbolContents = SymContentsOffset;
  274. }
  275. /// Return the size of a 'common' symbol.
  276. uint64_t getCommonSize() const {
  277. assert(isCommon() && "Not a 'common' symbol!");
  278. return CommonSize;
  279. }
  280. /// Mark this symbol as being 'common'.
  281. ///
  282. /// \param Size - The size of the symbol.
  283. /// \param Align - The alignment of the symbol.
  284. void setCommon(uint64_t Size, unsigned Align) {
  285. assert(getOffset() == 0);
  286. CommonSize = Size;
  287. SymbolContents = SymContentsCommon;
  288. assert((!Align || isPowerOf2_32(Align)) &&
  289. "Alignment must be a power of 2");
  290. unsigned Log2Align = Log2_32(Align) + 1;
  291. assert(Log2Align < (1U << NumCommonAlignmentBits) &&
  292. "Out of range alignment");
  293. CommonAlignLog2 = Log2Align;
  294. }
  295. /// Return the alignment of a 'common' symbol.
  296. unsigned getCommonAlignment() const {
  297. assert(isCommon() && "Not a 'common' symbol!");
  298. return CommonAlignLog2 ? (1U << (CommonAlignLog2 - 1)) : 0;
  299. }
  300. /// Declare this symbol as being 'common'.
  301. ///
  302. /// \param Size - The size of the symbol.
  303. /// \param Align - The alignment of the symbol.
  304. /// \return True if symbol was already declared as a different type
  305. bool declareCommon(uint64_t Size, unsigned Align) {
  306. assert(isCommon() || getOffset() == 0);
  307. if(isCommon()) {
  308. if(CommonSize != Size || getCommonAlignment() != Align)
  309. return true;
  310. } else
  311. setCommon(Size, Align);
  312. return false;
  313. }
  314. /// Is this a 'common' symbol.
  315. bool isCommon() const {
  316. return SymbolContents == SymContentsCommon;
  317. }
  318. MCFragment *getFragment() const {
  319. return SectionOrFragmentAndHasName.getPointer().dyn_cast<MCFragment *>();
  320. }
  321. void setFragment(MCFragment *Value) const {
  322. SectionOrFragmentAndHasName.setPointer(Value);
  323. }
  324. bool isExternal() const { return IsExternal; }
  325. void setExternal(bool Value) const { IsExternal = Value; }
  326. bool isPrivateExtern() const { return IsPrivateExtern; }
  327. void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
  328. /// print - Print the value to the stream \p OS.
  329. void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
  330. /// dump - Print the value to stderr.
  331. void dump() const;
  332. protected:
  333. /// Get the (implementation defined) symbol flags.
  334. uint32_t getFlags() const { return Flags; }
  335. /// Set the (implementation defined) symbol flags.
  336. void setFlags(uint32_t Value) const {
  337. assert(Value < (1U << NumFlagsBits) && "Out of range flags");
  338. Flags = Value;
  339. }
  340. /// Modify the flags via a mask
  341. void modifyFlags(uint32_t Value, uint32_t Mask) const {
  342. assert(Value < (1U << NumFlagsBits) && "Out of range flags");
  343. Flags = (Flags & ~Mask) | Value;
  344. }
  345. };
  346. inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
  347. Sym.print(OS, nullptr);
  348. return OS;
  349. }
  350. } // end namespace llvm
  351. #endif