title: gcc complaining about undefined reference when the reference really is defined
body: <p>I'm trying to build a program using Rust's build system which calls through to gcc to perform linking. The actual linker command it's using is this monster:</p>
  
  <pre><code>"cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/shum/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/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" "-o" "/home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077" "/home/shum/src/rust/malk-compiled/target/debug/deps/malk_compiled-bc1cbad2728a2077.crate.allocator.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "-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" "ffi" "-Wl,-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" "-Wl,-Bdynamic" "-l" "rt" "-l" "dl" "-l" "tinfo" "-l" "pthread" "-l" "z" "-l" "m" "-l" "stdc++" "-l" "util" "-l" "util" "-l" "dl" "-l" "rt" "-l" "pthread" "-l" "pthread" "-l" "gcc_s" "-l" "c" "-l" "m" "-l" "rt" "-l" "pthread" "-l" "util" "-l" "util"
  </code></pre>
  
  <p>The important part is that it includes <code>-l ffi</code>. The problem is, it still can't find symbols from libffi:</p>
  
  <pre><code>/home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `ffiTypeFor(llvm::Type*)':
  (.text._ZL10ffiTypeForPN4llvm4TypeE+0x3a): undefined reference to `ffi_type_float'
  /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `ffiTypeFor(llvm::Type*)':
  (.text._ZL10ffiTypeForPN4llvm4TypeE+0x43): undefined reference to `ffi_type_void'
  /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `ffiTypeFor(llvm::Type*)':
  (.text._ZL10ffiTypeForPN4llvm4TypeE+0x53): undefined reference to `ffi_type_pointer'
  /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `ffiTypeFor(llvm::Type*)':
  (.text._ZL10ffiTypeForPN4llvm4TypeE+0x63): undefined reference to `ffi_type_double'
  /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `ffiTypeFor(llvm::Type*)':
  (.text._ZL10ffiTypeForPN4llvm4TypeE+0x78): undefined reference to `ffi_type_sint8'
  /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `ffiTypeFor(llvm::Type*)':
  (.text._ZL10ffiTypeForPN4llvm4TypeE+0x83): undefined reference to `ffi_type_sint16'
  /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `ffiTypeFor(llvm::Type*)':
  (.text._ZL10ffiTypeForPN4llvm4TypeE+0x93): undefined reference to `ffi_type_sint64'
  /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `ffiTypeFor(llvm::Type*)':
  (.text._ZL10ffiTypeForPN4llvm4TypeE+0xb3): undefined reference to `ffi_type_sint32'
  /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `llvm::Interpreter::callExternalFunction(llvm::Function*, llvm::ArrayRef&lt;llvm::GenericValue&gt;)':
  (.text._ZN4llvm11Interpreter20callExternalFunctionEPNS_8FunctionENS_8ArrayRefINS_12GenericValueEEE+0x7d7): undefined reference to `ffi_prep_cif'
  /home/shum/src/rust/malk-compiled/target/debug/deps/libllvm_sys-afd0e40fe14e6732.rlib(ExternalFunctions.cpp.o): In function `llvm::Interpreter::callExternalFunction(llvm::Function*, llvm::ArrayRef&lt;llvm::GenericValue&gt;)':
  (.text._ZN4llvm11Interpreter20callExternalFunctionEPNS_8FunctionENS_8ArrayRefINS_12GenericValueEEE+0x82d): undefined reference to `ffi_call'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>This is weird since it's definitely finding libffi. I can confirm this by running the linking command through strace:</p>
  
  <pre><code>...
  20840 openat(AT_FDCWD, "/nix/store/fxfghra58jcx92csjypysd4l84gnz5wn-clang-5.0.1-lib/lib/libffi.so", O_RDONLY) = -1 ENOENT (No such file or directory)
  20840 openat(AT_FDCWD, "/nix/store/fxfghra58jcx92csjypysd4l84gnz5wn-clang-5.0.1-lib/lib/libffi.a", O_RDONLY) = -1 ENOENT (No such file or directory)
  20840 openat(AT_FDCWD, "/nix/store/1rh13l22z3j08ikir2bl4v6mldf747l6-libffi-3.2.1-dev/lib/libffi.so", O_RDONLY) = -1 ENOENT (No such file or directory)
  20840 openat(AT_FDCWD, "/nix/store/1rh13l22z3j08ikir2bl4v6mldf747l6-libffi-3.2.1-dev/lib/libffi.a", O_RDONLY) = -1 ENOENT (No such file or directory)
  20840 openat(AT_FDCWD, "/nix/store/5mg32439k1lam17dwq9xyk52i4hcwlqy-libffi-3.2.1/lib/libffi.so", O_RDONLY) = 31
  </code></pre>
  
  <p>I can also run <code>nm -D</code> on that file and confirm that it does indeed contain the missing symbols:</p>
  
  <pre><code>...
  00000000000063d0 R ffi_type_double
  00000000000063f0 R ffi_type_float
  00000000000063b0 R ffi_type_longdouble
  0000000000006410 R ffi_type_pointer
  ...
  </code></pre>
  
  <p>I can even see them being read-out in the strace log:</p>
  
  <pre><code>20840 read(31, "dlmmap.constprop.12\0open_temp_exec_file_mutex\0selinux_enabled\0emutramp_enabled\0classify_argument\0examine_argument\0__FRAME_END__\0ffi_prep_cif_core\0__dso_handle\0_DYNAMIC\0__GNU_EH_FRAME_HDR\0__TMC_END__\0_GLOBAL_OFFSET_TABLE_\0ffi_type_void\0getenv@@GLIBC_2.2.5\0ffi_type_double\0ffi_prep_closure_loc\0ffi_prep_cif\0free@@GLIBC_2.2.5\0ffi_type_complex_double\0endmntent@@GLIBC_2.2.5\0ffi_java_raw_to_ptrarray\0abort@@GLIBC_2.2.5\0__errno_location@@GLIBC_2.2.5\0unlink@@GLIBC_2.2.5\0_ITM_deregisterTMCloneTable\0ffi_type_sint8\0ffi_java_raw_call\0_edata\0fclose@@GLIBC_2.2.5\0ffi_prep_closure\0ffi_type_uint16\0_fini\0strlen@@GLIBC_2.2.5\0__stack_chk_fail@@GLIBC_2.4\0mmap@@GLIBC_2.2.5\0ffi_type_sint32\0getmntent_r@@GLIBC_2.2.5\0strchr@@GLIBC_2.2.5\0ftruncate@@GLIBC_2.2.5\0mkostemp@@GLIBC_2.7\0ffi_type_uint8\0ffi_type_float\0ffi_raw_to_ptrarray\0ffi_type_uint64\0ffi_type_sint64\0ffi_prep_raw_closure_loc\0__getdelim@@GLIBC_2.2.5\0ffi_type_pointer\0ffi_closure_unix64\0__gmon_start__\0memcpy@@GLIBC_2.14\0ffi_type_uint32\0ffi_type_sint16\0ffi_closure_unix64_inner\0statf"..., 4096) = 3576
  </code></pre>
  
  <p>So how is this possible? How is it that gcc can see the correct library, is reading it, but still complains about undefined references?</p>