A Simplistic Program Translation Scheme

Similar documents
Computer Organization: A Programmer's Perspective

Linking Oct. 15, 2002

Systems I. Linking II

LINKING. Jo, Heeseung

Linking Oct. 26, 2009"

(Extract from the slides by Terrance E. Boult

CS429: Computer Organization and Architecture

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

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

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

Linking February 24, 2005

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

Systems Programming and Computer Architecture ( ) Timothy Roscoe

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

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

Lecture 16: Linking Computer Architecture and Systems Programming ( )

Computer Systems. Linking. Han, Hwansoo

Exercise Session 7 Computer Architecture and Systems Programming

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

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

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

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

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

Revealing Internals of Linkers. Zhiqiang Lin

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

u Linking u Case study: Library interpositioning

Linking. Today. Next time. Static linking Object files Static & dynamically linked libraries. Exceptional control flows

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

Sungkyunkwan University

CS241 Computer Organization Spring Buffer Overflow

Computer Systems Organization

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

CS 550 Operating Systems Spring Process I

Executables and Linking. CS449 Spring 2016

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

Today. Linking. Example C Program. Sta7c Linking. Linking Case study: Library interposi7oning

Assembly Language Programming Linkers

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

A SimplisHc Program TranslaHon Scheme. TranslaHng the Example Program. Example C Program. Why Linkers? - Modularity. Linking

M2 Instruction Set Architecture

Link 7.A Static Linking

Linking : Introduc on to Computer Systems 13 th Lecture, October 11th, Instructor: Randy Bryant. Carnegie Mellon

Executables and Linking. CS449 Fall 2017

Linking. CS 485 Systems Programming Fall Instructor: James Griffioen

Compiler Drivers = GCC

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

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

CS2141 Software Development using C/C++ Libraries

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

Compiler, Assembler, and Linker

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

Function Calls and Stack Allocation

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

Process Environment. Pradipta De

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.

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

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

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

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

Function Calls and Stack Allocation

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

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

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

Today s Big Adventure

Today s Big Adventure

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

Outline. Unresolved references

ECE 598 Advanced Operating Systems Lecture 10

CSL373: Operating Systems Linking

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

COS 318: Operating Systems

Midterm. Median: 56, Mean: "midterm.data" using 1:2 1 / 37

Link 7. Static Linking

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

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

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

ECE 471 Embedded Systems Lecture 4

Memory and C/C++ modules

CSCI341. Lecture 22, MIPS Programming: Directives, Linkers, Loaders, Memory

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

CS Programming In C

From Source to Execution:

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

CS140 - Summer Handout #8

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

Incremental Linking with Gold

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

Topics. 2 Introduction to Common Object File Format 2-3

Making Address Spaces Smaller

CALL (Compiler/Assembler/Linker/ Loader)

Fixing/Making Holes in Binaries

ECE 471 Embedded Systems Lecture 5

CS 110 Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

[07] SEGMENTATION 1. 1

Compilation Pipeline. Assembler and Linker. Compilation Pipeline. Assembler. Compiler (gcc):.c.s translates high-level language to assembly language

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

12: Memory Management

CS3214 Spring 2017 Exercise 2

Transcription:

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) 1

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) 2

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> 3

What Does a Linker Do? Merges object files Merges multiple relocatable (.o) object files into a single executable object file that can be 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 */ 4

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. 5

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 program files (a.out) Shared object library files (.so) Core dump files Generic name: ELF binaries 6

ELF Object File Format Elf header 7 Magic number, type (.o, exec,.so,core), machine, byte ordering, etc. Program header table Page size, virtual addresses memory segments (sections), segment sizes..text section Code.data section Initialized (static) data.bss section Uninitialized (static) data Block Started by Symbol Better Save Space Has section header but occupies no space ELF header Program header table (required for executables).text section.data section.bss section.symtab.rel.txt.rel.data.debug Section header table (required for relocatables) 0

ELF Object File Format (cont).symtab section Symbol table Procedure and static variable names Section names and locations.rel.text section Relocation info for.text section Addresses of instructions that will need to be modified in the executable Instructions for modifying..rel.data section Relocation info for.data section Addresses of pointer data that will need to be modified in the merged executable.debug section Info for symbolic debugging (gcc -g) ELF header Program header table (required for executables).text section.data section.bss section.symtab.rel.text.rel.data.debug Section header table (required for relocatables) 0 8

Example C Program m.c int e=7; int main() { int r = a(); exit(0); } a.c extern int e; int *ep=&e; int x=15; int y; int a() { return *ep+x+y; } 9

