Skip to content

[zig cc/elf] does not pass "-r" to ld.lld #11683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
motiejus opened this issue May 20, 2022 · 4 comments · Fixed by #15201
Closed

[zig cc/elf] does not pass "-r" to ld.lld #11683

motiejus opened this issue May 20, 2022 · 4 comments · Fixed by #15201
Labels
bug Observed behavior contradicts documented or intended behavior zig cc Zig as a drop-in C compiler feature
Milestone

Comments

@motiejus
Copy link
Contributor

motiejus commented May 20, 2022

Zig Version

0.10.0-dev.2252+a4369918b

Steps to Reproduce

Compile an object file and pass -r to it:

zig-cc -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1086133851=/tmp/go-build -gno-record-gcc-switches -nostdlib -r -o /tmp/elf-zigcc/ldr.syso /tmp/elf-zigcc/x0.o /tmp/elf-zigcc/x1.o

Expected Behavior

The underlying ld.lld command also has -r

Actual Behavior

The linker line is missing -r:

ld.lld -error-limit=0 -O0 -z stack-size=16777216 --eh-frame-hdr -m elf_x86_64 -static -o /home/motiejus/.cache/zig/o/fecbc069c368f18f63f7f97d81ed0279/ldr.syso -L /usr/local/lib64 -L /usr/local/lib -L /usr/lib/x86_64-linux-gnu -L /lib64 -L /lib -L /usr/lib64 -L /usr/lib -L /lib/x86_64-linux-gnu /tmp/elf-zigcc/x0.o /tmp/elf-zigcc/x1.o /home/motiejus/.cache/zig/o/a400e3bbd0b20e8df86f8cb4900e42e6/libc.a /home/motiejus/.cache/zig/o/af8550d2991476a4c76b9fa2e199da85/libcompiler_rt.a --as-needed

This causes a Golang test cmd/link/elf_test.go:TestMinusRSymsWithSameName to fail when using zig cc as the compiler.

To reproduce: this will build the Go sdk with Zig and attempt to run it's cmd/link tests:

git clone https://github.com/golang/go
cd go/src
echo -e '#!/bin/sh\nexec zig cc -Wl,--no-gc-sections "$@"' > zig-cc; chmod a+x zig-cc
CC=$PWD/zig-cc sh -c "./make.bash && ./run.bash -run cmd/link"
<...>
##### Testing packages.
--- FAIL: TestMinusRSymsWithSameName (1.02s)
    elf_test.go:166: /home/motiejus/code/go/src/zig-cc [-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build777708704=/tmp/go-build -gno-record-gcc-switches] -c -o /tmp/TestMinusRSymsWithSameName4069257377/001/x0.o /tmp/TestMinusRSymsWithSameName4069257377/001/x0.c
    elf_test.go:166: /home/motiejus/code/go/src/zig-cc [-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build777708704=/tmp/go-build -gno-record-gcc-switches] -c -o /tmp/TestMinusRSymsWithSameName4069257377/001/x1.o /tmp/TestMinusRSymsWithSameName4069257377/001/x1.c
    elf_test.go:174: /home/motiejus/code/go/src/zig-cc [-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build777708704=/tmp/go-build -gno-record-gcc-switches] -nostdlib -r -o /tmp/TestMinusRSymsWithSameName4069257377/001/ldr.syso [/tmp/TestMinusRSymsWithSameName4069257377/001/x0.o /tmp/TestMinusRSymsWithSameName4069257377/001/x1.o]
    elf_test.go:194: /home/motiejus/code/go/bin/go build
    elf_test.go:199: # elf_test
        loadelf: $WORK/b001/_pkg_.a(ldr.syso): elf but not elf relocatable object
    elf_test.go:200: exit status 2

If we apply #11678, this test passes.

@motiejus motiejus added the bug Observed behavior contradicts documented or intended behavior label May 20, 2022
@motiejus
Copy link
Contributor Author

