76
Loaders & Linkers

Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Embed Size (px)

Citation preview

Page 1: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Loaders & Linkers

Page 2: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Introduction

• A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates execution.

Page 3: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 4: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 5: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 6: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 7: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

object file• In computer science, object code, or an object file, is the

representation of code that a compiler or assembler generates by processing a source code file.

• Object files contain compact code, often called "binaries".

• A linker is typically used to generate an executable file by linking object files together. The only essential element in an object file is machine code

• Object files often also contain data for use by the code at runtime, relocation information, program symbols (names of variables and functions) for linking and/or debugging purposes, and other debugging information.

Page 8: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 9: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Basic Functions of the Loader• ALLOCATION: Allocates space in memory for the

programs( calculation program size)

• RELOCATION: Adjustment of addresses of all address sensitive entities .

• LINKING:Resolve inter-segment (inter-program) symbolic reference in object program.

• LOADING: Physically place the machine instructions and data into memory and initiate execution.

Page 10: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 11: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Linking• In general ,a program can contain :

– External definitions– Public definitions

• Linker is the system software which performs :– Resolution of inter-segment symbolic references which

is called as “Linking”.– Establish correspondence between external definitions

and public definitions– To determine relocatability of symbols attributes.– Substitute relocatable addresses.

Page 12: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Functions of a Linker• Establish communication between external

symbol references and definitions and public definitions

• To determine the relocatability of symbol attributes and expressions.

• To substitute for these relocatable addresses.

Page 13: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

In actual practice, a complete program is built from many smaller routines possibly by many people. All these routines have to be connected logically and linked to form a single program.

A linker is a systems program that accounts for and reconciles all address references within and among modules and replaces those references with a single consistent scheme of relative addresses.

Linking is done after the code is generated and is closely associated with a loader.

Page 14: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Compilers and translators basically translate one procedure at a time and put the translated output on the disk. All the translated procedures have to be located and linked together to be run as a unit called an executable binary program.

In MS-DOS, Windows 95/98 etc object modules have extension .obj and the executable binary programs have .exe extension. In UNIX, object modules have .o extension and executable programs have no extension

Page 15: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 16: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 17: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 18: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 19: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 20: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 21: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 22: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 23: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 24: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 25: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 26: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Loading schemes

1. Assemble-and-go loader: The assembler simply places the code into memory and the loader executes a single instruction that transfers control to the starting instruction of the assembled program.

2. In this scheme, some portion of the memory is used by the assembler itself which would otherwise have been available for the object program.

Page 27: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Compile and Go Loader

• Assembling/compiling, linking, loading etc. goes in one shot.

• programmer or assembler must know the starting address in memory to load the program.

• External references of any kind are not allowed in this loader scheme.

Page 28: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Compile and go Loader

• The assembler runs in one part of the memory and place the assembled machine instructions and data as they are assembled directly into their assigned memory locations.

• When assembly is completed, the assembler causes a transfer to the starting instruction of the program.

Assemble and Go Loader

Code for ExecutionIn mainmemory

Source Program .ASM

Page 29: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Algorithm

1. Read pgm. Line by line till EOF , if EOF goto step 4

2. Analyze statement, find M/c code and dump it to memory at pre known location

3. Increment memory location counter appropriately and goto step 1

4. Load PC with starting address and issue EXEC call with the help of OS.

Page 30: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Compile and go loader

Advantages:• It is very simple to understand.

• It is simple to implement because loader is just an extension to the assembler design.

Page 31: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Disadvantages:• Portion of the memory is wasted because the

translator resides in the memory.• It is necessary to assemble every time even

though no modifications are being done.• Can not handle multiple program segments

especially if they are written in different languages .

• Scheme is good for small program not for bigger one

• Execution time = assemble time + load time

Page 32: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Absolute Loader Here assembler produces absolute object code which can be stored in secondary storage for use in future.

•No relocation information is needed , which saves space.

•Locations are being absolute , either programmer or assembler must know the memory management issue.

•Linking of different modules which are inter dependent is done by programmer .

•In short:•Allocation and linking is by programmer•Resolution is by assembler•Loading is done by loader.

Page 33: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Absolute Loader

MAIN

SQRT

AbsoluteLoader

2000

2100

1000

1500

2100

2000

1500

1000

SQRT

MAIN

AbsoluteLoader

MainMemory

~~ ~~

Page 34: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Operation• The loader simply accepts the machine

language and places it into the core as prescribe by the assembler.

• The loader then initiates execution by transferring control to the starting of the program.

Page 35: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Segment 1

Segment 2

Segment N

Assembler Object 1

length

Start Address

AbsoluteLoader

