Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PowerPC] Ghidra decompiler doesn't understand non-r2 SDA #325

Closed
NWPlayer123 opened this issue Apr 4, 2019 · 11 comments
Closed

[PowerPC] Ghidra decompiler doesn't understand non-r2 SDA #325

NWPlayer123 opened this issue Apr 4, 2019 · 11 comments
Labels
Type: Bug Something isn't working

Comments

@NWPlayer123
Copy link

Context: GameCube and Wii are compiled with two Small Data Area/Table Of Contents (SDA/TOC) registers, that it reserves for single-instruction data manipulation, r2 and r13. Right now it looks like it only understands r2.
Whenever it hits r13, it sets a variable and then manually shows the (r13 - 0x____) as a variable, instead of knowing that there's a variable there, which gives really terrible decompilation results.

Screenshot_444

@NWPlayer123 NWPlayer123 added the Type: Bug Something isn't working label Apr 4, 2019
@tmbinc
Copy link

tmbinc commented Apr 4, 2019

Some PowerPC code I've seen even uses r2,r13,r14,r15,r16 (GM E98 ECU; not sure which compiler this is).

@caheckman
Copy link
Contributor

Just looking at the screenshot, it looks like r13 may be getting treated as a save register, which I think is a non-standard ABI. You could try adding r13 to the unaffected list in the cspec to see if this helps decompilation. Its hard to tell from the screenshot, but I'm assuming you're using ppc_32_be.cspec. Look for the lines:

  <unaffected>
    <register name="r14"/>
    <register name="r15"/>
    <register name="r16"/>
    <register name="r17"/>

Add a " line, then save and restart.

You also might want to investigate turning on in-lining or applying a call-fixup for the _savegpr_26 function. It looks suspiciously like an internal compiler function.

@NWPlayer123
Copy link
Author

@caheckman yeah,

  1. GameCube/Wii are big endian aka PPC32BE
  2. restgpr and savegpr are part of the Metrowerks/CodeWarrior EABI w/e, I just didn't bother fixing the def and inlining for the screenshot
  3. I'll try that and report back. I haven't figured out how to refresh decomp but I'll take your word that it just needs a restart

@NWPlayer123
Copy link
Author

NWPlayer123 commented Apr 6, 2019

@caheckman it works, papa bless
Screenshot_450
Screenshot_451

@derek57
Copy link

derek57 commented Apr 26, 2019

@NWPlayer123:

I came across this a few days ago:

https://github.com/aldelaro5/ghidra-gekko-broadway-lang

Would you mind creating a pull request which the guy maintaining this plugin could add there in order to make decompilation of the Broadway / Gekko stuff even better? I mean also this stuff you described here. I came across this as well upon decompilation of the debugger kernel which was a royal pain in the a**...

Thanks in advance.

@aldelaro5
Copy link
Contributor

@NWPlayer123:

I came across this a few days ago:

https://github.com/aldelaro5/ghidra-gekko-broadway-lang

Would you mind creating a pull request which the guy maintaining this plugin could add there in order to make decompilation of the Broadway / Gekko stuff even better? I mean also this stuff you described here. I came across this as well upon decompilation of the debugger kernel which was a royal pain in the a**...

Thanks in advance.

It's already been fixed 20 days ago: aldelaro5/ghidra-gekko-broadway-lang@d4a57a6

@RenaKunisaki
Copy link

Am I doing something wrong? It still doesn't seem to recognize labels relative to r13.

@derek57
Copy link

derek57 commented Dec 12, 2019

Am I doing something wrong? It still doesn't seem to recognize labels relative to r13.

I'm also still having the same issue as you.

@NWPlayer123
Copy link
Author

Did you actually set r2/r13? @derek57 @RenaKunisaki
You need to actually set it, and then modify the address range to the whole thing, maybe a re-analyze, and then it should work.
If you're doing GameCube/Wii, https://github.com/Cuyler36/Ghidra-GameCube-Loader will auto-find the values for you with a custom analysis option.

Screenshot_466
Screenshot_467
Screenshot_468
Screenshot_469

@RenaKunisaki
Copy link

I did get it, but I'm really not sure how. It felt like it just decided to work.

@derek57
Copy link

derek57 commented Dec 16, 2019

Well, I finally got it done - thx to @NWPlayer123.

Isn't it possible to directly implement this into @aldelaro5's Ghidra Gekko repo at https://github.com/aldelaro5/ghidra-gekko-broadway-lang/ ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants