Re: how does process initialization work?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



ljp <lonnie.princehouse@xxxxxxxxx> writes:

> Specifically, how do the heap and stack get set up, and where do they reside
> in address space?  Is it glibc that's responsible for doing this, or the
> kernel, or some code added by gcc that executes before main() ?  Is thread
> local storage set up at the same time, and how does TLS currently work on
> Linux anyway?  I know that at least some of the memory layout comes from the
> linker (and I've read through linker scripts), but where's the code that
> makes this happen?

The linker script, when there is a linker script, is responsible for
laying out the executable (the code and global variables) in memory.
This is expressed in the executable as program segments (use readelf
-l to dump them).  The PT_LOAD program segments tell the kernel how to
load the flie into memory.

The kernel sets up the stack before it starts executing the program.
This basically consists of setting the stack pointer register and
allocating a page marked with the mmap option MAP_GROWSDOWN.

The heap works in two ways.  One way is the sbrk system call, which
adds pages beyond the end of where the program was loaded into memory.
Thus the linker script, if there is one, controls this.  In the old
days the C library used sbrk for memory allocation.  However, this
days it mostly uses anonymous mmap--every time the heap runs out of
memory, glibc uses mmap to grab some more.

TLS is different and the details are highly processor dependent.  The
Sun docs and Ulrich Drepper's paper give many of the details, although
there is quite a lot of interaction between the kernel and the dynamic
linker to get the space set up as each new thread is created.

Ian

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux