Skip to content

Commit

Permalink
Include callconv information in pdc output ##decompiler
Browse files Browse the repository at this point in the history
* Make afc/afci work without depending on function
  • Loading branch information
radare authored and trufae committed Oct 28, 2024
1 parent 2e1e27f commit f5578da
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
11 changes: 10 additions & 1 deletion libr/core/cmd_anal.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ static RCoreHelpMessage help_msg_afb = {
static RCoreHelpMessage help_msg_afc = {
"Usage:", "afc[agl?]", "# see also tcc command to manage all calling conventions",
"afc", " ccname", "manually set calling convention for current function",
"afc", "", "show calling convention for the Current function (same as tcc)",
"afc", "", "show default function calling convention (same as tcc)",
"afcr", "[j]", "show register usage for the current function",
"afcf", "[j] [name]", "prints return type function(arg1, arg2...), see afij",
"afci", "", "information about the current calling convention",
Expand Down Expand Up @@ -5707,6 +5707,15 @@ static int cmd_af(RCore *core, const char *input) {
if (!input[2] || input[2] == ' ' || input[2] == 'i' || input[2] == 'r' || input[2] == 'a') {
fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
if (!fcn) {
if (!input[2]) {
r_cons_println (r_config_get (core->config, "anal.cc"));
break;
}
if (input[2] == 'i') {
r_core_cmdf (core, "afcl %s",
r_config_get (core->config, "anal.cc"));
break;
}
R_LOG_ERROR ("afc: Cannot find function here");
break;
}
Expand Down
21 changes: 17 additions & 4 deletions libr/core/pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,30 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
n_bb, (int)r_anal_function_realsize (fcn));
NEWLINE (fcn->addr, indent);
const char *S0 = "esp";
PRINTF ("static inline void push (int reg) {%s -= %d; stack[%s] = reg; }\n", S0, (int)sizeof (int), S0);
PRINTF ("static inline void push(int reg) {%s -= %d; stack[%s] = reg; }\n", S0, (int)sizeof (int), S0);
PRINTF ("static inline int pop() {int r = stack[%s]; %s += %d; return r; }\n", S0, S0, (int)sizeof (int));
PRINTF ("\n");
}

char *fs = r_core_cmd_strf (core, "afs@0x%08"PFMT64x, fcn->addr);
{
char *cc = r_core_cmd_strf (core, "afci@0x%08"PFMT64x, fcn->addr);
r_str_trim (cc);
if (R_STR_ISNOTEMPTY (cc)) {
PRINTF ("// callconv: %s\n", cc);
}
free (cc);
}
if (R_STR_ISEMPTY (fs) || (r_str_startswith (fs, "void") && strstr (fs, "()"))) {
PRINTF ("int %s (int %s, int %s) {", fcn->name, a0, a1);
if (!strcmp (a0, a1)) {
PRINTF ("int %s (int %s) {", fcn->name, a0);
} else {
PRINTF ("int %s (int %s, int %s) {", fcn->name, a0, a1);
}
} else {
r_str_replace_char (fs, ';', '{');
PRINTF ("%s", fs);
r_str_replace_char (fs, ';', ' ');
r_str_trim (fs);
PRINTF ("%s {", fs);
}
free (fs);
indent++;
Expand Down
6 changes: 4 additions & 2 deletions test/db/cmd/cmd_pdc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ EXPECT=<<EOF
| 0x0040206e ff15b8304000 call dword [MessageBoxA] ; 0x4030b8 ; int MessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
| 0x00402074 6a00 push 0
\ 0x00402076 ff1584304000 call dword [ExitProcess] ; 0x403084 ; VOID ExitProcess(UINT uExitCode)
int entry0 (int stack, int stack) {
// callconv: eax cdecl (stack);
int entry0 (int stack) {
loc_0x0040200f:
si = 3
fcn.00402000 ()
Expand Down Expand Up @@ -150,7 +151,8 @@ af
pdc
EOF
EXPECT=<<EOF
int sym.func.100003a54 (int x0, int x1) {
// callconv: x0 arm64 (x0, x1, x2, x3, x4, x5, x6, x7, stack);
void sym.func.100003a54 (int64_t arg1, int64_t arg2) {
loc_0x100003a54:
x8 = [x0 + 0x60] // arg1
x8 = [x8 + 0x60]
Expand Down

0 comments on commit f5578da

Please sign in to comment.