6
6
6y6eH4uk2019-06-16 18:59:31
linux
6y6eH4uk, 2019-06-16 18:59:31

How does the low-level code that uses the stack know where it starts from?

When the operating system starts a process, it allocates stack space for it, but how does low-level code know the starting address of the stack?
9Fk3Z.png
If we take this scheme as a basis, it becomes clear how the process code can find out the addresses of text, initialized data, uninitialized data, and the heap (just add the amount of memory allocated using system calls to the end of the BSS). However, it is not clear how the stack address and command line arguments are known.
If you think a little about the brains, then the most logical answer to this question is that when the process starts, the OS itself sets up (I think that in this example it doesn’t matter which OS, but I’m pushing away from the linux example) the stack registers to the desired values, but I have no experience assembly language programming (only theory), and all the sources that I read do not explicitly indicate this, so I cannot be sure of this assumption. Well, if you follow this assumption, then the addresses of the command line arguments can be found by counting them from the stack address.
If all my guesses are correct, then I would like to get an affirmative answer from people who know this for sure, but if another method is used, then I would like to get a detailed description of it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2019-06-16
@6y6eH4uk

If we take this scheme as a basis, it becomes clear how the process code can find out the addresses of text, initialized data, uninitialized data

If we are talking about ordinary (non-PIE) ELF executable files, then they are linked to a specific address where they will be loaded, and all addresses within one executable file are calculated in advance. Therefore, loading the address of a function or variable in the data section is simply loading a constant into a register.
The heap is different because the heap and the objects in it are completely dynamic. A heap allocation is always a system call that returns the address of the allocated memory (both sbrk and mmap).
they are initialized by the OS kernel according to the ABI, see for example ABI x86_64 , section 3.4.1.

F
freeExec, 2019-06-16
@freeExec

It is strange that you have a question about the stack, not just how the process starts its work exactly from the right address, and not from where? A lot of these settings are stored in the headers of executable files: both the stack size and the address of the entry point, and which libraries and where to load them. And all these preparations are handled by the bootloader in the OS.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question