Updated dll.load. Added support for loading the library using the full path.

Fixed makefile for Whether  

git-svn-id: svn://kolibrios.org@9908 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Doczom 2023-03-28 16:39:08 +00:00
parent 3ade12b666
commit 77a4688f6d
3 changed files with 280 additions and 264 deletions

View File

@ -20,72 +20,80 @@ ERROR_ENTRY_NOT_FOUND = 0x101
; return 0 if all fine or error code LIBRARY_NOT_LOAD or ENTRY_NOT_FOUND ; return 0 if all fine or error code LIBRARY_NOT_LOAD or ENTRY_NOT_FOUND
; dirties all registers! eax, ebx, ecx, edx, esi, edi ; dirties all registers! eax, ebx, ecx, edx, esi, edi
proc dll.Load, import_table:dword proc dll.Load, import_table:dword
mov esi, [import_table] mov esi, [import_table]
.next_lib: .next_lib:
mov edx, [esi] mov edx, [esi]
or edx, edx or edx, edx
jz .exit jz .exit
push esi push esi
mov esi, [esi + 4] mov esi, [esi + 4]
mov edi, s_libdir.fname
mov edi, esi
cmp byte[esi], '/'
jz .load_lib
mov edi, s_libdir.fname
@@: @@:
lodsb lodsb
stosb stosb
or al, al or al, al
jnz @b jnz @b
mcall 68, 19, s_libdir
or eax, eax mov edi, s_libdir
jz .fail_load .load_lib:
push eax mcall 68, 19, edi ;s_libdir
stdcall dll.Link, eax, edx or eax, eax
test eax, eax jz .fail_load
jnz .fail_link push eax
;push eax stdcall dll.Link, eax, edx
mov eax, [esp] test eax, eax
mov eax, [eax] jnz .fail_link
cmp dword[eax], 'lib_' ;push eax
pop eax mov eax, [esp]
jnz @f mov eax, [eax]
stdcall dll.Init, [eax + 4] cmp dword[eax], 'lib_'
pop eax
jnz @f
stdcall dll.Init, [eax + 4]
@@: @@:
pop esi pop esi
add esi, 8 add esi, 8
jmp .next_lib jmp .next_lib
.exit: .exit:
xor eax, eax xor eax, eax
ret ret
.fail_load: .fail_load:
add esp, 4 add esp, 4
;xor eax, eax ;xor eax, eax
;inc eax ;inc eax
mov eax, ERROR_LIBRARY_NOT_LOAD mov eax, ERROR_LIBRARY_NOT_LOAD
ret ret
.fail_link: .fail_link:
add esp, 4 add esp, 4
ret ret
endp endp
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
; scans dll export table for a functions we want to import ; scans dll export table for a functions we want to import
; break scan on first unresolved import ; break scan on first unresolved import
; return value: 0 - success or ENTRY_NOT_FOUND ; return value: 0 - success or ENTRY_NOT_FOUND
proc dll.Link, exp:dword, imp:dword proc dll.Link, exp:dword, imp:dword
;push eax ;push eax
mov esi, [imp] mov esi, [imp]
; Import table alreary checked in APP_STARTUP_THUNK ; Import table alreary checked in APP_STARTUP_THUNK
;test esi, esi ;test esi, esi
;jz .fail1;.done ;jz .fail1;.done
.next: .next:
lodsd lodsd
test eax, eax test eax, eax
jz .done jz .done
mov ebx, eax mov ebx, eax
stdcall dll.GetProcAddress, [exp], eax stdcall dll.GetProcAddress, [exp], eax
or eax, eax or eax, eax
jz .fail ;.done jz .fail ;.done
mov [esi - 4], eax mov [esi - 4], eax
jmp .next jmp .next
; @@: ; @@:
;mov dword[esp], 0 ;mov dword[esp], 0
;.fail1: ;.fail1:
; No imports ; No imports
;mov eax, BAD_IMAGE ;mov eax, BAD_IMAGE
@ -94,67 +102,67 @@ proc dll.Link, exp:dword, imp:dword
mov [szEntryName],ebx mov [szEntryName],ebx
mov eax, ERROR_ENTRY_NOT_FOUND mov eax, ERROR_ENTRY_NOT_FOUND
.done: .done:
;pop eax ;pop eax
ret ret
endp endp
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
; calls lib_init with predefined parameters ; calls lib_init with predefined parameters
; no return value ; no return value
proc dll.Init, dllentry:dword proc dll.Init, dllentry:dword
pushad pushad
mov eax, mem.Alloc mov eax, mem.Alloc
mov ebx, mem.Free mov ebx, mem.Free
mov ecx, mem.ReAlloc mov ecx, mem.ReAlloc
mov edx, dll.Load mov edx, dll.Load
stdcall [dllentry] stdcall [dllentry]
popad popad
ret ret
endp endp
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
; scans export table for a sz_name function ; scans export table for a sz_name function
; returns in eax function address or 0 if not found ; returns in eax function address or 0 if not found
proc dll.GetProcAddress, exp:dword, sz_name:dword proc dll.GetProcAddress, exp:dword, sz_name:dword
mov edx, [exp] mov edx, [exp]
xor eax, eax xor eax, eax
.next: .next:
or edx, edx or edx, edx
jz .end jz .end
cmp dword[edx], 0 cmp dword[edx], 0
jz .end jz .end
stdcall strcmp, [edx], [sz_name] stdcall strcmp, [edx], [sz_name]
test eax, eax test eax, eax
jz .ok jz .ok
add edx, 8 add edx, 8
jmp .next jmp .next
.ok: .ok:
mov eax, [edx + 4] mov eax, [edx + 4]
.end: .end:
cmp eax, -1 cmp eax, -1
jnz @f jnz @f
xor eax, eax xor eax, eax
@@: @@:
ret ret
endp endp
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
; compares strings ; compares strings
; returns eax = 0 if equal, -1 otherwise ; returns eax = 0 if equal, -1 otherwise
proc strcmp, str1:dword, str2:dword proc strcmp, str1:dword, str2:dword
push esi edi push esi edi
mov esi, [str1] mov esi, [str1]
mov edi, [str2] mov edi, [str2]
xor eax, eax xor eax, eax
@@: @@:
lodsb lodsb
scasb scasb
jne .fail jne .fail
or al, al or al, al
jnz @b jnz @b
jmp .ok jmp .ok
.fail: .fail:
or eax, -1 or eax, -1
.ok: .ok:
pop edi esi pop edi esi
ret ret
endp endp
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
@ -166,27 +174,27 @@ szEntryName dd 0
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
proc mem.Alloc, size proc mem.Alloc, size
push ebx ecx push ebx ecx
mov ecx, [size] mov ecx, [size]
mcall 68, 12 mcall 68, 12
pop ecx ebx pop ecx ebx
ret ret
endp endp
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
proc mem.ReAlloc, mptr, size proc mem.ReAlloc, mptr, size
push ebx ecx edx push ebx ecx edx
mov ecx, [size] mov ecx, [size]
mov edx, [mptr] mov edx, [mptr]
mcall 68, 20 mcall 68, 20
pop edx ecx ebx pop edx ecx ebx
ret ret
endp endp
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
proc mem.Free, mptr proc mem.Free, mptr
push ebx ecx push ebx ecx
mov ecx,[mptr] mov ecx,[mptr]
mcall 68, 13 mcall 68, 13
pop ecx ebx pop ecx ebx
ret ret
endp endp
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------

