From ea475556756d5afbaf88d472e9b223ab2a1ca1f4 Mon Sep 17 00:00:00 2001 From: Matthew Lugg Date: Mon, 19 Oct 2026 11:04:01 +0100 Subject: [PATCH] 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. Resolves: https://codeberg.org/ziglang/zig/issues/37327 --- src/link/Elf2.zig | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/link/Elf2.zig b/src/link/Elf2.zig index f5fb9ef1a7f97ab78365c8a072ca7bf0c45e3a5d..3e5e0a7cc49cbbfb55382535f9b442f7f86f6aaf 100644 --- a/src/link/Elf2.zig +++ b/src/link/Elf2.zig @@ -8421,24 +8421,27 @@ fn prelinkInner(elf: *Elf) Error!void { if (comp.zcu) |_| self_hosted_codegen: { if (comp.config.use_llvm) break :self_hosted_codegen; - // We're using self-hosted codegen---add an input representing the Zig "object". - try elf.inputs.ensureUnusedCapacity(gpa, 1); - const zcu_name = try std.fmt.allocPrint(gpa, "{s}_zcu", .{comp.root_name}); - defer gpa.free(zcu_name); - const zcu_file_symbol = try elf.addLocalSymbol(.{ - .node = .none, - .name = zcu_name, - .value = 0, - .size = 0, - .type = .FILE, - .shndx = .ABS, - }); - elf.inputs.addOneAssumeCapacity().* = .{ - .path = elf.base.emit, - .member = null, - .extra = .{ .file_symbol = zcu_file_symbol }, - }; - elf.input_pending_index += 1; + if (elf.archive == null) { + // Self-hosted codegen is logically emitting a relocatable which is then immediately + // being linked into the output file, so add that fictional Zig object file input. + try elf.inputs.ensureUnusedCapacity(gpa, 1); + const zcu_name = try std.fmt.allocPrint(gpa, "{s}_zcu", .{comp.root_name}); + defer gpa.free(zcu_name); + const zcu_file_symbol = try elf.addLocalSymbol(.{ + .node = .none, + .name = zcu_name, + .value = 0, + .size = 0, + .type = .FILE, + .shndx = .ABS, + }); + elf.inputs.addOneAssumeCapacity().* = .{ + .path = elf.base.emit, + .member = null, + .extra = .{ .file_symbol = zcu_file_symbol }, + }; + elf.input_pending_index += 1; + } switch (elf.shndx.debug_addr) { .UNDEF => {}, -- 2.54.0