CS429: Computer Organization and Architecture

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

Systems I. Linking II

Linking Oct. 15, 2002

Linking Oct. 26, 2009"

LINKING. Jo, Heeseung

A Simplistic Program Translation Scheme

Computer Organization: A Programmer's Perspective

(Extract from the slides by Terrance E. Boult

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

CS 201 Linking Gerson Robboy Portland State University

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

Linking February 24, 2005

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

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

Systems Programming and Computer Architecture ( ) Timothy Roscoe

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

Lecture 16: Linking Computer Architecture and Systems Programming ( )

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

Computer Systems. Linking. Han, Hwansoo

Exercise Session 7 Computer Architecture and Systems Programming

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

E = 2 e lines per set. S = 2 s sets tag. valid bit B = 2 b bytes per cache block (the data) CSE351 Inaugural EdiNon Spring

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

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

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

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

Revealing Internals of Linkers. Zhiqiang Lin

CS241 Computer Organization Spring Buffer Overflow

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

Computer Systems Organization

Lecture 12-13: Linking

Sungkyunkwan University

u Linking u Case study: Library interpositioning

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

Link 7. Static Linking

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

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

CPEG421/621 Tutorial

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

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

Link 7.A Static Linking

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

Link 2. Object Files

Executables and Linking. CS449 Fall 2017

Binghamton University. CS-220 Spring Loading Code. Computer Systems Chapter 7.5, 7.8, 7.9

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

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

Executables and Linking. CS449 Spring 2016

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

Introduction Presentation A

Link Edits and Relocatable Code

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

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

CS2141 Software Development using C/C++ Libraries

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

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

Instruction Set Architectures

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

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CS429: Computer Organization and Architecture

Assembly Language Programming Linkers

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Machine Language, Assemblers and Linkers"

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

CS , Fall 2002 Exam 1

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

CIT Week13 Lecture

Instruction Set Architectures

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

Link 2. Object Files

Compiler Drivers = GCC

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

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Outline. Unresolved references

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

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

CS , Fall 2001 Exam 1

CS3214 Spring 2017 Exercise 2

M2 Instruction Set Architecture

Assembly Language: Function Calls" Goals of this Lecture"

Overview REWARDS TIE HOWARD Summary CS 6V Data Structure Reverse Engineering. Zhiqiang Lin

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

Machine Programming 1: Introduction

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

Assembly Language: Function Calls" Goals of this Lecture"

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

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture

Today s Big Adventure

Assembly Language: Function Calls

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Transcription:

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 and Resolving External References m.c Symbols are lexical entities that name functions and variables. Each symbol has a value (typically a memory address). Code consists of symbol definitions and references. References can be either local or external. int e = 7; int main() { int r = a() ; exit (0) ; } // def of global e // ref to external symbol a // ref to external symbol exit // ( defined in libc. so) Note that e is locally defined, but global in that it is visible to all modules. Declaring a variable static limits its scope to the current file module. CS429 Slideset 25: 2

Relocating Symbols and Resolving External References (2) a.c extern int e; int ep = &e; int x = 15; int y; // def of global ep, ref to // external symbol e // def of global x // def of global y int a() { // def of global a return ep+x+y; // refs of globals ep, x, y } CS429 Slideset 25: 3

m.o Relocation Info Disassembly of section.text m.c int e = 7; int main() { int r = a() ; exit (0) ; } Source: objdump 00000000 <main >: 0: 55 pushl %ebp 1: 89 e5 movl %esp, %ebp 3: e8 fc ff ff ff call 4<main+0x4> 4: R 386 PC32 a 8: 6a 00 pushl $0x0 a: e8 fc ff ff ff call b<main+0xb> b: R 386 PC32 exit f 90 nop Disassembly of section.data 00000000 <e >: 0: 07 00 00 00 CS429 Slideset 25: 4

a.o Relocation Info (.text) Disassembly of section.text a.c extern int e ; int ep = &e ; int x = 15; int y; int a() { return ep + x + y; } 00000000 <a >: 0: 55 pushl %ebp 1: 8b 15 00 00 00 movl 0x0, %edx 6: 00 3: R 386 32 ep 7: a1 00 00 00 00 movl 0x0, %eax 8: R 386 32 x c : 89 e5 movl %esp, %ebp e : 03 02 addl (%edx),%eax 10: 89 ec movl %ebp, %esp 12: 03 05 00 00 00 addl 0x0, %eax 17: 00 14: R 386 32 y 18: 5d popl %ebp 19: 3c ret CS429 Slideset 25: 5

a.o Relocation Info (.data) a.c extern int e; int ep = &e; int x = 15; int y; int a() { return ep + x + y; } Disassembly of section.data 00000000 <ep >: 0: 00 00 00 00 0: R 386 32 e 00000004 <x >: 4: 0f 00 00 00 CS429 Slideset 25: 6

Executable After Relocation (.text) 08048530 <main >: 8048530: 55 pushl %ebp 8048531: 89 e5 movl %esp, %ebp 8048533: e8 08 00 00 00 call 8048540 <a> 8048538: 6a 00 pushl $0x0 804853a: e8 35 ff ff ff call 8048474 < init+0x94> 804853 f : 90 nop 08048540 <a >: 8048540: 55 pushl %ebp 8048541: 8b 15 1c a0 04 movl 0x804a01c, %edx 8048546: 08 8048547: a1 20 a0 04 08 movl 0x804a020, %eax 804854c : 89 e5 movl %esp, %ebp 804854e : 03 02 addl (%edx), %eax 8048550: 89 ec movl %ebp, %esp 8048552: 03 05 d0 a3 04 addl 0x804a3d0, %eax 8048557: 08 8048558: 5d popl %ebp 8048559: c3 ret CS429 Slideset 25: 7

Executable After Relocation (.data) m.c int e = 7; int main() { int r = a() ; exit (0) ; } a.c extern int e; int ep = &e; int x = 15; int y; Disassembly of section.data 0804a018 <e>: 804a018 : 07 00 00 00 0804a01c <ep>: 804a01c : 18 a0 04 08 0804a020 <x>: 804a020 : 0f 00 00 00 int a() { return ep + x + y; } CS429 Slideset 25: 8

Strong and Weak Symbols Program symbols are either strong or weak. strong: procedures and initialized globals weak: uninitialized globals This doesn t apply to purely local variables. p1.c int foo = 5; // foo : strong p2.c int foo ; // foo : weak here p1() {... } // p1: strong p2() {... } // p2: strong CS429 Slideset 25: 9

Linker Symbol Rules Rule 1: A strong symbol can only appear once. Rule 2: A weak symbol can be overridden by a strong symbol of the same name. References to the weak symbol resolve to the strong symbol. Rule 3: If there are multiple weak symbols, the linker can pick one arbitrarily. CS429 Slideset 25: 10

Linker Puzzles What happens in each case? File 1 File 2 Result int x; p1() {} p1() {} int x; int x; p1() {} p2() {} int x; double x; int y; p2() {} p1() {} int x=7; double x; int y=5; p2() {} p1() {} int x=7; int x; p1() {} p2() {} CS429 Slideset 25: 11

Linker Puzzles Think carefully about each of these. File 1 File 2 Result int x; Link time error: two strong symbols (p1) p1() {} p1() {} int x; int x; References to x will refer to the same p1() {} p2() {} unitialized int. What you wanted? int x; double x; Writes to x in p2 might overwrite y! int y; p2() {} That s just evil! p1() {} int x=7; double x; Writes to x in p2 might overwrite y! int y=5; p2() {} Very nasty! p1() {} int x=7; int x; References to x will refer to the same p1() {} p2() {} initialized variable. Nightmare scenario: two identical weak structs, compiled by different compilers with different alignment rules. CS429 Slideset 25: 12

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. Programmers link big object file into their programs. Space and time inefficient. Option 2: Put each function in a separate source file. Programmers explicitly link appropriate binaries into their programs. More efficient, but burdensome on the programmer. CS429 Slideset 25: 13

Packaging Commonly Used Functions Solution: static libraries (.a archive files) Concatenate related relocatable object files into a single repository with an index (called an archive). Enhance the linker so that it tries to resolve unresolved external reference by looking for symbols in one or more archives. If an archive member resolves the reference, link into the executable. CS429 Slideset 25: 14

Static Libraries (archives) p1.c Translator p2.c Translator libc.a is a static library (archive) of relocatable object files concatenated into one file. p1.o p2.o libc.a Linker (ls) The output p is an executable object file that only contains code and data for libc functions called from p1.c and p2.c. p This further improves modularity and efficiency by packaging commonly used functions, e.g., C standard library (libc) or math library (libm). The linker extracts only those.o files from the archive that are actually needed by the program. CS429 Slideset 25: 15

Creating Static Libraries atoi.c printf.c random.c Translator Translator... Translator atoi.o printf.o... random.o Archiver (ar) libc.a Command: ar rs libc.a atoi.o printf.o... random.o Archiver allows incremental updates: Recompile a function that changes and replace the.o file in the archive. CS429 Slideset 25: 16

Commonly Used Libraries libc.a (the C standard library) 8MB archive of 900 object files I/O, memory allocation, signal handling, string handling, data and time, random numbers, integer math libm.a (the C math library) 1MB archive of 226 object files floating point math (sin, cos, tan, log, exp, sqrt,...) % ar t /usr/ lib /libc.a sort... fork.o... fprintf.o fpu control.o fputc.o freopen.o fscanf.o... CS429 Slideset 25: 17 % ar t /usr/ lib /libm.a sort... e acos.o e acosf.o e acosh.o e acoshf.o e acoshl.o e acosl.o...

Using Static Libraries Linker s algorithm for resolving external references: Problem: Scan.o files and.a files in the command line order. During the scan, keep a list of the current unresolved references. As each new.o or.a file obj is encountered, try to resolve each unresolved reference in the list against the symbols in obj. If there are any entries in the unresolved list at the end of the scan, then error. Command line order matters. Moral: put libraries at the end of the command line. > gcc L. libtest.o lmine > gcc L. lmine libtest.o libtest.o: In function main : libtest.o(. text+0x4) : undefined reference to libfun CS429 Slideset 25: 18

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

Limitations of Static Libraries Static libraries have some disadvantages: Potential for duplicating lots of common code in the executable files on a file system. (e.g., every program needs the standard C library). Potential for duplicating lots of code in the virtual memory space of many processes. Minor bug fixes of system libraries require each application to explicitly relink. CS429 Slideset 25: 20

Shared Libraries Solution: Shared libraries (dynamic link libraries DLLs) whose members are dynamically loaded into memory and linked into an application at run-time. Dynamic linking can occur when an executable is first loaded and run. (The common case for Linux, handled automatically by ld-linux.so.) Dynamic linking can also occur after the program has begun. In Linux, this is done explicitly by user with dlopen(). Basis for High-Performance Web Servers. Shared library routines can be shared by multiple processes. CS429 Slideset 25: 21

Dynamically Linked Shared Libraries m.c a.c Translators (cc1, as) Translators (cc1, as) m.o a.o Linker (ld) Partially linked executable p (on disk) p libc.so Shared library of dynamically relocatable object files Fully linked executable p (in memory) Loader/Dynamic Linker (ld linux.so) p libc.so functions called by m.c and a.c are loaded, linked, and (potentially) shared among processes. CS429 Slideset 25: 22

The Complete Picture m.c a.c Translators (cc1, as) Translators (cc1, as) m.o a.o libwhatever.a Linker (ld) p libc.so libm.so Loader/Dynamic Linker (ld linux.so) p CS429 Slideset 25: 23