The Journey Before main()
6 months ago
- #Kernel
- #ELF
- #Linux
- The article explores the process between the kernel being asked to run a program and the execution of the program's main function.
- The execve system call on Linux is used to signal the kernel to start loading a program, requiring the executable file's path, arguments, and environment variables.
- Higher-level language wrappers, like Rust's Command::new, handle path resolution similar to shells but the kernel requires a full path.
- Executable files on Linux use the ELF format, which includes headers and sections like .text (code), .data (initialized variables), and .bss (uninitialized variables).
- ELF files support dynamic linking through sections like the Procedure Linkage Table (PLT), allowing shared libraries like libc to be loaded at runtime.
- The kernel sets up the program's stack, which includes arguments (argv), environment variables (envp), and the ELF auxiliary vector (auxv) for system information.
- The entry point of a program is typically the _start function, which initializes the runtime before calling the main function.
- Different programming languages have varying runtime initialization processes, with languages like Rust and C having minimal setups compared to heavier runtimes like Java or Python.