c++gnulddlopenlinker-scripts

Querying the Target of a Linker Script?


I have some C++ code that is dlopening the "libm.so" library:

dlopen("libm.so", RTLD_GLOBAL | RTLD_NOW);

Under RHEL7, this was a sym-link:

libm.so -> libm.so.6

Under RHEL9, this is now a linker script:

> cat libm.so
/* GNU ld script
*/
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /lib64/libm.so.6  AS_NEEDED ( /lib64/libmvec.so.1 ) )

which means this dlopen() call fails because it cannot parse this linker script.

The only solution to this problem that I have found is to hard-code the library name to dlopen("libm.so.6", ..."). I am either looking for a way to query that number "6" so that this is not hard-coded in the source or to otherwise evaluate the linker script at build time to find the required target.


Solution

  • I am either looking for a way to query that number "6" so that this is not hard-coded in the source

    There is nothing wrong with hard-coding this number on Linux (or other GLIBC-based systems). It has not changed in the last 20 years and is very unlikely to ever change.

    or to otherwise evaluate the linker script at build time to find the required target.

    You can use grep -q 'libm\.so\.6' $(gcc --print-file-name=libm.so) to test that libm.so.6 is still the right version and error out if it's not.

    P.S. g++ links with -lm by default; it's very likely that your dlopen does not serve any useful purpose.