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

Size: px
Start display at page:

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

Transcription

1 CSE 2421: Systems I Low-Level Programming and Computer Organization Linking Read/Study: Bryant Gojko Babić Introduction to Linkers Linking is the process of collecting and combining pieces of code and data into a single file that can be loaded (copied) into memory and executed. Linking can be performed at: compile time, when the source code is translated into machine code, load time, when the program in machine code is being loaded into memory by the loader, run time, when the program is executing. Linkers play a critical role in software development because they enable separate compilation. Thus, instead of organizing a large application as one monolithic source file, we can decompose it into smaller and more manageable modules that can be modified and compiled separately. 2 1

2 Benefits of Understanding Linking Understanding linking will: help you building larger programs, help you avoid dangerous programming errors, help you understand how language scoping rules are implemented, enable you to exploit shared libraries programs, help you understand other important system concepts; the executable object files produced by linkers play key roles in important systems functions such as loading and running programs, virtual memory, and memory mapping. 3 Why Linkers? Reason 1: Modularity Program can be written as a collection of smaller source files, rather than one monolithic mass. Can build libraries of common functions, e.g., Math library, standard C library Reason 2: Efficiency Time: Separate compilation o Change one source file, compile, and then relink. o No need to recompile other source files. Space: Libraries o Common functions can be aggregated into a single file. o Yet executable files and running memory images contain only code for the functions they actually use. 4 2

3 Example C Program File: main.c long sum(long *a, long n); long array[2] = 1, 2; long main() long val = sum(array, 2); return val; File: sum.c long sum(long *a, long n) long i, s = 0; for (i = 0; i < n; i++) s += a[i]; return s; The function main() calls the function sum(), which adds the two elements in the global array. 5 The command line: linux> gcc c sum.c creates object file sum.o Compiling sum.c sum.c Preprocessor (cpp) sum.i Compiler (cc1) sum.s Assembler (as) sum.o Source program (text) Modified source program (text) Assembly program (text) Relocatable object programs (binary) But, the command line: gcc o sum sum.c would indicate error during linking: undefined reference to main 6 3

4 Compiling & Linking main.c with sum.o The command line: linux> gcc o p main.c sum.o compiles main.c into main.o, links it with sum.o and produces executable p. sum.o main.c Preprocessor (cpp) main.i Compiler (cc1) main.s Assembler (as) main.o Linker (ld) p Source program (text) Modified source program (text) Assembly program (text) Relocatable object programs (binary) The linker is one that combines (relocatable) object files (modules) to form executable p. To run the executable p, issue the command line: linux> p Executable object program (binary) 7 Compiling & Linking main.c and sum.c gcc is a compiler driver and it invokes preprocessor, compiler, assembler, and linker programs as needed on behalf of the user: linux> gcc -o p main.c sum.c main.c Translators (cpp, cc1, as) sum.c Translators (cpp, cc1, as).c files are C source files main.o Linker (ld) p sum.o.o files are separately compiled relocatable object files File p contains fully linked executable object file (contains code and data for all functions defined in main.c and sum.c), ready to be loaded into memory and to be run. 8 4

5 Three Kinds of Object Modules (Files) Relocatable object module (.o file): contains code and data in a form that can be combined with other relocatable object files to form executable object file. each.o file is produced from exactly one source (.c) file Executable object module (exec file): contains code and data in a form that can be copied directly into memory and then executed. Shared object module (.so file): special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or runtime. Executable and Linkable Format (ELF) is the standard binary format for all object files (modules). 9 ELF Relocatable Object File Format ELF header.text.rodata.data.bss.symtab.rel.text.rel.data.debug Section header table ELF header: contains info such as word size, byte ordering, file type (.o, exec,.so), machine type (e.g. x86), etc. Section header table: contains offsets and sizes of each section.text section: contains compiled program machine code,.rodata section: contains jump tables, printf format strings.data section: initialized global variables.bss section: uninitialized global variables Note:.data &.bss sections also include local variables with static attribute 10 5

6 ELF Relocatable Object File Format (cont.) ELF header.text.rodata.data.bss.symtab.rel.text.rel.data.debug Section header table.symtab section: contains symbol table with function names, global variable names and static local variable names.rel.text section: contains relocation info for.text section, such as a list of addresses of instructions that will need to be modified in the executable, i.e. those that call external functions or reference global variables.rel.data section: contains relocation information for.data section, such as addresses of pointer data that will need to be modified in the merged executable.debug section: contain information for symbolic debugging, when compiled with option -g 11 Step 1: Symbol resolution What Do Linkers Do? Programs define and reference symbols (global variables and functions): long sum() /* define symbol sum */ sum(); /* reference symbol sum */ long array[2] /* define global variable array*/ Symbol definitions are stored in object file (by assembler) in symbol table. Symbol table is an array of structs Each entry includes name, size, and location of symbol. During this step, the linker associates each symbol reference with exactly one symbol definition. 12 6

