Skip to content

Commit

Permalink
add stubs for dlopen, dlsym, etc.
Browse files Browse the repository at this point in the history
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 <joel.dice@fermyon.com>
  • Loading branch information
dicej committed Oct 20, 2023
1 parent be1704a commit 3a14878
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -445,7 +446,6 @@ MUSL_OMIT_HEADERS += \
"netdb.h" \
"resolv.h" \
"pty.h" \
"dlfcn.h" \
"setjmp.h" \
"ulimit.h" \
"sys/xattr.h" \
Expand Down
4 changes: 4 additions & 0 deletions expected/wasm32-wasi-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ difftime
dirfd
dirname
div
dlclose
dlerror
dlopen
dlsym
dprintf
drand48
drem
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi-threads/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include <crypt.h>
#include <ctype.h>
#include <dirent.h>
#include <dlfcn.h>
#include <endian.h>
#include <err.h>
#include <errno.h>
Expand Down
10 changes: 10 additions & 0 deletions expected/wasm32-wasi-threads/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,10 @@ difftime
dirfd
dirname
div
dlclose
dlerror
dlopen
dlsym
dprintf
drand48
drem
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include <crypt.h>
#include <ctype.h>
#include <dirent.h>
#include <dlfcn.h>
#include <endian.h>
#include <err.h>
#include <errno.h>
Expand Down
10 changes: 10 additions & 0 deletions expected/wasm32-wasi/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions libc-top-half/musl/src/misc/dl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#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;
}

0 comments on commit 3a14878

Please sign in to comment.