From 861c0c93d442291f42bd1d9578608139f1218934 Mon Sep 17 00:00:00 2001 From: James <49045138+ghidracadabra@users.noreply.github.com> Date: Thu, 3 Apr 2025 14:12:45 +0000 Subject: [PATCH] GP-5561 correct dwarf source file path parsing error --- .../DWARFLineInfoSourceMapScript.java | 4 ++-- .../app/util/bin/format/dwarf/DWARFImporter.java | 4 ++-- .../src/main/java/ghidra/util/SourceFileUtils.java | 13 ++++++++----- .../program/database/sourcemap/SourceFileTest.java | 14 ++++++++------ 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.java b/Ghidra/Features/Base/ghidra_scripts/DWARFLineInfoSourceMapScript.java index 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.java index 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.java index 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.java index 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)