问题
I'm overriding C++ global new/delete operators on Linux project. It all works nicely in my own code, until I found out that the new/delete symbols in system libraries gets also replaced with my code! This is a very bad problem since it goes way beyond 'level of evil' I intended.
So question is how do I prevent the linker/compiler from replacing the new/delete syms from other (system) shared libraries? Or more precisely how do I control what shared libraries link syms from my library? I would want that the system libraries would still use their default new/delete implementation. Especially when the executable later loads other optional dynamic libraries with dlopen() that are not under my control.
The custom global new/delete operator implementation is build into a shared library.
I searched all over the Internet how to control the dynamic linking but didn't succeed. I first tries change the library link order on the test executable but that didn't change anything.
回答1:
I found out that the new/delete symbols in system libraries gets also replaced with my code!
You can read an explanation for why this happens here.
So question is how do I prevent the linker/compiler from replacing the new/delete syms from other (system) shared libraries?
You can make your ::operator new
and ::operator delete
private to your library by building with -fvisibility=hidden
and explicitly marking the functions you do want to export with __attribute__((visibility("default")))
. Alternatively, you could use linker version script to achieve the same result.
来源:https://stackoverflow.com/questions/37145235/c-custom-global-new-delete-overriding-system-libraries