mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2026-09-28 17:11:11 -09:00
GP-2877: Refactoring Loader and AutoImporter to better accommodate loading more than one thing
This commit is contained in:
@@ -22,11 +22,20 @@
|
||||
// list.
|
||||
//
|
||||
//@category Symbol
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import ghidra.app.script.GhidraScript;
|
||||
import ghidra.app.util.importer.MessageLog;
|
||||
import ghidra.app.util.opinion.ElfLoader;
|
||||
import ghidra.app.util.opinion.Loaded;
|
||||
import ghidra.framework.model.*;
|
||||
import ghidra.program.model.listing.Library;
|
||||
import ghidra.program.model.listing.Program;
|
||||
import ghidra.program.util.ELFExternalSymbolResolver;
|
||||
import ghidra.util.Msg;
|
||||
import ghidra.util.exception.VersionException;
|
||||
|
||||
public class FixupELFExternalSymbolsScript extends GhidraScript {
|
||||
|
||||
@@ -38,10 +47,62 @@ public class FixupELFExternalSymbolsScript extends GhidraScript {
|
||||
")");
|
||||
return;
|
||||
}
|
||||
MessageLog msgLog = new MessageLog();
|
||||
ELFExternalSymbolResolver.fixUnresolvedExternalSymbols(currentProgram, false, msgLog,
|
||||
monitor);
|
||||
Msg.info(this, msgLog.toString());
|
||||
MessageLog messageLog = new MessageLog();
|
||||
Object consumer = new Object();
|
||||
ProjectData projectData = currentProgram.getDomainFile().getParent().getProjectData();
|
||||
List<Loaded<Program>> loadedPrograms = new ArrayList<>();
|
||||
|
||||
// Add current program to list
|
||||
loadedPrograms.add(new Loaded<>(currentProgram, currentProgram.getName(),
|
||||
currentProgram.getDomainFile().getPathname()));
|
||||
|
||||
// Add external libraries to list
|
||||
for (Library extLibrary : ELFExternalSymbolResolver.getLibrarySearchList(currentProgram)) {
|
||||
monitor.checkCanceled();
|
||||
String libName = extLibrary.getName();
|
||||
String libPath = extLibrary.getAssociatedProgramPath();
|
||||
if (libPath == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DomainFile libDomainFile = projectData.getFile(libPath);
|
||||
if (libDomainFile == null) {
|
||||
messageLog.appendMsg("Referenced external program not found: " + libPath);
|
||||
continue;
|
||||
}
|
||||
|
||||
DomainObject libDomainObject = null;
|
||||
try {
|
||||
libDomainObject =
|
||||
libDomainFile.getDomainObject(consumer, false, false, monitor);
|
||||
if (libDomainObject instanceof Program program) {
|
||||
loadedPrograms.add(new Loaded<>(program, libName, libPath));
|
||||
}
|
||||
else {
|
||||
messageLog
|
||||
.appendMsg("Referenced external program is not a program: " + libPath);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
// failed to open library
|
||||
messageLog.appendMsg("Failed to open library dependency project file: " +
|
||||
libDomainFile.getPathname());
|
||||
}
|
||||
catch (VersionException e) {
|
||||
messageLog.appendMsg(
|
||||
"Referenced external program requires updgrade, unable to consider symbols: " +
|
||||
libPath);
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve symbols
|
||||
ELFExternalSymbolResolver.fixUnresolvedExternalSymbols(loadedPrograms, messageLog, monitor);
|
||||
|
||||
// Cleanup
|
||||
for (int i = 1; i < loadedPrograms.size(); i++) {
|
||||
loadedPrograms.get(i).release(consumer);
|
||||
}
|
||||
Msg.info(this, messageLog.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
/* ###
|
||||
* IP: GHIDRA
|
||||
* REVIEWED: YES
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,15 +16,17 @@
|
||||
//Imports all programs from a selected directory.
|
||||
//@category Import
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import ghidra.app.script.GhidraScript;
|
||||
import ghidra.app.util.importer.AutoImporter;
|
||||
import ghidra.app.util.importer.MessageLog;
|
||||
import ghidra.app.util.opinion.LoadResults;
|
||||
import ghidra.framework.model.DomainFolder;
|
||||
import ghidra.program.model.lang.LanguageCompilerSpecPair;
|
||||
import ghidra.program.model.listing.Program;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ImportAllProgramsFromADirectoryScript extends GhidraScript {
|
||||
|
||||
@Override
|
||||
@@ -52,36 +53,23 @@ public class ImportAllProgramsFromADirectoryScript extends GhidraScript {
|
||||
continue;
|
||||
}
|
||||
|
||||
Program program = null;
|
||||
|
||||
LoadResults<Program> loadResults = null;
|
||||
try {
|
||||
program = importFile(file);
|
||||
loadResults = AutoImporter.importByLookingForLcs(file, state.getProject(),
|
||||
folder.getPathname(), language.getLanguage(), language.getCompilerSpec(), this,
|
||||
log, monitor);
|
||||
loadResults.getPrimary().save(state.getProject(), log, monitor);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (program == null) {
|
||||
try {
|
||||
program =
|
||||
AutoImporter.importByLookingForLcs(file, folder, language.getLanguage(),
|
||||
language.getCompilerSpec(), this, log, monitor);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (program == null) {
|
||||
catch (IOException e) {
|
||||
println("Unable to import program from file " + file.getName());
|
||||
}
|
||||
else {
|
||||
//openProgram( program );
|
||||
program.release(this);
|
||||
finally {
|
||||
if (loadResults != null) {
|
||||
loadResults.release(this);
|
||||
}
|
||||
}
|
||||
|
||||
println(log.toString());
|
||||
|
||||
log.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user