Skip to content

Commit d5db027

Browse files
authored
Merge pull request #22530 from alexrp/omit-unwind-tables
Fix building the standard library without CFI directives
2 parents 5cfcb01 + 45bb4f9 commit d5db027

18 files changed

+147
-8
lines changed

lib/std/os/linux/aarch64.zig

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const maxInt = std.math.maxInt;
34
const linux = std.os.linux;
@@ -122,7 +123,12 @@ pub fn clone() callconv(.Naked) usize {
122123
\\ ret
123124
\\
124125
\\ // child
125-
\\1: .cfi_undefined lr
126+
\\1:
127+
);
128+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
129+
\\ .cfi_undefined lr
130+
);
131+
asm volatile (
126132
\\ mov fp, 0
127133
\\ mov lr, 0
128134
\\

lib/std/os/linux/hexagon.zig

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const iovec = std.posix.iovec;
34
const iovec_const = std.posix.iovec_const;
@@ -117,8 +118,11 @@ pub fn clone() callconv(.Naked) usize {
117118
\\
118119
\\ p0 = cmp.eq(r0, #0)
119120
\\ if (!p0) dealloc_return
120-
\\
121+
);
122+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
121123
\\ .cfi_undefined r31
124+
);
125+
asm volatile (
122126
\\ r30 = #0
123127
\\ r31 = #0
124128
\\

lib/std/os/linux/loongarch64.zig

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const linux = std.os.linux;
34
const SYS = linux.SYS;
@@ -121,7 +122,11 @@ pub fn clone() callconv(.Naked) usize {
121122
\\ beqz $a0, 1f # whether child process
122123
\\ jirl $zero, $ra, 0 # parent process return
123124
\\1:
125+
);
126+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
124127
\\ .cfi_undefined 1
128+
);
129+
asm volatile (
125130
\\ move $fp, $zero
126131
\\ move $ra, $zero
127132
\\

lib/std/os/linux/mips.zig

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const maxInt = std.math.maxInt;
34
const linux = std.os.linux;
@@ -231,7 +232,11 @@ pub fn clone() callconv(.Naked) usize {
231232
\\ jr $ra
232233
\\ nop
233234
\\1:
235+
);
236+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
234237
\\ .cfi_undefined $ra
238+
);
239+
asm volatile (
235240
\\ move $fp, $zero
236241
\\ move $ra, $zero
237242
\\

lib/std/os/linux/mips64.zig

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const maxInt = std.math.maxInt;
34
const linux = std.os.linux;
@@ -210,7 +211,11 @@ pub fn clone() callconv(.Naked) usize {
210211
\\ jr $ra
211212
\\ nop
212213
\\1:
214+
);
215+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
213216
\\ .cfi_undefined $ra
217+
);
218+
asm volatile (
214219
\\ move $fp, $zero
215220
\\ move $ra, $zero
216221
\\

lib/std/os/linux/powerpc.zig

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const maxInt = std.math.maxInt;
34
const linux = std.os.linux;
@@ -176,7 +177,11 @@ pub fn clone() callconv(.Naked) usize {
176177
\\
177178
\\ #else: we're the child
178179
\\ 2:
180+
);
181+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
179182
\\ .cfi_undefined lr
183+
);
184+
asm volatile (
180185
\\ li 31, 0
181186
\\ mtlr 0
182187
\\

lib/std/os/linux/powerpc64.zig

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const maxInt = std.math.maxInt;
34
const linux = std.os.linux;
@@ -161,7 +162,11 @@ pub fn clone() callconv(.Naked) usize {
161162
\\ bnelr cr7
162163
\\
163164
\\ # we're the child
165+
);
166+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
164167
\\ .cfi_undefined lr
168+
);
169+
asm volatile (
165170
\\ li 31, 0
166171
\\ mtlr 0
167172
\\

lib/std/os/linux/riscv32.zig

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const iovec = std.posix.iovec;
34
const iovec_const = std.posix.iovec_const;
@@ -120,7 +121,12 @@ pub fn clone() callconv(.Naked) usize {
120121
\\ ret
121122
\\
122123
\\ # Child
123-
\\1: .cfi_undefined ra
124+
\\1:
125+
);
126+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
127+
\\ .cfi_undefined ra
128+
);
129+
asm volatile (
124130
\\ mv fp, zero
125131
\\ mv ra, zero
126132
\\

lib/std/os/linux/riscv64.zig

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const iovec = std.posix.iovec;
34
const iovec_const = std.posix.iovec_const;
@@ -120,7 +121,12 @@ pub fn clone() callconv(.Naked) usize {
120121
\\ ret
121122
\\
122123
\\ # Child
123-
\\1: .cfi_undefined ra
124+
\\1:
125+
);
126+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
127+
\\ .cfi_undefined ra
128+
);
129+
asm volatile (
124130
\\ mv fp, zero
125131
\\ mv ra, zero
126132
\\

lib/std/os/linux/s390x.zig

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const iovec = std.posix.iovec;
34
const iovec_const = std.posix.iovec_const;
@@ -134,7 +135,11 @@ pub fn clone() callconv(.Naked) usize {
134135
\\bnzr %%r14
135136
\\
136137
\\# we're the child
138+
);
139+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
137140
\\.cfi_undefined %%r14
141+
);
142+
asm volatile (
138143
\\lghi %%r11, 0
139144
\\lghi %%r14, 0
140145
\\

lib/std/os/linux/sparc64.zig

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const maxInt = std.math.maxInt;
34
const pid_t = linux.pid_t;
@@ -215,7 +216,11 @@ pub fn clone() callconv(.Naked) usize {
215216
\\ restore
216217
\\2:
217218
\\ # Child process
219+
);
220+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
218221
\\ .cfi_undefined %%i7
222+
);
223+
asm volatile (
219224
\\ mov %%g0, %%fp
220225
\\ mov %%g0, %%i7
221226
\\

lib/std/os/linux/x86.zig

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const maxInt = std.math.maxInt;
34
const linux = std.os.linux;
@@ -156,7 +157,11 @@ pub fn clone() callconv(.Naked) usize {
156157
\\ retl
157158
\\
158159
\\1:
160+
);
161+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
159162
\\ .cfi_undefined %%eip
163+
);
164+
asm volatile (
160165
\\ xorl %%ebp,%%ebp
161166
\\
162167
\\ popl %%eax

lib/std/os/linux/x86_64.zig

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const builtin = @import("builtin");
12
const std = @import("../../std.zig");
23
const maxInt = std.math.maxInt;
34
const linux = std.os.linux;
@@ -117,7 +118,12 @@ pub fn clone() callconv(.Naked) usize {
117118
\\ jz 1f
118119
\\ retq
119120
\\
120-
\\1: .cfi_undefined %%rip
121+
\\1:
122+
);
123+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
124+
\\ .cfi_undefined %%rip
125+
);
126+
asm volatile (
121127
\\ xorl %%ebp,%%ebp
122128
\\
123129
\\ popq %%rdi

lib/std/start.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ fn _start() callconv(.naked) noreturn {
231231
}
232232

233233
// This is the first userspace frame. Prevent DWARF-based unwinders from unwinding further. We
234-
// prevent FP-based unwinders from unwinding further by zeroing the register further below.
235-
asm volatile (switch (native_arch) {
234+
// prevent FP-based unwinders from unwinding further by zeroing the register below.
235+
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (switch (native_arch) {
236236
.arc => ".cfi_undefined blink",
237237
.arm, .armeb, .thumb, .thumbeb => "", // https://github.com/llvm/llvm-project/issues/115891
238238
.aarch64, .aarch64_be => ".cfi_undefined lr",

src/Package/Module.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
387387
.zig_backend = zig_backend,
388388
.output_mode = options.global.output_mode,
389389
.link_mode = options.global.link_mode,
390-
.unwind_tables = options.global.any_unwind_tables,
390+
.unwind_tables = unwind_tables,
391391
.is_test = options.global.is_test,
392392
.single_threaded = single_threaded,
393393
.link_libc = options.global.link_libc,

test/standalone/build.zig.zon

+3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@
183183
.empty_global_error_set = .{
184184
.path = "empty_global_error_set",
185185
},
186+
.omit_cfi = .{
187+
.path = "omit_cfi",
188+
},
186189
},
187190
.paths = .{
188191
"build.zig",

test/standalone/omit_cfi/build.zig

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
inline for (.{
5+
.aarch64,
6+
.aarch64_be,
7+
.hexagon,
8+
.loongarch64,
9+
.mips,
10+
.mipsel,
11+
.mips64,
12+
.mips64el,
13+
.powerpc,
14+
.powerpcle,
15+
.powerpc64,
16+
.powerpc64le,
17+
.riscv32,
18+
.riscv64,
19+
.s390x,
20+
.sparc64,
21+
.x86,
22+
.x86_64,
23+
}) |arch| {
24+
const target = b.resolveTargetQuery(.{
25+
.cpu_arch = arch,
26+
.os_tag = .linux,
27+
});
28+
29+
const omit_dbg = b.addExecutable(.{
30+
.name = b.fmt("{s}-linux-omit-dbg", .{@tagName(arch)}),
31+
.root_module = b.createModule(.{
32+
.root_source_file = b.path("main.zig"),
33+
.target = target,
34+
.optimize = .Debug,
35+
// We are mainly concerned with CFI directives in our non-libc startup code and syscall
36+
// code, so make it explicit that we don't want libc.
37+
.link_libc = false,
38+
.strip = true,
39+
}),
40+
});
41+
42+
const omit_uwt = b.addExecutable(.{
43+
.name = b.fmt("{s}-linux-omit-uwt", .{@tagName(arch)}),
44+
.root_module = b.createModule(.{
45+
.root_source_file = b.path("main.zig"),
46+
.target = target,
47+
.optimize = .Debug,
48+
.link_libc = false,
49+
.unwind_tables = .none,
50+
}),
51+
});
52+
53+
const omit_both = b.addExecutable(.{
54+
.name = b.fmt("{s}-linux-omit-both", .{@tagName(arch)}),
55+
.root_module = b.createModule(.{
56+
.root_source_file = b.path("main.zig"),
57+
.target = target,
58+
.optimize = .Debug,
59+
.link_libc = false,
60+
.strip = true,
61+
.unwind_tables = .none,
62+
}),
63+
});
64+
65+
inline for (.{ omit_dbg, omit_uwt, omit_both }) |step| b.installArtifact(step);
66+
}
67+
}

test/standalone/omit_cfi/main.zig

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn main() void {}

0 commit comments

Comments
 (0)