Change getRelocationAdditionalInfo to be ELF only.

It was only implemented for ELF where it collected the Addend, so this
patch also renames it to getRelocationAddend.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181502 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2013-05-09 03:39:05 +00:00
parent c496875f0c
commit 167957fa09
10 changed files with 65 additions and 59 deletions

View File

@ -149,8 +149,6 @@ protected:
uint64_t &Res) const; uint64_t &Res) const;
virtual error_code getRelocationTypeName(DataRefImpl Rel, virtual error_code getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const; SmallVectorImpl<char> &Result) const;
virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
int64_t &Res) const;
virtual error_code getRelocationValueString(DataRefImpl Rel, virtual error_code getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const; SmallVectorImpl<char> &Result) const;

View File

@ -677,6 +677,7 @@ public:
error_code getSymbolVersion(SymbolRef Symb, StringRef &Version, error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
bool &IsDefault) const; bool &IsDefault) const;
uint64_t getSymbolIndex(const Elf_Sym *sym) const; uint64_t getSymbolIndex(const Elf_Sym *sym) const;
error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
protected: protected:
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
@ -725,8 +726,6 @@ protected:
uint64_t &Res) const; uint64_t &Res) const;
virtual error_code getRelocationTypeName(DataRefImpl Rel, virtual error_code getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const; SmallVectorImpl<char> &Result) const;
virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
int64_t &Res) const;
virtual error_code getRelocationValueString(DataRefImpl Rel, virtual error_code getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const; SmallVectorImpl<char> &Result) const;
@ -2227,7 +2226,7 @@ error_code ELFObjectFile<ELFT>::getRelocationTypeName(
} }
template<class ELFT> template<class ELFT>
error_code ELFObjectFile<ELFT>::getRelocationAdditionalInfo( error_code ELFObjectFile<ELFT>::getRelocationAddend(
DataRefImpl Rel, int64_t &Result) const { DataRefImpl Rel, int64_t &Result) const {
const Elf_Shdr *sec = getSection(Rel.w.b); const Elf_Shdr *sec = getSection(Rel.w.b);
switch (sec->sh_type) { switch (sec->sh_type) {
@ -2949,6 +2948,35 @@ error_code ELFObjectFile<ELFT>::getSymbolVersion(const Elf_Shdr *section,
return object_error::success; return object_error::success;
} }
/// FIXME: Maybe we should have a base ElfObjectFile that is not a template
/// and make these member functions?
static inline error_code getELFRelocationAddend(const RelocationRef R,
int64_t &Addend) {
const ObjectFile *Obj = R.getObjectFile();
DataRefImpl DRI = R.getRawDataRefImpl();
// Little-endian 32-bit
if (const ELFObjectFile<ELFType<support::little, 4, false> > *ELFObj =
dyn_cast<ELFObjectFile<ELFType<support::little, 4, false> > >(Obj))
return ELFObj->getRelocationAddend(DRI, Addend);
// Big-endian 32-bit
if (const ELFObjectFile<ELFType<support::big, 4, false> > *ELFObj =
dyn_cast<ELFObjectFile<ELFType<support::big, 4, false> > >(Obj))
return ELFObj->getRelocationAddend(DRI, Addend);
// Little-endian 64-bit
if (const ELFObjectFile<ELFType<support::little, 8, true> > *ELFObj =
dyn_cast<ELFObjectFile<ELFType<support::little, 8, true> > >(Obj))
return ELFObj->getRelocationAddend(DRI, Addend);
// Big-endian 64-bit
if (const ELFObjectFile<ELFType<support::big, 8, true> > *ELFObj =
dyn_cast<ELFObjectFile<ELFType<support::big, 8, true> > >(Obj))
return ELFObj->getRelocationAddend(DRI, Addend);
llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
}
/// This is a generic interface for retrieving GNU symbol version /// This is a generic interface for retrieving GNU symbol version
/// information from an ELFObjectFile. /// information from an ELFObjectFile.
static inline error_code GetELFSymbolVersion(const ObjectFile *Obj, static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,

View File

@ -76,8 +76,6 @@ public:
virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const; virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
virtual error_code getRelocationTypeName(DataRefImpl Rel, virtual error_code getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const; SmallVectorImpl<char> &Result) const;
virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
int64_t &Res) const;
virtual error_code getRelocationValueString(DataRefImpl Rel, virtual error_code getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const; SmallVectorImpl<char> &Result) const;
virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const; virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;

View File

@ -119,7 +119,6 @@ public:
/// ///
/// This is for display purposes only. /// This is for display purposes only.
error_code getTypeName(SmallVectorImpl<char> &Result) const; error_code getTypeName(SmallVectorImpl<char> &Result) const;
error_code getAdditionalInfo(int64_t &Result) const;
/// @brief Get a string that represents the calculation of the value of this /// @brief Get a string that represents the calculation of the value of this
/// relocation. /// relocation.
@ -128,6 +127,7 @@ public:
error_code getValueString(SmallVectorImpl<char> &Result) const; error_code getValueString(SmallVectorImpl<char> &Result) const;
DataRefImpl getRawDataRefImpl() const; DataRefImpl getRawDataRefImpl() const;
const ObjectFile *getObjectFile() const;
}; };
typedef content_iterator<RelocationRef> relocation_iterator; typedef content_iterator<RelocationRef> relocation_iterator;
@ -342,8 +342,6 @@ protected:
uint64_t &Res) const = 0; uint64_t &Res) const = 0;
virtual error_code getRelocationTypeName(DataRefImpl Rel, virtual error_code getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const = 0; SmallVectorImpl<char> &Result) const = 0;
virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
int64_t &Res) const = 0;
virtual error_code getRelocationValueString(DataRefImpl Rel, virtual error_code getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const = 0; SmallVectorImpl<char> &Result) const = 0;
virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const { virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const {
@ -579,10 +577,6 @@ inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result)
return OwningObject->getRelocationTypeName(RelocationPimpl, Result); return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
} }
inline error_code RelocationRef::getAdditionalInfo(int64_t &Result) const {
return OwningObject->getRelocationAdditionalInfo(RelocationPimpl, Result);
}
inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result) inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result)
const { const {
return OwningObject->getRelocationValueString(RelocationPimpl, Result); return OwningObject->getRelocationValueString(RelocationPimpl, Result);
@ -596,6 +590,10 @@ inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
return RelocationPimpl; return RelocationPimpl;
} }
inline const ObjectFile *RelocationRef::getObjectFile() const {
return OwningObject;
}
// Inline function definitions. // Inline function definitions.
inline LibraryRef::LibraryRef(DataRefImpl LibraryP, const ObjectFile *Owner) inline LibraryRef::LibraryRef(DataRefImpl LibraryP, const ObjectFile *Owner)
: LibraryPimpl(LibraryP) : LibraryPimpl(LibraryP)

