Skip to content

Commit 759d5e6

Browse files
committedJan 25, 2019
[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 llvm-svn: 352195
1 parent 7822d25 commit 759d5e6

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
 

‎llvm/docs/CommandGuide/llvm-symbolizer.rst

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ OPTIONS
123123

124124
Strip directories when printing the file path.
125125

126+
.. option:: -adjust-vma=<offset>
127+
128+
Add the specified offset to object file addresses when performing lookups. This
129+
can be used to simplify lookups when the object is not loaded at a dynamically
130+
relocated address.
131+
126132
EXIT STATUS
127133
-----------
128134

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# REQUIRES: x86-registered-target
2+
3+
.type foo,@function
4+
.size foo,12
5+
foo:
6+
.space 10
7+
nop
8+
nop
9+
10+
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o -g
11+
12+
# RUN: llvm-symbolizer 0xa 0xb --print-address --obj=%t.o \
13+
# RUN: | FileCheck %s --check-prefix=NORMAL
14+
# RUN: llvm-symbolizer 0x10a 0x10b --print-address --adjust-vma 0x100 --obj=%t.o \
15+
# RUN: | FileCheck %s --check-prefix=ADJUST
16+
17+
# Show that we can handle addresses less than the offset.
18+
# RUN: llvm-symbolizer 0xa 0xb --print-address --adjust-vma 0xc --obj=%t.o \
19+
# RUN: | FileCheck %s --check-prefix=OVERFLOW
20+
21+
# NORMAL: 0xa
22+
# NORMAL-NEXT: foo
23+
# NORMAL-NEXT: adjust-vma.s:7:0
24+
# NORMAL-EMPTY:
25+
# NORMAL-NEXT: 0xb
26+
# NORMAL-NEXT: foo
27+
# NORMAL-NEXT: adjust-vma.s:8:0
28+
29+
# ADJUST: 0x10a
30+
# ADJUST-NEXT: foo
31+
# ADJUST-NEXT: adjust-vma.s:7:0
32+
# ADJUST-EMPTY:
33+
# ADJUST-NEXT: 0x10b
34+
# ADJUST-NEXT: foo
35+
# ADJUST-NEXT: adjust-vma.s:8:0
36+
37+
# OVERFLOW: 0xa
38+
# OVERFLOW-NEXT: ??
39+
# OVERFLOW-NEXT: ??

‎llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ static cl::opt<int> ClPrintSourceContextLines(
134134
static cl::opt<bool> ClVerbose("verbose", cl::init(false),
135135
cl::desc("Print verbose line info"));
136136

137+
// -adjust-vma
138+
static cl::opt<unsigned long long>
139+
ClAdjustVMA("adjust-vma", cl::init(0), cl::value_desc("offset"),
140+
cl::desc("Add specified offset to object file addresses"));
141+
137142
static cl::list<std::string> ClInputAddresses(cl::Positional,
138143
cl::desc("<input addresses>..."),
139144
cl::ZeroOrMore);
@@ -201,6 +206,7 @@ static void symbolizeInput(StringRef InputString, LLVMSymbolizer &Symbolizer,
201206
StringRef Delimiter = ClPrettyPrint ? ": " : "\n";
202207
outs() << Delimiter;
203208
}
209+
ModuleOffset -= ClAdjustVMA;
204210
if (IsData) {
205211
auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset);
206212
Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get());

0 commit comments

Comments
 (0)
Please sign in to comment.