authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-04 21:56:29-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:18-07:00
log77420af9d01b1932a583fdcbfa169f2eb2b7c221
treedfaec3c2eba04fe64c0f77e27ab423d1226ddbed
parentce94c28e53e706e506d4c95c25c8bf99e7a597eb

compiler: get the dynamic linker from the target

instead of passing it to Compilation separately and storing it separately in the linker options.

4 files changed, 13 insertions(+), 18 deletions(-)

src/Compilation.zig+1-3
......@@ -803,7 +803,6 @@ pub const InitOptions = struct {
803803 main_mod: ?*Package.Module,
804804 output_mode: std.builtin.OutputMode,
805805 thread_pool: *ThreadPool,
806 dynamic_linker: ?[]const u8 = null,
807806 sysroot: ?[]const u8 = null,
808807 /// `null` means to not emit a binary file.
809808 emit_bin: ?EmitLoc,
......@@ -1836,7 +1835,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
18361835 .root_name = root_name,
18371836 .module = module,
18381837 .target = options.target,
1839 .dynamic_linker = options.dynamic_linker,
18401838 .sysroot = sysroot,
18411839 .output_mode = options.output_mode,
18421840 .link_mode = link_mode,
......@@ -2800,7 +2798,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
28002798 man.hash.addOptionalBytes(libc_installation.kernel32_lib_dir);
28012799 }
28022800 }
2803 man.hash.addOptionalBytes(comp.bin_file.options.dynamic_linker);
2801 man.hash.addOptionalBytes(target.dynamic_linker.get());
28042802 }
28052803 man.hash.addOptionalBytes(comp.bin_file.options.soname);
28062804 man.hash.addOptional(comp.bin_file.options.version);
src/link.zig-1
......@@ -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+12-12
......@@ -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-2
......@@ -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,