From 56b416662aa89196d5b974632dcd927d26b5d12c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 13:04:59 +0100 Subject: [PATCH 01/17] compiler: Update clangMightShellOutForAssembly() for Clang 19. Clang only uses the system assembler for nvptx and xcore nowadays. --- src/target.zig | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/target.zig b/src/target.zig index aba1fffcbc..96cc36c9c3 100644 --- a/src/target.zig +++ b/src/target.zig @@ -327,9 +327,8 @@ pub fn libcFullLinkFlags(target: std.Target) []const []const u8 { } pub fn clangMightShellOutForAssembly(target: std.Target) bool { - // Clang defaults to using the system assembler over the internal one - // when targeting a non-BSD OS. - return target.cpu.arch.isSPARC(); + // Clang defaults to using the system assembler in some cases. + return target.cpu.arch.isNvptx() or target.cpu.arch == .xcore; } /// Each backend architecture in Clang has a different codepath which may or may not From b57819118ddf287a135b4c2fb7182615b402fbc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 14:29:44 +0100 Subject: [PATCH 02/17] Compilation: Move no_builtin to Package.Module. This option, by its very nature, needs to be attached to a module. If it isn't, the code in a module could break at random when compiled into an application that doesn't have this option set. After this change, skip_linker_dependencies no longer implies no_builtin in the LLVM backend. --- src/Compilation.zig | 22 +++++++++++++--------- src/Package/Module.zig | 12 ++++++++++++ src/codegen/llvm.zig | 6 ++---- src/glibc.zig | 8 ++++---- src/main.zig | 10 ++++------ src/mingw.zig | 6 +++--- src/musl.zig | 27 ++++++++++++++++++++------- src/wasi_libc.zig | 14 +++++++------- 8 files changed, 65 insertions(+), 40 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 51877d7973..24ee899610 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -89,7 +89,6 @@ windows_libs: std.StringArrayHashMapUnmanaged(void), version: ?std.SemanticVersion, libc_installation: ?*const LibCInstallation, skip_linker_dependencies: bool, -no_builtin: bool, function_sections: bool, data_sections: bool, link_eh_frame_hdr: bool, @@ -852,6 +851,7 @@ pub const cache_helpers = struct { hh.add(mod.fuzz); hh.add(mod.unwind_tables); hh.add(mod.structured_cfg); + hh.add(mod.no_builtin); hh.addListOfBytes(mod.cc_argv); } @@ -1057,7 +1057,6 @@ pub const CreateOptions = struct { want_lto: ?bool = null, function_sections: bool = false, data_sections: bool = false, - no_builtin: bool = false, time_report: bool = false, stack_report: bool = false, link_eh_frame_hdr: bool = false, @@ -1353,7 +1352,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil cache.hash.add(options.config.link_mode); cache.hash.add(options.function_sections); cache.hash.add(options.data_sections); - cache.hash.add(options.no_builtin); cache.hash.add(link_libc); cache.hash.add(options.config.link_libcpp); cache.hash.add(options.config.link_libunwind); @@ -1490,7 +1488,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil .framework_dirs = options.framework_dirs, .llvm_opt_bisect_limit = options.llvm_opt_bisect_limit, .skip_linker_dependencies = options.skip_linker_dependencies, - .no_builtin = options.no_builtin, .job_queued_update_builtin_zig = have_zcu, .function_sections = options.function_sections, .data_sections = options.data_sections, @@ -5261,7 +5258,7 @@ pub fn addCCArgs( try argv.append("-fdata-sections"); } - if (comp.no_builtin) { + if (mod.no_builtin) { try argv.append("-fno-builtin"); } @@ -6189,6 +6186,7 @@ fn buildOutputFromZig( .pic = comp.root_mod.pic, .optimize_mode = optimize_mode, .structured_cfg = comp.root_mod.structured_cfg, + .no_builtin = true, .code_model = comp.root_mod.code_model, }, .global = config, @@ -6237,7 +6235,6 @@ fn buildOutputFromZig( }, .function_sections = true, .data_sections = true, - .no_builtin = true, .emit_h = null, .verbose_cc = comp.verbose_cc, .verbose_link = comp.verbose_link, @@ -6262,16 +6259,21 @@ fn buildOutputFromZig( comp.queueLinkTaskMode(crt_file.full_object_path, output_mode); } +pub const CrtFileOptions = struct { + pic: ?bool = null, + no_builtin: ?bool = null, +}; + pub fn build_crt_file( comp: *Compilation, root_name: []const u8, output_mode: std.builtin.OutputMode, - pic: ?bool, misc_task_tag: MiscTask, prog_node: std.Progress.Node, /// These elements have to get mutated to add the owner module after it is /// created within this function. c_source_files: []CSourceFile, + options: CrtFileOptions, ) !void { const tracy_trace = trace(@src()); defer tracy_trace.end(); @@ -6319,10 +6321,12 @@ pub fn build_crt_file( .omit_frame_pointer = comp.root_mod.omit_frame_pointer, .valgrind = false, .unwind_tables = false, - // Some CRT objects (rcrt1.o, Scrt1.o) are opinionated about PIC. - .pic = pic orelse comp.root_mod.pic, + // Some CRT objects (e.g. musl's rcrt1.o and Scrt1.o) are opinionated about PIC. + .pic = options.pic orelse comp.root_mod.pic, .optimize_mode = comp.compilerRtOptMode(), .structured_cfg = comp.root_mod.structured_cfg, + // Some libcs (e.g. musl) are opinionated about -fno-builtin. + .no_builtin = options.no_builtin orelse comp.root_mod.no_builtin, }, .global = config, .cc_argv = &.{}, diff --git a/src/Package/Module.zig b/src/Package/Module.zig index 0a3d754e7a..0e777145d9 100644 --- a/src/Package/Module.zig +++ b/src/Package/Module.zig @@ -31,6 +31,7 @@ unwind_tables: bool, cc_argv: []const []const u8, /// (SPIR-V) whether to generate a structured control flow graph or not structured_cfg: bool, +no_builtin: bool, /// If the module is an `@import("builtin")` module, this is the `File` that /// is preallocated for it. Otherwise this field is null. @@ -95,6 +96,7 @@ pub const CreateOptions = struct { sanitize_thread: ?bool = null, fuzz: ?bool = null, structured_cfg: ?bool = null, + no_builtin: ?bool = null, }; }; @@ -298,6 +300,13 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module { }; }; + const no_builtin = b: { + if (options.inherited.no_builtin) |x| break :b x; + if (options.parent) |p| break :b p.no_builtin; + + break :b target.cpu.arch.isBpf(); + }; + const llvm_cpu_features: ?[*:0]const u8 = b: { if (resolved_target.llvm_cpu_features) |x| break :b x; if (!options.global.use_llvm) break :b null; @@ -350,6 +359,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module { .unwind_tables = unwind_tables, .cc_argv = options.cc_argv, .structured_cfg = structured_cfg, + .no_builtin = no_builtin, .builtin_file = null, }; @@ -442,6 +452,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module { .unwind_tables = unwind_tables, .cc_argv = &.{}, .structured_cfg = structured_cfg, + .no_builtin = no_builtin, .builtin_file = new_file, }; new_file.* = .{ @@ -502,6 +513,7 @@ pub fn createLimited(gpa: Allocator, options: LimitedOptions) Allocator.Error!*P .unwind_tables = undefined, .cc_argv = undefined, .structured_cfg = undefined, + .no_builtin = undefined, .builtin_file = null, }; return mod; diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 3a8fde75da..1b90453d49 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -3222,8 +3222,6 @@ pub const Object = struct { owner_mod: *Package.Module, omit_frame_pointer: bool, ) Allocator.Error!void { - const comp = o.pt.zcu.comp; - if (!owner_mod.red_zone) { try attributes.addFnAttr(.noredzone, &o.builder); } @@ -3242,8 +3240,7 @@ pub const Object = struct { if (owner_mod.unwind_tables) { try attributes.addFnAttr(.{ .uwtable = Builder.Attribute.UwTable.default }, &o.builder); } - const target = owner_mod.resolved_target.result; - if (comp.skip_linker_dependencies or comp.no_builtin or target.cpu.arch.isBpf()) { + if (owner_mod.no_builtin) { // The intent here is for compiler-rt and libc functions to not generate // infinite recursion. For example, if we are compiling the memcpy function, // and llvm detects that the body is equivalent to memcpy, it may replace the @@ -3258,6 +3255,7 @@ pub const Object = struct { try attributes.addFnAttr(.minsize, &o.builder); try attributes.addFnAttr(.optsize, &o.builder); } + const target = owner_mod.resolved_target.result; if (target.cpu.model.llvm_name) |s| { try attributes.addFnAttr(.{ .string = .{ .kind = try o.builder.string("target-cpu"), diff --git a/src/glibc.zig b/src/glibc.zig index 65161cec4e..c7f84a797c 100644 --- a/src/glibc.zig +++ b/src/glibc.zig @@ -221,7 +221,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .owner = comp.root_mod, }, }; - return comp.build_crt_file("crti", .Obj, null, .@"glibc crti.o", prog_node, &files); + return comp.build_crt_file("crti", .Obj, .@"glibc crti.o", prog_node, &files, .{}); }, .crtn_o => { var args = std.ArrayList([]const u8).init(arena); @@ -242,7 +242,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .owner = undefined, }, }; - return comp.build_crt_file("crtn", .Obj, null, .@"glibc crtn.o", prog_node, &files); + return comp.build_crt_file("crtn", .Obj, .@"glibc crtn.o", prog_node, &files, .{}); }, .scrt1_o => { const start_o: Compilation.CSourceFile = blk: { @@ -295,7 +295,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre }; var files = [_]Compilation.CSourceFile{ start_o, abi_note_o, init_o }; const basename = if (comp.config.output_mode == .Exe and !comp.config.pie) "crt1" else "Scrt1"; - return comp.build_crt_file(basename, .Obj, null, .@"glibc Scrt1.o", prog_node, &files); + return comp.build_crt_file(basename, .Obj, .@"glibc Scrt1.o", prog_node, &files, .{}); }, .libc_nonshared_a => { const s = path.sep_str; @@ -413,7 +413,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre files_index += 1; } const files = files_buf[0..files_index]; - return comp.build_crt_file("c_nonshared", .Lib, null, .@"glibc libc_nonshared.a", prog_node, files); + return comp.build_crt_file("c_nonshared", .Lib, .@"glibc libc_nonshared.a", prog_node, files, .{}); }, } } diff --git a/src/main.zig b/src/main.zig index 24fc0aa60c..e77effd443 100644 --- a/src/main.zig +++ b/src/main.zig @@ -810,7 +810,6 @@ fn buildOutputType( var compatibility_version: ?std.SemanticVersion = null; var function_sections = false; var data_sections = false; - var no_builtin = false; var listen: Listen = .none; var debug_compile_errors = false; var verbose_link = (native_os != .wasi or builtin.link_libc) and @@ -1550,9 +1549,9 @@ fn buildOutputType( } else if (mem.eql(u8, arg, "-fno-data-sections")) { data_sections = false; } else if (mem.eql(u8, arg, "-fbuiltin")) { - no_builtin = false; + mod_opts.no_builtin = false; } else if (mem.eql(u8, arg, "-fno-builtin")) { - no_builtin = true; + mod_opts.no_builtin = true; } else if (mem.startsWith(u8, arg, "-fopt-bisect-limit=")) { const next_arg = arg["-fopt-bisect-limit=".len..]; llvm_opt_bisect_limit = std.fmt.parseInt(c_int, next_arg, 0) catch |err| @@ -1963,8 +1962,8 @@ fn buildOutputType( .no_function_sections => function_sections = false, .data_sections => data_sections = true, .no_data_sections => data_sections = false, - .builtin => no_builtin = false, - .no_builtin => no_builtin = true, + .builtin => mod_opts.no_builtin = false, + .no_builtin => mod_opts.no_builtin = true, .color_diagnostics => color = .on, .no_color_diagnostics => color = .off, .stack_check => mod_opts.stack_check = true, @@ -3468,7 +3467,6 @@ fn buildOutputType( .image_base = image_base, .function_sections = function_sections, .data_sections = data_sections, - .no_builtin = no_builtin, .clang_passthrough_mode = clang_passthrough_mode, .clang_preprocessor_mode = clang_preprocessor_mode, .version = optional_version, diff --git a/src/mingw.zig b/src/mingw.zig index d4ef1ac5a7..cdee9acb07 100644 --- a/src/mingw.zig +++ b/src/mingw.zig @@ -41,7 +41,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .owner = undefined, }, }; - return comp.build_crt_file("crt2", .Obj, null, .@"mingw-w64 crt2.o", prog_node, &files); + return comp.build_crt_file("crt2", .Obj, .@"mingw-w64 crt2.o", prog_node, &files, .{}); }, .dllcrt2_o => { @@ -56,7 +56,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .owner = undefined, }, }; - return comp.build_crt_file("dllcrt2", .Obj, null, .@"mingw-w64 dllcrt2.o", prog_node, &files); + return comp.build_crt_file("dllcrt2", .Obj, .@"mingw-w64 dllcrt2.o", prog_node, &files, .{}); }, .mingw32_lib => { @@ -118,7 +118,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre } else { @panic("unsupported arch"); } - return comp.build_crt_file("mingw32", .Lib, null, .@"mingw-w64 mingw32.lib", prog_node, c_source_files.items); + return comp.build_crt_file("mingw32", .Lib, .@"mingw-w64 mingw32.lib", prog_node, c_source_files.items, .{}); }, } } diff --git a/src/musl.zig b/src/musl.zig index 3a0aeb9417..eb83bcf901 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -38,7 +38,9 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro .owner = undefined, }, }; - return comp.build_crt_file("crti", .Obj, null, .@"musl crti.o", prog_node, &files); + return comp.build_crt_file("crti", .Obj, .@"musl crti.o", prog_node, &files, .{ + .no_builtin = true, + }); }, .crtn_o => { var args = std.ArrayList([]const u8).init(arena); @@ -50,7 +52,9 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro .owner = undefined, }, }; - return comp.build_crt_file("crtn", .Obj, null, .@"musl crtn.o", prog_node, &files); + return comp.build_crt_file("crtn", .Obj, .@"musl crtn.o", prog_node, &files, .{ + .no_builtin = true, + }); }, .crt1_o => { var args = std.ArrayList([]const u8).init(arena); @@ -68,7 +72,9 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro .owner = undefined, }, }; - return comp.build_crt_file("crt1", .Obj, null, .@"musl crt1.o", prog_node, &files); + return comp.build_crt_file("crt1", .Obj, .@"musl crt1.o", prog_node, &files, .{ + .no_builtin = true, + }); }, .rcrt1_o => { var args = std.ArrayList([]const u8).init(arena); @@ -86,7 +92,10 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro .owner = undefined, }, }; - return comp.build_crt_file("rcrt1", .Obj, true, .@"musl rcrt1.o", prog_node, &files); + return comp.build_crt_file("rcrt1", .Obj, .@"musl rcrt1.o", prog_node, &files, .{ + .pic = true, + .no_builtin = true, + }); }, .scrt1_o => { var args = std.ArrayList([]const u8).init(arena); @@ -104,7 +113,10 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro .owner = undefined, }, }; - return comp.build_crt_file("Scrt1", .Obj, true, .@"musl Scrt1.o", prog_node, &files); + return comp.build_crt_file("Scrt1", .Obj, .@"musl Scrt1.o", prog_node, &files, .{ + .pic = true, + .no_builtin = true, + }); }, .libc_a => { // When there is a src//foo.* then it should substitute for src/foo.* @@ -197,7 +209,9 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro .owner = undefined, }; } - return comp.build_crt_file("c", .Lib, null, .@"musl libc.a", prog_node, c_source_files.items); + return comp.build_crt_file("c", .Lib, .@"musl libc.a", prog_node, c_source_files.items, .{ + .no_builtin = true, + }); }, .libc_so => { const optimize_mode = comp.compilerRtOptMode(); @@ -410,7 +424,6 @@ fn addCcArgs( try args.appendSlice(&[_][]const u8{ "-std=c99", "-ffreestanding", - "-fno-builtin", "-fexcess-precision=standard", "-frounding-math", "-ffp-contract=off", diff --git a/src/wasi_libc.zig b/src/wasi_libc.zig index 3b9d9c5446..c9fbbc6e7c 100644 --- a/src/wasi_libc.zig +++ b/src/wasi_libc.zig @@ -81,7 +81,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .owner = undefined, }, }; - return comp.build_crt_file("crt1-reactor", .Obj, null, .@"wasi crt1-reactor.o", prog_node, &files); + return comp.build_crt_file("crt1-reactor", .Obj, .@"wasi crt1-reactor.o", prog_node, &files, .{}); }, .crt1_command_o => { var args = std.ArrayList([]const u8).init(arena); @@ -96,7 +96,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .owner = undefined, }, }; - return comp.build_crt_file("crt1-command", .Obj, null, .@"wasi crt1-command.o", prog_node, &files); + return comp.build_crt_file("crt1-command", .Obj, .@"wasi crt1-command.o", prog_node, &files, .{}); }, .libc_a => { var libc_sources = std.ArrayList(Compilation.CSourceFile).init(arena); @@ -150,7 +150,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre } } - try comp.build_crt_file("c", .Lib, null, .@"wasi libc.a", prog_node, libc_sources.items); + try comp.build_crt_file("c", .Lib, .@"wasi libc.a", prog_node, libc_sources.items, .{}); }, .libwasi_emulated_process_clocks_a => { var args = std.ArrayList([]const u8).init(arena); @@ -167,7 +167,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .owner = undefined, }); } - try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, null, .@"libwasi-emulated-process-clocks.a", prog_node, emu_clocks_sources.items); + try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, .@"libwasi-emulated-process-clocks.a", prog_node, emu_clocks_sources.items, .{}); }, .libwasi_emulated_getpid_a => { var args = std.ArrayList([]const u8).init(arena); @@ -184,7 +184,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .owner = undefined, }); } - try comp.build_crt_file("wasi-emulated-getpid", .Lib, null, .@"libwasi-emulated-getpid.a", prog_node, emu_getpid_sources.items); + try comp.build_crt_file("wasi-emulated-getpid", .Lib, .@"libwasi-emulated-getpid.a", prog_node, emu_getpid_sources.items, .{}); }, .libwasi_emulated_mman_a => { var args = std.ArrayList([]const u8).init(arena); @@ -201,7 +201,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .owner = undefined, }); } - try comp.build_crt_file("wasi-emulated-mman", .Lib, null, .@"libwasi-emulated-mman.a", prog_node, emu_mman_sources.items); + try comp.build_crt_file("wasi-emulated-mman", .Lib, .@"libwasi-emulated-mman.a", prog_node, emu_mman_sources.items, .{}); }, .libwasi_emulated_signal_a => { var emu_signal_sources = std.ArrayList(Compilation.CSourceFile).init(arena); @@ -238,7 +238,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre } } - try comp.build_crt_file("wasi-emulated-signal", .Lib, null, .@"libwasi-emulated-signal.a", prog_node, emu_signal_sources.items); + try comp.build_crt_file("wasi-emulated-signal", .Lib, .@"libwasi-emulated-signal.a", prog_node, emu_signal_sources.items, .{}); }, } } From 7b1d2fa004ef8b1f59ff0b54327f47bb98e18563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 14:31:42 +0100 Subject: [PATCH 03/17] Compilation: Also set essential module options when including compiler-rt.o. Closes #21831. --- src/Compilation.zig | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 24ee899610..acf85f9fc1 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1298,7 +1298,11 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil }, .fully_qualified_name = "compiler_rt", .cc_argv = &.{}, - .inherited = .{}, + .inherited = .{ + .stack_check = false, + .stack_protector = 0, + .no_builtin = true, + }, .global = options.config, .parent = options.root_mod, .builtin_mod = options.root_mod.getBuiltinDependency(), From e88501a09076e134675160c373bbfb5321038541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 18:10:26 +0100 Subject: [PATCH 04/17] Compilation: Fix unwind table logic for compiler-rt. This looks to be a refactoring leftover. --- src/Compilation.zig | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index acf85f9fc1..b3684d7e61 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -6155,7 +6155,6 @@ fn buildOutputFromZig( assert(output_mode != .Exe); - const unwind_tables = comp.link_eh_frame_hdr; const strip = comp.compilerRtStrip(); const optimize_mode = comp.compilerRtOptMode(); @@ -6169,7 +6168,6 @@ fn buildOutputFromZig( .root_optimize_mode = optimize_mode, .root_strip = strip, .link_libc = comp.config.link_libc, - .any_unwind_tables = unwind_tables, }); const root_mod = try Package.Module.create(arena, .{ @@ -6186,7 +6184,7 @@ fn buildOutputFromZig( .stack_protector = 0, .red_zone = comp.root_mod.red_zone, .omit_frame_pointer = comp.root_mod.omit_frame_pointer, - .unwind_tables = unwind_tables, + .unwind_tables = comp.root_mod.unwind_tables, .pic = comp.root_mod.pic, .optimize_mode = optimize_mode, .structured_cfg = comp.root_mod.structured_cfg, From bdca2d0f485682f78ea155df9f9f1cbb5f461ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 14:33:13 +0100 Subject: [PATCH 05/17] llvm: Also apply the nobuiltin attribute for the no_builtin module option. From `zig build-exe --help`: -fno-builtin Disable implicit builtin knowledge of functions It seems entirely reasonable and even expected that this option should imply both no-builtins on functions (which disables transformation of recognized code patterns to libcalls) and nobuiltin on call sites (which disables transformation of libcalls to intrinsics). We now match Clang's behavior for -fno-builtin. In both cases, we're painting with a fairly broad brush by applying this to an entire module, but it's better than nothing. #21833 proposes a more fine-grained way to apply nobuiltin. --- src/codegen/llvm.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 1b90453d49..aa5a027f05 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -5576,6 +5576,10 @@ pub const FuncGen = struct { var attributes: Builder.FunctionAttributes.Wip = .{}; defer attributes.deinit(&o.builder); + if (self.ng.ownerModule().no_builtin) { + try attributes.addFnAttr(.nobuiltin, &o.builder); + } + switch (modifier) { .auto, .never_tail, .always_tail => {}, .never_inline => try attributes.addFnAttr(.@"noinline", &o.builder), From 0563525b214f776c9ae43d4b2c97da45bdc513d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 14:20:55 +0100 Subject: [PATCH 06/17] tsan: Synchronize CFLAGS with upstream. In particular: * -fms-extensions for MinGW * -fno-builtin * -fno-emulated-tls for Android 29+ * -fno-exceptions * -fomit-frame-pointer * -fvisibility=hidden --- src/libtsan.zig | 52 +++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/libtsan.zig b/src/libtsan.zig index d078fa2a38..38ae3dc4fc 100644 --- a/src/libtsan.zig +++ b/src/libtsan.zig @@ -93,11 +93,12 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo .sanitize_c = false, .sanitize_thread = false, .red_zone = comp.root_mod.red_zone, - .omit_frame_pointer = comp.root_mod.omit_frame_pointer, + .omit_frame_pointer = optimize_mode != .Debug and !target.os.tag.isDarwin(), .valgrind = false, .optimize_mode = optimize_mode, .structured_cfg = comp.root_mod.structured_cfg, .pic = true, + .no_builtin = true, }, .global = config, .cc_argv = &common_flags, @@ -123,10 +124,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo try cflags.append("-I"); try cflags.append(tsan_include_path); - try cflags.append("-nostdinc++"); - try cflags.append("-fvisibility-inlines-hidden"); - try cflags.append("-std=c++17"); - try cflags.append("-fno-rtti"); + try addCcArgs(target, &cflags); c_source_files.appendAssumeCapacity(.{ .src_path = try comp.zig_lib_directory.join(arena, &.{ "tsan", tsan_src }), @@ -147,10 +145,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo try cflags.append("-I"); try cflags.append(tsan_include_path); - try cflags.append("-nostdinc++"); - try cflags.append("-fvisibility-inlines-hidden"); - try cflags.append("-std=c++17"); - try cflags.append("-fno-rtti"); + try addCcArgs(target, &cflags); c_source_files.appendAssumeCapacity(.{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "tsan", tsan_src }), @@ -195,10 +190,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo try cflags.append("-I"); try cflags.append(tsan_include_path); - try cflags.append("-nostdinc++"); - try cflags.append("-fvisibility-inlines-hidden"); - try cflags.append("-std=c++17"); - try cflags.append("-fno-rtti"); + try addCcArgs(target, &cflags); c_source_files.appendAssumeCapacity(.{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ @@ -222,10 +214,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo try cflags.append("-I"); try cflags.append(tsan_include_path); - try cflags.append("-nostdinc++"); - try cflags.append("-fvisibility-inlines-hidden"); - try cflags.append("-std=c++17"); - try cflags.append("-fno-rtti"); + try addCcArgs(target, &cflags); c_source_files.appendAssumeCapacity(.{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ @@ -243,10 +232,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo try cflags.append("-I"); try cflags.append(tsan_include_path); - try cflags.append("-nostdinc++"); - try cflags.append("-fvisibility-inlines-hidden"); - try cflags.append("-std=c++17"); - try cflags.append("-fno-rtti"); + try addCcArgs(target, &cflags); c_source_files.appendAssumeCapacity(.{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ @@ -272,10 +258,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo try cflags.append("-I"); try cflags.append(tsan_include_path); - try cflags.append("-nostdinc++"); - try cflags.append("-fvisibility-inlines-hidden"); - try cflags.append("-std=c++17"); - try cflags.append("-fno-rtti"); + try addCcArgs(target, &cflags); c_source_files.appendAssumeCapacity(.{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ @@ -348,6 +331,25 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo comp.tsan_lib = crt_file; } +fn addCcArgs(target: std.Target, args: *std.ArrayList([]const u8)) error{OutOfMemory}!void { + try args.appendSlice(&[_][]const u8{ + "-nostdinc++", + "-fvisibility=hidden", + "-fvisibility-inlines-hidden", + "-std=c++17", + "-fno-rtti", + "-fno-exceptions", + }); + + if (target.abi.isAndroid() and target.os.version_range.linux.android >= 29) { + try args.append("-fno-emulated-tls"); + } + + if (target.isMinGW()) { + try args.append("-fms-extensions"); + } +} + const tsan_sources = [_][]const u8{ "tsan_debugging.cpp", "tsan_external.cpp", From 2a65b84572f19369e831d71c513da88a295193c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 15:07:32 +0100 Subject: [PATCH 07/17] tsan: Handle more Apple targets when picking library name. --- src/libtsan.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libtsan.zig b/src/libtsan.zig index 38ae3dc4fc..8b2427df1b 100644 --- a/src/libtsan.zig +++ b/src/libtsan.zig @@ -29,11 +29,11 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo const root_name = switch (target.os.tag) { // On Apple platforms, we use the same name as LLVM because the // TSAN library implementation hard-codes a check for these names. - .macos => "clang_rt.tsan_osx_dynamic", - .ios => switch (target.abi) { - .simulator => "clang_rt.tsan_iossim_dynamic", - else => "clang_rt.tsan_ios_dynamic", - }, + .driverkit, .macos => "clang_rt.tsan_osx_dynamic", + .ios => if (target.abi == .simulator) "clang_rt.tsan_iossim_dynamic" else "clang_rt.tsan_ios_dynamic", + .tvos => if (target.abi == .simulator) "clang_rt.tsan_tvossim_dynamic" else "clang_rt.tsan_tvos_dynamic", + .visionos => if (target.abi == .simulator) "clang_rt.tsan_xrossim_dynamic" else "clang_rt.tsan_xros_dynamic", + .watchos => if (target.abi == .simulator) "clang_rt.tsan_watchossim_dynamic" else "clang_rt.tsan_watchos_dynamic", else => "tsan", }; const link_mode: std.builtin.LinkMode = if (target.isDarwin()) .dynamic else .static; From f973d3e93e9bb419212a187e787de94929de928d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 12:18:35 +0100 Subject: [PATCH 08/17] glibc, musl, wasi-libc: Don't explicitly pass -fno-stack-protector. This is already handled by build_crt_file(). --- src/glibc.zig | 1 - src/musl.zig | 15 +++------------ src/wasi_libc.zig | 1 - 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/glibc.zig b/src/glibc.zig index c7f84a797c..f1ac544163 100644 --- a/src/glibc.zig +++ b/src/glibc.zig @@ -373,7 +373,6 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre "-fmerge-all-constants", "-frounding-math", "-Wno-unsupported-floating-point-opt", // For targets that don't support -frounding-math. - "-fno-stack-protector", "-fno-common", "-fmath-errno", "-ftls-model=initial-exec", diff --git a/src/musl.zig b/src/musl.zig index eb83bcf901..ec2b0674ff 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -59,10 +59,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro .crt1_o => { var args = std.ArrayList([]const u8).init(arena); try addCcArgs(comp, arena, &args, false); - try args.appendSlice(&[_][]const u8{ - "-fno-stack-protector", - "-DCRT", - }); + try args.append("-DCRT"); var files = [_]Compilation.CSourceFile{ .{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ @@ -79,10 +76,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro .rcrt1_o => { var args = std.ArrayList([]const u8).init(arena); try addCcArgs(comp, arena, &args, false); - try args.appendSlice(&[_][]const u8{ - "-fno-stack-protector", - "-DCRT", - }); + try args.append("-DCRT"); var files = [_]Compilation.CSourceFile{ .{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ @@ -100,10 +94,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro .scrt1_o => { var args = std.ArrayList([]const u8).init(arena); try addCcArgs(comp, arena, &args, false); - try args.appendSlice(&[_][]const u8{ - "-fno-stack-protector", - "-DCRT", - }); + try args.append("-DCRT"); var files = [_]Compilation.CSourceFile{ .{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ diff --git a/src/wasi_libc.zig b/src/wasi_libc.zig index c9fbbc6e7c..ce6131a470 100644 --- a/src/wasi_libc.zig +++ b/src/wasi_libc.zig @@ -279,7 +279,6 @@ fn addCCArgs( try args.appendSlice(&[_][]const u8{ "-std=gnu17", "-fno-trapping-math", - "-fno-stack-protector", "-w", // ignore all warnings o_arg, From 7fef0b4a23ae616ebfec978d6af8e9f716a63555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 15:33:28 +0100 Subject: [PATCH 09/17] musl: Pass -f(function,data)-sections via CrtFileOptions instead of CFLAGS. --- src/Compilation.zig | 4 ++++ src/musl.zig | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index b3684d7e61..0dcc121fac 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -6262,6 +6262,8 @@ fn buildOutputFromZig( } pub const CrtFileOptions = struct { + function_sections: ?bool = null, + data_sections: ?bool = null, pic: ?bool = null, no_builtin: ?bool = null, }; @@ -6356,6 +6358,8 @@ pub fn build_crt_file( .directory = null, // Put it in the cache directory. .basename = basename, }, + .function_sections = options.function_sections orelse false, + .data_sections = options.data_sections orelse false, .emit_h = null, .c_source_files = c_source_files, .verbose_cc = comp.verbose_cc, diff --git a/src/musl.zig b/src/musl.zig index ec2b0674ff..99662bbd2c 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -39,6 +39,8 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }, }; return comp.build_crt_file("crti", .Obj, .@"musl crti.o", prog_node, &files, .{ + .function_sections = true, + .data_sections = true, .no_builtin = true, }); }, @@ -53,6 +55,8 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }, }; return comp.build_crt_file("crtn", .Obj, .@"musl crtn.o", prog_node, &files, .{ + .function_sections = true, + .data_sections = true, .no_builtin = true, }); }, @@ -70,6 +74,8 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }, }; return comp.build_crt_file("crt1", .Obj, .@"musl crt1.o", prog_node, &files, .{ + .function_sections = true, + .data_sections = true, .no_builtin = true, }); }, @@ -87,6 +93,8 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }, }; return comp.build_crt_file("rcrt1", .Obj, .@"musl rcrt1.o", prog_node, &files, .{ + .function_sections = true, + .data_sections = true, .pic = true, .no_builtin = true, }); @@ -105,6 +113,8 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }, }; return comp.build_crt_file("Scrt1", .Obj, .@"musl Scrt1.o", prog_node, &files, .{ + .function_sections = true, + .data_sections = true, .pic = true, .no_builtin = true, }); @@ -201,6 +211,8 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro }; } return comp.build_crt_file("c", .Lib, .@"musl libc.a", prog_node, c_source_files.items, .{ + .function_sections = true, + .data_sections = true, .no_builtin = true, }); }, @@ -448,8 +460,6 @@ fn addCcArgs( "-fomit-frame-pointer", "-fno-unwind-tables", "-fno-asynchronous-unwind-tables", - "-ffunction-sections", - "-fdata-sections", "-Qunused-arguments", "-w", // disable all warnings From 796d4845ff6747611c0061f8fe5c3c1c5b967226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 14:47:36 +0100 Subject: [PATCH 10/17] musl: Pass -fomit-frame-pointer via CrtFileOptions. --- src/Compilation.zig | 4 +++- src/musl.zig | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 0dcc121fac..f646ef258a 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -6264,6 +6264,7 @@ fn buildOutputFromZig( pub const CrtFileOptions = struct { function_sections: ?bool = null, data_sections: ?bool = null, + omit_frame_pointer: ?bool = null, pic: ?bool = null, no_builtin: ?bool = null, }; @@ -6322,7 +6323,8 @@ pub fn build_crt_file( .sanitize_c = false, .sanitize_thread = false, .red_zone = comp.root_mod.red_zone, - .omit_frame_pointer = comp.root_mod.omit_frame_pointer, + // Some libcs (e.g. musl) are opinionated about -fomit-frame-pointer. + .omit_frame_pointer = options.omit_frame_pointer orelse comp.root_mod.omit_frame_pointer, .valgrind = false, .unwind_tables = false, // Some CRT objects (e.g. musl's rcrt1.o and Scrt1.o) are opinionated about PIC. diff --git a/src/musl.zig b/src/musl.zig index 99662bbd2c..b1d47361a8 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -41,6 +41,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro return comp.build_crt_file("crti", .Obj, .@"musl crti.o", prog_node, &files, .{ .function_sections = true, .data_sections = true, + .omit_frame_pointer = true, .no_builtin = true, }); }, @@ -57,6 +58,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro return comp.build_crt_file("crtn", .Obj, .@"musl crtn.o", prog_node, &files, .{ .function_sections = true, .data_sections = true, + .omit_frame_pointer = true, .no_builtin = true, }); }, @@ -76,6 +78,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro return comp.build_crt_file("crt1", .Obj, .@"musl crt1.o", prog_node, &files, .{ .function_sections = true, .data_sections = true, + .omit_frame_pointer = true, .no_builtin = true, }); }, @@ -95,6 +98,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro return comp.build_crt_file("rcrt1", .Obj, .@"musl rcrt1.o", prog_node, &files, .{ .function_sections = true, .data_sections = true, + .omit_frame_pointer = true, .pic = true, .no_builtin = true, }); @@ -115,6 +119,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro return comp.build_crt_file("Scrt1", .Obj, .@"musl Scrt1.o", prog_node, &files, .{ .function_sections = true, .data_sections = true, + .omit_frame_pointer = true, .pic = true, .no_builtin = true, }); @@ -213,6 +218,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro return comp.build_crt_file("c", .Lib, .@"musl libc.a", prog_node, c_source_files.items, .{ .function_sections = true, .data_sections = true, + .omit_frame_pointer = true, .no_builtin = true, }); }, @@ -457,7 +463,6 @@ fn addCcArgs( o_arg, - "-fomit-frame-pointer", "-fno-unwind-tables", "-fno-asynchronous-unwind-tables", From eb14fd8806885b43dd98492f0a6720038d8176c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 15:25:43 +0100 Subject: [PATCH 11/17] libcxx: Pass -fPIC via module options instead of CFLAGS. --- src/libcxx.zig | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/libcxx.zig b/src/libcxx.zig index a9f3030c42..ec89ac1cfc 100644 --- a/src/libcxx.zig +++ b/src/libcxx.zig @@ -195,7 +195,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError! .valgrind = false, .optimize_mode = optimize_mode, .structured_cfg = comp.root_mod.structured_cfg, - .pic = comp.root_mod.pic, + .pic = if (target_util.supports_fpic(target)) true else null, }, .global = config, .cc_argv = &.{}, @@ -278,9 +278,6 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError! try cflags.append("-faligned-allocation"); } - if (target_util.supports_fpic(target)) { - try cflags.append("-fPIC"); - } try cflags.append("-nostdinc++"); try cflags.append("-std=c++23"); try cflags.append("-Wno-user-defined-literals"); From 5685a10ded2dbb64070c8f43911a05584901c5eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 15:26:05 +0100 Subject: [PATCH 12/17] libunwind: Pass -fPIC -funwind-tables via module options instead of CFLAGS. --- src/libunwind.zig | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/libunwind.zig b/src/libunwind.zig index d7b22e2067..1e101da229 100644 --- a/src/libunwind.zig +++ b/src/libunwind.zig @@ -46,6 +46,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr ); return error.SubCompilationFailed; }; + const target = comp.root_mod.resolved_target.result; const root_mod = Module.create(arena, .{ .global_cache_directory = comp.global_cache_directory, .paths = .{ @@ -63,8 +64,9 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr .valgrind = false, .sanitize_c = false, .sanitize_thread = false, - .unwind_tables = false, - .pic = comp.root_mod.pic, + // necessary so that libunwind can unwind through its own stack frames + .unwind_tables = true, + .pic = if (target_util.supports_fpic(target)) true else null, .optimize_mode = comp.compilerRtOptMode(), }, .global = config, @@ -83,7 +85,6 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr const root_name = "unwind"; const link_mode = .static; - const target = comp.root_mod.resolved_target.result; const basename = try std.zig.binNameAlloc(arena, .{ .root_name = root_name, .target = target, @@ -114,16 +115,11 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr try cflags.append("-fno-exceptions"); try cflags.append("-I"); try cflags.append(try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libunwind", "include" })); - if (target_util.supports_fpic(target)) { - try cflags.append("-fPIC"); - } try cflags.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS"); try cflags.append("-Wa,--noexecstack"); try cflags.append("-fvisibility=hidden"); try cflags.append("-fvisibility-inlines-hidden"); try cflags.append("-fvisibility-global-new-delete=force-hidden"); - // necessary so that libunwind can unwind through its own stack frames - try cflags.append("-funwind-tables"); // This is intentionally always defined because the macro definition means, should it only // build for the target specified by compiler defines. Since we pass -target the compiler From 89a506a7efdf6934f2d17380cd98879c00e60824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 15:41:54 +0100 Subject: [PATCH 13/17] musl: Don't explicitly pass -fno-unwind-tables -fno-asynchronous-unwind-tables. These are already handled by build_crt_file(). --- src/musl.zig | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/musl.zig b/src/musl.zig index b1d47361a8..d1b2fd2e2d 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -463,9 +463,6 @@ fn addCcArgs( o_arg, - "-fno-unwind-tables", - "-fno-asynchronous-unwind-tables", - "-Qunused-arguments", "-w", // disable all warnings }); From 9e1dd3dec2e8a74798b152b199ae6484c2fc1070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 8 Nov 2024 11:54:30 +0100 Subject: [PATCH 14/17] c: Use internal linkage when running tests. This matches what we do for compiler-rt. --- lib/c.zig | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/c.zig b/lib/c.zig index 927d71d82f..31a7d3d6ff 100644 --- a/lib/c.zig +++ b/lib/c.zig @@ -11,6 +11,8 @@ const native_os = builtin.os.tag; const native_arch = builtin.cpu.arch; const native_abi = builtin.abi; +const linkage: std.builtin.GlobalLinkage = if (builtin.is_test) .internal else .strong; + const is_wasm = switch (native_arch) { .wasm32, .wasm64 => true, else => false, @@ -30,14 +32,14 @@ comptime { } if (builtin.link_libc) { - @export(&strcmp, .{ .name = "strcmp", .linkage = .strong }); - @export(&strncmp, .{ .name = "strncmp", .linkage = .strong }); - @export(&strerror, .{ .name = "strerror", .linkage = .strong }); - @export(&strlen, .{ .name = "strlen", .linkage = .strong }); - @export(&strcpy, .{ .name = "strcpy", .linkage = .strong }); - @export(&strncpy, .{ .name = "strncpy", .linkage = .strong }); - @export(&strcat, .{ .name = "strcat", .linkage = .strong }); - @export(&strncat, .{ .name = "strncat", .linkage = .strong }); + @export(&strcmp, .{ .name = "strcmp", .linkage = linkage }); + @export(&strncmp, .{ .name = "strncmp", .linkage = linkage }); + @export(&strerror, .{ .name = "strerror", .linkage = linkage }); + @export(&strlen, .{ .name = "strlen", .linkage = linkage }); + @export(&strcpy, .{ .name = "strcpy", .linkage = linkage }); + @export(&strncpy, .{ .name = "strncpy", .linkage = linkage }); + @export(&strcat, .{ .name = "strcat", .linkage = linkage }); + @export(&strncat, .{ .name = "strncat", .linkage = linkage }); } } From 9904a3ac9d35641ba4676ad97c8abd96b20aa1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 8 Nov 2024 11:54:57 +0100 Subject: [PATCH 15/17] c: Include Os.Tag.other in the list of freestanding OSs. --- lib/c.zig | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/c.zig b/lib/c.zig index 31a7d3d6ff..734940ff05 100644 --- a/lib/c.zig +++ b/lib/c.zig @@ -17,12 +17,8 @@ const is_wasm = switch (native_arch) { .wasm32, .wasm64 => true, else => false, }; -const is_msvc = switch (native_abi) { - .msvc => true, - else => false, -}; const is_freestanding = switch (native_os) { - .freestanding => true, + .freestanding, .other => true, else => false, }; From 15c920ff2ad6faef6f3d1a64822eaff9e1ab76a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Tue, 5 Nov 2024 20:44:37 +0100 Subject: [PATCH 16/17] std.Target: Fix toCoffMachine() value for thumb. --- lib/std/Target.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 298da21187..7bb6de93ad 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1076,7 +1076,7 @@ pub fn toElfMachine(target: Target) std.elf.EM { pub fn toCoffMachine(target: Target) std.coff.MachineType { return switch (target.cpu.arch) { .arm => .ARM, - .thumb => .THUMB, + .thumb => .ARMNT, .aarch64 => .ARM64, .loongarch32 => .LOONGARCH32, .loongarch64 => .LOONGARCH64, From c9052ef93105107bd20bd52ee51197d55e7ace34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 4 Nov 2024 17:51:11 +0100 Subject: [PATCH 17/17] llvm: Disable lowering to f16 on sparc. --- src/codegen/llvm.zig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index aa5a027f05..fb20d4d622 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -12730,6 +12730,8 @@ fn backendSupportsF16(target: std.Target) bool { .mips64, .mips64el, .s390x, + .sparc, + .sparc64, => false, .arm, .armeb,