Eli Bendersky | c4a4c07 | 2011-09-17 15:28:28 +0300 | [diff] [blame] | 1 | # Just a script for playing around with pyelftools during testing |
| 2 | # please ignore it! |
| 3 | # |
| 4 | |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 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 | |
eliben | 4455651 | 2011-09-19 12:54:32 +0300 | [diff] [blame^] | 13 | stream = open('tests/testfiles/z.elf', 'rb') |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 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(): |
Eli Bendersky | 3f4de3e | 2011-09-14 05:58:06 +0300 | [diff] [blame] | 27 | print type(seg), seg['p_type'], seg['p_offset'] |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 28 | |
Eli Bendersky | a42f82d | 2011-09-09 06:02:47 +0300 | [diff] [blame] | 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 | |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 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 |