ELF之學習心得01

来源:互联网 发布:九宫纳甲排盘软件 编辑:程序博客网 时间:2024/06/06 10:05
目前Linux的主要的可執行檔格式是ELF(Executable and Linking Format),ELF為COFF格式的後繼者,主要特徵是可擁有多個section,並且有32-bit與64-bit的數值用以區別其格式屬於32-bit或是64-bit。主要缺點是ELF設計時有一個假設,每個系統只會有一個ABI(Application Binary Interface),但是事實上這是錯的,如SYSV至少就有SVR、Solaris、SCO等ABI。(詳細內容請閱讀參考資料)

    ELF主要有三種類型的object files:
  • A relocatable file holds code and data suitable for linking with other object files to create an executable or a shared object file.
  • An executable file holds a program suitable for execution.
  • A shared object file holds code and data suitable for linking in two contexts. First, the link editor may process it with other relocatable and shared object files to create another object file. Second, the dynamic linker combines it with an executable file and other shared objects to create a process image.


這是ELF的layout,所謂的Linking View是指以檔案呈現之ELF(左圖),而Execution View則是指被載入到RAM上執行的ELF(右圖)。

    主要的section包含
  1. .text,存放程式碼的區域。
  2. .data用於存放已經初始化的變數。
  3. .bss用於存放未初始化的變數或者內容初始化為0的,該區域不占檔案空間。
  4. .rodata用於存放read-only data。
  5. 其他section和使用者自訂section後面再慢慢介紹。
這些以"."開頭的section為系統保留之section,使用者可以自訂section,但是應該避免使用"."開頭。
#include <stdio.h>int i0 = 0;int i1 = 1;static int si0 = 0;static int si1 = 1;const int ci0 = 0;const int ci1 = 1;const static int csi0 = 0;const static int csi1 = 1;int main(void){    return 0;}


objdump -x a.out SYMBOL TABLE:0000000000601034 l   O .bss     0000000000000004  si0000000000060101c l   O .data    0000000000000004  si100000000004005b4 l   O .rodata  0000000000000004  csi000000000004005b8 l   O .rodata  0000000000000004  csi100000000004005b0 g   O .rodata  0000000000000004  ci10000000000601030 g   O .bss     0000000000000004  i000000000004005ac g   O .rodata  0000000000000004  ci00000000000601018 g   O .data    0000000000000004  i1

根據前面的規則用const修飾的變數會被放置在.rodata中,有ci0、ci1、csi0、csi1。未初始化的變數或者內容初始化為0的都會被放置在.bss中,有i0、si0。已經初始化的變數則放在.data中,有i1、si1。 


转自:http://nano-chicken.blogspot.com/2011/06/elf01.html

ELF之學習心得01

ELF之學習心得01