Skip to content

Commit ae9edcc

Browse files
Lin Chencopybara-github
authored andcommitted
Ghidra: Try to promote uncaught undefined function before building the flow graphs.
PiperOrigin-RevId: 855584599 Change-Id: Ib062515259290388bdb3a007d0ef46c063d204d4
1 parent dcc099c commit ae9edcc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

java/src/main/java/com/google/security/binexport/BinExport2Builder.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.security.zynamics.BinExport.BinExport2;
2424
import com.google.security.zynamics.BinExport.BinExport2.Builder;
2525
import ghidra.app.nav.NavigationUtils;
26+
import ghidra.program.database.function.OverlappingFunctionException;
2627
import ghidra.program.database.symbol.EquateDB;
2728
import ghidra.program.model.address.Address;
2829
import ghidra.program.model.address.AddressSetView;
@@ -58,7 +59,9 @@
5859
import ghidra.program.model.symbol.Symbol;
5960
import ghidra.program.model.symbol.SymbolUtilities;
6061
import ghidra.program.util.DefinedDataIterator;
62+
import ghidra.util.UndefinedFunction;
6163
import ghidra.util.exception.CancelledException;
64+
import ghidra.util.exception.InvalidInputException;
6265
import ghidra.util.task.TaskMonitor;
6366
import java.io.File;
6467
import java.util.ArrayList;
@@ -744,6 +747,41 @@ private void buildBasicBlocks(
744747
}
745748
}
746749

750+
private void promoteUndefinedFunctions() throws CancelledException {
751+
FunctionManager funcManager = program.getFunctionManager();
752+
753+
for (var bbIter = bbModel.getCodeBlocks(monitor); bbIter.hasNext(); ) {
754+
CodeBlock bb = bbIter.next();
755+
Address bbEntryPoint = bb.getFirstStartAddress();
756+
// If a function is already defined or external, no need to promote it.
757+
Function func = funcManager.getFunctionContaining(bbEntryPoint);
758+
if (func != null && func.isExternal()) {
759+
continue;
760+
}
761+
UndefinedFunction undefinedFunction =
762+
UndefinedFunction.findFunction(program, bbEntryPoint, monitor);
763+
if (undefinedFunction != null) {
764+
try {
765+
Function newFunc =
766+
funcManager.createFunction(
767+
undefinedFunction.getName(),
768+
undefinedFunction.getEntryPoint(),
769+
undefinedFunction.getBody(),
770+
undefinedFunction.getSignatureSource());
771+
if (newFunc != null) {
772+
monitor.setMessage(
773+
String.format("Created undefined function at %x", getMappedAddress(bbEntryPoint)));
774+
System.out.printf("newUndefinedFunction: %x%n", getMappedAddress(bbEntryPoint));
775+
}
776+
} catch (InvalidInputException | OverlappingFunctionException e) {
777+
monitor.setMessage(
778+
String.format(
779+
"Failed to create undefined function at %x", getMappedAddress(bbEntryPoint)));
780+
}
781+
}
782+
}
783+
}
784+
747785
private void buildFlowGraphs(Map<Long, Integer> basicBlockIndices) throws CancelledException {
748786
FunctionManager funcManager = program.getFunctionManager();
749787
monitor.setIndeterminate(false);
@@ -1146,6 +1184,7 @@ public BinExport2 build(TaskMonitor taskMonitor) throws CancelledException {
11461184
buildStrings(instructionIndices);
11471185
buildDataReferences(instructionIndices);
11481186

1187+
promoteUndefinedFunctions();
11491188
monitor.setMessage("Exporting flow graphs");
11501189
buildFlowGraphs(basicBlockIndices);
11511190
monitor.setMessage("Exporting call graph");

0 commit comments

Comments
 (0)