是否有可能LD_ preLOAD不同参数的函数? [英] Is it possible to LD_PRELOAD a function with different parameters?

查看:127
本文介绍了是否有可能LD_ preLOAD不同参数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我通过创建一个共享对象,并使用LD_ preLOAD先加载它取代的功能。是否有可能有参数的函数从一个在原始库不同?

Say I replace a function by creating a shared object and using LD_PRELOAD to load it first. Is it possible to have parameters to that function different from the one in original library?

例如,如果我替换的pthread_mutex_lock ,这样,而不是参数的 pthread_mutex_t 需要的 pthread_my_mutex_t 。这可能吗?

For example, if I replace pthread_mutex_lock, such that instead of parameter pthread_mutex_t it takes pthread_my_mutex_t. Is it possible?

其次,除了功能,是它可以使用LD_ preLOAD改变结构的声明?例如,一个可以在一个多字段添加到结构

Secondly, besides function, is it possible to change structure declarations using LD_PRELOAD? For example, one may add one more field to a structure.

推荐答案

虽然可以安排提供修改后的 pthread_mutex_lock()的功能,code将有被编译调用标准功能。这将导致问题,当更换被调用传递给标准功能的参数。这是说的一种礼貌的方式:

Although you can arrange to provide your modified pthread_mutex_lock() function, the code will have been compiled to call the standard function. This will lead to problems when the replacement is called with the parameters passed to the standard function. This is a polite way of saying:


  • 期待它玉石俱焚

任何pre加载功能必须实现相同的接口—相同的名称,在相同的参数,相同的价值观出—作为函数它取代。该内部可以为不同的,因为你需要实现,但接口必须是相同的。

Any pre-loaded function must implement the same interface — same name, same arguments in, same values out — as the function it replaces. The internals can be implemented as differently as you need, but the interface must be the same.

同样与结构。现有的code编译期待一个尺码的结构,有一个特定的布局。你可能会蒙混过关,并在最后增加一个额外的领域,但非取代code将可能无法正常工作。它将分配空间结构的原始大小,而不是增强的结构等,它绝不会访问额外元素本身。它可能不是完全不可能的,但是您必须在设计程序动态处理不断变化的结构尺寸,它放在当你能做到这一点严重制约不足的答案:你不能大概是贴切(以及肯定是多少简单)。

Similarly with structures. The existing code was compiled to expect one size for the structure, with one specific layout. You might get away with adding an extra field at the end, but the non-substituted code will probably not work correctly. It will allocate space for the original size of structure, not the enhanced structure, etc. It will never access the extra element itself. It probably isn't quite impossible, but you must have designed the program to handle dynamically changing structure sizes, which places severe enough constraints on when you can do it that the answer "you can't" is probably apposite (and is certainly much simpler).

IMNSHO的LD_ preLOAD机制是可怕的紧急情况(并且是暂时的权宜之计对于给定的问题)。这不是你应该计划在任何远程类似经常使用的一种机制。

IMNSHO, the LD_PRELOAD mechanism is for dire emergencies (and is a temporary band-aid for a given problem). It is not a mechanism you should plan to use on anything remotely resembling a regular basis.

这篇关于是否有可能LD_ preLOAD不同参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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