blob: dc0eee9bfcfb04de2accd42a08b1d87261835bb8 [file] [log] [blame]
Eli Benderskyc4a4c072011-09-17 15:28:28 +03001# Just a script for playing around with pyelftools during testing
2# please ignore it!
3#
4
Eli Benderskye0735d52011-09-08 20:12:44 +03005import sys
6from elftools.elf.structs import ELFStructs
7from elftools.elf.elffile import ELFFile
8from elftools.elf.sections import *
9
10# read a little-endian, 64-bit file
11es = ELFStructs(True, 64)
12
13stream = open('binfiles/z.elf', 'rb')
14#stream = open('binfiles/z32.elf', 'rb')
15
16efile = ELFFile(stream)
17
18print '===> %s sections!' % efile.num_sections()
19print '===> %s segments!' % efile.num_segments()
20
21for 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
26for seg in efile.iter_segments():
Eli Bendersky3f4de3e2011-09-14 05:58:06 +030027 print type(seg), seg['p_type'], seg['p_offset']
Eli Benderskye0735d52011-09-08 20:12:44 +030028
Eli Benderskya42f82d2011-09-09 06:02:47 +030029for 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
Eli Benderskye0735d52011-09-08 20:12:44 +030035
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