Commit 435b74acd6

Andrew Kelley <andrew@ziglang.org>
2023-12-27 18:23:27
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.
1 parent c8c32a0
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}));
         }
 
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);
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
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,
         },
     };
 
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,
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);
         }
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, .{});
     }
 
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,
     };
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,
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),
     };
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,
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 <symbol>` for ELF/MachO and `/include:<symbol>` 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) {
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 <symbol>` for ELF/MachO and `/include:<symbol>` 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