【问题标题】:In Linux, what are these elf.h objects?在 Linux 中,这些 elf.h 对象是什么?
【发布时间】:2013-10-30 21:24:34
【问题描述】:

来自 c (Linux) 中的 elf.h:

#include <elf.h>

以下指针代表/做什么?我尝试查找文档,但没有任何关于它的内容。我刚刚看到其他人使用它来实现 ELF 加载器的一些源代码:

Elf32_Ehdr
Elf32_Phdr
Elf32_Shdr
Elf32_Sym

谢谢!

【问题讨论】:

    标签: c linux kernel executable elf


    【解决方案1】:

    它们只是包含大量信息的结构。例如,Elf32_Ehdr 是这样定义的:

    typedef struct {
        unsigned char   e_ident[EI_NIDENT]; /* ident bytes */
        Elf32_Half  e_type;         /* file type */
        Elf32_Half  e_machine;      /* target machine */
        Elf32_Word  e_version;      /* file version */
        Elf32_Addr  e_entry;        /* start address */
        Elf32_Off   e_phoff;        /* phdr file offset */
        Elf32_Off   e_shoff;        /* shdr file offset */
        Elf32_Word  e_flags;        /* file flags */
        Elf32_Half  e_ehsize;       /* sizeof ehdr */
        Elf32_Half  e_phentsize;        /* sizeof phdr */
        Elf32_Half  e_phnum;        /* number phdrs */
        Elf32_Half  e_shentsize;        /* sizeof shdr */
        Elf32_Half  e_shnum;        /* number shdrs */
        Elf32_Half  e_shstrndx;     /* shdr string index */
    } Elf32_Ehdr;
    

    您可以在此处找到所有定义:

    http://www.opensource.apple.com/source/dtrace/dtrace-78/sys/elf.h

    【讨论】:

      【解决方案2】:

      ELF 是一种非常通用的二进制文件格式(例如可执行文件、核心转储)。您可以从Wikipedia article 链接阅读its specification。

      在您了解格式是什么样子后,您将了解 ELF 标头、程序标头、节标头等等。 elf.h 头文件定义了与 ELF 结构的那些部分匹配的 C 结构。您可以使用它们来编写自己的 ELF 文件或加载现有的。

      tl;dr:如果您不想编写自己的二进制可执行文件或手动解析核心转储,则不需要知道这一点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-16
        • 1970-01-01
        • 2017-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多