authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-06 23:21:06-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-10 13:12:10-07:00
log53f74d6a04377c9e0d7c3affd32fceea0fd51454
tree1aaeb813b4f799b72449179c14367c5ff6d3644c
parent2a81a0f388fcf1b3ddbb55dce03fa93f4eecd804

move libssp into libcompiler_rt

closes #7265

7 files changed, 172 insertions(+), 260 deletions(-)

lib/compiler_rt.zig+1
...@@ -233,5 +233,6 @@ comptime {...@@ -233,5 +233,6 @@ comptime {
233 _ = @import("compiler_rt/memmove.zig");233 _ = @import("compiler_rt/memmove.zig");
234 _ = @import("compiler_rt/memcmp.zig");234 _ = @import("compiler_rt/memcmp.zig");
235 _ = @import("compiler_rt/bcmp.zig");235 _ = @import("compiler_rt/bcmp.zig");
236 _ = @import("compiler_rt/ssp.zig");
236 }237 }
237}238}
lib/compiler_rt/ssp.zig created+136
...@@ -0,0 +1,136 @@
1//!
2//! Small Zig reimplementation of gcc's libssp.
3//!
4//! This library implements most of the builtins required by the stack smashing
5//! protection as implemented by gcc&clang.
6//! Missing exports:
7//! - __gets_chk
8//! - __mempcpy_chk
9//! - __snprintf_chk
10//! - __sprintf_chk
11//! - __stpcpy_chk
12//! - __vsnprintf_chk
13//! - __vsprintf_chk
14
15const std = @import("std");
16const common = @import("./common.zig");
17const builtin = @import("builtin");
18
19extern fn strncpy(dest: [*:0]u8, src: [*:0]const u8, n: usize) callconv(.C) [*:0]u8;
20extern fn memset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
21extern fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8;
22extern fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8;
23
24comptime {
25 @export(__stack_chk_fail, .{ .name = "__stack_chk_fail", .linkage = common.linkage, .visibility = common.visibility });
26 @export(__chk_fail, .{ .name = "__chk_fail", .linkage = common.linkage, .visibility = common.visibility });
27 @export(__stack_chk_guard, .{ .name = "__stack_chk_guard", .linkage = common.linkage, .visibility = common.visibility });
28 @export(__strcpy_chk, .{ .name = "__strcpy_chk", .linkage = common.linkage, .visibility = common.visibility });
29 @export(__strncpy_chk, .{ .name = "__strncpy_chk", .linkage = common.linkage, .visibility = common.visibility });
30 @export(__strcat_chk, .{ .name = "__strcat_chk", .linkage = common.linkage, .visibility = common.visibility });
31 @export(__strncat_chk, .{ .name = "__strncat_chk", .linkage = common.linkage, .visibility = common.visibility });
32 @export(__memcpy_chk, .{ .name = "__memcpy_chk", .linkage = common.linkage, .visibility = common.visibility });
33 @export(__memmove_chk, .{ .name = "__memmove_chk", .linkage = common.linkage, .visibility = common.visibility });
34 @export(__memset_chk, .{ .name = "__memset_chk", .linkage = common.linkage, .visibility = common.visibility });
35}
36
37fn __stack_chk_fail() callconv(.C) noreturn {
38 @panic("stack smashing detected");
39}
40
41fn __chk_fail() callconv(.C) noreturn {
42 @panic("buffer overflow detected");
43}
44
45// TODO: Initialize the canary with random data
46var __stack_chk_guard: usize = blk: {
47 var buf = [1]u8{0} ** @sizeOf(usize);
48 buf[@sizeOf(usize) - 1] = 255;
49 buf[@sizeOf(usize) - 2] = '\n';
50 break :blk @as(usize, @bitCast(buf));
51};
52
53fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [*:0]u8 {
54 @setRuntimeSafety(false);
55
56 var i: usize = 0;
57 while (i < dest_n and src[i] != 0) : (i += 1) {
58 dest[i] = src[i];
59 }
60
61 if (i == dest_n) __chk_fail();
62
63 dest[i] = 0;
64
65 return dest;
66}
67
68fn __strncpy_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
69 if (dest_n < n) __chk_fail();
70 return strncpy(dest, src, n);
71}
72
73fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [*:0]u8 {
74 @setRuntimeSafety(false);
75
76 var avail = dest_n;
77
78 var dest_end: usize = 0;
79 while (avail > 0 and dest[dest_end] != 0) : (dest_end += 1) {
80 avail -= 1;
81 }
82
83 if (avail < 1) __chk_fail();
84
85 var i: usize = 0;
86 while (avail > 0 and src[i] != 0) : (i += 1) {
87 dest[dest_end + i] = src[i];
88 avail -= 1;
89 }
90
91 if (avail < 1) __chk_fail();
92
93 dest[dest_end + i] = 0;
94
95 return dest;
96}
97
98fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
99 @setRuntimeSafety(false);
100
101 var avail = dest_n;
102
103 var dest_end: usize = 0;
104 while (avail > 0 and dest[dest_end] != 0) : (dest_end += 1) {
105 avail -= 1;
106 }
107
108 if (avail < 1) __chk_fail();
109
110 var i: usize = 0;
111 while (avail > 0 and i < n and src[i] != 0) : (i += 1) {
112 dest[dest_end + i] = src[i];
113 avail -= 1;
114 }
115
116 if (avail < 1) __chk_fail();
117
118 dest[dest_end + i] = 0;
119
120 return dest;
121}
122
123fn __memcpy_chk(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
124 if (dest_n < n) __chk_fail();
125 return memcpy(dest, src, n);
126}
127
128fn __memmove_chk(dest: ?[*]u8, src: ?[*]const u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
129 if (dest_n < n) __chk_fail();
130 return memmove(dest, src, n);
131}
132
133fn __memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
134 if (dest_n < n) __chk_fail();
135 return memset(dest, c, n);
136}
lib/ssp.zig deleted-135
...@@ -1,135 +0,0 @@
1//!
2//! Small Zig reimplementation of gcc's libssp.
3//!
4//! This library implements most of the builtins required by the stack smashing
5//! protection as implemented by gcc&clang.
6//! Missing exports:
7//! - __gets_chk
8//! - __mempcpy_chk
9//! - __snprintf_chk
10//! - __sprintf_chk
11//! - __stpcpy_chk
12//! - __vsnprintf_chk
13//! - __vsprintf_chk
14
15const std = @import("std");
16
17extern fn strncpy(dest: [*:0]u8, src: [*:0]const u8, n: usize) callconv(.C) [*:0]u8;
18extern fn memset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
19extern fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8;
20extern fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8;
21
22// Avoid dragging in the runtime safety mechanisms into this .o file.
23pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24 _ = msg;
25 _ = error_return_trace;
26 @setCold(true);
27 std.os.abort();
28}
29
30export fn __stack_chk_fail() callconv(.C) noreturn {
31 @panic("stack smashing detected");
32}
33
34export fn __chk_fail() callconv(.C) noreturn {
35 @panic("buffer overflow detected");
36}
37
38// Emitted when targeting some architectures (eg. x86)
39// XXX: This symbol should be hidden
40export fn __stack_chk_fail_local() callconv(.C) noreturn {
41 __stack_chk_fail();
42}
43
44// XXX: Initialize the canary with random data
45export var __stack_chk_guard: usize = blk: {
46 var buf = [1]u8{0} ** @sizeOf(usize);
47 buf[@sizeOf(usize) - 1] = 255;
48 buf[@sizeOf(usize) - 2] = '\n';
49 break :blk @as(usize, @bitCast(buf));
50};
51
52export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [*:0]u8 {
53 @setRuntimeSafety(false);
54
55 var i: usize = 0;
56 while (i < dest_n and src[i] != 0) : (i += 1) {
57 dest[i] = src[i];
58 }
59
60 if (i == dest_n) __chk_fail();
61
62 dest[i] = 0;
63
64 return dest;
65}
66
67export fn __strncpy_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68 if (dest_n < n) __chk_fail();
69 return strncpy(dest, src, n);
70}
71
72export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [*:0]u8 {
73 @setRuntimeSafety(false);
74
75 var avail = dest_n;
76
77 var dest_end: usize = 0;
78 while (avail > 0 and dest[dest_end] != 0) : (dest_end += 1) {
79 avail -= 1;
80 }
81
82 if (avail < 1) __chk_fail();
83
84 var i: usize = 0;
85 while (avail > 0 and src[i] != 0) : (i += 1) {
86 dest[dest_end + i] = src[i];
87 avail -= 1;
88 }
89
90 if (avail < 1) __chk_fail();
91
92 dest[dest_end + i] = 0;
93
94 return dest;
95}
96
97export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98 @setRuntimeSafety(false);
99
100 var avail = dest_n;
101
102 var dest_end: usize = 0;
103 while (avail > 0 and dest[dest_end] != 0) : (dest_end += 1) {
104 avail -= 1;
105 }
106
107 if (avail < 1) __chk_fail();
108
109 var i: usize = 0;
110 while (avail > 0 and i < n and src[i] != 0) : (i += 1) {
111 dest[dest_end + i] = src[i];
112 avail -= 1;
113 }
114
115 if (avail < 1) __chk_fail();
116
117 dest[dest_end + i] = 0;
118
119 return dest;
120}
121
122export fn __memcpy_chk(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123 if (dest_n < n) __chk_fail();
124 return memcpy(dest, src, n);
125}
126
127export fn __memmove_chk(dest: ?[*]u8, src: ?[*]const u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128 if (dest_n < n) __chk_fail();
129 return memmove(dest, src, n);
130}
131
132export fn __memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133 if (dest_n < n) __chk_fail();
134 return memset(dest, c, n);
135}
src/Compilation.zig+22-99
...@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,...@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152/// Populated when we build the TSAN static library. A Job to build this is placed in the queue152/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153/// and resolved before calling linker.flush().153/// and resolved before calling linker.flush().
154tsan_static_lib: ?CRTFile = null,154tsan_static_lib: ?CRTFile = null,
155/// Populated when we build the libssp static library. A Job to build this is placed in the queue
156/// and resolved before calling linker.flush().
157libssp_static_lib: ?CRTFile = null,
158/// Populated when we build the libc static library. A Job to build this is placed in the queue155/// Populated when we build the libc static library. A Job to build this is placed in the queue
159/// and resolved before calling linker.flush().156/// and resolved before calling linker.flush().
160libc_static_lib: ?CRTFile = null,157libc_static_lib: ?CRTFile = null,
...@@ -286,7 +283,6 @@ const Job = union(enum) {...@@ -286,7 +283,6 @@ const Job = union(enum) {
286 libcxx: void,283 libcxx: void,
287 libcxxabi: void,284 libcxxabi: void,
288 libtsan: void,285 libtsan: void,
289 libssp: void,
290 /// needed when not linking libc and using LLVM for code generation because it generates286 /// needed when not linking libc and using LLVM for code generation because it generates
291 /// calls to, for example, memcpy and memset.287 /// calls to, for example, memcpy and memset.
292 zig_libc: void,288 zig_libc: void,
...@@ -683,7 +679,6 @@ pub const MiscTask = enum {...@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683 libtsan,679 libtsan,
684 wasi_libc_crt_file,680 wasi_libc_crt_file,
685 compiler_rt,681 compiler_rt,
686 libssp,
687 zig_libc,682 zig_libc,
688 analyze_mod,683 analyze_mod,
689684
...@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1353 if (stack_check and !target_util.supportsStackProbing(options.target))1348 if (stack_check and !target_util.supportsStackProbing(options.target))
1354 return error.StackCheckUnsupportedByTarget;1349 return error.StackCheckUnsupportedByTarget;
13551350
1356 const capable_of_building_ssp = canBuildLibSsp(options.target, use_llvm);1351 const stack_protector: u32 = sp: {
13571352 const zig_backend = zigBackend(options.target, use_llvm);
1358 const stack_protector: u32 = options.want_stack_protector orelse b: {1353 if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1359 if (!target_util.supportsStackProtector(options.target)) break :b @as(u32, 0);1354 if (options.want_stack_protector) |x| {
1355 if (x > 0) return error.StackProtectorUnsupportedByTarget;
1356 }
1357 break :sp 0;
1358 }
13601359
1361 // This logic is checking for linking libc because otherwise our start code1360 // This logic is checking for linking libc because otherwise our start code
1362 // which is trying to set up TLS (i.e. the fs/gs registers) but the stack1361 // which is trying to set up TLS (i.e. the fs/gs registers) but the stack
...@@ -1365,19 +1364,17 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1365,19 +1364,17 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1365 // as being exempt from stack protection checks, we could change this logic1364 // as being exempt from stack protection checks, we could change this logic
1366 // to supporting stack protection even when not linking libc.1365 // to supporting stack protection even when not linking libc.
1367 // TODO file issue about this1366 // TODO file issue about this
1368 if (!link_libc) break :b 0;1367 if (!link_libc) {
1369 if (!capable_of_building_ssp) break :b 0;1368 if (options.want_stack_protector) |x| {
1370 if (is_safe_mode) break :b default_stack_protector_buffer_size;1369 if (x > 0) return error.StackProtectorUnavailableWithoutLibC;
1371 break :b 0;1370 }
1371 break :sp 0;
1372 }
1373
1374 if (options.want_stack_protector) |x| break :sp x;
1375 if (is_safe_mode) break :sp default_stack_protector_buffer_size;
1376 break :sp 0;
1372 };1377 };
1373 if (stack_protector != 0) {
1374 if (!target_util.supportsStackProtector(options.target))
1375 return error.StackProtectorUnsupportedByTarget;
1376 if (!capable_of_building_ssp)
1377 return error.StackProtectorUnsupportedByBackend;
1378 if (!link_libc)
1379 return error.StackProtectorUnavailableWithoutLibC;
1380 }
13811378
1382 const include_compiler_rt = options.want_compiler_rt orelse1379 const include_compiler_rt = options.want_compiler_rt orelse
1383 (!options.skip_linker_dependencies and is_exe_or_dyn_lib);1380 (!options.skip_linker_dependencies and is_exe_or_dyn_lib);
...@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
2195 comp.job_queued_compiler_rt_obj = true;2192 comp.job_queued_compiler_rt_obj = true;
2196 }2193 }
2197 }2194 }
2198 if (needsCSymbols(
2199 options.skip_linker_dependencies,
2200 options.output_mode,
2201 options.link_mode,
2202 options.target,
2203 comp.bin_file.options.use_llvm,
2204 )) {
2205 // Related: https://github.com/ziglang/zig/issues/7265.
2206 if (comp.bin_file.options.stack_protector != 0 and
2207 (!comp.bin_file.options.link_libc or
2208 !target_util.libcProvidesStackProtector(target)))
2209 {
2210 try comp.work_queue.writeItem(.{ .libssp = {} });
2211 }
22122195
2213 if (!comp.bin_file.options.link_libc and capable_of_building_zig_libc) {2196 if (!comp.bin_file.options.skip_linker_dependencies and is_exe_or_dyn_lib and
2214 try comp.work_queue.writeItem(.{ .zig_libc = {} });2197 !comp.bin_file.options.link_libc and capable_of_building_zig_libc)
2215 }2198 {
2199 try comp.work_queue.writeItem(.{ .zig_libc = {} });
2216 }2200 }
2217 }2201 }
22182202
...@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {...@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
2258 if (self.compiler_rt_obj) |*crt_file| {2242 if (self.compiler_rt_obj) |*crt_file| {
2259 crt_file.deinit(gpa);2243 crt_file.deinit(gpa);
2260 }2244 }
2261 if (self.libssp_static_lib) |*crt_file| {
2262 crt_file.deinit(gpa);
2263 }
2264 if (self.libc_static_lib) |*crt_file| {2245 if (self.libc_static_lib) |*crt_file| {
2265 crt_file.deinit(gpa);2246 crt_file.deinit(gpa);
2266 }2247 }
...@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v...@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
4027 );4008 );
4028 };4009 };
4029 },4010 },
4030 .libssp => {
4031 const named_frame = tracy.namedFrame("libssp");
4032 defer named_frame.end();
4033
4034 comp.buildOutputFromZig(
4035 "ssp.zig",
4036 .Lib,
4037 &comp.libssp_static_lib,
4038 .libssp,
4039 prog_node,
4040 ) catch |err| switch (err) {
4041 error.OutOfMemory => return error.OutOfMemory,
4042 error.SubCompilationFailed => return, // error reported already
4043 else => comp.lockAndSetMiscFailure(
4044 .libssp,
4045 "unable to build libssp: {s}",
4046 .{@errorName(err)},
4047 ),
4048 };
4049 },
4050 .zig_libc => {4011 .zig_libc => {
4051 const named_frame = tracy.namedFrame("zig_libc");4012 const named_frame = tracy.namedFrame("zig_libc");
4052 defer named_frame.end();4013 defer named_frame.end();
...@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {...@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
6531 };6492 };
6532}6493}
65336494
6534fn canBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535 switch (target.os.tag) {
6536 .plan9 => return false,
6537 else => {},
6538 }
6539 switch (target.cpu.arch) {
6540 .spirv32, .spirv64 => return false,
6541 else => {},
6542 }
6543 return switch (zigBackend(target, use_llvm)) {
6544 .stage2_llvm => true,
6545 else => build_options.have_llvm,
6546 };
6547}
6548
6549/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.6495/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
6550/// This one builds lib/c.zig.6496/// This one builds lib/c.zig.
6551fn canBuildZigLibC(target: std.Target, use_llvm: bool) bool {6497fn canBuildZigLibC(target: std.Target, use_llvm: bool) bool {
...@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {...@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
6585 };6531 };
6586}6532}
65876533
6588fn needsCSymbols(
6589 skip_linker_dependencies: bool,
6590 output_mode: std.builtin.OutputMode,
6591 link_mode: ?std.builtin.LinkMode,
6592 target: std.Target,
6593 use_llvm: bool,
6594) bool {
6595 if (skip_linker_dependencies)
6596 return false;
6597
6598 switch (output_mode) {
6599 .Obj => return false,
6600 .Lib => if (link_mode != .Dynamic) return false,
6601 .Exe => {},
6602 }
6603
6604 // LLVM might generate calls to libc symbols.
6605 if (zigBackend(target, use_llvm) == .stage2_llvm)
6606 return true;
6607
6608 return false;
6609}
6610
6611pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {6534pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
6612 const tracy_trace = trace(@src());6535 const tracy_trace = trace(@src());
6613 defer tracy_trace.end();6536 defer tracy_trace.end();
src/link/Coff/lld.zig-6
...@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod...@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483 try argv.append(lib.full_object_path);483 try argv.append(lib.full_object_path);
484 }484 }
485 }485 }
486 // MinGW doesn't provide libssp symbols
487 if (target.abi.isGnu()) {
488 if (comp.libssp_static_lib) |lib| {
489 try argv.append(lib.full_object_path);
490 }
491 }
492 // MSVC compiler_rt is missing some stuff, so we build it unconditionally but486 // MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493 // and rely on weak linkage to allow MSVC compiler_rt functions to override ours.487 // and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494 if (comp.compiler_rt_obj) |obj| try argv.append(obj.full_object_path);488 if (comp.compiler_rt_obj) |obj| try argv.append(obj.full_object_path);
src/link/Elf.zig-18
...@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1040 }1040 }
1041 }1041 }
10421042
1043 // stack-protector.
1044 // Related: https://github.com/ziglang/zig/issues/7265
1045 if (comp.libssp_static_lib) |ssp| {
1046 try positionals.append(.{ .path = ssp.full_object_path });
1047 }
1048
1049 for (positionals.items) |obj| {1043 for (positionals.items) |obj| {
1050 var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined };1044 var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined };
1051 self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch |err|1045 self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch |err|
...@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {...@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
1689 }1683 }
1690 }1684 }
16911685
1692 // stack-protector.
1693 // Related: https://github.com/ziglang/zig/issues/7265
1694 if (comp.libssp_static_lib) |ssp| {
1695 try argv.append(ssp.full_object_path);
1696 }
1697
1698 // Shared libraries.1686 // Shared libraries.
1699 // Worst-case, we need an --as-needed argument for every lib, as well1687 // Worst-case, we need an --as-needed argument for every lib, as well
1700 // as one before and one after.1688 // as one before and one after.
...@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v...@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
2729 }2717 }
2730 }2718 }
27312719
2732 // stack-protector.
2733 // Related: https://github.com/ziglang/zig/issues/7265
2734 if (comp.libssp_static_lib) |ssp| {
2735 try argv.append(ssp.full_object_path);
2736 }
2737
2738 // Shared libraries.2720 // Shared libraries.
2739 if (is_exe_or_dyn_lib) {2721 if (is_exe_or_dyn_lib) {
2740 const system_libs = self.base.options.system_libs.keys();2722 const system_libs = self.base.options.system_libs.keys();
src/target.zig+13-2
...@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {...@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328 (target.cpu.arch == .x86 or target.cpu.arch == .x86_64);328 (target.cpu.arch == .x86 or target.cpu.arch == .x86_64);
329}329}
330330
331pub fn supportsStackProtector(target: std.Target) bool {331pub fn supportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332 return !target.isSpirV();332 switch (target.os.tag) {
333 .plan9 => return false,
334 else => {},
335 }
336 switch (target.cpu.arch) {
337 .spirv32, .spirv64 => return false,
338 else => {},
339 }
340 return switch (backend) {
341 .stage2_llvm => true,
342 else => false,
343 };
333}344}
334345
335pub fn libcProvidesStackProtector(target: std.Target) bool {346pub fn libcProvidesStackProtector(target: std.Target) bool {