为什么我的编译器预留更多的空间比所需的函数栈帧? [英] Why is my compiler reserving more space than required for a function stack frame?

查看:190
本文介绍了为什么我的编译器预留更多的空间比所需的函数栈帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数:

void func(int a)
{
    int x = a+2;
}

在组装code,在功能序言:

In the assembly code, in function prolog:

push %ebp
mov %esp, %ebp
sub $0x10, %esp

在code只需要预留空间,对于x,即4个字节。但它保留16字节。这是为什么 ?我一直看到它保留比需要更多的空间。

The code only needs to reserve space for x i.e. 4 bytes. But it is reserving 16 bytes. Why is that ? I have always seen it to reserve more space than required.

我的猜测:它往往在16个字节来存储。也就是说,如果我需要说的20个字节,它会预留32个字节,不管是什么。

My guess: it tends to store in 16 bytes. i.e. if I needed say 20 bytes, it will reserve 32 bytes, no matter what.

推荐答案

这在很大程度上取决于你的架构和编译器的标志,所以不可能指向一个单一的东西,并说这一定是它在这里。不过,我可以给你一些指点对您有帮助。

This highly depends on your architecture and compiler flags, so it is impossible to point to a single thing and say "this must be it" here. However, I can give you some pointers you may find helpful.

首先,考虑堆栈边界。你可能已经听说了-m preferred堆栈边界= X标志的GCC。如果不是,它主要是告诉你的编译器preFER堆栈上你的价值观为2 ^ X每个字节。那么你的编译器将尝试让这些价值观匹配在堆栈上尽可能地优化你的程序。另一方面,GCC改性剂如__packed__会使编译尝试尽可能紧地装配在堆叠中的数据

First, consider the stack boundary. You may have heard of the -mpreferred-stack-boundary=X flag to GCC. If not, it basically tells your compiler to prefer your values on the stack to be 2^X bytes each. Your compiler will then try to optimize your program so that these values fit on the stack as best as possible. On the other hand, GCC modifier such as __packed__ will make the compiler try to fit the data in the stack as tightly as possible.

还有堆栈保护。基本上,GCC会将堆栈上确保缓冲区溢出虚拟值不能超过你的段错误程序以外(这一点也不好玩,但比攻击者定位焊指令指针的控制更好)任何伤害。您可以轻松地尝试了这一点:最近采取任何GCC的版本,让用户溢出的缓冲区。你会注意到,该计划连同砸检测栈,终止的线路的消息退出。尝试用-fno堆栈保护器编译程序,并在栈上分配的本地内存可能会小一些。

There's also the stack protector. Basically, GCC places dummy values on the stack that make sure buffer overflows can't any harm other than segfaulting your program (which isn't fun, but better than an attacker tacking control of the instruction pointer). You can easily try this out: take any recent version of GCC and let the user overflow a buffer. You'll note that the program exits with a message along the lines of 'stack smashing detected, terminated'. Try compiling your program with -fno-stack-protector, and the allocated local memory on the stack will probably be smaller.

这篇关于为什么我的编译器预留更多的空间比所需的函数栈帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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