在 C 中,是否可以将导出的函数名称更改为不同的名称? [英] In C, is it possible to change exported function name to different one?
问题描述
全部.
我想链接一个调用 malloc()
函数的库.但是,我的目标环境是不同的malloc()
作为内联函数提供.
我怎样才能使库对 malloc()
的调用直接到我的目标环境的 malloc()
例程?
有什么办法可以改变导出的函数名吗?如果是这样我可以先编码 my_malloc()
并将其导出为 malloc()
并链接那个图书馆:
#include <my_environment.h>//malloc() 是在那里声明的内联函数无效my_malloc(无效){malloc(无效);}
<小时>
更具体地说,该库是来自 linux 发行版的库,因此它依赖于 libc.但是我的环境是嵌入式的,没有 libc 库,并且 malloc()
, free()
, ... 是自定义实现的.有些是内联函数,有些是库函数.
GNU 链接器 (ld) 支持 --wrap=functionname
参数.我将简单地引用手册页中的文档,因为它包含一个应该完全满足您需要的示例:
--wrap=symbol 对符号使用包装函数.任何未定义的符号引用都将解析为__wrap_symbol".任何对__real_symbol"的未定义引用都将被解析为符号.
这可以用来为系统函数提供一个包装器.包装函数应称为__wrap_symbol".如果它想调用系统函数,它应该调用__real_symbol".
这是一个简单的例子:
无效*__wrap_malloc (size_t c){printf ("使用 %zu
调用 malloc", c);返回 __real_malloc (c);}
<块引用>
如果您使用 --wrap malloc
将其他代码与该文件链接,那么所有对malloc
"的调用都会调用函数__wrap_malloc
"代替.在__wrap_malloc
"中对__real_malloc
"的调用会调用真正的malloc
"函数.
您可能还希望提供一个__real_malloc
"函数,以便没有 --wrap
选项的链接会成功.如果这样做,则不应将__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
", 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屋!