Created
October 12, 2014 00:55
-
-
Save lizhiwei/6403adcdc700a1ad6a75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Reconstitue an ELF file based on the ELF header in the *.b00 | |
file and the data in each of the other files. | |
""" | |
import sys, struct, glob, os | |
def readfmt(f, fmt) : | |
d = f.read(struct.calcsize(fmt)) | |
return struct.unpack(fmt, d) | |
def writefmt(f, fmt, *args) : | |
f.write(struct.pack(fmt, *args)) | |
class EHdr(object) : | |
def __init__(self, f) : | |
self.e_ident = f.read(16) | |
(self.e_type, | |
self.e_machine, | |
self.e_version, | |
self.e_entry, | |
self.e_phoff, | |
self.e_shoff, | |
self.e_flags, | |
self.e_ehsize, | |
self.e_phentsize, | |
self.e_phnum, | |
self.e_shentsize, | |
self.e_shnum, | |
self.e_shstrndx) = readfmt(f, '<HHIIIIIHHHHHH') | |
def write(self, f) : | |
f.write(self.e_ident) | |
writefmt(f, '<HHIIIIIHHHHHH', self.e_type, | |
self.e_machine, | |
self.e_version, | |
self.e_entry, | |
self.e_phoff, | |
self.e_shoff, | |
self.e_flags, | |
self.e_ehsize, | |
self.e_phentsize, | |
self.e_phnum, | |
self.e_shentsize, | |
self.e_shnum, | |
self.e_shstrndx) | |
class PHdr(object) : | |
def __init__(self, f) : | |
(self.p_type, | |
self.p_offset, | |
self.p_vaddr, | |
self.p_paddr, | |
self.p_filesz, | |
self.p_memsz, | |
self.p_flags, | |
self.p_align) = readfmt(f, "IIIIIIII") | |
def write(self, f) : | |
return writefmt(f, '<IIIIIIII', | |
self.p_type, | |
self.p_offset, | |
self.p_vaddr, | |
self.p_paddr, | |
self.p_filesz, | |
self.p_memsz, | |
self.p_flags, | |
self.p_align) | |
def pr(x, pref) : | |
for n in dir(x) : | |
if pref == n[:len(pref)] : | |
print '%s=%r' % (n, getattr(x, n)), | |
class Error(Exception) : | |
pass | |
def proc(fn) : | |
fn0 = '%s.b00' % fn | |
b = file(fn0, 'rb') | |
hdr = EHdr(b) | |
#pr(hdr, 'e_') | |
# load in program section headers and filter | |
b.seek(hdr.e_phoff) | |
#print '%x' % hdr.e_phoff | |
phdrs = [PHdr(b) for n in xrange(hdr.e_phnum)] | |
ofn = os.path.basename(fn) + '.elf' | |
print 'making', ofn | |
f = file(ofn, 'wb') | |
off = 0 | |
for n in xrange(len(phdrs)) : | |
fnn = '%s.b%02d' % (fn, n) | |
p = phdrs[n] | |
print '%s -- %x - %x (%x) @ %x' % (fnn, p.p_paddr, p.p_paddr + p.p_memsz, p.p_paddr + p.p_filesz, p.p_vaddr) | |
# copy out padding | |
pad = p.p_offset - off | |
if pad < 0 : | |
raise Error("negative padding!") | |
f.write('\0' * pad) | |
off += pad | |
# copy out file | |
if p.p_filesz : | |
dat = file(fnn, 'rb').read() | |
if len(dat) != p.p_filesz : | |
raise Error("bad section size") | |
f.write(dat) | |
off += len(dat) | |
f.close() | |
for arg in sys.argv[1:] : | |
proc(arg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment