编译器的原理

一、机器语言

  机器语言(machine language)是一种指令集的体系。这种指令集,称机器码(machine code),是电脑的CPU可直接解读的数据。

  可以理解为机器语言就是二进制

二、源代码

  源代码示例源程序(source code)是指未编译的按照一定的程序设计语言规范书写的文本文件。 源代码(也称源程序),是指一系列人类可读的计算机语言指令

三、预处理器

  预处理器是在真正的编译开始之前由编译器调用的独立程序。预处理器可以删除注释包含其他文件以及执行宏(宏macro是一段重复文字的简短描写)替代。预处理器可由语言(如C)要求或以后作为提供额外功能(诸如为FORTRAN提供Ratfor预处理器)的附加软件。

四、目标代码(目标文件)

  1)指源代码经编译后,产生的能被 CPU直接识别的二进制代码

  2)

目标代码(objectcode)指计算机科学中编译器或汇编器处理源代码后所生成的代码,它一般由机器代码或接近于机器语言的代码组成。[1]目标文件(objectfile)即存放目标代码的计算机文件,它常被称作二进制文件(binaries)。
目标文件包含着机器代码(可直接被计算机中央处理器执行)以及代码在运行时使用的数据,如重定位信息,如用于链接或调试的程序符号(变量和函数的名字),此外还包括其他调试信息。[2]目标文件是从源代码文件产生程序文件这一过程的中间产物,链接器正是通过把目标文件链接在一起来生成可执行文件或库文件。目标文件中唯一的要素是机器代码,例如,用于嵌入式系统的目标文件可能仅仅含有机器代码。

 

五、链接器

 

链接器(Linker)是一个程序,将一个或多个由编译器或汇编器生成的目标文件外加库链接为一个可执行文件
目标文件是包括机器码和链接器可用信息的程序模块。简单的讲,链接器的工作就是解析未定义的符号引用,将目标文件中的占位符替换为符号的地址。链接器还要完成程序中各目标文件的地址空间的组织,这可能涉及重定位工作。

 六、符号表

1)GCC命令之 objdump

objdump是用查看目标文件或者可执行的目标文件的构成的GCC工具

以下3条命令足够那些喜欢探索目标文件与源代码之间的丝丝的关系的朋友。
objdump   -x   obj 以某种分类信息的形式把目标文件的数据组织(被分为几大块)输出 <可查到该文件的所有动态库>  
objdump   -t   obj 输出目标文件的符号表()
objdump   -h   obj 输出目标文件的所有段概括()

2)objdump例子

代码ztao.cpp

  1 #include<iostream>
  2 int main()
  3 {
  4         std::cout<<"ztao"<<std::endl;
  5         return 0;
  6 }

符号表

[ztteng@ztteng 002]$ objdump -t ztao

ztao:     file format elf32-i386

