Ghidra is a software reverse engineering (SRE) framework
GP-5561 correct dwarf source file path parsing error
James861c0c93d442
diff --git a/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.java b/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.javaindex 5477145c49..09ca2640b4 100644--- a/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.java+++ b/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.java@@ -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());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.javaindex 580d1c3695..8efbb5e14e 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@@ -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());diff --git a/Ghidra/Features/Base/src/main/java/ghidra/util/SourceFileUtils.java b/Ghidra/Features/Base/src/main/java/ghidra/util/SourceFileUtils.javaindex 98cc002509..8c80185a65 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/util/SourceFileUtils.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/util/SourceFileUtils.java@@ -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();diff --git a/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/database/sourcemap/SourceFileTest.java b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/database/sourcemap/SourceFileTest.javaindex 4d754485e0..7c4b9ed7c3 100644--- a/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/database/sourcemap/SourceFileTest.java+++ b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/program/database/sourcemap/SourceFileTest.java@@ -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)