7 Step 1: Symbol Resolution Referencing a global symbol that s defined here long sum(long *a, long n); long array[2] = 1, 2; long sum(long *a, long n) long i, s = 0; long main() long val = sum(array, 2); return val; for (i = 0; i < n; i++) s += a[i]; return s; Defining a global symbol Linker knows nothing of val Referencing a global symbol.. that s defined here Linker knows nothing of i or s 13 What Do Linkers Do? (cont.) Step 2: Relocation Merges separate code and data sections into single sections Relocates symbols from their relative locations in the.o files to their final absolute memory locations in the executable. Updates all references to these symbols to reflect their new positions. 14 7

8 Step 2: Symbol Relocation Relocatable Object Files Executable Object File System code System data.text.data 0 Headers System code main.o main().text int array[2]=1,2.data sum.o sum().text main() sum() More system code System data int array[2]=1,2.symtab.debug.text.data 15 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: Option 1: Put all functions into a single source file and made one large relocatable object module, and o programmers link that object file into their programs, o but space and time inefficient. Option 2: Put each function in a separate source file, and made a relocatable object module for each function: o programmers explicitly link appropriate object modules into their programs, o but burdensome on the programmer, although more efficient. 16 8

9 Old-fashion Solution: Static Libraries First solution: Static libraries (.a archive files) Concatenate related relocatable object files into a single file with an index of symbols, called an archive; archive filenames use.a suffix 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 it into the executable. The GNU ar program (called Archiver) creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files called archive members. 17 Updating Static Libraries atoi.c printf.c random.c Compiler Compiler... Compiler atoi.o printf.o random.o Archiver (ar) libc.a C standard library linux> ar rs libc.a atoi.o printf.o random.o Archiver ar allows incremental updates. Recompile function that changes and replace.o file in archive. 18 9

10 GNU Development Tool: Archiver ar Archiver ar creates an index to the symbols defined in relocatable object modules in the archive. option modifier r: Insert the file member(s) into archive with replacement; by default, new members are added at the end of the file, but you may use one of the modifiers to request placement relative to some existing member. option modifier c: Create the archive option modifier s: Write an object-file index into the archive, or update an existing one. 19 Commonly Used Static Libraries C standard library (libc.a): size about 4.6MB archive of 1496 object files, I/O, memory allocation, signal handling, string handling, data and time, random numbers, integer math C math library (libm.a): about 2MB and 444 object files floating point math (sin, cos, tan, log, exp, sqrt, ) % ar t libc.a sort fork.o fprintf.o fpu_control.o fputc.o freopen.o fscanf.o fseek.o fstab.o % ar t 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 Note: Our Linux system does not use static libraries! 20 10

11 Creating Private Static Library File: addvec.c void addvec(int *x, int *y, int *z, int n) int i; for (i = 0; i < n; i++) z[i] = x[i] + y[i]; File: multvec.c void multvec(int *x, int *y, int *z, int n) int i; for (i = 0; i < n; i++) z[i] = x[i] * y[i]; This is how the private library libvector.a is created & updated: linux> gcc -c addvec.c multvec.c linux> ar rcs libvector.a addvec.o multvec.o Also, it would be useful to create this text file, so you do not have include function declarations in your code: File: vector.h void addvec(int *x, int *y, int *z, int n); void multvec(int *x, int *y, int *z, int n); 21 Using Private Static Library File: main2.c #include <stdio.h> #include "vector.h //quotes for private lib int x[2] = 1, 2; int y[2] = 3, 4; int z[2]; int main() addvec(x, y, z, 2); printf( z = %d %d,z[0],z[1]); return 0; To build the executable, following command compiles main2.c and link it with addvec.o from libvector.a and printf.o from C standard library libc.a (if we had static library libc.a) : linux> gcc -o p2 main2.c libvector.a 22 11

12 Linking with Static Libraries main2.c stdio.h vector.h Translators (cpp, cc1, as) Static libraries libvector.a libc.a main2.o addvec.o printf.o Relocatable object modules Linker (ld) p2 Fully linked executable object file p2 Now executable p2 can be loaded and run: linux> p2 z = Linker Symbols Each relocatable object module has a symbol table that contains information about the linker symbols that are defined and referenced by the module. There three different kinds of symbols relevant to a linker: Global symbols: symbols defined by the module that can be referenced from other modules; includes non-static functions and global variables that are defined without static attribute. External symbols: global symbols that are references by the some module but defined by some other module. Local symbols: symbols that are defined and referenced exclusively by the some module; those are functions, global variables and local variables that are all defined with the static attribute. 24 Local (linker) symbols are not local (program) variables. 12

13 Examples of Linker Symbols Global symbol Global symbol Global symbol External symbol Local symbol int buf[2] = 1, 2; extern int buf[]; int main() swap(); return 0; External symbol Linker knows nothing of temp No global uninitialized variable case here, nor local static variable. int *bufp0 = &buf[0]; static int *bufp1; void swap() int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; Global symbol Symbol tables are built by assemblers using symbols exported by the compilers into the assembly language.s files. 25 Local Linker Symbols Local variables that are defined with the C static attribute are not managed on the stack. int f() static int x = 0; return x; int g() static int x = 1; x = x+1 return x; Here, the compiler allocates space for two integers in.data section and exports two local linker symbols to the assembler. It might use x.1 for x definition in the function f and x.2 for x definition in function g

