Index: llvm/docs/CommandGuide/llvm-objdump.rst =================================================================== --- llvm/docs/CommandGuide/llvm-objdump.rst +++ 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. Index: llvm/docs/llvm-objdump.1 =================================================================== --- llvm/docs/llvm-objdump.1 +++ 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 Index: llvm/test/tools/llvm-objdump/X86/source-interleave-absolute-paths.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objdump/X86/source-interleave-absolute-paths.test @@ -0,0 +1,35 @@ +; Test --prefix option. +; UNSUPPORTED: system-windows + +; 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 remove all trailing separators (i.e. '/') in prefix. +; 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-TRAILING1 +; CHECK-TRAILING1: ; int foo() { + + +; The prefix 'myprefix///' is converted to 'myprefix'. + +; RUN: llvm-objdump --prefix myprefix/// --source %t.o 2>&1 | FileCheck %s --check-prefix CHECK-TRAILING2 -DFILE=%t.o +; CHECK-TRAILING2: warning: '[[FILE]]': failed to find source myprefix/Inputs/source-interleave-x86_64.c Index: llvm/tools/llvm-objdump/llvm-objdump.h =================================================================== --- llvm/tools/llvm-objdump/llvm-objdump.h +++ 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; Index: llvm/tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- llvm/tools/llvm-objdump/llvm-objdump.cpp +++ llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -348,6 +348,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, @@ -1026,6 +1030,15 @@ } } + if (!Prefix.empty()) { + if (sys::path::is_absolute(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) @@ -2882,6 +2895,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;