0

I'm compiling a software with CMake and GCC under Ubuntu 18. Here's the relevant CMake line:

    set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib/")

setting the runpath relative to the origin.

Once the software is installed using CMake, readelf gives this information:

$ readelf -a my_exe | grep RUNPATH
0x000000000000001d (RUNPATH)            Library runpath: [$ORIGIN/../..lib/]

When running LD_DEBUG=libs ./my_exe, this is the output (shortened for readability):

     10093: find library=libgmp.so.10 [0]; searching
     10093:  search path=/home/buildbot/install/exe/../lib/tls/haswell/x86_64:/home/buildbot/install/exe/../lib/tls/haswell:/home/buildbot/install/exe/../lib/tls/x86_64:/home/buildbot/install/exe/../lib/tls:/home/buildbot/install/exe/../lib/haswell/x86_64:/home/buildbot/install/exe/../lib/haswell:/home/buildbot/install/exe/../lib/x86_64:/home/buildbot/install/exe/../lib        (RUNPATH from file ./exe/my_exe)
     10093:   trying file=/home/buildbot/install/exe/../lib/tls/haswell/x86_64/libgmp.so.10
     10093:   trying file=/home/buildbot/install/exe/../lib/tls/haswell/libgmp.so.10
     10093:   trying file=/home/buildbot/install/exe/../lib/tls/x86_64/libgmp.so.10
     10093:   trying file=/home/buildbot/install/exe/../lib/tls/libgmp.so.10
     10093:   trying file=/home/buildbot/install/exe/../lib/haswell/x86_64/libgmp.so.10
     10093:   trying file=/home/buildbot/install/exe/../lib/haswell/libgmp.so.10
     10093:   trying file=/home/buildbot/install/exe/../lib/x86_64/libgmp.so.10
     10093:   trying file=/home/buildbot/install/exe/../lib/libgmp.so.10
     10093:  search cache=/etc/ld.so.cache
     10093:   trying file=/usr/lib/x86_64-linux-gnu/libgmp.so.10
     10093: 
     10093: find library=libpthread.so.0 [0]; searching
     10093:  search path=/home/buildbot/install/exe/../lib      (RUNPATH from file ./exe/my_exe)
     10093:   trying file=/home/buildbot/install/exe/../lib/libpthread.so.0
     10093:  search cache=/etc/ld.so.cache
     10093:   trying file=/lib/x86_64-linux-gnu/libpthread.so.0

[...]

     10093: 
     10093: find library=libc.so.6 [0]; searching
     10093:  search path=/home/buildbot/install/exe/../lib      (RUNPATH from file ./exe/my_exe)
     10093:   trying file=/home/buildbot/install/exe/../lib/libc.so.6
     10093:  search cache=/etc/ld.so.cache
     10093:   trying file=/lib/x86_64-linux-gnu/libc.so.6
     10093: 
     10093: find library=libz.so.1 [0]; searching
     10093:  search cache=/etc/ld.so.cache
     10093:   trying file=/lib/x86_64-linux-gnu/libz.so.1