14 Linker Step 1: Using Static Libraries The linker resolves symbol resolution by associating each reference with exactly one symbol definition from the symbol tables of its relocatable object files. It is simple for local symbols; the compiler allows only one definition of each local symbol per module. The compiler also ensures that the static local variables, which get local linker symbols, have unique names. When the compiler encounters a symbol that is not defined in the current module, it assumes that it is defined in some other module, and it generates a symbol table entry. Resolving references to global symbols is more involved, since the same symbol may be defined by multiple object files. If the linker is unable to find a definition for referenced symbol in any of its input modules, it terminates with error message. 27 How Does Linker Do Symbol Resolution Linker resolves external references as follows: scans.o files and.a files in the command line in order from left to right; Note: any.c file is automatically translated into.o file during the scan, keeps a list of the current unresolved references and a list of the previously defined symbols. as each new.o or.a file is encountered, tries to resolve each unresolved reference in the list; this may add to a list of previously defined symbols and also add to a list of the unresolved references. if any entry left in the unresolved list at end of scan, then error. Since, a command line order matters, put system libraries at the end of the command line

15 How Linker Resolves Duplicate Symbols Linker symbols are either strong or weak: Strong: functions and initialized global variables Weak: uninitialized global variables strong strong p1file.c int foo=5; p1()... p2file.c int foo; p2()... weak strong Rule 1: Multiple strong symbols with the same name are not allowed; each item can be defined only once; linker error, Rule 2: Given a strong symbol and multiple weak symbols all with the same name, choose the strong symbol, i.e. references to the weak symbol resolve to the strong symbol, Rule 3: If there are multiple weak symbols all with the same name, pick an arbitrary one. 29 Linker Puzzle int x; p1() p1() int x; p1() int x; int y; p1() int x; p2() double x; p2() Link time error: two strong symbols p1 References to x will refer to the one and the same uninitialized int x. Is this what you really want? Writes to x in p2 might overwrite y! Evil! int x=7; int y=5; p1() int x=7; p1() double x; p2() int x; p2() Writes to x in p2 will overwrite y! Nasty! References to x will refer to the one and the same initialized variable

16 Strong and Weak Symbols: Example A //File: foo3.c #include <stdio.h> void f(void); int x=1000; int main() f(); printf("x = %d\n",x); // file: bar3.c int x; void f() x=300; alpha ~/Cse2421/Linking> gcc -o foo3 foo3.c bar3.c alpha ~/Cse2421/Linking> foo3 x = 300 g. babic 31 Multiple Weak Symbols: Example //File: foo4.c #include <stdio.h> void f(void); int x; int main() x=2000; f(); printf("x = %d\n",x); // file: bar4.c int x; void f() x=600; alpha ~/Cse2421/Linking> gcc -o foo4 foo4.c bar4.c alpha ~/Cse2421/Linking> foo4 x = 600 g. babic 32 16

A Simplistic Program Translation Scheme

A Simplistic Program Translation Scheme 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:

More information

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

Linking. Computer Systems Organization (Spring 2017) CSCI-UA 201, Section 3. Instructor: Joanna Klukowska Linking Computer Systems Organization (Spring 2017) CSCI-UA 201, Section 3 Instructor: Joanna Klukowska Slides adapted from Randal E. Bryant and David R. O Hallaron (CMU) Mohamed Zahran (NYU) Example C

More information

Computer Systems. Linking. Han, Hwansoo

