可以替换全局“operator new()”。到处? [英] Is it possible to replace the global "operator new()" everywhere?

查看:282
本文介绍了可以替换全局“operator new()”。到处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换全局运算符new() operator delete()它们的变体),以便做一些内存管理技巧。我想让我的应用程序中的所有代码使用自定义运算符(包括我自己的DLL中的代码以及第三方DLL)。我已经读取的东西,链接器将选择链接时看到的第一个定义(例如,如果包含您的自定义运算符new()的库先链接,它将击败与CRT的链接)。有什么办法保证这会发生吗?这是什么规则,因为这真的是一个乘法定义的符号(例如, void * operator new(size_t size)在全局命名空间有两个定义)?

I would like to replace the global operator new() and operator delete() (along with all of their variants) in order to do some memory management tricks. I would like all code in my application to use the custom operators (including code in my own DLLs as well as third-party DLLs). I have read things to the effect that the linker will choose the first definition it sees when linking (e.g., if the library that contains your custom operator new() is linked first, it will "beat" the link with the CRT). Is there some way to guarantee that this will happen? What are the rules for this, since this really is a multiply-defined symbol (e.g., void* operator new(size_t size) has two definitions in the global namespace)?

第三方DLL可以静态链接到CRT?即使他们与CRT动态链接,是否有一些方法我可以让他们链接到 operator new()

What about third-party DLLs that may be statically linked with the CRT? Even if they are dynamically linked with the CRT, is there some way I can get them to link with my operator new()?

推荐答案

C ++标准明确允许你编写你自己的全局操作符new和delete(和数组变量)。链接器必须使它工作,虽然如何由实现器(例如,弱外部的东西可以有助于提供东西,如果,只有当一个不存在)。

The C++ standard explicitly allows you to write your own global operator new and delete (and array variants). The linker has to make it work, though exactly how is up to the implementors (e.g., things like weak externals can be helpful for supplying something if and only if one isn't already present).

对于DLLs,它将是棘手的:静态链接的DLL显然不会使用你的代码,没有很多额外的工作。静态链接意味着它已经具有复制到DLL中的库代码的副本,并且使用它的DLL中的任何代码都具有已经编码的代码的地址。为了解决这个问题,你必须找出DLL中的new代码在哪里,并动态修补所有调用它的代码来调用你的代码。)

As far as DLLs go, it's going to be tricky: a statically linked DLL clearly won't use your code without a lot of extra work. Static linking means it already has a copy of the library code copied into the DLL, and any code in the DLL that used it has the address of that code already encoded. To get around that, you'd have to figure out where the code for new is in the DLL and dynamically patch all the code that calls it to call yours instead).

如果DLL链接到标准库动态,它获得只稍微更容易 - 导入表仍然编码DLL的名称和函数在该DLL,提供它所需要的。这可以得到(例如像微软的 Detours库),但它有点不平凡虽然肯定比DLL链接标准库更容易静态)。

If the DLL links to the standard library dynamically, it gets only marginally easier -- the import table still encodes the name of the DLL and function in that DLL that provides what it needs. That can be gotten around (e.g. with something like Microsoft's Detours library) but it's somewhat non-trivial (though certainly easier than when the DLL links the standard library statically).

这篇关于可以替换全局“operator new()”。到处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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