ELF linker code crashes instead of reporting malformed archive #18205
Closed
Description
Zig Version
0.12.0-dev.1784+c357b902b
Steps to Reproduce and Observed Behavior
$ wget https://andrewkelley.me/temp/bad_libcompiler_rt.a
$ stage4/bin/zig build-obj ../test/standalone/hello_world/hello.zig -fno-llvm -fno-lld
$ stage4/bin/zig build-obj -fno-llvm -fno-lld hello.o bad_libcompiler_rt.a
thread 2810416 panic: index out of bounds: index 6148914691236517204, len 1126320
/home/andy/Downloads/zig/src/link/Elf/Object.zig:816:39: 0x6cf8334 in shdrContents (zig)
return self.data[shdr.sh_offset..][0..shdr.sh_size];
^
/home/andy/Downloads/zig/src/link/Elf/Object.zig:72:55: 0x773ba68 in parse (zig)
try self.strtab.appendSlice(gpa, self.shdrContents(self.header.?.e_shstrndx));
^
/home/andy/Downloads/zig/src/link/Elf.zig:1845:25: 0x727c8ff in parseArchive (zig)
try object.parse(self);
^
/home/andy/Downloads/zig/src/link/Elf.zig:1789:30: 0x6d54e12 in parseLibrary (zig)
try self.parseArchive(lib.path, must_link, ctx);
^
/home/andy/Downloads/zig/src/link/Elf.zig:1780:30: 0x6d544f1 in parsePositional (zig)
try self.parseLibrary(.{ .path = path }, must_link, ctx);
^
/home/andy/Downloads/zig/src/link/Elf.zig:1436:29: 0x6d537f5 in flushObject (zig)
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch |err|
^
/home/andy/Downloads/zig/src/link/Elf.zig:979:49: 0x68e6f89 in flushModule (zig)
if (self.isObject()) return self.flushObject(comp, module_obj_path);
^
/home/andy/Downloads/zig/src/link/Elf.zig:932:25: 0x65cbbfe in flush (zig)
try self.flushModule(comp, prog_node);
^
/home/andy/Downloads/zig/src/link.zig:823:68: 0x62992e3 in flush (zig)
.elf => return @fieldParentPtr(Elf, "base", base).flush(comp, prog_node),
^
/home/andy/Downloads/zig/src/Compilation.zig:2657:24: 0x6298b45 in flush (zig)
comp.bin_file.flush(comp, prog_node) catch |err| switch (err) {
^
/home/andy/Downloads/zig/src/Compilation.zig:2614:23: 0x62dcd3e in update (zig)
try comp.flush(main_progress_node);
^
/home/andy/Downloads/zig/src/main.zig:4283:24: 0x630c692 in updateModule (zig)
try comp.update(main_progress_node);
^
/home/andy/Downloads/zig/src/main.zig:3684:17: 0x632f25a in buildOutputType (zig)
updateModule(comp) catch |err| switch (err) {
^
/home/andy/Downloads/zig/src/main.zig:282:31: 0x613823f in mainArgs (zig)
return buildOutputType(gpa, arena, args, .{ .build = .Obj });
^
/home/andy/Downloads/zig/src/main.zig:222:20: 0x6135315 in main (zig)
return mainArgs(gpa, arena, args);
^
/home/andy/Downloads/zig/lib/std/start.zig:585:37: 0x6134d96 in main (zig)
const result = root.main() catch |err| {
^
???:?:?: 0x7f15c7fe7acd in ??? (libc.so.6)
Unwind information for `libc.so.6:0x7f15c7fe7acd` was not available, trace may be incomplete
Aborted (core dumped)
That's if you're lucky! A release build of the compiler has much more misleading outcomes.
Expected Behavior
It should report an error that the archive is malformed. If, for example, I run nm
on that file:
$ nm -a bad_libcompiler_rt.a
nm: compiler_rt.o: file format not recognized
This is much more helpful when diagnosing problems, especially when working on the compiler!
In general, the linker should not assert that input files are well-formed.