Skip to content

Commit

Permalink
BinExport: Ghidra: Export module mapping for imported functions
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 627454882
Change-Id: I9fb6e24ef6897b3d9d351caacecc2fe1aff7a19b
  • Loading branch information
mike-hunhoff authored and copybara-github committed Apr 23, 2024
1 parent 3173592 commit 39f6445
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.FunctionManager;
import ghidra.program.model.listing.Instruction;
import ghidra.program.model.listing.Library;
import ghidra.program.model.listing.Listing;
import ghidra.program.model.listing.Parameter;
import ghidra.program.model.listing.Program;
Expand Down Expand Up @@ -368,7 +369,7 @@ private void buildFlowGraphs(Map<Long, Integer> basicBlockIndices) throws Cancel
}
}

private void buildCallGraph() throws CancelledException {
private void buildCallGraphAndModuleList() throws CancelledException {
var callGraph = builder.getCallGraphBuilder();
FunctionManager funcManager = program.getFunctionManager();
monitor.setIndeterminate(false);
Expand All @@ -377,6 +378,7 @@ private void buildCallGraph() throws CancelledException {
int id = 0;
TreeMap<Address, Function> orderedFunctions = new TreeMap<>();
Map<Long, Integer> vertexIndices = new HashMap<>();
Map<String, Integer> moduleIndices = new HashMap<>();

// First round, create ordered function mapping because Ghidra does not guarantee the order of
// external (imported) functions and BinExport requires ordered vertices
Expand Down Expand Up @@ -410,6 +412,21 @@ private void buildCallGraph() throws CancelledException {
}
if (func.isExternal()) {
vertex.setType(BinExport2.CallGraph.Vertex.Type.IMPORTED);
// Add module mapping
String moduleName = func.getParentNamespace().getName();
if (!moduleName.equals(Library.UNKNOWN)) {
if (moduleName.contains(".")) {
moduleName = moduleName.substring(0, moduleName.lastIndexOf('.'));
}
Integer moduleId =
moduleIndices.computeIfAbsent(
moduleName,
(String k) -> {
builder.addModuleBuilder().setName(k);
return builder.getModuleCount() - 1;
});
vertex.setModuleIndex(moduleId);
}
}
if (!func.getName().equals(SymbolUtilities.getDefaultFunctionName(func.getEntryPoint()))) {
// Ghidra does not seem to provide both mangled and demangled names
Expand Down Expand Up @@ -701,7 +718,7 @@ public BinExport2 build(TaskMonitor taskMonitor) throws CancelledException {
monitor.setMessage("Exporting flow graphs");
buildFlowGraphs(basicBlockIndices);
monitor.setMessage("Exporting call graph");
buildCallGraph();
buildCallGraphAndModuleList();
buildSections();

return builder.build();
Expand Down

0 comments on commit 39f6445

Please sign in to comment.