堆栈分配,为什么多余的空间? [英] Stack allocation, why the extra space?
问题描述
为了更好地掌握调用约定以及堆栈是如何处理的,我玩了一段时间,但我无法弄清楚为什么在设置堆栈时主分配三个额外的双字(<主+ 0>
)。它没有对齐到8字节或16字节,所以这不是我所知道的原因。正如我所看到的,主要需要12个字节的两个参数func和返回值。
我缺少什么?
该程序是在x86架构上用gcc -ggdb编译的C代码。
编辑:我从gcc中删除了-O0标志,对输出没有影响。
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b转储函数main的汇编代码
0x080483d1 <+ 0>:sub esp,0x18
0x080483d4 <+ 3>:mov DWORD PTR [esp + 0x4],0x7
0x080483dc< + 11> ;: mov DWORD PTR [esp ],0x3
0x080483e3< + 18> ;:呼叫0x80483b4< func>
0x080483e8 <+ 23>:mov DWORD PTR [esp + 0x14],eax
0x080483ec <+ 27>:add esp,0x18
0x080483ef< + 30>:ret
汇编器转储结束。
编辑:当然我应该发布C代码:
int func(int a,int b){
int c = 9;
返回a + b + c;
}
void main(){
int x;
x = func(3,7);
}
该平台是Arch Linux i686。
它是一致的。我假定出于某种原因, esp
会从一开始就对齐,显然不是。
gcc
将堆栈帧调整为每个默认值16字节,这就是发生了什么。
I was playing around a bit to get a better grip on calling conventions and how the stack is handled, but I can't figure out why main allocates three extra double words when setting up the stack (at <main+0>
). It's neither aligned to 8 bytes nor 16 bytes, so that's not why as far as I know. As I see it, main requires 12 bytes for the two parameters to func and the return value.
What am I missing?
The program is C code compiled with "gcc -ggdb" on a x86 architecture.
Edit: I removed the -O0 flag from gcc, and it made no difference to the output.
(gdb) disas main
Dump of assembler code for function main:
0x080483d1 <+0>: sub esp,0x18
0x080483d4 <+3>: mov DWORD PTR [esp+0x4],0x7
0x080483dc <+11>: mov DWORD PTR [esp],0x3
0x080483e3 <+18>: call 0x80483b4 <func>
0x080483e8 <+23>: mov DWORD PTR [esp+0x14],eax
0x080483ec <+27>: add esp,0x18
0x080483ef <+30>: ret
End of assembler dump.
Edit: Of course I should have posted the C code:
int func(int a, int b) {
int c = 9;
return a + b + c;
}
void main() {
int x;
x = func(3, 7);
}
The platform is Arch Linux i686.
It's alignment. I assumed for some reason that esp
would be aligned from the start, which it clearly isn't.
gcc
aligns stack frames to 16 bytes per default, which is what happened.
这篇关于堆栈分配,为什么多余的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!