GP-4997: Fixed an exception that could occur in the MachoLoader when an

External Program had a space in its name/path (#6989)
This commit is contained in:
Ryan Kurtz 2024-10-10 07:25:10 -04:00
parent 3de21191eb
commit 419ea484cd
2 changed files with 49 additions and 59 deletions

View File

@ -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-------------------------
/**

View File

@ -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");
}
}
}
}