MCSymbol.h 14 KB

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