- 论坛徽章:
- 0
|
上面的错了,理论上会覆盖返回地址,但是gcc的编译器通常会自作多情。比如
#include <stdio.h>
void foo(void)
{
char b[5];
strcpy(b,"Hello,Aix");
printf("b is %s",b);
}
int main(int argc, char *argv[])
{
foo();
return 0;
}
对应的汇编是:
.file "a.c"
.text
LC0:
.ascii "Hello,Aix\0"
LC1:
.ascii "b is %s\0"
.globl _foo
.def _foo; .scl 2; .type 32; .endef
_foo:
pushl %ebp
movl %esp, %ebp
subl $24, %esp;b只有5个字节却要分给他24个字节的保留空间;而其当我把5改成25后,这里的24变成了40
subl $8, %esp
pushl $LC0
leal -24(%ebp), %eax
pushl %eax
call _strcpy
addl $16, %esp
subl $8, %esp
leal -24(%ebp), %eax
pushl %eax
pushl $LC1
call _printf
addl $16, %esp
leave
ret
.def ___main; .scl 2; .type 32; .endef
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
call __alloca
call ___main
call _foo
movl $0, %eax
leave
ret
.def _printf; .scl 2; .type 32; .endef
.def _strcpy; .scl 2; .type 32; .endef
因为栈sp指针的东西总是要是4的倍数的,所以我把第12行的$24改成了$8之后,编译这个汇编文件的到的可执行文件输出是:
C:\EP_Backup>a
b is ?@
C:\EP_Backup>
我实在WindowsXP 上用MingW带的gcc编译的 |
|