Commit 53f74d6

Browse files
committed
move libssp into libcompiler_rt
closes#7265
1 parent 2a81a0f commit 53f74d6

6 files changed

Lines changed: 60 additions & 148 deletions

File tree

‎lib/compiler_rt.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,6 @@ comptime {
233233
_=@import("compiler_rt/memmove.zig");
234234
_=@import("compiler_rt/memcmp.zig");
235235
_=@import("compiler_rt/bcmp.zig");
236+
_=@import("compiler_rt/ssp.zig");
236237
}
237238
}

lib/ssp.zig renamed to lib/compiler_rt/ssp.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,44 @@
1313
//! - __vsprintf_chk
1414

1515
conststd=@import("std");
16+
constcommon=@import("./common.zig");
17+
constbuiltin=@import("builtin");
1618

1719
externfnstrncpy(dest: [*:0]u8, src: [*:0]constu8, n: usize) callconv(.C) [*:0]u8;
1820
externfnmemset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
1921
externfnmemcpy(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2022
externfnmemmove(dest: ?[*]u8, src: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2123

22-
// Avoid dragging in the runtime safety mechanisms into this .o file.
23-
pubfnpanic(msg: []constu8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24-
_=msg;
25-
_=error_return_trace;
26-
@setCold(true);
27-
std.os.abort();
24+
comptime {
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 });
2835
}
2936

30-
exportfn__stack_chk_fail() callconv(.C) noreturn {
37+
fn__stack_chk_fail() callconv(.C) noreturn {
3138
@panic("stack smashing detected");
3239
}
3340

34-
exportfn__chk_fail() callconv(.C) noreturn {
41+
fn__chk_fail() callconv(.C) noreturn {
3542
@panic("buffer overflow detected");
3643
}
3744

38-
// Emitted when targeting some architectures (eg. x86)
39-
// XXX: This symbol should be hidden
40-
exportfn__stack_chk_fail_local() callconv(.C) noreturn {
41-
__stack_chk_fail();
42-
}
43-
44-
// XXX: Initialize the canary with random data
45-
exportvar__stack_chk_guard: usize=blk: {
45+
// TODO: Initialize the canary with random data
46+
var__stack_chk_guard: usize=blk: {
4647
varbuf= [1]u8{0} **@sizeOf(usize);
4748
buf[@sizeOf(usize) -1] =255;
4849
buf[@sizeOf(usize) -2] ='\n';
4950
break :blk@as(usize, @bitCast(buf));
5051
};
5152

52-
exportfn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
53+
fn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
5354
@setRuntimeSafety(false);
5455

5556
vari: usize=0;
@@ -64,12 +65,12 @@ export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
6465
returndest;
6566
}
6667

67-
exportfn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
fn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
6869
if (dest_n<n) __chk_fail();
6970
returnstrncpy(dest, src, n);
7071
}
7172

72-
exportfn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
73+
fn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
7374
@setRuntimeSafety(false);
7475

7576
varavail=dest_n;
@@ -94,7 +95,7 @@ export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
9495
returndest;
9596
}
9697

97-
exportfn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98+
fn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
9899
@setRuntimeSafety(false);
99100

100101
varavail=dest_n;
@@ -119,17 +120,17 @@ export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usi
119120
returndest;
120121
}
121122

122-
exportfn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123+
fn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123124
if (dest_n<n) __chk_fail();
124125
returnmemcpy(dest, src, n);
125126
}
126127

127-
exportfn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128+
fn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128129
if (dest_n<n) __chk_fail();
129130
returnmemmove(dest, src, n);
130131
}
131132

132-
exportfn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133+
fn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133134
if (dest_n<n) __chk_fail();
134135
returnmemset(dest, c, n);
135136
}

‎src/Compilation.zig‎

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152152
/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153153
/// and resolved before calling linker.flush().
154154
tsan_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().
157-
libssp_static_lib: ?CRTFile=null,
158155
/// Populated when we build the libc static library. A Job to build this is placed in the queue
159156
/// and resolved before calling linker.flush().
160157
libc_static_lib: ?CRTFile=null,
@@ -286,7 +283,6 @@ const Job = union(enum) {
286283
libcxx: void,
287284
libcxxabi: void,
288285
libtsan: void,
289-
libssp: void,
290286
/// needed when not linking libc and using LLVM for code generation because it generates
291287
/// calls to, for example, memcpy and memset.
292288
zig_libc: void,
@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683679
libtsan,
684680
wasi_libc_crt_file,
685681
compiler_rt,
686-
libssp,
687682
zig_libc,
688683
analyze_mod,
689684

@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13531348
if (stack_checkand!target_util.supportsStackProbing(options.target))
13541349
returnerror.StackCheckUnsupportedByTarget;
13551350

1356-
constcapable_of_building_ssp=canBuildLibSsp(options.target, use_llvm);
1357-
1358-
conststack_protector: u32=options.want_stack_protectororelseb: {
1359-
if (!target_util.supportsStackProtector(options.target)) break :b@as(u32, 0);
1351+
conststack_protector: u32=sp: {
1352+
constzig_backend=zigBackend(options.target, use_llvm);
1353+
if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1354+
if (options.want_stack_protector) |x| {
1355+
if (x>0) returnerror.StackProtectorUnsupportedByTarget;
1356+
}
1357+
break :sp0;
1358+
}
13601359

13611360
// This logic is checking for linking libc because otherwise our start code
13621361
// 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 {
13651364
// as being exempt from stack protection checks, we could change this logic
13661365
// to supporting stack protection even when not linking libc.
13671366
// TODO file issue about this
1368-
if (!link_libc) break :b0;
1369-
if (!capable_of_building_ssp) break :b0;
1370-
if (is_safe_mode) break :bdefault_stack_protector_buffer_size;
1371-
break :b0;
1367+
if (!link_libc) {
1368+
if (options.want_stack_protector) |x| {
1369+
if (x>0) returnerror.StackProtectorUnavailableWithoutLibC;
1370+
}
1371+
break :sp0;
1372+
}
1373+
1374+
if (options.want_stack_protector) |x|break :spx;
1375+
if (is_safe_mode) break :spdefault_stack_protector_buffer_size;
1376+
break :sp0;
13721377
};
1373-
if (stack_protector!=0) {
1374-
if (!target_util.supportsStackProtector(options.target))
1375-
returnerror.StackProtectorUnsupportedByTarget;
1376-
if (!capable_of_building_ssp)
1377-
returnerror.StackProtectorUnsupportedByBackend;
1378-
if (!link_libc)
1379-
returnerror.StackProtectorUnavailableWithoutLibC;
1380-
}
13811378

13821379
constinclude_compiler_rt=options.want_compiler_rtorelse
13831380
(!options.skip_linker_dependenciesandis_exe_or_dyn_lib);
@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
21952192
comp.job_queued_compiler_rt_obj=true;
21962193
}
21972194
}
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!=0and
2207-
(!comp.bin_file.options.link_libcor
2208-
!target_util.libcProvidesStackProtector(target)))
2209-
{
2210-
trycomp.work_queue.writeItem(.{ .libssp= {} });
2211-
}
22122195

2213-
if (!comp.bin_file.options.link_libcandcapable_of_building_zig_libc) {
2214-
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
2215-
}
2196+
if (!comp.bin_file.options.skip_linker_dependenciesandis_exe_or_dyn_liband
2197+
!comp.bin_file.options.link_libcandcapable_of_building_zig_libc)
2198+
{
2199+
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
22162200
}
22172201
}
22182202

@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
22582242
if (self.compiler_rt_obj) |*crt_file| {
22592243
crt_file.deinit(gpa);
22602244
}
2261-
if (self.libssp_static_lib) |*crt_file| {
2262-
crt_file.deinit(gpa);
2263-
}
22642245
if (self.libc_static_lib) |*crt_file| {
22652246
crt_file.deinit(gpa);
22662247
}
@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
40274008
);
40284009
};
40294010
},
4030-
.libssp=> {
4031-
constnamed_frame=tracy.namedFrame("libssp");
4032-
defernamed_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=>returnerror.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-
},
40504011
.zig_libc=> {
40514012
constnamed_frame=tracy.namedFrame("zig_libc");
40524013
defernamed_frame.end();
@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
65316492
};
65326493
}
65336494

6534-
fncanBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535-
switch (target.os.tag) {
6536-
.plan9=>returnfalse,
6537-
else=> {},
6538-
}
6539-
switch (target.cpu.arch) {
6540-
.spirv32, .spirv64=>returnfalse,
6541-
else=> {},
6542-
}
6543-
returnswitch (zigBackend(target, use_llvm)) {
6544-
.stage2_llvm=>true,
6545-
else=>build_options.have_llvm,
6546-
};
6547-
}
6548-
65496495
/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
65506496
/// This one builds lib/c.zig.
65516497
fncanBuildZigLibC(target: std.Target, use_llvm: bool) bool {
@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
65856531
};
65866532
}
65876533

6588-
fnneedsCSymbols(
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-
returnfalse;
6597-
6598-
switch (output_mode) {
6599-
.Obj=>returnfalse,
6600-
.Lib=>if (link_mode!=.Dynamic) returnfalse,
6601-
.Exe=> {},
6602-
}
6603-
6604-
// LLVM might generate calls to libc symbols.
6605-
if (zigBackend(target, use_llvm) ==.stage2_llvm)
6606-
returntrue;
6607-
6608-
returnfalse;
6609-
}
6610-
66116534
pubfngenerateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
66126535
consttracy_trace=trace(@src());
66136536
defertracy_trace.end();

‎src/link/Coff/lld.zig‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483483
tryargv.append(lib.full_object_path);
484484
}
485485
}
486-
// MinGW doesn't provide libssp symbols
487-
if (target.abi.isGnu()) {
488-
if (comp.libssp_static_lib) |lib| {
489-
tryargv.append(lib.full_object_path);
490-
}
491-
}
492486
// MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493487
// and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494488
if (comp.compiler_rt_obj) |obj|tryargv.append(obj.full_object_path);

‎src/link/Elf.zig‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10401040
}
10411041
}
10421042

1043-
// stack-protector.
1044-
// Related: https://github.com/ziglang/zig/issues/7265
1045-
if (comp.libssp_static_lib) |ssp| {
1046-
trypositionals.append(.{ .path=ssp.full_object_path });
1047-
}
1048-
10491043
for (positionals.items) |obj| {
10501044
varparse_ctx: ParseErrorCtx= .{ .detected_cpu_arch=undefined };
10511045
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch|err|
@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16891683
}
16901684
}
16911685

1692-
// stack-protector.
1693-
// Related: https://github.com/ziglang/zig/issues/7265
1694-
if (comp.libssp_static_lib) |ssp| {
1695-
tryargv.append(ssp.full_object_path);
1696-
}
1697-
16981686
// Shared libraries.
16991687
// Worst-case, we need an --as-needed argument for every lib, as well
17001688
// as one before and one after.
@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
27292717
}
27302718
}
27312719

2732-
// stack-protector.
2733-
// Related: https://github.com/ziglang/zig/issues/7265
2734-
if (comp.libssp_static_lib) |ssp| {
2735-
tryargv.append(ssp.full_object_path);
2736-
}
2737-
27382720
// Shared libraries.
27392721
if (is_exe_or_dyn_lib) {
27402722
constsystem_libs=self.base.options.system_libs.keys();

‎src/target.zig‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
(target.cpu.arch==.x86ortarget.cpu.arch==.x86_64);
329329
}
330330

331-
pubfnsupportsStackProtector(target: std.Target) bool {
332-
return!target.isSpirV();
331+
pubfnsupportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332+
switch (target.os.tag) {
333+
.plan9=>returnfalse,
334+
else=> {},
335+
}
336+
switch (target.cpu.arch) {
337+
.spirv32, .spirv64=>returnfalse,
338+
else=> {},
339+
}
340+
returnswitch (backend) {
341+
.stage2_llvm=>true,
342+
else=>false,
343+
};
333344
}
334345

335346
pubfnlibcProvidesStackProtector(target: std.Target) bool {

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 53f74d6

Browse files
committed
move libssp into libcompiler_rt
closes#7265
1 parent 2a81a0f commit 53f74d6

6 files changed

Lines changed: 60 additions & 148 deletions

File tree

‎lib/compiler_rt.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,6 @@ comptime {
233233
_=@import("compiler_rt/memmove.zig");
234234
_=@import("compiler_rt/memcmp.zig");
235235
_=@import("compiler_rt/bcmp.zig");
236+
_=@import("compiler_rt/ssp.zig");
236237
}
237238
}

lib/ssp.zig renamed to lib/compiler_rt/ssp.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,44 @@
1313
//! - __vsprintf_chk
1414

1515
conststd=@import("std");
16+
constcommon=@import("./common.zig");
17+
constbuiltin=@import("builtin");
1618

1719
externfnstrncpy(dest: [*:0]u8, src: [*:0]constu8, n: usize) callconv(.C) [*:0]u8;
1820
externfnmemset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
1921
externfnmemcpy(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2022
externfnmemmove(dest: ?[*]u8, src: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2123

22-
// Avoid dragging in the runtime safety mechanisms into this .o file.
23-
pubfnpanic(msg: []constu8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24-
_=msg;
25-
_=error_return_trace;
26-
@setCold(true);
27-
std.os.abort();
24+
comptime {
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 });
2835
}
2936

30-
exportfn__stack_chk_fail() callconv(.C) noreturn {
37+
fn__stack_chk_fail() callconv(.C) noreturn {
3138
@panic("stack smashing detected");
3239
}
3340

34-
exportfn__chk_fail() callconv(.C) noreturn {
41+
fn__chk_fail() callconv(.C) noreturn {
3542
@panic("buffer overflow detected");
3643
}
3744

38-
// Emitted when targeting some architectures (eg. x86)
39-
// XXX: This symbol should be hidden
40-
exportfn__stack_chk_fail_local() callconv(.C) noreturn {
41-
__stack_chk_fail();
42-
}
43-
44-
// XXX: Initialize the canary with random data
45-
exportvar__stack_chk_guard: usize=blk: {
45+
// TODO: Initialize the canary with random data
46+
var__stack_chk_guard: usize=blk: {
4647
varbuf= [1]u8{0} **@sizeOf(usize);
4748
buf[@sizeOf(usize) -1] =255;
4849
buf[@sizeOf(usize) -2] ='\n';
4950
break :blk@as(usize, @bitCast(buf));
5051
};
5152

52-
exportfn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
53+
fn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
5354
@setRuntimeSafety(false);
5455

5556
vari: usize=0;
@@ -64,12 +65,12 @@ export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
6465
returndest;
6566
}
6667

67-
exportfn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
fn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
6869
if (dest_n<n) __chk_fail();
6970
returnstrncpy(dest, src, n);
7071
}
7172

72-
exportfn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
73+
fn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
7374
@setRuntimeSafety(false);
7475

7576
varavail=dest_n;
@@ -94,7 +95,7 @@ export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
9495
returndest;
9596
}
9697

97-
exportfn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98+
fn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
9899
@setRuntimeSafety(false);
99100

100101
varavail=dest_n;
@@ -119,17 +120,17 @@ export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usi
119120
returndest;
120121
}
121122

122-
exportfn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123+
fn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123124
if (dest_n<n) __chk_fail();
124125
returnmemcpy(dest, src, n);
125126
}
126127

127-
exportfn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128+
fn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128129
if (dest_n<n) __chk_fail();
129130
returnmemmove(dest, src, n);
130131
}
131132

132-
exportfn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133+
fn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133134
if (dest_n<n) __chk_fail();
134135
returnmemset(dest, c, n);
135136
}

‎src/Compilation.zig‎

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152152
/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153153
/// and resolved before calling linker.flush().
154154
tsan_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().
157-
libssp_static_lib: ?CRTFile=null,
158155
/// Populated when we build the libc static library. A Job to build this is placed in the queue
159156
/// and resolved before calling linker.flush().
160157
libc_static_lib: ?CRTFile=null,
@@ -286,7 +283,6 @@ const Job = union(enum) {
286283
libcxx: void,
287284
libcxxabi: void,
288285
libtsan: void,
289-
libssp: void,
290286
/// needed when not linking libc and using LLVM for code generation because it generates
291287
/// calls to, for example, memcpy and memset.
292288
zig_libc: void,
@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683679
libtsan,
684680
wasi_libc_crt_file,
685681
compiler_rt,
686-
libssp,
687682
zig_libc,
688683
analyze_mod,
689684

@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13531348
if (stack_checkand!target_util.supportsStackProbing(options.target))
13541349
returnerror.StackCheckUnsupportedByTarget;
13551350

1356-
constcapable_of_building_ssp=canBuildLibSsp(options.target, use_llvm);
1357-
1358-
conststack_protector: u32=options.want_stack_protectororelseb: {
1359-
if (!target_util.supportsStackProtector(options.target)) break :b@as(u32, 0);
1351+
conststack_protector: u32=sp: {
1352+
constzig_backend=zigBackend(options.target, use_llvm);
1353+
if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1354+
if (options.want_stack_protector) |x| {
1355+
if (x>0) returnerror.StackProtectorUnsupportedByTarget;
1356+
}
1357+
break :sp0;
1358+
}
13601359

13611360
// This logic is checking for linking libc because otherwise our start code
13621361
// 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 {
13651364
// as being exempt from stack protection checks, we could change this logic
13661365
// to supporting stack protection even when not linking libc.
13671366
// TODO file issue about this
1368-
if (!link_libc) break :b0;
1369-
if (!capable_of_building_ssp) break :b0;
1370-
if (is_safe_mode) break :bdefault_stack_protector_buffer_size;
1371-
break :b0;
1367+
if (!link_libc) {
1368+
if (options.want_stack_protector) |x| {
1369+
if (x>0) returnerror.StackProtectorUnavailableWithoutLibC;
1370+
}
1371+
break :sp0;
1372+
}
1373+
1374+
if (options.want_stack_protector) |x|break :spx;
1375+
if (is_safe_mode) break :spdefault_stack_protector_buffer_size;
1376+
break :sp0;
13721377
};
1373-
if (stack_protector!=0) {
1374-
if (!target_util.supportsStackProtector(options.target))
1375-
returnerror.StackProtectorUnsupportedByTarget;
1376-
if (!capable_of_building_ssp)
1377-
returnerror.StackProtectorUnsupportedByBackend;
1378-
if (!link_libc)
1379-
returnerror.StackProtectorUnavailableWithoutLibC;
1380-
}
13811378

13821379
constinclude_compiler_rt=options.want_compiler_rtorelse
13831380
(!options.skip_linker_dependenciesandis_exe_or_dyn_lib);
@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
21952192
comp.job_queued_compiler_rt_obj=true;
21962193
}
21972194
}
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!=0and
2207-
(!comp.bin_file.options.link_libcor
2208-
!target_util.libcProvidesStackProtector(target)))
2209-
{
2210-
trycomp.work_queue.writeItem(.{ .libssp= {} });
2211-
}
22122195

2213-
if (!comp.bin_file.options.link_libcandcapable_of_building_zig_libc) {
2214-
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
2215-
}
2196+
if (!comp.bin_file.options.skip_linker_dependenciesandis_exe_or_dyn_liband
2197+
!comp.bin_file.options.link_libcandcapable_of_building_zig_libc)
2198+
{
2199+
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
22162200
}
22172201
}
22182202

@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
22582242
if (self.compiler_rt_obj) |*crt_file| {
22592243
crt_file.deinit(gpa);
22602244
}
2261-
if (self.libssp_static_lib) |*crt_file| {
2262-
crt_file.deinit(gpa);
2263-
}
22642245
if (self.libc_static_lib) |*crt_file| {
22652246
crt_file.deinit(gpa);
22662247
}
@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
40274008
);
40284009
};
40294010
},
4030-
.libssp=> {
4031-
constnamed_frame=tracy.namedFrame("libssp");
4032-
defernamed_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=>returnerror.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-
},
40504011
.zig_libc=> {
40514012
constnamed_frame=tracy.namedFrame("zig_libc");
40524013
defernamed_frame.end();
@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
65316492
};
65326493
}
65336494

6534-
fncanBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535-
switch (target.os.tag) {
6536-
.plan9=>returnfalse,
6537-
else=> {},
6538-
}
6539-
switch (target.cpu.arch) {
6540-
.spirv32, .spirv64=>returnfalse,
6541-
else=> {},
6542-
}
6543-
returnswitch (zigBackend(target, use_llvm)) {
6544-
.stage2_llvm=>true,
6545-
else=>build_options.have_llvm,
6546-
};
6547-
}
6548-
65496495
/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
65506496
/// This one builds lib/c.zig.
65516497
fncanBuildZigLibC(target: std.Target, use_llvm: bool) bool {
@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
65856531
};
65866532
}
65876533

6588-
fnneedsCSymbols(
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-
returnfalse;
6597-
6598-
switch (output_mode) {
6599-
.Obj=>returnfalse,
6600-
.Lib=>if (link_mode!=.Dynamic) returnfalse,
6601-
.Exe=> {},
6602-
}
6603-
6604-
// LLVM might generate calls to libc symbols.
6605-
if (zigBackend(target, use_llvm) ==.stage2_llvm)
6606-
returntrue;
6607-
6608-
returnfalse;
6609-
}
6610-
66116534
pubfngenerateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
66126535
consttracy_trace=trace(@src());
66136536
defertracy_trace.end();

‎src/link/Coff/lld.zig‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483483
tryargv.append(lib.full_object_path);
484484
}
485485
}
486-
// MinGW doesn't provide libssp symbols
487-
if (target.abi.isGnu()) {
488-
if (comp.libssp_static_lib) |lib| {
489-
tryargv.append(lib.full_object_path);
490-
}
491-
}
492486
// MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493487
// and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494488
if (comp.compiler_rt_obj) |obj|tryargv.append(obj.full_object_path);

‎src/link/Elf.zig‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10401040
}
10411041
}
10421042

1043-
// stack-protector.
1044-
// Related: https://github.com/ziglang/zig/issues/7265
1045-
if (comp.libssp_static_lib) |ssp| {
1046-
trypositionals.append(.{ .path=ssp.full_object_path });
1047-
}
1048-
10491043
for (positionals.items) |obj| {
10501044
varparse_ctx: ParseErrorCtx= .{ .detected_cpu_arch=undefined };
10511045
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch|err|
@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16891683
}
16901684
}
16911685

1692-
// stack-protector.
1693-
// Related: https://github.com/ziglang/zig/issues/7265
1694-
if (comp.libssp_static_lib) |ssp| {
1695-
tryargv.append(ssp.full_object_path);
1696-
}
1697-
16981686
// Shared libraries.
16991687
// Worst-case, we need an --as-needed argument for every lib, as well
17001688
// as one before and one after.
@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
27292717
}
27302718
}
27312719

2732-
// stack-protector.
2733-
// Related: https://github.com/ziglang/zig/issues/7265
2734-
if (comp.libssp_static_lib) |ssp| {
2735-
tryargv.append(ssp.full_object_path);
2736-
}
2737-
27382720
// Shared libraries.
27392721
if (is_exe_or_dyn_lib) {
27402722
constsystem_libs=self.base.options.system_libs.keys();

‎src/target.zig‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
(target.cpu.arch==.x86ortarget.cpu.arch==.x86_64);
329329
}
330330

331-
pubfnsupportsStackProtector(target: std.Target) bool {
332-
return!target.isSpirV();
331+
pubfnsupportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332+
switch (target.os.tag) {
333+
.plan9=>returnfalse,
334+
else=> {},
335+
}
336+
switch (target.cpu.arch) {
337+
.spirv32, .spirv64=>returnfalse,
338+
else=> {},
339+
}
340+
returnswitch (backend) {
341+
.stage2_llvm=>true,
342+
else=>false,
343+
};
333344
}
334345

