Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1181

Appendix C: Notes on C


Frame 1

Frame 2

Frame 3

Frame Pointer

Stack Pointer

Return Address
Old Frame Pointer

Local
Variable

Parameters for
Function Call

Stack
Frame 2 in Detail

Figure C-1: Activation records on the stack.

Figure C-1 also shows an enlargement of the second stack frame indicating its constituent parts, as
follows:


❑ At the top of the stack are the return address and the stored frame pointer value. While the
return address specifies the point in memory at which code flow resumes at the end of the pro-
cedure, the stored frame pointer holds the value of the frame pointer for thepreviousactivation
record. After completion of the local procedure,this value can be used to reconstruct the stack
area available to the calling procedure — this is important when attempting to debug a stack
trace.
❑ The main part of the activation record is the memory space reserved for local variables of the
procedure. In C, these variables are also referred to asautomaticvariables.
❑ Values to be passed as parameters to another function when the function is invoked are stored at
the bottom of the stack.

All common architectures provide the following two stack manipulation commands:


❑ pushplaces a value on the stack anddecrementsthe stack pointer by the number of bytes in mem-
ory required by the value. The end of the stack is moved down to lower addresses.
❑ popremoves a value from the stack andincrementsthe value of the stack pointer
accordingly — in other words, the end of the stack is moved up.

The following two commands are also provided to invoke and exit functions (with automatic return to
the calling procedure) — they also automatically manipulate the stack:


❑ callpushes the current value of the instruction pointer onto the stack and branches to the start
address of the function to be called.
❑ returnpops the bottom value from the stack and branches to the specified address. Procedures
must be implemented so thatreturnis the last command and the address placed on the stack by
callis at the bottom.
Free download pdf