日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux 文件格式elf,linux ELF 文件格式 | ZION

發布時間:2023/12/14 linux 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 文件格式elf,linux ELF 文件格式 | ZION 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ELF 文件類型

ELF (Executable Linkable Format) 是linux下的可執行文件格式,與windows下的PE (Portable Executable) 格式一樣,都是COFF (Common File Format)文件格式的變種。在linux下除了可執行文件,編譯過程中產生的目標文件(.o 文件),動態鏈接文件(.so文件),靜態鏈接庫文件(.a 文件) ,核心轉儲文件(Core Dump File)都按照 ELF 格式存儲。查看ELF文件類型可以用file命令

[code language="bash"]

$ file /lib/libc-2.11.2.so

/lib/libc-2.11.2.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

[/code]

ELF 文件結構

ELF文件結構在 /usr/include/elf.h 中有完整定義

一些有用的命令:

file elf_ile 輸出ELF各個段的size

readelf [options] elf_file

-S 輸出 section header

-t 輸出符號表 symbol table

-h 輸出ELF文件頭信息

objdump [options] elf_file

-h 輸出文件基本ELF信息

-d 反匯編代碼段

-s 輸出完整內容

-r 查看重定位表

-D 反匯編所有段內容

-S 輸出內容包含源代碼(需要gcc -g 參數支持)

ELF結構中比較重要的幾個段:

.data 數據段,存放已經初始化的全局/靜態變量

.bss(Block Started by Symbol) 存放未初始化的全局變量和靜態變量,因為這些變量

在程序加載的時候都會被初始化為零,所以不需要存放實際的數據,只需要預留位置

就可以了。

.text 代碼段,存放源代碼編譯后的機器指令

.rodata 只讀數據段,存放只讀變量

.symtab(Symbol Table) 符號表

.strtab(String Table) 字符串表

.plt(Procedure Linkage Table) 動態鏈接跳轉表

.got(Global Offset Table) 動態鏈接全局入口表

動態鏈接和靜態鏈接

在靜態鏈接過程中,編譯器將需要重定位的符號寫在ELF結構中的重定位表(Relocation Table)內,之后鏈接器(Linker)分析重定位表,從全局符號表中找到相應符號的地址,完成重定位

如果希望某個代碼文件生成的對象文件可以被動態的鏈接,需要在編譯時給GCC指定 -fPIC 參數,PIC(Position Independent Code)即位置無關代碼

[code language="C"]

/***** echo.c *******/

#include

extern int global_variable;

int echo(){

printf("%dn",global_variable);

return 0;

}

[/code]

[code language="C"]

/******* main.c *******/

#include

extern int echo();

int global_variable = 0;

int main() {

echo();

return 0;

}

[/code]

[code language="bash"]

$ g++ -fPIC -shared echo.cc -o libecho.so

$ g++ -c main.cc -o main.o

$ g++ main.o -o dynamic -lecho -L.

$ g++ main.cc echo.cc -o static

$ objdump -d main.o

00000000 :

0: 55 push %ebp

1: 89 e5 mov %esp,%ebp

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

6: e8 fc ff ff ff call 7

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

10: 89 ec mov %ebp,%esp

12: 5d pop %ebp

13: c3 ret

$ objdump -d dynamic

08048584 :

8048584: 55 push %ebp

8048585: 89 e5 mov %esp,%ebp

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

804858a: e8 11 ff ff ff call 80484a0

804858f: b8 00 00 00 00 mov $0x0,%eax

8048594: 89 ec mov %ebp,%esp

8048596: 5d pop %ebp

8048597: c3 ret

8048598: 90 nop

$ objdump -d static

080484c8 :

80484c8: 55 push %ebp

80484c9: 89 e5 mov %esp,%ebp

80484cb: 83 e4 f0 and $0xfffffff0,%esp

80484ce: e8 d1 ff ff ff call 80484a4

80484d3: b8 00 00 00 00 mov $0x0,%eax

80484d8: 89 ec mov %ebp,%esp

80484da: 5d pop %ebp

80484db: c3 ret

80484dc: 90 nop

PIC sample

$ g++ echo.cc -shared -o libecho.so

0000051c :

51c: 55 push %ebp

51d: 89 e5 mov %esp,%ebp

51f: 83 ec 18 sub $0x18,%esp

522: a1 00 00 00 00 mov 0x0,%eax

527: 89 44 24 04 mov %eax,0x4(%esp)

52b: c7 04 24 94 05 00 00 movl $0x594,(%esp)

532: e8 fc ff ff ff call 533

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

53c: c9 leave

53d: c3 ret

53e: 90 nop

53f: 90 nop

$ g++ echo.cc -shared -fPIC -o libecho.so

0000052c :

52c: 55 push %ebp

52d: 89 e5 mov %esp,%ebp

52f: 53 push %ebx

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

533: e8 ef ff ff ff call 527 <__i686.get_pc_thunk.bx>

538: 81 c3 e4 11 00 00 add $0x11e4,%ebx

53e: 8b 83 fc ff ff ff mov -0x4(%ebx),%eax

544: 8b 00 mov (%eax),%eax

546: 89 44 24 04 mov %eax,0x4(%esp)

54a: 8d 83 a8 ee ff ff lea -0x1158(%ebx),%eax

550: 89 04 24 mov %eax,(%esp)

553: e8 f0 fe ff ff call 448

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

55d: 83 c4 14 add $0x14,%esp

560: 5b pop %ebx

561: 5d pop %ebp

[/code]

GOT 和 PLT

[code language="C"]

#include

void foo() {

printf("test 0x%06xn", 10);

return;

}

int main () {

foo();

return 0;

}

[/code]

編譯后查看ELF的信息

[code language="bash"]

gcc -g test.c -o test

objdump -S test

[/code]

以下為部分段的內容:

[code language="C"]

080482cc :

80482cc: ff 35 c8 95 04 08 pushl 0x80495c8

80482d2: ff 25 cc 95 04 08 jmp *0x80495c

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

080482fc :

80482fc: ff 25 d8 95 04 08 jmp *0x80495d8

8048302: 68 10 00 00 00 push $0x10

8048307: e9 c0 ff ff ff jmp 80482cc

int main () {

80483f0: 55 push %ebp

80483f1: 89 e5 mov %esp,%ebp

80483f3: 83 ec 08 sub $0x8,%esp

80483f6: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp)

foo();

80483fd: e8 ce ff ff ff call 80483d0

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

return 0;

8048407: 83 c4 08 add $0x8,%esp

804840a: 5d pop %ebp

804840b: c3 ret

void foo() {

80483d0: 55 push %ebp

80483d1: 89 e5 mov %esp,%ebp

80483d3: 83 ec 08 sub $0x8,%esp

80483d6: 8d 05 d0 84 04 08 lea 0x80484d0,%eax

printf("testn");

80483dc: 89 04 24 mov %eax,(%esp)

80483df: e8 18 ff ff ff call 80482fc

return;

80483e4: 89 45 fc mov %eax,-0x4(%ebp)

80483e7: 83 c4 08 add $0x8,%esp

80483ea: 5d pop %ebp

80483eb: c3 ret

80483ec: 0f 1f 40 00 nopl 0x0(%eax)

[/code]

總結

以上是生活随笔為你收集整理的linux 文件格式elf,linux ELF 文件格式 | ZION的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。