diff --git a/src/Compilation.zig b/src/Compilation.zig index 86caba7651..d42d731188 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -86,6 +86,10 @@ no_builtin: bool, function_sections: bool, data_sections: bool, native_system_include_paths: []const []const u8, +/// List of symbols forced as undefined in the symbol table +/// thus forcing their resolution by the linker. +/// Corresponds to `-u ` for ELF/MachO and `/include:` for COFF/PE. +force_undefined_symbols: std.StringArrayHashMapUnmanaged(void), c_object_table: std.AutoArrayHashMapUnmanaged(*CObject, void) = .{}, win32_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 { .data_sections = options.data_sections, .native_system_include_paths = options.native_system_include_paths, .wasi_emulated_libs = options.wasi_emulated_libs, + .force_undefined_symbols = options.force_undefined_symbols, }; // Prevent some footguns by making the "any" fields of config reflect @@ -1569,7 +1574,6 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { .headerpad_size = options.headerpad_size, .headerpad_max_install_names = options.headerpad_max_install_names, .dead_strip_dylibs = options.dead_strip_dylibs, - .force_undefined_symbols = options.force_undefined_symbols, .pdb_source_path = options.pdb_source_path, .pdb_out_path = options.pdb_out_path, .entry_addr = null, // CLI does not expose this option (yet?) @@ -1830,18 +1834,16 @@ pub fn create(gpa: Allocator, options: CreateOptions) !*Compilation { try comp.work_queue.writeItem(.libtsan); } - if (comp.bin_file) |lf| { - if (target.isMinGW() and comp.config.any_non_single_threaded) { - // LLD might drop some symbols as unused during LTO and GCing, therefore, - // we force mark them for resolution here. + if (target.isMinGW() and comp.config.any_non_single_threaded) { + // LLD might drop some symbols as unused during LTO and GCing, therefore, + // we force mark them for resolution here. - const tls_index_sym = switch (target.cpu.arch) { - .x86 => "__tls_index", - else => "_tls_index", - }; + const tls_index_sym = switch (target.cpu.arch) { + .x86 => "__tls_index", + else => "_tls_index", + }; - try lf.force_undefined_symbols.put(comp.gpa, tls_index_sym, {}); - } + try comp.force_undefined_symbols.put(comp.gpa, tls_index_sym, {}); } if (comp.include_compiler_rt and capable_of_building_compiler_rt) { @@ -2447,6 +2449,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes man.hash.addOptionalBytes(comp.sysroot); man.hash.addOptional(comp.version); man.hash.addListOfBytes(comp.rc_include_dir_list); + man.hash.addListOfBytes(comp.force_undefined_symbols.keys()); cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_asm); cache_helpers.addOptionalEmitLoc(&man.hash, comp.emit_llvm_ir); @@ -2482,7 +2485,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes man.hash.add(lf.gc_sections); man.hash.addListOfBytes(lf.rpath_list); man.hash.add(lf.build_id); - man.hash.addListOfBytes(lf.force_undefined_symbols.keys()); man.hash.add(lf.allow_shlib_undefined); switch (lf.tag) { diff --git a/src/link.zig b/src/link.zig index fdcd5fcf41..6729f00ea9 100644 --- a/src/link.zig +++ b/src/link.zig @@ -64,10 +64,6 @@ pub const File = struct { print_gc_sections: bool, build_id: std.zig.BuildId, rpath_list: []const []const u8, - /// List of symbols forced as undefined in the symbol table - /// thus forcing their resolution by the linker. - /// Corresponds to `-u ` for ELF/MachO and `/include:` for COFF/PE. - force_undefined_symbols: std.StringArrayHashMapUnmanaged(void), allow_shlib_undefined: bool, stack_size: u64, @@ -129,7 +125,6 @@ pub const File = struct { print_icf_sections: bool, print_map: bool, - force_undefined_symbols: std.StringArrayHashMapUnmanaged(void), /// Use a wrapper function for symbol. Any undefined reference to symbol /// will be resolved to __wrap_symbol. Any undefined reference to /// __real_symbol will be resolved to symbol. This can be used to provide a diff --git a/src/link/C.zig b/src/link/C.zig index 669587bda9..958ce12a06 100644 --- a/src/link/C.zig +++ b/src/link/C.zig @@ -142,7 +142,6 @@ pub fn createEmpty( .disable_lld_caching = options.disable_lld_caching, .build_id = options.build_id, .rpath_list = options.rpath_list, - .force_undefined_symbols = options.force_undefined_symbols, }, }; diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 53f999b965..aefecb014e 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -284,7 +284,6 @@ pub fn createEmpty( .disable_lld_caching = options.disable_lld_caching, .build_id = options.build_id, .rpath_list = options.rpath_list, - .force_undefined_symbols = options.force_undefined_symbols, }, .ptr_width = ptr_width, .page_size = page_size, diff --git a/src/link/Coff/lld.zig b/src/link/Coff/lld.zig index 6afe741f66..3a833c40ae 100644 --- a/src/link/Coff/lld.zig +++ b/src/link/Coff/lld.zig @@ -103,7 +103,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod } } try link.hashAddSystemLibs(&man, comp.system_libs); - man.hash.addListOfBytes(self.base.force_undefined_symbols.keys()); + man.hash.addListOfBytes(comp.force_undefined_symbols.keys()); man.hash.addOptional(self.subsystem); man.hash.add(comp.config.is_test); man.hash.add(self.tsaware); @@ -217,7 +217,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod } } - for (self.base.force_undefined_symbols.keys()) |symbol| { + for (comp.force_undefined_symbols.keys()) |symbol| { try argv.append(try allocPrint(arena, "-INCLUDE:{s}", .{symbol})); } diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 65fc0ff5a3..085522d67b 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -285,7 +285,6 @@ pub fn createEmpty( .disable_lld_caching = options.disable_lld_caching, .build_id = options.build_id, .rpath_list = options.rpath_list, - .force_undefined_symbols = options.force_undefined_symbols, }, .ptr_width = ptr_width, .page_size = page_size, @@ -2473,7 +2472,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v man.hash.addOptionalBytes(self.soname); man.hash.addOptional(comp.version); try link.hashAddSystemLibs(&man, self.base.comp.system_libs); - man.hash.addListOfBytes(self.base.force_undefined_symbols.keys()); + man.hash.addListOfBytes(comp.force_undefined_symbols.keys()); man.hash.add(self.base.allow_shlib_undefined); man.hash.add(self.bind_global_refs_locally); man.hash.add(self.compress_debug_sections); @@ -2574,7 +2573,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v try argv.appendSlice(&.{ "--entry", name }); } - for (self.base.force_undefined_symbols.keys()) |sym| { + for (comp.force_undefined_symbols.keys()) |sym| { try argv.append("-u"); try argv.append(sym); } diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 8c0874e3cb..6bf4d95ef4 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -220,7 +220,6 @@ pub fn createEmpty( .disable_lld_caching = options.disable_lld_caching, .build_id = options.build_id, .rpath_list = options.rpath_list, - .force_undefined_symbols = options.force_undefined_symbols, }, .mode = mode, .pagezero_vmsize = options.pagezero_size orelse default_pagezero_vmsize, @@ -1642,7 +1641,7 @@ pub fn resolveSymbols(self: *MachO) !void { } // Force resolution of any symbols requested by the user. - for (self.base.force_undefined_symbols.keys()) |sym_name| { + for (comp.force_undefined_symbols.keys()) |sym_name| { _ = try self.addUndefined(sym_name, .{}); } diff --git a/src/link/MachO/dead_strip.zig b/src/link/MachO/dead_strip.zig index 02cf68e46e..fe3740e826 100644 --- a/src/link/MachO/dead_strip.zig +++ b/src/link/MachO/dead_strip.zig @@ -34,7 +34,9 @@ fn addRoot(macho_file: *MachO, roots: *AtomTable, file: u32, sym_loc: SymbolWith fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void { log.debug("collecting roots", .{}); - switch (macho_file.base.comp.config.output_mode) { + const comp = macho_file.base.comp; + + switch (comp.config.output_mode) { .Exe => { // Add entrypoint as GC root if (macho_file.getEntryPoint()) |global| { @@ -61,7 +63,7 @@ fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void { } // Add all symbols force-defined by the user. - for (macho_file.base.force_undefined_symbols.keys()) |sym_name| { + for (comp.force_undefined_symbols.keys()) |sym_name| { const global_index = macho_file.resolver.get(sym_name).?; const global = macho_file.globals.items[global_index]; const sym = macho_file.getSymbol(global); diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig index 05fa8e671a..81b10c59d9 100644 --- a/src/link/MachO/zld.zig +++ b/src/link/MachO/zld.zig @@ -85,7 +85,7 @@ pub fn linkWithZld( } try link.hashAddSystemLibs(&man, comp.system_libs); man.hash.addOptionalBytes(comp.sysroot); - man.hash.addListOfBytes(macho_file.base.force_undefined_symbols.keys()); + man.hash.addListOfBytes(comp.force_undefined_symbols.keys()); try man.addOptionalFile(macho_file.entitlements); // We don't actually care whether it's a cache hit or miss; we just diff --git a/src/link/NvPtx.zig b/src/link/NvPtx.zig index 2025372323..1f5af5b86e 100644 --- a/src/link/NvPtx.zig +++ b/src/link/NvPtx.zig @@ -60,7 +60,6 @@ pub fn createEmpty( .disable_lld_caching = options.disable_lld_caching, .build_id = options.build_id, .rpath_list = options.rpath_list, - .force_undefined_symbols = options.force_undefined_symbols, }, .llvm_object = llvm_object, }; diff --git a/src/link/Plan9.zig b/src/link/Plan9.zig index ceabfe2864..e93c889dcd 100644 --- a/src/link/Plan9.zig +++ b/src/link/Plan9.zig @@ -325,7 +325,6 @@ pub fn createEmpty( .disable_lld_caching = options.disable_lld_caching, .build_id = options.build_id, .rpath_list = options.rpath_list, - .force_undefined_symbols = options.force_undefined_symbols, }, .sixtyfour_bit = sixtyfour_bit, .bases = undefined, diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index 5124ff19f6..fc9e0c9a60 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -71,7 +71,6 @@ pub fn createEmpty( .disable_lld_caching = options.disable_lld_caching, .build_id = options.build_id, .rpath_list = options.rpath_list, - .force_undefined_symbols = options.force_undefined_symbols, }, .object = codegen.Object.init(gpa), }; diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 7f3ca877c6..cdd40891da 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -424,7 +424,6 @@ pub fn createEmpty( .disable_lld_caching = options.disable_lld_caching, .build_id = options.build_id, .rpath_list = options.rpath_list, - .force_undefined_symbols = options.force_undefined_symbols, }, .name = undefined, .import_table = options.import_table,