Skip to content

Commit

Permalink
Merge pull request #22530 from alexrp/omit-unwind-tables
Browse files Browse the repository at this point in the history
Fix building the standard library without CFI directives
  • Loading branch information
alexrp authored Jan 20, 2025
2 parents 5cfcb01 + 45bb4f9 commit d5db027
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 8 deletions.
8 changes: 7 additions & 1 deletion lib/std/os/linux/aarch64.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const linux = std.os.linux;
Expand Down Expand Up @@ -122,7 +123,12 @@ pub fn clone() callconv(.Naked) usize {
\\ ret
\\
\\ // child
\\1: .cfi_undefined lr
\\1:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined lr
);
asm volatile (
\\ mov fp, 0
\\ mov lr, 0
\\
Expand Down
6 changes: 5 additions & 1 deletion lib/std/os/linux/hexagon.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const iovec = std.posix.iovec;
const iovec_const = std.posix.iovec_const;
Expand Down Expand Up @@ -117,8 +118,11 @@ pub fn clone() callconv(.Naked) usize {
\\
\\ p0 = cmp.eq(r0, #0)
\\ if (!p0) dealloc_return
\\
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined r31
);
asm volatile (
\\ r30 = #0
\\ r31 = #0
\\
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/linux/loongarch64.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const linux = std.os.linux;
const SYS = linux.SYS;
Expand Down Expand Up @@ -121,7 +122,11 @@ pub fn clone() callconv(.Naked) usize {
\\ beqz $a0, 1f # whether child process
\\ jirl $zero, $ra, 0 # parent process return
\\1:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined 1
);
asm volatile (
\\ move $fp, $zero
\\ move $ra, $zero
\\
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/linux/mips.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const linux = std.os.linux;
Expand Down Expand Up @@ -231,7 +232,11 @@ pub fn clone() callconv(.Naked) usize {
\\ jr $ra
\\ nop
\\1:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined $ra
);
asm volatile (
\\ move $fp, $zero
\\ move $ra, $zero
\\
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/linux/mips64.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const linux = std.os.linux;
Expand Down Expand Up @@ -210,7 +211,11 @@ pub fn clone() callconv(.Naked) usize {
\\ jr $ra
\\ nop
\\1:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined $ra
);
asm volatile (
\\ move $fp, $zero
\\ move $ra, $zero
\\
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/linux/powerpc.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const linux = std.os.linux;
Expand Down Expand Up @@ -176,7 +177,11 @@ pub fn clone() callconv(.Naked) usize {
\\
\\ #else: we're the child
\\ 2:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined lr
);
asm volatile (
\\ li 31, 0
\\ mtlr 0
\\
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/linux/powerpc64.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const linux = std.os.linux;
Expand Down Expand Up @@ -161,7 +162,11 @@ pub fn clone() callconv(.Naked) usize {
\\ bnelr cr7
\\
\\ # we're the child
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined lr
);
asm volatile (
\\ li 31, 0
\\ mtlr 0
\\
Expand Down
8 changes: 7 additions & 1 deletion lib/std/os/linux/riscv32.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const iovec = std.posix.iovec;
const iovec_const = std.posix.iovec_const;
Expand Down Expand Up @@ -120,7 +121,12 @@ pub fn clone() callconv(.Naked) usize {
\\ ret
\\
\\ # Child
\\1: .cfi_undefined ra
\\1:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined ra
);
asm volatile (
\\ mv fp, zero
\\ mv ra, zero
\\
Expand Down
8 changes: 7 additions & 1 deletion lib/std/os/linux/riscv64.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const iovec = std.posix.iovec;
const iovec_const = std.posix.iovec_const;
Expand Down Expand Up @@ -120,7 +121,12 @@ pub fn clone() callconv(.Naked) usize {
\\ ret
\\
\\ # Child
\\1: .cfi_undefined ra
\\1:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined ra
);
asm volatile (
\\ mv fp, zero
\\ mv ra, zero
\\
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/linux/s390x.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const iovec = std.posix.iovec;
const iovec_const = std.posix.iovec_const;
Expand Down Expand Up @@ -134,7 +135,11 @@ pub fn clone() callconv(.Naked) usize {
\\bnzr %%r14
\\
\\# we're the child
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\.cfi_undefined %%r14
);
asm volatile (
\\lghi %%r11, 0
\\lghi %%r14, 0
\\
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/linux/sparc64.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const pid_t = linux.pid_t;
Expand Down Expand Up @@ -215,7 +216,11 @@ pub fn clone() callconv(.Naked) usize {
\\ restore
\\2:
\\ # Child process
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined %%i7
);
asm volatile (
\\ mov %%g0, %%fp
\\ mov %%g0, %%i7
\\
Expand Down
5 changes: 5 additions & 0 deletions lib/std/os/linux/x86.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const linux = std.os.linux;
Expand Down Expand Up @@ -156,7 +157,11 @@ pub fn clone() callconv(.Naked) usize {
\\ retl
\\
\\1:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined %%eip
);
asm volatile (
\\ xorl %%ebp,%%ebp
\\
\\ popl %%eax
Expand Down
8 changes: 7 additions & 1 deletion lib/std/os/linux/x86_64.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const linux = std.os.linux;
Expand Down Expand Up @@ -117,7 +118,12 @@ pub fn clone() callconv(.Naked) usize {
\\ jz 1f
\\ retq
\\
\\1: .cfi_undefined %%rip
\\1:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined %%rip
);
asm volatile (
\\ xorl %%ebp,%%ebp
\\
\\ popq %%rdi
Expand Down
4 changes: 2 additions & 2 deletions lib/std/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ fn _start() callconv(.naked) noreturn {
}

// This is the first userspace frame. Prevent DWARF-based unwinders from unwinding further. We
// prevent FP-based unwinders from unwinding further by zeroing the register further below.
asm volatile (switch (native_arch) {
// prevent FP-based unwinders from unwinding further by zeroing the register below.
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (switch (native_arch) {
.arc => ".cfi_undefined blink",
.arm, .armeb, .thumb, .thumbeb => "", // https://github.com/llvm/llvm-project/issues/115891
.aarch64, .aarch64_be => ".cfi_undefined lr",
Expand Down
2 changes: 1 addition & 1 deletion src/Package/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
.zig_backend = zig_backend,
.output_mode = options.global.output_mode,
.link_mode = options.global.link_mode,
.unwind_tables = options.global.any_unwind_tables,
.unwind_tables = unwind_tables,
.is_test = options.global.is_test,
.single_threaded = single_threaded,
.link_libc = options.global.link_libc,
Expand Down
3 changes: 3 additions & 0 deletions test/standalone/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@
.empty_global_error_set = .{
.path = "empty_global_error_set",
},
.omit_cfi = .{
.path = "omit_cfi",
},
},
.paths = .{
"build.zig",
Expand Down
67 changes: 67 additions & 0 deletions test/standalone/omit_cfi/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
inline for (.{
.aarch64,
.aarch64_be,
.hexagon,
.loongarch64,
.mips,
.mipsel,
.mips64,
.mips64el,
.powerpc,
.powerpcle,
.powerpc64,
.powerpc64le,
.riscv32,
.riscv64,
.s390x,
.sparc64,
.x86,
.x86_64,
}) |arch| {
const target = b.resolveTargetQuery(.{
.cpu_arch = arch,
.os_tag = .linux,
});

const omit_dbg = b.addExecutable(.{
.name = b.fmt("{s}-linux-omit-dbg", .{@tagName(arch)}),
.root_module = b.createModule(.{
.root_source_file = b.path("main.zig"),
.target = target,
.optimize = .Debug,
// We are mainly concerned with CFI directives in our non-libc startup code and syscall
// code, so make it explicit that we don't want libc.
.link_libc = false,
.strip = true,
}),
});

const omit_uwt = b.addExecutable(.{
.name = b.fmt("{s}-linux-omit-uwt", .{@tagName(arch)}),
.root_module = b.createModule(.{
.root_source_file = b.path("main.zig"),
.target = target,
.optimize = .Debug,
.link_libc = false,
.unwind_tables = .none,
}),
});

const omit_both = b.addExecutable(.{
.name = b.fmt("{s}-linux-omit-both", .{@tagName(arch)}),
.root_module = b.createModule(.{
.root_source_file = b.path("main.zig"),
.target = target,
.optimize = .Debug,
.link_libc = false,
.strip = true,
.unwind_tables = .none,
}),
});

inline for (.{ omit_dbg, omit_uwt, omit_both }) |step| b.installArtifact(step);
}
}
1 change: 1 addition & 0 deletions test/standalone/omit_cfi/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn main() void {}

0 comments on commit d5db027

Please sign in to comment.