mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-04-22 00:37:49 +00:00
[Object][ELF] Simplify ELFObjectFile by using ELFType.
This simplifies the usage and implementation of ELFObjectFile by using ELFType to replace: <endianness target_endianness, std::size_t max_alignment, bool is64Bits> This does complicate the base ELF types as they must now use template template parameters to partially specialize for the 32 and 64bit cases. However these are only defined once. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172515 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
b5f8cd5bb0
commit
ac97f5ce48
include/llvm/Object
lib
tools/llvm-objdump
File diff suppressed because it is too large
Load Diff
@ -28,8 +28,6 @@
|
||||
using namespace llvm;
|
||||
using namespace llvm::object;
|
||||
|
||||
using support::endianness;
|
||||
|
||||
namespace {
|
||||
|
||||
static inline
|
||||
@ -40,22 +38,22 @@ error_code check(error_code Err) {
|
||||
return Err;
|
||||
}
|
||||
|
||||
template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
|
||||
template<class ELFT>
|
||||
class DyldELFObject
|
||||
: public ELFObjectFile<target_endianness, max_alignment, is64Bits> {
|
||||
LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
|
||||
: public ELFObjectFile<ELFT> {
|
||||
LLVM_ELF_IMPORT_TYPES(ELFT)
|
||||
|
||||
typedef Elf_Shdr_Impl<target_endianness, max_alignment, is64Bits> Elf_Shdr;
|
||||
typedef Elf_Sym_Impl<target_endianness, max_alignment, is64Bits> Elf_Sym;
|
||||
typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
|
||||
typedef Elf_Sym_Impl<ELFT> Elf_Sym;
|
||||
typedef
|
||||
Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, false> Elf_Rel;
|
||||
Elf_Rel_Impl<ELFT, false> Elf_Rel;
|
||||
typedef
|
||||
Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, true> Elf_Rela;
|
||||
Elf_Rel_Impl<ELFT, true> Elf_Rela;
|
||||
|
||||
typedef Elf_Ehdr_Impl<target_endianness, max_alignment, is64Bits> Elf_Ehdr;
|
||||
typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
|
||||
|
||||
typedef typename ELFDataTypeTypedefHelper<
|
||||
target_endianness, max_alignment, is64Bits>::value_type addr_type;
|
||||
ELFT>::value_type addr_type;
|
||||
|
||||
public:
|
||||
DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
|
||||
@ -65,25 +63,25 @@ public:
|
||||
|
||||
// Methods for type inquiry through isa, cast and dyn_cast
|
||||
static inline bool classof(const Binary *v) {
|
||||
return (isa<ELFObjectFile<target_endianness, max_alignment, is64Bits> >(v)
|
||||
return (isa<ELFObjectFile<ELFT> >(v)
|
||||
&& classof(cast<ELFObjectFile
|
||||
<target_endianness, max_alignment, is64Bits> >(v)));
|
||||
<ELFT> >(v)));
|
||||
}
|
||||
static inline bool classof(
|
||||
const ELFObjectFile<target_endianness, max_alignment, is64Bits> *v) {
|
||||
const ELFObjectFile<ELFT> *v) {
|
||||
return v->isDyldType();
|
||||
}
|
||||
};
|
||||
|
||||
template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
|
||||
template<class ELFT>
|
||||
class ELFObjectImage : public ObjectImageCommon {
|
||||
protected:
|
||||
DyldELFObject<target_endianness, max_alignment, is64Bits> *DyldObj;
|
||||
DyldELFObject<ELFT> *DyldObj;
|
||||
bool Registered;
|
||||
|
||||
public:
|
||||
ELFObjectImage(ObjectBuffer *Input,
|
||||
DyldELFObject<target_endianness, max_alignment, is64Bits> *Obj)
|
||||
DyldELFObject<ELFT> *Obj)
|
||||
: ObjectImageCommon(Input, Obj),
|
||||
DyldObj(Obj),
|
||||
Registered(false) {}
|
||||
@ -119,16 +117,15 @@ class ELFObjectImage : public ObjectImageCommon {
|
||||
// The MemoryBuffer passed into this constructor is just a wrapper around the
|
||||
// actual memory. Ultimately, the Binary parent class will take ownership of
|
||||
// this MemoryBuffer object but not the underlying memory.
|
||||
template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
|
||||
DyldELFObject<target_endianness, max_alignment, is64Bits>
|
||||
::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
|
||||
: ELFObjectFile<target_endianness, max_alignment, is64Bits>(Wrapper, ec) {
|
||||
template<class ELFT>
|
||||
DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
|
||||
: ELFObjectFile<ELFT>(Wrapper, ec) {
|
||||
this->isDyldELFObject = true;
|
||||
}
|
||||
|
||||
template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
|
||||
void DyldELFObject<target_endianness, max_alignment, is64Bits>
|
||||
::updateSectionAddress(const SectionRef &Sec, uint64_t Addr) {
|
||||
template<class ELFT>
|
||||
void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
|
||||
uint64_t Addr) {
|
||||
DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
|
||||
Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
|
||||
reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
|
||||
@ -138,13 +135,12 @@ void DyldELFObject<target_endianness, max_alignment, is64Bits>
|
||||
shdr->sh_addr = static_cast<addr_type>(Addr);
|
||||
}
|
||||
|
||||
template<endianness target_endianness, std::size_t max_align, bool is64Bits>
|
||||
void DyldELFObject<target_endianness, max_align, is64Bits>
|
||||
::updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr){
|
||||
template<class ELFT>
|
||||
void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
|
||||
uint64_t Addr) {
|
||||
|
||||
Elf_Sym *sym = const_cast<Elf_Sym*>(
|
||||
ELFObjectFile<target_endianness, max_align, is64Bits>
|
||||
::getSymbol(SymRef.getRawDataRefImpl()));
|
||||
ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
|
||||
|
||||
// This assumes the address passed in matches the target address bitness
|
||||
// The template-based type cast handles everything else.
|
||||
@ -164,24 +160,28 @@ ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
|
||||
error_code ec;
|
||||
|
||||
if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
|
||||
DyldELFObject<support::little, 4, false> *Obj =
|
||||
new DyldELFObject<support::little, 4, false>(Buffer->getMemBuffer(), ec);
|
||||
return new ELFObjectImage<support::little, 4, false>(Buffer, Obj);
|
||||
DyldELFObject<ELFType<support::little, 4, false> > *Obj =
|
||||
new DyldELFObject<ELFType<support::little, 4, false> >(
|
||||
Buffer->getMemBuffer(), ec);
|
||||
return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj);
|
||||
}
|
||||
else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
|
||||
DyldELFObject<support::big, 4, false> *Obj =
|
||||
new DyldELFObject<support::big, 4, false>(Buffer->getMemBuffer(), ec);
|
||||
return new ELFObjectImage<support::big, 4, false>(Buffer, Obj);
|
||||
DyldELFObject<ELFType<support::big, 4, false> > *Obj =
|
||||
new DyldELFObject<ELFType<support::big, 4, false> >(
|
||||
Buffer->getMemBuffer(), ec);
|
||||
return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj);
|
||||
}
|
||||
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
|
||||
DyldELFObject<support::big, 8, true> *Obj =
|
||||
new DyldELFObject<support::big, 8, true>(Buffer->getMemBuffer(), ec);
|
||||
return new ELFObjectImage<support::big, 8, true>(Buffer, Obj);
|
||||
DyldELFObject<ELFType<support::big, 8, true> > *Obj =
|
||||
new DyldELFObject<ELFType<support::big, 8, true> >(
|
||||
Buffer->getMemBuffer(), ec);
|
||||
return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj);
|
||||
}
|
||||
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
|
||||
DyldELFObject<support::little, 8, true> *Obj =
|
||||
new DyldELFObject<support::little, 8, true>(Buffer->getMemBuffer(), ec);
|
||||
return new ELFObjectImage<support::little, 8, true>(Buffer, Obj);
|
||||
DyldELFObject<ELFType<support::little, 8, true> > *Obj =
|
||||
new DyldELFObject<ELFType<support::little, 8, true> >(
|
||||
Buffer->getMemBuffer(), ec);
|
||||
return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj);
|
||||
}
|
||||
else
|
||||
llvm_unreachable("Unexpected ELF format");
|
||||
|
@ -28,30 +28,30 @@ ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
|
||||
|
||||
if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
|
||||
if (MaxAlignment >= 4)
|
||||
return new ELFObjectFile<support::little, 4, false>(Object, ec);
|
||||
return new ELFObjectFile<ELFType<support::little, 4, false> >(Object, ec);
|
||||
else if (MaxAlignment >= 2)
|
||||
return new ELFObjectFile<support::little, 2, false>(Object, ec);
|
||||
return new ELFObjectFile<ELFType<support::little, 2, false> >(Object, ec);
|
||||
else
|
||||
llvm_unreachable("Invalid alignment for ELF file!");
|
||||
else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
|
||||
if (MaxAlignment >= 4)
|
||||
return new ELFObjectFile<support::big, 4, false>(Object, ec);
|
||||
return new ELFObjectFile<ELFType<support::big, 4, false> >(Object, ec);
|
||||
else if (MaxAlignment >= 2)
|
||||
return new ELFObjectFile<support::big, 2, false>(Object, ec);
|
||||
return new ELFObjectFile<ELFType<support::big, 2, false> >(Object, ec);
|
||||
else
|
||||
llvm_unreachable("Invalid alignment for ELF file!");
|
||||
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
|
||||
if (MaxAlignment >= 8)
|
||||
return new ELFObjectFile<support::big, 8, true>(Object, ec);
|
||||
return new ELFObjectFile<ELFType<support::big, 8, true> >(Object, ec);
|
||||
else if (MaxAlignment >= 2)
|
||||
return new ELFObjectFile<support::big, 2, true>(Object, ec);
|
||||
return new ELFObjectFile<ELFType<support::big, 2, true> >(Object, ec);
|
||||
else
|
||||
llvm_unreachable("Invalid alignment for ELF file!");
|
||||
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
|
||||
if (MaxAlignment >= 8)
|
||||
return new ELFObjectFile<support::little, 8, true>(Object, ec);
|
||||
return new ELFObjectFile<ELFType<support::little, 8, true> >(Object, ec);
|
||||
else if (MaxAlignment >= 2)
|
||||
return new ELFObjectFile<support::little, 2, true>(Object, ec);
|
||||
return new ELFObjectFile<ELFType<support::little, 2, true> >(Object, ec);
|
||||
else
|
||||
llvm_unreachable("Invalid alignment for ELF file!");
|
||||
}
|
||||
|
@ -22,10 +22,10 @@
|
||||
using namespace llvm;
|
||||
using namespace llvm::object;
|
||||
|
||||
template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
|
||||
template<class ELFT>
|
||||
void printProgramHeaders(
|
||||
const ELFObjectFile<target_endianness, max_alignment, is64Bits> *o) {
|
||||
typedef ELFObjectFile<target_endianness, max_alignment, is64Bits> ELFO;
|
||||
const ELFObjectFile<ELFT> *o) {
|
||||
typedef ELFObjectFile<ELFT> ELFO;
|
||||
outs() << "Program Header:\n";
|
||||
for (typename ELFO::Elf_Phdr_Iter pi = o->begin_program_headers(),
|
||||
pe = o->end_program_headers();
|
||||
@ -44,7 +44,7 @@ void printProgramHeaders(
|
||||
outs() << " UNKNOWN ";
|
||||
}
|
||||
|
||||
const char *Fmt = is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
|
||||
const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
|
||||
|
||||
outs() << "off "
|
||||
<< format(Fmt, (uint64_t)pi->p_offset)
|
||||
@ -68,22 +68,22 @@ void printProgramHeaders(
|
||||
|
||||
void llvm::printELFFileHeader(const object::ObjectFile *Obj) {
|
||||
// Little-endian 32-bit
|
||||
if (const ELFObjectFile<support::little, 4, false> *ELFObj =
|
||||
dyn_cast<ELFObjectFile<support::little, 4, false> >(Obj))
|
||||
if (const ELFObjectFile<ELFType<support::little, 4, false> > *ELFObj =
|
||||
dyn_cast<ELFObjectFile<ELFType<support::little, 4, false> > >(Obj))
|
||||
printProgramHeaders(ELFObj);
|
||||
|
||||
// Big-endian 32-bit
|
||||
if (const ELFObjectFile<support::big, 4, false> *ELFObj =
|
||||
dyn_cast<ELFObjectFile<support::big, 4, false> >(Obj))
|
||||
if (const ELFObjectFile<ELFType<support::big, 4, false> > *ELFObj =
|
||||
dyn_cast<ELFObjectFile<ELFType<support::big, 4, false> > >(Obj))
|
||||
printProgramHeaders(ELFObj);
|
||||
|
||||
// Little-endian 64-bit
|
||||
if (const ELFObjectFile<support::little, 8, true> *ELFObj =
|
||||
dyn_cast<ELFObjectFile<support::little, 8, true> >(Obj))
|
||||
if (const ELFObjectFile<ELFType<support::little, 8, true> > *ELFObj =
|
||||
dyn_cast<ELFObjectFile<ELFType<support::little, 8, true> > >(Obj))
|
||||
printProgramHeaders(ELFObj);
|
||||
|
||||
// Big-endian 64-bit
|
||||
if (const ELFObjectFile<support::big, 8, true> *ELFObj =
|
||||
dyn_cast<ELFObjectFile<support::big, 8, true> >(Obj))
|
||||
if (const ELFObjectFile<ELFType<support::big, 8, true> > *ELFObj =
|
||||
dyn_cast<ELFObjectFile<ELFType<support::big, 8, true> > >(Obj))
|
||||
printProgramHeaders(ELFObj);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user