From f10b226c7703f08b39f21d8739768818c4d57d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 27 Aug 2024 08:03:35 +0200 Subject: [PATCH 1/4] Compilation: Pass -fPIC for assembly files too, not just C files. There are targets (e.g. MIPS) where PIC actually affects assembler behavior. --- src/Compilation.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 3d40957a72f84a1244b4fe307415ee20256fc29d..4d4f1527a23e9a11ad6b8ab803ff523e5116ad53 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -5636,10 +5636,6 @@ pub fn addCCArgs( try argv.append("-Werror=date-time"); } - if (target_util.supports_fpic(target) and mod.pic) { - try argv.append("-fPIC"); - } - if (mod.unwind_tables) { try argv.append("-funwind-tables"); } else { @@ -5730,6 +5726,10 @@ pub fn addCCArgs( }, } + if (target_util.supports_fpic(target) and mod.pic) { + try argv.append("-fPIC"); + } + try argv.ensureUnusedCapacity(2); switch (comp.config.debug_format) { .strip => {}, -- 2.54.0 From 2de7296262b88f3d19ad9cb75f824bf69350c717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 27 Aug 2024 08:09:47 +0200 Subject: [PATCH 2/4] Compilation: Pass -mthumb for assembly files too, not just C files. --- src/Compilation.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 4d4f1527a23e9a11ad6b8ab803ff523e5116ad53..54fa1208569fcd9abe4bc1c6d9f50fb0a4ffdf58 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -5536,10 +5536,6 @@ pub fn addCCArgs( else => {}, } - if (target.cpu.arch.isThumb()) { - try argv.append("-mthumb"); - } - { var san_arg: std.ArrayListUnmanaged(u8) = .{}; const prefix = "-fsanitize="; @@ -5726,6 +5722,10 @@ pub fn addCCArgs( }, } + if (target.cpu.arch.isThumb()) { + try argv.append("-mthumb"); + } + if (target_util.supports_fpic(target) and mod.pic) { try argv.append("-fPIC"); } -- 2.54.0 From f021ad548fd8ad6c7d8d8ea7e7c409c695dce1e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 28 Aug 2024 10:27:29 +0200 Subject: [PATCH 3/4] musl: Build with -fno-builtin and -mimplicit-it=always (for thumb) like upstream. --- src/musl.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/musl.zig b/src/musl.zig index 1c20c209687bc1079f55cc1085fb46777261f1cf..acf59020334909a17a97f116405c2ca18efd8228 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -384,6 +384,7 @@ fn addCcArgs( try args.appendSlice(&[_][]const u8{ "-std=c99", "-ffreestanding", + "-fno-builtin", "-fexcess-precision=standard", "-frounding-math", "-fno-strict-aliasing", @@ -422,6 +423,10 @@ fn addCcArgs( "-Qunused-arguments", "-w", // disable all warnings }); + + if (target.cpu.arch.isThumb()) { + try args.append("-mimplicit-it=always"); + } } fn start_asm_path(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 { -- 2.54.0 From 0ecc6332b4eb1ced547ffa38f57471134aaa4d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 29 Aug 2024 16:07:46 +0200 Subject: [PATCH 4/4] start: Fix arm stack alignment code to work for thumb too. --- lib/std/start.zig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/std/start.zig b/lib/std/start.zig index ea6f347bd6658ff9b99885e12baaa1f2d292664f..5c94a4b591b885109c14f9dbb5406f1169cfd1ad 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -283,7 +283,9 @@ fn _start() callconv(.Naked) noreturn { \\ mov fp, #0 \\ mov lr, #0 \\ mov a1, sp - \\ and sp, #-16 + \\ mov ip, sp + \\ and ip, ip, #-16 + \\ mov sp, ip \\ b %[posixCallMainAndExit] , .csky => -- 2.54.0