//===----------------------------------------------------------------------===//
#include "llvm/MC/MCELFObjectWriter.h"
-#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
};
/// The target specific ELF writer instance.
- llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
+ std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
const MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap);
- virtual void RecordRelocation(const MCAssembler &Asm,
- const MCAsmLayout &Layout,
- const MCFragment *Fragment,
- const MCFixup &Fixup,
- MCValue Target, uint64_t &FixedValue);
+ void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
+ const MCFragment *Fragment, const MCFixup &Fixup,
+ MCValue Target, uint64_t &FixedValue) override;
uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
const MCSymbol *S);
// Map from a section to its offset
typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
- /// ComputeSymbolTable - Compute the symbol table data
+ /// Compute the symbol table data
///
/// \param Asm - The assembler.
/// \param SectionIndexMap - Maps a section to its index.
/// \param RevGroupMap - Maps a signature symbol to the group section.
/// \param NumRegularSections - Number of non-relocation sections.
- void ComputeSymbolTable(MCAssembler &Asm,
+ void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap,
RevGroupMapTy RevGroupMap,
unsigned NumRegularSections);
SectionIndexMapTy &SectionIndexMap,
const RelMapTy &RelMap);
- virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
- const MCAsmLayout &Layout);
+ void ExecutePostLayoutBinding(MCAssembler &Asm,
+ const MCAsmLayout &Layout) override;
void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
const MCAsmLayout &Layout,
MCDataFragment *F,
const MCSectionData *SD);
- virtual bool
+ bool
IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
const MCSymbolData &DataA,
const MCFragment &FB,
bool InSet,
- bool IsPCRel) const;
+ bool IsPCRel) const override;
- virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
+ void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
void WriteSection(MCAssembler &Asm,
const SectionIndexMapTy &SectionIndexMap,
uint32_t GroupSymbolIndex,
}
}
-uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
+uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &OrigData,
const MCAsmLayout &Layout) {
- if (Data.isCommon() && Data.isExternal())
- return Data.getCommonAlignment();
+ MCSymbolData *Data = &OrigData;
+ if (Data->isCommon() && Data->isExternal())
+ return Data->getCommonAlignment();
- const MCSymbol &Symbol = Data.getSymbol();
+ const MCSymbol *Symbol = &Data->getSymbol();
+ bool IsThumbFunc = OrigData.getFlags() & ELF_Other_ThumbFunc;
+
+ uint64_t Res = 0;
+ if (Symbol->isVariable()) {
+ const MCExpr *Expr = Symbol->getVariableValue();
+ MCValue Value;
+ if (!Expr->EvaluateAsRelocatable(Value, &Layout))
+ llvm_unreachable("Invalid expression");
+
+ assert(!Value.getSymB());
+
+ Res = Value.getConstant();
- if (Symbol.isAbsolute() && Symbol.isVariable()) {
- if (const MCExpr *Value = Symbol.getVariableValue()) {
- int64_t IntValue;
- if (Value->EvaluateAsAbsolute(IntValue, Layout))
- return (uint64_t)IntValue;
+ if (const MCSymbolRefExpr *A = Value.getSymA()) {
+ Symbol = &A->getSymbol();
+ Data = &Layout.getAssembler().getSymbolData(*Symbol);
+ } else {
+ Symbol = 0;
+ Data = 0;
}
}
- if (!Symbol.isInSection())
- return 0;
+ if (IsThumbFunc)
+ Res |= 1;
+ if (!Symbol || !Symbol->isInSection())
+ return Res;
- if (Data.getFragment()) {
- if (Data.getFlags() & ELF_Other_ThumbFunc)
- return Layout.getSymbolOffset(&Data)+1;
- else
- return Layout.getSymbolOffset(&Data);
- }
+ Res += Layout.getSymbolOffset(Data);
- return 0;
+ return Res;
}
void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
}
}
+static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
+ uint8_t Type = newType;
+
+ // Propagation rules:
+ // IFUNC > FUNC > OBJECT > NOTYPE
+ // TLS_OBJECT > OBJECT > NOTYPE
+ //
+ // dont let the new type degrade the old type
+ switch (origType) {
+ default:
+ break;
+ case ELF::STT_GNU_IFUNC:
+ if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
+ Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
+ Type = ELF::STT_GNU_IFUNC;
+ break;
+ case ELF::STT_FUNC:
+ if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
+ Type == ELF::STT_TLS)
+ Type = ELF::STT_FUNC;
+ break;
+ case ELF::STT_OBJECT:
+ if (Type == ELF::STT_NOTYPE)
+ Type = ELF::STT_OBJECT;
+ break;
+ case ELF::STT_TLS:
+ if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
+ Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
+ Type = ELF::STT_TLS;
+ break;
+ }
+
+ return Type;
+}
+
+static const MCSymbol *getBaseSymbol(const MCAsmLayout &Layout,
+ const MCSymbol &Symbol) {
+ if (!Symbol.isVariable())
+ return &Symbol;
+
+ const MCExpr *Expr = Symbol.getVariableValue();
+ MCValue Value;
+ if (!Expr->EvaluateAsRelocatable(Value, &Layout))
+ llvm_unreachable("Invalid Expression");
+ assert(!Value.getSymB());
+ const MCSymbolRefExpr *A = Value.getSymA();
+ if (!A)
+ return nullptr;
+ return getBaseSymbol(Layout, A->getSymbol());
+}
+
void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
MCDataFragment *ShndxF,
ELFSymbolData &MSD,
// Binding and Type share the same byte as upper and lower nibbles
uint8_t Binding = MCELF::GetBinding(OrigData);
- uint8_t Type = MCELF::GetType(Data);
+ uint8_t Type = MCELF::GetType(OrigData);
+ const MCSymbol *Base = getBaseSymbol(Layout, OrigData.getSymbol());
+ if (Base) {
+ MCSymbolData BaseSD = Layout.getAssembler().getSymbolData(*Base);
+ Type = mergeTypeForSet(Type, MCELF::GetType(BaseSD));
+ }
+ if (OrigData.getFlags() & ELF_Other_ThumbFunc)
+ Type = ELF::STT_FUNC;
uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
// Other and Visibility share the same byte with Visibility using the lower
uint8_t Other = MCELF::getOther(OrigData) << (ELF_STO_Shift - ELF_STV_Shift);
Other |= Visibility;
- uint64_t Value = SymbolValue(Data, Layout);
+ uint64_t Value = SymbolValue(OrigData, Layout);
+ if (OrigData.getFlags() & ELF_Other_ThumbFunc)
+ Value |= 1;
uint64_t Size = 0;
assert(!(Data.isCommon() && !Data.isExternal()));
MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
IsPCRel = true;
+ if (!SDB.getFragment())
+ Asm.getContext().FatalError(
+ Fixup.getLoc(),
+ Twine("symbol '") + SymbolB.getName() +
+ "' can not be undefined in a subtraction expression");
+
// Offset of the symbol in the section
int64_t a = Layout.getSymbolOffset(&SDB);
Index = 0;
}
} else {
- if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
+ if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_WEAKREF)
WeakrefUsedInReloc.insert(RelocSymbol);
else
UsedInReloc.insert(RelocSymbol);
bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
const MCSymbolData &Data,
bool Used, bool Renamed) {
- if (Data.getFlags() & ELF_Other_Weakref)
- return false;
+ const MCSymbol &Symbol = Data.getSymbol();
+ if (Symbol.isVariable()) {
+ const MCExpr *Expr = Symbol.getVariableValue();
+ if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
+ if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
+ return false;
+ }
+ }
if (Used)
return true;
if (Renamed)
return false;
- const MCSymbol &Symbol = Data.getSymbol();
-
if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
return true;
if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
return false;
- if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
- return false;
-
if (Symbol.isTemporary())
return false;
}
}
-void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
- const SectionIndexMapTy &SectionIndexMap,
- RevGroupMapTy RevGroupMap,
- unsigned NumRegularSections) {
+void
+ELFObjectWriter::computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
+ const SectionIndexMapTy &SectionIndexMap,
+ RevGroupMapTy RevGroupMap,
+ unsigned NumRegularSections) {
// FIXME: Is this the correct place to do this?
// FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
if (NeedsGOT) {
ELFSymbolData MSD;
MSD.SymbolData = it;
- const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
+ const MCSymbol *BaseSymbol = getBaseSymbol(Layout, Symbol);
// Undefined symbols are global, but this is the first place we
// are able to set it.
bool Local = isLocal(*it, isSignature, Used);
if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
- MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
+ assert(BaseSymbol);
+ MCSymbolData &SD = Asm.getSymbolData(*BaseSymbol);
MCELF::SetBinding(*it, ELF::STB_GLOBAL);
MCELF::SetBinding(SD, ELF::STB_GLOBAL);
}
- if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
- MCELF::SetBinding(*it, ELF::STB_WEAK);
-
- if (it->isCommon()) {
+ if (!BaseSymbol) {
+ MSD.SectionIndex = ELF::SHN_ABS;
+ } else if (it->isCommon()) {
assert(!Local);
MSD.SectionIndex = ELF::SHN_COMMON;
- } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
- MSD.SectionIndex = ELF::SHN_ABS;
- } else if (RefSymbol.isUndefined()) {
+ } else if (BaseSymbol->isUndefined()) {
if (isSignature && !Used)
MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
else
MSD.SectionIndex = ELF::SHN_UNDEF;
+ if (!Used && WeakrefUsed)
+ MCELF::SetBinding(*it, ELF::STB_WEAK);
} else {
const MCSectionELF &Section =
- static_cast<const MCSectionELF&>(RefSymbol.getSection());
+ static_cast<const MCSectionELF&>(BaseSymbol->getSection());
MSD.SectionIndex = SectionIndexMap.lookup(&Section);
if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
NeedsSymtabShndx = true;
unsigned NumRegularSections = NumUserSections + NumIndexedSections;
// Compute symbol table information.
- ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
-
+ computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
+ NumRegularSections);
WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
const MCFragment &FB,
bool InSet,
bool IsPCRel) const {
- if (DataA.getFlags() & ELF_STB_Weak)
+ if (DataA.getFlags() & ELF_STB_Weak || MCELF::GetType(DataA) == ELF::STT_GNU_IFUNC)
return false;
return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
Asm, DataA, FB,InSet, IsPCRel);