diff --git a/include/llvm/MC/MCMachOSymbolFlags.h b/include/llvm/MC/MCMachOSymbolFlags.h deleted file mode 100644 index 71f01fab206..00000000000 --- a/include/llvm/MC/MCMachOSymbolFlags.h +++ /dev/null @@ -1,46 +0,0 @@ -//===- MCMachOSymbolFlags.h - MachO Symbol Flags ----------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file declares the SymbolFlags used for the MachO target. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_MC_MCMACHOSYMBOLFLAGS_H -#define LLVM_MC_MCMACHOSYMBOLFLAGS_H - -// These flags are mostly used in MCMachOStreamer.cpp but also needed in -// MachObjectWriter.cpp to test for Weak Definitions of symbols to emit -// the correct relocation information. - -namespace llvm { - /// MachOSymbolFlags - We store the value for the 'desc' symbol field in the - /// lowest 16 bits of the implementation defined flags. - enum MachOSymbolFlags { // See . - SF_DescFlagsMask = 0xFFFF, - - // Reference type flags. - SF_ReferenceTypeMask = 0x0007, - SF_ReferenceTypeUndefinedNonLazy = 0x0000, - SF_ReferenceTypeUndefinedLazy = 0x0001, - SF_ReferenceTypeDefined = 0x0002, - SF_ReferenceTypePrivateDefined = 0x0003, - SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004, - SF_ReferenceTypePrivateUndefinedLazy = 0x0005, - - // Other 'desc' flags. - SF_ThumbFunc = 0x0008, - SF_NoDeadStrip = 0x0020, - SF_WeakReference = 0x0040, - SF_WeakDefinition = 0x0080, - SF_SymbolResolver = 0x0100 - }; - -} // end namespace llvm - -#endif diff --git a/include/llvm/MC/MCSymbolMachO.h b/include/llvm/MC/MCSymbolMachO.h index b7f85411d80..166ae9e755a 100644 --- a/include/llvm/MC/MCSymbolMachO.h +++ b/include/llvm/MC/MCSymbolMachO.h @@ -13,11 +13,109 @@ namespace llvm { class MCSymbolMachO : public MCSymbol { + /// \brief We store the value for the 'desc' symbol field in the + /// lowest 16 bits of the implementation defined flags. + enum MachOSymbolFlags : uint16_t { // See . + SF_DescFlagsMask = 0xFFFF, + + // Reference type flags. + SF_ReferenceTypeMask = 0x0007, + SF_ReferenceTypeUndefinedNonLazy = 0x0000, + SF_ReferenceTypeUndefinedLazy = 0x0001, + SF_ReferenceTypeDefined = 0x0002, + SF_ReferenceTypePrivateDefined = 0x0003, + SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004, + SF_ReferenceTypePrivateUndefinedLazy = 0x0005, + + // Other 'desc' flags. + SF_ThumbFunc = 0x0008, + SF_NoDeadStrip = 0x0020, + SF_WeakReference = 0x0040, + SF_WeakDefinition = 0x0080, + SF_SymbolResolver = 0x0100, + + // Common alignment + SF_CommonAlignmentMask = 0xF0FF, + SF_CommonAlignmentShift = 8 + }; public: MCSymbolMachO(const StringMapEntry *Name, bool isTemporary) : MCSymbol(SymbolKindMachO, Name, isTemporary) {} + // Reference type methods. + + void clearReferenceType() const { + modifyFlags(0, SF_ReferenceTypeMask); + } + + void setReferenceTypeUndefinedLazy(bool Value) const { + modifyFlags(Value ? SF_ReferenceTypeUndefinedLazy : 0, + SF_ReferenceTypeUndefinedLazy); + } + + // Other 'desc' methods. + + void setThumbFunc() const { + modifyFlags(SF_ThumbFunc, SF_ThumbFunc); + } + + bool isNoDeadStrip() const { + return getFlags() & SF_NoDeadStrip; + } + void setNoDeadStrip() const { + modifyFlags(SF_NoDeadStrip, SF_NoDeadStrip); + } + + bool isWeakReference() const { + return getFlags() & SF_WeakReference; + } + void setWeakReference() const { + modifyFlags(SF_WeakReference, SF_WeakReference); + } + + bool isWeakDefinition() const { + return getFlags() & SF_WeakDefinition; + } + void setWeakDefinition() const { + modifyFlags(SF_WeakDefinition, SF_WeakDefinition); + } + + bool isSymbolResolver() const { + return getFlags() & SF_SymbolResolver; + } + void setSymbolResolver() const { + modifyFlags(SF_SymbolResolver, SF_SymbolResolver); + } + + void setDesc(unsigned Value) const { + assert(Value == (Value & SF_DescFlagsMask) && + "Invalid .desc value!"); + setFlags(Value & SF_DescFlagsMask); + } + + /// \brief Get the encoded value of the flags as they will be emitted in to + /// the MachO binary + uint16_t getEncodedFlags() const { + uint16_t Flags = getFlags(); + + // Common alignment is packed into the 'desc' bits. + if (isCommon()) { + if (unsigned Align = getCommonAlignment()) { + unsigned Log2Size = Log2_32(Align); + assert((1U << Log2Size) == Align && "Invalid 'common' alignment!"); + if (Log2Size > 15) + report_fatal_error("invalid 'common' alignment '" + + Twine(Align) + "' for '" + getName() + "'", + false); + Flags = (Flags & SF_CommonAlignmentMask) | + (Log2Size << SF_CommonAlignmentShift); + } + } + + return Flags; + } + static bool classof(const MCSymbol *S) { return S->isMachO(); } }; } diff --git a/lib/MC/MCMachOStreamer.cpp b/lib/MC/MCMachOStreamer.cpp index dfa7d24edae..0cbe9744776 100644 --- a/lib/MC/MCMachOStreamer.cpp +++ b/lib/MC/MCMachOStreamer.cpp @@ -23,7 +23,7 @@ #include "llvm/MC/MCObjectStreamer.h" #include "llvm/MC/MCSection.h" #include "llvm/MC/MCSectionMachO.h" -#include "llvm/MC/MCSymbol.h" +#include "llvm/MC/MCSymbolMachO.h" #include "llvm/Support/Dwarf.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/TargetRegistry.h" @@ -172,7 +172,7 @@ void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol, getAssembler().registerSymbol(*Symbol); if (Symbol->isExternal()) EmitSymbolAttribute(EHSymbol, MCSA_Global); - if (Symbol->getFlags() & SF_WeakDefinition) + if (cast(Symbol)->isWeakDefinition()) EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition); if (Symbol->isPrivateExtern()) EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern); @@ -197,7 +197,7 @@ void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { // // FIXME: Cleanup this code, these bits should be emitted based on semantic // properties, not on the order of definition, etc. - Symbol->setFlags(Symbol->getFlags() & ~SF_ReferenceTypeMask); + cast(Symbol)->clearReferenceType(); } void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) { @@ -272,10 +272,13 @@ void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) { // Remember that the function is a thumb function. Fixup and relocation // values will need adjusted. getAssembler().setIsThumbFunc(Symbol); + cast(Symbol)->setThumbFunc(); } -bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, +bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym, MCSymbolAttr Attribute) { + MCSymbolMachO *Symbol = cast(Sym); + // Indirect symbols are handled differently, to match how 'as' handles // them. This makes writing matching .o files easier. if (Attribute == MCSA_IndirectSymbol) { @@ -324,25 +327,25 @@ bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, // // FIXME: Cleanup this code, these bits should be emitted based on semantic // properties, not on the order of definition, etc. - Symbol->setFlags(Symbol->getFlags() & ~SF_ReferenceTypeUndefinedLazy); + Symbol->setReferenceTypeUndefinedLazy(false); break; case MCSA_LazyReference: // FIXME: This requires -dynamic. - Symbol->setFlags(Symbol->getFlags() | SF_NoDeadStrip); + Symbol->setNoDeadStrip(); if (Symbol->isUndefined()) - Symbol->setFlags(Symbol->getFlags() | SF_ReferenceTypeUndefinedLazy); + Symbol->setReferenceTypeUndefinedLazy(true); break; // Since .reference sets the no dead strip bit, it is equivalent to // .no_dead_strip in practice. case MCSA_Reference: case MCSA_NoDeadStrip: - Symbol->setFlags(Symbol->getFlags() | SF_NoDeadStrip); + Symbol->setNoDeadStrip(); break; case MCSA_SymbolResolver: - Symbol->setFlags(Symbol->getFlags() | SF_SymbolResolver); + Symbol->setSymbolResolver(); break; case MCSA_PrivateExtern: @@ -353,17 +356,18 @@ bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, case MCSA_WeakReference: // FIXME: This requires -dynamic. if (Symbol->isUndefined()) - Symbol->setFlags(Symbol->getFlags() | SF_WeakReference); + Symbol->setWeakReference(); break; case MCSA_WeakDefinition: // FIXME: 'as' enforces that this is defined and global. The manual claims // it has to be in a coalesced section, but this isn't enforced. - Symbol->setFlags(Symbol->getFlags() | SF_WeakDefinition); + Symbol->setWeakDefinition(); break; case MCSA_WeakDefAutoPrivate: - Symbol->setFlags(Symbol->getFlags() | SF_WeakDefinition | SF_WeakReference); + Symbol->setWeakDefinition(); + Symbol->setWeakReference(); break; } @@ -372,10 +376,8 @@ bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { // Encode the 'desc' value into the lowest implementation defined bits. - assert(DescValue == (DescValue & SF_DescFlagsMask) && - "Invalid .desc value!"); getAssembler().registerSymbol(*Symbol); - Symbol->setFlags(DescValue & SF_DescFlagsMask); + cast(Symbol)->setDesc(DescValue); } void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, diff --git a/lib/MC/MachObjectWriter.cpp b/lib/MC/MachObjectWriter.cpp index c2f7f211359..fc046b85434 100644 --- a/lib/MC/MachObjectWriter.cpp +++ b/lib/MC/MachObjectWriter.cpp @@ -18,7 +18,7 @@ #include "llvm/MC/MCMachOSymbolFlags.h" #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCSectionMachO.h" -#include "llvm/MC/MCSymbol.h" +#include "llvm/MC/MCSymbolMachO.h" #include "llvm/MC/MCValue.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" @@ -46,7 +46,7 @@ bool MachObjectWriter::doesSymbolRequireExternRelocation(const MCSymbol &S) { // References to weak definitions require external relocation entries; the // definition may not always be the one in the same object file. - if (S.getFlags() & SF_WeakDefinition) + if (cast(S).isWeakDefinition()) return true; // Otherwise, we can use an internal relocation. @@ -327,7 +327,6 @@ void MachObjectWriter::writeNlist(MachSymbolData &MSD, const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol); uint8_t SectionIndex = MSD.SectionIndex; uint8_t Type = 0; - uint16_t Flags = Symbol->getFlags(); uint64_t Address = 0; bool IsAlias = Symbol != AliasedSymbol; @@ -371,23 +370,8 @@ void MachObjectWriter::writeNlist(MachSymbolData &MSD, // Common symbols are encoded with the size in the address // field, and their alignment in the flags. Address = Symbol->getCommonSize(); - - // Common alignment is packed into the 'desc' bits. - if (unsigned Align = Symbol->getCommonAlignment()) { - unsigned Log2Size = Log2_32(Align); - assert((1U << Log2Size) == Align && "Invalid 'common' alignment!"); - if (Log2Size > 15) - report_fatal_error("invalid 'common' alignment '" + - Twine(Align) + "' for '" + Symbol->getName() + "'", - false); - // FIXME: Keep this mask with the SymbolFlags enumeration. - Flags = (Flags & 0xF0FF) | (Log2Size << 8); - } } - if (Layout.getAssembler().isThumbFunc(Symbol)) - Flags |= SF_ThumbFunc; - // struct nlist (12 bytes) write32(MSD.StringIndex); @@ -396,7 +380,7 @@ void MachObjectWriter::writeNlist(MachSymbolData &MSD, // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' // value. - write16(Flags); + write16(cast(Symbol)->getEncodedFlags()); if (is64Bit()) write64(Address); else @@ -515,7 +499,7 @@ void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) { bool Created; Asm.registerSymbol(*it->Symbol, &Created); if (Created) - it->Symbol->setFlags(it->Symbol->getFlags() | 0x0001); + cast(it->Symbol)->setReferenceTypeUndefinedLazy(true); } }