Assembly Language Programming Linkers

Similar documents
A Simplistic Program Translation Scheme

COS 318: Operating Systems

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

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

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

CS 550 Operating Systems Spring Process I

Executables and Linking. CS449 Spring 2016

Computer Systems Organization

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.

Compiler Drivers = GCC

LINKING. Jo, Heeseung

Executables and Linking. CS449 Fall 2017

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

CS 201 Linking Gerson Robboy Portland State University

ECE 498 Linux Assembly Language Lecture 1

(Extract from the slides by Terrance E. Boult

Outline. Unresolved references

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

ECE 598 Advanced Operating Systems Lecture 10

ECE 471 Embedded Systems Lecture 4

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

Compiler, Assembler, and Linker

COS 318: Operating Systems. Overview. Prof. Margaret Martonosi Computer Science Department Princeton University

Exercise Session 7 Computer Architecture and Systems Programming

Computer Organization: A Programmer's Perspective

CS2141 Software Development using C/C++ Libraries

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

CS429: Computer Organization and Architecture

Linking Oct. 15, 2002

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

Dynamic libraries explained

Process Environment. Pradipta De

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

12: Memory Management

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

Machine Language, Assemblers and Linkers"

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

M2 Instruction Set Architecture

ALT-Assembly Language Tutorial

Embedded Systems. 2. Software Development. Lothar Thiele. Computer Engineering and Networks Laboratory

14.7 Dynamic Linking. Building a Runnable Program

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

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

9/19/18. COS 318: Operating Systems. Overview. Important Times. Hardware of A Typical Computer. Today CPU. I/O bus. Network

ECE 598 Advanced Operating Systems Lecture 11

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

Linking Oct. 26, 2009"

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

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

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

ECE 471 Embedded Systems Lecture 5

Incremental Linking with Gold

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

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

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

Performance of Various Levels of Storage. Movement between levels of storage hierarchy can be explicit or implicit

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

COS 318: Operating Systems. Overview. Jaswinder Pal Singh Computer Science Department Princeton University

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

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

Assembly basics CS 2XA3. Term I, 2017/18

Lecture 2 Assembly Language

Compilation, Disassembly, and Profiling (in Linux)

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

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

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

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

Embedded Systems Programming

The assembler has the pseudo op-codes listed in Table 8-1. Pseudo Op-Codes. .2byte expression1 [, expression2 ]...

CS2304-SYSTEM SOFTWARE 2 MARK QUESTION & ANSWERS. UNIT I INTRODUCTION

The Software Stack: From Assembly Language to Machine Code

Compile: compiler. Load: loader. compiler linker loader memory. source object load code module module 2

A tale of ELFs and DWARFs

C03c: Linkers and Loaders

Gechstudentszone.wordpress.com

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

Lecture 4 Processes. Dynamic Analysis. GDB

8086 ALP TOOLS (CH 2) CHAPTER 2

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

EE458 - Embedded Systems Lecture 4 Embedded Devel.

Administrivia. Deadlock Prevention Techniques. Handling Deadlock. Deadlock Avoidance

Memory Management II

Control Flow Integrity for COTS Binaries Report

IRIX is moving in the n32 direction, and n32 is now the default, but the toolchain still supports o32. When we started supporting native mode o32 was

Lecture 2 - Fundamental Concepts

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

Memory and C/C++ modules

Memory Management. CSCI 315 Operating Systems Design Department of Computer Science

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

UNIT III LOADERS AND LINKERS

Today s Big Adventure

Today s Big Adventure

Compila(on, Disassembly, and Profiling

Module 3 Instruction Set Architecture (ISA)

Chapter 2: System Structures

UNIT III LOADERS AND LINKERS PART A

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

AVR COFF (COMMON OBJECT FILE FORMAT) SPECIFICATION Preliminary release. Revision History

A Real Object File Format

Transcription:

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 addresses. Labor division: linker: allocation of memory = preparation of relative addresses in program loader: final relocation

Architectural solutions The hardware relocation (relocation registers) and virtual memory have simplified the job of a linker. Each program get the whole (virtual) address space. But there appeared a new element: code sharing, which forced the physical division of programs into code and data sections.

Linker tasks Consolidate binary modules Combine relocatable binary modules into a single executable file, which will be loaded by the loader. Solve external references They must be solved during consolidation process. External reference: reference to a symbol in another module. Relocate symbols Connect symbolic names with relative addresses described in the contexts of modules (.o files) with final absolute addresses in executable code. Example: getline in iosys module address 612 bytes from the beginning of the executable code. Perform code fixups: update all references to this symbols to make them correspond to their new addresses.

Relocation Assembler generated addresses are not relocated. For example assume that in mov eax,[a] mov [b],eax a has local address (offset) 0x1234, and b is imported. Code after assembly A1 34 12 00 00 A3 00 00 00 00 mov eax,[a] mov [b],eax During linkage process the linker decides, that the section containing a is to be relocated 0x10000 bytes, and b has address 0x9A12 A1 34 12 01 00 A3 12 9A 00 00 mov eax,[a] mov [b],eax

Relocation Similar modifications are necessary for data section, if it contains pointers, e.g. in the table of procedure addresses. RISC generate more problems, because an address is often build by two or three consequent instructions.

Formats of binary files In Unix binary files (and other files too) start with 32-bit long magic number, which determines the type of a file. Traditional, but now rarely used, format of binary file in Unix is a.out. Its magic number is 0x407. It has been mostly replaced by the ELF format.

Format a.out Header a.out Section text Section data Other sections Optional relocation information

Format ELF ELF header Program header table (dla loadera).text section.data section.bss section.symtab.rel.text.rel.data.debug Section header table (relocation info for linker)

ELF format (Executable and Linking Format) Now basic format in Unix with magic number 0x177 = ELF It is used for storing executable programs, as well as object modules and libraries (and also memory dumps). So has different type: relocatable, executable, shared, core image. So it is possible to place there informations necessary for liker and for loader too. File starts with general header, then there is program segment table (description of segments for loader).

ELF format This is followed by proper file contents, that is sections: code.text initialized data.data non-initialized data.bss.symtab: symbol table.rel.text: relocation info for.text.rel.data: relocation info for.data.debug: debugger info (if gcc -g was used) The file concludes with a section header table, which describes all sections for linker use.

Libraries Libraries divide into static and shared (aka. dynamic). With static libraries you have to relink program after each modification of program or libraries.

Shared libraries Shared libraries divide into loaded statically or dynamically. for libraries loaded statically the addresses (placement) are decided during program load (at load-time, if you prefer); libraries loaded dynamically can be loaded at run-time as needed, at the first call to their procedures (such procedures are sometimes called autoloaded). Shared libraries should contain Position Independent Code (PIC), to make it possible to load them into any area of memory (e.g. gcc compiler has an option for that).

Dynamic linkage: methods Implicit linking With a program we associate a linkage segment, describing external procedures called from dynamic libraries as pairs [name, address (initially equal 0, i.e. incorrect)]. The code contains indirect calls through this address, except for the first time, bacause initially it is a trap to dynamic linker, resulting in incremental linking of necessary library and filling the address field. Explicit linking Program in its prologue specifies all used shared libraries and links with them.

Creating a dynamic library To build a dynamic library, we use assembler as usual nasm -f elf64 pakiet.asm However when declaring the exported symbols we should specify their ich type: function or data, for example global random:function,seed:data Linkage will be different ld -shared -o libpakiet.so pakiet.o

Creating a dynamic library The library built that way can be used similarly to system libraries ld -L. -dynamic-linker /lib/ld-linux.so.2 \ -o program program.o -l pakiet Before running a program we must appropriately set the environment variable, for example export LD_LIBRARY_PATH=. Utility ldd called with binary program name will tell us, which shared libraries are used, and which have not been found. Utility nm gives all external symbols of any binary module.