mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-12 13:30:51 +00:00
Change relocation API to be per section.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141376 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
b76761351f
commit
f1164a2487
@ -59,14 +59,14 @@ namespace llvm {
|
||||
return reinterpret_cast<LLVMObjectFileRef>(const_cast<ObjectFile*>(OF));
|
||||
}
|
||||
|
||||
inline ObjectFile::section_iterator *unwrap(LLVMSectionIteratorRef SI) {
|
||||
return reinterpret_cast<ObjectFile::section_iterator*>(SI);
|
||||
inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
|
||||
return reinterpret_cast<section_iterator*>(SI);
|
||||
}
|
||||
|
||||
inline LLVMSectionIteratorRef
|
||||
wrap(const ObjectFile::section_iterator *SI) {
|
||||
wrap(const section_iterator *SI) {
|
||||
return reinterpret_cast<LLVMSectionIteratorRef>
|
||||
(const_cast<ObjectFile::section_iterator*>(SI));
|
||||
(const_cast<section_iterator*>(SI));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -84,6 +84,8 @@ private:
|
||||
error_code getSection(int32_t index,
|
||||
const coff_section *&Res) const;
|
||||
error_code getString(uint32_t offset, StringRef &Res) const;
|
||||
error_code getSymbol(uint32_t index,
|
||||
const coff_symbol *&Res) const;
|
||||
|
||||
const coff_symbol *toSymb(DataRefImpl Symb) const;
|
||||
const coff_section *toSec(DataRefImpl Sec) const;
|
||||
@ -110,6 +112,8 @@ protected:
|
||||
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
|
||||
virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
|
||||
bool &Result) const;
|
||||
virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
|
||||
virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
|
||||
|
||||
virtual error_code getRelocationNext(DataRefImpl Rel,
|
||||
RelocationRef &Res) const;
|
||||
@ -119,16 +123,19 @@ protected:
|
||||
SymbolRef &Res) const;
|
||||
virtual error_code getRelocationType(DataRefImpl Rel,
|
||||
uint32_t &Res) const;
|
||||
virtual error_code getRelocationTypeName(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const;
|
||||
virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
|
||||
int64_t &Res) const;
|
||||
virtual error_code getRelocationValueString(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const;
|
||||
|
||||
public:
|
||||
COFFObjectFile(MemoryBuffer *Object, error_code &ec);
|
||||
virtual symbol_iterator begin_symbols() const;
|
||||
virtual symbol_iterator end_symbols() const;
|
||||
virtual section_iterator begin_sections() const;
|
||||
virtual section_iterator end_sections() const;
|
||||
virtual relocation_iterator begin_relocations() const;
|
||||
virtual relocation_iterator end_relocations() const;
|
||||
|
||||
virtual uint8_t getBytesInAddress() const;
|
||||
virtual StringRef getFileFormatName() const;
|
||||
|
@ -27,12 +27,51 @@ namespace object {
|
||||
class ObjectFile;
|
||||
|
||||
union DataRefImpl {
|
||||
struct {
|
||||
// ELF needs this for relocations. This entire union should probably be a
|
||||
// char[max(8, sizeof(uintptr_t))] and require the impl to cast.
|
||||
uint16_t a, b;
|
||||
uint32_t c;
|
||||
} w;
|
||||
struct {
|
||||
uint32_t a, b;
|
||||
} d;
|
||||
uintptr_t p;
|
||||
};
|
||||
|
||||
template<class content_type>
|
||||
class content_iterator {
|
||||
content_type Current;
|
||||
public:
|
||||
content_iterator(content_type symb)
|
||||
: Current(symb) {}
|
||||
|
||||
const content_type* operator->() const {
|
||||
return &Current;
|
||||
}
|
||||
|
||||
const content_type &operator*() const {
|
||||
return Current;
|
||||
}
|
||||
|
||||
bool operator==(const content_iterator &other) const {
|
||||
return Current == other.Current;
|
||||
}
|
||||
|
||||
bool operator!=(const content_iterator &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
content_iterator& increment(error_code &err) {
|
||||
content_type next;
|
||||
if (error_code ec = Current.getNext(next))
|
||||
err = ec;
|
||||
else
|
||||
Current = next;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) {
|
||||
// Check bitwise identical. This is the only legal way to compare a union w/o
|
||||
// knowing which member is in use.
|
||||
@ -82,6 +121,7 @@ public:
|
||||
/// such as library functions
|
||||
error_code isGlobal(bool &Result) const;
|
||||
};
|
||||
typedef content_iterator<SymbolRef> symbol_iterator;
|
||||
|
||||
/// RelocationRef - This is a value type class that represents a single
|
||||
/// relocation in the list of relocations in the object file.
|
||||
@ -103,8 +143,20 @@ public:
|
||||
error_code getAddress(uint64_t &Result) const;
|
||||
error_code getSymbol(SymbolRef &Result) const;
|
||||
error_code getType(uint32_t &Result) const;
|
||||
|
||||
/// @brief Get a string that represents the type of this relocation.
|
||||
///
|
||||
/// This is for display purposes only.
|
||||
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
|
||||
/// relocation.
|
||||
///
|
||||
/// This is for display purposes only.
|
||||
error_code getValueString(SmallVectorImpl<char> &Result) const;
|
||||
};
|
||||
typedef content_iterator<RelocationRef> relocation_iterator;
|
||||
|
||||
/// SectionRef - This is a value type class that represents a single section in
|
||||
/// the list of sections in the object file.
|
||||
@ -135,7 +187,11 @@ public:
|
||||
error_code isBSS(bool &Result) const;
|
||||
|
||||
error_code containsSymbol(SymbolRef S, bool &Result) const;
|
||||
|
||||
relocation_iterator begin_relocations() const;
|
||||
relocation_iterator end_relocations() const;
|
||||
};
|
||||
typedef content_iterator<SectionRef> section_iterator;
|
||||
|
||||
const uint64_t UnknownAddressOrSize = ~0ULL;
|
||||
|
||||
@ -185,6 +241,8 @@ protected:
|
||||
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
|
||||
virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
|
||||
bool &Result) const = 0;
|
||||
virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0;
|
||||
virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0;
|
||||
|
||||
|
||||
// Same as above for RelocationRef.
|
||||
@ -197,46 +255,14 @@ protected:
|
||||
SymbolRef &Res) const = 0;
|
||||
virtual error_code getRelocationType(DataRefImpl Rel,
|
||||
uint32_t &Res) const = 0;
|
||||
virtual error_code getRelocationTypeName(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const = 0;
|
||||
virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
|
||||
int64_t &Res) const = 0;
|
||||
virtual error_code getRelocationValueString(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const = 0;
|
||||
|
||||
public:
|
||||
template<class content_type>
|
||||
class content_iterator {
|
||||
content_type Current;
|
||||
public:
|
||||
content_iterator(content_type symb)
|
||||
: Current(symb) {}
|
||||
|
||||
const content_type* operator->() const {
|
||||
return &Current;
|
||||
}
|
||||
|
||||
const content_type &operator*() const {
|
||||
return Current;
|
||||
}
|
||||
|
||||
bool operator==(const content_iterator &other) const {
|
||||
return Current == other.Current;
|
||||
}
|
||||
|
||||
bool operator!=(const content_iterator &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
content_iterator& increment(error_code &err) {
|
||||
content_type next;
|
||||
if (error_code ec = Current.getNext(next))
|
||||
err = ec;
|
||||
else
|
||||
Current = next;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
typedef content_iterator<SymbolRef> symbol_iterator;
|
||||
typedef content_iterator<SectionRef> section_iterator;
|
||||
typedef content_iterator<RelocationRef> relocation_iterator;
|
||||
|
||||
virtual symbol_iterator begin_symbols() const = 0;
|
||||
virtual symbol_iterator end_symbols() const = 0;
|
||||
@ -244,9 +270,6 @@ public:
|
||||
virtual section_iterator begin_sections() const = 0;
|
||||
virtual section_iterator end_sections() const = 0;
|
||||
|
||||
virtual relocation_iterator begin_relocations() const = 0;
|
||||
virtual relocation_iterator end_relocations() const = 0;
|
||||
|
||||
/// @brief The number of bytes used to represent an address in this object
|
||||
/// file format.
|
||||
virtual uint8_t getBytesInAddress() const = 0;
|
||||
@ -366,6 +389,14 @@ inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
|
||||
Result);
|
||||
}
|
||||
|
||||
inline relocation_iterator SectionRef::begin_relocations() const {
|
||||
return OwningObject->getSectionRelBegin(SectionPimpl);
|
||||
}
|
||||
|
||||
inline relocation_iterator SectionRef::end_relocations() const {
|
||||
return OwningObject->getSectionRelEnd(SectionPimpl);
|
||||
}
|
||||
|
||||
|
||||
/// RelocationRef
|
||||
inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
|
||||
@ -393,10 +424,20 @@ inline error_code RelocationRef::getType(uint32_t &Result) const {
|
||||
return OwningObject->getRelocationType(RelocationPimpl, Result);
|
||||
}
|
||||
|
||||
inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result)
|
||||
const {
|
||||
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)
|
||||
const {
|
||||
return OwningObject->getRelocationValueString(RelocationPimpl, Result);
|
||||
}
|
||||
|
||||
} // end namespace object
|
||||
} // end namespace llvm
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/Object/COFF.h"
|
||||
#include "llvm/ADT/SmallString.h"
|
||||
#include "llvm/ADT/StringSwitch.h"
|
||||
#include "llvm/ADT/Triple.h"
|
||||
|
||||
@ -364,6 +365,33 @@ error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
|
||||
const coff_section *sec = toSec(Sec);
|
||||
DataRefImpl ret;
|
||||
std::memset(&ret, 0, sizeof(ret));
|
||||
if (sec->NumberOfRelocations == 0)
|
||||
ret.p = 0;
|
||||
else
|
||||
ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
|
||||
|
||||
return relocation_iterator(RelocationRef(ret, this));
|
||||
}
|
||||
|
||||
relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
|
||||
const coff_section *sec = toSec(Sec);
|
||||
DataRefImpl ret;
|
||||
std::memset(&ret, 0, sizeof(ret));
|
||||
if (sec->NumberOfRelocations == 0)
|
||||
ret.p = 0;
|
||||
else
|
||||
ret.p = reinterpret_cast<uintptr_t>(
|
||||
reinterpret_cast<const coff_relocation*>(
|
||||
base() + sec->PointerToRelocations)
|
||||
+ sec->NumberOfRelocations);
|
||||
|
||||
return relocation_iterator(RelocationRef(ret, this));
|
||||
}
|
||||
|
||||
COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
|
||||
: ObjectFile(Binary::isCOFF, Object, ec) {
|
||||
// Check that we at least have enough room for a header.
|
||||
@ -427,14 +455,14 @@ COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
|
||||
ec = object_error::success;
|
||||
}
|
||||
|
||||
ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const {
|
||||
symbol_iterator COFFObjectFile::begin_symbols() const {
|
||||
DataRefImpl ret;
|
||||
std::memset(&ret, 0, sizeof(DataRefImpl));
|
||||
ret.p = reinterpret_cast<intptr_t>(SymbolTable);
|
||||
return symbol_iterator(SymbolRef(ret, this));
|
||||
}
|
||||
|
||||
ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const {
|
||||
symbol_iterator COFFObjectFile::end_symbols() const {
|
||||
// The symbol table ends where the string table begins.
|
||||
DataRefImpl ret;
|
||||
std::memset(&ret, 0, sizeof(DataRefImpl));
|
||||
@ -442,14 +470,14 @@ ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const {
|
||||
return symbol_iterator(SymbolRef(ret, this));
|
||||
}
|
||||
|
||||
ObjectFile::section_iterator COFFObjectFile::begin_sections() const {
|
||||
section_iterator COFFObjectFile::begin_sections() const {
|
||||
DataRefImpl ret;
|
||||
std::memset(&ret, 0, sizeof(DataRefImpl));
|
||||
ret.p = reinterpret_cast<intptr_t>(SectionTable);
|
||||
return section_iterator(SectionRef(ret, this));
|
||||
}
|
||||
|
||||
ObjectFile::section_iterator COFFObjectFile::end_sections() const {
|
||||
section_iterator COFFObjectFile::end_sections() const {
|
||||
DataRefImpl ret;
|
||||
std::memset(&ret, 0, sizeof(DataRefImpl));
|
||||
ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
|
||||
@ -508,42 +536,27 @@ error_code COFFObjectFile::getString(uint32_t offset,
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
error_code COFFObjectFile::getSymbol(uint32_t index,
|
||||
const coff_symbol *&Result) const {
|
||||
if (index > 0 && index < Header->NumberOfSymbols)
|
||||
Result = SymbolTable + index;
|
||||
else
|
||||
return object_error::parse_failed;
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
|
||||
assert(Rel.d.b < Header->NumberOfSections && "Section index out of range!");
|
||||
const coff_section *Sect = NULL;
|
||||
getSection(Rel.d.b, Sect);
|
||||
assert(Rel.d.a < Sect->NumberOfRelocations && "Relocation index out of range!");
|
||||
return
|
||||
reinterpret_cast<const coff_relocation*>(base() +
|
||||
Sect->PointerToRelocations) +
|
||||
Rel.d.a;
|
||||
return reinterpret_cast<const coff_relocation*>(Rel.p);
|
||||
}
|
||||
error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
|
||||
RelocationRef &Res) const {
|
||||
const coff_section *Sect = NULL;
|
||||
if (error_code ec = getSection(Rel.d.b, Sect))
|
||||
return ec;
|
||||
if (++Rel.d.a >= Sect->NumberOfRelocations) {
|
||||
Rel.d.a = 0;
|
||||
while (++Rel.d.b < Header->NumberOfSections) {
|
||||
const coff_section *Sect = NULL;
|
||||
getSection(Rel.d.b, Sect);
|
||||
if (Sect->NumberOfRelocations > 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
++*reinterpret_cast<const coff_relocation**>(&Rel.p);
|
||||
Res = RelocationRef(Rel, this);
|
||||
return object_error::success;
|
||||
}
|
||||
error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
|
||||
uint64_t &Res) const {
|
||||
const coff_section *Sect = NULL;
|
||||
if (error_code ec = getSection(Rel.d.b, Sect))
|
||||
return ec;
|
||||
const coff_relocation* R = toRel(Rel);
|
||||
Res = reinterpret_cast<uintptr_t>(base() +
|
||||
Sect->PointerToRawData +
|
||||
R->VirtualAddress);
|
||||
Res = toRel(Rel)->VirtualAddress;
|
||||
return object_error::success;
|
||||
}
|
||||
error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
|
||||
@ -560,24 +573,82 @@ error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
|
||||
Res = R->Type;
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
|
||||
case COFF::enum: res = #enum; break;
|
||||
|
||||
error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const {
|
||||
const coff_relocation *reloc = toRel(Rel);
|
||||
StringRef res;
|
||||
switch (Header->Machine) {
|
||||
case COFF::IMAGE_FILE_MACHINE_AMD64:
|
||||
switch (reloc->Type) {
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
|
||||
default:
|
||||
res = "Unknown";
|
||||
}
|
||||
break;
|
||||
case COFF::IMAGE_FILE_MACHINE_I386:
|
||||
switch (reloc->Type) {
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
|
||||
LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
|
||||
default:
|
||||
res = "Unknown";
|
||||
}
|
||||
break;
|
||||
default:
|
||||
res = "Unknown";
|
||||
}
|
||||
Result.append(res.begin(), res.end());
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
|
||||
|
||||
error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
|
||||
int64_t &Res) const {
|
||||
Res = 0;
|
||||
return object_error::success;
|
||||
}
|
||||
ObjectFile::relocation_iterator COFFObjectFile::begin_relocations() const {
|
||||
DataRefImpl ret;
|
||||
ret.d.a = 0;
|
||||
ret.d.b = 1;
|
||||
return relocation_iterator(RelocationRef(ret, this));
|
||||
error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const {
|
||||
const coff_relocation *reloc = toRel(Rel);
|
||||
const coff_symbol *symb;
|
||||
if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
|
||||
DataRefImpl sym;
|
||||
::memset(&sym, 0, sizeof(sym));
|
||||
sym.p = reinterpret_cast<uintptr_t>(symb);
|
||||
StringRef symname;
|
||||
if (error_code ec = getSymbolName(sym, symname)) return ec;
|
||||
Result.append(symname.begin(), symname.end());
|
||||
return object_error::success;
|
||||
}
|
||||
ObjectFile::relocation_iterator COFFObjectFile::end_relocations() const {
|
||||
DataRefImpl ret;
|
||||
ret.d.a = 0;
|
||||
ret.d.b = Header->NumberOfSections;
|
||||
return relocation_iterator(RelocationRef(ret, this));
|
||||
}
|
||||
|
||||
|
||||
namespace llvm {
|
||||
|
||||
|
@ -20,6 +20,8 @@
|
||||
#include "llvm/Support/Endian.h"
|
||||
#include "llvm/Support/ErrorHandling.h"
|
||||
#include "llvm/Support/MemoryBuffer.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
@ -286,6 +288,7 @@ class ELFObjectFile : public ObjectFile {
|
||||
|
||||
typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
|
||||
typedef DenseMap<unsigned, unsigned> IndexMap_t;
|
||||
typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
|
||||
|
||||
const Elf_Ehdr *Header;
|
||||
const Elf_Shdr *SectionHeaderTable;
|
||||
@ -293,12 +296,22 @@ class ELFObjectFile : public ObjectFile {
|
||||
const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
|
||||
Sections_t SymbolTableSections;
|
||||
IndexMap_t SymbolTableSectionsIndexMap;
|
||||
Sections_t RelocationTableSections;
|
||||
|
||||
/// @brief Map sections to an array of relocation sections that reference
|
||||
/// them sorted by section index.
|
||||
RelocMap_t SectionRelocMap;
|
||||
|
||||
/// @brief Get the relocation section that contains \a Rel.
|
||||
const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
|
||||
return getSection(Rel.w.b);
|
||||
}
|
||||
|
||||
void validateSymbol(DataRefImpl Symb) const;
|
||||
bool isRelocationHasAddend(DataRefImpl Rel) const;
|
||||
template<typename T>
|
||||
const T *getEntry(DataRefImpl Entry, Sections_t Sections) const;
|
||||
const T *getEntry(uint16_t Section, uint32_t Entry) const;
|
||||
template<typename T>
|
||||
const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
|
||||
const Elf_Sym *getSymbol(DataRefImpl Symb) const;
|
||||
const Elf_Shdr *getSection(DataRefImpl index) const;
|
||||
const Elf_Shdr *getSection(uint16_t index) const;
|
||||
@ -306,6 +319,7 @@ class ELFObjectFile : public ObjectFile {
|
||||
const Elf_Rela *getRela(DataRefImpl Rela) const;
|
||||
const char *getString(uint16_t section, uint32_t offset) const;
|
||||
const char *getString(const Elf_Shdr *section, uint32_t offset) const;
|
||||
error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const;
|
||||
|
||||
protected:
|
||||
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
|
||||
@ -328,6 +342,8 @@ protected:
|
||||
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
|
||||
virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
|
||||
bool &Result) const;
|
||||
virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
|
||||
virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
|
||||
|
||||
virtual error_code getRelocationNext(DataRefImpl Rel,
|
||||
RelocationRef &Res) const;
|
||||
@ -337,8 +353,12 @@ protected:
|
||||
SymbolRef &Res) const;
|
||||
virtual error_code getRelocationType(DataRefImpl Rel,
|
||||
uint32_t &Res) const;
|
||||
virtual error_code getRelocationTypeName(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const;
|
||||
virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
|
||||
int64_t &Res) const;
|
||||
virtual error_code getRelocationValueString(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const;
|
||||
|
||||
public:
|
||||
ELFObjectFile(MemoryBuffer *Object, error_code &ec);
|
||||
@ -346,8 +366,6 @@ public:
|
||||
virtual symbol_iterator end_symbols() const;
|
||||
virtual section_iterator begin_sections() const;
|
||||
virtual section_iterator end_sections() const;
|
||||
virtual relocation_iterator begin_relocations() const;
|
||||
virtual relocation_iterator end_relocations() const;
|
||||
|
||||
virtual uint8_t getBytesInAddress() const;
|
||||
virtual StringRef getFileFormatName() const;
|
||||
@ -404,18 +422,7 @@ error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
StringRef &Result) const {
|
||||
validateSymbol(Symb);
|
||||
const Elf_Sym *symb = getSymbol(Symb);
|
||||
if (symb->st_name == 0) {
|
||||
const Elf_Shdr *section = getSection(symb->st_shndx);
|
||||
if (!section)
|
||||
Result = "";
|
||||
else
|
||||
Result = getString(dot_shstrtab_sec, section->sh_name);
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
// Use the default symbol table name section.
|
||||
Result = getString(dot_strtab_sec, symb->st_name);
|
||||
return object_error::success;
|
||||
return getSymbolName(symb, Result);
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
@ -712,24 +719,65 @@ error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
relocation_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::getSectionRelBegin(DataRefImpl Sec) const {
|
||||
DataRefImpl RelData;
|
||||
memset(&RelData, 0, sizeof(RelData));
|
||||
const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
|
||||
RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
|
||||
if (sec != 0 && ittr != SectionRelocMap.end()) {
|
||||
RelData.w.a = getSection(ittr->second[0])->sh_link;
|
||||
RelData.w.b = ittr->second[0];
|
||||
RelData.w.c = 0;
|
||||
}
|
||||
return relocation_iterator(RelocationRef(RelData, this));
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
relocation_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::getSectionRelEnd(DataRefImpl Sec) const {
|
||||
DataRefImpl RelData;
|
||||
memset(&RelData, 0, sizeof(RelData));
|
||||
const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
|
||||
RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
|
||||
if (sec != 0 && ittr != SectionRelocMap.end()) {
|
||||
// Get the index of the last relocation section for this section.
|
||||
std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
|
||||
const Elf_Shdr *relocsec = getSection(relocsecindex);
|
||||
RelData.w.a = relocsec->sh_link;
|
||||
RelData.w.b = relocsecindex;
|
||||
RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
|
||||
}
|
||||
return relocation_iterator(RelocationRef(RelData, this));
|
||||
}
|
||||
|
||||
// Relocations
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
::getRelocationNext(DataRefImpl Rel,
|
||||
RelocationRef &Result) const {
|
||||
const Elf_Shdr *RelocationTableSection = RelocationTableSections[Rel.d.b];
|
||||
++Rel.w.c;
|
||||
const Elf_Shdr *relocsec = getSection(Rel.w.b);
|
||||
if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
|
||||
// We have reached the end of the relocations for this section. See if there
|
||||
// is another relocation section.
|
||||
RelocMap_t::mapped_type &relocseclist =
|
||||
SectionRelocMap.lookup(getSection(Rel.w.a));
|
||||
|
||||
// Check to see if we are at the end of this relocation table.
|
||||
if (++Rel.d.a >= RelocationTableSection->getEntityCount()) {
|
||||
// We are at the end. If there are other relocation tables, jump to them.
|
||||
Rel.d.a = 0;
|
||||
// Otherwise return the terminator.
|
||||
if (++Rel.d.b >= SymbolTableSections.size()) {
|
||||
Rel.d.a = std::numeric_limits<uint32_t>::max();
|
||||
Rel.d.b = std::numeric_limits<uint32_t>::max();
|
||||
// Do a binary search for the current reloc section index (which must be
|
||||
// present). Then get the next one.
|
||||
RelocMap_t::mapped_type::const_iterator loc =
|
||||
std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
|
||||
++loc;
|
||||
|
||||
// If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
|
||||
// to the end iterator.
|
||||
if (loc != relocseclist.end()) {
|
||||
Rel.w.b = *loc;
|
||||
Rel.w.a = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Result = RelocationRef(Rel, this);
|
||||
return object_error::success;
|
||||
}
|
||||
@ -739,7 +787,7 @@ error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
::getRelocationSymbol(DataRefImpl Rel,
|
||||
SymbolRef &Result) const {
|
||||
uint32_t symbolIdx;
|
||||
const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
|
||||
const Elf_Shdr *sec = getSection(Rel.w.b);
|
||||
switch (sec->sh_type) {
|
||||
default :
|
||||
report_fatal_error("Invalid section type in Rel!");
|
||||
@ -767,7 +815,7 @@ error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
::getRelocationAddress(DataRefImpl Rel,
|
||||
uint64_t &Result) const {
|
||||
uint64_t offset;
|
||||
const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
|
||||
const Elf_Shdr *sec = getSection(Rel.w.b);
|
||||
switch (sec->sh_type) {
|
||||
default :
|
||||
report_fatal_error("Invalid section type in Rel!");
|
||||
@ -781,8 +829,7 @@ error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
}
|
||||
}
|
||||
|
||||
const Elf_Shdr *secAddr = getSection(sec->sh_info);
|
||||
Result = offset + reinterpret_cast<uintptr_t>(base() + secAddr->sh_offset);
|
||||
Result = offset;
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
@ -790,7 +837,7 @@ template<support::endianness target_endianness, bool is64Bits>
|
||||
error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
::getRelocationType(DataRefImpl Rel,
|
||||
uint32_t &Result) const {
|
||||
const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
|
||||
const Elf_Shdr *sec = getSection(Rel.w.b);
|
||||
switch (sec->sh_type) {
|
||||
default :
|
||||
report_fatal_error("Invalid section type in Rel!");
|
||||
@ -806,11 +853,127 @@ error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
#define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
|
||||
case ELF::enum: res = #enum; break;
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
::getRelocationTypeName(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const {
|
||||
const Elf_Shdr *sec = getSection(Rel.w.b);
|
||||
uint8_t type;
|
||||
StringRef res;
|
||||
switch (sec->sh_type) {
|
||||
default :
|
||||
return object_error::parse_failed;
|
||||
case ELF::SHT_REL : {
|
||||
type = getRel(Rel)->getType();
|
||||
break;
|
||||
}
|
||||
case ELF::SHT_RELA : {
|
||||
type = getRela(Rel)->getType();
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch (Header->e_machine) {
|
||||
case ELF::EM_X86_64:
|
||||
switch (type) {
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
|
||||
default:
|
||||
res = "Unknown";
|
||||
}
|
||||
break;
|
||||
case ELF::EM_386:
|
||||
switch (type) {
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
|
||||
LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
|
||||
default:
|
||||
res = "Unknown";
|
||||
}
|
||||
break;
|
||||
default:
|
||||
res = "Unknown";
|
||||
}
|
||||
Result.append(res.begin(), res.end());
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
#undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
::getRelocationAdditionalInfo(DataRefImpl Rel,
|
||||
int64_t &Result) const {
|
||||
const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
|
||||
const Elf_Shdr *sec = getSection(Rel.w.b);
|
||||
switch (sec->sh_type) {
|
||||
default :
|
||||
report_fatal_error("Invalid section type in Rel!");
|
||||
@ -825,7 +988,60 @@ error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
::getRelocationValueString(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const {
|
||||
const Elf_Shdr *sec = getSection(Rel.w.b);
|
||||
uint8_t type;
|
||||
StringRef res;
|
||||
int64_t addend = 0;
|
||||
uint16_t symbol_index = 0;
|
||||
switch (sec->sh_type) {
|
||||
default :
|
||||
return object_error::parse_failed;
|
||||
case ELF::SHT_REL : {
|
||||
type = getRel(Rel)->getType();
|
||||
symbol_index = getRel(Rel)->getSymbol();
|
||||
// TODO: Read implicit addend from section data.
|
||||
break;
|
||||
}
|
||||
case ELF::SHT_RELA : {
|
||||
type = getRela(Rel)->getType();
|
||||
symbol_index = getRela(Rel)->getSymbol();
|
||||
addend = getRela(Rel)->r_addend;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
|
||||
StringRef symname;
|
||||
if (error_code ec = getSymbolName(symb, symname))
|
||||
return ec;
|
||||
switch (Header->e_machine) {
|
||||
case ELF::EM_X86_64:
|
||||
switch (type) {
|
||||
case ELF::R_X86_64_32S:
|
||||
res = symname;
|
||||
break;
|
||||
case ELF::R_X86_64_PC32: {
|
||||
std::string fmtbuf;
|
||||
raw_string_ostream fmt(fmtbuf);
|
||||
fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
|
||||
fmt.flush();
|
||||
Result.append(fmtbuf.begin(), fmtbuf.end());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
res = "Unknown";
|
||||
}
|
||||
break;
|
||||
default:
|
||||
res = "Unknown";
|
||||
}
|
||||
if (Result.empty())
|
||||
Result.append(res.begin(), res.end());
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
|
||||
@ -849,19 +1065,24 @@ ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
|
||||
|
||||
|
||||
// To find the symbol tables we walk the section table to find SHT_STMTAB.
|
||||
const Elf_Shdr* sh =
|
||||
reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
|
||||
const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
|
||||
for (unsigned i = 0; i < Header->e_shnum; ++i) {
|
||||
if (sh->sh_type == ELF::SHT_SYMTAB) {
|
||||
SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
|
||||
SymbolTableSections.push_back(sh);
|
||||
}
|
||||
if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
|
||||
RelocationTableSections.push_back(sh);
|
||||
SectionRelocMap[getSection(sh->sh_link)].push_back(i);
|
||||
}
|
||||
++sh;
|
||||
}
|
||||
|
||||
// Sort section relocation lists by index.
|
||||
for (RelocMap_t::iterator i = SectionRelocMap.begin(),
|
||||
e = SectionRelocMap.end(); i != e; ++i) {
|
||||
std::sort(i->second.begin(), i->second.end());
|
||||
}
|
||||
|
||||
// Get string table sections.
|
||||
dot_shstrtab_sec = getSection(Header->e_shstrndx);
|
||||
if (dot_shstrtab_sec) {
|
||||
@ -894,8 +1115,8 @@ ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
ObjectFile::symbol_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::begin_symbols() const {
|
||||
symbol_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::begin_symbols() const {
|
||||
DataRefImpl SymbolData;
|
||||
memset(&SymbolData, 0, sizeof(SymbolData));
|
||||
if (SymbolTableSections.size() == 0) {
|
||||
@ -909,8 +1130,8 @@ ObjectFile::symbol_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
ObjectFile::symbol_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::end_symbols() const {
|
||||
symbol_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::end_symbols() const {
|
||||
DataRefImpl SymbolData;
|
||||
memset(&SymbolData, 0, sizeof(SymbolData));
|
||||
SymbolData.d.a = std::numeric_limits<uint32_t>::max();
|
||||
@ -919,8 +1140,8 @@ ObjectFile::symbol_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
ObjectFile::section_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::begin_sections() const {
|
||||
section_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::begin_sections() const {
|
||||
DataRefImpl ret;
|
||||
memset(&ret, 0, sizeof(DataRefImpl));
|
||||
ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
|
||||
@ -928,8 +1149,8 @@ ObjectFile::section_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
ObjectFile::section_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::end_sections() const {
|
||||
section_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::end_sections() const {
|
||||
DataRefImpl ret;
|
||||
memset(&ret, 0, sizeof(DataRefImpl));
|
||||
ret.p = reinterpret_cast<intptr_t>(base()
|
||||
@ -938,31 +1159,6 @@ ObjectFile::section_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
return section_iterator(SectionRef(ret, this));
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
ObjectFile::relocation_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::begin_relocations() const {
|
||||
DataRefImpl RelData;
|
||||
memset(&RelData, 0, sizeof(RelData));
|
||||
if (RelocationTableSections.size() == 0) {
|
||||
RelData.d.a = std::numeric_limits<uint32_t>::max();
|
||||
RelData.d.b = std::numeric_limits<uint32_t>::max();
|
||||
} else {
|
||||
RelData.d.a = 0;
|
||||
RelData.d.b = 0;
|
||||
}
|
||||
return relocation_iterator(RelocationRef(RelData, this));
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
ObjectFile::relocation_iterator ELFObjectFile<target_endianness, is64Bits>
|
||||
::end_relocations() const {
|
||||
DataRefImpl RelData;
|
||||
memset(&RelData, 0, sizeof(RelData));
|
||||
RelData.d.a = std::numeric_limits<uint32_t>::max();
|
||||
RelData.d.b = std::numeric_limits<uint32_t>::max();
|
||||
return relocation_iterator(RelocationRef(RelData, this));
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
|
||||
return is64Bits ? 8 : 4;
|
||||
@ -1012,41 +1208,49 @@ unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
template<typename T>
|
||||
inline const T *
|
||||
ELFObjectFile<target_endianness, is64Bits>::getEntry(DataRefImpl Entry,
|
||||
Sections_t Sections) const {
|
||||
const Elf_Shdr *sec = Sections[Entry.d.b];
|
||||
ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
|
||||
uint32_t Entry) const {
|
||||
return getEntry<T>(getSection(Section), Entry);
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
template<typename T>
|
||||
inline const T *
|
||||
ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
|
||||
uint32_t Entry) const {
|
||||
return reinterpret_cast<const T *>(
|
||||
base()
|
||||
+ sec->sh_offset
|
||||
+ (Entry.d.a * sec->sh_entsize));
|
||||
+ Section->sh_offset
|
||||
+ (Entry * Section->sh_entsize));
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
|
||||
ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
|
||||
return getEntry<Elf_Sym>(Symb, SymbolTableSections);
|
||||
return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
|
||||
ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
|
||||
return getEntry<Elf_Rel>(Rel, RelocationTableSections);
|
||||
return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
|
||||
ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
|
||||
return getEntry<Elf_Rela>(Rela, RelocationTableSections);
|
||||
return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
|
||||
ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
|
||||
const Elf_Shdr *sec = getSection(Symb.d.b);
|
||||
if (sec->sh_type != ELF::SHT_SYMTAB)
|
||||
if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
|
||||
// FIXME: Proper error handling.
|
||||
report_fatal_error("Invalid symbol table section!");
|
||||
return sec;
|
||||
@ -1084,6 +1288,24 @@ const char *ELFObjectFile<target_endianness, is64Bits>
|
||||
return (const char *)base() + section->sh_offset + offset;
|
||||
}
|
||||
|
||||
template<support::endianness target_endianness, bool is64Bits>
|
||||
error_code ELFObjectFile<target_endianness, is64Bits>
|
||||
::getSymbolName(const Elf_Sym *symb,
|
||||
StringRef &Result) const {
|
||||
if (symb->st_name == 0) {
|
||||
const Elf_Shdr *section = getSection(symb->st_shndx);
|
||||
if (!section)
|
||||
Result = "";
|
||||
else
|
||||
Result = getString(dot_shstrtab_sec, section->sh_name);
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
// Use the default symbol table name section.
|
||||
Result = getString(dot_strtab_sec, symb->st_name);
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
// EI_CLASS, EI_DATA.
|
||||
static std::pair<unsigned char, unsigned char>
|
||||
getElfArchType(MemoryBuffer *Object) {
|
||||
|
@ -39,8 +39,6 @@ public:
|
||||
virtual symbol_iterator end_symbols() const;
|
||||
virtual section_iterator begin_sections() const;
|
||||
virtual section_iterator end_sections() const;
|
||||
virtual relocation_iterator begin_relocations() const;
|
||||
virtual relocation_iterator end_relocations() const;
|
||||
|
||||
virtual uint8_t getBytesInAddress() const;
|
||||
virtual StringRef getFileFormatName() const;
|
||||
@ -67,6 +65,8 @@ protected:
|
||||
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
|
||||
virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
|
||||
bool &Result) const;
|
||||
virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
|
||||
virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
|
||||
|
||||
virtual error_code getRelocationNext(DataRefImpl Rel,
|
||||
RelocationRef &Res) const;
|
||||
@ -76,8 +76,13 @@ protected:
|
||||
SymbolRef &Res) const;
|
||||
virtual error_code getRelocationType(DataRefImpl Rel,
|
||||
uint32_t &Res) const;
|
||||
virtual error_code getRelocationTypeName(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const;
|
||||
virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
|
||||
int64_t &Res) const;
|
||||
virtual error_code getRelocationValueString(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const;
|
||||
|
||||
private:
|
||||
MachOObject *MachOObj;
|
||||
mutable uint32_t RegisteredStringTable;
|
||||
@ -96,6 +101,7 @@ private:
|
||||
InMemoryStruct<macho::Section64> &Res) const;
|
||||
void getRelocation(DataRefImpl Rel,
|
||||
InMemoryStruct<macho::RelocationEntry> &Res) const;
|
||||
std::size_t getSectionIndex(DataRefImpl Sec) const;
|
||||
};
|
||||
|
||||
MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
|
||||
@ -324,7 +330,7 @@ error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
|
||||
}
|
||||
|
||||
|
||||
ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
|
||||
symbol_iterator MachOObjectFile::begin_symbols() const {
|
||||
// DRI.d.a = segment number; DRI.d.b = symbol index.
|
||||
DataRefImpl DRI;
|
||||
DRI.d.a = DRI.d.b = 0;
|
||||
@ -332,7 +338,7 @@ ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
|
||||
return symbol_iterator(SymbolRef(DRI, this));
|
||||
}
|
||||
|
||||
ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
|
||||
symbol_iterator MachOObjectFile::end_symbols() const {
|
||||
DataRefImpl DRI;
|
||||
DRI.d.a = MachOObj->getHeader().NumLoadCommands;
|
||||
DRI.d.b = 0;
|
||||
@ -380,6 +386,13 @@ MachOObjectFile::getSection(DataRefImpl DRI,
|
||||
MachOObj->ReadSection(LCI, DRI.d.b, Res);
|
||||
}
|
||||
|
||||
std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
|
||||
SectionList::const_iterator loc =
|
||||
std::find(Sections.begin(), Sections.end(), Sec);
|
||||
assert(loc != Sections.end() && "Sec is not a valid section!");
|
||||
return std::distance(Sections.begin(), loc);
|
||||
}
|
||||
|
||||
void
|
||||
MachOObjectFile::getSection64(DataRefImpl DRI,
|
||||
InMemoryStruct<macho::Section64> &Res) const {
|
||||
@ -511,14 +524,37 @@ error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
|
||||
relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
|
||||
DataRefImpl ret;
|
||||
ret.d.a = 0;
|
||||
ret.d.b = getSectionIndex(Sec);
|
||||
return relocation_iterator(RelocationRef(ret, this));
|
||||
}
|
||||
relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
|
||||
uint32_t last_reloc;
|
||||
if (is64BitLoadCommand(MachOObj, Sec)) {
|
||||
InMemoryStruct<macho::Section64> Sect;
|
||||
getSection64(Sec, Sect);
|
||||
last_reloc = Sect->NumRelocationTableEntries;
|
||||
} else {
|
||||
InMemoryStruct<macho::Section> Sect;
|
||||
getSection(Sec, Sect);
|
||||
last_reloc = Sect->NumRelocationTableEntries;
|
||||
}
|
||||
DataRefImpl ret;
|
||||
ret.d.a = last_reloc;
|
||||
ret.d.b = getSectionIndex(Sec);
|
||||
return relocation_iterator(RelocationRef(ret, this));
|
||||
}
|
||||
|
||||
section_iterator MachOObjectFile::begin_sections() const {
|
||||
DataRefImpl DRI;
|
||||
DRI.d.a = DRI.d.b = 0;
|
||||
moveToNextSection(DRI);
|
||||
return section_iterator(SectionRef(DRI, this));
|
||||
}
|
||||
|
||||
ObjectFile::section_iterator MachOObjectFile::end_sections() const {
|
||||
section_iterator MachOObjectFile::end_sections() const {
|
||||
DataRefImpl DRI;
|
||||
DRI.d.a = MachOObj->getHeader().NumLoadCommands;
|
||||
DRI.d.b = 0;
|
||||
@ -545,23 +581,6 @@ getRelocation(DataRefImpl Rel,
|
||||
error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
|
||||
RelocationRef &Res) const {
|
||||
++Rel.d.a;
|
||||
while (Rel.d.b < Sections.size()) {
|
||||
unsigned relocationCount;
|
||||
if (MachOObj->is64Bit()) {
|
||||
InMemoryStruct<macho::Section64> Sect;
|
||||
getSection64(Sections[Rel.d.b], Sect);
|
||||
relocationCount = Sect->NumRelocationTableEntries;
|
||||
} else {
|
||||
InMemoryStruct<macho::Section> Sect;
|
||||
getSection(Sections[Rel.d.b], Sect);
|
||||
relocationCount = Sect->NumRelocationTableEntries;
|
||||
}
|
||||
if (Rel.d.a < relocationCount)
|
||||
break;
|
||||
|
||||
Rel.d.a = 0;
|
||||
++Rel.d.b;
|
||||
}
|
||||
Res = RelocationRef(Rel, this);
|
||||
return object_error::success;
|
||||
}
|
||||
@ -610,6 +629,10 @@ error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
|
||||
Res = RE->Word1;
|
||||
return object_error::success;
|
||||
}
|
||||
error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const {
|
||||
return object_error::success;
|
||||
}
|
||||
error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
|
||||
int64_t &Res) const {
|
||||
InMemoryStruct<macho::RelocationEntry> RE;
|
||||
@ -631,16 +654,9 @@ error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
|
||||
}
|
||||
return object_error::success;
|
||||
}
|
||||
ObjectFile::relocation_iterator MachOObjectFile::begin_relocations() const {
|
||||
DataRefImpl ret;
|
||||
ret.d.a = ret.d.b = 0;
|
||||
return relocation_iterator(RelocationRef(ret, this));
|
||||
}
|
||||
ObjectFile::relocation_iterator MachOObjectFile::end_relocations() const {
|
||||
DataRefImpl ret;
|
||||
ret.d.a = 0;
|
||||
ret.d.b = Sections.size();
|
||||
return relocation_iterator(RelocationRef(ret, this));
|
||||
error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
|
||||
SmallVectorImpl<char> &Result) const {
|
||||
return object_error::success;
|
||||
}
|
||||
|
||||
/*===-- Miscellaneous -----------------------------------------------------===*/
|
||||
|
@ -27,8 +27,8 @@ void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
|
||||
}
|
||||
|
||||
LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
|
||||
ObjectFile::section_iterator SI = unwrap(ObjectFile)->begin_sections();
|
||||
return wrap(new ObjectFile::section_iterator(SI));
|
||||
section_iterator SI = unwrap(ObjectFile)->begin_sections();
|
||||
return wrap(new section_iterator(SI));
|
||||
}
|
||||
|
||||
void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
|
||||
|
@ -155,7 +155,7 @@ void llvm::DisassembleInputLibObject(StringRef Filename) {
|
||||
<< ":\tfile format " << Obj->getFileFormatName() << "\n\n";
|
||||
|
||||
error_code ec;
|
||||
for (ObjectFile::section_iterator i = Obj->begin_sections(),
|
||||
for (section_iterator i = Obj->begin_sections(),
|
||||
e = Obj->end_sections();
|
||||
i != e; i.increment(ec)) {
|
||||
if (error(ec)) break;
|
||||
@ -165,7 +165,7 @@ void llvm::DisassembleInputLibObject(StringRef Filename) {
|
||||
|
||||
// Make a list of all the symbols in this section.
|
||||
std::vector<std::pair<uint64_t, StringRef> > Symbols;
|
||||
for (ObjectFile::symbol_iterator si = Obj->begin_symbols(),
|
||||
for (symbol_iterator si = Obj->begin_symbols(),
|
||||
se = Obj->end_symbols();
|
||||
si != se; si.increment(ec)) {
|
||||
bool contains;
|
||||
|
Loading…
Reference in New Issue
Block a user