335346
pubfnlibcProvidesStackProtector(target: std.Target) bool {

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 53f74d6

Browse files
committed
move libssp into libcompiler_rt
closes#7265
1 parent 2a81a0f commit 53f74d6

6 files changed

Lines changed: 60 additions & 148 deletions

File tree

‎lib/compiler_rt.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,6 @@ comptime {
233233
_=@import("compiler_rt/memmove.zig");
234234
_=@import("compiler_rt/memcmp.zig");
235235
_=@import("compiler_rt/bcmp.zig");
236+
_=@import("compiler_rt/ssp.zig");
236237
}
237238
}

lib/ssp.zig renamed to lib/compiler_rt/ssp.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,44 @@
1313
//! - __vsprintf_chk
1414

1515
conststd=@import("std");
16+
constcommon=@import("./common.zig");
17+
constbuiltin=@import("builtin");
1618

1719
externfnstrncpy(dest: [*:0]u8, src: [*:0]constu8, n: usize) callconv(.C) [*:0]u8;
1820
externfnmemset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
1921
externfnmemcpy(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2022
externfnmemmove(dest: ?[*]u8, src: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2123

22-
// Avoid dragging in the runtime safety mechanisms into this .o file.
23-
pubfnpanic(msg: []constu8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24-
_=msg;
25-
_=error_return_trace;
26-
@setCold(true);
27-
std.os.abort();
24+
comptime {
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 });
2835
}
2936

30-
exportfn__stack_chk_fail() callconv(.C) noreturn {
37+
fn__stack_chk_fail() callconv(.C) noreturn {
3138
@panic("stack smashing detected");
3239
}
3340

34-
exportfn__chk_fail() callconv(.C) noreturn {
41+
fn__chk_fail() callconv(.C) noreturn {
3542
@panic("buffer overflow detected");
3643
}
3744

38-
// Emitted when targeting some architectures (eg. x86)
39-
// XXX: This symbol should be hidden
40-
exportfn__stack_chk_fail_local() callconv(.C) noreturn {
41-
__stack_chk_fail();
42-
}
43-
44-
// XXX: Initialize the canary with random data
45-
exportvar__stack_chk_guard: usize=blk: {
45+
// TODO: Initialize the canary with random data
46+
var__stack_chk_guard: usize=blk: {
4647
varbuf= [1]u8{0} **@sizeOf(usize);
4748
buf[@sizeOf(usize) -1] =255;
4849
buf[@sizeOf(usize) -2] ='\n';
4950
break :blk@as(usize, @bitCast(buf));
5051
};
5152

52-
exportfn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
53+
fn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
5354
@setRuntimeSafety(false);
5455

5556
vari: usize=0;
@@ -64,12 +65,12 @@ export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
6465
returndest;
6566
}
6667

67-
exportfn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
fn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
6869
if (dest_n<n) __chk_fail();
6970
returnstrncpy(dest, src, n);
7071
}
7172

72-
exportfn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
73+
fn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
7374
@setRuntimeSafety(false);
7475

7576
varavail=dest_n;
@@ -94,7 +95,7 @@ export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
9495
returndest;
9596
}
9697

97-
exportfn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98+
fn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
9899
@setRuntimeSafety(false);
99100

100101
varavail=dest_n;
@@ -119,17 +120,17 @@ export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usi
119120
returndest;
120121
}
121122

122-
exportfn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123+
fn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123124
if (dest_n<n) __chk_fail();
124125
returnmemcpy(dest, src, n);
125126
}
126127

127-
exportfn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128+
fn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128129
if (dest_n<n) __chk_fail();
129130
returnmemmove(dest, src, n);
130131
}
131132

132-
exportfn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133+
fn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133134
if (dest_n<n) __chk_fail();
134135
returnmemset(dest, c, n);
135136
}

‎src/Compilation.zig‎

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152152
/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153153
/// and resolved before calling linker.flush().
154154
tsan_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().
157-
libssp_static_lib: ?CRTFile=null,
158155
/// Populated when we build the libc static library. A Job to build this is placed in the queue
159156
/// and resolved before calling linker.flush().
160157
libc_static_lib: ?CRTFile=null,
@@ -286,7 +283,6 @@ const Job = union(enum) {
286283
libcxx: void,
287284
libcxxabi: void,
288285
libtsan: void,
289-
libssp: void,
290286
/// needed when not linking libc and using LLVM for code generation because it generates
291287
/// calls to, for example, memcpy and memset.
292288
zig_libc: void,
@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683679
libtsan,
684680
wasi_libc_crt_file,
685681
compiler_rt,
686-
libssp,
687682
zig_libc,
688683
analyze_mod,
689684

@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13531348
if (stack_checkand!target_util.supportsStackProbing(options.target))
13541349
returnerror.StackCheckUnsupportedByTarget;
13551350

1356-
constcapable_of_building_ssp=canBuildLibSsp(options.target, use_llvm);
1357-
1358-
conststack_protector: u32=options.want_stack_protectororelseb: {
1359-
if (!target_util.supportsStackProtector(options.target)) break :b@as(u32, 0);
1351+
conststack_protector: u32=sp: {
1352+
constzig_backend=zigBackend(options.target, use_llvm);
1353+
if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1354+
if (options.want_stack_protector) |x| {
1355+
if (x>0) returnerror.StackProtectorUnsupportedByTarget;
1356+
}
1357+
break :sp0;
1358+
}
13601359

13611360
// This logic is checking for linking libc because otherwise our start code
13621361
// 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 {
13651364
// as being exempt from stack protection checks, we could change this logic
13661365
// to supporting stack protection even when not linking libc.
13671366
// TODO file issue about this
1368-
if (!link_libc) break :b0;
1369-
if (!capable_of_building_ssp) break :b0;
1370-
if (is_safe_mode) break :bdefault_stack_protector_buffer_size;
1371-
break :b0;
1367+
if (!link_libc) {
1368+
if (options.want_stack_protector) |x| {
1369+
if (x>0) returnerror.StackProtectorUnavailableWithoutLibC;
1370+
}
1371+
break :sp0;
1372+
}
1373+
1374+
if (options.want_stack_protector) |x|break :spx;
1375+
if (is_safe_mode) break :spdefault_stack_protector_buffer_size;
1376+
break :sp0;
13721377
};
1373-
if (stack_protector!=0) {
1374-
if (!target_util.supportsStackProtector(options.target))
1375-
returnerror.StackProtectorUnsupportedByTarget;
1376-
if (!capable_of_building_ssp)
1377-
returnerror.StackProtectorUnsupportedByBackend;
1378-
if (!link_libc)
1379-
returnerror.StackProtectorUnavailableWithoutLibC;
1380-
}
13811378

13821379
constinclude_compiler_rt=options.want_compiler_rtorelse
13831380
(!options.skip_linker_dependenciesandis_exe_or_dyn_lib);
@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
21952192
comp.job_queued_compiler_rt_obj=true;
21962193
}
21972194
}
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!=0and
2207-
(!comp.bin_file.options.link_libcor
2208-
!target_util.libcProvidesStackProtector(target)))
2209-
{
2210-
trycomp.work_queue.writeItem(.{ .libssp= {} });
2211-
}
22122195

2213-
if (!comp.bin_file.options.link_libcandcapable_of_building_zig_libc) {
2214-
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
2215-
}
2196+
if (!comp.bin_file.options.skip_linker_dependenciesandis_exe_or_dyn_liband
2197+
!comp.bin_file.options.link_libcandcapable_of_building_zig_libc)
2198+
{
2199+
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
22162200
}
22172201
}
22182202

@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
22582242
if (self.compiler_rt_obj) |*crt_file| {
22592243
crt_file.deinit(gpa);
22602244
}
2261-
if (self.libssp_static_lib) |*crt_file| {
2262-
crt_file.deinit(gpa);
2263-
}
22642245
if (self.libc_static_lib) |*crt_file| {
22652246
crt_file.deinit(gpa);
22662247
}
@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
40274008
);
40284009
};
40294010
},
4030-
.libssp=> {
4031-
constnamed_frame=tracy.namedFrame("libssp");
4032-
defernamed_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=>returnerror.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-
},
40504011
.zig_libc=> {
40514012
constnamed_frame=tracy.namedFrame("zig_libc");
40524013
defernamed_frame.end();
@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
65316492
};
65326493
}
65336494

6534-
fncanBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535-
switch (target.os.tag) {
6536-
.plan9=>returnfalse,
6537-
else=> {},
6538-
}
6539-
switch (target.cpu.arch) {
6540-
.spirv32, .spirv64=>returnfalse,
6541-
else=> {},
6542-
}
6543-
returnswitch (zigBackend(target, use_llvm)) {
6544-
.stage2_llvm=>true,
6545-
else=>build_options.have_llvm,
6546-
};
6547-
}
6548-
65496495
/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
65506496
/// This one builds lib/c.zig.
65516497
fncanBuildZigLibC(target: std.Target, use_llvm: bool) bool {
@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
65856531
};
65866532
}
65876533

6588-
fnneedsCSymbols(
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-
returnfalse;
6597-
6598-
switch (output_mode) {
6599-
.Obj=>returnfalse,
6600-
.Lib=>if (link_mode!=.Dynamic) returnfalse,
6601-
.Exe=> {},
6602-
}
6603-
6604-
// LLVM might generate calls to libc symbols.
6605-
if (zigBackend(target, use_llvm) ==.stage2_llvm)
6606-
returntrue;
6607-
6608-
returnfalse;
6609-
}
6610-
66116534
pubfngenerateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
66126535
consttracy_trace=trace(@src());
66136536
defertracy_trace.end();

‎src/link/Coff/lld.zig‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483483
tryargv.append(lib.full_object_path);
484484
}
485485
}
486-
// MinGW doesn't provide libssp symbols
487-
if (target.abi.isGnu()) {
488-
if (comp.libssp_static_lib) |lib| {
489-
tryargv.append(lib.full_object_path);
490-
}
491-
}
492486
// MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493487
// and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494488
if (comp.compiler_rt_obj) |obj|tryargv.append(obj.full_object_path);

‎src/link/Elf.zig‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10401040
}
10411041
}
10421042

1043-
// stack-protector.
1044-
// Related: https://github.com/ziglang/zig/issues/7265
1045-
if (comp.libssp_static_lib) |ssp| {
1046-
trypositionals.append(.{ .path=ssp.full_object_path });
1047-
}
1048-
10491043
for (positionals.items) |obj| {
10501044
varparse_ctx: ParseErrorCtx= .{ .detected_cpu_arch=undefined };
10511045
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch|err|
@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16891683
}
16901684
}
16911685

1692-
// stack-protector.
1693-
// Related: https://github.com/ziglang/zig/issues/7265
1694-
if (comp.libssp_static_lib) |ssp| {
1695-
tryargv.append(ssp.full_object_path);
1696-
}
1697-
16981686
// Shared libraries.
16991687
// Worst-case, we need an --as-needed argument for every lib, as well
17001688
// as one before and one after.
@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
27292717
}
27302718
}
27312719

2732-
// stack-protector.
2733-
// Related: https://github.com/ziglang/zig/issues/7265
2734-
if (comp.libssp_static_lib) |ssp| {
2735-
tryargv.append(ssp.full_object_path);
2736-
}
2737-
27382720
// Shared libraries.
27392721
if (is_exe_or_dyn_lib) {
27402722
constsystem_libs=self.base.options.system_libs.keys();

‎src/target.zig‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
(target.cpu.arch==.x86ortarget.cpu.arch==.x86_64);
329329
}
330330

331-
pubfnsupportsStackProtector(target: std.Target) bool {
332-
return!target.isSpirV();
331+
pubfnsupportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332+
switch (target.os.tag) {
333+
.plan9=>returnfalse,
334+
else=> {},
335+
}
336+
switch (target.cpu.arch) {
337+
.spirv32, .spirv64=>returnfalse,
338+
else=> {},
339+
}
340+
returnswitch (backend) {
341+
.stage2_llvm=>true,
342+
else=>false,
343+
};
333344
}
334345

335346
pubfnlibcProvidesStackProtector(target: std.Target) bool {

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 53f74d6

Browse files
committed
move libssp into libcompiler_rt
closes#7265
1 parent 2a81a0f commit 53f74d6

6 files changed

Lines changed: 60 additions & 148 deletions

File tree

‎lib/compiler_rt.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,6 @@ comptime {
233233
_=@import("compiler_rt/memmove.zig");
234234
_=@import("compiler_rt/memcmp.zig");
235235
_=@import("compiler_rt/bcmp.zig");
236+
_=@import("compiler_rt/ssp.zig");
236237
}
237238
}

lib/ssp.zig renamed to lib/compiler_rt/ssp.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,44 @@
1313
//! - __vsprintf_chk
1414

1515
conststd=@import("std");
16+
constcommon=@import("./common.zig");
17+
constbuiltin=@import("builtin");
1618

1719
externfnstrncpy(dest: [*:0]u8, src: [*:0]constu8, n: usize) callconv(.C) [*:0]u8;
1820
externfnmemset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
1921
externfnmemcpy(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2022
externfnmemmove(dest: ?[*]u8, src: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2123

22-
// Avoid dragging in the runtime safety mechanisms into this .o file.
23-
pubfnpanic(msg: []constu8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24-
_=msg;
25-
_=error_return_trace;
26-
@setCold(true);
27-
std.os.abort();
24+
comptime {
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 });
2835
}
2936

30-
exportfn__stack_chk_fail() callconv(.C) noreturn {
37+
fn__stack_chk_fail() callconv(.C) noreturn {
3138
@panic("stack smashing detected");
3239
}
3340

34-
exportfn__chk_fail() callconv(.C) noreturn {
41+
fn__chk_fail() callconv(.C) noreturn {
3542
@panic("buffer overflow detected");
3643
}
3744

38-
// Emitted when targeting some architectures (eg. x86)
39-
// XXX: This symbol should be hidden
40-
exportfn__stack_chk_fail_local() callconv(.C) noreturn {
41-
__stack_chk_fail();
42-
}
43-
44-
// XXX: Initialize the canary with random data
45-
exportvar__stack_chk_guard: usize=blk: {
45+
// TODO: Initialize the canary with random data
46+
var__stack_chk_guard: usize=blk: {
4647
varbuf= [1]u8{0} **@sizeOf(usize);
4748
buf[@sizeOf(usize) -1] =255;
4849
buf[@sizeOf(usize) -2] ='\n';
4950
break :blk@as(usize, @bitCast(buf));
5051
};
5152

52-
exportfn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
53+
fn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
5354
@setRuntimeSafety(false);
5455

5556
vari: usize=0;
@@ -64,12 +65,12 @@ export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
6465
returndest;
6566
}
6667

67-
exportfn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
fn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
6869
if (dest_n<n) __chk_fail();
6970
returnstrncpy(dest, src, n);
7071
}
7172

72-
exportfn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
73+
fn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
7374
@setRuntimeSafety(false);
7475

7576
varavail=dest_n;
@@ -94,7 +95,7 @@ export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
9495
returndest;
9596
}
9697

97-
exportfn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98+
fn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
9899
@setRuntimeSafety(false);
99100

100101
varavail=dest_n;
@@ -119,17 +120,17 @@ export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usi
119120
returndest;
120121
}
121122

122-
exportfn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123+
fn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123124
if (dest_n<n) __chk_fail();
124125
returnmemcpy(dest, src, n);
125126
}
126127

127-
exportfn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128+
fn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128129
if (dest_n<n) __chk_fail();
129130
returnmemmove(dest, src, n);
130131
}
131132

132-
exportfn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133+
fn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133134
if (dest_n<n) __chk_fail();
134135
returnmemset(dest, c, n);
135136
}

‎src/Compilation.zig‎

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152152
/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153153
/// and resolved before calling linker.flush().
154154
tsan_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().
157-
libssp_static_lib: ?CRTFile=null,
158155
/// Populated when we build the libc static library. A Job to build this is placed in the queue
159156
/// and resolved before calling linker.flush().
160157
libc_static_lib: ?CRTFile=null,
@@ -286,7 +283,6 @@ const Job = union(enum) {
286283
libcxx: void,
287284
libcxxabi: void,
288285
libtsan: void,
289-
libssp: void,
290286
/// needed when not linking libc and using LLVM for code generation because it generates
291287
/// calls to, for example, memcpy and memset.
292288
zig_libc: void,
@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683679
libtsan,
684680
wasi_libc_crt_file,
685681
compiler_rt,
686-
libssp,
687682
zig_libc,
688683
analyze_mod,
689684

@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13531348
if (stack_checkand!target_util.supportsStackProbing(options.target))
13541349
returnerror.StackCheckUnsupportedByTarget;
13551350

1356-
constcapable_of_building_ssp=canBuildLibSsp(options.target, use_llvm);
1357-
1358-
conststack_protector: u32=options.want_stack_protectororelseb: {
1359-
if (!target_util.supportsStackProtector(options.target)) break :b@as(u32, 0);
1351+
conststack_protector: u32=sp: {
1352+
constzig_backend=zigBackend(options.target, use_llvm);
1353+
if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1354+
if (options.want_stack_protector) |x| {
1355+
if (x>0) returnerror.StackProtectorUnsupportedByTarget;
1356+
}
1357+
break :sp0;
1358+
}
13601359

13611360
// This logic is checking for linking libc because otherwise our start code
13621361
// 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 {
13651364
// as being exempt from stack protection checks, we could change this logic
13661365
// to supporting stack protection even when not linking libc.
13671366
// TODO file issue about this
1368-
if (!link_libc) break :b0;
1369-
if (!capable_of_building_ssp) break :b0;
1370-
if (is_safe_mode) break :bdefault_stack_protector_buffer_size;
1371-
break :b0;
1367+
if (!link_libc) {
1368+
if (options.want_stack_protector) |x| {
1369+
if (x>0) returnerror.StackProtectorUnavailableWithoutLibC;
1370+
}
1371+
break :sp0;
1372+
}
1373+
1374+
if (options.want_stack_protector) |x|break :spx;
1375+
if (is_safe_mode) break :spdefault_stack_protector_buffer_size;
1376+
break :sp0;
13721377
};
1373-
if (stack_protector!=0) {
1374-
if (!target_util.supportsStackProtector(options.target))
1375-
returnerror.StackProtectorUnsupportedByTarget;
1376-
if (!capable_of_building_ssp)
1377-
returnerror.StackProtectorUnsupportedByBackend;
1378-
if (!link_libc)
1379-
returnerror.StackProtectorUnavailableWithoutLibC;
1380-
}
13811378

13821379
constinclude_compiler_rt=options.want_compiler_rtorelse
13831380
(!options.skip_linker_dependenciesandis_exe_or_dyn_lib);
@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
21952192
comp.job_queued_compiler_rt_obj=true;
21962193
}
21972194
}
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!=0and
2207-
(!comp.bin_file.options.link_libcor
2208-
!target_util.libcProvidesStackProtector(target)))
2209-
{
2210-
trycomp.work_queue.writeItem(.{ .libssp= {} });
2211-
}
22122195

2213-
if (!comp.bin_file.options.link_libcandcapable_of_building_zig_libc) {
2214-
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
2215-
}
2196+
if (!comp.bin_file.options.skip_linker_dependenciesandis_exe_or_dyn_liband
2197+
!comp.bin_file.options.link_libcandcapable_of_building_zig_libc)
2198+
{
2199+
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
22162200
}
22172201
}
22182202

@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
22582242
if (self.compiler_rt_obj) |*crt_file| {
22592243
crt_file.deinit(gpa);
22602244
}
2261-
if (self.libssp_static_lib) |*crt_file| {
2262-
crt_file.deinit(gpa);
2263-
}
22642245
if (self.libc_static_lib) |*crt_file| {
22652246
crt_file.deinit(gpa);
22662247
}
@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
40274008
);
40284009
};
40294010
},
4030-
.libssp=> {
4031-
constnamed_frame=tracy.namedFrame("libssp");
4032-
defernamed_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=>returnerror.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-
},
40504011
.zig_libc=> {
40514012
constnamed_frame=tracy.namedFrame("zig_libc");
40524013
defernamed_frame.end();
@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
65316492
};
65326493
}
65336494

6534-
fncanBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535-
switch (target.os.tag) {
6536-
.plan9=>returnfalse,
6537-
else=> {},
6538-
}
6539-
switch (target.cpu.arch) {
6540-
.spirv32, .spirv64=>returnfalse,
6541-
else=> {},
6542-
}
6543-
returnswitch (zigBackend(target, use_llvm)) {
6544-
.stage2_llvm=>true,
6545-
else=>build_options.have_llvm,
6546-
};
6547-
}
6548-
65496495
/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
65506496
/// This one builds lib/c.zig.
65516497
fncanBuildZigLibC(target: std.Target, use_llvm: bool) bool {
@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
65856531
};
65866532
}
65876533

6588-
fnneedsCSymbols(
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-
returnfalse;
6597-
6598-
switch (output_mode) {
6599-
.Obj=>returnfalse,
6600-
.Lib=>if (link_mode!=.Dynamic) returnfalse,
6601-
.Exe=> {},
6602-
}
6603-
6604-
// LLVM might generate calls to libc symbols.
6605-
if (zigBackend(target, use_llvm) ==.stage2_llvm)
6606-
returntrue;
6607-
6608-
returnfalse;
6609-
}
6610-
66116534
pubfngenerateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
66126535
consttracy_trace=trace(@src());
66136536
defertracy_trace.end();

‎src/link/Coff/lld.zig‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483483
tryargv.append(lib.full_object_path);
484484
}
485485
}
486-
// MinGW doesn't provide libssp symbols
487-
if (target.abi.isGnu()) {
488-
if (comp.libssp_static_lib) |lib| {
489-
tryargv.append(lib.full_object_path);
490-
}
491-
}
492486
// MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493487
// and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494488
if (comp.compiler_rt_obj) |obj|tryargv.append(obj.full_object_path);

‎src/link/Elf.zig‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10401040
}
10411041
}
10421042

1043-
// stack-protector.
1044-
// Related: https://github.com/ziglang/zig/issues/7265
1045-
if (comp.libssp_static_lib) |ssp| {
1046-
trypositionals.append(.{ .path=ssp.full_object_path });
1047-
}
1048-
10491043
for (positionals.items) |obj| {
10501044
varparse_ctx: ParseErrorCtx= .{ .detected_cpu_arch=undefined };
10511045
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch|err|
@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16891683
}
16901684
}
16911685

1692-
// stack-protector.
1693-
// Related: https://github.com/ziglang/zig/issues/7265
1694-
if (comp.libssp_static_lib) |ssp| {
1695-
tryargv.append(ssp.full_object_path);
1696-
}
1697-
16981686
// Shared libraries.
16991687
// Worst-case, we need an --as-needed argument for every lib, as well
17001688
// as one before and one after.
@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
27292717
}
27302718
}
27312719

2732-
// stack-protector.
2733-
// Related: https://github.com/ziglang/zig/issues/7265
2734-
if (comp.libssp_static_lib) |ssp| {
2735-
tryargv.append(ssp.full_object_path);
2736-
}
2737-
27382720
// Shared libraries.
27392721
if (is_exe_or_dyn_lib) {
27402722
constsystem_libs=self.base.options.system_libs.keys();

‎src/target.zig‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
(target.cpu.arch==.x86ortarget.cpu.arch==.x86_64);
329329
}
330330

331-
pubfnsupportsStackProtector(target: std.Target) bool {
332-
return!target.isSpirV();
331+
pubfnsupportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332+
switch (target.os.tag) {
333+
.plan9=>returnfalse,
334+
else=> {},
335+
}
336+
switch (target.cpu.arch) {
337+
.spirv32, .spirv64=>returnfalse,
338+
else=> {},
339+
}
340+
returnswitch (backend) {
341+
.stage2_llvm=>true,
342+
else=>false,
343+
};
333344
}
334345

335346
pubfnlibcProvidesStackProtector(target: std.Target) bool {

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 53f74d6

Browse files
committed
move libssp into libcompiler_rt
closes#7265
1 parent 2a81a0f commit 53f74d6

6 files changed

Lines changed: 60 additions & 148 deletions

File tree

‎lib/compiler_rt.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,6 @@ comptime {
233233
_=@import("compiler_rt/memmove.zig");
234234
_=@import("compiler_rt/memcmp.zig");
235235
_=@import("compiler_rt/bcmp.zig");
236+
_=@import("compiler_rt/ssp.zig");
236237
}
237238
}

