Executables and Linking. CS449 Fall 2017

Size: px
Start display at page:

Download "Executables and Linking. CS449 Fall 2017"

Transcription

1 Executables and Linking CS449 Fall 2017

2 Remember External Linkage Scope? #include <stdio.h> int global = 0; void foo(); int main() { } foo(); printf( global=%d\n, global); return 0; <main.c> extern int global; void foo() { global += 10; } <foo.c> >> gcc./main.c./foo.c >>./a.out global=10 How did global in foo.c find global in main.c? How did foo() in main.c find foo() in foo.c? Where is the code for printf() and how does the call to printf() find it? What does a.out look like stored as a file? How does it look like while executing in memory? 2

3 Compiler gcc C source Preprocessed source Object files Executable.c cpp cc1.o ld Preprocessor Compiler Linker

4 Preprocessor gcc C source Preprocessed source Object files Executable.c cpp cc1.o ld Preprocessor Compiler Linker

5 Preprocessor Input: A C source file with directives Output: The source after directives have been processed #include directive Copies the contents of the (header) file to (source) file Source file (files with.c extensions): Contains function/variable definitions Header file (files with.h extensions): Contains function/variable declarations and type definitions Typically included at top of.c file hence the name #include <...> or #include... <...> searches for file under standard include path (e.g. /usr/include)... searches for file under local directory or paths given as I arguments (e.g. gcc I ~/local/include main.c)

6 Preprocessor #define directive Defines a macro (a symbol that is expanded to some other text before compilation) #define PI 3.14 All instances of PI gets replaced by 3.14 in source #define AVG(a, b) (((a) + (b)) / 2) All instances of AVG(2, 4) gets replaced by (((2) + (4)) / 2) Not a function call, just a text replacement No argument / return type declarations Will even work on AVG( Hello, 3.14), even though the result will not make sense at all -E option produces preprocessed source file gcc E main.c dumps preprocessed code to stdout

7 Compiler gcc C source Preprocessed source Object files Executable.c cpp cc1.o ld Preprocessor Compiler Linker

8 Compiler Input: A preprocessed C source file Output: A binary object file in machine language In the process it performs: Syntax and type checking Compiler optimizations to reduce execution time and memory footprint E.g. gcc O2 main.c (applies level 2 optimizations) Code generation for given machine One object file (.o) for one C source file (.c) The c option invokes the compiler only and produces object files instead of the final linked executable gcc main.c foo.c translates to: gcc c main.c foo.c (produces.o files; this is compilation) gcc main.o foo.o (produces a.out; this is linking)

9 Linker gcc C source Preprocessed source Object files Executable.c cpp cc1.o ld Preprocessor Compiler Linker

10 Linker Input: One or more object files Output: A linked executable image Why separate out compile and link stages? Leads to shorter executable generation time Compiling is slow, linking is fast On a source code update: Need only compile changed source file and then re-link (cheap) No need to recompile the entire project (expensive) Intermediate object files can be reused A library file is a package of one or more object files e.g. C standard library, Math library, Networking library etc. Libraries can be reused across multiple projects

11 Memory Space of Program 0xc Stack (Automatic variables) brk Dynamic Data 0 Heap (Malloc ed Memory) Data Segment (Static variables) Text Segment ( Function Code) Executable Image Linker: generates executable image from multiple object files Executable image: copied to memory when program launched

12 main.o foo.o The Linking Process [Data Segment] int global; [Text Segment] Int main() { foo(); 0 } [Data Segment] [Text Segment] void foo() { global += 10; 0 } Link [Data Segment] int global; [Text Segment] Int main() { foo(); } [Text Segment] void foo() { global += 10; } Relocation: Figuring out address of main, foo, global in a.out Symbol Resolution: Replacing uses of foo, global with addresses 0 [Data Segment] a.out

13 thoth $ objdump -S./main.o Disassembly of section.text: <main>: The Linking Process 0: 55 push %rbp... 9: e callq <foo> e: 8b mov... thoth $ objdump -S./foo.o Disassembly of section.text: <foo>: 0: 55 push %rbp... 14: c3 retq Reloca'on Resolu'on thoth $ gcc o./a.out./main.o./foo.o thoth $ objdump S./a.out Disassembly of section.text: c4 <main>: 4004c4: 55 push %rbp cd: e d2: 8b mov... Reloca'on f4 <foo>: callq 4004f4 <foo> 4004f4: 55 push %rbp : c3 retq objdump -S: dumps text segment for object 13

14 The Linking Process (in Detail) Previous compiler stage embeds two tables in each object Symbol table: Stores addresses of symbol locations Where each symbol is located at (in either data or code section) Entry fields: symbol name, section name, address Relocation table: Stores addresses of symbol uses Locations of all the uses of symbol (in code section) Entry fields: symbol name, address Step 1: Relocation of object files into executable image Merge individual code / data sections into single sections Merge individual symbol / relocation tables into single tables Translate address within each object to address in final image Step 2: Symbol resolution across object files Replaces all symbol uses with addresses using merged global symbol table and relocation table

15 Symbol Tables (Before Linking) #include <stdio.h> int global = 0; int global2 = 0; void foo(); int main() { foo(); printf( global=%d\n, global); return 0; } <main.c> extern int global; void foo() { global += 10; } <foo.c> >> gcc c./main.c./foo.c >> nm./main.o U foo B global B global T main >> nm./foo.o U printf T foo U global nm: a tool that shows the symbol table offset + section name + symbol name Symbols foo, printf undefined in main.o Symbol global undefined in foo.o 15

16 Relocation Tables (Before Linking) #include <stdio.h> int global = 0; int global2 = 0; void foo(); int main() { foo(); printf( global=%d\n, global); return 0; } <main.c> extern int global; void foo() { global += 10; } <foo.c> >> gcc c./main.c./foo.c >> objdump r./main.o [sic] OFFSET TYPE a R_X86_64_PC32 VALUE foo R_X86_64_32 global R_X86_64_32.rodata R_X86_64_PC32 printf objdump -r: dumps relocation table for object Offsets store code locations using symbol For example, in main.o at address 0xa: 9: e (callq <foo>) Linker will rewrite call target with final location of foo. In a.out after rewriting: 4004cd: e (callq 4004f4 <foo>) 16

17 Relocation Tables (Before Linking) #include <stdio.h> int global = 0; int global2 = 0; void foo(); int main() { foo(); printf( global=%d\n, global); return 0; } <main.c> extern int global; void foo() { global += 10; } <foo.c> >> gcc c./main.c./foo.c >> objdump r./foo.o [sic] OFFSET TYPE VALUE R_X86_64_PC32 global f R_X86_64_PC32 global In foo.o (read / write of global): 4: 8b mov 0x0(%rip),%eax a: 83 c0 0a add $0xa,%eax d: mov %eax,0x0(%rip) Linker will rewrite memory accesses with final location of global. In a.out after rewriting: 4: 8b 05 e mov 0x2003e2(%rip),%eax a: 83 c0 0a add $0xa,%eax d: d mov %eax, 0x2003d9(%rip) 17

18 Symbol Tables (After Linking) #include <stdio.h> int global = 0; int global2 = 0; void foo(); int main() { foo(); printf( global=%d\n, global); return 0; } <main.c> extern int global; void foo() { global += 10; } <foo.c> >> gcc c./main.c./foo.c >> gcc./main.o./foo.o >> nm./a.out f4 T foo e0 B global e4 B global c4 T main U printf@@glibc_2.2.5 Symbols foo, global defined and now have relocated addresses in memory Main.o data section relocated to 6008e0 Foo.o code section relocated to 4004f4 Symbol printf still undefined Will be defined through dynamic linking 18

19 Relocation Tables (After Linking) #include <stdio.h> int global = 0; int global2 = 0; void foo(); int main() { foo(); printf( global=%d\n, global); return 0; } <main.c> extern int global; void foo() { global += 10; } <foo.c> >> gcc c./main.c./foo.c >> objdump R./a.out [sic] OFFSET TYPE [sic] VALUE R_X86_64_JUMP_SLOT printf Relocation table of a.out contains printf since it has not yet been resolved This will be resolved later at load time during dynamic linking 19

