C ++自定义全局新/删除覆盖系统库 [英] C++ custom global new/delete overriding system libraries

查看:59
本文介绍了C ++自定义全局新/删除覆盖系统库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux项目上覆盖了C ++全局new/delete运算符.这一切在我自己的代码中运行良好,直到我发现系统库中的新/删除符号也被我的代码替换了!这是一个非常严重的问题,因为它超出了我打算的邪恶水平".

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.

所以问题是如何防止链接程序/编译器替换其他(系统)共享库中的新符号/删除符号?或者更确切地说,我该如何控制哪些共享库从我的库中链接符号?我希望系统库仍将使用其默认的new/delete实现.尤其是当可执行文件稍后使用dlopen()加载其他不受我控制的动态库时.

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.

自定义全局new/delete运算符实现已内置到共享库中.

The custom global new/delete operator implementation is build into a shared library.

我在Internet上搜索了如何控制动态链接,但没有成功.我首先尝试更改测试可执行文件上的库链接顺序,但没有任何改变.

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.

推荐答案

我发现系统库中的新/删除符号也被替换为我的代码!

I found out that the new/delete symbols in system libraries gets also replaced with my code!

您可以在此处阅读解释.

所以问题是如何防止链接程序/编译器替换其他(系统)共享库中的新符号/删除符号?

So question is how do I prevent the linker/compiler from replacing the new/delete syms from other (system) shared libraries?

您可以使用 -fvisibility = hidden 进行显式构建,从而使自己的 :: operator新 :: operator删除对您的库私有使用 __ attribute __((visibility("default")))标记要执行的功能.另外,您可以使用链接器版本脚本来获得相同的结果.

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.

这篇关于C ++自定义全局新/删除覆盖系统库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