170 likes | 264 Views
Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing. Memory Image of Running Programs. Executable Program File on Disk.
E N D
Executable file on disk,running program in memory,activation record,C-style and Pascal-styleparameter passing Memory Image of Running Programs
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:ECS low memEDSESSEES 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 ... argNThe 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 low mem ESP -12(EBP) c -8(EBP) b -4(EBP) EBP a Old EBP return addr x 8(EBP) y 12(EBP) 16(EBP) z 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)