lib/ssp.zig renamed to lib/compiler_rt/ssp.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,44 @@
1313
//! - __vsprintf_chk
1414

1515
conststd=@import("std");
16+
constcommon=@import("./common.zig");
17+
constbuiltin=@import("builtin");
1618

1719
externfnstrncpy(dest: [*:0]u8, src: [*:0]constu8, n: usize) callconv(.C) [*:0]u8;
1820
externfnmemset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
1921
externfnmemcpy(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2022
externfnmemmove(dest: ?[*]u8, src: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2123

22-
// Avoid dragging in the runtime safety mechanisms into this .o file.
23-
pubfnpanic(msg: []constu8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24-
_=msg;
25-
_=error_return_trace;
26-
@setCold(true);
27-
std.os.abort();
24+
comptime {
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 });
2835
}
2936

30-
exportfn__stack_chk_fail() callconv(.C) noreturn {
37+
fn__stack_chk_fail() callconv(.C) noreturn {
3138
@panic("stack smashing detected");
3239
}
3340

34-
exportfn__chk_fail() callconv(.C) noreturn {
41+
fn__chk_fail() callconv(.C) noreturn {
3542
@panic("buffer overflow detected");
3643
}
3744

38-
// Emitted when targeting some architectures (eg. x86)
39-
// XXX: This symbol should be hidden
40-
exportfn__stack_chk_fail_local() callconv(.C) noreturn {
41-
__stack_chk_fail();
42-
}
43-
44-
// XXX: Initialize the canary with random data
45-
exportvar__stack_chk_guard: usize=blk: {
45+
// TODO: Initialize the canary with random data
46+
var__stack_chk_guard: usize=blk: {
4647
varbuf= [1]u8{0} **@sizeOf(usize);
4748
buf[@sizeOf(usize) -1] =255;
4849
buf[@sizeOf(usize) -2] ='\n';
4950
break :blk@as(usize, @bitCast(buf));
5051
};
5152

52-
exportfn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
53+
fn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
5354
@setRuntimeSafety(false);
5455

5556
vari: usize=0;
@@ -64,12 +65,12 @@ export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
6465
returndest;
6566
}
6667

67-
exportfn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
fn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
6869
if (dest_n<n) __chk_fail();
6970
returnstrncpy(dest, src, n);
7071
}
7172

72-
exportfn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
73+
fn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
7374
@setRuntimeSafety(false);
7475

7576
varavail=dest_n;
@@ -94,7 +95,7 @@ export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
9495
returndest;
9596
}
9697

97-
exportfn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98+
fn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
9899
@setRuntimeSafety(false);
99100

100101
varavail=dest_n;
@@ -119,17 +120,17 @@ export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usi
119120
returndest;
120121
}
121122

122-
exportfn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123+
fn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123124
if (dest_n<n) __chk_fail();
124125
returnmemcpy(dest, src, n);
125126
}
126127

127-
exportfn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128+
fn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128129
if (dest_n<n) __chk_fail();
129130
returnmemmove(dest, src, n);
130131
}
131132

132-
exportfn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133+
fn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133134
if (dest_n<n) __chk_fail();
134135
returnmemset(dest, c, n);
135136
}

‎src/Compilation.zig‎

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152152
/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153153
/// and resolved before calling linker.flush().
154154
tsan_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().
157-
libssp_static_lib: ?CRTFile=null,
158155
/// Populated when we build the libc static library. A Job to build this is placed in the queue
159156
/// and resolved before calling linker.flush().
160157
libc_static_lib: ?CRTFile=null,
@@ -286,7 +283,6 @@ const Job = union(enum) {
286283
libcxx: void,
287284
libcxxabi: void,
288285
libtsan: void,
289-
libssp: void,
290286
/// needed when not linking libc and using LLVM for code generation because it generates
291287
/// calls to, for example, memcpy and memset.
292288
zig_libc: void,
@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683679
libtsan,
684680
wasi_libc_crt_file,
685681
compiler_rt,
686-
libssp,
687682
zig_libc,
688683
analyze_mod,
689684

@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13531348
if (stack_checkand!target_util.supportsStackProbing(options.target))
13541349
returnerror.StackCheckUnsupportedByTarget;
13551350

1356-
constcapable_of_building_ssp=canBuildLibSsp(options.target, use_llvm);
1357-
1358-
conststack_protector: u32=options.want_stack_protectororelseb: {
1359-
if (!target_util.supportsStackProtector(options.target)) break :b@as(u32, 0);
1351+
conststack_protector: u32=sp: {
1352+
constzig_backend=zigBackend(options.target, use_llvm);
1353+
if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1354+
if (options.want_stack_protector) |x| {
1355+
if (x>0) returnerror.StackProtectorUnsupportedByTarget;
1356+
}
1357+
break :sp0;
1358+
}
13601359

13611360
// This logic is checking for linking libc because otherwise our start code
13621361
// 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 {
13651364
// as being exempt from stack protection checks, we could change this logic
13661365
// to supporting stack protection even when not linking libc.
13671366
// TODO file issue about this
1368-
if (!link_libc) break :b0;
1369-
if (!capable_of_building_ssp) break :b0;
1370-
if (is_safe_mode) break :bdefault_stack_protector_buffer_size;
1371-
break :b0;
1367+
if (!link_libc) {
1368+
if (options.want_stack_protector) |x| {
1369+
if (x>0) returnerror.StackProtectorUnavailableWithoutLibC;
1370+
}
1371+
break :sp0;
1372+
}
1373+
1374+
if (options.want_stack_protector) |x|break :spx;
1375+
if (is_safe_mode) break :spdefault_stack_protector_buffer_size;
1376+
break :sp0;
13721377
};
1373-
if (stack_protector!=0) {
1374-
if (!target_util.supportsStackProtector(options.target))
1375-
returnerror.StackProtectorUnsupportedByTarget;
1376-
if (!capable_of_building_ssp)
1377-
returnerror.StackProtectorUnsupportedByBackend;
1378-
if (!link_libc)
1379-
returnerror.StackProtectorUnavailableWithoutLibC;
1380-
}
13811378

13821379
constinclude_compiler_rt=options.want_compiler_rtorelse
13831380
(!options.skip_linker_dependenciesandis_exe_or_dyn_lib);
@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
21952192
comp.job_queued_compiler_rt_obj=true;
21962193
}
21972194
}
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!=0and
2207-
(!comp.bin_file.options.link_libcor
2208-
!target_util.libcProvidesStackProtector(target)))
2209-
{
2210-
trycomp.work_queue.writeItem(.{ .libssp= {} });
2211-
}
22122195

2213-
if (!comp.bin_file.options.link_libcandcapable_of_building_zig_libc) {
2214-
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
2215-
}
2196+
if (!comp.bin_file.options.skip_linker_dependenciesandis_exe_or_dyn_liband
2197+
!comp.bin_file.options.link_libcandcapable_of_building_zig_libc)
2198+
{
2199+
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
22162200
}
22172201
}
22182202

@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
22582242
if (self.compiler_rt_obj) |*crt_file| {
22592243
crt_file.deinit(gpa);
22602244
}
2261-
if (self.libssp_static_lib) |*crt_file| {
2262-
crt_file.deinit(gpa);
2263-
}
22642245
if (self.libc_static_lib) |*crt_file| {
22652246
crt_file.deinit(gpa);
22662247
}
@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
40274008
);
40284009
};
40294010
},
4030-
.libssp=> {
4031-
constnamed_frame=tracy.namedFrame("libssp");
4032-
defernamed_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=>returnerror.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-
},
40504011
.zig_libc=> {
40514012
constnamed_frame=tracy.namedFrame("zig_libc");
40524013
defernamed_frame.end();
@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
65316492
};
65326493
}
65336494

6534-
fncanBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535-
switch (target.os.tag) {
6536-
.plan9=>returnfalse,
6537-
else=> {},
6538-
}
6539-
switch (target.cpu.arch) {
6540-
.spirv32, .spirv64=>returnfalse,
6541-
else=> {},
6542-
}
6543-
returnswitch (zigBackend(target, use_llvm)) {
6544-
.stage2_llvm=>true,
6545-
else=>build_options.have_llvm,
6546-
};
6547-
}
6548-
65496495
/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
65506496
/// This one builds lib/c.zig.
65516497
fncanBuildZigLibC(target: std.Target, use_llvm: bool) bool {
@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
65856531
};
65866532
}
65876533

6588-
fnneedsCSymbols(
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-
returnfalse;
6597-
6598-
switch (output_mode) {
6599-
.Obj=>returnfalse,
6600-
.Lib=>if (link_mode!=.Dynamic) returnfalse,
6601-
.Exe=> {},
6602-
}
6603-
6604-
// LLVM might generate calls to libc symbols.
6605-
if (zigBackend(target, use_llvm) ==.stage2_llvm)
6606-
returntrue;
6607-
6608-
returnfalse;
6609-
}
6610-
66116534
pubfngenerateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
66126535
consttracy_trace=trace(@src());
66136536
defertracy_trace.end();

‎src/link/Coff/lld.zig‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483483
tryargv.append(lib.full_object_path);
484484
}
485485
}
486-
// MinGW doesn't provide libssp symbols
487-
if (target.abi.isGnu()) {
488-
if (comp.libssp_static_lib) |lib| {
489-
tryargv.append(lib.full_object_path);
490-
}
491-
}
492486
// MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493487
// and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494488
if (comp.compiler_rt_obj) |obj|tryargv.append(obj.full_object_path);

‎src/link/Elf.zig‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10401040
}
10411041
}
10421042

1043-
// stack-protector.
1044-
// Related: https://github.com/ziglang/zig/issues/7265
1045-
if (comp.libssp_static_lib) |ssp| {
1046-
trypositionals.append(.{ .path=ssp.full_object_path });
1047-
}
1048-
10491043
for (positionals.items) |obj| {
10501044
varparse_ctx: ParseErrorCtx= .{ .detected_cpu_arch=undefined };
10511045
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch|err|
@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16891683
}
16901684
}
16911685

1692-
// stack-protector.
1693-
// Related: https://github.com/ziglang/zig/issues/7265
1694-
if (comp.libssp_static_lib) |ssp| {
1695-
tryargv.append(ssp.full_object_path);
1696-
}
1697-
16981686
// Shared libraries.
16991687
// Worst-case, we need an --as-needed argument for every lib, as well
17001688
// as one before and one after.
@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
27292717
}
27302718
}
27312719

2732-
// stack-protector.
2733-
// Related: https://github.com/ziglang/zig/issues/7265
2734-
if (comp.libssp_static_lib) |ssp| {
2735-
tryargv.append(ssp.full_object_path);
2736-
}
2737-
27382720
// Shared libraries.
27392721
if (is_exe_or_dyn_lib) {
27402722
constsystem_libs=self.base.options.system_libs.keys();

‎src/target.zig‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
(target.cpu.arch==.x86ortarget.cpu.arch==.x86_64);
329329
}
330330

331-
pubfnsupportsStackProtector(target: std.Target) bool {
332-
return!target.isSpirV();
331+
pubfnsupportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332+
switch (target.os.tag) {
333+
.plan9=>returnfalse,
334+
else=> {},
335+
}
336+
switch (target.cpu.arch) {
337+
.spirv32, .spirv64=>returnfalse,
338+
else=> {},
339+
}
340+
returnswitch (backend) {
341+
.stage2_llvm=>true,
342+
else=>false,
343+
};
333344
}
334345

335346
pubfnlibcProvidesStackProtector(target: std.Target) bool {

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 53f74d6

Browse files
committed
move libssp into libcompiler_rt
closes#7265
1 parent 2a81a0f commit 53f74d6

6 files changed

Lines changed: 60 additions & 148 deletions

File tree

‎lib/compiler_rt.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,6 @@ comptime {
233233
_=@import("compiler_rt/memmove.zig");
234234
_=@import("compiler_rt/memcmp.zig");
235235
_=@import("compiler_rt/bcmp.zig");
236+
_=@import("compiler_rt/ssp.zig");
236237
}
237238
}

