从C上读取Linux上的ELF字符串表

Sam*_*aid 2 ptrace elf

我想编写一个读取二进制字符串表的程序.二进制在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
Run Code Online (Sandbox Code Playgroud)

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

  1. 在上面的输出中,我们可以看到Section Headers有多个条目,其中sh_type = 3(SHT_STRTAB).所以我不明白我如何将索引(在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
--------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

Sam*_*aid 5

我自己能够找到答案:).虽然代码花了很多时间.以下是如果有人想要将其引用以供日后使用 - 每个二进制文件通常包含三个String表 -

1. .dynstr
2. .shstrtab
3. .strtab
Run Code Online (Sandbox Code Playgroud)

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

offset = ((elfHdr.e_shstrndx)*elfHdr.e_shentsize)+elfHdr.e_shoff
Run Code Online (Sandbox Code Playgroud)

每个参数的含义 -

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.
Run Code Online (Sandbox Code Playgroud)