c - Reading the program header contents of an ELF file -
how possible extract loadable program headers individually elf files? examining binary using readelf 1 can output similar to:
$ readelf -l helloworld elf file type exec (executable file) entry point 0x400440 there 9 program headers, starting @ 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 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 question answers how loadable headers being mapped memory(and where) not specify where(from offset , size) sections read within given binary.
determined current program header's fields p_offset , p_filesz?
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; (; ph < eph; ph++) if(ph->p_type == pt_load) /*read_pload (dst address in memory, how many bytes read, offset in file) */ read_pload(ph->p_pa, ph->p_memsz, ph->p_offset);
Comments
Post a Comment