lib/ssp.zig renamed to lib/compiler_rt/ssp.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,44 @@
1313
//! - __vsprintf_chk
1414

1515
conststd=@import("std");
16+
constcommon=@import("./common.zig");
17+
constbuiltin=@import("builtin");
1618

1719
externfnstrncpy(dest: [*:0]u8, src: [*:0]constu8, n: usize) callconv(.C) [*:0]u8;
1820
externfnmemset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
1921
externfnmemcpy(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2022
externfnmemmove(dest: ?[*]u8, src: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2123

22-
// Avoid dragging in the runtime safety mechanisms into this .o file.
23-
pubfnpanic(msg: []constu8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24-
_=msg;
25-
_=error_return_trace;
26-
@setCold(true);
27-
std.os.abort();
24+
comptime {
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 });
2835
}
2936

30-
exportfn__stack_chk_fail() callconv(.C) noreturn {
37+
fn__stack_chk_fail() callconv(.C) noreturn {
3138
@panic("stack smashing detected");
3239
}
3340

34-
exportfn__chk_fail() callconv(.C) noreturn {
41+
fn__chk_fail() callconv(.C) noreturn {
3542
@panic("buffer overflow detected");
3643
}
3744

38-
// Emitted when targeting some architectures (eg. x86)
39-
// XXX: This symbol should be hidden
40-
exportfn__stack_chk_fail_local() callconv(.C) noreturn {
41-
__stack_chk_fail();
42-
}
43-
44-
// XXX: Initialize the canary with random data
45-
exportvar__stack_chk_guard: usize=blk: {
45+
// TODO: Initialize the canary with random data
46+
var__stack_chk_guard: usize=blk: {
4647
varbuf= [1]u8{0} **@sizeOf(usize);
4748
buf[@sizeOf(usize) -1] =255;
4849
buf[@sizeOf(usize) -2] ='\n';
4950
break :blk@as(usize, @bitCast(buf));
5051
};
5152

52-
exportfn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
53+
fn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
5354
@setRuntimeSafety(false);
5455

5556
vari: usize=0;
@@ -64,12 +65,12 @@ export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
6465
returndest;
6566
}
6667

67-
exportfn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
fn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
6869
if (dest_n<n) __chk_fail();
6970
returnstrncpy(dest, src, n);
7071
}
7172

72-
exportfn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
73+
fn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
7374
@setRuntimeSafety(false);
7475

7576
varavail=dest_n;
@@ -94,7 +95,7 @@ export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
9495
returndest;
9596
}
9697

97-
exportfn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98+
fn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
9899
@setRuntimeSafety(false);
99100

100101
varavail=dest_n;
@@ -119,17 +120,17 @@ export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usi
119120
returndest;
120121
}
121122

122-
exportfn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123+
fn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123124
if (dest_n<n) __chk_fail();
124125
returnmemcpy(dest, src, n);
125126
}
126127

127-
exportfn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128+
fn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128129
if (dest_n<n) __chk_fail();
129130
returnmemmove(dest, src, n);
130131
}
131132

132-
exportfn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133+
fn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133134
if (dest_n<n) __chk_fail();
134135
returnmemset(dest, c, n);
135136
}

‎src/Compilation.zig‎

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152152
/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153153
/// and resolved before calling linker.flush().
154154
tsan_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().
157-
libssp_static_lib: ?CRTFile=null,
158155
/// Populated when we build the libc static library. A Job to build this is placed in the queue
159156
/// and resolved before calling linker.flush().
160157
libc_static_lib: ?CRTFile=null,
@@ -286,7 +283,6 @@ const Job = union(enum) {
286283
libcxx: void,
287284
libcxxabi: void,
288285
libtsan: void,
289-
libssp: void,
290286
/// needed when not linking libc and using LLVM for code generation because it generates
291287
/// calls to, for example, memcpy and memset.
292288
zig_libc: void,
@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683679
libtsan,
684680
wasi_libc_crt_file,
685681
compiler_rt,
686-
libssp,
687682
zig_libc,
688683
analyze_mod,
689684

@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13531348
if (stack_checkand!target_util.supportsStackProbing(options.target))
13541349
returnerror.StackCheckUnsupportedByTarget;
13551350

1356-
constcapable_of_building_ssp=canBuildLibSsp(options.target, use_llvm);
1357-
1358-
conststack_protector: u32=options.want_stack_protectororelseb: {
1359-
if (!target_util.supportsStackProtector(options.target)) break :b@as(u32, 0);
1351+
conststack_protector: u32=sp: {
1352+
constzig_backend=zigBackend(options.target, use_llvm);
1353+
if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1354+
if (options.want_stack_protector) |x| {
1355+
if (x>0) returnerror.StackProtectorUnsupportedByTarget;
1356+
}
1357+
break :sp0;
1358+
}
13601359

13611360
// This logic is checking for linking libc because otherwise our start code
13621361
// 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 {
13651364
// as being exempt from stack protection checks, we could change this logic
13661365
// to supporting stack protection even when not linking libc.
13671366
// TODO file issue about this
1368-
if (!link_libc) break :b0;
1369-
if (!capable_of_building_ssp) break :b0;
1370-
if (is_safe_mode) break :bdefault_stack_protector_buffer_size;
1371-
break :b0;
1367+
if (!link_libc) {
1368+
if (options.want_stack_protector) |x| {
1369+
if (x>0) returnerror.StackProtectorUnavailableWithoutLibC;
1370+
}
1371+
break :sp0;
1372+
}
1373+
1374+
if (options.want_stack_protector) |x|break :spx;
1375+
if (is_safe_mode) break :spdefault_stack_protector_buffer_size;
1376+
break :sp0;
13721377
};
1373-
if (stack_protector!=0) {
1374-
if (!target_util.supportsStackProtector(options.target))
1375-
returnerror.StackProtectorUnsupportedByTarget;
1376-
if (!capable_of_building_ssp)
1377-
returnerror.StackProtectorUnsupportedByBackend;
1378-
if (!link_libc)
1379-
returnerror.StackProtectorUnavailableWithoutLibC;
1380-
}
13811378

13821379
constinclude_compiler_rt=options.want_compiler_rtorelse
13831380
(!options.skip_linker_dependenciesandis_exe_or_dyn_lib);
@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
21952192
comp.job_queued_compiler_rt_obj=true;
21962193
}
21972194
}
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!=0and
2207-
(!comp.bin_file.options.link_libcor
2208-
!target_util.libcProvidesStackProtector(target)))
2209-
{
2210-
trycomp.work_queue.writeItem(.{ .libssp= {} });
2211-
}
22122195

2213-
if (!comp.bin_file.options.link_libcandcapable_of_building_zig_libc) {
2214-
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
2215-
}
2196+
if (!comp.bin_file.options.skip_linker_dependenciesandis_exe_or_dyn_liband
2197+
!comp.bin_file.options.link_libcandcapable_of_building_zig_libc)
2198+
{
2199+
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
22162200
}
22172201
}
22182202

@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
22582242
if (self.compiler_rt_obj) |*crt_file| {
22592243
crt_file.deinit(gpa);
22602244
}
2261-
if (self.libssp_static_lib) |*crt_file| {
2262-
crt_file.deinit(gpa);
2263-
}
22642245
if (self.libc_static_lib) |*crt_file| {
22652246
crt_file.deinit(gpa);
22662247
}
@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
40274008
);
40284009
};
40294010
},
4030-
.libssp=> {
4031-
constnamed_frame=tracy.namedFrame("libssp");
4032-
defernamed_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=>returnerror.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-
},
40504011
.zig_libc=> {
40514012
constnamed_frame=tracy.namedFrame("zig_libc");
40524013
defernamed_frame.end();
@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
65316492
};
65326493
}
65336494

6534-
fncanBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535-
switch (target.os.tag) {
6536-
.plan9=>returnfalse,
6537-
else=> {},
6538-
}
6539-
switch (target.cpu.arch) {
6540-
.spirv32, .spirv64=>returnfalse,
6541-
else=> {},
6542-
}
6543-
returnswitch (zigBackend(target, use_llvm)) {
6544-
.stage2_llvm=>true,
6545-
else=>build_options.have_llvm,
6546-
};
6547-
}
6548-
65496495
/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
65506496
/// This one builds lib/c.zig.
65516497
fncanBuildZigLibC(target: std.Target, use_llvm: bool) bool {
@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
65856531
};
65866532
}
65876533

6588-
fnneedsCSymbols(
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-
returnfalse;
6597-
6598-
switch (output_mode) {
6599-
.Obj=>returnfalse,
6600-
.Lib=>if (link_mode!=.Dynamic) returnfalse,
6601-
.Exe=> {},
6602-
}
6603-
6604-
// LLVM might generate calls to libc symbols.
6605-
if (zigBackend(target, use_llvm) ==.stage2_llvm)
6606-
returntrue;
6607-
6608-
returnfalse;
6609-
}
6610-
66116534
pubfngenerateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
66126535
consttracy_trace=trace(@src());
66136536
defertracy_trace.end();

‎src/link/Coff/lld.zig‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483483
tryargv.append(lib.full_object_path);
484484
}
485485
}
486-
// MinGW doesn't provide libssp symbols
487-
if (target.abi.isGnu()) {
488-
if (comp.libssp_static_lib) |lib| {
489-
tryargv.append(lib.full_object_path);
490-
}
491-
}
492486
// MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493487
// and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494488
if (comp.compiler_rt_obj) |obj|tryargv.append(obj.full_object_path);

‎src/link/Elf.zig‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10401040
}
10411041
}
10421042

1043-
// stack-protector.
1044-
// Related: https://github.com/ziglang/zig/issues/7265
1045-
if (comp.libssp_static_lib) |ssp| {
1046-
trypositionals.append(.{ .path=ssp.full_object_path });
1047-
}
1048-
10491043
for (positionals.items) |obj| {
10501044
varparse_ctx: ParseErrorCtx= .{ .detected_cpu_arch=undefined };
10511045
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch|err|
@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16891683
}
16901684
}
16911685

1692-
// stack-protector.
1693-
// Related: https://github.com/ziglang/zig/issues/7265
1694-
if (comp.libssp_static_lib) |ssp| {
1695-
tryargv.append(ssp.full_object_path);
1696-
}
1697-
16981686
// Shared libraries.
16991687
// Worst-case, we need an --as-needed argument for every lib, as well
17001688
// as one before and one after.
@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
27292717
}
27302718
}
27312719

2732-
// stack-protector.
2733-
// Related: https://github.com/ziglang/zig/issues/7265
2734-
if (comp.libssp_static_lib) |ssp| {
2735-
tryargv.append(ssp.full_object_path);
2736-
}
2737-
27382720
// Shared libraries.
27392721
if (is_exe_or_dyn_lib) {
27402722
constsystem_libs=self.base.options.system_libs.keys();

‎src/target.zig‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
(target.cpu.arch==.x86ortarget.cpu.arch==.x86_64);
329329
}
330330

331-
pubfnsupportsStackProtector(target: std.Target) bool {
332-
return!target.isSpirV();
331+
pubfnsupportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332+
switch (target.os.tag) {
333+
.plan9=>returnfalse,
334+
else=> {},
335+
}
336+
switch (target.cpu.arch) {
337+
.spirv32, .spirv64=>returnfalse,
338+
else=> {},
339+
}
340+
returnswitch (backend) {
341+
.stage2_llvm=>true,
342+
else=>false,
343+
};
333344
}
334345

335346
pubfnlibcProvidesStackProtector(target: std.Target) bool {

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 53f74d6

Browse files
committed
move libssp into libcompiler_rt
closes#7265
1 parent 2a81a0f commit 53f74d6

6 files changed

Lines changed: 60 additions & 148 deletions

File tree

‎lib/compiler_rt.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,6 @@ comptime {
233233
_=@import("compiler_rt/memmove.zig");
234234
_=@import("compiler_rt/memcmp.zig");
235235
_=@import("compiler_rt/bcmp.zig");
236+
_=@import("compiler_rt/ssp.zig");
236237
}
237238
}

lib/ssp.zig renamed to lib/compiler_rt/ssp.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,44 @@
1313
//! - __vsprintf_chk
1414

1515
conststd=@import("std");
16+
constcommon=@import("./common.zig");
17+
constbuiltin=@import("builtin");
1618

1719
externfnstrncpy(dest: [*:0]u8, src: [*:0]constu8, n: usize) callconv(.C) [*:0]u8;
1820
externfnmemset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
1921
externfnmemcpy(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2022
externfnmemmove(dest: ?[*]u8, src: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2123

22-
// Avoid dragging in the runtime safety mechanisms into this .o file.
23-
pubfnpanic(msg: []constu8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24-
_=msg;
25-
_=error_return_trace;
26-
@setCold(true);
27-
std.os.abort();
24+
comptime {
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 });
2835
}
2936

30-
exportfn__stack_chk_fail() callconv(.C) noreturn {
37+
fn__stack_chk_fail() callconv(.C) noreturn {
3138
@panic("stack smashing detected");
3239
}
3340

34-
exportfn__chk_fail() callconv(.C) noreturn {
41+
fn__chk_fail() callconv(.C) noreturn {
3542
@panic("buffer overflow detected");
3643
}
3744

38-
// Emitted when targeting some architectures (eg. x86)
39-
// XXX: This symbol should be hidden
40-
exportfn__stack_chk_fail_local() callconv(.C) noreturn {
41-
__stack_chk_fail();
42-
}
43-
44-
// XXX: Initialize the canary with random data
45-
exportvar__stack_chk_guard: usize=blk: {
45+
// TODO: Initialize the canary with random data
46+
var__stack_chk_guard: usize=blk: {
4647
varbuf= [1]u8{0} **@sizeOf(usize);
4748
buf[@sizeOf(usize) -1] =255;
4849
buf[@sizeOf(usize) -2] ='\n';
4950
break :blk@as(usize, @bitCast(buf));
5051
};
5152

52-
exportfn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
53+
fn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
5354
@setRuntimeSafety(false);
5455

5556
vari: usize=0;
@@ -64,12 +65,12 @@ export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
6465
returndest;
6566
}
6667

67-
exportfn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
fn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
6869
if (dest_n<n) __chk_fail();
6970
returnstrncpy(dest, src, n);
7071
}
7172

72-
exportfn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
73+
fn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
7374
@setRuntimeSafety(false);
7475

7576
varavail=dest_n;
@@ -94,7 +95,7 @@ export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
9495
returndest;
9596
}
9697

97-
exportfn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98+
fn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
9899
@setRuntimeSafety(false);
99100

100101
varavail=dest_n;
@@ -119,17 +120,17 @@ export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usi
119120
returndest;
120121
}
121122

122-
exportfn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123+
fn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123124
if (dest_n<n) __chk_fail();
124125
returnmemcpy(dest, src, n);
125126
}
126127

127-
exportfn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128+
fn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128129
if (dest_n<n) __chk_fail();
129130
returnmemmove(dest, src, n);
130131
}
131132

132-
exportfn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133+
fn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133134
if (dest_n<n) __chk_fail();
134135
returnmemset(dest, c, n);
135136
}

‎src/Compilation.zig‎

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152152
/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153153
/// and resolved before calling linker.flush().
154154
tsan_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().
157-
libssp_static_lib: ?CRTFile=null,
158155
/// Populated when we build the libc static library. A Job to build this is placed in the queue
159156
/// and resolved before calling linker.flush().
160157
libc_static_lib: ?CRTFile=null,
@@ -286,7 +283,6 @@ const Job = union(enum) {
286283
libcxx: void,
287284
libcxxabi: void,
288285
libtsan: void,
289-
libssp: void,
290286
/// needed when not linking libc and using LLVM for code generation because it generates
291287
/// calls to, for example, memcpy and memset.
292288
zig_libc: void,
@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683679
libtsan,
684680
wasi_libc_crt_file,
685681
compiler_rt,
686-
libssp,
687682
zig_libc,
688683
analyze_mod,
689684

@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13531348
if (stack_checkand!target_util.supportsStackProbing(options.target))
13541349
returnerror.StackCheckUnsupportedByTarget;
13551350

1356-
constcapable_of_building_ssp=canBuildLibSsp(options.target, use_llvm);
1357-
1358-
conststack_protector: u32=options.want_stack_protectororelseb: {
1359-
if (!target_util.supportsStackProtector(options.target)) break :b@as(u32, 0);
1351+
conststack_protector: u32=sp: {
1352+
constzig_backend=zigBackend(options.target, use_llvm);
1353+
if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1354+
if (options.want_stack_protector) |x| {
1355+
if (x>0) returnerror.StackProtectorUnsupportedByTarget;
1356+
}
1357+
break :sp0;
1358+
}
13601359

13611360
// This logic is checking for linking libc because otherwise our start code
13621361
// 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 {
13651364
// as being exempt from stack protection checks, we could change this logic
13661365
// to supporting stack protection even when not linking libc.
13671366
// TODO file issue about this
1368-
if (!link_libc) break :b0;
1369-
if (!capable_of_building_ssp) break :b0;
1370-
if (is_safe_mode) break :bdefault_stack_protector_buffer_size;
1371-
break :b0;
1367+
if (!link_libc) {
1368+
if (options.want_stack_protector) |x| {
1369+
if (x>0) returnerror.StackProtectorUnavailableWithoutLibC;
1370+
}
1371+
break :sp0;
1372+
}
1373+
1374+
if (options.want_stack_protector) |x|break :spx;
1375+
if (is_safe_mode) break :spdefault_stack_protector_buffer_size;
1376+
break :sp0;
13721377
};
1373-
if (stack_protector!=0) {
1374-
if (!target_util.supportsStackProtector(options.target))
1375-
returnerror.StackProtectorUnsupportedByTarget;
1376-
if (!capable_of_building_ssp)
1377-
returnerror.StackProtectorUnsupportedByBackend;
1378-
if (!link_libc)
1379-
returnerror.StackProtectorUnavailableWithoutLibC;
1380-
}
13811378

13821379
constinclude_compiler_rt=options.want_compiler_rtorelse
13831380
(!options.skip_linker_dependenciesandis_exe_or_dyn_lib);
@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
21952192
comp.job_queued_compiler_rt_obj=true;
21962193
}
21972194
}
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!=0and
2207-
(!comp.bin_file.options.link_libcor
2208-
!target_util.libcProvidesStackProtector(target)))
2209-
{
2210-
trycomp.work_queue.writeItem(.{ .libssp= {} });
2211-
}
22122195

2213-
if (!comp.bin_file.options.link_libcandcapable_of_building_zig_libc) {
2214-
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
2215-
}
2196+
if (!comp.bin_file.options.skip_linker_dependenciesandis_exe_or_dyn_liband
2197+
!comp.bin_file.options.link_libcandcapable_of_building_zig_libc)
2198+
{
2199+
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
22162200
}
22172201
}
22182202

@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
22582242
if (self.compiler_rt_obj) |*crt_file| {
22592243
crt_file.deinit(gpa);
22602244
}
2261-
if (self.libssp_static_lib) |*crt_file| {
2262-
crt_file.deinit(gpa);
2263-
}
22642245
if (self.libc_static_lib) |*crt_file| {
22652246
crt_file.deinit(gpa);
22662247
}
@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
40274008
);
40284009
};
40294010
},
4030-
.libssp=> {
4031-
constnamed_frame=tracy.namedFrame("libssp");
4032-
defernamed_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=>returnerror.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-
},
40504011
.zig_libc=> {
40514012
constnamed_frame=tracy.namedFrame("zig_libc");
40524013
defernamed_frame.end();
@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
65316492
};
65326493
}
65336494

6534-
fncanBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535-
switch (target.os.tag) {
6536-
.plan9=>returnfalse,
6537-
else=> {},
6538-
}
6539-
switch (target.cpu.arch) {
6540-
.spirv32, .spirv64=>returnfalse,
6541-
else=> {},
6542-
}
6543-
returnswitch (zigBackend(target, use_llvm)) {
6544-
.stage2_llvm=>true,
6545-
else=>build_options.have_llvm,
6546-
};
6547-
}
6548-
65496495
/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
65506496
/// This one builds lib/c.zig.
65516497
fncanBuildZigLibC(target: std.Target, use_llvm: bool) bool {
@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
65856531
};
65866532
}
65876533

6588-
fnneedsCSymbols(
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-
returnfalse;
6597-
6598-
switch (output_mode) {
6599-
.Obj=>returnfalse,
6600-
.Lib=>if (link_mode!=.Dynamic) returnfalse,
6601-
.Exe=> {},
6602-
}
6603-
6604-
// LLVM might generate calls to libc symbols.
6605-
if (zigBackend(target, use_llvm) ==.stage2_llvm)
6606-
returntrue;
6607-
6608-
returnfalse;
6609-
}
6610-
66116534
pubfngenerateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
66126535
consttracy_trace=trace(@src());
66136536
defertracy_trace.end();

‎src/link/Coff/lld.zig‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483483
tryargv.append(lib.full_object_path);
484484
}
485485
}
486-
// MinGW doesn't provide libssp symbols
487-
if (target.abi.isGnu()) {
488-
if (comp.libssp_static_lib) |lib| {
489-
tryargv.append(lib.full_object_path);
490-
}
491-
}
492486
// MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493487
// and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494488
if (comp.compiler_rt_obj) |obj|tryargv.append(obj.full_object_path);

‎src/link/Elf.zig‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10401040
}
10411041
}
10421042

1043-
// stack-protector.
1044-
// Related: https://github.com/ziglang/zig/issues/7265
1045-
if (comp.libssp_static_lib) |ssp| {
1046-
trypositionals.append(.{ .path=ssp.full_object_path });
1047-
}
1048-
10491043
for (positionals.items) |obj| {
10501044
varparse_ctx: ParseErrorCtx= .{ .detected_cpu_arch=undefined };
10511045
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch|err|
@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16891683
}
16901684
}
16911685

1692-
// stack-protector.
1693-
// Related: https://github.com/ziglang/zig/issues/7265
1694-
if (comp.libssp_static_lib) |ssp| {
1695-
tryargv.append(ssp.full_object_path);
1696-
}
1697-
16981686
// Shared libraries.
16991687
// Worst-case, we need an --as-needed argument for every lib, as well
17001688
// as one before and one after.
@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
27292717
}
27302718
}
27312719

2732-
// stack-protector.
2733-
// Related: https://github.com/ziglang/zig/issues/7265
2734-
if (comp.libssp_static_lib) |ssp| {
2735-
tryargv.append(ssp.full_object_path);
2736-
}
2737-
27382720
// Shared libraries.
27392721
if (is_exe_or_dyn_lib) {
27402722
constsystem_libs=self.base.options.system_libs.keys();

‎src/target.zig‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
(target.cpu.arch==.x86ortarget.cpu.arch==.x86_64);
329329
}
330330

331-
pubfnsupportsStackProtector(target: std.Target) bool {
332-
return!target.isSpirV();
331+
pubfnsupportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332+
switch (target.os.tag) {
333+
.plan9=>returnfalse,
334+
else=> {},
335+
}
336+
switch (target.cpu.arch) {
337+
.spirv32, .spirv64=>returnfalse,
338+
else=> {},
339+
}
340+
returnswitch (backend) {
341+
.stage2_llvm=>true,
342+
else=>false,
343+
};
333344
}
334345

335346
pubfnlibcProvidesStackProtector(target: std.Target) bool {

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 53f74d6

Browse files
committed
move libssp into libcompiler_rt
closes#7265
1 parent 2a81a0f commit 53f74d6

6 files changed

Lines changed: 60 additions & 148 deletions

File tree

‎lib/compiler_rt.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,6 @@ comptime {
233233
_=@import("compiler_rt/memmove.zig");
234234
_=@import("compiler_rt/memcmp.zig");
235235
_=@import("compiler_rt/bcmp.zig");
236+
_=@import("compiler_rt/ssp.zig");
236237
}
237238
}

lib/ssp.zig renamed to lib/compiler_rt/ssp.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,44 @@
1313
//! - __vsprintf_chk
1414

1515
conststd=@import("std");
16+
constcommon=@import("./common.zig");
17+
constbuiltin=@import("builtin");
1618

1719
externfnstrncpy(dest: [*:0]u8, src: [*:0]constu8, n: usize) callconv(.C) [*:0]u8;
1820
externfnmemset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
1921
externfnmemcpy(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2022
externfnmemmove(dest: ?[*]u8, src: ?[*]constu8, n: usize) callconv(.C) ?[*]u8;
2123

22-
// Avoid dragging in the runtime safety mechanisms into this .o file.
23-
pubfnpanic(msg: []constu8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24-
_=msg;
25-
_=error_return_trace;
26-
@setCold(true);
27-
std.os.abort();
24+
comptime {
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 });
2835
}
2936

30-
exportfn__stack_chk_fail() callconv(.C) noreturn {
37+
fn__stack_chk_fail() callconv(.C) noreturn {
3138
@panic("stack smashing detected");
3239
}
3340

34-
exportfn__chk_fail() callconv(.C) noreturn {
41+
fn__chk_fail() callconv(.C) noreturn {
3542
@panic("buffer overflow detected");
3643
}
3744

38-
// Emitted when targeting some architectures (eg. x86)
39-
// XXX: This symbol should be hidden
40-
exportfn__stack_chk_fail_local() callconv(.C) noreturn {
41-
__stack_chk_fail();
42-
}
43-
44-
// XXX: Initialize the canary with random data
45-
exportvar__stack_chk_guard: usize=blk: {
45+
// TODO: Initialize the canary with random data
46+
var__stack_chk_guard: usize=blk: {
4647
varbuf= [1]u8{0} **@sizeOf(usize);
4748
buf[@sizeOf(usize) -1] =255;
4849
buf[@sizeOf(usize) -2] ='\n';
4950
break :blk@as(usize, @bitCast(buf));
5051
};
5152

52-
exportfn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
53+
fn__strcpy_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
5354
@setRuntimeSafety(false);
5455

5556
vari: usize=0;
@@ -64,12 +65,12 @@ export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
6465
returndest;
6566
}
6667

67-
exportfn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
fn__strncpy_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
6869
if (dest_n<n) __chk_fail();
6970
returnstrncpy(dest, src, n);
7071
}
7172

72-
exportfn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
73+
fn__strcat_chk(dest: [*:0]u8, src: [*:0]constu8, dest_n: usize) callconv(.C) [*:0]u8 {
7374
@setRuntimeSafety(false);
7475

7576
varavail=dest_n;
@@ -94,7 +95,7 @@ export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
9495
returndest;
9596
}
9697

97-
exportfn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98+
fn__strncat_chk(dest: [*:0]u8, src: [*:0]constu8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
9899
@setRuntimeSafety(false);
99100

100101
varavail=dest_n;
@@ -119,17 +120,17 @@ export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usi
119120
returndest;
120121
}
121122

122-
exportfn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123+
fn__memcpy_chk(noaliasdest: ?[*]u8, noaliassrc: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123124
if (dest_n<n) __chk_fail();
124125
returnmemcpy(dest, src, n);
125126
}
126127

127-
exportfn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128+
fn__memmove_chk(dest: ?[*]u8, src: ?[*]constu8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128129
if (dest_n<n) __chk_fail();
129130
returnmemmove(dest, src, n);
130131
}
131132

132-
exportfn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133+
fn__memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133134
if (dest_n<n) __chk_fail();
134135
returnmemset(dest, c, n);
135136
}

‎src/Compilation.zig‎

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152152
/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153153
/// and resolved before calling linker.flush().
154154
tsan_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().
157-
libssp_static_lib: ?CRTFile=null,
158155
/// Populated when we build the libc static library. A Job to build this is placed in the queue
159156
/// and resolved before calling linker.flush().
160157
libc_static_lib: ?CRTFile=null,
@@ -286,7 +283,6 @@ const Job = union(enum) {
286283
libcxx: void,
287284
libcxxabi: void,
288285
libtsan: void,
289-
libssp: void,
290286
/// needed when not linking libc and using LLVM for code generation because it generates
291287
/// calls to, for example, memcpy and memset.
292288
zig_libc: void,
@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683679
libtsan,
684680
wasi_libc_crt_file,
685681
compiler_rt,
686-
libssp,
687682
zig_libc,
688683
analyze_mod,
689684

@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13531348
if (stack_checkand!target_util.supportsStackProbing(options.target))
13541349
returnerror.StackCheckUnsupportedByTarget;
13551350

1356-
constcapable_of_building_ssp=canBuildLibSsp(options.target, use_llvm);
1357-
1358-
conststack_protector: u32=options.want_stack_protectororelseb: {
1359-
if (!target_util.supportsStackProtector(options.target)) break :b@as(u32, 0);
1351+
conststack_protector: u32=sp: {
1352+
constzig_backend=zigBackend(options.target, use_llvm);
1353+
if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1354+
if (options.want_stack_protector) |x| {
1355+
if (x>0) returnerror.StackProtectorUnsupportedByTarget;
1356+
}
1357+
break :sp0;
1358+
}
13601359

13611360
// This logic is checking for linking libc because otherwise our start code
13621361
// 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 {
13651364
// as being exempt from stack protection checks, we could change this logic
13661365
// to supporting stack protection even when not linking libc.
13671366
// TODO file issue about this
1368-
if (!link_libc) break :b0;
1369-
if (!capable_of_building_ssp) break :b0;
1370-
if (is_safe_mode) break :bdefault_stack_protector_buffer_size;
1371-
break :b0;
1367+
if (!link_libc) {
1368+
if (options.want_stack_protector) |x| {
1369+
if (x>0) returnerror.StackProtectorUnavailableWithoutLibC;
1370+
}
1371+
break :sp0;
1372+
}
1373+
1374+
if (options.want_stack_protector) |x|break :spx;
1375+
if (is_safe_mode) break :spdefault_stack_protector_buffer_size;
1376+
break :sp0;
13721377
};
1373-
if (stack_protector!=0) {
1374-
if (!target_util.supportsStackProtector(options.target))
1375-
returnerror.StackProtectorUnsupportedByTarget;
1376-
if (!capable_of_building_ssp)
1377-
returnerror.StackProtectorUnsupportedByBackend;
1378-
if (!link_libc)
1379-
returnerror.StackProtectorUnavailableWithoutLibC;
1380-
}
13811378

13821379
constinclude_compiler_rt=options.want_compiler_rtorelse
13831380
(!options.skip_linker_dependenciesandis_exe_or_dyn_lib);
@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
21952192
comp.job_queued_compiler_rt_obj=true;
21962193
}
21972194
}
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!=0and
2207-
(!comp.bin_file.options.link_libcor
2208-
!target_util.libcProvidesStackProtector(target)))
2209-
{
2210-
trycomp.work_queue.writeItem(.{ .libssp= {} });
2211-
}
22122195

2213-
if (!comp.bin_file.options.link_libcandcapable_of_building_zig_libc) {
2214-
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
2215-
}
2196+
if (!comp.bin_file.options.skip_linker_dependenciesandis_exe_or_dyn_liband
2197+
!comp.bin_file.options.link_libcandcapable_of_building_zig_libc)
2198+
{
2199+
trycomp.work_queue.writeItem(.{ .zig_libc= {} });
22162200
}
22172201
}
22182202

@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
22582242
if (self.compiler_rt_obj) |*crt_file| {
22592243
crt_file.deinit(gpa);
22602244
}
2261-
if (self.libssp_static_lib) |*crt_file| {
2262-
crt_file.deinit(gpa);
2263-
}
22642245
if (self.libc_static_lib) |*crt_file| {
22652246
crt_file.deinit(gpa);
22662247
}
@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
40274008
);
40284009
};
40294010
},
4030-
.libssp=> {
4031-
constnamed_frame=tracy.namedFrame("libssp");
4032-
defernamed_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=>returnerror.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-
},
40504011
.zig_libc=> {
40514012
constnamed_frame=tracy.namedFrame("zig_libc");
40524013
defernamed_frame.end();
@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
65316492
};
65326493
}
65336494

6534-
fncanBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535-
switch (target.os.tag) {
6536-
.plan9=>returnfalse,
6537-
else=> {},
6538-
}
6539-
switch (target.cpu.arch) {
6540-
.spirv32, .spirv64=>returnfalse,
6541-
else=> {},
6542-
}
6543-
returnswitch (zigBackend(target, use_llvm)) {
6544-
.stage2_llvm=>true,
6545-
else=>build_options.have_llvm,
6546-
};
6547-
}
6548-
65496495
/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
65506496
/// This one builds lib/c.zig.
65516497
fncanBuildZigLibC(target: std.Target, use_llvm: bool) bool {
@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
65856531
};
65866532
}
65876533

6588-
fnneedsCSymbols(
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-
returnfalse;
6597-
6598-
switch (output_mode) {
6599-
.Obj=>returnfalse,
6600-
.Lib=>if (link_mode!=.Dynamic) returnfalse,
6601-
.Exe=> {},
6602-
}
6603-
6604-
// LLVM might generate calls to libc symbols.
6605-
if (zigBackend(target, use_llvm) ==.stage2_llvm)
6606-
returntrue;
6607-
6608-
returnfalse;
6609-
}
6610-
66116534
pubfngenerateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
66126535
consttracy_trace=trace(@src());
66136536
defertracy_trace.end();

‎src/link/Coff/lld.zig‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483483
tryargv.append(lib.full_object_path);
484484
}
485485
}
486-
// MinGW doesn't provide libssp symbols
487-
if (target.abi.isGnu()) {
488-
if (comp.libssp_static_lib) |lib| {
489-
tryargv.append(lib.full_object_path);
490-
}
491-
}
492486
// MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493487
// and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494488
if (comp.compiler_rt_obj) |obj|tryargv.append(obj.full_object_path);

‎src/link/Elf.zig‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10401040
}
10411041
}
10421042

1043-
// stack-protector.
1044-
// Related: https://github.com/ziglang/zig/issues/7265
1045-
if (comp.libssp_static_lib) |ssp| {
1046-
trypositionals.append(.{ .path=ssp.full_object_path });
1047-
}
1048-
10491043
for (positionals.items) |obj| {
10501044
varparse_ctx: ParseErrorCtx= .{ .detected_cpu_arch=undefined };
10511045
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch|err|
@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16891683
}
16901684
}
16911685

1692-
// stack-protector.
1693-
// Related: https://github.com/ziglang/zig/issues/7265
1694-
if (comp.libssp_static_lib) |ssp| {
1695-
tryargv.append(ssp.full_object_path);
1696-
}
1697-
16981686
// Shared libraries.
16991687
// Worst-case, we need an --as-needed argument for every lib, as well
17001688
// as one before and one after.
@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
27292717
}
27302718
}
27312719

2732-
// stack-protector.
2733-
// Related: https://github.com/ziglang/zig/issues/7265
2734-
if (comp.libssp_static_lib) |ssp| {
2735-
tryargv.append(ssp.full_object_path);
2736-
}
2737-
27382720
// Shared libraries.
27392721
if (is_exe_or_dyn_lib) {
27402722
constsystem_libs=self.base.options.system_libs.keys();

‎src/target.zig‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
(target.cpu.arch==.x86ortarget.cpu.arch==.x86_64);
329329
}
330330

331-
pubfnsupportsStackProtector(target: std.Target) bool {
332-
return!target.isSpirV();
331+
pubfnsupportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332+
switch (target.os.tag) {
333+
.plan9=>returnfalse,
334+
else=> {},
335+
}
336+
switch (target.cpu.arch) {
337+
.spirv32, .spirv64=>returnfalse,
338+
else=> {},
339+
}
340+
returnswitch (backend) {
341+
.stage2_llvm=>true,
342+
else=>false,
343+
};
333344
}
334345

335346
pubfnlibcProvidesStackProtector(target: std.Target) bool {

0 commit comments

Comments
 (0)