Computer Systems. Linking. Han, Hwansoo Computer Systems Linking Han, Hwansoo Example C Program int sum(int *a, int n); int array[2] = {1, 2}; int sum(int *a, int n) { int i, s = 0; int main() { int val = sum(array, 2); return val; } main.c

More information

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Systems Programming and Computer Architecture ( ) Timothy Roscoe Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 AS 2016 Linking 1 12: Linking Computer Architecture

More information

Systems I. Linking II

Systems I. Linking II Systems I Linking II Topics Relocation Static libraries Loading Dynamic linking of shared libraries Relocating Symbols and Resolving External References Symbols are lexical entities that name functions

More information

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

Example C program. 11: Linking. Why linkers? Modularity! Static linking. Why linkers? Efficiency! What do linkers do? 10/28/2013 Example C program 11: Linking Computer Architecture and Systems Programming 252 61, Herbstsemester 213 Timothy Roscoe main.c int buf[2] = 1, 2; swap(); return ; swap.c static int *bufp = &buf[]; void swap()

More information

Linking Oct. 15, 2002

Linking Oct. 15, 2002 15-213 The course that gives CMU its Zip! Topics Linking Oct. 15, 2002 Static linking Object files Static libraries Loading Dynamic linking of shared libraries class15.ppt Linker Puzzles int x; p1() {}

More information

Lecture 16: Linking Computer Architecture and Systems Programming ( )

Lecture 16: Linking Computer Architecture and Systems Programming ( ) Systems Group Department of Computer Science ETH Zürich Lecture 16: Linking Computer Architecture and Systems Programming (252-0061-00) Timothy Roscoe Herbstsemester 2012 Last time: memory hierarchy L1/L2

More information

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

Example C Program The course that gives CMU its Zip! Linking March 2, Static Linking. Why Linkers? Page # Topics 15-213 The course that gives CMU its Zip! Topics Linking March 2, 24 Static linking Dynamic linking Case study: Library interpositioning Example C Program main.c int buf[2] = 1, 2; int main() swap(); return

More information

Linking February 24, 2005

Linking February 24, 2005 15-213 The course that gives CMU its Zip! Linking February 24, 2005 Topics Static linking Dynamic linking Case study: Library interpositioning 13-linking.ppt Example C Program main.c int buf[2] = {1, 2};

More information

Computer Organization: A Programmer's Perspective

Computer Organization: A Programmer's Perspective A Programmer's Perspective Linking Gal A. Kaminka galk@cs.biu.ac.il A Simplistic Program Translation Scheme m.c ASCII source file Translator p Binary executable object file (memory image on disk) Problems:

More information

Exercise Session 7 Computer Architecture and Systems Programming

Exercise Session 7 Computer Architecture and Systems Programming Systems Group Department of Computer Science ETH Zürich Exercise Session 7 Computer Architecture and Systems Programming Herbstsemester 2014 Review of last week s excersice structs / arrays in Assembler

More information

LINKING. Jo, Heeseung

LINKING. Jo, Heeseung LINKING Jo, Heeseung PROGRAM TRANSLATION (1) A simplistic program translation scheme m.c ASCII source file Translator p Binary executable object file (memory image on disk) Problems: - Efficiency: small

More information

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

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

More information

Linking Oct. 26, 2009"

Linking Oct. 26, 2009 Linking Oct. 26, 2009" Linker Puzzles" int x; p1() {} p1() {} int x; p1() {} int x; p2() {} int x; int y; p1() {} int x=7; int y=5; p1() {} double x; p2() {} double x; p2() {} int x=7; p1() {} int x; p2()

More information

Link 7.A Static Linking

Link 7.A Static Linking Link 7.A Static Linking Young W. Lim 2019-01-04 Fri Young W. Lim Link 7.A Static Linking 2019-01-04 Fri 1 / 27 Outline 1 Linking - 7.A Static Linking Based on Static Library Examples Linking with Static

More information

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: January 13, 2017 at 08:55 CS429 Slideset 25: 1 Relocating Symbols

More information

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

Relocating Symbols and Resolving External References. CS429: Computer Organization and Architecture. m.o Relocation Info Relocating Symbols and Resolving External References CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: January 13,

More information

(Extract from the slides by Terrance E. Boult

(Extract from the slides by Terrance E. Boult 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

More information

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

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 Last Time Caches E = 2 e lines per set Address of word: t bits s bits b bits S = 2 s sets tag set index block offset data begins at this offset v tag 0 1 2 B 1 valid bit B = 2 b bytes per cache block (the

More information

CS 201 Linking Gerson Robboy Portland State University

CS 201 Linking Gerson Robboy Portland State University CS 201 Linking Gerson Robboy Portland State University 1 15-213, F 02 A Simplistic Program Translation Scheme m.c ASCII source file Translator p Binary executable object file (memory image on disk) Problems:

More information

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

Linker Puzzles The course that gives CMU its Zip! Linking Mar 4, A Simplistic Program Translation Scheme. A Better Scheme Using a Linker 15-213 The course that gives CMU its Zi! Toics Static linking Object files Linking Mar 4, 2003 Static libraries Loading Dynamic linking of shared libraries Linker Puzzles 1() { 1() { 1() { 1() { int x=7;

More information

u Linking u Case study: Library interpositioning

u Linking u Case study: Library interpositioning u Linking u Case study: Library interpositioning int sum(int *a, int n); int array[2] = {1, 2}; int sum(int *a, int n) { int i, s = 0; int main() { int val = sum(array, 2); return val; } main.c } for (i

More information

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

Outline. 1 Background. 2 ELF Linking. 3 Static Linking. 4 Dynamic Linking. 5 Summary. Linker. Various Stages. 1 Linking can be done at compile. Outline CS 6V81-05: System Security and Malicious Code Analysis Revealing Internals of Linkers Zhiqiang Lin Department of Computer Science University of Texas at Dallas March 26 th, 2012 1 Background 2

More information

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

Linking and Loading. CS61, Lecture 16. Prof. Stephen Chong October 25, 2011 Linking and Loading CS61, Lecture 16 Prof. Stephen Chong October 25, 2011 Announcements Midterm exam in class on Thursday 80 minute exam Open book, closed note. No electronic devices allowed Please be

More information

Lecture 12-13: Linking

Lecture 12-13: Linking CSCI-UA.0201-003 Computer Systems Organization Lecture 12-13: Linking Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Source Code to Execution C source Assembly Assembly Compiler Assembly

More information

Revealing Internals of Linkers. Zhiqiang Lin

Revealing Internals of Linkers. Zhiqiang Lin CS 6V81-05: System Security and Malicious Code Analysis Revealing Internals of Linkers Zhiqiang Lin Department of Computer Science University of Texas at Dallas March 26 th, 2012 Outline 1 Background 2

More information

Link 7. Static Linking

Link 7. Static Linking Link 7. Static Linking Young W. Lim 2018-12-21 Fri Young W. Lim Link 7. Static Linking 2018-12-21 Fri 1 / 41 Outline 1 Linking - 7. Static Linking Based on Static Library Examples Linking with Static Libraries

More information

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

Linking. Today. Next time. Static linking Object files Static & dynamically linked libraries. Exceptional control flows Linking Today Static linking Object files Static & dynamically linked libraries Next time Exceptional control flows Fabián E. Bustamante, 2007 Example C program main.c void swap(); int buf[2] = {1, 2;

More information

Sungkyunkwan University

Sungkyunkwan University Linking Case study: Library interpositioning main.c int buf[2] = {1, 2}; int main() { swap(); return 0; } swap.c extern int buf[]; int *bufp0 = &buf[0]; static int *bufp1; void swap() { int temp; } bufp1

More information

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

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition 1 Linking 15-213: Introduction to Computer Systems 13 th Lecture, October 10th, 2017 Instructor: Randy Bryant 2 Today Linking Motivation What it does How it works Dynamic linking Case study: Library interpositioning

More information

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

Systemprogrammering och operativsystem Laborationer. Linker Puzzles. Systemprogrammering 2007 Föreläsning 1 Compilation and Linking Systemprogrammering och operativsystem 2007 Denna kurs behandlar programmering nära operativsystemet. Det operativsystem vi använder är Unix (Solaris på Sun-maskiner och Linux på PC), men principerna gäller

More information

CS241 Computer Organization Spring Buffer Overflow

CS241 Computer Organization Spring Buffer Overflow CS241 Computer Organization Spring 2015 Buffer Overflow 4-02 2015 Outline! Linking & Loading, continued! Buffer Overflow Read: CSAPP2: section 3.12: out-of-bounds memory references & buffer overflow K&R:

More information

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

COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES. Credit: Mostly Bryant & O Hallaron COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES Credit: Mostly Bryant & O Hallaron Word-Oriented Memory Organization Addresses Specify Byte Locations Address of first byte in word Addresses

More information

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization Spring 2013 CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization Kitty Reeves TWRF 8:00-8:55am 1 Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing,

More information

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

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42 Link 3. Symbols Young W. Lim 2017-09-11 Mon Young W. Lim Link 3. Symbols 2017-09-11 Mon 1 / 42 Outline 1 Linking - 3. Symbols Based on Symbols Symbol Tables Symbol Table Examples main.o s symbol table

More information

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

Linking : Introduc on to Computer Systems 13 th Lecture, October 11th, Instructor: Randy Bryant. Carnegie Mellon Linking 15-213: Introduc on to Computer Systems 13 th Lecture, October 11th, 2016 Instructor: Randy Bryant 1 Today Linking Mo va on How it works Dynamic linking Case study: Library interposi oning 2 Example

More information

Link 8.A Dynamic Linking

Link 8.A Dynamic Linking Link 8.A Dynamic Linking Young W. Lim 2019-01-04 Fri Young W. Lim Link 8.A Dynamic Linking 2019-01-04 Fri 1 / 42 Outline 1 Linking - 8.A Dynamic Linking Based on Dynamic linking with a shared library example

More information

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

Generating Programs and Linking. Professor Rick Han Department of Computer Science University of Colorado at Boulder Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder CSCI 3753 Announcements Moodle - posted last Thursday s lecture Programming shell assignment

More information

Link 7. Dynamic Linking

Link 7. Dynamic Linking Link 7. Dynamic Linking Young W. Lim 2018-10-05 Fri Young W. Lim Link 7. Dynamic Linking 2018-10-05 Fri 1 / 26 Outline 1 Linking - 7. Dynamic Linking Based on Dynamic Shared Library Examples Young W. Lim

More information

Executables and Linking. CS449 Spring 2016

Executables and Linking. CS449 Spring 2016 Executables and Linking CS449 Spring 2016 Remember External Linkage Scope? #include int global = 0; void foo(); int main() { foo(); printf( global=%d\n, global); return 0; } extern int

More information

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.

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. Linking Topics How do you transform a collection of object files into an executable? How is an executable structured? Why is an executable structured as it is? Learning Objectives: Explain what ELF format

More information

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

Today. Linking. Example C Program. Sta7c Linking. Linking Case study: Library interposi7oning Today Linking Linking Case study: Library interposi7oning 15-213 / 18-213: Introduc2on to Computer Systems 12 th Lecture, Feb. 23, 2012 Instructors: Todd C. Mowry & Anthony Rowe 1 2 Example C Program Sta7c

More information

CS 550 Operating Systems Spring Process I

CS 550 Operating Systems Spring Process I CS 550 Operating Systems Spring 2018 Process I 1 Process Informal definition: A process is a program in execution. Process is not the same as a program. Program is a passive entity stored in the disk Process

More information

Compiler Drivers = GCC

Compiler Drivers = GCC Compiler Drivers = GCC When you invoke GCC, it normally does preprocessing, compilation, assembly and linking, as needed, on behalf of the user accepts options and file names as operands % gcc O1 -g -o

More information

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

CIT 595 Spring System Software: Programming Tools. Assembly Process Example: First Pass. Assembly Process Example: Second Pass. System Software: Programming Tools Programming tools carry out the mechanics of software creation within the confines of the operating system and hardware environment Linkers & Loaders CIT 595 Spring 2010

More information

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

A SimplisHc Program TranslaHon Scheme. TranslaHng the Example Program. Example C Program. Why Linkers? - Modularity. Linking A SimplisHc Program TranslaHon Scheme Linking ASCII (Text) source file The American Standard Code for InformaHon Interchange (ASCII) CSCI 221: Machine Architecture and OrganizaHon Pen- Chung Yew Department

More information

Linking. CS 485 Systems Programming Fall Instructor: James Griffioen

Linking. CS 485 Systems Programming Fall Instructor: James Griffioen Linking CS 485 Systems Programming Fall 2015 Instructor: James Griffioen Adapted from slides by R. Bryant and D. O Hallaron (hip://csapp.cs.cmu.edu/public/instructors.html) 1 Today Linking Case study:

More information

Executables and Linking. CS449 Fall 2017

Executables and Linking. CS449 Fall 2017 Executables and Linking CS449 Fall 2017 Remember External Linkage Scope? #include int global = 0; void foo(); int main() { } foo(); printf( global=%d\n, global); return 0; extern int

More information

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

Linking and Loading. ICS312 - Spring 2010 Machine-Level and Systems Programming. Henri Casanova Linking and Loading ICS312 - Spring 2010 Machine-Level and Systems Programming Henri Casanova (henric@hawaii.edu) The Big Picture High-level code char *tmpfilename; int num_schedulers=0; int num_request_submitters=0;

More information

CS2141 Software Development using C/C++ Libraries

CS2141 Software Development using C/C++ Libraries CS2141 Software Development using C/C++ Compilation and linking /* p1.c */ int x; int z; main() { x=0; z=0; printf("f(3)=%d x=%d z=%d\n",f(3),x,z); } Code for int f(int) not available yet, nor printf()

More information

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

Deadlock Detection. Several Instances of a Resource Type. Single Instance of Each Resource Type CS341: Operating System Lect26: 08 th Oct 2014 Dr. A. Sahu Dept of Comp. Sc. & Engg. Indian Institute of Technology Guwahati Deadlock Conditions Mutex, Hold & Wait, No Preemption and Circular wait Deadlock

More information

Computer Systems Organization

Computer Systems Organization Computer Systems Organization 1 Outline 2 A software view User Interface 3 How it works 4 The gcc compilation system 5 The gcc compilation system hello.c (source code) Pre-processor (cpp) hello.i (modified

More information

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

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science High Performance Computing Lecture 1 Matthew Jacob Indian Institute of Science Agenda 1. Program execution: Compilation, Object files, Function call and return, Address space, Data & its representation

More information

M2 Instruction Set Architecture

M2 Instruction Set Architecture M2 Instruction Set Architecture Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages and object code. Subroutine

More information

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

Linkers and Loaders. CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved. Linkers and Loaders CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved. Does Location Matter? int main(int argc, char *[ ]) { return(argc); } main: pushl %ebp ; push frame pointer movl

More information

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009 Compiler Theory (GCC the GNU Compiler Collection) Sandro Spina 2009 GCC Probably the most used compiler. Not only a native compiler but it can also cross-compile any program, producing executables for

More information

CS3214 Spring 2017 Exercise 2

CS3214 Spring 2017 Exercise 2 Due: See website for due date. What to submit: Upload a tar archive that contains a text file answers.txt with your answers for the questions not requiring code, as well as individual files for those that

More information

Link 8. Dynamic Linking

Link 8. Dynamic Linking Link 8. Dynamic Linking Young W. Lim 2018-12-27 Thr Young W. Lim Link 8. Dynamic Linking 2018-12-27 Thr 1 / 66 Outline 1 Linking - 8. Dynamic Linking Based on Dynamic linking with a shared library example

More information

gpio timer uart printf malloc keyboard fb gl console shell

gpio timer uart printf malloc keyboard fb gl console shell Where are We Going? Processor and memory architecture Peripherals: GPIO, timers, UART Assembly language and machine code From C to assembly language Function calls and stack frames Serial communication

More information

Process Environment. Pradipta De

Process Environment. Pradipta De Process Environment Pradipta De pradipta.de@sunykorea.ac.kr Today s Topic Program to process How is a program loaded by the kernel How does kernel set up the process Outline Review of linking and loading

More information

Memory and C/C++ modules

Memory and C/C++ modules Memory and C/C++ modules From Reading #5 and mostly #6 More OOP topics (templates; libraries) as time permits later Program building l Have: source code human readable instructions l Need: machine language

More information

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

Systemprogrammering och operativsystem Linker Puzzles. Systemprogrammering 2009 Före lä s n in g 1 Compilation and Linking Systemrogrammering och oerativsystem 2009 Denna kurs behandlar rogrammering nära oerativsystemet. Det oerativsystem vi använder är Unix (Solaris å Sun-maskiner och Linux å PC), m en rincierna gäller även

More information

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

Lecture 8: linking CS 140. Dawson Engler Stanford CS department Lecture 8: linking CS 140 Dawson Engler Stanford CS department Today s Big Adventure Linking f.c gcc f.s as f.o c.c gcc c.s as c.o ld a.out how to name and refer to things that don t exist yet how to merge

More information

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CMPSC 311- Introduction to Systems Programming Module: Build Processing CMPSC 311- Introduction to Systems Programming Module: Build Processing Professor Patrick McDaniel Fall 2016 UNIX Pipes Pipes are ways of redirecting the output of one command to the input of another Make

More information

From Source to Execution:

From Source to Execution: From Source to Execution: Translation and Linking CSE 410, Spring 2009 Computer Systems http://www.cs.washington.edu/410 4/16/2009 cse410-08-link 2006-09 Perkins, DW Johnson and University of Washington

More information

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

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp) A software view User Interface Computer Systems MTSU CSCI 3240 Spring 2016 Dr. Hyrum D. Carroll Materials from CMU and Dr. Butler How it works hello.c #include int main() { printf( hello, world\n

More information

CS Programming In C

CS Programming In C CS 24000 - Programming In C Week Two: Basic C Program Organization and Data Types Zhiyuan Li Department of Computer Science Purdue University, USA 2 int main() { } return 0; The Simplest C Program C programs

More information

Introduction Presentation A

Introduction Presentation A CSE 2421/5042: Systems I Low-Level Programming and Computer Organization Introduction Presentation A Read carefully: Bryant Chapter 1 Study: Reek Chapter 2 Skim: Reek Chapter 1 08/22/2018 Gojko Babić Some

More information

Department of Computer Science and Engineering Yonghong Yan

Department of Computer Science and Engineering Yonghong Yan Appendix A and Chapter 2.12: Compiler, Assembler, Linker and Program Execution CSCE 212 Introduction to Computer Architecture, Spring 2019 https://passlab.github.io/csce212/ Department of Computer Science

More information

LC-3 Assembly Language

LC-3 Assembly Language Chapter 7 LC-3 Assembly Language CS Reality You ve got to know assembly Chances are, you ll never write program in assembly Compilers are much better & more patient than you are Understanding assembly

More information

COS 318: Operating Systems

COS 318: Operating Systems COS 318: Operating Systems Overview Kai Li Computer Science Department Princeton University (http://www.cs.princeton.edu/courses/cos318/) Important Times Lectures 9/20 Lecture is here Other lectures in

More information

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

CS 33. Linkers. CS33 Intro to Computer Systems XXV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. CS 33 Linkers CS33 Intro to Computer Systems XXV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. gcc Steps 1) Compile to start here, supply.c file to stop here: gcc -S (produces.s file) if not

More information

Object Files. An Overview of. And Linking. Harry H. Porter III. Goals of this Paper. Portland State University cs.pdx.edu/~harry.

Object Files. An Overview of. And Linking. Harry H. Porter III. Goals of this Paper. Portland State University cs.pdx.edu/~harry. An Overview of Object Files And Linking Goals of this Paper Audience: CS-201 students Coverage: Compiling Linking Object files External Symbols Relocatable Code Segments (.text,.data.,.bss) Harry H. Porter

More information

CALL (Compiler/Assembler/Linker/ Loader)

CALL (Compiler/Assembler/Linker/ Loader) CALL (Compiler/Assembler/Linker/ Loader) 1 Integer Multiplication (1/3) Paper and pencil example (unsigned): Multiplicand 1000 8 Multiplier x1001 9 1000 0000 0000 +1000 01001000 72 m bits x n bits = m

More information

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

Midterm. Median: 56, Mean: midterm.data using 1:2 1 / 37 30 Midterm "midterm.data" using 1:2 25 20 15 10 5 0 0 20 40 60 80 100 Median: 56, Mean: 53.13 1 / 37 Today s Big Adventure f.c gcc f.s as f.o c.c gcc c.s as c.o ld a.out How to name and refer to things

More information

Link 4. Relocation. Young W. Lim Wed. Young W. Lim Link 4. Relocation Wed 1 / 22

Link 4. Relocation. Young W. Lim Wed. Young W. Lim Link 4. Relocation Wed 1 / 22 Link 4. Relocation Young W. Lim 2017-09-13 Wed Young W. Lim Link 4. Relocation 2017-09-13 Wed 1 / 22 Outline 1 Linking - 4. Relocation Based on Relocation Relocation Entries Relocating Symbol Reference

More information

ECE 15B COMPUTER ORGANIZATION

ECE 15B COMPUTER ORGANIZATION ECE 15B COMPUTER ORGANIZATION Lecture 17 Executing Programs: Compiling, Assembling, Linking and Loading (Part II) Project #3 Due June 10, 5pm Announcements Submit via email Homework #4 Due June 5, 5pm

More information

2 Compiling a C program

2 Compiling a C program 2 Compiling a C program This chapter describes how to compile C programs using gcc. Programs can be compiled from a single source file or from multiple source files, and may use system libraries and header

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

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

From Code to Program: CALL Con'nued (Linking, and Loading) ecture 13 Computer Science 61C Spring 2017 February 15th, 2017 From Code to Program: CALL Con'nued (Linking, and Loading) 1 Administrivia We know it still sucks but: Waitlist students: Please be patient.

More information

Assembly Language Programming Linkers

Assembly Language Programming Linkers Assembly Language Programming Linkers November 14, 2017 Placement problem (relocation) Because there can be more than one program in the memory, during compilation it is impossible to forecast their real

More information

Lecture 10: Program Development versus Execution Environment

Lecture 10: Program Development versus Execution Environment Lecture 10: Program Development versus Execution Environment CSE 30: Computer Organization and Systems Programming Winter 2010 Rajesh Gupta / Ryan Kastner Dept. of Computer Science and Engineering University

More information

C compiler. Memory map. Program in RAM

C compiler. Memory map. Program in RAM C compiler. Memory map. Program in RAM Sections:.text: Program code. Read only 0x40001fff stack.rodata: constants (constmodifier) and strings. Read only.data: Initialized global and static variables (startup

More information

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

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8 Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8 Ershaad Ahamed TUE-CMS, JNCASR May 2012 1 Introduction In the first session we discussed instructions that the CPU processes

More information

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

COS 318: Operating Systems. Overview. Andy Bavier Computer Science Department Princeton University COS 318: Operating Systems Overview Andy Bavier Computer Science Department Princeton University http://www.cs.princeton.edu/courses/archive/fall10/cos318/ Logistics Precepts: Tue: 7:30pm-8:30pm, 105 CS

More information

Building an Executable

Building an Executable Building an Executable CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan http://xkcd.com/1790/ Administrivia Lab 2 due Monday (7/16) Homework 3 due

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Function Calls and Stack Allocation

Function Calls and Stack Allocation Function Calls and Allocation Topics Pushing and Popping Role of in (Automatic) Allocation Parameter Passing Region of memory managed with stack discipline Grows toward lower addresses pointer indicates

More information

Function Calls and Stack Allocation

Function Calls and Stack Allocation Function Calls and Allocation Topics Pushing and Popping Role of in (Automatic) Allocation Parameter Passing Region of memory managed with stack discipline Grows toward lower addresses pointer indicates

More information

Memory Allocation in C

Memory Allocation in C Memory Allocation in C When a C program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment and heap segment. The text segment (also called

More information

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

Stack. Stack. Function Calls and Stack Allocation. Stack Popping Popping. Stack Pushing Pushing. Page 1 Function Calls and Allocation Region of memory managed with stack discipline Grows toward lower addresses pointer indicates lowest stack address Bottom Increasing Addresses Topics Pushing and Popping Role

More information

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

CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading) CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading) Instructors: Nicholas Weaver & Vladimir Stojanovic http://inst.eecs.berkeley.edu/~cs61c/sp16 1 Where Are We Now? 2 Linker

More information

Compiler, Assembler, and Linker

Compiler, Assembler, and Linker Compiler, Assembler, and Linker Minsoo Ryu Department of Computer Science and Engineering Hanyang University msryu@hanyang.ac.kr What is a Compilation? Preprocessor Compiler Assembler Linker Loader Contents

More information

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Embedded Software Development Tools Module No: CS/ES/36 Quadrant 1 e-text

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Embedded Software Development Tools Module No: CS/ES/36 Quadrant 1 e-text e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Embedded Software Development Tools Module No: CS/ES/36 Quadrant 1 e-text In this module, we will discuss about the host and target

More information

Today s Big Adventure

Today s Big Adventure Today s Big Adventure - How to name and refer to things that don t exist yet - How to merge separate name spaces into a cohesive whole Readings - man a.out & elf on a Solaris machine - run nm or objdump

More information

Today s Big Adventure

Today s Big Adventure 1/34 Today s Big Adventure - How to name and refer to things that don t exist yet - How to merge separate name spaces into a cohesive whole Readings - man a.out & elf on a Solaris machine - run nm or objdump

More information

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

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018 238P: Operating Systems Lecture 7: Basic Architecture of a Program Anton Burtsev January, 2018 What is a program? What parts do we need to run code? Parts needed to run a program Code itself By convention

More information

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4 BIL 104E Introduction to Scientific and Engineering Computing Lecture 4 Introduction Divide and Conquer Construct a program from smaller pieces or components These smaller pieces are called modules Functions

More information

Outline. Unresolved references

Outline. Unresolved references Outline CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 36: Linking and Loading 21 Nov 11 Static linking Object files Libraries Shared libraries Relocatable Dynamic linking explicit

More information