Commit 7c9035f635

Alex Rønne Petersen <alex@alexrp.com>
2025-05-10 11:11:57
link.Elf: Don't require linking libc for dynamic linker path to take effect.
Closes #23813.
1 parent 5584836
Changed files (4)
src/link/Elf.zig
@@ -1533,8 +1533,7 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
     const link_mode = comp.config.link_mode;
     const is_dyn_lib = link_mode == .dynamic and is_lib;
     const is_exe_or_dyn_lib = is_dyn_lib or output_mode == .Exe;
-    const have_dynamic_linker = comp.config.link_libc and
-        link_mode == .dynamic and is_exe_or_dyn_lib;
+    const have_dynamic_linker = link_mode == .dynamic and is_exe_or_dyn_lib;
     const target = self.getTarget();
     const compiler_rt_path: ?Path = blk: {
         if (comp.compiler_rt_lib) |x| break :blk x.full_object_path;
@@ -1616,9 +1615,9 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
             if (comp.libc_installation) |libc_installation| {
                 man.hash.addBytes(libc_installation.crt_dir.?);
             }
-            if (have_dynamic_linker) {
-                man.hash.addOptionalBytes(target.dynamic_linker.get());
-            }
+        }
+        if (have_dynamic_linker) {
+            man.hash.addOptionalBytes(target.dynamic_linker.get());
         }
         man.hash.addOptionalBytes(self.soname);
         man.hash.addOptional(comp.version);
@@ -1908,12 +1907,14 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
                 try argv.append("-L");
                 try argv.append(libc_installation.crt_dir.?);
             }
+        }
 
-            if (have_dynamic_linker) {
-                if (target.dynamic_linker.get()) |dynamic_linker| {
-                    try argv.append("-dynamic-linker");
-                    try argv.append(dynamic_linker);
-                }
+        if (have_dynamic_linker and
+            (comp.config.link_libc or comp.root_mod.resolved_target.is_explicit_dynamic_linker))
+        {
+            if (target.dynamic_linker.get()) |dynamic_linker| {
+                try argv.append("-dynamic-linker");
+                try argv.append(dynamic_linker);
             }
         }
 
src/Package/Module.zig
@@ -88,6 +88,7 @@ pub const ResolvedTarget = struct {
     result: std.Target,
     is_native_os: bool,
     is_native_abi: bool,
+    is_explicit_dynamic_linker: bool,
     llvm_cpu_features: ?[*:0]const u8 = null,
 };
 
@@ -365,6 +366,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
             .result = target,
             .is_native_os = resolved_target.is_native_os,
             .is_native_abi = resolved_target.is_native_abi,
+            .is_explicit_dynamic_linker = resolved_target.is_explicit_dynamic_linker,
             .llvm_cpu_features = llvm_cpu_features,
         },
         .optimize_mode = optimize_mode,
@@ -441,6 +443,7 @@ pub fn createBuiltin(arena: Allocator, opts: Builtin, dirs: Compilation.Director
             // These values are not in `opts`, but do not matter because `builtin.zig` contains no runtime code.
             .is_native_os = false,
             .is_native_abi = false,
+            .is_explicit_dynamic_linker = false,
             .llvm_cpu_features = null,
         },
         .optimize_mode = opts.optimize_mode,
src/Compilation.zig
@@ -1351,6 +1351,7 @@ pub const cache_helpers = struct {
         hh.add(target.ofmt);
         hh.add(resolved_target.is_native_os);
         hh.add(resolved_target.is_native_abi);
+        hh.add(resolved_target.is_explicit_dynamic_linker);
     }
 
     pub fn addEmitLoc(hh: *Cache.HashHelper, emit_loc: EmitLoc) void {
@@ -4748,6 +4749,7 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) anye
 
         .is_native_os = false,
         .is_native_abi = false,
+        .is_explicit_dynamic_linker = false,
     };
 
     const config = try Config.resolve(.{
src/main.zig
@@ -3910,6 +3910,7 @@ fn createModule(
             .result = target,
             .is_native_os = target_query.isNativeOs(),
             .is_native_abi = target_query.isNativeAbi(),
+            .is_explicit_dynamic_linker = !target_query.dynamic_linker.eql(.none),
         };
     };
 
@@ -5041,6 +5042,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
                     .result = std.zig.resolveTargetQueryOrFatal(target_query),
                     .is_native_os = false,
                     .is_native_abi = false,
+                    .is_explicit_dynamic_linker = false,
                 };
             }
         }
@@ -5048,6 +5050,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
             .result = std.zig.resolveTargetQueryOrFatal(.{}),
             .is_native_os = true,
             .is_native_abi = true,
+            .is_explicit_dynamic_linker = false,
         };
     };
 
@@ -5465,6 +5468,7 @@ fn jitCmd(
         .result = std.zig.resolveTargetQueryOrFatal(target_query),
         .is_native_os = true,
         .is_native_abi = true,
+        .is_explicit_dynamic_linker = false,
     };
 
     const exe_basename = try std.zig.binNameAlloc(arena, .{