c - 是否可以将调用包装到静态链接的第 3 方库?

标签 c linux linker

我想跟踪从另一个第 3 方库发出的对某些第 3 方库的调用。

示例:我想跟踪对库 A 的调用。我的应用程序静态链接库 B,而库 B 又静态链接到库 A。所以基本上我拥有的是 libAB.a

在动态链接的情况下,我可以编写带有函数包装器的库 A2,我想跟踪库 A 并使用 LD_PRELOAD=A2.so。然后,我的包装器将被调用,我将看到跟踪。 就我而言,我不能使用动态链接。

是否可以使用静态链接实现相同的目的?

在理想情况下,我想将我的应用程序与 libAB.a 和跟踪库 libA2.a 链接起来并获取跟踪。

谢谢,
罗布斯塔

最佳答案

好的,我找到了:)

男人

       --wrap symbol
       Use a wrapper function for symbol.  Any undefined reference to symbol will be resolved to "__wrap_symbol".  Any undefined ref‐
       erence to "__real_symbol" will be resolved to symbol.

       This  can  be  used to provide a wrapper for a system function.  The wrapper function should be called "__wrap_symbol".  If it
       wishes to call the system function, it should call "__real_symbol".

       Here is a trivial example:

               void *
               __wrap_malloc (size_t c)
               {
                 printf ("malloc called with %zu\n", c);
                 return __real_malloc (c);
               }

       If you link other code with this file using --wrap malloc, then all calls to "malloc" will call the  function  "__wrap_malloc"
       instead.  The call to "__real_malloc" in "__wrap_malloc" will call the real "malloc" function.

关于c - 是否可以将调用包装到静态链接的第 3 方库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3053472/

相关文章:

c++ - Dll内存管理

linux - "searchd"显示 "FATAL: bind() failed on 0.0.0.0: Address already in use"

c++ - 简单的 C++ 代码链接器错误

c - 在 Oracle Linux 7.2 上编译 C 代码时出错

c - 如果 C 标准说禁止使用空格,为什么 clang 和 gcc 只在反斜杠后有空格时发出警告?

linux - SQLITE 比较两个表中的列以查找 LIKE 数据

javascript - Linux NodeJS 全局 NPM 包 ":No such file or directory"

macos - 链接器在链接时是否生成绝对虚拟地址

ios - 如何在 Swift5 中关闭应用程序时更改应用程序状态?

c - 尚未运行的函数导致的错误