GCC

来源:互联网 发布:淘宝如何部分商品退款 编辑:程序博客网 时间:2024/06/06 03:15

先编写一段程序,以hello.c存储


最一般的gcc用法如下

$ gcc hello.c -o hello

$ ./hello

如果我们分布执行,输入命令

预处理

gcc -E hello.c -o hello.i

预处理的结果是进行宏替换,将include的头文件全部拷贝到该文件中。所以自己写的程序应该位于新生成的文件末尾

.....................extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));# 943 "/usr/include/stdio.h" 3 4# 2 "hello.c" 2int main(){ printf("hello,world!"); return 0;}



编译

gcc –S hello.i –o hello.s

编译后的结果如下

        .file   "hello.c"        .section        .rodata     ;;表示程序分段,下面属于只读数据段                                 .LC0:        .string "hello,world!"        .text        .globl  main        .type   main, @functionmain:.LFB0:        .cfi_startproc        pushl   %ebp        .cfi_def_cfa_offset 8        .cfi_offset 5, -8        movl    %esp, %ebp        .cfi_def_cfa_register 5        andl    $-16, %esp        subl    $16, %esp        movl    $.LC0, (%esp)        call    printf        movl    $0, %eax        leave        .cfi_restore 5        .cfi_def_cfa 4, 4        ret        .cfi_endproc.LFE0:        .size   main, .-main        .ident  "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"        .section        .note.GNU-stack,"",@progbits


汇编

gcc –c hello.c –o hello.o

链接

ld –static crt1.o crti.o crtbeginT.ohello.o –start-group –lgcc –lgcc_eh –lc-end-group crtend.o crtn.o 

(注意省略了文件路径名)


其中,以.开头的语句不会被翻译成机器语言,他们是给汇编器的指示。(//后面的注释是手工添加的)

上述所输出的可执行文件,和.o文件,都属于ELF文件。推荐使用readelf(linux自带)进行分析。

objdump(linux自带)也可以,而且可以进行反汇编。



原创粉丝点击