3

Is there good documentation of what happen when I run some executable in Linux. For example: I start ./a.out, so probably some bootloader assembly is run (come with c runtime?), and it finds start symbol in program, doing dynamic relocation, finally call main.

I know the above is not correct, but looking for detailed documentation of how this process happen. Can you please explain, or point to links or books that do?

1

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, R, but how does kernel know that program is dynamically linked or statically linked? And are not all Linux programs dynamic linked (At least with libc?)
As much as Drepper wishes you had to dynamic link everything, no, there is no requirement to use dynamic linking. gcc -static will generate a static-linked binary. The kernel knows the difference by the presence or absence of a PT_INTERP program header in the ELF.
R.. - where can I found the source code of dynamic linker to learn? I mean of ld-linux?
It's part of glibc, and it's rather unwieldy reading... There's a simpler version that comes with uClibc which might be more educational to read.
1

A book "Linker and Loader" gives a detail description about the loading process. Maybe it can give you some help on the problem.

2 Comments

but does it talk about modern linux?
It talks more about the general principles. You can find linux-related documents follow @Torp 's advice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.