Merge branch 'emekoi-dynlib-load'

closes #2598
master
Andrew Kelley 2019-12-10 13:24:01 -05:00
commit 0d99744acc
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
7 changed files with 113 additions and 21 deletions

View File

@ -226,3 +226,7 @@ pub extern "c" fn pthread_cond_destroy(cond: *pthread_cond_t) c_int;
pub const pthread_t = *@OpaqueType();
pub const FILE = @OpaqueType();
pub extern "c" fn dlopen(path: [*:0]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: [*:0]const u8) ?*c_void;

View File

@ -98,3 +98,10 @@ const __SIZEOF_PTHREAD_MUTEX_T = if (builtin.os == .fuchsia) 40 else switch (bui
},
else => unreachable,
};
pub const RTLD_LAZY = 1;
pub const RTLD_NOW = 2;
pub const RTLD_NOLOAD = 4;
pub const RTLD_NODELETE = 4096;
pub const RTLD_GLOBAL = 256;
pub const RTLD_LOCAL = 0;

View File

@ -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,25 +125,43 @@ 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 openC(path_c: [*:0]const u8) !LinuxDynLib {
return open(mem.toSlice(u8, path_c));
}
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: [:0]const u8) ?T {
if (self.elf_lib.lookup("", name)) |symbol| {
return @intToPtr(T, symbol);
} else {
return null;
}
}
};
pub const ElfLib = struct {
pub const Error = error{
NotElfFile,
NotDynamicLibrary,
MissingDynamicLinkingInformation,
BaseNotFound,
ElfStringSectionNotFound,
ElfSymSectionNotFound,
ElfHashTableNotFound,
};
strings: [*:0]u8,
syms: [*]elf.Sym,
hashtab: [*]os.Elf_Symndx,
@ -245,13 +267,24 @@ 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);
const path_w = try windows.sliceToPrefixedFileW(path);
return openW(&path_w);
}
pub fn openC(path_c: [*:0]const u8) !WindowsDynLib {
const path_w = try windows.cStrToPrefixedFileW(path);
return openW(&path_w);
}
pub fn openW(path_w: [*:0]const u16) !WindowsDynLib {
return WindowsDynLib{
.dll = try windows.LoadLibraryW(&wpath),
// + 4 to skip over the \??\
.dll = try windows.LoadLibraryW(path_w + 4),
};
}
@ -260,8 +293,46 @@ 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 lookup(self: *WindowsDynLib, comptime T: type, name: [:0]const u8) ?T {
if (windows.kernel32.GetProcAddress(self.dll, name.ptr)) |addr| {
return @ptrCast(T, addr);
} else {
return null;
}
}
};
pub const DlDynlib = struct {
pub const Error = error{FileNotFound};
handle: *c_void,
pub fn open(path: []const u8) !DlDynlib {
const path_c = try os.toPosixPath(path);
return openC(&path_c);
}
pub fn openC(path_c: [*:0]const u8) !DlDynlib {
return DlDynlib{
.handle = system.dlopen(path_c, system.RTLD_LAZY) orelse {
return error.FileNotFound;
},
};
}
pub fn close(self: *DlDynlib) void {
_ = system.dlclose(self.handle);
self.* = undefined;
}
pub fn lookup(self: *DlDynlib, comptime T: type, name: [:0]const u8) ?T {
// dlsym (and other dl-functions) secretly take shadow parameter - return address on stack
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826
if (@call(.{ .modifier = .never_tail }, system.dlsym, .{ self.handle, name.ptr })) |symbol| {
return @ptrCast(T, symbol);
} else {
return null;
}
}
};
@ -269,12 +340,12 @@ test "dynamic_library" {
const libname = switch (builtin.os) {
.linux => "invalid_so.so",
.windows => "invalid_dll.dll",
else => return,
.macosx, .tvos, .watchos, .ios => "invalid_dylib.dylib",
else => return error.SkipZigTest,
};
const dynlib = DynLib.open(libname) catch |err| {
testing.expect(err == error.FileNotFound);
return;
};
@panic("Expected error from function");
}

View File

@ -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);

View File

@ -6561,6 +6561,7 @@ static IrInstruction *ir_gen_prefix_op_id(IrBuilder *irb, Scope *scope, AstNode
}
static IrInstruction *ir_expr_wrap(IrBuilder *irb, Scope *scope, IrInstruction *inst, ResultLoc *result_loc) {
if (inst == irb->codegen->invalid_instruction) return inst;
ir_build_end_expr(irb, scope, inst->source_node, inst, result_loc);
return inst;
}

View File

@ -1,6 +1,5 @@
const std = @import("std");
const tests = @import("tests.zig");
const builtin = @import("builtin");
const is_windows = builtin.os == builtin.Os.windows;
pub fn addCases(cases: *tests.StandaloneContext) void {
cases.add("test/standalone/hello_world/hello.zig");
@ -19,13 +18,10 @@ 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
if (std.Target.current.getOs() != .wasi) {
cases.addBuildFile("test/standalone/load_dynamic_library/build.zig");
}
if (builtin.arch == builtin.Arch.x86_64) { // TODO add C ABI support for other architectures
if (std.Target.current.getArch() == .x86_64) { // TODO add C ABI support for other architectures
cases.addBuildFile("test/stage1/c_abi/build.zig");
}
}

View File

@ -9,8 +9,7 @@ pub fn main() !void {
var lib = try std.DynLib.open(dynlib_name);
defer lib.close();
const addr = lib.lookup("add") orelse return error.SymbolNotFound;
const addFn = @intToPtr(extern fn (i32, i32) i32, addr);
const addFn = lib.lookup(extern fn (i32, i32) i32, "add") orelse return error.SymbolNotFound;
const result = addFn(12, 34);
std.debug.assert(result == 46);