base: style cleanup for ObjectFile and ElfObject
This commit is contained in:
parent
d7c083864c
commit
de8077763e
|
@ -60,196 +60,175 @@
|
||||||
#include "gelf.h"
|
#include "gelf.h"
|
||||||
#include "sim/byteswap.hh"
|
#include "sim/byteswap.hh"
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
ObjectFile *
|
ObjectFile *
|
||||||
ElfObject::tryFile(const string &fname, size_t len, uint8_t *data,
|
ElfObject::tryFile(const std::string &fname, size_t len, uint8_t *data,
|
||||||
bool skip_interp_check)
|
bool skip_interp_check)
|
||||||
{
|
{
|
||||||
Elf *elf;
|
|
||||||
GElf_Ehdr ehdr;
|
|
||||||
Arch arch = UnknownArch;
|
|
||||||
OpSys opSys = UnknownOpSys;
|
|
||||||
|
|
||||||
// check that header matches library version
|
// check that header matches library version
|
||||||
if (elf_version(EV_CURRENT) == EV_NONE)
|
if (elf_version(EV_CURRENT) == EV_NONE)
|
||||||
panic("wrong elf version number!");
|
panic("wrong elf version number!");
|
||||||
|
|
||||||
// get a pointer to elf structure
|
// get a pointer to elf structure
|
||||||
elf = elf_memory((char*)data,len);
|
|
||||||
assert(elf != NULL);
|
|
||||||
|
|
||||||
// Check that we actually have a elf file
|
// Check that we actually have a elf file
|
||||||
|
Elf *elf = elf_memory((char*)data, len);
|
||||||
|
assert(elf);
|
||||||
|
|
||||||
|
GElf_Ehdr ehdr;
|
||||||
if (gelf_getehdr(elf, &ehdr) == 0) {
|
if (gelf_getehdr(elf, &ehdr) == 0) {
|
||||||
DPRINTFR(Loader, "Not ELF\n");
|
DPRINTFR(Loader, "Not ELF\n");
|
||||||
elf_end(elf);
|
elf_end(elf);
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
}
|
||||||
//Detect the architecture
|
|
||||||
//Since we don't know how to check for alpha right now, we'll
|
// Detect the architecture
|
||||||
//just assume if it wasn't something else and it's 64 bit, that's
|
Arch arch = UnknownArch;
|
||||||
//what it must be.
|
|
||||||
if (ehdr.e_machine == EM_SPARC64 ||
|
if (ehdr.e_machine == EM_SPARC64 ||
|
||||||
(ehdr.e_machine == EM_SPARC &&
|
(ehdr.e_machine == EM_SPARC &&
|
||||||
ehdr.e_ident[EI_CLASS] == ELFCLASS64)||
|
ehdr.e_ident[EI_CLASS] == ELFCLASS64) ||
|
||||||
ehdr.e_machine == EM_SPARCV9) {
|
ehdr.e_machine == EM_SPARCV9) {
|
||||||
arch = ObjectFile::SPARC64;
|
arch = SPARC64;
|
||||||
} else if (ehdr.e_machine == EM_SPARC32PLUS ||
|
} else if (ehdr.e_machine == EM_SPARC32PLUS ||
|
||||||
(ehdr.e_machine == EM_SPARC &&
|
(ehdr.e_machine == EM_SPARC &&
|
||||||
ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
|
ehdr.e_ident[EI_CLASS] == ELFCLASS32)) {
|
||||||
arch = ObjectFile::SPARC32;
|
arch = SPARC32;
|
||||||
} else if (ehdr.e_machine == EM_MIPS
|
} else if (ehdr.e_machine == EM_MIPS &&
|
||||||
&& ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
|
ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
|
||||||
if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
|
arch = Mips;
|
||||||
arch = ObjectFile::Mips;
|
if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
|
||||||
} else {
|
|
||||||
fatal("The binary you're trying to load is compiled for big "
|
fatal("The binary you're trying to load is compiled for big "
|
||||||
"endian MIPS. M5\nonly supports little endian MIPS. "
|
"endian MIPS. gem5\nonly supports little endian MIPS. "
|
||||||
"Please recompile your binary.\n");
|
"Please recompile your binary.\n");
|
||||||
}
|
}
|
||||||
} else if (ehdr.e_machine == EM_X86_64 &&
|
} else if (ehdr.e_machine == EM_X86_64 &&
|
||||||
ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
|
ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
|
||||||
arch = ObjectFile::X86_64;
|
arch = X86_64;
|
||||||
} else if (ehdr.e_machine == EM_386 &&
|
} else if (ehdr.e_machine == EM_386 &&
|
||||||
ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
|
ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
|
||||||
arch = ObjectFile::I386;
|
arch = I386;
|
||||||
} else if (ehdr.e_machine == EM_ARM &&
|
} else if (ehdr.e_machine == EM_ARM &&
|
||||||
ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
|
ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
|
||||||
if (bits(ehdr.e_entry, 0)) {
|
arch = bits(ehdr.e_entry, 0) ? Thumb : Arm;
|
||||||
arch = ObjectFile::Thumb;
|
} else if (ehdr.e_machine == EM_AARCH64 &&
|
||||||
} else {
|
|
||||||
arch = ObjectFile::Arm;
|
|
||||||
}
|
|
||||||
} else if ((ehdr.e_machine == EM_AARCH64) &&
|
|
||||||
ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
|
ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
|
||||||
arch = ObjectFile::Arm64;
|
arch = Arm64;
|
||||||
} else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
|
|
||||||
arch = ObjectFile::Alpha;
|
|
||||||
} else if (ehdr.e_machine == EM_PPC &&
|
} else if (ehdr.e_machine == EM_PPC &&
|
||||||
ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
|
ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
|
||||||
if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
|
arch = Power;
|
||||||
arch = ObjectFile::Power;
|
if (ehdr.e_ident[EI_DATA] != ELFDATA2MSB) {
|
||||||
} else {
|
|
||||||
fatal("The binary you're trying to load is compiled for "
|
fatal("The binary you're trying to load is compiled for "
|
||||||
"little endian Power.\nM5 only supports big "
|
"little endian Power.\ngem5 only supports big "
|
||||||
"endian Power. Please recompile your binary.\n");
|
"endian Power. Please recompile your binary.\n");
|
||||||
}
|
}
|
||||||
} else if (ehdr.e_machine == EM_PPC64) {
|
} else if (ehdr.e_machine == EM_PPC64) {
|
||||||
fatal("The binary you're trying to load is compiled for 64-bit "
|
fatal("The binary you're trying to load is compiled for 64-bit "
|
||||||
"Power. M5\n only supports 32-bit Power. Please "
|
"Power. M5\n only supports 32-bit Power. Please "
|
||||||
"recompile your binary.\n");
|
"recompile your binary.\n");
|
||||||
|
} else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
|
||||||
|
// Since we don't know how to check for alpha right now, we'll
|
||||||
|
// just assume if it wasn't something else and it's 64 bit, that's
|
||||||
|
// what it must be.
|
||||||
|
arch = Alpha;
|
||||||
} else {
|
} else {
|
||||||
warn("Unknown architecture: %d\n", ehdr.e_machine);
|
warn("Unknown architecture: %d\n", ehdr.e_machine);
|
||||||
arch = ObjectFile::UnknownArch;
|
arch = UnknownArch;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Detect the operating system
|
// Detect the operating system
|
||||||
|
OpSys op_sys;
|
||||||
switch (ehdr.e_ident[EI_OSABI]) {
|
switch (ehdr.e_ident[EI_OSABI]) {
|
||||||
case ELFOSABI_LINUX:
|
case ELFOSABI_LINUX:
|
||||||
opSys = ObjectFile::Linux;
|
op_sys = Linux;
|
||||||
break;
|
break;
|
||||||
case ELFOSABI_SOLARIS:
|
case ELFOSABI_SOLARIS:
|
||||||
opSys = ObjectFile::Solaris;
|
op_sys = Solaris;
|
||||||
break;
|
break;
|
||||||
case ELFOSABI_TRU64:
|
case ELFOSABI_TRU64:
|
||||||
opSys = ObjectFile::Tru64;
|
op_sys = Tru64;
|
||||||
break;
|
break;
|
||||||
case ELFOSABI_ARM:
|
case ELFOSABI_ARM:
|
||||||
opSys = ObjectFile::LinuxArmOABI;
|
op_sys = LinuxArmOABI;
|
||||||
break;
|
break;
|
||||||
case ELFOSABI_FREEBSD:
|
case ELFOSABI_FREEBSD:
|
||||||
opSys = ObjectFile::FreeBSD;
|
op_sys = FreeBSD;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
opSys = ObjectFile::UnknownOpSys;
|
op_sys = UnknownOpSys;
|
||||||
}
|
}
|
||||||
|
|
||||||
//take a look at the .note.ABI section
|
// Take a look at the .note.ABI section.
|
||||||
//It can let us know what's what.
|
// It can let us know what's what.
|
||||||
if (opSys == ObjectFile::UnknownOpSys) {
|
if (op_sys == UnknownOpSys) {
|
||||||
Elf_Scn *section;
|
int sec_idx = 1;
|
||||||
GElf_Shdr shdr;
|
|
||||||
Elf_Data *data;
|
|
||||||
uint32_t osAbi;
|
|
||||||
uint32_t *elem;
|
|
||||||
int secIdx = 1;
|
|
||||||
|
|
||||||
// Get the first section
|
// Get the first section
|
||||||
section = elf_getscn(elf, secIdx);
|
Elf_Scn *section = elf_getscn(elf, sec_idx);
|
||||||
|
|
||||||
// While there are no more sections
|
// While there are no more sections
|
||||||
while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
|
while (section && op_sys == UnknownOpSys) {
|
||||||
|
GElf_Shdr shdr;
|
||||||
gelf_getshdr(section, &shdr);
|
gelf_getshdr(section, &shdr);
|
||||||
if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
|
|
||||||
elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
|
char *e_str = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
|
||||||
|
if (shdr.sh_type == SHT_NOTE &&
|
||||||
|
!strcmp(".note.ABI-tag", e_str)) {
|
||||||
// we have found a ABI note section
|
// we have found a ABI note section
|
||||||
// Check the 5th 32bit word for OS 0 == linux, 1 == hurd,
|
// Check the 5th 32bit word for OS 0 == linux, 1 == hurd,
|
||||||
// 2 == solaris, 3 == freebsd
|
// 2 == solaris, 3 == freebsd
|
||||||
data = elf_rawdata(section, NULL);
|
Elf_Data *raw_data = elf_rawdata(section, NULL);
|
||||||
assert(data->d_buf);
|
assert(raw_data && raw_data->d_buf);
|
||||||
if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
|
|
||||||
osAbi = htole(((uint32_t*)data->d_buf)[4]);
|
|
||||||
else
|
|
||||||
osAbi = htobe(((uint32_t*)data->d_buf)[4]);
|
|
||||||
|
|
||||||
switch(osAbi) {
|
uint32_t raw_abi = ((uint32_t*)raw_data->d_buf)[4];
|
||||||
|
bool is_le = ehdr.e_ident[EI_DATA] == ELFDATA2LSB;
|
||||||
|
uint32_t os_abi = is_le ? htole(raw_abi) : htobe(raw_abi);
|
||||||
|
|
||||||
|
switch (os_abi) {
|
||||||
case 0:
|
case 0:
|
||||||
opSys = ObjectFile::Linux;
|
op_sys = Linux;
|
||||||
break;
|
break;
|
||||||
|
case 1:
|
||||||
|
fatal("gem5 does not support the HURD ABI.\n");
|
||||||
case 2:
|
case 2:
|
||||||
opSys = ObjectFile::Solaris;
|
op_sys = Solaris;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
op_sys = FreeBSD;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} // if section found
|
} // if section found
|
||||||
if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
|
|
||||||
opSys = ObjectFile::Solaris;
|
|
||||||
if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
|
|
||||||
opSys = ObjectFile::Solaris;
|
|
||||||
if (shdr.sh_type == SHT_NOTE && !strcmp(".note.tag",
|
|
||||||
elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
|
|
||||||
data = elf_rawdata(section, NULL);
|
|
||||||
assert(data->d_buf);
|
|
||||||
elem = (uint32_t *)data->d_buf;
|
|
||||||
if (elem[0] == 0x8) { //size of name
|
|
||||||
if (memcmp((void *)&elem[3], "FreeBSD", 0x8) == 0)
|
|
||||||
opSys = ObjectFile::FreeBSD;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
section = elf_getscn(elf, ++secIdx);
|
if (!strcmp(".SUNW_version", e_str) ||
|
||||||
|
!strcmp(".stab.index", e_str))
|
||||||
|
op_sys = Solaris;
|
||||||
|
|
||||||
|
section = elf_getscn(elf, ++sec_idx);
|
||||||
} // while sections
|
} // while sections
|
||||||
}
|
}
|
||||||
|
|
||||||
ElfObject * result = new ElfObject(fname, len, data, arch, opSys);
|
ElfObject * result = new ElfObject(fname, len, data, arch, op_sys);
|
||||||
|
|
||||||
//The number of headers in the file
|
// The number of headers in the file
|
||||||
result->_programHeaderCount = ehdr.e_phnum;
|
result->_programHeaderCount = ehdr.e_phnum;
|
||||||
//Record the size of each entry
|
// Record the size of each entry
|
||||||
result->_programHeaderSize = ehdr.e_phentsize;
|
result->_programHeaderSize = ehdr.e_phentsize;
|
||||||
if (result->_programHeaderCount) //If there is a program header table
|
result->_programHeaderTable = 0;
|
||||||
{
|
if (result->_programHeaderCount) { // If there is a program header table
|
||||||
//Figure out the virtual address of the header table in the
|
// Figure out the virtual address of the header table in the
|
||||||
//final memory image. We use the program headers themselves
|
// final memory image. We use the program headers themselves
|
||||||
//to translate from a file offset to the address in the image.
|
// to translate from a file offset to the address in the image.
|
||||||
GElf_Phdr phdr;
|
GElf_Phdr phdr;
|
||||||
uint64_t e_phoff = ehdr.e_phoff;
|
uint64_t e_phoff = ehdr.e_phoff;
|
||||||
result->_programHeaderTable = 0;
|
|
||||||
for (int hdrnum = 0; hdrnum < result->_programHeaderCount; hdrnum++)
|
for (int i = 0; i < result->_programHeaderCount; i++) {
|
||||||
{
|
gelf_getphdr(elf, i, &phdr);
|
||||||
gelf_getphdr(elf, hdrnum, &phdr);
|
// Check if we've found the segment with the headers in it
|
||||||
//Check if we've found the segment with the headers in it
|
|
||||||
if (phdr.p_offset <= e_phoff &&
|
if (phdr.p_offset <= e_phoff &&
|
||||||
phdr.p_offset + phdr.p_filesz > e_phoff)
|
phdr.p_offset + phdr.p_filesz > e_phoff) {
|
||||||
{
|
|
||||||
result->_programHeaderTable =
|
result->_programHeaderTable =
|
||||||
phdr.p_paddr + (e_phoff - phdr.p_offset);
|
phdr.p_paddr + (e_phoff - phdr.p_offset);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
result->_programHeaderTable = 0;
|
|
||||||
|
|
||||||
|
|
||||||
if (!skip_interp_check) {
|
if (!skip_interp_check) {
|
||||||
for (int i = 0; i < ehdr.e_phnum; i++) {
|
for (int i = 0; i < ehdr.e_phnum; i++) {
|
||||||
|
@ -262,10 +241,8 @@ ElfObject::tryFile(const string &fname, size_t len, uint8_t *data,
|
||||||
|
|
||||||
char *interp_path = (char*)data + phdr.p_offset;
|
char *interp_path = (char*)data + phdr.p_offset;
|
||||||
int fd = open(interp_path, O_RDONLY);
|
int fd = open(interp_path, O_RDONLY);
|
||||||
if (fd == -1) {
|
if (fd == -1)
|
||||||
fatal("Unable to open dynamic executable's "
|
fatal("Unable to open dynamic executable's interpreter.\n");
|
||||||
"interpreter.\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
M5_VAR_USED int check_i = fstat(fd, &sb);
|
M5_VAR_USED int check_i = fstat(fd, &sb);
|
||||||
|
@ -274,7 +251,7 @@ ElfObject::tryFile(const string &fname, size_t len, uint8_t *data,
|
||||||
void *mm = mmap(nullptr, sb.st_size, PROT_READ,
|
void *mm = mmap(nullptr, sb.st_size, PROT_READ,
|
||||||
MAP_PRIVATE, fd, 0);
|
MAP_PRIVATE, fd, 0);
|
||||||
assert(mm != MAP_FAILED);
|
assert(mm != MAP_FAILED);
|
||||||
::close(fd);
|
close(fd);
|
||||||
|
|
||||||
uint8_t *interp_image = (uint8_t*)mm;
|
uint8_t *interp_image = (uint8_t*)mm;
|
||||||
ObjectFile *obj = tryFile(interp_path, sb.st_size,
|
ObjectFile *obj = tryFile(interp_path, sb.st_size,
|
||||||
|
@ -288,30 +265,26 @@ ElfObject::tryFile(const string &fname, size_t len, uint8_t *data,
|
||||||
|
|
||||||
elf_end(elf);
|
elf_end(elf);
|
||||||
return result;
|
return result;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ElfObject::ElfObject(const std::string &_filename, size_t _len,
|
||||||
ElfObject::ElfObject(const string &_filename, size_t _len, uint8_t *_data,
|
uint8_t *_data, Arch _arch, OpSys _op_sys)
|
||||||
Arch _arch, OpSys _opSys)
|
: ObjectFile(_filename, _len, _data, _arch, _op_sys),
|
||||||
: ObjectFile(_filename, _len, _data, _arch, _opSys),
|
|
||||||
_programHeaderTable(0), _programHeaderSize(0), _programHeaderCount(0),
|
_programHeaderTable(0), _programHeaderSize(0), _programHeaderCount(0),
|
||||||
interpreter(nullptr), ldBias(0), relocate(true),
|
interpreter(nullptr), ldBias(0), relocate(true),
|
||||||
ldMin(std::numeric_limits<Addr>::max()),
|
ldMin(std::numeric_limits<Addr>::max()),
|
||||||
ldMax(std::numeric_limits<Addr>::min())
|
ldMax(std::numeric_limits<Addr>::min())
|
||||||
{
|
{
|
||||||
Elf *elf;
|
|
||||||
GElf_Ehdr ehdr;
|
|
||||||
|
|
||||||
// check that header matches library version
|
// check that header matches library version
|
||||||
if (elf_version(EV_CURRENT) == EV_NONE)
|
if (elf_version(EV_CURRENT) == EV_NONE)
|
||||||
panic("wrong elf version number!");
|
panic("wrong elf version number!");
|
||||||
|
|
||||||
// get a pointer to elf structure
|
// get a pointer to elf structure
|
||||||
elf = elf_memory((char*)fileData,len);
|
Elf *elf = elf_memory((char*)fileData,len);
|
||||||
assert(elf != NULL);
|
assert(elf);
|
||||||
|
|
||||||
// Check that we actually have a elf file
|
// Check that we actually have a elf file
|
||||||
|
GElf_Ehdr ehdr;
|
||||||
if (gelf_getehdr(elf, &ehdr) ==0) {
|
if (gelf_getehdr(elf, &ehdr) ==0) {
|
||||||
panic("Not ELF, shouldn't be here");
|
panic("Not ELF, shouldn't be here");
|
||||||
}
|
}
|
||||||
|
@ -322,30 +295,29 @@ ElfObject::ElfObject(const string &_filename, size_t _len, uint8_t *_data,
|
||||||
text.size = data.size = bss.size = 0;
|
text.size = data.size = bss.size = 0;
|
||||||
text.baseAddr = data.baseAddr = bss.baseAddr = 0;
|
text.baseAddr = data.baseAddr = bss.baseAddr = 0;
|
||||||
|
|
||||||
int secIdx = 1;
|
int sec_idx = 1;
|
||||||
Elf_Scn *section;
|
|
||||||
GElf_Shdr shdr;
|
|
||||||
|
|
||||||
// The first address of some important sections.
|
// The first address of some important sections.
|
||||||
Addr textSecStart = 0;
|
Addr text_sec_start = 0;
|
||||||
Addr dataSecStart = 0;
|
Addr data_sec_start = 0;
|
||||||
Addr bssSecStart = 0;
|
Addr bss_sec_start = 0;
|
||||||
|
|
||||||
// Get the first section
|
// Get the first section
|
||||||
section = elf_getscn(elf, secIdx);
|
Elf_Scn *section = elf_getscn(elf, sec_idx);
|
||||||
|
|
||||||
// Find the beginning of the most interesting sections.
|
// Find the beginning of the most interesting sections.
|
||||||
while (section != NULL) {
|
while (section) {
|
||||||
|
GElf_Shdr shdr;
|
||||||
gelf_getshdr(section, &shdr);
|
gelf_getshdr(section, &shdr);
|
||||||
char * secName = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
|
char *sec_name = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
|
||||||
|
|
||||||
if (secName) {
|
if (sec_name) {
|
||||||
if (!strcmp(".text", secName)) {
|
if (!strcmp(".text", sec_name)) {
|
||||||
textSecStart = shdr.sh_addr;
|
text_sec_start = shdr.sh_addr;
|
||||||
} else if (!strcmp(".data", secName)) {
|
} else if (!strcmp(".data", sec_name)) {
|
||||||
dataSecStart = shdr.sh_addr;
|
data_sec_start = shdr.sh_addr;
|
||||||
} else if (!strcmp(".bss", secName)) {
|
} else if (!strcmp(".bss", sec_name)) {
|
||||||
bssSecStart = shdr.sh_addr;
|
bss_sec_start = shdr.sh_addr;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Elf_Error errorNum = (Elf_Error)elf_errno();
|
Elf_Error errorNum = (Elf_Error)elf_errno();
|
||||||
|
@ -355,7 +327,7 @@ ElfObject::ElfObject(const string &_filename, size_t _len, uint8_t *_data,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
section = elf_getscn(elf, ++secIdx);
|
section = elf_getscn(elf, ++sec_idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go through all the segments in the program, record them, and scrape
|
// Go through all the segments in the program, record them, and scrape
|
||||||
|
@ -375,8 +347,8 @@ ElfObject::ElfObject(const string &_filename, size_t _len, uint8_t *_data,
|
||||||
ldMax = std::max(ldMax, phdr.p_vaddr + phdr.p_memsz);
|
ldMax = std::max(ldMax, phdr.p_vaddr + phdr.p_memsz);
|
||||||
|
|
||||||
// Check to see if this segment contains the bss section.
|
// Check to see if this segment contains the bss section.
|
||||||
if (phdr.p_paddr <= bssSecStart &&
|
if (phdr.p_paddr <= bss_sec_start &&
|
||||||
phdr.p_paddr + phdr.p_memsz > bssSecStart &&
|
phdr.p_paddr + phdr.p_memsz > bss_sec_start &&
|
||||||
phdr.p_memsz - phdr.p_filesz > 0) {
|
phdr.p_memsz - phdr.p_filesz > 0) {
|
||||||
bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
|
bss.baseAddr = phdr.p_paddr + phdr.p_filesz;
|
||||||
bss.size = phdr.p_memsz - phdr.p_filesz;
|
bss.size = phdr.p_memsz - phdr.p_filesz;
|
||||||
|
@ -384,8 +356,8 @@ ElfObject::ElfObject(const string &_filename, size_t _len, uint8_t *_data,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check to see if this is the text or data segment
|
// Check to see if this is the text or data segment
|
||||||
if (phdr.p_vaddr <= textSecStart &&
|
if (phdr.p_vaddr <= text_sec_start &&
|
||||||
phdr.p_vaddr + phdr.p_filesz > textSecStart) {
|
phdr.p_vaddr + phdr.p_filesz > text_sec_start) {
|
||||||
|
|
||||||
// If this value is nonzero, we need to flip the relocate flag.
|
// If this value is nonzero, we need to flip the relocate flag.
|
||||||
if (phdr.p_vaddr != 0)
|
if (phdr.p_vaddr != 0)
|
||||||
|
@ -394,8 +366,8 @@ ElfObject::ElfObject(const string &_filename, size_t _len, uint8_t *_data,
|
||||||
text.baseAddr = phdr.p_paddr;
|
text.baseAddr = phdr.p_paddr;
|
||||||
text.size = phdr.p_filesz;
|
text.size = phdr.p_filesz;
|
||||||
text.fileImage = fileData + phdr.p_offset;
|
text.fileImage = fileData + phdr.p_offset;
|
||||||
} else if (phdr.p_vaddr <= dataSecStart &&
|
} else if (phdr.p_vaddr <= data_sec_start &&
|
||||||
phdr.p_vaddr + phdr.p_filesz > dataSecStart) {
|
phdr.p_vaddr + phdr.p_filesz > data_sec_start) {
|
||||||
data.baseAddr = phdr.p_paddr;
|
data.baseAddr = phdr.p_paddr;
|
||||||
data.size = phdr.p_filesz;
|
data.size = phdr.p_filesz;
|
||||||
data.fileImage = fileData + phdr.p_offset;
|
data.fileImage = fileData + phdr.p_offset;
|
||||||
|
@ -426,15 +398,6 @@ ElfObject::ElfObject(const string &_filename, size_t _len, uint8_t *_data,
|
||||||
bool
|
bool
|
||||||
ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
|
ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
|
||||||
{
|
{
|
||||||
Elf *elf;
|
|
||||||
int sec_idx = 1; // there is a 0 but it is nothing, go figure
|
|
||||||
Elf_Scn *section;
|
|
||||||
GElf_Shdr shdr;
|
|
||||||
Elf_Data *data;
|
|
||||||
int count, ii;
|
|
||||||
bool found = false;
|
|
||||||
GElf_Sym sym;
|
|
||||||
|
|
||||||
if (!symtab)
|
if (!symtab)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -443,26 +406,29 @@ ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
|
||||||
panic("wrong elf version number!");
|
panic("wrong elf version number!");
|
||||||
|
|
||||||
// get a pointer to elf structure
|
// get a pointer to elf structure
|
||||||
elf = elf_memory((char*)fileData,len);
|
Elf *elf = elf_memory((char*)fileData,len);
|
||||||
|
|
||||||
assert(elf != NULL);
|
assert(elf != NULL);
|
||||||
|
|
||||||
// Get the first section
|
// Get the first section
|
||||||
section = elf_getscn(elf, sec_idx);
|
int sec_idx = 1; // there is a 0 but it is nothing, go figure
|
||||||
|
Elf_Scn *section = elf_getscn(elf, sec_idx);
|
||||||
|
|
||||||
// While there are no more sections
|
// While there are no more sections
|
||||||
|
bool found = false;
|
||||||
while (section != NULL) {
|
while (section != NULL) {
|
||||||
|
GElf_Shdr shdr;
|
||||||
gelf_getshdr(section, &shdr);
|
gelf_getshdr(section, &shdr);
|
||||||
|
|
||||||
if (shdr.sh_type == SHT_SYMTAB) {
|
if (shdr.sh_type == SHT_SYMTAB) {
|
||||||
found = true;
|
found = true;
|
||||||
data = elf_getdata(section, NULL);
|
Elf_Data *data = elf_getdata(section, NULL);
|
||||||
count = shdr.sh_size / shdr.sh_entsize;
|
int count = shdr.sh_size / shdr.sh_entsize;
|
||||||
DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
|
DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
|
||||||
|
|
||||||
// loop through all the symbols, only loading global ones
|
// loop through all the symbols, only loading global ones
|
||||||
for (ii = 0; ii < count; ++ii) {
|
for (int i = 0; i < count; ++i) {
|
||||||
gelf_getsym(data, ii, &sym);
|
GElf_Sym sym;
|
||||||
|
gelf_getsym(data, i, &sym);
|
||||||
if (GELF_ST_BIND(sym.st_info) == binding) {
|
if (GELF_ST_BIND(sym.st_info) == binding) {
|
||||||
char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name);
|
char *sym_name = elf_strptr(elf, shdr.sh_link, sym.st_name);
|
||||||
if (sym_name && sym_name[0] != '$') {
|
if (sym_name && sym_name[0] != '$') {
|
||||||
|
@ -483,41 +449,39 @@ ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addrMask)
|
ElfObject::loadGlobalSymbols(SymbolTable *symtab, Addr addr_mask)
|
||||||
{
|
{
|
||||||
return loadSomeSymbols(symtab, STB_GLOBAL, addrMask);
|
return loadSomeSymbols(symtab, STB_GLOBAL, addr_mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addrMask)
|
ElfObject::loadLocalSymbols(SymbolTable *symtab, Addr addr_mask)
|
||||||
{
|
{
|
||||||
bool found_local = loadSomeSymbols(symtab, STB_LOCAL, addrMask);
|
bool found_local = loadSomeSymbols(symtab, STB_LOCAL, addr_mask);
|
||||||
bool found_weak = loadSomeSymbols(symtab, STB_WEAK, addrMask);
|
bool found_weak = loadSomeSymbols(symtab, STB_WEAK, addr_mask);
|
||||||
return found_local || found_weak;
|
return found_local || found_weak;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ElfObject::loadWeakSymbols(SymbolTable *symtab, Addr addrMask)
|
ElfObject::loadWeakSymbols(SymbolTable *symtab, Addr addr_mask)
|
||||||
{
|
{
|
||||||
return loadSomeSymbols(symtab, STB_WEAK, addrMask);
|
return loadSomeSymbols(symtab, STB_WEAK, addr_mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ElfObject::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset)
|
ElfObject::loadSections(PortProxy& mem_proxy, Addr addr_mask, Addr offset)
|
||||||
{
|
{
|
||||||
if (!ObjectFile::loadSections(memProxy, addrMask, offset))
|
if (!ObjectFile::loadSections(mem_proxy, addr_mask, offset))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
vector<Segment>::iterator extraIt;
|
for (auto seg : extraSegments) {
|
||||||
for (extraIt = extraSegments.begin();
|
if (!loadSection(&seg, mem_proxy, addr_mask, offset)) {
|
||||||
extraIt != extraSegments.end(); extraIt++) {
|
|
||||||
if (!loadSection(&(*extraIt), memProxy, addrMask, offset)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (interpreter)
|
if (interpreter)
|
||||||
interpreter->loadSections(memProxy, addrMask, offset);
|
interpreter->loadSections(mem_proxy, addr_mask, offset);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -525,13 +489,6 @@ ElfObject::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset)
|
||||||
void
|
void
|
||||||
ElfObject::getSections()
|
ElfObject::getSections()
|
||||||
{
|
{
|
||||||
Elf *elf;
|
|
||||||
int sec_idx = 1; // there is a 0 but it is nothing, go figure
|
|
||||||
Elf_Scn *section;
|
|
||||||
GElf_Shdr shdr;
|
|
||||||
|
|
||||||
GElf_Ehdr ehdr;
|
|
||||||
|
|
||||||
assert(!sectionNames.size());
|
assert(!sectionNames.size());
|
||||||
|
|
||||||
// check that header matches library version
|
// check that header matches library version
|
||||||
|
@ -539,19 +496,22 @@ ElfObject::getSections()
|
||||||
panic("wrong elf version number!");
|
panic("wrong elf version number!");
|
||||||
|
|
||||||
// get a pointer to elf structure
|
// get a pointer to elf structure
|
||||||
elf = elf_memory((char*)fileData,len);
|
Elf *elf = elf_memory((char*)fileData,len);
|
||||||
assert(elf != NULL);
|
assert(elf != NULL);
|
||||||
|
|
||||||
// Check that we actually have a elf file
|
// Check that we actually have a elf file
|
||||||
|
GElf_Ehdr ehdr;
|
||||||
if (gelf_getehdr(elf, &ehdr) ==0) {
|
if (gelf_getehdr(elf, &ehdr) ==0) {
|
||||||
panic("Not ELF, shouldn't be here");
|
panic("Not ELF, shouldn't be here");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the first section
|
// Get the first section
|
||||||
section = elf_getscn(elf, sec_idx);
|
int sec_idx = 1; // there is a 0 but it is nothing, go figure
|
||||||
|
Elf_Scn *section = elf_getscn(elf, sec_idx);
|
||||||
|
|
||||||
// While there are no more sections
|
// While there are no more sections
|
||||||
while (section != NULL) {
|
while (section) {
|
||||||
|
GElf_Shdr shdr;
|
||||||
gelf_getshdr(section, &shdr);
|
gelf_getshdr(section, &shdr);
|
||||||
sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
|
sectionNames.insert(elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
|
||||||
section = elf_getscn(elf, ++sec_idx);
|
section = elf_getscn(elf, ++sec_idx);
|
||||||
|
@ -559,10 +519,11 @@ ElfObject::getSections()
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ElfObject::sectionExists(string sec)
|
ElfObject::sectionExists(std::string sec)
|
||||||
{
|
{
|
||||||
if (!sectionNames.size())
|
if (!sectionNames.size())
|
||||||
getSections();
|
getSections();
|
||||||
|
|
||||||
return sectionNames.find(sec) != sectionNames.end();
|
return sectionNames.find(sec) != sectionNames.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,9 +53,9 @@ using namespace std;
|
||||||
|
|
||||||
ObjectFile::ObjectFile(const string &_filename,
|
ObjectFile::ObjectFile(const string &_filename,
|
||||||
size_t _len, uint8_t *_data,
|
size_t _len, uint8_t *_data,
|
||||||
Arch _arch, OpSys _opSys)
|
Arch _arch, OpSys _op_sys)
|
||||||
: filename(_filename), fileData(_data), len(_len),
|
: filename(_filename), fileData(_data), len(_len),
|
||||||
arch(_arch), opSys(_opSys), entry(0), globalPtr(0),
|
arch(_arch), opSys(_op_sys), entry(0), globalPtr(0),
|
||||||
text{0, nullptr, 0}, data{0, nullptr, 0}, bss{0, nullptr, 0}
|
text{0, nullptr, 0}, data{0, nullptr, 0}, bss{0, nullptr, 0}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -63,21 +63,25 @@ ObjectFile::ObjectFile(const string &_filename,
|
||||||
|
|
||||||
ObjectFile::~ObjectFile()
|
ObjectFile::~ObjectFile()
|
||||||
{
|
{
|
||||||
close();
|
if (fileData) {
|
||||||
|
::munmap((char*)fileData, len);
|
||||||
|
fileData = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ObjectFile::loadSection(Section *sec, PortProxy& memProxy, Addr addrMask, Addr offset)
|
ObjectFile::loadSection(Section *sec, PortProxy& mem_proxy, Addr addr_mask,
|
||||||
|
Addr offset)
|
||||||
{
|
{
|
||||||
if (sec->size != 0) {
|
if (sec->size != 0) {
|
||||||
Addr addr = (sec->baseAddr & addrMask) + offset;
|
Addr addr = (sec->baseAddr & addr_mask) + offset;
|
||||||
if (sec->fileImage) {
|
if (sec->fileImage) {
|
||||||
memProxy.writeBlob(addr, sec->fileImage, sec->size);
|
mem_proxy.writeBlob(addr, sec->fileImage, sec->size);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// no image: must be bss
|
// no image: must be bss
|
||||||
memProxy.memsetBlob(addr, 0, sec->size);
|
mem_proxy.memsetBlob(addr, 0, sec->size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -85,21 +89,11 @@ ObjectFile::loadSection(Section *sec, PortProxy& memProxy, Addr addrMask, Addr o
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ObjectFile::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset)
|
ObjectFile::loadSections(PortProxy& mem_proxy, Addr addr_mask, Addr offset)
|
||||||
{
|
{
|
||||||
return (loadSection(&text, memProxy, addrMask, offset)
|
return (loadSection(&text, mem_proxy, addr_mask, offset)
|
||||||
&& loadSection(&data, memProxy, addrMask, offset)
|
&& loadSection(&data, mem_proxy, addr_mask, offset)
|
||||||
&& loadSection(&bss, memProxy, addrMask, offset));
|
&& loadSection(&bss, mem_proxy, addr_mask, offset));
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
ObjectFile::close()
|
|
||||||
{
|
|
||||||
if (fileData) {
|
|
||||||
::munmap((char*)fileData, len);
|
|
||||||
fileData = NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
|
@ -182,36 +176,37 @@ createObjectFile(const string &fname, bool raw)
|
||||||
auto len = static_cast<size_t>(off);
|
auto len = static_cast<size_t>(off);
|
||||||
|
|
||||||
// mmap the whole shebang
|
// mmap the whole shebang
|
||||||
auto fileData = (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
|
uint8_t *file_data = (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED,
|
||||||
|
fd, 0);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
if (fileData == MAP_FAILED) {
|
if (file_data == MAP_FAILED) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectFile *fileObj = NULL;
|
ObjectFile *file_obj = NULL;
|
||||||
|
|
||||||
// figure out what we have here
|
// figure out what we have here
|
||||||
if ((fileObj = ElfObject::tryFile(fname, len, fileData)) != NULL) {
|
if ((file_obj = ElfObject::tryFile(fname, len, file_data)) != NULL) {
|
||||||
return fileObj;
|
return file_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((fileObj = EcoffObject::tryFile(fname, len, fileData)) != NULL) {
|
if ((file_obj = EcoffObject::tryFile(fname, len, file_data)) != NULL) {
|
||||||
return fileObj;
|
return file_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((fileObj = AoutObject::tryFile(fname, len, fileData)) != NULL) {
|
if ((file_obj = AoutObject::tryFile(fname, len, file_data)) != NULL) {
|
||||||
return fileObj;
|
return file_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((fileObj = DtbObject::tryFile(fname, len, fileData)) != NULL) {
|
if ((file_obj = DtbObject::tryFile(fname, len, file_data)) != NULL) {
|
||||||
return fileObj;
|
return file_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (raw)
|
if (raw)
|
||||||
return RawObject::tryFile(fname, len, fileData);
|
return RawObject::tryFile(fname, len, file_data);
|
||||||
|
|
||||||
// don't know what it is
|
// don't know what it is
|
||||||
munmap((char*)fileData, len);
|
munmap((char*)file_data, len);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,8 +82,6 @@ class ObjectFile
|
||||||
public:
|
public:
|
||||||
virtual ~ObjectFile();
|
virtual ~ObjectFile();
|
||||||
|
|
||||||
void close();
|
|
||||||
|
|
||||||
virtual bool loadSections(PortProxy& memProxy, Addr addrMask =
|
virtual bool loadSections(PortProxy& memProxy, Addr addrMask =
|
||||||
std::numeric_limits<Addr>::max(),
|
std::numeric_limits<Addr>::max(),
|
||||||
Addr offset = 0);
|
Addr offset = 0);
|
||||||
|
|
Loading…
Reference in a new issue