I'm trying to learn more about the ELF format, particularly, section headers and I just came across the following:
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;
}
Now, I understand that this is basically iterating through the section table and printing the section names but I'm still confused about the sh_offset field. What exactly does it do? If e_shstrndx is already pointing to the string table section why do we need sh_offset?