Skip to content

Commit 53cab17

Browse files
committed
Fix #17523 - Resolve internal symbols referenced from the PLT ##bin
1 parent 1c8bd6d commit 53cab17

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

libr/bin/format/elf/elf.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -991,9 +991,7 @@ static Sdb *store_versioninfo_gnu_verdef(ELFOBJ *bin, Elf_(Shdr) *shdr, int sz)
991991
}
992992
size_t shsize = shdr->sh_size;
993993
if (shdr->sh_size > bin->size) {
994-
if (bin->verbose) {
995-
eprintf ("Truncating shsize from %d to %d\n", (int)shdr->sh_size, (int)bin->size);
996-
}
994+
R_LOG_DEBUG ("Truncating shsize from %d to %d", (int)shdr->sh_size, (int)bin->size);
997995
if (bin->size > shdr->sh_offset) {
998996
shsize = bin->size - shdr->sh_offset;
999997
} else {
@@ -1535,6 +1533,7 @@ static ut64 get_import_addr_loongarch(ELFOBJ *bin, RBinElfReloc *rel) {
15351533
ut64 pos = COMPUTE_PLTGOT_POSITION(rel, got_addr, 0x2);
15361534
return plt_addr + LOONGARCH_PLT_OFFSET + pos * LOONGARCH_PLT_ENTRY_SIZE;
15371535
}
1536+
15381537
static ut64 get_import_addr_sparc(ELFOBJ *bin, RBinElfReloc *rel) {
15391538
if (rel->type != R_SPARC_JMP_SLOT) {
15401539
R_LOG_DEBUG ("Unknown sparc reloc type %d", rel->type);
@@ -1686,7 +1685,7 @@ static ut64 get_import_addr(ELFOBJ *bin, int sym) {
16861685
case EM_LOONGARCH:
16871686
return get_import_addr_loongarch(bin, rel);
16881687
default:
1689-
eprintf ("Unsupported relocs type %" PFMT64u " for arch %d\n",
1688+
R_LOG_WARN ("Unsupported relocs type %" PFMT64u " for arch %d",
16901689
(ut64) rel->type, bin->ehdr.e_machine);
16911690
return UT64_MAX;
16921691
}
@@ -1914,7 +1913,7 @@ ut64 Elf_(r_bin_elf_get_main_offset)(ELFOBJ *bin) {
19141913
/* non-thumb entry points */
19151914
if (!memcmp (buf, "\x00\xb0\xa0\xe3\x00\xe0\xa0\xe3", 8)) {
19161915
if (buf[0x40 + 2] == 0xff && buf[0x40 + 3] == 0xeb) {
1917-
// eprintf ("custom\n");
1916+
// nothing to do
19181917
} else if (!memcmp (buf + 0x28 + 2, "\xff\xeb", 2)) {
19191918
return Elf_(r_bin_elf_v2p) (bin, r_read_le32 (&buf[0x34]) & ~1);
19201919
}
@@ -3998,6 +3997,7 @@ RBinSymbol *Elf_(_r_bin_elf_convert_symbol)(struct Elf_(r_bin_elf_obj_t) *bin, s
39983997
ptr->bind = symbol->bind;
39993998
ptr->type = symbol->type;
40003999
ptr->is_imported = symbol->is_imported;
4000+
// ptr->is_internal = symbol->is_internal;
40014001
ptr->paddr = paddr;
40024002
ptr->vaddr = vaddr;
40034003
ptr->size = symbol->size;
@@ -4112,7 +4112,7 @@ typedef struct import_info_t {
41124112
int nsym;
41134113
} ImportInfo;
41144114

4115-
static RVector *_load_additional_imported_symbols (ELFOBJ *bin, ImportInfo *import_info) {
4115+
static RVector *_load_additional_imported_symbols(ELFOBJ *bin, ImportInfo *import_info) {
41164116
// Elf_(fix_symbols) may find additional symbols, some of which could be
41174117
// imported symbols. Let's reserve additional space for them.
41184118
int nsym = import_info->nsym;
@@ -4312,14 +4312,12 @@ static RVector /* <RBinElfSymbol> */ *Elf_(_r_bin_elf_load_symbols_and_imports)(
43124312
bool is_sht_null = false;
43134313
bool is_vaddr = false;
43144314
bool is_imported = false;
4315+
bool is_internal = false;
43154316
if (type == R_BIN_ELF_IMPORT_SYMBOLS) {
4316-
if (memory.sym[k].st_value) {
4317-
toffset = memory.sym[k].st_value;
4318-
} else if ((toffset = get_import_addr (bin, k)) == -1) {
4319-
toffset = 0;
4320-
}
4317+
toffset = get_import_addr (bin, k);
43214318
tsize = 16;
4322-
is_imported = memory.sym[k].st_shndx == STN_UNDEF;
4319+
is_imported = true;
4320+
is_internal = memory.sym[k].st_shndx != STN_UNDEF;
43234321
} else {
43244322
tsize = memory.sym[k].st_size;
43254323
toffset = (ut64)memory.sym[k].st_value;
@@ -4362,6 +4360,7 @@ static RVector /* <RBinElfSymbol> */ *Elf_(_r_bin_elf_load_symbols_and_imports)(
43624360
es->is_sht_null = is_sht_null;
43634361
es->is_vaddr = is_vaddr;
43644362
es->is_imported = is_imported;
4363+
es->is_internal = is_internal;
43654364
if (type == R_BIN_ELF_IMPORT_SYMBOLS && is_imported) {
43664365
import_ret_ctr++;
43674366
}

libr/bin/format/elf/elf.h

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef struct r_bin_elf_symbol_t {
5858
bool is_sht_null;
5959
bool is_vaddr; /* when true, offset is virtual address, otherwise it's physical */
6060
bool is_imported;
61+
bool is_internal;
6162
} RBinElfSymbol;
6263

6364
typedef struct r_bin_elf_reloc_t {

libr/bin/p/bin_elf.inc

+3-1
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,13 @@ static RList* imports(RBinFile *bf) {
403403

404404
RBinElfSymbol *import_symbol;
405405
r_vector_foreach (import_symbols, import_symbol) {
406+
if (import_symbol->is_internal) {
407+
continue;
408+
}
406409
RBinImport *ptr = R_NEW0 (RBinImport);
407410
if (!ptr) {
408411
break;
409412
}
410-
411413
ptr->name = strdup (import_symbol->name);
412414
ptr->bind = import_symbol->bind;
413415
ptr->type = import_symbol->type;

0 commit comments

Comments
 (0)