function calling convention
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=31
February 19, 2013
function calling convention
(original works by Peixu Zhu)
Function calling conventions are important for inter-operations between different languages, and debug inside. Since the birth of C language, the language has evolved for many years (it is older than me !!), with richer function calling conventions appeared. Some ideas validated before may be demanded to refresh.
Let us review current available conventions:
1. cdecl
- on the i386/x86_64 architecture, the cdecl attributes causes the compiler to assume that the calling function will pop off the stack space used to pass arguments, in another word, the caller is responsible to pop off the calling stack.
- arguments are all pushed into stack, with right to left.
- ’cause the stack is recovered by the caller function, thus, it is possible to implement functions with variable number of arguments, like fprintf series functions, since the compiler knows how many arguments are pushed in the stack.
- returned values normally in register RAX/EAX, or ST0.
2. stdcall
- on the i386/x86_64 architecture, the stdcall attribute cause the compiler to assume that the called function will pop off the stack space used to pass arguments, unless it take a variable number of arguments, i.e., the callee function is responsible to pop off the stack space before the function is returned to caller.
- arguments are all pushed into stack from right to left.
- it is possible to make functions with variable number of arguments, with supporting of compilers.
- returned values normally in register RAX/EAX.
3. fastcall
- on the i386 architecture, the fastcall atributes causes the compiler to pass the first argument(if of integral type) in the register ECX and the second argument(if of integral type) in the register EDX. subsequent and other typed arguments are passed on the stack from left to right.
- The callee function will pop the arguments off the stack, just like stdcall does.
- this is compiler dependent, some compilers may utilize other registers to pass argument.
- it makes less access of memory, and improve efficiency.
4. numbered register parameters
- on the i386 architectures, the attributes causes the compiler to pass arguments number one to number if they are of integral type in registers EAX, EDX, and ECX instead of on the stack.
- Functions that take a variable number of arguments will continue to be passed all of their arugments on the stack.
- return value in EAX.
- This is compiler dependent.
5. SSE register parameters
- on the i386 with SSE support, the attribute causes the compiler to pass up to 3 floating point arguments in SSE registers instead of on the stack.
- Functions that take a variable number of arguments will continue to pass all of their floating point arguments on the stack.
- Return value in EAX.
- This is a compiler dependent and CPU dependent feature.
- Greatly improve efficiency.
6. x86_64
- In 64-bit Intel programs the first six parameters are passed in registers: %rdi, %rsi, %rdx, %rcx, %r8, %r9.
- The return address is on the stack.
7. Power PC
- In Power PC programs, 32- or 64-bit, the first eight parameters are passed in registers: %r3, %r4, %r5, %r6, %r7, %r8, %r9, %r10.
- The return address is in register %lr.
Conclusion
Matching calling conventions is basically required. In practice, compiler optimization may cause the convention be changed potentially. Thus, be careful to specify call conventions of libraries, multi-threaded programs, and make sure mutex object has been specified volatile, otherwise, the potential register passing may cause nightmare.
function calling convention的更多相关文章
- C&C++ Calling Convention
tkorays(tkorays@hotmail.com) 调用约定(Calling Convention) 是计算机编程中一个比较底层的设计,它主要涉及: 函数参数通过寄存器传递还是栈? 函数参数从左 ...
- X86调用约定 calling convention
http://zh.wikipedia.org/wiki/X86%E8%B0%83%E7%94%A8%E7%BA%A6%E5%AE%9A 这里描述了在x86芯片架构上的调用约定(calling con ...
- C/C++:函数的调用约定(Calling Convention)和名称修饰(Decorated Name)以及两者不匹配引起的问题
转自:http://blog.csdn.net/zskof/article/details/3475182 注:C++有着与C不同的名称修饰,主要是为了解决重载(overload):调用约定则影响函数 ...
- 从栈不平衡问题 理解 calling convention
最近在开发的过程中遇到了几个很诡异的问题,造成了栈不平衡从而导致程序崩溃. 经过几经排查发现是和调用规约(calling convention)相关的问题,特此分享出来. 首先,讲一下什么是调用规约. ...
- Calling Convention的总结
因为经常需要和不同的Calling Convention打交道,前段时间整理了一下它们之间的区别,如下: 清理堆栈 参数压栈顺序 命名规则 (MSVC++) 备注 Cdecl 调用者 (Caller) ...
- PatentTips – Java native function calling
BACKGROUND OF INVENTION This invention relates to a system and method for providing a native functio ...
- [转]ARM64 Function Calling Conventions
from apple In general, iOS adheres to the generic ABI specified by ARM for the ARM64 architecture. H ...
- sparc v8 stack frame calling convention
main.c ; int main() { int a, b; int sum; a = ; b = ; sum = add(a, b); ; } int add(int a, int b) { in ...
- 调用惯例Calling Convention (或者说:调用约定)
调用惯例影响执行效率,参数的传递方式以及栈清除的方式. 调用惯例 参数传递顺序 谁负责清除参数 参数是否使用暂存器 register 从左到右 被调用者 是 pascal 从左到右 被调用者 否 ...
随机推荐
- linux初级学习笔记一:linux操作系统及常用命令,及如何获取命令的使用帮助!(视频序号:02_1,2)
本节学习的命令:ls,cd,type,pwd, printenv, hash, date, clock, man, hwclock, info, cal, echo, printf, file! 本节 ...
- AM335x Android eMMC mkmmc-android.sh hacking
# AM335x Android eMMC mkmmc-android.sh hacking # # . 有空解读一下android的分区文件. # . 代码来源:https://github.com ...
- 【Codeforces 757B】 Bash's big day
[题目链接] 点击打开链接 [算法] 若gcd(s1,s2,s3....sk) > 1, 则说明 : 一定存在一个整数d满足d|s1,d|s2,d|s3....,d|sk 因为我们要使|s|尽可 ...
- Myeclipse----Hibernate环境搭建
使用myEclipse来生成hibernate 持久化类和映射文件 总体步骤:创建数据库----创建web工程----创建数据视图中的数据库-----导入hibernate框架需要的capabilit ...
- 148D
概率dp+记忆化搜索 dp[i][j][0]表示当前公主走公主赢的概率,dp[i][j][1]表示当前龙走公主赢的概率,然后剩下的就是一些细节的讨论,记忆化搜索很方便 #include<bits ...
- MFC程序中的 _T("") 什么意思?
_T("")就是把引号内的字符串转换为宽字节的Unicode编码 宽字节就是unicode.
- k8s-helm-二十四
一.介绍 Helm是Kubernetes的一个包管理工具,用来简化Kubernetes应用的部署和管理.可以把Helm比作CentOS的yum工具. yum不光要解决包之间的依赖关系,还要提供具体的程 ...
- ASP.NET Core MVC 2.x 全面教程__ASP.NET Core MVC 19. XSS & CSRF
存库之前先净化,净化之后再提交到数据库 刚才插入的那笔数据 把默认的Razor引擎默认的EnCode去掉.Razor默认会开启htmlEnCodding 数据恢复回来 插入数据库之前对插入的数据进行净 ...
- 黑客攻防技术宝典web实战篇:Web 应用程序技术习题
猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. OPTIONS 方法有什么作用? OPTIONS 方法要求服务器报告可用于特定资源的 HT ...
- AForge.net简介和认识
AForge.NET是一个专门为开发者和研究者基于C#框架设计的,他包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,模糊系统,机器人控制等领域.这个框架由一系列的类库组成.主要包括有 ...