读取ELF文件的程序头内容

时间:2015-03-28 17:58:28

标签: c linux ubuntu linux-kernel elf

如何从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_offset p_filesz 决定的吗?

3 个答案:

答案 0 :(得分:3)

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);

答案 1 :(得分:1)

  

是否由当前程序标题的字段p_offset和p_filesz确定?

是的,确实。

答案 2 :(得分:0)

通过阅读e_phoff获取程序标头表地址,通过读取e_phnum来读取e_phentsize以及每个标头的大小,通过从elf文件标头中读取e_phentsize来获取程序标头表地址。诀窍是每个标题的大小都相同e_phentsize。因此,在每e_phnum后,新标头开始,标题为总reshape(aggregate(item ~ area + sector, data = df, paste, collapse = ","), idvar = "sector", timevar = "area", direction = "wide") sector item.East item.North item.South item.West 1 A <NA> <NA> Baidu,Tencent <NA> 5 B Microsoft,Google,Facebook <NA> <NA> <NA> 9 C <NA> Alibaba <NA> <NA> 13 D <NA> <NA> <NA> Amazon 17 E <NA> <NA> <NA> <NA>