| ... | @@ -7,11 +7,13 @@ const assert = std.debug.assert; | ... | @@ -7,11 +7,13 @@ const assert = std.debug.assert; |
| 7 | const testing = std.testing; | 7 | const testing = std.testing; |
| 8 | const elf = std.elf; | 8 | const elf = std.elf; |
| 9 | const windows = std.os.windows; | 9 | const windows = std.os.windows; |
| | 10 | const system = std.os.system; |
| 10 | const maxInt = std.math.maxInt; | 11 | const maxInt = std.math.maxInt; |
| 11 | | 12 | |
| 12 | pub const DynLib = switch (builtin.os) { | 13 | pub const DynLib = switch (builtin.os) { |
| 13 | .linux => LinuxDynLib, | 14 | .linux => if (builtin.link_libc) DlDynlib else LinuxDynLib, |
| 14 | .windows => WindowsDynLib, | 15 | .windows => WindowsDynLib, |
| | 16 | .macosx, .tvos, .watchos, .ios => DlDynlib, |
| 15 | else => void, | 17 | else => void, |
| 16 | }; | 18 | }; |
| 17 | | 19 | |
| ... | @@ -99,12 +101,14 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator { | ... | @@ -99,12 +101,14 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator { |
| 99 | } | 101 | } |
| 100 | | 102 | |
| 101 | pub const LinuxDynLib = struct { | 103 | pub const LinuxDynLib = struct { |
| | 104 | pub const Error = ElfLib.Error; |
| | 105 | |
| 102 | elf_lib: ElfLib, | 106 | elf_lib: ElfLib, |
| 103 | fd: i32, | 107 | fd: i32, |
| 104 | memory: []align(mem.page_size) u8, | 108 | memory: []align(mem.page_size) u8, |
| 105 | | 109 | |
| 106 | /// Trusts the file | 110 | /// Trusts the file |
| 107 | pub fn open(path: []const u8) !DynLib { | 111 | pub fn open(path: []const u8) !LinuxDynLib { |
| 108 | const fd = try os.open(path, 0, os.O_RDONLY | os.O_CLOEXEC); | 112 | const fd = try os.open(path, 0, os.O_RDONLY | os.O_CLOEXEC); |
| 109 | errdefer os.close(fd); | 113 | errdefer os.close(fd); |
| 110 | | 114 | |
| ... | @@ -121,26 +125,42 @@ pub const LinuxDynLib = struct { | ... | @@ -121,26 +125,42 @@ pub const LinuxDynLib = struct { |
| 121 | ); | 125 | ); |
| 122 | errdefer os.munmap(bytes); | 126 | errdefer os.munmap(bytes); |
| 123 | | 127 | |
| 124 | return DynLib{ | 128 | return LinuxDynLib{ |
| 125 | .elf_lib = try ElfLib.init(bytes), | 129 | .elf_lib = try ElfLib.init(bytes), |
| 126 | .fd = fd, | 130 | .fd = fd, |
| 127 | .memory = bytes, | 131 | .memory = bytes, |
| 128 | }; | 132 | }; |
| 129 | } | 133 | } |
| 130 | | 134 | |
| 131 | pub fn close(self: *DynLib) void { | 135 | pub fn close(self: *LinuxDynLib) void { |
| 132 | os.munmap(self.memory); | 136 | os.munmap(self.memory); |
| 133 | os.close(self.fd); | 137 | os.close(self.fd); |
| 134 | self.* = undefined; | 138 | self.* = undefined; |
| 135 | } | 139 | } |
| 136 | | 140 | |
| 137 | pub fn lookup(self: *DynLib, name: []const u8) ?usize { | 141 | pub fn lookup(self: *LinuxDynLib, comptime T: type, name: []const u8) ?T { |
| 138 | return self.elf_lib.lookup("", name); | 142 | if (self.elf_lib.lookup("", name)) |symbol| { |
| | 143 | return @ptrCast(T, symbol); |
| | 144 | } else { |
| | 145 | return null; |
| | 146 | } |
| 139 | } | 147 | } |
| 140 | }; | 148 | }; |
| 141 | | 149 | |
| 142 | pub const ElfLib = struct { | 150 | pub const ElfLib = struct { |
| 143 | strings: [*:0]u8, | 151 | strings: [*:0]u8, |
| | 152 | |
| | 153 | pub const Error = error{ |
| | 154 | NotElfFile, |
| | 155 | NotDynamicLibrary, |
| | 156 | MissingDynamicLinkingInformation, |
| | 157 | BaseNotFound, |
| | 158 | ElfStringSectionNotFound, |
| | 159 | ElfSymSectionNotFound, |
| | 160 | ElfHashTableNotFound, |
| | 161 | }; |
| | 162 | |
| | 163 | strings: [*]u8, |
| 144 | syms: [*]elf.Sym, | 164 | syms: [*]elf.Sym, |
| 145 | hashtab: [*]os.Elf_Symndx, | 165 | hashtab: [*]os.Elf_Symndx, |
| 146 | versym: ?[*]u16, | 166 | versym: ?[*]u16, |
| ... | @@ -245,11 +265,12 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [ | ... | @@ -245,11 +265,12 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [ |
| 245 | } | 265 | } |
| 246 | | 266 | |
| 247 | pub const WindowsDynLib = struct { | 267 | pub const WindowsDynLib = struct { |
| | 268 | pub const Error = error{FileNotFound}; |
| | 269 | |
| 248 | dll: windows.HMODULE, | 270 | dll: windows.HMODULE, |
| 249 | | 271 | |
| 250 | pub fn open(path: []const u8) !WindowsDynLib { | 272 | pub fn open(path: []const u8) !WindowsDynLib { |
| 251 | const wpath = try windows.sliceToPrefixedFileW(path); | 273 | const wpath = try windows.sliceToPrefixedFileW(path); |
| 252 | | | |
| 253 | return WindowsDynLib{ | 274 | return WindowsDynLib{ |
| 254 | .dll = try windows.LoadLibraryW(&wpath), | 275 | .dll = try windows.LoadLibraryW(&wpath), |
| 255 | }; | 276 | }; |
| ... | @@ -260,8 +281,57 @@ pub const WindowsDynLib = struct { | ... | @@ -260,8 +281,57 @@ pub const WindowsDynLib = struct { |
| 260 | self.* = undefined; | 281 | self.* = undefined; |
| 261 | } | 282 | } |
| 262 | | 283 | |
| 263 | pub fn lookup(self: *WindowsDynLib, name: []const u8) ?usize { | 284 | pub fn lookupC(self: *WindowsDynLib, comptime T: type, name: [*:0]const u8) ?T { |
| 264 | return @ptrToInt(windows.kernel32.GetProcAddress(self.dll, name.ptr)); | 285 | if (windows.kernel32.GetProcAddress(self.dll, name)) |addr| { |
| | 286 | return @ptrCast(T, addr); |
| | 287 | } else { |
| | 288 | return null; |
| | 289 | } |
| | 290 | } |
| | 291 | |
| | 292 | pub fn lookup(self: *DlDynlib, comptime T: type, comptime max_name_len: usize, name: []const u8) ?T { |
| | 293 | const c_name: [max_name_len]u8 = undefined; |
| | 294 | mem.copy(&c_name, name); |
| | 295 | c_name[name.len] = 0; |
| | 296 | return self.lookupC(T, &c_name); |
| | 297 | } |
| | 298 | }; |
| | 299 | |
| | 300 | pub const DlDynlib = struct { |
| | 301 | pub const Error = error{FileNotFound}; |
| | 302 | |
| | 303 | handle: *c_void, |
| | 304 | |
| | 305 | pub fn open(path: []const u8) !DlDynlib { |
| | 306 | if (!builtin.link_libc and !os.darwin.is_the_target) { |
| | 307 | @compileError("DlDynlib requires libc"); |
| | 308 | } |
| | 309 | |
| | 310 | return DlDynlib{ |
| | 311 | .handle = system.dlopen(path.ptr, system.RTLD_LAZY) orelse { |
| | 312 | return error.FileNotFound; |
| | 313 | }, |
| | 314 | }; |
| | 315 | } |
| | 316 | |
| | 317 | pub fn close(self: *DlDynlib) void { |
| | 318 | _ = system.dlclose(self.handle); |
| | 319 | self.* = undefined; |
| | 320 | } |
| | 321 | |
| | 322 | pub fn lookupC(self: *DlDynlib, comptime T: type, name: [*:0]const u8) ?T { |
| | 323 | if (system.dlsym(self.handle, name)) |symbol| { |
| | 324 | return @ptrCast(T, symbol); |
| | 325 | } else { |
| | 326 | return null; |
| | 327 | } |
| | 328 | } |
| | 329 | |
| | 330 | pub fn lookup(self: *DlDynlib, comptime T: type, comptime max_name_len: usize, name: []const u8) ?T { |
| | 331 | const c_name: [max_name_len]u8 = undefined; |
| | 332 | mem.copy(&c_name, name); |
| | 333 | c_name[name.len] = 0; |
| | 334 | return self.lookupC(T, &c_name); |
| 265 | } | 335 | } |
| 266 | }; | 336 | }; |
| 267 | | 337 | |
| ... | @@ -269,6 +339,7 @@ test "dynamic_library" { | ... | @@ -269,6 +339,7 @@ test "dynamic_library" { |
| 269 | const libname = switch (builtin.os) { | 339 | const libname = switch (builtin.os) { |
| 270 | .linux => "invalid_so.so", | 340 | .linux => "invalid_so.so", |
| 271 | .windows => "invalid_dll.dll", | 341 | .windows => "invalid_dll.dll", |
| | 342 | .macosx, .tvos, .watchos, .ios => "invalid_dylib.dylib", |
| 272 | else => return, | 343 | else => return, |
| 273 | }; | 344 | }; |
| 274 | | 345 | |
| ... | @@ -276,5 +347,4 @@ test "dynamic_library" { | ... | @@ -276,5 +347,4 @@ test "dynamic_library" { |
| 276 | testing.expect(err == error.FileNotFound); | 347 | testing.expect(err == error.FileNotFound); |
| 277 | return; | 348 | return; |
| 278 | }; | 349 | }; |
| 279 | @panic("Expected error from function"); | | |
| 280 | } | 350 | } |