Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stubs for dlopen, dlsym, etc. #443

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ LIBWASI_EMULATED_SIGNAL_SOURCES = \
LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES = \
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/signal/psignal.c \
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/strsignal.c
LIBDL_SOURCES = $(LIBC_TOP_HALF_MUSL_SRC_DIR)/misc/dl.c
LIBC_BOTTOM_HALF_CRT_SOURCES = $(wildcard $(LIBC_BOTTOM_HALF_DIR)/crt/*.c)
LIBC_TOP_HALF_DIR = libc-top-half
LIBC_TOP_HALF_MUSL_DIR = $(LIBC_TOP_HALF_DIR)/musl
Expand Down Expand Up @@ -379,6 +380,7 @@ LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS = $(call objs,$(LIBWASI_EMULATED_PROCESS_CL
LIBWASI_EMULATED_GETPID_OBJS = $(call objs,$(LIBWASI_EMULATED_GETPID_SOURCES))
LIBWASI_EMULATED_SIGNAL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_SOURCES))
LIBWASI_EMULATED_SIGNAL_MUSL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES))
LIBDL_OBJS = $(call objs,$(LIBDL_SOURCES))
LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES))

# These variables describe the locations of various files and
Expand Down Expand Up @@ -445,7 +447,6 @@ MUSL_OMIT_HEADERS += \
"netdb.h" \
"resolv.h" \
"pty.h" \
"dlfcn.h" \
"setjmp.h" \
"ulimit.h" \
"sys/xattr.h" \
Expand Down Expand Up @@ -481,6 +482,7 @@ LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULA
LIBWASI_EMULATED_GETPID_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_GETPID_OBJS))
LIBWASI_EMULATED_SIGNAL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_SIGNAL_OBJS))
LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS))
LIBDL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBDL_OBJS))
BULK_MEMORY_SO_OBJS = $(patsubst %.o,%.pic.o,$(BULK_MEMORY_OBJS))
DLMALLOC_SO_OBJS = $(patsubst %.o,%.pic.o,$(DLMALLOC_OBJS))
LIBC_BOTTOM_HALF_ALL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBC_BOTTOM_HALF_ALL_OBJS))
Expand Down Expand Up @@ -521,6 +523,8 @@ $(OBJDIR)/libwasi-emulated-getpid.so.a: $(LIBWASI_EMULATED_GETPID_SO_OBJS)

$(OBJDIR)/libwasi-emulated-signal.so.a: $(LIBWASI_EMULATED_SIGNAL_SO_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS)

$(OBJDIR)/libdl.so.a: $(LIBDL_SO_OBJS)

$(SYSROOT_LIB)/libc.a: $(LIBC_OBJS)

$(SYSROOT_LIB)/libc-printscan-long-double.a: $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS)
Expand All @@ -535,6 +539,8 @@ $(SYSROOT_LIB)/libwasi-emulated-getpid.a: $(LIBWASI_EMULATED_GETPID_OBJS)

$(SYSROOT_LIB)/libwasi-emulated-signal.a: $(LIBWASI_EMULATED_SIGNAL_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS)

$(SYSROOT_LIB)/libdl.a: $(LIBDL_OBJS)

%.a:
@mkdir -p "$(@D)"
# On Windows, the commandline for the ar invocation got too long, so it needs to be split up.
Expand Down Expand Up @@ -603,7 +609,7 @@ startup_files $(LIBC_BOTTOM_HALF_ALL_OBJS) $(LIBC_BOTTOM_HALF_ALL_SO_OBJS): CFLA
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal

$(LIBC_TOP_HALF_ALL_OBJS) $(LIBC_TOP_HALF_ALL_SO_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_SO_OBJS) $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS): CFLAGS += \
$(LIBC_TOP_HALF_ALL_OBJS) $(LIBC_TOP_HALF_ALL_SO_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_SO_OBJS) $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS) $(LIBDL_OBJS) $(LIBDL_SO_OBJS): CFLAGS += \
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal \
-I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 \
Expand Down Expand Up @@ -664,7 +670,8 @@ LIBC_SO = \
$(SYSROOT_LIB)/libwasi-emulated-mman.so \
$(SYSROOT_LIB)/libwasi-emulated-process-clocks.so \
$(SYSROOT_LIB)/libwasi-emulated-getpid.so \
$(SYSROOT_LIB)/libwasi-emulated-signal.so
$(SYSROOT_LIB)/libwasi-emulated-signal.so \
$(SYSROOT_LIB)/libdl.so
endif

libc_so: include_dirs $(LIBC_SO)
Expand All @@ -676,13 +683,14 @@ libc: include_dirs \
$(SYSROOT_LIB)/libwasi-emulated-mman.a \
$(SYSROOT_LIB)/libwasi-emulated-process-clocks.a \
$(SYSROOT_LIB)/libwasi-emulated-getpid.a \
$(SYSROOT_LIB)/libwasi-emulated-signal.a
$(SYSROOT_LIB)/libwasi-emulated-signal.a \
$(SYSROOT_LIB)/libdl.a

finish: startup_files libc
#
# Create empty placeholder libraries.
#
for name in m rt pthread crypt util xnet resolv dl; do \
for name in m rt pthread crypt util xnet resolv; do \
$(AR) crs "$(SYSROOT_LIB)/lib$${name}.a"; \
done

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
9 changes: 9 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,14 @@
#define RRFIXEDSZ NS_RRFIXEDSZ
#define RRQ 01
#define RS_HIPRI 0x01
#define RTLD_DEFAULT ((void *)0)
#define RTLD_GLOBAL 256
#define RTLD_LAZY 1
#define RTLD_LOCAL 8
#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 +2052,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
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
9 changes: 9 additions & 0 deletions expected/wasm32-wasi/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,14 @@
#define RRFIXEDSZ NS_RRFIXEDSZ
#define RRQ 01
#define RS_HIPRI 0x01
#define RTLD_DEFAULT ((void *)0)
#define RTLD_GLOBAL 256
#define RTLD_LAZY 1
#define RTLD_LOCAL 8
#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 +2018,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
12 changes: 11 additions & 1 deletion libc-top-half/musl/include/dlfcn.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@ extern "C" {
#define RTLD_NOLOAD 4
#define RTLD_NODELETE 4096
#define RTLD_GLOBAL 256
#ifdef __wasilibc_unmodified_upstream
#define RTLD_LOCAL 0
#else
/* For WASI, we give `RTLD_LOCAL` a non-zero value, avoiding ambiguity and
* allowing us to defer the decision of whether `RTLD_LOCAL` or `RTLD_GLOBAL`
* should be the default when neither is specified.
*/
#define RTLD_LOCAL 8
#endif

#define RTLD_NEXT ((void *)-1)
#define RTLD_DEFAULT ((void *)0)

#ifdef __wasilibc_unmodified_upstream
#define RTLD_DI_LINKMAP 2
#endif

int dlclose(void *);
char *dlerror(void);
void *dlopen(const char *, int);
void *dlsym(void *__restrict, const char *__restrict);

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
#if defined(__wasilibc_unmodified_upstream) && (defined(_GNU_SOURCE) || defined(_BSD_SOURCE))
typedef struct {
const char *dli_fname;
void *dli_fbase;
Expand Down
45 changes: 45 additions & 0 deletions libc-top-half/musl/src/misc/dl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* This file is used to build libdl.so with stub versions of `dlopen`, `dlsym`,
* etc. The intention is that this stubbed libdl.so can be used to build
* libraries and applications which use `dlopen` without committing to a
* specific runtime implementation. Later, it can be replaced with a real,
* working libdl.so (e.g. at runtime or component composition time).
*
* For example, the `wasm-tools component link` subcommand can be used to create
* a component that bundles any `dlopen`-able libraries in such a way that their
* function exports can be resolved symbolically at runtime using an
* implementation of libdl.so designed for that purpose. In other cases, a
* runtime might provide Emscripten-style dynamic linking via URLs or else a
* more traditional, filesystem-based implementation. Finally, even this
* stubbed version of libdl.so can be used at runtime in cases where dynamic
* library resolution cannot or should not be supported (and the application can
* handle this situation gracefully). */

#include <stddef.h>
#include <dlfcn.h>

static const char *error = NULL;

weak int dlclose(void *library)
{
error = "dlclose not implemented";
return -1;
}

weak char *dlerror(void)
{
const char *var = error;
error = NULL;
return (char*) var;
}

weak void *dlopen(const char *name, int flags)
{
error = "dlopen not implemented";
return NULL;
}

weak void *dlsym(void *library, const char *name)
{
error = "dlsym not implemented";
return NULL;
}