I have been reading “Programmer Self-cultivation: Link Loading and Libraries” recently. Here are the study notes for chapter 10:

The stack frame

So, first of all, what is a stack?

In data structures, a stack is a linear table that limits insert or delete operations to the end of the table. A stack is a data structure that stores data according to the principle of last in, first out. The data entered first is pushed to the bottom of the stack, and the last data is placed at the top of the stack. When data needs to be read, the data will be ejected from the top of the stack.Copy the code
In a computer system, the stack can also be called stack memory is a dynamic memory area that stores local variables and method calls and function parameter values inside functions (including main function). It is automatically allocated by the system and is generally fast. The storage addresses are contiguous and there is a limited stack capacity, which can cause overflow. The program can push data onto the stack or pop data off the top of the stack. The push operation makes the stack larger, and the pop operation makes the stack smaller. The context in which the stack is used to maintain function calls cannot be implemented without them.Copy the code

And what is a Stack Frame? Each call to a function maintains a separate stack frame on the Call stack. Each independent stack frame generally includes:

  • The return address and arguments of the function
  • Temporary variables: Includes non-static local variables of a function and other temporary variables automatically generated by the compiler
  • The context stack of a function call extends from high address to low address, and the stack frame of a function is bounded by two registers, EBP, which points to the bottom of the current stack frame, and ESP, which points to the top of the stack frame. The EBP register is also called a Frame Pointer. The ESP register is also called a Stack Pointer.

A function call

Function calls are divided into the following steps:

  • Parameter pushing: push parameters into the system stack according to the call convention (C is from right to left).
  • Return address push: to push the address of the next instruction to be called in the current code area into the stack for the function to continue when it returns;
  • Code jump: the handler jumps the code area to the entry point of the called function;
  • Stack frame adjustment:

    1. The caller’s EBP is pushed onto the stack, and the address pointing to the bottom of the stack is saved (for on-site recovery after the function returns). At this time, ESP points to the new top position of the stack;push ebp

    2. Switch the current stack frame to the new stack frame (load THE EPS value into eBP and update the bottom of the stack frame), then the EBP points to the top of the stack, and the top of the stack is the old EBPmov ebp, esp

    3. Allocate space for new stack framessub esp, XXX

The function returns

The return from the function is divided into the following steps:

  • Saves the return value of the called function into the EAX registermov eax, xxx
  • Restore ESP and reclaim local variable spacemov ebp, esp
  • Restores the bottom position of the previous stack frame to eBPpop ebp
  • Pops the current top of the stack element, fetches the return address from the stack, and jumps to that locationret

At this point, the stack frames and function calls and returns are over, and there is some assembly involved here, but there is no record of different platform calling conventions and some special registers.

reference

  1. Programmer self-cultivation – linking, loading, and libraries. Chapter 10
  2. https://www.cnblogs.com/dwlsxj/p/Stack.html
  3. Compilation principle