Search code examples
c++cshared-librariesld

what will happen if you overwrite a share library (currently mapped by a program) with a new version?


I'm wondering if anyone can expalin what will happen if you overwrite a share library file (currently mapped by a program) with a new version by copying the new *.so over the old *.so?

apparently, it should not affect the existing running program (prog1) which is linked to the old version. but what if you start another program (prog2) which use the same share library? will the prog2 simply map the old version of the library currently used by the running prog1 or a the new version of the share library will get loaded and linked by prog2?

do executables and shared libs have the same notion of file "caching" like the data files do?

does the ld.so detect versions of the share library

I'm working on the Unix platform and assuming the signature of the functions in the new version of share lib remain the same but their implementation has changed.


Solution

  • If you unlink the original file and replace it with another file -- which is what a normal cp or mv will do -- nothing particularly surprising happens. Any processes that are already running will continue using the library that was deleted, and any newly launched processes will use the new one.

    If you try to overwrite the existing file, you'll find out that's impossible. You'll get an ETXTBSY ("Text file busy") if you try to open a file for write that's currently mapped to executable pages. (This includes both executables and shared libraries.) This protection is implemented by the kernel.

    If you somehow manage to bypass those protections and overwrite an executable or shared library which is in use, Bad Things will happen. Any pages from that object which are currently swapped out will be replaced by pages of the new executable when they are paged in, resulting in unexpected behavior -- probably a crash.