From 673fbb4d3643b19766193e045bc5a7f50d38554b Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 12 May 2022 20:52:43 +0100 Subject: [PATCH] Implemented dlfcn functions --- photon/libc/sys/horizon/dlfcn.c | 40 +++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/photon/libc/sys/horizon/dlfcn.c b/photon/libc/sys/horizon/dlfcn.c index 9c45323..7229e8f 100644 --- a/photon/libc/sys/horizon/dlfcn.c +++ b/photon/libc/sys/horizon/dlfcn.c @@ -1,30 +1,56 @@ #include #include +#include +#include #include +#include +#include - -__attribute__((weak)) void *dyld_load_object(mx_handle_t handle, int mode) { return NULL; } +__attribute__((weak)) void *dyld_load_object(const char *path, mx_handle_t handle, int mode) { return NULL; } __attribute__((weak)) void *dyld_get_symbol(void *handle, const char *name) { return NULL; } __attribute__((weak)) int dyld_close_object(void *handle) { return 0; } __attribute__((weak)) char *dyld_get_error(void) { return NULL; } +static char dl_error[512] = {}; + void *dlopen(const char *path, int mode) { - dyld_load_object(MX_NULL_HANDLE, 0); - return NULL; + /* TODO include error message from errno */ + + int fd = open(path, O_RDONLY); + if (fd == -1) { + snprintf(dl_error, sizeof dl_error, "cannot open '%s'", path); + return NULL; + } + + mx_handle_t image_vmo = MX_NULL_HANDLE; + int err = mio_map_file(fd, &image_vmo); + close(fd); + + if (err != 0) { + snprintf(dl_error, sizeof dl_error, "cannot load '%s'", path); + return NULL; + } + + dl_error[0] = '\0'; + return dyld_load_object(path, image_vmo, mode); } void *dlsym(void *handle, const char *name) { - return NULL; + return dyld_get_symbol(handle, name); } int dlclose(void *handle) { - return 0; + return dyld_close_object(handle); } char *dlerror(void) { - return NULL; + if (dl_error[0] != '\0') { + return dl_error; + } + + return dyld_get_error(); }