ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.95k stars 2.55k forks source link

zig build-obj emits two object files #21538

Closed jmc-88 closed 1 month ago

jmc-88 commented 1 month ago

Zig Version

0.14.0-dev.1671+085cc54aa

Steps to Reproduce and Observed Behavior

As per issue title. I built a recent version from Git HEAD to see if the behavior was still present, but I first noticed this on Zig v0.13.0.

$ cat test.zig
export fn noop() void {}
$ zig build-obj -O ReleaseSmall test.zig
$ ls -lh
total 12K
-rw-rw-r-- 1 jmc jmc 864 Sep 28 15:02 test.o
-rw-rw-r-- 1 jmc jmc 752 Sep 28 15:02 test.o.o
-rw-rw-r-- 1 jmc jmc  25 Sep 28 13:17 test.zig

Notice the .o file as well as the .o.o file. The two object files are almost identical in content but not in size:

$ objdump -DSls test.o

test.o:     file format elf64-x86-64

Contents of section .text:
 0000 c3                                   .
Contents of section .eh_frame:
 0000 14000000 00000000 017a5200 01781001  .........zR..x..
 0010 1b0c0708 90010000 14000000 1c000000  ................
 0020 00000000 01000000 00000000 00000000  ................

Disassembly of section .text:

0000000000000000 <noop>:
noop():
   0:   c3                      ret

Disassembly of section .eh_frame:

0000000000000000 <.eh_frame>:
   0:   14 00                   adc    $0x0,%al
   2:   00 00                   add    %al,(%rax)
   4:   00 00                   add    %al,(%rax)
   6:   00 00                   add    %al,(%rax)
   8:   01 7a 52                add    %edi,0x52(%rdx)
   b:   00 01                   add    %al,(%rcx)
   d:   78 10                   js     1f <.eh_frame+0x1f>
   f:   01 1b                   add    %ebx,(%rbx)
  11:   0c 07                   or     $0x7,%al
  13:   08 90 01 00 00 14       or     %dl,0x14000001(%rax)
  19:   00 00                   add    %al,(%rax)
  1b:   00 1c 00                add    %bl,(%rax,%rax,1)
  1e:   00 00                   add    %al,(%rax)
  20:   00 00                   add    %al,(%rax)
  22:   00 00                   add    %al,(%rax)
  24:   01 00                   add    %eax,(%rax)
    ...
$ objdump -DSls test.o.o

test.o.o:     file format elf64-x86-64

Contents of section .text:
 0000 c3                                   .
Contents of section .eh_frame:
 0000 14000000 00000000 017a5200 01781001  .........zR..x..
 0010 1b0c0708 90010000 14000000 1c000000  ................
 0020 00000000 01000000 00000000 00000000  ................

Disassembly of section .text:

0000000000000000 <noop>:
noop():
   0:   c3                      ret

Disassembly of section .eh_frame:

0000000000000000 <.eh_frame>:
   0:   14 00                   adc    $0x0,%al
   2:   00 00                   add    %al,(%rax)
   4:   00 00                   add    %al,(%rax)
   6:   00 00                   add    %al,(%rax)
   8:   01 7a 52                add    %edi,0x52(%rdx)
   b:   00 01                   add    %al,(%rcx)
   d:   78 10                   js     1f <.eh_frame+0x1f>
   f:   01 1b                   add    %ebx,(%rbx)
  11:   0c 07                   or     $0x7,%al
  13:   08 90 01 00 00 14       or     %dl,0x14000001(%rax)
  19:   00 00                   add    %al,(%rax)
  1b:   00 1c 00                add    %bl,(%rax,%rax,1)
  1e:   00 00                   add    %al,(%rax)
  20:   00 00                   add    %al,(%rax)
  22:   00 00                   add    %al,(%rax)
  24:   01 00                   add    %eax,(%rax)
    ...

Expected Behavior

Only one object file is emitted.

Vexu commented 1 month ago

Duplicate of #13179