Thread: [Wisp-cvs] wisp/users/dig pe.py,NONE,1.1 Makefile.am,1.12,1.13 make-pe-exe.py,1.59,1.60
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-05-16 14:09:59
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv7709 Modified Files: Makefile.am make-pe-exe.py Added Files: pe.py Log Message: renamed make_mz_prefix to make_pe_mz_stub and moved it to pe.py --- NEW FILE: pe.py --- #### pe.py - the PE executable format # # 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: pe.py,v 1.1 2003/05/16 14:09:55 digg Exp $ from mz import * def make_pe_mz_stub (message = 'OS too broken'): # The result's assumed origin address is 0x100. if message.find('$') != -1: raise 'MZ message may not contain dollar signs', message if message.find('\r') == -1: # if no CRs, insert them message = '\r\n'.join(message.split('\n')) b = make_mz_header() b.place_symbol('#mz/reloc-count', 0) # no relocation b.place_symbol('#mz/header-size', 0) # empty MZ header--load it all b.place_symbol('#mz/low-memory-limit', 0x40) b.place_symbol('#mz/high-memory-limit', 0x40) b.place_symbol('#mz/ss', -0x10) b.place_symbol('#mz/sp', 0x100) b.place_symbol('#mz/checksum', 0) b.place_symbol('#mz/ip', 0x100 + b.memsz()) b.place_symbol('#mz/cs', -0x10) b.place_symbol('!mz/reloc', 0) b.place_symbol('#mz/overlay', 0) b[::1] = 0x8C, 0xC8, 0x8E, 0xD8 # mov %ax, %cs; mov %ds, mov %ax b[::1] = 0xB4, 0x09 # mov %ah, 0x09 b[::1] = 0xBA; b[::2] = '&mz/message' # mov %dx, message b[::1] = 0xCD, 0x21 # int 0x21 b[::1] = 0xB8, 0xFF, 0x4C, 0xCD, 0x21 # mov %ax, 0x4CFF; int 0x21 message += '$' # Dollar the Terminator # a pointer to the PE signature must appear at the offset 0x003C room = 0x003C - b.filesz() if message[0] == '!': room += 1 if len(message) <= room: b.place_symbol('&mz/message') b.emit_string(message) b.emit_string('\x00' * (0x003C - b.filesz())) b[::4] = '!pe' else: b.emit_string('\x00' * (0x003C - b.filesz())) b[::4] = '!pe' b.place_symbol('&mz/message') b.emit_string(message) b.place_symbol('#mz/bytes-in-last-block', b.filesz() % 0x200) b.place_symbol('#mz/blocks-in-file', (b.filesz() + 0x1FF) / 0x200) return b Index: Makefile.am =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/Makefile.am,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- Makefile.am 16 May 2003 14:05:38 -0000 1.12 +++ Makefile.am 16 May 2003 14:09:56 -0000 1.13 @@ -7,7 +7,7 @@ #### @(#) $Id$ EXTRA_DIST = .cvsignore .pycheckrc struburn.wisp bits.py linkie.py \ - coff.py elf.py \ + coff.py elf.py pe.py \ makehello.py elfdump.py all: Index: make-pe-exe.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/make-pe-exe.py,v retrieving revision 1.59 retrieving revision 1.60 diff -u -d -r1.59 -r1.60 --- make-pe-exe.py 16 May 2003 13:52:01 -0000 1.59 +++ make-pe-exe.py 16 May 2003 14:09:56 -0000 1.60 @@ -9,7 +9,7 @@ #### @(#) $Id$ from linkie import Linkie -from mz import * +from pe import * import time # This file is *very* incomplete @@ -17,48 +17,6 @@ def roundup (value, boundary): return (value + boundary - 1) & ~(boundary - 1) -def make_mz_prefix (message = 'OS too broken'): - # The result's assumed origin address is 0x100. - if message.find('$') != -1: - raise 'MZ message may not contain dollar signs', message - if message.find('\r') == -1: # if no CRs, insert them - message = '\r\n'.join(message.split('\n')) - b = make_mz_header() - b.place_symbol('#mz/reloc-count', 0) # no relocation - b.place_symbol('#mz/header-size', 0) # empty MZ header--load it all - b.place_symbol('#mz/low-memory-limit', 0x40) - b.place_symbol('#mz/high-memory-limit', 0x40) - b.place_symbol('#mz/ss', -0x10) - b.place_symbol('#mz/sp', 0x100) - b.place_symbol('#mz/checksum', 0) - b.place_symbol('#mz/ip', 0x100 + b.memsz()) - b.place_symbol('#mz/cs', -0x10) - b.place_symbol('!mz/reloc', 0) - b.place_symbol('#mz/overlay', 0) - b[::1] = 0x8C, 0xC8, 0x8E, 0xD8 # mov %ax, %cs; mov %ds, mov %ax - b[::1] = 0xB4, 0x09 # mov %ah, 0x09 - b[::1] = 0xBA; b[::2] = '&mz/message' # mov %dx, message - b[::1] = 0xCD, 0x21 # int 0x21 - b[::1] = 0xB8, 0xFF, 0x4C, 0xCD, 0x21 # mov %ax, 0x4CFF; int 0x21 - - message += '$' # Dollar the Terminator - # a pointer to the PE signature must appear at the offset 0x003C - room = 0x003C - b.filesz() - if message[0] == '!': room += 1 - if len(message) <= room: - b.place_symbol('&mz/message') - b.emit_string(message) - b.emit_string('\x00' * (0x003C - b.filesz())) - b[::4] = '!pe' - else: - b.emit_string('\x00' * (0x003C - b.filesz())) - b[::4] = '!pe' - b.place_symbol('&mz/message') - b.emit_string(message) - b.place_symbol('#mz/bytes-in-last-block', b.filesz() % 0x200) - b.place_symbol('#mz/blocks-in-file', (b.filesz() + 0x1FF) / 0x200) - return b - def make_coff_header (): h = Linkie('<') h.align(4) @@ -228,7 +186,7 @@ sectnames = ['.text', '.istubs', '.data', '.bss', '.imports'] e = Linkie('<'); -e.glue(0, make_mz_prefix('OS too broken'), 0x100) +e.glue(0, make_pe_mz_stub('OS too broken'), 0x100) if e.memsz() < 0x80: e.align(0x80) # attempt to create a file(1)-friendly exe e.align(8) # PE header must be aligned to 8 e.place_symbol('!pe') |