从同一编译单元覆盖C中的函数调用

vij*_*raj 6 c linker gcc overriding function

我试图在C中覆盖一个函数调用,但是当在同一个编译单元中使用该函数时,我遇到了问题.在下面的代码中,我试图替换函数get_resolution(),但我只能在test.c中完成但不能从display.c中实现它.

// display.c -------------------------------------------------------------

#include <stdio.h>

void get_resolution()
{
    printf("Original get_resolution\n");
}

void display()
{
    get_resolution();
}

// test.c ----------------------------------------------------------------

#include <stdio.h>

void __wrap_get_resolution()
{
    printf("Mock get_resolution\n");
    // __real_get_resolution(); // Should be possible to call original
}

int main()
{
    display();         // **ISSUE** Original get_resolution() is called
    get_resolution();  // __wrap_get_resolution() is called
    return 0;
}

// gcc -Wl,--wrap,get_resolution display.c test.c
Run Code Online (Sandbox Code Playgroud)

我的要求是,当我从main()调用display()时,我希望执行__wrap_get_resolution(),但我总是看到正在调用原始的get_resolution().对拆卸的一点分析表明函数get_resolution被不同地调用:

在display() - > get_resolution()的地址已经解析

00000000 <_get_resolution>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 18                sub    $0x18,%esp
   6:   c7 04 24 00 00 00 00    movl   $0x0,(%esp)
   d:   e8 00 00 00 00          call   12 <_get_resolution+0x12>
  12:   c9                      leave  
  13:   c3                      ret    

00000014 <_display>:
  14:   55                      push   %ebp
  15:   89 e5                   mov    %esp,%ebp
  17:   83 ec 08                sub    $0x8,%esp
  1a:   e8 e1 ff ff ff          call   0 <_get_resolution>
  1f:   c9                      leave  
  20:   c3                      ret    
  21:   90                      nop
  22:   90                      nop
  23:   90                      nop
Run Code Online (Sandbox Code Playgroud)

在main() - > get_resolution的地址尚未解析

00000000 <___wrap_get_resolution>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 18                sub    $0x18,%esp
   6:   c7 04 24 00 00 00 00    movl   $0x0,(%esp)
   d:   e8 00 00 00 00          call   12 <___wrap_get_resolution+0x12>
  12:   c9                      leave  
  13:   c3                      ret    

00000014 <_main>:
  14:   55                      push   %ebp
  15:   89 e5                   mov    %esp,%ebp
  17:   83 e4 f0                and    $0xfffffff0,%esp
  1a:   e8 00 00 00 00          call   1f <_main+0xb>
  1f:   e8 00 00 00 00          call   24 <_main+0x10>
  24:   e8 00 00 00 00          call   29 <_main+0x15>
  29:   b8 00 00 00 00          mov    $0x0,%eax
  2e:   c9                      leave  
  2f:   c3                      ret    
Run Code Online (Sandbox Code Playgroud)

现在的问题是,如何防止编译器解析函数display()中使用的get_resolution()的地址,而是使用重定位表,以便在链接阶段可以覆盖get_resolution()函数?

编辑:

  1. 基于hroptatyr的响应,添加void get_resolution() __attribute__((weak));解决了使用mingw-gcc但不在我的目标平台QNX/ARM/gcc(4.4.2)中的问题
  2. 如果某人可以指向支持ARM目标的好库,那么即使像函数钩子这样的运行时方法也是可以接受的.

hro*_*tyr 5

只需使用预处理器:

void __wrap_get_resolution()
{
    /* calling the real one here */
    get_resolution();
}

#define get_resolution   __wrap_get_resolution

int main()
{
    /* the __wrap... gets called */
    get_resolution();
    ...
}
Run Code Online (Sandbox Code Playgroud)

想法是在放入需要查看原始函数的所有代码后,将函数调用"重命名"到包装函数.

更脏的版本可能是在本地遮蔽函数地址,如下所示:

int main()
{
    void(*get_resolution)() = __wrap_get_resolution;
    get_resolution();
    ...
}
Run Code Online (Sandbox Code Playgroud)

这个可以工作,但可以给你一些讨厌的警告.

编辑
尽管注释中指出display.c不需要更改,但这里是weak属性解决方案,来自http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

ELF目标支持弱符号,并且在使用GNU汇编器和链接器时也支持a.out目标.

/* in display.c */
void __real_get_resolution()
{
    ...
}
void get_resolution() __attribute__((weak, alias("__real_get_resolution")));

/* in test.c */
void get_resolution()
{
    /* this version will take precedence over get_resolution() in display.c */
    ...
    /* lastly call the real thing */
    __real_get_resolution();
}
Run Code Online (Sandbox Code Playgroud)

从现在开始,无论你在哪里调用get_resolution()(并编译"强"版本),都会调用包装版本.


TOC*_*TOC 0

这里可能提供答案: Override a function call in C

如果您想覆盖库中的函数,您可以使用 (unser linux) LD_PRELOAD。

希望这有帮助。

问候。