It's next to impossible to exec into an arbitrary memory image.
ELF files must be mmaped in a particular way in the process address space, almost never as a contiguous image of the underlying file.
The closest workable approach I can think of is saving the image into a (possibly nameless) file and exec'ing that file via execveat(fd, "", ...)
. Check execveat(2) around where they talk about empty pathname, and open(2) on O_TMPFILE.
A named temp file and a regular execve(2) would work as well, as long as the gap between close/munmap and execve when the file may be changed is not an issue.
Another idea would be to do ELF loading completely in userspace, setting up the proper address space for the new process, calling LD and such. Definitely not easy. Does not require any execve(2) call, just fork and work from there. Because execve(2) won't be used, certain things like O_CLOEXEC handling and un-mmaping extra memory will require non-trivial workarounds.