1

我想知道 a与解析 ELF 文件时的.shstrtaba 相比如何识别?.strtab从阅读elf(5) - Linux 手册页开始,两者都属于部分标题类型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

所以它们在我看来是一样的,除了偏移量。

4

2 回答 2

1

来自相同的文档: e_shstrndx: 这个成员 [in ElfN_Ehdr] 保存与节名称字符串表关联的条目的节头表索引。如果文件没有节名字符串表,则该成员保存值 SHN_UNDEF。

于 2020-11-24T09:55:57.177 回答
1

正如您已经展示的那样,一个 ELF 文件中可能有多个字符串表,它们都共享 section type 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
...

.dynstr

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

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

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

.strtab

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

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


.shstrtab

这是节标题字符串表。

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

于 2021-11-08T19:30:47.883 回答