| 1 | ; $Id: ldstub-nasm.asm 3665 2010-09-17 00:56:18Z bird $
|
|---|
| 2 | ;
|
|---|
| 3 | ; Micro stub for <app> -> <app>.exe forwarding, nasm version.
|
|---|
| 4 | ;
|
|---|
| 5 | ; Build Instructions:
|
|---|
| 6 | ; Stantard version:
|
|---|
| 7 | ; nasm -f obj -o ldstub-nasm.o ldstub-nasm.asm
|
|---|
| 8 | ; ilink /pmtype:vio /ALIGN:1 /OUT:ldstub.bin ldstub-nasm.obj ldstub.def os2386.lib
|
|---|
| 9 | ;
|
|---|
| 10 | ; OVERLAY may be defined for execv()/spawnv(OVERLAY,..) is to be emulated.
|
|---|
| 11 | ;
|
|---|
| 12 | ; InnoTek Systemberatung GmbH Confidential
|
|---|
| 13 | ;
|
|---|
| 14 | ; Copyright (c) 2003 InnoTek Systemberatung GmbH
|
|---|
| 15 | ; Author: knut st. osmundsen <bird@anduin.net>
|
|---|
| 16 | ;
|
|---|
| 17 | ; All Rights Reserved
|
|---|
| 18 | ;
|
|---|
| 19 | ;
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | ;*******************************************************************************
|
|---|
| 23 | ;* Global Variables *
|
|---|
| 24 | ;*******************************************************************************
|
|---|
| 25 | extern DosExecPgm
|
|---|
| 26 | extern DosQueryModuleName
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | ;*******************************************************************************
|
|---|
| 30 | ;* Segment definitions *
|
|---|
| 31 | ;*******************************************************************************
|
|---|
| 32 | segment TEXT32 public align=1 use32 FLAT class=CODE FLAT
|
|---|
| 33 | segment STACK32 stack align=16 use32 class=STACK FLAT
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | ;*******************************************************************************
|
|---|
| 37 | ;* Structures and Typedefs *
|
|---|
| 38 | ;*******************************************************************************
|
|---|
| 39 | segment TEXT32 ; works around warning and dummy 'text' segment.
|
|---|
| 40 | struc RESC
|
|---|
| 41 | .codeTerminate resd 1
|
|---|
| 42 | .codeResult resd 1
|
|---|
| 43 | endstruc
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | ; Program startup registers are defined as follows.
|
|---|
| 47 | ; EIP = Starting program entry address.
|
|---|
| 48 | ; ESP = Top of stack address.
|
|---|
| 49 | ; CS = Code selector for base of linear address space.
|
|---|
| 50 | ; DS = ES = SS = Data selector for base of linear address space.
|
|---|
| 51 | ; FS = Data selector of base of Thread Information Block (TIB).
|
|---|
| 52 | ; GS = 0.
|
|---|
| 53 | ; EAX = EBX = 0.
|
|---|
| 54 | ; ECX = EDX = 0.
|
|---|
| 55 | ; ESI = EDI = 0.
|
|---|
| 56 | ; EBP = 0.
|
|---|
| 57 | ; [ESP+0] = Return address to routine which calls DosExit(1,EAX).
|
|---|
| 58 | ; [ESP+4] = Module handle for program module.
|
|---|
| 59 | ; [ESP+8] = Reserved.
|
|---|
| 60 | ; [ESP+12] = Environment data object address.
|
|---|
| 61 | ; [ESP+16] = Command line linear address in environment data object.
|
|---|
| 62 | ;
|
|---|
| 63 | ; @remark I don't care about cleaning up arguments as the leave does so.
|
|---|
| 64 | ;
|
|---|
| 65 | segment TEXT32
|
|---|
| 66 | start:
|
|---|
| 67 | ..start:
|
|---|
| 68 | BITS 32
|
|---|
| 69 | enter 260 + 4 + RESC_size, 0
|
|---|
| 70 | %define achNewExeName ebp - 260 - RESC_size - 4 ; == esp right now
|
|---|
| 71 | %define res ebp - RESC_size
|
|---|
| 72 |
|
|---|
| 73 | ;
|
|---|
| 74 | ; Get the executable name.
|
|---|
| 75 | ;
|
|---|
| 76 | ; ULONG DosQueryModuleName(HMODULE hmod, ULONG ulNameLength, PCHAR pNameBuf);
|
|---|
| 77 | ;
|
|---|
| 78 | mov ebx, esp ; esp=achNewExeName
|
|---|
| 79 | push ebx ; pNameBuf
|
|---|
| 80 | push 260 ; ulNameLength
|
|---|
| 81 | push dword [ebp+8] ; hmod
|
|---|
| 82 | call DosQueryModuleName
|
|---|
| 83 | or eax, eax
|
|---|
| 84 | jnz .exec_failed ; this ain't supposed to happen
|
|---|
| 85 |
|
|---|
| 86 | ;
|
|---|
| 87 | ; Append .EXE to the module name.
|
|---|
| 88 | ;
|
|---|
| 89 | xor ecx, ecx
|
|---|
| 90 | dec ecx
|
|---|
| 91 | mov edi, ebx ; achNewExeName
|
|---|
| 92 | repne scasb
|
|---|
| 93 | mov dword [edi-1], 'EXE.'
|
|---|
| 94 | mov dword [edi+3], esi ; esi = 0 at this point.
|
|---|
| 95 |
|
|---|
| 96 | ;
|
|---|
| 97 | ; Try execute the .EXE appended module name.
|
|---|
| 98 | ;
|
|---|
| 99 | ; APIRET APIENTRY DosExecPgm(PCHAR pObjname,
|
|---|
| 100 | ; LONG cbObjname,
|
|---|
| 101 | ; ULONG execFlag,
|
|---|
| 102 | ; PCSZ pArg,
|
|---|
| 103 | ; PCSZ pEnv,
|
|---|
| 104 | ; PRESULTCODES pRes,
|
|---|
| 105 | ; PCSZ pName);
|
|---|
| 106 | ;
|
|---|
| 107 | push ebx ; pName (achNewExeName)
|
|---|
| 108 | lea edi, [res]
|
|---|
| 109 | push edi ; pRes
|
|---|
| 110 | push dword [ebp + 10h] ; pEnv
|
|---|
| 111 | push dword [ebp + 14h] ; pArg
|
|---|
| 112 | %ifdef OVERLAY
|
|---|
| 113 | push 1 ; execFlag = EXEC_ASYNC
|
|---|
| 114 | %else
|
|---|
| 115 | push esi ; execFlag = EXEC_SYNC = 0
|
|---|
| 116 | %endif
|
|---|
| 117 | push esi ; cbObjname
|
|---|
| 118 | push esi ; pObjname
|
|---|
| 119 | call DosExecPgm
|
|---|
| 120 | or eax, eax
|
|---|
| 121 | jz .exec_success
|
|---|
| 122 |
|
|---|
| 123 | .exec_failed:
|
|---|
| 124 | mov eax, 07fh
|
|---|
| 125 | jmp .done
|
|---|
| 126 |
|
|---|
| 127 | .exec_success:
|
|---|
| 128 | %ifndef OVERLAY
|
|---|
| 129 | ;
|
|---|
| 130 | ; Retrieve the result code.
|
|---|
| 131 | ;
|
|---|
| 132 | mov eax, [res + RESC.codeResult]
|
|---|
| 133 | cmp [res + RESC.codeTerminate], esi
|
|---|
| 134 | jz .done
|
|---|
| 135 |
|
|---|
| 136 | ;
|
|---|
| 137 | ; Something bad happend and the result code is 0.
|
|---|
| 138 | ; We shouldn't return 0 when we trap, crash or is killed.
|
|---|
| 139 | ;
|
|---|
| 140 | mov eax, 0ffh
|
|---|
| 141 | %endif
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | .done:
|
|---|
| 145 | leave
|
|---|
| 146 | ret
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | ; for emxomfld
|
|---|
| 150 | ABSOLUTE 0
|
|---|
| 151 | global WEAK$ZERO
|
|---|
| 152 | WEAK$ZERO:
|
|---|
| 153 |
|
|---|