Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ custom global new/delete overriding system libraries

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.

like image 302
JATothrim Avatar asked Oct 26 '25 12:10

JATothrim


1 Answers

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.

like image 57
Employed Russian Avatar answered Oct 28 '25 01:10

Employed Russian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!