Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

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