Rev 7520 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
145 | halyavin | 1 | format ELF |
2 | section '.text' executable |
||
3 | public start |
||
6424 | siemargl | 4 | public start as '_start' |
5 | ;extrn mf_init |
||
145 | halyavin | 6 | extrn main |
6424 | siemargl | 7 | ;include 'debug2.inc' |
7925 | Boppan | 8 | include '..\..\..\..\..\proc32.inc' |
9 | include '..\..\..\..\..\macros.inc' |
||
10 | include '..\..\..\..\..\dll.inc' |
||
6424 | siemargl | 11 | __DEBUG__=0 |
215 | victor | 12 | |
6424 | siemargl | 13 | ;start_: |
145 | halyavin | 14 | virtual at 0 |
215 | victor | 15 | db 'MENUET01' ; 1. Magic number (8 bytes) |
16 | dd 0x01 ; 2. Version of executable file |
||
6424 | siemargl | 17 | dd start ; 3. Start address |
7925 | Boppan | 18 | imgsz dd 0x0 ; 4. Size of image |
215 | victor | 19 | dd 0x100000 ; 5. Size of needed memory |
20 | dd 0x100000 ; 6. Pointer to stack |
||
21 | hparams dd 0x0 ; 7. Pointer to program arguments |
||
22 | hpath dd 0x0 ; 8. Pointer to program path |
||
145 | halyavin | 23 | end virtual |
6424 | siemargl | 24 | |
145 | halyavin | 25 | start: |
6424 | siemargl | 26 | ;DEBUGF 'Start programm\n' |
27 | ;init heap of memory |
||
28 | mov eax,68 |
||
29 | mov ebx,11 |
||
30 | int 0x40 |
||
31 | |||
7925 | Boppan | 32 | mov [argc], 0 |
145 | halyavin | 33 | mov eax, [hparams] |
34 | test eax, eax |
||
215 | victor | 35 | jz .without_path |
145 | halyavin | 36 | mov eax, path |
7520 | siemargl | 37 | cmp word ptr eax, 32fh ; '/#3' UTF8 |
38 | jne .without_path |
||
39 | mov word ptr eax, 12fh ; '/#1' fix to CP866 |
||
145 | halyavin | 40 | .without_path: |
41 | mov esi, eax |
||
42 | call push_param |
||
43 | ; retrieving parameters |
||
44 | mov esi, params |
||
45 | xor edx, edx ; dl - идёт параметр(1) или разделители(0) |
||
215 | victor | 46 | ; dh - символ с которого начался параметр (1 кавычки, 0 остальное) |
145 | halyavin | 47 | mov ecx, 1 ; cl = 1 |
215 | victor | 48 | ; ch = 0 просто ноль |
145 | halyavin | 49 | .parse: |
50 | lodsb |
||
51 | test al, al |
||
215 | victor | 52 | jz .run |
145 | halyavin | 53 | test dl, dl |
54 | jnz .findendparam |
||
215 | victor | 55 | ;{если был разделитель |
145 | halyavin | 56 | cmp al, ' ' |
215 | victor | 57 | jz .parse ;загружен пробел, грузим следующий символ |
145 | halyavin | 58 | mov dl, cl ;начинается параметр |
59 | cmp al, '"' |
||
215 | victor | 60 | jz @f ;загружены кавычки |
145 | halyavin | 61 | mov dh, ch ;параметр без кавычек |
62 | dec esi |
||
63 | call push_param |
||
64 | inc esi |
||
65 | jmp .parse |
||
66 | |||
67 | @@: |
||
68 | mov dh, cl ;параметр в кавычеках |
||
69 | call push_param ;если не пробел значит начинается какой то параметр |
||
70 | jmp .parse ;если был разделитель} |
||
71 | |||
72 | .findendparam: |
||
73 | test dh, dh |
||
215 | victor | 74 | jz @f ; без кавычек |
145 | halyavin | 75 | cmp al, '"' |
215 | victor | 76 | jz .clear |
145 | halyavin | 77 | jmp .parse |
78 | @@: |
||
79 | cmp al, ' ' |
||
80 | jnz .parse |
||
81 | |||
82 | .clear: |
||
83 | lea ebx, [esi - 1] |
||
84 | mov [ebx], ch |
||
85 | mov dl, ch |
||
86 | jmp .parse |
||
87 | |||
88 | .run: |
||
7925 | Boppan | 89 | call load_imports |
215 | victor | 90 | push argv |
145 | halyavin | 91 | push [argc] |
92 | call main |
||
93 | .exit: |
||
94 | xor eax,eax |
||
95 | dec eax |
||
96 | int 0x40 |
||
215 | victor | 97 | dd -1 |
145 | halyavin | 98 | .crash: |
99 | jmp .exit |
||
100 | ;============================ |
||
101 | push_param: |
||
102 | ;============================ |
||
103 | ;parameters |
||
104 | ; esi - pointer |
||
105 | ;description |
||
106 | ; procedure increase argc |
||
107 | ; and add pointer to array argv |
||
108 | ; procedure changes ebx |
||
109 | mov ebx, [argc] |
||
110 | cmp ebx, max_parameters |
||
111 | jae .dont_add |
||
112 | mov [argv+4*ebx], esi |
||
113 | inc [argc] |
||
114 | .dont_add: |
||
115 | ret |
||
116 | ;============================== |
||
7925 | Boppan | 117 | |
118 | ;============================== |
||
119 | load_imports: |
||
120 | ;============================== |
||
121 | ;parameters |
||
122 | ; none |
||
123 | ;description |
||
124 | ; imports must be located at end of image (but before BSS sections) |
||
125 | ; the address of end of imports (next byte after imports) is located in imgsz |
||
126 | ; look at each import from that address up to illegal import |
||
127 | ; legal import is such that: |
||
128 | ; first pointer points to procedure name |
||
129 | ; and is smaller than imgsz |
||
130 | ; second pointer points lo library name, starting with 0x55, 0xAA |
||
131 | ; and is smaller than imgsz |
||
132 | ; each library should be initialized as appropriate, once |
||
133 | ; so as library is initialized, its name will be replaced 0x00 |
||
134 | mov ebx, [imgsz] ; byte after imports |
||
135 | .handle_next_import: |
||
136 | sub ebx, 4 ; ebx = pointer to pointer to library name |
||
137 | mov esi, dword[ebx] ; esi = pointer to library name |
||
138 | push ebx |
||
139 | push esi |
||
140 | call load_library ; eax = pointer to library exports |
||
141 | pop esi |
||
142 | pop ebx |
||
143 | test eax, eax |
||
144 | jz .done |
||
145 | sub ebx, 4 ; ebx = pointer to pointer to symbol name |
||
146 | push ebx |
||
147 | stdcall dll.GetProcAddress, eax, dword[ebx] |
||
148 | pop ebx |
||
149 | test eax, eax |
||
150 | jz .fail |
||
151 | mov dword[ebx], eax |
||
152 | jmp .handle_next_import |
||
153 | .done: |
||
154 | ret |
||
155 | .fail: |
||
156 | ret |
||
157 | ;============================== |
||
158 | |||
159 | ;============================== |
||
160 | load_library: |
||
161 | ;============================== |
||
162 | ;parameters |
||
163 | ; ebx: library name address |
||
164 | ;description |
||
165 | ; each library should be initialized as appropriate, once |
||
166 | ; so as library is initialized, its name will be replaced 0x00 |
||
167 | ; and 4 next bytes will be set to address of library |
||
168 | ; first two bytes of library name must be 0x55, 0xAA (is like a magic) |
||
169 | cld ; move esi further, not back |
||
170 | cmp esi, [imgsz] |
||
171 | ja .fail |
||
172 | lodsb ; al = first byte of library name |
||
173 | cmp al, 0x55 |
||
174 | jne .fail |
||
175 | lodsb ; al = second byte of library name |
||
176 | cmp al, 0xAA |
||
177 | jne .fail |
||
178 | lodsb ; al = third byte of library name (0x00 if the library is already loaded) |
||
179 | test al, al |
||
180 | jnz .load |
||
181 | lodsd ; if we here, then third byte is 0x00 => address of library is in next 4 bytes |
||
182 | ; now eax contains address of library |
||
183 | ret |
||
184 | .load: |
||
185 | dec esi ; we checked on 0 before, let's go back |
||
186 | mov eax, 68 |
||
187 | mov ebx, 19 |
||
188 | mov ecx, esi |
||
189 | int 0x40 ; eax = address of exports |
||
190 | mov byte[esi], 0 ; library is loaded, let's place 0 in first byte of name |
||
191 | mov [esi + 1], eax ; now next 4 bytes of library name are replaced by address of library |
||
192 | ; call lib_init |
||
193 | stdcall dll.GetProcAddress, eax, lib_init_str ; eax = address of lib_init |
||
194 | test eax, eax |
||
195 | jz .ret |
||
196 | stdcall dll.Init, eax |
||
197 | .ret: |
||
198 | mov eax, [esi + 1] ; put address of library into eax |
||
199 | ret |
||
200 | .fail: |
||
201 | mov eax, 0 |
||
202 | ret |
||
203 | ;============================== |
||
204 | |||
205 | lib_init_str db 'lib_init', 0 |
||
206 | |||
6424 | siemargl | 207 | public argc as '__argc' |
145 | halyavin | 208 | public params as '__argv' |
209 | public path as '__path' |
||
210 | |||
6443 | siemargl | 211 | section '.bss' |
145 | halyavin | 212 | buf_len = 0x400 |
213 | max_parameters=0x20 |
||
215 | victor | 214 | argc rd 1 |
215 | argv rd max_parameters |
||
6443 | siemargl | 216 | path rb buf_len |
217 | params rb buf_len |
||
145 | halyavin | 218 | |
6424 | siemargl | 219 | ;section '.data' |
7520 | siemargl | 220 | ;include_debug_strings ; ALWAYS present in data section |