CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.

Slides:



Advertisements
Similar presentations
Hand-Held Devices and Embedded Systems Course Student: Tomás Sánchez López Student ID:
Advertisements

Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Target Code Generation
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
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
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.
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Object Code.
CS 311 – Lecture 21 Outline Memory management in UNIX
Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder.
Loader- Machine Independent Loader Features
1 Assemblers and Linkers CS Goals of This Lecture Compilation process  Compile, assemble, archive, link, execute Assembling  Representing instructions.
Software Development and Software Loading in Embedded Systems.
Chapter 91 Memory Management Chapter 9   Review of process from source to executable (linking, loading, addressing)   General discussion of memory.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
MIPS coding. SPIM Some links can be found such as:
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
Topic 2d High-Level languages and Systems Software
C OMPUTER A RCHITECTURE & O RGANIZATION Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
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.
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.
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Different Types of Libraries
Loader and Linker.
CSc 453 Linking and Loading
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.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
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.
Program Execution and ELF Files Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014 Abed Asi.
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)
Lecture 3 Translation.
Assemblers, linkers, loaders
Instruction Set Architectures
Computer Architecture & Operations I
System Programming and administration
The University of Adelaide, School of Computer Science
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
Introduction to Compilers Tim Teitelbaum
Topic 2e High-Level languages and Systems Software
Machine Independent Features
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
CS399 New Beginnings Jonathan Walpole.
Memory Allocation CS 217.
Memory Management Overview
Computer Organization and Design Assembly & Compilation
The Assembly Language Level
Linking & Loading CS-502 Operating Systems
Computer Architecture
Program Execution in Linux
10/6: Lecture Topics C Brainteaser More on Procedure Call
Linking & Loading CS-502 Operating Systems
Target Code Generation
Program Assembly.
Computer Architecture and System Programming Laboratory
Presentation transcript:

CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 2 Administration Prelim 2 on Friday PA 4 due April 28

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 3 Outline Static linking –Object files –Libraries –Shared libraries –Relocatable code Dynamic linking –explicit vs. implicit linking –dynamically linked libraries/dynamic shared objects

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 4 Object files Output of compiler is an object file –not executable –may refer to external symbols (variables, functions, etc.) whose definition is not known. Linker joins together object files, resolves external references source code object code executable image source code object code compiler linker

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 5 Unresolved references extern int abs( int x ); … y = y + abs(x); PUSH ecx CALL _abs ADD ebx, eax 9A D8 00 to be filled in by linker source code assembly code object code

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 6 Object file structure text section: unresolved machine code symbol table (maps identifiers to machine code locations) relocation info file header initialized data Object file contains various sections text section contains the compiled code with some patching needed For uninitialized data, only need to know total size of data segment Describes structure of text and data sections Points to places in text and data section that need fix-up

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 7 Action of Linker text1 sym1 rel1 init1 text2 sym2 rel3 init2 text3 sym3 rel3 init3 text3 text2 text1 init3 init2 init1 uninitialized data executable image memory layout object files code segment data segment +peephole optimizations

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 8 Executable file structure Same as object file, but code is ready to be executed as-is Pages of code and data brought in lazily from text and data section as needed: allows rapid program start-up Text section shared Symbols: debugging text section: execution- ready machine code optional: symbol table file header initialized data

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 9 Libraries A library is a collection of object files Linker adds all object files necessary to resolve undefined references in explicitly named files Object files, libraries searched in user- specified order for external references ld main.o foo.o /usr/lib/X11/libX11.a /usr/lib/libc.a Library provides index over all object files for rapid searching

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 10 Shared libraries Problem: libraries take up a lot of memory when linked into many running applications Solution: shared libraries (e.g. DLLs) ls cat emacs xterm libc X11 libc X11 libc X11

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 11 Jump tables Executable file refers to, does not contain library code; library code loaded dynamically Library code found in separate shared library file (similar to DLL); linking done against import library that does not contain code Library compiled at fixed address, starts with jump table to allow new versions; client code jumps to jump table: indirection. scanf: jmp real_scanf call printfprintf: jmp real_printf putchar: jmp real_putchar

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 12 Global tables Problem: shared libraries may depend on external symbols (even symbols within the shared library); different applications may have different linkage: ld -o prog1 main.o malloc.o /usr/lib/libc.a ld -o prog2 main.o /usr/lib/libc.a When a routine in libc.a calls malloc(), for prog1 should get version in malloc.o ; for prog2 should get version in libc.a Calls to external symbols are made through global tables unique to each program

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 13 Using global tables Global table contains entries for all external references malloc(n)  push [ebp + n] mov eax, [malloc_entry] call eax; indirect jump! Same-module references can still be used directly Global table entries are placed in non-shared memory locations so each program can have different linkage Initialized by hard-wired code when program begins

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 14 Relocation Before widespread support for virtual memory, programs had to be brought into memory at different locations: code had to be relocatable (could not contain fixed memory addresses) With virtual memory, all programs could start at same address, could contain fixed addresses Problem with shared libraries (e.g., DLLs): if allocated at fixed addresses, can collide in virtual memory and need to be copied and explicitly relocated to new address

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 15 Dynamic shared objects Unix systems: Code is typically compiled as a dynamic shared object (DSO): relocatable shared library Allows shared libraries to be mapped at any address in virtual memory—no copying needed! Questions: how can we make code completely relocatable? What performance impact does it have?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 16 Relocation difficulties Can’t use absolute addresses : directly named memory locations Can’t use addresses in call s to external functions Can’t use addresses for global variables— breaks the shared library scheme push [ebp + n] mov eax, [malloc_addr] ; Oops! call eax Not a problem: branch instructions, local calls. Instructions have relative addressing

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 17 Global tables Can’t put the global table at a fixed address: not relocatable! Three approaches: –pass global table address as an extra argument (possibly in a register) : affects function ptr rep –use address arithmetic on current program counter ( eip register) to find global table. Offset between eip and global table is a constant –stick global table entries into the current object’s dispatch vector : DV is the global table (only works for methods)

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 18 Cost of DSOs Assume esi contains global table pointer (can set up at beginning of function/method) Function calls: call [esi + constant] Global variable accesses: mov eax, [esi + constant] mov ebx, [eax] Calling global functions  calling methods Accessing global variables is more expensive than accessing local variables Most computer benchmarks run w/o DSOs!

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 19 Link-time optimization When object files are linked together, linker provides flags to allow peephole optimization of inter-module references Unix: -non_shared link option causes application to get its own copy of library code, allowing calls and global variables to be performed directly (peephole opt.) Allows performance/functionality trade-off call [esi + malloc_addr] call malloc

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 20 Dynamic linking Both shared libraries and DSOs can be linked dynamically into a running program Normal case is implicit linking: when setting up global tables, shared libraries are automatically loaded if necessary (maybe lazily), symbols looked up. Explicit dynamic linking: application can choose how to extend its own functionality –Unix: h = dlopen(filename) loads an object file into some free memory (if necessary), allows querying of globals: p = dlsym(h, name) –Windows: h = LoadLibrary(filename), p = GetProcAddress(h, name)

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 21 Conclusions Shared libraries and DSOs allow efficient memory use on a machine running many different programs that share code Improves cache, TLB performance overall Hurts individual program performance by adding indirections through global tables, bloating code with extra instructions Important new functionality: dynamic extension of program Peephole linker optimization can restore performance, but with loss of functionality