1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.

Slides:



Advertisements
Similar presentations
Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
Advertisements

Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Assembly Code Example Selection Sort.
SPARC Architecture & Assembly Language
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
The University of Adelaide, School of Computer Science
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Linking & Loading CS-502 Operating Systems
Chapter 3 Loaders and Linkers
Loaders and Linkers CS 230 이준원. 2 Overview assembler –generates an object code in a predefined format »COFF (common object file format) »ELF (executable.
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder.
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Roy Wang inst.eecs.berkeley.edu/~cs61c-tf inst.eecs.berkeley.edu/~cs61c.
Software Development and Software Loading in Embedded Systems.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
CSC 3210 Computer Organization and Programming Chapter 9 EXTERNAL DATA AND TEXT D.M. Rasanjalee Himali.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
MIPS coding. SPIM Some links can be found such as:
chap13 Chapter 13 Programming in the Large.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez- Rivera Topic 2: Program Structure and Using GDB.
Topic 2d High-Level languages and Systems Software
C OMPUTER A RCHITECTURE & O RGANIZATION Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Blackfin Array Handling Part 1 Making an array of Zeros void MakeZeroASM(int foo[ ], int N);
Linking Ⅱ.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
Chapter 13 : Symbol Management in Linking
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
CSc 453 Linking and Loading
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Program Translation and Execution I: Linking Sept. 29, 1998 Topics object files linkers class11.ppt Introduction to Computer Systems.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 10 – Loaders.
Operating Systems A Biswas, Dept. of Information Technology.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Program Execution in Linux David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Programs – Preprocessing, Compilation and Linking
Lecture 3 Translation.
Computer Architecture & Operations I
Computer Systems MTSU CSCI 3240 Spring 2016 Dr. Hyrum D. Carroll
A bit of C programming Lecture 3 Uli Raich.
The University of Adelaide, School of Computer Science
Linking & Loading.
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
Program Execution in Linux
CS-3013 Operating Systems C-term 2008
Topic 2e High-Level languages and Systems Software
Computer Organization & Compilation Process
Memory Allocation CS 217.
Understanding Program Address Space
Linking & Loading CS-502 Operating Systems
Program Execution in Linux
Linking & Loading CS-502 Operating Systems
Computer Organization & Compilation Process
Program Assembly.
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
Presentation transcript:

1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University

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

Memory Sections The virtual address space is organized into sections: Stack Text Data Bss Heap Shared Libs

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).

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.

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 }

Building a Program The programmer writes a program hello.c The preprocessor expands #define, #include, #ifdef etc 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.

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.

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

Original file hello.c #include main() { printf("Hello\n"); }

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; typedef struct __FILE __FILE; typedef int ssize_t; struct FILE {…}; extern int fprintf(FILE *, const char *,...); extern int fscanf(FILE *, const char *,...); extern int printf(const char *,...); /* and more */ main() { printf("Hello\n"); }

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.

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] |0x |0x |FILE |LOCL |0 |ABS |hello.c [2] |0x |0x |NOTY |LOCL |0 |2 |gcc2_compiled [3] |0x |0x |SECT |LOCL |0 |2 | [4] |0x |0x |SECT |LOCL |0 |3 | [5] |0x |0x |NOTY |GLOB |0 |UNDEF |printf [6] |0x |0x c|FUNC |GLOB |0 |2 |main

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

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] |0x |0x |OBJT |LOCL |0 |1 |_START_ [65] |0x c|0x |FUNC |GLOB |0 |9 |_start [43] |0x |0x |FUNC |LOCL |0 |9 |fini_dummy [60] |0x000105c4|0x c|FUNC |GLOB |0 |9 |main [71] |0x000206d8|0x |FUNC |GLOB |0 |UNDEF |atexit [72] |0x000206f0|0x |FUNC |GLOB |0 |UNDEF |_exit [67] |0x |0x |FUNC |GLOB |0 |UNDEF |printf

Loading a Program Loader (runtime linker) (/usr/lib/ld.so.1) Executable File Process in memory Shared libraries (.so,.dll)

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

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)

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.