From 25542c3443c616d0ef546e424619cf88553d9b59 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Tue, 15 Aug 2023 17:20:04 -0400 Subject: [PATCH 1/7] netbsd: std.c.REG: populate indexes for x86_64 --- lib/std/c/netbsd.zig | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/std/c/netbsd.zig b/lib/std/c/netbsd.zig index 4b627ed083..1986d4e9bc 100644 --- a/lib/std/c/netbsd.zig +++ b/lib/std/c/netbsd.zig @@ -1227,9 +1227,32 @@ pub const REG = switch (builtin.cpu.arch) { pub const PC = 15; }, .x86_64 => struct { + pub const RDI = 0; + pub const RSI = 1; + pub const RDX = 2; + pub const RCX = 3; + pub const R8 = 4; + pub const R9 = 5; + pub const R10 = 6; + pub const R11 = 7; + pub const R12 = 8; + pub const R13 = 9; + pub const R14 = 10; + pub const R15 = 11; pub const RBP = 12; + pub const RBX = 13; + pub const RAX = 14; + pub const GS = 15; + pub const FS = 16; + pub const ES = 17; + pub const DS = 18; + pub const TRAPNO = 19; + pub const ERR = 20; pub const RIP = 21; + pub const CS = 22; + pub const RFLAGS = 23; pub const RSP = 24; + pub const SS = 25; }, else => struct {}, }; From bac3c2748ff0a0222b5c7f4df13120f390f503f3 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Tue, 15 Aug 2023 17:20:04 -0400 Subject: [PATCH 2/7] netbsd: std.dwarf.abi: disable x86_64 regBytes() We do not yet have correct implementation to access xmm registers from the world of ucontext/mcontext. Unimplement .netbsd to allow building zig compiler. --- lib/std/dwarf/abi.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/dwarf/abi.zig b/lib/std/dwarf/abi.zig index 9a1385ba6f..b097fd8b15 100644 --- a/lib/std/dwarf/abi.zig +++ b/lib/std/dwarf/abi.zig @@ -229,7 +229,7 @@ pub fn regBytes( else => error.UnimplementedOs, }, .x86_64 => switch (builtin.os.tag) { - .linux, .netbsd, .solaris => switch (reg_number) { + .linux, .solaris => switch (reg_number) { 0 => mem.asBytes(&ucontext_ptr.mcontext.gregs[os.REG.RAX]), 1 => mem.asBytes(&ucontext_ptr.mcontext.gregs[os.REG.RDX]), 2 => mem.asBytes(&ucontext_ptr.mcontext.gregs[os.REG.RCX]), From 4532ff278096dce71f5726a8aff051f834855d54 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Tue, 15 Aug 2023 17:20:04 -0400 Subject: [PATCH 3/7] netbsd: std.c: fix pthread_rwlock_t - make .owner field optional --- lib/std/c/netbsd.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/c/netbsd.zig b/lib/std/c/netbsd.zig index 1986d4e9bc..2380314429 100644 --- a/lib/std/c/netbsd.zig +++ b/lib/std/c/netbsd.zig @@ -93,7 +93,7 @@ pub const pthread_rwlock_t = extern struct { wblocked_first: ?*u8 = null, wblocked_last: ?*u8 = null, nreaders: c_uint = 0, - owner: std.c.pthread_t = null, + owner: ?std.c.pthread_t = null, private: ?*anyopaque = null, }; From e1216686f3b481eda5e0e6dd025711e0f1f1f2d0 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Tue, 15 Aug 2023 17:20:05 -0400 Subject: [PATCH 4/7] dragonfly: std.c: more decls - add ucontext_t, mcontext_t - add pthread_rwlock_t --- lib/std/c/dragonfly.zig | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/lib/std/c/dragonfly.zig b/lib/std/c/dragonfly.zig index d4f2867fda..15a5f7ef04 100644 --- a/lib/std/c/dragonfly.zig +++ b/lib/std/c/dragonfly.zig @@ -34,6 +34,10 @@ pub const pthread_attr_t = extern struct { // copied from freebsd __align: c_long, }; +pub const pthread_rwlock_t = extern struct { + ptr: ?*anyopaque = null, +}; + pub const sem_t = ?*opaque {}; pub extern "c" fn pthread_setname_np(thread: std.c.pthread_t, name: [*:0]const u8) E; @@ -55,6 +59,56 @@ pub const gid_t = u32; pub const time_t = isize; pub const suseconds_t = c_long; +pub const ucontext_t = extern struct { + sigmask: sigset_t, + mcontext: mcontext_t, + link: ?*ucontext_t, + stack: stack_t, + cofunc: ?*fn (?*ucontext_t, ?*anyopaque) void, + arg: ?*void, + _spare: [4]c_int, +}; + +pub const mcontext_t = extern struct { + onstack: register_t, // XXX - sigcontext compat. + rdi: register_t, + rsi: register_t, + rdx: register_t, + rcx: register_t, + r8: register_t, + r9: register_t, + rax: register_t, + rbx: register_t, + rbp: register_t, + r10: register_t, + r11: register_t, + r12: register_t, + r13: register_t, + r14: register_t, + r15: register_t, + xflags: register_t, + trapno: register_t, + addr: register_t, + flags: register_t, + err: register_t, + rip: register_t, + cs: register_t, + rflags: register_t, + rsp: register_t, // machine state + ss: register_t, + + len: c_uint, // sizeof(mcontext_t) + fpformat: c_uint, + ownedfp: c_uint, + reserved: c_uint, + unused: [8]c_uint, + + // NOTE! 64-byte aligned as of here. Also must match savefpu structure. + fpregs: [256]c_int align(64), +}; + +pub const register_t = isize; + pub const E = enum(u16) { /// No error occurred. SUCCESS = 0, From 3eedaab731d9a31ed1039dddc9eb52d8d7002c55 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Tue, 15 Aug 2023 17:20:05 -0400 Subject: [PATCH 5/7] bsd: std.c.fs: skip test w/ getFdPath in calltree --- lib/std/fs/test.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index 50804bf614..2b4eded4c8 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -102,7 +102,10 @@ test "openDir cwd parent .." { } test "openDir non-cwd parent .." { - if (builtin.os.tag == .wasi) return error.SkipZigTest; + switch (builtin.os.tag) { + .wasi, .netbsd, .openbsd => return error.SkipZigTest, + else => {}, + } var tmp = tmpDir(.{}); defer tmp.cleanup(); From c685b675e547f61c81c5fb63159bc12b26405100 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Tue, 15 Aug 2023 17:20:05 -0400 Subject: [PATCH 6/7] std.debug: avoid os.getFdPath Commit ea9917d9bd921f9fc14028a8dbf9e3f0f2579120 introduced usage of fs.Dir.realpath which eventually calls os.getFdpath which is forbidden to be used by the compiler. It causes building zig to fail on OpenBsd, NetBSD and older versions of FreeBSD and DragonFly. This patch substitutes with os.realpath on libc targets and eventually calls c.realpath and allows zig to build. Any use of realpath is not desired but this is the lesser evil. --- lib/std/debug.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/debug.zig b/lib/std/debug.zig index db9b93dff6..0657d667c2 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -1228,7 +1228,7 @@ pub fn readElfDebugInfo( } var cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined; - const cwd_path = fs.cwd().realpath("", &cwd_buf) catch break :blk; + const cwd_path = os.realpath(".", &cwd_buf) catch break :blk; // // for (global_debug_directories) |global_directory| { From e288c546996d202e87168f25d689a2f21fe03b60 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Thu, 17 Aug 2023 10:50:52 -0400 Subject: [PATCH 7/7] std.debug: openbsd does not have getcontext --- lib/std/debug.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 0657d667c2..7a32271953 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -168,6 +168,7 @@ pub fn relocateContext(context: *ThreadContext) void { } pub const have_getcontext = @hasDecl(os.system, "getcontext") and + builtin.os.tag != .openbsd and (builtin.os.tag != .linux or switch (builtin.cpu.arch) { .x86, .x86_64,