Main MemoryObject 2

length

Start Address

Object N

length

Start Address

~ ~

General Schematic of Absolute Loader.

Page 36: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Algorithm of an Absolute Loader.• INPUT:Object modules consisting of machine code and data,starting address and

length.

• OUTPUT:Executable code to be placed in the main memory.

Begin for each program module

begin 1) read the header to get the information about starting address and length.

2)mem_loc=starting addrs.3)present_byte_counter=14)max_byte_counter=lengthwhile(present _byte_counter<max_byte_counter)

for current object code do begin

1)read present_byte_conter byte 2)put it at location mem_locn

3)increment mem_locn & increment present_byte_counter end;

end; end;

Page 37: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Absolute loader.Advantages:• More core is available to the user since the

translator is not in the memory.• No relocation info. Required so size of object

module will be small.• Absolute loader are simple to implement.• Loader is being very small, it eats up very small

region of memory• Execution time = load time• It is fast and efficient

Page 38: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Disadvantages:

•Programmer must specify the starting address of each module so programmer must know memory management as well as memory status at any time.

Page 39: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Relocation• Linker will generally performs this activity by

using relocation information , length , machine code and data from object file

• The .EXE file contains :– machine code and data w.r.t zero address– Relocation information– Length of file

Page 40: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Relocation

• In computer science, relocation is the process of replacing symbolic references or names of libraries with actual usable addresses in memory before running a program.

• It is typically done by the linker during compilation (at compile time), although it can be done at runtime by a loader

Page 41: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Relocation table

• The relocation table is a list of pointers created by the linker and linked into the MSDOS .exe file.

• The relocation table tells the loader where these segments can be found in program image loaded into memory.

Page 42: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Ways to put Relocation information

1. Put relocation information after every byte of machine code and data

2. Put the relocation information at the end of object code.

Page 43: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Opcode operand

Load A

Add B

Store C

Stop

Endp

A DB 5

B DB 3

C DW ?

Machine code

8 07

9 08

9 09

05

03

xx

Relocation bits

0 1

0 1

0 1

0

0

0

0

Way1 to put relocation information

Second way to put relocation information

8 07 01 08 09 09 12 05 03 00

0 1 0 1 0 1 0 0 0 0

Page 44: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Program Relocation

• Machine code w.r.t 20

08 27 01 28 09 29 12 05 03 xx xx

• Machine code w.r.t 40

08 47 01 48 09 49 12 05 03 xx xx

Page 45: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Program Relocatability

Relocatable program

Non-Relocatable Programs

Self Relocatable Programs

Page 46: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Non- Relocatable Programs

• This type of program can not be executed in any area of memory other than one distinguished for it at the time of its coding .

Page 47: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Relocatable Programs

• Using relocation information , it is possible to relocate the program to execute from the area other than one which is distinguished for it at the time of translation.

• The agent which performs such relocation is called as Linkage Editors or Relocating Loaders

Page 48: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Self- Relocatable Programs

• This is the program which can itself perform the relocation of its address sensitive portions of the code.

Page 49: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 50: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Relocating loader3. Relocating loader:

This loader allows multiple procedure segments and one data segment which is common to all assembler assembles each segment independently and passes on to loader the following info:

• M/C code• relocation information• length of program• inter segment reference

Page 51: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

• For each program, Assembler produces m/c code prefixed by “ Transfer vector “

• Transfer vector consists of names of subroutines referenced by source program

• Transfer vector is kind of table that contains info. About external subroutines and their addresses which may be filled by loader.

• Transfer vector is a global table for object file.

Page 52: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Entities and their usage in R L

• Relocation bits : to solve the problem of relocation.

• Transfer vector: to solve problem of linking.

• Program length : to solve the problem of allocation.

Page 53: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

RELOCATING LOADER• ADVANTAGES:• The relocating loader takes

care of the program modification dynamically ( i.e change in program size)

• Using the transfer vectors,the required sub-routines can be brought into the main memory rather than keeping all routines in the memory all the time .

DISADVANTAGES:• Transfer vector linkage is

useful only for subroutines but not for loading or storing external data.

• If Transfer vector itself increases, the size of object program in memory increases, as Transfer Vector has to be there in memory all the time

Page 54: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Linking loader vs. Linkage EditorObject

program(s)

Linking loader

Memory

Library

Objectprogram(s)

Linkage editorLibrary

Linkedprogram

Simple Loader

Memory

A linkage editor produces a linked version of the program (load module), which is written to a file or library for later execution.

Page 55: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Linkage editors

• Advantages:– One linking and the most of relocation for

many executions.– Possibility to create subroutines libraries.– Actual loading is very simple.

