Linux下的ELF文件格式簡介

 發佈時間:2002.01.26 12:20     來源:開放系統世界    作者:杜昆
              
            
            
              
               
                  
                    
                                       
                               
              
              1. 概述
Executable and linking format(ELF)文件是x86 Linux系統下的一種常用目標文件(object file)格式,有三種主要類型:
(1)適於連接的可重定位文件(relocatable file),可與其它目標文件一起創建可執行文件和共享目標文件。
(2)適於執行的可執行文件(executable file),用於提供程序的進程映像,加載的內存執行。
(3)共享目標文件(shared object file),連接器可將它與其它可重定位文件和共享目標文件連接成其它的目標文件,動態連接器又可將它與可執行文件和其它共享目標文件結合起來創建一個進程映像。
ELF文件格式比較複雜,本文只是簡要介紹它的結構,希望能給想了解ELF文件結構的讀者以幫助。具體詳盡的資料請參閱專門的ELF文檔。
2. 文件格式
爲了方便和高效,ELF文件內容有兩個平行的視角:一個是程序連接角度,另一個是程序運行角度,如圖1所示。
ELF
header在文件開始處描述了整個文件的組織,Section提供了目標文件的各項信息(如指令、數據、符號表、重定位信息等),Program
header table指出怎樣創建進程映像,含有每個program header的入口,Section header
table包含每一個section的入口,給出名字、大小等信息。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://tech.ccidnet.com/col/attachment/2002/1/9048.jpg" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>
圖1               
3. 數據表示
ELF數據編碼順序與機器相關,數據類型有六種,見表1。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://tech.ccidnet.com/col/attachment/2002/1/9049.jpg" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>
4. ELF文件頭
象bmp、exe等文件一樣,ELF的文件頭包含整個文件的控制結構。它的定義如下:

   
#define EI_NIDENT 16
typedef struct{
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
}Elf32_Ehdr;
   
  
其中E_ident的16個字節標明是個ELF文件
(7F+'E'+'L'+'F'+class
+data+version+pad)。E_type表示文件類型,2表示可執行文件。E_machine說明機器類別,3表示386機器,8表示
MIPS機器。E_entry給出進程開始的虛地址,即系統將控制轉移的位置。E_phoff指出program header
table的文件偏移,e_phentsize表示一個program
header表中的入口的長度(字節數表示),e_phnum給出program
header表中的入口數目。類似的,e_shoff,e_shentsize,e_shnum 分別表示section
header表的文件偏移,表中每個入口的的字節數和入口數目。E_flags給出與處理器相關的標誌,e_ehsize給出ELF文件頭的長度(字節數
表示)。E_shstrndx表示section名錶的位置,指出在section header表中的索引。
下面有個elf文件頭的例子,可以對照理解,見圖2。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://tech.ccidnet.com/col/attachment/2002/1/9050.jpg" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>
圖2
5. section header
目標文件的section header table可以定位所有的section,它是一個Elf32_Shdr結構的數組,Section頭表的索引是這個數組的下標。有些索引號是保留的,目標文件不能使用這些特殊的索引。
Section包含目標文件除了ELF文件頭、程序頭表、section頭表的所有信息,而且目標文件section滿足幾個條件:
(1)目標文件中的每個section都只有一個section頭項描述,可以存在不指示任何section的section頭項。
(2)每個section在文件中佔據一塊連續的空間。
(3)Section之間不可重疊。
(4)目標文件可以有非活動空間,各種headers和sections沒有覆蓋目標文件的每一個字節,這些非活動空間是沒有定義的。
Section header結構定義如下:

   
typedef struct{
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
}Elf32_Shdr;
   
  
其中sh_name指出section的名字,它的值是後面將會講到的
section header string
table中的索引,指出一個以null結尾的字符串。Sh_type是類別,sh_flags指示該section在進程執行時的特性。Sh_addr
指出若此section在進程的內存映像中出現,則給出開始的虛地址。Sh_offset給出此section在文件中的偏移。其它字段的意義不太常用,
在此不細述。
文件的section含有程序和控制信息,系統使用一些特定的section,並
有其固定的類型和屬性(由sh_type和sh_info指出)。下面介紹幾個常用到的section:“.bss”段含有佔據程序內存映像的未初始化數
據,當程序開始運行時系統對這段數據初始爲零,但這個section並不佔文件空間。“.data.”和“data1”段包含佔據內存映像的初始化數據。
“.rodata”和“.rodata1”段含程序映像中的只讀數據。“.shstrtab”段含有每個section的名字,由section入口結構
中的sh_name索引。“.strtab”段含有表示符號表(symbol
table)名字的字符串。“.symtab”段含有文件的符號表,在後文專門介紹。“.text”段包含程序的可執行指令。
6. symbol table
目標文件的符號表包含定位或重定位程序符號定義和引用時所需要的信息。符號表入口結構定義如下:

   
typedef struct{
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Word st_size;
Unsigned char st_info;
Unsigned char st_other;
Elf32_Half st_shndx;
}Elf32_Sym;
   
  
其中st_name包含指向符號表字符串表(strtab)中的索引,從而可以獲得
符號名。St_value指出符號的值,可能是一個絕對值、地址等。St_size指出符號相關的內存大小,比如一個數據結構包含的字節數等。
St_info規定了符號的類型和綁定屬性,指出這個符號是一個數據名、函數名、section名還是源文件名;並且指出該符號的綁定屬性是local、
global還是weak。
發佈了58 篇原創文章 · 獲贊 0 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章