计算机系统程序链接。。。

本文详细介绍了连接器在程序链接过程中的作用,包括如何处理未定义的变量、合并相同的段以及重定位变量。此外,还探讨了可执行文件在运行时动态链接库的工作原理。同时,通过readelf命令的实例解析,展示了如何分析ELF文件的头部信息、节区、动态链接等关键部分,帮助读者深入理解程序链接和装载过程。
摘要由CSDN通过智能技术生成

程序的链接过程:
一 连接器的基本功能
1 对各个目标模块中没有定义的变量,在其它目标文件中找到相关的定义
2 把不同目标文件中生成的相同类型的段进行合并
3 把不同目标文件中的变量进行地质重定位
二 可执行文件的装载动态链接库:
程序在运行的时候才去定位这个库,并且把这个库链接到进程的虚拟地质空间。对于某一个动态链接库而言,所有使用这个库的可执行文件都共享同一块物理地址空间,该物理地址空间在当前动态链接库第一次被链接时加载到内存中。
静态链接库:将不同的可重定位模块打包成一个文件,在链接的时候会自动从这个文件中抽取用到的模块。

计算机系统基础第四章程序的链接
readelf命令用法解析:
readelf的命令参数

readelf使用实例–helloword.

root@localhost test]# readelf -a helloworld
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2’s complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x8048310
Start of program headers: 52 (bytes into file)
Start of section headers: 2096 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 8
Size of section headers: 40 (bytes)
Number of section headers: 30
Section header string table index: 27
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .interp PROGBITS 08048134 000134 000013 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 08048148 000148 000020 00 A 0 0 4
[ 3] .note.gnu.build-i NOTE 08048168 000168 000024 00 A 0 0 4
[ 4] .gnu.hash GNU_HASH 0804818c 00018c 000020 04 A 5 0 4
[ 5] .dynsym DYNSYM 080481ac 0001ac 000050 10 A 6 1 4
[ 6] .dynstr STRTAB 080481fc 0001fc 00004c 00 A 0 0 1
[ 7] .gnu.version VERSYM 08048248 000248 00000a 02 A 5 0 2
[ 8] .gnu.version_r VERNEED 08048254 000254 000020 00 A 6 1 4
[ 9] .rel.dyn REL 08048274 000274 000008 08 A 5 0 4
[10] .rel.plt REL 0804827c 00027c 000018 08 A 5 12 4
[11] .init PROGBITS 08048294 000294 000030 00 AX 0 0 4
[12] .plt PROGBITS 080482c4 0002c4 000040 04 AX 0 0 4
[13] .text PROGBITS 08048310 000310 00016c 00 AX 0 0 16
[14] .fini PROGBITS 0804847c 00047c 00001c 00 AX 0 0 4
[15] .rodata PROGBITS 08048498 000498 000019 00 A 0 0 4
[16] .eh_frame_hdr PROGBITS 080484b4 0004b4 00001c 00 A 0 0 4
[17] .eh_frame PROGBITS 080484d0 0004d0 000058 00 A 0 0 4
[18] .ctors PROGBITS 08049528 000528 000008 00 WA 0 0 4
[19] .dtors PROGBITS 08049530 000530 000008 00 WA 0 0 4
[20] .jcr PROGBITS 08049538 000538 000004 00 WA 0 0 4
[21] .dynamic DYNAMIC 0804953c 00053c 0000c8 08 WA 6 0 4
[22] .got PROGBITS 08049604 000604 000004 04 WA 0 0 4
[23] .got.plt PROGBITS 08049608 000608 000018 04 WA 0 0 4
[24] .data PROGBITS 08049620 000620 000004 00 WA 0 0 4
[25] .bss NOBITS 08049624 000624 000008 00 WA 0 0 4
[26] .comment PROGBITS 00000000 000624 00010e 00 0 0 1
[27] .shstrtab STRTAB 00000000 000732 0000fc 00 0 0 1
[28] .symtab SYMTAB 00000000 000ce0 000410 10 29 45 4
[29] .strtab STRTAB 00000000 0010f0 000202 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
There are no section groups in this file.
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x08048034 0x08048034 0x00100 0x00100 R E 0x4
INTERP 0x000134 0x08048134 0x08048134 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x000000 0x08048000 0x08048000 0x00528 0x00528 R E 0x1000
LOAD 0x000528 0x08049528 0x08049528 0x000fc 0x00104 RW 0x1000
DYNAMIC 0x00053c 0x0804953c 0x0804953c 0x000c8 0x000c8 RW 0x4
NOTE 0x000148 0x08048148 0x08048148 0x00044 0x00044 R 0x4
GNU_EH_FRAME 0x0004b4 0x080484b4 0x080484b4 0x0001c 0x0001c R 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4
Section to Segment mapping:
Segment Sections…
00
01 .interp
02 .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame
03 .ctors .dtors .jcr .dynamic .got .got.plt .data .bss
04 .dynamic
05 .note.ABI-tag .note.gnu.build-id
06 .eh_frame_hdr
07
Dynamic section at offset 0x53c contains 20 entries:
Tag Type Name/Value
0x00000001 (NEEDED) Shared library: [libc.so.6]
0x0000000c (INIT) 0x8048294
0x0000000d (FINI) 0x804847c
0x6ffffef5 (GNU_HASH) 0x804818c
0x00000005 (STRTAB) 0x80481fc
0x00000006 (SYMTAB) 0x80481ac
0x0000000a (STRSZ) 76 (bytes)
0x0000000b (SYMENT) 16 (bytes)
0x00000015 (DEBUG) 0x0
0x00000003 (PLTGOT) 0x8049608
0x00000002 (PLTRELSZ) 24 (bytes)
0x00000014 (PLTREL) REL
0x00000017 (JMPREL) 0x804827c
0x00000011 (REL) 0x8048274
0x00000012 (RELSZ) 8 (bytes)
0x00000013 (RELENT) 8 (bytes)
0x6ffffffe (VERNEED) 0x8048254
0x6fffffff (VERNEEDNUM) 1
0x6ffffff0 (VERSYM) 0x8048248
0x00000000 (NULL) 0x0
Relocation section ‘.rel.dyn’ at offset 0x274 contains 1 entries:
Offset Info Type Sym.Value Sym. Name
08049604 00000106 R_386_GLOB_DAT 00000000 gmon_start
Relocation section ‘.rel.plt’ at offset 0x27c contains 3 entries:
Offset Info Type Sym.Value Sym. Name
08049614 00000107 R_386_JUMP_SLOT 00000000 gmon_start
08049618 00000207 R_386_JUMP_SLOT 00000000 __libc_start_main
0804961c 00000307 R_386_JUMP_SLOT 00000000 printf
There are no unwind sections in this file.
Symbol table ‘.dynsym’ contains 5 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 NOTYPE WEAK DEFAULT UND gmon_start
2: 00000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.0 (2)
3: 00000000 0 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.0 (2)
4: 0804849c 4 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used
Symbol table ‘.symtab’ contains 65 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 08048134 0 SECTION LOCAL DEFAULT 1
2: 08048148 0 SECTION LOCAL DEFAULT 2
3: 08048168 0 SECTION LOCAL DEFAULT 3
4: 0804818c 0 SECTION LOCAL DEFAULT 4
5: 080481ac 0 SECTION LOCAL DEFAULT 5
6: 080481fc 0 SECTION LOCAL DEFAULT 6
7: 08048248 0 SECTION LOCAL DEFAULT 7
8: 08048254 0 SECTION LOCAL DEFAULT 8
9: 08048274 0 SECTION LOCAL DEFAULT 9
10: 0804827c 0 SECTION LOCAL DEFAULT 10
11: 08048294 0 SECTION LOCAL DEFAULT 11
12: 080482c4 0 SECTION LOCAL DEFAULT 12
13: 08048310 0 SECTION LOCAL DEFAULT 13
14: 0804847c 0 SECTION LOCAL DEFAULT 14
15: 08048498 0 SECTION LOCAL DEFAULT 15
16: 080484b4 0 SECTION LOCAL DEFAULT 16
17: 080484d0 0 SECTION LOCAL DEFAULT 17
18: 08049528 0 SECTION LOCAL DEFAULT 18
19: 08049530 0 SECTION LOCAL DEFAULT 19
20: 08049538 0 SECTION LOCAL DEFAULT 20
21: 0804953c 0 SECTION LOCAL DEFAULT 21
22: 08049604 0 SECTION LOCAL DEFAULT 22
23: 08049608 0 SECTION LOCAL DEFAULT 23
24: 08049620 0 SECTION LOCAL DEFAULT 24
25: 08049624 0 SECTION LOCAL DEFAULT 25
26: 00000000 0 SECTION LOCAL DEFAULT 26
27: 00000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
28: 08049528 0 OBJECT LOCAL DEFAULT 18 CTOR_LIST
29: 08049530 0 OBJECT LOCAL DEFAULT 19 DTOR_LIST
30: 08049538 0 OBJECT LOCAL DEFAULT 20 JCR_LIST
31: 08048340 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux
32: 08049624 1 OBJECT LOCAL DEFAULT 25 completed.5921
33: 08049628 4 OBJECT LOCAL DEFAULT 25 dtor_idx.5923
34: 080483a0 0 FUNC LOCAL DEFAULT 13 frame_dummy
35: 00000000 0 FILE LOCAL DEFAULT ABS crtstuff.c
36: 0804952c 0 OBJECT LOCAL DEFAULT 18 CTOR_END
37: 08048524 0 OBJECT LOCAL DEFAULT 17 FRAME_END
38: 08049538 0 OBJECT LOCAL DEFAULT 20 JCR_END
39: 08048450 0 FUNC LOCAL DEFAULT 13 __do_global_ctors_aux
40: 00000000 0 FILE LOCAL DEFAULT ABS helloworld.c
41: 08049608 0 OBJECT LOCAL HIDDEN 23 GLOBAL_OFFSET_TABLE
42: 08049528 0 NOTYPE LOCAL HIDDEN 18 __init_array_end
43: 08049528 0 NOTYPE LOCAL HIDDEN 18 __init_array_start
44: 0804953c 0 OBJECT LOCAL HIDDEN 21 _DYNAMIC
45: 08049620 0 NOTYPE WEAK DEFAULT 24 data_start
46: 080483e0 5 FUNC GLOBAL DEFAULT 13 __libc_csu_fini
47: 08048310 0 FUNC GLOBAL DEFAULT 13 _start
48: 00000000 0 NOTYPE WEAK DEFAULT UND gmon_start
49: 00000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
50: 08048498 4 OBJECT GLOBAL DEFAULT 15 _fp_hw
51: 0804847c 0 FUNC GLOBAL DEFAULT 14 _fini
52: 00000000 0 FUNC GLOBAL DEFAULT UND _libc_start_main@@GLIBC
53: 0804849c 4 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used
54: 08049620 0 NOTYPE GLOBAL DEFAULT 24 __data_start
55: 080484a0 0 OBJECT GLOBAL HIDDEN 15 __dso_handle
56: 08049534 0 OBJECT GLOBAL HIDDEN 19 DTOR_END
57: 080483f0 90 FUNC GLOBAL DEFAULT 13 __libc_csu_init
58: 00000000 0 FUNC GLOBAL DEFAULT UND printf@@GLIBC_2.0
59: 08049624 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
60: 0804962c 0 NOTYPE GLOBAL DEFAULT ABS _end
61: 08049624 0 NOTYPE GLOBAL DEFAULT ABS _edata
62: 0804844a 0 FUNC GLOBAL HIDDEN 13 __i686.get_pc_thunk.bx
63: 080483c4 24 FUNC GLOBAL DEFAULT 13 main
64: 08048294 0 FUNC GLOBAL DEFAULT 11 _init
Histogram for `.gnu.hash’ bucket list length (total of 2 buckets):
Length Number % of total Coverage
0 1 ( 50.0%)
1 1 ( 50.0%) 100.0%
Version symbols section ‘.gnu.version’ contains 5 entries:
Addr: 0000000008048248 Offset: 0x000248 Link: 5 (.dynsym)
000: 0 (local) 0 (local) 2 (GLIBC_2.0) 2 (GLIBC_2.0)
004: 1 (global)
Version needs section ‘.gnu.version_r’ contains 1 entries:
Addr: 0x0000000008048254 Offset: 0x000254 Link: 6 (.dynstr)
000000: Version: 1 File: libc.so.6 Cnt: 1
0x0010: Name: GLIBC_2.0 Flags: none Version: 2
Notes at offset 0x00000148 with length 0x00000020:
Owner Data size Description
GNU 0x00000010 NT_GNU_ABI_TAG (ABI version tag)
Notes at offset 0x00000168 with length 0x00000024:
Owner Data size Description
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring)
[root@localhost test]#
实例链接https://blog.csdn.net/zhwfnh/article/details/5472869添加链接描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值