What is the role of a loader when it loads a process on a linux machine. Does it only create the virtual address space or does it do some other things as well, like setting up stack pointers, initializing BSS segment to zeroes, etc. Or does the C runtime library has something to do with it?
Asked
Active
Viewed 229 times
2
-
I would expect the BSS, as well as any other initialized global variables, to be a part of the executable image loaded into the memory space of the process (so no runtime initialization is ever required for those variables). By the way, there is only one stack pointer. I would expect the executable image to contain the values that are relevant to the initial execution of the program at a well defined segment, so that the OS will be able to set up registers properly (for example: SP = stack base address, PC = address of `main`, etc). – barak manos Dec 26 '14 at 07:17
-
2@barakmanos: A single-threaded process only has a single stack pointer (so a process at startup only has a single stack pointer), but a multi-threaded process has a separate stack pointer for each thread. – Jonathan Leffler Dec 26 '14 at 07:25
-
@barakmanos: ELF executables don't have the BSS segment as such. The BSS segment is either set up by the loader or the runtime initialization - my question. As far as stack is considered, the address space is randomized so stack will not have a fixed address known to the compiler. – tapananand Dec 26 '14 at 07:25
-
@JonathanLeffler: But the threads (and their contexts, which include the SP values) can only be created at runtime, so how would the OS set SP values upon load? What I meant to say was, that at the beginning of execution there is only a single stack pointer. – barak manos Dec 26 '14 at 07:49
-
2The loader adapts the executable code to match the actual VM address where the code is loaded. Which is not always the intended address when there are loading conflicts with, say, two modules trying to get loaded at the same address or the address already committed to something else like heap or mmap. Or ASLR. PIC is very common on Linux btw. Spelled out in detail in [this blog post](http://eli.thegreenplace.net/2011/08/25/load-time-relocation-of-shared-libraries). – Hans Passant Dec 26 '14 at 10:27
-
@HansPassant: so are you saying that loader allows for relocation by doing things such as setting up the relocation register? And also is it the runtime initialization that sets up stack and bss? – tapananand Dec 27 '14 at 07:35
1 Answers
1
It does not create the address space; that's the kernel's job. The kernel also sets up a stack. The loader loads the program and libraries into the address space, including mapping zeroes into BSS segments, etc. Most Linux systems/programs use the loader that comes with the GNU C library, glibc.

SamB
- 9,039
- 5
- 49
- 56
-
-
@TapanAnand: Whatever it needs to do. The loader pretty much just loads the executable and libraries into the address space and deals with whatever relocations might need to be done; other libraries, including libc, are left to initialize themselves as appropriate. – SamB Dec 28 '14 at 03:41
-
-
1There is an answer at: http://stackoverflow.com/questions/27596818/difference-between-the-roles-of-loader-and-c-runtime-initialization – tapananand Dec 29 '14 at 06:32