【问题标题】:Reading the program header contents of an ELF file读取 ELF 文件的程序头内容
【发布时间】:2015-06-01 22:59:04
【问题描述】:

如何从 ELF 文件中单独提取可加载的程序头文件? 通过使用 readelf 检查二进制文件,可以获得类似于以下内容的输出:

$ readelf -l helloworld

Elf file type is EXEC (Executable file)
Entry point 0x400440
There are 9 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001f8 0x00000000000001f8  R E    8
  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x000000000000070c 0x000000000000070c  R E    200000
  LOAD           0x0000000000000e10 0x0000000000600e10 0x0000000000600e10
                 0x0000000000000230 0x0000000000000238  RW     200000
  DYNAMIC        0x0000000000000e28 0x0000000000600e28 0x0000000000600e28
                 0x00000000000001d0 0x00000000000001d0  RW     8
  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x00000000000005e4 0x00000000004005e4 0x00000000004005e4
                 0x0000000000000034 0x0000000000000034  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     10
  GNU_RELRO      0x0000000000000e10 0x0000000000600e10 0x0000000000600e10
                 0x00000000000001f0 0x00000000000001f0  R      1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
   03     .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss 
   04     .dynamic 
   05     .note.ABI-tag .note.gnu.build-id 
   06     .eh_frame_hdr 
   07     
   08     .init_array .fini_array .jcr .dynamic .got 

This 问题回答了如何将可加载的标头映射到内存(以及在哪里),但没有指定从哪里(从哪个偏移量和大小)是在给定二进制文件中读取的部分。
是由当前程序头的p_offsetp_filesz字段决定的吗?

【问题讨论】:

  • 为什么不检查readelf的来源?
  • 建议添加ELF标签。
  • @alk 源代码很长,可以评论更多。
  • 无中生有...尤其是免费的! ;-)

标签: c linux ubuntu linux-kernel elf


【解决方案1】:
struct Proghdr {
        uint32_t p_type;
        uint32_t p_offset;
        uint32_t p_va;
        uint32_t p_pa;
        uint32_t p_filesz;
        uint32_t p_memsz;
        uint32_t p_flags; 
        uint32_t p_align;
};


struct Elf *elf_header = ...
struct Proghdr *ph;
if (elf_header->e_magic != ELF_MAGIC)
    goto bad;
ph = (struct Proghdr *) ((uint8_t *) elf_header + elf_header->e_phoff);
eph = ph + ELFHDR->e_phnum;
for (; ph < eph; ph++)
    if(ph->p_type == PT_LOAD)
        /*read_pload (dst address in memory, how many bytes to read, offset in the file) */
        read_pload(ph->p_pa, ph->p_memsz, ph->p_offset);

【讨论】:

  • @Employed 俄语解释
  • read 采用 3 个参数:文件描述符、要读入的缓冲区和长度。你给它:物理地址,内存大小,文件偏移量。没有任何意义。
  • 你不认为这是一个重要的细节吗?此外,使用非标准签名命名您自己的函数read 是一个非常糟糕的主意(TM)。
  • 当然。它是仅 32 位的代码,它用 20 行代码回答了这个问题,其中一个简单的“是”就足够了,否则也没关系。
  • 有两个问题。第一个是“如何从 ELF 文件中单独提取可加载的程序头文件?”我两个都回答了。无论如何,感谢您的帮助,祝您好运。
【解决方案2】:

是由当前程序头的字段p_offset和p_filesz决定的吗?

是的,没错。

【讨论】:

    【解决方案3】:

    通过读取e_phoff获取程序头表地址,通过读取e_phnum获取头数(头数),通过从elf文件头中读取e_phentsize获取每个头的大小。诀窍是每个标题的大小相同e_phentsize。因此,在每个e_phentsize 之后,都会启动新的标头,并且标头总数为e_phnum

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 2013-04-08
      • 2015-05-17
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2012-05-08
      相关资源
      最近更新 更多