matt-kempster/m2c

Unrecognized linker macro %gp_rel

Closed this issue · 1 comments

mkst commented

The macro handling in m2c does not support addiu $a1, $gp, %gp_rel(D_800D0988):

Error occurred while processing instruction: addiu $a1, $gp, %gp_rel(D_800D0988)

This can be trivially fixed by adding gp_rel to the list of known macros:

diff --git a/m2c/translate.py b/m2c/translate.py
index 298b307..20220b5 100644
--- a/m2c/translate.py
+++ b/m2c/translate.py
@@ -2846,7 +2846,7 @@ def strip_macros(arg: Argument) -> Argument:
     just the upper part. This preserves semantics in most cases (though not when %hi's
     are reused for different %lo's...)"""
     if isinstance(arg, Macro):
-        if arg.macro_name in ["sda2", "sda21"]:
+        if arg.macro_name in ["sda2", "sda21", "gp_rel"]:
             return arg.argument
         if arg.macro_name == "hi":
             raise DecompFailure("%hi macro outside of lui")

.. but then we get dumb behaviour where m2c wants to add $gp to D_800D0988 rather than just using D_800D0988 itself:

        temp_a0_2 = ((0x1F40 - func_80039F28(arg0 + 0x5C, saved_reg_gp + &D_800D0988)) * temp_a1) / 8000;

Perhaps this can be tackled in a different Issue though, I haven't looked at how tricky it would be to solve it.

Edit: I raised this issue off the back of this scratch as decomp.me treats the failures as a total failure to decompile, which isnt true.

Fixed by adding some logic to the addiu handler. It's weird that sda2/sda21 are listed in strip_macros, feels like they should have the same problem of adding the ppc equivalent of saved_reg_gp. To look into at some later point, maybe.