authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-07-26 21:05:40+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-28 14:37:03+02:00
log642cd730c8b588778d8c1d6953134408b30dfab3
tree430fe8e39eb8ea0b7d3f3c07cde56c523f02f79b
parent91c17979f10db4fdc6639b176c0563718f060f47

link: Accept `-Brepro` linker option and pass it to LLD.

Enable it by default when building Zig code in release modes. Contributes to #9432.

7 files changed, 20 insertions(+), 5 deletions(-)

src/Compilation.zig+5-2
......@@ -1122,6 +1122,7 @@ pub const CreateOptions = struct {
11221122 linker_enable_new_dtags: ?bool = null,
11231123 soname: ?[]const u8 = null,
11241124 linker_gc_sections: ?bool = null,
1125 linker_repro: ?bool = null,
11251126 linker_allow_shlib_undefined: ?bool = null,
11261127 linker_bind_global_refs_locally: ?bool = null,
11271128 linker_import_symbols: bool = false,
......@@ -1602,6 +1603,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
16021603 .framework_dirs = options.framework_dirs,
16031604 .rpath_list = options.rpath_list,
16041605 .symbol_wrap_set = options.symbol_wrap_set,
1606 .repro = options.linker_repro orelse (options.root_mod.optimize_mode != .Debug),
16051607 .allow_shlib_undefined = options.linker_allow_shlib_undefined,
16061608 .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
16071609 .compress_debug_sections = options.linker_compress_debug_sections orelse .none,
......@@ -2560,7 +2562,7 @@ fn prepareWholeEmitSubPath(arena: Allocator, opt_emit: ?EmitLoc) error{OutOfMemo
25602562/// to remind the programmer to update multiple related pieces of code that
25612563/// are in different locations. Bump this number when adding or deleting
25622564/// anything from the link cache manifest.
2563pub const link_hash_implementation_version = 13;
2565pub const link_hash_implementation_version = 14;
25642566
25652567fn addNonIncrementalStuffToCacheManifest(
25662568 comp: *Compilation,
......@@ -2569,7 +2571,7 @@ fn addNonIncrementalStuffToCacheManifest(
25692571) !void {
25702572 const gpa = comp.gpa;
25712573
2572 comptime assert(link_hash_implementation_version == 13);
2574 comptime assert(link_hash_implementation_version == 14);
25732575
25742576 if (comp.module) |mod| {
25752577 try addModuleTableToCacheHash(gpa, arena, &man.hash, mod.root_mod, mod.main_mod, .{ .files = man });
......@@ -2660,6 +2662,7 @@ fn addNonIncrementalStuffToCacheManifest(
26602662 }
26612663 man.hash.addOptionalBytes(target.dynamic_linker.get());
26622664 }
2665 man.hash.add(opts.repro);
26632666 man.hash.addOptional(opts.allow_shlib_undefined);
26642667 man.hash.add(opts.bind_global_refs_locally);
26652668
src/link.zig+1
......@@ -114,6 +114,7 @@ pub const File = struct {
114114 major_subsystem_version: ?u16,
115115 minor_subsystem_version: ?u16,
116116 gc_sections: ?bool,
117 repro: bool,
117118 allow_shlib_undefined: ?bool,
118119 allow_undefined_version: bool,
119120 enable_new_dtags: ?bool,
src/link/Coff.zig+2
......@@ -21,6 +21,7 @@ entry: link.File.OpenOptions.Entry,
2121entry_addr: ?u32,
2222module_definition_file: ?[]const u8,
2323pdb_out_path: ?[]const u8,
24repro: bool,
2425
2526ptr_width: PtrWidth,
2627page_size: u32,
......@@ -319,6 +320,7 @@ pub fn createEmpty(
319320 return error.EntryAddressTooBig,
320321 .module_definition_file = options.module_definition_file,
321322 .pdb_out_path = options.pdb_out_path,
323 .repro = options.repro,
322324 };
323325 if (use_llvm and comp.config.have_zcu) {
324326 self.llvm_object = try LlvmObject.create(arena, comp);
src/link/Coff/lld.zig+6-1
......@@ -71,7 +71,7 @@ pub fn linkWithLLD(self: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_no
7171 man = comp.cache_parent.obtain();
7272 self.base.releaseLock();
7373
74 comptime assert(Compilation.link_hash_implementation_version == 13);
74 comptime assert(Compilation.link_hash_implementation_version == 14);
7575
7676 for (comp.objects) |obj| {
7777 _ = try man.addFile(obj.path, null);
......@@ -110,6 +110,7 @@ pub fn linkWithLLD(self: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_no
110110 // strip does not need to go into the linker hash because it is part of the hash namespace
111111 man.hash.add(self.major_subsystem_version);
112112 man.hash.add(self.minor_subsystem_version);
113 man.hash.add(self.repro);
113114 man.hash.addOptional(comp.version);
114115 try man.addOptionalFile(self.module_definition_file);
115116
......@@ -227,6 +228,10 @@ pub fn linkWithLLD(self: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_no
227228 try argv.append(try allocPrint(arena, "-ENTRY:{s}", .{name}));
228229 }
229230
231 if (self.repro) {
232 try argv.append("-BREPRO");
233 }
234
230235 if (self.tsaware) {
231236 try argv.append("-tsaware");
232237 }
src/link/Elf.zig+1-1
......@@ -2270,7 +2270,7 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
22702270 // We are about to obtain this lock, so here we give other processes a chance first.
22712271 self.base.releaseLock();
22722272
2273 comptime assert(Compilation.link_hash_implementation_version == 13);
2273 comptime assert(Compilation.link_hash_implementation_version == 14);
22742274
22752275 try man.addOptionalFile(self.linker_script);
22762276 try man.addOptionalFile(self.version_script);
src/link/Wasm.zig+1-1
......@@ -3393,7 +3393,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node:
33933393 // We are about to obtain this lock, so here we give other processes a chance first.
33943394 wasm.base.releaseLock();
33953395
3396 comptime assert(Compilation.link_hash_implementation_version == 13);
3396 comptime assert(Compilation.link_hash_implementation_version == 14);
33973397
33983398 for (comp.objects) |obj| {
33993399 _ = try man.addFile(obj.path, null);
src/main.zig+4
......@@ -860,6 +860,7 @@ fn buildOutputType(
860860 var want_compiler_rt: ?bool = null;
861861 var linker_script: ?[]const u8 = null;
862862 var version_script: ?[]const u8 = null;
863 var linker_repro: ?bool = null;
863864 var linker_allow_undefined_version: bool = false;
864865 var linker_enable_new_dtags: ?bool = null;
865866 var disable_c_depfile = false;
......@@ -2481,6 +2482,8 @@ fn buildOutputType(
24812482 {
24822483 emit_implib = .{ .yes = linker_args_it.nextOrFatal() };
24832484 emit_implib_arg_provided = true;
2485 } else if (mem.eql(u8, arg, "-Brepro") or mem.eql(u8, arg, "/Brepro")) {
2486 linker_repro = true;
24842487 } else if (mem.eql(u8, arg, "-undefined")) {
24852488 const lookup_type = linker_args_it.nextOrFatal();
24862489 if (mem.eql(u8, "dynamic_lookup", lookup_type)) {
......@@ -3315,6 +3318,7 @@ fn buildOutputType(
33153318 .soname = resolved_soname,
33163319 .linker_sort_section = linker_sort_section,
33173320 .linker_gc_sections = linker_gc_sections,
3321 .linker_repro = linker_repro,
33183322 .linker_allow_shlib_undefined = linker_allow_shlib_undefined,
33193323 .linker_bind_global_refs_locally = linker_bind_global_refs_locally,
33203324 .linker_import_symbols = linker_import_symbols,