c - How do I load and execute an ELF binary executable manually? -
suppose binary pic, how can load memory , execute entry point? i'm doing familiar elf execve
not allowed.
these basic steps:
- read program headers find load directives , determine total length of mappings you'll need, in pages.
- map lowest-address load directive total length (which may greater file length), letting
mmap
assign address. reserve contiguous virtual address space. - map remining load directives on top of parts of mapping using
map_fixed
. - use program headers find
dynamic
vector, in turn give address of relocation vector(s). - apply relocations. assuming binary static-linked pie binary, should consist entirely of
relative
relocations (just adding base load address), meaning don't have perform symbol lookups or fancy. construct elf program entry stack consisting of following sequence of system-word-sized values in array on stack:
argc argv[0] argv[1] ... argv[argc-1] 0 environ[0] environ[1] ... environ[n] 0 0
(this step requires asm!) point stack pointer @ beginning of array , jump loaded program's entry point address (which can found in program headers).
Comments
Post a Comment