User:Raphi/Import ROM map from Ghidra: Difference between revisions

From Boktai Hacking Wiki
(Created page with "Limited support for importing a ROM map from Ghidra. '''TODO:''' We need the ability to merge the wikitext output into an already existing page. 1. Export your functions from Ghidra using the following script: <syntaxhighlight lang="c" lines> /* ### * IP: GHIDRA * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apach...")
 
No edit summary
Line 1: Line 1:
Limited support for importing a ROM map from Ghidra. '''TODO:''' We need the ability to merge the wikitext output into an already existing page.
Limited support for importing a ROM map from Ghidra. '''TODO:''' We need the ability to merge the wikitext output into an already existing page.


1. Export your functions from Ghidra using the following script:
Running this script will export all named functions in the current program to a wikitext file:


<syntaxhighlight lang="c" lines>
<syntaxhighlight lang="c" lines>
Line 24: Line 24:
// * Ignores thunks and FUN_ functions
// * Ignores thunks and FUN_ functions


// List function names and entry point addresses to a file in JSON format
// List function names and entry point addresses to a file in wikitext format
//@category Functions
//@category Functions


import java.io.File;
import java.io.File;
import java.io.FileWriter;
import java.io.FileWriter;
import com.google.gson.*;
import com.google.gson.stream.JsonWriter;


import ghidra.app.script.GhidraScript;
import ghidra.app.script.GhidraScript;
Line 45: Line 42:
@Override
@Override
public void run() throws Exception {
public void run() throws Exception {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
File outputFile = askFile("Please Select Output File", "Choose");
File outputFile = askFile("Please Select Output File", "Choose");
JsonWriter jsonWriter = new JsonWriter(new FileWriter(outputFile));
FileWriter fileWriter = new FileWriter(outputFile);
jsonWriter.beginArray();


Listing listing = currentProgram.getListing();
Listing listing = currentProgram.getListing();
Line 64: Line 57:
FunctionSignature sig = f.getSignature();
FunctionSignature sig = f.getSignature();


JsonObject json = new JsonObject();
fileWriter.write("|-\n");
json.addProperty(NAME, name);
fileWriter.write("| 0x");
json.addProperty(ENTRY, entry.toString());
fileWriter.write(entry.toString());
json.addProperty(SIGNATURE, sig.getPrototypeString());
fileWriter.write(" || ");
 
fileWriter.write(sig.getPrototypeString());
gson.toJson(json, jsonWriter);
fileWriter.write("\n");
}
}


jsonWriter.endArray();
fileWriter.close();
jsonWriter.close();
 
println("Wrote functions to " + outputFile);
println("Wrote functions to " + outputFile);
}
}
}
}
</syntaxhighlight>
2. Convert the JSON file you got to WikiText using the following Python script:
<syntaxhighlight lang="python" lines>
# Point this at the .json file generated by Boktai_ExportFunctionInfoScript.java
import json
import sys
with open(sys.argv[1]) as f:
    fns = json.load(f)
for f in fns:
    name = f["name"]
    sig = f["signature"]
    addr = int(f["entry"], 16)
    print( "|-")
    print(f"| 0x{addr:08x} || {sig}")
</syntaxhighlight>
</syntaxhighlight>

Revision as of 09:34, 31 August 2024

Limited support for importing a ROM map from Ghidra. TODO: We need the ability to merge the wikitext output into an already existing page.

Running this script will export all named functions in the current program to a wikitext file:

/* ###
 * IP: GHIDRA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Modified by Raphi for boktaihacking.net:
// * Exports entire signature, not just the name
// * Ignores thunks and FUN_ functions

// List function names and entry point addresses to a file in wikitext format
//@category Functions

import java.io.File;
import java.io.FileWriter;

import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.*;

public class Boktai_ExportFunctions extends GhidraScript {

	private static final String NAME = "name";
	private static final String ENTRY = "entry";
	private static final String SIGNATURE = "signature";

	@Override
	public void run() throws Exception {
		File outputFile = askFile("Please Select Output File", "Choose");
		FileWriter fileWriter = new FileWriter(outputFile);

		Listing listing = currentProgram.getListing();
		FunctionIterator iter = listing.getFunctions(true);
		while (iter.hasNext() && !monitor.isCancelled()) {
			Function f = iter.next();
			if (f.isThunk() || f.getName().startsWith("FUN_")) {
				continue;
			}

			String name = f.getName();
			Address entry = f.getEntryPoint();
			FunctionSignature sig = f.getSignature();

			fileWriter.write("|-\n");
			fileWriter.write("| 0x");
			fileWriter.write(entry.toString());
			fileWriter.write(" || ");
			fileWriter.write(sig.getPrototypeString());
			fileWriter.write("\n");
		}

		fileWriter.close();
		println("Wrote functions to " + outputFile);
	}
}