3

我想编写一个读取二进制字符串表的程序。二进制文件在 ELF 中运行在 REDHAT linux 32 上。我做了以下 -

  1. 阅读精灵标题
  2. 阅读所有部分

下面是我的程序的输出。

Entry Address of Binary - 0x8048340
Start of Program Header - 52
Start of section header - 3272
Size of header - 52
Number of section headers - 36
Size of each section headers - 40
Number of section headers - 36
Section header Offset - 3272
string tbl index for section[0] is 0
string tbl index for section[1] is 27
string tbl index for section[7] is 35
string tbl index for section[1879048182] is 49
string tbl index for section[11] is 59
string tbl index for section[3] is 67
string tbl index for section[1879048191] is 75
string tbl index for section[1879048190] is 88
string tbl index for section[9] is 103
string tbl index for section[9] is 112
string tbl index for section[1] is 121
string tbl index for section[1] is 116
string tbl index for section[1] is 127
string tbl index for section[1] is 133
string tbl index for section[1] is 139
string tbl index for section[1] is 147
string tbl index for section[1] is 157
string tbl index for section[1] is 164
string tbl index for section[1] is 171
string tbl index for section[6] is 176
string tbl index for section[1] is 185
string tbl index for section[1] is 190
string tbl index for section[1] is 199
string tbl index for section[8] is 205
string tbl index for section[1] is 210
string tbl index for section[1] is 219
string tbl index for section[1] is 234
string tbl index for section[1] is 250
string tbl index for section[1] is 262
string tbl index for section[1] is 276
string tbl index for section[1] is 288
string tbl index for section[1] is 301
string tbl index for section[1] is 312
string tbl index for section[3] is 17
string tbl index for section[2] is 1
string tbl index for section[3] is 9

我知道 Elf32_Shdr 中的 sh_name 基本上是字符串表的索引,它实际上包含一个以 NULL 结尾的字符串。我想显示这个以空字符结尾的字符串。我在这里有一个问题-

  1. 在上面的输出中,我们可以看到有多个 sh_type = 3 (SHT_STRTAB) 的 Section Headers 条目。所以我不明白如何将索引(Elf32_Shdr 中的 sh_name)映射到哪个部分?

在为具有 sh_type = 3 的部分打印 Elf32_Shdr 时,我得到以下输出 -

Section header Offset - 3272
sh_name - 67
sh_type - 3
sh_flags - 2
sh_addr - 80481e8
sh_offset - 488
sh_size - 94
sh_link - 0
sh_info - 0
sh_addralign - 1
sh_entsize - 0
--------------------------------------------------------------
sh_name - 17
sh_type - 3
sh_flags - 0
sh_addr - 0
sh_offset - 2948
sh_size - 323
sh_link - 0
sh_info - 0
sh_addralign - 1
sh_entsize - 0
--------------------------------------------------------------
sh_name - 9
sh_type - 3
sh_flags - 0
sh_addr - 0
sh_offset - 6008
sh_size - 664
sh_link - 0
sh_info - 0
sh_addralign - 1
sh_entsize - 0
--------------------------------------------------------------
4

2 回答 2

9

我能够自己找到答案:)。虽然写代码花了很多时间。如果有人想在将来引用它,这是如何完成的 - 每个二进制文件通常包含三个字符串表 -

1. .dynstr
2. .shstrtab
3. .strtab

在上述问题中,我们关注 .shstrtab,它在扩展时代表 - Section Header STRing TABle。在读取 ELF 标头后,我们在 ELF 标头中找到以下字段 - e_shstrndx。这是我们可以找到 .shstrtab 的索引。以下公式可用于计算它将如何完成 -

offset = ((elfHdr.e_shstrndx)*elfHdr.e_shentsize)+elfHdr.e_shoff

每个参数的含义 -

elfHdr.e_shstrndx = index where we can find .shstrtab
elfHdr.e_shentsize = Size of each Section Header
elfHdr.e_shoff = Offset at which section header starts.
于 2012-05-18T15:08:34.570 回答
2

简而言之,e_shstrndxELF Executable Header 的字段包含保存段名的 ELF 字符串表的索引。

libelf by Example ”教程有一个更长的解释,以及显示如何使用 ELF(3) API 中的函数检索节名称的示例代码。

于 2012-05-18T14:55:06.603 回答