Subversion Repositories Kolibri OS

Rev

Rev 7520 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7520 Rev 7925
Line 3... Line 3...
3
public start
3
public start
4
public start as '_start'
4
public start as '_start'
5
;extrn mf_init
5
;extrn mf_init
6
extrn main
6
extrn main
7
;include 'debug2.inc'
7
;include 'debug2.inc'
-
 
8
include '..\..\..\..\..\proc32.inc'
-
 
9
include '..\..\..\..\..\macros.inc'
-
 
10
include '..\..\..\..\..\dll.inc'
8
__DEBUG__=0
11
__DEBUG__=0
Line 9... Line 12...
9
 
12
 
10
;start_:
13
;start_:
11
virtual at 0
14
virtual at 0
12
	db 'MENUET01' ; 1. Magic number (8 bytes)
15
	db 'MENUET01' ; 1. Magic number (8 bytes)
13
	dd 0x01       ; 2. Version of executable file
16
	dd 0x01       ; 2. Version of executable file
14
	dd start       ; 3. Start address
17
	dd start       ; 3. Start address
15
	dd 0x0	      ; 4. Size of image
18
imgsz	dd 0x0	      ; 4. Size of image
16
	dd 0x100000   ; 5. Size of needed memory
19
	dd 0x100000   ; 5. Size of needed memory
17
	dd 0x100000   ; 6. Pointer to stack
20
	dd 0x100000   ; 6. Pointer to stack
18
hparams dd 0x0	      ; 7. Pointer to program arguments
21
hparams dd 0x0	      ; 7. Pointer to program arguments
19
hpath	dd 0x0	      ; 8. Pointer to program path
22
hpath	dd 0x0	      ; 8. Pointer to program path
Line 24... Line 27...
24
    ;init heap of memory
27
    ;init heap of memory
25
    mov eax,68
28
    mov eax,68
26
    mov ebx,11
29
    mov ebx,11
27
    int 0x40
30
    int 0x40
Line 28... Line -...
28
 
-
 
29
;DEBUGF ' path "%s"\n params "%s"\n', .path, .params
-
 
30
; check for overflow
-
 
31
;; that not work
-
 
32
;    mov  al, [path+buf_len-1]
-
 
33
;    or	 al, [params+buf_len-1]
-
 
34
;    jnz   .crash
-
 
35
; check if path written by OS
31
 
36
	mov  [argc], 0
32
    mov  [argc], 0
37
    mov  eax, [hparams]
33
    mov  eax, [hparams]
38
    test eax, eax
34
    test eax, eax
39
    jz	 .without_path
35
    jz	 .without_path
Line 88... Line 84...
88
    mov  [ebx], ch
84
    mov  [ebx], ch
89
    mov  dl, ch
85
    mov  dl, ch
90
    jmp  .parse
86
    jmp  .parse
Line 91... Line 87...
91
 
87
 
92
.run:
-
 
93
;DEBUGF 'call main(%x, %x) with params:\n', [argc], argv
-
 
94
if __DEBUG__ = 1
88
.run:
95
    mov  ecx, [argc]
-
 
96
  @@:
-
 
97
    lea  esi, [ecx * 4 + argv-4]
-
 
98
    DEBUGF '0x%x) "%s"\n', cx, [esi]
-
 
99
    loop @b
-
 
100
end if
89
    call load_imports
101
    push argv
90
    push argv
102
    push [argc]
91
    push [argc]
103
    call main
92
    call main
104
.exit:
-
 
105
;DEBUGF 'Exit from prog\n';
93
.exit:
106
    xor  eax,eax
94
    xor  eax,eax
107
    dec  eax
95
    dec  eax
108
    int  0x40
96
    int  0x40
109
    dd	 -1
97
    dd	 -1
110
.crash:
-
 
111
;DEBUGF 'E:buffer overflowed\n'
98
.crash:
112
    jmp  .exit
99
    jmp  .exit
113
;============================
100
;============================
114
push_param:
101
push_param:
115
;============================
102
;============================
Line 125... Line 112...
125
    mov  [argv+4*ebx], esi
112
    mov  [argv+4*ebx], esi
126
    inc  [argc]
113
    inc  [argc]
127
.dont_add:    
114
.dont_add:    
128
    ret
115
    ret
129
;==============================
116
;==============================
-
 
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
 
130
public argc as '__argc'
207
public argc as '__argc'
131
public params as '__argv'
208
public params as '__argv'
132
public path as '__path'
209
public path as '__path'
Line 133... Line 210...
133
 
210