1 # Just a script for playing around with pyelftools during testing
6 from elftools
.elf
.structs
import ELFStructs
7 from elftools
.elf
.elffile
import ELFFile
8 from elftools
.elf
.sections
import *
10 # read a little-endian, 64-bit file
11 es
= ELFStructs(True, 64)
13 stream
= open('tests/testfiles/z.elf', 'rb')
14 #stream = open('binfiles/z32.elf', 'rb')
16 efile
= ELFFile(stream
)
18 print '===> %s sections!' % efile
.num_sections()
19 print '===> %s segments!' % efile
.num_segments()
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
26 for seg
in efile
.iter_segments():
27 print type(seg
), seg
['p_type'], seg
['p_offset']
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'])
36 #~ print 'num', efile.num_sections()
37 #~ sec = efile.get_section(39)
40 #~ print sec['sh_type']
41 #~ print map(ord, sec.data())
44 #~ print map(ord, efile._stringtable)
48 #~ print efile['e_type']
50 #~ shtable_offset = efile['e_shoff']
51 #~ strtable_section_offset = shtable_offset + efile['e_shstrndx'] * efile['e_shentsize']
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)
59 #~ # yay, looks correct!!
60 #~ stream.seek(sheader.sh_offset)
61 #~ buf = stream.read(sheader.sh_size)
63 #~ sys.stdout.write('%02X' % ord(c))