[Wisp-cvs] wisp/users/dig make-pe-exe.py,1.3,1.4
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-04-14 21:47:10
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv23439 Modified Files: make-pe-exe.py Log Message: generate more of PE headers Index: make-pe-exe.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/make-pe-exe.py,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- make-pe-exe.py 1 Apr 2003 08:03:01 -0000 1.3 +++ make-pe-exe.py 14 Apr 2003 21:47:06 -0000 1.4 @@ -64,23 +64,150 @@ b.link() return b -p = make_mz_prefix('OS too broken') -p.get_file().tofile(open('pehello1.exe', 'w')) - -p = make_mz_prefix('OS too broken:(') -p.get_file().tofile(open('pehello2.exe', 'w')) - -p = make_mz_prefix('OS too broken :(') -p.get_file().tofile(open('pehello3.exe', 'w')) - -p = make_mz_prefix('! OS too broken!') -p.get_file().tofile(open('pehello4.exe', 'w')) - -p = make_mz_prefix('! OS too broken !') -p.get_file().tofile(open('pehello5.exe', 'w')) +def make_coff_header (): + h = Linkie('<') + h.align(4) + h.emit_wyde_sum(['#coff/magic']) + # Known magic values: + # 0x0000 unknown + # 0x014C i80386 + # 0x014D i80486 + # 0x014E Pentium + # 0x0162 Mips Mark I (R2000, R3000) + # 0x0163 Mips Mark II (R6000) + # 0x0166 Mips Mark III (R4000) + # 0x0168 R10000 + # 0x0184 Alpha AXP + # 0x01A2 Hitachi SH3 + # 0x01A6 Hitachi SH4 + # 0x01C0 ARM + # 0x01F0 PowerPC LittleEndian + # 0x0200 ia64 + # 0x0266 Mips 16 + # 0x0268 m68k + # 0x0284 Alpha AXP 64-bit + # 0x0366 Mips with FPU + # 0x0466 Mips 16 with FPU + h.emit_wyde_sum(['#coff/nscns']) + h.emit_tetra_sum(['#coff/timdat']) + h.emit_tetra_sum(['!coff/symptr']) + h.emit_tetra_sum(['#coff/nsyms']) + h.emit_wyde_sum(['#coff/opthdr']) + h.emit_wyde_sum(['#coff/flags']) + # Flags for PE: + # 0x0001 no relocations -- can only be loaded at the preferred address + # 0x0002 executable + # 0x0004 no COFF line number data + # 0x0008 no COFF local symbol entries + # 0x0010 aggressively trim working set (?) + # 0x0020 program is aware of addresses larger than 2Gi + # 0x0040 reserved + # 0x0200 debugging information stripped away + # 0x0400 if file on removable media, copy and run from swap (?) + # 0x1000 is a system program (?) + # 0x2000 library + # 0x4000 should only be run on uniprocessor machines + return h -p = make_mz_prefix('A very long message\nspanning several lines.') -p.get_file().tofile(open('pehello6.exe', 'w')) +def make_pe_aout_header (): + h = Linkie('<') + h.align(4) + # #aout/magic = 0x10B + h.emit_wyde_sum(['#aout/magic']) + h.emit_byte_sum(['#aout/linker-version-major']) + h.emit_byte_sum(['#aout/linker-version-minor']) + h.emit_tetra_sum(['#aout/text-size']) + h.emit_tetra_sum(['#aout/data-size']) + h.emit_tetra_sum(['#aout/bss-size']) + h.emit_tetra_sum(['%_entry']) + h.emit_tetra_sum(['!.text']) + h.emit_tetra_sum(['!.data']) # not present in PE32+ (?) + # #aout/image-base must be multiple of 64ki + h.emit_tetra_sum(['#aout/image-base']) + h.emit_tetra_sum(['#aout/memory-align']) + # #aout/file-align must be a power of 2 in range of [512 ... 64Ki] + h.emit_tetra_sum(['#aout/file-align']) + h.emit_wyde_sum(['#aout/os-version-major']) + h.emit_wyde_sum(['#aout/os-version-minor']) + h.emit_wyde_sum(['#aout/image-version-major']) + h.emit_wyde_sum(['#aout/image-version-minor']) + h.emit_wyde_sum(['#aout/subsys-version-major']) + h.emit_wyde_sum(['#aout/subsys-version-minor']) + h.emit_tetra(0) + # #aout/image-size must be a multiple of #aout/object-align + h.emit_tetra_sum(['#aout/image-size']) + h.emit_tetra_sum(['#aout/header-size']) + h.emit_tetra(0) # checksum + h.emit_wyde_sum(['#aout/subsys']) + # Known values for #aout/subsys + # 0x0000 - unknown + # 0x0001 - native + # 0x0002 - Windows GUI + # 0x0003 - Windows character + # 0x0005 - OS/2 character + # 0x0007 - POSIX character + # 0x0009 - Windows CE GUI + h.emit_wyde_sum(['#aout/dll-flags']) + # Known flags for #aout/dll-flags + # 0x0001 - per-process library initialization + # 0x0002 - per-process library termination + # 0x0004 - per-thread library initialization + # 0x0008 - per-thread library termination + # All others must be zero. + h.emit_tetra_sum(['#aout/stack-reserve-size']) + h.emit_tetra_sum(['#aout/stack-commit-size']) + h.emit_tetra_sum(['#aout/heap-reserve-size']) + h.emit_tetra_sum(['#aout/heap-commit-size']) + h.emit_tetra(0) # loader flags - obsolete + h.emit_tetra_sum(['#aout/dict-entry-count']) + h.emit_tetra_sum(['%export-table']) + h.emit_tetra_sum(['#export-table/size']) + h.emit_tetra_sum(['%import-table']) + h.emit_tetra_sum(['#import-table/size']) + h.emit_tetra_sum(['%resource-table']) + h.emit_tetra_sum(['#resource-table/size']) + h.emit_tetra_sum(['%exception-table']) + h.emit_tetra_sum(['#exception-table/size']) + h.emit_tetra_sum(['!certificate-table']) + h.emit_tetra_sum(['#certificate-table/size']) + h.emit_tetra_sum(['%base-relocation-table']) + h.emit_tetra_sum(['#base-relocation-table/size']) + h.emit_tetra_sum(['%debug-data']) + h.emit_tetra_sum(['#debug-data/size']) + h.emit_tetra_sum(['%architecture-specific']) + h.emit_tetra_sum(['#architecture-specific/size']) + h.emit_tetra_sum(['%global-pointer']) + h.emit_tetra(0) + h.emit_tetra_sum(['%thread-local-storage']) + h.emit_tetra_sum(['#thread-local-storage/size']) + h.emit_tetra_sum(['%load-config-table']) + h.emit_tetra_sum(['#load-config-table/size']) + h.emit_tetra_sum(['%bound-import-table']) + h.emit_tetra_sum(['#bound-import-table/size']) + h.emit_tetra_sum(['%import-address-table']) + h.emit_tetra_sum(['#import-address-table/size']) + h.emit_tetra_sum(['%delay-import-descriptor']) + h.emit_tetra_sum(['#delay-import-descriptor/size']) + h.emit_tetra_sum(['%COM+-runtime-header']) + h.emit_tetra_sum(['#COM+-runtime-header/size']) + h.emit_tetra(0); h.emit_tetra(0) # reserved + return h -p = make_mz_prefix('Many lines spanning several blocks in total.\n' * 40) -p.get_file().tofile(open('pehello7.exe', 'w')) +e = Linkie('<'); e.paste(0, make_mz_prefix('OS too broken')) +e.align(8) # PE header must be aligned to 8 +e.place_symbol('!pe') +e.emit_string('PE\0\0') +e.paste(None, make_coff_header()) +e.place_symbol('#coff/magic', 0x014C) # I386MAGIC +e.place_symbol('#coff/timdat', int(time.time())) +e.place_symbol('!coff/symptr', 0) +e.place_symbol('#coff/nsyms', 0) +e.place_symbol('#coff/opthdr', 0x00E0) +e.place_symbol('#coff/flags', 0x0002) +e.place_symbol('#aout/image-base', 0x00400000) +e.place_symbol('#aout/memory-align', 4096) +e.place_symbol('#aout/file-align', 512) +e.paste(None, make_pe_aout_header()) +e.link() +e.dump() +e.get_file().tofile(open('pehello.exe', 'w')) |