Skip to content

Commit

Permalink
Fix #17523 - Resolve internal symbols referenced from the PLT ##bin
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed May 14, 2023
1 parent f5273b1 commit c1d2f34
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
23 changes: 11 additions & 12 deletions libr/bin/format/elf/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,7 @@ static Sdb *store_versioninfo_gnu_verdef(ELFOBJ *bin, Elf_(Shdr) *shdr, int sz)
}
size_t shsize = shdr->sh_size;
if (shdr->sh_size > bin->size) {
if (bin->verbose) {
eprintf ("Truncating shsize from %d to %d\n", (int)shdr->sh_size, (int)bin->size);
}
R_LOG_DEBUG ("Truncating shsize from %d to %d", (int)shdr->sh_size, (int)bin->size);
if (bin->size > shdr->sh_offset) {
shsize = bin->size - shdr->sh_offset;
} else {
Expand Down Expand Up @@ -1535,6 +1533,7 @@ static ut64 get_import_addr_loongarch(ELFOBJ *bin, RBinElfReloc *rel) {
ut64 pos = COMPUTE_PLTGOT_POSITION(rel, got_addr, 0x2);
return plt_addr + LOONGARCH_PLT_OFFSET + pos * LOONGARCH_PLT_ENTRY_SIZE;
}

static ut64 get_import_addr_sparc(ELFOBJ *bin, RBinElfReloc *rel) {
if (rel->type != R_SPARC_JMP_SLOT) {
R_LOG_DEBUG ("Unknown sparc reloc type %d", rel->type);
Expand Down Expand Up @@ -1686,7 +1685,7 @@ static ut64 get_import_addr(ELFOBJ *bin, int sym) {
case EM_LOONGARCH:
return get_import_addr_loongarch(bin, rel);
default:
eprintf ("Unsupported relocs type %" PFMT64u " for arch %d\n",
R_LOG_WARN ("Unsupported relocs type %" PFMT64u " for arch %d",
(ut64) rel->type, bin->ehdr.e_machine);
return UT64_MAX;
}
Expand Down Expand Up @@ -1914,7 +1913,7 @@ ut64 Elf_(r_bin_elf_get_main_offset)(ELFOBJ *bin) {
/* non-thumb entry points */
if (!memcmp (buf, "\x00\xb0\xa0\xe3\x00\xe0\xa0\xe3", 8)) {
if (buf[0x40 + 2] == 0xff && buf[0x40 + 3] == 0xeb) {
// eprintf ("custom\n");
// nothing to do
} else if (!memcmp (buf + 0x28 + 2, "\xff\xeb", 2)) {
return Elf_(r_bin_elf_v2p) (bin, r_read_le32 (&buf[0x34]) & ~1);
}
Expand Down Expand Up @@ -3998,6 +3997,7 @@ RBinSymbol *Elf_(_r_bin_elf_convert_symbol)(struct Elf_(r_bin_elf_obj_t) *bin, s
ptr->bind = symbol->bind;
ptr->type = symbol->type;
ptr->is_imported = symbol->is_imported;
// ptr->is_internal = symbol->is_internal;
ptr->paddr = paddr;
ptr->vaddr = vaddr;
ptr->size = symbol->size;
Expand Down Expand Up @@ -4112,7 +4112,7 @@ typedef struct import_info_t {
int nsym;
} ImportInfo;

static RVector *_load_additional_imported_symbols (ELFOBJ *bin, ImportInfo *import_info) {
static RVector *_load_additional_imported_symbols(ELFOBJ *bin, ImportInfo *import_info) {
// Elf_(fix_symbols) may find additional symbols, some of which could be
// imported symbols. Let's reserve additional space for them.
int nsym = import_info->nsym;
Expand Down Expand Up @@ -4312,14 +4312,12 @@ static RVector /* <RBinElfSymbol> */ *Elf_(_r_bin_elf_load_symbols_and_imports)(
bool is_sht_null = false;
bool is_vaddr = false;
bool is_imported = false;
bool is_internal = false;
if (type == R_BIN_ELF_IMPORT_SYMBOLS) {
if (memory.sym[k].st_value) {
toffset = memory.sym[k].st_value;
} else if ((toffset = get_import_addr (bin, k)) == -1) {
toffset = 0;
}
toffset = get_import_addr (bin, k);
tsize = 16;
is_imported = memory.sym[k].st_shndx == STN_UNDEF;
is_imported = true;
is_internal = memory.sym[k].st_shndx != STN_UNDEF;
} else {
tsize = memory.sym[k].st_size;
toffset = (ut64)memory.sym[k].st_value;
Expand Down Expand Up @@ -4362,6 +4360,7 @@ static RVector /* <RBinElfSymbol> */ *Elf_(_r_bin_elf_load_symbols_and_imports)(
es->is_sht_null = is_sht_null;
es->is_vaddr = is_vaddr;
es->is_imported = is_imported;
es->is_internal = is_internal;
if (type == R_BIN_ELF_IMPORT_SYMBOLS && is_imported) {
import_ret_ctr++;
}
Expand Down
1 change: 1 addition & 0 deletions libr/bin/format/elf/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef struct r_bin_elf_symbol_t {
bool is_sht_null;
bool is_vaddr; /* when true, offset is virtual address, otherwise it's physical */
bool is_imported;
bool is_internal;
} RBinElfSymbol;

typedef struct r_bin_elf_reloc_t {
Expand Down
4 changes: 3 additions & 1 deletion libr/bin/p/bin_elf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,13 @@ static RList* imports(RBinFile *bf) {

RBinElfSymbol *import_symbol;
r_vector_foreach (import_symbols, import_symbol) {
if (import_symbol->is_internal) {
continue;
}
RBinImport *ptr = R_NEW0 (RBinImport);
if (!ptr) {
break;
}

ptr->name = strdup (import_symbol->name);
ptr->bind = import_symbol->bind;
ptr->type = import_symbol->type;
Expand Down

0 comments on commit c1d2f34

Please sign in to comment.