SYMBOL TABLE:
08048134 l    d  .interp        00000000              .interp
08048148 l    d  .note.ABI-tag  00000000              .note.ABI-tag
08048168 l    d  .note.gnu.build-id     00000000              .note.gnu.build-id
0804818c l    d  .gnu.hash      00000000              .gnu.hash
080481c0 l    d  .dynsym        00000000              .dynsym
08048290 l    d  .dynstr        00000000              .dynstr
08048414 l    d  .gnu.version   00000000              .gnu.version
08048430 l    d  .gnu.version_r 00000000              .gnu.version_r
08048490 l    d  .rel.dyn       00000000              .rel.dyn
080484a0 l    d  .rel.plt       00000000              .rel.plt
080484e8 l    d  .init  00000000              .init
08048518 l    d  .plt   00000000              .plt
080485c0 l    d  .text  00000000              .text
080487ac l    d  .fini  00000000              .fini
080487c8 l    d  .rodata        00000000              .rodata
080487dc l    d  .eh_frame_hdr  00000000              .eh_frame_hdr
08048818 l    d  .eh_frame      00000000              .eh_frame
08049910 l    d  .ctors 00000000              .ctors
0804991c l    d  .dtors 00000000              .dtors
08049924 l    d  .jcr   00000000              .jcr
08049928 l    d  .dynamic       00000000              .dynamic
08049a08 l    d  .got   00000000              .got
08049a0c l    d  .got.plt       00000000              .got.plt
08049a3c l    d  .data  00000000              .data
08049a40 l    d  .bss   00000000              .bss
00000000 l    d  .comment       00000000              .comment
00000000 l    d  .debug_aranges 00000000              .debug_aranges
00000000 l    d  .debug_pubnames        00000000              .debug_pubnames
00000000 l    d  .debug_info    00000000              .debug_info
00000000 l    d  .debug_abbrev  00000000              .debug_abbrev
00000000 l    d  .debug_line    00000000              .debug_line
00000000 l    d  .debug_str     00000000              .debug_str
00000000 l    d  .debug_pubtypes        00000000              .debug_pubtypes
00000000 l    df *ABS*  00000000              crtstuff.c
08049910 l     O .ctors 00000000              __CTOR_LIST__
0804991c l     O .dtors 00000000              __DTOR_LIST__
08049924 l     O .jcr   00000000              __JCR_LIST__
080485f0 l     F .text  00000000              __do_global_dtors_aux
08049acc l     O .bss   00000001              completed.5963
08049ad0 l     O .bss   00000004              dtor_idx.5965
08048650 l     F .text  00000000              frame_dummy
00000000 l    df *ABS*  00000000              crtstuff.c
08049918 l     O .ctors 00000000              __CTOR_END__
0804890c l     O .eh_frame      00000000              __FRAME_END__
08049924 l     O .jcr   00000000              __JCR_END__
08048780 l     F .text  00000000              __do_global_ctors_aux
00000000 l    df *ABS*  00000000              ztao.cpp
08049ad4 l     O .bss   00000001              _ZStL8__ioinit
080486a8 l     F .text  00000040              _Z41__static_initialization_and_destruction_0ii
080486e8 l     F .text  0000001c              _GLOBAL__I_main
08049a0c l     O .got.plt       00000000              _GLOBAL_OFFSET_TABLE_
08049910 l       .ctors 00000000              __init_array_end
08049910 l       .ctors 00000000              __init_array_start
08049928 l     O .dynamic       00000000              _DYNAMIC
08049a3c  w      .data  00000000              data_start
00000000       F *UND*  00000000              __cxa_atexit@@GLIBC_2.1.3
08048710 g     F .text  00000005              __libc_csu_fini
080485c0 g     F .text  00000000              _start
00000000  w      *UND*  00000000              __gmon_start__
00000000  w      *UND*  00000000              _Jv_RegisterClasses
080487c8 g     O .rodata        00000004              _fp_hw
080487ac g     F .fini  00000000              _fini
00000000       F *UND*  00000000              _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4
00000000       F *UND*  00000000              __libc_start_main@@GLIBC_2.0
08048568       F *UND*  00000000              _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4
00000000       F *UND*  00000000              _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4
080487cc g     O .rodata        00000004              _IO_stdin_used
08049a3c g       .data  00000000              __data_start
08049a40 g     O .bss   0000008c              _ZSt4cout@@GLIBCXX_3.4
080487d0 g     O .rodata        00000000              .hidden __dso_handle
08049920 g     O .dtors 00000000              .hidden __DTOR_END__
08048720 g     F .text  0000005a              __libc_csu_init
08049a40 g       *ABS*  00000000              __bss_start
08049ad8 g       *ABS*  00000000              _end
00000000       F *UND*  00000000              _ZNSolsEPFRSoS_E@@GLIBCXX_3.4
08048598       F *UND*  00000000              _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4
08049a40 g       *ABS*  00000000              _edata
080485a8       F *UND*  00000000              __gxx_personality_v0@@CXXABI_1.3
0804877a g     F .text  00000000              .hidden __i686.get_pc_thunk.bx
08048674 g     F .text  00000034              main
080484e8 g     F .init  00000000              _init

 

 七、编译器

  简单讲,编译器就是将“高级语言”翻译为“机器语言(低级语言)”的程序。一个现代编译器的主要工作流程:源代码 (source code) → 预处理器 (preprocessor) → 编译器 (compiler) → 目标代码 (object code) → 链接器 (Linker) → 可执行程序 (executables)

  译器将汇编或高级计算机语言源程序(Source program)作为输入,翻译成目标语言(Target language)机器代码的等价程序

 

 

 

 

 

八、C语言程序编译的内存分配:

 

1.栈区(stack) --编译器自动分配释放,主要存放函数的参数值,局部变量值等;

 

2.堆区(heap)  --由程序员分配释放;

 

3.全局区或静态区 --存放全局变量和静态变量;程序结束时由系统释放,分为全局初始化区和全局未初始化区;

 

4.字符常量区    --常量字符串放与此,程序结束时由系统释放;

 

5.程序代码区

 

posted @ 2013-11-09 09:37  z折腾  阅读(1437)  评论(0编辑  收藏  举报