【问题标题】:Reading ELF String Table on Linux from C从 C 读取 Linux 上的 ELF 字符串表
【发布时间】:2012-05-16 20:28:45
【问题描述】:

我想编写一个读取二进制字符串表的程序。二进制文件在 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 结尾的字符串。我想显示这个以 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
--------------------------------------------------------------

【问题讨论】:

  • 我自己找到了答案:)。虽然写代码花了很多时间。如果 some1 想在将来引用它,这是如何完成的 -

标签: elf ptrace


【解决方案1】:

我自己找到了答案:)。虽然写代码花了很多时间。如果有人想将来参考它,这是如何完成的 - 每个 Binary 一般包含三个 String 表 -

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.

【讨论】:

  • 你的意思是我们可以使用“sh_name”从Section Header STRing TABle中查找字符串表的名称,它由“e_shstrndx”表示。那么最后我们可以得到 3 个不同的名字(它们是你上面提到的 .dynstr .shstrtab 和 .strtab),那么我们可以通过名字来区分这 3 个 Elf32_Shdr 的节吗?
【解决方案2】:

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

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

【讨论】:

  • 我想编写可以从二进制文件本身读取的代码,而不是使用 libelf 或 libbfd,因为编写代码本身有助于更多地了解 ELF 对象的排列方式。
  • 虽然他不想使用图书馆(我完全理解)这个答案确实回答了这个问题。 elf 标头的e_shstrndx 字段包含包含节名称字符串表的节的索引
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
相关资源
最近更新 更多