I have a C++ dynamic shared library abc.so
on Linux and my executable program dynamic loading it with dlopen
, and then cover abc.so
with a new version using rm + cp
in case of change inode of the using abc.so
, but there are also coredump sometimes, I realize that this may relate to the delay-loaded on dynamic shared library, but I can't confirm it, anybody could help me to figure out why? thanks very much!
Asked
Active
Viewed 216 times
3
-
3POSIX systems (like Linux) don't really overwrite files that are open by some application. Instead a new file is created on disk which will be found by subsequent open calls. The old file will still exist until the last application referencing the file have "closed" it. – Some programmer dude Sep 19 '19 at 06:29
-
Probably nothing until you restart your program. Ubuntu is now shipping hot-patching of kernels to avoid machine restarts, but I am not aware of hot-patching being applied to userland. Also see [Kernel Livepatch](https://wiki.ubuntu.com/Kernel/Livepatch) on the Ubuntu wiki. Microsoft has had hot patching for years for both kernel and user libraries. Also see the [Microsoft Detours](https://www.microsoft.com/en-us/research/project/detours/) library. – jww Sep 19 '19 at 06:29
1 Answers
4
Without the possibility to investigate it myself, this becomes speculative but using:
rm abc.so
cp new_version.so abc.so
has no effect on programs that has already loaded abc.so
. For programs linked with abc.so
(or using dlopen
to load it) it will present a problem if they are started (or uses dlopen
) while the file is removed or it's being copied into place. A core dump could very well be the result.
A better way to replace your abc.so
:
copy new_version.so to the same filesystem as abc.so (like the same directory)
mv new_version.so abc.so
This assures that there is always a complete version of abc.so
where it's expected to be. Any program needing it will either get the old version or the new version - and there's nothing in between.

Ted Lyngmo
- 93,841
- 5
- 60
- 108
-
Thanks for your reply. I found that the result becomes speculative if I replace the abc.so with new_version.so (even use mv) when my executable program dlopen the abc.so with RTLD_LAZY, especially the ABI of abc.so has been changed – W.Terry Sep 20 '19 at 07:49
-
1@W.Terry I'm surprised that `RTLD_LAZY` has any effect on this. What if you start your program, let it `dlopen(RTLD_LAZY...)` and then remove `abc.so` without replacing it at all? Will it crash when resolving symbols? If you change the interface you need to recompile the applications using `abc.so` so I hope you just did that as a test? – Ted Lyngmo Sep 20 '19 at 08:41
-
1Thanks @Ted Lyngmo, I have retest all cases, and the results prove that your conclusion is right. – W.Terry Oct 08 '19 at 02:31