Added dwarf/compileunit.py and dwarf/dwarfinfo.py and started filling in details
[pyelftools.git] / z.py
1 # Just a script for playing around with pyelftools during testing
2 # please ignore it!
3 #
4
5 import sys
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
13 stream = open('tests/testfiles/z.elf', 'rb')
14 #stream = open('binfiles/z32.elf', 'rb')
15
16 efile = ELFFile(stream)
17
18 print '===> %s sections!' % efile.num_sections()
19 print '===> %s segments!' % efile.num_segments()
20
21 for sec in efile.iter_sections():
22 print type(sec), sec.name
23 if isinstance(sec, SymbolTableSection):
24 print ' linked string table:', sec.stringtable.name
25
26 for seg in efile.iter_segments():
27 print type(seg), seg['p_type'], seg['p_offset']
28
29 for sec in efile.iter_sections():
30 if isinstance(sec, SymbolTableSection):
31 print 'symbol table "%s ~~~"' % sec.name
32 for sym in sec.iter_symbols():
33 print '%-26s %s %s' % (sym.name, sym['st_info']['type'], sym['st_info']['bind'])
34
35
36 #~ print 'num', efile.num_sections()
37 #~ sec = efile.get_section(39)
38 #~ print sec.header
39 #~ print sec.name
40 #~ print sec['sh_type']
41 #~ print map(ord, sec.data())
42
43 #~ print sec.stream
44 #~ print map(ord, efile._stringtable)
45
46 #~ print efile.header
47 #~ print dir(efile)
48 #~ print efile['e_type']
49
50 #~ shtable_offset = efile['e_shoff']
51 #~ strtable_section_offset = shtable_offset + efile['e_shstrndx'] * efile['e_shentsize']
52
53 #~ # get to the section header for the sh string table
54 #~ print strtable_section_offset
55 #~ stream.seek(strtable_section_offset)
56 #~ sheader = es.Elf_Shdr.parse_stream(stream)
57 #~ print sheader
58
59 #~ # yay, looks correct!!
60 #~ stream.seek(sheader.sh_offset)
61 #~ buf = stream.read(sheader.sh_size)
62 #~ for c in buf:
63 #~ sys.stdout.write('%02X' % ord(c))
64
65
66
67
68 #~ print es.Elf_Ehdr