2020-04-30 10:47:52 +02:00
|
|
|
#include <kos32sys1.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *name;
|
|
|
|
void *ptr;
|
|
|
|
} KosExp;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void **importNames;
|
|
|
|
char * libraryName;
|
|
|
|
} KosImp;
|
|
|
|
|
2020-05-19 14:05:54 +02:00
|
|
|
static int stdcall dll_Load(KosImp *importTableEntry);
|
2020-04-30 10:47:52 +02:00
|
|
|
|
2020-05-19 14:05:54 +02:00
|
|
|
static const char *__error;
|
2020-04-30 10:47:52 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-05-19 14:05:54 +02:00
|
|
|
// load library using syscall
|
2020-04-30 10:47:52 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 14:05:54 +02:00
|
|
|
// call anything starting with "lib_"
|
2020-04-30 10:47:52 +02:00
|
|
|
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) {
|
2020-04-30 13:01:55 +02:00
|
|
|
char *ret = __error ? strdup(__error) : NULL;
|
|
|
|
__error = NULL;
|
|
|
|
return ret;
|
2020-04-30 10:47:52 +02:00
|
|
|
}
|