Repository navigation
Open
Labels
bugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone
Description
Zig Version
0.10.0-dev.1855+a315d51c0
Steps to Reproduce
TLDR: zig build-obj file.zig -ofmt=elf panics with attempt to use null value
% zig version
0.10.0-dev.1855+a315d51c0
# build-obj without -ofmt=elf works properly
% zig build-obj empty.zig
% file empty.o
empty.o: Mach-O 64-bit object x86_64
% zig build-obj empty.zig -ofmt=elf
thread 635830 panic: attempt to use null value
/Users/mike/scratch/zig/src/link/Elf.zig:2802:69: 0x10abf26df in link.Elf.writeAllGlobalSymbols (zig1)
const syms_sect = &self.sections.items[self.symtab_section_index.?];
^
/Users/mike/scratch/zig/src/link/Elf.zig:1009:35: 0x10abc1374 in link.Elf.flushModule (zig1)
try self.writeAllGlobalSymbols();
^
/Users/mike/scratch/zig/src/link/Elf.zig:946:46: 0x10abad09c in link.Elf.flush (zig1)
.Exe, .Obj => return self.flushModule(comp),
^
/Users/mike/scratch/zig/src/link.zig:601:68: 0x10abaa222 in link.File.flush (zig1)
.elf => return @fieldParentPtr(Elf, "base", base).flush(comp),
^
/Users/mike/scratch/zig/src/Compilation.zig:2192:28: 0x10ab79b40 in Compilation.flush (zig1)
try comp.bin_file.flush(comp); // This is needed before reading the error flags.
^
/Users/mike/scratch/zig/src/Compilation.zig:2174:23: 0x10ab6f9eb in Compilation.update (zig1)
try comp.flush();
^
/Users/mike/scratch/zig/src/main.zig:3101:20: 0x10ab043f0 in main.updateModule (zig1)
try comp.update();
^
/Users/mike/scratch/zig/src/main.zig:2788:17: 0x10aa1cecd in main.buildOutputType (zig1)
updateModule(gpa, comp, hook) catch |err| switch (err) {
^
/Users/mike/scratch/zig/src/main.zig:214:31: 0x10a9e40c7 in main.mainArgs (zig1)
return buildOutputType(gpa, arena, args, .{ .build = .Obj });
^
/Users/mike/scratch/zig/src/stage1.zig:48:24: 0x10a9e392a in main (zig1)
stage2.mainArgs(gpa, arena, args) catch unreachable;
^
???:?:?: 0x120d3451d in ??? (???)
???:?:?: 0x0 in ??? (???)
[1] 77062 abort zig build-obj empty.zig -ofmt=elfI built this version of zig from master on my machine just now.
If I run on 0.9.1, I get a segfault:
% zig version
0.9.1 # installed via homebrew
% touch empty.zig
% zig build-obj empty.zig
# I can build Mach-O objects ok
% file empty.o
empty.o: Mach-O 64-bit object x86_64
# but not ELF
% zig build-obj empty.zig -ofmt=elf
[1] 77126 segmentation fault zig build-obj empty.zig -ofmt=elf
(lldb) target create /usr/local/bin/zig
Current executable set to '/usr/local/bin/zig' (x86_64).
(lldb) settings set -- target.run-args "build-obj" "empty.zig" "-ofmt=elf"
(lldb) r
Process 77146 launched: '/usr/local/bin/zig' (x86_64)
Process 77146 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x18)
frame #0: 0x00000001000d1bbc zig`link.Elf.flushModule + 156
zig`link.Elf.flushModule:
-> 0x1000d1bbc <+156>: addq 0x18(%rdx,%rax), %rsi
0x1000d1bc1 <+161>: movq %rsi, -0x40(%rbp)
0x1000d1bc5 <+165>: movq 0x488(%r10), %r11
0x1000d1bcc <+172>: movq 0x8(%rbp), %rbx
Target 0: (zig) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x18)
* frame #0: 0x00000001000d1bbc zig`link.Elf.flushModule + 156
frame #1: 0x0000000100095376 zig`Compilation.update + 10566
frame #2: 0x000000010005997f zig`main.updateModule + 31
frame #3: 0x0000000100030ad4 zig`main.buildOutputType + 127860
frame #4: 0x00000001000066d8 zig`main + 4536
frame #5: 0x0000000109ccd51e dyld`start + 462
Host platform: macOS Monterey 12.3
Expected Behavior
An ELF object file is created.
Actual Behavior
zig panics or segfaults.
Metadata
Metadata
Assignees
Labels
bugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.Tokenization, parsing, AstGen, Sema, and Liveness.
Activity
kubkon commentedon Apr 22, 2022
Hi @mikegedelman. Thanks for the report. Let me see if I understand your issue correctly. While on macOS as your host, you'd like to output an ELF relocatable object file, correct? I actually never thought of using
-ofmt=elfoption to achieve this - you could try with-target x86_64-linuxinsteadwhich should work as you'd expect. Having said that, a segfault/runtime trap should not happen either for
-ofmt=option.mikegedelman commentedon Apr 22, 2022
Hey @kubkon, thanks for the response. So I’m this case I’m wanting to build an ELF object file for a freestanding target. I was experimenting with porting parts of my hobby osdev kernel to Zig. So I don’t think x86_64-linux would work? I could target none (or freestanding, can’t remember), but since the host is MacOS I’d need to explicitly specify ELF still, right?
(sorry if you got multiple notifications, accidentally commented on an old account)