ziglang / zig

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

linkLibC causes linker script to be used instead of library #14888

Open RossComputerGuy opened 1 year ago

RossComputerGuy commented 1 year ago

Zig Version

0.11.0-dev.1910+6d7fb8f19

Steps to Reproduce and Observed Behavior

Use linkLibC() on an executable and in many cases, it'll link with a libc.so linker script instead of an actual shared library. It seems Zig does not do any sort of checks to prevent this when using the builder. In my case, I am using Nix and on the Zig Discord I've discovered that others may have this issue as well. I discovered this when my executable refused to execute and I ran ldd and found this:

outputs/out/bin/neutron-runner: error while loading shared libraries: /nix/store/76l4v99sk83ylfwkz8wmwrm4s8h73rhd-glibc-2.35-224/lib/libc.so: invalid ELF header

I ran file on the file it mentions and it says it's a text file. I checked the contents and it contains this:

/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /nix/store/76l4v99sk83ylfwkz8wmwrm4s8h73rhd-glibc-2.35-224/lib/libc.so.6 /nix/store/76l4v99sk83ylfwkz8wmwrm4s8h73rhd-glibc-2.35-224/lib/libc_nonshared.a  AS_NEEDED ( /nix/store/76l4v99sk83ylfwkz8wmwrm4s8h73rhd-glibc-2.35-224/lib/ld-linux-x86-64.so.2 ) )

Expected Behavior

Zig's build system should check if the libc is a linker script, if it is then it should parse it and figure out what to do with it. It should also check for other libc's by soversion in the same directory.

andrewrk commented 1 year ago

Can you please enhance the steps to reproduce? It's not enough information for me to reproduce.

RossComputerGuy commented 1 year ago

I wasn't able to recreate this as well. Something must've changed between when I reported this issue and it popped up. I'd say it's safe to close it but it can be reopened if someone else experiences it as well.