Home > Software > TDR: Need For Speed 4
TDR: Need For Speed 4
This is a basic walkthrough of decompiling the 1999-02-22 prototype version of Need For Speed 4 using Ghidra and This Dust Remembers What It Once Was.
- In this version of Need For Speed 4, everything is already named in a way that requires no changes. Copy NFS4.EXE, and NFS4.SYM to your working directory.
- Open a command prompt or PowerShell prompt and change directory to your working directory.
- Convert NFS4.EXE to ELF format by running the following command:
PlayStationELFConverter.exe --exe2elf NFS4.EXE NFS4.ELF > PlayStationELFConverter_Log.txt 2>&1
- Generate the JSON version of the debug symbols by running the following command:
SymDumpTE.exe --debug --ignore-duplicate-definitions --rename-for-compatibility --auto-rename-fakes --inline-fakes --json NFS4.SYM NFS4.json > SymDumpTE_Log.txt 2>&1
- Generate the header/stub files, Ghidra scripts, and an updated/extended/mapped version of the JSON debug symbol file by running the following command:
CreateSkeleton.exe --create-playstation-memory --assume-sn-gp-base --map-sld-functions --name NFS4 --externs-to-labels --output-updated-json NFS4-Mapped.json --output Output NFS4.json > CreateSkeleton_Log.txt 2>&1
- Examine the contents of README-NFS4-CreateSkeleton-Manual_Changes_Required.txt in the Output directory. If you want to follow up on any of the recommendations in it, do so now, then re-run the previous CreateSkeleton.exe command.
- Need for Speed 4's header file contains a declaration of wchar_t which conflicts with the default one in Ghidra. So you'll need to make a quick detour to remove that.
- Open the file Output/NFS4.H in your favourite text editor.
- Search for the line which reads:
typedef unsigned char wchar_t;
- Delete that entire line.
- Save the file and exit.
- Launch Ghidra, and create a new project. For the base directory, use the Output directory created by TDR.
- Import the ELF file you generated earlier. Ghidra will default to 64-bit MIPS, which is wrong. Click the ... button next to the Language field. Scroll up in the list and choose MIPS/default/32/little/default processor architecture, which will show up as MIPS:LE:32:default:default in the import file window. Click OK to begin the import.
- Close the import summary dialogue.
- Double-click on the ELF in the project list.
- An Analyze prompt will appear. Click No, because you don't want that to happen until the debug symbols have been imported.
- From the Edit menu, choose Tool Options.
- Expand Decompiler, and select Analysis. Uncheck Eliminate unreachable code. Click OK.
- From the File menu, choose Parse C Source option. Click the green plus sign button. Open the NFS4.H file in the Output directory. Click Parse to Program.
- Click Parse to Program. Click Continue. Click Continue?.
- After a moment, you should receive a message indicating that the header has been parsed successfully. If you don't, make sure you resolved any naming conflicts in the JSON file, re-run the CreateSkeleton.exe above, and then re-import the NFS4.H file. Otherwise, Click OK, then click Dismiss.
- Copy the NFS4TDRAggressiveArrayIdentification.java, NFS4TDRDecompile.java, NFS4TDRDefineFunctions.java, NFS4TDRExportData.java, and NFS4MapMemoryAndCreateLabels.java scripts from the Output/ghidra_files/ directory into your own Ghidra scripts directory (probably something like C:\Users\yourname\ghidra_scripts). Note: these files are dynamically generated, so you will need to re-copy them (overwriting the existing copies if necessary) every time they change, or when working on multiple projects.
- In Ghidra, from the Window menu, choose Script Manager option.
- In the Script Manager window, click on the the NFS4TDRMapMemoryAndCreateLabels.java entry, then click the green-and-white play button in the upper-right corner of the window. This script creates any necessary PlayStation memory segments and applies labels found in the debug symbols.
- After a noticeable delay, you should see a NFS4TDRMapMemoryAndCreateLabels.java> Finished! message in the console at the bottom of the main Ghidra window.
- Use the Script Manager to execute the NFS4TDRDefineFunctions.java entry. This script imports function definitions and a few other things from the debug symbols.
- From the Analysis menu, choose Auto Analyze 'NFS4.ELF'. Check the Decompiler Parameter ID box if it's not already checked. Switch to the MIPS Constant Reference Analyzer section. Uncheck Recover global GP register writes if it's checked. Optionally, check Attempt to recover switch tables. Click Analyze.
- Wait for the analysis to complete (progress is in the lower-right corner of the main Ghidra window.
- Optional, but highly recommended: use the Script Manager to execute the NFS4TDRAggressiveArrayIdentification.java entry. The options in the script popup are preset by TDR - you shouldn't need to change them in most cases. This script attempts to detect cases where a global variable exists with embedded data in the PlayStation binary, but Ghidra has only identified the first element of the entire array. It will generally do a very good job, but some manual cleanup work may be necessary later.
- Do any manual reverse-engineering work you want in Ghidra. This may be a very lengthy step if you're trying to rebuild a working binary. If you're just trying things out, you can skip this for now and come back to it later.
- When you're ready to proceed with generating source code, go back to the Script Manager window, and run the NFS4TDRDecompile.java script. Click OK in the popup - the location of the output file is preset by TDR, and you shouldn't change it in normal use.
- Wait for the decompilation to happen. This will usually take quite awhile (15-30 minutes or more) with Need for Speed 4. You should see a NFS4TDRDecompile.java> Finished! message in the console at the bottom of the main Ghidra window when it's complete.
- In the Script Manager window, run the NFS4TDRExportData.java script and wait for it to finish. The options in the script popup are preset by TDR - you shouldn't need to change them in most cases. This script will create a file named XPRTDATA.C in your output directory which contains C code that should create any embedded data from the game binary which is referenced by the decompiled code (global variables, etc.).
- Back in the command prompt, create another set of C source code files which contain the decompiled functions and global variable data output by Ghidra by running the following command:
PopulateSkeleton.exe --name NFS4 --input-json NFS4-Mapped.json --input-source Output\NFS4.C --input-data Output\XPRTDATA.C --output Output > PopulateSkeleton_Log.txt 2>&1
- Examine the contents of Output/PRIMARY/source-decompiled, which should contain TDR's best attempt at reconstructing the original source code in all of the separate files that were originally used. Anything not matched to one of those files will be placed in THISDUST.C or THISDUST.H instead.