Linking I Topics Assembly and symbol resolution Static linking Systems I.

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.
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
Linker and Loader. Program building into four stages (C Program) Preprocessing (Preprocessor) It processes include files, conditional compilation instructions.
Mr. D. J. Patel, AITS, Rajkot 1 Operating Systems, by Dhananjay Dhamdhere1 Static and Dynamic Memory Allocation Memory allocation is an aspect of a more.
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
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.
ITEC 352 Lecture 27 Memory(4). Review Questions? Cache control –L1/L2  Main memory example –Formulas for hits.
CS 4284 Systems Capstone Godmar Back Linking and Loading.
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Object Code.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
1 Assemblers and Linkers Professor Jennifer Rexford COS 217.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
“The course that gives CMU its Zip!”
1 Assemblers and Linkers CS Goals of This Lecture Compilation process  Compile, assemble, archive, link, execute Assembling  Representing instructions.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
CS 330 What software engineers need to know about linking and a few things about execution.
Guide To UNIX Using Linux Third Edition
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Linking Topics Static linking Object files Static libraries Loading Dynamic linking of shared libraries CS213.
CS 201 Computer Systems Organization. Today’s agenda Overview of how things work Compilation and linking system Operating system Computer organization.
6.828: PC hardware and x86 Frans Kaashoek
CS 3204 Operating Systems Godmar Back Lecture 11.
MIPS coding. SPIM Some links can be found such as:
CS 367 Linking Topics Static linking Object files Static libraries
© 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.
Computer System Chapter 7. Linking Lynn Choi Korea University.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Linking Topics Static linking Object files Static libraries Loading.
Linking Topics Static linking Dynamic linking Case study: Library interpositioning.
Linking Ⅱ.
Processes and Threads-I Static and dynamic linking, Loading, Anatomy of a Process.
Linking October 5, 2002 Topics static linking object files static libraries loading dynamic linking of shared libraries Reading: Chapter 7 Problems: 7.8.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
Computer System Organization. Overview of how things work Compilation and linking system Operating system Computer organization Today’s agenda.
1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.
CSc 453 Linking and Loading
Computer System Chapter 7. Linking Lynn Choi Korea University.
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.
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)
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Lecture 3 Translation.
Instruction Set Architecture
Assemblers, linkers, loaders
Slides adapted from Bryant and O’Hallaron
Overview of today’s lecture
Linking Topics Static linking Object files Static libraries Loading
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
143A: Principles of Operating Systems Lecture 8: Basic Architecture of a Program Anton Burtsev October, 2017.
ICS143A: Principles of Operating Systems Lecture 21: Program linking and loading Anton Burtsev March, 2017.
CS 5204 Operating Systems Linking and Loading Godmar Back.
Machine-Level Programming 1 Introduction
Computer Architecture and Assembly Language
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
CS 4284 Systems Capstone Linking and Loading Godmar Back.
“The course that gives CMU its Zip!”
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
“The course that gives CMU its Zip!”
Machine-Level Programming: Introduction
Program Execution in Linux
10/6: Lecture Topics C Brainteaser More on Procedure Call
Program Assembly.
“The course that gives CMU its Zip!”
Presentation transcript:

Linking I Topics Assembly and symbol resolution Static linking Systems I

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

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

4 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/cca o /tmp/cca07630.s ld -o p [system obj files] /tmp/cca o /tmp/cca o bass>

5 Compiling/Assembling C Code double sum_loop(int val) { int sum = 0; double pi = 3.14; int i; for(i=3; i<=val; i++) { sum = sum + i; } return sum+pi; } Generated Assembly sum_loop: pushl %ebp movl %esp, %ebp movl 8(%ebp), %ecx movl $0, %edx cmpl $2, %ecx jle.L4 movl $0, %edx movl $3, %eax.L5: addl %eax, %edx addl $1, %eax cmpl %eax, %ecx jge.L5.L4: pushl %edx fildl (%esp) leal 4(%esp), %esp faddl.LC0 popl %ebp ret.LC0:.long long Obtain with command gcc -O -S sum_loop.c Produces file code.s

6 Role of the Assembler Translate assembly code into machine code Compiled or hand-generated Translate data into binary codes (using directives) Resolve symbols Translate into relocatable offsets Error check Syntax checking Ensure that constants are not too large for fields

7 Where did the labels go? Disassembled Object Code : : 55 push %ebp : 89 e5 mov %esp,%ebp : 8b 4d 08 mov 0x8(%ebp),%ecx a: ba mov $0x0,%edx f: 83 f9 02 cmp $0x2,%ecx : 7e 13 jle : ba mov $0x0,%edx : b mov $0x3,%eax e: 01 c2 add %eax,%edx : 83 c0 01 add $0x1,%eax : 39 c1 cmp %eax,%ecx : 7d f7 jge e : 52 push %edx : db fildl (%esp) b: 8d lea 0x4(%esp),%esp f: dc faddl 0x : 5d pop %ebp : c3 ret

8 Label Resolution Disassembled Object Code : 7e 13 jle … : 7d f7 jge e … f: dc faddl 0x Byte relative offsets for jle and jge jle: 13 bytes forward jge: 9 bytes bytes backward (two’s comp. of xf7) Relocatable absolute address faddl x

9 How does the assembler work One pass Record label definitions When use is found, compute offset Two pass Pass 1: scan for label instantiations - creates symbol table Pass 2: compute offsets from label use/def Can detect if computed offset is too large for assembly instruction

10 Symbol Table g F.text sum_loop Tracks location of symbols in object file Symbols that can be resolved need not be included Symbols that may be needed during linking must be included symbol type (global) segment offset from segment start symbol name

11 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 */

12 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 libraryEfficiency 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.

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

14 ELF Object File Format Elf header Magic number, type (.o, exec,.so), 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.text.rel.data.debug Section header table (required for relocatables) 0

15 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

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

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

18 Summary Today Compilation/Assembly/Linking Symbol resolution and symbol tables Next Time Code and data relocation Loading Libraries Dynamically linked libraries