aboutsummaryrefslogtreecommitdiff
path: root/src/link
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-05-16 20:00:47 -0700
committerAndrew Kelley <andrew@ziglang.org>2023-05-16 20:39:01 -0700
commit728ce2d7c18e23ca6c36d86f4ee1ea4ce3ac81e2 (patch)
treeb84ffae450a01a7fa2cf22d87176fb633125b359 /src/link
parentdf5085bde012773b974f58e8ee28ed90ff686468 (diff)
downloadzig-728ce2d7c18e23ca6c36d86f4ee1ea4ce3ac81e2.tar.gz
zig-728ce2d7c18e23ca6c36d86f4ee1ea4ce3ac81e2.zip
tweaks to --build-id
* build.zig: the result of b.option() can be assigned directly in many cases thanks to the return type being an optional * std.Build: make the build system aware of the std.Build.Step.Compile.BuildId type when used as an option. - remove extraneous newlines in error logs * simplify caching logic * simplify hexstring parsing tests and use a doc test * simplify hashing logic. don't use an optional when the `none` tag already provides this meaning. * CLI: fix incorrect linker arg parsing
Diffstat (limited to 'src/link')
-rw-r--r--src/link/Elf.zig21
-rw-r--r--src/link/Wasm.zig50
2 files changed, 38 insertions, 33 deletions
diff --git a/src/link/Elf.zig b/src/link/Elf.zig
index 289c687270..f90f4ebd46 100644
--- a/src/link/Elf.zig
+++ b/src/link/Elf.zig
@@ -1399,8 +1399,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
man.hash.add(self.base.options.each_lib_rpath);
if (self.base.options.output_mode == .Exe) {
man.hash.add(stack_size);
- if (self.base.options.build_id) |build_id|
- build_id.hash(&man.hash.hasher);
+ man.hash.add(self.base.options.build_id);
}
man.hash.addListOfBytes(self.base.options.symbol_wrap_set.keys());
man.hash.add(self.base.options.skip_linker_dependencies);
@@ -1543,12 +1542,18 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
try argv.append("-z");
try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}));
- if (self.base.options.build_id) |build_id| {
- const fmt_str = "--build-id={s}{s}";
- try argv.append(switch (build_id) {
- .hexstring => |str| try std.fmt.allocPrint(arena, fmt_str, .{ "0x", str }),
- .none, .fast, .uuid, .sha1, .md5 => try std.fmt.allocPrint(arena, fmt_str, .{ "", @tagName(build_id) }),
- });
+ switch (self.base.options.build_id) {
+ .none => {},
+ .fast, .uuid, .sha1, .md5 => {
+ try argv.append(try std.fmt.allocPrint(arena, "--build-id={s}", .{
+ @tagName(self.base.options.build_id),
+ }));
+ },
+ .hexstring => |hs| {
+ try argv.append(try std.fmt.allocPrint(arena, "--build-id=0x{s}", .{
+ std.fmt.fmtSliceHexLower(hs.toSlice()),
+ }));
+ },
}
}
diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig
index 9396377c73..cd9c44d656 100644
--- a/src/link/Wasm.zig
+++ b/src/link/Wasm.zig
@@ -3163,8 +3163,7 @@ fn linkWithZld(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) l
try man.addOptionalFile(compiler_rt_path);
man.hash.addOptionalBytes(options.entry);
man.hash.addOptional(options.stack_size_override);
- if (wasm.base.options.build_id) |build_id|
- build_id.hash(&man.hash.hasher);
+ man.hash.add(wasm.base.options.build_id);
man.hash.add(options.import_memory);
man.hash.add(options.import_table);
man.hash.add(options.export_table);
@@ -3798,27 +3797,29 @@ fn writeToFile(
if (!wasm.base.options.strip) {
// The build id must be computed on the main sections only,
// so we have to do it now, before the debug sections.
- if (wasm.base.options.build_id) |build_id| {
- switch (build_id) {
- .none => {},
- .fast => {
- var id: [16]u8 = undefined;
- std.crypto.hash.sha3.TurboShake128(null).hash(binary_bytes.items, &id, .{});
- var uuid: [36]u8 = undefined;
- _ = try std.fmt.bufPrint(&uuid, "{s}-{s}-{s}-{s}-{s}", .{
- std.fmt.fmtSliceHexLower(id[0..4]),
- std.fmt.fmtSliceHexLower(id[4..6]),
- std.fmt.fmtSliceHexLower(id[6..8]),
- std.fmt.fmtSliceHexLower(id[8..10]),
- std.fmt.fmtSliceHexLower(id[10..]),
- });
- try emitBuildIdSection(&binary_bytes, &uuid);
- },
- .hexstring => |str| {
- try emitBuildIdSection(&binary_bytes, str);
- },
- else => |mode| log.err("build-id '{s}' is not supported for WASM", .{@tagName(mode)}),
- }
+ switch (wasm.base.options.build_id) {
+ .none => {},
+ .fast => {
+ var id: [16]u8 = undefined;
+ std.crypto.hash.sha3.TurboShake128(null).hash(binary_bytes.items, &id, .{});
+ var uuid: [36]u8 = undefined;
+ _ = try std.fmt.bufPrint(&uuid, "{s}-{s}-{s}-{s}-{s}", .{
+ std.fmt.fmtSliceHexLower(id[0..4]),
+ std.fmt.fmtSliceHexLower(id[4..6]),
+ std.fmt.fmtSliceHexLower(id[6..8]),
+ std.fmt.fmtSliceHexLower(id[8..10]),
+ std.fmt.fmtSliceHexLower(id[10..]),
+ });
+ try emitBuildIdSection(&binary_bytes, &uuid);
+ },
+ .hexstring => |hs| {
+ var buffer: [32 * 2]u8 = undefined;
+ const str = std.fmt.bufPrint(&buffer, "{s}", .{
+ std.fmt.fmtSliceHexLower(hs.toSlice()),
+ }) catch unreachable;
+ try emitBuildIdSection(&binary_bytes, str);
+ },
+ else => |mode| log.err("build-id '{s}' is not supported for WASM", .{@tagName(mode)}),
}
// if (wasm.dwarf) |*dwarf| {
@@ -4211,8 +4212,7 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
try man.addOptionalFile(compiler_rt_path);
man.hash.addOptionalBytes(wasm.base.options.entry);
man.hash.addOptional(wasm.base.options.stack_size_override);
- if (wasm.base.options.build_id) |build_id|
- build_id.hash(&man.hash.hasher);
+ man.hash.add(wasm.base.options.build_id);
man.hash.add(wasm.base.options.import_memory);
man.hash.add(wasm.base.options.import_table);
man.hash.add(wasm.base.options.export_table);