我想知道在解析 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
所以除了偏移量之外,它们对我来说看起来是一样的。
最佳答案
正如您已经展示的那样,一个 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
部分包含用于动态链接的符号名称。这些符号存储在 .dynsym
表中。
您可以通过两种独立的方式识别与动态符号表关联的字符串表:
- 解析
DYNAMIC
部分。应该有两个条目STRTAB
和SYMTAB
,分别保存动态字符串和符号表的偏移量。 - 寻找
DYNSYM
类型的部分。 通常,其节头应在其sh_link
字段中存储相关字符串表节的索引(这在 ELF specification 中标记为“特定于操作系统”,但在实践中似乎效果很好)。
.strtab
.strtab
部分与符号表 .symtab
相关联,它主要用于调试目的,而不是在运行时使用。
您可以通过查看sh_link
字段再次识别与.symtab
关联的字符串表,在大多数情况下,该字段应包含字符串表的节索引。
.shstrtab
这是节头字符串表。
您可以通过从 ELF header 中读取 e_shstrndx
来安全地识别它 - 此字段包含包含节 header 字符串表的节的索引。
关于linux - 区分ELF文件中的.shstrtab和.strtab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64967077/