* r_bin_elf

- New method to resolve the base address
  - Fix bug in bins with bin->ehdr.e_phnum = 0
  - Fix resolution of symbol address in relocatable bins
This commit is contained in:
Nibble 2010-05-29 17:11:41 +02:00
parent e9e8740a62
commit 6fa84400a6
2 changed files with 19 additions and 9 deletions

View File

@ -47,6 +47,8 @@ static int Elf_(r_bin_elf_init_ehdr)(struct Elf_(r_bin_elf_obj_t) *bin) {
static int Elf_(r_bin_elf_init_phdr)(struct Elf_(r_bin_elf_obj_t) *bin) {
int phdr_size, len;
if (bin->ehdr.e_phnum == 0)
return R_FALSE;
phdr_size = bin->ehdr.e_phnum * sizeof (Elf_(Phdr));
if ((bin->phdr = (Elf_(Phdr) *)malloc (phdr_size)) == NULL) {
perror ("malloc (phdr)");
@ -190,9 +192,14 @@ static ut64 Elf_(get_import_addr)(struct Elf_(r_bin_elf_obj_t) *bin, int sym) {
}
ut64 Elf_(r_bin_elf_get_baddr)(struct Elf_(r_bin_elf_obj_t) *bin) {
int i;
if (!bin->phdr)
return -1;
return bin->phdr->p_vaddr & ELF_ADDR_MASK;
return 0;
for (i = 0; i < bin->ehdr.e_phnum; i++)
if (bin->phdr[i].p_type == PT_LOAD && bin->phdr[i].p_offset == 0)
return (ut64)bin->phdr[i].p_vaddr;
return 0;
}
ut64 Elf_(r_bin_elf_get_entry_offset)(struct Elf_(r_bin_elf_obj_t) *bin) {
@ -536,14 +543,17 @@ struct r_bin_elf_symbol_t* Elf_(r_bin_elf_get_symbols)(struct Elf_(r_bin_elf_obj
Elf_(Sym) *sym;
struct r_bin_elf_symbol_t *ret = NULL;
char *strtab;
ut64 sym_offset, toffset;
ut64 sym_offset = 0, data_offset = 0, toffset;
int tsize, nsym, ret_ctr, i, j, k, len;
if (!bin->shdr)
return NULL;
sym_offset = (bin->ehdr.e_type == ET_REL ? Elf_(r_bin_elf_get_section_offset)(bin, ".text") : 0);
if (bin->ehdr.e_shnum == 0)
if (!bin->shdr || bin->ehdr.e_shnum == 0)
return NULL;
if (bin->ehdr.e_type == ET_REL) {
if ((sym_offset = Elf_(r_bin_elf_get_section_offset)(bin, ".text")) == -1)
sym_offset = 0;
if ((data_offset = Elf_(r_bin_elf_get_section_offset)(bin, ".rodata")) == -1)
data_offset = 0;
}
for (i = 0; i < bin->ehdr.e_shnum; i++)
if ((type == R_BIN_ELF_IMPORTS &&
bin->shdr[i].sh_type == (bin->ehdr.e_type == ET_REL ? SHT_SYMTAB : SHT_DYNSYM)) ||
@ -585,7 +595,8 @@ struct r_bin_elf_symbol_t* Elf_(r_bin_elf_get_symbols)(struct Elf_(r_bin_elf_obj
tsize = 0;
} else if (type == R_BIN_ELF_SYMBOLS && sym[k].st_shndx != STN_UNDEF &&
ELF_ST_TYPE(sym[k].st_info) != STT_SECTION && ELF_ST_TYPE(sym[k].st_info) != STT_FILE) {
toffset = (ut64)sym[k].st_value + sym_offset;
toffset = (ut64)sym[k].st_value +
(ELF_ST_TYPE(sym[k].st_info) == STT_FUNC?sym_offset:data_offset);
tsize = sym[k].st_size;
} else continue;
if ((ret = realloc (ret, (ret_ctr + 1) * sizeof (struct r_bin_elf_symbol_t))) == NULL) {

View File

@ -60,7 +60,6 @@
#define _INCLUDE_ELF_SPECS_H
#define ELF_STRING_LENGTH 256
#define ELF_ADDR_MASK 0xFFFFFFFFFFFF8000LL
#define ELF_GOTOFF_MASK 0xFFFFFFFFFFFFF000LL
/* Type for a 16-bit quantity. */