Sefu
Sefu

Reputation: 2494

Replicating execve in c (Linux)?

I am doing a project to learn how a program is executed in Linux. Basically, I am trying to replicate the functionality of execve by running a series of system calls in a c program to take an executable binary, load it into memory, and successfully run it.

Are there any relatively easy-to-understand online resources (or tips) I can use to learn how to do this? I don't have much experience with this, and I'm trying to learn. It seems like a fairly complicated task, and I'm completely stuck at the moment.

Thank you.

Upvotes: 1

Views: 211

Answers (1)

immortal
immortal

Reputation: 3188

Your main problem here is that part of the exec system call is overriding the process descriptor in the kernel. It's something you can't do in userspace. Even if you close all file descriptors there are still plenty of other values you can't reach, nor can you free up dynamically loaded libraries and release you own program's code pages (since they would be write protected).

The basic approach to loading and running a code file would be to mmap it into the memory, then clear the stack, parse the ELF headers and jump to the program start function (assembly jmp instruction, mind you) But there's much more to an ELF file so it might not work without other initializations and dynamic linkage...

Upvotes: 2

Related Questions