Skip to content

zig build-exe should not strip indirectly needed libraries #23215

Open

Description

@martinetd

Zig Version

0.13.0
also tested latest 0.15.0-dev.37+423907c27

Steps to Reproduce and Observed Behavior

Building the following main.c against libgsl produces a binary that cannot run

  • main.c:
#include <gsl/gsl_cdf.h>
#include <stdio.h>

int main(){
    double bottom_tail = gsl_cdf_gaussian_P(-1.96, 1);
    printf("Area betweeen [-1.96, 1.96]: %g\n", 1 - 2*bottom_tail);
}
  • build (and comparison with gcc/clang)
$ pkg-config --cflags --libs gsl
 -lgsl -lgslcblas -lm
$ zig build-exe main.c -femit-bin=test-zig -lgsl -lgslcblas -lm
$ gcc main.c -o test-gcc -lgsl -lgslcblas -lm
$ clang main.c -o test-clang -lgsl -lgslcblas -lm
$ for t in test-*; do echo $t; ldd $t | grep gsl; done
test-clang
	libgsl.so.27 => /lib64/libgsl.so.27 (0x00007f7c2fe00000)
	libgslcblas.so.0 => /lib64/libgslcblas.so.0 (0x00007f7c301dc000)
test-gcc
	libgsl.so.27 => /lib64/libgsl.so.27 (0x00007f7213600000)
	libgslcblas.so.0 => /lib64/libgslcblas.so.0 (0x00007f7213914000)
test-zig
	libgsl.so.27 => /lib64/libgsl.so.27 (0x00007fdc05400000)
$ ./test-zig 
./test-zig: symbol lookup error: /lib64/libgsl.so.27: undefined symbol: cblas_ctrmv
$ LD_PRELOAD=/lib64/libgslcblas.so.0 ./test-zig
Area betweeen [-1.96, 1.96]: 0.950004

Expected Behavior

The immediate problem is that main.c itself doesn't depend on libgslcblas, so the library is stripped from the elf's NEEDED libs, but libgsl does need it and it's not explicit with libgsl on purpose to allow using other cblas libraries (according to https://www.gnu.org/software/gsl/doc/html/usage.html )

gcc/clang properly keep the indirectly needed libraries (even with -Wl,--as-needed), so zig probably should detect it is needed as well somehow.

Thank you!

Credits

Thanks to @ju1m for the reproducer, coming from https://discourse.nixos.org/t/gsl-dependency-cblas-in-nix-flake/38133/3

Activity

  1. added
    bug
    on Mar 12, 2025
  2. martinetd commented on Mar 12, 2025

    @martinetd
    Author

    (wow, there just was something in the pipes for that?) (looks unrelated)

    workaround until then, just using any symbol from gslcblas works so this main.c properly runs:

    #include <gsl/gsl_cdf.h>
    #include <stdio.h>
    
    extern void *cblas_ctrmv;
    
    int main(){
        void *foo = cblas_ctrmv;
        (void) foo;
        double bottom_tail = gsl_cdf_gaussian_P(-1.96, 1);
        printf("Area betweeen [-1.96, 1.96]: %g\n", 1 - 2*bottom_tail);
    }
    
  3. xdBronch commented on Mar 12, 2025

    @xdBronch
    Contributor

    I'm not sure how exactly this detection works but I find it odd it works differently on clang since we use LLD, you can use -needed-l instead of -l to force it

  4. martinetd commented on Mar 13, 2025

    @martinetd
    Author

    Yeah, that's the reason I tried clang...
    OTOH I can confirm I reproduce this with LLD, so I guess I can report this up to LLD next :|

    $ ld.lld --as-needed main.o -L /lib64 -l c -l gsl -l gslcblas -l m /lib64/crt1.o 
    $ ldd a.out
    	linux-vdso.so.1 (0x00007f66309cb000)
    	libc.so.6 => /lib64/libc.so.6 (0x00007f66307a1000)
    	libgsl.so.27 => /lib64/libgsl.so.27 (0x00007f6630400000)
    	/lib64/ld-linux-x86-64.so.2 (0x00007f66309cd000)
    	libm.so.6 => /lib64/libm.so.6 (0x00007f663031a000)
    
    $ ld.bfd --as-needed main.o -L /lib64 -l c -l gsl -l gslcblas -l m /lib64/crt1.o 
    $ ldd a.out
    	linux-vdso.so.1 (0x00007f6c67641000)
    	libc.so.6 => /lib64/libc.so.6 (0x00007f6c67417000)
    	libgsl.so.27 => /lib64/libgsl.so.27 (0x00007f6c67000000)
    	libgslcblas.so.0 => /lib64/libgslcblas.so.0 (0x00007f6c673d1000)
    	/lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x00007f6c67643000)
    	libm.so.6 => /lib64/libm.so.6 (0x00007f6c672eb000)
    

    (oh, mold does the same too, fun...)

    Well, up to you what to do with this then; when I find time I'll prod lld folks about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions