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) { for (DWARFCompilationUnit cu : compUnits) {
DWARFLine dLine = cu.getLine(); DWARFLine dLine = cu.getLine();
monitor.increment(); monitor.increment();
for (int i = 0; i < dLine.getNumFiles(); ++i) { for (SourceFileInfo sfi : dLine.getAllSourceFileInfos()) {
String filePath = dLine.getFilePath(i, true);
if (filePath == null) {
continue;
}
byte[] md5 = dLine.getFile(i).getMD5();
SourceFileInfo sfi = new SourceFileInfo(filePath, md5);
if (sourceFileInfoToSourceFile.containsKey(sfi)) { if (sourceFileInfoToSourceFile.containsKey(sfi)) {
continue; continue;
} }
@ -102,11 +96,11 @@ public class DWARFLineInfoSourceMapScript extends GhidraScript {
continue; continue;
} }
try { try {
String path = SourceFileUtils.normalizeDwarfPath(filePath, String path = SourceFileUtils.normalizeDwarfPath(sfi.filePath(),
COMPILATION_ROOT_DIRECTORY); COMPILATION_ROOT_DIRECTORY);
SourceFileIdType type = SourceFileIdType type =
md5 == null ? SourceFileIdType.NONE : SourceFileIdType.MD5; sfi.md5() == null ? SourceFileIdType.NONE : SourceFileIdType.MD5;
SourceFile sFile = new SourceFile(path, type, md5); SourceFile sFile = new SourceFile(path, type, sfi.md5());
sourceManager.addSourceFile(sFile); sourceManager.addSourceFile(sFile);
sourceFileInfoToSourceFile.put(sfi, 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.attribs.*;
import ghidra.app.util.bin.format.dwarf.expression.*; 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.app.util.bin.format.dwarf.line.DWARFLine;
import ghidra.util.Msg; import ghidra.util.Msg;
@ -471,9 +472,9 @@ public class DIEAggregate {
} }
try { try {
int fileNum = attr.getUnsignedIntExact(); int fileNum = attr.getUnsignedIntExact();
DWARFCompilationUnit cu = attrInfo.die.getCompilationUnit(); DWARFLine line = attrInfo.die.getCompilationUnit().getLine();
DWARFLine line = cu.getLine(); DWARFFile file = line.getFile(fileNum);
return line.getFilePath(fileNum, false); return file.getName();
} }
catch (IOException e) { catch (IOException e) {
return null; return null;

View file

@ -223,13 +223,7 @@ public class DWARFImporter {
for (DWARFCompilationUnit cu : compUnits) { for (DWARFCompilationUnit cu : compUnits) {
DWARFLine dLine = cu.getLine(); DWARFLine dLine = cu.getLine();
monitor.increment(1); monitor.increment(1);
for (int i = 0; i < dLine.getNumFiles(); ++i) { for (SourceFileInfo sfi : dLine.getAllSourceFileInfos()) {
String filePath = dLine.getFilePath(i, true);
if (filePath == null) {
continue;
}
byte[] md5 = dLine.getFile(i).getMD5();
SourceFileInfo sfi = new SourceFileInfo(filePath, md5);
if (sourceFileInfoToSourceFile.containsKey(sfi)) { if (sourceFileInfoToSourceFile.containsKey(sfi)) {
continue; continue;
} }
@ -237,11 +231,11 @@ public class DWARFImporter {
continue; continue;
} }
try { try {
String path = SourceFileUtils.normalizeDwarfPath(filePath, String path = SourceFileUtils.normalizeDwarfPath(sfi.filePath(),
DEFAULT_COMPILATION_DIR); DEFAULT_COMPILATION_DIR);
SourceFileIdType type = SourceFileIdType type =
md5 == null ? SourceFileIdType.NONE : SourceFileIdType.MD5; sfi.md5() == null ? SourceFileIdType.NONE : SourceFileIdType.MD5;
SourceFile sFile = new SourceFile(path, type, md5); SourceFile sFile = new SourceFile(path, type, sfi.md5());
sourceManager.addSourceFile(sFile); sourceManager.addSourceFile(sFile);
sourceFileInfoToSourceFile.put(sfi, sFile); sourceFileInfoToSourceFile.put(sfi, sFile);
} }

View file

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 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.BinaryReader;
import ghidra.app.util.bin.format.dwarf.DWARFCompilationUnit; import ghidra.app.util.bin.format.dwarf.DWARFCompilationUnit;
import ghidra.app.util.bin.format.dwarf.attribs.*; import ghidra.app.util.bin.format.dwarf.attribs.*;
import ghidra.formats.gfilesystem.FSUtilities;
import ghidra.program.model.data.LEB128; import ghidra.program.model.data.LEB128;
/** /**
@ -119,6 +120,7 @@ public class DWARFFile {
* @param directory_index index of the directory for this file * @param directory_index index of the directory for this file
* @param modification_time modification time of the file * @param modification_time modification time of the file
* @param length length 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, public DWARFFile(String name, int directory_index, long modification_time, long length,
byte[] md5) { byte[] md5) {
@ -133,6 +135,17 @@ public class DWARFFile {
return this.name; 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) { public DWARFFile withName(String newName) {
return new DWARFFile(newName, directory_index, modification_time, length, md5); 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)) { try (DWARFLineProgramExecutor lpe = getLineProgramexecutor(cu, reader)) {
List<SourceFileAddr> results = new ArrayList<>(); List<SourceFileAddr> results = new ArrayList<>();
for (DWARFLineProgramState row : lpe.allRows()) { for (DWARFLineProgramState row : lpe.allRows()) {
byte[] md5 = null; try {
if (cu.getDWARFVersion() >= 5 && (row.file < cu.getLine().getNumFiles())) { DWARFFile file = getFile(row.file);
md5 = cu.getLine().getFile(row.file).getMD5(); results.add(new SourceFileAddr(row.address, file.getPathName(this),
file.getMD5(), row.line, row.isEndSequence));
} }
String filePath = getFilePath(row.file, true); catch (IOException e) {
if (filePath != null) {
results.add(new SourceFileAddr(row.address, filePath, md5, row.line,
row.isEndSequence));
} else {
cu.getProgram().getImportSummary().badSourceFileCount++; 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 { public DWARFFile getDir(int index) throws IOException {
if (0 <= index && index < directories.size()) { if (0 <= index && index < directories.size()) {
return directories.get(index); return directories.get(index);
@ -308,7 +314,7 @@ public class DWARFLine {
/** /**
* Get a file name given a file index. * 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} * @return file {@link DWARFFile}
* @throws IOException if invalid index * @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)); "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 @Override
public String toString() { public String toString() {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();

View file

@ -399,7 +399,7 @@ public class GoFuncData implements StructureMarkup<GoFuncData> {
sfman.addSourceMapEntry(sourceFile, lineNum, startAddr, len); sfman.addSourceMapEntry(sourceFile, lineNum, startAddr, len);
} }
catch (AddressOverflowException | IllegalArgumentException e) { 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());
} }
} }
} }