Merge remote-tracking branch 'origin/patch'

This commit is contained in:
Ryan Kurtz
2025-04-03 13:18:59 -04:00
4 changed files with 20 additions and 15 deletions

View File

@@ -144,9 +144,9 @@ public class DWARFLineInfoSourceMapScript extends GhidraScript {
SourceFile source = sfasToSourceFiles.get(sourceFileAddr);
if (source == null) {
String path = SourceFileUtils.fixDwarfRelativePath(sourceFileAddr.fileName(),
COMPILATION_ROOT_DIRECTORY);
try {
String path = SourceFileUtils.normalizeDwarfPath(sourceFileAddr.fileName(),
COMPILATION_ROOT_DIRECTORY);
SourceFileIdType type =
sourceFileAddr.md5() == null ? SourceFileIdType.NONE : SourceFileIdType.MD5;
source = new SourceFile(path, type, sourceFileAddr.md5());

View File

@@ -283,9 +283,9 @@ public class DWARFImporter {
SourceFile source = sfasToSourceFiles.get(sfa);
if (source == null) {
String path = SourceFileUtils.fixDwarfRelativePath(sfa.fileName(),
DEFAULT_COMPILATION_DIR);
try {
String path = SourceFileUtils.normalizeDwarfPath(sfa.fileName(),
DEFAULT_COMPILATION_DIR);
SourceFileIdType type =
sfa.md5() == null ? SourceFileIdType.NONE : SourceFileIdType.MD5;
source = new SourceFile(path, type, sfa.md5());

View File

@@ -152,17 +152,19 @@ public class SourceFileUtils {
}
/**
* Corrects potentially relative paths encountered in DWARF debug info.
* Relative paths are based at /{@code baseDir}/. If normalization of "/../" subpaths
* results in a path "above" /{@code baseDir}/, the returned path will be based at "baseDir_i"
* where i is the count of initial "/../" in the normalized path.
* Normalizes paths encountered in DWARF debug info.
* Relative paths are made absolute with base /{@code baseDir}/. If normalization of "/../"
* subpaths results in a path "above" /{@code baseDir}/, the returned path will be based at
* "baseDir_i" where i is the count of initial "/../" in the normalized path.
* Additionally, any backslashes are converted to forward slashes (backslashes can occur in
* files produced by MinGW).
* @param path path to normalize
* @param baseDir name of artificial root directory
* @return normalized path
* @throws IllegalArgumentException if the path is not valid or if baseDir contains a
* non-alphanumeric, non-underscore character
*/
public static String fixDwarfRelativePath(String path, String baseDir) {
public static String normalizeDwarfPath(String path, String baseDir) {
if (StringUtils.isEmpty(baseDir)) {
throw new IllegalArgumentException("baseDir cannot be empty");
}
@@ -176,6 +178,7 @@ public class SourceFileUtils {
path = "/" + baseDir + path.substring(1);
based = true;
}
path = FSUtilities.normalizeNativePath(path);
try {
URI uri = new URI("file", null, path, null).normalize();
path = uri.getPath();

View File

@@ -76,17 +76,19 @@ public class SourceFileTest extends AbstractSourceFileTest {
public void testFixDwarfRelativePath() {
String baseDirName = "root_dir";
assertEquals("/src/file.c",
SourceFileUtils.fixDwarfRelativePath("/src/file.c", baseDirName));
SourceFileUtils.normalizeDwarfPath("/src/file.c", baseDirName));
assertEquals("/file.c",
SourceFileUtils.fixDwarfRelativePath("/src/../file.c", baseDirName));
SourceFileUtils.normalizeDwarfPath("/src/../file.c", baseDirName));
assertEquals("/root_dir/file.c",
SourceFileUtils.fixDwarfRelativePath("./file.c", baseDirName));
SourceFileUtils.normalizeDwarfPath("./file.c", baseDirName));
assertEquals("/root_dir_1/file.c",
SourceFileUtils.fixDwarfRelativePath("/../file.c", baseDirName));
SourceFileUtils.normalizeDwarfPath("/../file.c", baseDirName));
assertEquals("/root_dir_2/file.c",
SourceFileUtils.fixDwarfRelativePath("/.././../file.c", baseDirName));
SourceFileUtils.normalizeDwarfPath("/.././../file.c", baseDirName));
assertEquals("/root_dir_1/file.c",
SourceFileUtils.fixDwarfRelativePath("./../file.c", baseDirName));
SourceFileUtils.normalizeDwarfPath("./../file.c", baseDirName));
assertEquals("/C:/Users/test/src/dir1/file.c",
SourceFileUtils.normalizeDwarfPath("C:\\Users\\test/src/dir1/file.c", baseDirName));
}
@Test(expected = IllegalArgumentException.class)