chaoticgd / ghidra-emotionengine-reloaded

An extension for Ghidra that adds support for the PlayStation 2.
Apache License 2.0
118 stars 11 forks source link

"Memory storage not found for global variable" on interacting with RAM addresses #58

Closed PerikiyoXD closed 3 months ago

PerikiyoXD commented 3 months ago

imagen Tried renaming: imagen

I tried making an overlay for RAM as described here;

https://psi-rockin.github.io/ps2tek/#memorymap

but didn't help.

chaoticgd commented 3 months ago

You can try either creating the rest of EE memory as a non-overlay memory block, or use the savestate importer script (which should do that automatically).

PerikiyoXD commented 3 months ago

I did create an extra memory block after the binary up to the full 32 MB of the size, that made it work.

We could try to automatically create an overlay if that'd make the same effect. I couldn't make it work using a overlay, might be skill issue.

BUT

I've pinpointed a bug in MIPS "J" instructions... https://scc.ustc.edu.cn/zlsc/lxwycj/200910/W020100308600769158777.pdf It seems that the .sinc definition has a bug and does not follow either R5900 or MIPS standard docs.

PerikiyoXD commented 3 months ago

Ghidra: imagen IDA: imagen

PerikiyoXD commented 3 months ago

https://github.com/NationalSecurityAgency/ghidra/pull/4193 Seems that's in the process of being fixed? I hope

chaoticgd commented 3 months ago

Can you give an example of a specific place in a specific game where this is a problem? It looks like a fix has been merged into ghidra's master branch (for the issue you linked), and I'd like to have a proper test case.

Also it's unclear to me what you're trying to show with the ghidra/ida comparsion above.

PerikiyoXD commented 3 months ago

Can you give an example of a specific place in a specific game where this is a problem? It looks like a fix has been merged into ghidra's master branch (for the issue you linked), and I'd like to have a proper test case.

SLES-503.06, 0x002DD620, j instruction.

Would work for any j or jal

Also it's unclear to me what you're trying to show with the ghidra/ida comparsion above.

Proving that the resolved addresses are between ida and ghidra differ, thus making the ghidra output erroneous.

ghidra doesn't properly substract the address on the j/jal, thus pointing to a x2-ish address. seems to be an issue with the MIPS sleigh pcode. Might be PS2 specific but I've checked both ISA manuals: EE Core Instruction Set Manual Version 6.0 and MIPS32™ Architecture For Programmers Volume II: The MIPS32™ Instruction Set

and I suspect the pcode for both MIPS and R5900 processor definitions (which is based from MIPS) are wrong.

For example: SLES-503.06, 0x002DD3E4

ghidra:

002dd3e4 60 db 16 0c                       jal                                                         FUN_005b6d80                                     undefined FUN_005b6d80()

ida

main:002DD3E4 B0 6D 0B 0C                             jal     dword_5B6C8C-0x2DB5CC  # hex(0x5B6C8C-0x2DB5CC) => '0x2db6c0'

# hex(0x5B6C8C-0x2DB5CC) => '0x2db6c0' is the arithmetic logic I've manually calculated. It points to the proper address in ida, doesn't for ghidra.

PerikiyoXD commented 3 months ago

SLES-503.06, 0x0012b0c0 in the main function show that rapidly: imagen

israpps commented 3 months ago

I did create an extra memory block after the binary up to the full 32 MB of the size, that made it work.

We could try to automatically create an overlay if that'd make the same effect. I couldn't make it work using a overlay, might be skill issue.

BUT

I've pinpointed a bug in MIPS "J" instructions... https://scc.ustc.edu.cn/zlsc/lxwycj/200910/W020100308600769158777.pdf It seems that the .sinc definition has a bug and does not follow either R5900 or MIPS standard docs.

What about making it configurable? PSX-DESR has 64mb for EE RAM and Devkits have 128mb

PerikiyoXD commented 3 months ago

I did create an extra memory block after the binary up to the full 32 MB of the size, that made it work. We could try to automatically create an overlay if that'd make the same effect. I couldn't make it work using a overlay, might be skill issue. BUT I've pinpointed a bug in MIPS "J" instructions... https://scc.ustc.edu.cn/zlsc/lxwycj/200910/W020100308600769158777.pdf It seems that the .sinc definition has a bug and does not follow either R5900 or MIPS standard docs.

What about making it configurable? PSX-DESR has 64mb for EE RAM and Devkits have 128mb

I'd probably add a configuration on the processor settings panel to decide RAM size.

chaoticgd commented 3 months ago

SLES-503.06, 0x002DD620, j instruction.

I've checked it and can't reproduce it on the latest version of the extension, although I noticed the executable file has a relocation table.

Are you on the latest version? Earlier versions had a bug where it would incorrectly apply relocations, even for statically linked executable files.

chaoticgd commented 3 months ago

In the dialog for importing a file there should be an option for disabling relocations if you want to test it without updating.

PerikiyoXD commented 3 months ago

Did clone ghidra and self-built it. It still points to bad addresses.

imagen

Trying without relocs.

chaoticgd commented 3 months ago

Sorry I meant the version of this extension, not ghidra.

PerikiyoXD commented 3 months ago

I'm using latest ghidra. Doesn't make any difference so far.

I've just downloaded latest release of ghidra-emotionengine-reloaded.

Same behaviour, unchecked relocs in the import settings.

imagen

PerikiyoXD commented 3 months ago

I'm going to try find a random MIPS .elf file to test if there's this issue in ghidra and check on ida too.

chaoticgd commented 3 months ago

The machine code for that instruction is definitely different to what's shown in my ghidra and your IDA. What does the relocation table window in Ghidra say about that address?

PerikiyoXD commented 3 months ago

Could you provide a screenshot of that main just like I did? Just as visual comparison for me.

The machine code for that instruction is definitely different to what's shown in my ghidra and your IDA. What does the relocation table window in Ghidra say about that address?

"0012b0c0","APPLIED","0x4","0x22e9","5c a2 0b 0c","njWaitVSync"

All relocations:

https://gist.githubusercontent.com/PerikiyoXD/12ef5d1b73030a077b4c4d67485fdb15/raw/40eeb1a9b22d13be638568128782512691c51916/relocations.csv

chaoticgd commented 3 months ago

If relocations are disabled none of them should be being applied. Even if they're enabled they should be being discarded by this logic:

        // Some versions of the Metrowerks compiler produced relocation tables
        // in statically linked executables. We want to disable relocation in
        // those cases.
        if(elfRelocationContext.getElfHeader().isExecutable()) {
            return RelocationResult.SKIPPED;
        }

Here's what I see in Ghidra:

Screenshot_20240322_220634

PerikiyoXD commented 3 months ago

I think this is what you meant: https://github.com/chaoticgd/ghidra-emotionengine-reloaded/assets/3116368/3c91273a-bf1e-4ae9-a1a3-2412a8741eb0

I will try again

PerikiyoXD commented 3 months ago

It didn't change the outcome. I will try rebuilding ghidra-emotionengine-reloaded myself... I guess

PerikiyoXD commented 3 months ago

For reference sake I used https://github.com/chaoticgd/ghidra-emotionengine-reloaded/releases/download/v2.1.13/ghidra_11.0.1_PUBLIC_20240202_ghidra-emotionengine-reloaded.zip

PerikiyoXD commented 3 months ago

Seems that ghidra-emotionengine-reloaded won't compile against latest ghidra main branch:

PS C:\Users\pxd\Desktop\ghidraDevelopment\ghidra-emotionengine-reloaded> gradle build

> Task :compileJava
C:\Users\pxd\Desktop\ghidraDevelopment\ghidra-emotionengine-reloaded\src\main\java\ghidra\emotionengine\relocation\EmotionEngine_ElfRelocationHandler.java:56: error: relocate(ElfRelocationContext,ElfRelocation,Address) in EmotionEngine_ElfRelocationHandler cannot override relocate(ElfRelocationContext,ElfRelocation,Address) in AbstractElfRelocationHandler
        public RelocationResult relocate(ElfRelocationContext elfRelocationContext, ElfRelocation relocation,
                                ^
  overridden method is final
Note: C:\Users\pxd\Desktop\ghidraDevelopment\ghidra-emotionengine-reloaded\src\main\java\ghidra\emotionengine\relocation\EmotionEngine_ElfRelocationHandler.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

> Task :compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
> Run with --info option to get more log output.
> Run with --scan to get full insights.

BUILD FAILED in 5s
1 actionable task: 1 executed
PerikiyoXD commented 3 months ago

Backing to ghidra 11.0.1 to test this

PerikiyoXD commented 3 months ago

It was a version mismatch. I was using ghidra 11.0 and now I used ghidra 11.0.1 and it's working properly.

chaoticgd commented 3 months ago

Okay, good we figured that out. If you don't have any more questions you can close this.