Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1596 DWARF helper methods

dev7473683d2148da15eb
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/DWARFAnalyzer.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/DWARFAnalyzer.java
index 689748c3e7..6773377546 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/DWARFAnalyzer.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/DWARFAnalyzer.java
@@ -80,6 +80,18 @@ public class DWARFAnalyzer extends AbstractAnalyzer {
private static final String DWARF_ANALYZER_DESCRIPTION =
"Automatically extracts DWARF info from an ELF file.";
+ /**
+ * Returns true if DWARF has already been imported into the specified program.
+ *
+ * @param program {@link Program} to check
+ * @return true if DWARF has already been imported, false if not yet
+ */
+ public static boolean isAlreadyImported(Program program) {
+ Options propList = program.getOptions(Program.PROGRAM_INFO);
+ return propList.getBoolean(DWARF_LOADED_OPTION_NAME, false) ||
+ oldCheckIfDWARFImported(program);
+ }
+
private DWARFImportOptions importOptions = new DWARFImportOptions();
private long lastTxId = -1;
@@ -110,10 +122,7 @@ public class DWARFAnalyzer extends AbstractAnalyzer {
}
lastTxId = txId;
- Options propList = program.getOptions(Program.PROGRAM_INFO);
- boolean alreadyLoaded = propList.getBoolean(DWARF_LOADED_OPTION_NAME, false) ||
- oldCheckIfDWARFImported(program);
- if (alreadyLoaded) {
+ if (isAlreadyImported(program)) {
Msg.info(this, "DWARF already imported, skipping.");
return false;
}
@@ -140,6 +149,7 @@ public class DWARFAnalyzer extends AbstractAnalyzer {
DWARFImportSummary parseResults = dp.parse();
parseResults.logSummaryResults();
}
+ Options propList = program.getOptions(Program.PROGRAM_INFO);
propList.setBoolean(DWARF_LOADED_OPTION_NAME, true);
dsp.updateProgramInfo(program);
return true;
@@ -161,7 +171,7 @@ public class DWARFAnalyzer extends AbstractAnalyzer {
}
@Deprecated(forRemoval = true, since = "10.0")
- private boolean oldCheckIfDWARFImported(Program prog) {
+ private static boolean oldCheckIfDWARFImported(Program prog) {
// this was the old way of checking if the DWARF analyzer had already been run. Keep
// it around for a little bit so existing programs that have already imported DWARF data
// don't get re-run. Remove after a release or two.
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java
index 2d05720910..e9296f5540 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java
@@ -65,7 +65,7 @@ public class DWARFProgram implements Closeable {
* for more info.
* <p>
* @param program {@link Program} to test
- * @return boolean true if program has DWARF info, false if not
+ * @return boolean true if program probably has DWARF info, false if not
*/
public static boolean isDWARF(Program program) {
String format = Objects.requireNonNullElse(program.getExecutableFormat(), "");
@@ -81,6 +81,27 @@ public class DWARFProgram implements Closeable {
return false;
}
+ /**
+ * Returns true if the specified {@link Program program} has DWARF information.
+ * <p>
+ * This is similar to {@link #isDWARF(Program)}, but is a stronger check that is more
+ * expensive as it could involve searching for external files.
+ * <p>
+ *
+ * @param program {@link Program} to test
+ * @param monitor {@link TaskMonitor} that can be used to cancel
+ * @return boolean true if the program has DWARF info, false if not
+ */
+ public static boolean hasDWARFData(Program program, TaskMonitor monitor) {
+ if (!isDWARF(program)) {
+ return false;
+ }
+ try (DWARFSectionProvider dsp =
+ DWARFSectionProviderFactory.createSectionProviderFor(program, monitor)) {
+ return dsp != null;
+ }
+ }
+
private final Program program;
private DWARFImportOptions importOptions;
private DWARFNameInfo rootDNI =