Is it the program or OS that is responsible for configuring the stack

This question is asked because the author is building a compiler (src โ†’ asm) and is in the process of writing code to generate assembly code.

One of the first things that happen when a program runs on a Linux operating system (on any OS) is that the stack is installed and the SP register is assigned a memory address at the beginning of the stack.

I was curious if I was not responsible for the above tasks using the program or the OS itself. If so, then the responsibility lies with the program, how is this achieved by the program (in particular, in the base Linux environment)?

What other actions should a separate program perform before it can begin to perform its main function?

Examples with comments on the i386 assembler NASM are welcome. In addition, any web resource that could help the author in his goal would be greatly appreciated.

+6
assembly linux
source share
3 answers

The OS will install SP for you and push the program arguments onto the stack. Nothing special is needed to run your program.

Regarding the actual stack layout on Linux when you run your program, you can check this document for details.

+1
source share

You can search ELF format search for linux. And PE-COFF for windows.

This is the way formats are executed, and this is what the OS knows about. On Linux, you have the modules responsible for loading the program, and it uses an executable format in another to load the program correctly.

EDIT: a hint from glibc gives the answer:

/* [snip] %esp The stack contains the arguments and environment: 0(%esp) argc 4(%esp) argv[0] ... (4*argc)(%esp) NULL (4*(argc+1))(%esp) envp[0] ... NULL [snip] */ [snip] _start: /* Clear the frame pointer. The ABI suggests this be done, to mark the outermost frame obviously. */ xorl %ebp, %ebp /* Extract the arguments as encoded on the stack and set up the arguments for `main': argc, argv. envp will be determined later in __libc_start_main. */ popl %esi /* Pop the argument count. */ movl %esp, %ecx /* argv starts just at the current stack top.*/ /* Before pushing the arguments align the stack to a 16-byte (SSE needs 16-byte alignment) boundary to avoid penalties from misaligned accesses. Thanks to Edward Seidl < seidl@janed.com > for pointing this out. */ andl $0xfffffff0, %esp pushl %eax /* Push garbage because we allocate 28 more bytes. */ /* Provide the highest stack address to the user code (for stacks which grow downwards). */ pushl %esp pushl %edx /* Push address of the shared library termination function. */ [snip] 
+2
source share

The operating system program loader is responsible for allocating stack space to executable processes. The stack is just one memory segment (look at the link map) that the loader fixes when loading the program into RAM for execution. Other segments include uninitialized memory and initialized memory. The bootloader is also called a โ€œroamingโ€ bootloader to indicate that it takes care of loading the program in a convenient place in memory.

When cross-compiling / binding for an embedded system, the link specification contains heap and stack information that is used at boot time.

0
source share

All Articles