linux - ELF文件格式中的section和segment有什么区别

来自维基 Executable and Linkable Format :

The segments contain information that is necessary for runtime execution of the file, while sections contain important data for linking and relocation. Any byte in the entire file can be owned by at most one section, and there can be orphan bytes which are not owned by any section.

但是section和segment有什么区别呢? 在一个可执行的 ELF 文件中,一个段是否包含一个或多个节?

最佳答案

But what's difference between section and segment?

正是您引用的内容:段包含运行时所需的信息,而部分包含链接期间所需的信息。

does a segment contain one or more sections?

一个段可以包含 0 个或多个部分。示例:

readelf -l /bin/date

Elf file type is EXEC (Executable file)
Entry point 0x402000
There are 9 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001f8 0x00000000000001f8  R E    8
  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x000000000000d5ac 0x000000000000d5ac  R E    200000
  LOAD           0x000000000000de10 0x000000000060de10 0x000000000060de10
                 0x0000000000000440 0x0000000000000610  RW     200000
  DYNAMIC        0x000000000000de38 0x000000000060de38 0x000000000060de38
                 0x00000000000001a0 0x00000000000001a0  RW     8
  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x000000000000c700 0x000000000040c700 0x000000000040c700
                 0x00000000000002a4 0x00000000000002a4  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     8
  GNU_RELRO      0x000000000000de10 0x000000000060de10 0x000000000060de10
                 0x00000000000001f0 0x00000000000001f0  R      1

 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 .rela.dyn .rela.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     
   08     .ctors .dtors .jcr .dynamic .got 

这里,PHDR段包含0个段,INTERP段包含.interp段,第一个LOAD段包含一大堆部分。

Further reading有一个不错的illustration :

https://stackoverflow.com/questions/14361248/

相关文章:

python - set() 是如何实现的?

python - 不区分大小写 'in'

python - 如何使用列表中的键和默认为零的值创建字典?

python - 用python练习BDD

python - 如何在 Python 自己的调试器 (PDB) 中执行多行语句

android - 找不到构建工具修订版 21.1.1 - sdk 已更新

linux - 重新加载 Flash 17 次会导致错误 #2046 并需要重新启动浏览器

linux - 如何为输出添加行号,提示行,然后根据输入进行操作?

linux - 在同一终端中一次运行多个命令

linux - 如何在 Unix 系统上编辑二进制文件