forked from KolibriOS/kolibrios
620 lines
18 KiB
Plaintext
620 lines
18 KiB
Plaintext
; The macros for load any library/libraries:
|
|
; Copyright (c) 2009, <Lrz>
|
|
; All rights reserved.
|
|
;
|
|
; Redistribution and use in source and binary forms, with or without
|
|
; modification, are permitted provided that the following conditions are met:
|
|
; * Redistributions of source code must retain the above copyright
|
|
; notice, this list of conditions and the following disclaimer.
|
|
; * Redistributions in binary form must reproduce the above copyright
|
|
; notice, this list of conditions and the following disclaimer in the
|
|
; documentation and/or other materials provided with the distribution.
|
|
; * Neither the name of the <organization> nor the
|
|
; names of its contributors may be used to endorse or promote products
|
|
; derived from this software without specific prior written permission.
|
|
;
|
|
; THIS SOFTWARE IS PROVIDED BY Alexey Teplov aka <Lrz> ''AS IS'' AND ANY
|
|
; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
; DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
|
|
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
;*****************************************************************************
|
|
; This macros based on source code:
|
|
; <Lrz> - Alexey Teplov / Алексей Теплов
|
|
; Mario79, Mario - Marat Zakiyanov / Марат Закиянов
|
|
; Diamondz - Evgeny Grechnikov / Евгений Гречников
|
|
;------------------------
|
|
; DESCRIPTION
|
|
; Macro load_library
|
|
; Logick of work.
|
|
; A first time we must to check system path, where I belive find a system library. System path is "/sys/lib/".
|
|
; If I cannot found my library, i must to check second way. Second way is current dirrectory.
|
|
; If we cannot load library, we must show the error message:
|
|
; "I'm sorry,the programm cannot found system library box_lib.obj."
|
|
; "The find was make on 2 ways: /sys/lib/ and current dirrectory."
|
|
;
|
|
;
|
|
;---------------------------------------------------------------------
|
|
; Macro sys_load_library
|
|
; A first time we must to check own path in current dirrectory the program, where I belive find a system library.
|
|
; If I cannot found my library, i must to check second way. Second way is system path a "/sys/lib/".
|
|
; If we cannot load library, we must show the error message:
|
|
; "I'm sorry,the programm cannot found system library box_lib.obj."
|
|
; "The find was make on 2 ways: /sys/lib/ and current dirrectory."
|
|
;
|
|
;---------------------------------------------------------------------
|
|
; How can I use it?
|
|
;---------------------------------------------------------------------
|
|
;-Example using single load library
|
|
;-universal load library/librarys
|
|
;load_library library_name__, cur_dir_path__, library_path__, system_path__, \
|
|
;err_message_found_lib__, head_f_l__, myimport, err_message_import__, head_f_i__
|
|
;-if return code =-1 then exit, else normally work
|
|
; cmp eax,-1
|
|
; jz exit
|
|
;- Well, if you get
|
|
;
|
|
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
;DATA данные
|
|
;Всегда соблюдать последовательность в имени.
|
|
;system_path__ db '/sys/lib/'
|
|
;library_name__ db 'box_lib.obj',0
|
|
; Если есть желание разъединить, то нужно использовать следующию конструкцию
|
|
;system_path__ db '/sys/lib/box_lib.obj',0
|
|
;... любая последовательность других команд и определений.
|
|
;library_name__ db 'box_lib.obj',0
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;err_message_found_lib__ db 'Sorry I cannot found library box_lib.obj',0
|
|
;head_f_i__:
|
|
;head_f_l__ db 'System error',0
|
|
;err_message_import__ db 'Error on load import library box_lib.obj',0
|
|
|
|
;myimport:
|
|
;
|
|
;edit_box_draw dd aEdit_box_draw
|
|
;edit_box_key dd aEdit_box_key
|
|
;edit_box_mouse dd aEdit_box_mouse
|
|
;version_ed dd aVersion_ed
|
|
; dd 0,0
|
|
;aEdit_box_draw db 'edit_box',0
|
|
;aEdit_box_key db 'edit_box_key',0
|
|
;aEdit_box_mouse db 'edit_box_mouse',0
|
|
;aVersion_ed db 'version_ed',0
|
|
|
|
|
|
|
|
macro @use_library mem_alloc,mem_free,mem_realloc,dll_load
|
|
{
|
|
local lp1
|
|
local lp2
|
|
local lp3
|
|
local lp4
|
|
local lp5
|
|
local lp6
|
|
local file_name
|
|
|
|
library_fun_memory_alloc equ mem_alloc
|
|
library_fun_memory_free equ mem_free
|
|
library_fun_memory_realloc equ mem_realloc
|
|
library_fun_dll_load equ dll_load
|
|
|
|
align 4
|
|
arrea_xx dd 0
|
|
file_name db '/sys/@notify',0
|
|
|
|
align 4
|
|
run_notify_struct:
|
|
.Function dd 7
|
|
.Position dd 0
|
|
.Flags dd ?
|
|
.Count dd 0
|
|
.Buffer dd 0
|
|
db 0
|
|
.FileName dd file_name
|
|
|
|
@library_name equ dword [esp+16]
|
|
@cur_dir_path equ dword [esp+12]
|
|
@library_path equ dword [esp+8]
|
|
@point_dir_name equ dword [esp+4]
|
|
|
|
;description:
|
|
; готовим текст для показа через @notify:
|
|
; 1) выделяем память в [arrea_xx] но не больше одного раза
|
|
; 2) копируем заголовок (если есть имя функции то добавляем его к заголовку) и текст сообщения в [arrea_xx]
|
|
;input:
|
|
; ebp+8 - head message
|
|
; ebp+12 - error message
|
|
; ebp+16 - 0 или имя функции, которую не удалось экспортировать
|
|
;output:
|
|
; eax = -1
|
|
align 4
|
|
l_lib_init_error_window:
|
|
push ebp
|
|
mov ebp,esp
|
|
cmp dword[arrea_xx],0
|
|
jne .no_msg ;если раньше было создано другое сообщение
|
|
pushad
|
|
|
|
mcall 68,11
|
|
mcall 68,12,4096
|
|
mov [arrea_xx],eax
|
|
|
|
mov edi,eax
|
|
mov esi,[ebp+8]
|
|
align 4
|
|
@@:
|
|
movsb
|
|
cmp byte[esi],0
|
|
jne @b
|
|
mov word[edi],0xa0d
|
|
add edi,2
|
|
|
|
;добавляем имя функции в заголовок
|
|
mov esi,[ebp+16]
|
|
or esi,esi
|
|
jz .lp1
|
|
mov word[edi-2],0x2020
|
|
@@:
|
|
movsb
|
|
cmp byte[esi],0
|
|
jne @b
|
|
mov word[edi],0xa0d
|
|
add edi,2
|
|
.lp1:
|
|
|
|
mov esi,[ebp+12]
|
|
align 4
|
|
@@:
|
|
movsb
|
|
cmp byte[esi],0
|
|
jne @b
|
|
mov byte[edi],0
|
|
popad
|
|
.no_msg:
|
|
or eax,-1
|
|
pop ebp
|
|
ret 12
|
|
|
|
align 4
|
|
@copy_path:
|
|
mov esi,@cur_dir_path
|
|
mov edi,@library_path
|
|
xor eax,eax
|
|
cld
|
|
align 4
|
|
.lp1:
|
|
lodsb
|
|
stosb
|
|
test eax,eax
|
|
jnz .lp1
|
|
mov esi,edi
|
|
dec esi ;переход на символ конца строки @cur_dir_path
|
|
std
|
|
align 4
|
|
.lp2:
|
|
lodsb
|
|
cmp al,'/'
|
|
jnz .lp2
|
|
mov edi,esi
|
|
add edi,2
|
|
cld
|
|
mov esi,@point_dir_name
|
|
test esi,esi
|
|
jz .str_lp4
|
|
|
|
;проверка относительных путей c двумя точками '../'
|
|
cmp word[esi],'..'
|
|
jne .lp3
|
|
dec edi ;для перехода на '/'
|
|
.lp6:
|
|
add esi,3 ;пропускаем одно поднятие '../'
|
|
.lp5:
|
|
dec edi ;идем по папкам
|
|
cmp byte[edi],'/'
|
|
jnz .lp5
|
|
cmp word[esi],'..'
|
|
je .lp6
|
|
inc edi ;для перехода на '/'
|
|
|
|
;копирование относительного пути
|
|
align 4
|
|
.lp3:
|
|
lodsb
|
|
stosb
|
|
test eax,eax
|
|
jnz .lp3
|
|
dec edi
|
|
.str_lp4:
|
|
mov esi,@library_name
|
|
align 4
|
|
.lp4:
|
|
lodsb
|
|
stosb
|
|
test eax,eax
|
|
jnz .lp4
|
|
ret
|
|
}
|
|
;---------------------------------------------------------------------
|
|
|
|
macro @use_library_mem mem_alloc,mem_free,mem_realloc,dll_load
|
|
{
|
|
@use_library mem_alloc,mem_free,mem_realloc,dll_load
|
|
}
|
|
|
|
macro sys_load_library library_name__, cur_dir_path__, library_path__, system_path__, err_message_found_lib__, head_f_l__, myimport, err_message_import__, head_f_i__,point_dir_name__
|
|
{
|
|
local i_begin
|
|
local i_error
|
|
local i_exit
|
|
|
|
mcall 68,19,system_path__ ; load of sys directory
|
|
test eax,eax
|
|
jnz i_begin
|
|
|
|
if point_dir_name__ eq
|
|
copy_path library_name__, [32], library_path__,0
|
|
else
|
|
;the macros making way /current path a program/ + name system library
|
|
copy_path library_name__, [32], library_path__,point_dir_name__
|
|
end if
|
|
mcall 68,19,library_path__ ; load of alternative
|
|
test eax,eax
|
|
jnz i_begin
|
|
push eax
|
|
push dword err_message_found_lib__
|
|
push dword head_f_l__
|
|
jmp i_error
|
|
align 4
|
|
i_begin:
|
|
import_boxlib myimport
|
|
test eax,eax
|
|
jz i_exit
|
|
push eax
|
|
push dword err_message_import__
|
|
push dword head_f_i__
|
|
i_error:
|
|
call l_lib_init_error_window
|
|
notify_window_run [arrea_xx] ; создаем окно @notify
|
|
i_exit:
|
|
}
|
|
;---------------------------------------------------------------------
|
|
|
|
macro load_library library_name__, cur_dir_path__, library_path__, system_path__, err_message_found_lib__, head_f_l__, myimport, err_message_import__, head_f_i__,point_dir_name__
|
|
{
|
|
local i_begin
|
|
local i_error
|
|
local i_exit
|
|
|
|
if point_dir_name__ eq
|
|
copy_path library_name__, [32], library_path__,0
|
|
else
|
|
;the macros making way /current path a program/ + name system library
|
|
copy_path library_name__, [32], library_path__,point_dir_name__
|
|
end if
|
|
mcall 68,19,library_path__ ; load of alternative
|
|
test eax,eax
|
|
jnz i_begin
|
|
|
|
mcall 68,19,system_path__ ; load of sys directory
|
|
test eax,eax
|
|
jnz i_begin
|
|
push eax
|
|
push dword err_message_found_lib__
|
|
push dword head_f_l__
|
|
jmp i_error
|
|
align 4
|
|
i_begin:
|
|
import_boxlib myimport
|
|
test eax,eax
|
|
jz i_exit
|
|
push eax
|
|
push dword err_message_found_lib__
|
|
push dword head_f_l__
|
|
i_error:
|
|
call l_lib_init_error_window
|
|
notify_window_run [arrea_xx] ; создаем окно @notify
|
|
i_exit:
|
|
;---------------------------------------------------------------------
|
|
}
|
|
|
|
;description:
|
|
; макрос загрузки библиотек из системной папки, если библиотека не найдена
|
|
; тогда поиск идет в текущей папке с программой
|
|
macro sys_load_libraries _start,_end
|
|
{
|
|
local cycle0
|
|
local end_steep
|
|
local cycle0n
|
|
local cycle1
|
|
local cycle1n
|
|
local cycle1e
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
library_name__ equ [ebp]
|
|
library_path__ equ [ebp+4]
|
|
system_path__ equ [ebp+8]
|
|
err_message_found_lib__ equ [ebp+12]
|
|
head_f_l__ equ [ebp+16]
|
|
my_import equ [ebp+20]
|
|
err_message_import__ equ [ebp+24]
|
|
head_f_i__ equ [ebp+28]
|
|
point_dir_name__ equ [ebp+32]
|
|
adr_load_lib equ dword [ebp+36]
|
|
status_lib equ dword [ebp+40]
|
|
|
|
mov ebp,_start
|
|
mov ecx,(_end-_start)/ll_struc_size
|
|
align 4
|
|
cycle0:
|
|
push ecx
|
|
mcall 68,19,system_path__ ; load of sys directory
|
|
test eax,eax
|
|
jnz end_steep
|
|
|
|
;the macros making way /current path a program/ + name system library
|
|
copy_path library_name__, [32], library_path__,point_dir_name__
|
|
|
|
mcall 68,19,library_path__ ; load of alternative
|
|
test eax,eax
|
|
jnz end_steep
|
|
or status_lib,1 ; status of code - enable error - not found library
|
|
|
|
push eax
|
|
push dword err_message_found_lib__
|
|
push dword head_f_l__
|
|
call l_lib_init_error_window
|
|
jmp cycle0n
|
|
|
|
align 4
|
|
end_steep:
|
|
mov adr_load_lib,eax ;save adr lib in memory
|
|
import_boxlib my_import
|
|
test eax,eax
|
|
jz cycle0n
|
|
or status_lib,2 ; status of code - enable error - import error
|
|
push eax
|
|
push dword err_message_import__
|
|
push dword head_f_i__
|
|
call l_lib_init_error_window
|
|
cycle0n:
|
|
pop ecx
|
|
add ebp,ll_struc_size
|
|
dec ecx
|
|
jnz cycle0
|
|
|
|
;вывод сообщения об ошибке при загрузке
|
|
mov ebp,_start
|
|
mov ecx,(_end-_start)/ll_struc_size
|
|
align 4
|
|
cycle1:
|
|
mov eax,status_lib
|
|
test eax,eax
|
|
jz cycle1n
|
|
notify_window_run [arrea_xx] ; создаем окно @notify
|
|
mov eax,-1
|
|
jmp cycle1e
|
|
align 4
|
|
cycle1n:
|
|
add ebp,ll_struc_size
|
|
dec ecx
|
|
jnz cycle1
|
|
cycle1e:
|
|
}
|
|
|
|
;description:
|
|
; макрос загрузки библиотек из текущей папки с программой, если библиотека не найдена
|
|
; тогда поиск идет в системной папке
|
|
macro load_libraries _start,_end
|
|
{
|
|
local cycle0
|
|
local end_steep
|
|
local cycle0n
|
|
local cycle1
|
|
local cycle1n
|
|
local cycle1e
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
library_name__ equ [ebp]
|
|
library_path__ equ [ebp+4]
|
|
system_path__ equ [ebp+8]
|
|
err_message_found_lib__ equ [ebp+12]
|
|
head_f_l__ equ [ebp+16]
|
|
my_import equ [ebp+20]
|
|
err_message_import__ equ [ebp+24]
|
|
head_f_i__ equ [ebp+28]
|
|
point_dir_name__ equ [ebp+32]
|
|
adr_load_lib equ dword [ebp+36]
|
|
status_lib equ dword [ebp+40]
|
|
|
|
mov ebp,_start
|
|
mov ecx,(_end-_start)/ll_struc_size
|
|
align 4
|
|
cycle0:
|
|
push ecx
|
|
|
|
;the macros making way /current path a program/ + name system library
|
|
copy_path library_name__, [32], library_path__,point_dir_name__
|
|
|
|
mcall 68,19,library_path__ ; load of alternative
|
|
test eax,eax
|
|
jnz end_steep
|
|
|
|
mcall 68,19,system_path__ ; load of sys directory
|
|
test eax,eax
|
|
jnz end_steep
|
|
or status_lib,1 ; status of code - enable error - not found library
|
|
|
|
push eax
|
|
push dword err_message_found_lib__
|
|
push dword head_f_l__
|
|
call l_lib_init_error_window
|
|
jmp cycle0n
|
|
|
|
align 4
|
|
end_steep:
|
|
mov adr_load_lib,eax ;save adr lib in memory
|
|
import_boxlib my_import
|
|
test eax,eax
|
|
jz cycle0n
|
|
or status_lib,2 ; status of code - enable error - import error
|
|
push eax
|
|
push dword err_message_import__
|
|
push dword head_f_i__
|
|
call l_lib_init_error_window
|
|
cycle0n:
|
|
pop ecx
|
|
add ebp,ll_struc_size
|
|
dec ecx
|
|
jnz cycle0
|
|
|
|
;вывод сообщения об ошибке при загрузке
|
|
mov ebp,_start
|
|
mov ecx,(_end-_start)/ll_struc_size
|
|
align 4
|
|
cycle1:
|
|
mov eax,status_lib
|
|
test eax,eax
|
|
jz cycle1n
|
|
notify_window_run [arrea_xx] ; создаем окно @notify
|
|
mov eax,-1
|
|
jmp cycle1e
|
|
align 4
|
|
cycle1n:
|
|
add ebp,ll_struc_size
|
|
dec ecx
|
|
jnz cycle1
|
|
cycle1e:
|
|
}
|
|
|
|
|
|
macro copy_path lib_name,dir_path,lib_path,point_dir_name
|
|
{
|
|
pushad ;save all registers
|
|
push dword lib_name
|
|
push dword dir_path
|
|
push dword lib_path
|
|
push dword point_dir_name
|
|
call @copy_path
|
|
|
|
add esp,16
|
|
;notify_window_run lib_path ;unblok for test load path
|
|
popad ;restore all registers
|
|
}
|
|
|
|
; включаем показ сообщения через @notify:
|
|
macro notify_window_run message
|
|
{
|
|
push eax ebx
|
|
mov eax,message ;параметры для командной строки
|
|
mov [run_notify_struct.Flags],eax
|
|
mov eax,70 ;run @notify
|
|
mov ebx,run_notify_struct
|
|
int 0x40
|
|
pop ebx eax
|
|
}
|
|
|
|
|
|
;input:
|
|
; eax - адрес библиотеки в памяти
|
|
; myimport - импортируемые функции
|
|
;output:
|
|
; eax - если удачно то 0 или указатель на имя функции которую не удалось загрузить
|
|
macro import_boxlib myimport
|
|
{
|
|
local import_loop
|
|
local import_find
|
|
local lp
|
|
local import_find_next
|
|
local import_found
|
|
local import_done
|
|
local exit
|
|
local import_not_found
|
|
; initialize import
|
|
|
|
mov edx, eax
|
|
mov esi,myimport
|
|
import_loop:
|
|
lodsd ;mov eax,dword[esi] ;add esi,4 ;получаем в eax указатель на имя импортируемой функции
|
|
test eax, eax
|
|
jz import_done ;если указатель на имя функции = 0 (в пользовательской программе)
|
|
push edx ;сохраняем начало библиотечных указателей на функции
|
|
import_find:
|
|
mov ebx, [edx]
|
|
test ebx, ebx
|
|
jz import_not_found ;если указатель на имя функции = 0 (в библиотеке)
|
|
push eax ;eax - указатель на имя экспортируемой функции (в пользовательской программе)
|
|
lp:
|
|
mov cl, [eax]
|
|
cmp cl, [ebx] ;сравниваем имена функций в библиотеке и в пользовательской программе
|
|
jnz import_find_next ;если названия не совпали
|
|
test cl, cl
|
|
jz import_found ;если названия совпали, и уже конец строки (cl=0)
|
|
inc eax
|
|
inc ebx
|
|
jmp lp
|
|
import_find_next:
|
|
pop eax
|
|
add edx, 8 ;8 = 4 байта указатель на название и 4 байта указатель на функцию
|
|
jmp import_find
|
|
import_found:
|
|
pop ebx ;востанавливаем указатель на имя функции (который был в eax) и освобождаем стек
|
|
mov eax, [edx+4] ;eax = указатель на функцию (в библиотеке)
|
|
mov [esi-4], eax ;копируем указатель (на функцию) в программу, -4 ставим потому что esi было сдвинуто командой lodsd
|
|
pop edx ;устанавливаем edx на начало библиотечных функций
|
|
;--- проверяем совпадает ли имя экспортированной функции с 'lib_init'
|
|
if library_fun_memory_alloc eq
|
|
else
|
|
cmp dword[ebx],'lib_'
|
|
jne import_loop
|
|
cmp dword[ebx+4],'init'
|
|
jne import_loop
|
|
;--- если имя функции совпало с 'lib_init' попадаем сюда
|
|
;подключение функций для работы с памятью
|
|
;push eax
|
|
;call dll.Init
|
|
pushad
|
|
mov esi,eax
|
|
mov eax,library_fun_memory_alloc
|
|
mov ebx,library_fun_memory_free
|
|
mov ecx,library_fun_memory_realloc
|
|
mov edx,library_fun_dll_load
|
|
call dword esi
|
|
popad
|
|
end if
|
|
jmp import_loop
|
|
import_not_found:
|
|
add esp,4
|
|
jmp exit
|
|
import_done:
|
|
xor eax,eax ;=0 все загрузилось удачно
|
|
exit:
|
|
}
|
|
;---------------------------------------------------------------------
|
|
|
|
ll_struc_size = 44;($-library_name__) ; constant size of struct
|
|
struc l_libs library_name__, cur_dir_path__, library_path__, system_path__, err_message_found_lib__, head_f_l__, my_import, err_message_import__, head_f_i__,point_dir_name; struct for loading libraries
|
|
{
|
|
.library_name__ dd library_name__ ; имя загружаемой библиотеки
|
|
|
|
.library_path__ dd library_path__ ; указатель на буфер в котором будет софоримирован путь к библиотеки, если нужно вычислить путь до либы с места запуска программы, обычно нужно, в случаях, если либа расположена в той же папке
|
|
.complete_path dd system_path__ ; путь который четко содержит путь
|
|
|
|
.err_message_found_lib__ dd err_message_found_lib__
|
|
.head_f_l__ dd head_f_l__
|
|
.my_import dd my_import
|
|
.err_message_import__ dd err_message_import__
|
|
.head_f_i__ dd head_f_i__
|
|
if point_dir_name eq
|
|
.point_dir_name__ dd 0
|
|
else
|
|
.point_dir_name__ dd point_dir_name ; имя вложенной дирректории в кторой храняться подгружаемые модули.
|
|
end if
|
|
.adr_load_lib dd 0
|
|
.status_lib dd 0 ;status of load library
|
|
;
|
|
}
|