20 Quick Quiz Q: Are automatic local variables relocated and resolved? A: No. Local variables are stored in the stack and is not part of the executable image generated by linker. Q: Are internal linkage symbols relocated and resolved? A: Yes. Internal linkage symbols are part of the data segment and need to be relocated. Also, uses of the symbols need to be resolved to the final address. Q: Are static local symbols relocated and resolved? A: Yes. Static local symbols are also part of the data segment since their lifetime is static. Hence, they need to be relocated and resolved for the same reasons.

21 3 Ways to Link in Libraries Static Linking Copy library code into executable image at compile time Done by linker (ld) at last stage of compilation Dynamic Linking Copy library code into memory at program load time Done by dynamic linker (ld-linux.so) when program first launched Dynamic Loading Copy library code into memory in the middle of execution Done through dynamic loader (libdl.so) library by program itself DL library provides APIs to load/unload libraries into memory, search in library symbol table to perform symbol resolution

22 Static Linking #include <stdio.h> #include <math.h> int main() { printf( The sqrt of 9 is %f\n, sqrt(9)); return 0; } Archives /usr/lib/libc.a Object files /usr/lib/libm.a Executable.o ld Linker

23 Static Linking #include <stdio.h> int global = 0; int global2 = 0; void foo(); int main() { foo(); printf( global=%d\n, global); return 0; } <main.c> extern int global; void foo() { global += 10; } <foo.c> >> gcc -m32 -static./main.c./foo.c -lc >> ls l./a.out -rwxr-xr-x 1 wahn UNKNOWN Sep 30 15:20./a.out >> objdump R./a.out objdump:./a.out: not a dynamic object objdump:./a.out: Invalid operation -static tells gcc linker to do static linking -lc links in library libc.a (found in /usr/lib) Static linking copies over all libraries into binary (just like with object files) That s why a.out is so large! 23

24 Dynamic Linking Stack /usr/lib/libc.so Shared Objects /usr/lib/libm.so libc.so libm.so Executable ld-linux.so Dynamic Linker Data (Heap) Data (Globals) Text (Code)

25 Dynamic Linking #include <stdio.h> int global = 0; int global2 = 0; void foo(); int main() { foo(); printf( global=%d\n, global); return 0; } <main.c> extern int global; void foo() { global += 10; } <foo.c> >> gcc m32./main.c./foo.c -lc >> ls l./a.out -rwxr-xr-x 1 wahn UNKNOWN Sep 30 15:26./a.out >> ldd./a.out linux-gate.so.1 => (0x ) libc.so.6 => /lib/libc.so.6 (0x00bbd000) /lib/ld-linux.so.2 (0x00b9b000) By default gcc does dynamic linking Now a.out is much smaller! -lc: marks dependency on libc.so ldd: prints out shared library dependencies Program will not start if not satisfied 25

26 Dynamic Loading Stack Stack libc.so libm.so Data (Heap) Data (Heap) Data (Globals) Text (Code) dlopen( libc.so ); dlopen( libm.so ); Data (Globals) Text (Code)

27 Function Pointers How are function names translated to function addresses on a library call? Static linking: linker resolves address in image Dynamic linking: dynamic linker resolves address when image is loaded into memory Dynamic loading: program itself does resolving Address of function is searched in object file symbol table, with the help of dynamic loader library The type of that address is a function pointer

28 Dynamic Loading #include <dlfcn.h> int main() { } void *handle; int (*pf)(const char *format,...); /* load C library into memory*/ handle = dlopen("libc.so.6", RTLD_LAZY); /* find printf in symbol table */ pf = dlsym(handle, "printf"); pf("hello world!\n"); /* unload C library from memory */ dlclose(handle); return 0; >> gcc -ldl./dlsym.c >>./a.out Hello world! For when program doesn t know what libraries will be used beforehand E.g. browser plugins that start working right away after download 28