As mentioned by @andrewrk in #11678, #7094 and #7547 are related to this.

@andrewrk andrewrk added the zig cc Zig as a drop-in C compiler feature label Jun 2, 2022
@andrewrk andrewrk added this to the 0.10.0 milestone Jun 2, 2022
@andrewrk andrewrk modified the milestones: 0.10.0, 0.10.1 Oct 12, 2022
@andrewrk andrewrk modified the milestones: 0.10.1, 0.11.0 Jan 10, 2023
@motiejus
Copy link
Contributor Author

motiejus commented Apr 7, 2023

I have been working through the Go unit tests with Zig as the C compiler. The test below is the last Go unit test that fails on Go's master1.

Steps to reproduce

$HOME/zcc

#!/bin/sh
export ZIG_GLOBAL_CACHE_DIR=/home/motiejus/.cache/zig
export ZIG_LOCAL_CACHE_DIR=/home/motiejus/.cache/zig
exec /code/zig/build/stage3/bin/zig cc "$@"

run

$ git clone https://github.com/golang/go; cd go/src
$ ./make.bash
< wait for Go to build >
$ cd cmd/link
$ CC=$HOME/zcc ../../../bin/go test -run TestMinusRSymsWithSameName$                                                                          
--- FAIL: TestMinusRSymsWithSameName (1.02s)                                                                                                                                       
    elf_test.go:164: /home/motiejus/zcc [-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1568376719=/tmp/go-build -gno-record-gcc-switches] -c -o /tmp/TestMinusRSymsWithSameName1171769842/001/x0.o /tmp/TestMinusRSymsWithSameName1171769842/001/x0.c                          
    elf_test.go:164: /home/motiejus/zcc [-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build15683
76719=/tmp/go-build -gno-record-gcc-switches] -c -o /tmp/TestMinusRSymsWithSameName1171769842/001/x1.o /tmp/TestMinusRSymsWithSameName1171769842/001/x1.c                          
    elf_test.go:172: /home/motiejus/zcc [-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1568376719=/tmp/go-build -gno-record-gcc-switches] -nostdlib -r -o /tmp/TestMinusRSymsWithSameName1171769842/001/ldr.syso [/tmp/TestMinusRSymsWithSameName1171769842/001/x0.o /tmp/TestM
inusRSymsWithSameName1171769842/001/x1.o]                                                                                                                                          
    elf_test.go:192: /code/go/bin/go build                                                                                                                                         
    elf_test.go:197: # elf_test                                                                                                                                                    
        loadelf: $WORK/b001/_pkg_.a(ldr.syso): elf but not elf relocatable object                                                                                                  
    elf_test.go:198: exit status 1                                                                                                                                                 
FAIL                                                                                                                                                                               
exit status 1                                                                                                                                                                      
FAIL    cmd/link        1.025s

As we can see, the resulting syso is built with -r flag. Not 100% sure it is caused by what is registered in this ticket, but it strikes me as quite close.

Footnotes

  1. having cherry-picked https://go-review.googlesource.com/c/go/+/483035 to Go and zig cc: support reading from stdin via "-x LANG -" #14462 to Zig.

andrewrk added a commit that referenced this issue Apr 7, 2023
This makes -r treated the same as -c which is to output an object file.
Zig's ELF linker code already handles multiple object files into an
object file with the -r flag to LLD.

closes #11683
@andrewrk
Copy link
Member

andrewrk commented Apr 7, 2023

Could you give #15201 a try?

@motiejus
Copy link
Contributor Author

motiejus commented Apr 8, 2023

Yes. It helps for that particular test. Thank you!

andrewrk added a commit that referenced this issue Apr 8, 2023
This makes -r treated the same as -c which is to output an object file.
Zig's ELF linker code already handles multiple object files into an
object file with the -r flag to LLD.

closes #11683
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior zig cc Zig as a drop-in C compiler feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants