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.

Similar documents
Exercise Session 7 Computer Architecture and Systems Programming

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

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

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

CS 550 Operating Systems Spring Process I

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

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Lecture 16: Linking Computer Architecture and Systems Programming ( )

Computer Systems Organization

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

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

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

CSCI341. Lecture 22, MIPS Programming: Directives, Linkers, Loaders, Memory

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

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

Linking February 24, 2005

Process Environment. Pradipta De

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

Executables and Linking. CS449 Spring 2016

Assembly Language Programming Linkers

Compiler Drivers = GCC

CS2141 Software Development using C/C++ Libraries

ELF (1A) Young Won Lim 10/22/14

LINKING. Jo, Heeseung

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

ECE260: Fundamentals of Computer Engineering

Outline. Unresolved references

From Source to Execution:

Lec 13: Linking and Memory. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

Link 2. Object Files

gpio timer uart printf malloc keyboard fb gl console shell

CS241 Computer Organization Spring Data Alignment

Link 4. Relocation. Young W. Lim Thr. Young W. Lim Link 4. Relocation Thr 1 / 26

Link 2. Object Files

COS 318: Operating Systems

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

198:231 Intro to Computer Organization. 198:231 Introduction to Computer Organization Lecture 14

Computer Systems. Linking. Han, Hwansoo

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

Week 5 Lecture 3. Compiler Errors

CSC258: Computer Organization. Functions and the Compiler Tool Chain

CS140 - Summer Handout #8

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

EE458 - Embedded Systems Lecture 4 Embedded Devel.

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

Executables and Linking. CS449 Fall 2017

ELF (1A) Young Won Lim 3/24/16

Classifying Information Stored in Memory! Memory Management in a Uniprogrammed System! Segments of a Process! Processing a User Program!

Today s Big Adventure

A Simplistic Program Translation Scheme

Lecture 10: Program Development versus Execution Environment

Today s Big Adventure

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

Incremental Linking with Gold

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

ECE 598 Advanced Operating Systems Lecture 10

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

