MCSymbol.h 14 KB

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