MCSymbol.h 14 KB

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