• Disadvantages:– recompiling for nearly every execution

(program development).- infrequently used programs (storage waste).

Page 56: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Direct linking loader:

• It belongs to the category of Re-locatable Loader.

• It is most widely used loader.

• It allows the programmer to use multiple procedure segments and multiple data segments.

• References to non local data and subroutine is allowed .

Page 57: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

• For Direct Linking Loader, assembler must provide:

• Length of each and every program segment

• PUBLIC TABLE which defines Name of the Symbol & address of it’s definition.

• EXTERN TABLE which defines Name of the symbol and address of its Usage.

Page 58: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Program 1prog 1:public xpublic y MACHINE EQUIVALENTExt AExt B after pass1 after pass2 | |load A 0000 08 -- 0000 08 ----

mult x 0002 03 -- 0002 03 0050add y 0004 01 -- 0004 01 0051 | |store B 0020 09 -- 0020 09 ---- |store A 0040 09 -- 0040 09 ----endpx db ? 0050 xx 0050 xxy db ? 0051 xx 0051 xx

Page 59: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Public table for program 1

Name of symbol Address of its definition

x 0050

Y 0051

Page 60: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Extern table for program 1

Name of the symbol Address of its usage

A 0001,0041

B 0021

Page 61: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Program 2:Public APublic BExt xExt y | After pass 1 After pass 2 load A 0000 08 -- 0000 08 0037 mult x 0002 03 -- 0002 03 --- add y 0004 01 -- 0004 01 --- | store B 0015 09 -- 0015 09 0038 |endp store x 0035 09 -- 0035 09 –A db ? 0037 xx -- 0037 xxB db? 0038 xx 0038 xxend

Page 62: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Public table for program 2

Name of symbol Address of its definition

A 0037

B 0038

Page 63: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Extern table of program 2

Name of the symbol Address of its usage

X 0003,0036

Y 0005

Page 64: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

• Assume that the object code of pgm 2 will be loaded at the end of pgm1.

• Hence all relative addresses in the segment are to be adjusted by length of first program.( which is 52)

Page 65: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Modified pub table for program 2

Name of symbol Address of its definition

A 0089

B 0090

Page 66: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Modified ext table for program 2

Name of symbol Address of its definition

x 55,88

y 57

Page 67: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

• Use public table of program 2 with extern table of program 1 and vice versa to resolve everything

Page 68: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

DLL : Algorithm( Pass 1)Purpose :To build global extern symbol

table( GST) 1. Initialize ST, STPTR, LC2. Get name of procedure from user and store it

into ST.3. Search for obj file, if not found, report “error”

and stop.4. Input record and determine its type.

Case 1: procedure name 1. save module size2. mark proc as found in GST.3. STPTR++4. goto step 4

Page 69: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Case 2 : Public definition table1. enter global data area name into ST and mark as found.

2. add offset to LC of each global variable.3. STPTR++4. Goto step 4

Case 3 : Extern table1.if extern symbols are not in ST enter them and mark them as not found

2. GOTO step 4.

Case 4: End of module1. Add the size of module to LC2. if all entries in ST are marked ”FOUND” goto pass 23. else set STP to next entry with no address and goto step 3

Page 70: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

DLL : Algorithm( Pass 2)

Purpose :To generate load moduleStep 1 : initialize STPTR and LCStep 2: fetch object module pointed to in ST from

file to memoryStep 3: update all external reference using using

public table and extern table.Step 4 : move all code and data records to load

module and update STPTR and LCStep 5: if no more ST entry then stop else GOTO

step 2.

Page 71: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Dynamic Linking

Dynamic linking (dynamic loading, load on call) postpones the linking function until execution time; a subroutine is loaded and linked to the rest of the program when it first called.

Page 72: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Dynamic Linking

• Advantages:– Several executing programs can share one copy of a subroutine

or library (Xwindows, C support routines).– The implementation of the method can be determined or even

changed at the time the program is run.– The subroutine name might simply be treated as an input item (!)

• Additional advantages:– Applications with rarely used subroutines (f.e. errors handling)– Application with a lot of possible services (f.e. mathematic

packages)

Page 73: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Dynamic loader (OS)

User program

ERRHANDL

Library

Dynamic loader (OS)

User program

ERRHANDL

Dynamic loader (OS)

User program

ERRHANDL

Dynamic loader (OS)

User program

ERRHANDL

Load-and-callERRHANDL

Dynamic loader (OS)

User program

Load-and-callERRHANDL

Page 74: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates

Bootstrap Loaders

This absolute loader (bootstrap loader) is a system software , resident in ROM or “primary loaded” by some hardware function.

Page 75: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates
Page 76: Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates