Just a self notes to refresh my mind on this, as it keep popping from time to time and I have to google it to answer the question.
- How program executed? - keyword "how main() executed".
- Wikipedia Execution (computing) - not much concrete information but still useful to trigger the long term memory section in your brain ;)
- Understanding the Memory Layout of Linux Executables - keyword - "linux start memory address".
- This post on building OS with Rust also informative on how OS being loaded and execute program.
- Attach to the a current running process by gathering its PID.
- Send a SIGSTOP to the program to halt it's execution
- Dump its registers (specifically rip/eip)
- Write your code to the stack where rip is pointing to.
- Send a SIGCONT to the program to return control.
Also look into how strace work.
- main() was defined in libc - the function in libc that assume the first function in C program to call is named
- What address to load that contain the start of the program is defined in ELF headers.