堆栈比需要的多 8 个字节
stack get 8 byte more than its need
我用 c 写了一个非常简单的程序如下:
include<stdio.h>
#include<string.h>
main(int argc,char **argv)
{
char buffer[80];
getchar();
strcpy(buffer,argv[1]);
return 1;
}
当我拆解它时,它看起来像下面这样:
0x08048414 <+0>: push %ebp
0x08048415 <+1>: mov %esp,%ebp
0x08048417 <+3>: sub [=11=]x58,%esp
0x0804841a <+6>: call 0x8048320 <getchar@plt>
0x0804841f <+11>: mov 0xc(%ebp),%eax
0x08048422 <+14>: add [=11=]x4,%eax
0x08048425 <+17>: mov (%eax),%eax
0x08048427 <+19>: mov %eax,0x4(%esp)
0x0804842b <+23>: lea -0x50(%ebp),%eax
0x0804842e <+26>: mov %eax,(%esp)
0x08048431 <+29>: call 0x8048330 <strcpy@plt>
0x08048436 <+34>: mov [=11=]x1,%eax
0x0804843b <+39>: leave
0x0804843c <+40>: ret
我的问题是为什么它低于 %esp 的 $0x58,而不是 $0x50?
我 运行 程序以 80 "A" 作为参数
这是 strcpy(buffer,argv[1]); 之后堆栈的结果;
x/24xw $esp
0xbffff290: 0xbffff298 0xbffff51e 0x41414141 0x41414141
0xbffff2a0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff2b0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff2c0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff2d0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff2e0: 0x41414141 0x41414141 0x00000000 0xb7e3f4d3
额外的 8 个字节用于 strcpy
的参数。出于某种原因,您的编译器选择在堆栈上预分配它们,并使用 esp
作为索引寄存器直接存储它们的值,而不是通常使用 push
指令的方式。
因此,如果指令 mov %eax,0x4(%esp)
被替换为 push %eax
并且指令 mov %eax,(%esp)
也被替换为 [=14= ,编译器只能在堆栈上分配 80 个字节].
我用 c 写了一个非常简单的程序如下:
include<stdio.h>
#include<string.h>
main(int argc,char **argv)
{
char buffer[80];
getchar();
strcpy(buffer,argv[1]);
return 1;
}
当我拆解它时,它看起来像下面这样:
0x08048414 <+0>: push %ebp
0x08048415 <+1>: mov %esp,%ebp
0x08048417 <+3>: sub [=11=]x58,%esp
0x0804841a <+6>: call 0x8048320 <getchar@plt>
0x0804841f <+11>: mov 0xc(%ebp),%eax
0x08048422 <+14>: add [=11=]x4,%eax
0x08048425 <+17>: mov (%eax),%eax
0x08048427 <+19>: mov %eax,0x4(%esp)
0x0804842b <+23>: lea -0x50(%ebp),%eax
0x0804842e <+26>: mov %eax,(%esp)
0x08048431 <+29>: call 0x8048330 <strcpy@plt>
0x08048436 <+34>: mov [=11=]x1,%eax
0x0804843b <+39>: leave
0x0804843c <+40>: ret
我的问题是为什么它低于 %esp 的 $0x58,而不是 $0x50? 我 运行 程序以 80 "A" 作为参数 这是 strcpy(buffer,argv[1]); 之后堆栈的结果;
x/24xw $esp
0xbffff290: 0xbffff298 0xbffff51e 0x41414141 0x41414141
0xbffff2a0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff2b0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff2c0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff2d0: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff2e0: 0x41414141 0x41414141 0x00000000 0xb7e3f4d3
额外的 8 个字节用于 strcpy
的参数。出于某种原因,您的编译器选择在堆栈上预分配它们,并使用 esp
作为索引寄存器直接存储它们的值,而不是通常使用 push
指令的方式。
因此,如果指令 mov %eax,0x4(%esp)
被替换为 push %eax
并且指令 mov %eax,(%esp)
也被替换为 [=14= ,编译器只能在堆栈上分配 80 个字节].