View File

@ -18,6 +18,7 @@
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Object/ObjectFile.h" #include "llvm/Object/ObjectFile.h"
#include "llvm/Object/ELF.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/ELF.h" #include "llvm/Support/ELF.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
@ -123,6 +124,12 @@ private:
StringRef FileFormat; StringRef FileFormat;
bool HasError; bool HasError;
int64_t getAddend(RelocationRef R) {
int64_t Addend;
getELFRelocationAddend(R, Addend);
return Addend;
}
/// Operations /// Operations
/// 386-ELF /// 386-ELF
@ -133,15 +140,13 @@ private:
// Ideally the Addend here will be the addend in the data for // Ideally the Addend here will be the addend in the data for
// the relocation. It's not actually the case for Rel relocations. // the relocation. It's not actually the case for Rel relocations.
RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) { RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
return RelocToApply(Value + Addend, 4); return RelocToApply(Value + Addend, 4);
} }
RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value, RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
uint64_t SecAddr) { uint64_t SecAddr) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
uint64_t Address; uint64_t Address;
R.getOffset(Address); R.getOffset(Address);
return RelocToApply(Value + Addend - Address, 4); return RelocToApply(Value + Addend - Address, 4);
@ -152,51 +157,44 @@ private:
return RelocToApply(0, 0); return RelocToApply(0, 0);
} }
RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) { RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
return RelocToApply(Value + Addend, 8); return RelocToApply(Value + Addend, 8);
} }
RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value, RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
uint64_t SecAddr) { uint64_t SecAddr) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
uint64_t Address; uint64_t Address;
R.getOffset(Address); R.getOffset(Address);
return RelocToApply(Value + Addend - Address, 4); return RelocToApply(Value + Addend - Address, 4);
} }
RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) { RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
uint32_t Res = (Value + Addend) & 0xFFFFFFFF; uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
return RelocToApply(Res, 4); return RelocToApply(Res, 4);
} }
RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) { RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
int32_t Res = (Value + Addend) & 0xFFFFFFFF; int32_t Res = (Value + Addend) & 0xFFFFFFFF;
return RelocToApply(Res, 4); return RelocToApply(Res, 4);
} }
/// PPC64 ELF /// PPC64 ELF
RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) { RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
uint32_t Res = (Value + Addend) & 0xFFFFFFFF; uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
return RelocToApply(Res, 4); return RelocToApply(Res, 4);
} }
/// MIPS ELF /// MIPS ELF
RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) { RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
uint32_t Res = (Value + Addend) & 0xFFFFFFFF; uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
return RelocToApply(Res, 4); return RelocToApply(Res, 4);
} }
// AArch64 ELF // AArch64 ELF
RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) { RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
int64_t Res = Value + Addend; int64_t Res = Value + Addend;
// Overflow check allows for both signed and unsigned interpretation. // Overflow check allows for both signed and unsigned interpretation.
@ -207,15 +205,13 @@ private:
} }
RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) { RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
return RelocToApply(Value + Addend, 8); return RelocToApply(Value + Addend, 8);
} }
// SystemZ ELF // SystemZ ELF
RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) { RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
int64_t Res = Value + Addend; int64_t Res = Value + Addend;
// Overflow check allows for both signed and unsigned interpretation. // Overflow check allows for both signed and unsigned interpretation.
@ -226,8 +222,7 @@ private:
} }
RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) { RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
int64_t Addend; int64_t Addend = getAddend(R);
R.getAdditionalInfo(Addend);
return RelocToApply(Value + Addend, 8); return RelocToApply(Value + Addend, 8);
} }
}; };

View File

@ -519,10 +519,10 @@ void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
SymbolRef TargetSymbol; SymbolRef TargetSymbol;
uint64_t TargetSymbolOffset; uint64_t TargetSymbolOffset;
int64_t TargetAdditionalInfo;
check(i->getSymbol(TargetSymbol)); check(i->getSymbol(TargetSymbol));
check(i->getOffset(TargetSymbolOffset)); check(i->getOffset(TargetSymbolOffset));
check(i->getAdditionalInfo(TargetAdditionalInfo)); int64_t Addend;
check(getELFRelocationAddend(*i, Addend));
i = i.increment(err); i = i.increment(err);
if (i == e) if (i == e)
@ -544,7 +544,7 @@ void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
section_iterator tsi(Obj.end_sections()); section_iterator tsi(Obj.end_sections());
check(TargetSymbol.getSection(tsi)); check(TargetSymbol.getSection(tsi));
Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections); Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
Rel.Addend = (intptr_t)TargetAdditionalInfo; Rel.Addend = (intptr_t)Addend;
return; return;
} }
} }
@ -742,7 +742,7 @@ void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
uint64_t RelType; uint64_t RelType;
Check(RelI.getType(RelType)); Check(RelI.getType(RelType));
int64_t Addend; int64_t Addend;
Check(RelI.getAdditionalInfo(Addend)); Check(getELFRelocationAddend(RelI, Addend));
SymbolRef Symbol; SymbolRef Symbol;
Check(RelI.getSymbol(Symbol)); Check(RelI.getSymbol(Symbol));

View File

@ -798,11 +798,6 @@ error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
int64_t &Res) const {
Res = 0;
return object_error::success;
}
error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel, error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const { SmallVectorImpl<char> &Result) const {
const coff_relocation *reloc = toRel(Rel); const coff_relocation *reloc = toRel(Rel);

View File

@ -989,12 +989,6 @@ MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
return object_error::success; return object_error::success;
} }
error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
int64_t &Res) const {
Res = 0;
return object_error::success;
}
error_code error_code
MachOObjectFile::getRelocationValueString(DataRefImpl Rel, MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const { SmallVectorImpl<char> &Result) const {

View File

@ -7,12 +7,12 @@ RUN: | FileCheck %s
// CHECK-NEXT: Offset: 0x4018F8 // CHECK-NEXT: Offset: 0x4018F8
// CHECK-NEXT: Type: R_X86_64_JUMP_SLOT (7) // CHECK-NEXT: Type: R_X86_64_JUMP_SLOT (7)
// CHECK-NEXT: Symbol: __libc_start_main // CHECK-NEXT: Symbol: __libc_start_main
// CHECK-NEXT: Info: 0x0 // CHECK-NEXT: Addend: 0x0
// CHECK-NEXT: } // CHECK-NEXT: }
// CHECK-NEXT: Relocation { // CHECK-NEXT: Relocation {
// CHECK-NEXT: Offset: 0x401900 // CHECK-NEXT: Offset: 0x401900
// CHECK-NEXT: Type: R_X86_64_JUMP_SLOT (7) // CHECK-NEXT: Type: R_X86_64_JUMP_SLOT (7)
// CHECK-NEXT: Symbol: puts // CHECK-NEXT: Symbol: puts
// CHECK-NEXT: Info: 0x0 // CHECK-NEXT: Addend: 0x0
// CHECK-NEXT: } // CHECK-NEXT: }
// CHECK-NEXT: } // CHECK-NEXT: }

View File

@ -579,7 +579,7 @@ void ELFDumper<ELFT>::printRelocation(section_iterator Sec,
uint64_t Offset; uint64_t Offset;
uint64_t RelocType; uint64_t RelocType;
SmallString<32> RelocName; SmallString<32> RelocName;
int64_t Info; int64_t Addend;
StringRef SymbolName; StringRef SymbolName;
SymbolRef Symbol; SymbolRef Symbol;
if (Obj->getElfHeader()->e_type == ELF::ET_REL){ if (Obj->getElfHeader()->e_type == ELF::ET_REL){
@ -589,7 +589,7 @@ void ELFDumper<ELFT>::printRelocation(section_iterator Sec,
} }
if (error(RelI->getType(RelocType))) return; if (error(RelI->getType(RelocType))) return;
if (error(RelI->getTypeName(RelocName))) return; if (error(RelI->getTypeName(RelocName))) return;
if (error(RelI->getAdditionalInfo(Info))) return; if (error(getELFRelocationAddend(*RelI, Addend))) return;
if (error(RelI->getSymbol(Symbol))) return; if (error(RelI->getSymbol(Symbol))) return;
if (error(Symbol.getName(SymbolName))) return; if (error(Symbol.getName(SymbolName))) return;
@ -598,13 +598,13 @@ void ELFDumper<ELFT>::printRelocation(section_iterator Sec,
W.printHex("Offset", Offset); W.printHex("Offset", Offset);
W.printNumber("Type", RelocName, RelocType); W.printNumber("Type", RelocName, RelocType);
W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-"); W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
W.printHex("Info", Info); W.printHex("Addend", Addend);
} else { } else {
raw_ostream& OS = W.startLine(); raw_ostream& OS = W.startLine();
OS << W.hex(Offset) OS << W.hex(Offset)
<< " " << RelocName << " " << RelocName
<< " " << (SymbolName.size() > 0 ? SymbolName : "-") << " " << (SymbolName.size() > 0 ? SymbolName : "-")
<< " " << W.hex(Info) << " " << W.hex(Addend)
<< "\n"; << "\n";
} }
} }