可变参数和x64 [英] Variadic arguments and x64

查看:56
本文介绍了可变参数和x64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

va_arg \ va_start \ va_list \ va_end宏在x64中如何工作?

How do the va_arg \ va_start \ va_list \ va_end macros work under the hood in x64?

i386中的调用约定在堆栈上传递参数,因此宏仅增加一些指向堆栈基础的指针并将其转发. 但是,在x64中,所有参数都由寄存器传递....那么在那里发生了什么?被调用函数如何知道哪些寄存器用于传递参数以确保其不会破坏参数?

The calling convention in i386 passes parameters on the stack, hence the macro just increments some pointer that points to the stack base and forwards it. However, in x64, all parameters are passed by registers.... so what happens there? How does the called function know which registers were used to pass arguments to ensure it doesn't clobber them?

推荐答案

这是由ABI为该体系结构定义的.在用于amd64的SysV ABI(Windows以外几乎所有其他东西)上, ABI文档说(第56页及以后):

This is defined by the ABI for the architecture. On the SysV ABI for amd64 (so pretty much everything other than Windows), the ABI document says (page 56 and forward):

采用可变参数列表并已知调用宏va_start的函数的序言有望将参数寄存器保存到寄存器保存区域.

The prologue of a function taking a variable argument list and known to call the macro va_start is expected to save the argument registers to the register save area.

然后va_list是带有指针的结构,该指针指向寄存器保存区以及堆栈上可能已传递一些其他参数的位置(并非所有函数参数都适合寄存器).

Then va_list is a struct with a pointer to the register save area and the place on the stack where some additional arguments could have been passed (not all function arguments fit in registers).

这篇关于可变参数和x64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