[...]

     10093: 
     10093: find library=libcrypto.so.1.1 [0]; searching
     10093:  search cache=/etc/ld.so.cache
     10093:   trying file=/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
     10093: 
     10093: find library=libHalf-2_3.so.24 [0]; searching
     10093:  search cache=/etc/ld.so.cache
     10093:  search path=/lib/x86_64-linux-gnu/tls/haswell/x86_64:/lib/x86_64-linux-gnu/tls/haswell:/lib/x86_64-linux-gnu/tls/x86_64:/lib/x86_64-linux-gnu/tls:/lib/x86_64-linux-gnu/haswell/x86_64:/lib/x86_64-linux-gnu/haswell:/lib/x86_64-linux-gnu/x86_64:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/tls/haswell/x86_64:/usr/lib/x86_64-linux-gnu/tls/haswell:/usr/lib/x86_64-linux-gnu/tls/x86_64:/usr/lib/x86_64-linux-gnu/tls:/usr/lib/x86_64-linux-gnu/haswell/x86_64:/usr/lib/x86_64-linux-gnu/haswell:/usr/lib/x86_64-linux-gnu/x86_64:/usr/lib/x86_64-linux-gnu:/lib/tls/haswell/x86_64:/lib/tls/haswell:/lib/tls/x86_64:/lib/tls:/lib/haswell/x86_64:/lib/haswell:/lib/x86_64:/lib:/usr/lib/tls/haswell/x86_64:/usr/lib/tls/haswell:/usr/lib/tls/x86_64:/usr/lib/tls:/usr/lib/haswell/x86_64:/usr/lib/haswell:/usr/lib/x86_64:/usr/lib        (system search path)
     10093:   trying file=/lib/x86_64-linux-gnu/tls/haswell/x86_64/libHalf-2_3.so.24
     10093:   trying file=/lib/x86_64-linux-gnu/tls/haswell/libHalf-2_3.so.24
     10093:   trying file=/lib/x86_64-linux-gnu/tls/x86_64/libHalf-2_3.so.24
     10093:   trying file=/lib/x86_64-linux-gnu/tls/libHalf-2_3.so.24
     10093:   trying file=/lib/x86_64-linux-gnu/haswell/x86_64/libHalf-2_3.so.24
     10093:   trying file=/lib/x86_64-linux-gnu/haswell/libHalf-2_3.so.24
     10093:   trying file=/lib/x86_64-linux-gnu/x86_64/libHalf-2_3.so.24
     10093:   trying file=/lib/x86_64-linux-gnu/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/x86_64-linux-gnu/tls/haswell/x86_64/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/x86_64-linux-gnu/tls/haswell/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/x86_64-linux-gnu/tls/x86_64/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/x86_64-linux-gnu/tls/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/x86_64-linux-gnu/haswell/x86_64/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/x86_64-linux-gnu/haswell/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/x86_64-linux-gnu/x86_64/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/x86_64-linux-gnu/libHalf-2_3.so.24
     10093:   trying file=/lib/tls/haswell/x86_64/libHalf-2_3.so.24
     10093:   trying file=/lib/tls/haswell/libHalf-2_3.so.24
     10093:   trying file=/lib/tls/x86_64/libHalf-2_3.so.24
     10093:   trying file=/lib/tls/libHalf-2_3.so.24
     10093:   trying file=/lib/haswell/x86_64/libHalf-2_3.so.24
     10093:   trying file=/lib/haswell/libHalf-2_3.so.24
     10093:   trying file=/lib/x86_64/libHalf-2_3.so.24
     10093:   trying file=/lib/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/tls/haswell/x86_64/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/tls/haswell/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/tls/x86_64/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/tls/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/haswell/x86_64/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/haswell/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/x86_64/libHalf-2_3.so.24
     10093:   trying file=/usr/lib/libHalf-2_3.so.24
     10093: 
./exe/my_exe: error while loading shared libraries: libHalf-2_3.so.24: cannot open shared object file: No such file or directory

Between the first and second lib, we can see that the search path from RUNPATH is greatly reduced. I imagine there's an optimization discarding all the folders that don't exist.

But then the RUNPATH is dropped (between libc.so.6 and libz.so.1) from the search. Which cause an issue down the road because libHalf-2_3.so.24 is in the lib directory relative to my_exe.

I haven't found an explanation for this behavior, and how to solve it.

Cyril
  • 559
  • 5
  • 17
  • Is `libz.so.1` required **directly** by your executable, or is it required by some library, on which your executable depends? – Tsyvarev Aug 28 '23 at 07:48
  • Directly, we use zlib for creating archives in the software – Cyril Aug 28 '23 at 07:49
  • That is, `libz.so.1` is listed as `(NEEDED)` in the output of `readelf --dynamic my_exe`? Please, check that. – Tsyvarev Aug 28 '23 at 07:57
  • No it is not listed, not even as needed – Cyril Aug 28 '23 at 08:02
  • So, `libz.so.1` is required (NEEDED) by some other library. `RUNPATH` settings of your executable don't affect on searching dependencies of the other library. – Tsyvarev Aug 28 '23 at 08:04
  • Any way to change this behavior? – Cyril Aug 28 '23 at 08:10
  • 1
    Documentation for [ld.so](https://man7.org/linux/man-pages/man8/ld.so.8.html) notes `DT_RPATH` attribute, which unlike to `DT_RUNPATH` affects on the searching for inner dependencies too. But generally you should fix RPATH of the library, which directly needs `libz.so.1` (or `libHalf-2_3.so.24`, as exactly given file is not found according to your description). – Tsyvarev Aug 28 '23 at 08:16

0 Answers0