MCSymbol.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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/StringMap.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', or -1.
  104. ///
  105. /// The alignment is stored as log2(align) + 1. This allows all values from
  106. /// 0 to 2^31 to be stored which is every power of 2 representable by an
  107. /// unsigned.
  108. enum : unsigned { NumCommonAlignmentBits = 5 };
  109. unsigned CommonAlignLog2 : NumCommonAlignmentBits;
  110. /// The Flags field is used by object file implementations to store
  111. /// additional per symbol information which is not easily classified.
  112. enum : unsigned { NumFlagsBits = 16 };
  113. mutable uint32_t Flags : NumFlagsBits;
  114. /// Index field, for use by the object file implementation.
  115. mutable uint32_t Index = 0;
  116. union {
  117. /// The offset to apply to the fragment address to form this symbol's value.
  118. uint64_t Offset;
  119. /// The size of the symbol, if it is 'common'.
  120. uint64_t CommonSize;
  121. /// If non-null, the value for a variable symbol.
  122. const MCExpr *Value;
  123. };
  124. // MCContext creates and uniques these.
  125. friend class MCExpr;
  126. friend class MCContext;
  127. /// The name for a symbol.
  128. /// MCSymbol contains a uint64_t so is probably aligned to 8. On a 32-bit
  129. /// system, the name is a pointer so isn't going to satisfy the 8 byte
  130. /// alignment of uint64_t. Account for that here.
  131. using NameEntryStorageTy = union {
  132. const StringMapEntry<bool> *NameEntry;
  133. uint64_t AlignmentPadding;
  134. };
  135. MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
  136. : IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
  137. IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
  138. Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset),
  139. CommonAlignLog2(0), Flags(0) {
  140. Offset = 0;
  141. FragmentAndHasName.setInt(!!Name);
  142. if (Name)
  143. getNameEntryPtr() = Name;
  144. }
  145. // Provide custom new/delete as we will only allocate space for a name
  146. // if we need one.
  147. void *operator new(size_t s, const StringMapEntry<bool> *Name,
  148. MCContext &Ctx);
  149. private:
  150. void operator delete(void *);
  151. /// Placement delete - required by std, but never called.
  152. void operator delete(void*, unsigned) {
  153. llvm_unreachable("Constructor throws?");
  154. }
  155. /// Placement delete - required by std, but never called.
  156. void operator delete(void*, unsigned, bool) {
  157. llvm_unreachable("Constructor throws?");
  158. }
  159. /// Get a reference to the name field. Requires that we have a name
  160. const StringMapEntry<bool> *&getNameEntryPtr() {
  161. assert(FragmentAndHasName.getInt() && "Name is required");
  162. NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
  163. return (*(Name - 1)).NameEntry;
  164. }
  165. const StringMapEntry<bool> *&getNameEntryPtr() const {
  166. return const_cast<MCSymbol*>(this)->getNameEntryPtr();
  167. }
  168. public:
  169. MCSymbol(const MCSymbol &) = delete;
  170. MCSymbol &operator=(const MCSymbol &) = delete;
  171. /// getName - Get the symbol name.
  172. StringRef getName() const {
  173. if (!FragmentAndHasName.getInt())
  174. return StringRef();
  175. return getNameEntryPtr()->first();
  176. }
  177. bool isRegistered() const { return IsRegistered; }
  178. void setIsRegistered(bool Value) const { IsRegistered = Value; }
  179. void setUsedInReloc() const { IsUsedInReloc = true; }
  180. bool isUsedInReloc() const { return IsUsedInReloc; }
  181. /// \name Accessors
  182. /// @{
  183. /// isTemporary - Check if this is an assembler temporary symbol.
  184. bool isTemporary() const { return IsTemporary; }
  185. /// isUsed - Check if this is used.
  186. bool isUsed() const { return IsUsed; }
  187. /// Check if this symbol is redefinable.
  188. bool isRedefinable() const { return IsRedefinable; }
  189. /// Mark this symbol as redefinable.
  190. void setRedefinable(bool Value) { IsRedefinable = Value; }
  191. /// Prepare this symbol to be redefined.
  192. void redefineIfPossible() {
  193. if (IsRedefinable) {
  194. if (SymbolContents == SymContentsVariable) {
  195. Value = nullptr;
  196. SymbolContents = SymContentsUnset;
  197. }
  198. setUndefined();
  199. IsRedefinable = false;
  200. }
  201. }
  202. /// @}
  203. /// \name Associated Sections
  204. /// @{
  205. /// isDefined - Check if this symbol is defined (i.e., it has an address).
  206. ///
  207. /// Defined symbols are either absolute or in some section.
  208. bool isDefined() const { return !isUndefined(); }
  209. /// isInSection - Check if this symbol is defined in some section (i.e., it
  210. /// is defined but not absolute).
  211. bool isInSection() const {
  212. return isDefined() && !isAbsolute();
  213. }
  214. /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
  215. bool isUndefined(bool SetUsed = true) const {
  216. return getFragment(SetUsed) == nullptr;
  217. }
  218. /// isAbsolute - Check if this is an absolute symbol.
  219. bool isAbsolute() const {
  220. return getFragment() == AbsolutePseudoFragment;
  221. }
  222. /// Get the section associated with a defined, non-absolute symbol.
  223. MCSection &getSection() const {
  224. assert(isInSection() && "Invalid accessor!");
  225. return *getFragment()->getParent();
  226. }
  227. /// Mark the symbol as defined in the fragment \p F.
  228. void setFragment(MCFragment *F) const {
  229. assert(!isVariable() && "Cannot set fragment of variable");
  230. FragmentAndHasName.setPointer(F);
  231. }
  232. /// Mark the symbol as undefined.
  233. void setUndefined() { FragmentAndHasName.setPointer(nullptr); }
  234. bool isELF() const { return Kind == SymbolKindELF; }
  235. bool isCOFF() const { return Kind == SymbolKindCOFF; }
  236. bool isGOFF() const { return Kind == SymbolKindGOFF; }
  237. bool isMachO() const { return Kind == SymbolKindMachO; }
  238. bool isWasm() const { return Kind == SymbolKindWasm; }
  239. bool isXCOFF() const { return Kind == SymbolKindXCOFF; }
  240. /// @}
  241. /// \name Variable Symbols
  242. /// @{
  243. /// isVariable - Check if this is a variable symbol.
  244. bool isVariable() const {
  245. return SymbolContents == SymContentsVariable;
  246. }
  247. /// getVariableValue - Get the value for variable symbols.
  248. const MCExpr *getVariableValue(bool SetUsed = true) const {
  249. assert(isVariable() && "Invalid accessor!");
  250. IsUsed |= SetUsed;
  251. return Value;
  252. }
  253. void setVariableValue(const MCExpr *Value);
  254. /// @}
  255. /// Get the (implementation defined) index.
  256. uint32_t getIndex() const {
  257. return Index;
  258. }
  259. /// Set the (implementation defined) index.
  260. void setIndex(uint32_t Value) const {
  261. Index = Value;
  262. }
  263. bool isUnset() const { return SymbolContents == SymContentsUnset; }
  264. uint64_t getOffset() const {
  265. assert((SymbolContents == SymContentsUnset ||
  266. SymbolContents == SymContentsOffset) &&
  267. "Cannot get offset for a common/variable symbol");
  268. return Offset;
  269. }
  270. void setOffset(uint64_t Value) {
  271. assert((SymbolContents == SymContentsUnset ||
  272. SymbolContents == SymContentsOffset) &&
  273. "Cannot set offset for a common/variable symbol");
  274. Offset = Value;
  275. SymbolContents = SymContentsOffset;
  276. }
  277. /// Return the size of a 'common' symbol.
  278. uint64_t getCommonSize() const {
  279. assert(isCommon() && "Not a 'common' symbol!");
  280. return CommonSize;
  281. }
  282. /// Mark this symbol as being 'common'.
  283. ///
  284. /// \param Size - The size of the symbol.
  285. /// \param Align - The alignment of the symbol.
  286. /// \param Target - Is the symbol a target-specific common-like symbol.
  287. void setCommon(uint64_t Size, unsigned Align, bool Target = false) {
  288. assert(getOffset() == 0);
  289. CommonSize = Size;
  290. SymbolContents = Target ? SymContentsTargetCommon : SymContentsCommon;
  291. assert((!Align || isPowerOf2_32(Align)) &&
  292. "Alignment must be a power of 2");
  293. unsigned Log2Align = Log2_32(Align) + 1;
  294. assert(Log2Align < (1U << NumCommonAlignmentBits) &&
  295. "Out of range alignment");
  296. CommonAlignLog2 = Log2Align;
  297. }
  298. /// Return the alignment of a 'common' symbol.
  299. unsigned getCommonAlignment() const {
  300. assert(isCommon() && "Not a 'common' symbol!");
  301. return CommonAlignLog2 ? (1U << (CommonAlignLog2 - 1)) : 0;
  302. }
  303. /// Declare this symbol as being 'common'.
  304. ///
  305. /// \param Size - The size of the symbol.
  306. /// \param Align - The alignment of the symbol.
  307. /// \param Target - Is the symbol a target-specific common-like symbol.
  308. /// \return True if symbol was already declared as a different type
  309. bool declareCommon(uint64_t Size, unsigned Align, bool Target = false) {
  310. assert(isCommon() || getOffset() == 0);
  311. if(isCommon()) {
  312. if (CommonSize != Size || getCommonAlignment() != Align ||
  313. isTargetCommon() != Target)
  314. return true;
  315. } else
  316. setCommon(Size, Align, Target);
  317. return false;
  318. }
  319. /// Is this a 'common' symbol.
  320. bool isCommon() const {
  321. return SymbolContents == SymContentsCommon ||
  322. SymbolContents == SymContentsTargetCommon;
  323. }
  324. /// Is this a target-specific common-like symbol.
  325. bool isTargetCommon() const {
  326. return SymbolContents == SymContentsTargetCommon;
  327. }
  328. MCFragment *getFragment(bool SetUsed = true) const {
  329. MCFragment *Fragment = FragmentAndHasName.getPointer();
  330. if (Fragment || !isVariable())
  331. return Fragment;
  332. Fragment = getVariableValue(SetUsed)->findAssociatedFragment();
  333. FragmentAndHasName.setPointer(Fragment);
  334. return Fragment;
  335. }
  336. bool isExternal() const { return IsExternal; }
  337. void setExternal(bool Value) const { IsExternal = Value; }
  338. bool isPrivateExtern() const { return IsPrivateExtern; }
  339. void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
  340. /// print - Print the value to the stream \p OS.
  341. void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
  342. /// dump - Print the value to stderr.
  343. void dump() const;
  344. protected:
  345. /// Get the (implementation defined) symbol flags.
  346. uint32_t getFlags() const { return Flags; }
  347. /// Set the (implementation defined) symbol flags.
  348. void setFlags(uint32_t Value) const {
  349. assert(Value < (1U << NumFlagsBits) && "Out of range flags");
  350. Flags = Value;
  351. }
  352. /// Modify the flags via a mask
  353. void modifyFlags(uint32_t Value, uint32_t Mask) const {
  354. assert(Value < (1U << NumFlagsBits) && "Out of range flags");
  355. Flags = (Flags & ~Mask) | Value;
  356. }
  357. };
  358. inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
  359. Sym.print(OS, nullptr);
  360. return OS;
  361. }
  362. } // end namespace llvm
  363. #endif // LLVM_MC_MCSYMBOL_H
  364. #ifdef __GNUC__
  365. #pragma GCC diagnostic pop
  366. #endif