简体   繁体   English

自定义X86_64调用约定到C函数调用

[英]Custom X86_64 calling convention to C function call

I've to do an interface (say, a wrapper) that allow a call from X86_64 assembly code using his calling convention to C function, with other calling convention. 我要做一个接口(比如一个包装器)允许从X86_64汇编代码调用它的调用约定到C函数,以及其他调用约定。 The best thing would be to be pretty "compiler independant" (juste modifying the wrapper), so i'm looking for something that puts registers/stack stuff on compiler back. 最好的事情是非常“编译器独立”(juste修改包装器),所以我正在寻找将寄存器/堆栈内容放在编译器上的东西。 I've already look here : Custom calling convention for P/Invoke and C# and it's come close from what i've to do. 我已经看过这里: P / Invoke和C#的自定义调用约定 ,它与我要做的事情很接近。 Currently, I'm using GCC, but hints from other compilers are welcome ! 目前,我正在使用GCC,但欢迎来自其他编译器的提示!

So, here is the thing, for a best view of the problem (the custom coding convention is strange ) : 所以,这就是问题的最佳视图(自定义编码约定很奇怪):

pushq  %r11    # saves r11 for call
movq 64bits_address %r11 # move a 64 bits address that points on a data structure
callq *8(%r11) # calls an address in the data structure
popq %r11      # restores %r11 ; return value is in the structure

I need to be able to call a "special" (wrapper) C function ; 我需要能够调用“特殊”(包装)C函数; here job will be to dispatch calls between other C functions. 这里的工作是在其他C函数之间调用调用。 So this wrapper needs to find %r11, save all registers and prepare the stack for further call. 所以这个包装器需要找到%r11,保存所有寄存器并准备堆栈以便进一步调用。 Is there a proper way to do this in C (with some inline asm) ? 有没有一种正确的方法在C中执行此操作(使用一些内联asm)?

Thanks a lot 非常感谢

For documentation about calling conventions and how are the parameter passed to a function (in registers? which? what's on the stack etc) have a look at Agner Fog's document . 有关调用约定的文档以及如何将参数传递给函数(在寄存器中?哪些?堆栈中的内容等)请查看Agner Fog的文档

Then, you can have a look at libffi 's source code to see how they do it. 然后,您可以查看libffi的源代码,了解它们是如何做到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM