matt-kempster / m2c

A MIPS and PowerPC decompiler.
GNU General Public License v3.0
386 stars 46 forks source link

Arithmetic expression being dropped #243

Closed InusualZ closed 1 year ago

InusualZ commented 1 year ago

Sorry about the tittle, I don't know enough about how the decompiler works so I went with what I understand is happening

m2c output image

Ghidra Output image

arguments

python m2c.py -t ppc-mwcc-c --context context.c input.s

input.s context.c

simonlindholm commented 1 year ago

Looks like we don't recognize that r5 is an argument to sprintf, so we optimize it out. Knowing which registers to pass as arguments to functions on ppc is difficult in general, unfortunately, though we should be able to add a heuristic that captures this particular case based on the deduction that the register is not used in any other way.

InusualZ commented 1 year ago

Ohh, I see.

I changed the function definition to:

void sprintf(char* __s, char* __format, unsigned int d); // @8045decc

from:

void sprintf(char* __s, char* __format, ...); // @8045deccZ

And now it "work" image

Maybe you can see also check if the function is variadic

simonlindholm commented 1 year ago

And have variadic functions consume all the registers they can? Yeah, that's not unreasonable. Will lead to false positives but that may be okay given variadic functions are a rare case.

InusualZ commented 1 year ago

consume all the registers they can

I don't know if possible, but you can exclude most registers that haven't being touched since the last call to a function (before the call to the variadic)

simonlindholm commented 1 year ago

Yeah, we already take into account function calls clobbering registers and such. But argument registers are very often used as temps, and called unknown functions are treated as writing to r3/r4/f1.

simonlindholm commented 1 year ago

Fixed in 008adfcf9afeb0ea6df2cbee3d2351c832cd0bef (for variadic functions, lmk if this generates too many false positives and I can back it out) and 6b6e1390bf30013247343623194964ff13d0c45b (for functions not in context, based on the "unread register" heuristic).

InusualZ commented 1 year ago

Thank you, seems to be working correctly as far as I can see.