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

Slides:



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

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.
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.
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Object Code.
1 Machine-Independent Features Automatic Library Search automatically incorporate routines from a subprogram library Loading Options.
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”
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.
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
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.
Kernel Structure and Infrastructure David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
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.
Linux Boot Process on the Raspberry Pi 2 1 David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis,
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.
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)
Exploiting the Linux Dynamic Loader with LD_PRELOAD David Kaplan DC9723 – June 2011.
Lecture 3 Translation.
Assemblers, linkers, loaders
Computer Architecture & Operations I
Slides adapted from Bryant and O’Hallaron
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.
Program Execution in Linux
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
Machine Independent Features
Kernel Structure and Infrastructure
Memory Management Overview
Linking & Loading CS-502 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.
CSE 542: Operating Systems
Presentation transcript:

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

Creating an 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 522S – Advanced Operating Systems2 //Source code #include int foo = 20; int main( int argc, char* argv[]){ printf(“Hello, world!\n”); return 0; } Compiler Relocatable Object file: D foo T main U puts Linker Executable file

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! CSE 522S – Advanced Operating Systems3 int foo = 10; int bar = 20; int main( int argc, char* argv[] ){ printf(“Hello, world!\n”); return 0; }

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 CSE 522S – Advanced Operating Systems4 Library Code Program Data Program code Static: Library Code Program Data Program code my_program.o Dynamic: libc.so my_program.o

Parts of a Program 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. CSE 522S – Advanced Operating Systems5 Stack Memory Map Segment Heap.bss.data.text Virtual Address Space 0xc000_0000 0x0000_0000

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

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 522S – Advanced Operating Systems7

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 522S – Advanced Operating Systems8

Running a Statically Linked Program 1.User forks() an existing process to get a new process space 2.execve() reads program into memory 3.Starts executing at _start() in the C runtime, which sets up environment 4.C runtime eventually calls main() 5.After main returns, C runtime does some cleanup CSE 522S – Advanced Operating Systems9

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 CSE 522S – Advanced Operating Systems10 Stack Memory Map Segment Heap.bss.data.text

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 522S – Advanced Operating Systems11

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

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

Static vs. Dynamic Linking Static: 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 522S – Advanced Operating Systems14

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 522S – Advanced Operating Systems15

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 522S – Advanced Operating Systems16