Golang calling convention

I confirmed caller allocates stack for arguments and return value and callee uses it as below. It didn’t use rcx, rdx, r8, r9 for args, rax for return value (different from x86_64 calling convention).

Sample Function:

  9 c := f1(5,6)
...
 15 func f1(a int, b int) int {
 16   return f2(a, b)
 17 }

Compiled Code:

// c := f1(5,6)
   0x000000000000228f <+191>:	mov    QWORD PTR [rsp],0x5
   0x0000000000002297 <+199>:	mov    QWORD PTR [rsp+0x8],0x6
   0x00000000000022a0 <+208>:	call   0x2000 
   0x00000000000022a5 <+213>:	mov    rcx,QWORD PTR [rsp+0x10]        :	mov    rcx,QWORD PTR gs:0x8a0
   0x0000000000002009 <+9>:	cmp    rsp,QWORD PTR [rcx]
   0x000000000000200c <+12>:	ja     0x2015 <main.f1+21>
   0x000000000000200e <+14>:	call   0x27fb0 
   0x0000000000002013 <+19>:	jmp    0x2000 
   0x0000000000002015 <+21>:	sub    rsp,0x18                         :	mov    rbx,QWORD PTR [rsp+0x20]
   0x000000000000201e <+30>:	mov    QWORD PTR [rsp],rbx              :	mov    rbx,QWORD PTR [rsp+0x28]
   0x0000000000002027 <+39>:	mov    QWORD PTR [rsp+0x8],rbx          :	call   0x2040 
   0x0000000000002031 <+49>:	mov    rbx,QWORD PTR [rsp+0x10]         :	mov    QWORD PTR [rsp+0x30],rbx
   0x000000000000203b <+59>:	add    rsp,0x18
   0x000000000000203f <+63>:	ret    

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *