cs503 operating systems spring 2014 part 0 program structure n.
Skip this Video
Loading SlideShow in 5 Seconds..
CS503: Operating Systems Spring 2014 Part 0: Program Structure PowerPoint Presentation
Download Presentation
CS503: Operating Systems Spring 2014 Part 0: Program Structure

CS503: Operating Systems Spring 2014 Part 0: Program Structure

107 Vues Download Presentation
Télécharger la présentation

CS503: Operating Systems Spring 2014 Part 0: Program Structure

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. CS503: Operating SystemsSpring 2014Part 0: Program Structure Dongyan Xu Department of Computer SciencePurdue University

  2. Virtual Address Space of a Program • A program sees (virtual) memory as an array of bytes that goes from address 0 to 232-1 (0 to 4GB-1) • That is assuming a 32-bit architecture. (4GB-1) 232-1 0

  3. Memory Sections • The virtual address space is organized into sections: 232-1 Stack Shared Libs Heap Bss Data Text 0

  4. Memory Sections • Each section has different permissions: read/write/execute or a combination of them. • Text- Instructions that the program runs • Data – Initialized global variables. • Bss – Uninitialized global variables. They are initialized to zeroes (saving object file size). • Heap – Memory returned when calling malloc/new. It grows upwards (from low to high addr). • Stack – It stores local variables and return addresses. It grows downwards (from high to low addr).

  5. Memory Sections • Dynamic libraries – They are libraries shared with other processes. • Each dynamic library has its own text, data, and bss. • Each run of the program (a.k.a. process) has its own view of the memory that is independent of each other. • This view is called the “Virtual Address Space” of the process. • If a process modifies a byte in its own address space, it will not modify the address space of another process.

  6. Example Program hello.c int a = 5; // Stored in ?section int b[20]; // Stored in ? section int main() { // Stored in ? section int x; // Stored in ? section int *p =(int*) malloc(sizeof(int)); //In ? section }

  7. Building a Program • The programmer writes a program hello.c • The preprocessor expands #define, #include, #ifdefetc preprocessor statements and generates a hello.i file. • The compiler compiles hello.i, optimizes it and generates an assembly instruction listing hello.s • The assembler (as) assembles hello.s and generates an object file hello.o • The compiler (cc or gcc) by default hides all these intermediate steps. You can use compiler options to run each step independently.

  8. Building a program • The linker puts together all object files as well as the object files in static libraries. • The linker also takes the definitions in shared libraries and verifies that the symbols (functions and variables) needed by the program are completely satisfied. • If there is symbol that is not defined in either the executable or shared libraries, the linker will give an error. • Static libraries (.a files) are added to the executable. shared libraries (.so files) are not added to the executable file.

  9. Building a Program hello.c hello.i C Preprocessor Editor Compiler(cc) Optimizer Programmer hello.s Executable File (hello) hello.o (static) Linker (ld) Assembler (as) Shared Libraries (.so files). Only definitions. It does not add to size of executable. Other .o files Static libraries (.a files) They add to the size of the executable.

  10. Original file hello.c #include <stdio.h> main() { printf("Hello\n"); }

  11. After preprocessor gcc -E hello.c > hello.i (-E stops compiler after running preprocessor) hello.i: /* Expanded /usr/include/stdio.h */ typedef void *__va_list; typedefstruct __FILE __FILE; typedefintssize_t; struct FILE {…}; extern intfprintf(FILE *, const char *, ...); extern intfscanf(FILE *, const char *, ...); extern intprintf(const char *, ...); /* and more */ main() { printf("Hello\n"); }

  12. After Compilation gcc -S hello.c (-S stops compiler after compilation) hello.s: .align 8 .LLC0: .asciz "Hello\n" .section ".text" .align 4 .global main .type main,#function .proc 04 main: save %sp, -112, %sp sethi %hi(.LLC0), %o1 or %o1, %lo(.LLC0), %o0 call printf, 0 nop .LL2: ret restore .

  13. Output of assembler: Object file • “gcc -c hello.c” generates hello.o • hello.o has undefined symbols, like the printf function call that we don’t know where it is placed. • The main function already has a value relative to the object file hello.o csh> nm -xv hello.o hello.o: [Index] Value Size Type Bind Other Shndx Name [1] |0x00000000|0x00000000|FILE |LOCL |0 |ABS |hello.c [2] |0x00000000|0x00000000|NOTY |LOCL |0 |2 |gcc2_compiled [3] |0x00000000|0x00000000|SECT |LOCL |0 |2 | [4] |0x00000000|0x00000000|SECT |LOCL |0 |3 | [5] |0x00000000|0x00000000|NOTY |GLOB |0 |UNDEF |printf [6] |0x00000000|0x0000001c|FUNC |GLOB |0 |2 |main

  14. Object file contains the following: • Object file header • Size and position of other sections • Text segment • Data segment • Relocation information • Symbol table • Labels not defined such as external references • Debugging information

  15. After linking • “gcc –o hello hello.c” generates the hello executable • Printf does not have a value yet until the program is loaded csh> nm hello [Index] Value Size Type Bind Other Shndx Name [29] |0x00010000|0x00000000|OBJT |LOCL |0 |1 |_START_ [65] |0x0001042c|0x00000074|FUNC |GLOB |0 |9 |_start [43] |0x00010564|0x00000000|FUNC |LOCL |0 |9 |fini_dummy [60] |0x000105c4|0x0000001c|FUNC |GLOB |0 |9 |main [71] |0x000206d8|0x00000000|FUNC |GLOB |0 |UNDEF |atexit [72] |0x000206f0|0x00000000|FUNC |GLOB |0 |UNDEF |_exit [67] |0x00020714|0x00000000|FUNC |GLOB |0 |UNDEF |printf

  16. Loading a Program Loader (runtime linker) (/usr/lib/ Process inmemory Executable File Shared libraries (.so, .dll)

  17. Loading a Program • The loader is part of the OS to prepare a process for an executable program. • Reads the executable file header to determine size of text and data segments • Creates address space large enough for text and data • Copies instructions and data from executable file into memory

  18. Loading a Program • Copies parameters to the main program (argc, argv) onto the stack • Initialize registers and set stack pointer • Jumps to a startup routine _start that in turn calls the main() routine. When the main routine returns, the startup routine terminates the program by making system call (executed by the OS) exit(). (Will see how this is done in Xinu)

  19. Static and Shared Libraries • Shared libraries are shared across different processes. • There is only one instance of each shared library for the entire system. • Static libraries are not shared. • There is an instance of an static library for each process.