手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

结合“hello world”探讨gcc编译程序的过程

[日期:2011-05-27] 来源:csdn  作者:阿波ID:livelylittlefish [字体:大 中 小]

3.3 汇编(assembly)

 

汇编阶段是把编译阶段生成的.s文件转成二进制目标代码。

 

gcc manual中对选项-C的解释如下。

`-c'

     Compile or assemble the source files, but do not link. The linking stage simply is not done.  The ultimate output is in the form of an object file for each source file.

     By default, the object file name for a source file is made by replacing the suffix `.c', `.i', `.s', etc., with `.o'. Unrecognized input files, not requiring compilation or assembly, are ignored.

     编译或汇编源文件,但不连接,编译器输出对应于源文件的目标文件。

     缺省情况下,GCC通过用'.o'替换源文件名后缀`.c', `.i', `.s'等等以产生目标文件名。可以使用-o选项指定选择其他名字。GCC忽略-c选项后面任何无法识别的不需要编译或汇编的输入文件。

 

gcc manual中对选项-V的解释如下。

`-v'

     Print (on standard error output) the commands executed to run the stages of compilation.  Also print the version number of the compiler driver program and of the preprocessor and the compiler proper.

    (在标准错误输出)显示执行编译阶段的命令,同时显示编译器驱动程序、预处理器、编译器的版本号。

 

格式:gcc –c hello.s –o hello.o

作用:将编译输出的.s文件汇编成二进制目标代码.o文件

 

我们可以通过objdump程序来查看hello.o的内容。

# objdump -d hello.o

 

hello.o:     file format elf32-i386

 

Disassembly of section .text:

 

00000000 <main>:

   0:   8d 4c 24 04             lea    0x4(%esp),%ecx

   4:   83 e4 f0                and    $0xfffffff0,%esp

   7:   ff 71 fc                pushl  -0x4(%ecx)

   a:   55                      push   %ebp

   b:   89 e5                   mov    %esp,%ebp

   d:   51                      push   %ecx

   e:   83 ec 14                sub    $0x14,%esp

  11:   c7 44 24 04 00 00 00    movl   $0x0,0x4(%esp)

  18:   00

  19:   c7 04 24 00 00 00 00    movl   $0x0,(%esp)

  20:   e8 fc ff ff ff          call   21 <main+0x21>

  25:   b8 00 00 00 00          mov    $0x0,%eax

  2a:   83 c4 14                add    $0x14,%esp

  2d:   59                      pop    %ecx

  2e:   5d                      pop    %ebp

  2f:   8d 61 fc                lea    -0x4(%ecx),%esp

  32:   c3                      ret   

 

3.4 链接(linking)

 

编译成功后,就进入链接。

格式:gcc hello.o -o hello.exe

  或者gcc hello.o -o hello

作用:将编译输出的.o文件链接为最终可执行文件。

 

同样,我们也可以通过objdump程序来查看链接后的输出hello文件的内容。

 

hello:     file format elf32-i386

 

Disassembly of section .init:

 

08048298 <_init>:

 8048298:        55                           push   %ebp

 8048299:        89 e5                        mov    %esp,%ebp

 804829b:        83 ec 08                     sub    $0x8,%esp

 804829e:        e8 71 00 00 00               call   8048314 <call_gmon_start>

 80482a3:        e8 f8 00 00 00               call   80483a0 <frame_dummy>

 80482a8:        e8 d3 01 00 00               call   8048480 <__do_global_ctors_aux>

 80482ad:        c9                           leave 

 80482ae:        c3                           ret   

Disassembly of section .plt:

 

080482b0 <__gmon_start__@plt-0x10>:

 80482b0:        ff 35 40 96 04 08            pushl  0x8049640

 80482b6:        ff 25 44 96 04 08            jmp    *0x8049644

 80482bc:        00 00                        add    %al,(%eax)

...

 

080482c0 <__gmon_start__@plt>:

 80482c0:        ff 25 48 96 04 08            jmp    *0x8049648

 80482c6:        68 00 00 00 00               push   $0x0

 80482cb:        e9 e0 ff ff ff               jmp    80482b0 <_init+0x18>

 

080482d0 <__libc_start_main@plt>:

 80482d0:        ff 25 4c 96 04 08            jmp    *0x804964c

 80482d6:        68 08 00 00 00               push   $0x8

 80482db:        e9 d0 ff ff ff               jmp    80482b0 <_init+0x18>

 

080482e0 <printf@plt>:

 80482e0:        ff 25 50 96 04 08            jmp    *0x8049650

 80482e6:        68 10 00 00 00               push   $0x10

 80482eb:        e9 c0 ff ff ff               jmp    80482b0 <_init+0x18>

 

...

 

080483c4 <main>:

 80483c4:        8d 4c 24 04                  lea    0x4(%esp),%ecx

 80483c8:        83 e4 f0                     and    $0xfffffff0,%esp

 80483cb:        ff 71 fc                     pushl  -0x4(%ecx)

 80483ce:        55                           push   %ebp

 80483cf:        89 e5                        mov    %esp,%ebp

 80483d1:        51                           push   %ecx

 80483d2:        83 ec 14                     sub    $0x14,%esp

 80483d5:        c7 44 24 04 e0 82 04         movl   $0x80482e0,0x4(%esp)

 80483dc:        08

 80483dd:        c7 04 24 d0 84 04 08         movl   $0x80484d0,(%esp)

 80483e4:        e8 f7 fe ff ff               call   80482e0 <printf@plt>

 80483e9:        b8 00 00 00 00               mov    $0x0,%eax

 80483ee:        83 c4 14                     add    $0x14,%esp

 80483f1:        59                           pop    %ecx

 80483f2:        5d                           pop    %ebp

 80483f3:        8d 61 fc                     lea    -0x4(%ecx),%esp

 80483f6:        c3                           ret   

 80483f7:        90                           nop   

 

...

 

4. 小结

 

编译程序的4个过程:

预处理:gcc -E hello.c -o hello.i

编译:gcc –S hello.i –o hello.s
汇编:gcc –c hello.s –o hello.o
链接:gcc hello.o -o hello

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款