C++ custom global new/delete overriding system libraries

有些话、适合烂在心里 提交于 2021-02-07 19:51:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!