link: Improve handling of --build-id when using LLD.

This commit is contained in:
Alex Rønne Petersen 2025-04-12 20:03:10 +02:00
parent d5ac3be608
commit 6eabdc8972
3 changed files with 27 additions and 20 deletions

View File

@ -1763,6 +1763,7 @@ fn linkWithLLD(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node:
man.hash.addOptionalBytes(entry_name);
man.hash.add(coff.base.stack_size);
man.hash.add(coff.image_base);
man.hash.add(coff.base.build_id);
{
// TODO remove this, libraries must instead be resolved by the frontend.
for (coff.lib_directories) |lib_directory| man.hash.addOptionalBytes(lib_directory.path);
@ -1895,6 +1896,12 @@ fn linkWithLLD(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node:
}
try argv.append(try allocPrint(arena, "-BASE:{d}", .{coff.image_base}));
switch (coff.base.build_id) {
.none => try argv.append("-BUILD-ID:NO"),
.fast => try argv.append("-BUILD-ID"),
.uuid, .sha1, .md5, .hexstring => {},
}
if (target.cpu.arch == .x86) {
try argv.append("-MACHINE:X86");
} else if (target.cpu.arch == .x86_64) {

View File

@ -1596,8 +1596,8 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
man.hash.addListOfBytes(self.rpath_table.keys());
if (output_mode == .Exe) {
man.hash.add(self.base.stack_size);
man.hash.add(self.base.build_id);
}
man.hash.add(self.base.build_id);
man.hash.addListOfBytes(self.symbol_wrap_set.keys());
man.hash.add(comp.skip_linker_dependencies);
man.hash.add(self.z_nodelete);
@ -1753,20 +1753,14 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
});
}
if (is_exe_or_dyn_lib) {
switch (self.base.build_id) {
.none => {},
.fast, .uuid, .sha1, .md5 => {
try argv.append(try std.fmt.allocPrint(arena, "--build-id={s}", .{
@tagName(self.base.build_id),
}));
},
.hexstring => |hs| {
try argv.append(try std.fmt.allocPrint(arena, "--build-id=0x{s}", .{
std.fmt.fmtSliceHexLower(hs.toSlice()),
}));
},
}
switch (self.base.build_id) {
.none => try argv.append("--build-id=none"),
.fast, .uuid, .sha1, .md5 => try argv.append(try std.fmt.allocPrint(arena, "--build-id={s}", .{
@tagName(self.base.build_id),
})),
.hexstring => |hs| try argv.append(try std.fmt.allocPrint(arena, "--build-id=0x{s}", .{
std.fmt.fmtSliceHexLower(hs.toSlice()),
})),
}
try argv.append(try std.fmt.allocPrint(arena, "--image-base={d}", .{self.image_base}));

View File

@ -4078,6 +4078,17 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node:
try std.fmt.allocPrint(arena, "stack-size={d}", .{wasm.base.stack_size}),
});
switch (wasm.base.build_id) {
.none => try argv.append("--build-id=none"),
.fast, .uuid, .sha1 => try argv.append(try std.fmt.allocPrint(arena, "--build-id={s}", .{
@tagName(wasm.base.build_id),
})),
.hexstring => |hs| try argv.append(try std.fmt.allocPrint(arena, "--build-id=0x{s}", .{
std.fmt.fmtSliceHexLower(hs.toSlice()),
})),
.md5 => {},
}
if (wasm.import_symbols) {
try argv.append("--allow-undefined");
}
@ -4089,11 +4100,6 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node:
try argv.append("--pie");
}
// XXX - TODO: add when wasm-ld supports --build-id.
// if (wasm.base.build_id) {
// try argv.append("--build-id=tree");
// }
try argv.appendSlice(&.{ "-o", full_out_path });
if (target.cpu.arch == .wasm64) {