authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-13 18:28:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:19-07:00
log33cdf33b95676dca9f91216e0f8742f9cf7e84fb
tree59ccf012a4fc839def23bce321c1735b6bdf5651
parentb162c3c820d8a6446bbdfba90b3c135ebcd76fcb

compiler: update many references to bin_file.options


11 files changed, 319 insertions(+), 231 deletions(-)

src/Compilation.zig+197-173
......@@ -77,6 +77,7 @@ system_libs: std.StringArrayHashMapUnmanaged(SystemLib),
7777version: ?std.SemanticVersion,
7878libc_installation: ?*const LibCInstallation,
7979skip_linker_dependencies: bool,
80no_builtin: bool,
8081
8182c_object_table: std.AutoArrayHashMapUnmanaged(*CObject, void) = .{},
8283win32_resource_table: if (build_options.only_core_functionality) void else std.AutoArrayHashMapUnmanaged(*Win32Resource, void) =
......@@ -988,10 +989,9 @@ pub const InitOptions = struct {
988989 linker_tsaware: bool = false,
989990 linker_nxcompat: bool = false,
990991 linker_dynamicbase: bool = true,
991 linker_optimization: ?u8 = null,
992 linker_compress_debug_sections: ?link.CompressDebugSections = null,
992 linker_compress_debug_sections: ?link.File.Elf.CompressDebugSections = null,
993993 linker_module_definition_file: ?[]const u8 = null,
994 linker_sort_section: ?link.SortSection = null,
994 linker_sort_section: ?link.File.Elf.SortSection = null,
995995 major_subsystem_version: ?u32 = null,
996996 minor_subsystem_version: ?u32 = null,
997997 clang_passthrough_mode: bool = false,
......@@ -1011,7 +1011,7 @@ pub const InitOptions = struct {
10111011 /// building such dependencies themselves, this flag must be set to avoid
10121012 /// infinite recursion.
10131013 skip_linker_dependencies: bool = false,
1014 hash_style: link.HashStyle = .both,
1014 hash_style: link.File.Elf.HashStyle = .both,
10151015 entry: ?[]const u8 = null,
10161016 force_undefined_symbols: std.StringArrayHashMapUnmanaged(void) = .{},
10171017 stack_size: ?u64 = null,
......@@ -1049,8 +1049,6 @@ pub const InitOptions = struct {
10491049 /// (Windows) PDB output path
10501050 pdb_out_path: ?[]const u8 = null,
10511051 error_limit: ?Compilation.Module.ErrorInt = null,
1052 /// (SPIR-V) whether to generate a structured control flow graph or not
1053 want_structured_cfg: ?bool = null,
10541052};
10551053
10561054fn addModuleTableToCacheHash(
......@@ -1277,10 +1275,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
12771275 }
12781276 }
12791277
1280 const linker_optimization: u8 = options.linker_optimization orelse switch (options.root_mod.optimize_mode) {
1281 .Debug => @as(u8, 0),
1282 else => @as(u8, 3),
1283 };
12841278 // TODO: https://github.com/ziglang/zig/issues/17969
12851279 const formatted_panics = options.formatted_panics orelse (options.root_mod.optimize_mode == .Debug);
12861280
......@@ -1359,7 +1353,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13591353 hash.add(formatted_panics);
13601354 hash.add(options.emit_h != null);
13611355 hash.add(error_limit);
1362 hash.addOptional(options.want_structured_cfg);
13631356
13641357 // In the case of incremental cache mode, this `zig_cache_artifact_directory`
13651358 // is computed based on a hash of non-linker inputs, and it is where all
......@@ -1641,13 +1634,13 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16411634 .framework_dirs = options.framework_dirs,
16421635 .llvm_opt_bisect_limit = options.llvm_opt_bisect_limit,
16431636 .skip_linker_dependencies = options.skip_linker_dependencies,
1637 .no_builtin = options.no_builtin,
16441638 };
16451639
16461640 if (bin_file_emit) |emit| {
16471641 comp.bin_file = try link.File.open(arena, .{
16481642 .comp = comp,
16491643 .emit = emit,
1650 .optimization = linker_optimization,
16511644 .linker_script = options.linker_script,
16521645 .z_nodelete = options.linker_z_nodelete,
16531646 .z_notext = options.linker_z_notext,
......@@ -1666,7 +1659,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16661659 .symbol_wrap_set = options.symbol_wrap_set,
16671660 .function_sections = options.function_sections,
16681661 .data_sections = options.data_sections,
1669 .no_builtin = options.no_builtin,
16701662 .allow_shlib_undefined = options.linker_allow_shlib_undefined,
16711663 .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
16721664 .compress_debug_sections = options.linker_compress_debug_sections orelse .none,
......@@ -1713,7 +1705,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
17131705 .force_undefined_symbols = options.force_undefined_symbols,
17141706 .pdb_source_path = options.pdb_source_path,
17151707 .pdb_out_path = options.pdb_out_path,
1716 .want_structured_cfg = options.want_structured_cfg,
17171708 .entry_addr = null, // CLI does not expose this option (yet?)
17181709 });
17191710 }
......@@ -2076,7 +2067,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
20762067 };
20772068 if (is_hit) {
20782069 comp.last_update_was_cache_hit = true;
2079 log.debug("CacheMode.whole cache hit for {s}", .{comp.bin_file.options.root_name});
2070 log.debug("CacheMode.whole cache hit for {s}", .{comp.root_name});
20802071 const digest = man.final();
20812072
20822073 comp.wholeCacheModeSetBinFilePath(&digest);
......@@ -2085,7 +2076,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
20852076 comp.bin_file.lock = man.toOwnedLock();
20862077 return;
20872078 }
2088 log.debug("CacheMode.whole cache miss for {s}", .{comp.bin_file.options.root_name});
2079 log.debug("CacheMode.whole cache miss for {s}", .{comp.root_name});
20892080
20902081 // Initialize `bin_file.emit` with a temporary Directory so that compilation can
20912082 // continue on the same path as incremental, using the temporary Directory.
......@@ -2169,7 +2160,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
21692160 // import_table here.
21702161 // Likewise, in the case of `zig test`, the test runner is the root source file,
21712162 // and so there is nothing to import the main file.
2172 if (comp.bin_file.options.is_test) {
2163 if (comp.config.is_test) {
21732164 _ = try module.importPkg(module.main_mod);
21742165 }
21752166
......@@ -2194,7 +2185,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
21942185 }
21952186
21962187 try comp.work_queue.writeItem(.{ .analyze_mod = std_mod });
2197 if (comp.bin_file.options.is_test) {
2188 if (comp.config.is_test) {
21982189 try comp.work_queue.writeItem(.{ .analyze_mod = module.main_mod });
21992190 }
22002191
......@@ -2208,20 +2199,20 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
22082199 if (comp.module) |module| {
22092200 if (builtin.mode == .Debug and comp.verbose_intern_pool) {
22102201 std.debug.print("intern pool stats for '{s}':\n", .{
2211 comp.bin_file.options.root_name,
2202 comp.root_name,
22122203 });
22132204 module.intern_pool.dump();
22142205 }
22152206
22162207 if (builtin.mode == .Debug and comp.verbose_generic_instances) {
22172208 std.debug.print("generic instances for '{s}:0x{x}':\n", .{
2218 comp.bin_file.options.root_name,
2209 comp.root_name,
22192210 @as(usize, @intFromPtr(module)),
22202211 });
22212212 module.intern_pool.dumpGenericInstances(comp.gpa);
22222213 }
22232214
2224 if (comp.bin_file.options.is_test and comp.totalErrorCount() == 0) {
2215 if (comp.config.is_test and comp.totalErrorCount() == 0) {
22252216 // The `test_functions` decl has been intentionally postponed until now,
22262217 // at which point we must populate it with the list of test functions that
22272218 // have been discovered and not filtered out.
......@@ -2336,7 +2327,7 @@ fn maybeGenerateAutodocs(comp: *Compilation, prog_node: *std.Progress.Node) !voi
23362327 // file copies at the end of generate() can also be extracted to
23372328 // separate jobs
23382329 if (!build_options.only_c and !build_options.only_core_functionality) {
2339 if (comp.bin_file.options.docs_emit) |emit| {
2330 if (comp.docs_emit) |emit| {
23402331 var dir = try emit.directory.handle.makeOpenPath(emit.sub_path, .{});
23412332 defer dir.close();
23422333
......@@ -2426,24 +2417,16 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24262417 try addModuleTableToCacheHash(gpa, arena, &man.hash, mod.main_mod, .{ .files = man });
24272418
24282419 // Synchronize with other matching comments: ZigOnlyHashStuff
2429 man.hash.add(comp.config.use_llvm);
2430 man.hash.add(comp.config.use_lib_llvm);
2431 man.hash.add(comp.bin_file.options.dll_export_fns);
2432 man.hash.add(comp.bin_file.options.is_test);
2433 man.hash.add(comp.test_evented_io);
2420 man.hash.add(comp.config.test_evented_io);
24342421 man.hash.addOptionalBytes(comp.test_filter);
24352422 man.hash.addOptionalBytes(comp.test_name_prefix);
24362423 man.hash.add(comp.skip_linker_dependencies);
24372424 man.hash.add(comp.formatted_panics);
24382425 man.hash.add(mod.emit_h != null);
24392426 man.hash.add(mod.error_limit);
2440 man.hash.add(comp.bin_file.options.want_structured_cfg);
24412427 }
24422428
2443 try man.addOptionalFile(comp.bin_file.options.linker_script);
2444 try man.addOptionalFile(comp.bin_file.options.version_script);
2445
2446 for (comp.bin_file.options.objects) |obj| {
2429 for (comp.objects) |obj| {
24472430 _ = try man.addFile(obj.path, null);
24482431 man.hash.add(obj.must_link);
24492432 man.hash.add(obj.loption);
......@@ -2469,39 +2452,19 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24692452 }
24702453 }
24712454
2455 man.hash.addOptionalBytes(comp.sysroot);
2456 man.hash.addOptional(comp.version);
24722457 man.hash.addListOfBytes(comp.rc_include_dir_list);
24732458
24742459 cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_asm);
24752460 cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_llvm_ir);
24762461 cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_llvm_bc);
24772462
2478 man.hash.add(comp.bin_file.stack_size);
2479 man.hash.add(comp.bin_file.options.gc_sections);
2480 man.hash.add(comp.bin_file.options.eh_frame_hdr);
2481 man.hash.add(comp.bin_file.options.emit_relocs);
2482 man.hash.add(comp.bin_file.options.rdynamic);
2483 man.hash.addListOfBytes(comp.bin_file.options.lib_dirs);
2484 man.hash.addListOfBytes(comp.bin_file.options.rpath_list);
2485 man.hash.addListOfBytes(comp.bin_file.options.symbol_wrap_set.keys());
2486 man.hash.add(comp.bin_file.options.each_lib_rpath);
2487 man.hash.add(comp.bin_file.build_id);
24882463 man.hash.add(comp.skip_linker_dependencies);
2489 man.hash.add(comp.bin_file.options.z_nodelete);
2490 man.hash.add(comp.bin_file.options.z_notext);
2491 man.hash.add(comp.bin_file.options.z_defs);
2492 man.hash.add(comp.bin_file.options.z_origin);
2493 man.hash.add(comp.bin_file.options.z_nocopyreloc);
2494 man.hash.add(comp.bin_file.options.z_now);
2495 man.hash.add(comp.bin_file.options.z_relro);
2496 man.hash.add(comp.bin_file.options.z_common_page_size orelse 0);
2497 man.hash.add(comp.bin_file.options.z_max_page_size orelse 0);
2498 man.hash.add(comp.bin_file.options.hash_style);
2499 man.hash.add(comp.bin_file.options.compress_debug_sections);
25002464 man.hash.add(comp.include_compiler_rt);
2501 man.hash.add(comp.bin_file.options.sort_section);
25022465 if (comp.config.link_libc) {
2503 man.hash.add(comp.bin_file.options.libc_installation != null);
2504 if (comp.bin_file.options.libc_installation) |libc_installation| {
2466 man.hash.add(comp.libc_installation != null);
2467 if (comp.libc_installation) |libc_installation| {
25052468 man.hash.addOptionalBytes(libc_installation.crt_dir);
25062469 if (target.abi == .msvc) {
25072470 man.hash.addOptionalBytes(libc_installation.msvc_lib_dir);
......@@ -2510,55 +2473,93 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
25102473 }
25112474 man.hash.addOptionalBytes(target.dynamic_linker.get());
25122475 }
2513 man.hash.addOptionalBytes(comp.bin_file.options.soname);
2514 man.hash.add(comp.bin_file.options.version);
25152476 try link.hashAddSystemLibs(man, comp.system_libs);
2516 man.hash.addListOfBytes(comp.bin_file.options.force_undefined_symbols.keys());
2517 man.hash.add(comp.bin_file.allow_shlib_undefined);
2518 man.hash.add(comp.bin_file.options.bind_global_refs_locally);
2519 man.hash.add(comp.bin_file.options.tsan);
2520 man.hash.addOptionalBytes(comp.bin_file.options.sysroot);
2521 man.hash.add(comp.bin_file.options.linker_optimization);
2522
2523 switch (comp.bin_file.tag) {
2524 .elf => {
2525 const elf = comp.bin_file.cast(link.File.Elf).?;
2526 man.hash.add(elf.image_base);
2527 },
2528 .wasm => {
2529 const wasm = comp.bin_file.cast(link.File.Wasm).?;
2530 man.hash.add(comp.config.import_memory);
2531 man.hash.add(comp.config.export_memory);
2532 man.hash.add(comp.config.shared_memory);
2533 man.hash.add(wasm.initial_memory);
2534 man.hash.add(wasm.max_memory);
2535 man.hash.add(wasm.global_base);
2536 },
2537 .macho => {
2538 const macho = comp.bin_file.cast(link.File.MachO).?;
2539 man.hash.addListOfBytes(comp.framework_dirs);
2540 try link.hashAddFrameworks(man, macho.frameworks);
2541 try man.addOptionalFile(macho.entitlements);
2542 man.hash.add(macho.pagezero_size);
2543 man.hash.add(macho.headerpad_size);
2544 man.hash.add(macho.headerpad_max_install_names);
2545 man.hash.add(macho.dead_strip_dylibs);
2546 },
2547 .coff => {
2548 const coff = comp.bin_file.cast(link.File.Coff).?;
2549 man.hash.add(coff.image_base);
2550 man.hash.add(coff.subsystem);
2551 man.hash.add(coff.tsaware);
2552 man.hash.add(coff.nxcompat);
2553 man.hash.add(coff.dynamicbase);
2554 man.hash.add(coff.major_subsystem_version);
2555 man.hash.add(coff.minor_subsystem_version);
2556 },
2477
2478 man.hash.add(comp.config.use_llvm);
2479 man.hash.add(comp.config.use_lib_llvm);
2480 man.hash.add(comp.config.is_test);
2481 man.hash.add(comp.config.import_memory);
2482 man.hash.add(comp.config.export_memory);
2483 man.hash.add(comp.config.shared_memory);
2484
2485 if (comp.bin_file) |lf| {
2486 man.hash.add(lf.stack_size);
2487 man.hash.add(lf.gc_sections);
2488 man.hash.addListOfBytes(lf.rpath_list);
2489 man.hash.add(lf.build_id);
2490 man.hash.addListOfBytes(lf.force_undefined_symbols.keys());
2491 man.hash.add(lf.allow_shlib_undefined);
2492
2493 switch (lf.tag) {
2494 .elf => {
2495 const elf = lf.cast(link.File.Elf).?;
2496 man.hash.add(elf.rdynamic);
2497 man.hash.add(elf.eh_frame_hdr);
2498 man.hash.add(elf.image_base);
2499 man.hash.add(elf.emit_relocs);
2500 man.hash.add(elf.z_nodelete);
2501 man.hash.add(elf.z_notext);
2502 man.hash.add(elf.z_defs);
2503 man.hash.add(elf.z_origin);
2504 man.hash.add(elf.z_nocopyreloc);
2505 man.hash.add(elf.z_now);
2506 man.hash.add(elf.z_relro);
2507 man.hash.add(elf.z_common_page_size orelse 0);
2508 man.hash.add(elf.z_max_page_size orelse 0);
2509 man.hash.addListOfBytes(elf.lib_dirs);
2510 man.hash.add(elf.hash_style);
2511 man.hash.add(elf.compress_debug_sections);
2512 man.hash.addListOfBytes(elf.symbol_wrap_set.keys());
2513 man.hash.add(elf.each_lib_rpath);
2514 man.hash.addOptional(elf.sort_section);
2515 man.hash.addOptionalBytes(elf.soname);
2516 man.hash.add(elf.bind_global_refs_locally);
2517 try man.addOptionalFile(elf.linker_script);
2518 try man.addOptionalFile(elf.version_script);
2519 },
2520 .wasm => {
2521 const wasm = lf.cast(link.File.Wasm).?;
2522 man.hash.add(wasm.rdynamic);
2523 man.hash.add(wasm.initial_memory);
2524 man.hash.add(wasm.max_memory);
2525 man.hash.add(wasm.global_base);
2526 },
2527 .macho => {
2528 const macho = lf.cast(link.File.MachO).?;
2529 man.hash.addListOfBytes(comp.framework_dirs);
2530 try link.File.MachO.hashAddFrameworks(man, macho.frameworks);
2531 try man.addOptionalFile(macho.entitlements);
2532 man.hash.add(macho.pagezero_vmsize);
2533 man.hash.add(macho.headerpad_size);
2534 man.hash.add(macho.headerpad_max_install_names);
2535 man.hash.add(macho.dead_strip_dylibs);
2536 },
2537 .coff => {
2538 const coff = lf.cast(link.File.Coff).?;
2539 man.hash.add(coff.dll_export_fns);
2540 man.hash.add(coff.image_base);
2541 man.hash.addOptional(coff.subsystem);
2542 man.hash.add(coff.tsaware);
2543 man.hash.add(coff.nxcompat);
2544 man.hash.add(coff.dynamicbase);
2545 man.hash.add(coff.major_subsystem_version);
2546 man.hash.add(coff.minor_subsystem_version);
2547 man.hash.addListOfBytes(coff.lib_dirs);
2548 },
2549 .spirv => {
2550 const spirv = lf.cast(link.File.SpirV).?;
2551 _ = spirv;
2552 // TODO
2553 },
2554 .c => {}, // TODO
2555 .plan9 => {}, // TODO
2556 .nvptx => {}, // TODO
2557 }
25572558 }
25582559}
25592560
25602561fn emitOthers(comp: *Compilation) void {
2561 if (comp.bin_file.options.output_mode != .Obj or comp.module != null or
2562 if (comp.config.output_mode != .Obj or comp.module != null or
25622563 comp.c_object_table.count() == 0)
25632564 {
25642565 return;
......@@ -2724,10 +2725,10 @@ pub fn saveState(comp: *Compilation) !void {
27242725 var bufs_list: [6]std.os.iovec_const = undefined;
27252726 var bufs_len: usize = 0;
27262727
2727 const emit = comp.bin_file.options.emit orelse return;
2728 const lf = comp.bin_file orelse return;
27282729
2729 if (comp.module) |mod| {
2730 const ip = &mod.intern_pool;
2730 if (comp.module) |zcu| {
2731 const ip = &zcu.intern_pool;
27312732 const header: Header = .{
27322733 .intern_pool = .{
27332734 .items_len = @intCast(ip.items.len),
......@@ -2751,7 +2752,7 @@ pub fn saveState(comp: *Compilation) !void {
27512752 }
27522753 var basename_buf: [255]u8 = undefined;
27532754 const basename = std.fmt.bufPrint(&basename_buf, "{s}.zcs", .{
2754 comp.bin_file.options.root_name,
2755 comp.root_name,
27552756 }) catch o: {
27562757 basename_buf[basename_buf.len - 4 ..].* = ".zcs".*;
27572758 break :o &basename_buf;
......@@ -2759,7 +2760,7 @@ pub fn saveState(comp: *Compilation) !void {
27592760
27602761 // Using an atomic file prevents a crash or power failure from corrupting
27612762 // the previous incremental compilation state.
2762 var af = try emit.directory.handle.atomicFile(basename, .{});
2763 var af = try lf.emit.directory.handle.atomicFile(basename, .{});
27632764 defer af.deinit();
27642765 try af.file.pwritevAll(bufs_list[0..bufs_len], 0);
27652766 try af.finish();
......@@ -3507,7 +3508,7 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
35073508 error.AnalysisFail => return,
35083509 };
35093510 const decl = module.declPtr(decl_index);
3510 if (decl.kind == .@"test" and comp.bin_file.options.is_test) {
3511 if (decl.kind == .@"test" and comp.config.is_test) {
35113512 // Tests are always emitted in test binaries. The decl_refs are created by
35123513 // Module.populateTestFunctions, but this will not queue body analysis, so do
35133514 // that now.
......@@ -3850,7 +3851,7 @@ pub fn obtainCObjectCacheManifest(comp: *const Compilation) Cache.Manifest {
38503851 // based on the zig library directory alone. The zig lib directory file
38513852 // path is purposefully either in the cache or not in the cache. The
38523853 // decision should not be overridden here.
3853 if (comp.bin_file.options.libc_installation != null) {
3854 if (comp.libc_installation != null) {
38543855 man.hash.addListOfBytes(comp.libc_include_dir_list);
38553856 }
38563857
......@@ -4248,9 +4249,9 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
42484249 // file and building an object we need to link them together, but with just one it should go
42494250 // directly to the output file.
42504251 const direct_o = comp.c_source_files.len == 1 and comp.module == null and
4251 comp.bin_file.options.output_mode == .Obj and comp.bin_file.options.objects.len == 0;
4252 comp.config.output_mode == .Obj and comp.objects.len == 0;
42524253 const o_basename_noext = if (direct_o)
4253 comp.bin_file.options.root_name
4254 comp.root_name
42544255 else
42554256 c_source_basename[0 .. c_source_basename.len - std.fs.path.extension(c_source_basename).len];
42564257
......@@ -4304,8 +4305,8 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
43044305 try argv.appendSlice(c_object.src.extra_flags);
43054306 try argv.appendSlice(c_object.src.cache_exempt_flags);
43064307
4307 const out_obj_path = if (comp.bin_file.options.emit) |emit|
4308 try emit.directory.join(arena, &.{emit.sub_path})
4308 const out_obj_path = if (comp.bin_file) |lf|
4309 try lf.emit.directory.join(arena, &.{lf.emit.sub_path})
43094310 else
43104311 "/dev/null";
43114312
......@@ -4883,8 +4884,9 @@ pub fn addCCArgs(
48834884 argv: *std.ArrayList([]const u8),
48844885 ext: FileExt,
48854886 out_dep_path: ?[]const u8,
4887 mod: *Package.Module,
48864888) !void {
4887 const target = comp.getTarget();
4889 const target = mod.resolved_target.result;
48884890
48894891 // As of Clang 16.x, it will by default read extra flags from /etc/clang.
48904892 // I'm sure the person who implemented this means well, but they have a lot
......@@ -4905,15 +4907,15 @@ pub fn addCCArgs(
49054907 try argv.append("-fno-caret-diagnostics");
49064908 }
49074909
4908 if (comp.bin_file.options.function_sections) {
4910 if (comp.bin_file.function_sections) {
49094911 try argv.append("-ffunction-sections");
49104912 }
49114913
4912 if (comp.bin_file.options.data_sections) {
4914 if (comp.bin_file.data_sections) {
49134915 try argv.append("-fdata-sections");
49144916 }
49154917
4916 if (comp.bin_file.options.no_builtin) {
4918 if (comp.no_builtin) {
49174919 try argv.append("-fno-builtin");
49184920 }
49194921
......@@ -4953,7 +4955,7 @@ pub fn addCCArgs(
49534955 }));
49544956 }
49554957
4956 if (comp.bin_file.options.link_libunwind) {
4958 if (comp.config.link_libunwind) {
49574959 const libunwind_include_path = try std.fs.path.join(arena, &[_][]const u8{
49584960 comp.zig_lib_directory.path.?, "libunwind", "include",
49594961 });
......@@ -4987,7 +4989,7 @@ pub fn addCCArgs(
49874989 "-nostdinc",
49884990 "-fno-spell-checking",
49894991 });
4990 if (comp.bin_file.options.lto) {
4992 if (comp.config.lto) {
49914993 try argv.append("-flto");
49924994 }
49934995
......@@ -5042,9 +5044,8 @@ pub fn addCCArgs(
50425044 argv.appendAssumeCapacity(arg);
50435045 }
50445046 }
5045 const code_model = comp.bin_file.options.machine_code_model;
5046 if (code_model != .default) {
5047 try argv.append(try std.fmt.allocPrint(arena, "-mcmodel={s}", .{@tagName(code_model)}));
5047 if (mod.code_model != .default) {
5048 try argv.append(try std.fmt.allocPrint(arena, "-mcmodel={s}", .{@tagName(mod.code_model)}));
50485049 }
50495050
50505051 switch (target.os.tag) {
......@@ -5093,7 +5094,7 @@ pub fn addCCArgs(
50935094 try argv.append("-mthumb");
50945095 }
50955096
5096 if (comp.sanitize_c and !comp.bin_file.options.tsan) {
5097 if (mod.sanitize_c and !mod.sanitize_thread) {
50975098 try argv.append("-fsanitize=undefined");
50985099 try argv.append("-fsanitize-trap=undefined");
50995100 // It is very common, and well-defined, for a pointer on one side of a C ABI
......@@ -5104,27 +5105,27 @@ pub fn addCCArgs(
51045105 // Without this flag, Clang would invoke UBSAN when such an extern
51055106 // function was called.
51065107 try argv.append("-fno-sanitize=function");
5107 } else if (comp.sanitize_c and comp.bin_file.options.tsan) {
5108 } else if (mod.sanitize_c and mod.sanitize_thread) {
51085109 try argv.append("-fsanitize=undefined,thread");
51095110 try argv.append("-fsanitize-trap=undefined");
51105111 try argv.append("-fno-sanitize=function");
5111 } else if (!comp.sanitize_c and comp.bin_file.options.tsan) {
5112 } else if (!mod.sanitize_c and mod.sanitize_thread) {
51125113 try argv.append("-fsanitize=thread");
51135114 }
51145115
5115 if (comp.bin_file.options.red_zone) {
5116 if (mod.red_zone) {
51165117 try argv.append("-mred-zone");
51175118 } else if (target_util.hasRedZone(target)) {
51185119 try argv.append("-mno-red-zone");
51195120 }
51205121
5121 if (comp.bin_file.options.omit_frame_pointer) {
5122 if (mod.omit_frame_pointer) {
51225123 try argv.append("-fomit-frame-pointer");
51235124 } else {
51245125 try argv.append("-fno-omit-frame-pointer");
51255126 }
51265127
5127 const ssp_buf_size = comp.bin_file.options.stack_protector;
5128 const ssp_buf_size = mod.stack_protector;
51285129 if (ssp_buf_size != 0) {
51295130 try argv.appendSlice(&[_][]const u8{
51305131 "-fstack-protector-strong",
......@@ -5135,7 +5136,7 @@ pub fn addCCArgs(
51355136 try argv.append("-fno-stack-protector");
51365137 }
51375138
5138 switch (comp.bin_file.options.optimize_mode) {
5139 switch (mod.optimize_mode) {
51395140 .Debug => {
51405141 // windows c runtime requires -D_DEBUG if using debug libraries
51415142 try argv.append("-D_DEBUG");
......@@ -5165,7 +5166,7 @@ pub fn addCCArgs(
51655166 },
51665167 }
51675168
5168 if (target_util.supports_fpic(target) and comp.bin_file.options.pic) {
5169 if (target_util.supports_fpic(target) and mod.pic) {
51695170 try argv.append("-fPIC");
51705171 }
51715172
......@@ -5940,7 +5941,7 @@ pub fn get_libc_crt_file(comp: *Compilation, arena: Allocator, basename: []const
59405941 {
59415942 return comp.crt_files.get(basename).?.full_object_path;
59425943 }
5943 const lci = comp.bin_file.options.libc_installation orelse return error.LibCInstallationNotAvailable;
5944 const lci = comp.libc_installation orelse return error.LibCInstallationNotAvailable;
59445945 const crt_dir_path = lci.crt_dir orelse return error.LibCInstallationMissingCRTDir;
59455946 const full_path = try std.fs.path.join(arena, &[_][]const u8{ crt_dir_path, basename });
59465947 return full_path;
......@@ -6112,7 +6113,7 @@ fn canBuildZigLibC(target: std.Target, use_llvm: bool) bool {
61126113
61136114pub fn getZigBackend(comp: Compilation) std.builtin.CompilerBackend {
61146115 const target = comp.root_mod.resolved_target.result;
6115 return target_util.zigBackend(target, comp.bin_file.options.use_llvm);
6116 return target_util.zigBackend(target, comp.config.use_llvm);
61166117}
61176118
61186119pub fn updateSubCompilation(
......@@ -6165,6 +6166,9 @@ fn buildOutputFromZig(
61656166
61666167 assert(output_mode != .Exe);
61676168
6169 const lf = comp.bin_file.?;
6170 const unwind_tables = if (lf.cast(link.File.Elf)) |elf| elf.eh_frame_hdr else false;
6171
61686172 const config = try Config.resolve(.{
61696173 .output_mode = output_mode,
61706174 .resolved_target = comp.root_mod.resolved_target,
......@@ -6173,6 +6177,7 @@ fn buildOutputFromZig(
61736177 .emit_bin = true,
61746178 .root_optimize_mode = comp.compilerRtOptMode(),
61756179 .link_libc = comp.config.link_libc,
6180 .any_unwind_tables = unwind_tables,
61766181 });
61776182
61786183 const root_mod = Package.Module.create(.{
......@@ -6187,9 +6192,10 @@ fn buildOutputFromZig(
61876192 .stack_protector = 0,
61886193 .red_zone = comp.root_mod.red_zone,
61896194 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
6190 .unwind_tables = comp.bin_file.options.eh_frame_hdr,
6195 .unwind_tables = unwind_tables,
61916196 .pic = comp.root_mod.pic,
61926197 .optimize_mode = comp.compilerRtOptMode(),
6198 .structured_cfg = comp.root_mod.structured_cfg,
61936199 },
61946200 .global = config,
61956201 .cc_argv = &.{},
......@@ -6210,21 +6216,21 @@ fn buildOutputFromZig(
62106216 .global_cache_directory = comp.global_cache_directory,
62116217 .local_cache_directory = comp.global_cache_directory,
62126218 .zig_lib_directory = comp.zig_lib_directory,
6219 .self_exe_path = comp.self_exe_path,
62136220 .resolved = config,
62146221 .root_mod = root_mod,
62156222 .cache_mode = .whole,
62166223 .root_name = root_name,
62176224 .thread_pool = comp.thread_pool,
6218 .libc_installation = comp.bin_file.options.libc_installation,
6225 .libc_installation = comp.libc_installation,
62196226 .emit_bin = emit_bin,
62206227 .link_mode = .Static,
62216228 .function_sections = true,
62226229 .data_sections = true,
62236230 .no_builtin = true,
62246231 .emit_h = null,
6225 .self_exe_path = comp.self_exe_path,
62266232 .verbose_cc = comp.verbose_cc,
6227 .verbose_link = comp.bin_file.options.verbose_link,
6233 .verbose_link = comp.verbose_link,
62286234 .verbose_air = comp.verbose_air,
62296235 .verbose_intern_pool = comp.verbose_intern_pool,
62306236 .verbose_generic_instances = comp.verbose_intern_pool,
......@@ -6234,7 +6240,6 @@ fn buildOutputFromZig(
62346240 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
62356241 .clang_passthrough_mode = comp.clang_passthrough_mode,
62366242 .skip_linker_dependencies = true,
6237 .want_structured_cfg = comp.bin_file.options.want_structured_cfg,
62386243 });
62396244 defer sub_compilation.destroy();
62406245
......@@ -6242,8 +6247,8 @@ fn buildOutputFromZig(
62426247
62436248 assert(out.* == null);
62446249 out.* = Compilation.CRTFile{
6245 .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{
6246 sub_compilation.bin_file.options.emit.?.sub_path,
6250 .full_object_path = try sub_compilation.bin_file.?.emit.directory.join(comp.gpa, &[_][]const u8{
6251 sub_compilation.bin_file.?.emit.sub_path,
62476252 }),
62486253 .lock = sub_compilation.bin_file.toOwnedLock(),
62496254 };
......@@ -6260,51 +6265,70 @@ pub fn build_crt_file(
62606265 const tracy_trace = trace(@src());
62616266 defer tracy_trace.end();
62626267
6263 const target = comp.getTarget();
6264 const basename = try std.zig.binNameAlloc(comp.gpa, .{
6268 const gpa = comp.gpa;
6269 const basename = try std.zig.binNameAlloc(gpa, .{
62656270 .root_name = root_name,
6266 .target = target,
6271 .target = comp.root_mod.resolved_target.result,
62676272 .output_mode = output_mode,
62686273 });
6269 errdefer comp.gpa.free(basename);
6274 errdefer gpa.free(basename);
62706275
6271 const sub_compilation = try Compilation.create(comp.gpa, .{
6276 const config = try Config.resolve(.{
6277 .output_mode = output_mode,
6278 .resolved_target = comp.root_mod.resolved_target,
6279 .is_test = false,
6280 .have_zcu = false,
6281 .emit_bin = true,
6282 .root_optimize_mode = comp.compilerRtOptMode(),
6283 .link_libc = false,
6284 .lto = switch (output_mode) {
6285 .Lib => comp.config.lto,
6286 .Obj, .Exe => false,
6287 },
6288 });
6289 const root_mod = Package.Module.create(.{
6290 .paths = .{
6291 .root = .{ .root_dir = comp.zig_lib_directory },
6292 .root_src_path = "",
6293 },
6294 .fully_qualified_name = "root",
6295 .inherited = .{
6296 .strip = comp.compilerRtStrip(),
6297 .stack_check = false,
6298 .stack_protector = 0,
6299 .sanitize_c = false,
6300 .sanitize_thread = false,
6301 .red_zone = comp.root_mod.red_zone,
6302 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
6303 .valgrind = false,
6304 .unwind_tables = false,
6305 .pic = comp.root_mod.pic,
6306 .optimize_mode = comp.compilerRtOptMode(),
6307 .structured_cfg = comp.root_mod.structured_cfg,
6308 },
6309 .global = config,
6310 .cc_argv = &.{},
6311 });
6312
6313 const sub_compilation = try Compilation.create(gpa, .{
62726314 .local_cache_directory = comp.global_cache_directory,
62736315 .global_cache_directory = comp.global_cache_directory,
62746316 .zig_lib_directory = comp.zig_lib_directory,
6317 .self_exe_path = comp.self_exe_path,
62756318 .cache_mode = .whole,
6276 .target = target,
6319 .resolved = config,
6320 .root_mod = root_mod,
62776321 .root_name = root_name,
6278 .main_mod = null,
6279 .output_mode = output_mode,
62806322 .thread_pool = comp.thread_pool,
6281 .libc_installation = comp.bin_file.options.libc_installation,
6323 .libc_installation = comp.libc_installation,
62826324 .emit_bin = .{
62836325 .directory = null, // Put it in the cache directory.
62846326 .basename = basename,
62856327 },
6286 .optimize_mode = comp.compilerRtOptMode(),
6287 .want_sanitize_c = false,
6288 .want_stack_check = false,
6289 .want_stack_protector = 0,
6290 .want_red_zone = comp.bin_file.options.red_zone,
6291 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
6292 .want_valgrind = false,
6293 .want_tsan = false,
6294 .want_pic = comp.bin_file.options.pic,
6295 .want_pie = null,
6296 .want_lto = switch (output_mode) {
6297 .Lib => comp.bin_file.options.lto,
6298 .Obj, .Exe => false,
6299 },
63006328 .emit_h = null,
6301 .strip = comp.compilerRtStrip(),
6302 .is_native_os = comp.bin_file.options.is_native_os,
6303 .is_native_abi = comp.bin_file.options.is_native_abi,
6304 .self_exe_path = comp.self_exe_path,
63056329 .c_source_files = c_source_files,
63066330 .verbose_cc = comp.verbose_cc,
6307 .verbose_link = comp.bin_file.options.verbose_link,
6331 .verbose_link = comp.verbose_link,
63086332 .verbose_air = comp.verbose_air,
63096333 .verbose_intern_pool = comp.verbose_intern_pool,
63106334 .verbose_generic_instances = comp.verbose_generic_instances,
......@@ -6314,17 +6338,16 @@ pub fn build_crt_file(
63146338 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
63156339 .clang_passthrough_mode = comp.clang_passthrough_mode,
63166340 .skip_linker_dependencies = true,
6317 .want_structured_cfg = comp.bin_file.options.want_structured_cfg,
63186341 });
63196342 defer sub_compilation.destroy();
63206343
63216344 try comp.updateSubCompilation(sub_compilation, misc_task_tag, prog_node);
63226345
6323 try comp.crt_files.ensureUnusedCapacity(comp.gpa, 1);
6346 try comp.crt_files.ensureUnusedCapacity(gpa, 1);
63246347
63256348 comp.crt_files.putAssumeCapacityNoClobber(basename, .{
6326 .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{
6327 sub_compilation.bin_file.options.emit.?.sub_path,
6349 .full_object_path = try sub_compilation.bin_file.?.emit.directory.join(gpa, &[_][]const u8{
6350 sub_compilation.bin_file.?.emit.sub_path,
63286351 }),
63296352 .lock = sub_compilation.bin_file.toOwnedLock(),
63306353 });
......@@ -6357,10 +6380,11 @@ pub fn addLinkLib(comp: *Compilation, lib_name: []const u8) !void {
63576380/// compiler-rt, libcxx, libc, libunwind, etc.
63586381pub fn compilerRtOptMode(comp: Compilation) std.builtin.OptimizeMode {
63596382 if (comp.debug_compiler_runtime_libs) {
6360 return comp.bin_file.options.optimize_mode;
6383 return comp.root_mod.optimize_mode;
63616384 }
6362 switch (comp.bin_file.options.optimize_mode) {
6363 .Debug, .ReleaseSafe => return target_util.defaultCompilerRtOptimizeMode(comp.getTarget()),
6385 const target = comp.root_mod.resolved_target.result;
6386 switch (comp.root_mod.optimize_mode) {
6387 .Debug, .ReleaseSafe => return target_util.defaultCompilerRtOptimizeMode(target),
63646388 .ReleaseFast => return .ReleaseFast,
63656389 .ReleaseSmall => return .ReleaseSmall,
63666390 }
......@@ -6369,5 +6393,5 @@ pub fn compilerRtOptMode(comp: Compilation) std.builtin.OptimizeMode {
63696393/// This decides whether to strip debug info for all zig-provided libraries, including
63706394/// compiler-rt, libcxx, libc, libunwind, etc.
63716395pub fn compilerRtStrip(comp: Compilation) bool {
6372 return comp.bin_file.options.strip;
6396 return comp.root_mod.strip;
63736397}
src/Package/Module.zig+17
......@@ -30,6 +30,8 @@ sanitize_c: bool,
3030sanitize_thread: bool,
3131unwind_tables: bool,
3232cc_argv: []const []const u8,
33/// (SPIR-V) whether to generate a structured control flow graph or not
34structured_cfg: bool,
3335
3436/// The contents of `@import("builtin")` for this module.
3537generated_builtin_source: []const u8,
......@@ -82,6 +84,7 @@ pub const CreateOptions = struct {
8284 unwind_tables: ?bool = null,
8385 sanitize_c: ?bool = null,
8486 sanitize_thread: ?bool = null,
87 structured_cfg: ?bool = null,
8588 };
8689};
8790
......@@ -268,6 +271,17 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
268271 break :sp target_util.default_stack_protector_buffer_size;
269272 };
270273
274 const structured_cfg = b: {
275 if (options.inherited.structured_cfg) |x| break :b x;
276 if (options.parent) |p| break :b p.structured_cfg;
277 // We always want a structured control flow in shaders. This option is
278 // only relevant for OpenCL kernels.
279 break :b switch (target.os.tag) {
280 .opencl => false,
281 else => true,
282 };
283 };
284
271285 const llvm_cpu_features: ?[*:0]const u8 = b: {
272286 if (resolved_target.llvm_cpu_features) |x| break :b x;
273287 if (!options.global.use_llvm) break :b null;
......@@ -350,6 +364,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
350364 .sanitize_thread = sanitize_thread,
351365 .unwind_tables = unwind_tables,
352366 .cc_argv = &.{},
367 .structured_cfg = structured_cfg,
353368 };
354369 break :b new;
355370 };
......@@ -381,6 +396,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
381396 .sanitize_thread = sanitize_thread,
382397 .unwind_tables = unwind_tables,
383398 .cc_argv = options.cc_argv,
399 .structured_cfg = structured_cfg,
384400 };
385401
386402 try mod.deps.ensureUnusedCapacity(arena, 1);
......@@ -422,6 +438,7 @@ pub fn createLimited(gpa: Allocator, options: LimitedOptions) Allocator.Error!*P
422438 .unwind_tables = undefined,
423439 .cc_argv = undefined,
424440 .generated_builtin_source = undefined,
441 .structured_cfg = undefined,
425442 };
426443 return mod;
427444}
src/codegen/llvm.zig+1-1
......@@ -3084,7 +3084,7 @@ pub const Object = struct {
30843084 if (comp.unwind_tables) {
30853085 try attributes.addFnAttr(.{ .uwtable = Builder.Attribute.UwTable.default }, &o.builder);
30863086 }
3087 if (comp.skip_linker_dependencies or comp.bin_file.options.no_builtin) {
3087 if (comp.skip_linker_dependencies or comp.no_builtin) {
30883088 // The intent here is for compiler-rt and libc functions to not generate
30893089 // infinite recursion. For example, if we are compiling the memcpy function,
30903090 // and llvm detects that the body is equivalent to memcpy, it may replace the
src/codegen/spirv.zig+4-8
......@@ -191,13 +191,9 @@ pub const Object = struct {
191191 air: Air,
192192 liveness: Liveness,
193193 ) !void {
194 const target = mod.getTarget();
195 // We always want a structured control flow in shaders. This option is only relevant
196 // for OpenCL kernels.
197 const want_structured_cfg = switch (target.os.tag) {
198 .opencl => mod.comp.bin_file.options.want_structured_cfg orelse false,
199 else => true,
200 };
194 const decl = mod.declPtr(decl_index);
195 const namespace = mod.namespacePtr(decl.src_namespace);
196 const structured_cfg = namespace.file_scope.mod.structured_cfg;
201197
202198 var decl_gen = DeclGen{
203199 .gpa = self.gpa,
......@@ -208,7 +204,7 @@ pub const Object = struct {
208204 .air = air,
209205 .liveness = liveness,
210206 .type_map = &self.type_map,
211 .control_flow = switch (want_structured_cfg) {
207 .control_flow = switch (structured_cfg) {
212208 true => .{ .structured = .{} },
213209 false => .{ .unstructured = .{} },
214210 },
src/link.zig+4-15
......@@ -32,8 +32,6 @@ pub const SystemLib = struct {
3232 path: ?[]const u8,
3333};
3434
35pub const SortSection = enum { name, alignment };
36
3735pub const CacheMode = enum { incremental, whole };
3836
3937pub fn hashAddSystemLibs(
......@@ -51,10 +49,6 @@ pub fn hashAddSystemLibs(
5149
5250pub const producer_string = if (builtin.is_test) "zig test" else "zig " ++ build_options.version;
5351
54pub const HashStyle = enum { sysv, gnu, both };
55
56pub const CompressDebugSections = enum { none, zlib, zstd };
57
5852pub const File = struct {
5953 tag: Tag,
6054
......@@ -105,12 +99,9 @@ pub const File = struct {
10599 image_base: ?u64,
106100 function_sections: bool,
107101 data_sections: bool,
108 no_builtin: bool,
109102 eh_frame_hdr: bool,
110103 emit_relocs: bool,
111104 rdynamic: bool,
112 optimization: u8,
113 linker_script: ?[]const u8,
114105 z_nodelete: bool,
115106 z_notext: bool,
116107 z_defs: bool,
......@@ -123,7 +114,7 @@ pub const File = struct {
123114 tsaware: bool,
124115 nxcompat: bool,
125116 dynamicbase: bool,
126 compress_debug_sections: CompressDebugSections,
117 compress_debug_sections: Elf.CompressDebugSections,
127118 bind_global_refs_locally: bool,
128119 import_symbols: bool,
129120 import_table: bool,
......@@ -136,13 +127,14 @@ pub const File = struct {
136127 each_lib_rpath: bool,
137128 build_id: std.zig.BuildId,
138129 disable_lld_caching: bool,
139 hash_style: HashStyle,
140 sort_section: ?SortSection,
130 hash_style: Elf.HashStyle,
131 sort_section: ?Elf.SortSection,
141132 major_subsystem_version: ?u32,
142133 minor_subsystem_version: ?u32,
143134 gc_sections: ?bool,
144135 allow_shlib_undefined: ?bool,
145136 subsystem: ?std.Target.SubSystem,
137 linker_script: ?[]const u8,
146138 version_script: ?[]const u8,
147139 soname: ?[]const u8,
148140 print_gc_sections: bool,
......@@ -192,9 +184,6 @@ pub const File = struct {
192184 /// (Windows) .def file to specify when linking
193185 module_definition_file: ?[]const u8,
194186
195 /// (SPIR-V) whether to generate a structured control flow graph or not
196 want_structured_cfg: ?bool,
197
198187 wasi_emulated_libs: []const wasi_libc.CRTFile,
199188 };
200189
src/link/Coff.zig+20-4
......@@ -10,6 +10,15 @@ base: link.File,
1010image_base: u64,
1111error_flags: link.File.ErrorFlags = .{},
1212dll_export_fns: bool,
13subsystem: ?std.Target.SubSystem,
14tsaware: bool,
15nxcompat: bool,
16dynamicbase: bool,
17/// TODO this and minor_subsystem_version should be combined into one property and left as
18/// default or populated together. They should not be separate fields.
19major_subsystem_version: u32,
20minor_subsystem_version: u32,
21lib_dirs: []const []const u8,
1322
1423ptr_width: PtrWidth,
1524page_size: u32,
......@@ -403,6 +412,13 @@ pub fn createEmpty(arena: Allocator, options: link.File.OpenOptions) !*Coff {
403412 },
404413
405414 .dll_export_fns = options.dll_export_fns,
415 .subsystem = options.subsystem,
416 .tsaware = options.tsaware,
417 .nxcompat = options.nxcompat,
418 .dynamicbase = options.dynamicbase,
419 .major_subsystem_version = options.major_subsystem_version orelse 6,
420 .minor_subsystem_version = options.minor_subsystem_version orelse 0,
421 .lib_dirs = options.lib_dirs,
406422 };
407423
408424 const use_llvm = comp.config.use_llvm;
......@@ -2305,8 +2321,8 @@ fn writeHeader(self: *Coff) !void {
23052321 .minor_operating_system_version = 0,
23062322 .major_image_version = 0,
23072323 .minor_image_version = 0,
2308 .major_subsystem_version = 6,
2309 .minor_subsystem_version = 0,
2324 .major_subsystem_version = self.major_subsystem_version,
2325 .minor_subsystem_version = self.minor_subsystem_version,
23102326 .win32_version_value = 0,
23112327 .size_of_image = size_of_image,
23122328 .size_of_headers = size_of_headers,
......@@ -2339,8 +2355,8 @@ fn writeHeader(self: *Coff) !void {
23392355 .minor_operating_system_version = 0,
23402356 .major_image_version = 0,
23412357 .minor_image_version = 0,
2342 .major_subsystem_version = 6,
2343 .minor_subsystem_version = 0,
2358 .major_subsystem_version = self.major_subsystem_version,
2359 .minor_subsystem_version = self.minor_subsystem_version,
23442360 .win32_version_value = 0,
23452361 .size_of_image = size_of_image,
23462362 .size_of_headers = size_of_headers,
src/link/Elf.zig+54-6
......@@ -1,6 +1,29 @@
11base: link.File,
22image_base: u64,
33rdynamic: bool,
4eh_frame_hdr: bool,
5emit_relocs: bool,
6z_nodelete: bool,
7z_notext: bool,
8z_defs: bool,
9z_origin: bool,
10z_nocopyreloc: bool,
11z_now: bool,
12z_relro: bool,
13/// TODO make this non optional and resolve the default in open()
14z_common_page_size: ?u64,
15/// TODO make this non optional and resolve the default in open()
16z_max_page_size: ?u64,
17lib_dirs: []const []const u8,
18hash_style: HashStyle,
19compress_debug_sections: CompressDebugSections,
20symbol_wrap_set: std.StringArrayHashMapUnmanaged(void),
21each_lib_rpath: bool,
22sort_section: ?SortSection,
23soname: ?[]const u8,
24bind_global_refs_locally: bool,
25linker_script: ?[]const u8,
26version_script: ?[]const u8,
427
528ptr_width: PtrWidth,
629
......@@ -201,6 +224,9 @@ const minimum_atom_size = 64;
201224pub const min_text_capacity = padToIdeal(minimum_atom_size);
202225
203226pub const PtrWidth = enum { p32, p64 };
227pub const HashStyle = enum { sysv, gnu, both };
228pub const CompressDebugSections = enum { none, zlib, zstd };
229pub const SortSection = enum { name, alignment };
204230
205231pub fn open(arena: Allocator, options: link.File.OpenOptions) !*Elf {
206232 if (build_options.only_c) unreachable;
......@@ -374,6 +400,27 @@ pub fn createEmpty(arena: Allocator, options: link.File.OpenOptions) !*Elf {
374400 },
375401
376402 .rdynamic = options.rdynamic,
403 .eh_frame_hdr = options.eh_frame_hdr,
404 .emit_relocs = options.emit_relocs,
405 .z_nodelete = options.z_nodelete,
406 .z_notext = options.z_notext,
407 .z_defs = options.z_defs,
408 .z_origin = options.z_origin,
409 .z_nocopyreloc = options.z_nocopyreloc,
410 .z_now = options.z_now,
411 .z_relro = options.z_relro,
412 .z_common_page_size = options.z_common_page_size,
413 .z_max_page_size = options.z_max_page_size,
414 .lib_dirs = options.lib_dirs,
415 .hash_style = options.hash_style,
416 .compress_debug_sections = options.compress_debug_sections,
417 .symbol_wrap_set = options.symbol_wrap_set,
418 .each_lib_rpath = options.each_lib_rpath,
419 .sort_section = options.sort_section,
420 .soname = options.soname,
421 .bind_global_refs_locally = options.bind_global_refs_locally,
422 .linker_script = options.linker_script,
423 .version_script = options.version_script,
377424 };
378425 if (use_llvm and comp.config.have_zcu) {
379426 self.llvm_object = try LlvmObject.create(arena, options);
......@@ -2424,8 +2471,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
24242471 man.hash.add(self.bind_global_refs_locally);
24252472 man.hash.add(self.compress_debug_sections);
24262473 man.hash.add(comp.config.any_sanitize_thread);
2427 man.hash.addOptionalBytes(self.sysroot);
2428 man.hash.add(self.optimization);
2474 man.hash.addOptionalBytes(comp.sysroot);
24292475
24302476 // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock.
24312477 _ = try man.hit();
......@@ -2500,7 +2546,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
25002546
25012547 try argv.append("--error-limit=0");
25022548
2503 if (self.sysroot) |sysroot| {
2549 if (comp.sysroot) |sysroot| {
25042550 try argv.append(try std.fmt.allocPrint(arena, "--sysroot={s}", .{sysroot}));
25052551 }
25062552
......@@ -2511,9 +2557,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
25112557 .ReleaseFast, .ReleaseSafe => try argv.append("--lto-O3"),
25122558 }
25132559 }
2514 try argv.append(try std.fmt.allocPrint(arena, "-O{d}", .{
2515 self.optimization,
2516 }));
2560 switch (comp.root_mod.optimize_mode) {
2561 .Debug => {},
2562 .ReleaseSmall => try argv.append("-O2"),
2563 .ReleaseFast, .ReleaseSafe => try argv.append("-O3"),
2564 }
25172565
25182566 if (comp.config.entry) |entry| {
25192567 try argv.append("--entry");
src/link/MachO/load_commands.zig+1-1
......@@ -476,7 +476,7 @@ pub fn inferSdkVersion(gpa: Allocator, comp: *const Compilation) ?std.SemanticVe
476476
477477 const sdk_layout = macho_file.sdk_layout orelse return null;
478478 const sdk_dir = switch (sdk_layout) {
479 .sdk => macho_file.sysroot.?,
479 .sdk => comp.sysroot.?,
480480 .vendored => std.fs.path.join(arena, &.{ comp.zig_lib_directory.path.?, "libc", "darwin" }) catch return null,
481481 };
482482 if (readSdkVersionFromSettings(arena, sdk_dir)) |ver| {
src/link/MachO/zld.zig+1-1
......@@ -249,7 +249,7 @@ pub fn linkWithZld(
249249 }
250250 }
251251
252 if (macho_file.sysroot) |syslibroot| {
252 if (comp.sysroot) |syslibroot| {
253253 try argv.append("-syslibroot");
254254 try argv.append(syslibroot);
255255 }
src/link/SpirV.zig+2
......@@ -47,6 +47,8 @@ base: link.File,
4747
4848object: codegen.Object,
4949
50pub const base_tag: link.File.Tag = .spirv;
51
5052pub fn createEmpty(arena: Allocator, options: link.File.OpenOptions) !*SpirV {
5153 const gpa = options.comp.gpa;
5254 const target = options.comp.root_mod.resolved_target.result;
src/main.zig+18-22
......@@ -828,9 +828,9 @@ fn buildOutputType(
828828 var linker_script: ?[]const u8 = null;
829829 var version_script: ?[]const u8 = null;
830830 var disable_c_depfile = false;
831 var linker_sort_section: ?link.SortSection = null;
831 var linker_sort_section: ?link.File.Elf.SortSection = null;
832832 var linker_gc_sections: ?bool = null;
833 var linker_compress_debug_sections: ?link.CompressDebugSections = null;
833 var linker_compress_debug_sections: ?link.File.Elf.CompressDebugSections = null;
834834 var linker_allow_shlib_undefined: ?bool = null;
835835 var linker_bind_global_refs_locally: ?bool = null;
836836 var linker_import_symbols: bool = false;
......@@ -855,7 +855,7 @@ fn buildOutputType(
855855 var linker_tsaware = false;
856856 var linker_nxcompat = false;
857857 var linker_dynamicbase = true;
858 var linker_optimization: ?u8 = null;
858 var linker_optimization: ?[]const u8 = null;
859859 var linker_module_definition_file: ?[]const u8 = null;
860860 var test_no_exec = false;
861861 var force_undefined_symbols: std.StringArrayHashMapUnmanaged(void) = .{};
......@@ -881,7 +881,7 @@ fn buildOutputType(
881881 var enable_link_snapshots: bool = false;
882882 var debug_incremental: bool = false;
883883 var install_name: ?[]const u8 = null;
884 var hash_style: link.HashStyle = .both;
884 var hash_style: link.File.Elf.HashStyle = .both;
885885 var entitlements: ?[]const u8 = null;
886886 var pagezero_size: ?u64 = null;
887887 var lib_search_strategy: SystemLib.SearchStrategy = .paths_first;
......@@ -894,7 +894,6 @@ fn buildOutputType(
894894 var pdb_out_path: ?[]const u8 = null;
895895 var debug_format: ?link.File.DebugFormat = null;
896896 var error_limit: ?Module.ErrorInt = null;
897 var want_structured_cfg: ?bool = null;
898897 // These are before resolving sysroot.
899898 var lib_dir_args: std.ArrayListUnmanaged([]const u8) = .{};
900899 var extra_cflags: std.ArrayListUnmanaged([]const u8) = .{};
......@@ -1110,9 +1109,9 @@ fn buildOutputType(
11101109 try extra_rcflags.append(arena, next_arg);
11111110 }
11121111 } else if (mem.startsWith(u8, arg, "-fstructured-cfg")) {
1113 want_structured_cfg = true;
1112 mod_opts.structured_cfg = true;
11141113 } else if (mem.startsWith(u8, arg, "-fno-structured-cfg")) {
1115 want_structured_cfg = false;
1114 mod_opts.structured_cfg = false;
11161115 } else if (mem.eql(u8, arg, "--color")) {
11171116 const next_arg = args_iter.next() orelse {
11181117 fatal("expected [auto|on|off] after --color", .{});
......@@ -1152,11 +1151,11 @@ fn buildOutputType(
11521151 install_name = args_iter.nextOrFatal();
11531152 } else if (mem.startsWith(u8, arg, "--compress-debug-sections=")) {
11541153 const param = arg["--compress-debug-sections=".len..];
1155 linker_compress_debug_sections = std.meta.stringToEnum(link.CompressDebugSections, param) orelse {
1154 linker_compress_debug_sections = std.meta.stringToEnum(link.File.Elf.CompressDebugSections, param) orelse {
11561155 fatal("expected --compress-debug-sections=[none|zlib|zstd], found '{s}'", .{param});
11571156 };
11581157 } else if (mem.eql(u8, arg, "--compress-debug-sections")) {
1159 linker_compress_debug_sections = link.CompressDebugSections.zlib;
1158 linker_compress_debug_sections = link.File.Elf.CompressDebugSections.zlib;
11601159 } else if (mem.eql(u8, arg, "-pagezero_size")) {
11611160 const next_arg = args_iter.nextOrFatal();
11621161 pagezero_size = std.fmt.parseUnsigned(u64, eatIntPrefix(next_arg, 16), 16) catch |err| {
......@@ -2067,7 +2066,7 @@ fn buildOutputType(
20672066 if (it.only_arg.len == 0) {
20682067 linker_compress_debug_sections = .zlib;
20692068 } else {
2070 linker_compress_debug_sections = std.meta.stringToEnum(link.CompressDebugSections, it.only_arg) orelse {
2069 linker_compress_debug_sections = std.meta.stringToEnum(link.File.Elf.CompressDebugSections, it.only_arg) orelse {
20712070 fatal("expected [none|zlib|zstd] after --compress-debug-sections, found '{s}'", .{it.only_arg});
20722071 };
20732072 }
......@@ -2138,14 +2137,9 @@ fn buildOutputType(
21382137 } else if (mem.eql(u8, arg, "-version-script") or mem.eql(u8, arg, "--version-script")) {
21392138 version_script = linker_args_it.nextOrFatal();
21402139 } else if (mem.eql(u8, arg, "-O")) {
2141 const opt = linker_args_it.nextOrFatal();
2142 linker_optimization = std.fmt.parseUnsigned(u8, opt, 10) catch |err| {
2143 fatal("unable to parse optimization level '{s}': {s}", .{ opt, @errorName(err) });
2144 };
2140 linker_optimization = linker_args_it.nextOrFatal();
21452141 } else if (mem.startsWith(u8, arg, "-O")) {
2146 linker_optimization = std.fmt.parseUnsigned(u8, arg["-O".len..], 10) catch |err| {
2147 fatal("unable to parse optimization level '{s}': {s}", .{ arg, @errorName(err) });
2148 };
2142 linker_optimization = arg["-O".len..];
21492143 } else if (mem.eql(u8, arg, "-pagezero_size")) {
21502144 const next_arg = linker_args_it.nextOrFatal();
21512145 pagezero_size = std.fmt.parseUnsigned(u64, eatIntPrefix(next_arg, 16), 16) catch |err| {
......@@ -2176,7 +2170,7 @@ fn buildOutputType(
21762170 linker_print_map = true;
21772171 } else if (mem.eql(u8, arg, "--sort-section")) {
21782172 const arg1 = linker_args_it.nextOrFatal();
2179 linker_sort_section = std.meta.stringToEnum(link.SortSection, arg1) orelse {
2173 linker_sort_section = std.meta.stringToEnum(link.File.Elf.SortSection, arg1) orelse {
21802174 fatal("expected [name|alignment] after --sort-section, found '{s}'", .{arg1});
21812175 };
21822176 } else if (mem.eql(u8, arg, "--allow-shlib-undefined") or
......@@ -2222,7 +2216,7 @@ fn buildOutputType(
22222216 try linker_export_symbol_names.append(arena, linker_args_it.nextOrFatal());
22232217 } else if (mem.eql(u8, arg, "--compress-debug-sections")) {
22242218 const arg1 = linker_args_it.nextOrFatal();
2225 linker_compress_debug_sections = std.meta.stringToEnum(link.CompressDebugSections, arg1) orelse {
2219 linker_compress_debug_sections = std.meta.stringToEnum(link.File.Elf.CompressDebugSections, arg1) orelse {
22262220 fatal("expected [none|zlib|zstd] after --compress-debug-sections, found '{s}'", .{arg1});
22272221 };
22282222 } else if (mem.startsWith(u8, arg, "-z")) {
......@@ -2405,7 +2399,7 @@ fn buildOutputType(
24052399 mem.eql(u8, arg, "--hash-style"))
24062400 {
24072401 const next_arg = linker_args_it.nextOrFatal();
2408 hash_style = std.meta.stringToEnum(link.HashStyle, next_arg) orelse {
2402 hash_style = std.meta.stringToEnum(link.File.Elf.HashStyle, next_arg) orelse {
24092403 fatal("expected [sysv|gnu|both] after --hash-style, found '{s}'", .{
24102404 next_arg,
24112405 });
......@@ -2625,6 +2619,10 @@ fn buildOutputType(
26252619 };
26262620 defer global_cache_directory.handle.close();
26272621
2622 if (linker_optimization) |o| {
2623 warn("ignoring deprecated linker optimization setting '{s}'", .{o});
2624 }
2625
26282626 create_module.global_cache_directory = global_cache_directory;
26292627 create_module.opts.emit_llvm_ir = emit_llvm_ir != .no;
26302628 create_module.opts.emit_llvm_bc = emit_llvm_bc != .no;
......@@ -3409,7 +3407,6 @@ fn buildOutputType(
34093407 .linker_tsaware = linker_tsaware,
34103408 .linker_nxcompat = linker_nxcompat,
34113409 .linker_dynamicbase = linker_dynamicbase,
3412 .linker_optimization = linker_optimization,
34133410 .linker_compress_debug_sections = linker_compress_debug_sections,
34143411 .linker_module_definition_file = linker_module_definition_file,
34153412 .major_subsystem_version = major_subsystem_version,
......@@ -3458,7 +3455,6 @@ fn buildOutputType(
34583455 .reference_trace = reference_trace,
34593456 .pdb_out_path = pdb_out_path,
34603457 .error_limit = error_limit,
3461 .want_structured_cfg = want_structured_cfg,
34623458 }) catch |err| switch (err) {
34633459 error.LibCUnavailable => {
34643460 const triple_name = try target.zigTriple(arena);