From 3a14878e457555761cafe26c6c1f328da35b9b77 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 9 Aug 2023 15:56:39 -0600 Subject: [PATCH] add stubs for dlopen, dlsym, etc. This adds weak exports for the POSIX `dlopen`, `dlsym`, `dlclose`, and `dlerror` functions, allowing code which uses those features to compile. The implementations are stubs which always fail since there is currently no official standard for runtime dynamic linking. Since the symbols are weak, they can be overriden with useful, runtime-specific implementations, e.g. based on host functions or statically-generated tables (see https://github.com/dicej/component-linking-demo for an example of the latter). Signed-off-by: Joel Dice --- Makefile | 2 +- .../wasm32-wasi-threads/defined-symbols.txt | 4 +++ expected/wasm32-wasi-threads/include-all.c | 1 + .../wasm32-wasi-threads/predefined-macros.txt | 10 ++++++ expected/wasm32-wasi/defined-symbols.txt | 4 +++ expected/wasm32-wasi/include-all.c | 1 + expected/wasm32-wasi/predefined-macros.txt | 10 ++++++ libc-top-half/musl/src/misc/dl.c | 31 +++++++++++++++++++ 8 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 libc-top-half/musl/src/misc/dl.c diff --git a/Makefile b/Makefile index 548c5cace..82dd1cefa 100644 --- a/Makefile +++ b/Makefile @@ -90,6 +90,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \ misc/a64l.c \ misc/basename.c \ misc/dirname.c \ + misc/dl.c \ misc/ffs.c \ misc/ffsl.c \ misc/ffsll.c \ @@ -445,7 +446,6 @@ MUSL_OMIT_HEADERS += \ "netdb.h" \ "resolv.h" \ "pty.h" \ - "dlfcn.h" \ "setjmp.h" \ "ulimit.h" \ "sys/xattr.h" \ diff --git a/expected/wasm32-wasi-threads/defined-symbols.txt b/expected/wasm32-wasi-threads/defined-symbols.txt index c913f1620..d64d022ea 100644 --- a/expected/wasm32-wasi-threads/defined-symbols.txt +++ b/expected/wasm32-wasi-threads/defined-symbols.txt @@ -573,6 +573,10 @@ difftime dirfd dirname div +dlclose +dlerror +dlopen +dlsym dprintf drand48 drem diff --git a/expected/wasm32-wasi-threads/include-all.c b/expected/wasm32-wasi-threads/include-all.c index 0b43b07d2..28b592438 100644 --- a/expected/wasm32-wasi-threads/include-all.c +++ b/expected/wasm32-wasi-threads/include-all.c @@ -75,6 +75,7 @@ #include #include #include +#include #include #include #include diff --git a/expected/wasm32-wasi-threads/predefined-macros.txt b/expected/wasm32-wasi-threads/predefined-macros.txt index 820f84911..8461161fc 100644 --- a/expected/wasm32-wasi-threads/predefined-macros.txt +++ b/expected/wasm32-wasi-threads/predefined-macros.txt @@ -1499,6 +1499,15 @@ #define RRFIXEDSZ NS_RRFIXEDSZ #define RRQ 01 #define RS_HIPRI 0x01 +#define RTLD_DEFAULT ((void *)0) +#define RTLD_DI_LINKMAP 2 +#define RTLD_GLOBAL 256 +#define RTLD_LAZY 1 +#define RTLD_LOCAL 0 +#define RTLD_NEXT ((void *)-1) +#define RTLD_NODELETE 4096 +#define RTLD_NOLOAD 4 +#define RTLD_NOW 2 #define RUSAGE_CHILDREN 2 #define RUSAGE_SELF 1 #define R_OK (4) @@ -2044,6 +2053,7 @@ #define _Complex_I (0.0f+1.0fi) #define _DIRENT_H #define _DIRENT_HAVE_D_TYPE +#define _DLFCN_H #define _ENDIAN_H #define _ERRNO_H #define _ERR_H diff --git a/expected/wasm32-wasi/defined-symbols.txt b/expected/wasm32-wasi/defined-symbols.txt index 6e743228b..a66d84472 100644 --- a/expected/wasm32-wasi/defined-symbols.txt +++ b/expected/wasm32-wasi/defined-symbols.txt @@ -509,6 +509,10 @@ difftime dirfd dirname div +dlclose +dlerror +dlopen +dlsym dprintf drand48 drem diff --git a/expected/wasm32-wasi/include-all.c b/expected/wasm32-wasi/include-all.c index 86297f3ef..297e48b14 100644 --- a/expected/wasm32-wasi/include-all.c +++ b/expected/wasm32-wasi/include-all.c @@ -75,6 +75,7 @@ #include #include #include +#include #include #include #include diff --git a/expected/wasm32-wasi/predefined-macros.txt b/expected/wasm32-wasi/predefined-macros.txt index 2f2ca8fdb..f75daa0a7 100644 --- a/expected/wasm32-wasi/predefined-macros.txt +++ b/expected/wasm32-wasi/predefined-macros.txt @@ -1467,6 +1467,15 @@ #define RRFIXEDSZ NS_RRFIXEDSZ #define RRQ 01 #define RS_HIPRI 0x01 +#define RTLD_DEFAULT ((void *)0) +#define RTLD_DI_LINKMAP 2 +#define RTLD_GLOBAL 256 +#define RTLD_LAZY 1 +#define RTLD_LOCAL 0 +#define RTLD_NEXT ((void *)-1) +#define RTLD_NODELETE 4096 +#define RTLD_NOLOAD 4 +#define RTLD_NOW 2 #define RUSAGE_CHILDREN 2 #define RUSAGE_SELF 1 #define R_OK (4) @@ -2010,6 +2019,7 @@ #define _Complex_I (0.0f+1.0fi) #define _DIRENT_H #define _DIRENT_HAVE_D_TYPE +#define _DLFCN_H #define _ENDIAN_H #define _ERRNO_H #define _ERR_H diff --git a/libc-top-half/musl/src/misc/dl.c b/libc-top-half/musl/src/misc/dl.c new file mode 100644 index 000000000..58164b90a --- /dev/null +++ b/libc-top-half/musl/src/misc/dl.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +#define EXPORT __attribute__((visibility("default"))) __attribute__((used)) +#define WEAK __attribute__((__weak__)) + +static const char *error = 0; + +EXPORT WEAK int dlclose(void *library) { + error = "dlclose not implemented"; + return -1; +} + +EXPORT WEAK char *dlerror(void) { + const char *var = error; + error = 0; + return (char*) var; +} + +EXPORT WEAK void *dlopen(const char *name, int flags) { + error = "dlopen not implemented"; + return 0; +} + +EXPORT WEAK void *dlsym(void *library, const char *name) { + error = "dlsym not implemented"; + return 0; +}