How to find the offset of the section header string table of an elf file?

前端 未结 4 1556
醉酒成梦
醉酒成梦 2020-12-08 17:48

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

4条回答
  •  醉梦人生
    2020-12-08 18:02

    Here is how you would get to section name string table:

    • The 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.
    • The e_shentsize field of the EHDR specifies the size in bytes of each section header table entry.
    • The 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.

提交回复
热议问题