authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-10-19 11:04:01+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-10-19 18:30:35+01:00
logf7626b95269bc3baad2c46e4d3b7acc16221c424
tree8b7268c178c4e4313fea5f6bd3e64000b179b1e3
parentf67c5e92fc010b4fd43638d9ff9c996de014155d
signature Commit is signed but in an unrecognized format.

Elf2: don't emulate a ZCU input object when building a static library

In this case, there is a *literal* ZCU input object in the static library itself. This was causing a safety check to occasionally trip in safe compiler builds. First noticed when debugging an unrelated issue: see https://codeberg.org/ziglang/zig/issues/37327#issuecomment-25643513.

1 files changed, 21 insertions(+), 18 deletions(-)

src/link/Elf2.zig+21-18
...@@ -8904,24 +8904,27 @@ fn prelinkInner(elf: *Elf) Error!void {...@@ -8904,24 +8904,27 @@ fn prelinkInner(elf: *Elf) Error!void {
8904 if (comp.zcu) |_| self_hosted_codegen: {8904 if (comp.zcu) |_| self_hosted_codegen: {
8905 if (comp.config.use_llvm) break :self_hosted_codegen;8905 if (comp.config.use_llvm) break :self_hosted_codegen;
89068906
8907 // We're using self-hosted codegen---add an input representing the Zig "object".8907 if (elf.archive == null) {
8908 try elf.inputs.ensureUnusedCapacity(gpa, 1);8908 // Self-hosted codegen is logically emitting a relocatable which is then immediately
8909 const zcu_name = try std.fmt.allocPrint(gpa, "{s}_zcu", .{comp.root_name});8909 // being linked into the output file, so add that fictional Zig object file input.
8910 defer gpa.free(zcu_name);8910 try elf.inputs.ensureUnusedCapacity(gpa, 1);
8911 const zcu_file_symbol = try elf.addLocalSymbol(.{8911 const zcu_name = try std.fmt.allocPrint(gpa, "{s}_zcu", .{comp.root_name});
8912 .node = .none,8912 defer gpa.free(zcu_name);
8913 .name = zcu_name,8913 const zcu_file_symbol = try elf.addLocalSymbol(.{
8914 .value = 0,8914 .node = .none,
8915 .size = 0,8915 .name = zcu_name,
8916 .type = .FILE,8916 .value = 0,
8917 .shndx = .ABS,8917 .size = 0,
8918 });8918 .type = .FILE,
8919 elf.inputs.addOneAssumeCapacity().* = .{8919 .shndx = .ABS,
8920 .path = elf.base.emit,8920 });
8921 .member = null,8921 elf.inputs.addOneAssumeCapacity().* = .{
8922 .extra = .{ .file_symbol = zcu_file_symbol },8922 .path = elf.base.emit,
8923 };8923 .member = null,
8924 elf.input_pending_index += 1;8924 .extra = .{ .file_symbol = zcu_file_symbol },
8925 };
8926 elf.input_pending_index += 1;
8927 }
89258928
8926 switch (elf.shndx.debug_addr) {8929 switch (elf.shndx.debug_addr) {
8927 .UNDEF => {},8930 .UNDEF => {},