我想编写一个读取二进制字符串表的程序。 Binary在ELF上运行在REDHAT linux 32上。 我做了以下 -
以下是我的节目的输出。
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基本上是String表的索引,它实际上包含一个以NULL结尾的String。我想显示这个以null结尾的String。我在这里有一个问题 -
在为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
--------------------------------------------------------------
答案 0 :(得分:5)
我能够自己找到答案:)。虽然代码花了很多时间。如果有人想将其推荐给未来,这是如何做到的 - 每个二进制文件通常包含三个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.
答案 1 :(得分:2)
简而言之,ELF可执行文件头的e_shstrndx
字段包含保存部分名称的ELF字符串表的索引。
“libelf by Example”教程有一个较长的解释,以及显示如何使用ELF(3)API中的函数检索节名称的示例代码。