zer*_*ola 3 compiler-construction printf gcc gdb
当我的程序在gdb中被反汇编时,我可以看到buf的地址被压入堆栈,但是我没有看到格式字符串被压到它上面.这是什么原因?它是一个聪明的编译器优化?
我已经尝试编译一些printf语句的不同变体,看看我是否可以模仿"%s"字符串(或它的地址)没有被压入堆栈,但我无法做到.
这是程序代码:
int main(int argc, char **argv) {
char buf[128];
if(argc < 2) return 1;
strcpy(buf, argv[1]);
printf("%s\n", buf);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用gcc 4.5.2编译,32位linux
是的,似乎gcc将丢弃"printf("%s \n",buff)"并将"puts()"替换为其位置:
Run Code Online (Sandbox Code Playgroud)vi tmp.c =>
#include <stdio.h>
#include <string.h>
int
main(int argc, char **argv)
{
char buf[128];
if(argc < 2)
return 1;
strcpy(buf, argv[1]);
printf("%s\n", buf);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ gcc -S -Wall -pedantic tmp.c less tmp.s =>
.file "tmp.c"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $148, %esp
movl %ecx, -140(%ebp)
movl -140(%ebp), %eax
cmpl $1, (%eax)
jg .L2
movl $1, -136(%ebp)
jmp .L4
.L2:
movl -140(%ebp), %edx
movl 4(%edx), %eax
addl $4, %eax
movl (%eax), %eax
movl %eax, 4(%esp)
leal -132(%ebp), %eax
movl %eax, (%esp)
call strcpy
leal -132(%ebp), %eax
movl %eax, (%esp)
call puts
movl $0, -136(%ebp)
.L4:
movl -136(%ebp), %eax
addl $148, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-48)"
.section .note.GNU-stack,"",@progbits
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
383 次 |
最近记录: |