Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-0 fix NPE in DWARF external debug info logic

dev747368b2781bd22f9e
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/ExternalDebugInfo.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/ExternalDebugInfo.java
index a9b1541f5f..c6e82a7b05 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/ExternalDebugInfo.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/ExternalDebugInfo.java
@@ -23,7 +23,7 @@ import ghidra.program.model.listing.Program;
import ghidra.util.NumericUtilities;
/**
- * Information needed to find an ELF/DWARF external debug file, found in an ELF binary's
+ * Metadata needed to find an ELF/DWARF external debug file, retrieved from an ELF binary's
* ".gnu_debuglink" section and/or ".note.gnu.build-id" section.
* <p>
* The debuglink can provide a filename and crc of the external debug file, while the build-id
@@ -31,38 +31,79 @@ import ghidra.util.NumericUtilities;
*/
public class ExternalDebugInfo {
+ /**
+ * Create a new {@link ExternalDebugInfo} from information found in the specified program.
+ *
+ * @param program {@link Program} to query
+ * @return new {@link ExternalDebugInfo} or null if no external debug metadata found in
+ * program
+ */
public static ExternalDebugInfo fromProgram(Program program) {
GnuDebugLinkSectionValues debugLinkValues = GnuDebugLinkSection.fromProgram(program);
GnuBuildIdValues buildIdValues = GnuBuildIdSection.fromProgram(program);
if (buildIdValues != null && !buildIdValues.isValid()) {
buildIdValues = null;
}
- if (debugLinkValues == null || buildIdValues == null) {
+ if (debugLinkValues == null && buildIdValues == null) {
return null;
}
- return new ExternalDebugInfo(debugLinkValues.getFilename(), debugLinkValues.getCrc(),
- buildIdValues != null ? buildIdValues.getDescription() : null);
+ String filename = debugLinkValues != null ? debugLinkValues.getFilename() : null;
+ int crc = debugLinkValues != null ? debugLinkValues.getCrc() : 0;
+ byte[] hash = buildIdValues != null ? buildIdValues.getDescription() : null;
+ return new ExternalDebugInfo(filename, crc, hash);
}
private String filename;
private int crc;
private byte[] hash;
+ /**
+ * Constructor to create an {@link ExternalDebugInfo} instance.
+ *
+ * @param filename filename of external debug file, or null
+ * @param crc crc32 of external debug file, or 0 if no filename
+ * @param hash build-id hash digest found in ".note.gnu.build-id" section, or null if
+ * not present
+ */
public ExternalDebugInfo(String filename, int crc, byte[] hash) {
this.filename = filename;
this.crc = crc;
this.hash = hash;
}
+ /**
+ * Return true if there is a filename
+ *
+ * @return boolean true if filename is available, false if not
+ */
+ public boolean hasFilename() {
+ return filename != null && !filename.isBlank();
+ }
+
+ /**
+ * Return the filename of the external debug file, or null if not specified.
+ *
+ * @return String filename of external debug file, or null if not specified
+ */
public String getFilename() {
return filename;
}
+ /**
+ * Return the crc of the external debug file. Not valid if filename is missing.
+ *
+ * @return int crc32 of external debug file.
+ */
public int getCrc() {
return crc;
}
+ /**
+ * Return the build-id hash digest.
+ *
+ * @return byte array containing the build-id hash (usually 20 bytes)
+ */
public byte[] getHash() {
return hash;
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/LocalDirectorySearchLocation.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/LocalDirectorySearchLocation.java
index 25c51cd222..f87c5f56e0 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/LocalDirectorySearchLocation.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/LocalDirectorySearchLocation.java
@@ -15,9 +15,8 @@
*/
package ghidra.app.util.bin.format.dwarf4.external;
-import java.util.zip.CRC32;
-
import java.io.*;
+import java.util.zip.CRC32;
import ghidra.formats.gfilesystem.FSRL;
import ghidra.formats.gfilesystem.FileSystemService;
@@ -81,11 +80,10 @@ public class LocalDirectorySearchLocation implements SearchLocation {
@Override
public FSRL findDebugFile(ExternalDebugInfo debugInfo, TaskMonitor monitor)
throws CancelledException, IOException {
- String filename = debugInfo.getFilename();
- if (filename == null || filename.isBlank()) {
+ if (!debugInfo.hasFilename()) {
return null;
}
- ensureSafeFilename(filename);
+ ensureSafeFilename(debugInfo.getFilename());
return findFile(searchDir, debugInfo, monitor);
}
@@ -98,6 +96,9 @@ public class LocalDirectorySearchLocation implements SearchLocation {
FSRL findFile(File dir, ExternalDebugInfo debugInfo, TaskMonitor monitor)
throws IOException, CancelledException {
+ if (!debugInfo.hasFilename()) {
+ return null;
+ }
File file = new File(dir, debugInfo.getFilename());
if (file.isFile()) {
int fileCRC = calcCRC(file);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/SameDirSearchLocation.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/SameDirSearchLocation.java
index e511f33131..1e7be9de7c 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/SameDirSearchLocation.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/external/SameDirSearchLocation.java
@@ -80,6 +80,9 @@ public class SameDirSearchLocation implements SearchLocation {
@Override
public FSRL findDebugFile(ExternalDebugInfo debugInfo, TaskMonitor monitor)
throws IOException, CancelledException {
+ if (!debugInfo.hasFilename()) {
+ return null;
+ }
File file = new File(progDir, debugInfo.getFilename());
if (!file.isFile()) {
return null;