Merge remote-tracking branch

'origin/GP-5834_dev747368_dwarfline_sourcefile_iterate' (Closes #8329)
This commit is contained in:
Ryan Kurtz 2025-07-21 06:11:06 -04:00
commit 32782037a9
6 changed files with 43 additions and 59 deletions

View file

@ -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);
}

View file

@ -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;

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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();

View file

@ -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());
}
}
}