From 56a399023aba6cf1348533df04732950c43eaca7 Mon Sep 17 00:00:00 2001 From: Jason W Kim Date: Mon, 6 Dec 2010 21:57:34 +0000 Subject: [PATCH] Refactor ELFObjectWriter. + ARM/X86/MBlaze now share a common RecordRelocation + ARM/X86/MBlaze arch specific routines are limited to GetRelocType() git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121043 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/ELFObjectWriter.cpp | 322 ++++++++++++------------------------- 1 file changed, 106 insertions(+), 216 deletions(-) diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index bd6c5f85499..549288773bb 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -286,10 +286,8 @@ namespace { const SectionIndexMapTy &SectionIndexMap); virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, - const MCFragment *Fragment, const MCFixup &Fixup, - MCValue Target, uint64_t &FixedValue) { - assert(0 && "RecordRelocation is not specific enough"); - } + const MCFragment *Fragment, const MCFixup &Fixup, + MCValue Target, uint64_t &FixedValue); virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm, const MCSymbol *S); @@ -348,6 +346,13 @@ namespace { uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size, uint64_t Alignment, 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 -------------------------------------------===// @@ -359,14 +364,12 @@ namespace { 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) { + protected: + virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, + bool IsPCRel, bool IsRelocWithSymbol, + int64_t Addend); + + virtual bool isFixupKindPCRel(unsigned Kind) const { switch (Kind) { default: return false; @@ -390,18 +393,11 @@ namespace { 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) { + virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, + bool IsPCRel, bool IsRelocWithSymbol, + int64_t Addend); + virtual bool isFixupKindPCRel(unsigned Kind) const { switch (Kind) { default: return false; @@ -425,14 +421,12 @@ namespace { 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) { + protected: + virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, + bool IsPCRel, bool IsRelocWithSymbol, + int64_t Addend); + + virtual bool isFixupKindPCRel(unsigned Kind) const { switch (Kind) { default: 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 ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm, const MCSymbol *S) { @@ -1467,7 +1525,9 @@ ARMELFObjectWriter::~ARMELFObjectWriter() unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target, const MCFixup &Fixup, - bool IsPCRel) { + bool IsPCRel, + bool IsRelocWithSymbol, + int64_t Addend) { MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); @@ -1501,70 +1561,6 @@ unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target, 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(raw_ostream &_OS, bool _Is64Bit, @@ -1579,54 +1575,11 @@ MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, MBlazeELFObjectWriter::~MBlazeELFObjectWriter() { } -void MBlazeELFObjectWriter::RecordRelocation(const MCAssembler &Asm, - const MCAsmLayout &Layout, - const MCFragment *Fragment, +unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target, 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; - - // 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; - + bool IsPCRel, + bool IsRelocWithSymbol, + int64_t Addend) { // determine the type of the relocation unsigned Type; if (IsPCRel) { @@ -1644,25 +1597,16 @@ void MBlazeELFObjectWriter::RecordRelocation(const MCAssembler &Asm, 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; + Type = ((IsRelocWithSymbol || 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); + return Type; } //===- X86ELFObjectWriter -------------------------------------------===// @@ -1679,59 +1623,11 @@ X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, 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; - - // 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 X86ELFObjectWriter::GetRelocType(const MCValue &Target, + const MCFixup &Fixup, + bool IsPCRel, + bool IsRelocWithSymbol, + int64_t Addend) { // determine the type of the relocation MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? @@ -1866,11 +1762,5 @@ void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, 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); + return Type; } -- 2.34.1