authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-10 11:11:57+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-21 06:08:50+02:00
log7c9035f63590021906de66c7490458a49afe9e0a
treeaf2e872aaa3856cc902f034371b1fcc44f142796
parent55848363fd5c88bd6313880e770515a9c548c0df

link.Elf: Don't require linking libc for dynamic linker path to take effect.

Closes #23813.

4 files changed, 20 insertions(+), 10 deletions(-)

src/Compilation.zig+2
...@@ -1351,6 +1351,7 @@ pub const cache_helpers = struct {...@@ -1351,6 +1351,7 @@ pub const cache_helpers = struct {
1351 hh.add(target.ofmt);1351 hh.add(target.ofmt);
1352 hh.add(resolved_target.is_native_os);1352 hh.add(resolved_target.is_native_os);
1353 hh.add(resolved_target.is_native_abi);1353 hh.add(resolved_target.is_native_abi);
1354 hh.add(resolved_target.is_explicit_dynamic_linker);
1354 }1355 }
13551356
1356 pub fn addEmitLoc(hh: *Cache.HashHelper, emit_loc: EmitLoc) void {1357 pub fn addEmitLoc(hh: *Cache.HashHelper, emit_loc: EmitLoc) void {
...@@ -4748,6 +4749,7 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -4748,6 +4749,7 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) anye
47484749
4749 .is_native_os = false,4750 .is_native_os = false,
4750 .is_native_abi = false,4751 .is_native_abi = false,
4752 .is_explicit_dynamic_linker = false,
4751 };4753 };
47524754
4753 const config = try Config.resolve(.{4755 const config = try Config.resolve(.{
src/Package/Module.zig+3
...@@ -88,6 +88,7 @@ pub const ResolvedTarget = struct {...@@ -88,6 +88,7 @@ pub const ResolvedTarget = struct {
88 result: std.Target,88 result: std.Target,
89 is_native_os: bool,89 is_native_os: bool,
90 is_native_abi: bool,90 is_native_abi: bool,
91 is_explicit_dynamic_linker: bool,
91 llvm_cpu_features: ?[*:0]const u8 = null,92 llvm_cpu_features: ?[*:0]const u8 = null,
92};93};
9394
...@@ -365,6 +366,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {...@@ -365,6 +366,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
365 .result = target,366 .result = target,
366 .is_native_os = resolved_target.is_native_os,367 .is_native_os = resolved_target.is_native_os,
367 .is_native_abi = resolved_target.is_native_abi,368 .is_native_abi = resolved_target.is_native_abi,
369 .is_explicit_dynamic_linker = resolved_target.is_explicit_dynamic_linker,
368 .llvm_cpu_features = llvm_cpu_features,370 .llvm_cpu_features = llvm_cpu_features,
369 },371 },
370 .optimize_mode = optimize_mode,372 .optimize_mode = optimize_mode,
...@@ -441,6 +443,7 @@ pub fn createBuiltin(arena: Allocator, opts: Builtin, dirs: Compilation.Director...@@ -441,6 +443,7 @@ pub fn createBuiltin(arena: Allocator, opts: Builtin, dirs: Compilation.Director
441 // These values are not in `opts`, but do not matter because `builtin.zig` contains no runtime code.443 // These values are not in `opts`, but do not matter because `builtin.zig` contains no runtime code.
442 .is_native_os = false,444 .is_native_os = false,
443 .is_native_abi = false,445 .is_native_abi = false,
446 .is_explicit_dynamic_linker = false,
444 .llvm_cpu_features = null,447 .llvm_cpu_features = null,
445 },448 },
446 .optimize_mode = opts.optimize_mode,449 .optimize_mode = opts.optimize_mode,
src/link/Elf.zig+11-10
...@@ -1533,8 +1533,7 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s...@@ -1533,8 +1533,7 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
1533 const link_mode = comp.config.link_mode;1533 const link_mode = comp.config.link_mode;
1534 const is_dyn_lib = link_mode == .dynamic and is_lib;1534 const is_dyn_lib = link_mode == .dynamic and is_lib;
1535 const is_exe_or_dyn_lib = is_dyn_lib or output_mode == .Exe;1535 const is_exe_or_dyn_lib = is_dyn_lib or output_mode == .Exe;
1536 const have_dynamic_linker = comp.config.link_libc and1536 const have_dynamic_linker = link_mode == .dynamic and is_exe_or_dyn_lib;
1537 link_mode == .dynamic and is_exe_or_dyn_lib;
1538 const target = self.getTarget();1537 const target = self.getTarget();
1539 const compiler_rt_path: ?Path = blk: {1538 const compiler_rt_path: ?Path = blk: {
1540 if (comp.compiler_rt_lib) |x| break :blk x.full_object_path;1539 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...@@ -1616,9 +1615,9 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
1616 if (comp.libc_installation) |libc_installation| {1615 if (comp.libc_installation) |libc_installation| {
1617 man.hash.addBytes(libc_installation.crt_dir.?);1616 man.hash.addBytes(libc_installation.crt_dir.?);
1618 }1617 }
1619 if (have_dynamic_linker) {1618 }
1620 man.hash.addOptionalBytes(target.dynamic_linker.get());1619 if (have_dynamic_linker) {
1621 }1620 man.hash.addOptionalBytes(target.dynamic_linker.get());
1622 }1621 }
1623 man.hash.addOptionalBytes(self.soname);1622 man.hash.addOptionalBytes(self.soname);
1624 man.hash.addOptional(comp.version);1623 man.hash.addOptional(comp.version);
...@@ -1908,12 +1907,14 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s...@@ -1908,12 +1907,14 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
1908 try argv.append("-L");1907 try argv.append("-L");
1909 try argv.append(libc_installation.crt_dir.?);1908 try argv.append(libc_installation.crt_dir.?);
1910 }1909 }
1910 }
19111911
1912 if (have_dynamic_linker) {1912 if (have_dynamic_linker and
1913 if (target.dynamic_linker.get()) |dynamic_linker| {1913 (comp.config.link_libc or comp.root_mod.resolved_target.is_explicit_dynamic_linker))
1914 try argv.append("-dynamic-linker");1914 {
1915 try argv.append(dynamic_linker);1915 if (target.dynamic_linker.get()) |dynamic_linker| {
1916 }1916 try argv.append("-dynamic-linker");
1917 try argv.append(dynamic_linker);
1917 }1918 }
1918 }1919 }
19191920
src/main.zig+4
...@@ -3910,6 +3910,7 @@ fn createModule(...@@ -3910,6 +3910,7 @@ fn createModule(
3910 .result = target,3910 .result = target,
3911 .is_native_os = target_query.isNativeOs(),3911 .is_native_os = target_query.isNativeOs(),
3912 .is_native_abi = target_query.isNativeAbi(),3912 .is_native_abi = target_query.isNativeAbi(),
3913 .is_explicit_dynamic_linker = !target_query.dynamic_linker.eql(.none),
3913 };3914 };
3914 };3915 };
39153916
...@@ -5041,6 +5042,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -5041,6 +5042,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
5041 .result = std.zig.resolveTargetQueryOrFatal(target_query),5042 .result = std.zig.resolveTargetQueryOrFatal(target_query),
5042 .is_native_os = false,5043 .is_native_os = false,
5043 .is_native_abi = false,5044 .is_native_abi = false,
5045 .is_explicit_dynamic_linker = false,
5044 };5046 };
5045 }5047 }
5046 }5048 }
...@@ -5048,6 +5050,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -5048,6 +5050,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
5048 .result = std.zig.resolveTargetQueryOrFatal(.{}),5050 .result = std.zig.resolveTargetQueryOrFatal(.{}),
5049 .is_native_os = true,5051 .is_native_os = true,
5050 .is_native_abi = true,5052 .is_native_abi = true,
5053 .is_explicit_dynamic_linker = false,
5051 };5054 };
5052 };5055 };
50535056
...@@ -5465,6 +5468,7 @@ fn jitCmd(...@@ -5465,6 +5468,7 @@ fn jitCmd(
5465 .result = std.zig.resolveTargetQueryOrFatal(target_query),5468 .result = std.zig.resolveTargetQueryOrFatal(target_query),
5466 .is_native_os = true,5469 .is_native_os = true,
5467 .is_native_abi = true,5470 .is_native_abi = true,
5471 .is_explicit_dynamic_linker = false,
5468 };5472 };
54695473
5470 const exe_basename = try std.zig.binNameAlloc(arena, .{5474 const exe_basename = try std.zig.binNameAlloc(arena, .{