Rustc generated linker command doesn't work


#1

I’m trying to build a program that uses the llvm-sys crate which links against libffi. I’ve added the following to my build.rs:

println!("cargo:rustc-link-lib=dylib=ffi");

But despite being able to find the library, the linker still fails.

The precise command that rustc is running is:

/nix/store/4r5kszyy0iirc5agfah45lvz7mnnsrb4-gcc-wrapper-7.3.0/bin/ld -plugin /nix/store/cm5znbhvylrh702hhsy9hnad7wk3v7xv-gcc-7.3.0/libexec/gcc/x86_64-unknown-linux-gnu/7.3.0/liblto_plugin.so -plugin-opt=/nix/store/cm5znbhvylrh702hhsy9hnad7wk3v7xv-gcc-7.3.0/libexec/gcc/x86_64-unknown-linux-gnu/7.3.0/lto-wrapper -plugin-opt=-fresolution=/run/user/1000/ccwFffNZ.res --eh-frame-hdr -m elf_x86_64 -dynamic-linker /nix/store/84h2zni7h805k0i1ys2bba3dsp1cqnhh-glibc-2.26-131/lib64/ld-linux-x86-64.so.2 -pie -o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077 /nix/store/84h2zni7h805k0i1ys2bba3dsp1cqnhh-glibc-2.26-131/lib/Scrt1.o /nix/store/84h2zni7h805k0i1ys2bba3dsp1cqnhh-glibc-2.26-131/lib/crti.o /nix/store/cm5znbhvylrh702hhsy9hnad7wk3v7xv-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-gnu/7.3.0/crtbeginS.o -L/home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib -L/home/shum/src/rust/malk-compiled/target/debug/deps -L/nix/store/7z3ly87dkri33jp19maqvkdnjv0h1m1z-llvm-5.0.1-lib/lib -L/nix/store/z2xnpl533a60xcw3mr2l58b0vaa6cbpf-llvm-5.0.1/lib -L/home/shum/src/rust/malk-compiled/target/debug/build/llvm-sys-40845203bbf3195b/out -L/home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib -L/nix/store/z2xnpl533a60xcw3mr2l58b0vaa6cbpf-llvm-5.0.1/lib -L/nix/store/vbrd04c1d93vbmigf7zvjf5cjkl63xds-ncurses-6.1-dev/lib -L/nix/store/s8c50k081m5xdc2lxz0dwimsnm74rlnb-ncurses-6.1/lib -L/nix/store/spxwrxnsf28sq5gdp42xvqac7psvlqya-zlib-1.2.11-dev/lib -L/nix/store/sghbqqxllij185b0n3i1ckk30yyjng14-zlib-1.2.11/lib -L/nix/store/7z3ly87dkri33jp19maqvkdnjv0h1m1z-llvm-5.0.1-lib/lib -L/nix/store/fxfghra58jcx92csjypysd4l84gnz5wn-clang-5.0.1-lib/lib -L/nix/store/1rh13l22z3j08ikir2bl4v6mldf747l6-libffi-3.2.1-dev/lib -L/nix/store/5mg32439k1lam17dwq9xyk52i4hcwlqy-libffi-3.2.1/lib -L/nix/store/z2xnpl533a60xcw3mr2l58b0vaa6cbpf-llvm-5.0.1/lib -L/nix/store/vbrd04c1d93vbmigf7zvjf5cjkl63xds-ncurses-6.1-dev/lib -L/nix/store/s8c50k081m5xdc2lxz0dwimsnm74rlnb-ncurses-6.1/lib -L/nix/store/spxwrxnsf28sq5gdp42xvqac7psvlqya-zlib-1.2.11-dev/lib -L/nix/store/sghbqqxllij185b0n3i1ckk30yyjng14-zlib-1.2.11/lib -L/nix/store/7z3ly87dkri33jp19maqvkdnjv0h1m1z-llvm-5.0.1-lib/lib -L/nix/store/fxfghra58jcx92csjypysd4l84gnz5wn-clang-5.0.1-lib/lib -L/nix/store/1rh13l22z3j08ikir2bl4v6mldf747l6-libffi-3.2.1-dev/lib -L/nix/store/5mg32439k1lam17dwq9xyk52i4hcwlqy-libffi-3.2.1/lib -L/nix/store/84h2zni7h805k0i1ys2bba3dsp1cqnhh-glibc-2.26-131/lib -L/nix/store/qc84dvliy1dzpidw10yvpi3di5f0q4vj-gcc-7.3.0-lib/lib -L/nix/store/qc84dvliy1dzpidw10yvpi3di5f0q4vj-gcc-7.3.0-lib/lib -L/nix/store/84h2zni7h805k0i1ys2bba3dsp1cqnhh-glibc-2.26-131/lib -L/nix/store/4r5kszyy0iirc5agfah45lvz7mnnsrb4-gcc-wrapper-7.3.0/bin -L/nix/store/cm5znbhvylrh702hhsy9hnad7wk3v7xv-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-gnu/7.3.0 -L/nix/store/cm5znbhvylrh702hhsy9hnad7wk3v7xv-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-gnu/7.3.0/../../../../lib64 -L/nix/store/cm5znbhvylrh702hhsy9hnad7wk3v7xv-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-gnu/7.3.0/../../.. -dynamic-linker /nix/store/84h2zni7h805k0i1ys2bba3dsp1cqnhh-glibc-2.26-131/lib/ld-linux-x86-64.so.2 -v --as-needed -z noexecstack /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.16u6js6g0l3k1ic6.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.1im38lueib99jsk0.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.1y16o1qfye96o7m0.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.1zeawhkbeobww1zn.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.2f0hry2t7c05ttdi.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.2jqywn86b2gsqohu.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.2m5v7dirmv8f2te8.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.3171x0bwu82dptu7.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.3327un9ffw56pxvo.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.3ayaeypdcro9d6yk.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.3cx7oljifvb206q7.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.3j3lkwpgdcki40xe.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.3rngp6bm2u2q5z0y.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.45nf4z58qqykpcpi.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.49a7n47po4ttqjl7.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.4w4n11m1e8fds2qd.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.4xq48u46a1pwiqn7.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.4yh8x2b62dcih00t.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.8xzrsc1ux72v29j.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.98g0d9x8aw3akpe.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.9elsx31vb4it187.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.c6lbtaiefvx3wya.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.kt25z0521ngsjub.rcgu.o /home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.crate.allocator.rcgu.o --gc-sections -z relro -z now -lffi -Bstatic /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib /home/shum/src/rust/malk-compiled/target/debug/deps/liblibc-aa4528b7e880ecd5.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-6c1b973d3d0bbe04.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-78c5d8bda65d9986.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_jemalloc-a48412ac73370f74.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-1d8fa13d955fcd38.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_system-0f7e40be987b8a0e.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-9279fa66ae3bbdf0.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-5645a289c27985c1.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_unicode-77550ee3b4648e92.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-7f17c84607abc32e.rlib /home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-c4234f5034e47b78.rlib -Bdynamic -lrt -ldl -ltinfo -lpthread -lz -lm -lstdc++ -lutil -lutil -ldl -lrt -lpthread -lpthread -lgcc_s -lc -lm -lrt -lpthread -lutil -lutil -rpath /nix/store/4izpinnm82cj23gimi41z0npa11sy8hv-env/lib64 -rpath /nix/store/4izpinnm82cj23gimi41z0npa11sy8hv-env/lib /nix/store/cm5znbhvylrh702hhsy9hnad7wk3v7xv-gcc-7.3.0/lib/gcc/x86_64-unknown-linux-gnu/7.3.0/crtendS.o /nix/store/84h2zni7h805k0i1ys2bba3dsp1cqnhh-glibc-2.26-131/lib/crtn.o 

However if I move the -lffi to near the start of the command line then linking works!

So my question is: how can I get rustc to generate correct command-line arguments such that it specifies -lffi earlier?


#2

I don’t think there’s any explicit control over ordering.

Have you tried setting this env var for a hack?

https://bitbucket.org/tari/llvm-sys.rs/src/9eb485ee97927a5b0689bc2b73d4e70f1956923a/build.rs?at=default&fileviewer=file-view-default#build.rs-313:320


#3

Thanks for pointing that out! It doesn’t fix the problem though :frowning:.