Runtime Storage Organization Jeremy Singer updated version after




















![ldr r 2, [fp, #-8] ldr r 3, [fp, #-12] add r 3, ldr r 2, [fp, #-8] ldr r 3, [fp, #-12] add r 3,](https://slidetodoc.com/presentation_image_h/3285fb3a71ce7ed3223301c999a48d5d/image-21.jpg)
















- Slides: 37
Runtime Storage Organization Jeremy Singer (updated version after lecture on ) University of Glasgow
The specific details of this presentation are based on C programs running on a Unix platform (ideally Linux!) but general principles should apply to all programming languages and operating systems.
where is data stored? (and how long) • Local variables (sometimes called automatic variables) live on the runtime stack. int x; x=1; &x; int a, b, c, d, e, f; (spilled) • Function parameter values also live on the runtime stack. • Dynamically allocated data (from malloc or new) lives on the runtime heap. (generally survives for longer than a single functioncall) • Global variables and static variables live in the data segment generally live for the entire life of the program • (or perhaps the bss segment, for uninitialized data) • Binary code lives in the text segment
A high-level view of process virtual address space Stack grows downwards (a descending stack) as more data is pushed onto it. Note there is one stack per thread, whereas only one heap per process. Heap grows upwards as dynamic data expands. TOP stack pointer system brk heap bss data text 0
Q: Look at this program fragment: where is each data element located in runtime memory? int x; // lives in bss int y = 42; // lives in data int f(int i) { // f lives in text, i is on stack static int z = 3; // z lives in the data segment int tmp = x+y; // tmp lives in the stack void *p = malloc(20*sizeof(char)); // p is on the stack, but it points to data on the heap // the malloc’d data leaks return tmp*z; }
Some hints about runtime memory regions • For C source code, do gcc –S file. c and look at the assembler, look for. text, . data, . bss directives – see which data lives in each segment (or use objdump to disassemble an object file) • For a running process, look at /proc/PID/maps and see what resides in each part of memory. You should see the program, the [heap], mmap’d libraries, and the [stack]. If you mmap a file yourself, you should see this in the virtual address space too
For the rest of this lecture … • we will focus on the stack and the heap – these are the most dynamic memory areas at runtime
The stack • There is one stack per thread • The stack grows downwards in memory • A stack is composed of stack frames – one frame per function invocation • Generally, there are two registers associated with the current (bottom!) stack frame – the frame pointer (FP, or %rbp) and the stack pointer (SP, or %rsp). • Let’s look at the anatomy of a single stack frame …
A single stack frame previous stack frame caller return address previous frame pointer fixed for the lifetime of the frame local variables for this function stack pointer changes as data is pushed/popped empty space
What might live in a stack frame? • local variables • callee-save registers (non-volatile registers, whose data is preserved across function calls) from caller function • spilled temporary values • parameters for functions we are going to call • stack-allocated data, e. g. using the alloca library function
The life of a stack frame • a stack frame is created during a function prolog – part of the sequence of instructions that executes during a function call • a stack frame is destroyed during a function epilog – part of the sequence of instructions that executes during a function return • when a stack frame is destroyed, all its data is lost
A stack of stack frames, showing linkage between them caller return address frame pointer previous frame pointer local variables for this function stack pointer
A stack of stack frames, showing linkage between them • set up arguments, as part of function call preparation in caller return address frame pointer previous frame pointer local variables for this function pushed argument values for callee function stack pointer
A stack of stack frames, showing linkage between them • Issue the CALL instruction, pushing the return address onto the stack caller return address frame pointer previous frame pointer local variables for this function pushed argument values for callee function return address stack pointer
A stack of stack frames, showing linkage between them the prolog will save the old frame pointer, and set up a new frame (updates the frame ptr reg) caller return address frame pointer previous frame pointer local variables for this function pushed argument values for callee function caller return address caller frame pointer stack pointer
A stack of stack frames, showing linkage between them begin computing in this callee function caller return address previous frame pointer local variables for this function pushed argument values for callee function caller return address caller frame pointer local variables for this function frame pointer stack pointer
Procedure Calling Convention • at a function call, there are several things we need to remember • what are the parameter values for this call? – context for the callee function • these are stored in registers or pushed to the stack, at the top of the caller stack frame • what needs to be saved? – context from the caller function • the return address, pushed as the final entry in the caller stack frame • the caller stack frame pointer, pushed as the first entry in the callee stack frame • non-volatile registers (values must be preserved across the function call)
Case study: example ARM 32 -bit Procedure Call Standard on Linux • See https: //en. wikipedia. org/wiki/Calling_convention#ARM_. 28 A 32. 29 for details
Example assembler code (gcc –O 0 –S) int x} () int f = 3; int g = 4; char c[4] = { 'f', 'o', '