Search code examples
celfreadelf

How to get the strings of shstrtab?


I am writing a simplified version of the Linux readelf command.

I want to print section information, so I need the names of the sections. In the Elf64_Shdr struct, the sh_name variable only points me to an index in the shstrtab. But this does not seem to be an index in the shstrtab header. It is an index into its associated data.

I am wondering how to get there, since the ELF header only points me to the shstrtab section header, but not to its associated data. From what I can see in the hex dump of the file, the structure of the file is as follows:

ELF HEADER 
phdr1
phdr2
segment1
segment2
shstrtab strings (i want this address, to make use of the sh_name indices)
shdr1
shdr2
shdr3 (this is the shstrtab section)

Am I thinking this wrong? Can anybody please guide me in getting to the section names?


Solution

  • I just found the solution myself!

    To get to the associated data of a section one just use the sh_offset in the Elf64_Shdr struct. If you add sh_offset to the start address of the file, you get directly to the section data.