[Wisp-cvs] wisp/users/dig elfdump.py,NONE,1.1 Makefile.am,1.3,1.4
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-02-06 21:30:49
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv5770 Modified Files: Makefile.am Added Files: elfdump.py Log Message: added elfdump.py --- NEW FILE: elfdump.py --- #! /usr/bin/python #### elfdump.py - parse ELF data structures # # Copyleft © 2003 by Andres Soolo (di...@us...) # This file is licensed under the GNU GPL v2. If you # don't know what that means, please do read the GPL. # #### @(#) $Id: elfdump.py,v 1.1 2003/02/06 21:30:45 digg Exp $ from __future__ import nested_scopes import sys import string import struct from elf import * args = sys.argv[1:] def state_enum (field_name, value, dict): print '%s: %i' % (field_name, value), try: print '(' + dict[value] + ')' except: print '- INVALID' def enum_shortly (length, value, dict): try: print ('%%-%is' % length) % dict[value], except: print ('%%%ix' % length) % value, def flags_shortly (pattern, value): r = '' badp = 0 v = value for i in range(len(pattern)): if v & 1: c = pattern[-1 - i] if c == '-': badp = 1 r = c + r else: r = '-' + r v >>= 1 if v: badp = 1 if not badp: print r, else: print ('%%%ix' % len(pattern)) % value, def asciiz (all, start): stop = string.find(all, chr(0), start) if stop == -1: return all[start:] else: return all[start:stop] if args: for filename in args: f = open(filename, 'r') e_ident = f.read(16) print 'ELF header of %r:' % filename print 'Magic: %r:' % e_ident[0:4] state_enum('File class', ord(e_ident[4]), {1: '32-bit', 2: '64-bit'}) state_enum('Data encoding', ord(e_ident[5]), { 1: "2's complement LittleEndian", 2: "2's complement BigEndian"}) if ord(e_ident[5]) == 1: bytesex = '<' elif ord(e_ident[5]) == 2: bytesex = '>' else: bytesex = '' state_enum('File version', ord(e_ident[6]), {1: 'current'}) print 'Padding bytes: %0r' % e_ident[7:] e_type, e_machine, e_version, e_entry, e_phoff = \ struct.unpack(bytesex + 'HHLLL', f.read(16)) state_enum('Object file type', e_type, { 1: 'relocatable', 2: 'executable', 3: 'shared object', 4: 'core'}) state_enum('Required architecture', e_machine, { 1: 'AT&T WE 32100', 2: 'SPARC', 3: 'Intel 80386', 4: 'Motorola 68000', 5: 'Motorola 88000', 7: 'Intel 80860', 8: 'MIPS RS3000'}) state_enum('Object file version', e_version, {1: 'current'}) print 'Entry point: [%08x]' % e_entry e_shoff, e_flags, e_ehsize, e_phentsize, e_phnum, e_shentsize = \ struct.unpack(bytesex + 'LLHHHH', f.read(16)) print 'Processor-specific flags: [%08x]' % e_flags print 'ELF header size: [%08x] bytes' % e_ehsize print 'Program header table: %i entries at [%08x], 0x%x bytes each' % \ (e_phnum, e_phoff, e_phentsize) e_shnum, e_shstrndx = \ struct.unpack(bytesex + 'HH', f.read(4)) print 'Section header table: %i entries at [%08x], 0x%x bytes each' % \ (e_shnum, e_shoff, e_shentsize) print 'Section header strings in section #%i' % e_shstrndx print # load section header string table if e_shstrndx <> 0: f.seek(e_shoff + e_shentsize * e_shstrndx) shstrshent = f.read(e_shentsize) if len(shstrshent) == e_shentsize >= 24: shstr_ofs, = struct.unpack(bytesex + ' L', shstrshent[16:20]) shstr_size, = struct.unpack(bytesex + ' L', shstrshent[20:24]) f.seek(shstr_ofs) shstr = f.read(shstr_size) if len(shstr) <> shstr_size: print 'Broken section header string table' shstr = None else: shstr = None print 'Too short section header table entries' else: shstr = None if e_phnum <> 0: print 'Program header table:' f.seek(e_phoff) print 'No type offset vaddr paddr filesz memsz fla align' print '== ==== ====== ======== ======== ====== ====== === =====' for i in range(e_phnum): phentry = f.read(e_phentsize) p_type, p_offset, p_vaddr, p_paddr, p_filesz, p_memsz, \ p_flags, p_align = struct.unpack(bytesex + ' 8L', phentry[:32]) print '%2x' % i, enum_shortly(4, p_type, {PT.NULL: 'null', PT.LOAD: 'load', PT.DYNAMIC: 'dyna', PT.INTERP: 'inte', PT.NOTE: 'note', PT.SHLIB: 'shli', PT.PHDR: 'phdr', PT.TLS: 'tls '}) print '%6x' % p_offset, print '%8x' % p_vaddr, print '%8x' % p_paddr, if p_filesz > p_memsz: c = '>' elif p_filesz < p_memsz: c = '<' else: c = ' ' print '%6x%c%6x' % (p_filesz, c, p_memsz), flags_shortly('rwx', p_flags) print '%5x' % p_align, print print else: print 'No program header table' print print 'Section header table:' f.seek(e_shoff) print 'No type fla addr offset size link info align each' print '== ==== === ======== ====== ====== ==== ==== ===== ====' for i in range(e_shnum): shentry = f.read(e_shentsize) sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, \ sh_link, sh_info, sh_addralign, sh_entsize = \ struct.unpack(bytesex + ' 10L', shentry[0:40]) print '%2x' % i, try: print {SHT.NULL: 'null', SHT.PROGBITS: 'prog', SHT.SYMTAB: 'symt', SHT.STRTAB: 'strt', SHT.RELA: 'rela', SHT.HASH: 'hash', SHT.DYNAMIC: 'dyna', SHT.NOTE: 'note', SHT.NOBITS: 'nobi', SHT.REL: 'rel ', SHT.SHLIB: 'shli', SHT.DYNSYM: 'dyns', SHT.INIT_ARRAY: 'init', SHT.FINI_ARRAY: 'fini', SHT.PREINIT_ARRAY: 'prei', SHT.GROUP: 'grou', SHT.GNU_verdef: 'verd', SHT.GNU_verneed: 'vern', SHT.GNU_versym: 'vers'}[sh_type], except: print '%4x' % sh_type, flags_shortly('xaw', sh_flags) print '%8x' % sh_addr, print '%6x' % sh_offset, print '%6x' % sh_size, print '%4x' % sh_link, print '%4x' % sh_info, print '%5x' % sh_addralign, print '%4x' % sh_entsize, if shstr <> None: name = asciiz(shstr, sh_name) if name == '': name = '0x%x' % sh_name print name, print print else: print 'Usage: elfdump.py file ...' Index: Makefile.am =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/Makefile.am,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- Makefile.am 6 Feb 2003 21:16:01 -0000 1.3 +++ Makefile.am 6 Feb 2003 21:30:46 -0000 1.4 @@ -6,7 +6,8 @@ # #### @(#) $Id$ -EXTRA_DIST = .cvsignore struburn.wisp elf.py linkie.py makehello.py +EXTRA_DIST = .cvsignore struburn.wisp elf.py linkie.py makehello.py \ + elfdump.py all: |