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 zig cc Zig as a drop-in C compiler feature
Milestone

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
a-khabarov added a commit to a-khabarov/zig that referenced this issue May 20, 2024
This commit makes the way `zig cc` passes `-l/-L` flags for ELF linking
consistent with Clang and GCC.

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

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

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

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

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

Closes ziglang#19699
@andrewrk andrewrk added this to the 0.14.0 milestone Aug 15, 2024
@andrewrk andrewrk added the zig cc Zig as a drop-in C compiler feature label Aug 15, 2024
@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Feb 10, 2025
a-khabarov added a commit to a-khabarov/zig that referenced this issue Feb 21, 2025
This commit makes the way `zig cc` passes `-l`/`-L` flags for ELF
linking consistent with Clang and GCC.

The fix itself is in `src/link/Elf.zig`.

The new test is in `test/link/elf.zig`. It follows the example from
ziglang#19699.

The rest of the changes are new functionality needed for the test:
* `omit_soname` option for `Build.Module`, `OverlayOptions`.
  This passes `-fno-soname` to the linker when enabled.
* `addLibraryPathSpecial` for `Build.Module`, `Build.Step.Compile`.
  Like `addLibraryPath` but for `[]const u8`.
  `addRPathSpecial` is also added for completeness.
* `setCwd` for `Build.Step.Compile`.
  Allows setting current directory for compile steps.
* `opt_cwd` for `evalZigProcess` in `Build.Step`.
  Allows running `evalZigProcess` in a specific directory.
  Needed for setting current directory for compile steps.
* `--zig-lib-dir` is now appended differently in `Build.Step.Compile`.
  This is needed for compatibility with `setCwd`.
a-khabarov added a commit to a-khabarov/zig that referenced this issue Feb 21, 2025
This commit makes the way `zig cc` passes `-l`/`-L` flags for ELF
linking consistent with Clang and GCC.

The fix itself is in `src/link/Elf.zig`.

The new test is in `test/link/elf.zig`. It follows the example from
ziglang#19699.

The rest of the changes are new functionality needed for the test:
* `omit_soname` option for `Build.Module`, `OverlayOptions`.
  This passes `-fno-soname` to the linker when enabled.
* `addLibraryPathSpecial` for `Build.Module`, `Build.Step.Compile`.
  Like `addLibraryPath` but for `[]const u8`.
  `addRPathSpecial` is also added for completeness.
* `setCwd` for `Build.Step.Compile`.
  Allows setting current directory for compile steps.
* `opt_cwd` for `evalZigProcess` in `Build.Step`.
  Allows running `evalZigProcess` in a specific directory.
  Needed for setting current directory for compile steps.
* `--zig-lib-dir` is now appended differently in `Build.Step.Compile`.
  This is needed for compatibility with `setCwd`.
a-khabarov added a commit to a-khabarov/zig that referenced this issue Feb 24, 2025
This commit makes the way `zig cc` passes `-l`/`-L` flags for ELF
linking consistent with Clang and GCC.

The fix itself is in `src/link.zig` and `src/link/Elf.zig`.

The new test is in `test/link/elf.zig`. It follows the example from
ziglang#19699.

The rest of the changes are new functionality needed for the test:
* `omit_soname` option for `Build.Module`, `OverlayOptions`.
  This passes `-fno-soname` to the linker when enabled.
* `addLibraryPathSpecial` for `Build.Module`, `Build.Step.Compile`.
  Like `addLibraryPath` but for `[]const u8`.
  `addRPathSpecial` is also added for completeness.
* `setCwd` for `Build.Step.Compile`.
  Allows setting current directory for compile steps.
* `opt_cwd` for `evalZigProcess` in `Build.Step`.
  Allows running `evalZigProcess` in a specific directory.
  Needed for setting current directory for compile steps.
* `--zig-lib-dir` is now appended differently in `Build.Step.Compile`.
  This is needed for compatibility with `setCwd`.
a-khabarov added a commit to a-khabarov/zig that referenced this issue Feb 25, 2025
a-khabarov added a commit to a-khabarov/zig that referenced this issue Feb 25, 2025
a-khabarov added a commit to a-khabarov/zig that referenced this issue Feb 25, 2025
a-khabarov added a commit to a-khabarov/zig that referenced this issue Feb 25, 2025
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.

3 participants