Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid bit hints on some arm/thumb binaries ##bin #23460

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions libr/bin/format/elf/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4320,6 +4320,7 @@ static void _set_arm_thumb_bits(struct Elf_(obj_t) *eo, RBinSymbol **symp) {
RBinSymbol *sym = *symp;
const char *name = r_bin_name_tostring2 (sym->name, 'o');
int len = strlen (name);
sym->bits = bin_bits;
if (name[0] == '$' && (len >= 2 && !name[2])) {
switch (name[1]) {
case 'a' : // arm
Expand All @@ -4334,14 +4335,16 @@ static void _set_arm_thumb_bits(struct Elf_(obj_t) *eo, RBinSymbol **symp) {
sym->paddr--;
}
return;
#if 0
case 'd': // data
sym->bits = 32;
return;
#endif
default:
break;
}
}
sym->bits = bin_bits;
if (bin_bits != 64) {
if (sym->bits != 64) {
sym->bits = 32;
if (sym->paddr != UT64_MAX) {
if (sym->vaddr & 1) {
Expand Down
36 changes: 27 additions & 9 deletions libr/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ static void load_types_from(RCore *core, const char *fmt, ...) {

R_API void r_core_anal_type_init(RCore *core) {
R_RETURN_IF_FAIL (core && core->anal);
int bits = core->rasm->config->bits;
const int bits = core->rasm->config->bits;
Sdb *types = core->anal->sdb_types;
// make sure they are empty this is initializing
sdb_reset (types);
Expand Down Expand Up @@ -2418,18 +2418,36 @@ static void handle_arm_special_symbol(RCore *core, RBinSymbol *symbol, int va) {
}
}

static void handle_arm_hint(RCore *core, RBinInfo *info, ut64 paddr, ut64 vaddr, int bits, int va) {
if (info->bits > 32) { // we look at 16 or 32 bit only
static void handle_arm_hint(RCore *core, RBinInfo *bi, ut64 paddr, ut64 vaddr, int sym_bits, int va) {
if (bi->bits > 32) { // we look at 16 or 32 bit only
return;
}

int force_bits = 0;
ut64 addr = compute_addr (core->bin, paddr, vaddr, va);
if (paddr & 1 || bits == 16) {
if (paddr & 1 || sym_bits == 16) {
force_bits = 16;
} else if (info->bits == 16 && bits == 32) {
force_bits = 32;
} else if (!(paddr & 1) && bits == 32) {
} else if (bi->bits == 16 && sym_bits == 32) {
#if 1
// ignore this case, which causes false positives on half-arm-thumb binaries
if (vaddr & 1) {
force_bits = 16;
} else {
// XXX ruseli fails
force_bits = 32;
#if 0
RAnalHint *hint = r_anal_hint_get (core->anal, addr);
if (hint && hint->bits == 32) {
force_bits = 32;
} else {
force_bits = 32;
//return;
// force_bits = 0;
}
#endif
}
#endif
} else if (!(paddr & 1) && sym_bits == 32) {
force_bits = 32;
}
if (force_bits) {
Expand Down Expand Up @@ -2741,8 +2759,8 @@ static bool bin_symbols(RCore *r, PJ *pj, int mode, ut64 laddr, int va, ut64 at,

// handle thumb and arm for entry point since they are not present in symbols
if (is_arm) {
r_list_foreach (entries, iter, entry) {
if (IS_MODE_SET (mode)) {
if (IS_MODE_SET (mode)) {
r_list_foreach (entries, iter, entry) {
handle_arm_entry (r, entry, info, va);
}
}
Expand Down
42 changes: 42 additions & 0 deletions test/db/anal/thumb
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,45 @@ svc 0x42
svc 0xb6
EOF
RUN

NAME=arm/thumb imports
FILE=bins/elf/libmagic.so
CMDS=<<EOF
pd 10
?en ahb32=
ahb*~b 32?
?en ahb16=
ahb*~b 16?
0x00005e34
ahb.
-b asm.lines=0
pd 4
EOF
EXPECT=<<EOF
: ;-- section..text:
: ;-- entry0:
: ;-- entry.fini0:
: 0x00005fa8 04009fe5 ldr r0, [0x00005fb4] ; [0x5fb4:4]=0x1104c ; [09] -r-x section size 53128 named .text
: 0x00005fac 00008fe0 add r0, pc, r0
`=< 0x00005fb0 93ffffea b sym.imp.__cxa_finalize
0x00005fb4 4c100100 andeq r1, r1, ip, asr 32
;-- entry.init0:
0x00005fb8 10402de9 push {r4, lr}
0x00005fbc 40409fe5 ldr r4, [0x00006004] ; [0x6004:4]=0x11064
0x00005fc0 40109fe5 ldr r1, [0x00006008] ; [0x6008:4]=0x29e0 "xxabiv115__forced_unwindD2Ev"
0x00005fc4 04408fe0 add r4, pc, r4
0x00005fc8 0030a0e3 mov r3, 0
0x00005fcc 0400a0e1 mov r0, r4
ahb32=323
ahb16=65
32
;-- rsym.strdup:
;-- strdup:
0x00005e34 00c68fe2 add ip, pc, 0, 12
0x00005e38 11ca8ce2 add ip, ip, 0x11000
0x00005e3c 48f1bce5 ldr pc, [ip, 0x148]!
;-- rsym.strstr:
;-- strstr:
0x00005e40 00c68fe2 add ip, pc, 0, 12
EOF
RUN
44 changes: 44 additions & 0 deletions test/db/cmd/cmd_ah
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,47 @@ ahb 0 @ 0x84
0x00000084 => bits=RESET
EOF
RUN

NAME=arm thumb hints32
FILE=bins/elf/r2pay-arm32.so
CMDS=<<EOF
ahb.
0x00004560
ahb.
?e ---
-e asm.bytes=1
-e asm.lines=0
s entry0
pd 20
EOF
EXPECT=<<EOF
32
16
---
;-- section..text:
;-- entry0:
;-- entry.fini0:
0x00004524 04009fe5 ldr r0, [0x00004530] ; [0x4530:4]=0xb1ad0 ; [12] -r-x section size 714606 named .text
0x00004528 00008fe0 add r0, pc, r0
0x0000452c 85feffea b sym.imp.__cxa_finalize
0x00004530 d01a0b00 ldrdeq r1, r2, [fp], -r0
0x00004534 000050e3 cmp r0, 0
0x00004538 1eff2f01 bxeq lr
0x0000453c 10ff2fe1 bx r0
0x00004540 0010a0e1 mov r1, r0
0x00004544 0c209fe5 ldr r2, [0x00004558] ; [0x4558:4]=0xb1aac
0x00004548 0c009fe5 ldr r0, [0x0000455c] ; [0x455c:4]=0xffffffdc
0x0000454c 02208fe0 add r2, pc, r2
0x00004550 00008fe0 add r0, pc, r0
0x00004554 78feffea b sym.imp.__cxa_atexit
0x00004558 ac1a0b00 andeq r1, fp, ip, lsr 21
0x0000455c dcffffff invalid
;-- .datadiv_decode1794556967687044894:
0x00004560 7047 bx lr
0x00004562 0000 movs r0, r0
;-- d7a068d43e39e826:
0x00004564 0148 ldr r0, [0x0000456c] ; [0x456c:4]=0xb161a
0x00004566 7844 add r0, pc
0x00004568 0068 ldr r0, [r0]
EOF
RUN
42 changes: 21 additions & 21 deletions test/db/formats/dwarf
Original file line number Diff line number Diff line change
Expand Up @@ -721,28 +721,28 @@ colu: 12
addr: 0x00001149
EOF
EXPECT_ERR=<<EOF
DEBUG: [cbin.c:3348] (section .dynsym) Cd 8[21] @ 0x3d8
DEBUG: [cbin.c:3348] (section .dynstr) Css 141 @ 0x480
DEBUG: [cbin.c:3348] (section .rela.dyn) Cd 8[24] @ 0x550
DEBUG: [cbin.c:3348] (section .rela.plt) Cd 8[3] @ 0x610
DEBUG: [cbin.c:3348] (section .init_array) Cd 8[1] @ 0x3db8
DEBUG: [cbin.c:3348] (section .fini_array) Cd 8[1] @ 0x3dc0
DEBUG: [cbin.c:3348] (section .dynamic) Cd 8[62] @ 0x3dc8
DEBUG: [cbin.c:3348] (section .got) Cd 8[9] @ 0x3fb8
DEBUG: [cbin.c:3348] (section .dynsym) Cd 8[21] @ 0x3d8
DEBUG: [cbin.c:3348] (section .dynstr) Css 141 @ 0x480
DEBUG: [cbin.c:3348] (section .rela.dyn) Cd 8[24] @ 0x550
DEBUG: [cbin.c:3348] (section .rela.plt) Cd 8[3] @ 0x610
DEBUG: [cbin.c:3348] (section .init_array) Cd 8[1] @ 0x3db8
DEBUG: [cbin.c:3348] (section .fini_array) Cd 8[1] @ 0x3dc0
DEBUG: [cbin.c:3348] (section .dynamic) Cd 8[62] @ 0x3dc8
DEBUG: [cbin.c:3348] (section .got) Cd 8[9] @ 0x3fb8
DEBUG: [cbin.c:3356] (section .dynsym) Cd 8[21] @ 0x3d8
DEBUG: [cbin.c:3356] (section .dynstr) Css 141 @ 0x480
DEBUG: [cbin.c:3356] (section .rela.dyn) Cd 8[24] @ 0x550
DEBUG: [cbin.c:3356] (section .rela.plt) Cd 8[3] @ 0x610
DEBUG: [cbin.c:3356] (section .init_array) Cd 8[1] @ 0x3db8
DEBUG: [cbin.c:3356] (section .fini_array) Cd 8[1] @ 0x3dc0
DEBUG: [cbin.c:3356] (section .dynamic) Cd 8[62] @ 0x3dc8
DEBUG: [cbin.c:3356] (section .got) Cd 8[9] @ 0x3fb8
DEBUG: [cbin.c:3356] (section .dynsym) Cd 8[21] @ 0x3d8
DEBUG: [cbin.c:3356] (section .dynstr) Css 141 @ 0x480
DEBUG: [cbin.c:3356] (section .rela.dyn) Cd 8[24] @ 0x550
DEBUG: [cbin.c:3356] (section .rela.plt) Cd 8[3] @ 0x610
DEBUG: [cbin.c:3356] (section .init_array) Cd 8[1] @ 0x3db8
DEBUG: [cbin.c:3356] (section .fini_array) Cd 8[1] @ 0x3dc0
DEBUG: [cbin.c:3356] (section .dynamic) Cd 8[62] @ 0x3dc8
DEBUG: [cbin.c:3356] (section .got) Cd 8[9] @ 0x3fb8
WARN: [cbin.c:1899] Relocs has not been applied. Please use `-e bin.relocs.apply=true` or `-e bin.cache=true` next time
DEBUG: [cbin.c:2593] Cannot resolve symbol address __libc_start_main
DEBUG: [cbin.c:2593] Cannot resolve symbol address _ITM_deregisterTMCloneTable
DEBUG: [cbin.c:2593] Cannot resolve symbol address __gmon_start__
DEBUG: [cbin.c:2593] Cannot resolve symbol address _ITM_registerTMCloneTable
DEBUG: [cbin.c:2593] Cannot resolve symbol address __cxa_finalize
DEBUG: [cbin.c:2601] Cannot resolve symbol address __libc_start_main
DEBUG: [cbin.c:2601] Cannot resolve symbol address _ITM_deregisterTMCloneTable
DEBUG: [cbin.c:2601] Cannot resolve symbol address __gmon_start__
DEBUG: [cbin.c:2601] Cannot resolve symbol address _ITM_registerTMCloneTable
DEBUG: [cbin.c:2601] Cannot resolve symbol address __cxa_finalize
EOF
RUN

Expand Down
Loading