如何在 C 中为外部定义的函数创建别名? [英] How I can make alias on external defined function in C?
问题描述
在编译这个 C 代码时
During compiling this C code
extern void Default_Handler(void);
void NMI_Handler(void) __attribute__ ((weak, alias ("Default_Handler")));
我已经收到了
error: 'NMI_Handler' aliased to undefined symbol 'Default_Handler'
如何为外部定义的函数创建别名?
How I can make alias on external defined function?
编译器:
gcc version 7.2.1 20170904 (release) [ARM/embedded-7-branch revision 255204] (GNU Tools for Arm Embedded Processors 7-2017-q4-major)
推荐答案
要给外部函数取别名,可以使用 objcopy
.举个例子:
To alias an external function, you can use objcopy
. Here's an example:
假设我有一个函数的定义并且我给它起了别名,就像下面的程序(叫做 myimp.c
):
Say I have the definition of a function and I alias it, like in the following program (called myimp.c
):
// myimp.c
int
myadd(int x, int y)
{
return x+y;
}
int
coolguy (int x, int y) __attribute__((alias("myadd")));
如果我们编译这个(但不链接),我们会得到一个目标文件,我们可以检查它的符号:
If we compile this (but don't link) we get an object file and we can check out its symbols:
# compile
$ cc -c myimp.c
# look at the symbols
$ nm myimp.o
0000000000000000 T coolguy
0000000000000000 T myadd
所以我们可以看到 __attribute__((alias("myadd"))
只是添加了一个符号 coolguy
,其值与 相同,0000000000000000myadd
.如果你查看 nm 的 man
页面,它会说 T
表示它是 .text中的全局符号code> 部分.这是有道理的,因为该函数不是
static
并且包含指令(与数据相反).
So we can see that the __attribute__((alias("myadd"))
is simply adding a symbol coolguy
with the same value, 0000000000000000, as myadd
. If you look in the man
page for nm, it will say that T
means it is a global symbol in the .text
section. This makes sense because the function is not static
and comprises instructions (as opposed to data).
所以如果我们有目标文件,但没有源文件,并且我们想添加一个函数别名,我们可以使用 objcopy --add-symbol
来实现.
So if we have the object file, but not the source, and we want to add a function alias we can do so using objcopy --add-symbol
.
假设我们有编译这个源代码的目标文件:
Say we have the object file resulting from compiling this source code:
// myimp2.c
int
myadd(int x, int y)
{
return x+y;
}
如上编译,我们将得到myimp2.o
,其符号表工具如下:
Compiling as above, we'll get myimp2.o
, whose symbol table tools like this:
# look at the symbols
$ nm myimp2.o
0000000000000000 T myadd
所以我们要添加coolguy
符号,我们这样做
So we want to add the coolguy
symbol, which we do as follows
# objcopy --add-symbol coolguy=.text:0000000000000000,global myimp2.o myimp2_augmented.o
查看objcopy
man
页面中关于--add-symbol
的文档.基本上 .text
指定了部分,在冒号之后是值,然后 global
我通过传递一个错误类型发现并且 objcopy
失败并告诉我有效类型的列表,我从中选择了 global
.
Look at the documentation on --add-symbol
in the objcopy
man
page. Basically .text
specifies the section, after the colon the value, and then global
I found by passing a bad type and objcopy
failed and told me a list of the valid types, from which I chose global
.
尝试在 myimp2_augmented.o
上运行 nm
,您会看到我们添加了符号.
Try running nm
on myimp2_augmented.o
, you'll see that we've added the symbol.
现在您应该能够使用 myimp2_augmented.o
而不是 myimp.o
进行链接,并且您的程序可以调用 coolguy
而不会出现链接错误.
Now you should be able to link with myimp2_augmented.o
instead of myimp.o
and your program can call coolguy
without linking errors.
这篇关于如何在 C 中为外部定义的函数创建别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!