libc.obj: Use crt0.asm by Coldy as default

git-svn-id: svn://kolibrios.org@9554 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat 2022-01-01 16:38:32 +00:00
parent 0428045f25
commit c62ef7970d
2 changed files with 57 additions and 157 deletions

View File

@ -1,39 +1,36 @@
;
; 2021, Edited by Coldy
;
; This module same as original crt0.asm, but cut:
; 1. virtual header block (hparams change to __app_params, hpath change to __app_path)
; 2. init heap of memory - not needed because 68.18 (68.19) init heap implicitly
; (it is does dll.obj)
; 3. loader (he lives in dll.obj)
;
format ELF format ELF
section '.text' executable section '.text' executable
public start public start
public start as '_start' public start as '_start'
;extrn mf_init
extrn main
include '../../../../../proc32.inc'
include '../../../../../macros.inc'
include '../../../../../dll.inc'
;include '../../../../../debug.inc'
;start_: extrn main
virtual at 0 ;include 'debug2.inc'
db 'MENUET01' ; 1. Magic number (8 bytes) include '/../../../../../../proc32.inc'
dd 0x01 ; 2. Version of executable file include '/../../../../../../macros.inc'
dd start ; 3. Start address __DEBUG__ = 0
imgsz dd 0x0 ; 4. Size of image
dd 0x100000 ; 5. Size of needed memory __app_params equ 0x1C ; Pointer to program arguments
dd 0x100000 ; 6. Pointer to stack ;__app_path equ 0x20 ; Pointer to program path
hparams dd 0x0 ; 7. Pointer to program arguments
hpath dd 0x0 ; 8. Pointer to program path
end virtual
start: start:
;DEBUGF 'Start programm\n' ;DEBUGF 'Start programm\n'
;init heap of memory
mov eax,68
mov ebx,11
int 0x40
mov [argc], 0 mov [argc], 0
mov eax, [hparams] mov eax, [__app_params]
test eax, eax test eax, eax
jz .without_path jz .without_path
mov eax, path mov eax, path
cmp word ptr eax, 32fh ; '/#3' UTF8 cmp word ptr eax, 32fh ; '/#3' UTF8
jne .without_path jne .without_path
mov word ptr eax, 12fh ; '/#1' fix to CP866 mov word ptr eax, 12fh ; '/#1' fix to CP866
.without_path: .without_path:
@ -42,21 +39,21 @@ start:
; retrieving parameters ; retrieving parameters
mov esi, params mov esi, params
xor edx, edx ; dl - èä¸ò ïàðàìåòð(1) èëè ðàçäåëèòåëè(0) xor edx, edx ; dl - èä¸ò ïàðàìåòð(1) èëè ðàçäåëèòåëè(0)
; dh - ñèìâîë ñ êîòîðîãî íà÷àëñÿ ïàðàìåòð (1 êàâû÷êè, 0 îñòàëüíîå) ; dh - ñèìâîë ñ êîòîðîãî íà÷àëñÿ ïàðàìåòð (1 êàâû÷êè, 0 îñòàëüíîå)
mov ecx, 1 ; cl = 1 mov ecx, 1 ; cl = 1
; ch = 0 ïðîñòî íîëü ; ch = 0 ïðîñòî íîëü
.parse: .parse:
lodsb lodsb
test al, al test al, al
jz .run jz .run
test dl, dl test dl, dl
jnz .findendparam jnz .findendparam
;{åñëè áûë ðàçäåëèòåëü ;{åñëè áûë ðàçäåëèòåëü
cmp al, ' ' cmp al, ' '
jz .parse ;çàãðóæåí ïðîáåë, ãðóçèì ñëåäóþùèé ñèìâîë jz .parse ;çàãðóæåí ïðîáåë, ãðóçèì ñëåäóþùèé ñèìâîë
mov dl, cl ;íà÷èíàåòñÿ ïàðàìåòð mov dl, cl ;íà÷èíàåòñÿ ïàðàìåòð
cmp al, '"' cmp al, '"'
jz @f ;çàãðóæåíû êàâû÷êè jz @f ;çàãðóæåíû êàâû÷êè
mov dh, ch ;ïàðàìåòð áåç êàâû÷åê mov dh, ch ;ïàðàìåòð áåç êàâû÷åê
dec esi dec esi
call push_param call push_param
@ -70,9 +67,9 @@ start:
.findendparam: .findendparam:
test dh, dh test dh, dh
jz @f ; áåç êàâû÷åê jz @f ; áåç êàâû÷åê
cmp al, '"' cmp al, '"'
jz .clear jz .clear
jmp .parse jmp .parse
@@: @@:
cmp al, ' ' cmp al, ' '
@ -85,7 +82,6 @@ start:
jmp .parse jmp .parse
.run: .run:
call load_imports
push argv push argv
push [argc] push [argc]
call main call main
@ -93,7 +89,7 @@ start:
xor eax,eax xor eax,eax
dec eax dec eax
int 0x40 int 0x40
dd -1 dd -1
.crash: .crash:
jmp .exit jmp .exit
;============================ ;============================
@ -112,101 +108,9 @@ push_param:
inc [argc] inc [argc]
.dont_add: .dont_add:
ret ret
;============================== ;==============================
;==============================
load_imports:
;==============================
;parameters
; none
;description
; imports must be located at end of image (but before BSS sections)
; the address of end of imports (next byte after imports) is located in imgsz
; look at each import from that address up to illegal import
; legal import is such that:
; first pointer points to procedure name
; and is smaller than imgsz
; second pointer points lo library name, starting with 0x55, 0xAA
; and is smaller than imgsz
; each library should be initialized as appropriate, once
; so as library is initialized, its name will be replaced 0x00
mov ebx, [imgsz] ; byte after imports
.handle_next_import:
sub ebx, 4 ; ebx = pointer to pointer to library name
mov esi, dword[ebx] ; esi = pointer to library name
push ebx
push esi
call load_library ; eax = pointer to library exports
pop esi
pop ebx
test eax, eax
jz .done
sub ebx, 4 ; ebx = pointer to pointer to symbol name
push ebx
stdcall dll.GetProcAddress, eax, dword[ebx]
pop ebx
test eax, eax
jz .fail
mov dword[ebx], eax
jmp .handle_next_import
.done:
;DEBUGF 1, "Library: %s not loaded!\n", esi
;mcall -1
ret
.fail:
ret
;==============================
;==============================
load_library:
;==============================
;parameters
; ebx: library name address
;description
; each library should be initialized as appropriate, once
; so as library is initialized, its name will be replaced 0x00
; and 4 next bytes will be set to address of library
; first two bytes of library name must be 0x55, 0xAA (is like a magic)
cld ; move esi further, not back
cmp esi, [imgsz]
ja .fail
lodsb ; al = first byte of library name
cmp al, 0x55
jne .fail
lodsb ; al = second byte of library name
cmp al, 0xAA
jne .fail
lodsb ; al = third byte of library name (0x00 if the library is already loaded)
test al, al
jnz .load
lodsd ; if we here, then third byte is 0x00 => address of library is in next 4 bytes
; now eax contains address of library
ret
.load:
dec esi ; we checked on 0 before, let's go back
mov eax, 68
mov ebx, 19
mov ecx, esi
int 0x40 ; eax = address of exports
mov byte[esi], 0 ; library is loaded, let's place 0 in first byte of name
mov [esi + 1], eax ; now next 4 bytes of library name are replaced by address of library
; call lib_init
stdcall dll.GetProcAddress, eax, lib_init_str ; eax = address of lib_init
test eax, eax
jz .ret
stdcall dll.Init, eax
.ret:
mov eax, [esi + 1] ; put address of library into eax
ret
.fail:
mov eax, 0
ret
;==============================
lib_init_str db 'lib_init', 0
public argc as '__argc' public argc as '__argc'
public params as '__argv' public params as '__argv'
public path as '__path' public path as '__path'
@ -214,8 +118,10 @@ public path as '__path'
section '.bss' section '.bss'
buf_len = 0x400 buf_len = 0x400
max_parameters=0x20 max_parameters=0x20
argc rd 1 argc rd 1
argv rd max_parameters argv rd max_parameters
path rb buf_len path rb buf_len
params rb buf_len params rb buf_len
;section '.data'
;include_debug_strings ; ALWAYS present in data section

View File

@ -7,31 +7,25 @@ static char* __con_dllname = "/sys/lib/console.obj";
int __con_is_load = 0; int __con_is_load = 0;
void stdcall (*__con_init_hidden)(int wnd_width, unsigned wnd_height, int scr_width, int scr_height, const char* title); void __stdcall (*__con_init_hidden)(int wnd_width, unsigned wnd_height, int scr_width, int scr_height, const char* title);
void stdcall (*con_exit)(int); void __stdcall (*con_exit)(int);
void stdcall (*con_set_title)(const char* title); void __stdcall (*con_set_title)(const char* title);
void stdcall (*con_write_asciiz)(const char* str); void __stdcall (*con_write_asciiz)(const char* str);
void stdcall (*con_write_string)(const char* str, dword length); void __stdcall (*con_write_string)(const char* str, dword length);
int cdecl (*con_printf)(const char* format, ...); int __cdecl (*con_printf)(const char* format, ...);
dword stdcall (*con_get_flags)(void); dword __stdcall (*con_get_flags)(void);
dword stdcall (*con_set_flags)(dword new_flags); dword __stdcall (*con_set_flags)(dword new_flags);
int stdcall (*con_get_font_height)(void); int __stdcall (*con_get_font_height)(void);
int stdcall (*con_get_cursor_height)(void); int __stdcall (*con_get_cursor_height)(void);
int stdcall (*con_set_cursor_height)(int new_height); int __stdcall (*con_set_cursor_height)(int new_height);
int stdcall (*con_getch)(void); int __stdcall (*con_getch)(void);
word stdcall (*con_getch2)(void); word __stdcall (*con_getch2)(void);
int stdcall (*con_kbhit)(void); int __stdcall (*con_kbhit)(void);
char* stdcall (*con_gets)(char* str, int n); char* __stdcall (*con_gets)(char* str, int n);
char* stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n); char* __stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n);
void stdcall (*con_cls)(); void __stdcall (*con_cls)();
void stdcall (*con_get_cursor_pos)(int* px, int* py); void __stdcall (*con_get_cursor_pos)(int* px, int* py);
void stdcall (*con_set_cursor_pos)(int x, int y); void __stdcall (*con_set_cursor_pos)(int x, int y);
/*static void __con_panic(char* func_name)
{
debug_printf("In console.obj %s=NULL!\n", func_name);
_ksys_exit();
}*/
static void __con_lib_link(ksys_dll_t *exp) static void __con_lib_link(ksys_dll_t *exp)
{ {