| ... | @@ -8,9 +8,12 @@ const windows = std.os.windows; | ... | @@ -8,9 +8,12 @@ const windows = std.os.windows; |
| 8 | const system = std.os.system; | 8 | const system = std.os.system; |
| 9 | | 9 | |
| 10 | pub const DynLib = switch (builtin.os.tag) { | 10 | pub const DynLib = switch (builtin.os.tag) { |
| 11 | .linux => if (builtin.link_libc) DlDynlib else ElfDynLib, | 11 | .linux => if (!builtin.link_libc or builtin.abi == .musl and builtin.link_mode == .Static) |
| | 12 | ElfDynLib |
| | 13 | else |
| | 14 | DlDynLib, |
| 12 | .windows => WindowsDynLib, | 15 | .windows => WindowsDynLib, |
| 13 | .macos, .tvos, .watchos, .ios, .freebsd, .netbsd, .openbsd, .dragonfly, .solaris, .illumos => DlDynlib, | 16 | .macos, .tvos, .watchos, .ios, .freebsd, .netbsd, .openbsd, .dragonfly, .solaris, .illumos => DlDynLib, |
| 14 | else => void, | 17 | else => void, |
| 15 | }; | 18 | }; |
| 16 | | 19 | |
| ... | @@ -352,30 +355,30 @@ pub const WindowsDynLib = struct { | ... | @@ -352,30 +355,30 @@ pub const WindowsDynLib = struct { |
| 352 | } | 355 | } |
| 353 | }; | 356 | }; |
| 354 | | 357 | |
| 355 | pub const DlDynlib = struct { | 358 | pub const DlDynLib = struct { |
| 356 | pub const Error = error{FileNotFound}; | 359 | pub const Error = error{FileNotFound}; |
| 357 | | 360 | |
| 358 | handle: *anyopaque, | 361 | handle: *anyopaque, |
| 359 | | 362 | |
| 360 | pub fn open(path: []const u8) !DlDynlib { | 363 | pub fn open(path: []const u8) !DlDynLib { |
| 361 | const path_c = try os.toPosixPath(path); | 364 | const path_c = try os.toPosixPath(path); |
| 362 | return openZ(&path_c); | 365 | return openZ(&path_c); |
| 363 | } | 366 | } |
| 364 | | 367 | |
| 365 | pub fn openZ(path_c: [*:0]const u8) !DlDynlib { | 368 | pub fn openZ(path_c: [*:0]const u8) !DlDynLib { |
| 366 | return DlDynlib{ | 369 | return DlDynLib{ |
| 367 | .handle = system.dlopen(path_c, system.RTLD.LAZY) orelse { | 370 | .handle = system.dlopen(path_c, system.RTLD.LAZY) orelse { |
| 368 | return error.FileNotFound; | 371 | return error.FileNotFound; |
| 369 | }, | 372 | }, |
| 370 | }; | 373 | }; |
| 371 | } | 374 | } |
| 372 | | 375 | |
| 373 | pub fn close(self: *DlDynlib) void { | 376 | pub fn close(self: *DlDynLib) void { |
| 374 | _ = system.dlclose(self.handle); | 377 | _ = system.dlclose(self.handle); |
| 375 | self.* = undefined; | 378 | self.* = undefined; |
| 376 | } | 379 | } |
| 377 | | 380 | |
| 378 | pub fn lookup(self: *DlDynlib, comptime T: type, name: [:0]const u8) ?T { | 381 | pub fn lookup(self: *DlDynLib, comptime T: type, name: [:0]const u8) ?T { |
| 379 | // dlsym (and other dl-functions) secretly take shadow parameter - return address on stack | 382 | // dlsym (and other dl-functions) secretly take shadow parameter - return address on stack |
| 380 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826 | 383 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826 |
| 381 | if (@call(.never_tail, system.dlsym, .{ self.handle, name.ptr })) |symbol| { | 384 | if (@call(.never_tail, system.dlsym, .{ self.handle, name.ptr })) |symbol| { |