authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2024-04-18 21:47:47+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-01-29 12:48:56+01:00
loge60072635eca3f3c983d92819aa67c0decc7d4a6
treebe8950a91fecf0e879e8cc797d6a1c32a1c03781
parent8a8da49b5212030d603000d699a8ca9ed92e24cd
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

Add libdl shims from wasi-libc


4 files changed, 130 insertions(+), 0 deletions(-)

lib/libc/include/wasm-wasi-musl/dlfcn.h created+56
......@@ -0,0 +1,56 @@
1#ifndef _DLFCN_H
2#define _DLFCN_H
3
4#ifdef __cplusplus
5extern "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
32int dlclose(void *);
33char *dlerror(void);
34void *dlopen(const char *, int);
35void *dlsym(void *__restrict, const char *__restrict);
36
37#if defined(__wasilibc_unmodified_upstream) && (defined(_GNU_SOURCE) || defined(_BSD_SOURCE))
38typedef struct {
39 const char *dli_fname;
40 void *dli_fbase;
41 const char *dli_sname;
42 void *dli_saddr;
43} Dl_info;
44int dladdr(const void *, Dl_info *);
45int 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
lib/libc/wasi/libc-top-half/musl/src/misc/dl.c created+45
......@@ -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
20static const char *error = NULL;
21
22weak int dlclose(void *library)
23{
24 error = "dlclose not implemented";
25 return -1;
26}
27
28weak char *dlerror(void)
29{
30 const char *var = error;
31 error = NULL;
32 return (char*) var;
33}
34
35weak void *dlopen(const char *name, int flags)
36{
37 error = "dlopen not implemented";
38 return NULL;
39}
40
41weak void *dlsym(void *library, const char *name)
42{
43 error = "dlsym not implemented";
44 return NULL;
45}
src/Compilation.zig+1
......@@ -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+28
......@@ -10,6 +10,7 @@ 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,
......@@ -17,6 +18,9 @@ pub const CrtFile = enum {
1718};
1819
1920pub 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
3539pub 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{
11731197const crt1_command_src_file = "wasi/libc-bottom-half/crt/crt1-command.c";
11741198const crt1_reactor_src_file = "wasi/libc-bottom-half/crt/crt1-reactor.c";
11751199
1200const emulated_dl_src_files = &[_][]const u8{
1201 "wasi/libc-top-half/musl/src/misc/dl.c",
1202};
1203
11761204const emulated_process_clocks_src_files = &[_][]const u8{
11771205 "wasi/libc-bottom-half/clocks/clock.c",
11781206 "wasi/libc-bottom-half/clocks/getrusage.c",