Skip to content

Commit e600726

Browse files
jedisct1alexrp
authored andcommitted
Add libdl shims from wasi-libc
1 parent 8a8da49 commit e600726

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef _DLFCN_H
2+
#define _DLFCN_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <features.h>
9+
10+
#define RTLD_LAZY 1
11+
#define RTLD_NOW 2
12+
#define RTLD_NOLOAD 4
13+
#define RTLD_NODELETE 4096
14+
#define RTLD_GLOBAL 256
15+
#ifdef __wasilibc_unmodified_upstream
16+
#define RTLD_LOCAL 0
17+
#else
18+
/* For WASI, we give `RTLD_LOCAL` a non-zero value, avoiding ambiguity and
19+
* allowing us to defer the decision of whether `RTLD_LOCAL` or `RTLD_GLOBAL`
20+
* should be the default when neither is specified.
21+
*/
22+
#define RTLD_LOCAL 8
23+
#endif
24+
25+
#define RTLD_NEXT ((void *)-1)
26+
#define RTLD_DEFAULT ((void *)0)
27+
28+
#ifdef __wasilibc_unmodified_upstream
29+
#define RTLD_DI_LINKMAP 2
30+
#endif
31+
32+
int dlclose(void *);
33+
char *dlerror(void);
34+
void *dlopen(const char *, int);
35+
void *dlsym(void *__restrict, const char *__restrict);
36+
37+
#if defined(__wasilibc_unmodified_upstream) && (defined(_GNU_SOURCE) || defined(_BSD_SOURCE))
38+
typedef struct {
39+
const char *dli_fname;
40+
void *dli_fbase;
41+
const char *dli_sname;
42+
void *dli_saddr;
43+
} Dl_info;
44+
int dladdr(const void *, Dl_info *);
45+
int dlinfo(void *, int, void *);
46+
#endif
47+
48+
#if _REDIR_TIME64
49+
__REDIR(dlsym, __dlsym_time64);
50+
#endif
51+
52+
#ifdef __cplusplus
53+
}
54+
#endif
55+
56+
#endif
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* This file is used to build libdl.so with stub versions of `dlopen`, `dlsym`,
2+
* etc. The intention is that this stubbed libdl.so can be used to build
3+
* libraries and applications which use `dlopen` without committing to a
4+
* specific runtime implementation. Later, it can be replaced with a real,
5+
* working libdl.so (e.g. at runtime or component composition time).
6+
*
7+
* For example, the `wasm-tools component link` subcommand can be used to create
8+
* a component that bundles any `dlopen`-able libraries in such a way that their
9+
* function exports can be resolved symbolically at runtime using an
10+
* implementation of libdl.so designed for that purpose. In other cases, a
11+
* runtime might provide Emscripten-style dynamic linking via URLs or else a
12+
* more traditional, filesystem-based implementation. Finally, even this
13+
* stubbed version of libdl.so can be used at runtime in cases where dynamic
14+
* library resolution cannot or should not be supported (and the application can
15+
* handle this situation gracefully). */
16+
17+
#include <stddef.h>
18+
#include <dlfcn.h>
19+
20+
static const char *error = NULL;
21+
22+
weak int dlclose(void *library)
23+
{
24+
error = "dlclose not implemented";
25+
return -1;
26+
}
27+
28+
weak char *dlerror(void)
29+
{
30+
const char *var = error;
31+
error = NULL;
32+
return (char*) var;
33+
}
34+
35+
weak void *dlopen(const char *name, int flags)
36+
{
37+
error = "dlopen not implemented";
38+
return NULL;
39+
}
40+
41+
weak void *dlsym(void *library, const char *name)
42+
{
43+
error = "dlsym not implemented";
44+
return NULL;
45+
}

src/Compilation.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ pub const MiscTask = enum {
806806
@"wasi crt1-reactor.o",
807807
@"wasi crt1-command.o",
808808
@"wasi libc.a",
809+
@"wasi libdl.a",
809810
@"libwasi-emulated-process-clocks.a",
810811
@"libwasi-emulated-getpid.a",
811812
@"libwasi-emulated-mman.a",

src/wasi_libc.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ pub const CrtFile = enum {
1010
crt1_reactor_o,
1111
crt1_command_o,
1212
libc_a,
13+
libdl_a,
1314
libwasi_emulated_process_clocks_a,
1415
libwasi_emulated_getpid_a,
1516
libwasi_emulated_mman_a,
1617
libwasi_emulated_signal_a,
1718
};
1819

1920
pub fn getEmulatedLibCrtFile(lib_name: []const u8) ?CrtFile {
21+
if (mem.eql(u8, lib_name, "dl")) {
22+
return .libdl_a;
23+
}
2024
if (mem.eql(u8, lib_name, "wasi-emulated-process-clocks")) {
2125
return .libwasi_emulated_process_clocks_a;
2226
}
@@ -34,6 +38,7 @@ pub fn getEmulatedLibCrtFile(lib_name: []const u8) ?CrtFile {
3438

3539
pub fn emulatedLibCRFileLibName(crt_file: CrtFile) []const u8 {
3640
return switch (crt_file) {
41+
.libdl_a => "libdl.a",
3742
.libwasi_emulated_process_clocks_a => "libwasi-emulated-process-clocks.a",
3843
.libwasi_emulated_getpid_a => "libwasi-emulated-getpid.a",
3944
.libwasi_emulated_mman_a => "libwasi-emulated-mman.a",
@@ -154,6 +159,25 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
154159

155160
try comp.build_crt_file("c", .Lib, .@"wasi libc.a", prog_node, libc_sources.items, .{});
156161
},
162+
163+
.libdl_a => {
164+
var args = std.ArrayList([]const u8).init(arena);
165+
try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
166+
try addLibcBottomHalfIncludes(comp, arena, &args);
167+
168+
var emu_dl_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
169+
for (emulated_dl_src_files) |file_path| {
170+
try emu_dl_sources.append(.{
171+
.src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
172+
"libc", try sanitize(arena, file_path),
173+
}),
174+
.extra_flags = args.items,
175+
.owner = undefined,
176+
});
177+
}
178+
try comp.build_crt_file("dl", .Lib, .@"wasi libdl.a", prog_node, emu_dl_sources.items, .{});
179+
},
180+
157181
.libwasi_emulated_process_clocks_a => {
158182
var args = std.ArrayList([]const u8).init(arena);
159183
try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
@@ -1173,6 +1197,10 @@ const libc_top_half_src_files = [_][]const u8{
11731197
const crt1_command_src_file = "wasi/libc-bottom-half/crt/crt1-command.c";
11741198
const crt1_reactor_src_file = "wasi/libc-bottom-half/crt/crt1-reactor.c";
11751199

1200+
const emulated_dl_src_files = &[_][]const u8{
1201+
"wasi/libc-top-half/musl/src/misc/dl.c",
1202+
};
1203+
11761204
const emulated_process_clocks_src_files = &[_][]const u8{
11771205
"wasi/libc-bottom-half/clocks/clock.c",
11781206
"wasi/libc-bottom-half/clocks/getrusage.c",

0 commit comments

Comments
 (0)