在C中,是有可能改变导出的函数名称不同呢? [英] In C, is it possible to change exported function name to different one?

查看:123
本文介绍了在C中,是有可能改变导出的函数名称不同呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有

我要链接这就要求的malloc()函数库。
然而,我的目标环境是不同的一个,
malloc()函数是作为内联函数。

我怎样才能使图书馆的号召,直接向MALLOC
我的目标环境中的malloc()函数?

难道没有办法更改导出函数的名字吗?如果是这样
我可以code my_malloc()首先并将其导出为malloc()和链接
图书馆一句:

 的#include< my_environment.h> // malloc()函数声明有内联函数
无效my_malloc(无效){
   的malloc(无效);
}


更具体地,
图书馆是一个来自Linux发行版,因此依赖于libc中。
但我的环境是嵌入式之一,并没有libc库和的malloc(),free()的,......是定制实现的。有些是内联函数,有些是库函数。


解决方案

GNU链接器(LD)支持 - 包装= functionname 参数。我会简单地引用从该名男子页的文件,因为它包括哪些应该做一个例子正是你需要的:


  

- 包=符号使用包装函数符号。任何未定义的参考符号将被解析为__wrap_symbol。任何未定义的引用__real_symbol将会被解析为符号。


  
  

这可被用来提供一个系统功能的包装。包装函数应被称为__wrap_symbol。如果它希望调用系统函数,它应该叫__real_symbol。


  
  

下面是一个简单的例子:


 无效*
__wrap_malloc(为size_t C)
{
    的printf(调用%祖\\ n的malloc,C);
    返回__real_malloc(C);
}


  

如果您链接其他code,使用此文件 - 包装的malloc ,那么所有来电的malloc 将调用函数__ wrap_malloc 来代替。调用 __ real_malloc 中的 __ wrap_malloc 将调用真正的的malloc 的功能。


  
  

您可能希望提供一个 __ real_malloc 功能一样,所以,如果没有链接 - 包装选项​​会成功。如果你这样做,你不应该把的定义 __ real_malloc 在同一个文件的 __ wrap_malloc ;如果你这样做,汇编器可能会解决呼叫的连接器有机会就换到之前的的malloc


all.

I want to link a library which calls "malloc()" function. However, my target environment is different one and malloc() is supplied as inline-function.

How can I make the library's call to malloc direct to my target environment's malloc() routine?

Is it any way to change the exported function name? If so I can code my_malloc() first and export it as malloc() and link the library to that one:

#include <my_environment.h>  // malloc() is inline function declared there 
void my_malloc (void) {
   malloc (void);             
}


More specifically, the library is one from linux distro so it depends on libc. but my environment is embedded one and has no libc library and malloc(), free(), ... are custom-implemented. Some are inline functions and some are library functions.

解决方案

The GNU linker (ld) supports a --wrap=functionname parameter. I'll simply quote the documentation from the man page as it includes an example which should do exactly what you need:

--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.

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.

You may wish to provide a "__real_malloc" function as well, so that links without the --wrap option will succeed. If you do this, you should not put the definition of "__real_malloc" in the same file as "__wrap_malloc"; if you do, the assembler may resolve the call before the linker has a chance to wrap it to "malloc".

这篇关于在C中,是有可能改变导出的函数名称不同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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