Skip to content

Commit

Permalink
Remove redundant function info while disassembling (#3955)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeiweiHu authored Nov 6, 2023
1 parent c9aabf8 commit 5d3307a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 41 deletions.
9 changes: 0 additions & 9 deletions librz/analysis/dwarf_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -1937,15 +1937,6 @@ static bool dwarf_integrate_function(void *user, const ut64 k, const void *value
return true;
}

/* Apply signature as a comment at a function address */
RzCallable *callable = dw_fn->prefer_name ? rz_type_func_get(analysis->typedb, dw_fn->prefer_name)
: ht_up_find(analysis->debug_info->callable_by_offset, dw_fn->offset, NULL);
if (callable) {
char *sig = rz_type_callable_as_string(analysis->typedb, callable);
rz_meta_set_string(analysis, RZ_META_TYPE_COMMENT, dw_fn->low_pc, sig);
free(sig);
}

if (dw_fn->prefer_name && !rz_str_startswith(dw_fn->prefer_name, "anonymous")) {
char *dwf_name = rz_str_newf("dbg.%s", dw_fn->prefer_name);
rz_analysis_function_rename((RzAnalysisFunction *)fn, dwf_name);
Expand Down
21 changes: 20 additions & 1 deletion librz/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,24 @@ static void __preline_flag(RzDisasmState *ds, RzFlagItem *flag) {
}
}

// check the equality between names from symbol tab and debug info
// like sym.func_name and dbg.func_name
static bool is_sym_dbg_equal(const char *a, const char *b) {
if (!rz_str_startswith(a, "sym.") && !rz_str_startswith(a, "dbg.")) {
return false;
}
if (!rz_str_startswith(b, "sym.") && !rz_str_startswith(b, "dbg.")) {
return false;
}
const size_t sym_n = strlen("sym.");
return !strcmp(a + sym_n, b + sym_n);
}

static inline bool is_flag_overlapped(RzFlagItem *flag, RzAnalysisFunction *f) {
const bool name_overlapped = !strcmp(flag->name, f->name) || is_sym_dbg_equal(flag->name, f->name);
return f->addr == flag->offset && name_overlapped;
}

#define printPre (outline || !*comma)
static void ds_show_flags(RzDisasmState *ds, bool overlapped) {
// const char *beginch;
Expand All @@ -2197,7 +2215,7 @@ static void ds_show_flags(RzDisasmState *ds, bool overlapped) {
RzAnalysisBlock *switch_block = NULL;
const char *switch_enum_name = NULL;
rz_list_foreach (uniqlist, iter, flag) {
if (!overlapped && f && f->addr == flag->offset && !strcmp(flag->name, f->name)) {
if (!overlapped && f && is_flag_overlapped(flag, f)) {
// do not show non-overlapped flags that have the same name as the function
continue;
}
Expand Down Expand Up @@ -5516,6 +5534,7 @@ RZ_API int rz_core_print_disasm(RZ_NONNULL RzCore *core, ut64 addr, RZ_NONNULL u
ds_show_refs(ds);
}
}

core->print->resetbg = true;
ds_newline(ds);
if (ds->line) {
Expand Down
8 changes: 4 additions & 4 deletions test/db/analysis/rl78
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ EXPECT=<<EOF
| ; arg char *decrypted @ ...
| ; arg const char *encrypted @ ...
| ; arg size_t n @ ...
| 0x00000552 subw sp, #0x12 ; caesar.c:15 ; int decrypt(char *decrypted, const char *encrypted, size_t n)
| 0x00000552 subw sp, #0x12 ; caesar.c:15 ; [06] -r-x section size 4316 named .text
| 0x00000554 movw [sp+0x0], ax
| 0x00000556 movw ax, bc
| 0x00000557 movw [sp+0x2], ax
Expand All @@ -79,7 +79,7 @@ EXPECT=<<EOF
/ void make_uppercase(char *buf, size_t n)
| ; arg char *buf @ ...
| ; arg size_t n @ ...
| 0x000005a3 subw sp, #0x18 ; caesar.c:24 ; void make_uppercase(char *buf, size_t n)
| 0x000005a3 subw sp, #0x18 ; caesar.c:24
| 0x000005a5 movw [sp+0x0], ax
| 0x000005a7 movw ax, bc
| 0x000005a8 movw [sp+0x2], ax
Expand All @@ -92,7 +92,7 @@ EXPECT=<<EOF
| ; arg const char *buf @ ...
| ; arg size_t n @ ...
| ; var int sum @ ...
| 0x00000634 subw sp, #0x1e ; caesar.c:33 ; int hash(const char *buf, size_t n)
| 0x00000634 subw sp, #0x1e ; caesar.c:33
| 0x00000636 movw [sp+0x4], ax
| 0x00000638 movw ax, bc
| 0x00000639 movw [sp+0x6], ax
Expand All @@ -113,7 +113,7 @@ EXPECT=<<EOF
| ; var char *uppercase @ ...
| ; var const char *pw @ ...
| ; var size_t pwlen @ ...
| 0x000006b3 subw sp, #0x26 ; caesar.c:43 ; int main(int argc, char **argv)
| 0x000006b3 subw sp, #0x26 ; caesar.c:43
| 0x000006b5 movw de, #0x0
| 0x000006b8 movw [sp+0x14], ax
| 0x000006ba movw ax, de
Expand Down
32 changes: 10 additions & 22 deletions test/db/analysis/vars
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@ e asm.comments=0
pdf
EOF
EXPECT=<<EOF
;-- varfunc:
/ int varfunc()
| ; var int lightbulb @ stack - 0x18
| ; var int sun @ stack - 0x14
Expand Down Expand Up @@ -588,7 +587,6 @@ e asm.comments=0
pdf
EOF
EXPECT=<<EOF
;-- varfunc:
/ int varfunc()
| ; var int lightbulb @ stack - 0x10
| ; var int sun @ stack - 0xc
Expand Down Expand Up @@ -618,7 +616,6 @@ pdf
EOF
EXPECT=<<EOF
; CALL XREF from dbg.main @ 0x11c0
;-- foo:
/ int foo(int num)
| ; var int64_t arg1 @ rdi
| ; arg int num @ stack - 0x2c
Expand All @@ -628,7 +625,7 @@ EXPECT=<<EOF
| ; var int mult_int @ stack - 0x1c
| ; var long int add_long @ stack - 0x10
| ; var short int sub_short @ stack - 0x22
| 0x00001139 push rbp ; example.c:3 ; int foo(int num)
| 0x00001139 push rbp ; example.c:3
| 0x0000113a mov rbp, rsp
| 0x0000113d sub rsp, 0x30
| 0x00001141 mov dword [num], edi ; arg1
Expand Down Expand Up @@ -899,12 +896,11 @@ true fmt const char * DWARF loclist: [(0x80000c22, 10): a4, (0x8
false ans int DWARF empty
int printf(const char *fmt, ...);
; CALL XREFS from dbg.main @ 0x8000054e, 0x80000636
;-- printf:
/ int printf(const char *fmt, ...)
| ; arg const char *fmt @ a4
| ; var int ans @ ...
| ; var va_list ap @ stack + 0x4
| 0x80000c22 mov.aa a6, a4 ; printf.c:10 ; arg5 ; int printf(const char *fmt, ...)
| 0x80000c22 mov.aa a6, a4 ; printf.c:10 ; arg5
---------
arg void *str @ a4
arg const char *buf @ a4
Expand All @@ -917,13 +913,12 @@ true buf const char * DWARF loclist: [(0x80000c04, 12): a5, (0x8
false arg6 int32_t rizin a5
true n size_t DWARF loclist: [(0x80000c04, 10): d4, (0x80000c0e, 5): d5, (0x80000c13, 7): d15, (0x80000c1a, 8): <evaluation waiting>]
void * prout(void *str, const char *buf, size_t n);
;-- prout:
/ void * prout(void *str, const char *buf, size_t n)
| ; var int32_t arg6 @ a5
| ; arg void *str @ a4
| ; arg const char *buf @ a4
| ; arg size_t n @ d5
| 0x80000c04 mov.aa a15, a4 ; printf.c:5 ; arg5 ; void * prout(void *str, const char *buf, size_t n)
| 0x80000c04 mov.aa a15, a4 ; printf.c:5 ; arg5
---------
arg fp_number_type *b @ a2
arg fp_number_type *a @ a4
Expand All @@ -948,7 +943,6 @@ false a_fraction fractype DWARF loclist: [(0x80003cd4, 1
fp_number_type * _fpadd_parts(fp_number_type *a, fp_number_type *b, fp_number_type *tmp);
; CALL XREF from dbg.__adddf3 @ 0x80003e32
; CALL XREF from dbg.__subdf3 @ 0x80003e72
;-- _fpadd_parts:
/ fp_number_type * _fpadd_parts(fp_number_type *a, fp_number_type *b, fp_number_type *tmp)
| ; var int32_t arg6 @ a5
| ; arg fp_number_type *a @ a4
Expand All @@ -959,7 +953,7 @@ fp_number_type * _fpadd_parts(fp_number_type *a, fp_number_type *b, fp_number_ty
| ; var int b_normal_exp @ d2
| ; var fractype a_fraction @ LOCLIST
| ; var fractype b_fraction @ COMPOSITE
| 0x80003c60 ld.bu d15, [a4]#0 ; fp-bit.c:604 ; arg5 ; fp_number_type * _fpadd_parts(fp_number_type *a, fp_number_type *b, fp_number_type *tmp)
| 0x80003c60 ld.bu d15, [a4]#0 ; fp-bit.c:604 ; arg5
---------
arg const char *s @ a2
var const char *sc @ a2
Expand All @@ -973,13 +967,12 @@ false arg5 int32_t rizin a4
true maxsize size_t DWARF loclist: [(0x800030ca, 8): d4, (0x800030ea, 2): d4]
size_t strnlen_s(const char *s, size_t maxsize);
; CALL XREF from dbg._Fail_s @ 0x8000191e
;-- strnlen_s:
/ size_t strnlen_s(const char *s, size_t maxsize)
| ; var int32_t arg5 @ a4
| ; arg const char *s @ a2
| ; arg size_t maxsize @ d4
| ; var const char *sc @ a2
| 0x800030ca mov d2, #0 ; strnlen_s.c:6 ; size_t strnlen_s(const char *s, size_t maxsize)
| 0x800030ca mov d2, #0 ; strnlen_s.c:6
---------
var _Statab *pwcstate @ a13
arg char *s @ a4
Expand All @@ -995,14 +988,13 @@ true wc wchar_t DWARF loclist: [(0x800018a6, 21): d4, (
false pmbstate _Statab * DWARF empty
int _Wctomb(char *s, wchar_t wc, mbstate_t *pst);
; CALL XREF from dbg._Putstr @ 0x800014d2
;-- _Wctomb:
/ int _Wctomb(char *s, wchar_t wc, mbstate_t *pst)
| ; arg char *s @ a4
| ; arg wchar_t wc @ d4
| ; arg mbstate_t *pst @ a5
| ; var _Statab *pmbstate @ ...
| ; var _Statab *pwcstate @ a13
| 0x800018a6 movh.a a2, #0xd000 ; xwctomb.c:123 ; int _Wctomb(char *s, wchar_t wc, mbstate_t *pst)
| 0x800018a6 movh.a a2, #0xd000 ; xwctomb.c:123
---------
arg mbstate_t *pst @ a12
arg const char *s @ a13
Expand All @@ -1022,7 +1014,6 @@ false arg7 int32_t rizin a6
true nin size_t DWARF loclist: [(0x80003084, 23): d4, (0x8000309b, 21): d15]
int _Mbtowc(wchar_t *pwc, const char *s, size_t nin, mbstate_t *pst);
; CALL XREF from dbg._Printf @ 0x80000d02
;-- _Mbtowc:
/ int _Mbtowc(wchar_t *pwc, const char *s, size_t nin, mbstate_t *pst)
| ; var int32_t arg5 @ a4
| ; var int32_t arg6 @ a5
Expand All @@ -1031,18 +1022,17 @@ int _Mbtowc(wchar_t *pwc, const char *s, size_t nin, mbstate_t *pst);
| ; arg const char *s @ a13
| ; arg size_t nin @ d15
| ; arg mbstate_t *pst @ a12
| 0x80003084 movh.a a15, #0xd000 ; xmbtowc.c:150 ; int _Mbtowc(wchar_t *pwc, const char *s, size_t nin, mbstate_t *pst)
| 0x80003084 movh.a a15, #0xd000 ; xmbtowc.c:150
---------
arg int except @ d4
is_arg name type constraints origin addr
-------------------------------------------
true except int DWARF d4
int feraiseexcept(int except);
; CALL XREF from dbg._Feraise @ 0x8000343e
;-- feraiseexcept:
/ int feraiseexcept(int except)
| ; arg int except @ d4
| 0x800037d8 mov d2, #0 ; feraiseexcept.c:173 ; int feraiseexcept(int except)
| 0x800037d8 mov d2, #0 ; feraiseexcept.c:173
---------
var size_t size_arg @ a0
var Ppvoidfn newfuns @ a2
Expand All @@ -1055,12 +1045,11 @@ false inc size_t DWARF d15
int _Atrealloc();
; CALL XREF from dbg.atexit @ 0x80001a4c
; CALL XREF from dbg._Atexit @ 0x800033b8
;-- _Atrealloc:
/ int _Atrealloc()
| ; var size_t size_arg @ a0
| ; var size_t inc @ d15
| ; var Ppvoidfn newfuns @ a2
| 0x80001994 movh.a a15, #0xd000 ; exit.c:22 ; int _Atrealloc()
| 0x80001994 movh.a a15, #0xd000 ; exit.c:22
---------
arg some_t **out @ stack - 0x18
arg struct Some *gg @ stack - 0x14
Expand All @@ -1084,7 +1073,6 @@ false arg6 int32_t rizin a5
false arg7 int32_t rizin a6
void dbg.fn1(int a, char *g, double q, struct Some *gg, some_t **out);
; CALL XREF from dbg.main @ 0x8000059e
;-- fn1:
/ _Bool fn1(int a, char *g, double q, struct Some *gg, some_t **out)
| ; var int32_t arg5 @ a4
| ; var int32_t arg6 @ a5
Expand All @@ -1095,7 +1083,7 @@ void dbg.fn1(int a, char *g, double q, struct Some *gg, some_t **out);
| ; arg double q @ stack - 0x10
| ; arg struct Some *gg @ stack - 0x14
| ; arg some_t **out @ stack - 0x18
| 0x8000041c mov.aa a14, sp ; float_ex1.c:15 ; _Bool fn1(int a, char *g, double q, struct Some *gg, some_t **out)
| 0x8000041c mov.aa a14, sp ; float_ex1.c:15
---------
EOF
RUN
Expand Down
8 changes: 3 additions & 5 deletions test/db/cmd/dwarf
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,7 @@ EXPECT=<<EOF
| ; arg SmallInt mag @ stack - 0x10
| ; arg SmallInt szel @ stack - 0x18
| ; var SmallInt i @ stack - 0x1c
| 0x00401980 55 push rbp ; void palya(SmallInt mag, SmallInt szel)
| 0x00401980 55 push rbp ; unit p$mozgkigyo palya(smallint,smallint)
| 0x00401981 4889e5 mov rbp, rsp
| 0x00401984 488d6424d0 lea rsp, [rsp - 0x30]
| 0x00401989 48895dd8 mov qword [var_30h], rbx
Expand Down Expand Up @@ -6523,10 +6523,9 @@ var float a @ ...
var float b @ ...
var double c @ ...
; CALL XREFS from dbg.main @ 0x817c, 0x8184
;-- new_some:
/ some_t * new_some()
| ; var struct Some *n @ r4
| 0x0000813c push {r4, lr} ; some_t * new_some()
| 0x0000813c push {r4, lr}
| 0x0000813e movs r0, 0x14 ; size_t size
| 0x00008140 bl malloc ; sym.malloc ; void *malloc(size_t size)
| 0x00008144 mov r4, r0
Expand Down Expand Up @@ -6574,10 +6573,9 @@ var float a @ ...
var float b @ ...
var double c @ ...
; CALL XREFS from dbg.main @ 0x817c, 0x8184
;-- new_some:
/ some_t * new_some()
| ; var struct Some *n @ r4
| 0x0000813c push {r4, lr} ; some_t * new_some()
| 0x0000813c push {r4, lr}
| 0x0000813e movs r0, 0x14 ; size_t size
| 0x00008140 bl sym.malloc ; void *malloc(size_t size)
| 0x00008144 mov r4, r0
Expand Down

0 comments on commit 5d3307a

Please sign in to comment.