From 711f5a93cf708e9b40c2f937469934f270a02476 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Thu, 29 Oct 2020 16:12:21 -0400 Subject: outline --- src/dlfcn/RTLD_GLOBAL.h | 5 +++++ src/dlfcn/RTLD_LAZY.h | 5 +++++ src/dlfcn/RTLD_LOCAL.h | 5 +++++ src/dlfcn/RTLD_NOW.h | 5 +++++ src/dlfcn/_dlfcn.h | 12 ++++++++++++ src/dlfcn/dlclose.c | 13 +++++++++++++ src/dlfcn/dlerror.c | 12 ++++++++++++ src/dlfcn/dlopen.c | 34 ++++++++++++++++++++++++++++++++++ src/dlfcn/dlsym.c | 13 +++++++++++++ 9 files changed, 104 insertions(+) create mode 100644 src/dlfcn/RTLD_GLOBAL.h create mode 100644 src/dlfcn/RTLD_LAZY.h create mode 100644 src/dlfcn/RTLD_LOCAL.h create mode 100644 src/dlfcn/RTLD_NOW.h create mode 100644 src/dlfcn/_dlfcn.h create mode 100644 src/dlfcn/dlclose.c create mode 100644 src/dlfcn/dlerror.c create mode 100644 src/dlfcn/dlopen.c create mode 100644 src/dlfcn/dlsym.c diff --git a/src/dlfcn/RTLD_GLOBAL.h b/src/dlfcn/RTLD_GLOBAL.h new file mode 100644 index 00000000..d9dee8b2 --- /dev/null +++ b/src/dlfcn/RTLD_GLOBAL.h @@ -0,0 +1,5 @@ +#define RTLD_GLOBAL (1<<2) + +/* +XOPEN(500) +*/ diff --git a/src/dlfcn/RTLD_LAZY.h b/src/dlfcn/RTLD_LAZY.h new file mode 100644 index 00000000..f123753b --- /dev/null +++ b/src/dlfcn/RTLD_LAZY.h @@ -0,0 +1,5 @@ +#define RTLD_LAZY (1<<0) + +/* +XOPEN(500) +*/ diff --git a/src/dlfcn/RTLD_LOCAL.h b/src/dlfcn/RTLD_LOCAL.h new file mode 100644 index 00000000..817d3adf --- /dev/null +++ b/src/dlfcn/RTLD_LOCAL.h @@ -0,0 +1,5 @@ +#define RTLD_LOCAL (1<<3) + +/* +XOPEN(500) +*/ diff --git a/src/dlfcn/RTLD_NOW.h b/src/dlfcn/RTLD_NOW.h new file mode 100644 index 00000000..91b98afb --- /dev/null +++ b/src/dlfcn/RTLD_NOW.h @@ -0,0 +1,5 @@ +#define RTLD_NOW (1<<1) + +/* +XOPEN(500) +*/ diff --git a/src/dlfcn/_dlfcn.h b/src/dlfcn/_dlfcn.h new file mode 100644 index 00000000..fd5e30d0 --- /dev/null +++ b/src/dlfcn/_dlfcn.h @@ -0,0 +1,12 @@ +#ifndef ___DLFCN_H__ +#define ___DLFCN_H__ + +#include + +struct dlhandle { + void *base; + size_t size; + int fd; +}; + +#endif diff --git a/src/dlfcn/dlclose.c b/src/dlfcn/dlclose.c new file mode 100644 index 00000000..cfe43112 --- /dev/null +++ b/src/dlfcn/dlclose.c @@ -0,0 +1,13 @@ +#include +#include "_dlfcn.h" + +int dlclose(void *handle) +{ + struct dlhandle *h = handle; + munmap(h->base, h->size); + close(h->fd); +} + +/* +XOPEN(500) +*/ diff --git a/src/dlfcn/dlerror.c b/src/dlfcn/dlerror.c new file mode 100644 index 00000000..fb1dc0e6 --- /dev/null +++ b/src/dlfcn/dlerror.c @@ -0,0 +1,12 @@ +#include +#include "_dlfcn.h" + +char *dlerror(void) +{ + extern char *__dlerror; + return __dlerror; +} + +/* +XOPEN(500) +*/ diff --git a/src/dlfcn/dlopen.c b/src/dlfcn/dlopen.c new file mode 100644 index 00000000..f67e51ff --- /dev/null +++ b/src/dlfcn/dlopen.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include "_dlfcn.h" + +void *dlopen(const char *file, int mode) +{ + if ((mode & (RTLD_LAZY | RTLD_NOW)) == (RTLD_LAZY | RTLD_NOW)) { + return NULL; + } + + if ((mode & (RTLD_GLOBAL | RTLD_LOCAL)) == (RTLD_GLOBAL | RTLD_LOCAL)) { + return NULL; + } + + struct dlhandle *h = malloc(sizeof(*h)); + if (h == NULL) { + return NULL; + } + + h->fd = open(file, O_RDONLY | O_EXEC); + if (h->fd == -1) { + free(h); + return NULL; + } + + /* map and verify file header */ + + return h; +} + +/* +XOPEN(500) +*/ diff --git a/src/dlfcn/dlsym.c b/src/dlfcn/dlsym.c new file mode 100644 index 00000000..1648fdbc --- /dev/null +++ b/src/dlfcn/dlsym.c @@ -0,0 +1,13 @@ +#include +#include "_dlfcn.h" + +void *dlsym(void *restrict handle, const char *restrict name) +{ + struct dlhandle *h = handle; + (void)name; + return h; +} + +/* +XOPEN(500) +*/ -- cgit v1.2.1