user6123183
user6123183

Reputation:

Which string table does the section header list point to?

An Elf file can have multiple String Tables however the list of Section Headers sh_name field is an index into this table. How does the file know which string table to refer to?

Upvotes: 0

Views: 397

Answers (1)

GalAbra
GalAbra

Reputation: 5148

There are two string tables in an ELF file:

  1. .strtab, which contains the symbols' names.
  2. .shstrtab (Section Header String Table), which contains the sections' names.

If you want to use sectionHeader.sh_name, then you're probably looking for the section's name in the .shstrtab table. It can be obtained using the following code (obviously for 64 bit):

Elf64_Ehdr* header = (Elf64_Ehdr*) map;
Elf64_Shdr* stringTable = (Elf64_Shdr*) (map + header->e_shoff +
                             header->e_shstrndx * header->e_shentsize);        
char* sectionName = map + stringTable->sh_offset + sectionHeader->sh_name;

// 'map' is a pointer to the beginning of your mapped ELF file


Thank you @Employed Russian for pointing a better way to retrieve the header's size.

Upvotes: 1

Related Questions