Skip to content
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

zig cc -l example sets the DSO paths incorrectly #19699

Open
a-khabarov opened this issue Apr 19, 2024 · 2 comments · May be fixed by #19818
Open

zig cc -l example sets the DSO paths incorrectly #19699

a-khabarov opened this issue Apr 19, 2024 · 2 comments · May be fixed by #19818
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@a-khabarov
Copy link

a-khabarov commented Apr 19, 2024

Zig Version

0.12.0-dev.3677+22a97cd23

Steps to Reproduce and Observed Behavior

$ mkdir -p /tmp/libtest/mylib
$ cd /tmp/libtest
$ echo 'void f() {}' > mylib/mylib.c
$ echo 'void f();' > mylib/mylib.h
$ echo '#include "mylib/mylib.h"' > main.c
$ echo 'int main() { f(); }' >> main.c
$ zig cc -shared mylib/mylib.c -o mylib/libmylib.so
$ zig cc main.c -Lmylib -lmylib -o main  # Link with name lookup
$ readelf -a main | grep -E 'NEEDED|RUNPATH'
 0x000000000000001d (RUNPATH)            Library runpath: [mylib]
 0x0000000000000001 (NEEDED)             Shared library: [mylib/libmylib.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

Expected Behavior

zig cc with -lmylib (lookup without explicit basename) shouldn't pass the whole resolved path to ld.lld.
Here is what clang (17.0.6) does for cases like this:

$ clang -shared mylib/mylib.c -o mylib/libmylib.so
$ clang main.c -Lmylib -lmylib -o main  # Link with name lookup
$ readelf -a main | grep -E 'NEEDED|RUNPATH'
 0x0000000000000001 (NEEDED)             Shared library: [libmylib.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

Note the absence of mylib/.
gcc (13.2.1 20230801) has the same behavior as clang here:

$ gcc -shared mylib/mylib.c -o mylib/libmylib.so
$ gcc main.c -Lmylib -lmylib -o main  # Link with name lookup
$ readelf -a main | grep -E 'NEEDED|RUNPATH'
 0x0000000000000001 (NEEDED)             Shared library: [libmylib.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

This is essentialy the behavior of zig cc when linking with explicit basename (-l:libmylib.so):

$ zig cc -shared mylib/mylib.c -o mylib/libmylib.so
$ zig cc main.c -Lmylib -l:libmylib.so -o main  # Link with explicit basename via `:`
$ readelf -a main | grep -E 'NEEDED|RUNPATH'
 0x000000000000001d (RUNPATH)            Library runpath: [mylib]
 0x0000000000000001 (NEEDED)             Shared library: [libmylib.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

Related comment: #15743 (comment)
Related issue: #15743

@a-khabarov a-khabarov added the bug Observed behavior contradicts documented or intended behavior label Apr 19, 2024
@a-khabarov
Copy link
Author

a-khabarov commented Apr 19, 2024

@andrewrk @kubkon This bug is very similar to #15743 and might be a blocker for the 0.12 release.

@JoshuaLeivers
Copy link

This issue affects uber/hermetic_cc_toolchain as mentioned here. Linking @motiejus to provide visibility on progress.

a-khabarov added a commit to a-khabarov/zig that referenced this issue Apr 30, 2024
This commit makes the way `zig cc` passes `-l/-L` flags for ELF linking
consistent with Clang and GCC.

Closes ziglang#19699
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants