process of running linux executable-Collection of common programming errors
For dynamic linked programs, the kernel detects the PT_INTERP
header in the ELF file and first mmaps the dynamic linker (/lib/ld-linux.so.2
or similar), and starts execution at the e_entry
address from the main ELF header of the dynamic linker. The initial state of the stack contains the information the dynamic linker needs to find the main program binary (already in memory). It’s responsible for reading this and finding all the additional libraries that must be loaded, loading them, performing relocations, and jumping to the e_entry
address of the main program.
For static linked programs, the kernel uses the e_entry
address from the main program’s ELF header directly.
In either case, the main program begins with a routine written in assembly traditionally called _start
(but the name is not important as long as its address is in the e_entry
field of the ELF header). It uses the initial stack contents to determine argc
, argv
, environ
, etc. and calls the right implementation-internal functions (usually written in C) to run global constructors (if any) and perform any libc initialization needed prior to the entry to main
. This usually ends with a call to exit(main(argc, argv));
or equivalent.