I have to write a C program that prints an ELF file. I\'m having trouble figuring out where the section header string table is.
Let\'s say I have a file that gave me
Here is how you would get to section name string table:
e_shstrndx
field of the ELF Executable Header (EHDR) specifies the index of the section header table entry describing the string table containing section names.e_shentsize
field of the EHDR specifies the size in bytes of each section header table entry.e_shoff
field of the EHDR specifies the file offset to the start of the section header table.Thus the header entry for the string table will be at file offset:
header_table_entry_offset = (e_shentsize * e_shstrndx) + e_shoff
Depending on the ELF class of the object, this header table entry will be either an Elf32_Shdr
or an Elf64_Shdr
. Note that the file representation of the header table entry may differ from its in-memory representation on the host that your program is running on.
The sh_offset
field in the header entry will specify the starting file offset of the string table, while the sh_size
field will specify its size.
Further reading: The "libelf by Example" tutorial covers the ELF Executable Header and ELF Section Header Table in greater depth.