removed CRs
[pyelftools.git] / z.py
1 import sys
2 from elftools.elf.structs import ELFStructs
3 from elftools.elf.elffile import ELFFile
4 from elftools.elf.sections import *
5
6 # read a little-endian, 64-bit file
7 es = ELFStructs(True, 64)
8
9 stream = open('binfiles/z.elf', 'rb')
10 #stream = open('binfiles/z32.elf', 'rb')
11
12 efile = ELFFile(stream)
13
14 print '===> %s sections!' % efile.num_sections()
15 print '===> %s segments!' % efile.num_segments()
16
17 for 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
22 for seg in efile.iter_segments():
23 print seg['p_type'], seg['p_offset']
24
25
26 #~ print 'num', efile.num_sections()
27 #~ sec = efile.get_section(39)
28 #~ print sec.header
29 #~ print sec.name
30 #~ print sec['sh_type']
31 #~ print map(ord, sec.data())
32
33 #~ print sec.stream
34 #~ print map(ord, efile._stringtable)
35
36 #~ print efile.header
37 #~ print dir(efile)
38 #~ print efile['e_type']
39
40 #~ shtable_offset = efile['e_shoff']
41 #~ strtable_section_offset = shtable_offset + efile['e_shstrndx'] * efile['e_shentsize']
42
43 #~ # get to the section header for the sh string table
44 #~ print strtable_section_offset
45 #~ stream.seek(strtable_section_offset)
46 #~ sheader = es.Elf_Shdr.parse_stream(stream)
47 #~ print sheader
48
49 #~ # yay, looks correct!!
50 #~ stream.seek(sheader.sh_offset)
51 #~ buf = stream.read(sheader.sh_size)
52 #~ for c in buf:
53 #~ sys.stdout.write('%02X' % ord(c))
54
55
56
57
58 #~ print es.Elf_Ehdr