Skip to content

Commit dcc3043

Browse files
committed
GP-4145 improve External Symbol Resolver analyzer handling of binaries
The analyzer wasn't handling external library binaries that needed to be updated because the Ghidra version changed, and wasn't logging any error messages or other useful diagnostic info.
1 parent 1a47a82 commit dcc3043

File tree

3 files changed

+412
-220
lines changed

3 files changed

+412
-220
lines changed

Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/ExternalSymbolResolverAnalyzer.java

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,16 @@
1515
*/
1616
package ghidra.app.plugin.core.analysis;
1717

18-
import java.io.IOException;
19-
import java.util.ArrayList;
20-
import java.util.List;
21-
2218
import ghidra.app.services.*;
2319
import ghidra.app.util.importer.MessageLog;
24-
import ghidra.app.util.opinion.*;
25-
import ghidra.framework.model.*;
20+
import ghidra.app.util.opinion.ElfLoader;
21+
import ghidra.app.util.opinion.MachoLoader;
2622
import ghidra.framework.options.Options;
2723
import ghidra.program.model.address.AddressSetView;
28-
import ghidra.program.model.listing.Library;
2924
import ghidra.program.model.listing.Program;
3025
import ghidra.program.util.ExternalSymbolResolver;
26+
import ghidra.util.Msg;
3127
import ghidra.util.exception.CancelledException;
32-
import ghidra.util.exception.VersionException;
3328
import ghidra.util.task.TaskMonitor;
3429

3530
/**
@@ -61,7 +56,7 @@ public boolean canAnalyze(Program program) {
6156
if (program.getDomainFile().getParent() == null) {
6257
return false;
6358
}
64-
59+
6560
Options options = program.getOptions(Program.PROGRAM_INFO);
6661
String format = options.getString("Executable Format", null);
6762
return ElfLoader.ELF_NAME.equals(format) || MachoLoader.MACH_O_NAME.equals(format);
@@ -71,65 +66,16 @@ public boolean canAnalyze(Program program) {
7166
public boolean added(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log)
7267
throws CancelledException {
7368

74-
Object consumer = new Object();
75-
log = new MessageLog(); // For now, we don't want the analysis log spammed
76-
ProjectData projectData = program.getDomainFile().getParent().getProjectData();
77-
List<Loaded<Program>> loadedPrograms = new ArrayList<>();
78-
79-
// Add program to list
80-
loadedPrograms.add(new Loaded<>(program, program.getName(),
81-
program.getDomainFile().getParent().getPathname()));
82-
83-
// Add external libraries to list
84-
for (Library extLibrary : ExternalSymbolResolver.getLibrarySearchList(program)) {
85-
monitor.checkCancelled();
86-
String libPath = extLibrary.getAssociatedProgramPath();
87-
if (libPath == null) {
88-
continue;
89-
}
90-
91-
DomainFile libDomainFile = projectData.getFile(libPath);
92-
if (libDomainFile == null) {
93-
log.appendMsg("Referenced external program not found: " + libPath);
94-
continue;
95-
}
96-
97-
try {
98-
DomainObject libDomainObject =
99-
libDomainFile.getDomainObject(consumer, false, false, monitor);
100-
if (libDomainObject instanceof Program p) {
101-
loadedPrograms.add(new Loaded<>(p, libDomainFile.getName(),
102-
libDomainFile.getParent().getPathname()));
103-
}
104-
else {
105-
libDomainObject.release(consumer);
106-
log.appendMsg("Referenced external program is not a program: " + libPath);
107-
}
69+
try (ExternalSymbolResolver esr = new ExternalSymbolResolver(
70+
program.getDomainFile().getParent().getProjectData(), monitor)) {
71+
esr.addProgramToFixup(program);
72+
esr.fixUnresolvedExternalSymbols();
73+
esr.logInfo(s -> Msg.info(this, s), false);
74+
if (esr.hasProblemLibraries()) {
75+
// causes a popup message at end of analysis session
76+
esr.logInfo(log::appendMsg, true);
10877
}
109-
catch (IOException e) {
110-
log.appendMsg("Failed to open library dependency project file: " +
111-
libDomainFile.getPathname());
112-
}
113-
catch (VersionException e) {
114-
log.appendMsg(
115-
"Referenced external program requires updgrade, unable to consider symbols: " +
116-
libPath);
117-
}
118-
}
119-
120-
// Resolve symbols
121-
try {
122-
ExternalSymbolResolver.fixUnresolvedExternalSymbols(loadedPrograms, false, log,
123-
monitor);
12478
return true;
12579
}
126-
catch (IOException e) {
127-
return false;
128-
}
129-
finally {
130-
for (int i = 1; i < loadedPrograms.size(); i++) {
131-
loadedPrograms.get(i).release(consumer);
132-
}
133-
}
13480
}
13581
}

Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfLoader.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,16 @@ protected void postLoadProgramFixups(List<Loaded<Program>> loadedPrograms, Proje
158158
throws CancelledException, IOException {
159159
super.postLoadProgramFixups(loadedPrograms, project, options, messageLog, monitor);
160160

161-
ExternalSymbolResolver.fixUnresolvedExternalSymbols(loadedPrograms, true, messageLog,
162-
monitor);
161+
try (ExternalSymbolResolver esr =
162+
new ExternalSymbolResolver(project.getProjectData(), monitor)) {
163+
for (Loaded<Program> loadedProgram : loadedPrograms) {
164+
esr.addProgramToFixup(
165+
loadedProgram.getProjectFolderPath() + loadedProgram.getName(),
166+
loadedProgram.getDomainObject());
167+
}
168+
esr.fixUnresolvedExternalSymbols();
169+
esr.logInfo(messageLog::appendMsg, true);
170+
}
163171
}
164172

165173
@Override

0 commit comments

Comments
 (0)