简体   繁体   English

怀疑堆栈指针

[英]Doubts with stack pointer

Conside a program: 考虑一个程序:

#include <stdio.h>

void function(int a, int b, int c){
    char buffer1[5];
    char buffer2[10];
}

void main(){
        function(1,2,3);
}

compiling this with 用这个来编译
gcc test.c -m32 -g -o test -fno-stack-protector gcc test.c -m32 -g -o test -fno-stack-protector
and doing 并做
objdump -S test > test.dis objdump -S test> test.dis

I get the following dump for the function "function" 我获得了函数“function”的以下转储

void function(int a, int b, int c){
 80483ed:       55                      push   %ebp
 80483ee:       89 e5                   mov    %esp,%ebp
 80483f0:       83 ec 10                sub    $0x10,%esp
        char buffer1[5];
        char buffer2[10];
}

Consider another variant of the same program: 考虑同一程序的另一个变体:

#include <stdio.h>

void function(int a, int b, int c){
        char buffer1[7];
        char buffer2[10];
}

void main(){
        function(1,2,3);
}

on compiling and generating the dump with same commands i get: 在使用相同的命令编译和生成转储时,我得到:

void function(int a, int b, int c){
 80483ed:       55                      push   %ebp
 80483ee:       89 e5                   mov    %esp,%ebp
 80483f0:       83 ec 20                sub    $0x20,%esp
        char buffer1[7];
        char buffer2[10];
}

my question is what causes the stack pointer to be decremented by 16 in first case and 32 in second case while only 2 more bytes are required in 2nd case? 我的问题是什么原因导致堆栈指针在第一种情况下减少16而在第二种情况下减少32,而在第二种情况下只需要2个字节?

I am running 64 bit ubuntu 14.04 on Intel processor 我在英特尔处理器上运行64位ubuntu 14.04

From the GCC documentation : 海湾合作委员会文件

-mpreferred-stack-boundary = num -mpreferred-stack-boundary = num
Attempt to keep the stack boundary aligned to a 2 raised to num byte boundary. 尝试将堆栈边界保持对齐2到num字节边界。 If -mpreferred-stack-boundary is not specified, the default is 4 ( 16 bytes or 128 bits), except when optimizing for code size ( -Os ), in which case the default is the minimum correct alignment (4 bytes for x86, and 8 bytes for x86-64). 如果未指定-mpreferred-stack-boundary则默认值为 4( 16字节或128位),除非优化代码大小( -Os ),在这种情况下,默认值是最小正确对齐(x86为4个字节,和x86-64的8个字节)。

So as space is allocated on the stack for the arrays, the stack is also being aligned on the default boundary of 16 bytes. 因此,当堆栈上为数组分配空间时,堆栈也在16字节的默认边界上对齐。 In the first case you've got 15 bytes of data which is less than 16, so 16 bytes are allocated. 在第一种情况下,您有15个字节的数据小于16,因此分配了16个字节。 In the second case you've got 17 bytes of data which is more than 16 but less than (the next multiple of 16) 32, so 32 bytes of space is allocated. 在第二种情况下,你有17个字节的数据超过16但小于(16的下一个数字)32,所以分配了32个字节的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM