Custom call to call X86_64 to call function C

I need to make an interface (say, a wrapper) that allows calling from X86_64 assembler code, using its calling convention for function C, with a different calling convention. It would be best to be "compiler independent" (juste modifying the wrapper), so I'm looking for something that adds registry / stack elements to the compiler. I already look here: The user calling convention for P / Invoke and C # , and it comes close to what I have to do. I am currently using GCC, but hints from other compilers are welcome!

So, here's what, for a better overview of the problem (weird conditional coding):

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 the "special" (wrapping) function C; here the task will direct calls between other functions of C. Therefore, this shell needs to find% r11, save all the registers and prepare the stack for further calls. Is there a proper way to do this in C (with some built-in asm)?

thanks a lot

+5
source share
1 answer

For documentation of calling conventions and how function parameters are passed (in registers, which? What's on the stack, etc.), see Agner Fog document .

Then you can look at the libffi source code to see how they do it.

+3

All Articles