The function stack structure is aligned with memory

function stack

The following figure is the structure of the function stack of x86-64, the process of function P calling function Q, and Q is executing.
write picture description here
Only two places are explained here, the others are easy to understand, so I won't go into details.

  • The return address is used to implement the return of the function. When Q wants to return to P, it will call this address to get the return location.
  • The parameter construction area is a copy of the parameters passed by the calling function P, because when P passes too many parameters, it cannot be passed through registers, so it has to be saved in the stack frame of P; when calling Q, it is copied to the parameter construction area

x86-64 procedures allocate only the portion of the stack frame they need, and many functions don't even need a stack frame at all.

transfer of control

instruction describe
call Label procedure call
call *Operand procedure call
right return from procedure call

data alignment

Many computer systems impose some restrictions on the legal addresses of basic data types, requiring that the address of an object of a certain type must be a multiple of some value K (usually 2, 4, or 8). One is for reading efficiency, and the other is some hardware device requirements.

K Types of
1 char
2 short
4 int, float
8 long, double, char*

The alignment takes the minimum value of the system requirement value and the current data type, that is, min(data, Sys); for example, for data of type int, if the system requirement is 8, then the alignment requirement of int in memory is 4 = min(4, 8 ).
The data structure struct also needs to be aligned, and its alignment size takes the value of the largest alignment requirement in the structure. In this way, as long as the structure satisfies the alignment, the data in it can also satisfy the alignment; otherwise, if the structure does not satisfy the alignment, there will always be unsatisfactory situations in how the data in the structure is adjusted.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325908143&siteId=291194637