]> granicus.if.org Git - llvm/commitdiff
[llvm-symbolizer] Add switch to adjust addresses by fixed offset
authorJames Henderson <jh7370@my.bristol.ac.uk>
Fri, 25 Jan 2019 11:49:21 +0000 (11:49 +0000)
committerJames Henderson <jh7370@my.bristol.ac.uk>
Fri, 25 Jan 2019 11:49:21 +0000 (11:49 +0000)
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

docs/CommandGuide/llvm-symbolizer.rst
test/tools/llvm-symbolizer/adjust-vma.s [new file with mode: 0644]
tools/llvm-symbolizer/llvm-symbolizer.cpp

index afd94483a424ee29a77c60376d6c6bd44b39f4f4..c6933219dacbce279bd5ecea6e7467e0c3da9a34 100644 (file)
@@ -123,6 +123,12 @@ OPTIONS
 
  Strip directories when printing the file path.
 
+.. option:: -adjust-vma=<offset>
+
+ 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 (file)
index 0000000..5bb5a33
--- /dev/null
@@ -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: ??
index 0134f1c6a1ea1759e4dbdaeaa132528e1d1159ce..15b2b9dddd4309beaf9e79c23a0d27417717f292 100644 (file)
@@ -134,6 +134,11 @@ static cl::opt<int> ClPrintSourceContextLines(
 static cl::opt<bool> ClVerbose("verbose", cl::init(false),
                                cl::desc("Print verbose line info"));
 
+// -adjust-vma
+static cl::opt<unsigned long long>
+    ClAdjustVMA("adjust-vma", cl::init(0), cl::value_desc("offset"),
+                cl::desc("Add specified offset to object file addresses"));
+
 static cl::list<std::string> ClInputAddresses(cl::Positional,
                                               cl::desc("<input addresses>..."),
                                               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());