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

@ -5,124 +5,132 @@
; 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
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_lib:
stdcall dll.Link, eax, edx mcall 68, 19, edi ;s_libdir
push eax or eax, eax
mov eax, [eax] jz .fail
cmp dword[eax], 'lib_' stdcall dll.Link, eax, edx
pop eax push eax
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: .fail:
add esp, 4 add esp, 4
xor eax, eax xor eax, eax
inc eax inc eax
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
; no return value ; no return value
proc dll.Link, exp:dword, imp:dword proc dll.Link, exp:dword, imp:dword
push eax push eax
mov esi, [imp] mov esi, [imp]
test esi, esi test esi, esi
jz .done jz .done
.next: .next:
lodsd lodsd
test eax, eax test eax, eax
jz .done jz .done
stdcall dll.GetProcAddress, [exp], eax stdcall dll.GetProcAddress, [exp], eax
or eax, eax or eax, eax
jz @f jz @f
mov [esi - 4], eax mov [esi - 4], eax
jmp .next jmp .next
@@: @@:
mov dword[esp], 0 mov dword[esp], 0
.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
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
if defined dll.Load if defined dll.Load
@ -132,27 +140,27 @@ s_libdir:
end if end if
;----------------------------------------------------------------------------- ;-----------------------------------------------------------------------------
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,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)