Reputation: 6031
Suppose the binary is PIC, how can I load it into memory and execute the entry point?
I'm doing this to get familiar with ELF so execve
is not allowed.
Upvotes: 11
Views: 6108
Reputation: 215547
These are the basic steps:
mmap
assign you an address. This will reserve contiguous virtual address space.MAP_FIXED
.DYNAMIC
vector, which will in turn give you the address of the relocation vector(s).RELATIVE
relocations (just adding the base load address), meaning you don't have to perform any symbol lookups or anything fancy.Construct an ELF program entry stack consisting of the following sequence of system-word-sized values in an array on the stack:
ARGC ARGV[0] ARGV[1] ... ARGV[ARGC-1] 0 ENVIRON[0] ENVIRON[1] ... ENVIRON[N] 0 0
(This step requires ASM!) Point the stack pointer at the beginning of this array and jump to the loaded program's entry point address (which can be found in the program headers).
Upvotes: 10