Refactor ELFObjectWriter.

+ ARM/X86/MBlaze now share a common RecordRelocation
+ ARM/X86/MBlaze arch specific routines are limited to GetRelocType()

llvm-svn: 121043
This commit is contained in:
Jason W Kim 2010-12-06 21:57:34 +00:00
parent 7ff0ba41bd
commit 495c2bb9a6
1 changed files with 104 additions and 214 deletions

View File

@ -286,10 +286,8 @@ namespace {
const SectionIndexMapTy &SectionIndexMap); const SectionIndexMapTy &SectionIndexMap);
virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup, const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, uint64_t &FixedValue) { MCValue Target, uint64_t &FixedValue);
assert(0 && "RecordRelocation is not specific enough");
}
virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm, virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
const MCSymbol *S); const MCSymbol *S);
@ -348,6 +346,13 @@ namespace {
uint32_t GroupSymbolIndex, uint32_t GroupSymbolIndex,
uint64_t Offset, uint64_t Size, uint64_t Alignment, uint64_t Offset, uint64_t Size, uint64_t Alignment,
const MCSectionELF &Section); const MCSectionELF &Section);
protected:
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, bool IsRelocWithSymbol,
int64_t Addend) = 0;
virtual bool isFixupKindPCRel(unsigned Kind) const = 0;
}; };
//===- X86ELFObjectWriter -------------------------------------------===// //===- X86ELFObjectWriter -------------------------------------------===//
@ -359,14 +364,12 @@ namespace {
Triple::OSType _OSType); Triple::OSType _OSType);
virtual ~X86ELFObjectWriter(); virtual ~X86ELFObjectWriter();
virtual void RecordRelocation(const MCAssembler &Asm, protected:
const MCAsmLayout &Layout, virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
const MCFragment *Fragment, bool IsPCRel, bool IsRelocWithSymbol,
const MCFixup &Fixup, MCValue Target, int64_t Addend);
uint64_t &FixedValue);
private: virtual bool isFixupKindPCRel(unsigned Kind) const {
static bool isFixupKindPCRel(unsigned Kind) {
switch (Kind) { switch (Kind) {
default: default:
return false; return false;
@ -390,18 +393,11 @@ namespace {
Triple::OSType _OSType); Triple::OSType _OSType);
virtual ~ARMELFObjectWriter(); virtual ~ARMELFObjectWriter();
virtual void RecordRelocation(const MCAssembler &Asm,
const MCAsmLayout &Layout,
const MCFragment *Fragment,
const MCFixup &Fixup, MCValue Target,
uint64_t &FixedValue);
protected: protected:
// Fixme: pull up to ELFObjectWriter virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, bool IsPCRel, bool IsRelocWithSymbol,
bool IsPCRel); int64_t Addend);
private: virtual bool isFixupKindPCRel(unsigned Kind) const {
static bool isFixupKindPCRel(unsigned Kind) {
switch (Kind) { switch (Kind) {
default: default:
return false; return false;
@ -425,14 +421,12 @@ namespace {
Triple::OSType _OSType); Triple::OSType _OSType);
virtual ~MBlazeELFObjectWriter(); virtual ~MBlazeELFObjectWriter();
virtual void RecordRelocation(const MCAssembler &Asm, protected:
const MCAsmLayout &Layout, virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
const MCFragment *Fragment, bool IsPCRel, bool IsRelocWithSymbol,
const MCFixup &Fixup, MCValue Target, int64_t Addend);
uint64_t &FixedValue);
private: virtual bool isFixupKindPCRel(unsigned Kind) const {
static bool isFixupKindPCRel(unsigned Kind) {
switch (Kind) { switch (Kind) {
default: default:
return false; return false;
@ -761,6 +755,70 @@ const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
} }
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 *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;
// Offset of the symbol in the section
int64_t a = Layout.getSymbolOffset(&SDB);
// 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;
// Offset of the symbol in the section
Value += Layout.getSymbolOffset(&SD);
} 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;
unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
(RelocSymbol != 0), Addend);
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);
}
uint64_t uint64_t
ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm, ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
const MCSymbol *S) { const MCSymbol *S) {
@ -1467,7 +1525,9 @@ ARMELFObjectWriter::~ARMELFObjectWriter()
unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target, unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
const MCFixup &Fixup, const MCFixup &Fixup,
bool IsPCRel) { bool IsPCRel,
bool IsRelocWithSymbol,
int64_t Addend) {
MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
@ -1501,70 +1561,6 @@ unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
return -1; 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;
// Offset of the symbol in the section
int64_t a = Layout.getSymbolOffset(&SDB);
// 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;
// Offset of the symbol in the section
Value += Layout.getSymbolOffset(&SD);
} 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::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
@ -1579,54 +1575,11 @@ MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
MBlazeELFObjectWriter::~MBlazeELFObjectWriter() { MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
} }
void MBlazeELFObjectWriter::RecordRelocation(const MCAssembler &Asm, unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
const MCAsmLayout &Layout,
const MCFragment *Fragment,
const MCFixup &Fixup, const MCFixup &Fixup,
MCValue Target, bool IsPCRel,
uint64_t &FixedValue) { bool IsRelocWithSymbol,
int64_t Addend = 0; int64_t Addend) {
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;
// Offset of the symbol in the section
int64_t a = Layout.getSymbolOffset(&SDB);
// 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();
// Offset of the symbol in the section
Value += Layout.getSymbolOffset(&SD);
} 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 // determine the type of the relocation
unsigned Type; unsigned Type;
if (IsPCRel) { if (IsPCRel) {
@ -1644,25 +1597,16 @@ void MBlazeELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
switch ((unsigned)Fixup.getKind()) { switch ((unsigned)Fixup.getKind()) {
default: llvm_unreachable("invalid fixup kind!"); default: llvm_unreachable("invalid fixup kind!");
case FK_Data_4: case FK_Data_4:
Type = (RelocSymbol || Addend !=0) ? ELF::R_MICROBLAZE_32 Type = ((IsRelocWithSymbol || Addend !=0)
: ELF::R_MICROBLAZE_64; ? ELF::R_MICROBLAZE_32
: ELF::R_MICROBLAZE_64);
break; break;
case FK_Data_2: case FK_Data_2:
Type = ELF::R_MICROBLAZE_32; Type = ELF::R_MICROBLAZE_32;
break; break;
} }
} }
return Type;
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 -------------------------------------------===//
@ -1679,59 +1623,11 @@ X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
X86ELFObjectWriter::~X86ELFObjectWriter() X86ELFObjectWriter::~X86ELFObjectWriter()
{} {}
void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
const MCAsmLayout &Layout, const MCFixup &Fixup,
const MCFragment *Fragment, bool IsPCRel,
const MCFixup &Fixup, bool IsRelocWithSymbol,
MCValue Target, int64_t Addend) {
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;
// Offset of the symbol in the section
int64_t a = Layout.getSymbolOffset(&SDB);
// 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;
// Offset of the symbol in the section
Value += Layout.getSymbolOffset(&SD);
} 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 // determine the type of the relocation
MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
@ -1866,11 +1762,5 @@ void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
if (RelocNeedsGOT(Modifier)) if (RelocNeedsGOT(Modifier))
NeedsGOT = true; NeedsGOT = true;
return Type;
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);
} }