29 Dynamic Linking (Loading) Pros/Cons Pro: Library code is not duplicated Efficient use of file storage library code need not be duplicated in every application using it Easier to update libraries don t have to relink every application on update Con: Program binary is not self-contained Less portable program relies on certain libraries being installed on the system to run correctly Maintain multiple versions of same library (DLL Hell) if programs rely on specific versions Security challenges security properties of program change depending on libraries installed on system

30 Executable File Includes everything needed to run on a system Executable image (that is loaded directly into memory) Code segment Data segment (with initial values of variables) Symbol / relocation tables (if applicable) Code / Data segment may be relocated (again) through dynamic linking Executable files and object files share same format Executables are generated by the linker Object files are generated by the compiler But they need mostly the same things

31 Older Executable Formats a.out (Assembler OUTput) Oldest UNIX format No longer commonly used COFF (Common Object File Format) Older UNIX Format No longer commonly used

32 Modern Executable Formats ELF (Executable and Linkable Format) Linux/UNIX format PE (Portable Executable) Windows format Based on COFF Mach-O file Mac format

33 a.out exec header (describes structure) text segment (code) data segment (static lifetime data) symbol table string table (symbol name strings) text relocation table data relocation table

34 Header Every a.out formatted binary file begins with an exec header structure: struct exec { unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long }; a_midmag; // format identifier a_text; // size of text segment a_data; // size of initialized data a_bss; // size of uninitialized data a_syms; // size of symbol table a_entry; // entry point of program a_trsize; // size of text relocation table a_drsize; // size of data relocation table

35 Linux Address Space

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

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

