Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.

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.
Part IV: Memory Management
Chapter 10 Linking and Loading. Separate assembly creates “.mob” files.
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
Chapter 3 Loaders and Linkers. Purpose and Function Places object program in memory Linking – Combines 2 or more obj programs Relocation – Allows loading.
Linker and Loader. Program building into four stages (C Program) Preprocessing (Preprocessor) It processes include files, conditional compilation instructions.
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.
CS 31003: Compilers ANIRUDDHA GUPTA 11CS10004 G2 CLASS DATE : 24/07/2013.
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.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
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.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002.
Chapter 91 Memory Management Chapter 9   Review of process from source to executable (linking, loading, addressing)   General discussion of memory.
OBJECT MODULE FORMATS. The object module format we have employed as an educational device is called OMF (relocatable object format). It’s one of the earliest.
Enabling the ARM Learning in INDIA ARM DEVELOPMENT TOOL SETUP.
MIPS coding. SPIM Some links can be found such as:
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.
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
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.
Linking February 20, 2001 Topics static linking object files static libraries loading dynamic linking of shared libraries class16.ppt “The course.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Linking Ⅱ.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
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.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
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)
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
Overview of today’s lecture
Linking Topics Static linking Object files Static libraries Loading
The University of Adelaide, School of Computer Science
Linking & Loading.
Chapter 8 Main Memory.
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
“The course that gives CMU its Zip!”
“The course that gives CMU its Zip!”
Linking.
The Assembly Language Level
Linking & Loading CS-502 Operating Systems
Computer Architecture
Program Execution in Linux
Linking & Loading CS-502 Operating Systems
Program Assembly.
“The course that gives CMU its Zip!”
SPL – PS1 Introduction to C++.
Presentation transcript:

Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective

Linkers and Loaders 1 Agenda..  Basic concepts  Object Files  Program Loading  Linking with static libraries  Linking with dynamic libraries

Linkers and Loaders 1 The Basics..  Compiler in Action… gcc foo.c bar.c –o a.out run assembler (as) foo.c bar.c foo.sbar.s foo.obar.o a.out run preprocessor (cpp) & compiler proper (cc1) linker a.out = fully linked executable

Linkers and Loaders 1 What is Linker ?  Combines multiple relocatable object files  Produces fully linked executable – directly loadable in memory  How? Symbol resolution – associating one symbol definition with each symbol reference Relocation – relocating different sections of input relocatable files

Linkers and Loaders 1 Object files..  Types – Relocatable : Requires linking to create executable Executable : Loaded directly into memory for execution Shared Objects : Linked dynamically, at run time or load time  Formats – a.out, IBM360, OMF, COFF, PE, ELF, ELF-64 …

Linkers and Loaders 1 Object Files.. (Cntd)  ELF relocatable Object File.text – machine code.rodata – format strings in printf.data – initialized globals.bss – uninitialized globals.strtab.line.debug.rel.data.rel.text.symtab.bss.data.rodata.text ELF HEADER

Linkers and Loaders 1 Program Loading  Linux run-time memory image on execve

Linkers and Loaders 1 Symbol Resolution..  3 types of symbols resolved during linking Non-static global symbols defined by object file Extern global symbols referenced by object file Static symbols local to object file/function 1. Local Automatic variables : managed on stack & not of interest to linkers

Linkers and Loaders 1 Symbol Resolution..(Cntd)  Resolving Global Symbols – Strong Symbols : functions, initialized global variables Weak Symbols : uninitialized global variables  Rules of thumb – Multiple strong symbols – not allowed Given a strong and multiple weak symbols, choose the strong symbol Given multiple weak symbols, choose any weak symbol

Linkers and Loaders 1 Linking with Static Libraries  Collection of concatenated object files – stored on disk in a particular format – archive  An input to Linker Referenced object files copied to executable libm.a printf.o & fopen.o linker(ld) foo.o bar.o libc.a a.out fully linked executable object file

Linkers and Loaders 1 Resolving symbols using static libs.  Scans input relocatable files from left to right as on command line Maintains set E of object files req to form executable, set U of unresolved symbols, set D of symbols defined in prev files. Updates E, U and D while scanning input relocatable files U must be empty at the end – contents of E used to form executable  Problems ? Libraries must be placed at the end of command line. Cyclic dependency ?? Size of the executable ??? Change in library requires re-linking

Linkers and Loaders 1 Relocation – The heart of Linker  Relocating sections and symbol definitions Merges all sections of similar types Assigns unique run-time address to every instruction/var  Relocating symbol references within sections Modifies symbol references inside sections – make them point to correct run-time addresses Uses relocation entries for the above purpose – Created for every un-defined reference – Placed in.relo.text &.relo.data sections – Contains offset, symbol & type (algorithm) Iterates over relocation entries and relocates

Linkers and Loaders 1 Dynamic Linking – Shared Libraries  Addresses disadvantages of static libraries Ensures one copy of text & data in memory Change in shared library does not require executable to be built again Loaded at run-time by dynamic linker, at arbitrary memory address, linked with programs in memory On loading, dynamic linker relocates text & data of shared object; also relocates any references in executable to symbols defined in shared object E.g..so files in Linux/Sun;.sl in HPUX; DLLs in Microsoft Windows Can be loaded dynamically in the middle of execution – dlopen, dlsym, dlclose calls in Linux/Sun; shl_load, shl_findsym in HPUX, LoadLibrary, GetProcAddress in Windows

Linkers and Loaders 1 Shared Libraries..(Cntd)  Linker creates libfoo.so (PIC) from a.o b.o  a.out – partially executable – dependency on libfoo.so .interp section in a.out – invokes dynamic linker  Dynamic linker maps shared library into program’s address space linker a.o b.o libfoo.so (position independent shared object) bar.o loader (execve) dynamic linker (ld-linux.so) Partially linked executable – dependency on libfoo.so a.out fully linked executable in memory -fPIC

Linkers and Loaders 1 Position Independent Code (PIC)  Important property – required by shared libraries No absolute addresses – hence can be loaded and executed at any address Uses PC-relative/indirect addressing  Indirect addressing – required for externally defined functions and globals Uses Global Offset Table (GOT) to resolve unreferenced global variables Uses a Procedure Linkage Table (PLT) along with GOT to resolve unreferenced functions GOT resides at the start of data segment, GOT entries are fixed at run-time to point to correct run-time address Lazy binding of function calls

Linkers and Loaders 1 Thank You all !!