View File

@ -1,158 +1,166 @@
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
; load one or more DLL file in COFF format and try to import functions by our list ; load one or more DLL file in COFF format and try to import functions by our list
; if first function in import list begins with 'lib_', call it as DLL initialization ; if first function in import list begins with 'lib_', call it as DLL initialization
; return eax = 1 as fail, if anyone of .obj file not found in /sys/lib ; return eax = 1 as fail, if anyone of .obj file not found in /sys/lib
; return 0 if all fine, but 0 not garantees in succesfull import - see dll.Link comment ; return 0 if all fine, but 0 not garantees in succesfull import - see dll.Link comment
; dirties all registers! eax, ebx, ecx, edx, esi, edi ; dirties all registers! eax, ebx, ecx, edx, esi, edi
proc dll.Load, import_table:dword proc dll.Load, import_table:dword
mov esi, [import_table] mov esi, [import_table]
.next_lib: .next_lib:
mov edx, [esi] mov edx, [esi]
or edx, edx or edx, edx
jz .exit jz .exit
push esi push esi
mov esi, [esi + 4] mov esi, [esi + 4]
mov edi, s_libdir.fname
@@: mov edi, esi
lodsb cmp byte[esi], '/'
stosb jz .load_lib
or al, al
jnz @b mov edi, s_libdir.fname
mcall 68, 19, s_libdir @@:
or eax, eax lodsb
jz .fail stosb
stdcall dll.Link, eax, edx or al, al
push eax jnz @b
mov eax, [eax]
cmp dword[eax], 'lib_' mov edi, s_libdir
pop eax .load_lib:
jnz @f mcall 68, 19, edi ;s_libdir
stdcall dll.Init, [eax + 4] or eax, eax
@@: jz .fail
pop esi stdcall dll.Link, eax, edx
add esi, 8 push eax
jmp .next_lib mov eax, [eax]
.exit: cmp dword[eax], 'lib_'
xor eax, eax pop eax
ret jnz @f
.fail: stdcall dll.Init, [eax + 4]
add esp, 4 @@:
xor eax, eax pop esi
inc eax add esi, 8
ret jmp .next_lib
endp .exit:
;----------------------------------------------------------------------------- xor eax, eax
; scans dll export table for a functions we want to import ret
; break scan on first unresolved import .fail:
; no return value add esp, 4
proc dll.Link, exp:dword, imp:dword xor eax, eax
push eax inc eax
mov esi, [imp] ret
test esi, esi endp
jz .done ;-----------------------------------------------------------------------------
.next: ; scans dll export table for a functions we want to import
lodsd ; break scan on first unresolved import
test eax, eax ; no return value
jz .done proc dll.Link, exp:dword, imp:dword
stdcall dll.GetProcAddress, [exp], eax push eax
or eax, eax mov esi, [imp]
jz @f test esi, esi
mov [esi - 4], eax jz .done
jmp .next .next:
@@: lodsd
mov dword[esp], 0 test eax, eax
.done: jz .done
pop eax stdcall dll.GetProcAddress, [exp], eax
ret or eax, eax
endp jz @f
;----------------------------------------------------------------------------- mov [esi - 4], eax
; calls lib_init with predefined parameters jmp .next
; no return value @@:
proc dll.Init, dllentry:dword mov dword[esp], 0
pushad .done:
mov eax, mem.Alloc pop eax
mov ebx, mem.Free ret
mov ecx, mem.ReAlloc endp
mov edx, dll.Load ;-----------------------------------------------------------------------------
stdcall [dllentry] ; calls lib_init with predefined parameters
popad ; no return value
ret proc dll.Init, dllentry:dword
endp pushad
;----------------------------------------------------------------------------- mov eax, mem.Alloc
; scans export table for a sz_name function mov ebx, mem.Free
; returns in eax function address or 0 if not found mov ecx, mem.ReAlloc
proc dll.GetProcAddress, exp:dword, sz_name:dword mov edx, dll.Load
mov edx, [exp] stdcall [dllentry]
xor eax, eax popad
.next: ret
or edx, edx endp
jz .end ;-----------------------------------------------------------------------------
cmp dword[edx], 0 ; scans export table for a sz_name function
jz .end ; returns in eax function address or 0 if not found
stdcall strcmp, [edx], [sz_name] proc dll.GetProcAddress, exp:dword, sz_name:dword
test eax, eax mov edx, [exp]
jz .ok xor eax, eax
add edx, 8 .next:
jmp .next or edx, edx
.ok: jz .end
mov eax, [edx + 4] cmp dword[edx], 0
.end: jz .end
cmp eax, -1 stdcall strcmp, [edx], [sz_name]
jnz @f test eax, eax
xor eax, eax jz .ok
@@: add edx, 8
ret jmp .next
endp .ok:
;----------------------------------------------------------------------------- mov eax, [edx + 4]
; compares strings .end:
; returns eax = 0 if equal, -1 otherwise cmp eax, -1
proc strcmp, str1:dword, str2:dword jnz @f
push esi edi xor eax, eax
mov esi, [str1] @@:
mov edi, [str2] ret
xor eax, eax endp
@@: ;-----------------------------------------------------------------------------
lodsb ; compares strings
scasb ; returns eax = 0 if equal, -1 otherwise
jne .fail proc strcmp, str1:dword, str2:dword
or al, al push esi edi
jnz @b mov esi, [str1]
jmp .ok mov edi, [str2]
.fail: xor eax, eax
or eax, -1 @@:
.ok: lodsb
pop edi esi scasb
ret jne .fail
endp or al, al
;----------------------------------------------------------------------------- jnz @b
if defined dll.Load jmp .ok
s_libdir: .fail:
db '/sys/lib/' or eax, -1
.fname rb 32 .ok:
end if pop edi esi
;----------------------------------------------------------------------------- ret
proc mem.Alloc, size endp
push ebx ecx ;-----------------------------------------------------------------------------
mov ecx, [size] if defined dll.Load
mcall 68, 12 s_libdir:
pop ecx ebx db '/sys/lib/'
ret .fname rb 32
endp end if
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
proc mem.ReAlloc, mptr, size proc mem.Alloc, size
push ebx ecx edx push ebx ecx
mov ecx, [size] mov ecx, [size]
mov edx, [mptr] mcall 68, 12
mcall 68, 20 pop ecx ebx
pop edx ecx ebx ret
ret endp
endp ;-----------------------------------------------------------------------------
;----------------------------------------------------------------------------- proc mem.ReAlloc, mptr, size
proc mem.Free, mptr push ebx ecx edx
push ebx ecx mov ecx, [size]
mov ecx,[mptr] mov edx, [mptr]
mcall 68, 13 mcall 68, 20
pop ecx ebx pop edx ecx ebx
ret ret
endp endp
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
proc mem.Free, mptr
push ebx ecx
mov ecx,[mptr]
mcall 68, 13
pop ecx ebx
ret
endp
;-----------------------------------------------------------------------------

View File

@ -1,5 +1,5 @@
KTCC_DIR = ../../develop/ktcc/trunk KTCC_DIR = ../../develop/ktcc/trunk
KLIBC = ../../develop/libraries/kolibri-libc KLIBC = ../../develop/ktcc/trunk/libc.obj
NAME = weather NAME = weather
@ -7,8 +7,8 @@ KTCC=$(KTCC_DIR)/bin/kos32-tcc
KPACK = kpack KPACK = kpack
SRC= weather.c json/json.c SRC= weather.c json/json.c
FLAGS= -nostdlib $(KLIBC)/lib/crt0.o -I $(KLIBC)/include -I $(KTCC_DIR)/libc/include -L$(KLIBC)/lib FLAGS= -B$(KTCC_DIR)/bin -I $(KLIBC)/include
LIBS =-ltcc -limg -lhttp -lc.obj LIBS = -limg -lhttp
all: all:
$(KTCC) $(FLAGS) $(SRC) $(LIBS) -o $(NAME) $(KTCC) $(FLAGS) $(SRC) $(LIBS) -o $(NAME)