authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-27 10:23:27-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 19:49:07-07:00
log435b74acd6384029588755ae87568d03911da5c2
treec3957c7cd604ee6dd937cedf4c53e7e491f3bfe4
parentc8c32a056989cc669008f1fc295a29d55b753cc0

move force_undefined_symbols into Compilation

This field is needed by Compilation regardless of whether a link file is instantiated. Fixes an invalid check for bin_file=null.

13 files changed, 24 insertions(+), 33 deletions(-)

src/Compilation.zig+14-12
......@@ -86,6 +86,10 @@ no_builtin: bool,
8686function_sections: bool,
8787data_sections: bool,
8888native_system_include_paths: []const []const u8,
89/// List of symbols forced as undefined in the symbol table
90/// thus forcing their resolution by the linker.
91/// Corresponds to `-u <symbol>` for ELF/MachO and `/include:<symbol>` for COFF/PE.
92force_undefined_symbols: std.StringArrayHashMapUnmanaged(void),
8993
9094c_object_table: std.AutoArrayHashMapUnmanaged(*CObject, void) = .{},
9195win32_resource_table: if (build_options.only_core_functionality) void else std.AutoArrayHashMapUnmanaged(*Win32Resource, void) =
......@@ -1504,6 +1508,7 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
15041508 .data_sections = options.data_sections,
15051509 .native_system_include_paths = options.native_system_include_paths,
15061510 .wasi_emulated_libs = options.wasi_emulated_libs,
1511 .force_undefined_symbols = options.force_undefined_symbols,
15071512 };
15081513
15091514 // Prevent some footguns by making the "any" fields of config reflect
......@@ -1569,7 +1574,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
15691574 .headerpad_size = options.headerpad_size,
15701575 .headerpad_max_install_names = options.headerpad_max_install_names,
15711576 .dead_strip_dylibs = options.dead_strip_dylibs,
1572 .force_undefined_symbols = options.force_undefined_symbols,
15731577 .pdb_source_path = options.pdb_source_path,
15741578 .pdb_out_path = options.pdb_out_path,
15751579 .entry_addr = null, // CLI does not expose this option (yet?)
......@@ -1830,18 +1834,16 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
18301834 try comp.work_queue.writeItem(.libtsan);
18311835 }
18321836
1833 if (comp.bin_file) |lf| {
1834 if (target.isMinGW() and comp.config.any_non_single_threaded) {
1835 // LLD might drop some symbols as unused during LTO and GCing, therefore,
1836 // we force mark them for resolution here.
1837 if (target.isMinGW() and comp.config.any_non_single_threaded) {
1838 // LLD might drop some symbols as unused during LTO and GCing, therefore,
1839 // we force mark them for resolution here.
18371840
1838 const tls_index_sym = switch (target.cpu.arch) {
1839 .x86 => "__tls_index",
1840 else => "_tls_index",
1841 };
1841 const tls_index_sym = switch (target.cpu.arch) {
1842 .x86 => "__tls_index",
1843 else => "_tls_index",
1844 };
18421845
1843 try lf.force_undefined_symbols.put(comp.gpa, tls_index_sym, {});
1844 }
1846 try comp.force_undefined_symbols.put(comp.gpa, tls_index_sym, {});
18451847 }
18461848
18471849 if (comp.include_compiler_rt and capable_of_building_compiler_rt) {
......@@ -2447,6 +2449,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24472449 man.hash.addOptionalBytes(comp.sysroot);
24482450 man.hash.addOptional(comp.version);
24492451 man.hash.addListOfBytes(comp.rc_include_dir_list);
2452 man.hash.addListOfBytes(comp.force_undefined_symbols.keys());
24502453
24512454 cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_asm);
24522455 cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_llvm_ir);
......@@ -2482,7 +2485,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24822485 man.hash.add(lf.gc_sections);
24832486 man.hash.addListOfBytes(lf.rpath_list);
24842487 man.hash.add(lf.build_id);
2485 man.hash.addListOfBytes(lf.force_undefined_symbols.keys());
24862488 man.hash.add(lf.allow_shlib_undefined);
24872489
24882490 switch (lf.tag) {
src/link.zig-5
......@@ -64,10 +64,6 @@ pub const File = struct {
6464 print_gc_sections: bool,
6565 build_id: std.zig.BuildId,
6666 rpath_list: []const []const u8,
67 /// List of symbols forced as undefined in the symbol table
68 /// thus forcing their resolution by the linker.
69 /// Corresponds to `-u <symbol>` for ELF/MachO and `/include:<symbol>` for COFF/PE.
70 force_undefined_symbols: std.StringArrayHashMapUnmanaged(void),
7167 allow_shlib_undefined: bool,
7268 stack_size: u64,
7369
......@@ -129,7 +125,6 @@ pub const File = struct {
129125 print_icf_sections: bool,
130126 print_map: bool,
131127
132 force_undefined_symbols: std.StringArrayHashMapUnmanaged(void),
133128 /// Use a wrapper function for symbol. Any undefined reference to symbol
134129 /// will be resolved to __wrap_symbol. Any undefined reference to
135130 /// __real_symbol will be resolved to symbol. This can be used to provide a
src/link/C.zig-1
......@@ -142,7 +142,6 @@ pub fn createEmpty(
142142 .disable_lld_caching = options.disable_lld_caching,
143143 .build_id = options.build_id,
144144 .rpath_list = options.rpath_list,
145 .force_undefined_symbols = options.force_undefined_symbols,
146145 },
147146 };
148147
src/link/Coff.zig-1
......@@ -284,7 +284,6 @@ pub fn createEmpty(
284284 .disable_lld_caching = options.disable_lld_caching,
285285 .build_id = options.build_id,
286286 .rpath_list = options.rpath_list,
287 .force_undefined_symbols = options.force_undefined_symbols,
288287 },
289288 .ptr_width = ptr_width,
290289 .page_size = page_size,
src/link/Coff/lld.zig+2-2
......@@ -103,7 +103,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
103103 }
104104 }
105105 try link.hashAddSystemLibs(&man, comp.system_libs);
106 man.hash.addListOfBytes(self.base.force_undefined_symbols.keys());
106 man.hash.addListOfBytes(comp.force_undefined_symbols.keys());
107107 man.hash.addOptional(self.subsystem);
108108 man.hash.add(comp.config.is_test);
109109 man.hash.add(self.tsaware);
......@@ -217,7 +217,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
217217 }
218218 }
219219
220 for (self.base.force_undefined_symbols.keys()) |symbol| {
220 for (comp.force_undefined_symbols.keys()) |symbol| {
221221 try argv.append(try allocPrint(arena, "-INCLUDE:{s}", .{symbol}));
222222 }
223223
src/link/Elf.zig+2-3
......@@ -285,7 +285,6 @@ pub fn createEmpty(
285285 .disable_lld_caching = options.disable_lld_caching,
286286 .build_id = options.build_id,
287287 .rpath_list = options.rpath_list,
288 .force_undefined_symbols = options.force_undefined_symbols,
289288 },
290289 .ptr_width = ptr_width,
291290 .page_size = page_size,
......@@ -2473,7 +2472,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
24732472 man.hash.addOptionalBytes(self.soname);
24742473 man.hash.addOptional(comp.version);
24752474 try link.hashAddSystemLibs(&man, self.base.comp.system_libs);
2476 man.hash.addListOfBytes(self.base.force_undefined_symbols.keys());
2475 man.hash.addListOfBytes(comp.force_undefined_symbols.keys());
24772476 man.hash.add(self.base.allow_shlib_undefined);
24782477 man.hash.add(self.bind_global_refs_locally);
24792478 man.hash.add(self.compress_debug_sections);
......@@ -2574,7 +2573,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
25742573 try argv.appendSlice(&.{ "--entry", name });
25752574 }
25762575
2577 for (self.base.force_undefined_symbols.keys()) |sym| {
2576 for (comp.force_undefined_symbols.keys()) |sym| {
25782577 try argv.append("-u");
25792578 try argv.append(sym);
25802579 }
src/link/MachO.zig+1-2
......@@ -220,7 +220,6 @@ pub fn createEmpty(
220220 .disable_lld_caching = options.disable_lld_caching,
221221 .build_id = options.build_id,
222222 .rpath_list = options.rpath_list,
223 .force_undefined_symbols = options.force_undefined_symbols,
224223 },
225224 .mode = mode,
226225 .pagezero_vmsize = options.pagezero_size orelse default_pagezero_vmsize,
......@@ -1642,7 +1641,7 @@ pub fn resolveSymbols(self: *MachO) !void {
16421641 }
16431642
16441643 // Force resolution of any symbols requested by the user.
1645 for (self.base.force_undefined_symbols.keys()) |sym_name| {
1644 for (comp.force_undefined_symbols.keys()) |sym_name| {
16461645 _ = try self.addUndefined(sym_name, .{});
16471646 }
16481647
src/link/MachO/dead_strip.zig+4-2
......@@ -34,7 +34,9 @@ fn addRoot(macho_file: *MachO, roots: *AtomTable, file: u32, sym_loc: SymbolWith
3434fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void {
3535 log.debug("collecting roots", .{});
3636
37 switch (macho_file.base.comp.config.output_mode) {
37 const comp = macho_file.base.comp;
38
39 switch (comp.config.output_mode) {
3840 .Exe => {
3941 // Add entrypoint as GC root
4042 if (macho_file.getEntryPoint()) |global| {
......@@ -61,7 +63,7 @@ fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void {
6163 }
6264
6365 // Add all symbols force-defined by the user.
64 for (macho_file.base.force_undefined_symbols.keys()) |sym_name| {
66 for (comp.force_undefined_symbols.keys()) |sym_name| {
6567 const global_index = macho_file.resolver.get(sym_name).?;
6668 const global = macho_file.globals.items[global_index];
6769 const sym = macho_file.getSymbol(global);
src/link/MachO/zld.zig+1-1
......@@ -85,7 +85,7 @@ pub fn linkWithZld(
8585 }
8686 try link.hashAddSystemLibs(&man, comp.system_libs);
8787 man.hash.addOptionalBytes(comp.sysroot);
88 man.hash.addListOfBytes(macho_file.base.force_undefined_symbols.keys());
88 man.hash.addListOfBytes(comp.force_undefined_symbols.keys());
8989 try man.addOptionalFile(macho_file.entitlements);
9090
9191 // We don't actually care whether it's a cache hit or miss; we just
src/link/NvPtx.zig-1
......@@ -60,7 +60,6 @@ pub fn createEmpty(
6060 .disable_lld_caching = options.disable_lld_caching,
6161 .build_id = options.build_id,
6262 .rpath_list = options.rpath_list,
63 .force_undefined_symbols = options.force_undefined_symbols,
6463 },
6564 .llvm_object = llvm_object,
6665 };
src/link/Plan9.zig-1
......@@ -325,7 +325,6 @@ pub fn createEmpty(
325325 .disable_lld_caching = options.disable_lld_caching,
326326 .build_id = options.build_id,
327327 .rpath_list = options.rpath_list,
328 .force_undefined_symbols = options.force_undefined_symbols,
329328 },
330329 .sixtyfour_bit = sixtyfour_bit,
331330 .bases = undefined,
src/link/SpirV.zig-1
......@@ -71,7 +71,6 @@ pub fn createEmpty(
7171 .disable_lld_caching = options.disable_lld_caching,
7272 .build_id = options.build_id,
7373 .rpath_list = options.rpath_list,
74 .force_undefined_symbols = options.force_undefined_symbols,
7574 },
7675 .object = codegen.Object.init(gpa),
7776 };
src/link/Wasm.zig-1
......@@ -424,7 +424,6 @@ pub fn createEmpty(
424424 .disable_lld_caching = options.disable_lld_caching,
425425 .build_id = options.build_id,
426426 .rpath_list = options.rpath_list,
427 .force_undefined_symbols = options.force_undefined_symbols,
428427 },
429428 .name = undefined,
430429 .import_table = options.import_table,