diff --git a/llvm/include/llvm/MC/MCSymbol.h b/llvm/include/llvm/MC/MCSymbol.h index ce160bd2c3cc..13d74157bc01 100644 --- a/llvm/include/llvm/MC/MCSymbol.h +++ b/llvm/include/llvm/MC/MCSymbol.h @@ -42,11 +42,10 @@ class raw_ostream; class MCSymbol { protected: // A symbol can be regular, equated to an expression, or a common symbol. - enum Contents : uint8_t { - SymContentsUnset, - SymContentsVariable, - SymContentsCommon, - SymContentsTargetCommon, // Index stores the section index + enum Kind : uint8_t { + Regular, + Equated, + Common, }; // Special sentinel value for the absolute pseudo fragment. @@ -65,9 +64,9 @@ protected: /// relative to, if any. mutable MCFragment *Fragment = nullptr; - /// This is actually a Contents enumerator, but is unsigned to avoid sign - /// extension and achieve better bitpacking with MSVC. - unsigned SymbolContents : 2; + /// The symbol kind. Use an unsigned bitfield to achieve better bitpacking + /// with MSVC. + unsigned kind : 2; /// True if this symbol is named. A named symbol will have a pointer to the /// name allocated in the bytes immediately prior to the MCSymbol. @@ -145,10 +144,10 @@ protected: }; MCSymbol(const MCSymbolTableEntry *Name, bool isTemporary) - : SymbolContents(SymContentsUnset), IsTemporary(isTemporary), - IsRedefinable(false), IsRegistered(false), IsExternal(false), - IsPrivateExtern(false), IsWeakExternal(false), IsUsedInReloc(false), - IsResolving(0), CommonAlignLog2(0), Flags(0) { + : kind(Kind::Regular), IsTemporary(isTemporary), IsRedefinable(false), + IsRegistered(false), IsExternal(false), IsPrivateExtern(false), + IsWeakExternal(false), IsUsedInReloc(false), IsResolving(0), + CommonAlignLog2(0), Flags(0) { Offset = 0; HasName = !!Name; if (Name) @@ -212,9 +211,9 @@ public: /// Prepare this symbol to be redefined. void redefineIfPossible() { if (IsRedefinable) { - if (SymbolContents == SymContentsVariable) { + if (kind == Kind::Equated) { Value = nullptr; - SymbolContents = SymContentsUnset; + kind = Kind::Regular; } setUndefined(); IsRedefinable = false; @@ -268,9 +267,7 @@ public: /// @{ /// isVariable - Check if this is a variable symbol. - bool isVariable() const { - return SymbolContents == SymContentsVariable; - } + bool isVariable() const { return kind == Equated; } /// Get the expression of the variable symbol. const MCExpr *getVariableValue() const { @@ -293,12 +290,12 @@ public: } uint64_t getOffset() const { - assert(SymbolContents == SymContentsUnset && + assert(kind == Kind::Regular && "Cannot get offset for a common/variable symbol"); return Offset; } void setOffset(uint64_t Value) { - assert(SymbolContents == SymContentsUnset && + assert(kind == Kind::Regular && "Cannot set offset for a common/variable symbol"); Offset = Value; } @@ -314,10 +311,10 @@ public: /// \param Size - The size of the symbol. /// \param Alignment - The alignment of the symbol. /// \param Target - Is the symbol a target-specific common-like symbol. - void setCommon(uint64_t Size, Align Alignment, bool Target = false) { + void setCommon(uint64_t Size, Align Alignment) { assert(getOffset() == 0); CommonSize = Size; - SymbolContents = Target ? SymContentsTargetCommon : SymContentsCommon; + kind = Kind::Common; unsigned Log2Align = encode(Alignment); assert(Log2Align < (1U << NumCommonAlignmentBits) && @@ -335,29 +332,19 @@ public: /// /// \param Size - The size of the symbol. /// \param Alignment - The alignment of the symbol. - /// \param Target - Is the symbol a target-specific common-like symbol. /// \return True if symbol was already declared as a different type - bool declareCommon(uint64_t Size, Align Alignment, bool Target = false) { + bool declareCommon(uint64_t Size, Align Alignment) { assert(isCommon() || getOffset() == 0); if(isCommon()) { - if (CommonSize != Size || getCommonAlignment() != Alignment || - isTargetCommon() != Target) + if (CommonSize != Size || getCommonAlignment() != Alignment) return true; } else - setCommon(Size, Alignment, Target); + setCommon(Size, Alignment); return false; } /// Is this a 'common' symbol. - bool isCommon() const { - return SymbolContents == SymContentsCommon || - SymbolContents == SymContentsTargetCommon; - } - - /// Is this a target-specific common-like symbol. - bool isTargetCommon() const { - return SymbolContents == SymContentsTargetCommon; - } + bool isCommon() const { return kind == Kind::Common; } MCFragment *getFragment() const { if (Fragment || !isVariable() || isWeakExternal()) diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index 8f3814a1dd62..759d3e0e1429 100644 --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -541,12 +541,12 @@ void ELFWriter::computeSymbolTable(const RevGroupMapTy &RevGroupMap) { if (Symbol.isAbsolute()) { MSD.SectionIndex = ELF::SHN_ABS; } else if (Symbol.isCommon()) { - if (Symbol.isTargetCommon()) { - MSD.SectionIndex = Symbol.getIndex(); - } else { + auto Shndx = Symbol.getIndex(); + if (!Shndx) { assert(!Local); - MSD.SectionIndex = ELF::SHN_COMMON; + Shndx = ELF::SHN_COMMON; } + MSD.SectionIndex = Shndx; } else if (Symbol.isUndefined()) { if (Symbol.isSignature() && !Symbol.isUsedInReloc()) { MSD.SectionIndex = RevGroupMap.lookup(&Symbol); diff --git a/llvm/lib/MC/MCSymbol.cpp b/llvm/lib/MC/MCSymbol.cpp index b19842aae46c..771b1204272d 100644 --- a/llvm/lib/MC/MCSymbol.cpp +++ b/llvm/lib/MC/MCSymbol.cpp @@ -48,12 +48,11 @@ void *MCSymbol::operator new(size_t s, const MCSymbolTableEntry *Name, } void MCSymbol::setVariableValue(const MCExpr *Value) { - assert(Value && "Invalid variable value!"); - assert((SymbolContents == SymContentsUnset || - SymbolContents == SymContentsVariable) && - "Cannot give common/offset symbol a variable value"); + assert(Value && "Invalid equated expression"); + assert((kind == Kind::Regular || kind == Kind::Equated) && + "Cannot equate a common symbol"); this->Value = Value; - SymbolContents = SymContentsVariable; + kind = Kind::Equated; setUndefined(); } diff --git a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp index 1f35e92151bf..f000b2cc60c9 100644 --- a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp +++ b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp @@ -886,7 +886,7 @@ void AMDGPUTargetELFStreamer::emitAMDGPULDS(MCSymbol *Symbol, unsigned Size, if (!SymbolELF->isBindingSet()) SymbolELF->setBinding(ELF::STB_GLOBAL); - if (SymbolELF->declareCommon(Size, Alignment, true)) { + if (SymbolELF->declareCommon(Size, Alignment)) { report_fatal_error("Symbol: " + Symbol->getName() + " redeclared as different type"); }