5

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. 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. I've already look here : Custom calling convention for P/Invoke and C# and it's come close from what i've to do. Currently, I'm using GCC, but hints from other compilers are welcome !

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 ; here job will be to dispatch calls between other C functions. So this wrapper needs to find %r11, save all registers and prepare the stack for further call. Is there a proper way to do this in C (with some inline asm) ?

Thanks a lot

Community
  • 1
  • 1
Matthieu
  • 53
  • 1
  • 3

1 Answers1

3

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.

Then, you can have a look at libffi's source code to see how they do it.

Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • Thanks for answering ; I didn't know libffi, i'm gonna check this, it's sound great. – Matthieu Jan 15 '10 at 10:48
  • If I were you, if possible, I would just rely on libffi. It's stable, maintained and handles many corner cases – Gregory Pakosz Jan 15 '10 at 10:51
  • OK, Agner Fog's document is very useful. But libffi does not save all registers, in particular YMM registers (not yet ?). By the way, i should be able to do some stuff with that ! Thanks a lot. – Matthieu Jan 15 '10 at 15:31