2022-02-27 17:03:15 +00:00
|
|
|
#include <magenta/types.h>
|
|
|
|
|
#include <stddef.h>
|
2022-05-12 20:52:43 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
2022-02-27 17:03:15 +00:00
|
|
|
#include <dlfcn.h>
|
2022-05-12 20:52:43 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <mio/fs.h>
|
2022-02-27 17:03:15 +00:00
|
|
|
|
2022-05-12 20:52:43 +01:00
|
|
|
__attribute__((weak)) void *dyld_load_object(const char *path, mx_handle_t handle, int mode) { return NULL; }
|
2022-02-27 17:03:15 +00:00
|
|
|
__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; }
|
|
|
|
|
|
2022-05-12 20:52:43 +01:00
|
|
|
static char dl_error[512] = {};
|
|
|
|
|
|
2022-02-27 17:03:15 +00:00
|
|
|
void *dlopen(const char *path, int mode)
|
|
|
|
|
{
|
2022-05-12 20:52:43 +01:00
|
|
|
/* 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);
|
2022-02-27 17:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *dlsym(void *handle, const char *name)
|
|
|
|
|
{
|
2022-05-12 20:52:43 +01:00
|
|
|
return dyld_get_symbol(handle, name);
|
2022-02-27 17:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int dlclose(void *handle)
|
|
|
|
|
{
|
2022-05-12 20:52:43 +01:00
|
|
|
return dyld_close_object(handle);
|
2022-02-27 17:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *dlerror(void)
|
|
|
|
|
{
|
2022-05-12 20:52:43 +01:00
|
|
|
if (dl_error[0] != '\0') {
|
|
|
|
|
return dl_error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dyld_get_error();
|
2022-02-27 17:03:15 +00:00
|
|
|
}
|