Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5834 fix DWARF file / line issue

dev74736820c8d97c855b
diff --git a/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.java b/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.java
index 5f2b65e49f..1bd7e86625 100644
--- a/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.java
+++ b/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.java
@@ -88,13 +88,7 @@ public class DWARFLineInfoSourceMapScript extends GhidraScript {
for (DWARFCompilationUnit cu : compUnits) {
DWARFLine dLine = cu.getLine();
monitor.increment();
- for (int i = 0; i < dLine.getNumFiles(); ++i) {
- String filePath = dLine.getFilePath(i, true);
- if (filePath == null) {
- continue;
- }
- byte[] md5 = dLine.getFile(i).getMD5();
- SourceFileInfo sfi = new SourceFileInfo(filePath, md5);
+ for (SourceFileInfo sfi : dLine.getAllSourceFileInfos()) {
if (sourceFileInfoToSourceFile.containsKey(sfi)) {
continue;
}
@@ -102,11 +96,11 @@ public class DWARFLineInfoSourceMapScript extends GhidraScript {
continue;
}
try {
- String path = SourceFileUtils.normalizeDwarfPath(filePath,
+ String path = SourceFileUtils.normalizeDwarfPath(sfi.filePath(),
COMPILATION_ROOT_DIRECTORY);
SourceFileIdType type =
- md5 == null ? SourceFileIdType.NONE : SourceFileIdType.MD5;
- SourceFile sFile = new SourceFile(path, type, md5);
+ sfi.md5() == null ? SourceFileIdType.NONE : SourceFileIdType.MD5;
+ SourceFile sFile = new SourceFile(path, type, sfi.md5());
sourceManager.addSourceFile(sFile);
sourceFileInfoToSourceFile.put(sfi, sFile);
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DIEAggregate.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DIEAggregate.java
index 74165170d0..d176c0b0ae 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DIEAggregate.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DIEAggregate.java
@@ -25,6 +25,7 @@ import org.apache.commons.lang3.ArrayUtils;
import ghidra.app.util.bin.format.dwarf.attribs.*;
import ghidra.app.util.bin.format.dwarf.expression.*;
+import ghidra.app.util.bin.format.dwarf.line.DWARFFile;
import ghidra.app.util.bin.format.dwarf.line.DWARFLine;
import ghidra.util.Msg;
@@ -471,9 +472,9 @@ public class DIEAggregate {
}
try {
int fileNum = attr.getUnsignedIntExact();
- DWARFCompilationUnit cu = attrInfo.die.getCompilationUnit();
- DWARFLine line = cu.getLine();
- return line.getFilePath(fileNum, false);
+ DWARFLine line = attrInfo.die.getCompilationUnit().getLine();
+ DWARFFile file = line.getFile(fileNum);
+ return file.getName();
}
catch (IOException e) {
return null;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFImporter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFImporter.java
index 6b6d16d7d3..ce78149b0d 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFImporter.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFImporter.java
@@ -223,13 +223,7 @@ public class DWARFImporter {
for (DWARFCompilationUnit cu : compUnits) {
DWARFLine dLine = cu.getLine();
monitor.increment(1);
- for (int i = 0; i < dLine.getNumFiles(); ++i) {
- String filePath = dLine.getFilePath(i, true);
- if (filePath == null) {
- continue;
- }
- byte[] md5 = dLine.getFile(i).getMD5();
- SourceFileInfo sfi = new SourceFileInfo(filePath, md5);
+ for (SourceFileInfo sfi : dLine.getAllSourceFileInfos()) {
if (sourceFileInfoToSourceFile.containsKey(sfi)) {
continue;
}
@@ -237,11 +231,11 @@ public class DWARFImporter {
continue;
}
try {
- String path = SourceFileUtils.normalizeDwarfPath(filePath,
+ String path = SourceFileUtils.normalizeDwarfPath(sfi.filePath(),
DEFAULT_COMPILATION_DIR);
SourceFileIdType type =
- md5 == null ? SourceFileIdType.NONE : SourceFileIdType.MD5;
- SourceFile sFile = new SourceFile(path, type, md5);
+ sfi.md5() == null ? SourceFileIdType.NONE : SourceFileIdType.MD5;
+ SourceFile sFile = new SourceFile(path, type, sfi.md5());
sourceManager.addSourceFile(sFile);
sourceFileInfoToSourceFile.put(sfi, sFile);
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFFile.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFFile.java
index 35fce39037..38363e60ba 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFFile.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFFile.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,6 +21,7 @@ import java.util.List;
import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.format.dwarf.DWARFCompilationUnit;
import ghidra.app.util.bin.format.dwarf.attribs.*;
+import ghidra.formats.gfilesystem.FSUtilities;
import ghidra.program.model.data.LEB128;
/**
@@ -119,6 +120,7 @@ public class DWARFFile {
* @param directory_index index of the directory for this file
* @param modification_time modification time of the file
* @param length length of the file
+ * @param md5 bytes of md5 hash
*/
public DWARFFile(String name, int directory_index, long modification_time, long length,
byte[] md5) {
@@ -133,6 +135,17 @@ public class DWARFFile {
return this.name;
}
+ public String getPathName(DWARFLine parentLine) {
+ try {
+ String dir = directory_index >= 0 ? parentLine.getDir(directory_index).getName() : "";
+
+ return FSUtilities.appendPath(dir, name);
+ }
+ catch (IOException e) {
+ return name;
+ }
+ }
+
public DWARFFile withName(String newName) {
return new DWARFFile(newName, directory_index, modification_time, length, md5);
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java
index 59aa685071..6fac604dbf 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java
@@ -280,15 +280,12 @@ public class DWARFLine {
try (DWARFLineProgramExecutor lpe = getLineProgramexecutor(cu, reader)) {
List<SourceFileAddr> results = new ArrayList<>();
for (DWARFLineProgramState row : lpe.allRows()) {
- byte[] md5 = null;
- if (cu.getDWARFVersion() >= 5 && (row.file < cu.getLine().getNumFiles())) {
- md5 = cu.getLine().getFile(row.file).getMD5();
+ try {
+ DWARFFile file = getFile(row.file);
+ results.add(new SourceFileAddr(row.address, file.getPathName(this),
+ file.getMD5(), row.line, row.isEndSequence));
}
- String filePath = getFilePath(row.file, true);
- if (filePath != null) {
- results.add(new SourceFileAddr(row.address, filePath, md5, row.line,
- row.isEndSequence));
- } else {
+ catch (IOException e) {
cu.getProgram().getImportSummary().badSourceFileCount++;
}
}
@@ -297,6 +294,15 @@ public class DWARFLine {
}
}
+ public List<SourceFileInfo> getAllSourceFileInfos() {
+ List<SourceFileInfo> result = new ArrayList<>();
+ files.forEach(df -> {
+ // TODO: last_mod info not included yet
+ result.add(new SourceFileInfo(df.getPathName(this), df.getMD5()));
+ });
+ return result;
+ }
+
public DWARFFile getDir(int index) throws IOException {
if (0 <= index && index < directories.size()) {
return directories.get(index);
@@ -308,7 +314,7 @@ public class DWARFLine {
/**
* Get a file name given a file index.
*
- * @param index index of the file
+ * @param index index of the file, where index may not be zero based depending on dwarf version
* @return file {@link DWARFFile}
* @throws IOException if invalid index
*/
@@ -328,30 +334,6 @@ public class DWARFLine {
"Invalid file index %d for line table at 0x%x: ".formatted(index, startOffset));
}
- /**
- * Returns the number of indexed files
- * @return num files
- */
- public int getNumFiles() {
- return files.size();
- }
-
- public String getFilePath(int index, boolean includePath) {
- try {
- DWARFFile f = getFile(index);
- if (!includePath) {
- return f.getName();
- }
-
- String dir = f.getDirectoryIndex() >= 0 ? getDir(f.getDirectoryIndex()).getName() : "";
-
- return FSUtilities.appendPath(dir, f.getName());
- }
- catch (IOException e) {
- return null;
- }
- }
-
@Override
public String toString() {
StringBuffer buffer = new StringBuffer();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/GoFuncData.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/GoFuncData.java
index b1c6d25d32..6cfb3f67a9 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/GoFuncData.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/GoFuncData.java
@@ -399,7 +399,7 @@ public class GoFuncData implements StructureMarkup<GoFuncData> {
sfman.addSourceMapEntry(sourceFile, lineNum, startAddr, len);
}
catch (AddressOverflowException | IllegalArgumentException e) {
- Msg.error(this, "Failed to add source file mapping", e);
+ Msg.error(this, "Failed to add source file mapping: " + e.getMessage());
}
}
}