ELF中64位符号table条目的格式

Format of 64 bit symbol table entry in ELF

所以我试图通过仔细查看所有内容之间的关系来了解 ELF,但无法理解为什么符号 table 条目是它们的大小。

当我 运行 readelf -W -S tiny.o 我得到:

    Section Headers:
      [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
      [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
      [ 1] .bss              NOBITS          0000000000000000 000200 000001 00  WA  0   0  4
      [ 2] .text             PROGBITS        0000000000000000 000200 00002a 00  AX  0   0 16
      [ 3] .shstrtab         STRTAB          0000000000000000 000230 000031 00      0   0  1
      [ 4] .symtab           SYMTAB          0000000000000000 000270 000090 18      5   5  4
      [ 5] .strtab           STRTAB          0000000000000000 000300 000015 00      0   0  1
      [ 6] .rela.text        RELA            0000000000000000 000320 000030 18      4   2  4

显示符号 table 每个条目有 0x18 或 24 个字节,总大小为 (0x300-0x270) 或 0x90,给我们 6 个条目。

这与 readelf -W -s tiny.o 所说的匹配:

    Symbol table '.symtab' contains 6 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
         1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS tiny.asm
         2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
         3: 0000000000000000     0 SECTION LOCAL  DEFAULT    2 
         4: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    1 str
         5: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    2 _start

很明显,24 字节的大小是正确的,但这将对应于 this 32 bit spec 中描述的 32 位 table 条目。

鉴于我使用的是 64 位系统并且 ELF 文件是 64 位的,我希望该条目与 this 64 bit spec 中描述的一样。

在查看文件的十六进制转储后,我发现文件中字段的布局似乎是根据这个 64 位模式。

那么,尽管使用 64 位布局并且是 64 位文件,为什么 ELF 文件似乎使用尺寸过小的符号 table 条目?

So then why is the ELF file seemingly using undersized symbol table entries

是什么让您认为它们尺寸偏小?

Elf64_Sym中,我们有:

int    st_name
char   st_info
char   st_other
short  st_shndx
                <--- 8 bytes
long   st_value
                <--- 8 bytes
long   st_size
                <--- 8 bytes.

总共 24 个字节,与您预期的完全一样。

为了让自己相信一切正常,编译这个程序:

#include <elf.h>
#include <stdio.h>

int main()
{
  Elf64_Sym s64;
  Elf32_Sym s32;
  printf("%zu %zu\n", sizeof(s32), sizeof(s64));
  return 0;
}

运行 它产生 16 24。也可以在GDB下运行,查看各个字段的偏移量,例如

(gdb) p (char*)&s64.st_value - (char*)&s64
 = 8
(gdb) p (char*)&s64.st_size - (char*)&s64
 = 16