为什么 gcc 4.x 在调用方法时默认为 linux 上的堆栈保留 8 个字节? [英] why gcc 4.x default reserve 8 bytes for stack on linux when calling a method?

查看:17
本文介绍了为什么 gcc 4.x 在调用方法时默认为 linux 上的堆栈保留 8 个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为asm的初学者,我正在检查gcc -S生成的asm代码来学习.

as a beginner of asm, I am checking gcc -S generated asm code to learn.

为什么 gcc 4.x 在调用方法时默认为堆栈保留 8 个字节?

why gcc 4.x default reserve 8 bytes for stack when calling a method?

func18 是没有返回没有参数没有定义本地变量的空函数.我不知道为什么这里保留了 8 个字节(任何论坛/网站都没有提到这个原因,人们似乎认为这是理所当然的)是为了 %ebp 只是推动?还是返回类型?!非常感谢!

func18 is the empty function with no return no param no local var defined. I can't figure out why 8 bytes is reserved here (neither any forum/site mention for the reason, ppl seems take it for granted) is it for the %ebp just push? or return type?! many thx!

      .globl _func18
  _func18:
     pushl   %ebp 
     movl    %esp, %ebp 
     subl    $8, %esp 
     .text 

推荐答案

某些指令要求某些数据类型与 16 字节边界对齐(特别是 SSE 数据类型 __m128).为了满足这个要求,gcc 确保堆栈最初是 16 字节对齐的,并以 16 字节的倍数分配堆栈空间.如果只需要压入一个 4 字节的返回地址和 4 字节的帧指针,则需要 8 个额外的字节来保持堆栈与 16 字节边界对齐.然而,如果 gcc 确定额外的对齐是不必要的(即没有使用花哨的数据类型并且没有调用外部函数),那么它可能会省略任何用于对齐堆栈的额外指令.确定这一点所需的分析可能需要执行某些优化过程.

Some instructions require certain data types to be aligned to as much as a 16-byte boundary (in particular, the SSE data type __m128). To meet this requirement, gcc ensures that the stack is initially 16-byte aligned, and allocates stack space in multiples of 16 bytes. If only a 4-byte return address and 4-byte frame pointer need to be pushed, 8 additional bytes are needed to keep the stack aligned to a 16-byte boundary. However, if gcc determines that the additional alignment is unnecessary (i.e. the fancy data types are not used and no external functions are called), then it may omit any additional instructions used to align the stack. The analysis necessary to determine this may require certain optimization passes to be performed.

另请参阅 gcc 文档以了解选项 -mpreferred-stack-boundary=num.

See also the gcc documentation for the option -mpreferred-stack-boundary=num.

这篇关于为什么 gcc 4.x 在调用方法时默认为 linux 上的堆栈保留 8 个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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