-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaddr2line.cc
309 lines (271 loc) · 7.44 KB
/
addr2line.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dwarf.h>
#include <elf.h>
#include <string>
#include <vector>
using namespace std;
#ifdef __x86_64__
# define ElfW(x) Elf64##_##x
#else
// There are only x86-64 and x86 in the world!
# define ElfW(x) Elf32##_##x
#endif
typedef unsigned long addr_t;
typedef unsigned char ubyte;
typedef unsigned int uint;
typedef unsigned long ulong;
void error(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
exit(1);
}
char* read_file(const char* file) {
FILE* fp = fopen(file, "rb");
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* buf = (char*)malloc(size);
fread(buf, 1, size, fp);
return buf;
}
ulong uleb128(char*& p) {
ulong r = 0;
int s = 0;
for (;;) {
ubyte b = *(ubyte*)p++;
if (b < 0x80) {
r += b << s;
break;
}
r += (b & 0x7f) << s;
s += 7;
}
return r;
}
long sleb128(char*& p) {
long r = 0;
int s = 0;
for (;;) {
ubyte b = *(ubyte*)p++;
if (b < 0x80) {
if (b & 0x40) {
r -= (0x80 - b) << s;
}
else {
r += (b & 0x3f) << s;
}
break;
}
r += (b & 0x7f) << s;
s += 7;
}
return r;
}
string parse_filename(char*& p,
const vector<const char*>& include_directories) {
const char* filename = p;
while (*p) p++;
p++;
ulong dir = uleb128(p);
const char* dirname = include_directories[dir];
// last modified.
uleb128(p);
// size of the file.
uleb128(p);
if (dirname[0]) {
return string(dirname) + "/" + filename;
} else {
return filename;
}
}
void dump_debug_line_cu(char*& debug_line, unsigned long size) {
char* p = debug_line;
ulong unit_length = *(uint*)p;
p += sizeof(uint);
if (unit_length == 0xffffffff) {
unit_length = *(ulong*)p;
p += sizeof(ulong);
}
char* cu_end = p + unit_length;
int dwarf_version = *(unsigned short*)p;
p += 2;
uint header_length = *(uint*)p;
p += sizeof(uint);
char* cu_start = p + header_length;
uint minimum_instruction_length = *(ubyte*)p;
p++;
bool default_is_stmt = *(ubyte*)p;
p++;
int line_base = *(char*)p;
p++;
uint line_range = *(ubyte*)p;
p++;
uint opcode_base = *(ubyte*)p;
p++;
vector<int> standard_opcode_lengths(opcode_base);
for (int i = 1; i < opcode_base; i++) {
standard_opcode_lengths[i] = *(ubyte*)p;
p++;
}
vector<const char*> include_directories;
include_directories.push_back("");
while (*p) {
include_directories.push_back(p);
//printf("%s\n", p);
while (*p) p++;
p++;
}
p++;
vector<string> filenames;
filenames.push_back("");
while (*p) {
filenames.push_back(parse_filename(p, include_directories));
//printf("%s\n", filenames.back().c_str());
}
p++;
// The registers.
addr_t addr = 0;
uint file = 1;
uint line = 1;
uint column = 0;
bool is_stmt = default_is_stmt;
bool basic_block = false;
bool end_sequence = false;
bool prologue_end = false;
bool epilogue_begin = false;
uint isa = 0;
#define DUMP_LINE() \
do { \
printf("%s:%d: %p\n", filenames[file].c_str(), line, addr); \
basic_block = prologue_end = epilogue_begin = false; \
} while (0)
//printf("%d %d %d\n", p-debug_line, unit_length, header_length);
if (p != cu_start) {
error("Unexpected header size\n");
}
while (p < cu_end) {
ulong a;
ubyte op = *p++;
switch (op) {
case DW_LNS_copy:
DUMP_LINE();
break;
case DW_LNS_advance_pc:
a = uleb128(p);
addr += a;
break;
case DW_LNS_advance_line: {
long a = sleb128(p);
line += a;
//printf("DW_LNS_advance_line %ld => %d\n", a, line);
break;
}
case DW_LNS_set_file:
file = uleb128(p);
break;
case DW_LNS_set_column:
column = uleb128(p);
break;
case DW_LNS_negate_stmt:
is_stmt = !is_stmt;
break;
case DW_LNS_set_basic_block:
basic_block = true;
break;
case DW_LNS_const_add_pc:
a = ((255 - opcode_base) / line_range) * minimum_instruction_length;
addr += a;
break;
case DW_LNS_fixed_advance_pc:
a = *(ubyte*)p++;
addr += a;
break;
case DW_LNS_set_prologue_end:
prologue_end = true;
break;
case DW_LNS_set_epilogue_begin:
epilogue_begin = true;
break;
case DW_LNS_set_isa:
isa = uleb128(p);
break;
case 0:
a = *(ubyte*)p++;
op = *p++;
//printf("extended op: %d size=%d\n", op, a);
switch (op) {
case DW_LNE_end_sequence:
end_sequence = true;
DUMP_LINE();
addr = 0;
file = 1;
line = 1;
column = 0;
is_stmt = default_is_stmt;
end_sequence = false;
isa = 0;
break;
case DW_LNE_set_address:
addr = *(ulong*)p;
p += sizeof(ulong);
break;
case DW_LNE_define_file:
filenames.push_back(parse_filename(p, include_directories));
break;
default:
error("Unknown extended opcode: %d\n", op);
}
break;
default: {
a = op - opcode_base;
uint addr_incr = (a / line_range) * minimum_instruction_length;
int line_incr = line_base + (a % line_range);
addr += addr_incr;
line += line_incr;
//printf("special: addr +%d => %p, line +%d => %d\n",
// addr_incr, addr, line_incr, line);
DUMP_LINE();
}
}
}
debug_line = p;
}
void dump_debug_line(char* debug_line, unsigned long size) {
char* end = debug_line + size;
while (debug_line < end) {
dump_debug_line_cu(debug_line, size);
}
if (debug_line != end) {
error("Unexpected size of debug_line\n");
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
error("Usage: %s ELF-binary\n", argv[0]);
}
char* file = read_file(argv[1]);
ElfW(Ehdr)* ehdr = (ElfW(Ehdr)*)file;
ElfW(Shdr)* shdr = (ElfW(Shdr)*)(file + ehdr->e_shoff);
ElfW(Shdr)* shstr_shdr = shdr + ehdr->e_shstrndx;
char* shstr = file + shstr_shdr->sh_offset;
ElfW(Shdr)* debug_line_shdr = 0;
for (int i = 0; i < ehdr->e_shnum; i++) {
char* name = shstr + shdr[i].sh_name;
if (!strcmp(name, ".debug_line")) {
debug_line_shdr = shdr + i;
break;
}
}
if (!debug_line_shdr) {
error("no debug info?\n");
}
char* debug_line = file + debug_line_shdr->sh_offset;
dump_debug_line(debug_line, debug_line_shdr->sh_size);
free(file);
}