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 {...@@ -803,7 +803,6 @@ pub const InitOptions = struct {
803 main_mod: ?*Package.Module,803 main_mod: ?*Package.Module,
804 output_mode: std.builtin.OutputMode,804 output_mode: std.builtin.OutputMode,
805 thread_pool: *ThreadPool,805 thread_pool: *ThreadPool,
806 dynamic_linker: ?[]const u8 = null,
807 sysroot: ?[]const u8 = null,806 sysroot: ?[]const u8 = null,
808 /// `null` means to not emit a binary file.807 /// `null` means to not emit a binary file.
809 emit_bin: ?EmitLoc,808 emit_bin: ?EmitLoc,
...@@ -1836,7 +1835,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1836,7 +1835,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1836 .root_name = root_name,1835 .root_name = root_name,
1837 .module = module,1836 .module = module,
1838 .target = options.target,1837 .target = options.target,
1839 .dynamic_linker = options.dynamic_linker,
1840 .sysroot = sysroot,1838 .sysroot = sysroot,
1841 .output_mode = options.output_mode,1839 .output_mode = options.output_mode,
1842 .link_mode = link_mode,1840 .link_mode = link_mode,
...@@ -2800,7 +2798,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes...@@ -2800,7 +2798,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
2800 man.hash.addOptionalBytes(libc_installation.kernel32_lib_dir);2798 man.hash.addOptionalBytes(libc_installation.kernel32_lib_dir);
2801 }2799 }
2802 }2800 }
2803 man.hash.addOptionalBytes(comp.bin_file.options.dynamic_linker);2801 man.hash.addOptionalBytes(target.dynamic_linker.get());
2804 }2802 }
2805 man.hash.addOptionalBytes(comp.bin_file.options.soname);2803 man.hash.addOptionalBytes(comp.bin_file.options.soname);
2806 man.hash.addOptional(comp.bin_file.options.version);2804 man.hash.addOptional(comp.bin_file.options.version);
src/link.zig-1
...@@ -103,7 +103,6 @@ pub const Options = struct {...@@ -103,7 +103,6 @@ pub const Options = struct {
103 root_name: [:0]const u8,103 root_name: [:0]const u8,
104 /// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`.104 /// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`.
105 module: ?*Module,105 module: ?*Module,
106 dynamic_linker: ?[]const u8,
107 /// The root path for the dynamic linker and system libraries (as well as frameworks on Darwin)106 /// The root path for the dynamic linker and system libraries (as well as frameworks on Darwin)
108 sysroot: ?[]const u8,107 sysroot: ?[]const u8,
109 /// Used for calculating how much space to reserve for symbols in case the binary file108 /// 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 {...@@ -1574,7 +1574,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
1574 }1574 }
1575 } else {1575 } else {
1576 if (!self.isStatic()) {1576 if (!self.isStatic()) {
1577 if (self.base.options.dynamic_linker) |path| {1577 if (self.base.options.target.dynamic_linker.get()) |path| {
1578 try argv.append("-dynamic-linker");1578 try argv.append("-dynamic-linker");
1579 try argv.append(path);1579 try argv.append(path);
1580 }1580 }
...@@ -2374,7 +2374,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v...@@ -2374,7 +2374,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
2374 man.hash.addBytes(libc_installation.crt_dir.?);2374 man.hash.addBytes(libc_installation.crt_dir.?);
2375 }2375 }
2376 if (have_dynamic_linker) {2376 if (have_dynamic_linker) {
2377 man.hash.addOptionalBytes(self.base.options.dynamic_linker);2377 man.hash.addOptionalBytes(self.base.options.target.dynamic_linker.get());
2378 }2378 }
2379 }2379 }
2380 man.hash.addOptionalBytes(self.base.options.soname);2380 man.hash.addOptionalBytes(self.base.options.soname);
...@@ -2687,7 +2687,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v...@@ -2687,7 +2687,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
2687 }2687 }
26882688
2689 if (have_dynamic_linker) {2689 if (have_dynamic_linker) {
2690 if (self.base.options.dynamic_linker) |dynamic_linker| {2690 if (self.base.options.target.dynamic_linker.get()) |dynamic_linker| {
2691 try argv.append("-dynamic-linker");2691 try argv.append("-dynamic-linker");
2692 try argv.append(dynamic_linker);2692 try argv.append(dynamic_linker);
2693 }2693 }
...@@ -3503,7 +3503,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3503,7 +3503,7 @@ fn initSyntheticSections(self: *Elf) !void {
3503 // a segfault in the dynamic linker trying to load a binary that is static3503 // a segfault in the dynamic linker trying to load a binary that is static
3504 // and doesn't contain .dynamic section.3504 // and doesn't contain .dynamic section.
3505 if (self.isStatic() and !self.base.options.pie) break :blk false;3505 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;
3507 };3507 };
3508 if (needs_interp) {3508 if (needs_interp) {
3509 self.interp_section_index = try self.addSection(.{3509 self.interp_section_index = try self.addSection(.{
...@@ -4244,7 +4244,7 @@ fn updateSectionSizes(self: *Elf) !void {...@@ -4244,7 +4244,7 @@ fn updateSectionSizes(self: *Elf) !void {
4244 }4244 }
42454245
4246 if (self.interp_section_index) |index| {4246 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;
4248 }4248 }
42494249
4250 if (self.hash_section_index) |index| {4250 if (self.hash_section_index) |index| {
...@@ -4938,14 +4938,14 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4938,14 +4938,14 @@ fn writeSyntheticSections(self: *Elf) !void {
4938 const gpa = self.base.allocator;4938 const gpa = self.base.allocator;
49394939
4940 if (self.interp_section_index) |shndx| {4940 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];
4941 const shdr = self.shdrs.items[shndx];4946 const shdr = self.shdrs.items[shndx];
4942 const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;4947 assert(shdr.sh_size == contents.len);
4943 var buffer = try gpa.alloc(u8, sh_size);4948 try self.base.file.?.pwriteAll(contents, shdr.sh_offset);
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);
4949 }4949 }
49504950
4951 if (self.hash_section_index) |shndx| {4951 if (self.hash_section_index) |shndx| {
src/main.zig-2
...@@ -3456,7 +3456,6 @@ fn buildOutputType(...@@ -3456,7 +3456,6 @@ fn buildOutputType(
3456 .target = target,3456 .target = target,
3457 .is_native_os = target_query.isNativeOs(),3457 .is_native_os = target_query.isNativeOs(),
3458 .is_native_abi = target_query.isNativeAbi(),3458 .is_native_abi = target_query.isNativeAbi(),
3459 .dynamic_linker = target.dynamic_linker.get(),
3460 .sysroot = sysroot,3459 .sysroot = sysroot,
3461 .output_mode = output_mode,3460 .output_mode = output_mode,
3462 .main_mod = main_mod,3461 .main_mod = main_mod,
...@@ -5287,7 +5286,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -5287,7 +5286,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
5287 .target = target,5286 .target = target,
5288 .is_native_os = target_query.isNativeOs(),5287 .is_native_os = target_query.isNativeOs(),
5289 .is_native_abi = target_query.isNativeAbi(),5288 .is_native_abi = target_query.isNativeAbi(),
5290 .dynamic_linker = target.dynamic_linker.get(),
5291 .output_mode = .Exe,5289 .output_mode = .Exe,
5292 .main_mod = &main_mod,5290 .main_mod = &main_mod,
5293 .emit_bin = emit_bin,5291 .emit_bin = emit_bin,