无法理解程序 header 和节 header 中的偏移量成员

can't understand offset member in program header and section header

我读男人page。它说

ELF header:

e_phoff - 此成员保存程序 header table 的文件偏移量(以字节为单位)。

e_shoff - 该成员保存 header table 部分的文件偏移量(以字节为单位)。

计划中Header

p_offset 这个成员保存了从文件开始的偏移量 段的第一个字节所在的位置。

在第 Header

部分

sh_offset 该成员的值保存了从文件开头到节中第一个字节的字节偏移量

我很困惑。在我看来,这意味着在 Elf header 中我可以看到所有程序和部分 header 的偏移量。在程序 header 中,我可以看到文件中具体段的偏移量。在 header 部分,我可以看到文件中具体部分的偏移量。但事实并非如此。我找到了简单的精灵解析,我已经看到了这个结果

segment offset: 52
section offset: 6032
Program Entry point: 0x8048420
Section header list:

.interp: 0x8048154
offset: 340
.note.ABI-tag: 0x8048168
offset: 360
.note.gnu.build-id: 0x8048188
offset: 392
.gnu.hash: 0x80481ac
offset: 428
.dynsym: 0x80481e8
offset: 488
.dynstr: 0x80482b8
offset: 696
.gnu.version: 0x8048342
offset: 834
.gnu.version_r: 0x804835c
offset: 860
.rel.dyn: 0x804837c
offset: 892
.rel.plt: 0x8048394
offset: 916
.init: 0x80483ac
offset: 940
.plt: 0x80483d0
offset: 976
.plt.got: 0x8048410
offset: 1040
.text: 0x8048420
offset: 1056
.fini: 0x8048604
offset: 1540
.rodata: 0x8048618
offset: 1560
.eh_frame_hdr: 0x8048628
offset: 1576
.eh_frame: 0x8048664
offset: 1636
.init_array: 0x8049efc
offset: 3836
.fini_array: 0x8049f00
offset: 3840
.dynamic: 0x8049f04
offset: 3844
.got: 0x8049ff4
offset: 4084
.got.plt: 0x804a000
offset: 4096
.data: 0x804a018
offset: 4120
.bss: 0x804a020
offset: 4128
.comment: 0x0
offset: 4128
.symtab: 0x0
offset: 4172
.strtab: 0x0
offset: 5244
.shstrtab: 0x0
offset: 5768

Program header list

Phdr segment: 0x8048034
offset: 52
Interpreter: /lib/ld-linux.so.2
offset: 340
Text segment: 0x8048000
offset: 0
Data segment: 0x8049efc
offset: 3836
Dynamic segment: 0x8049f04
offset: 3844
Note segment: 0x8048168
offset: 360
PT_GNU_EH_FRAME: 0x8048628
offset: 1576
PT_GNU_STACK: 0x0
offset: 0
PT_GNU_RELRO: 0x8049efc
offset: 3836

如您所见,Elf 偏移量的部分偏移量 = 6032,但所有部分的偏移量均小于 Elf 偏移量。实际上这个程序中的所有部分都有像 6032 + (n * sizeof(Elf32_Shdr)) 这样的偏移量。在这种情况下,我无法理解 header 部分中的偏移量是什么意思?我以为它在过程映像中有偏移量,但手册页正在谈论文件内的偏移量。关于程序中偏移量的相同问题header。请说明所有相同的意思是部分 header 偏移量和程序 header 偏移量。

解析器太大,所以我没有附上。但是如果有人需要,我会做

Actually all sections in this program have offset like 6032 + (n * sizeof(Elf32_Shdr)).

不,并非所有 部分 都有此偏移量,但部分 header 的所有 部分条目 ]table.

您看到的是 table 的偏移量高于其定义的 sections 条目.

在你的例子中:

在文件的偏移量 1056 处开始“.text”部分。

在文件的偏移量 6032 开始部分 header table。它的第 14 个条目(在 6032 + 13 * sizeof(Elf32_Shdr))定义了“.text”部分并将其偏移量指定为 1056.