区分ELF文件中的.shstrtab和.strtab

Distinguish .shstrtab and .strtab in ELF file

我想知道在解析 ELF 文件时如何识别 .shstrtab.strtab?从阅读 elf(5) - Linux manual page 两者都属于 header 类型 SHT_STRTAB,那么我怎么知道我遇到的是其中之一?

他们的描述是:

.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.

在执行 readelf file.o 时,我看到以下内容:

...
[18] .strtab           STRTAB           0000000000000000  00000548
       0000000000000033  0000000000000000           0     0     1
[19] .shstrtab         STRTAB           0000000000000000  000007a8
       00000000000000a8  0000000000000000           0     0     1

所以除了偏移量之外,它们对我来说是一样的。

来自同一个 docse_shstrndx:此成员 [在 ElfN_Ehdr] 中包含与部分名称字符串 table。如果文件没有节名字符串 table,则此成员的值为 SHN_UNDEF.

如您所见,一个 ELF 文件中可能有多个字符串 table,它们都共享 STRTAB.

节类型

通常有三个,您可以根据其他部分 header 的信息来区分它们 - 不一定要看它们的名字。

(缩短)readelf -a 的输出:

ELF Header:
...
  Size of section headers:           64 (bytes)
  Number of section headers:         32
  Section header string table index: 30

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
...
  [ 6] .dynsym           DYNSYM           0000000000000408  00000408
       0000000000000360  0000000000000018   A       7     1     8
  [ 7] .dynstr           STRTAB           0000000000000768  00000768
       0000000000000230  0000000000000000   A       0     0     1
...
  [23] .dynamic          DYNAMIC          0000000000003ce0  00002ce0
       0000000000000200  0000000000000010  WA       7     0     8
...
  [28] .symtab           SYMTAB           0000000000000000  00003080
       0000000000000a08  0000000000000018          29    47     8
  [29] .strtab           STRTAB           0000000000000000  00003a88
       00000000000005f7  0000000000000000           0     0     1
  [30] .shstrtab         STRTAB           0000000000000000  0000407f
       0000000000000126  0000000000000000           0     0     1

Dynamic section at offset 0x2ce0 contains 28 entries:
  Tag        Type                         Name/Value
...
 0x0000000000000005 (STRTAB)             0x768
 0x0000000000000006 (SYMTAB)             0x408
...

.dynstr

.dynstr 部分包含用于动态链接的符号名称。这些符号存储在 .dynsym table.

您可以通过两种独立的方式识别与动态符号 table 关联的字符串 table:

  1. 解析 DYNAMIC 部分。应该有两个条目 STRTABSYMTAB,它们分别保存动态字符串和符号 tables 的偏移量。
  2. 寻找 DYNSYM 类型的部分。 通常,它的 header 部分应该在它的 sh_link 字段中存储关联字符串 table 部分的索引(这被标记为“操作 system-specific" 在 ELF specification 中,但在实践中似乎运行良好。

.strtab

.strtab 部分与符号 table .symtab 相关联,它主要用于调试目的,而不是在运行时使用。

您可以通过查看 sh_link 字段再次识别与 .symtab 关联的字符串 table,该字段应包含字符串 table 在大多数情况下的部分索引案例。


.shstrtab

这是 header 部分字符串 table。

您可以通过从 ELF header 中读取 e_shstrndx 来安全地识别它 - 此字段包含包含 header 字符串 table 部分的部分的索引。