QUESTION
Where does the code that runs before main() come from?
The ELF loader does not load an executable by consulting section names. It creates VMAs from the file offsets, virtual addresses, and permissions specified by PT_LOAD program headers, and also loads the dynamic linker when PT_INTERP is present.
The first userspace instruction of a new process image is the ELF entry point, not main. In a typical glibc executable, _start from crt1 extracts argc and argv from the initial stack and passes them to __libc_start_main.
STRUCTURE
Structure diagram
PT_LOAD creates runtime mappings; the section table does not directly determine this layout. ASLR can change addresses on each execution.
CALL PATH
Call path
The section table is primarily link/debug information; program headers determine runtime mappings. Match readelf -l output against /proc/PID/maps.
This diagram is not for memorizing function names. Follow which return value, file descriptor, memory mapping, or wait queue is passed to the next stage.
SOURCE COORDINATES
Linux 6.18.37 LTS source locations
Go beyond the glibc function to the files where the syscall implementation meets kernel objects. Each link points to the original file at the same tag.
| File | Function / structure | What to inspect |
|---|---|---|
| fs/exec.c | do_execveat_common(), begin_new_exec() | The point that discards the old address space and commits to the new binary |
| fs/binfmt_elf.c | load_elf_binary(), create_elf_tables() | PT_LOAD mappings and initial-stack construction |
| arch/x86/include/asm/processor.h | start_thread() | Set the new instruction pointer and stack pointer |
COMPLETE PROGRAM
Complete runnable example
The code below is not pseudocode with explanatory lines omitted. It is a minimal example that can be built and run as a file.
cc -std=c17 -Wall -Wextra -O2 elf_start.c -o elf_start01#include <stdio.h>
02#include <stdlib.h>
03
04static void before_main(void) __attribute__((constructor));
05static void after_main(void) __attribute__((destructor));
06
07static void before_main(void)
08{
09 puts("constructor: runtime is ready");
10}
11
12static void after_main(void)
13{
14 puts("destructor: normal exit path");
15}
16
17int main(int argc, char **argv, char **envp)
18{
19 printf("main: argc=%d argv0=%s\n", argc, argv[0]);
20 printf("argv=%p envp=%p\n", (void *)argv, (void *)envp);
21 return EXIT_SUCCESS;
22}
CODE NOTES
Code notes
__attribute__((constructor))The linker places the function address in .init_array, and the C runtime walks the array before main. The ELF entry point itself is not changed to this function.
__attribute__((destructor))Called through .fini_array on the normal exit path. It does not run after _exit(), a fatal signal, or power loss.
puts("constructorDynamic relocations and libc initialization are complete by this point, so stdio is available.
int main(int argcThe original argc/argv/envp begin on the initial stack built by the kernel, but the C runtime reconstructs the call to main according to the ABI.
return EXIT_SUCCESSReturning from main leads to exit() inside __libc_start_main, which runs atexit handlers and flushes stdio.
DETAILS
Detailed behavior
PT_LOAD is the blueprint for a VMA
p_offset and p_vaddr must have matching page offsets. The loader maps the file-backed range and fills the tail where p_memsz exceeds p_filesz with 0 to create .bss.
Check W^X policy in the program-header permissions and the mprotect operations performed for relocations.
The initial stack contains more than strings
The auxiliary vector follows argc, the argv pointer array, and the envp pointer array. libc and the dynamic linker consume entries such as AT_PHDR, AT_ENTRY, AT_RANDOM, and AT_SYSINFO_EHDR.
getauxval() reads these values without requiring you to parse /proc/self/auxv directly.
A successful exec has no return path
When execve succeeds, it replaces the calling process's code, data, and stack, so execution never returns to the next instruction in the old image. -1 and errno are returned only on failure.
When called from a multithreaded process, all other threads disappear and only the calling thread becomes the initial thread of the new image.
OBJECTS
Objects and lifetimes
| Object | Creation and release | Values to inspect |
|---|---|---|
linux_binprm | Exists temporarily during exec preparation and is consumed by the binary handler | file, buf, argc/envc |
PT_LOAD VMA | Created by exec and retained until munmap, the next exec, or process exit | offset, protection, p_filesz/p_memsz |
initial stack | Built by create_elf_tables and consumed by _start and libc | argc, argv, envp, auxv |
FAILURE PATH
Failure conditions and common misconceptions
| Observed symptom | Likely causes | How to verify |
|---|---|---|
| ENOEXEC | ELF magic, architecture, or format mismatch | file, readelf -h, kernel log |
| ENOENT even though the file exists | The dynamic linker named by PT_INTERP is absent | Inspect the interpreter reported by readelf -l |
| A crash occurs before main | A relocation, constructor, stack, or ABI problem | LD_DEBUG, gdb starti, core dump |
LAB
Verify it yourself
- Compare the LOAD addresses from readelf -l with /proc/$PID/maps at runtime for PIE and non-PIE builds.
- Use starti in gdb to inspect the stack at the first _start instruction, and use x/32gx $rsp to find argc and the pointer arrays.
- Change the program to call _exit(0) and verify that destructors and stdio flushing do not run.
./elf_start one tworeadelf -h -l ./elf_start && strace -f -e execve,mmap,mprotect ./elf_startPRIMARY REFERENCES
Primary sources
On RISC-V, distinguish the ISA from the calling convention
Instruction support does not establish binary-call compatibility. This conceptual comparison uses psABI revision 3095deae52ae; it does not report a RISC-V build or run of the x86-64 example above.
The standard integer convention passes arguments in a0–a7 and also uses the first two for return values. Type-specific rules still apply, including an implicit pointer for large returns. The callee preserves s0–s11; gp and tp are not ordinary scratch registers. A frame pointer is optional and uses s0 when present.
The standard ABI keeps sp 16-byte aligned from procedure entry throughout execution. The first stack argument is at entry sp+0. ILP32E has different rules, including a0–a5 arguments, s0–s1 preservation and 4-byte stack alignment.
The hardware floating-point convention adds fa0–fa7 for named arguments. Preservation of fs0–fs11 applies to values no wider than ABI_FLEN; variadic arguments follow the integer convention. F/D instruction support alone does not choose this calling convention. In the standard vector convention, v0–v31 are temporary registers and are not used for arguments or returns. The vector variant has separate rules, including preservation of v1–v7 and v24–v31.
Inspect ELF EI_CLASS, EI_DATA and e_flags. Float ABI flags describe call-boundary value passing, not permission to generate floating-point instructions internally. With the relevant relocations and range conditions, R_RISCV_RELAX permits call relaxation such as reducing AUIPC+JALR to JAL; that optimization cannot repair incompatible ABIs.
This revision lists the usual ILP32/LP64 floating-point variants as ratified, and ILP32E, RV64ILP32 variants and big-endian ABIs as draft. Do not equate an ISA extension's status with an ABI's status. Testing a real combination requires compiler/linker versions, ISA/ABI options and the resulting ELF and disassembly; no such execution validation is claimed here.
Sources: RISC-V International, ABI status, calling conventions and ELF and relaxation (CC BY 4.0). This is a summary of that fixed revision. See also atomic mapping compatibility.