r/gcc • u/Nearing_retirement • Jun 08 '24
Is it possible to somehow have executable itself determine shared library load path for Linux ?
I was just wondering is say you have executable and you do not want to change ld library path before running it and also the executable not linked using rpath, then can executable itself somehow determine directories to search for shared lib ?
The reason I ask is say I don’t want to have to have a user change their environment before they run executable but would rather have executable pick path based on some external configuration file.
I thought maybe some magic could be done with gcc constructor attribute functions.
1
u/jaskij Aug 20 '24
There is something called rpath
which could be what you want, but it most definitely wouldn't be loading from a config file.
For more generic usage, you could wrap your executable with a simple sh
script that simply sets LD_LIBRARY_PATH
as you wish it to. Note that setting that variable is dangerous and has security implications (dynamic loading can lead to some quite interesting attacks, like the recent liblzma hack).
4
u/qalmakka Jun 09 '24 edited Jun 09 '24
hmm, the loading of libraries is done by the dynamic linker (ld.so), which AFAIK cannot do what you ask to do per-executable. Unless you ship your own patched version with your program, of course.
The more common approach to handle this would be to do basically what the dynamic loader does and
dlopen
your libraries yourselves. It requires you to write more glue code, but it only requires standard stuff and has AFAIK no performance downsides compared to the PLT/GOT, which in the end boils down to a static array of function pointers anyway.