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.
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Operating Systems1 9. Linking and Sharing 9.1 Single-Copy Sharing –Why Share –Requirements for Sharing –Linking and Sharing 9.2 Sharing in Systems without.
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.
Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder.
C and Data Structures Baojian Hua
Memory Layout C and Data Structures Baojian Hua
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
F13 Forensic tool analysis Dr. John P. Abraham Professor UTPA.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
Chapter 10 - Memory Management –Memory management can have a large influence on the performance of a program. –Operating system handles allocation of memory.
CSU System Programming, NTNU CSIE1 / 99 Linkers and Libraries Advisor: Dr. Gwan-Hwan Hwang Lecturer: Che-Sheng Lin.
Programming With C.
Topic 2d High-Level languages and Systems Software
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
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
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.
The World Leader in High Performance Signal Processing Solutions Toolchain Basics.
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
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
Software Development with uMPS
CS-3013 Operating Systems C-term 2008
CSE 351 Section 1: HW 0 + Intro to C
Topic 2e High-Level languages and Systems Software
Memory Management Overview
Linking & Loading CS-502 Operating Systems
System Calls David Ferry CSCI 3500 – Operating Systems
Semester Review Brian Kocoloski
Appendix F C Programming Environment on UNIX Systems
Linking & Loading CS-502 Operating Systems
CSE 542: Operating Systems
Program Assembly.
Processes David Ferry, Chris Gill, Brian Kocoloski
OPERATING SYSTEMS MEMORY MANAGEMENT BY DR.V.R.ELANGOVAN.
CSE 542: Operating Systems
Presentation transcript:

Program Execution in Linux David Ferry, Chris Gill, Brian Kocoloski 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

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/Data Program Data Library Code/Data CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Static Linking With multiple programs, what happens to the size of the executables? Program code Program Data Library Code CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Static Linking With multiple programs, what happens to the size of the executables? Program code Program code Program code Program code Program Data Program Data Program Data Program Data Library Code Library Code Library Code Library Code Program code Program code Program code Program Data Program Data Program Data Library Code Library Code Library Code CSE 422S – Operating Systems Organization

CSE 422S – Operating Systems Organization Dynamic Linking With multiple programs, what happens to the size of the executables? Program code Program code Program code Program code Program Data Program Data Program Data Program Data Library Code Program code Program code Program code Program Data Program Data Program Data CSE 422S – Operating Systems Organization

Running a Statically Linked Program All functions and data needed by the process space are linked as the last step of the compiler Only that code/data needed by the program are loaded into virtual memory Stack Static library A code/data Static library B code/data Heap .bss .data .text CSE 422S – Operating Systems Organization

Running a Statically Linked Program A statically linked program is entirely self-contained 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 (s) (added at runtime by linker) 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

CSE 422S – Operating Systems Organization Linking at Runtime Procedure Linkage Table (PLT) Used by dynamic linker to resolve locations of library functions All function calls to shared libraries are replaced by stub functions that query the PLT at runtime for the address of the function Global Offset Table (GOT) Used by dynamic linker to resolve locations of library data All references to variables in shared libraries are replaced with references to the GOT 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

CSE 422S – Operating Systems Organization Example Uses the following tools: objdump nm strace gdb You aren’t responsible for following all of the details of this example – it’s just to reinforce the concepts behind the PLT and GOT CSE 422S – Operating Systems Organization

Static vs. Dynamic Linking Tradeoffs Does not need to look up libraries at runtime Does not need extra PLT indirection Consumes more memory with copies of each library in every program Dynamic: Less disk space/memory (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

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

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