ELF - sh_offset 字段的用途是什么?

ELF - what's the purpose o the sh_offset field?

我正在尝试了解有关 ELF 格式的更多信息,特别是 headers 部分,我只是 came across 以下内容:

Elf32_Ehdr *ehdr = (Elf32_Ehdr*)p;
Elf32_Shdr *shdr = (Elf32_Shdr *)(p + ehdr->e_shoff);
int shnum = ehdr->e_shnum;

Elf32_Shdr *sh_strtab = &shdr[ehdr->e_shstrndx];
const char *const sh_strtab_p = p + sh_strtab->sh_offset;

for (int i = 0; i < shnum; ++i) {
   printf("%2d: %4d '%s'\n", i, shdr[i].sh_name,
          sh_strtab_p + shdr[i].sh_name);
}

 return 0;
}

现在,我知道这基本上是遍历 table 部分并打印部分名称,但我仍然对 sh_offset 感到困惑场地。它到底是做什么的?如果 e_shstrndx 已经指向字符串 table 部分为什么我们需要 sh_offset?

If e_shstrndx is already pointing to the string table section why do we need sh_offset

e_shstrndx 部分 table 的索引;它告诉您哪个部分描述(包含)字符串 table.

但它不会告诉您数据(字符串本身)所在的位置。为此你需要 sh_offset

这里是picture.e_shoff 告诉你 table 部分从哪里开始,.e_shstrndx 告诉你那个 table 的哪个元素是你想要的,那个元素的 .sh_offset 告诉您文件 .shstrtab 部分的 data 所在的位置(即字符串本身所在的位置)。