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,...@@ -86,6 +86,10 @@ no_builtin: bool,
86function_sections: bool,86function_sections: bool,
87data_sections: bool,87data_sections: bool,
88native_system_include_paths: []const []const u8,88native_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
90c_object_table: std.AutoArrayHashMapUnmanaged(*CObject, void) = .{},94c_object_table: std.AutoArrayHashMapUnmanaged(*CObject, void) = .{},
91win32_resource_table: if (build_options.only_core_functionality) void else std.AutoArrayHashMapUnmanaged(*Win32Resource, void) =95win32_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 {...@@ -1504,6 +1508,7 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
1504 .data_sections = options.data_sections,1508 .data_sections = options.data_sections,
1505 .native_system_include_paths = options.native_system_include_paths,1509 .native_system_include_paths = options.native_system_include_paths,
1506 .wasi_emulated_libs = options.wasi_emulated_libs,1510 .wasi_emulated_libs = options.wasi_emulated_libs,
1511 .force_undefined_symbols = options.force_undefined_symbols,
1507 };1512 };
15081513
1509 // Prevent some footguns by making the "any" fields of config reflect1514 // Prevent some footguns by making the "any" fields of config reflect
...@@ -1569,7 +1574,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {...@@ -1569,7 +1574,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
1569 .headerpad_size = options.headerpad_size,1574 .headerpad_size = options.headerpad_size,
1570 .headerpad_max_install_names = options.headerpad_max_install_names,1575 .headerpad_max_install_names = options.headerpad_max_install_names,
1571 .dead_strip_dylibs = options.dead_strip_dylibs,1576 .dead_strip_dylibs = options.dead_strip_dylibs,
1572 .force_undefined_symbols = options.force_undefined_symbols,
1573 .pdb_source_path = options.pdb_source_path,1577 .pdb_source_path = options.pdb_source_path,
1574 .pdb_out_path = options.pdb_out_path,1578 .pdb_out_path = options.pdb_out_path,
1575 .entry_addr = null, // CLI does not expose this option (yet?)1579 .entry_addr = null, // CLI does not expose this option (yet?)
...@@ -1830,18 +1834,16 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {...@@ -1830,18 +1834,16 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation {
1830 try comp.work_queue.writeItem(.libtsan);1834 try comp.work_queue.writeItem(.libtsan);
1831 }1835 }
18321836
1833 if (comp.bin_file) |lf| {1837 if (target.isMinGW() and comp.config.any_non_single_threaded) {
1834 if (target.isMinGW() and comp.config.any_non_single_threaded) {1838 // LLD might drop some symbols as unused during LTO and GCing, therefore,
1835 // LLD might drop some symbols as unused during LTO and GCing, therefore,1839 // we force mark them for resolution here.
1836 // we force mark them for resolution here.
18371840
1838 const tls_index_sym = switch (target.cpu.arch) {1841 const tls_index_sym = switch (target.cpu.arch) {
1839 .x86 => "__tls_index",1842 .x86 => "__tls_index",
1840 else => "_tls_index",1843 else => "_tls_index",
1841 };1844 };
18421845
1843 try lf.force_undefined_symbols.put(comp.gpa, tls_index_sym, {});1846 try comp.force_undefined_symbols.put(comp.gpa, tls_index_sym, {});
1844 }
1845 }1847 }
18461848
1847 if (comp.include_compiler_rt and capable_of_building_compiler_rt) {1849 if (comp.include_compiler_rt and capable_of_building_compiler_rt) {
...@@ -2447,6 +2449,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes...@@ -2447,6 +2449,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
2447 man.hash.addOptionalBytes(comp.sysroot);2449 man.hash.addOptionalBytes(comp.sysroot);
2448 man.hash.addOptional(comp.version);2450 man.hash.addOptional(comp.version);
2449 man.hash.addListOfBytes(comp.rc_include_dir_list);2451 man.hash.addListOfBytes(comp.rc_include_dir_list);
2452 man.hash.addListOfBytes(comp.force_undefined_symbols.keys());
24502453
2451 cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_asm);2454 cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_asm);
2452 cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_llvm_ir);2455 cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_llvm_ir);
...@@ -2482,7 +2485,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes...@@ -2482,7 +2485,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
2482 man.hash.add(lf.gc_sections);2485 man.hash.add(lf.gc_sections);
2483 man.hash.addListOfBytes(lf.rpath_list);2486 man.hash.addListOfBytes(lf.rpath_list);
2484 man.hash.add(lf.build_id);2487 man.hash.add(lf.build_id);
2485 man.hash.addListOfBytes(lf.force_undefined_symbols.keys());
2486 man.hash.add(lf.allow_shlib_undefined);2488 man.hash.add(lf.allow_shlib_undefined);
24872489
2488 switch (lf.tag) {2490 switch (lf.tag) {
src/link.zig-5
...@@ -64,10 +64,6 @@ pub const File = struct {...@@ -64,10 +64,6 @@ pub const File = struct {
64 print_gc_sections: bool,64 print_gc_sections: bool,
65 build_id: std.zig.BuildId,65 build_id: std.zig.BuildId,
66 rpath_list: []const []const u8,66 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),
71 allow_shlib_undefined: bool,67 allow_shlib_undefined: bool,
72 stack_size: u64,68 stack_size: u64,
7369
...@@ -129,7 +125,6 @@ pub const File = struct {...@@ -129,7 +125,6 @@ pub const File = struct {
129 print_icf_sections: bool,125 print_icf_sections: bool,
130 print_map: bool,126 print_map: bool,
131127
132 force_undefined_symbols: std.StringArrayHashMapUnmanaged(void),
133 /// Use a wrapper function for symbol. Any undefined reference to symbol128 /// Use a wrapper function for symbol. Any undefined reference to symbol
134 /// will be resolved to __wrap_symbol. Any undefined reference to129 /// will be resolved to __wrap_symbol. Any undefined reference to
135 /// __real_symbol will be resolved to symbol. This can be used to provide a130 /// __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(...@@ -142,7 +142,6 @@ pub fn createEmpty(
142 .disable_lld_caching = options.disable_lld_caching,142 .disable_lld_caching = options.disable_lld_caching,
143 .build_id = options.build_id,143 .build_id = options.build_id,
144 .rpath_list = options.rpath_list,144 .rpath_list = options.rpath_list,
145 .force_undefined_symbols = options.force_undefined_symbols,
146 },145 },
147 };146 };
148147
src/link/Coff.zig-1
...@@ -284,7 +284,6 @@ pub fn createEmpty(...@@ -284,7 +284,6 @@ pub fn createEmpty(
284 .disable_lld_caching = options.disable_lld_caching,284 .disable_lld_caching = options.disable_lld_caching,
285 .build_id = options.build_id,285 .build_id = options.build_id,
286 .rpath_list = options.rpath_list,286 .rpath_list = options.rpath_list,
287 .force_undefined_symbols = options.force_undefined_symbols,
288 },287 },
289 .ptr_width = ptr_width,288 .ptr_width = ptr_width,
290 .page_size = page_size,289 .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...@@ -103,7 +103,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
103 }103 }
104 }104 }
105 try link.hashAddSystemLibs(&man, comp.system_libs);105 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());
107 man.hash.addOptional(self.subsystem);107 man.hash.addOptional(self.subsystem);
108 man.hash.add(comp.config.is_test);108 man.hash.add(comp.config.is_test);
109 man.hash.add(self.tsaware);109 man.hash.add(self.tsaware);
...@@ -217,7 +217,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod...@@ -217,7 +217,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
217 }217 }
218 }218 }
219219
220 for (self.base.force_undefined_symbols.keys()) |symbol| {220 for (comp.force_undefined_symbols.keys()) |symbol| {
221 try argv.append(try allocPrint(arena, "-INCLUDE:{s}", .{symbol}));221 try argv.append(try allocPrint(arena, "-INCLUDE:{s}", .{symbol}));
222 }222 }
223223
src/link/Elf.zig+2-3
...@@ -285,7 +285,6 @@ pub fn createEmpty(...@@ -285,7 +285,6 @@ pub fn createEmpty(
285 .disable_lld_caching = options.disable_lld_caching,285 .disable_lld_caching = options.disable_lld_caching,
286 .build_id = options.build_id,286 .build_id = options.build_id,
287 .rpath_list = options.rpath_list,287 .rpath_list = options.rpath_list,
288 .force_undefined_symbols = options.force_undefined_symbols,
289 },288 },
290 .ptr_width = ptr_width,289 .ptr_width = ptr_width,
291 .page_size = page_size,290 .page_size = page_size,
...@@ -2473,7 +2472,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v...@@ -2473,7 +2472,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
2473 man.hash.addOptionalBytes(self.soname);2472 man.hash.addOptionalBytes(self.soname);
2474 man.hash.addOptional(comp.version);2473 man.hash.addOptional(comp.version);
2475 try link.hashAddSystemLibs(&man, self.base.comp.system_libs);2474 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());
2477 man.hash.add(self.base.allow_shlib_undefined);2476 man.hash.add(self.base.allow_shlib_undefined);
2478 man.hash.add(self.bind_global_refs_locally);2477 man.hash.add(self.bind_global_refs_locally);
2479 man.hash.add(self.compress_debug_sections);2478 man.hash.add(self.compress_debug_sections);
...@@ -2574,7 +2573,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v...@@ -2574,7 +2573,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
2574 try argv.appendSlice(&.{ "--entry", name });2573 try argv.appendSlice(&.{ "--entry", name });
2575 }2574 }
25762575
2577 for (self.base.force_undefined_symbols.keys()) |sym| {2576 for (comp.force_undefined_symbols.keys()) |sym| {
2578 try argv.append("-u");2577 try argv.append("-u");
2579 try argv.append(sym);2578 try argv.append(sym);
2580 }2579 }
src/link/MachO.zig+1-2
...@@ -220,7 +220,6 @@ pub fn createEmpty(...@@ -220,7 +220,6 @@ pub fn createEmpty(
220 .disable_lld_caching = options.disable_lld_caching,220 .disable_lld_caching = options.disable_lld_caching,
221 .build_id = options.build_id,221 .build_id = options.build_id,
222 .rpath_list = options.rpath_list,222 .rpath_list = options.rpath_list,
223 .force_undefined_symbols = options.force_undefined_symbols,
224 },223 },
225 .mode = mode,224 .mode = mode,
226 .pagezero_vmsize = options.pagezero_size orelse default_pagezero_vmsize,225 .pagezero_vmsize = options.pagezero_size orelse default_pagezero_vmsize,
...@@ -1642,7 +1641,7 @@ pub fn resolveSymbols(self: *MachO) !void {...@@ -1642,7 +1641,7 @@ pub fn resolveSymbols(self: *MachO) !void {
1642 }1641 }
16431642
1644 // Force resolution of any symbols requested by the user.1643 // 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| {
1646 _ = try self.addUndefined(sym_name, .{});1645 _ = try self.addUndefined(sym_name, .{});
1647 }1646 }
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...@@ -34,7 +34,9 @@ fn addRoot(macho_file: *MachO, roots: *AtomTable, file: u32, sym_loc: SymbolWith
34fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void {34fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void {
35 log.debug("collecting roots", .{});35 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) {
38 .Exe => {40 .Exe => {
39 // Add entrypoint as GC root41 // Add entrypoint as GC root
40 if (macho_file.getEntryPoint()) |global| {42 if (macho_file.getEntryPoint()) |global| {
...@@ -61,7 +63,7 @@ fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void {...@@ -61,7 +63,7 @@ fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void {
61 }63 }
6264
63 // Add all symbols force-defined by the user.65 // 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| {
65 const global_index = macho_file.resolver.get(sym_name).?;67 const global_index = macho_file.resolver.get(sym_name).?;
66 const global = macho_file.globals.items[global_index];68 const global = macho_file.globals.items[global_index];
67 const sym = macho_file.getSymbol(global);69 const sym = macho_file.getSymbol(global);
src/link/MachO/zld.zig+1-1
...@@ -85,7 +85,7 @@ pub fn linkWithZld(...@@ -85,7 +85,7 @@ pub fn linkWithZld(
85 }85 }
86 try link.hashAddSystemLibs(&man, comp.system_libs);86 try link.hashAddSystemLibs(&man, comp.system_libs);
87 man.hash.addOptionalBytes(comp.sysroot);87 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());
89 try man.addOptionalFile(macho_file.entitlements);89 try man.addOptionalFile(macho_file.entitlements);
9090
91 // We don't actually care whether it's a cache hit or miss; we just91 // 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(...@@ -60,7 +60,6 @@ pub fn createEmpty(
60 .disable_lld_caching = options.disable_lld_caching,60 .disable_lld_caching = options.disable_lld_caching,
61 .build_id = options.build_id,61 .build_id = options.build_id,
62 .rpath_list = options.rpath_list,62 .rpath_list = options.rpath_list,
63 .force_undefined_symbols = options.force_undefined_symbols,
64 },63 },
65 .llvm_object = llvm_object,64 .llvm_object = llvm_object,
66 };65 };
src/link/Plan9.zig-1
...@@ -325,7 +325,6 @@ pub fn createEmpty(...@@ -325,7 +325,6 @@ pub fn createEmpty(
325 .disable_lld_caching = options.disable_lld_caching,325 .disable_lld_caching = options.disable_lld_caching,
326 .build_id = options.build_id,326 .build_id = options.build_id,
327 .rpath_list = options.rpath_list,327 .rpath_list = options.rpath_list,
328 .force_undefined_symbols = options.force_undefined_symbols,
329 },328 },
330 .sixtyfour_bit = sixtyfour_bit,329 .sixtyfour_bit = sixtyfour_bit,
331 .bases = undefined,330 .bases = undefined,
src/link/SpirV.zig-1
...@@ -71,7 +71,6 @@ pub fn createEmpty(...@@ -71,7 +71,6 @@ pub fn createEmpty(
71 .disable_lld_caching = options.disable_lld_caching,71 .disable_lld_caching = options.disable_lld_caching,
72 .build_id = options.build_id,72 .build_id = options.build_id,
73 .rpath_list = options.rpath_list,73 .rpath_list = options.rpath_list,
74 .force_undefined_symbols = options.force_undefined_symbols,
75 },74 },
76 .object = codegen.Object.init(gpa),75 .object = codegen.Object.init(gpa),
77 };76 };
src/link/Wasm.zig-1
...@@ -424,7 +424,6 @@ pub fn createEmpty(...@@ -424,7 +424,6 @@ pub fn createEmpty(
424 .disable_lld_caching = options.disable_lld_caching,424 .disable_lld_caching = options.disable_lld_caching,
425 .build_id = options.build_id,425 .build_id = options.build_id,
426 .rpath_list = options.rpath_list,426 .rpath_list = options.rpath_list,
427 .force_undefined_symbols = options.force_undefined_symbols,
428 },427 },
429 .name = undefined,428 .name = undefined,
430 .import_table = options.import_table,429 .import_table = options.import_table,