The program I am reverse engineering has a function which I have identified as the C standard div function div_t div(int, int). The div_t result is returned in two ARM registers, r0 for the quotient and r1 for the remainder. The definition of div_t is struct div_t { int quot; int rem; };.

If I set the return type of the function to int (returned in r0), code using the division looks fairly clean, like iVar0 = div(x, 3); however, obviously, Ghidra (9.2.3) will lose track of the remainder, and when that is used, an extraout_r1 variable appears.

If I set the return type to div_t returned in r1:r0, Ghidra doesn't seem willing to access the struct members in the calling site, but casts it to longlong and does shifts (for remainder) or SUB84 (for quotient), and other complicated stuff. Here's what it produces for int f(int x) { return (x+1) % 3; }:

int f(int x)

  div_t dVar1;
  dVar1 = div(x + 1,3);
  return (int)((ulonglong)dVar1 >> 0x20);

... which I consider less than ideal for clarity.

Is there a way to make this function result in nicer code at the call site?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.