Program Execution in Linux

Slides:



Advertisements
Similar presentations
Hand-Held Devices and Embedded Systems Course Student: Tomás Sánchez López Student ID:
Advertisements

Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
Fabián E. Bustamante, Spring 2007 Linking Today Static linking Object files Static & dynamically linked libraries Next time Exceptional control flows.
Program Development Tools The GNU (GNU’s Not Unix) Toolchain The GNU toolchain has played a vital role in the development of the Linux kernel, BSD, and.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
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.
Lecture 10: Linking and loading. Lecture 10 / Page 2AE4B33OSS 2011 Contents Linker vs. loader Linking the executable Libraries Loading executable ELF.
Mr. D. J. Patel, AITS, Rajkot 1 Operating Systems, by Dhananjay Dhamdhere1 Static and Dynamic Memory Allocation Memory allocation is an aspect of a more.
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
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++
Compilation (Semester A, 2013/14) Lecture 13: Assembler, Linker & Loader Noam Rinetzky Slides credit: Eli Bendersky, Mooly Sagiv & Sanjeev Setia.
Linking and Loading Fred Prussack CS 518. L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic.
Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
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.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
Enabling the ARM Learning in INDIA ARM DEVELOPMENT TOOL SETUP.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
CSU System Programming, NTNU CSIE1 / 99 Linkers and Libraries Advisor: Dr. Gwan-Hwan Hwang Lecturer: Che-Sheng Lin.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Topic 2d High-Level languages and Systems Software
CSE451 Linking and Loading Autumn 2002 Gary Kimura Lecture #21 December 9, 2002.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Linking Ⅱ.
Static Shared Library. Non-shared v.s. Shared Library A library is a collection of pre-written function calls. Using existing libraries can save a programmer.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Chapter 13 : Symbol Management in Linking
Different Types of Libraries
WEEK 5 LINKING AND LOADING MEMORY MANAGEMENT PAGING AND SEGMENTATION Operating Systems CS3013 / CS502.
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.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
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.
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.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Main Memory CSSE 332 Operating Systems Rose-Hulman Institute of Technology.
Program Execution in Linux David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Lecture 3 Translation.
Assemblers, linkers, loaders
Slides adapted from Bryant and O’Hallaron
Computer Systems MTSU CSCI 3240 Spring 2016 Dr. Hyrum D. Carroll
The University of Adelaide, School of Computer Science
Linking & Loading.
Linux Userspace Process Memory Layout
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
ICS143A: Principles of Operating Systems Lecture 21: Program linking and loading Anton Burtsev March, 2017.
CS-3013 Operating Systems C-term 2008
CSE 351 Section 1: HW 0 + Intro to C
Topic 2e High-Level languages and Systems Software
Loaders and Linkers: Features
Kernel Structure and Infrastructure
Memory Management Overview
Linking & Loading CS-502 Operating Systems
System Calls David Ferry CSCI 3500 – Operating Systems
Program Execution in Linux
10/6: Lecture Topics C Brainteaser More on Procedure Call
Linking & Loading CS-502 Operating Systems
CSE 542: Operating Systems
Program Assembly.
Processes David Ferry, Chris Gill, Brian Kocoloski
CSE 542: Operating Systems
Presentation transcript:

Program Execution in Linux David Ferry, Chris Gill CSE 422S - Operating Systems Organization Washington University in St. Louis St. Louis, MO 63143

Creating an Executable File //Source code #include <stdio.h> int foo = 20; int main( int argc, char* argv[]){ printf(“Hello, world!\n”); return 0; } Compiler Relocatable Object file: 00000000 D foo 00000000 T main U puts Linker Executable file Two stages: Compilation Linking The compiler translates source code to machine code. The linker connects binary files to libraries to create an executable. CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization The Symbol Table Program binaries have a symbol table that keep track of data and code: Example: The linker must resolve undefined symbols before the program can be run! int foo = 10; int bar = 20; int main( int argc, char* argv[] ){ printf(“Hello, world!\n”); return 0; } CSE 422S – Operating Systems Organization

Static vs. Dynamic Linking Static linking – required code and data is copied into executable at compile time Dynamic linking – required code and data is linked to executable at runtime my_program.o Static: Dynamic: Program code my_program.o libc.so Program code Program Data Library Code Program Data Library Code CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Parts of a Program Virtual Address Space A program has two components: Data Code Either component may be: static (fixed at compile time) dynamic (linked at run time) The compiler creates static sections as part of a binary. The linker links dynamic sections from other binaries. 0xc000_0000 Stack Memory Map Segment Heap .bss .data .text 0x0000_0000 CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Program Segmentation Virtual Address Space Static code: .text segment Dynamic code: Memory map segment Static data: .data segment (initialized) Dynamic data: Initialized at runtime: Stack Heap .bss 0xc000_0000 Stack Memory Map Segment Heap .bss .data .text 0x0000_0000 CSE 422S – Operating Systems Organization

Running a Statically Linked Program A statically linked program is entirely self-contained: The loader creates a valid process by loading a binary image into memory On Linux, execve() system call The C runtime initializes the process to execute normal C code Usually called crt0.o CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization The C Runtime Initializes the C stack and heap Sets up argc and argv Calls user-specified program constructors and destructors Does C library intialization CSE 422S – Operating Systems Organization

Running a Statically Linked Program User forks() an existing process to get a new process space execve() reads program into memory Starts executing at _start() in the C runtime, which sets up environment C runtime eventually calls main() After main returns, C runtime does some cleanup CSE 422S – Operating Systems Organization

Running a Dynamically Linked Program Some functions and data do not exist in process space at runtime The dynamic linker (called ld) maps these into the memory map segment on-demand Stack Memory Map Segment Heap .bss .data .text CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Linking at Runtime At compile time: The linker (ld) is embedded in program Addresses of dynamic functions are replaced with calls to the linker At runtime the linker does lazy-binding: Program runs as normal until it encounters an unresolved function Program jumps to linker Linker maps shared library into address space and replaces the unresolved address with the resolved address CSE 422S – Operating Systems Organization

Runtime Linker Implementation Uses a procedure link table (PLT) to do lazy binding Stack //Source code #include <stdio.h> int foo = 20; int main( int argc, char* argv[]){ printf(“Hello, world!\n”); return 0; } Heap .bss Procedure Link Table (PLT) .data linker_stub() .text CSE 422S – Operating Systems Organization

Runtime Linker Implementation Uses a procedure link table (PLT) to do lazy binding Stack //Source code #include <stdio.h> int foo = 20; int main( int argc, char* argv[]){ printf(“Hello, world!\n”); return 0; } Library with printf() function Heap .bss Procedure Link Table (PLT) .data library printf() .text CSE 422S – Operating Systems Organization

Static vs. Dynamic Linking Does not need to look up libraries at runtime Does not need extra PLT indirection Replicates disk space Dynamic: Less disk space (7K vs 571K for hello world) Shared libraries already in memory and in hot cache Incurs lookup and indirection overheads CSE 422S – Operating Systems Organization

Executable File Format The current binary file format is called ELF - Executable and Linking Format First part of file is the ELF Header, which defines contents of the rest of the file Segments contain data & code needed at runtime Sections contain linking & relocation data Adds additional segments past .text, .data, etc.: .rodata – read-only data .debug – debugging symbol table and more… GCC adds it’s own sections… CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Binary File Utilities nm – prints symbol table objdump – prints all binary data readelf – prints ELF data pmap – prints memory map of a running process ldd – prints dynamic library dependencies of a binary strip – strips symbol data from a binary CSE 422S – Operating Systems Organization