Commit 29fd727b79

emekoi <emekankurumeh@outlook.com>
2019-05-31 03:03:56
fixed windows dynamic library loading and added loading for darwin
1 parent 9561e7c
Changed files (4)
lib/std/c/darwin.zig
@@ -128,3 +128,7 @@ pub const pthread_attr_t = extern struct {
     __sig: c_long,
     __opaque: [56]u8,
 };
+
+pub extern "c" fn dlopen(path: [*]const u8, mode: c_int) ?*c_void;
+pub extern "c" fn dlclose(handle: *c_void) c_int;
+pub extern "c" fn dlsym(handle: ?*c_void, symbol: [*]const u8) ?*c_void;
lib/std/os/bits/darwin.zig
@@ -1183,6 +1183,7 @@ pub fn S_ISSOCK(m: u32) bool {
 pub fn S_IWHT(m: u32) bool {
     return m & S_IFMT == S_IFWHT;
 }
+
 pub const HOST_NAME_MAX = 72;
 
 pub const AT_FDCWD = -2;
@@ -1209,3 +1210,16 @@ pub const addrinfo = extern struct {
     addr: ?*sockaddr,
     next: ?*addrinfo,
 };
+
+pub const RTLD_LAZY = 0x1;
+pub const RTLD_NOW = 0x2;
+pub const RTLD_LOCAL = 0x4;
+pub const RTLD_GLOBAL = 0x8;
+pub const RTLD_NOLOAD = 0x10;
+pub const RTLD_NODELETE = 0x80;
+pub const RTLD_FIRST = 0x100;
+
+pub const RTLD_NEXT = @intToPtr(*c_void, ~maxInt(usize));
+pub const RTLD_DEFAULT = @intToPtr(*c_void, ~maxInt(usize) - 1);
+pub const RTLD_SELF = @intToPtr(*c_void, ~maxInt(usize) - 2);
+pub const RTLD_MAIN_ONLY = @intToPtr(*c_void, ~maxInt(usize) - 4);
lib/std/dynamic_library.zig
@@ -7,11 +7,13 @@ const assert = std.debug.assert;
 const testing = std.testing;
 const elf = std.elf;
 const windows = std.os.windows;
+const system = std.os.system;
 const maxInt = std.math.maxInt;
 
 pub const DynLib = switch (builtin.os) {
-    .linux => LinuxDynLib,
+    .linux => if (builtin.link_libc) DlDynlib else LinuxDynLib,
     .windows => WindowsDynLib,
+    .macosx, .tvos, .watchos, .ios => DlDynlib,
     else => void,
 };
 
@@ -99,12 +101,14 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
 }
 
 pub const LinuxDynLib = struct {
+    pub const Error = ElfLib.Error;
+
     elf_lib: ElfLib,
     fd: i32,
     memory: []align(mem.page_size) u8,
 
     /// Trusts the file
-    pub fn open(path: []const u8) !DynLib {
+    pub fn open(path: []const u8) !LinuxDynLib {
         const fd = try os.open(path, 0, os.O_RDONLY | os.O_CLOEXEC);
         errdefer os.close(fd);
 
@@ -121,26 +125,42 @@ pub const LinuxDynLib = struct {
         );
         errdefer os.munmap(bytes);
 
-        return DynLib{
+        return LinuxDynLib{
             .elf_lib = try ElfLib.init(bytes),
             .fd = fd,
             .memory = bytes,
         };
     }
 
-    pub fn close(self: *DynLib) void {
+    pub fn close(self: *LinuxDynLib) void {
         os.munmap(self.memory);
         os.close(self.fd);
         self.* = undefined;
     }
 
-    pub fn lookup(self: *DynLib, name: []const u8) ?usize {
-        return self.elf_lib.lookup("", name);
+    pub fn lookup(self: *LinuxDynLib, comptime T: type, name: []const u8) ?T {
+        if (self.elf_lib.lookup("", name)) |symbol| {
+            return @ptrCast(T, symbol);
+        } else {
+            return null;
+        }
     }
 };
 
 pub const ElfLib = struct {
     strings: [*:0]u8,
+
+    pub const Error = error{
+        NotElfFile,
+        NotDynamicLibrary,
+        MissingDynamicLinkingInformation,
+        BaseNotFound,
+        ElfStringSectionNotFound,
+        ElfSymSectionNotFound,
+        ElfHashTableNotFound,
+    };
+
+    strings: [*]u8,
     syms: [*]elf.Sym,
     hashtab: [*]os.Elf_Symndx,
     versym: ?[*]u16,
@@ -245,11 +265,12 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [
 }
 
 pub const WindowsDynLib = struct {
+    pub const Error = error{FileNotFound};
+
     dll: windows.HMODULE,
 
     pub fn open(path: []const u8) !WindowsDynLib {
         const wpath = try windows.sliceToPrefixedFileW(path);
-
         return WindowsDynLib{
             .dll = try windows.LoadLibraryW(&wpath),
         };
@@ -260,8 +281,57 @@ pub const WindowsDynLib = struct {
         self.* = undefined;
     }
 
-    pub fn lookup(self: *WindowsDynLib, name: []const u8) ?usize {
-        return @ptrToInt(windows.kernel32.GetProcAddress(self.dll, name.ptr));
+    pub fn lookupC(self: *WindowsDynLib, comptime T: type, name: [*:0]const u8) ?T {
+        if (windows.kernel32.GetProcAddress(self.dll, name)) |addr| {
+            return @ptrCast(T, addr);
+        } else {
+            return null;
+        }
+    }
+
+    pub fn lookup(self: *DlDynlib, comptime T: type, comptime max_name_len: usize, name: []const u8) ?T {
+        const c_name: [max_name_len]u8 = undefined;
+        mem.copy(&c_name, name);
+        c_name[name.len] = 0;
+        return self.lookupC(T, &c_name);
+    }
+};
+
+pub const DlDynlib = struct {
+    pub const Error = error{FileNotFound};
+
+    handle: *c_void,
+
+    pub fn open(path: []const u8) !DlDynlib {
+        if (!builtin.link_libc and !os.darwin.is_the_target) {
+            @compileError("DlDynlib requires libc");
+        }
+
+        return DlDynlib{
+            .handle = system.dlopen(path.ptr, system.RTLD_LAZY) orelse {
+                return error.FileNotFound;
+            },
+        };
+    }
+
+    pub fn close(self: *DlDynlib) void {
+        _ = system.dlclose(self.handle);
+        self.* = undefined;
+    }
+
+    pub fn lookupC(self: *DlDynlib, comptime T: type, name: [*:0]const u8) ?T {
+        if (system.dlsym(self.handle, name)) |symbol| {
+            return @ptrCast(T, symbol);
+        } else {
+            return null;
+        }
+    }
+
+    pub fn lookup(self: *DlDynlib, comptime T: type, comptime max_name_len: usize, name: []const u8) ?T {
+        const c_name: [max_name_len]u8 = undefined;
+        mem.copy(&c_name, name);
+        c_name[name.len] = 0;
+        return self.lookupC(T, &c_name);
     }
 };
 
@@ -269,6 +339,7 @@ test "dynamic_library" {
     const libname = switch (builtin.os) {
         .linux => "invalid_so.so",
         .windows => "invalid_dll.dll",
+        .macosx, .tvos, .watchos, .ios => "invalid_dylib.dylib",
         else => return,
     };
 
@@ -276,5 +347,4 @@ test "dynamic_library" {
         testing.expect(err == error.FileNotFound);
         return;
     };
-    @panic("Expected error from function");
 }
test/standalone.zig
@@ -19,10 +19,11 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
     cases.addBuildFile("test/standalone/use_alias/build.zig");
     cases.addBuildFile("test/standalone/brace_expansion/build.zig");
     cases.addBuildFile("test/standalone/empty_env/build.zig");
-    if (builtin.os == builtin.Os.linux) {
-        // TODO hook up the DynLib API for windows using LoadLibraryA
-        // TODO figure out how to make this work on darwin - probably libSystem has dlopen/dlsym in it
-        cases.addBuildFile("test/standalone/load_dynamic_library/build.zig");
+    switch (builtin.os) {
+        .linux, .windows, .macosx, .tvos, .watchos, .ios => {
+            cases.addBuildFile("test/standalone/load_dynamic_library/build.zig");
+        },
+        else => {},
     }
 
     if (builtin.arch == builtin.Arch.x86_64) { // TODO add C ABI support for other architectures