Merging Relocatable Object Files into an Executable Object File Relocatable Object Files Executable Object File system code.text 0 headers system data.data system code main().text m.o a.o main() int e = 7 a() int *ep = &e int x = 15 int y.text.data.text.data.bss a() more system code system data int e = 7 int *ep = &e int x = 15 uninitialized data.symtab.debug.data.bss 10

Relocating Symbols and Resolving External References Symbols are lexical entities that name functions and variables. Each symbol has a value (typically a memory address). Code consists of symbol definitions and references. References can be either local or external. Def of local symbol e Ref to external symbol exit (defined in libc.so) 11 m.c int e=7; int main() { int r = a(); exit(0); } Ref to external symbol a Def of local symbol ep a.c extern int e; int *ep=&e; int x=15; int y; int a() { return *ep+x+y; } Def of local symbol a Refs of local symbols ep,x,y Ref to external symbol e Defs of local symbols x and y

Strong and Weak Symbols Program symbols are either strong or weak strong: procedures and initialized globals weak: uninitialized globals p1.c p2.c strong int foo=5; int foo; weak strong p1() { } p2() { } strong 12

Linker s Symbol Rules Rule 1. A strong symbol can only appear once. Rule 2. A weak symbol can be overridden by a strong symbol of the same name. references to the weak symbol resolve to the strong symbol. Rule 3. If there are multiple weak symbols, the linker can pick an arbitrary one. 13

Linker Puzzles int x; p1() {} p1() {} Link time error: two strong symbols (p1) int x; p1() {} int x; p2() {} References to x will refer to the same uninitialized int. Is this what you really want? int x; int y; p1() {} double x; p2() {} Writes to x in p2 might overwrite y! Evil! int x=7; int y=5; p1() {} double x; p2() {} Writes to x in p2 will overwrite y! Nasty! int x=7; p1() {} int x; p2() {} References to x will refer to the same initialized variable. 14 Nightmare scenario: two identical weak structs, compiled by different compilers with different alignment rules.

Packaging Commonly Used Functions How to package functions commonly used by programmers? Math, I/O, memory management, string manipulation, etc. Awkward, given the linker framework so far: 15 Option 1: Put all functions in a single source file Programmers link big object file into their programs Space and time inefficient Option 2: Put each function in a separate source file Programmers explicitly link appropriate binaries into their programs More efficient, but burdensome on the programmer Solution: static libraries (.a archive files) Concatenate related relocatable object files into a single file with an index (called an archive). Enhance linker so that it tries to resolve unresolved external references by looking for the symbols in one or more archives. If an archive member file resolves reference, link into executable.

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)] 16 Linker selectively includes only the.o files in the archive that are actually needed by the program.

Creating Static Libraries atoi.c printf.c random.c Translator Translator... Translator atoi.o printf.o random.o 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. 17

Commonly Used Libraries libc.a (the C standard library) 18 2.4 MB archive of 1393 object files on mercury.cs.uml.edu. I/O, memory allocation, signal handling, string handling, data and time, random numbers, integer math $ ar t /usr/lib/i686-redhat-linux5e/lib/libc.a wc l results: 1393 libm.a (the C math library) 450 KB archive of 401 object files. floating point math (sin, cos, tan, log, exp, sqrt, ) % ar -t /usr/lib/<libc.a> sort fork.o fprintf.o fpu_control.o fputc.o freopen.o fscanf.o fseek.o fstab.o % ar -t /usr/lib/<libm.a> sort e_acos.o e_acosf.o e_acosh.o e_acoshf.o e_acoshl.o e_acosl.o e_asin.o e_asinf.o e_asinl.o

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. 19 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'

Loading Executable Binaries Executable object file for example program p ELF header Program header table (required for executables).text section 0 Process image init and shared lib segments Virtual addr 0x080483e0.data section.bss section.symtab.text segment (r/o) 0x08048494.rel.text.rel.data.data segment (initialized r/w) 0x0804a010.debug Section header table (required for relocatables).bss segment (uninitialized r/w) 0x0804a3b0 20

Variably sized byte regions overlaid on a fixed page size object A 4 KB page Text Object 8 KB, 2 pages initialized globals uninitialized globals 4 KB 4 KB Data Object 24 KB, 6 pages heap space 16 KB Stack Object 8 KB, 2 pages 21

Shared Libraries Static libraries have the following disadvantages: 22 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.

Dynamically Linked Shared Libraries m.c a.c Translators (cc1, as) Translators (cc1,as) m.o a.o Linker (ld) Partially linked executable p (on disk) p libc.so Shared library of dynamically relocatable object files 23 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 24