C++ – Linux process loader in C++

Linux process loader in C++… here is a solution to the problem.

Linux process loader in C++

I’m using C++ to develop a process loader for the Linux platform; It’s just entertainment. Basically, I want to manually load all parts of the executable into memory and then execute it.

I asked a related but more specific question here About a week ago. However, after reading more, I realized that memory mapping was irrelevant until I learned how to actually create processes. So my question is, is there a way to create a “blank” new process on Linux with its own virtual address space that I will be able to access to load code and data?

Even a general resource about process loader development would be useful, as I haven’t found any yet.

Solution

If you’re interested in knowing what the Linux process loader really does behind the scenes when you ask the Linux process loader to perform the exec() operation, get a copy of the kernel source code and check out fs/exec.c for do_execve_common().

The ELF

handler itself is in fs/binfmt_elf.c, and if you try to run the ELF binary, it will eventually be called. In particular, load_elf_binary() performs the actual load, and the given struct linux_binprm contains the necessary information to mirror the loading process from disk.

Observe that Linux supports binary formats with many binfmt_*.c files; If you have a custom binary format, then you can, in principle, write your own binary format and provide it as a kernel module. The internal structure of process loading is opaque to user-mode code (and rightfully so: failure to do so would result in a serious security vulnerability).

Related Problems and Solutions