What's inside the stack?

后端 未结 4 1076
逝去的感伤
逝去的感伤 2021-02-20 07:32

If I run a program, just like

#include 
int main(int argc, char *argv[], char *env[]) {
  printf(\"My references are at %p, %p, %p\\n\", &argc         


        
4条回答
  •  死守一世寂寞
    2021-02-20 08:07

    The content of the stack will vary depending on the architecture ABI, the compiler, and probably various compiler settings and options.

    A good place to start is the published ABI for your target architecture, then check that your particular compiler conforms to that standard. Ultimately you could analyse the assembler output of the compiler or observe the instruction level operation in your debugger.

    Remember also that a compiler need not initialise the stack, and will certainly not "clear it down", when it has finished with it, so when it is allocated to a process or thread, it might contain any value - even at power-on, SDRAM for example will not contain any specific or predictable value, if the physical RAM address has been previously used by another process since power on or even an earlier called function in the same process, the content will have whatever that process left in it. So just looking at the raw stack does not tell you much.

    Commonly a generic stack frame may contain the address that control will jump to when the function returns, the values of all the parameters passed, and the value of all auto local variables in the function. However the ARM ABI for example passes the first four arguments to a function in registers R0 to R3, and holds the return value of the leaf function in the LR register, so it is not as simple in all cases as the "typical" implementation I have suggested.

提交回复
热议问题