authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-11-12 16:40:00+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-11-12 16:40:00+01:00
log1db8cade5ab5c05fa3c6a884e50947633ebcf065
tree5a74083c154ad53748440fa85c75a16b009043d9
parentbbbc95afd0d035224047443d56ed2252d8f47cb9
parentc9052ef93105107bd20bd52ee51197d55e7ace34
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21920 from alexrp/nobuiltin

compiler: Improve handling of `-fno-builtin` and compiler-rt options

14 files changed, 156 insertions(+), 124 deletions(-)

lib/c.zig+11-13
...@@ -11,16 +11,14 @@ const native_os = builtin.os.tag;...@@ -11,16 +11,14 @@ const native_os = builtin.os.tag;
11const native_arch = builtin.cpu.arch;11const native_arch = builtin.cpu.arch;
12const native_abi = builtin.abi;12const native_abi = builtin.abi;
1313
14const linkage: std.builtin.GlobalLinkage = if (builtin.is_test) .internal else .strong;
15
14const is_wasm = switch (native_arch) {16const is_wasm = switch (native_arch) {
15 .wasm32, .wasm64 => true,17 .wasm32, .wasm64 => true,
16 else => false,18 else => false,
17};19};
18const is_msvc = switch (native_abi) {
19 .msvc => true,
20 else => false,
21};
22const is_freestanding = switch (native_os) {20const is_freestanding = switch (native_os) {
23 .freestanding => true,21 .freestanding, .other => true,
24 else => false,22 else => false,
25};23};
2624
...@@ -30,14 +28,14 @@ comptime {...@@ -30,14 +28,14 @@ comptime {
30 }28 }
3129
32 if (builtin.link_libc) {30 if (builtin.link_libc) {
33 @export(&strcmp, .{ .name = "strcmp", .linkage = .strong });31 @export(&strcmp, .{ .name = "strcmp", .linkage = linkage });
34 @export(&strncmp, .{ .name = "strncmp", .linkage = .strong });32 @export(&strncmp, .{ .name = "strncmp", .linkage = linkage });
35 @export(&strerror, .{ .name = "strerror", .linkage = .strong });33 @export(&strerror, .{ .name = "strerror", .linkage = linkage });
36 @export(&strlen, .{ .name = "strlen", .linkage = .strong });34 @export(&strlen, .{ .name = "strlen", .linkage = linkage });
37 @export(&strcpy, .{ .name = "strcpy", .linkage = .strong });35 @export(&strcpy, .{ .name = "strcpy", .linkage = linkage });
38 @export(&strncpy, .{ .name = "strncpy", .linkage = .strong });36 @export(&strncpy, .{ .name = "strncpy", .linkage = linkage });
39 @export(&strcat, .{ .name = "strcat", .linkage = .strong });37 @export(&strcat, .{ .name = "strcat", .linkage = linkage });
40 @export(&strncat, .{ .name = "strncat", .linkage = .strong });38 @export(&strncat, .{ .name = "strncat", .linkage = linkage });
41 }39 }
42}40}
4341
lib/std/Target.zig+1-1
...@@ -1076,7 +1076,7 @@ pub fn toElfMachine(target: Target) std.elf.EM {...@@ -1076,7 +1076,7 @@ pub fn toElfMachine(target: Target) std.elf.EM {
1076pub fn toCoffMachine(target: Target) std.coff.MachineType {1076pub fn toCoffMachine(target: Target) std.coff.MachineType {
1077 return switch (target.cpu.arch) {1077 return switch (target.cpu.arch) {
1078 .arm => .ARM,1078 .arm => .ARM,
1079 .thumb => .THUMB,1079 .thumb => .ARMNT,
1080 .aarch64 => .ARM64,1080 .aarch64 => .ARM64,
1081 .loongarch32 => .LOONGARCH32,1081 .loongarch32 => .LOONGARCH32,
1082 .loongarch64 => .LOONGARCH64,1082 .loongarch64 => .LOONGARCH64,
src/Compilation.zig+26-14
...@@ -89,7 +89,6 @@ windows_libs: std.StringArrayHashMapUnmanaged(void),...@@ -89,7 +89,6 @@ windows_libs: std.StringArrayHashMapUnmanaged(void),
89version: ?std.SemanticVersion,89version: ?std.SemanticVersion,
90libc_installation: ?*const LibCInstallation,90libc_installation: ?*const LibCInstallation,
91skip_linker_dependencies: bool,91skip_linker_dependencies: bool,
92no_builtin: bool,
93function_sections: bool,92function_sections: bool,
94data_sections: bool,93data_sections: bool,
95link_eh_frame_hdr: bool,94link_eh_frame_hdr: bool,
...@@ -852,6 +851,7 @@ pub const cache_helpers = struct {...@@ -852,6 +851,7 @@ pub const cache_helpers = struct {
852 hh.add(mod.fuzz);851 hh.add(mod.fuzz);
853 hh.add(mod.unwind_tables);852 hh.add(mod.unwind_tables);
854 hh.add(mod.structured_cfg);853 hh.add(mod.structured_cfg);
854 hh.add(mod.no_builtin);
855 hh.addListOfBytes(mod.cc_argv);855 hh.addListOfBytes(mod.cc_argv);
856 }856 }
857857
...@@ -1057,7 +1057,6 @@ pub const CreateOptions = struct {...@@ -1057,7 +1057,6 @@ pub const CreateOptions = struct {
1057 want_lto: ?bool = null,1057 want_lto: ?bool = null,
1058 function_sections: bool = false,1058 function_sections: bool = false,
1059 data_sections: bool = false,1059 data_sections: bool = false,
1060 no_builtin: bool = false,
1061 time_report: bool = false,1060 time_report: bool = false,
1062 stack_report: bool = false,1061 stack_report: bool = false,
1063 link_eh_frame_hdr: bool = false,1062 link_eh_frame_hdr: bool = false,
...@@ -1299,7 +1298,11 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil...@@ -1299,7 +1298,11 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
1299 },1298 },
1300 .fully_qualified_name = "compiler_rt",1299 .fully_qualified_name = "compiler_rt",
1301 .cc_argv = &.{},1300 .cc_argv = &.{},
1302 .inherited = .{},1301 .inherited = .{
1302 .stack_check = false,
1303 .stack_protector = 0,
1304 .no_builtin = true,
1305 },
1303 .global = options.config,1306 .global = options.config,
1304 .parent = options.root_mod,1307 .parent = options.root_mod,
1305 .builtin_mod = options.root_mod.getBuiltinDependency(),1308 .builtin_mod = options.root_mod.getBuiltinDependency(),
...@@ -1353,7 +1356,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil...@@ -1353,7 +1356,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
1353 cache.hash.add(options.config.link_mode);1356 cache.hash.add(options.config.link_mode);
1354 cache.hash.add(options.function_sections);1357 cache.hash.add(options.function_sections);
1355 cache.hash.add(options.data_sections);1358 cache.hash.add(options.data_sections);
1356 cache.hash.add(options.no_builtin);
1357 cache.hash.add(link_libc);1359 cache.hash.add(link_libc);
1358 cache.hash.add(options.config.link_libcpp);1360 cache.hash.add(options.config.link_libcpp);
1359 cache.hash.add(options.config.link_libunwind);1361 cache.hash.add(options.config.link_libunwind);
...@@ -1490,7 +1492,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil...@@ -1490,7 +1492,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
1490 .framework_dirs = options.framework_dirs,1492 .framework_dirs = options.framework_dirs,
1491 .llvm_opt_bisect_limit = options.llvm_opt_bisect_limit,1493 .llvm_opt_bisect_limit = options.llvm_opt_bisect_limit,
1492 .skip_linker_dependencies = options.skip_linker_dependencies,1494 .skip_linker_dependencies = options.skip_linker_dependencies,
1493 .no_builtin = options.no_builtin,
1494 .job_queued_update_builtin_zig = have_zcu,1495 .job_queued_update_builtin_zig = have_zcu,
1495 .function_sections = options.function_sections,1496 .function_sections = options.function_sections,
1496 .data_sections = options.data_sections,1497 .data_sections = options.data_sections,
...@@ -5261,7 +5262,7 @@ pub fn addCCArgs(...@@ -5261,7 +5262,7 @@ pub fn addCCArgs(
5261 try argv.append("-fdata-sections");5262 try argv.append("-fdata-sections");
5262 }5263 }
52635264
5264 if (comp.no_builtin) {5265 if (mod.no_builtin) {
5265 try argv.append("-fno-builtin");5266 try argv.append("-fno-builtin");
5266 }5267 }
52675268
...@@ -6154,7 +6155,6 @@ fn buildOutputFromZig(...@@ -6154,7 +6155,6 @@ fn buildOutputFromZig(
61546155
6155 assert(output_mode != .Exe);6156 assert(output_mode != .Exe);
61566157
6157 const unwind_tables = comp.link_eh_frame_hdr;
6158 const strip = comp.compilerRtStrip();6158 const strip = comp.compilerRtStrip();
6159 const optimize_mode = comp.compilerRtOptMode();6159 const optimize_mode = comp.compilerRtOptMode();
61606160
...@@ -6168,7 +6168,6 @@ fn buildOutputFromZig(...@@ -6168,7 +6168,6 @@ fn buildOutputFromZig(
6168 .root_optimize_mode = optimize_mode,6168 .root_optimize_mode = optimize_mode,
6169 .root_strip = strip,6169 .root_strip = strip,
6170 .link_libc = comp.config.link_libc,6170 .link_libc = comp.config.link_libc,
6171 .any_unwind_tables = unwind_tables,
6172 });6171 });
61736172
6174 const root_mod = try Package.Module.create(arena, .{6173 const root_mod = try Package.Module.create(arena, .{
...@@ -6185,10 +6184,11 @@ fn buildOutputFromZig(...@@ -6185,10 +6184,11 @@ fn buildOutputFromZig(
6185 .stack_protector = 0,6184 .stack_protector = 0,
6186 .red_zone = comp.root_mod.red_zone,6185 .red_zone = comp.root_mod.red_zone,
6187 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,6186 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
6188 .unwind_tables = unwind_tables,6187 .unwind_tables = comp.root_mod.unwind_tables,
6189 .pic = comp.root_mod.pic,6188 .pic = comp.root_mod.pic,
6190 .optimize_mode = optimize_mode,6189 .optimize_mode = optimize_mode,
6191 .structured_cfg = comp.root_mod.structured_cfg,6190 .structured_cfg = comp.root_mod.structured_cfg,
6191 .no_builtin = true,
6192 .code_model = comp.root_mod.code_model,6192 .code_model = comp.root_mod.code_model,
6193 },6193 },
6194 .global = config,6194 .global = config,
...@@ -6237,7 +6237,6 @@ fn buildOutputFromZig(...@@ -6237,7 +6237,6 @@ fn buildOutputFromZig(
6237 },6237 },
6238 .function_sections = true,6238 .function_sections = true,
6239 .data_sections = true,6239 .data_sections = true,
6240 .no_builtin = true,
6241 .emit_h = null,6240 .emit_h = null,
6242 .verbose_cc = comp.verbose_cc,6241 .verbose_cc = comp.verbose_cc,
6243 .verbose_link = comp.verbose_link,6242 .verbose_link = comp.verbose_link,
...@@ -6262,16 +6261,24 @@ fn buildOutputFromZig(...@@ -6262,16 +6261,24 @@ fn buildOutputFromZig(
6262 comp.queueLinkTaskMode(crt_file.full_object_path, output_mode);6261 comp.queueLinkTaskMode(crt_file.full_object_path, output_mode);
6263}6262}
62646263
6264pub const CrtFileOptions = struct {
6265 function_sections: ?bool = null,
6266 data_sections: ?bool = null,
6267 omit_frame_pointer: ?bool = null,
6268 pic: ?bool = null,
6269 no_builtin: ?bool = null,
6270};
6271
6265pub fn build_crt_file(6272pub fn build_crt_file(
6266 comp: *Compilation,6273 comp: *Compilation,
6267 root_name: []const u8,6274 root_name: []const u8,
6268 output_mode: std.builtin.OutputMode,6275 output_mode: std.builtin.OutputMode,
6269 pic: ?bool,
6270 misc_task_tag: MiscTask,6276 misc_task_tag: MiscTask,
6271 prog_node: std.Progress.Node,6277 prog_node: std.Progress.Node,
6272 /// These elements have to get mutated to add the owner module after it is6278 /// These elements have to get mutated to add the owner module after it is
6273 /// created within this function.6279 /// created within this function.
6274 c_source_files: []CSourceFile,6280 c_source_files: []CSourceFile,
6281 options: CrtFileOptions,
6275) !void {6282) !void {
6276 const tracy_trace = trace(@src());6283 const tracy_trace = trace(@src());
6277 defer tracy_trace.end();6284 defer tracy_trace.end();
...@@ -6316,13 +6323,16 @@ pub fn build_crt_file(...@@ -6316,13 +6323,16 @@ pub fn build_crt_file(
6316 .sanitize_c = false,6323 .sanitize_c = false,
6317 .sanitize_thread = false,6324 .sanitize_thread = false,
6318 .red_zone = comp.root_mod.red_zone,6325 .red_zone = comp.root_mod.red_zone,
6319 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,6326 // Some libcs (e.g. musl) are opinionated about -fomit-frame-pointer.
6327 .omit_frame_pointer = options.omit_frame_pointer orelse comp.root_mod.omit_frame_pointer,
6320 .valgrind = false,6328 .valgrind = false,
6321 .unwind_tables = false,6329 .unwind_tables = false,
6322 // Some CRT objects (rcrt1.o, Scrt1.o) are opinionated about PIC.6330 // Some CRT objects (e.g. musl's rcrt1.o and Scrt1.o) are opinionated about PIC.
6323 .pic = pic orelse comp.root_mod.pic,6331 .pic = options.pic orelse comp.root_mod.pic,
6324 .optimize_mode = comp.compilerRtOptMode(),6332 .optimize_mode = comp.compilerRtOptMode(),
6325 .structured_cfg = comp.root_mod.structured_cfg,6333 .structured_cfg = comp.root_mod.structured_cfg,
6334 // Some libcs (e.g. musl) are opinionated about -fno-builtin.
6335 .no_builtin = options.no_builtin orelse comp.root_mod.no_builtin,
6326 },6336 },
6327 .global = config,6337 .global = config,
6328 .cc_argv = &.{},6338 .cc_argv = &.{},
...@@ -6350,6 +6360,8 @@ pub fn build_crt_file(...@@ -6350,6 +6360,8 @@ pub fn build_crt_file(
6350 .directory = null, // Put it in the cache directory.6360 .directory = null, // Put it in the cache directory.
6351 .basename = basename,6361 .basename = basename,
6352 },6362 },
6363 .function_sections = options.function_sections orelse false,
6364 .data_sections = options.data_sections orelse false,
6353 .emit_h = null,6365 .emit_h = null,
6354 .c_source_files = c_source_files,6366 .c_source_files = c_source_files,
6355 .verbose_cc = comp.verbose_cc,6367 .verbose_cc = comp.verbose_cc,
src/Package/Module.zig+12
...@@ -31,6 +31,7 @@ unwind_tables: bool,...@@ -31,6 +31,7 @@ unwind_tables: bool,
31cc_argv: []const []const u8,31cc_argv: []const []const u8,
32/// (SPIR-V) whether to generate a structured control flow graph or not32/// (SPIR-V) whether to generate a structured control flow graph or not
33structured_cfg: bool,33structured_cfg: bool,
34no_builtin: bool,
3435
35/// If the module is an `@import("builtin")` module, this is the `File` that36/// If the module is an `@import("builtin")` module, this is the `File` that
36/// is preallocated for it. Otherwise this field is null.37/// is preallocated for it. Otherwise this field is null.
...@@ -95,6 +96,7 @@ pub const CreateOptions = struct {...@@ -95,6 +96,7 @@ pub const CreateOptions = struct {
95 sanitize_thread: ?bool = null,96 sanitize_thread: ?bool = null,
96 fuzz: ?bool = null,97 fuzz: ?bool = null,
97 structured_cfg: ?bool = null,98 structured_cfg: ?bool = null,
99 no_builtin: ?bool = null,
98 };100 };
99};101};
100102
...@@ -298,6 +300,13 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {...@@ -298,6 +300,13 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
298 };300 };
299 };301 };
300302
303 const no_builtin = b: {
304 if (options.inherited.no_builtin) |x| break :b x;
305 if (options.parent) |p| break :b p.no_builtin;
306
307 break :b target.cpu.arch.isBpf();
308 };
309
301 const llvm_cpu_features: ?[*:0]const u8 = b: {310 const llvm_cpu_features: ?[*:0]const u8 = b: {
302 if (resolved_target.llvm_cpu_features) |x| break :b x;311 if (resolved_target.llvm_cpu_features) |x| break :b x;
303 if (!options.global.use_llvm) break :b null;312 if (!options.global.use_llvm) break :b null;
...@@ -350,6 +359,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {...@@ -350,6 +359,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
350 .unwind_tables = unwind_tables,359 .unwind_tables = unwind_tables,
351 .cc_argv = options.cc_argv,360 .cc_argv = options.cc_argv,
352 .structured_cfg = structured_cfg,361 .structured_cfg = structured_cfg,
362 .no_builtin = no_builtin,
353 .builtin_file = null,363 .builtin_file = null,
354 };364 };
355365
...@@ -442,6 +452,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {...@@ -442,6 +452,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
442 .unwind_tables = unwind_tables,452 .unwind_tables = unwind_tables,
443 .cc_argv = &.{},453 .cc_argv = &.{},
444 .structured_cfg = structured_cfg,454 .structured_cfg = structured_cfg,
455 .no_builtin = no_builtin,
445 .builtin_file = new_file,456 .builtin_file = new_file,
446 };457 };
447 new_file.* = .{458 new_file.* = .{
...@@ -502,6 +513,7 @@ pub fn createLimited(gpa: Allocator, options: LimitedOptions) Allocator.Error!*P...@@ -502,6 +513,7 @@ pub fn createLimited(gpa: Allocator, options: LimitedOptions) Allocator.Error!*P
502 .unwind_tables = undefined,513 .unwind_tables = undefined,
503 .cc_argv = undefined,514 .cc_argv = undefined,
504 .structured_cfg = undefined,515 .structured_cfg = undefined,
516 .no_builtin = undefined,
505 .builtin_file = null,517 .builtin_file = null,
506 };518 };
507 return mod;519 return mod;
src/codegen/llvm.zig+8-4
...@@ -3222,8 +3222,6 @@ pub const Object = struct {...@@ -3222,8 +3222,6 @@ pub const Object = struct {
3222 owner_mod: *Package.Module,3222 owner_mod: *Package.Module,
3223 omit_frame_pointer: bool,3223 omit_frame_pointer: bool,
3224 ) Allocator.Error!void {3224 ) Allocator.Error!void {
3225 const comp = o.pt.zcu.comp;
3226
3227 if (!owner_mod.red_zone) {3225 if (!owner_mod.red_zone) {
3228 try attributes.addFnAttr(.noredzone, &o.builder);3226 try attributes.addFnAttr(.noredzone, &o.builder);
3229 }3227 }
...@@ -3242,8 +3240,7 @@ pub const Object = struct {...@@ -3242,8 +3240,7 @@ pub const Object = struct {
3242 if (owner_mod.unwind_tables) {3240 if (owner_mod.unwind_tables) {
3243 try attributes.addFnAttr(.{ .uwtable = Builder.Attribute.UwTable.default }, &o.builder);3241 try attributes.addFnAttr(.{ .uwtable = Builder.Attribute.UwTable.default }, &o.builder);
3244 }3242 }
3245 const target = owner_mod.resolved_target.result;3243 if (owner_mod.no_builtin) {
3246 if (comp.skip_linker_dependencies or comp.no_builtin or target.cpu.arch.isBpf()) {
3247 // The intent here is for compiler-rt and libc functions to not generate3244 // The intent here is for compiler-rt and libc functions to not generate
3248 // infinite recursion. For example, if we are compiling the memcpy function,3245 // infinite recursion. For example, if we are compiling the memcpy function,
3249 // and llvm detects that the body is equivalent to memcpy, it may replace the3246 // and llvm detects that the body is equivalent to memcpy, it may replace the
...@@ -3258,6 +3255,7 @@ pub const Object = struct {...@@ -3258,6 +3255,7 @@ pub const Object = struct {
3258 try attributes.addFnAttr(.minsize, &o.builder);3255 try attributes.addFnAttr(.minsize, &o.builder);
3259 try attributes.addFnAttr(.optsize, &o.builder);3256 try attributes.addFnAttr(.optsize, &o.builder);
3260 }3257 }
3258 const target = owner_mod.resolved_target.result;
3261 if (target.cpu.model.llvm_name) |s| {3259 if (target.cpu.model.llvm_name) |s| {
3262 try attributes.addFnAttr(.{ .string = .{3260 try attributes.addFnAttr(.{ .string = .{
3263 .kind = try o.builder.string("target-cpu"),3261 .kind = try o.builder.string("target-cpu"),
...@@ -5578,6 +5576,10 @@ pub const FuncGen = struct {...@@ -5578,6 +5576,10 @@ pub const FuncGen = struct {
5578 var attributes: Builder.FunctionAttributes.Wip = .{};5576 var attributes: Builder.FunctionAttributes.Wip = .{};
5579 defer attributes.deinit(&o.builder);5577 defer attributes.deinit(&o.builder);
55805578
5579 if (self.ng.ownerModule().no_builtin) {
5580 try attributes.addFnAttr(.nobuiltin, &o.builder);
5581 }
5582
5581 switch (modifier) {5583 switch (modifier) {
5582 .auto, .never_tail, .always_tail => {},5584 .auto, .never_tail, .always_tail => {},
5583 .never_inline => try attributes.addFnAttr(.@"noinline", &o.builder),5585 .never_inline => try attributes.addFnAttr(.@"noinline", &o.builder),
...@@ -12728,6 +12730,8 @@ fn backendSupportsF16(target: std.Target) bool {...@@ -12728,6 +12730,8 @@ fn backendSupportsF16(target: std.Target) bool {
12728 .mips64,12730 .mips64,
12729 .mips64el,12731 .mips64el,
12730 .s390x,12732 .s390x,
12733 .sparc,
12734 .sparc64,
12731 => false,12735 => false,
12732 .arm,12736 .arm,
12733 .armeb,12737 .armeb,
src/glibc.zig+4-5
...@@ -221,7 +221,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -221,7 +221,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
221 .owner = comp.root_mod,221 .owner = comp.root_mod,
222 },222 },
223 };223 };
224 return comp.build_crt_file("crti", .Obj, null, .@"glibc crti.o", prog_node, &files);224 return comp.build_crt_file("crti", .Obj, .@"glibc crti.o", prog_node, &files, .{});
225 },225 },
226 .crtn_o => {226 .crtn_o => {
227 var args = std.ArrayList([]const u8).init(arena);227 var args = std.ArrayList([]const u8).init(arena);
...@@ -242,7 +242,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -242,7 +242,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
242 .owner = undefined,242 .owner = undefined,
243 },243 },
244 };244 };
245 return comp.build_crt_file("crtn", .Obj, null, .@"glibc crtn.o", prog_node, &files);245 return comp.build_crt_file("crtn", .Obj, .@"glibc crtn.o", prog_node, &files, .{});
246 },246 },
247 .scrt1_o => {247 .scrt1_o => {
248 const start_o: Compilation.CSourceFile = blk: {248 const start_o: Compilation.CSourceFile = blk: {
...@@ -295,7 +295,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -295,7 +295,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
295 };295 };
296 var files = [_]Compilation.CSourceFile{ start_o, abi_note_o, init_o };296 var files = [_]Compilation.CSourceFile{ start_o, abi_note_o, init_o };
297 const basename = if (comp.config.output_mode == .Exe and !comp.config.pie) "crt1" else "Scrt1";297 const basename = if (comp.config.output_mode == .Exe and !comp.config.pie) "crt1" else "Scrt1";
298 return comp.build_crt_file(basename, .Obj, null, .@"glibc Scrt1.o", prog_node, &files);298 return comp.build_crt_file(basename, .Obj, .@"glibc Scrt1.o", prog_node, &files, .{});
299 },299 },
300 .libc_nonshared_a => {300 .libc_nonshared_a => {
301 const s = path.sep_str;301 const s = path.sep_str;
...@@ -373,7 +373,6 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -373,7 +373,6 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
373 "-fmerge-all-constants",373 "-fmerge-all-constants",
374 "-frounding-math",374 "-frounding-math",
375 "-Wno-unsupported-floating-point-opt", // For targets that don't support -frounding-math.375 "-Wno-unsupported-floating-point-opt", // For targets that don't support -frounding-math.
376 "-fno-stack-protector",
377 "-fno-common",376 "-fno-common",
378 "-fmath-errno",377 "-fmath-errno",
379 "-ftls-model=initial-exec",378 "-ftls-model=initial-exec",
...@@ -413,7 +412,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -413,7 +412,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
413 files_index += 1;412 files_index += 1;
414 }413 }
415 const files = files_buf[0..files_index];414 const files = files_buf[0..files_index];
416 return comp.build_crt_file("c_nonshared", .Lib, null, .@"glibc libc_nonshared.a", prog_node, files);415 return comp.build_crt_file("c_nonshared", .Lib, .@"glibc libc_nonshared.a", prog_node, files, .{});
417 },416 },
418 }417 }
419}418}
src/libcxx.zig+1-4
...@@ -195,7 +195,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!...@@ -195,7 +195,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
195 .valgrind = false,195 .valgrind = false,
196 .optimize_mode = optimize_mode,196 .optimize_mode = optimize_mode,
197 .structured_cfg = comp.root_mod.structured_cfg,197 .structured_cfg = comp.root_mod.structured_cfg,
198 .pic = comp.root_mod.pic,198 .pic = if (target_util.supports_fpic(target)) true else null,
199 },199 },
200 .global = config,200 .global = config,
201 .cc_argv = &.{},201 .cc_argv = &.{},
...@@ -278,9 +278,6 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!...@@ -278,9 +278,6 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
278 try cflags.append("-faligned-allocation");278 try cflags.append("-faligned-allocation");
279 }279 }
280280
281 if (target_util.supports_fpic(target)) {
282 try cflags.append("-fPIC");
283 }
284 try cflags.append("-nostdinc++");281 try cflags.append("-nostdinc++");
285 try cflags.append("-std=c++23");282 try cflags.append("-std=c++23");
286 try cflags.append("-Wno-user-defined-literals");283 try cflags.append("-Wno-user-defined-literals");
src/libtsan.zig+32-30
...@@ -29,11 +29,11 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo...@@ -29,11 +29,11 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
29 const root_name = switch (target.os.tag) {29 const root_name = switch (target.os.tag) {
30 // On Apple platforms, we use the same name as LLVM because the30 // On Apple platforms, we use the same name as LLVM because the
31 // TSAN library implementation hard-codes a check for these names.31 // TSAN library implementation hard-codes a check for these names.
32 .macos => "clang_rt.tsan_osx_dynamic",32 .driverkit, .macos => "clang_rt.tsan_osx_dynamic",
33 .ios => switch (target.abi) {33 .ios => if (target.abi == .simulator) "clang_rt.tsan_iossim_dynamic" else "clang_rt.tsan_ios_dynamic",
34 .simulator => "clang_rt.tsan_iossim_dynamic",34 .tvos => if (target.abi == .simulator) "clang_rt.tsan_tvossim_dynamic" else "clang_rt.tsan_tvos_dynamic",
35 else => "clang_rt.tsan_ios_dynamic",35 .visionos => if (target.abi == .simulator) "clang_rt.tsan_xrossim_dynamic" else "clang_rt.tsan_xros_dynamic",
36 },36 .watchos => if (target.abi == .simulator) "clang_rt.tsan_watchossim_dynamic" else "clang_rt.tsan_watchos_dynamic",
37 else => "tsan",37 else => "tsan",
38 };38 };
39 const link_mode: std.builtin.LinkMode = if (target.isDarwin()) .dynamic else .static;39 const link_mode: std.builtin.LinkMode = if (target.isDarwin()) .dynamic else .static;
...@@ -93,11 +93,12 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo...@@ -93,11 +93,12 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
93 .sanitize_c = false,93 .sanitize_c = false,
94 .sanitize_thread = false,94 .sanitize_thread = false,
95 .red_zone = comp.root_mod.red_zone,95 .red_zone = comp.root_mod.red_zone,
96 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,96 .omit_frame_pointer = optimize_mode != .Debug and !target.os.tag.isDarwin(),
97 .valgrind = false,97 .valgrind = false,
98 .optimize_mode = optimize_mode,98 .optimize_mode = optimize_mode,
99 .structured_cfg = comp.root_mod.structured_cfg,99 .structured_cfg = comp.root_mod.structured_cfg,
100 .pic = true,100 .pic = true,
101 .no_builtin = true,
101 },102 },
102 .global = config,103 .global = config,
103 .cc_argv = &common_flags,104 .cc_argv = &common_flags,
...@@ -123,10 +124,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo...@@ -123,10 +124,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
123 try cflags.append("-I");124 try cflags.append("-I");
124 try cflags.append(tsan_include_path);125 try cflags.append(tsan_include_path);
125126
126 try cflags.append("-nostdinc++");127 try addCcArgs(target, &cflags);
127 try cflags.append("-fvisibility-inlines-hidden");
128 try cflags.append("-std=c++17");
129 try cflags.append("-fno-rtti");
130128
131 c_source_files.appendAssumeCapacity(.{129 c_source_files.appendAssumeCapacity(.{
132 .src_path = try comp.zig_lib_directory.join(arena, &.{ "tsan", tsan_src }),130 .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...@@ -147,10 +145,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
147 try cflags.append("-I");145 try cflags.append("-I");
148 try cflags.append(tsan_include_path);146 try cflags.append(tsan_include_path);
149147
150 try cflags.append("-nostdinc++");148 try addCcArgs(target, &cflags);
151 try cflags.append("-fvisibility-inlines-hidden");
152 try cflags.append("-std=c++17");
153 try cflags.append("-fno-rtti");
154149
155 c_source_files.appendAssumeCapacity(.{150 c_source_files.appendAssumeCapacity(.{
156 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "tsan", tsan_src }),151 .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...@@ -195,10 +190,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
195 try cflags.append("-I");190 try cflags.append("-I");
196 try cflags.append(tsan_include_path);191 try cflags.append(tsan_include_path);
197192
198 try cflags.append("-nostdinc++");193 try addCcArgs(target, &cflags);
199 try cflags.append("-fvisibility-inlines-hidden");
200 try cflags.append("-std=c++17");
201 try cflags.append("-fno-rtti");
202194
203 c_source_files.appendAssumeCapacity(.{195 c_source_files.appendAssumeCapacity(.{
204 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{196 .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...@@ -222,10 +214,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
222 try cflags.append("-I");214 try cflags.append("-I");
223 try cflags.append(tsan_include_path);215 try cflags.append(tsan_include_path);
224216
225 try cflags.append("-nostdinc++");217 try addCcArgs(target, &cflags);
226 try cflags.append("-fvisibility-inlines-hidden");
227 try cflags.append("-std=c++17");
228 try cflags.append("-fno-rtti");
229218
230 c_source_files.appendAssumeCapacity(.{219 c_source_files.appendAssumeCapacity(.{
231 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{220 .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...@@ -243,10 +232,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
243 try cflags.append("-I");232 try cflags.append("-I");
244 try cflags.append(tsan_include_path);233 try cflags.append(tsan_include_path);
245234
246 try cflags.append("-nostdinc++");235 try addCcArgs(target, &cflags);
247 try cflags.append("-fvisibility-inlines-hidden");
248 try cflags.append("-std=c++17");
249 try cflags.append("-fno-rtti");
250236
251 c_source_files.appendAssumeCapacity(.{237 c_source_files.appendAssumeCapacity(.{
252 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{238 .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...@@ -272,10 +258,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
272 try cflags.append("-I");258 try cflags.append("-I");
273 try cflags.append(tsan_include_path);259 try cflags.append(tsan_include_path);
274260
275 try cflags.append("-nostdinc++");261 try addCcArgs(target, &cflags);
276 try cflags.append("-fvisibility-inlines-hidden");
277 try cflags.append("-std=c++17");
278 try cflags.append("-fno-rtti");
279262
280 c_source_files.appendAssumeCapacity(.{263 c_source_files.appendAssumeCapacity(.{
281 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{264 .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...@@ -348,6 +331,25 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
348 comp.tsan_lib = crt_file;331 comp.tsan_lib = crt_file;
349}332}
350333
334fn addCcArgs(target: std.Target, args: *std.ArrayList([]const u8)) error{OutOfMemory}!void {
335 try args.appendSlice(&[_][]const u8{
336 "-nostdinc++",
337 "-fvisibility=hidden",
338 "-fvisibility-inlines-hidden",
339 "-std=c++17",
340 "-fno-rtti",
341 "-fno-exceptions",
342 });
343
344 if (target.abi.isAndroid() and target.os.version_range.linux.android >= 29) {
345 try args.append("-fno-emulated-tls");
346 }
347
348 if (target.isMinGW()) {
349 try args.append("-fms-extensions");
350 }
351}
352
351const tsan_sources = [_][]const u8{353const tsan_sources = [_][]const u8{
352 "tsan_debugging.cpp",354 "tsan_debugging.cpp",
353 "tsan_external.cpp",355 "tsan_external.cpp",
src/libunwind.zig+4-8
...@@ -46,6 +46,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr...@@ -46,6 +46,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
46 );46 );
47 return error.SubCompilationFailed;47 return error.SubCompilationFailed;
48 };48 };
49 const target = comp.root_mod.resolved_target.result;
49 const root_mod = Module.create(arena, .{50 const root_mod = Module.create(arena, .{
50 .global_cache_directory = comp.global_cache_directory,51 .global_cache_directory = comp.global_cache_directory,
51 .paths = .{52 .paths = .{
...@@ -63,8 +64,9 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr...@@ -63,8 +64,9 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
63 .valgrind = false,64 .valgrind = false,
64 .sanitize_c = false,65 .sanitize_c = false,
65 .sanitize_thread = false,66 .sanitize_thread = false,
66 .unwind_tables = false,67 // necessary so that libunwind can unwind through its own stack frames
67 .pic = comp.root_mod.pic,68 .unwind_tables = true,
69 .pic = if (target_util.supports_fpic(target)) true else null,
68 .optimize_mode = comp.compilerRtOptMode(),70 .optimize_mode = comp.compilerRtOptMode(),
69 },71 },
70 .global = config,72 .global = config,
...@@ -83,7 +85,6 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr...@@ -83,7 +85,6 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
8385
84 const root_name = "unwind";86 const root_name = "unwind";
85 const link_mode = .static;87 const link_mode = .static;
86 const target = comp.root_mod.resolved_target.result;
87 const basename = try std.zig.binNameAlloc(arena, .{88 const basename = try std.zig.binNameAlloc(arena, .{
88 .root_name = root_name,89 .root_name = root_name,
89 .target = target,90 .target = target,
...@@ -114,16 +115,11 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr...@@ -114,16 +115,11 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
114 try cflags.append("-fno-exceptions");115 try cflags.append("-fno-exceptions");
115 try cflags.append("-I");116 try cflags.append("-I");
116 try cflags.append(try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libunwind", "include" }));117 try cflags.append(try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libunwind", "include" }));
117 if (target_util.supports_fpic(target)) {
118 try cflags.append("-fPIC");
119 }
120 try cflags.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");118 try cflags.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");
121 try cflags.append("-Wa,--noexecstack");119 try cflags.append("-Wa,--noexecstack");
122 try cflags.append("-fvisibility=hidden");120 try cflags.append("-fvisibility=hidden");
123 try cflags.append("-fvisibility-inlines-hidden");121 try cflags.append("-fvisibility-inlines-hidden");
124 try cflags.append("-fvisibility-global-new-delete=force-hidden");122 try cflags.append("-fvisibility-global-new-delete=force-hidden");
125 // necessary so that libunwind can unwind through its own stack frames
126 try cflags.append("-funwind-tables");
127123
128 // This is intentionally always defined because the macro definition means, should it only124 // This is intentionally always defined because the macro definition means, should it only
129 // build for the target specified by compiler defines. Since we pass -target the compiler125 // build for the target specified by compiler defines. Since we pass -target the compiler
src/main.zig+4-6
...@@ -810,7 +810,6 @@ fn buildOutputType(...@@ -810,7 +810,6 @@ fn buildOutputType(
810 var compatibility_version: ?std.SemanticVersion = null;810 var compatibility_version: ?std.SemanticVersion = null;
811 var function_sections = false;811 var function_sections = false;
812 var data_sections = false;812 var data_sections = false;
813 var no_builtin = false;
814 var listen: Listen = .none;813 var listen: Listen = .none;
815 var debug_compile_errors = false;814 var debug_compile_errors = false;
816 var verbose_link = (native_os != .wasi or builtin.link_libc) and815 var verbose_link = (native_os != .wasi or builtin.link_libc) and
...@@ -1550,9 +1549,9 @@ fn buildOutputType(...@@ -1550,9 +1549,9 @@ fn buildOutputType(
1550 } else if (mem.eql(u8, arg, "-fno-data-sections")) {1549 } else if (mem.eql(u8, arg, "-fno-data-sections")) {
1551 data_sections = false;1550 data_sections = false;
1552 } else if (mem.eql(u8, arg, "-fbuiltin")) {1551 } else if (mem.eql(u8, arg, "-fbuiltin")) {
1553 no_builtin = false;1552 mod_opts.no_builtin = false;
1554 } else if (mem.eql(u8, arg, "-fno-builtin")) {1553 } else if (mem.eql(u8, arg, "-fno-builtin")) {
1555 no_builtin = true;1554 mod_opts.no_builtin = true;
1556 } else if (mem.startsWith(u8, arg, "-fopt-bisect-limit=")) {1555 } else if (mem.startsWith(u8, arg, "-fopt-bisect-limit=")) {
1557 const next_arg = arg["-fopt-bisect-limit=".len..];1556 const next_arg = arg["-fopt-bisect-limit=".len..];
1558 llvm_opt_bisect_limit = std.fmt.parseInt(c_int, next_arg, 0) catch |err|1557 llvm_opt_bisect_limit = std.fmt.parseInt(c_int, next_arg, 0) catch |err|
...@@ -1963,8 +1962,8 @@ fn buildOutputType(...@@ -1963,8 +1962,8 @@ fn buildOutputType(
1963 .no_function_sections => function_sections = false,1962 .no_function_sections => function_sections = false,
1964 .data_sections => data_sections = true,1963 .data_sections => data_sections = true,
1965 .no_data_sections => data_sections = false,1964 .no_data_sections => data_sections = false,
1966 .builtin => no_builtin = false,1965 .builtin => mod_opts.no_builtin = false,
1967 .no_builtin => no_builtin = true,1966 .no_builtin => mod_opts.no_builtin = true,
1968 .color_diagnostics => color = .on,1967 .color_diagnostics => color = .on,
1969 .no_color_diagnostics => color = .off,1968 .no_color_diagnostics => color = .off,
1970 .stack_check => mod_opts.stack_check = true,1969 .stack_check => mod_opts.stack_check = true,
...@@ -3468,7 +3467,6 @@ fn buildOutputType(...@@ -3468,7 +3467,6 @@ fn buildOutputType(
3468 .image_base = image_base,3467 .image_base = image_base,
3469 .function_sections = function_sections,3468 .function_sections = function_sections,
3470 .data_sections = data_sections,3469 .data_sections = data_sections,
3471 .no_builtin = no_builtin,
3472 .clang_passthrough_mode = clang_passthrough_mode,3470 .clang_passthrough_mode = clang_passthrough_mode,
3473 .clang_preprocessor_mode = clang_preprocessor_mode,3471 .clang_preprocessor_mode = clang_preprocessor_mode,
3474 .version = optional_version,3472 .version = optional_version,
src/mingw.zig+3-3
...@@ -41,7 +41,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -41,7 +41,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
41 .owner = undefined,41 .owner = undefined,
42 },42 },
43 };43 };
44 return comp.build_crt_file("crt2", .Obj, null, .@"mingw-w64 crt2.o", prog_node, &files);44 return comp.build_crt_file("crt2", .Obj, .@"mingw-w64 crt2.o", prog_node, &files, .{});
45 },45 },
4646
47 .dllcrt2_o => {47 .dllcrt2_o => {
...@@ -56,7 +56,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -56,7 +56,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
56 .owner = undefined,56 .owner = undefined,
57 },57 },
58 };58 };
59 return comp.build_crt_file("dllcrt2", .Obj, null, .@"mingw-w64 dllcrt2.o", prog_node, &files);59 return comp.build_crt_file("dllcrt2", .Obj, .@"mingw-w64 dllcrt2.o", prog_node, &files, .{});
60 },60 },
6161
62 .mingw32_lib => {62 .mingw32_lib => {
...@@ -118,7 +118,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -118,7 +118,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
118 } else {118 } else {
119 @panic("unsupported arch");119 @panic("unsupported arch");
120 }120 }
121 return comp.build_crt_file("mingw32", .Lib, null, .@"mingw-w64 mingw32.lib", prog_node, c_source_files.items);121 return comp.build_crt_file("mingw32", .Lib, .@"mingw-w64 mingw32.lib", prog_node, c_source_files.items, .{});
122 },122 },
123 }123 }
124}124}
src/musl.zig+41-25
...@@ -38,7 +38,12 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -38,7 +38,12 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
38 .owner = undefined,38 .owner = undefined,
39 },39 },
40 };40 };
41 return comp.build_crt_file("crti", .Obj, null, .@"musl crti.o", prog_node, &files);41 return comp.build_crt_file("crti", .Obj, .@"musl crti.o", prog_node, &files, .{
42 .function_sections = true,
43 .data_sections = true,
44 .omit_frame_pointer = true,
45 .no_builtin = true,
46 });
42 },47 },
43 .crtn_o => {48 .crtn_o => {
44 var args = std.ArrayList([]const u8).init(arena);49 var args = std.ArrayList([]const u8).init(arena);
...@@ -50,15 +55,17 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -50,15 +55,17 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
50 .owner = undefined,55 .owner = undefined,
51 },56 },
52 };57 };
53 return comp.build_crt_file("crtn", .Obj, null, .@"musl crtn.o", prog_node, &files);58 return comp.build_crt_file("crtn", .Obj, .@"musl crtn.o", prog_node, &files, .{
59 .function_sections = true,
60 .data_sections = true,
61 .omit_frame_pointer = true,
62 .no_builtin = true,
63 });
54 },64 },
55 .crt1_o => {65 .crt1_o => {
56 var args = std.ArrayList([]const u8).init(arena);66 var args = std.ArrayList([]const u8).init(arena);
57 try addCcArgs(comp, arena, &args, false);67 try addCcArgs(comp, arena, &args, false);
58 try args.appendSlice(&[_][]const u8{68 try args.append("-DCRT");
59 "-fno-stack-protector",
60 "-DCRT",
61 });
62 var files = [_]Compilation.CSourceFile{69 var files = [_]Compilation.CSourceFile{
63 .{70 .{
64 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{71 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
...@@ -68,15 +75,17 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -68,15 +75,17 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
68 .owner = undefined,75 .owner = undefined,
69 },76 },
70 };77 };
71 return comp.build_crt_file("crt1", .Obj, null, .@"musl crt1.o", prog_node, &files);78 return comp.build_crt_file("crt1", .Obj, .@"musl crt1.o", prog_node, &files, .{
79 .function_sections = true,
80 .data_sections = true,
81 .omit_frame_pointer = true,
82 .no_builtin = true,
83 });
72 },84 },
73 .rcrt1_o => {85 .rcrt1_o => {
74 var args = std.ArrayList([]const u8).init(arena);86 var args = std.ArrayList([]const u8).init(arena);
75 try addCcArgs(comp, arena, &args, false);87 try addCcArgs(comp, arena, &args, false);
76 try args.appendSlice(&[_][]const u8{88 try args.append("-DCRT");
77 "-fno-stack-protector",
78 "-DCRT",
79 });
80 var files = [_]Compilation.CSourceFile{89 var files = [_]Compilation.CSourceFile{
81 .{90 .{
82 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{91 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
...@@ -86,15 +95,18 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -86,15 +95,18 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
86 .owner = undefined,95 .owner = undefined,
87 },96 },
88 };97 };
89 return comp.build_crt_file("rcrt1", .Obj, true, .@"musl rcrt1.o", prog_node, &files);98 return comp.build_crt_file("rcrt1", .Obj, .@"musl rcrt1.o", prog_node, &files, .{
99 .function_sections = true,
100 .data_sections = true,
101 .omit_frame_pointer = true,
102 .pic = true,
103 .no_builtin = true,
104 });
90 },105 },
91 .scrt1_o => {106 .scrt1_o => {
92 var args = std.ArrayList([]const u8).init(arena);107 var args = std.ArrayList([]const u8).init(arena);
93 try addCcArgs(comp, arena, &args, false);108 try addCcArgs(comp, arena, &args, false);
94 try args.appendSlice(&[_][]const u8{109 try args.append("-DCRT");
95 "-fno-stack-protector",
96 "-DCRT",
97 });
98 var files = [_]Compilation.CSourceFile{110 var files = [_]Compilation.CSourceFile{
99 .{111 .{
100 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{112 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
...@@ -104,7 +116,13 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -104,7 +116,13 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
104 .owner = undefined,116 .owner = undefined,
105 },117 },
106 };118 };
107 return comp.build_crt_file("Scrt1", .Obj, true, .@"musl Scrt1.o", prog_node, &files);119 return comp.build_crt_file("Scrt1", .Obj, .@"musl Scrt1.o", prog_node, &files, .{
120 .function_sections = true,
121 .data_sections = true,
122 .omit_frame_pointer = true,
123 .pic = true,
124 .no_builtin = true,
125 });
108 },126 },
109 .libc_a => {127 .libc_a => {
110 // When there is a src/<arch>/foo.* then it should substitute for src/foo.*128 // When there is a src/<arch>/foo.* then it should substitute for src/foo.*
...@@ -197,7 +215,12 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -197,7 +215,12 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
197 .owner = undefined,215 .owner = undefined,
198 };216 };
199 }217 }
200 return comp.build_crt_file("c", .Lib, null, .@"musl libc.a", prog_node, c_source_files.items);218 return comp.build_crt_file("c", .Lib, .@"musl libc.a", prog_node, c_source_files.items, .{
219 .function_sections = true,
220 .data_sections = true,
221 .omit_frame_pointer = true,
222 .no_builtin = true,
223 });
201 },224 },
202 .libc_so => {225 .libc_so => {
203 const optimize_mode = comp.compilerRtOptMode();226 const optimize_mode = comp.compilerRtOptMode();
...@@ -410,7 +433,6 @@ fn addCcArgs(...@@ -410,7 +433,6 @@ fn addCcArgs(
410 try args.appendSlice(&[_][]const u8{433 try args.appendSlice(&[_][]const u8{
411 "-std=c99",434 "-std=c99",
412 "-ffreestanding",435 "-ffreestanding",
413 "-fno-builtin",
414 "-fexcess-precision=standard",436 "-fexcess-precision=standard",
415 "-frounding-math",437 "-frounding-math",
416 "-ffp-contract=off",438 "-ffp-contract=off",
...@@ -441,12 +463,6 @@ fn addCcArgs(...@@ -441,12 +463,6 @@ fn addCcArgs(
441463
442 o_arg,464 o_arg,
443465
444 "-fomit-frame-pointer",
445 "-fno-unwind-tables",
446 "-fno-asynchronous-unwind-tables",
447 "-ffunction-sections",
448 "-fdata-sections",
449
450 "-Qunused-arguments",466 "-Qunused-arguments",
451 "-w", // disable all warnings467 "-w", // disable all warnings
452 });468 });
src/target.zig+2-3
...@@ -327,9 +327,8 @@ pub fn libcFullLinkFlags(target: std.Target) []const []const u8 {...@@ -327,9 +327,8 @@ pub fn libcFullLinkFlags(target: std.Target) []const []const u8 {
327}327}
328328
329pub fn clangMightShellOutForAssembly(target: std.Target) bool {329pub fn clangMightShellOutForAssembly(target: std.Target) bool {
330 // Clang defaults to using the system assembler over the internal one330 // Clang defaults to using the system assembler in some cases.
331 // when targeting a non-BSD OS.331 return target.cpu.arch.isNvptx() or target.cpu.arch == .xcore;
332 return target.cpu.arch.isSPARC();
333}332}
334333
335/// Each backend architecture in Clang has a different codepath which may or may not334/// Each backend architecture in Clang has a different codepath which may or may not
src/wasi_libc.zig+7-8
...@@ -81,7 +81,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -81,7 +81,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
81 .owner = undefined,81 .owner = undefined,
82 },82 },
83 };83 };
84 return comp.build_crt_file("crt1-reactor", .Obj, null, .@"wasi crt1-reactor.o", prog_node, &files);84 return comp.build_crt_file("crt1-reactor", .Obj, .@"wasi crt1-reactor.o", prog_node, &files, .{});
85 },85 },
86 .crt1_command_o => {86 .crt1_command_o => {
87 var args = std.ArrayList([]const u8).init(arena);87 var args = std.ArrayList([]const u8).init(arena);
...@@ -96,7 +96,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -96,7 +96,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
96 .owner = undefined,96 .owner = undefined,
97 },97 },
98 };98 };
99 return comp.build_crt_file("crt1-command", .Obj, null, .@"wasi crt1-command.o", prog_node, &files);99 return comp.build_crt_file("crt1-command", .Obj, .@"wasi crt1-command.o", prog_node, &files, .{});
100 },100 },
101 .libc_a => {101 .libc_a => {
102 var libc_sources = std.ArrayList(Compilation.CSourceFile).init(arena);102 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...@@ -150,7 +150,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
150 }150 }
151 }151 }
152152
153 try comp.build_crt_file("c", .Lib, null, .@"wasi libc.a", prog_node, libc_sources.items);153 try comp.build_crt_file("c", .Lib, .@"wasi libc.a", prog_node, libc_sources.items, .{});
154 },154 },
155 .libwasi_emulated_process_clocks_a => {155 .libwasi_emulated_process_clocks_a => {
156 var args = std.ArrayList([]const u8).init(arena);156 var args = std.ArrayList([]const u8).init(arena);
...@@ -167,7 +167,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -167,7 +167,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
167 .owner = undefined,167 .owner = undefined,
168 });168 });
169 }169 }
170 try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, null, .@"libwasi-emulated-process-clocks.a", prog_node, emu_clocks_sources.items);170 try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, .@"libwasi-emulated-process-clocks.a", prog_node, emu_clocks_sources.items, .{});
171 },171 },
172 .libwasi_emulated_getpid_a => {172 .libwasi_emulated_getpid_a => {
173 var args = std.ArrayList([]const u8).init(arena);173 var args = std.ArrayList([]const u8).init(arena);
...@@ -184,7 +184,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -184,7 +184,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
184 .owner = undefined,184 .owner = undefined,
185 });185 });
186 }186 }
187 try comp.build_crt_file("wasi-emulated-getpid", .Lib, null, .@"libwasi-emulated-getpid.a", prog_node, emu_getpid_sources.items);187 try comp.build_crt_file("wasi-emulated-getpid", .Lib, .@"libwasi-emulated-getpid.a", prog_node, emu_getpid_sources.items, .{});
188 },188 },
189 .libwasi_emulated_mman_a => {189 .libwasi_emulated_mman_a => {
190 var args = std.ArrayList([]const u8).init(arena);190 var args = std.ArrayList([]const u8).init(arena);
...@@ -201,7 +201,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre...@@ -201,7 +201,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
201 .owner = undefined,201 .owner = undefined,
202 });202 });
203 }203 }
204 try comp.build_crt_file("wasi-emulated-mman", .Lib, null, .@"libwasi-emulated-mman.a", prog_node, emu_mman_sources.items);204 try comp.build_crt_file("wasi-emulated-mman", .Lib, .@"libwasi-emulated-mman.a", prog_node, emu_mman_sources.items, .{});
205 },205 },
206 .libwasi_emulated_signal_a => {206 .libwasi_emulated_signal_a => {
207 var emu_signal_sources = std.ArrayList(Compilation.CSourceFile).init(arena);207 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...@@ -238,7 +238,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
238 }238 }
239 }239 }
240240
241 try comp.build_crt_file("wasi-emulated-signal", .Lib, null, .@"libwasi-emulated-signal.a", prog_node, emu_signal_sources.items);241 try comp.build_crt_file("wasi-emulated-signal", .Lib, .@"libwasi-emulated-signal.a", prog_node, emu_signal_sources.items, .{});
242 },242 },
243 }243 }
244}244}
...@@ -279,7 +279,6 @@ fn addCCArgs(...@@ -279,7 +279,6 @@ fn addCCArgs(
279 try args.appendSlice(&[_][]const u8{279 try args.appendSlice(&[_][]const u8{
280 "-std=gnu17",280 "-std=gnu17",
281 "-fno-trapping-math",281 "-fno-trapping-math",
282 "-fno-stack-protector",
283 "-w", // ignore all warnings282 "-w", // ignore all warnings
284283
285 o_arg,284 o_arg,