blob: dd2ab54fd09876b3b3d22995d5116a0e11fb01b4 [file] [log] [blame]
Eli Benderskye0735d52011-09-08 20:12:44 +03001import sys
2from elftools.elf.structs import ELFStructs
3from elftools.elf.elffile import ELFFile
4from elftools.elf.sections import *
5
6# read a little-endian, 64-bit file
7es = ELFStructs(True, 64)
8
9stream = open('binfiles/z.elf', 'rb')
10#stream = open('binfiles/z32.elf', 'rb')
11
12efile = ELFFile(stream)
13
14print '===> %s sections!' % efile.num_sections()
15print '===> %s segments!' % efile.num_segments()
16
17for sec in efile.iter_sections():
18 print type(sec), sec.name
19 if isinstance(sec, SymbolTableSection):
20 print ' linked string table:', sec.stringtable.name
21
22for seg in efile.iter_segments():
23 print seg['p_type'], seg['p_offset']
24
Eli Benderskya42f82d2011-09-09 06:02:47 +030025for sec in efile.iter_sections():
26 if isinstance(sec, SymbolTableSection):
27 print 'symbol table "%s ~~~"' % sec.name
28 for sym in sec.iter_symbols():
29 print '%-26s %s %s' % (sym.name, sym['st_info']['type'], sym['st_info']['bind'])
30
Eli Benderskye0735d52011-09-08 20:12:44 +030031
32#~ print 'num', efile.num_sections()
33#~ sec = efile.get_section(39)
34#~ print sec.header
35#~ print sec.name
36#~ print sec['sh_type']
37#~ print map(ord, sec.data())
38
39#~ print sec.stream
40#~ print map(ord, efile._stringtable)
41
42#~ print efile.header
43#~ print dir(efile)
44#~ print efile['e_type']
45
46#~ shtable_offset = efile['e_shoff']
47#~ strtable_section_offset = shtable_offset + efile['e_shstrndx'] * efile['e_shentsize']
48
49#~ # get to the section header for the sh string table
50#~ print strtable_section_offset
51#~ stream.seek(strtable_section_offset)
52#~ sheader = es.Elf_Shdr.parse_stream(stream)
53#~ print sheader
54
55#~ # yay, looks correct!!
56#~ stream.seek(sheader.sh_offset)
57#~ buf = stream.read(sheader.sh_size)
58#~ for c in buf:
59 #~ sys.stdout.write('%02X' % ord(c))
60
61
62
63
64#~ print es.Elf_Ehdr