(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

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

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

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005 Systems Programming Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005 Outline How to assemble and link nasm ld gcc Debugging Using gdb; breakpoints,registers,

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

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

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

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

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

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 Chapter 1 Program Structure In the beginning there were 0s and 1s. GRR 1.1 Introduction In this chapter we will talk about memory: bits, bytes and how data is represented in the computer. We will also

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

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

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

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

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

Generic Programming in C

Generic Programming in C Generic Programming in C Void * This is where the real fun starts There is too much coding everywhere else! 1 Using void * and function pointers to write generic code Using libraries to reuse code without

More information

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

CSE 2421: Systems I Low-Level Programming and Computer Organization. Linking. Presentation N. Introduction to Linkers CSE 2421: Systems I Low-Level Programming and Computer Organization Linking Read/Study: Bryant 7.1 7.10 Gojko Babić 11-15-2017 Introduction to Linkers Linking is the process of collecting and combining

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

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

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

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

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

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

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

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

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

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC.

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC. Building cross compilers Linux as the target platform Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC. configure --target=i486-linux --host=xxx on

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

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

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017 Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles CS449 Fall 2017 Multi-file Development Multi-file Development Why break code into multiple source files? Parallel development

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

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

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

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

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

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

CS140 - Summer Handout #8

CS140 - Summer Handout #8 CS1 - Summer 2 - Handout # Today s Big Adventure Linking f.c gcc f.s as c.c gcc c.s as c.o how to name and refer to things that don t exist yet how to merge separate name spaces into a cohesive whole Readings

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

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

Compilation, Disassembly, and Profiling (in Linux)

Compilation, Disassembly, and Profiling (in Linux) Compilation, Disassembly, and Profiling (in Linux) CS 485: Systems Programming Spring 2016 Instructor: Neil Moore 1 Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc O1 p1.c

More information

C03c: Linkers and Loaders

C03c: Linkers and Loaders CISC 3320 MW3 C03c: Linkers and Loaders Hui Chen Department of Computer & Information Science CUNY Brooklyn College 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 1 Outline Linkers and linking Loaders and

More information

2012 LLVM Euro - Michael Spencer. lld. Friday, April 13, The LLVM Linker

2012 LLVM Euro - Michael Spencer. lld. Friday, April 13, The LLVM Linker lld Friday, April 13, 2012 The LLVM Linker What is lld? A system linker Produce final libraries and executables, no other tools or runtime required Understands platform ABI What is lld? A system linker

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

CSL373: Operating Systems Linking

CSL373: Operating Systems Linking CSL373: Operating Systems Linking Today Linking f.c gcc f.s as c.c gcc c.s as f.o c.o ld a.out e.g., f.o = hello.o (uses printf); c.o = libc.o (implements printf) How to name and refer to things that don

More information

Compila(on, Disassembly, and Profiling

Compila(on, Disassembly, and Profiling Compila(on, Disassembly, and Profiling (in Linux) CS 485: Systems Programming Fall 2015 Instructor: James Griffioen 1 Recall the compila(on process/steps 2 Turning C into Object Code Code in files p1.c

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

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 2: Hello World! Cristina Nita-Rotaru Lecture 2/ Fall 2013 1 Introducing C High-level programming language Developed between 1969 and 1973 by Dennis Ritchie at the Bell Labs

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

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

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

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006 C Compilation Model Comp-206 : Introduction to Software Systems Lecture 9 Alexandre Denault Computer Science McGill University Fall 2006 Midterm Date: Thursday, October 19th, 2006 Time: from 16h00 to 17h30

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

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

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

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

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

Outline. Outline. Common Linux tools to explore object/executable files. Revealing Internals of Loader. Zhiqiang Lin

Outline. Outline. Common Linux tools to explore object/executable files. Revealing Internals of Loader. Zhiqiang Lin CS 6V81-05: System Security and Malicious Code Analysis Revealing Internals of Loader Zhiqiang Lin Department of Computer Science University of Texas at Dallas March 28 th, 2012 Common Linux tools to explore

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

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

Link Edits and Relocatable Code

Link Edits and Relocatable Code Link Edits and Relocatable Code Computer Systems Chapter 7.4-7.7 gcc g o ttt ttt.c ttt.c gcc ttt Pre-Processor Linker Compiler Assembler ttt.s ttt.o gcc g o ttt ttt.c main.c gcc cmd util.c Pre-Processor

More information

Making Address Spaces Smaller

Making Address Spaces Smaller ICS332 Operating Systems Spring 2018 Smaller Address Spaces Having small address spaces is always a good idea This is good for swapping: don t swap as often (because if address spaces are small, then RAM

More information

The C standard library

The C standard library C introduction The C standard library The C standard library 1 / 12 Contents Do not reinvent the wheel Useful headers Man page The C standard library 2 / 12 The Hitchhiker s Guide to the standard library

More information

Process Address Spaces and Binary Formats

Process Address Spaces and Binary Formats Process Address Spaces and Binary Formats Don Porter Background We ve talked some about processes This lecture: discuss overall virtual memory organizafon Key abstracfon: Address space We will learn about

More information

CS631 - Advanced Programming in the UNIX Environment

CS631 - Advanced Programming in the UNIX Environment CS631 - Advanced Programming in the UNIX Environment Slide 1 CS631 - Advanced Programming in the UNIX Environment Shared Libraries Department of Computer Science Stevens Institute of Technology Jan Schaumann

More information

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

#include <stdio.h> int main() { printf (hello class\n); return 0; } C #include int main() printf ("hello class\n"); return 0; Working environment Linux, gcc We ll work with c9.io website, which works with ubuntu I recommend to install ubuntu too Also in tirgul

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Numbers wrap-up This is not on the exam! Break C Primer Administrivia,

More information

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

CS 33. Libraries. CS33 Intro to Computer Systems XXVIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. CS 33 Libraries CS33 Intro to Computer Systems XXVIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Libraries Collections of useful stuff Allow you to: incorporate items into your program substitute

More information

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Numbers wrap-up This is not on the exam! Break C Primer Administrivia,

More information

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming TDDB68 Concurrent Programming and Operating Systems Lecture 2: Introduction to C programming Mikael Asplund, Senior Lecturer Real-time Systems Laboratory Department of Computer and Information Science

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

ECE 598 Advanced Operating Systems Lecture 10

ECE 598 Advanced Operating Systems Lecture 10 ECE 598 Advanced Operating Systems Lecture 10 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 22 February 2018 Announcements Homework #5 will be posted 1 Blocking vs Nonblocking

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

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

Lecture 3: C Programm

Lecture 3: C Programm 0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory

More information

Important From Last Time

Important From Last Time Important From Last Time Embedded C Pros and cons Macros and how to avoid them Intrinsics Interrupt syntax Inline assembly Today Advanced C What C programs mean How to create C programs that mean nothing

More information

x86 assembly CS449 Fall 2017

x86 assembly CS449 Fall 2017 x86 assembly CS449 Fall 2017 x86 is a CISC CISC (Complex Instruction Set Computer) e.g. x86 Hundreds of (complex) instructions Only a handful of registers RISC (Reduced Instruction Set Computer) e.g. MIPS

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012 TOPICS TODAY Assembling & Linking Assembly Language Separate Compilation in C Scope and Lifetime LINKING IN ASSEMBLY

More information

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Important From Last Time Today Embedded C Pros and cons Macros and how to avoid them Intrinsics Interrupt syntax Inline assembly Advanced C What C programs mean How to create C programs that mean nothing

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

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

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Announcements Exam 1 (20%): Feb. 27 (Tuesday) Tentative Proposal Deadline:

More information

238P: Operating Systems. Lecture 4: Linking and Loading (Basic architecture of a program) Anton Burtsev October, 2018

238P: Operating Systems. Lecture 4: Linking and Loading (Basic architecture of a program) Anton Burtsev October, 2018 238P: Operating Systems Lecture 4: Linking and Loading (Basic architecture of a program) Anton Burtsev October, 2018 What is a program? What parts do we need to run code? Parts needed to run a program

More information

Errors During Compilation and Execution Background Information

Errors During Compilation and Execution Background Information Errors During Compilation and Execution Background Information Preprocessor Directives and Compilation #define - defines a macro, identified by . During compilation, all instances of

More information

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static

More information

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level

More information

Important From Last Time

Important From Last Time Important From Last Time Embedded C Ø Pros and cons Macros and how to avoid them Intrinsics Interrupt syntax Inline assembly Today Advanced C What C programs mean How to create C programs that mean nothing

More information

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c How to learn C? CSCI [4 6]730: A C Refresher or Introduction Hello Word! ~/ctutorial/ In addition to syntax you need to learn: the Tools. the Libraries. And the Documentation (how to access) Practice on

More information

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4 Beyond this course Readings: CP:AMA 2.1, 15.4 CS 136 Spring 2018 13: Beyond 1 Machine code In Section 04 we briefly discussed compiling: converting source code into machine code so it can be run or executed.

More information

143A: Principles of Operating Systems. Lecture 4: Linking and Loading (Basic architecture of a program) Anton Burtsev October, 2018

143A: Principles of Operating Systems. Lecture 4: Linking and Loading (Basic architecture of a program) Anton Burtsev October, 2018 143A: Principles of Operating Systems Lecture 4: Linking and Loading (Basic architecture of a program) Anton Burtsev October, 2018 What is a program? What parts do we need to run code? Parts needed to

More information

CS 107 Lecture 18: GCC and Make

CS 107 Lecture 18: GCC and Make S 107 Lecture 18: G and Make Monday, March 12, 2018 omputer Systems Winter 2018 Stanford University omputer Science Department Lecturers: Gabbi Fisher and hris hute Today's Topics 1. What really happens

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

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang Topics History of Programming Languages Compilation Process Anatomy of C CMSC 104 Coding Standards Machine Code In the beginning,

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

Dynamic & Static Libraries in Linux (New Talk!) Wim Cardoen and Brett Milash CHPC User Services

Dynamic & Static Libraries in Linux (New Talk!) Wim Cardoen and Brett Milash CHPC User Services Dynamic & Static Libraries in Linux (New Talk!) Wim Cardoen and Brett Milash CHPC User Services Overview Warm-up exercise: Install GSL What is a library? Static vs. Dynamic libraries Create static & dynamic

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

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

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

Operating Systems CMPSC 473. Process Management January 29, Lecture 4 Instructor: Trent Jaeger Operating Systems CMPSC 473 Process Management January 29, 2008 - Lecture 4 Instructor: Trent Jaeger Last class: Operating system structure and basics Today: Process Management Why Processes? We have programs,

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

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c! Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Hello Word! ~/ctest/ In addition to syntax you need to learn: the Tools the Libraries. And the Documentation. Maria Hybinette,

More information