Skip to content

Commit e1e5dfe

Browse files
committed
compiler: get the dynamic linker from the target
instead of passing it to Compilation separately and storing it separately in the linker options.
1 parent afe0074 commit e1e5dfe

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

src/Compilation.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,6 @@ pub const InitOptions = struct {
812812
main_mod: ?*Package.Module,
813813
output_mode: std.builtin.OutputMode,
814814
thread_pool: *ThreadPool,
815-
dynamic_linker: ?[]const u8 = null,
816815
sysroot: ?[]const u8 = null,
817816
/// `null` means to not emit a binary file.
818817
emit_bin: ?EmitLoc,
@@ -1854,7 +1853,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
18541853
.root_name = root_name,
18551854
.module = module,
18561855
.target = options.target,
1857-
.dynamic_linker = options.dynamic_linker,
18581856
.sysroot = sysroot,
18591857
.output_mode = options.output_mode,
18601858
.link_mode = link_mode,
@@ -2824,7 +2822,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
28242822
man.hash.addOptionalBytes(libc_installation.kernel32_lib_dir);
28252823
}
28262824
}
2827-
man.hash.addOptionalBytes(comp.bin_file.options.dynamic_linker);
2825+
man.hash.addOptionalBytes(target.dynamic_linker.get());
28282826
}
28292827
man.hash.addOptionalBytes(comp.bin_file.options.soname);
28302828
man.hash.addOptional(comp.bin_file.options.version);

src/link.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ pub const Options = struct {
103103
root_name: [:0]const u8,
104104
/// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`.
105105
module: ?*Module,
106-
dynamic_linker: ?[]const u8,
107106
/// The root path for the dynamic linker and system libraries (as well as frameworks on Darwin)
108107
sysroot: ?[]const u8,
109108
/// Used for calculating how much space to reserve for symbols in case the binary file

src/link/Elf.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
15741574
}
15751575
} else {
15761576
if (!self.isStatic()) {
1577-
if (self.base.options.dynamic_linker) |path| {
1577+
if (self.base.options.target.dynamic_linker.get()) |path| {
15781578
try argv.append("-dynamic-linker");
15791579
try argv.append(path);
15801580
}
@@ -2374,7 +2374,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
23742374
man.hash.addBytes(libc_installation.crt_dir.?);
23752375
}
23762376
if (have_dynamic_linker) {
2377-
man.hash.addOptionalBytes(self.base.options.dynamic_linker);
2377+
man.hash.addOptionalBytes(self.base.options.target.dynamic_linker.get());
23782378
}
23792379
}
23802380
man.hash.addOptionalBytes(self.base.options.soname);
@@ -2687,7 +2687,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
26872687
}
26882688

26892689
if (have_dynamic_linker) {
2690-
if (self.base.options.dynamic_linker) |dynamic_linker| {
2690+
if (self.base.options.target.dynamic_linker.get()) |dynamic_linker| {
26912691
try argv.append("-dynamic-linker");
26922692
try argv.append(dynamic_linker);
26932693
}
@@ -3503,7 +3503,7 @@ fn initSyntheticSections(self: *Elf) !void {
35033503
// a segfault in the dynamic linker trying to load a binary that is static
35043504
// and doesn't contain .dynamic section.
35053505
if (self.isStatic() and !self.base.options.pie) break :blk false;
3506-
break :blk self.base.options.dynamic_linker != null;
3506+
break :blk self.base.options.target.dynamic_linker.get() != null;
35073507
};
35083508
if (needs_interp) {
35093509
self.interp_section_index = try self.addSection(.{
@@ -4244,7 +4244,7 @@ fn updateSectionSizes(self: *Elf) !void {
42444244
}
42454245

42464246
if (self.interp_section_index) |index| {
4247-
self.shdrs.items[index].sh_size = self.base.options.dynamic_linker.?.len + 1;
4247+
self.shdrs.items[index].sh_size = self.base.options.target.dynamic_linker.get().?.len + 1;
42484248
}
42494249

42504250
if (self.hash_section_index) |index| {
@@ -4938,14 +4938,14 @@ fn writeSyntheticSections(self: *Elf) !void {
49384938
const gpa = self.base.allocator;
49394939

49404940
if (self.interp_section_index) |shndx| {
4941+
var buffer: [256]u8 = undefined;
4942+
const interp = self.base.options.target.dynamic_linker.get().?;
4943+
@memcpy(buffer[0..interp.len], interp);
4944+
buffer[interp.len] = 0;
4945+
const contents = buffer[0 .. interp.len + 1];
49414946
const shdr = self.shdrs.items[shndx];
4942-
const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
4943-
var buffer = try gpa.alloc(u8, sh_size);
4944-
defer gpa.free(buffer);
4945-
const dylinker = self.base.options.dynamic_linker.?;
4946-
@memcpy(buffer[0..dylinker.len], dylinker);
4947-
buffer[dylinker.len] = 0;
4948-
try self.base.file.?.pwriteAll(buffer, shdr.sh_offset);
4947+
assert(shdr.sh_size == contents.len);
4948+
try self.base.file.?.pwriteAll(contents, shdr.sh_offset);
49494949
}
49504950

49514951
if (self.hash_section_index) |shndx| {

src/main.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3456,7 +3456,6 @@ fn buildOutputType(
34563456
.target = target,
34573457
.is_native_os = target_query.isNativeOs(),
34583458
.is_native_abi = target_query.isNativeAbi(),
3459-
.dynamic_linker = target.dynamic_linker.get(),
34603459
.sysroot = sysroot,
34613460
.output_mode = output_mode,
34623461
.main_mod = main_mod,
@@ -5287,7 +5286,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
52875286
.target = target,
52885287
.is_native_os = target_query.isNativeOs(),
52895288
.is_native_abi = target_query.isNativeAbi(),
5290-
.dynamic_linker = target.dynamic_linker.get(),
52915289
.output_mode = .Exe,
52925290
.main_mod = &main_mod,
52935291
.emit_bin = emit_bin,

0 commit comments

Comments
 (0)