Search code examples
clinuxlinkershared-librariesloader

How does the linker or loader deal with broken soft links for shared libraries?


I have the same shared library in three different paths in the system. Let's say the paths are PATH1, PATH2 & PATH3.

Shared library name is libmylib.so

Now, in PATH1 if I replace the libmylib.so with a broken soft link, it goes and searches in PATH2 for the library.

But, in PATH1 if I replace libmylib.so with some other text file (or some unrelated file) which has the name libmylib.so, then application execution fails stating "not an ELF Header"

I am a little confused by the behaviour? Why does it search the other paths incase of a broken soft link and fails in case of an incorrect file. I was expecting it to search other paths for incorrect file also.


Solution

  • It probably just try to open it. Dangling symbolic link or not present, it will returns the same error. You'd need to do test explicitly for symbolic link if you wanted to do something different. Few programs care.