#include "llvm/Target/TargetAsmBackend.h"
#include "../Target/X86/X86FixupKinds.h"
+#include "../Target/ARM/ARMFixupKinds.h"
#include <vector>
using namespace llvm;
return Visibility;
}
-static bool isFixupKindX86PCRel(unsigned Kind) {
- switch (Kind) {
- default:
- return false;
- case X86::reloc_pcrel_1byte:
- case X86::reloc_pcrel_4byte:
- case X86::reloc_riprel_4byte:
- case X86::reloc_riprel_4byte_movq_load:
- return true;
- }
-}
static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
switch (Variant) {
namespace {
class ELFObjectWriter : public MCObjectWriter {
+ protected:
/*static bool isFixupKindX86RIPRel(unsigned Kind) {
return Kind == X86::reloc_riprel_4byte ||
Kind == X86::reloc_riprel_4byte_movq_load;
const MCSymbol *Symbol;
uint64_t r_addend;
+ ELFRelocationEntry()
+ : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
+
+ ELFRelocationEntry(uint64_t RelocOffset, int Idx,
+ unsigned RelType, const MCSymbol *Sym,
+ uint64_t Addend)
+ : r_offset(RelocOffset), Index(Idx), Type(RelType),
+ Symbol(Sym), r_addend(Addend) {}
+
// Support lexicographic sorting.
bool operator<(const ELFRelocationEntry &RE) const {
return RE.r_offset < r_offset;
Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
OSType(_OSType), EMachine(_EMachine) {
}
-
+
+ virtual ~ELFObjectWriter();
+
void WriteWord(uint64_t W) {
if (Is64Bit)
Write64(W);
F.getContents() += StringRef(buf, 8);
}
- void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
+ virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
- void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
+ virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
uint64_t name, uint8_t info,
uint64_t value, uint64_t size,
uint8_t other, uint32_t shndx,
bool Reserved);
- void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
+ virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
ELFSymbolData &MSD,
const MCAsmLayout &Layout);
typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
- void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
+ virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
const MCAssembler &Asm,
const MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap);
- void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
+ virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup,
- MCValue Target, uint64_t &FixedValue);
+ MCValue Target, uint64_t &FixedValue) {
+ assert(0 && "RecordRelocation is not specific enough");
+ }
- uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
+ virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
const MCSymbol *S);
// Map from a group section to the signature symbol
/// \param StringTable [out] - The string table data.
/// \param StringIndexMap [out] - Map from symbol names to offsets in the
/// string table.
- void ComputeSymbolTable(MCAssembler &Asm,
+ virtual void ComputeSymbolTable(MCAssembler &Asm,
const SectionIndexMapTy &SectionIndexMap,
RevGroupMapTy RevGroupMap);
- void ComputeIndexMap(MCAssembler &Asm,
+ virtual void ComputeIndexMap(MCAssembler &Asm,
SectionIndexMapTy &SectionIndexMap);
- void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
+ virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
const MCSectionData &SD);
- void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
+ virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
for (MCAssembler::const_iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) {
WriteRelocation(Asm, Layout, *it);
}
}
- void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
+ virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap);
- void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
+ virtual void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
- void ExecutePostLayoutBinding(MCAssembler &Asm);
+ virtual void ExecutePostLayoutBinding(MCAssembler &Asm);
- void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
+ virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
uint64_t Address, uint64_t Offset,
uint64_t Size, uint32_t Link, uint32_t Info,
uint64_t Alignment, uint64_t EntrySize);
- void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
+ virtual void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
const MCSectionData *SD);
- bool IsFixupFullyResolved(const MCAssembler &Asm,
+ virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
const MCValue Target,
bool IsPCRel,
const MCFragment *DF) const;
- void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
- void WriteSection(MCAssembler &Asm,
+ virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
+ virtual void WriteSection(MCAssembler &Asm,
const SectionIndexMapTy &SectionIndexMap,
uint32_t GroupSymbolIndex,
uint64_t Offset, uint64_t Size, uint64_t Alignment,
const MCSectionELF &Section);
};
+ //===- X86ELFObjectWriter -------------------------------------------===//
+
+ class X86ELFObjectWriter : public ELFObjectWriter {
+ public:
+ X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
+ uint16_t _EMachine, bool _HasRelAddend,
+ Triple::OSType _OSType);
+
+ virtual ~X86ELFObjectWriter();
+ virtual void RecordRelocation(const MCAssembler &Asm,
+ const MCAsmLayout &Layout,
+ const MCFragment *Fragment,
+ const MCFixup &Fixup, MCValue Target,
+ uint64_t &FixedValue);
+
+ private:
+ static bool isFixupKindPCRel(unsigned Kind) {
+ switch (Kind) {
+ default:
+ return false;
+ case FK_PCRel_1:
+ case FK_PCRel_2:
+ case FK_PCRel_4:
+ case X86::reloc_riprel_4byte:
+ case X86::reloc_riprel_4byte_movq_load:
+ return true;
+ }
+ }
+ };
+
+
+ //===- ARMELFObjectWriter -------------------------------------------===//
+
+ class ARMELFObjectWriter : public ELFObjectWriter {
+ public:
+ ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
+ uint16_t _EMachine, bool _HasRelAddend,
+ Triple::OSType _OSType);
+
+ virtual ~ARMELFObjectWriter();
+ virtual void RecordRelocation(const MCAssembler &Asm,
+ const MCAsmLayout &Layout,
+ const MCFragment *Fragment,
+ const MCFixup &Fixup, MCValue Target,
+ uint64_t &FixedValue);
+
+ protected:
+ // Fixme: pull up to ELFObjectWriter
+ unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
+ bool IsPCRel);
+ private:
+ static bool isFixupKindPCRel(unsigned Kind) {
+ switch (Kind) {
+ default:
+ return false;
+ case FK_PCRel_1:
+ case FK_PCRel_2:
+ case FK_PCRel_4:
+ case ARM::fixup_arm_ldst_pcrel_12:
+ case ARM::fixup_arm_pcrel_10:
+ case ARM::fixup_arm_branch:
+ return true;
+ }
+ }
+ };
+
+ //===- MBlazeELFObjectWriter -------------------------------------------===//
+
+ class MBlazeELFObjectWriter : public ELFObjectWriter {
+ public:
+ MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
+ uint16_t _EMachine, bool _HasRelAddend,
+ Triple::OSType _OSType);
+
+ virtual ~MBlazeELFObjectWriter();
+ virtual void RecordRelocation(const MCAssembler &Asm,
+ const MCAsmLayout &Layout,
+ const MCFragment *Fragment,
+ const MCFixup &Fixup, MCValue Target,
+ uint64_t &FixedValue);
+
+ private:
+ static bool isFixupKindPCRel(unsigned Kind) {
+ switch (Kind) {
+ default:
+ return false;
+ case FK_PCRel_1:
+ case FK_PCRel_2:
+ case FK_PCRel_4:
+ return true;
+ }
+ }
+ };
}
+ELFObjectWriter::~ELFObjectWriter()
+{}
+
// Emit the ELF header.
void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
unsigned NumberOfSections) {
return 0;
}
-static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
- const MCSymbol *S = &Symbol;
- while (S->isVariable()) {
- const MCExpr *Value = S->getVariableValue();
- MCExpr::ExprKind Kind = Value->getKind();
- switch (Kind) {
- case MCExpr::SymbolRef: {
- const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
- S = &Ref->getSymbol();
- break;
- }
- case MCExpr::Target: {
- const MCTargetExpr *TExp = static_cast<const MCTargetExpr*>(Value);
- MCValue Res;
- TExp->EvaluateAsRelocatableImpl(Res, NULL);
- S = &Res.getSymA()->getSymbol();
- break;
- }
- default:
- return *S;
- }
- }
- return *S;
-}
-
void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
// The presence of symbol versions causes undefined symbols and
// versions declared with @@@ to be renamed.
for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
ie = Asm.symbol_end(); it != ie; ++it) {
const MCSymbol &Alias = it->getSymbol();
- const MCSymbol &Symbol = AliasedSymbol(Alias);
+ const MCSymbol &Symbol = Alias.AliasedSymbol();
MCSymbolData &SD = Asm.getSymbolData(Symbol);
// Not an alias.
const MCAsmLayout &Layout) {
MCSymbolData &OrigData = *MSD.SymbolData;
MCSymbolData &Data =
- Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
+ Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
Data.getSymbol().isVariable();
const MCValue &Target,
const MCFragment &F) const {
const MCSymbol &Symbol = Target.getSymA()->getSymbol();
- const MCSymbol &ASymbol = AliasedSymbol(Symbol);
- const MCSymbol *RenamedP = Renames.lookup(&Symbol);
-
- if (!RenamedP) {
- if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_None ||
- Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
- RenamedP = &ASymbol;
- else
- RenamedP = &Symbol;
+ const MCSymbol &ASymbol = Symbol.AliasedSymbol();
+ const MCSymbol *Renamed = Renames.lookup(&Symbol);
+ const MCSymbolData &SD = Asm.getSymbolData(Symbol);
+
+ if (ASymbol.isUndefined()) {
+ if (Renamed)
+ return Renamed;
+ return &ASymbol;
}
- const MCSymbol &Renamed = *RenamedP;
-
- MCSymbolData &SD = Asm.getSymbolData(Symbol);
- if (Symbol.isUndefined())
- return &Renamed;
-
- if (SD.isExternal())
- return &Renamed;
+ if (SD.isExternal()) {
+ if (Renamed)
+ return Renamed;
+ return &Symbol;
+ }
const MCSectionELF &Section =
- static_cast<const MCSectionELF&>(Symbol.getSection());
+ static_cast<const MCSectionELF&>(ASymbol.getSection());
+ const SectionKind secKind = Section.getKind();
- if (Section.getKind().isBSS())
+ if (secKind.isBSS())
return NULL;
+ if (secKind.isThreadLocal()) {
+ if (Renamed)
+ return Renamed;
+ return &Symbol;
+ }
+
MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
const MCSectionELF &Sec2 =
static_cast<const MCSectionELF&>(F.getParent()->getSection());
if (&Sec2 != &Section &&
(Kind == MCSymbolRefExpr::VK_PLT ||
Kind == MCSymbolRefExpr::VK_GOTPCREL ||
- Kind == MCSymbolRefExpr::VK_GOTOFF))
- return &Renamed;
+ Kind == MCSymbolRefExpr::VK_GOTOFF)) {
+ if (Renamed)
+ return Renamed;
+ return &Symbol;
+ }
if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
- if (Target.getConstant() != 0)
- return &Renamed;
- return NULL;
+ if (Target.getConstant() == 0)
+ return NULL;
+ if (Renamed)
+ return Renamed;
+ return &Symbol;
}
return NULL;
}
-// FIXME: this is currently X86/X86_64 only
-void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
- const MCAsmLayout &Layout,
- const MCFragment *Fragment,
- const MCFixup &Fixup,
- MCValue Target,
- uint64_t &FixedValue) {
- int64_t Addend = 0;
- int Index = 0;
- int64_t Value = Target.getConstant();
- const MCSymbol &Symbol = Target.getSymA()->getSymbol();
- const MCSymbol *RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
-
- bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
- if (!Target.isAbsolute()) {
- if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
- const MCSymbol &SymbolB = RefB->getSymbol();
- MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
- IsPCRel = true;
- MCSectionData *Sec = Fragment->getParent();
-
- // Offset of the symbol in the section
- int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
-
- // Ofeset of the relocation in the section
- int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
- Value += b - a;
- }
-
- if (!RelocSymbol) {
- MCSymbolData &SD = Asm.getSymbolData(Symbol);
- MCFragment *F = SD.getFragment();
-
- Index = F->getParent()->getOrdinal();
-
- MCSectionData *FSD = F->getParent();
- // Offset of the symbol in the section
- Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
- } else {
- if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
- WeakrefUsedInReloc.insert(RelocSymbol);
- else
- UsedInReloc.insert(RelocSymbol);
- Index = -1;
- }
- Addend = Value;
- // Compensate for the addend on i386.
- if (Is64Bit)
- Value = 0;
- }
-
- FixedValue = Value;
-
- // determine the type of the relocation
-
- MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
- unsigned Type;
- if (Is64Bit) {
- if (IsPCRel) {
- switch (Modifier) {
- default:
- llvm_unreachable("Unimplemented");
- case MCSymbolRefExpr::VK_None:
- Type = ELF::R_X86_64_PC32;
- break;
- case MCSymbolRefExpr::VK_PLT:
- Type = ELF::R_X86_64_PLT32;
- break;
- case MCSymbolRefExpr::VK_GOTPCREL:
- Type = ELF::R_X86_64_GOTPCREL;
- break;
- case MCSymbolRefExpr::VK_GOTTPOFF:
- Type = ELF::R_X86_64_GOTTPOFF;
- break;
- case MCSymbolRefExpr::VK_TLSGD:
- Type = ELF::R_X86_64_TLSGD;
- break;
- case MCSymbolRefExpr::VK_TLSLD:
- Type = ELF::R_X86_64_TLSLD;
- break;
- }
- } else {
- switch ((unsigned)Fixup.getKind()) {
- default: llvm_unreachable("invalid fixup kind!");
- case FK_Data_8: Type = ELF::R_X86_64_64; break;
- case X86::reloc_signed_4byte:
- case X86::reloc_pcrel_4byte:
- assert(isInt<32>(Target.getConstant()));
- switch (Modifier) {
- default:
- llvm_unreachable("Unimplemented");
- case MCSymbolRefExpr::VK_None:
- Type = ELF::R_X86_64_32S;
- break;
- case MCSymbolRefExpr::VK_GOT:
- Type = ELF::R_X86_64_GOT32;
- break;
- case MCSymbolRefExpr::VK_GOTPCREL:
- Type = ELF::R_X86_64_GOTPCREL;
- break;
- case MCSymbolRefExpr::VK_TPOFF:
- Type = ELF::R_X86_64_TPOFF32;
- break;
- case MCSymbolRefExpr::VK_DTPOFF:
- Type = ELF::R_X86_64_DTPOFF32;
- break;
- }
- break;
- case FK_Data_4:
- Type = ELF::R_X86_64_32;
- break;
- case FK_Data_2: Type = ELF::R_X86_64_16; break;
- case X86::reloc_pcrel_1byte:
- case FK_Data_1: Type = ELF::R_X86_64_8; break;
- }
- }
- } else {
- if (IsPCRel) {
- switch (Modifier) {
- default:
- llvm_unreachable("Unimplemented");
- case MCSymbolRefExpr::VK_None:
- Type = ELF::R_386_PC32;
- break;
- case MCSymbolRefExpr::VK_PLT:
- Type = ELF::R_386_PLT32;
- break;
- }
- } else {
- switch ((unsigned)Fixup.getKind()) {
- default: llvm_unreachable("invalid fixup kind!");
-
- case X86::reloc_global_offset_table:
- Type = ELF::R_386_GOTPC;
- break;
-
- // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
- // instead?
- case X86::reloc_signed_4byte:
- case X86::reloc_pcrel_4byte:
- case FK_Data_4:
- switch (Modifier) {
- default:
- llvm_unreachable("Unimplemented");
- case MCSymbolRefExpr::VK_None:
- Type = ELF::R_386_32;
- break;
- case MCSymbolRefExpr::VK_GOT:
- Type = ELF::R_386_GOT32;
- break;
- case MCSymbolRefExpr::VK_GOTOFF:
- Type = ELF::R_386_GOTOFF;
- break;
- case MCSymbolRefExpr::VK_TLSGD:
- Type = ELF::R_386_TLS_GD;
- break;
- case MCSymbolRefExpr::VK_TPOFF:
- Type = ELF::R_386_TLS_LE_32;
- break;
- case MCSymbolRefExpr::VK_INDNTPOFF:
- Type = ELF::R_386_TLS_IE;
- break;
- case MCSymbolRefExpr::VK_NTPOFF:
- Type = ELF::R_386_TLS_LE;
- break;
- case MCSymbolRefExpr::VK_GOTNTPOFF:
- Type = ELF::R_386_TLS_GOTIE;
- break;
- case MCSymbolRefExpr::VK_TLSLDM:
- Type = ELF::R_386_TLS_LDM;
- break;
- case MCSymbolRefExpr::VK_DTPOFF:
- Type = ELF::R_386_TLS_LDO_32;
- break;
- }
- break;
- case FK_Data_2: Type = ELF::R_386_16; break;
- case X86::reloc_pcrel_1byte:
- case FK_Data_1: Type = ELF::R_386_8; break;
- }
- }
- }
-
- if (RelocNeedsGOT(Modifier))
- NeedsGOT = true;
-
- ELFRelocationEntry ERE;
-
- ERE.Index = Index;
- ERE.Type = Type;
- ERE.Symbol = RelocSymbol;
-
- ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
-
- if (HasRelocationAddend)
- ERE.r_addend = Addend;
- else
- ERE.r_addend = 0; // Silence compiler warning.
-
- Relocations[Fragment->getParent()].push_back(ERE);
-}
uint64_t
ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
return true;
- const MCSymbol &A = AliasedSymbol(Symbol);
+ const MCSymbol &A = Symbol.AliasedSymbol();
if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
return false;
return false;
const MCSymbol &Symbol = Data.getSymbol();
- const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
+ const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
if (isSignature && !isUsedInReloc)
ELFSymbolData MSD;
MSD.SymbolData = it;
- const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
+ const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
// Undefined symbols are global, but this is the first place we
// are able to set it.
MCDataFragment *F = new MCDataFragment(&RelaSD);
WriteRelocationsFragment(Asm, F, &SD);
-
- Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
}
}
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
ELFRelocationEntry entry = Relocs[e - i - 1];
- if (entry.Index < 0)
+ if (!entry.Index)
+ ;
+ else if (entry.Index < 0)
entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
else
- entry.Index += LocalSymbolData.size() + 1;
+ entry.Index += LocalSymbolData.size();
if (Is64Bit) {
String64(*F, entry.r_offset);
MCDataFragment *ShndxF = NULL;
if (NeedsSymtabShndx) {
ShndxF = new MCDataFragment(SymtabShndxSD);
- Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
}
WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
- Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
F = new MCDataFragment(&StrtabSD);
F->getContents().append(StringTable.begin(), StringTable.end());
- Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
F = new MCDataFragment(&ShstrtabSD);
F->getContents() += Name;
F->getContents() += '\x00';
}
-
- Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
}
bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
const MCSymbol *SymbolA = 0;
if (const MCSymbolRefExpr *A = Target.getSymA()) {
SymbolA = &A->getSymbol();
- SectionA = &SymbolA->getSection();
+ SectionA = &SymbolA->AliasedSymbol().getSection();
}
const MCSection *SectionB = 0;
+ const MCSymbol *SymbolB = 0;
if (const MCSymbolRefExpr *B = Target.getSymB()) {
- SectionB = &B->getSymbol().getSection();
+ SymbolB = &B->getSymbol();
+ SectionB = &SymbolB->AliasedSymbol().getSection();
}
if (!BaseSection)
return SectionA == SectionB;
+ if (SymbolB)
+ return false;
+
+ // Absolute address but PCrel instruction, so we need a relocation.
+ if (!SymbolA)
+ return false;
+
+ // FIXME: This is in here just to match gnu as output. If the two ends
+ // are in the same section, there is nothing that the linker can do to
+ // break it.
const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
if (DataA.isExternal())
return false;
- return !SectionB && BaseSection == SectionA;
+ return BaseSection == SectionA;
}
void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
MCDataFragment *F = new MCDataFragment(&Data);
String32(*F, NumGroups + Index);
}
-
- for (RevGroupMapTy::const_iterator i = RevGroupMap.begin(),
- e = RevGroupMap.end(); i != e; ++i) {
- const MCSectionELF *Group = i->second;
- MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
- Asm.AddSectionToTheEnd(*this, Data, Layout);
- }
}
void ELFObjectWriter::WriteSection(MCAssembler &Asm,
Alignment, Section.getEntrySize());
}
-void ELFObjectWriter::WriteObject(MCAssembler &Asm,
- const MCAsmLayout &Layout) {
- GroupMapTy GroupMap;
- RevGroupMapTy RevGroupMap;
- CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
- RevGroupMap);
+static bool IsELFMetaDataSection(const MCSectionData &SD) {
+ return SD.getAddress() == ~UINT64_C(0) &&
+ !SD.getSection().isVirtualSection();
+}
- SectionIndexMapTy SectionIndexMap;
+static uint64_t DataSectionSize(const MCSectionData &SD) {
+ uint64_t Ret = 0;
+ for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
+ ++i) {
+ const MCFragment &F = *i;
+ assert(F.getKind() == MCFragment::FT_Data);
+ Ret += cast<MCDataFragment>(F).getContents().size();
+ }
+ return Ret;
+}
- ComputeIndexMap(Asm, SectionIndexMap);
+static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
+ const MCSectionData &SD) {
+ if (IsELFMetaDataSection(SD))
+ return DataSectionSize(SD);
+ return Layout.getSectionFileSize(&SD);
+}
+
+static uint64_t GetSectionSize(const MCAsmLayout &Layout,
+ const MCSectionData &SD) {
+ if (IsELFMetaDataSection(SD))
+ return DataSectionSize(SD);
+ return Layout.getSectionSize(&SD);
+}
+
+static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
+ for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
+ ++i) {
+ const MCFragment &F = *i;
+ assert(F.getKind() == MCFragment::FT_Data);
+ W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
+ }
+}
+
+void ELFObjectWriter::WriteObject(MCAssembler &Asm,
+ const MCAsmLayout &Layout) {
+ GroupMapTy GroupMap;
+ RevGroupMapTy RevGroupMap;
+ CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
+ RevGroupMap);
+
+ SectionIndexMapTy SectionIndexMap;
+
+ ComputeIndexMap(Asm, SectionIndexMap);
// Compute symbol table information.
ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
// Get the size of the section in the output file (including padding).
- uint64_t Size = Layout.getSectionFileSize(&SD);
-
- FileOff += Size;
+ FileOff += GetSectionFileSize(Layout, SD);
}
FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
// Remember the offset into the file for this section.
SectionOffsetMap[&Section] = FileOff;
- FileOff += Layout.getSectionFileSize(&SD);
+ FileOff += GetSectionFileSize(Layout, SD);
- Asm.WriteSectionData(&SD, Layout, this);
+ if (IsELFMetaDataSection(SD))
+ WriteDataSectionData(this, SD);
+ else
+ Asm.WriteSectionData(&SD, Layout, this);
}
uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
else
GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
+ uint64_t Size = GetSectionSize(Layout, SD);
+
WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
- SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
+ SectionOffsetMap[&Section], Size,
SD.getAlignment(), Section);
}
}
uint16_t EMachine,
bool IsLittleEndian,
bool HasRelocationAddend) {
- return new ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
- HasRelocationAddend, OSType);
+ switch (EMachine) {
+ case ELF::EM_386:
+ case ELF::EM_X86_64:
+ return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
+ HasRelocationAddend, OSType); break;
+ case ELF::EM_ARM:
+ return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
+ HasRelocationAddend, OSType); break;
+ case ELF::EM_MBLAZE:
+ return new MBlazeELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
+ HasRelocationAddend, OSType); break;
+ default: llvm_unreachable("Unsupported architecture"); break;
+ }
+}
+
+
+/// START OF SUBCLASSES for ELFObjectWriter
+//===- ARMELFObjectWriter -------------------------------------------===//
+
+ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
+ bool _IsLittleEndian,
+ uint16_t _EMachine, bool _HasRelocationAddend,
+ Triple::OSType _OSType)
+ : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
+ _HasRelocationAddend, _OSType)
+{}
+
+ARMELFObjectWriter::~ARMELFObjectWriter()
+{}
+
+unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
+ const MCFixup &Fixup,
+ bool IsPCRel) {
+ MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
+ MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
+
+ if (IsPCRel) {
+ switch (Modifier) {
+ default: assert(0 && "Unimplemented Modifier");
+ case MCSymbolRefExpr::VK_None: break;
+ }
+ switch ((unsigned)Fixup.getKind()) {
+ default: assert(0 && "Unimplemented");
+ case ARM::fixup_arm_branch: return ELF::R_ARM_CALL; break;
+ }
+ } else {
+ switch ((unsigned)Fixup.getKind()) {
+ default: llvm_unreachable("invalid fixup kind!");
+ case ARM::fixup_arm_ldst_pcrel_12:
+ case ARM::fixup_arm_pcrel_10:
+ case ARM::fixup_arm_adr_pcrel_12:
+ assert(0 && "Unimplemented"); break;
+ case ARM::fixup_arm_branch:
+ return ELF::R_ARM_CALL; break;
+ case ARM::fixup_arm_movt_hi16:
+ return ELF::R_ARM_MOVT_ABS; break;
+ case ARM::fixup_arm_movw_lo16:
+ return ELF::R_ARM_MOVW_ABS_NC; break;
+ }
+ }
+
+ if (RelocNeedsGOT(Modifier))
+ NeedsGOT = true;
+ return -1;
+}
+
+void ARMELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
+ const MCAsmLayout &Layout,
+ const MCFragment *Fragment,
+ const MCFixup &Fixup,
+ MCValue Target,
+ uint64_t &FixedValue) {
+ int64_t Addend = 0;
+ int Index = 0;
+ int64_t Value = Target.getConstant();
+ const MCSymbol *RelocSymbol = NULL;
+
+ bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
+ if (!Target.isAbsolute()) {
+ const MCSymbol &Symbol = Target.getSymA()->getSymbol();
+ const MCSymbol &ASymbol = Symbol.AliasedSymbol();
+ RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
+
+ if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
+ const MCSymbol &SymbolB = RefB->getSymbol();
+ MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
+ IsPCRel = true;
+ MCSectionData *Sec = Fragment->getParent();
+
+ // Offset of the symbol in the section
+ int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
+
+ // Ofeset of the relocation in the section
+ int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
+ Value += b - a;
+ }
+
+ if (!RelocSymbol) {
+ MCSymbolData &SD = Asm.getSymbolData(ASymbol);
+ MCFragment *F = SD.getFragment();
+
+ Index = F->getParent()->getOrdinal() + 1;
+
+ MCSectionData *FSD = F->getParent();
+ // Offset of the symbol in the section
+ Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
+ } else {
+ if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
+ WeakrefUsedInReloc.insert(RelocSymbol);
+ else
+ UsedInReloc.insert(RelocSymbol);
+ Index = -1;
+ }
+ Addend = Value;
+ // Compensate for the addend on i386.
+ if (Is64Bit)
+ Value = 0;
+ }
+
+ FixedValue = Value;
+
+ // determine the type of the relocation
+ unsigned Type = GetRelocType(Target, Fixup, IsPCRel);
+
+ uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
+ Fixup.getOffset();
+
+ if (!HasRelocationAddend) Addend = 0;
+ ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
+ Relocations[Fragment->getParent()].push_back(ERE);
+}
+
+//===- MBlazeELFObjectWriter -------------------------------------------===//
+
+MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
+ bool _IsLittleEndian,
+ uint16_t _EMachine,
+ bool _HasRelocationAddend,
+ Triple::OSType _OSType)
+ : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
+ _HasRelocationAddend, _OSType) {
+}
+
+MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
+}
+
+void MBlazeELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
+ const MCAsmLayout &Layout,
+ const MCFragment *Fragment,
+ const MCFixup &Fixup,
+ MCValue Target,
+ uint64_t &FixedValue) {
+ int64_t Addend = 0;
+ int Index = 0;
+ int64_t Value = Target.getConstant();
+ const MCSymbol &Symbol = Target.getSymA()->getSymbol();
+ const MCSymbol &ASymbol = Symbol.AliasedSymbol();
+ const MCSymbol *RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
+
+ bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
+ if (!Target.isAbsolute()) {
+ if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
+ const MCSymbol &SymbolB = RefB->getSymbol();
+ MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
+ IsPCRel = true;
+ MCSectionData *Sec = Fragment->getParent();
+
+ // Offset of the symbol in the section
+ int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
+
+ // Ofeset of the relocation in the section
+ int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
+ Value += b - a;
+ }
+
+ if (!RelocSymbol) {
+ MCSymbolData &SD = Asm.getSymbolData(ASymbol);
+ MCFragment *F = SD.getFragment();
+
+ Index = F->getParent()->getOrdinal();
+
+ MCSectionData *FSD = F->getParent();
+ // Offset of the symbol in the section
+ Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
+ } else {
+ if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
+ WeakrefUsedInReloc.insert(RelocSymbol);
+ else
+ UsedInReloc.insert(RelocSymbol);
+ Index = -1;
+ }
+ Addend = Value;
+ }
+
+ FixedValue = Value;
+
+ // determine the type of the relocation
+ unsigned Type;
+ if (IsPCRel) {
+ switch ((unsigned)Fixup.getKind()) {
+ default:
+ llvm_unreachable("Unimplemented");
+ case FK_PCRel_4:
+ Type = ELF::R_MICROBLAZE_64_PCREL;
+ break;
+ case FK_PCRel_2:
+ Type = ELF::R_MICROBLAZE_32_PCREL;
+ break;
+ }
+ } else {
+ switch ((unsigned)Fixup.getKind()) {
+ default: llvm_unreachable("invalid fixup kind!");
+ case FK_Data_4:
+ Type = (RelocSymbol || Addend !=0) ? ELF::R_MICROBLAZE_32
+ : ELF::R_MICROBLAZE_64;
+ break;
+ case FK_Data_2:
+ Type = ELF::R_MICROBLAZE_32;
+ break;
+ }
+ }
+
+ MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
+ if (RelocNeedsGOT(Modifier))
+ NeedsGOT = true;
+
+ uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
+ Fixup.getOffset();
+
+ if (!HasRelocationAddend) Addend = 0;
+ ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
+ Relocations[Fragment->getParent()].push_back(ERE);
+}
+
+//===- X86ELFObjectWriter -------------------------------------------===//
+
+
+X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
+ bool _IsLittleEndian,
+ uint16_t _EMachine, bool _HasRelocationAddend,
+ Triple::OSType _OSType)
+ : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
+ _HasRelocationAddend, _OSType)
+{}
+
+X86ELFObjectWriter::~X86ELFObjectWriter()
+{}
+
+void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
+ const MCAsmLayout &Layout,
+ const MCFragment *Fragment,
+ const MCFixup &Fixup,
+ MCValue Target,
+ uint64_t &FixedValue) {
+ int64_t Addend = 0;
+ int Index = 0;
+ int64_t Value = Target.getConstant();
+ const MCSymbol *RelocSymbol = NULL;
+
+ bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
+ if (!Target.isAbsolute()) {
+ const MCSymbol &Symbol = Target.getSymA()->getSymbol();
+ const MCSymbol &ASymbol = Symbol.AliasedSymbol();
+ RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
+
+ if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
+ const MCSymbol &SymbolB = RefB->getSymbol();
+ MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
+ IsPCRel = true;
+ MCSectionData *Sec = Fragment->getParent();
+
+ // Offset of the symbol in the section
+ int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
+
+ // Ofeset of the relocation in the section
+ int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
+ Value += b - a;
+ }
+
+ if (!RelocSymbol) {
+ MCSymbolData &SD = Asm.getSymbolData(ASymbol);
+ MCFragment *F = SD.getFragment();
+
+ Index = F->getParent()->getOrdinal() + 1;
+
+ MCSectionData *FSD = F->getParent();
+ // Offset of the symbol in the section
+ Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
+ } else {
+ if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
+ WeakrefUsedInReloc.insert(RelocSymbol);
+ else
+ UsedInReloc.insert(RelocSymbol);
+ Index = -1;
+ }
+ Addend = Value;
+ // Compensate for the addend on i386.
+ if (Is64Bit)
+ Value = 0;
+ }
+
+ FixedValue = Value;
+
+ // determine the type of the relocation
+
+ MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
+ MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
+ unsigned Type;
+ if (Is64Bit) {
+ if (IsPCRel) {
+ switch (Modifier) {
+ default:
+ llvm_unreachable("Unimplemented");
+ case MCSymbolRefExpr::VK_None:
+ Type = ELF::R_X86_64_PC32;
+ break;
+ case MCSymbolRefExpr::VK_PLT:
+ Type = ELF::R_X86_64_PLT32;
+ break;
+ case MCSymbolRefExpr::VK_GOTPCREL:
+ Type = ELF::R_X86_64_GOTPCREL;
+ break;
+ case MCSymbolRefExpr::VK_GOTTPOFF:
+ Type = ELF::R_X86_64_GOTTPOFF;
+ break;
+ case MCSymbolRefExpr::VK_TLSGD:
+ Type = ELF::R_X86_64_TLSGD;
+ break;
+ case MCSymbolRefExpr::VK_TLSLD:
+ Type = ELF::R_X86_64_TLSLD;
+ break;
+ }
+ } else {
+ switch ((unsigned)Fixup.getKind()) {
+ default: llvm_unreachable("invalid fixup kind!");
+ case FK_Data_8: Type = ELF::R_X86_64_64; break;
+ case X86::reloc_signed_4byte:
+ case FK_PCRel_4:
+ assert(isInt<32>(Target.getConstant()));
+ switch (Modifier) {
+ default:
+ llvm_unreachable("Unimplemented");
+ case MCSymbolRefExpr::VK_None:
+ Type = ELF::R_X86_64_32S;
+ break;
+ case MCSymbolRefExpr::VK_GOT:
+ Type = ELF::R_X86_64_GOT32;
+ break;
+ case MCSymbolRefExpr::VK_GOTPCREL:
+ Type = ELF::R_X86_64_GOTPCREL;
+ break;
+ case MCSymbolRefExpr::VK_TPOFF:
+ Type = ELF::R_X86_64_TPOFF32;
+ break;
+ case MCSymbolRefExpr::VK_DTPOFF:
+ Type = ELF::R_X86_64_DTPOFF32;
+ break;
+ }
+ break;
+ case FK_Data_4:
+ Type = ELF::R_X86_64_32;
+ break;
+ case FK_Data_2: Type = ELF::R_X86_64_16; break;
+ case FK_PCRel_1:
+ case FK_Data_1: Type = ELF::R_X86_64_8; break;
+ }
+ }
+ } else {
+ if (IsPCRel) {
+ switch (Modifier) {
+ default:
+ llvm_unreachable("Unimplemented");
+ case MCSymbolRefExpr::VK_None:
+ Type = ELF::R_386_PC32;
+ break;
+ case MCSymbolRefExpr::VK_PLT:
+ Type = ELF::R_386_PLT32;
+ break;
+ }
+ } else {
+ switch ((unsigned)Fixup.getKind()) {
+ default: llvm_unreachable("invalid fixup kind!");
+
+ case X86::reloc_global_offset_table:
+ Type = ELF::R_386_GOTPC;
+ break;
+
+ // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
+ // instead?
+ case X86::reloc_signed_4byte:
+ case FK_PCRel_4:
+ case FK_Data_4:
+ switch (Modifier) {
+ default:
+ llvm_unreachable("Unimplemented");
+ case MCSymbolRefExpr::VK_None:
+ Type = ELF::R_386_32;
+ break;
+ case MCSymbolRefExpr::VK_GOT:
+ Type = ELF::R_386_GOT32;
+ break;
+ case MCSymbolRefExpr::VK_GOTOFF:
+ Type = ELF::R_386_GOTOFF;
+ break;
+ case MCSymbolRefExpr::VK_TLSGD:
+ Type = ELF::R_386_TLS_GD;
+ break;
+ case MCSymbolRefExpr::VK_TPOFF:
+ Type = ELF::R_386_TLS_LE_32;
+ break;
+ case MCSymbolRefExpr::VK_INDNTPOFF:
+ Type = ELF::R_386_TLS_IE;
+ break;
+ case MCSymbolRefExpr::VK_NTPOFF:
+ Type = ELF::R_386_TLS_LE;
+ break;
+ case MCSymbolRefExpr::VK_GOTNTPOFF:
+ Type = ELF::R_386_TLS_GOTIE;
+ break;
+ case MCSymbolRefExpr::VK_TLSLDM:
+ Type = ELF::R_386_TLS_LDM;
+ break;
+ case MCSymbolRefExpr::VK_DTPOFF:
+ Type = ELF::R_386_TLS_LDO_32;
+ break;
+ }
+ break;
+ case FK_Data_2: Type = ELF::R_386_16; break;
+ case FK_PCRel_1:
+ case FK_Data_1: Type = ELF::R_386_8; break;
+ }
+ }
+ }
+
+ if (RelocNeedsGOT(Modifier))
+ NeedsGOT = true;
+
+
+ uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
+ Fixup.getOffset();
+
+ if (!HasRelocationAddend) Addend = 0;
+ ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
+ Relocations[Fragment->getParent()].push_back(ERE);
}