使用GCC的内联汇编直接C函数调用 [英] Direct C function call using GCC's inline assembly
问题描述
如果要从内联汇编程序调用C / C ++函数,可以执行以下操作:
If you want to call a C/C++ function from inline assembly, you can do something like this:
void callee() {}
void caller()
{
asm("call *%0" : : "r"(callee));
}
GCC随后将发出如下代码:
GCC will then emit code which looks like this:
movl $callee, %eax
call *%eax
这可能是有问题的,因为间接调用将破坏较旧CPU上的管道。
This can be problematic since the indirect call will destroy the pipeline on older CPUs.
由于地址 callee
最终是一个常量,可以想象可以使用 i
约束。从GCC在线报价 docs :
Since the address of callee
is eventually a constant, one can imagine that it would be possible to use the i
constraint. Quoting from the GCC online docs:
`i'
`i'
允许使用立即数整数(一个常量)。该
包含符号常量,其
的值仅在汇编
或更晚的时间才知道。
An immediate integer operand (one with constant value) is allowed. This includes symbolic constants whose values will be known only at assembly time or later.
如果我尝试这样使用它:
If I try to use it like this:
asm("call %0" : : "i"(callee));
我从汇编器中收到以下错误:
I get the following error from the assembler:
错误:后缀或操作数对'call'无效
Error: suffix or operands invalid for `call'
这是因为GCC发出了代码
This is because GCC emits the code
call $callee
而不是
call callee
所以我的问题是,是否有可能使GCC输出正确的 call
。
So my question is whether it is possible to make GCC output the correct call
.
推荐答案
我得到了 answer 从GCC的邮件列表中:
I got the answer from GCC's mailing list:
asm("call %P0" : : "i"(callee));
现在我只需要找出%P0
实际上意味着因为它似乎是一个未记录的功能...
Now I just need to find out what %P0
actually means because it seems to be an undocumented feature...
编辑:在查看了GCC源代码之后,它并不完全是清除约束前面的代码 P
的含义。但是,除其他外,它阻止了GCC将 $
放在常量值前面。在这种情况下,正是我所需要的。
Edit: After looking at the GCC source code, it's not exactly clear what the code P
in front of a constraint means. But, among other things, it prevents GCC from putting a $
in front of constant values. Which is exactly what I need in this case.
这篇关于使用GCC的内联汇编直接C函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!