diff --git a/llvm/docs/CommandGuide/llvm-objdump.rst b/llvm/docs/CommandGuide/llvm-objdump.rst --- a/llvm/docs/CommandGuide/llvm-objdump.rst +++ b/llvm/docs/CommandGuide/llvm-objdump.rst @@ -167,6 +167,10 @@ When disassembling, do not print the raw bytes of each instruction. +.. option:: --prefix= + + When disassembling with the ``--source`` option, prepend ``prefix`` to absolute paths. + .. option:: --print-imm-hex Use hex format when printing immediate values in disassembly output. diff --git a/llvm/docs/llvm-objdump.1 b/llvm/docs/llvm-objdump.1 --- a/llvm/docs/llvm-objdump.1 +++ b/llvm/docs/llvm-objdump.1 @@ -105,6 +105,10 @@ Print no leading headers. .It Fl -no-show-raw-insn When disassembling instructions, do not print the instruction bytes. +.It Fl -prefix Ns = Ns Ar PREFIX +When disassembling, add +.Ar PREFIX +to absolute paths. .It Fl -print-imm-hex Use hex format for immediate values. .It Fl -private-header diff --git a/llvm/test/tools/llvm-objdump/X86/source-interleave-absolute-paths.test b/llvm/test/tools/llvm-objdump/X86/source-interleave-absolute-paths.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objdump/X86/source-interleave-absolute-paths.test @@ -0,0 +1,29 @@ +;; Test --prefix option. + +;; Generate %t with /Inputs/source-interleave-x86_64.c +; RUN: sed -e "s,SRC_COMPDIR,/Inputs,g" %p/Inputs/source-interleave.ll > %t.ll +; RUN: llc -o %t.o -filetype=obj -mtriple=x86_64-pc-linux %t.ll + +;; Generate %t2 with %p/Inputs/source-interleave-x86_64.c +; RUN: sed -e "s,SRC_COMPDIR,%/p/Inputs,g" %p/Inputs/source-interleave.ll > %t2.ll +; RUN: llc -o %t2.o -filetype=obj -mtriple=x86_64-pc-linux %t2.ll + +;; Test invalid source interleave fixed by adding the correct prefix. + +; RUN: llvm-objdump --prefix %p --source %t.o 2>&1 | FileCheck %s --check-prefix CHECK-MISS-PREFIX-FIX +; CHECK-MISS-PREFIX-FIX: ; int foo() { + +;; Test valid source interleave being broken by adding a wrong prefix. + +; RUN: llvm-objdump --prefix myprefix --source %t2.o 2>&1 | FileCheck %s --check-prefix CHECK-BROKEN-PREFIX -DFILE=%t2.o -DROOT=%/p +; CHECK-BROKEN-PREFIX: warning: '[[FILE]]': failed to find source myprefix[[ROOT]]/Inputs/source-interleave-x86_64.c + +;; Test that trailing separators (i.e. '/') are removed from the prefix argument. +;; The prefix '/' is the same as not using the `--prefix` option. + +; RUN: llvm-objdump --prefix / --source %t2.o 2>&1 | FileCheck %s --check-prefix CHECK-MISS-PREFIX-FIX + +;; The prefix 'myprefix///' is converted to 'myprefix'. + +; RUN: llvm-objdump --prefix myprefix/// --source %t.o 2>&1 | FileCheck %s --check-prefix CHECK-TRAILING -DFILE=%t.o +; CHECK-TRAILING: warning: '[[FILE]]': failed to find source myprefix/Inputs/source-interleave-x86_64.c diff --git a/llvm/tools/llvm-objdump/llvm-objdump.h b/llvm/tools/llvm-objdump/llvm-objdump.h --- a/llvm/tools/llvm-objdump/llvm-objdump.h +++ b/llvm/tools/llvm-objdump/llvm-objdump.h @@ -48,6 +48,7 @@ extern cl::opt SymbolTable; extern cl::opt TripleName; extern cl::opt UnwindInfo; +extern cl::opt Prefix; extern StringSet<> FoundSectionSet; diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -353,6 +353,10 @@ cl::cat(ObjdumpCat)); static cl::alias WideShort("w", cl::Grouping, cl::aliasopt(Wide)); +cl::opt objdump::Prefix("prefix", + cl::desc("Add prefix to absolute paths"), + cl::cat(ObjdumpCat)); + enum DebugVarsFormat { DVDisabled, DVUnicode, @@ -417,6 +421,30 @@ /*IncrementIndex=*/true}; } +/// GNU's objdump is_absolute() equivalent. +/// +/// LLVM's is_absolute() matches C++17 behavior that essentially says that the +/// path must be unambiguous (i.e. that there must be no drive letter for +/// Windows). However GNU's objdump treat a path as absolute with or without +/// the drive letter. Therefore in Windows targets '/', '\' and 'c:' are +/// treated as absolute paths by GNU's objdump. +static bool +objdumpIsAbsolute(const Twine &path, + sys::path::Style style = sys::path::Style::native) { + if (style == sys::path::Style::windows) { + SmallString<128> path_storage; + StringRef p = path.toStringRef(path_storage); + + if (p.front() == '/' || p.front() == '\\') + return true; + + if (p.size() >= 2 && (p[0] && p[1] == ':')) + return true; + } + + return sys::path::is_absolute(path, style); +} + SectionFilter objdump::ToolSectionFilter(object::ObjectFile const &O, uint64_t *Idx) { // Start at UINT64_MAX so that the first index returned after an increment is @@ -1031,6 +1059,15 @@ } } + if (!Prefix.empty()) { + if (objdumpIsAbsolute(LineInfo.FileName)) { + SmallString<128> FilePath; + sys::path::append(FilePath, Prefix, LineInfo.FileName); + + LineInfo.FileName = std::string(FilePath); + } + } + if (PrintLines) printLines(OS, LineInfo, Delimiter, LVP); if (PrintSource) @@ -2969,6 +3006,11 @@ if (InputFilenames.empty()) InputFilenames.push_back("a.out"); + // The --prefix option removes the trailing separators from the given prefix.. + if (!Prefix.empty()) + while (sys::path::is_separator(Prefix.back())) + Prefix.pop_back(); + if (AllHeaders) ArchiveHeaders = FileHeaders = PrivateHeaders = Relocations = SectionHeaders = SymbolTable = true;