Skip to content

Commit 7c9035f

Browse files
committed
link.Elf: Don't require linking libc for dynamic linker path to take effect.
Closes #23813.
1 parent 5584836 commit 7c9035f

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

src/Compilation.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,7 @@ pub const cache_helpers = struct {
13511351
hh.add(target.ofmt);
13521352
hh.add(resolved_target.is_native_os);
13531353
hh.add(resolved_target.is_native_abi);
1354+
hh.add(resolved_target.is_explicit_dynamic_linker);
13541355
}
13551356

13561357
pub fn addEmitLoc(hh: *Cache.HashHelper, emit_loc: EmitLoc) void {
@@ -4748,6 +4749,7 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) anye
47484749

47494750
.is_native_os = false,
47504751
.is_native_abi = false,
4752+
.is_explicit_dynamic_linker = false,
47514753
};
47524754

47534755
const config = try Config.resolve(.{

src/Package/Module.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub const ResolvedTarget = struct {
8888
result: std.Target,
8989
is_native_os: bool,
9090
is_native_abi: bool,
91+
is_explicit_dynamic_linker: bool,
9192
llvm_cpu_features: ?[*:0]const u8 = null,
9293
};
9394

@@ -365,6 +366,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
365366
.result = target,
366367
.is_native_os = resolved_target.is_native_os,
367368
.is_native_abi = resolved_target.is_native_abi,
369+
.is_explicit_dynamic_linker = resolved_target.is_explicit_dynamic_linker,
368370
.llvm_cpu_features = llvm_cpu_features,
369371
},
370372
.optimize_mode = optimize_mode,
@@ -441,6 +443,7 @@ pub fn createBuiltin(arena: Allocator, opts: Builtin, dirs: Compilation.Director
441443
// These values are not in `opts`, but do not matter because `builtin.zig` contains no runtime code.
442444
.is_native_os = false,
443445
.is_native_abi = false,
446+
.is_explicit_dynamic_linker = false,
444447
.llvm_cpu_features = null,
445448
},
446449
.optimize_mode = opts.optimize_mode,

src/link/Elf.zig

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,8 +1533,7 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
15331533
const link_mode = comp.config.link_mode;
15341534
const is_dyn_lib = link_mode == .dynamic and is_lib;
15351535
const is_exe_or_dyn_lib = is_dyn_lib or output_mode == .Exe;
1536-
const have_dynamic_linker = comp.config.link_libc and
1537-
link_mode == .dynamic and is_exe_or_dyn_lib;
1536+
const have_dynamic_linker = link_mode == .dynamic and is_exe_or_dyn_lib;
15381537
const target = self.getTarget();
15391538
const compiler_rt_path: ?Path = blk: {
15401539
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
16161615
if (comp.libc_installation) |libc_installation| {
16171616
man.hash.addBytes(libc_installation.crt_dir.?);
16181617
}
1619-
if (have_dynamic_linker) {
1620-
man.hash.addOptionalBytes(target.dynamic_linker.get());
1621-
}
1618+
}
1619+
if (have_dynamic_linker) {
1620+
man.hash.addOptionalBytes(target.dynamic_linker.get());
16221621
}
16231622
man.hash.addOptionalBytes(self.soname);
16241623
man.hash.addOptional(comp.version);
@@ -1908,12 +1907,14 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
19081907
try argv.append("-L");
19091908
try argv.append(libc_installation.crt_dir.?);
19101909
}
1910+
}
19111911

1912-
if (have_dynamic_linker) {
1913-
if (target.dynamic_linker.get()) |dynamic_linker| {
1914-
try argv.append("-dynamic-linker");
1915-
try argv.append(dynamic_linker);
1916-
}
1912+
if (have_dynamic_linker and
1913+
(comp.config.link_libc or comp.root_mod.resolved_target.is_explicit_dynamic_linker))
1914+
{
1915+
if (target.dynamic_linker.get()) |dynamic_linker| {
1916+
try argv.append("-dynamic-linker");
1917+
try argv.append(dynamic_linker);
19171918
}
19181919
}
19191920

src/main.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,6 +3910,7 @@ fn createModule(
39103910
.result = target,
39113911
.is_native_os = target_query.isNativeOs(),
39123912
.is_native_abi = target_query.isNativeAbi(),
3913+
.is_explicit_dynamic_linker = !target_query.dynamic_linker.eql(.none),
39133914
};
39143915
};
39153916

@@ -5041,13 +5042,15 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50415042
.result = std.zig.resolveTargetQueryOrFatal(target_query),
50425043
.is_native_os = false,
50435044
.is_native_abi = false,
5045+
.is_explicit_dynamic_linker = false,
50445046
};
50455047
}
50465048
}
50475049
break :t .{
50485050
.result = std.zig.resolveTargetQueryOrFatal(.{}),
50495051
.is_native_os = true,
50505052
.is_native_abi = true,
5053+
.is_explicit_dynamic_linker = false,
50515054
};
50525055
};
50535056

@@ -5465,6 +5468,7 @@ fn jitCmd(
54655468
.result = std.zig.resolveTargetQueryOrFatal(target_query),
54665469
.is_native_os = true,
54675470
.is_native_abi = true,
5471+
.is_explicit_dynamic_linker = false,
54685472
};
54695473

54705474
const exe_basename = try std.zig.binNameAlloc(arena, .{

0 commit comments

Comments
 (0)