(Extract from the slides by Terrance E. Boult

Similar documents
A Simplistic Program Translation Scheme

LINKING. Jo, Heeseung

Linking Oct. 15, 2002

Computer Organization: A Programmer's Perspective

Systems I. Linking II

CS429: Computer Organization and Architecture

Relocating Symbols and Resolving External References. CS429: Computer Organization and Architecture. m.o Relocation Info

Linking Oct. 26, 2009"

CS 201 Linking Gerson Robboy Portland State University

Linker Puzzles The course that gives CMU its Zip! Linking Mar 4, A Simplistic Program Translation Scheme. A Better Scheme Using a Linker

Exercise Session 7 Computer Architecture and Systems Programming

CSE 2421: Systems I Low-Level Programming and Computer Organization. Linking. Presentation N. Introduction to Linkers

Example C Program The course that gives CMU its Zip! Linking March 2, Static Linking. Why Linkers? Page # Topics

Systemprogrammering och operativsystem Laborationer. Linker Puzzles. Systemprogrammering 2007 Föreläsning 1 Compilation and Linking

Linking February 24, 2005

Example C program. 11: Linking. Why linkers? Modularity! Static linking. Why linkers? Efficiency! What do linkers do? 10/28/2013

Linking. Computer Systems Organization (Spring 2017) CSCI-UA 201, Section 3. Instructor: Joanna Klukowska

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Computer Systems. Linking. Han, Hwansoo

Lecture 16: Linking Computer Architecture and Systems Programming ( )

Example C Program. Linking CS Instructor: Sanjeev Se(a. int buf[2] = {1, 2}; extern int buf[]; int main() { swap(); return 0; }

Compiler Drivers = GCC

CIT 595 Spring System Software: Programming Tools. Assembly Process Example: First Pass. Assembly Process Example: Second Pass.

COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES. Credit: Mostly Bryant & O Hallaron

E = 2 e lines per set. S = 2 s sets tag. valid bit B = 2 b bytes per cache block (the data) CSE351 Inaugural EdiNon Spring

Computer Systems Organization

Executables and Linking. CS449 Spring 2016

Linking and Loading. CS61, Lecture 16. Prof. Stephen Chong October 25, 2011

Executables and Linking. CS449 Fall 2017

M2 Instruction Set Architecture

CS2141 Software Development using C/C++ Libraries

Outline. 1 Background. 2 ELF Linking. 3 Static Linking. 4 Dynamic Linking. 5 Summary. Linker. Various Stages. 1 Linking can be done at compile.

Lecture 12-13: Linking

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

Generating Programs and Linking. Professor Rick Han Department of Computer Science University of Colorado at Boulder

Linking and Loading. ICS312 - Spring 2010 Machine-Level and Systems Programming. Henri Casanova

CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading)

Revealing Internals of Linkers. Zhiqiang Lin

Function Calls and Stack Allocation

CS5460/6460: Operating Systems. Lecture 21: Shared libraries. Anton Burtsev March, 2014

u Linking u Case study: Library interpositioning

From Code to Program: CALL Con'nued (Linking, and Loading)

Compiler, Assembler, and Linker

Link 7.A Static Linking

Function Calls and Stack Allocation

Stack. Stack. Function Calls and Stack Allocation. Stack Popping Popping. Stack Pushing Pushing. Page 1

Linkers and Loaders. CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved.

CS 550 Operating Systems Spring Process I

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

Systemprogrammering och operativsystem Linker Puzzles. Systemprogrammering 2009 Före lä s n in g 1 Compilation and Linking

Sungkyunkwan University

Deadlock Detection. Several Instances of a Resource Type. Single Instance of Each Resource Type

Operating Systems CMPSC 473. Process Management January 29, Lecture 4 Instructor: Trent Jaeger

Your first C and C++ programs

CS241 Computer Organization Spring Data Alignment

CS241 Computer Organization Spring Buffer Overflow

From Source to Execution:

Outline. Unresolved references

COS 318: Operating Systems. Overview. Andy Bavier Computer Science Department Princeton University

Assembly Language Programming Linkers

Lecture 8: linking CS 140. Dawson Engler Stanford CS department

9/19/18. COS 318: Operating Systems. Overview. Important Times. Hardware of A Typical Computer. Today CPU. I/O bus. Network

COS 318: Operating Systems

Incremental Linking with Gold

Lec 13: Linking and Memory. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

C03c: Linkers and Loaders

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Making Address Spaces Smaller

CSE 303: Concepts and Tools for Software Development

CS 33. Libraries. CS33 Intro to Computer Systems XXIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Process Environment. Pradipta De

CSL373: Operating Systems Linking

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

Linking. Explain what ELF format is. Explain what an executable is and how it got that way. With huge thanks to Steve Chong for his notes from CS61.

COS 318: Operating Systems. Overview. Prof. Margaret Martonosi Computer Science Department Princeton University

CS 33. Libraries. CS33 Intro to Computer Systems XXVIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

COSC345 Software Engineering. Linkers Loaders Libraries

12: Memory Management

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

Introduction to Computer Systems

Introduction Presentation A

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

2 Compiling a C program

Memory and C/C++ modules

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8

Fixing/Making Holes in Binaries

LC-3 Assembly Language

ECE260: Fundamentals of Computer Engineering

ECE 598 Advanced Operating Systems Lecture 10

Loaders. Systems Programming. Outline. Basic Loader Functions

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CALL (Compiler/Assembler/Linker/ Loader)

Where Are We Now? Linker (1/3) Linker (2/3) Four Types of Addresses we ll discuss. Linker (3/3)

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

Chris Riesbeck, Fall Introduction to Computer Systems

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

gpio timer uart printf malloc keyboard fb gl console shell

Generic Programming in C

Transcription:

What software engineers need to know about linking and a few things about execution (Extract from the slides by Terrance E. Boult http://vast.uccs.edu/~tboult/)

A Simplistic Program Translation Scheme m.c ASCII source file Translator p Binary executable object file (memory image on disk) Problems: Efficiency: small change requires complete recompilation Modularity: hard to share common functions (e.g. printf) Solution: Static linker (or linker)

A Better Scheme Using a Linker m.c a.c Translators Translators m.o a.o Separately compiled relocatable object files Linker (ld) p Executable object file (contains code and data for all functions defined in m.c and a.c)

Translating the Example Program Compiler driver coordinates all steps in the translation and linking process. Typically included with each compilation system (e.g., gcc) Invokes preprocessor (cpp), compiler (cc1), assembler (as), and linker (ld). Passes command line arguments to appropriate phases Example: create executable p from m.c and a.c: bass> gcc -O2 -v -o p m.c a.c cpp [args] m.c /tmp/cca07630.i cc1 /tmp/cca07630.i m.c -O2 [args] -o /tmp/cca07630.s as [args] -o /tmp/cca076301.o /tmp/cca07630.s <similar process for a.c> ld -o p [system obj files] /tmp/cca076301.o /tmp/cca076302.o bass>

What Does a Linker Do? Merges object files Merges multiple relocatable (.o) object files into a single executable object file that can loaded and executed by the loader. Resolves external references As part of the merging process, resolves external references. External reference: reference to a symbol defined in another object file. Relocates symbols Relocates symbols from their relative locations in the.o files to new absolute positions in the executable. Updates all references to these symbols to reflect their new positions. References can be in either code or data code: a(); /* reference to symbol a */ data: int *xp=&x; /* reference to symbol x */

Why Linkers? Modularity Program can be written as a collection of smaller source files, rather than one monolithic mass. Can build libraries of common functions (more on this later) e.g., Math library, standard C library Efficiency Time: Change one source file, compile, and then relink. No need to recompile other source files. Space: Libraries of common functions can be aggregated into a single file... Yet executable files and running memory images contain only code for the functions they actually use.

Executable and Linkable Format (ELF) Standard binary format for object files Derives from AT&T System V Unix Later adopted by BSD Unix variants and Linux One unified format for Relocatable object files (.o), Executable object files Shared object files (.so) Generic name: ELF binaries Better support for shared libraries than old a.out formats.

FF Linux Memory Layout Upper 2 hex digits of address C0 BF 80 7F 40 3F 08 00 Stack Heap DLLs Heap Data Text Stack Runtime stack Heap Dynamically allocated storage When call malloc, calloc, new DLLs Dynamically Linked Libraries Library routines (e.g., printf, malloc) Linked into object code when first executed Data Statically allocated data E.g., arrays & strings declared in code Text Executable machine instructions Read-only

Static Libraries (archives) p1.c p2.c Translator Translator p1.o p2.o libc.a static library (archive) of relocatable object files concatenated into one file. Linker (ld) p executable object file (only contains code and data for libc functions that are called from p1.c and p2.c) Further improves modularity and efficiency by packaging commonly used functions [e.g., C standard library (libc), math library (libm)] Linker selectively links only the.o files in the archive that are actually needed by the program.

Creating Static Libraries atoi.c Translator atoi.o printf.c Translator printf.o random.c... Translator random.o r: replace existing or insert new file(s) into the archive s: create an archive index Archiver (ar) ar rs libc.a \ atoi.o printf.o random.o libc.a C standard library Archiver allows incremental updates: Recompile function that changes and replace.o file in archive.

Using Static Libraries Linker s algorithm for resolving external references: Scan.o files and.a files in the command line order. During the scan, keep a list of the current unresolved references. As each new.o or.a file obj is encountered, try to resolve each unresolved reference in the list against the symbols in obj. If any entries in the unresolved list at end of scan, then error. Problem: Command line order matters! Moral: put libraries at the end of the command line. bass> gcc -L. libtest.o -lmine bass> gcc -L. -lmine libtest.o libtest.o: In function `main': libtest.o(.text+0x4): undefined reference to `libfun'

Shared Libraries Static libraries have the following disadvantages: Potential for duplicating lots of common code in the executable files on a filesystem. e.g., every C program needs the standard C library Potential for duplicating lots of code in the virtual memory space of many processes. Minor bug fixes of system libraries require each application to explicitly relink Solution: Shared libraries (dynamic link libraries, DLLs) whose members are dynamically loaded into memory and linked into an application at run-time. Dynamic linking can occur when executable is first loaded and run. Common case for Linux, handled automatically by ld-linux.so. Dynamic linking can also occur after program has begun. In Linux, this is done explicitly by user with dlopen(). Basis for High-Performance Web Servers. Shared library routines can be shared by multiple processes.

m.c Translators (cc1, as) a.c Translators (cc1,as) Dynamically Linked Shared Libraries m.o a.o Linker (ld) Partially linked executable p (on disk) p libc.so Shared library of dynamically relocatable object files Fully linked executable p (in memory) Loader/Dynamic Linker (ld-linux.so) P libc.so functions called by m.c and a.c are loaded, linked, and (potentially) shared among processes.

The Complete Picture m.c a.c Translator Translator m.o a.o libwhatever.a Static Linker (ld) p libc.so libm.so Loader/Dynamic Linker (ld-linux.so) p