compiler: get the dynamic linker from the target

instead of passing it to Compilation separately and storing it
separately in the linker options.
This commit is contained in:
Andrew Kelley 2023-12-04 21:56:29 -07:00
parent ce94c28e53
commit 77420af9d0
4 changed files with 13 additions and 18 deletions

View File

@ -803,7 +803,6 @@ pub const InitOptions = struct {
main_mod: ?*Package.Module,
output_mode: std.builtin.OutputMode,
thread_pool: *ThreadPool,
dynamic_linker: ?[]const u8 = null,
sysroot: ?[]const u8 = null,
/// `null` means to not emit a binary file.
emit_bin: ?EmitLoc,
@ -1836,7 +1835,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
.root_name = root_name,
.module = module,
.target = options.target,
.dynamic_linker = options.dynamic_linker,
.sysroot = sysroot,
.output_mode = options.output_mode,
.link_mode = link_mode,
@ -2800,7 +2798,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
man.hash.addOptionalBytes(libc_installation.kernel32_lib_dir);
}
}
man.hash.addOptionalBytes(comp.bin_file.options.dynamic_linker);
man.hash.addOptionalBytes(target.dynamic_linker.get());
}
man.hash.addOptionalBytes(comp.bin_file.options.soname);
man.hash.addOptional(comp.bin_file.options.version);

View File

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

View File

@ -1574,7 +1574,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
}
} else {
if (!self.isStatic()) {
if (self.base.options.dynamic_linker) |path| {
if (self.base.options.target.dynamic_linker.get()) |path| {
try argv.append("-dynamic-linker");
try argv.append(path);
}
@ -2374,7 +2374,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
man.hash.addBytes(libc_installation.crt_dir.?);
}
if (have_dynamic_linker) {
man.hash.addOptionalBytes(self.base.options.dynamic_linker);
man.hash.addOptionalBytes(self.base.options.target.dynamic_linker.get());
}
}
man.hash.addOptionalBytes(self.base.options.soname);
@ -2687,7 +2687,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
}
if (have_dynamic_linker) {
if (self.base.options.dynamic_linker) |dynamic_linker| {
if (self.base.options.target.dynamic_linker.get()) |dynamic_linker| {
try argv.append("-dynamic-linker");
try argv.append(dynamic_linker);
}
@ -3503,7 +3503,7 @@ fn initSyntheticSections(self: *Elf) !void {
// a segfault in the dynamic linker trying to load a binary that is static
// and doesn't contain .dynamic section.
if (self.isStatic() and !self.base.options.pie) break :blk false;
break :blk self.base.options.dynamic_linker != null;
break :blk self.base.options.target.dynamic_linker.get() != null;
};
if (needs_interp) {
self.interp_section_index = try self.addSection(.{
@ -4244,7 +4244,7 @@ fn updateSectionSizes(self: *Elf) !void {
}
if (self.interp_section_index) |index| {
self.shdrs.items[index].sh_size = self.base.options.dynamic_linker.?.len + 1;
self.shdrs.items[index].sh_size = self.base.options.target.dynamic_linker.get().?.len + 1;
}
if (self.hash_section_index) |index| {
@ -4938,14 +4938,14 @@ fn writeSyntheticSections(self: *Elf) !void {
const gpa = self.base.allocator;
if (self.interp_section_index) |shndx| {
var buffer: [256]u8 = undefined;
const interp = self.base.options.target.dynamic_linker.get().?;
@memcpy(buffer[0..interp.len], interp);
buffer[interp.len] = 0;
const contents = buffer[0 .. interp.len + 1];
const shdr = self.shdrs.items[shndx];
const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
var buffer = try gpa.alloc(u8, sh_size);
defer gpa.free(buffer);
const dylinker = self.base.options.dynamic_linker.?;
@memcpy(buffer[0..dylinker.len], dylinker);
buffer[dylinker.len] = 0;
try self.base.file.?.pwriteAll(buffer, shdr.sh_offset);
assert(shdr.sh_size == contents.len);
try self.base.file.?.pwriteAll(contents, shdr.sh_offset);
}
if (self.hash_section_index) |shndx| {

View File

@ -3456,7 +3456,6 @@ fn buildOutputType(
.target = target,
.is_native_os = target_query.isNativeOs(),
.is_native_abi = target_query.isNativeAbi(),
.dynamic_linker = target.dynamic_linker.get(),
.sysroot = sysroot,
.output_mode = output_mode,
.main_mod = main_mod,
@ -5287,7 +5286,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
.target = target,
.is_native_os = target_query.isNativeOs(),
.is_native_abi = target_query.isNativeAbi(),
.dynamic_linker = target.dynamic_linker.get(),
.output_mode = .Exe,
.main_mod = &main_mod,
.emit_bin = emit_bin,