-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf.py
executable file
·455 lines (371 loc) · 12.7 KB
/
elf.py
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
"""
elf.py
Handle parsing of the DWARF DIE tree and locating variables
"""
from elftools.elf.elffile import ELFFile
if __name__ != "__main__":
from flask import g
else:
class Object():
""" Hack for running main method outside of flask.
"""
pass
from pprint import pprint
# Relevant tags
FCN_TAG = 'DW_TAG_subprogram'
VAR_TAG = 'DW_TAG_variable'
PAR_TAG = 'DW_TAG_formal_parameter'
TYP_TAG = 'DW_TAG_base_type'
DEF_TAG = 'DW_TAG_typedef'
PTR_TAG = 'DW_TAG_pointer_type'
ARR_TAG = 'DW_TAG_array_type'
STC_TAG = 'DW_TAG_structure_type'
ENM_TAG = 'DW_TAG_enumeration_type'
CST_TAG = 'DW_TAG_const_type'
TYPES = (TYP_TAG, ARR_TAG, PTR_TAG, ENM_TAG, STC_TAG, DEF_TAG, CST_TAG)
ENGLISH = {
VAR_TAG: 'local variable',
PAR_TAG: 'parameter'
}
# Relevant attributes
LOC = 'DW_AT_location'
TYPE = 'DW_AT_type'
NAME = 'DW_AT_name'
LINE = 'DW_AT_decl_line'
BASE = 'DW_AT_frame_base'
LOPC = 'DW_AT_low_pc'
HIPC = 'DW_AT_high_pc'
FILE = 'DW_AT_decl_file'
CVAL = 'DW_AT_const_value'
# Forms
EXPR = 'DW_FORM_exprloc'
SECO = 'DW_FORM_sec_offset'
# Relevant conversions
OP_CFA = 0x91
OP_REG = 0x50
OP_BREG = 0x71
regs = [ # Register names in order
'rax', 'rcx', 'rdx', 'rbx',
'rsp', 'rbp', 'rsi', 'rdi',
'r8' , 'r9' , 'r10', 'r11',
'r12', 'r14', 'r14', 'r15'
]
def get_leb128(leb, signed=True):
""" Decode a LEB128 signed integer represented as a list of bytes.
Adapted from pseudocode provided in https://en.wikipedia.org/wiki/LEB128
"""
try:
value = 0
i = 0
while leb[i] & 128 > 0:
value |= (leb[i] & 127) << 7*i
i += 1
if signed:
value |= ((leb[i] & 63) - (leb[i] & 64)) << 7*i
else:
value |= (leb[i] & 127) << 7*i
return value
except:
# Give info about crash
print('LEB128 decoding error: \n\t%s could not be decoded.' % leb)
raise
def find_variables(dwarf):
""" Take a dwarf_info object and generate a dictionary of source code
symbols and stack/register positions.
"""
# Dictionary to hold stuff
symbols = {}
types = {}
eliminated = []
root = list(dwarf.iter_CUs())[0].get_top_DIE()
for die in root.iter_children():
# Walk through top level children, includes functions and types
if die.tag == FCN_TAG:
if g.debug:
print('(func) %d: %s'
% (die.offset, die.tag))
# Handle function, descend
fcn = parse_name(die)
# Add general function attributes to dictionary
symbols[fcn] = {'$info': get_fnc_info(die)}
# Iterate over function's children
for child in die.iter_children():
try:
if g.debug:
print('(var) %d: %s'
% (child.offset, child.tag))
if child.tag == VAR_TAG or child.tag == PAR_TAG:
# Skip variables declared in other files
if FILE in child.attributes and child.attributes[FILE].value != 1:
continue
# Get variable name, type, and locations
name = parse_name(child)
typ = parse_type(child)
line = parse_line(child)
loc = parse_location(child)
# Add to dictionary
symbols[fcn][name] = {
'type' : typ,
'line' : line,
'loc' : loc,
'role' : ENGLISH[child.tag]
}
except:
print('Error parsing debugging entry at %d\n\t(%s, child of %s)'
% (child.offset, child.tag, fcn))
print(child)
raise
elif die.tag in TYPES:
try:
if g.debug:
try: name = die.attributes[NAME].value
except: name = 'none'
print('(type) %d: %s\t%s' % (die.offset, die.tag, name))
# regular types entries with a name
name = parse_name(die)
# pointer types and typedefs have another reference
ref = parse_type(die)
# Save in types dictionary by offset
types[die.offset] = {
'name' : name,
'tag' : die.tag,
'ref' : ref
}
except:
print('Error parsing top level debugging entry at %d\n\t(%s)'
% (die.offset, die.tag))
print(child)
raise
elif g.debug:
print('\n(???) %d: %s' % (die.offset, die.tag))
print(die, '\n')
# Turn type references into actual types
for fcn in symbols:
for name in symbols[fcn]:
if "$" in name: continue
try:
symbols[fcn][name]['type'] = resolve_type(types, symbols[fcn][name]['type'])
except KeyError:
if g.debug:
print('Type %s not found in DIE tree' % symbols[fcn][name]['type'])
return symbols
def parse_name(die):
""" Get the name attribute from a debugging entry.
"""
if NAME in die.attributes:
return die.attributes[NAME].value.decode('UTF-8')
else:
if g.debug:
print('%s with no name attribute' % die.tag)
print(die)
return 'none'
def parse_type(die):
""" Get the type attribute from a debugging entry.
"""
if TYPE in die.attributes:
try:
return die.attributes[TYPE].value.decode('UTF-8')
except:
return die.attributes[TYPE].value
else:
if g.debug:
print('%s with no type' % die.tag)
print(die)
return 0
def parse_line(die):
""" Get declaration line from a debugging entry.
"""
try:
return die.attributes[LINE].value
except:
return 0
def parse_location(die):
""" Take the value of a location attribute (DW_AT_location) loc
and return a string corresponding to its location in memory in
x86-assembly (usually either a register or offset from one).
"""
if LOC in die.attributes:
loc = die.attributes[LOC]
elif CVAL in die.attributes:
return '$' + str(die.attributes[CVAL].value)
else:
return ''
if loc.form != EXPR:
print('Unrecognized location encoding:')
print('\t%s\t%s' % (die.attributes[LOC].form, die.attributes[LOC].value))
return '???'
try:
if hasattr(loc, 'value'):
loc = loc.value
# shitty hack
if type(loc) is int:
loc = [loc]
if loc[0] == OP_CFA:
if len(loc) > 1:
# Indicates (signed) LEB128 offset from base pointer
return get_leb128(loc[1:])
else:
# Not sure what this means, maybe just %rbp ?
return '%rbp'
if loc[0] >= OP_REG and loc[0] < OP_BREG:
# Indicates in-register location
# TODO: figure out size of operand and change register name accordingly
result = regs[loc[0] - OP_REG]
return '%' + result
if loc[0] >= OP_BREG:
if len(loc) > 1:
# Get offset from register
offset = get_leb128(loc[1:])
else:
offset = ''
try:
# Get register
reg = regs[loc[0] - OP_BREG]
return [offset, reg]
except:
return '???'
except:
print('Unable to resolve location: %s' % loc)
try: print('\t(decoded: %s)' % get_leb128(loc))
except: pass
raise
def resolve_type(types, key):
""" Lookup a type in the dictionary generated from the
DWARF tree, taking account of pointers and enumerations
and return a string name of the type.
"""
tag = types[key]['tag']
if tag == TYP_TAG:
# return name with trailing space
return types[key]['name'] + ' '
if tag == PTR_TAG:
newkey = types[key]['ref']
return resolve_type(types, newkey) + '*'
if tag == ARR_TAG:
newkey = types[key]['ref']
return resolve_type(types, newkey) + '[] '
if tag == ENM_TAG:
return 'enum ' + types[key]['name'] + ' '
if tag == DEF_TAG:
suffix = ' ({}) '.format(resolve_type(types, types[key]['ref']))
return types[key]['name'] + suffix
if tag == CST_TAG:
newkey = types[key]['ref']
return 'const ' + resolve_type(types, newkey)
print('Unable to resolve type:\t%s (%s)' % (key, tag))
return tag
def get_fnc_info(die):
""" Get the hi/lo program counter, frame base, and line number of the
function encoded by the debugging informating entity.
"""
info = {}
for att in (BASE, LINE, LOPC, HIPC):
if att in die.attributes:
info[att] = die.attributes[att].value
else:
info[att] = 0
return info
def format_location(loc, offset):
# Format string for indirect addressing
ind = "{}({})"
if type(loc) is int:
# offset from %rbp, corrected by cfa_offset
return ind.format(loc + offset, '%rbp')
if type(loc) is list:
# register + offset pair
return ind.format(loc[0], loc[1])
if type(loc) is str:
# register only
return loc
if g.debug:
print('location format not understood: ' + str(loc))
return '???'
def find_locations(symbols, asm):
# Refactor dictionary and include symbols
locs = {}
# Get cfa offsets from assembly file
name = None
for line in asm:
if "@function" in line:
tokens = line.strip().split()
name = tokens[1].strip(',')
if name is not None and ".cfi_def_cfa_offset" in line:
tokens = line.strip().split()
offset = int(tokens[1])
locs[name] = {'offset' : offset}
# Get symbols by location, corrected by offset
for fcn in symbols:
# skip library functions where we don't have variable delcarations
if fcn not in locs:
continue
# Lookup base pointer offset
for sym in symbols[fcn]:
if sym.startswith('$'): continue
# Translate location into x86-assembly
loc = format_location(symbols[fcn][sym]['loc'], locs[fcn]['offset'])
# Add to dictionary
locs[fcn][loc] = {
'name' : sym,
'type' : symbols[fcn][sym]['type'],
'line' : symbols[fcn][sym]['line'],
'role' : symbols[fcn][sym]['role']
}
return locs
def desc(die, func=print):
""" Apply a function to every node in the the DWARF tree, traversing it
in preorder. """
# Find first element
if hasattr(die, 'iter_CUs'):
die = list(die.iter_CUs())[0]
if hasattr(die, 'get_top_DIE'):
die = die.get_top_DIE()
# Call the function on the first entry
func(die)
# Recur for each child
for child in die.iter_children():
desc(child)
def parse_elf(stream, asm):
""" Take an open stream corresponding to and ELF (.o) file,
and it's corresponding assembly code as a list of strings
and parse the debugging information into a dictionary of
locations and symbols. Close the stream when finished.
"""
try:
# Parse the stream
elf = ELFFile(stream)
dwarf = elf.get_dwarf_info()
# Process elf stuff
syms = find_variables(dwarf)
locs = find_locations(syms, asm)
except:
if g.debug:
desc(dwarf)
raise
finally:
stream.close()
return locs
def print_locs(locs):
""" Pretty output of locations found in DWARF info.
"""
for func in locs:
print(func)
for loc in locs[func]:
if loc == 'offset': continue
print('\t%10s\t%s' % (locs[func][loc]['name'], loc))
if __name__ == "__main__":
from elftools.elf.elffile import ELFFile
import sys, time
# Fake flask global object to give debugging flag
g = Object()
g.debug = True
# Process each file
for filename in sys.argv[1:]:
# Time execution for each file
start = time.time()
with open(filename, 'rb') as ofile:
with open(filename[:-2] + '.s') as sfile:
asm = sfile.readlines()
locs = parse_elf(ofile, asm)
elapsed = 1000 * (time.time() - start)
print('\nLocations: (%.1f ms)' % elapsed)
print_locs(locs)