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

4
我想知道在解析ELF文件时,如何区分.shstrtab.strtab?从阅读elf(5) - Linux manual page得知,两者都是类型为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

除了偏移量之外,它们在我的眼中看起来是一样的。

2个回答

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

.dynstr

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

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

  1. 解析DYNAMIC部分。应该有两个条目STRTABSYMTAB,分别保存动态字符串和符号表的偏移量。
  2. 查找类型为DYNSYM的节。通常情况下,它的节头应将相关字符串表节的索引存储在其sh_link字段中(这在ELF规范中标记为“操作系统特定”,但实际上表现良好)。

.strtab

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

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


.shstrtab

这是节头字符串表。

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



1

从同一个文档中: e_shstrndx: 这个成员 [在ElfN_Ehdr中] 保存了与节名字符串表相关联的条目的节头表索引。如果文件没有节名字符串表,则此成员保存值SHN_UNDEF。


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