Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-4997: Fixed an exception that could occur in the MachoLoader when an External Program had a space in its name/path (#6989)

Ryan Kurtz419ea484cd74
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java
index 66a1473cb7..c5f0a4498e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java
@@ -24,15 +24,15 @@ import ghidra.app.util.bin.format.macho.dyld.DyldChainedPtr;
import ghidra.app.util.bin.format.macho.dyld.DyldChainedPtr.DyldChainType;
import ghidra.app.util.bin.format.macho.dyld.DyldFixup;
import ghidra.app.util.importer.MessageLog;
+import ghidra.app.util.opinion.MachoProgramBuilder;
import ghidra.program.model.address.Address;
-import ghidra.program.model.listing.Library;
import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.Memory;
import ghidra.program.model.mem.MemoryAccessException;
import ghidra.program.model.reloc.Relocation.Status;
-import ghidra.program.model.symbol.*;
+import ghidra.program.model.symbol.Symbol;
+import ghidra.program.model.symbol.SymbolTable;
import ghidra.util.exception.CancelledException;
-import ghidra.util.exception.InvalidInputException;
import ghidra.util.task.TaskMonitor;
public class DyldChainedFixups {
@@ -165,8 +165,8 @@ public class DyldChainedFixups {
}
if (fixup.symbol() != null && fixup.libOrdinal() != null) {
try {
- fixupExternalLibrary(program, libraryPaths, fixup.libOrdinal(), fixup.symbol(),
- log, monitor);
+ MachoProgramBuilder.fixupExternalLibrary(program, libraryPaths,
+ fixup.libOrdinal(), fixup.symbol());
}
catch (Exception e) {
log.appendMsg("WARNING: Problem fixing up symbol '%s' - %s"
@@ -178,45 +178,6 @@ public class DyldChainedFixups {
return fixedAddrs;
}
- /**
- * Associates the given {@link Symbol} with the correct external {@link Library} (fixing
- * the <EXTERNAL> association)
- *
- * @param program The {@link Program}
- * @param libraryPaths A {@link List} of library paths
- * @param libraryOrdinal The library ordinal
- * @param symbol The {@link Symbol}
- * @param log The log
- * @param monitor A cancellable monitor
- * @throws Exception if an unexpected problem occurs
- */
- private static void fixupExternalLibrary(Program program, List<String> libraryPaths,
- int libraryOrdinal, Symbol symbol, MessageLog log, TaskMonitor monitor)
- throws Exception {
- ExternalManager extManager = program.getExternalManager();
- int libraryIndex = libraryOrdinal - 1;
- if (libraryIndex < 0 || libraryIndex >= libraryPaths.size()) {
- throw new Exception(
- "Library ordinal '%d' outside of expected range".formatted(libraryOrdinal));
- }
- String libraryName = libraryPaths.get(libraryIndex);
- Library library = extManager.getExternalLibrary(libraryName);
- if (library == null) {
- throw new Exception(
- "Library '%s' not found in external program list".formatted(libraryName));
- }
- ExternalLocation loc =
- extManager.getUniqueExternalLocation(Library.UNKNOWN, symbol.getName());
- if (loc != null) {
- try {
- loc.setName(library, symbol.getName(), SourceType.IMPORTED);
- }
- catch (InvalidInputException e) {
- throw new Exception("Symbol name contains illegal characters");
- }
- }
- }
-
//---------------------Below are used only by handled __thread_starts-------------------------
/**
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
index 0b3dfb1526..2463965617 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
@@ -963,7 +963,14 @@ public class MachoProgramBuilder {
space.getAddress(segments.get(binding.getSegmentIndex()).getVMaddress() +
binding.getSegmentOffset());
- fixupExternalLibrary(binding.getLibraryOrdinal(), symbol, libraryPaths);
+ try {
+ fixupExternalLibrary(program, libraryPaths, binding.getLibraryOrdinal(),
+ symbol);
+ }
+ catch (Exception e) {
+ log.appendMsg("WARNING: Problem fixing up symbol '%s' - %s"
+ .formatted(symbol.getName(), e.getMessage()));
+ }
boolean success = false;
try {
@@ -983,20 +990,6 @@ public class MachoProgramBuilder {
}
}
- private void fixupExternalLibrary(int libraryOrdinal, Symbol symbol, List<String> libraryPaths)
- throws InvalidInputException {
- ExternalManager extManager = program.getExternalManager();
- int libraryIndex = libraryOrdinal - 1;
- if (libraryIndex >= 0 && libraryIndex < libraryPaths.size()) {
- Library library = extManager.getExternalLibrary(libraryPaths.get(libraryIndex));
- ExternalLocation loc =
- extManager.getUniqueExternalLocation(Library.UNKNOWN, symbol.getName());
- if (loc != null) {
- loc.setName(library, symbol.getName(), SourceType.IMPORTED);
- }
- }
- }
-
protected void markupHeaders(MachHeader header, Address headerAddr) throws Exception {
monitor.setMessage("Processing header markup...");
@@ -1896,4 +1889,40 @@ public class MachoProgramBuilder {
SourceType.IMPORTED);
}
}
+
+ /**
+ * Associates the given {@link Symbol} with the correct external {@link Library} (fixing
+ * the {@code <EXTERNAL>} association)
+ *
+ * @param program The {@link Program}
+ * @param libraryPaths A {@link List} of library paths
+ * @param libraryOrdinal The library ordinal
+ * @param symbol The {@link Symbol}
+ * @throws Exception if an unexpected problem occurs
+ */
+ public static void fixupExternalLibrary(Program program, List<String> libraryPaths,
+ int libraryOrdinal, Symbol symbol) throws Exception {
+ ExternalManager extManager = program.getExternalManager();
+ int libraryIndex = libraryOrdinal - 1;
+ if (libraryIndex < 0 || libraryIndex >= libraryPaths.size()) {
+ throw new Exception(
+ "Library ordinal '%d' outside of expected range".formatted(libraryOrdinal));
+ }
+ String libraryName = libraryPaths.get(libraryIndex).replaceAll(" ", "_");
+ Library library = extManager.getExternalLibrary(libraryName);
+ if (library == null) {
+ throw new Exception(
+ "Library '%s' not found in external program list".formatted(libraryName));
+ }
+ ExternalLocation loc =
+ extManager.getUniqueExternalLocation(Library.UNKNOWN, symbol.getName());
+ if (loc != null) {
+ try {
+ loc.setName(library, symbol.getName(), SourceType.IMPORTED);
+ }
+ catch (InvalidInputException e) {
+ throw new Exception("Symbol name contains illegal characters");
+ }
+ }
+ }
}