如何查找 ELF 文件中节头部字符串表的偏移量?

9

我需要编写一个C程序,用于打印ELF文件。但是我无法确定节头字符串表的位置。

假设我有一个文件,并通过以下命令获得了以下输出:

readelf -h

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)  
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          17636 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         23
  Section header string table index: 20

并且使用:

readelf -S:

There are 23 section headers, starting at offset 0x44e4:
Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00000000 000034 000028 00  AX  0   0  4
  [ 2] .rel.text         REL             00000000 0049d0 000018 08     21   1  4
  [ 3] .data             PROGBITS        00000000 00005c 000000 00  WA  0   0  4
  [ 4] .bss              NOBITS          00000000 00005c 000000 00  WA  0   0  4
  [ 5] .rodata           PROGBITS        00000000 00005c 00000a 00   A  0   0  1
  [ 6] .debug_info       PROGBITS        00000000 000066 00008f 00      0   0  1
  [ 7] .rel.debug_info   REL             00000000 0049e8 0000b0 08     21   6  4
  [ 8] .debug_abbrev     PROGBITS        00000000 0000f5 000041 00      0   0  1
  [ 9] .debug_loc        PROGBITS        00000000 000136 000038 00      0   0  1
  [10] .debug_aranges    PROGBITS        00000000 00016e 000020 00      0   0  1
  [11] .rel.debug_arange REL             00000000 004a98 000010 08     21  10  4
  [12] .debug_line       PROGBITS        00000000 00018e 0001b3 00      0   0  1
  [13] .rel.debug_line   REL             00000000 004aa8 000008 08     21  12  4
  [14] .debug_macinfo    PROGBITS        00000000 000341 003fb9 00      0   0  1
  [15] .debug_str        PROGBITS        00000000 0042fa 0000bf 01  MS  0   0  1
  [16] .comment          PROGBITS        00000000 0043b9 00002b 01  MS  0   0  1
  [17] .note.GNU-stack   PROGBITS        00000000 0043e4 000000 00      0   0  1
  [18] .eh_frame         PROGBITS        00000000 0043e4 000038 00   A  0   0  4
  [19] .rel.eh_frame     REL             00000000 004ab0 000008 08     21  18  4
  [20] .shstrtab         STRTAB          00000000 00441c 0000c5 00      0   0  1
  [21] .symtab           SYMTAB          00000000 00487c 000130 10     22  16  4
  [22] .strtab           STRTAB          00000000 0049ac 000021 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

我该如何计算节头字符串表的位置?

1
如果你在 ghex 中找到了它,难道你不能通过查看文件中找到它的地址来看到偏移量吗?我不熟悉它,但当我谷歌搜索时,屏幕截图显示文件视图的地址在左侧列中。 - Floris
4个回答

15

这就是我所做的事情:

#pragma pack(push,1)

typedef struct
{
  uint8  e_ident[16];
  uint16 e_type;
  uint16 e_machine;
  uint32 e_version;
  uint32 e_entry;
  uint32 e_phoff;
  uint32 e_shoff;
  uint32 e_flags;
  uint16 e_ehsize;
  uint16 e_phentsize;
  uint16 e_phnum;
  uint16 e_shentsize;
  uint16 e_shnum;
  uint16 e_shstrndx;
} Elf32Hdr;

typedef struct
{
  uint32 sh_name;
  uint32 sh_type;
  uint32 sh_flags;
  uint32 sh_addr;
  uint32 sh_offset;
  uint32 sh_size;
  uint32 sh_link;
  uint32 sh_info;
  uint32 sh_addralign;
  uint32 sh_entsize;
} Elf32SectHdr;

#pragma pack(pop)

{
  FILE* ElfFile = NULL;
  char* SectNames = NULL;
  Elf32Hdr elfHdr;
  Elf32SectHdr sectHdr;
  uint idx;

  // ...

  // read ELF header
  fread(&elfHdr, 1, sizeof elfHdr, ElfFile);

  // read section name string table
  // first, read its header
  fseek(ElfFile, elfHdr.e_shoff + elfHdr.e_shstrndx * sizeof sectHdr, SEEK_SET);
  fread(&sectHdr, 1, sizeof sectHdr, ElfFile);

  // next, read the section, string data
  SectNames = malloc(sectHdr.sh_size);
  fseek(ElfFile, sectHdr.sh_offset, SEEK_SET);
  fread(SectNames, 1, sectHdr.sh_size, ElfFile);

  // read all section headers
  for (idx = 0; idx < elfHdr.e_shnum; idx++)
  {
    const char* name = "";

    fseek(ElfFile, elfHdr.e_shoff + idx * sizeof sectHdr, SEEK_SET);
    fread(&sectHdr, 1, sizeof sectHdr, ElfFile);

    // print section name
    if (sectHdr.sh_name);
      name = SectNames + sectHdr.sh_name;
    printf("%2u %s\n", idx, name);
  }

  // ...
}

4
以下是关于获取节名称字符串表的方法:
  • ELF可执行文件头(EHDR)的e_shstrndx字段指定了描述包含节名称的字符串表的节头表条目的索引。
  • EHDR的e_shentsize字段指定了每个节头表条目的大小(以字节为单位)。
  • EHDR的e_shoff字段指定了节头表开始的文件偏移量。

因此,字符串表的头条目将位于文件偏移量处:

header_table_entry_offset = (e_shentsize * e_shstrndx) + e_shoff

根据对象的ELF类别,此标题表条目将是Elf32_ShdrElf64_Shdr。请注意,在运行程序的主机上,标题表条目的文件表示可能与其内存表示不同。
标题条目中的sh_offset字段将指定字符串表的起始文件偏移量,而sh_size字段将指定其大小。
进一步阅读:《libelf by Example》教程更深入地介绍了ELF可执行头和ELF节头表。

2

看起来你需要的是STRTAB,它的偏移量为0x441c(第20行)。文件头开始于文件的第17636个字节(根据)

Start of section headers:          17636 (bytes into file)

我猜测你的字符串表从文件开始处的17636 + 17436 = 35072字节处开始。

我可能完全错误,但这是我会开始的地方。你知道如何使用od或类似的实用程序对文件字节进行转储吗?


那么字符串表应该在文件的17436字节处?检查这两个是否正确应该很容易。 - Floris

0
节头表从您的文件起始点+e_shoff开始。 elf.h文件已经有Elfxx_Shdr结构和Elfxx_Ehdr结构,其中上面的xx分别为32或64,表示32位或64位。

您可以使用Elfxx_Ehdr中的e_shentsize获取节头字符串表索引,然后使用Elfxx_Shdr中的sh_namesh_offsetsh_name将为您提供strtab中的索引,如果加上sh_offset,那么将为您提供节的完整名称。对于类型、标志等,也可以使用sh_typesh_flag等来实现。

我建议您查看可执行和可链接格式维基百科文章以及efl.h文件手册页


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接