如何在 C 中包装现有函数 [英] How to wrap existing function in C

查看:25
本文介绍了如何在 C 中包装现有函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试包装现有函数.

下面的代码是完美的.

#includeint __real_main();int __wrap_main(){printf("包装主\n");返回 __real_main();}int main(){printf("主\n");返回0;}

命令:

gcc main.c -Wl,-wrap,main

输出:

包裹主主要的

所以我用温度改变了主要功能.我的目标是包装 temp() 函数.

下面是代码

temp.c

#include内部温度();int __real_temp();int __wrap_temp(){printf("包装温度\n");返回 __real_temp();}内部温度(){printf("临时\n");返回0;}int main(){温度();返回0;}

命令:

gcc temp.c -Wl,-wrap,temp

输出:

temp

包裹的温度未打印.请指导我包装函数温度.

解决方案

ld 的联机帮助页说:

<块引用>

 --wrap=symbol对符号使用包装函数.对符号的任何未定义引用都将解析为__wrap_symbol".任何对__real_symbol"的未定义引用将被解析为符号.

此处的关键字未定义.

如果将定义 temp 放在与使用它的代码相同的翻译单元中,则它不会在使用它的代码中未定义.

您需要拆分代码定义和使用它的代码:

#!/bin/sh猫 >user.c <<'EOF'#includeint temp(void);int __real_temp(void);int __wrap_temp(){printf("包装温度\n");返回 __real_temp();}int main(){温度();返回0;}EOF猫 >temp.c <<'EOF'#include内部温度(){printf("临时\n");返回0;}EOFgcc user.c -Wl,-wrap,temp temp.c # OK./a.out

将构建拆分为两个单独的编译可能会更清楚:

$ gcc -c user.c$ gcc -c temp.c$ nm user.o temp.o速度:看跌期权0000000000000000 T 温度用户.o:0000000000000015 T主看跌期权你 __real_temp温度0000000000000000 T __wrap_temp

现在因为 tempuser.c 中未定义,链接器可以执行它的 __real_/__wrap_magic就可以了.

$ gcc user.o temp.o -Wl,-wrap=temp$ ./a.out包裹温度温度

I am trying to wrap existing function.

below code is perfectly worked.

#include<stdio.h>

int __real_main();

int __wrap_main()
{
    printf("Wrapped main\n");
    return __real_main();
}

int main()
{
    printf("main\n");
    return 0;
}

command:

gcc main.c -Wl,-wrap,main

output:

Wrapped main
main

So i have changed main function with temp. my goal is to wrap temp() function.

Below is the code

temp.c

#include<stdio.h>

int temp();

int __real_temp();

int __wrap_temp()
{
    printf("Wrapped temp\n");
    return __real_temp();
}

int temp()
{
    printf("temp\n");
    return 0;
}

int main()
{
    temp();
    return 0;
}

command:

gcc temp.c -Wl,-wrap,temp

output:

temp

Wrapped temp is not printing. please guide me to wrap funciton temp.

解决方案

The manpage for ld says:

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

The keyword here is undefined.

If you put the definition temp in the same translation unit as the code that uses it, it will not be undefined in the code that uses it.

You need to split the code definition and the code that uses it:

#!/bin/sh

cat > user.c  <<'EOF'
#include<stdio.h>

int temp(void);

int __real_temp(void);

int __wrap_temp()
{
    printf("Wrapped temp\n");
    return __real_temp();
}
int main()
{
    temp();
    return 0;
}
EOF

cat > temp.c <<'EOF'
#include<stdio.h>
int temp()
{
    printf("temp\n");
    return 0;
}
EOF


gcc user.c  -Wl,-wrap,temp temp.c  # OK
./a.out

Splitting the build into two separate compiles perhaps makes it clearer:

$ gcc -c user.c
$ gcc -c temp.c
$ nm user.o temp.o

temp.o:
                 U puts
0000000000000000 T temp

user.o:
0000000000000015 T main
                 U puts
                 U __real_temp
                 U temp
0000000000000000 T __wrap_temp

Now since temp is undefined in user.c, the linker can do its __real_/__wrap_magic on it.

$ gcc  user.o temp.o  -Wl,-wrap=temp
$ ./a.out
  Wrapped temp
  temp

这篇关于如何在 C 中包装现有函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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