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

Mul*_*Kid 4 linux elf string-table

我想知道解析 ELF 文件时.shstrtab与 a 相比如何识别a ?.strtab从阅读elf(5) - Linux 手册页来看,两者都是节标题类型SHT_STRTAB,那么我如何知道我遇到的是其中之一呢?

他们的描述是:

.shstrtab
    This section holds section names.  This section is of type
    SHT_STRTAB.  No attribute types are used.
Run Code Online (Sandbox Code Playgroud)
.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.
Run Code Online (Sandbox Code Playgroud)

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

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

Run Code Online (Sandbox Code Playgroud)

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

Jan*_*ann 7

正如您已经展示的,一个 ELF 文件中可能有多个字符串表,它们都共享节类型STRTAB

通常有三个,您可以根据其他节标题的信息来区分它们 - 而不必查看它们的名称。

(缩短)输出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
...
Run Code Online (Sandbox Code Playgroud)

.dynstr

.dynstr部分保存用于动态链接的符号名称。这些符号存储在.dynsym表中。

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

  1. 解析该DYNAMIC部分。应该有两个条目STRTABSYMTAB,分别保存动态字符串和符号表的偏移量。
  2. 寻找类型为 的部分DYNSYM通常,它的节头应该在其字段中存储关联字符串表节的索引(这在ELF 规范sh_link中被标记为“特定于操作系统” ,但在实践中似乎工作得很好)。

.strtab

.strtab部分与符号表相关联.symtab,符号表主要用于调试目的,并且在运行时不使用。

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


.shstrtab

这是节头字符串表。

您可以通过从 ELF 标头中读取来安全地识别它e_shstrndx- 该字段包含保存节标头字符串表的节的索引。