Thread overview
LDC calling convention
Apr 09, 2019
Alexandru Militaru
Apr 09, 2019
Radu
Apr 10, 2019
kinke
April 09, 2019
Hello!

I am trying to port a kernel module to D and I am using LDC to compile the code. I want to call a function with 7 arguments and after some debugging sessions I’ve observed that the second argument has a different value in the callee function than the one I am passing in the caller function. After I’ve inspected with objdump the kernel module it seems to me that the LDC places the parameters in registers in a weird order. Note that the calling convention was fine until now and everything worked. Maybe the problem is that the function has 7 arguments. In the kernel, the calling convention is: first 6 arguments in rdi, rsi, rdx, rcx, r8, r9 and then on the stack.

Here is an image with the assembly code generated: in the left, the instructions generated by GCC for the kernel, in the right, the instructions generated by LDC. The parameters are, in this order: 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16. Can someone explain to me why this is happening and how to solve it?

https://imgur.com/6sSMWIY
April 09, 2019
On Tuesday, 9 April 2019 at 08:58:00 UTC, Alexandru Militaru wrote:
> Hello!
>
> I am trying to port a kernel module to D and I am using LDC to compile the code. I want to call a function with 7 arguments and after some debugging sessions I’ve observed that the second argument has a different value in the callee function than the one I am passing in the caller function. After I’ve inspected with objdump the kernel module it seems to me that the LDC places the parameters in registers in a weird order. Note that the calling convention was fine until now and everything worked. Maybe the problem is that the function has 7 arguments. In the kernel, the calling convention is: first 6 arguments in rdi, rsi, rdx, rcx, r8, r9 and then on the stack.
>
> [...]

The calling convention will be System V AMD64 ABI - a quick test shows that LDC passes the arguments in the expected order.

https://godbolt.org/z/0H1yD5

April 10, 2019
It is the SysV ABI, with the exception that the arguments/parameters are reversed for extern(D) - the rightmost parameter is passed in the first register etc. That's what DMD does and required for compatibility with DMD-style inline assembly in druntime/Phobos.