可执行的C程序中的段

11

我在阅读有关节和段的内容。似乎可以将节和段之间的映射列出如下。

$ readelf -l test

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

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x08048034 0x08048034 0x00120 0x00120 R E 0x4
  INTERP         0x000154 0x08048154 0x08048154 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /lib/ld-linux.so.2]
  LOAD           0x000000 0x08048000 0x08048000 0x0065c 0x0065c R E 0x1000
  LOAD           0x000f14 0x08049f14 0x08049f14 0x00104 0x00110 RW  0x1000
  DYNAMIC        0x000f28 0x08049f28 0x08049f28 0x000c8 0x000c8 RW  0x4
  NOTE           0x000168 0x08048168 0x08048168 0x00044 0x00044 R   0x4
  GNU_EH_FRAME   0x000564 0x08048564 0x08048564 0x00034 0x00034 R   0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4
  GNU_RELRO      0x000f14 0x08049f14 0x08049f14 0x000ec 0x000ec R   0x1

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

我的问题,

  1. 我不理解程序头是什么意思?它们与段有什么关系?
  2. 区段映射很清楚,但有人可以给它命名吗?我只看到数字。我确认了代码段(03),数据段(02)和堆栈(07)。

这并不是一个真正的编程问题。这个问题是否应该放在其他地方,比如“Unix和Linux”? - John Eipe
2个回答

12
为了理解 readelf 的输出结果,建议您先了解 ELF 文件格式,可以参考这个文档:链接
如果想要了解如何解释 readelf 的输出结果,这个链接可能会有所帮助。
关于您的第二个问题,请参考此链接中描述的段落。在该文档中搜索“Various sections hold program and control information:”,即可找到描述段名称的区域。
该文档将段定义如下:
   .bss      This section holds uninitialized data that contributes to the
             program's memory image.  By definition, the system initializes the
             data with zeros when the program begins to run.  This section is of
             type SHT_NOBITS.  The attribute types are SHF_ALLOC and SHF_WRITE.

   .comment  This section holds version control information.  This section is of
             type SHT_PROGBITS.  No attribute types are used.

   .ctors    This section holds initialized pointers to the C++ constructor
             functions.  This section is of type SHT_PROGBITS.  The attribute
             types are SHF_ALLOC and SHF_WRITE.

   .data     This section holds initialized data that contribute to the program's
             memory image.  This section is of type SHT_PROGBITS.  The attribute
             types are SHF_ALLOC and SHF_WRITE.

   .data1    This section holds initialized data that contribute to the program's
             memory image.  This section is of type SHT_PROGBITS.  The attribute
             types are SHF_ALLOC and SHF_WRITE.

   .debug    This section holds information for symbolic debugging.  The contents
             are unspecified.  This section is of type SHT_PROGBITS.  No
             attribute types are used.

   .dtors    This section holds initialized pointers to the C++ destructor
             functions.  This section is of type SHT_PROGBITS.  The attribute
             types are SHF_ALLOC and SHF_WRITE.

   .dynamic  This section holds dynamic linking information.  The section's
             attributes will include the SHF_ALLOC bit.  Whether the SHF_WRITE
             bit is set is processor-specific.  This section is of type
             SHT_DYNAMIC.  See the attributes above.

   .dynstr   This section holds strings needed for dynamic linking, most commonly
             the strings that represent the names associated with symbol table
             entries.  This section is of type SHT_STRTAB.  The attribute type
             used is SHF_ALLOC.

   .dynsym   This section holds the dynamic linking symbol table.  This section
             is of type SHT_DYNSYM.  The attribute used is SHF_ALLOC.

   .fini     This section holds executable instructions that contribute to the
             process termination code.  When a program exits normally the system
             arranges to execute the code in this section.  This section is of
             type SHT_PROGBITS.  The attributes used are SHF_ALLOC and
             SHF_EXECINSTR.

   .gnu.version
             This section holds the version symbol table, an array of ElfN_Half
             elements.  This section is of type SHT_GNU_versym.  The attribute
             type used is SHF_ALLOC.

   .gnu.version_d
             This section holds the version symbol definitions, a table of
             ElfN_Verdef structures.  This section is of type SHT_GNU_verdef.
             The attribute type used is SHF_ALLOC.

   .gnu.version_r
             This section holds the version symbol needed elements, a table of
             ElfN_Verneed structures.  This section is of type SHT_GNU_versym.
             The attribute type used is SHF_ALLOC.

   .got      This section holds the global offset table.  This section is of type
             SHT_PROGBITS.  The attributes are processor specific.

   .hash     This section holds a symbol hash table.  This section is of type
             SHT_HASH.  The attribute used is SHF_ALLOC.

   .init     This section holds executable instructions that contribute to the
             process initialization code.  When a program starts to run the
             system arranges to execute the code in this section before calling
             the main program entry point.  This section is of type SHT_PROGBITS.
             The attributes used are SHF_ALLOC and SHF_EXECINSTR.

   .interp   This section holds the pathname of a program interpreter.  If the
             file has a loadable segment that includes the section, the section's
             attributes will include the SHF_ALLOC bit.  Otherwise, that bit will
             be off.  This section is of type SHT_PROGBITS.

   .line     This section holds line number information for symbolic debugging,
             which describes the correspondence between the program source and
             the machine code.  The contents are unspecified.  This section is of
             type SHT_PROGBITS.  No attribute types are used.

   .note     This section holds information in the "Note Section" format.  This
             section is of type SHT_NOTE.  No attribute types are used.  OpenBSD
             native executables usually contain a .note.openbsd.ident section to
             identify themselves, for the kernel to bypass any compatibility ELF
             binary emulation tests when loading the file.

   .note.GNU-stack
             This section is used in Linux object files for declaring stack
             attributes.  This section is of type SHT_PROGBITS.  The only
             attribute used is SHF_EXECINSTR.  This indicates to the GNU linker
             that the object file requires an executable stack.

   .plt      This section holds the procedure linkage table.  This section is of
             type SHT_PROGBITS.  The attributes are processor specific.

   .relNAME  This section holds relocation information as described below.  If
             the file has a loadable segment that includes relocation, the
             section's attributes will include the SHF_ALLOC bit.  Otherwise the
             bit will be off.  By convention, "NAME" is supplied by the section
             to which the relocations apply.  Thus a relocation section for .text
             normally would have the name .rel.text.  This section is of type
             SHT_REL.

   .relaNAME This section holds relocation information as described below.  If
             the file has a loadable segment that includes relocation, the
             section's attributes will include the SHF_ALLOC bit.  Otherwise the
             bit will be off.  By convention, "NAME" is supplied by the section
             to which the relocations apply.  Thus a relocation section for .text
             normally would have the name .rela.text.  This section is of type
             SHT_RELA.

   .rodata   This section holds read-only data that typically contributes to a
             nonwritable segment in the process image.  This section is of type
             SHT_PROGBITS.  The attribute used is SHF_ALLOC.

   .rodata1  This section holds read-only data that typically contributes to a
             nonwritable segment in the process image.  This section is of type
             SHT_PROGBITS.  The attribute used is SHF_ALLOC.

   .shstrtab This section holds section names.  This section is of type
             SHT_STRTAB.  No attribute types are used.

   .strtab   This section holds strings, most commonly the strings that represent
             the names associated with symbol table entries.  If the file has a
             loadable segment that includes the symbol string table, the
             section's attributes will include the SHF_ALLOC bit.  Otherwise the
             bit will be off.  This section is of type SHT_STRTAB.

   .symtab   This section holds a symbol table.  If the file has a loadable
             segment that includes the symbol table, the section's attributes
             will include the SHF_ALLOC bit.  Otherwise the bit will be off.
             This section is of type SHT_SYMTAB.

   .text     This section holds the "text", or executable instructions, of a
             program.  This section is of type SHT_PROGBITS.  The attributes used
             are SHF_ALLOC and SHF_EXECINSTR.

我没有查看链接,但从您的解释来看,对于新手来说,段落和部分似乎是相同的。 - user926857
段和节不是同一回事。节通常由静态链接器(链接编辑器)使用,而段主要由动态加载程序使用。只需使用“readelf -l二进制文件”即可查看许多节可能被分组在一个段中。 - JohnTortugo

3
在ELF二进制文件中,程序头描述了二进制文件的运行方式。其中有趣的部分是LOAD头,它将二进制文件的一部分加载到内存的不同位置。一个二进制文件中可以有任意数量的LOAD头,但通常链接器会将所有只读和可执行的内容放在一个头中,将所有可读写的内容放在另一个头中。有些操作系统还会为略微提高安全性而设置只读数据LOAD头、可读写数据和只读可执行代码。
这里的"segments"指的是二进制文件在内存中加载的不同部分,也就是不同的LOAD头。
"Sections"是指在链接时组织数据的方式。由于各种原因,您希望比仅有数据/代码更好地组织事物的粒度更好。一些数据是只读的,它们被放置在".rodata"中。代码位于".text"中,初始化数据位于".data"中,而在程序启动时被清零的变量数据位于".bss"中。
"section to segment mapping"告诉您哪些部分位于哪些段(不同的LOAD头)中。因此,".text"和".rodata"位于第一个LOAD头(第三个程序头)中,而".data"位于第二个LOAD头(第四个程序头)中。
堆栈是操作系统在执行时提供给您的,并且不是由ELF二进制文件描述的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接