Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz.
Computer Architecture CSCE 350
Ch. 8 Functions.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
User-Level Memory Management in Linux Programming
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Run-Time Storage Organization
Run time vs. Compile time
Semantics of Calls and Returns
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
System Calls 1.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Topic 2d High-Level languages and Systems Software
Exploitation Of Windows Buffer Overflows. What is a Buffer Overflow A buffer overflow is when memory is copied to a location that is outside of its allocated.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements,
CNIT 127: Exploit Development Ch 1: Before you begin.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’. In.
Functions/Methods in Assembly
CS 155 Section 1 PP1 Eu-Jin Goh. Setting up Environment Demo.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Information Security - 2. A Stack Frame. Pushed to stack on function CALL The return address is copied to the CPU Instruction Pointer when the function.
1 Assembly Language: Function Calls Jennifer Rexford.
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Design issues for Object-Oriented Languages
Practical Session 3.
Lecture 3 Translation.
Instructions for test_function
Assembly function call convention
Chapter 14 Functions.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Virtualization Virtualize hardware resources through abstraction CPU
Computer Architecture and Assembly Language
Command line arguments
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Introduction to Compilers Tim Teitelbaum
Chapter 14 Functions.
Computer Architecture and Assembly Language
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
X86 Assembly Review.
“Way easier than when we were students”
Computer Architecture and System Programming Laboratory
Chapter 14 Functions.
Topic 2b ISA Support for High-Level Languages
Implementing Functions: Overview
Presentation transcript:

Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing

Executable Program File on Disk ● The executable binary program file on disk contains two segments: Text – read only program code Data – initialized global and local variables Text Data

Program Running in Memory ● A program running in memory consists of 3-4 segments: ECSlow mem EDS ESS EES high mem Text Data Stack Heap

Program Running in Memory ● Text segment – read-only binary program code ● Data segment – Initialized global variables – Uninitialized global variables, cleared to zero when program starts ● Stack segment – stack for activation records of functions, containing local variables ● Heap segment – memory for dynamic allocation

Pages of the Program ● Each segment is shown as one contiguous piece of memory pointed to by a segment register (ECS, EDS, ESS, EES), but the memory management feature of the operating system might actually implement each segment as physically made up of several separated pages, each page typically 4KB in size. While the program is running, when memory is tight. not all the pages are present in memory, but are fetch from disk when needed.

Intel Base Registers ● The segments of the program running on an Intel box are pointed to by the base registers, and the actual position in the segment is indicated by an offset to the base register: – ECS: code segment register, EPC: pointer to actual code – EDS: data segment register, offset: pointer to actual data – ESS: stack segment register, ESP: pointer to top of stack, EBP: stack marker

Sharing of Segments ● When two or more instances of the same program are running, they may share several segments: – Text segment (code) – shared, read-only – Data segment – shared, “copy on write” – Stack segment – separate ● The heap is managed by the operating system and portions thereof are given out to programs upon their request for memory allocation.

Shared, “Copy on Write” Data Segment ● When two or more instances of the same program are running, they initially share a single data segment. Only when one instance attempts to write to the data segment is a copy made for it, which becomes an unshared exclusive copy for that instance.

Environment of a Program ● When a program is executed, it inherits the environment of the shell program that started it. The environment is a list of strings of environment variables and their values: – “PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin” – “MANPATH=/usr/man:/usr/X11/man:/var/man” – “logname=pmana” – “TERM=linux” ● The environment is part of the data segment of the program

Command Line Arguments ● When the command to execute a program is given to the shell, the command is issued with command-line arguments. The format of the command is: progname arg1 arg2 arg3... argN The entire command line string is made available to the running program in its data segment.

Program Initialization ● When a program starts executing, the program initialization code (c0.asm in Turbo C and crt*.s in GCC) creates an activation record on the stack for the main() function: – Push onto the stack the address of a null-terminated array of pointers to the environment strings – Push onto the stack the address of an array of pointers to the command-line arguments – Push onto the stack the number of command-line arguments – Then call the function main().

The Main Program ● When “main()” executes it must go through a “function entry protocol” in order to be able to access the values in the activation record on the stack that has been prepared for it. Main() sees these values as the values of the variables argc, argv, envv in its declaration: int main(int argc, char *argv[], char *envv[]) As main() exits, it must go though a “function exit protocol” to arrange the return of an “int” value and to clean up whatever changes it made to its activation record and to the registers.

Function Entry Protocol ● All functions go through the same function entry protocol for C programs. For example if the program is “int func(int x, int y, int z) { int a, b, c;... } then, the function entry protocol is: func:push %ebp mov %esp, %ebp sub $12, %esp...

Activation Record for func ● Before func is called to execution, the calling program has prepared an activation record for func by pushing the values of z, y, and x on the stack (reverse order), so that x is on top. ● When func executes, it goes through its function entry protocol, making z, y, and x available within func via EBP, and reserving space on the stack for a, b, and c

Activation Record for func ● The activation record now looks like z y x return addr Old EBP a b c EBP ESP 8(EBP) 12(EBP) 16(EBP) -4(EBP) -8(EBP) -12(EBP) low mem high mem

Function Exit Protocol ● As a function exits and returns to the caller, it must undo the changes that it made to the activation record and to the registers, and must arrange to return a value to the EAX register, if the return value is an int. The function exit protocol is: mov %eds:value, %eax mov %ebp, %esp pop %ebp ret

Function Body ● To access the arguments and local variables within func, their offsets with respect to EBP are used. For example the assignment, b = z + 5 translates to the assembly code: mov 16(%ebp), %eax add $15, %eax mov %eax, -8(%ebp)