【问题标题】:Why the target OS is set to HP-UX instead of Linux in this elf file?为什么这个 elf 文件中的目标操作系统设置为 HP-UX 而不是 Linux?
【发布时间】:2020-07-19 15:53:36
【问题描述】:

来自this wiki

它指出偏移量 7 处的数字标识目标操作系统。

我已经为一台 linux 机器编译了一个 c 程序,并检查了结果文件的 elf header(前 64 个字节):

> xxd -l 64 helloworld
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
00000010: 0300 3e00 0100 0000 8012 0000 0000 0000  ..>.............
00000020: 4000 0000 0000 0000 183f 0000 0000 0000  @........?......
00000030: 0000 0000 4000 3800 0d00 4000 1f00 1e00  ....@.8...@.....

为什么我在第 7 个字节得到 01?不应该是03吗?

【问题讨论】:

    标签: c linux elf


    【解决方案1】:

    为什么我的第 7 个字节是 01?

    偏移量7的字节是第8个字节,它是0,即System V1 用于 e_ident[EI_VERSION],它设置为 1 表示“为 ELF 的原始和当前版本设置为 1”。

    7f45 4c46 0201 0100 ...
                     ^^ - OSABI
                   ^^ - VERSION
                ^^  DATA
              ^^ CLASS
    ^^^^^^^^^ - MAG{0..3}
    

    不应该是03吗?

    正如您的链接所解释的,“无论目标平台如何,它通常都设置为 0”。

    【讨论】:

    • 为什么是System V 而不是Linux
    • @Klaus 精灵魔法不应该只有 4 个字节(7f45 4c46)吗?
    • @StackExchange123 解释是这样的:因为这个可执行文件需要的目标操作系统 ABI 需要与 SystemV 兼容,而不是专门与 Linux 兼容。
    • @Klaus 不,他没有。幻数有 4 个字节。
    【解决方案2】:

    来自 binutils 标头描述:

      35 #define EI_MAG0     0   /* File identification byte 0 index */
      36 #define ELFMAG0        0x7F /* Magic number byte 0 */
      37 
      38 #define EI_MAG1     1   /* File identification byte 1 index */
      39 #define ELFMAG1         'E' /* Magic number byte 1 */
      40 
      41 #define EI_MAG2     2   /* File identification byte 2 index */
      42 #define ELFMAG2         'L' /* Magic number byte 2 */
      43 
      44 #define EI_MAG3     3   /* File identification byte 3 index */
      45 #define ELFMAG3         'F' /* Magic number byte 3 */
      46 
      47 #define EI_CLASS    4   /* File class */
      48 #define ELFCLASSNONE          0 /* Invalid class */
      49 #define ELFCLASS32        1 /* 32-bit objects */
      50 #define ELFCLASS64        2 /* 64-bit objects */
      51 
      52 #define EI_DATA     5   /* Data encoding */
      53 #define ELFDATANONE       0 /* Invalid data encoding */
      54 #define ELFDATA2LSB       1 /* 2's complement, little endian */
      55 #define ELFDATA2MSB       2 /* 2's complement, big endian */
      56 
      57 #define EI_VERSION  6   /* File version */
      58 
      59 #define EI_OSABI    7   /* Operating System/ABI indication */
      60 #define ELFOSABI_NONE         0 /* UNIX System V ABI */
      61 #define ELFOSABI_HPUX         1 /* HP-UX operating system */
      62 #define ELFOSABI_NETBSD       2 /* NetBSD */
      63 #define ELFOSABI_GNU          3 /* GNU */
      64 #define ELFOSABI_LINUX        3 /* Alias for ELFOSABI_GNU */
      65 #define ELFOSABI_SOLARIS      6 /* Solaris */
      66 #define ELFOSABI_AIX          7 /* AIX */
      67 #define ELFOSABI_IRIX         8 /* IRIX */
      68 #define ELFOSABI_FREEBSD      9 /* FreeBSD */
      ...
    

    但是为什么有人应该在精灵标题中寻找位?如果您想要文本标题描述,请使用工具!

    readelf -h a.out

    ELF Header:
      Magic:   7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00 
      Class:                             ELF64
      Data:                              2's complement, little endian
      Version:                           1 (current)
      OS/ABI:                            UNIX - GNU
      ABI Version:                       0
      Type:                              EXEC (Executable file)
      Machine:                           Advanced Micro Devices X86-64
      Version:                           0x1
      Entry point address:               0x4013f0
      Start of program headers:          64 (bytes into file)
      Start of section headers:          486392 (bytes into file)
      Flags:                             0x0
      Size of this header:               64 (bytes)
      Size of program headers:           56 (bytes)
      Number of program headers:         11
      Size of section headers:           64 (bytes)
      Number of section headers:         38
      Section header string table index: 37
    

    【讨论】:

    • “readelf”提供的!如果您有疑问,请查看 binutils 源文件,那里会找到当前 elf 文件的头结构。但同样:如果您正在使用某种机器,请使用可用的工具。如果您能够编译,您通常也可以使用阅读器工具。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2018-04-26
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多