From: James Henderson Date: Fri, 25 Jan 2019 11:49:21 +0000 (+0000) Subject: [llvm-symbolizer] Add switch to adjust addresses by fixed offset X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0dc3042203fe0c435286affab1b44d85436c4148;p=llvm [llvm-symbolizer] Add switch to adjust addresses by fixed offset If a stack trace or similar has a list of addresses from an executable or DSO loaded at a variable address (e.g. due to ASLR), the addresses will not directly correspond to the addresses stored in the object file. If a user wishes to use llvm-symbolizer, they have to subtract the load address from every address. This is somewhat inconvenient, especially as the output of --print-address will result in the adjusted address being listed, rather than the address coming from the stack trace, making it harder to map results between the two. This change adds a new switch to llvm-symbolizer --adjust-vma which takes an offset, which is then used to automatically do this calculation. The printed address remains the input address (allowing for easy mapping), whilst the specified offset is applied to the addresses when performing the lookup. The switch is conceptually similar to llvm-objdump's new switch of the same name (see D57051), which in turn mirrors a GNU switch. There is no equivalent switch in addr2line. Reviewed by: grimar Differential Revision: https://reviews.llvm.org/D57151 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@352195 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/CommandGuide/llvm-symbolizer.rst b/docs/CommandGuide/llvm-symbolizer.rst index afd94483a42..c6933219dac 100644 --- a/docs/CommandGuide/llvm-symbolizer.rst +++ b/docs/CommandGuide/llvm-symbolizer.rst @@ -123,6 +123,12 @@ OPTIONS Strip directories when printing the file path. +.. option:: -adjust-vma= + + Add the specified offset to object file addresses when performing lookups. This + can be used to simplify lookups when the object is not loaded at a dynamically + relocated address. + EXIT STATUS ----------- diff --git a/test/tools/llvm-symbolizer/adjust-vma.s b/test/tools/llvm-symbolizer/adjust-vma.s new file mode 100644 index 00000000000..5bb5a336a52 --- /dev/null +++ b/test/tools/llvm-symbolizer/adjust-vma.s @@ -0,0 +1,39 @@ +# REQUIRES: x86-registered-target + +.type foo,@function +.size foo,12 +foo: + .space 10 + nop + nop + +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o -g + +# RUN: llvm-symbolizer 0xa 0xb --print-address --obj=%t.o \ +# RUN: | FileCheck %s --check-prefix=NORMAL +# RUN: llvm-symbolizer 0x10a 0x10b --print-address --adjust-vma 0x100 --obj=%t.o \ +# RUN: | FileCheck %s --check-prefix=ADJUST + +# Show that we can handle addresses less than the offset. +# RUN: llvm-symbolizer 0xa 0xb --print-address --adjust-vma 0xc --obj=%t.o \ +# RUN: | FileCheck %s --check-prefix=OVERFLOW + +# NORMAL: 0xa +# NORMAL-NEXT: foo +# NORMAL-NEXT: adjust-vma.s:7:0 +# NORMAL-EMPTY: +# NORMAL-NEXT: 0xb +# NORMAL-NEXT: foo +# NORMAL-NEXT: adjust-vma.s:8:0 + +# ADJUST: 0x10a +# ADJUST-NEXT: foo +# ADJUST-NEXT: adjust-vma.s:7:0 +# ADJUST-EMPTY: +# ADJUST-NEXT: 0x10b +# ADJUST-NEXT: foo +# ADJUST-NEXT: adjust-vma.s:8:0 + +# OVERFLOW: 0xa +# OVERFLOW-NEXT: ?? +# OVERFLOW-NEXT: ?? diff --git a/tools/llvm-symbolizer/llvm-symbolizer.cpp b/tools/llvm-symbolizer/llvm-symbolizer.cpp index 0134f1c6a1e..15b2b9dddd4 100644 --- a/tools/llvm-symbolizer/llvm-symbolizer.cpp +++ b/tools/llvm-symbolizer/llvm-symbolizer.cpp @@ -134,6 +134,11 @@ static cl::opt ClPrintSourceContextLines( static cl::opt ClVerbose("verbose", cl::init(false), cl::desc("Print verbose line info")); +// -adjust-vma +static cl::opt + ClAdjustVMA("adjust-vma", cl::init(0), cl::value_desc("offset"), + cl::desc("Add specified offset to object file addresses")); + static cl::list ClInputAddresses(cl::Positional, cl::desc("..."), cl::ZeroOrMore); @@ -201,6 +206,7 @@ static void symbolizeInput(StringRef InputString, LLVMSymbolizer &Symbolizer, StringRef Delimiter = ClPrettyPrint ? ": " : "\n"; outs() << Delimiter; } + ModuleOffset -= ClAdjustVMA; if (IsData) { auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset); Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get());