Commit fd6b7b160d
Changed files (6)
lib/std/c/darwin.zig
@@ -128,7 +128,3 @@ 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/c/linux.zig
@@ -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;
lib/std/c.zig
@@ -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;
lib/std/dynamic_library.zig
@@ -132,6 +132,10 @@ pub const LinuxDynLib = struct {
};
}
+ 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);
@@ -148,8 +152,6 @@ pub const LinuxDynLib = struct {
};
pub const ElfLib = struct {
- strings: [*:0]u8,
-
pub const Error = error{
NotElfFile,
NotDynamicLibrary,
@@ -160,7 +162,7 @@ pub const ElfLib = struct {
ElfHashTableNotFound,
};
- strings: [*]u8,
+ strings: [*:0]u8,
syms: [*]elf.Sym,
hashtab: [*]os.Elf_Symndx,
versym: ?[*]u16,
@@ -270,9 +272,18 @@ pub const WindowsDynLib = struct {
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),
+ .dll = try windows.LoadLibraryW(path_w),
};
}
@@ -281,20 +292,13 @@ pub const WindowsDynLib = struct {
self.* = undefined;
}
- pub fn lookupC(self: *WindowsDynLib, comptime T: type, name: [*:0]const u8) ?T {
- if (windows.kernel32.GetProcAddress(self.dll, name)) |addr| {
+ pub fn lookup(self: *DlDynlib, 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 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 {
@@ -303,12 +307,13 @@ pub const DlDynlib = struct {
handle: *c_void,
pub fn open(path: []const u8) !DlDynlib {
- if (!builtin.link_libc and !os.darwin.is_the_target) {
- @compileError("DlDynlib requires libc");
- }
+ 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.ptr, system.RTLD_LAZY) orelse {
+ .handle = system.dlopen(path_c, system.RTLD_LAZY) orelse {
return error.FileNotFound;
},
};
@@ -319,20 +324,15 @@ pub const DlDynlib = struct {
self.* = undefined;
}
- pub fn lookupC(self: *DlDynlib, comptime T: type, name: [*:0]const u8) ?T {
- if (system.dlsym(self.handle, name)) |symbol| {
+ 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;
}
}
-
- 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);
- }
};
test "dynamic_library" {
@@ -340,7 +340,7 @@ test "dynamic_library" {
.linux => "invalid_so.so",
.windows => "invalid_dll.dll",
.macosx, .tvos, .watchos, .ios => "invalid_dylib.dylib",
- else => return,
+ else => return error.SkipZigTest,
};
const dynlib = DynLib.open(libname) catch |err| {
src/ir.cpp
@@ -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;
}
test/standalone.zig
@@ -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,14 +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");
- switch (builtin.os) {
- .linux, .windows, .macosx, .tvos, .watchos, .ios => {
- cases.addBuildFile("test/standalone/load_dynamic_library/build.zig");
- },
- else => {},
+ 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");
}
}