diff --git a/programs/develop/ktcc/trunk/libc/build.bat b/programs/develop/ktcc/trunk/libc/build.bat index 0d16df02f6..265d5a2259 100644 --- a/programs/develop/ktcc/trunk/libc/build.bat +++ b/programs/develop/ktcc/trunk/libc/build.bat @@ -6,11 +6,11 @@ echo #################################################### rem #### CONFIG SECTION #### set LIBNAME=libck.a set INCLUDE=include -set CC=kos32-tcc +set CC=kos32-tcc set CFLAGS=-c -nostdinc -DGNUC -I"%cd%\%INCLUDE%" -Wall set AR=kos32-ar set ASM=fasm -set dirs=stdio memory kolibrisys string stdlib math +set dirs=stdio memory kolibrisys string stdlib math dlfcn rem #### END OF CONFIG SECTION #### set objs= diff --git a/programs/develop/ktcc/trunk/libc/dlfcn/dlfcn.c b/programs/develop/ktcc/trunk/libc/dlfcn/dlfcn.c new file mode 100644 index 0000000000..c6f8076ea5 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/dlfcn/dlfcn.c @@ -0,0 +1,89 @@ +#include + +#include +#include +#include + +typedef struct { + char *name; + void *ptr; +} KosExp; + +typedef struct { + void **importNames; + char * libraryName; +} KosImp; + +static const char *__error; + +static int stdcall dll_Load(KosImp *importTable); + +static int stdcall dll_Load(KosImp *importTableEntry) { + for (; importTableEntry->importNames; importTableEntry++) { + char libPath[256] = "/sys/lib/"; + KosExp *exports = NULL; + void **libImports = importTableEntry->importNames; + + strcat(libPath, importTableEntry->libraryName); + if (!(exports = dlopen(libPath, 0))) { return 1; } + for (; *libImports; libImports++) { + if (!(*libImports = dlsym(exports, *libImports))) { return 1; } + } + } + return 0; +} + +// https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlopen.html +// Current implementation fully ignores "mode" parameter +void *dlopen(const char *name, int mode) { + KosExp *exports = NULL; + + // загрузить либу сискаллом + asm volatile ("int $0x40":"=a"(exports):"a"(68), "b"(19), "c"(name)); + if (!exports) { + char libPath[256] = "/sys/lib/"; + + strcat(libPath, name); + asm volatile ("int $0x40":"=a"(exports):"a"(68), "b"(19), "c"(libPath)); + if (!exports) { + __error = "Library not found in \"/sys/lib/\" nor current folder"; + return NULL; + } + } + // Вызвать что-нибудь что начинается с "lib_" + for (KosExp *export = exports; export->name; export++) { + if (!memcmp(export->name, "lib_", 4)) { + asm volatile ( + "call *%4" :: + "a"(sysmalloc), + "b"(sysfree), + "c"(sysrealloc), + "d"(dll_Load), + "r"(export->ptr)); + } + } + return exports; +} + +// https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlsym.html +void *dlsym(void *handle, const char *name) { + KosExp *exp = handle; + + for (; exp->name; exp++) { + if (!strcmp(exp->name, name)) { + return exp->ptr; + } + } + __error = "Symbol not found"; + return NULL; +} + +// https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlclose.html +int dlclose(void *handle) { + return 0; +} + +// https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlerror.html +char *dlerror(void) { + return strdup(__error); +} diff --git a/programs/develop/ktcc/trunk/libc/include/dlfcn.h b/programs/develop/ktcc/trunk/libc/include/dlfcn.h new file mode 100644 index 0000000000..019d10ee7a --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/dlfcn.h @@ -0,0 +1,14 @@ +#ifndef _DLFCN_H +#define _DLFCN_H + +#define RTLD_LAZY 0x00001 +#define RTLD_NOW 0x00002 +#define RTLD_GLOBAL 0x00100 +#define RTLD_LOCAL 0 + +int dlclose(void *handle); +char *dlerror(void); +void *dlopen(const char *name, int mode); +void *dlsym(void *restrict handle, const char *restrict name); + +#endif \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc/memory/memalloc.asm b/programs/develop/ktcc/trunk/libc/memory/memalloc.asm index 9b0a710742..a68f188bd5 100644 --- a/programs/develop/ktcc/trunk/libc/memory/memalloc.asm +++ b/programs/develop/ktcc/trunk/libc/memory/memalloc.asm @@ -9,30 +9,38 @@ public sysrealloc align 4 sysmalloc: push ebx - mov eax,68 - mov ebx,12 - mov ecx,[esp+8] ;size - int 0x40 + push ecx + mov eax,68 + mov ebx,12 + mov ecx,[esp+12] ;size + int 0x40 + pop ecx pop ebx - ret 4 + ret 4 align 4 sysfree: push ebx - mov eax,68 - mov ebx,13 - mov ecx,[esp+8] - int 0x40 + push ecx + mov eax,68 + mov ebx,13 + mov ecx,[esp+12] + int 0x40 + pop ecx pop ebx - ret 4 + ret 4 align 4 sysrealloc: push ebx - mov ebx,20 - mov eax,68 - mov edx,[esp+8] ; pointer - mov ecx,[esp+12] ; size - int 0x40 + push ecx + push edx + mov eax,68 + mov ebx,20 + mov ecx,[esp+20] ; size + mov edx,[esp+16] ; pointer + int 0x40 + pop edx + pop ecx pop ebx - ret 8 + ret 8