(Extract from the slides by Terrance E. Boult

For Teacher's Use Only Q No Total Q No Q No

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

Dynamic Memory: Alignment and Fragmentation

CS240: Programming in C

CS1100 Introduction to Programming. Week 8: Modular Programming Sorting Integer Arrays

3. Memory Management

CS 201 Linking Gerson Robboy Portland State University

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

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

Compiler, Assembler, and Linker

Link 4. Relocation. Young W. Lim Sat. Young W. Lim Link 4. Relocation Sat 1 / 33

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

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

The Software Stack: From Assembly Language to Machine Code

Dynamic libraries explained

Revealing Internals of Linkers. Zhiqiang Lin

Machine Language, Assemblers and Linkers"

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

CPEG421/621 Tutorial

Memory and C/C++ modules

o Code, executable, and process o Main memory vs. virtual memory

Dynamic Memory Allocation

Topic 7: Activation Records

Memory Management: The process by which memory is shared, allocated, and released. Not applicable to cache memory.

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

Lecture 6: Assembly Programs

Link Edits and Relocatable Code

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

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

CSC 1600 Memory Layout for Unix Processes"

12: Memory Management

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

CS399 New Beginnings. Jonathan Walpole

CSL373: Operating Systems Linking

Link 8. Dynamic Linking

ECE 471 Embedded Systems Lecture 5

Programs in memory. The layout of memory is roughly:

CS 314 Principles of Programming Languages. Lecture 11

M2 Instruction Set Architecture

Memory Management. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

ecture 12 From Code to Program: CALL (Compiling, Assembling, Linking, and Loading) Friedland and Weaver Computer Science 61C Spring 2017

Memory Management Minsoo Ryu Real-Time Computing and Communications Lab. Hanyang University

Transcription:

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 is. Explain what an executable is and how it got that way With huge thanks to Steve Chong for his notes from CS61. 2/4/16 CS161 Spring 2016 1

What is Linking? The process of merging all the code and data from multiple object files into a single file that is ready to be loaded into memory and executed. Two forms of linking: Static: compile/link time executable contains all the code and data needed to execute. Dynamic: load time or run time parts of the code and/or data are integrated after link time. Think shared libraries. 2/4/16 CS161 Spring 2016 2

What does the Linker do? Copy code and data from each object file into the executable. Resolve references between object files Relocate symbols to use absolute addresses rather than relative addresses. 2/4/16 CS161 Spring 2016 3

Symbols Symbols are a convenience for the programmer, but the compiler, assembler, linker and loader have to deal with transforming symbols into something meaningful. Local variables: easy compiler translates those into references into the stack. Local functions: relatively straight forward place the functions somewhere and then translate references to the functions into addresses. Global variables: slightly more work When a module allocates space for them, the compiler has to allocate space for them and translate references to the appropriate address. When a module uses them, the compiler has to leave a note indicating that at link time, we will need to figure out where those global variables live and fix up references to them. External functions: similar to global variables Defining module: place the code and remember that you have the definition of the symbol corresponding to the function name. Using module: remember that you have to fill in calls to it later. 2/4/16 CS161 Spring 2016 4

Example Consider the two files: main.c and swap.c: main.c swap.c int buf[2] = 1,2; extern int buf[]; int main() swap(); return 0; void swap() int temp; temp = buf[1]; buf[1] = buf[0]; buf[0] = temp; 2/4/16 CS161 Spring 2016 5

Example: Local Variable Consider the two files: main.c and swap.c: main.c swap.c Local variable int buf[2] = 1,2; extern int buf[]; int main() swap(); return 0; void swap() int temp; temp = buf[1]; buf[1] = buf[0]; buf[0] = temp; 2/4/16 CS161 Spring 2016 6

Example: Define Global Variable Consider the two files: main.c and swap.c: main.c swap.c int buf[2] = 1,2; extern int buf[]; int main() swap(); return 0; Global variable declared void swap() int temp; temp = buf[1]; buf[1] = buf[0]; buf[0] = temp; 2/4/16 CS161 Spring 2016 7

Example: Use of Global Variable Consider the two files: main.c and swap.c: main.c swap.c int buf[2] = 1,2; extern int buf[]; int main() swap(); return 0; void swap() int temp; temp = buf[1]; buf[1] = buf[0]; buf[0] = temp; Global variable referenced 2/4/16 CS161 Spring 2016 8

Example: Define Function Consider the two files: main.c and swap.c: main.c swap.c int buf[2] = 1,2; extern int buf[]; int main() swap(); return 0; void swap() int temp; temp = buf[1]; buf[1] = buf[0]; buf[0] = temp; External function defined 2/4/16 CS161 Spring 2016 9

Example: Use External Function Consider the two files: main.c and swap.c: main.c swap.c int buf[2] = 1,2; extern int buf[]; int main() swap(); return 0; External function used void swap() int temp; temp = buf[1]; buf[1] = buf[0]; buf[0] = temp; 2/4/16 CS161 Spring 2016 10

main.o code for main().o files swap.o code for swap() buf[] symbol table name section off main.text 0 buf.data 0 swap undefined relocation info for.text name offset swap 12 symbol table name section off swap.text 0 buf undefined relocation info for.text name offset buf 0x5 buf 0xa buf 0xf buf 0x15 2/4/16 CS161 Spring 2016 11

main.o code for main() buf[] symbol table name section off main.text 0 buf.data 0 swap undefined relocation info for.text name offset swap 12.o -> executable myprog code for main() buf[] swap.o code for swap() symbol table name section off swap.text 0 buf undefined relocation info for.text name offset buf 0x5 buf 0xa buf 0xf buf 0x15 2/4/16 CS161 Spring 2016 12

main.o code for main() buf[] symbol table name section off main.text 0 buf.data 0 swap undefined relocation info for.text name offset swap 12.o -> executable myprog code for main() code for swap() buf[] Can now resolve references to buf swap.o code for swap() symbol table name section off swap.text 0 buf undefined relocation info for.text name offset buf 0x5 buf 0xa buf 0xf buf 0x15 2/4/16 CS161 Spring 2016 13

main.o code for main() buf[] symbol table name section off main.text 0 buf.data 0 swap undefined relocation info for.text name offset swap 12.o -> executable myprog code for main() code for swap() buf[] Can also resolve reference to swap swap.o code for swap() symbol table name section off swap.text 0 buf undefined relocation info for.text name offset buf 0x5 buf 0xa buf 0xf buf 0x15 2/4/16 CS161 Spring 2016 14

ELF: Executable and Linking Format A single format to represent object files, shared libraries (.so), and executables. Header: magic number, type, etc. Segment header: maps segments in file to (runtime) segments in memory. Text section: code Rodata: Read-only data Bss: Unitialized global variables Symtab: Symbol Table Rel.Text: Relocation for text Rel.Data: Relocation for data Debug: Debugging info (gcc g) Section header table: offsets and sizes of each section ELF header Segment header table (required for executables).rodata section.bss section.symtab section.rel.rel.debug section Section header table 0 2/4/16 CS161 Spring 2016 15

From ELF to Process ELF header Segment header table (required for executables) 0 OS memory User stack 0xc0000000 %esp.rodata section.bss section.symtab section.rel.rel.debug section Section header table Shared libraries Heap (used by malloc) Read/write segment.data,.bss Read-only segment.text,.rodata unused 2/4/16 CS161 Spring 2016 16 0x40000000 0x08048000 0x00000000

Populating the Address Space Until you have virtual memory, you will need to copy all the code and data from the executable into the address space. The heap and stack are not present in the executable, so what do you do? When you need pages in the heap or stack, you produce zero-filled pages. 2/4/16 CS161 Spring 2016 17