Skip to content

Conversation

@castholm
Copy link
Contributor

@castholm castholm commented Nov 5, 2025

Closes #25129

Brought to my attention by a post on Ziggit. The self-hosted ELF linker currently initializes all archive header fields to all spaces without any digits, which ld.lld doesn't like.

Tested by compiling

//! By convention, root.zig is the root source file when making a library. If
//! you are making an executable, the convention is to delete this file and
//! start with main.zig instead.
const std = @import("std");
const testing = std.testing;

export fn add_one(number: i64) i64 {
    return number + 1;
}

test "basic add functionality" {
    try testing.expect(add_one(3) == 4);
}

(from #25129 (comment)) using zig build-lib root.zig -target x86_64-linux-gnu -fno-llvm. Before/after:

 Offset(h) 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
 
 00000000  21 3c 61 72 63 68 3e 0a 2f 53 59 4d 36 34 2f 20  !<arch>./SYM64/ 
-00000010  20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
+00000010  20 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20          0       
-00000020  20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
+00000020  20 20 20 20 30 20 20 20 20 20 30 20 20 20 20 20      0     0     
-00000030  20 20 20 20 20 20 20 20 32 34 20 20 20 20 20 20          24      
+00000030  30 20 20 20 20 20 20 20 32 34 20 20 20 20 20 20  0       24      
 00000040  20 20 60 0a 00 00 00 00 00 00 00 01 00 00 00 00    `.............
 00000050  00 00 00 5c 61 64 64 5f 6f 6e 65 00 72 6f 6f 74  ...\add_one.root
-00000060  2e 6f 2f 20 20 20 20 20 20 20 20 20 20 20 20 20  .o/             
+00000060  2e 6f 2f 20 20 20 20 20 20 20 20 20 30 20 20 20  .o/         0   
-00000070  20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
+00000070  20 20 20 20 20 20 20 20 30 20 20 20 20 20 30 20          0     0 
-00000080  20 20 20 20 20 20 20 20 20 20 20 20 38 38 34 33              8843
+00000080  20 20 20 20 30 20 20 20 20 20 20 20 37 36 38 30      0       7680
-00000090  31 32 30 20 20 20 60 0a 7f 45 4c 46 02 01 01 00  120   `..ELF....
+00000090  35 32 38 20 20 20 60 0a 7f 45 4c 46 02 01 01 00  528   `..ELF....

Side note: There are three different archive definitions in the code base, std.elf.ar_hdr, link.MachO.Archive.ar_hdr and link.Wasm.Archive.Header, which each do things in subtly different ways. Maybe it would be useful to consolidate them into std.ar or something like that?

ld.lld fails with "truncated or malformed archive" errors when reading
archive header field values that are not valid numbers.
@squeek502 squeek502 merged commit 43eb9b5 into ziglang:master Nov 8, 2025
9 checks passed
@squeek502
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zig build-lib sometimes creates truncated or malformed archives

2 participants