Eli Bendersky | c4a4c07 | 2011-09-17 15:28:28 +0300 | [diff] [blame] | 1 | # Just a script for playing around with pyelftools during testing |
| 2 | # please ignore it! |
| 3 | # |
| 4 | |
eliben | c6db4c4 | 2011-09-20 16:03:59 +0300 | [diff] [blame] | 5 | import sys, pprint |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 6 | from elftools.elf.structs import ELFStructs |
| 7 | from elftools.elf.elffile import ELFFile |
| 8 | from elftools.elf.sections import * |
| 9 | |
| 10 | # read a little-endian, 64-bit file |
| 11 | es = ELFStructs(True, 64) |
| 12 | |
eliben | 7f13df2 | 2011-09-26 11:17:03 +0300 | [diff] [blame] | 13 | stream = open('tests/testfiles/exe_simple64.elf', 'rb') |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 14 | #stream = open('binfiles/z32.elf', 'rb') |
| 15 | |
| 16 | efile = ELFFile(stream) |
eliben | 033b44f | 2011-09-19 15:48:39 +0300 | [diff] [blame] | 17 | print efile.elfclass, efile.little_endian |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 18 | print '===> %s sections!' % efile.num_sections() |
eliben | 54e39b2 | 2011-09-19 13:10:57 +0300 | [diff] [blame] | 19 | |
eliben | 033b44f | 2011-09-19 15:48:39 +0300 | [diff] [blame] | 20 | print efile.has_dwarf_info() |
| 21 | |
eliben | ce5ec71 | 2011-09-20 10:20:32 +0300 | [diff] [blame] | 22 | dwarfinfo = efile.get_dwarf_info() |
eliben | 3b9ad82 | 2011-09-22 11:46:26 +0300 | [diff] [blame] | 23 | |
eliben | 8eff3ee | 2011-10-02 09:50:37 +0200 | [diff] [blame] | 24 | cu = dwarfinfo.get_CU(3) |
eliben | 3b9ad82 | 2011-09-22 11:46:26 +0300 | [diff] [blame] | 25 | print 'CU header', cu.header |
eliben | 8eb8e7f | 2011-09-22 13:17:07 +0300 | [diff] [blame] | 26 | topdie = cu.get_top_DIE() |
| 27 | |
eliben | 8eff3ee | 2011-10-02 09:50:37 +0200 | [diff] [blame] | 28 | c = topdie.iter_children().next() |
| 29 | |
| 30 | print c |
| 31 | |
| 32 | print 'siblings.....' |
| 33 | |
| 34 | for s in c.iter_siblings(): |
| 35 | print s |
| 36 | |
| 37 | #~ print c.get_parent() |
| 38 | #~ print topdie |
| 39 | |
| 40 | #~ def recp(d, indent=0): |
| 41 | #~ s = str(d) |
| 42 | #~ lines = s.split('\n') |
| 43 | #~ print '\n'.join(' ' * indent + l for l in lines) |
| 44 | |
| 45 | #~ for c in d.iter_children(): |
| 46 | #~ recp(c, indent + 6) |
| 47 | |
| 48 | #~ recp(topdie) |
| 49 | |
| 50 | #~ for c in topdie.iter_children(): |
| 51 | #~ print c |
| 52 | #~ for die in cu._dielist: |
| 53 | #~ print 'DIE %s, size=%s' % (die.tag, die.size) |
| 54 | #~ for attrname, val in die.attributes.iteritems(): |
| 55 | #~ print ' ', attrname, val |
eliben | 7f13df2 | 2011-09-26 11:17:03 +0300 | [diff] [blame] | 56 | |
| 57 | #~ topdie = cu.get_top_DIE() |
| 58 | |
| 59 | #~ print topdie.size, topdie.tag |
| 60 | |
| 61 | #~ print len(cu._dielist) |
eliben | a98028e | 2011-09-21 12:09:53 +0300 | [diff] [blame] | 62 | |
eliben | c6db4c4 | 2011-09-20 16:03:59 +0300 | [diff] [blame] | 63 | #~ print dwarfinfo.structs.Dwarf_abbrev_entry.parse('\x13\x01\x01\x03\x50\x04\x00\x00') |
| 64 | |
eliben | a98028e | 2011-09-21 12:09:53 +0300 | [diff] [blame] | 65 | #~ abbrevtable = dwarfinfo.get_abbrev_table(95) |
| 66 | #~ print id(abbrevtable) |
| 67 | #~ pprint.pprint(abbrevtable._abbrev_map) |
eliben | c6db4c4 | 2011-09-20 16:03:59 +0300 | [diff] [blame] | 68 | |
eliben | a98028e | 2011-09-21 12:09:53 +0300 | [diff] [blame] | 69 | #~ ab1 = abbrevtable.get_abbrev(2) |
| 70 | #~ print ab1.has_children() |
| 71 | #~ for name, form in ab1.iter_attr_specs(): |
| 72 | #~ print name, form |
| 73 | |
| 74 | #~ print dwarfinfo.get_abbrev_table(0).get_abbrev(1).has_children() |
eliben | c6db4c4 | 2011-09-20 16:03:59 +0300 | [diff] [blame] | 75 | |
| 76 | #~ for cu in dwarfinfo._CU: |
| 77 | #~ print cu, cu.header |
| 78 | |
| 79 | |
eliben | 033b44f | 2011-09-19 15:48:39 +0300 | [diff] [blame] | 80 | |
| 81 | |
| 82 | #~ print efile.get_section_by_name('.debug_info').name |
eliben | 54e39b2 | 2011-09-19 13:10:57 +0300 | [diff] [blame] | 83 | |
| 84 | #~ print '===> %s segments!' % efile.num_segments() |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 85 | |
eliben | 033b44f | 2011-09-19 15:48:39 +0300 | [diff] [blame] | 86 | #~ for sec in efile.iter_sections(): |
| 87 | #~ print type(sec), sec.name |
| 88 | #~ if isinstance(sec, SymbolTableSection): |
| 89 | #~ print ' linked string table:', sec.stringtable.name |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 90 | |
eliben | 54e39b2 | 2011-09-19 13:10:57 +0300 | [diff] [blame] | 91 | #~ for seg in efile.iter_segments(): |
| 92 | #~ print type(seg), seg['p_type'], seg['p_offset'] |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 93 | |
eliben | 54e39b2 | 2011-09-19 13:10:57 +0300 | [diff] [blame] | 94 | #~ for sec in efile.iter_sections(): |
| 95 | #~ if isinstance(sec, SymbolTableSection): |
| 96 | #~ print 'symbol table "%s ~~~"' % sec.name |
| 97 | #~ for sym in sec.iter_symbols(): |
| 98 | #~ print '%-26s %s %s' % (sym.name, sym['st_info']['type'], sym['st_info']['bind']) |
Eli Bendersky | a42f82d | 2011-09-09 06:02:47 +0300 | [diff] [blame] | 99 | |