The x64 disassembly parameter passing convention under Windows, in a word, the call sequence is from left to right, Function( rcx, rdx, r8,r9, [rsp+0x20], [rsp+0x28], [rsp+0x30]..

x64 disassembly parameter passing convention under Windows

In a word, the call sequence is from left to right, Function( rcx, rdx, r8,r9, [rsp+0x20], [rsp+0x28], [rsp+0x30], [rsp+0x38], [rsp+0x40] , [rsp+0x48], [rsp+0x50], [rsp+0x58], [rsp+0x60] ...)

Note: The information in this article is collected from the Internet.

x64 architecture

The x64 architecture is a backward compatible extension of x86. It offers the same old 32-bit mode as x86, and a new 64-bit mode.

The term "x64" includes AMD64 and Intel64. The instruction sets are nearly identical.

register

x64 extends x86's 8 general-purpose registers to 64 bits and adds 8 new 64-bit registers. The names of 64-bit registers start with "r", so for example,   the 64-bit extension of  eax is rax . The new registers are named  r8  to  r15 .

The lower 32 bits, 16 bits and 8 bits of each register can be directly manipulated in the operand. This includes registers such as  esi whose lower 8 bits were not previously manipulable. The following table specifies assembly language names for the lower half of the 64-bit registers.

64-bit registers lower 32 bits lower 16 bits lower 8 bits

rax

eax

ax

aluminum

rbx

ebx

bx

bl

rcx

ecx

remaining snow

Cl

rdx

edx

Dx

Dl

rsi

Esi

Four

Sil

rdi

Edi

Of

dil

rbp

Ebp

Bp

bpl

particle

Esp

sp

Spl

r8

r8d

r8w

r8b

r9

r9d

r9w

r9b

r10

r10d

r10w

r10b

r11

r11d

r11w

r11b

r12

r12d

r12w

r12b

r13

r13d

r13w

r13b

r14

r14d

r14w

r14b

r15

r15d

r15w

r15b

Operations that output to a 32-bit sub-register are automatically zero-extended to the entire 64-bit register. Operations that output to 8-bit or 16-bit subregisters are not zero-extended (this is compatible x86 behavior).

The upper 8 bits of ax , bx , cx  and  dx can still  be addressed  as  ah , bh , ch , dh , but not for all types of operands.

The instruction pointer, eip  , and flags registers have been extended to (and  rflags respectively ) 64 bits.

x64 processors also provide several sets of floating-point registers:

  • Eight 80-bit x87 registers.

  • Eight 64-bit MMX registers. (with x87 registers.)

  • The original set of eight 128-bit SSE registers has been increased to sixteen.

calling convention

Unlike x86, the C/C++ compiler supports only one calling convention on x64. This calling convention takes advantage of the increased number of registers available on x64:

  • The first four integer or pointer arguments are passed in  the rcxrdxr8  , and  r9  registers.

  • The first four floating point arguments  are passed in the first four SSE registers xmm0xmm3 - .

  • The caller reserves space on the stack for parameters passed in registers. Called functions can use this space to spill the contents of registers onto the stack.

  • Any other arguments are passed on the stack.

  • Integer or pointer return values ​​are returned in  the rax  register, while floating-point return values  ​​are returned in xmm0 .

  • rax , rcx , rdx , r8r11 - are mutable.

  • rbx , rbp , rdi, rsi , r12r15 - are non-volatile.

C++'s calling convention is very similar:  this  pointer is passed as the implicit first argument. The next three arguments are passed in the remaining registers, and the rest are passed on the stack.

addressing mode

Addressing modes in 64-bit mode are similar to, but not identical to, x86.

  • Instructions that refer to 64-bit registers are automatically executed with 64-bit precision. (e.g.  mov rax,[rbx]  moves 8 bytes   from  rbx to rax .)

  • A special form of the mov instruction has been added for 64-bit immediate constants or constant addresses   . For all other instructions, the immediate constant or constant address remains 32 bits.

  • x64 provides new  addressing modes related to meta- addressing. Instructions that refer to a single constant address are encoded as  offsets from which to rip . For example, the mov rax,[ addr ]  instruction   moves 8 bytes starting  from addrrip + into rax .

Instructions that implicitly refer to the instruction pointer (such as  jmpcallpush , and  pop ) and the stack pointer treat them as 64-bit registers on x64.

 The purpose of registers under  Win64

Register

Status

Use

RAX Volatile Return value register
RCX Volatile First integer argument
RDX Volatile Second integer argument
R8 Volatile Third integer argument
R9 Volatile Fourth integer argument
R10:R1 Volatile Must be preserved as needed by caller; used in syscall/sysret instructions
R12:R15 Nonvolatile Must be preserved by callee
RDI Nonvolatile Must be preserved by callee
RSI Nonvolatile Must be preserved by callee
RBX Nonvolatile Must be preserved by callee
RBP Nonvolatile May be used as a frame pointer; must be preserved by callee
RSP Nonvolatile Stack pointer
XMM0 Volatile First FP argument
XMM1 Volatile Second FP argument
XMM2 Volatile Third FP argument
XMM3 Volatile Fourth FP argument
XMM4:XMM5 Volatile Must be preserved as needed by caller
XMM6:XMM15 Nonvolatile Must be preserved as needed by callee.

1. 传递参数

在 Win64 里使用下面寄存器来传递参数:

  • rcx - 第 1 个参数
  • rdx - 第 2 个参数
  • r8 - 第 3 个参数
  • r9 - 第 4 个参数

其它多出来的参数通过 stack 传递。

使用下面寄存器来传递浮数数:

  • xmm0 - 第 1 个参数
  • xmm1 - 第 2 个参数
  • xmm2 - 第 3 个参数
  • xmm3 - 第 4 个参数

下面的代码:

void EditTextFile(HWND hEdit, LPCTSTR szFileName)
{
        HANDLE hFile;
        DWORD dwFileSize;
        DWORD dwFileSizeHigh;
        LPTSTR lpFileText;
        LPTSTR lpFileTextW;
        WORD wSignature;
        DWORD dwReadSize;

        hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

        ... ...

}

CreateFile() 的参数有 7 个,那么看看 VC 是怎样安排参数传递:

void EditTextFile(HWND hEdit, LPCTSTR szFileName)
{
000000013F791570 40 56                push        rsi  
000000013F791572 41 54                push        r12  
000000013F791574 41 55                push        r13  
000000013F791576 48 83 EC 50          sub         rsp,50h  
000000013F79157A 48 8B C2             mov         rax,rdx  
        HANDLE hFile;
        DWORD dwFileSize;
        DWORD dwFileSizeHigh;
        LPTSTR lpFileText;
        LPTSTR lpFileTextW;
        WORD wSignature;
        DWORD dwReadSize;

        hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
000000013F79157D 45 33 ED             xor         r13d,r13d  
000000013F791580 4C 8B E1             mov         r12,rcx  
000000013F791583 4C 89 6C 24 30       mov         qword ptr [rsp+30h],r13       // 第 7 个参数  
000000013F791588 45 8D 45 01          lea         r8d,[r13+1]                   // 第 3 个参数
000000013F79158C 45 33 C9             xor         r9d,r9d                       // 第 4 个参数
000000013F79158F BA 00 00 00 80       mov         edx,80000000h                 // 第 2 个参数
000000013F791594 48 8B C8             mov         rcx,rax                       // 第 1 个参数  
000000013F791597 C7 44 24 28 80 00 00 00 mov      dword ptr [rsp+28h],80h       // 第 6 个参数
000000013F79159F C7 44 24 20 03 00 00 00 mov      dword ptr [rsp+20h>],3         // 第 5 个参数
000000013F7915A7 FF 15 2B 0B 00 00    call        qword ptr [__imp_CreateFileW (13F7920D8h)]  
000000013F7915AD 48 8B F0             mov         rsi,rax 

        ... ...

上面已经对 7 个参数的传递进行了标注,前 4 个参数通过 rcx,rdx,r8 以及 r9 寄存器传递,后 3 个参数确实通过 stack 传递。

可是,事情并没有这么简单:

在 Win64 下,会为每个参数保留一份用来传递的 stack 空间,以便回写 caller 的 stack

在上面的例子中:

  • [rsp+20h] - 第 5 个参数
  • [rsp+28h] - 第 6 个参数
  • [rsp+30h] - 第 7 个参数

实际上已经为前面 4 个参数保留了 stack 空间,分别是:

  • [rsp] - 第 1 个参数(使用 rcx 代替)
  • [rsp+08h] - 第 2 个参数(使用 rdx 代替)
  • [rsp+10h] - 第 3 个参数(使用 r8 代替)
  • [rsp+18h] - 第 4 个参数(使用 r9 代替)

虽然是使用了 registers 来传递参数,然而还是保留了 stack 空间。接下着就是 [rsp+20h][rsp+28h] 以及[rsp+30h] 对应的 4,5,个参数

2. 回写 caller stack

VC 使用了下面编译参数来实现回写 caller stack

/homeparams

当使用了这个编译选项或者在 Debug 版下,它强制将 registers 里的值写回 stack 中

正如下面的代码:

CreateFileWImplementation:
0000000076EC2A30 48 89 5C 24 08       mov         qword ptr [rsp+8],rbx             // 回写 caller stack
0000000076EC2A35 48 89 6C 24 10       mov         qword ptr [rsp+10h],rbp           // 回写 caller stack
0000000076EC2A3A 48 89 74 24 18       mov         qword ptr [rsp+18h],rsi           // 回写 caller stack
0000000076EC2A3F 57                   push        rdi  
0000000076EC2A40 48 83 EC 50          sub         rsp,50h  
0000000076EC2A44 8B DA                mov         ebx,edx  
0000000076EC2A46 48 8B F9             mov         rdi,rcx  
0000000076EC2A49 48 8B D1             mov         rdx,rcx  
0000000076EC2A4C 48 8D 4C 24 40       lea         rcx,[rsp+40h]  
0000000076EC2A51 49 8B F1             mov         rsi,r9  
0000000076EC2A54 41 8B E8             mov         ebp,r8d  
0000000076EC2A57 FF 15 33 A1 08 00    call        qword ptr [__imp_RtlInitUnicodeStringEx (76F4CB90h)] 

上面所显示的是 CreateFile() 在 kernel32.dll 模块里的实现代码,上面对回写机制进行了标注,回写的 stack 正好是 caller 调用时未参数所保留的 stack 空间,上面的代码并不是那么直观。

下面我演示一下使用 /homeparams 选项来编译代码。

上面的 EditTextFile() 函数结果如下:

void EditTextFile(HWND hEdit, LPCTSTR szFileName)
{
000000013F6A15C0 48 89 54 24 10       mov         qword ptr [rsp+10h],rdx          // 第 2 个参数回写
000000013F6A15C5 48 89 4C 24 08       mov         qword ptr [rsp+8],rcx            // 第 1 个参数回写
000000013F6A15CA 56                   push        rsi  
000000013F6A15CB 41 54                push        r12  
000000013F6A15CD 48 83 EC 58          sub         rsp,58h  
 HANDLE hFile;
 DWORD dwFileSize;
 DWORD dwFileSizeHigh;
 LPTSTR lpFileText;
 LPTSTR lpFileTextW;
 WORD wSignature;
 DWORD dwReadSize;

 hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
000000013F6A15D1 48 8B 4C 24 78       mov         rcx,qword ptr [szFileName]  
000000013F6A15D6 45 33 E4             xor         r12d,r12d  
000000013F6A15D9 45 33 C9             xor         r9d,r9d  
000000013F6A15DC 4C 89 64 24 30       mov         qword ptr [rsp+30h],r12  
000000013F6A15E1 45 8D 44 24 01       lea         r8d,[r12+1]  
000000013F6A15E6 BA 00 00 00 80       mov         edx,80000000h  
000000013F6A15EB C7 44 24 28 80 00 00 00 mov         dword ptr [rsp+28h],80h  
000000013F6A15F3 C7 44 24 20 03 00 00 00 mov         dword ptr [rsp+20h],3  
000000013F6A15FB FF 15 D7 1A 00 00    call        qword ptr [__imp_CreateFileW (13F6A30D8h)]

第 1 个参数回写 [rsp+8] 处,第 2 个参数回写 [rsp+10h] 处。

注意这里的 stack 就是对应 caller 调用时的 stack,经过调用后 [rsp] 是返回地址值,因此,在 callee 里设置:

  • callee 写 [rsp+8] = caller 的 [rsp]
  • callee 写 [rsp+10h] = caller 的 [rsp+8]
  • callee 的 [rsp] = return address

上面很直观地显示了使用 /homeparams 选项时的效果,对比前一段没有使用 /homeparams 选项编译时的结果,很容易发现这个机制。

回写 caller stack 机制目的是为了 Debug 所需。

3. 由 callee 保存

在一个程序里应尽量使用 registers,在 x64 里有 16 个通用寄存器和 16 个 xmm 寄存器,可是一些 registers 在使用前必须保存原来值,以防丢失原来值。

因此,在 callee 使用它们时会将原值压入栈中保存,在 Win64 里,下面 registers 由 callee 负责保存:

  • rbxrbprsirdi
  • r12 - r15
  • xmm6 - xmm15

每进入一个 callee,在使用它们之前都保存起来,返回 caller 之前,恢复原来值。因此这些寄存器的值是保持恒定的。

void EditTextFile(HWND hEdit, LPCTSTR szFileName)
{
000000013F9115C0 48 89 54 24 10       mov         qword ptr [rsp+10h],rdx  
000000013F9115C5 48 89 4C 24 08       mov         qword ptr [rsp+8],rcx  
000000013F9115CA 56                   push        rsi                     // 保存
000000013F9115CB 41 54                push        r12                     // 保存
000000013F9115CD 48 83 EC 58          sub         rsp,58h  
        HANDLE hFile;
        DWORD dwFileSize;

... ...


000000013F911708 48 83 C4 58          add         rsp,58h  
000000013F91170C 41 5C                pop         r12                    // 恢复
000000013F91170E 5E                   pop         rsi                    // 恢复
000000013F91170F C3                   ret

4. stack frame 结构

进入每个 callee 时,都会生成属于自己的 stack frame 结构,返回时会注销自己的 stack frame

  • rbp
  • rsp

由这两个 registers 来构造 stack frame 结构,rbp  stack frame pointerrsp 是 stack pointer

可是,在 Win64 里,似乎不使用 stack frame 结构,VC 不会为每个函数创建 stack frame 结构

在 Win64 里,始终在使用动态使用 rsp 来维护 stack

void EditTextFile(HWND hEdit, LPCTSTR szFileName)
{
000000013F9115C0 48 89 54 24 10       mov         qword ptr [rsp+10h],rdx  
000000013F9115C5 48 89 4C 24 08       mov         qword ptr [rsp+8],rcx  
000000013F9115CA 56                   push        rsi                    
000000013F9115CB 41 54                push        r12                     
000000013F9115CD 48 83 EC 58          sub         rsp,58h             // 为 callee 分配 stack
        HANDLE hFile;
        DWORD dwFileSize;

... ...


000000013F911708 48 83 C4 58          add         rsp,58h            // 注销 callee stack 结构
000000013F91170C 41 5C                pop         r12                    
000000013F91170E 5E                   pop         rsi                   
000000013F91170F C3 

VC 不会生成 x86 下典型的 stack frame 结构,始终由 rsp 维护 stack,/Gd 编译选项在 Win64 下会被忽略,rbp 被保留起来

在 Win64 里,rdi 寄存器的角色变得很微妙,在某些场合下它充当了一部分 stack frame pointer 的角色。

5. r11 与 rcx 以及 r10

在 64 位模式下,在 sysret 指令返回时,将从 rcx 处得到返回地址,从 r11 处得到 rflags 值,因此在进入 system services routine(系统服务例程)前,或者在系统服务例程中的第1个任务是 rcx 与 r11 寄存器,以便 sysret 返回。

在 Win64 里,r10 寄存器充当保存 rcx 值的作用,如下:

NtCallbackReturn:
00000000770FFDA0 4C 8B D1             mov         r10,rcx  
00000000770FFDA3 B8 02 00 00 00       mov         eax,2  
00000000770FFDA8 0F 05                syscall  
00000000770FFDAA C3                   ret 

在进入 system call 之前,保存 rcx 的值。

x86:又名 x32 ,表示 Intel x86 架构,即 Intel 的32位 80386 汇编指令集。

x64:表示 AMD64 和 Intel 的 EM64T ,而不包括 IA64 。至于三者间的区别,可自行搜索。

x64 跟 x86 相比寄存器的变化,如图:

从图上可以看到,X64架构相对于X86架构的主要变化,是将原来所有的寄存器都扩大了一倍,例如EAX现在扩充成RAX,同时,又新增加了从R8~R15这8个64位的寄位器,有点RISC的味道(RISC特点就是寄存器多)。

然后还有下面的一些改变:

  • x64上面默认的函数调用约定是 fast call ,也就是 ABI 是 fast call ;
  • 一个函数在调用时,前四个参数是从左至右依次存放于RCX、RDX、R8、R9寄存器里面,剩下的参数从左至右顺序入栈;
  • 调用者负责在栈上分配32字节的“shadow space”,用于存放那四个存放调用参数的寄存器的值(亦即前四个调用参数);
  • 小于64位(bit)的参数传递时高位并不填充零(例如只传递ecx),大于64位需要按照地址传递;
  • 被调用函数的返回值是整数时,则返回值会被存放于RAX;
  • 被调用函数不负责清栈,调用者负责清理栈;
  • RAX,RCX,RDX,R8,R9,R10,R11是“易挥发”的,不用特别保护,其余寄存器需要保护。(x86下只有eax, ecx, edx是易挥发的)
  • 栈需要16字节对齐,“call”指令会入栈一个8字节的返回值(注:即函数调用前原来的RIP指令寄存器的值),这样一来,栈就对不齐了(因为RCX、RDX、R8、R9四个寄存器刚好是32个字节,是16字节对齐的,现在多出来了8个字节)。所以,所有非叶子结点调用的函数,都必须调整栈RSP的地址为16n+8,来使栈对齐。
  • 对于 R8~R15 寄存器,我们可以使用 r8, r8d, r8w, r8b 分别代表 r8 寄存器的64位、低32位、低16位和低8位。

一些其他要注意的小问题:

  • 另外一些小问题要注意,AMD64不支持 push 32bit 寄存器的指令,最好的方法就是 push 和 pop 都用64位寄存器,即 push rbx ,不要使用 push ebx 。
  • 另外要补充的一点是,在一般情况下,X64 平台的 RBP 栈基指针被废弃掉,只作为普通寄存器来用,所有的栈操作都通过 RSP 指针来完成。

遗留问题

  以上都是关于 Windows 上的调用约定,即 Visual Studio 上使用的调用约定,至于 GCC 的函数调用约定是否一致,还不清楚,有知道的请指点一下,我从 asmlib 的64位汇编看,GCC 好像第一个参数用的是 rdi ,而不是 rcx 。

示例:

; 示例代码 1.asm

; 语法:GoASM

DATA SECTION

text     db 'Hello x64!',

caption  db 'My First x64 Application',

CODE SECTION

START:

sub rsp, 28h           ; 堆栈预留 shadow space (40 + 8)字节

xor r9d, r9d           ; r9

lea r8, caption        ; r8

lea rdx, text          ; rdx

xor rcx, rcx           ; rcx

call MessageBoxA

add rsp, 28h           ; 调用者自己恢复堆栈

ret

一般编译器实现调用调用约定无外乎以下这几种:

  • CDECL:C/C++默认的调用约定,调用方平栈,不定参数的函数可以使用,参数通过堆栈传递.
  • STDCALL:被调方平栈,不定参数的函数无法使用,参数默认全部通过堆栈传递.
  • FASTCALL32:被调方平栈,不定参数的函数无法使用,前两个参数放入(ECX, EDX),剩下的参数压栈保存.
  • FASTCALL64:被调方平栈,不定参数的函数无法使用,前四个参数放入(RCX, RDX, R8, R9),剩下的参数压栈保存.
  • System V:类Linux系统默认约定,前八个参数放入(RDI,RSI, RDX, RCX, R8, R9),剩下的参数压栈保存.

当栈顶指针esp小于栈底指针ebp时,就形成了栈帧,栈帧中可以寻址的数据有局部变量,函数返回地址,函数参数等。不同的两次函数调用,所形成的栈帧也不相同,当由一个函数进入另一个函数时,就会针对调用的函数开辟出其所需的栈空间,形成此函数的独有栈帧,而当调用结束时,则清除掉它所使用的栈空间,关闭栈帧,该过程通俗的讲叫做栈平衡。而如果栈在使用结束后没有恢复或过度恢复,则会造成栈的上溢或下溢,给程序带来致命错误。

cdecl 调用者平栈: cdecl是C/C++默认调用约定,该调用方式在函数内不进行任何平衡参数操作,而是在退出函数后对esp执行加4操作,从而实现栈平衡。

该约定会采用复写传播优化,将每次参数平衡的操作进行归并,在函数结束后一次性平衡栈顶指针esp,且不定参数函数可使用此约定。

stdcall 被调用者平栈: stdcall与cdecl只在参数平衡上有所不同,其余部分都一样,但该约定不定参数函数无法使用。

cdecl调用方式的函数在同一作用域内多次被调用,会在效率上比stdcall高一些,因为它可以使用复写传播优化,而stdcall在函数内平衡栈,无法使用复写传播优化。

fastcall 被调用者平栈: fastcall效率最高,它可利用寄存器传递参数,一般前两个或前四个参数用寄存器传递,其余参数传递则转换为栈传递,此约定不定参数函数无法使用。

对于32位来说使用ecx,edx传递前两个参数,后面的用堆栈传递。
对于64位则会使用RCX,RDX,R8,R9传递前四个参数,后面的用堆栈传递。

使用esp寻址: 在O2编译器选项中,为了提高程序执行效率,只要栈顶是稳定的,就可以不再使用ebp指针,而是利用esp指针直接访问局部变量,这样可节省一个寄存器资源。

Guess you like

Origin blog.csdn.net/a2831942318/article/details/127774181