authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-21 14:21:01+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-28 03:10:43+02:00
log31069984126ebd98b13aa3ef583871c5bd66d03f
tree874dbc07628dc775f2e9d27b4cf01b1819a6415d
parent338730d2fa1189dc9fad53bbf7be77352af83b2d
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

compiler_rt: Implement __chkstk() for thumb-windows-gnu.

https://github.com/llvm/llvm-project/blob/ad435bcc14f42dc97286c717cd12446a0facb2ee/compiler-rt/lib/builtins/arm/chkstk.S

1 files changed, 32 insertions(+), 21 deletions(-)

lib/compiler_rt/stack_probe.zig+32-21
......@@ -5,9 +5,6 @@ const arch = builtin.cpu.arch;
55const abi = builtin.abi;
66const is_test = builtin.is_test;
77
8const is_gnu = abi.isGnu();
9const is_mingw = os_tag == .windows and is_gnu;
10
118const linkage: std.builtin.GlobalLinkage = if (builtin.is_test) .internal else .weak;
129const strong_linkage: std.builtin.GlobalLinkage = if (builtin.is_test) .internal else .strong;
1310pub const panic = @import("common.zig").panic;
......@@ -15,11 +12,11 @@ pub const panic = @import("common.zig").panic;
1512comptime {
1613 if (builtin.os.tag == .windows) {
1714 // Default stack-probe functions emitted by LLVM
18 if (is_mingw) {
15 if (builtin.target.isMinGW()) {
1916 @export(&_chkstk, .{ .name = "_alloca", .linkage = linkage });
2017 @export(&___chkstk_ms, .{ .name = "___chkstk_ms", .linkage = linkage });
2118
22 if (arch.isAARCH64()) {
19 if (arch == .thumb or arch == .aarch64) {
2320 @export(&__chkstk, .{ .name = "__chkstk", .linkage = linkage });
2421 }
2522 } else if (!builtin.link_libc) {
......@@ -100,6 +97,35 @@ fn win_probe_stack_only() void {
10097 @setRuntimeSafety(false);
10198
10299 switch (arch) {
100 .thumb => {
101 asm volatile (
102 \\ lsl r4, r4, #2
103 \\ mov r12, sp
104 \\ push {r5, r6}
105 \\ mov r5, r4
106 \\1:
107 \\ sub r12, r12, #4096
108 \\ subs r5, r5, #4096
109 \\ ldr r6, [r12]
110 \\ bgt 1b
111 \\ pop {r5, r6}
112 \\ bx lr
113 );
114 },
115 .aarch64 => {
116 asm volatile (
117 \\ lsl x16, x15, #4
118 \\ mov x17, sp
119 \\1:
120 \\
121 \\ sub x17, x17, 4096
122 \\ subs x16, x16, 4096
123 \\ ldr xzr, [x17]
124 \\ b.gt 1b
125 \\
126 \\ ret
127 );
128 },
103129 .x86_64 => {
104130 asm volatile (
105131 \\ push %%rcx
......@@ -144,21 +170,6 @@ fn win_probe_stack_only() void {
144170 },
145171 else => {},
146172 }
147 if (comptime arch.isAARCH64()) {
148 // NOTE: page size hardcoded to 4096 for now
149 asm volatile (
150 \\ lsl x16, x15, #4
151 \\ mov x17, sp
152 \\1:
153 \\
154 \\ sub x17, x17, 4096
155 \\ subs x16, x16, 4096
156 \\ ldr xzr, [x17]
157 \\ b.gt 1b
158 \\
159 \\ ret
160 );
161 }
162173
163174 unreachable;
164175}
......@@ -240,7 +251,7 @@ pub fn _chkstk() callconv(.Naked) void {
240251}
241252pub fn __chkstk() callconv(.Naked) void {
242253 @setRuntimeSafety(false);
243 if (comptime arch.isAARCH64()) {
254 if (arch == .thumb or arch == .aarch64) {
244255 @call(.always_inline, win_probe_stack_only, .{});
245256 } else switch (arch) {
246257 .x86 => @call(.always_inline, win_probe_stack_adjust_sp, .{}),