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;
1111const native_arch = builtin.cpu.arch;
1212const native_abi = builtin.abi;
1313
14const linkage: std.builtin.GlobalLinkage = if (builtin.is_test) .internal else .strong;
15
1416const is_wasm = switch (native_arch) {
1517 .wasm32, .wasm64 => true,
1618 else => false,
1719};
18const is_msvc = switch (native_abi) {
19 .msvc => true,
20 else => false,
21};
2220const is_freestanding = switch (native_os) {
23 .freestanding => true,
21 .freestanding, .other => true,
2422 else => false,
2523};
2624
......@@ -30,14 +28,14 @@ comptime {
3028 }
3129
3230 if (builtin.link_libc) {
33 @export(&strcmp, .{ .name = "strcmp", .linkage = .strong });
34 @export(&strncmp, .{ .name = "strncmp", .linkage = .strong });
35 @export(&strerror, .{ .name = "strerror", .linkage = .strong });
36 @export(&strlen, .{ .name = "strlen", .linkage = .strong });
37 @export(&strcpy, .{ .name = "strcpy", .linkage = .strong });
38 @export(&strncpy, .{ .name = "strncpy", .linkage = .strong });
39 @export(&strcat, .{ .name = "strcat", .linkage = .strong });
40 @export(&strncat, .{ .name = "strncat", .linkage = .strong });
31 @export(&strcmp, .{ .name = "strcmp", .linkage = linkage });
32 @export(&strncmp, .{ .name = "strncmp", .linkage = linkage });
33 @export(&strerror, .{ .name = "strerror", .linkage = linkage });
34 @export(&strlen, .{ .name = "strlen", .linkage = linkage });
35 @export(&strcpy, .{ .name = "strcpy", .linkage = linkage });
36 @export(&strncpy, .{ .name = "strncpy", .linkage = linkage });
37 @export(&strcat, .{ .name = "strcat", .linkage = linkage });
38 @export(&strncat, .{ .name = "strncat", .linkage = linkage });
4139 }
4240}
4341
lib/std/Target.zig+1-1
......@@ -1076,7 +1076,7 @@ pub fn toElfMachine(target: Target) std.elf.EM {
10761076pub fn toCoffMachine(target: Target) std.coff.MachineType {
10771077 return switch (target.cpu.arch) {
10781078 .arm => .ARM,
1079 .thumb => .THUMB,
1079 .thumb => .ARMNT,
10801080 .aarch64 => .ARM64,
10811081 .loongarch32 => .LOONGARCH32,
10821082 .loongarch64 => .LOONGARCH64,
src/Compilation.zig+26-14
......@@ -89,7 +89,6 @@ windows_libs: std.StringArrayHashMapUnmanaged(void),
8989version: ?std.SemanticVersion,
9090libc_installation: ?*const LibCInstallation,
9191skip_linker_dependencies: bool,
92no_builtin: bool,
9392function_sections: bool,
9493data_sections: bool,
9594link_eh_frame_hdr: bool,
......@@ -852,6 +851,7 @@ pub const cache_helpers = struct {
852851 hh.add(mod.fuzz);
853852 hh.add(mod.unwind_tables);
854853 hh.add(mod.structured_cfg);
854 hh.add(mod.no_builtin);
855855 hh.addListOfBytes(mod.cc_argv);
856856 }
857857
......@@ -1057,7 +1057,6 @@ pub const CreateOptions = struct {
10571057 want_lto: ?bool = null,
10581058 function_sections: bool = false,
10591059 data_sections: bool = false,
1060 no_builtin: bool = false,
10611060 time_report: bool = false,
10621061 stack_report: bool = false,
10631062 link_eh_frame_hdr: bool = false,
......@@ -1299,7 +1298,11 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
12991298 },
13001299 .fully_qualified_name = "compiler_rt",
13011300 .cc_argv = &.{},
1302 .inherited = .{},
1301 .inherited = .{
1302 .stack_check = false,
1303 .stack_protector = 0,
1304 .no_builtin = true,
1305 },
13031306 .global = options.config,
13041307 .parent = options.root_mod,
13051308 .builtin_mod = options.root_mod.getBuiltinDependency(),
......@@ -1353,7 +1356,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
13531356 cache.hash.add(options.config.link_mode);
13541357 cache.hash.add(options.function_sections);
13551358 cache.hash.add(options.data_sections);
1356 cache.hash.add(options.no_builtin);
13571359 cache.hash.add(link_libc);
13581360 cache.hash.add(options.config.link_libcpp);
13591361 cache.hash.add(options.config.link_libunwind);
......@@ -1490,7 +1492,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
14901492 .framework_dirs = options.framework_dirs,
14911493 .llvm_opt_bisect_limit = options.llvm_opt_bisect_limit,
14921494 .skip_linker_dependencies = options.skip_linker_dependencies,
1493 .no_builtin = options.no_builtin,
14941495 .job_queued_update_builtin_zig = have_zcu,
14951496 .function_sections = options.function_sections,
14961497 .data_sections = options.data_sections,
......@@ -5261,7 +5262,7 @@ pub fn addCCArgs(
52615262 try argv.append("-fdata-sections");
52625263 }
52635264
5264 if (comp.no_builtin) {
5265 if (mod.no_builtin) {
52655266 try argv.append("-fno-builtin");
52665267 }
52675268
......@@ -6154,7 +6155,6 @@ fn buildOutputFromZig(
61546155
61556156 assert(output_mode != .Exe);
61566157
6157 const unwind_tables = comp.link_eh_frame_hdr;
61586158 const strip = comp.compilerRtStrip();
61596159 const optimize_mode = comp.compilerRtOptMode();
61606160
......@@ -6168,7 +6168,6 @@ fn buildOutputFromZig(
61686168 .root_optimize_mode = optimize_mode,
61696169 .root_strip = strip,
61706170 .link_libc = comp.config.link_libc,
6171 .any_unwind_tables = unwind_tables,
61726171 });
61736172
61746173 const root_mod = try Package.Module.create(arena, .{
......@@ -6185,10 +6184,11 @@ fn buildOutputFromZig(
61856184 .stack_protector = 0,
61866185 .red_zone = comp.root_mod.red_zone,
61876186 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
6188 .unwind_tables = unwind_tables,
6187 .unwind_tables = comp.root_mod.unwind_tables,
61896188 .pic = comp.root_mod.pic,
61906189 .optimize_mode = optimize_mode,
61916190 .structured_cfg = comp.root_mod.structured_cfg,
6191 .no_builtin = true,
61926192 .code_model = comp.root_mod.code_model,
61936193 },
61946194 .global = config,
......@@ -6237,7 +6237,6 @@ fn buildOutputFromZig(
62376237 },
62386238 .function_sections = true,
62396239 .data_sections = true,
6240 .no_builtin = true,
62416240 .emit_h = null,
62426241 .verbose_cc = comp.verbose_cc,
62436242 .verbose_link = comp.verbose_link,
......@@ -6262,16 +6261,24 @@ fn buildOutputFromZig(
62626261 comp.queueLinkTaskMode(crt_file.full_object_path, output_mode);
62636262}
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
62656272pub fn build_crt_file(
62666273 comp: *Compilation,
62676274 root_name: []const u8,
62686275 output_mode: std.builtin.OutputMode,
6269 pic: ?bool,
62706276 misc_task_tag: MiscTask,
62716277 prog_node: std.Progress.Node,
62726278 /// These elements have to get mutated to add the owner module after it is
62736279 /// created within this function.
62746280 c_source_files: []CSourceFile,
6281 options: CrtFileOptions,
62756282) !void {
62766283 const tracy_trace = trace(@src());
62776284 defer tracy_trace.end();
......@@ -6316,13 +6323,16 @@ pub fn build_crt_file(
63166323 .sanitize_c = false,
63176324 .sanitize_thread = false,
63186325 .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,
63206328 .valgrind = false,
63216329 .unwind_tables = false,
6322 // Some CRT objects (rcrt1.o, Scrt1.o) are opinionated about PIC.
6323 .pic = pic orelse comp.root_mod.pic,
6330 // Some CRT objects (e.g. musl's rcrt1.o and Scrt1.o) are opinionated about PIC.
6331 .pic = options.pic orelse comp.root_mod.pic,
63246332 .optimize_mode = comp.compilerRtOptMode(),
63256333 .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,
63266336 },
63276337 .global = config,
63286338 .cc_argv = &.{},
......@@ -6350,6 +6360,8 @@ pub fn build_crt_file(
63506360 .directory = null, // Put it in the cache directory.
63516361 .basename = basename,
63526362 },
6363 .function_sections = options.function_sections orelse false,
6364 .data_sections = options.data_sections orelse false,
63536365 .emit_h = null,
63546366 .c_source_files = c_source_files,
63556367 .verbose_cc = comp.verbose_cc,
src/Package/Module.zig+12
......@@ -31,6 +31,7 @@ unwind_tables: bool,
3131cc_argv: []const []const u8,
3232/// (SPIR-V) whether to generate a structured control flow graph or not
3333structured_cfg: bool,
34no_builtin: bool,
3435
3536/// If the module is an `@import("builtin")` module, this is the `File` that
3637/// is preallocated for it. Otherwise this field is null.
......@@ -95,6 +96,7 @@ pub const CreateOptions = struct {
9596 sanitize_thread: ?bool = null,
9697 fuzz: ?bool = null,
9798 structured_cfg: ?bool = null,
99 no_builtin: ?bool = null,
98100 };
99101};
100102
......@@ -298,6 +300,13 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
298300 };
299301 };
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
301310 const llvm_cpu_features: ?[*:0]const u8 = b: {
302311 if (resolved_target.llvm_cpu_features) |x| break :b x;
303312 if (!options.global.use_llvm) break :b null;
......@@ -350,6 +359,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
350359 .unwind_tables = unwind_tables,
351360 .cc_argv = options.cc_argv,
352361 .structured_cfg = structured_cfg,
362 .no_builtin = no_builtin,
353363 .builtin_file = null,
354364 };
355365
......@@ -442,6 +452,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
442452 .unwind_tables = unwind_tables,
443453 .cc_argv = &.{},
444454 .structured_cfg = structured_cfg,
455 .no_builtin = no_builtin,
445456 .builtin_file = new_file,
446457 };
447458 new_file.* = .{
......@@ -502,6 +513,7 @@ pub fn createLimited(gpa: Allocator, options: LimitedOptions) Allocator.Error!*P
502513 .unwind_tables = undefined,
503514 .cc_argv = undefined,
504515 .structured_cfg = undefined,
516 .no_builtin = undefined,
505517 .builtin_file = null,
506518 };
507519 return mod;
src/codegen/llvm.zig+8-4
......@@ -3222,8 +3222,6 @@ pub const Object = struct {
32223222 owner_mod: *Package.Module,
32233223 omit_frame_pointer: bool,
32243224 ) Allocator.Error!void {
3225 const comp = o.pt.zcu.comp;
3226
32273225 if (!owner_mod.red_zone) {
32283226 try attributes.addFnAttr(.noredzone, &o.builder);
32293227 }
......@@ -3242,8 +3240,7 @@ pub const Object = struct {
32423240 if (owner_mod.unwind_tables) {
32433241 try attributes.addFnAttr(.{ .uwtable = Builder.Attribute.UwTable.default }, &o.builder);
32443242 }
3245 const target = owner_mod.resolved_target.result;
3246 if (comp.skip_linker_dependencies or comp.no_builtin or target.cpu.arch.isBpf()) {
3243 if (owner_mod.no_builtin) {
32473244 // The intent here is for compiler-rt and libc functions to not generate
32483245 // infinite recursion. For example, if we are compiling the memcpy function,
32493246 // and llvm detects that the body is equivalent to memcpy, it may replace the
......@@ -3258,6 +3255,7 @@ pub const Object = struct {
32583255 try attributes.addFnAttr(.minsize, &o.builder);
32593256 try attributes.addFnAttr(.optsize, &o.builder);
32603257 }
3258 const target = owner_mod.resolved_target.result;
32613259 if (target.cpu.model.llvm_name) |s| {
32623260 try attributes.addFnAttr(.{ .string = .{
32633261 .kind = try o.builder.string("target-cpu"),
......@@ -5578,6 +5576,10 @@ pub const FuncGen = struct {
55785576 var attributes: Builder.FunctionAttributes.Wip = .{};
55795577 defer attributes.deinit(&o.builder);
55805578
5579 if (self.ng.ownerModule().no_builtin) {
5580 try attributes.addFnAttr(.nobuiltin, &o.builder);
5581 }
5582
55815583 switch (modifier) {
55825584 .auto, .never_tail, .always_tail => {},
55835585 .never_inline => try attributes.addFnAttr(.@"noinline", &o.builder),
......@@ -12728,6 +12730,8 @@ fn backendSupportsF16(target: std.Target) bool {
1272812730 .mips64,
1272912731 .mips64el,
1273012732 .s390x,
12733 .sparc,
12734 .sparc64,
1273112735 => false,
1273212736 .arm,
1273312737 .armeb,
src/glibc.zig+4-5
......@@ -221,7 +221,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
221221 .owner = comp.root_mod,
222222 },
223223 };
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, .{});
225225 },
226226 .crtn_o => {
227227 var args = std.ArrayList([]const u8).init(arena);
......@@ -242,7 +242,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
242242 .owner = undefined,
243243 },
244244 };
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, .{});
246246 },
247247 .scrt1_o => {
248248 const start_o: Compilation.CSourceFile = blk: {
......@@ -295,7 +295,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
295295 };
296296 var files = [_]Compilation.CSourceFile{ start_o, abi_note_o, init_o };
297297 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, .{});
299299 },
300300 .libc_nonshared_a => {
301301 const s = path.sep_str;
......@@ -373,7 +373,6 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
373373 "-fmerge-all-constants",
374374 "-frounding-math",
375375 "-Wno-unsupported-floating-point-opt", // For targets that don't support -frounding-math.
376 "-fno-stack-protector",
377376 "-fno-common",
378377 "-fmath-errno",
379378 "-ftls-model=initial-exec",
......@@ -413,7 +412,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
413412 files_index += 1;
414413 }
415414 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, .{});
417416 },
418417 }
419418}
src/libcxx.zig+1-4
......@@ -195,7 +195,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
195195 .valgrind = false,
196196 .optimize_mode = optimize_mode,
197197 .structured_cfg = comp.root_mod.structured_cfg,
198 .pic = comp.root_mod.pic,
198 .pic = if (target_util.supports_fpic(target)) true else null,
199199 },
200200 .global = config,
201201 .cc_argv = &.{},
......@@ -278,9 +278,6 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
278278 try cflags.append("-faligned-allocation");
279279 }
280280
281 if (target_util.supports_fpic(target)) {
282 try cflags.append("-fPIC");
283 }
284281 try cflags.append("-nostdinc++");
285282 try cflags.append("-std=c++23");
286283 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
2929 const root_name = switch (target.os.tag) {
3030 // On Apple platforms, we use the same name as LLVM because the
3131 // TSAN library implementation hard-codes a check for these names.
32 .macos => "clang_rt.tsan_osx_dynamic",
33 .ios => switch (target.abi) {
34 .simulator => "clang_rt.tsan_iossim_dynamic",
35 else => "clang_rt.tsan_ios_dynamic",
36 },
32 .driverkit, .macos => "clang_rt.tsan_osx_dynamic",
33 .ios => if (target.abi == .simulator) "clang_rt.tsan_iossim_dynamic" else "clang_rt.tsan_ios_dynamic",
34 .tvos => if (target.abi == .simulator) "clang_rt.tsan_tvossim_dynamic" else "clang_rt.tsan_tvos_dynamic",
35 .visionos => if (target.abi == .simulator) "clang_rt.tsan_xrossim_dynamic" else "clang_rt.tsan_xros_dynamic",
36 .watchos => if (target.abi == .simulator) "clang_rt.tsan_watchossim_dynamic" else "clang_rt.tsan_watchos_dynamic",
3737 else => "tsan",
3838 };
3939 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
9393 .sanitize_c = false,
9494 .sanitize_thread = false,
9595 .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(),
9797 .valgrind = false,
9898 .optimize_mode = optimize_mode,
9999 .structured_cfg = comp.root_mod.structured_cfg,
100100 .pic = true,
101 .no_builtin = true,
101102 },
102103 .global = config,
103104 .cc_argv = &common_flags,
......@@ -123,10 +124,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
123124 try cflags.append("-I");
124125 try cflags.append(tsan_include_path);
125126
126 try cflags.append("-nostdinc++");
127 try cflags.append("-fvisibility-inlines-hidden");
128 try cflags.append("-std=c++17");
129 try cflags.append("-fno-rtti");
127 try addCcArgs(target, &cflags);
130128
131129 c_source_files.appendAssumeCapacity(.{
132130 .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
147145 try cflags.append("-I");
148146 try cflags.append(tsan_include_path);
149147
150 try cflags.append("-nostdinc++");
151 try cflags.append("-fvisibility-inlines-hidden");
152 try cflags.append("-std=c++17");
153 try cflags.append("-fno-rtti");
148 try addCcArgs(target, &cflags);
154149
155150 c_source_files.appendAssumeCapacity(.{
156151 .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
195190 try cflags.append("-I");
196191 try cflags.append(tsan_include_path);
197192
198 try cflags.append("-nostdinc++");
199 try cflags.append("-fvisibility-inlines-hidden");
200 try cflags.append("-std=c++17");
201 try cflags.append("-fno-rtti");
193 try addCcArgs(target, &cflags);
202194
203195 c_source_files.appendAssumeCapacity(.{
204196 .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
222214 try cflags.append("-I");
223215 try cflags.append(tsan_include_path);
224216
225 try cflags.append("-nostdinc++");
226 try cflags.append("-fvisibility-inlines-hidden");
227 try cflags.append("-std=c++17");
228 try cflags.append("-fno-rtti");
217 try addCcArgs(target, &cflags);
229218
230219 c_source_files.appendAssumeCapacity(.{
231220 .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
243232 try cflags.append("-I");
244233 try cflags.append(tsan_include_path);
245234
246 try cflags.append("-nostdinc++");
247 try cflags.append("-fvisibility-inlines-hidden");
248 try cflags.append("-std=c++17");
249 try cflags.append("-fno-rtti");
235 try addCcArgs(target, &cflags);
250236
251237 c_source_files.appendAssumeCapacity(.{
252238 .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
272258 try cflags.append("-I");
273259 try cflags.append(tsan_include_path);
274260
275 try cflags.append("-nostdinc++");
276 try cflags.append("-fvisibility-inlines-hidden");
277 try cflags.append("-std=c++17");
278 try cflags.append("-fno-rtti");
261 try addCcArgs(target, &cflags);
279262
280263 c_source_files.appendAssumeCapacity(.{
281264 .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
348331 comp.tsan_lib = crt_file;
349332}
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
351353const tsan_sources = [_][]const u8{
352354 "tsan_debugging.cpp",
353355 "tsan_external.cpp",
src/libunwind.zig+4-8
......@@ -46,6 +46,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
4646 );
4747 return error.SubCompilationFailed;
4848 };
49 const target = comp.root_mod.resolved_target.result;
4950 const root_mod = Module.create(arena, .{
5051 .global_cache_directory = comp.global_cache_directory,
5152 .paths = .{
......@@ -63,8 +64,9 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
6364 .valgrind = false,
6465 .sanitize_c = false,
6566 .sanitize_thread = false,
66 .unwind_tables = false,
67 .pic = comp.root_mod.pic,
67 // necessary so that libunwind can unwind through its own stack frames
68 .unwind_tables = true,
69 .pic = if (target_util.supports_fpic(target)) true else null,
6870 .optimize_mode = comp.compilerRtOptMode(),
6971 },
7072 .global = config,
......@@ -83,7 +85,6 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
8385
8486 const root_name = "unwind";
8587 const link_mode = .static;
86 const target = comp.root_mod.resolved_target.result;
8788 const basename = try std.zig.binNameAlloc(arena, .{
8889 .root_name = root_name,
8990 .target = target,
......@@ -114,16 +115,11 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
114115 try cflags.append("-fno-exceptions");
115116 try cflags.append("-I");
116117 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 }
120118 try cflags.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");
121119 try cflags.append("-Wa,--noexecstack");
122120 try cflags.append("-fvisibility=hidden");
123121 try cflags.append("-fvisibility-inlines-hidden");
124122 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
128124 // This is intentionally always defined because the macro definition means, should it only
129125 // 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(
810810 var compatibility_version: ?std.SemanticVersion = null;
811811 var function_sections = false;
812812 var data_sections = false;
813 var no_builtin = false;
814813 var listen: Listen = .none;
815814 var debug_compile_errors = false;
816815 var verbose_link = (native_os != .wasi or builtin.link_libc) and
......@@ -1550,9 +1549,9 @@ fn buildOutputType(
15501549 } else if (mem.eql(u8, arg, "-fno-data-sections")) {
15511550 data_sections = false;
15521551 } else if (mem.eql(u8, arg, "-fbuiltin")) {
1553 no_builtin = false;
1552 mod_opts.no_builtin = false;
15541553 } else if (mem.eql(u8, arg, "-fno-builtin")) {
1555 no_builtin = true;
1554 mod_opts.no_builtin = true;
15561555 } else if (mem.startsWith(u8, arg, "-fopt-bisect-limit=")) {
15571556 const next_arg = arg["-fopt-bisect-limit=".len..];
15581557 llvm_opt_bisect_limit = std.fmt.parseInt(c_int, next_arg, 0) catch |err|
......@@ -1963,8 +1962,8 @@ fn buildOutputType(
19631962 .no_function_sections => function_sections = false,
19641963 .data_sections => data_sections = true,
19651964 .no_data_sections => data_sections = false,
1966 .builtin => no_builtin = false,
1967 .no_builtin => no_builtin = true,
1965 .builtin => mod_opts.no_builtin = false,
1966 .no_builtin => mod_opts.no_builtin = true,
19681967 .color_diagnostics => color = .on,
19691968 .no_color_diagnostics => color = .off,
19701969 .stack_check => mod_opts.stack_check = true,
......@@ -3468,7 +3467,6 @@ fn buildOutputType(
34683467 .image_base = image_base,
34693468 .function_sections = function_sections,
34703469 .data_sections = data_sections,
3471 .no_builtin = no_builtin,
34723470 .clang_passthrough_mode = clang_passthrough_mode,
34733471 .clang_preprocessor_mode = clang_preprocessor_mode,
34743472 .version = optional_version,
src/mingw.zig+3-3
......@@ -41,7 +41,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
4141 .owner = undefined,
4242 },
4343 };
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, .{});
4545 },
4646
4747 .dllcrt2_o => {
......@@ -56,7 +56,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
5656 .owner = undefined,
5757 },
5858 };
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, .{});
6060 },
6161
6262 .mingw32_lib => {
......@@ -118,7 +118,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
118118 } else {
119119 @panic("unsupported arch");
120120 }
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, .{});
122122 },
123123 }
124124}
src/musl.zig+41-25
......@@ -38,7 +38,12 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
3838 .owner = undefined,
3939 },
4040 };
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 });
4247 },
4348 .crtn_o => {
4449 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
5055 .owner = undefined,
5156 },
5257 };
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 });
5464 },
5565 .crt1_o => {
5666 var args = std.ArrayList([]const u8).init(arena);
5767 try addCcArgs(comp, arena, &args, false);
58 try args.appendSlice(&[_][]const u8{
59 "-fno-stack-protector",
60 "-DCRT",
61 });
68 try args.append("-DCRT");
6269 var files = [_]Compilation.CSourceFile{
6370 .{
6471 .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
6875 .owner = undefined,
6976 },
7077 };
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 });
7284 },
7385 .rcrt1_o => {
7486 var args = std.ArrayList([]const u8).init(arena);
7587 try addCcArgs(comp, arena, &args, false);
76 try args.appendSlice(&[_][]const u8{
77 "-fno-stack-protector",
78 "-DCRT",
79 });
88 try args.append("-DCRT");
8089 var files = [_]Compilation.CSourceFile{
8190 .{
8291 .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
8695 .owner = undefined,
8796 },
8897 };
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 });
90105 },
91106 .scrt1_o => {
92107 var args = std.ArrayList([]const u8).init(arena);
93108 try addCcArgs(comp, arena, &args, false);
94 try args.appendSlice(&[_][]const u8{
95 "-fno-stack-protector",
96 "-DCRT",
97 });
109 try args.append("-DCRT");
98110 var files = [_]Compilation.CSourceFile{
99111 .{
100112 .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
104116 .owner = undefined,
105117 },
106118 };
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 });
108126 },
109127 .libc_a => {
110128 // 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
197215 .owner = undefined,
198216 };
199217 }
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 });
201224 },
202225 .libc_so => {
203226 const optimize_mode = comp.compilerRtOptMode();
......@@ -410,7 +433,6 @@ fn addCcArgs(
410433 try args.appendSlice(&[_][]const u8{
411434 "-std=c99",
412435 "-ffreestanding",
413 "-fno-builtin",
414436 "-fexcess-precision=standard",
415437 "-frounding-math",
416438 "-ffp-contract=off",
......@@ -441,12 +463,6 @@ fn addCcArgs(
441463
442464 o_arg,
443465
444 "-fomit-frame-pointer",
445 "-fno-unwind-tables",
446 "-fno-asynchronous-unwind-tables",
447 "-ffunction-sections",
448 "-fdata-sections",
449
450466 "-Qunused-arguments",
451467 "-w", // disable all warnings
452468 });
src/target.zig+2-3
......@@ -327,9 +327,8 @@ pub fn libcFullLinkFlags(target: std.Target) []const []const u8 {
327327}
328328
329329pub fn clangMightShellOutForAssembly(target: std.Target) bool {
330 // Clang defaults to using the system assembler over the internal one
331 // when targeting a non-BSD OS.
332 return target.cpu.arch.isSPARC();
330 // Clang defaults to using the system assembler in some cases.
331 return target.cpu.arch.isNvptx() or target.cpu.arch == .xcore;
333332}
334333
335334/// 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
8181 .owner = undefined,
8282 },
8383 };
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, .{});
8585 },
8686 .crt1_command_o => {
8787 var args = std.ArrayList([]const u8).init(arena);
......@@ -96,7 +96,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
9696 .owner = undefined,
9797 },
9898 };
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, .{});
100100 },
101101 .libc_a => {
102102 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
150150 }
151151 }
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, .{});
154154 },
155155 .libwasi_emulated_process_clocks_a => {
156156 var args = std.ArrayList([]const u8).init(arena);
......@@ -167,7 +167,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
167167 .owner = undefined,
168168 });
169169 }
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, .{});
171171 },
172172 .libwasi_emulated_getpid_a => {
173173 var args = std.ArrayList([]const u8).init(arena);
......@@ -184,7 +184,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
184184 .owner = undefined,
185185 });
186186 }
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, .{});
188188 },
189189 .libwasi_emulated_mman_a => {
190190 var args = std.ArrayList([]const u8).init(arena);
......@@ -201,7 +201,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
201201 .owner = undefined,
202202 });
203203 }
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, .{});
205205 },
206206 .libwasi_emulated_signal_a => {
207207 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
238238 }
239239 }
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, .{});
242242 },
243243 }
244244}
......@@ -279,7 +279,6 @@ fn addCCArgs(
279279 try args.appendSlice(&[_][]const u8{
280280 "-std=gnu17",
281281 "-fno-trapping-math",
282 "-fno-stack-protector",
283282 "-w", // ignore all warnings
284283
285284 o_arg,