85
Chapter - 3 Loaders & Linkers

Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Embed Size (px)

Citation preview

Page 1: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Chapter - 3

Loaders & Linkers

Page 2: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Overview of Chapter 3• Basic Loader Functions• Design Issues of: An Absolute Loader Simple Bootstrap Loader• Machine-Dependent loader Features Relocation Program linking Algorithm & Data Structures for a Linking Loader• Machine-Independent loader Features Automatic Library Search Loader Options

Page 3: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

• Loader Design options

– Linkage Editors

– Dynamic Linking

– Bootstrap Loaders

– Implementation Examples

Overview of Chapter 3 (contd…)

Page 4: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Basic loader functions:

The loader brings an object program into memory and starts its executions.

Page 5: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

There are 2 type of loader:-

1) Absolute loader.

2) A simple bootstrap loader.

Types of loaders:

Page 6: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Absolute loader need not perform the functions like:-

Program Linking and

Program RelocationAll the functions are performed in a single pass Header record is checked for program correctness Text records gets the object code is moved into the

memory as per the indicated address

Design of an absolute loader.

Page 7: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Consider the COPY program:Line Loc Source statement Object code5 1000 COPY START 1000 ---10 1000 STL RETADR 14103315 1003 CLOOP JSUB RDREC 48203920 1006 LDA LENGTH 00103625 1009 COMP ZERO 28103030 100C JEQ ENDFIL 30101535 100F JSUB WRREC 48206140 1012 J CLOOP 3C1003 45 1015 ENDFIL LDA EOF 00102A50 1018 STA BUFFER 0C1039 55 101B LDA THREE 00102D 60 101E STA LENGTH 0C103665 1021 JSUB WRREC 482061 70 1024 LDL RETADR 081033

Page 8: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Line Loc Sourse Statement Object Code75 1027 RSUB 4C000080 102A EOF BYTE C’EOF’ 454F4685 102D THREE WORD 3 00000390 1030 ZERO WORD 0 000000 95 1033 RETADR RESW 1 100 1036 LENGTH RESW 1105 1039 BUFFER RESB 4096110 . 115 . subroutine to read record in to buffer120 .125 2039 RDREC LDX ZERO 041030130 203C LDA ZERO 001030

Page 9: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Line Loc Source Statement Object Code 135 203F RLOOP TD INPUT E0205D140 2042 JEQ RLOOP 30203F 145 2045 RD INPUT D8205D150 2048 COMP ZERO 281030155 204B JEQ EXIT 302057160 204E STCH BUFFER ,X 549039165 2051 TIX MAXLEN 2C205E170 2054 JLT RLOOP 38203F175 2057 EXIT STX LENGTH 101036180 205A R SUB 4C0000185 205D INPUT BYTE X ‘F1’ F1190 205E MAXLEN WORD 4096 001000

Page 10: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Line Loc Source Statement Object Code 195 .200 . Subroutine to write record from buffer205 .

210 2061 WRREC LDX ZERO 041030215 2064 WLOOP TD OUTPUT E02079 220 2067 JEQ WLOOP 302064225 206A LDCH BUFFER X 509039230 206D WD OUTPUT DC2079235 207D TIX LENGTH 2C1026240 2073 JLT WLOOP 382064245 2076 RSUB 4C0000250 2079 OUTPUT BYTE X ‘05’ 05 255 END FIRST

Page 11: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Object program H^COPY ^ OO107AT^001000^1E^141033^482039^001036^281030^

301015^482061^361003^00102A^OC1039^00102D

T^00101E^15^OC1036^482061^081033^4C000A 454F46^000003^000000

T^002039^1E^041030^001030^E0205D^30203F^

D8205D^281030^302057^549039^2C205E^38203F

T^002057^1C^101036^4C0000^F1^001000^041030^

E02079^302064^509039^DC2079^2C1036

T^002073^07^382064^4C0000^05E^001000

Page 12: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Memory

Address Contents

0000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx

0010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx

………………………….

1000 14103348 20390010 36281030 30101548

1010 20613C10 0300102A OC103900 102D0C10

1020 36482061 0810334C 0000454F 46000003

1030 000000xx xxxxxxxx xxxxxxxx xxxxxxxx

……………………………

2030 xxxxxxxx xxxxxxxx XX041030 001030E0

2040 392C205E 45498989 3456D0C 045B0900

2050 9876B432 87654321 056430C1 09876455

2060 09890123 0210C012 0560C123 0C876543

2070 2C103638 20644C00 0005 xxxx xxxxxxxx

Absolute Program loaded in memory

0 1 2 3

COPY

Page 13: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Algorithm for an absolute loaderbegin

read Header recordverify program name and lengthread first Text record while record type is ≠ ‘E’ do

begin { if object code is in character form, convert into

internal representation } move object code to specified location in memory

read next object program record end

jump to address specified in End recordend

Page 14: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

A simple bootstrap loader

¢ Bootstrap Loader (usually resides in ROM)

¢ When a computer is first tuned on or

restarted, a special type of absolute loader,

called the bootstrap loader loads

the first program (usually O.S.)

to be run into memory.

Page 15: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

O.S

“F1” deviceBootstrap loader

O.S

0

80

Example (SIC bootstrap loader)Example (SIC bootstrap loader)The bootstrap itself begins at address 0The bootstrap itself begins at address 0It loads the OS starting address 0x80It loads the OS starting address 0x80

After loading the OS, After loading the OS,

No header record or control information,No header record or control information,The object code is consecutive bytes of The object code is consecutive bytes of memorymemory

the control is transferred to the the control is transferred to the instruction at address 80.instruction at address 80.

Page 16: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Bootstrap loader for SIC/XE START @ LOCATION 0

CLEAR REGISTER ‘A’ TO ZERO

INITIALIZE REGISTER X TO HEX 80

READ HEX DIGIT FROM PROGRAM

SAVE IN REGISTER S MOVE TO HIGH-ORDER 4 BITS OF BYTE

GET NEXT HEX DIGIT

COMBINE DIGITS TO FROM ONE BYTE

STORE AT ADDRESS IN REGISTER X ADD 1 TO MEMORY ADDRESS BEING LOADED

UNTIL END OF INPUT IS REACHED

Page 17: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

SUBROUTINE GETCBegin TEST INPUT DEVICE

LOOP UNTIL READY READ CHARCTER

IF CHARACTER IS HEX 04(eof) JUMP TO START OF PROGRAM JUST LOADED COMPARE TO HEX 30 (CHARACTER ‘0’) SKIP CHARACTERS LESS THAN ‘0’

SUBTRAC HEX 30 FROM ASCII CODE IF RESULT IS LESS THAN 10,

CONVERSION IS COMPLETE, RETURN

else SUBTRACT #7 (FOR HEX DIGITS ‘A’ THROUGH ‘F’ ) RETURN TO CALLER

END

Page 18: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Bootstrap loader for SIC/XEBOOT START #0 BOOTSTRAP LOADER FOR SIC/XE

CLEAR A CLEAR REGISTER ‘A’ TO ZERO

LDX #128 INITIALIZE REGISTER X TO HEX 80

LOOP JSUB GETC READ HEX DIGIT FROM PROGRAM BEING LOADED

RMO A , S SAVE IN REGISTER S SHIFTL S , 4 MOVE TO HIGH-ORDER 4 BITS OF BYTE

JSUB GETC GET NEXT HEX DIGIT

ADDR S , A COMBINE DIGITS TO FROM ONE BYTE

STCH 0 , X STORE AT ADDRESS IN REGISTER X TIXR X , 1 ADD 1 TO MEMORY ADDRESS BEING LOADED

J LOOP UNTIL END OF INPUT IS REACHED

Page 19: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

SUBROUTINE GETCGETC TD INPUT TEST INPUT DEVICE

JEQ GETC LOOP UNTIL READY RD INPUT READ CHARCTER

COMP #4 IF CHARACTER IS HEX 04(eof) JEQ 80 JUMP TO START OF PROGRAM JUST LOADED COMP #48 COMPARE TO HEX 30 (CHARACTER ‘0’) JLT GETC SKIP CHARACTERS LESS THAN ‘0’ SUB #48 SUBTRAC HEX 30 FROM ASCII CODE COMP #10 IF RESULT IS LESS THAN 10, CONVERSION IS COMPLETE, JLT RETURN

OTHERWISE, SUBTRACT #7 MORE SUB #7 (FOR HEX DIGITS ‘A’ THROUGH ‘F’ )RETURN RSUB RETURN TO CALLERINPUT BYTE X’F1’ CODE FOR INPUT DEVICE

END LOOP

Page 20: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Machine - Dependent Loader features

There are three feature:-

1) Relocation

2) Program Linking

3) Algorithm and Data Structures for a Linking Loader.

Page 21: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

1) Relocation

The loaders that allow for program relocation are called relocating loaders or relative loaders

Definition:-Definition:-

Page 22: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

a) Modification Records

Extended Instruction formats

Page 23: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Example of a SIC/XE programLine Loc Source statement Object code5 0000 COPY START 0 ---10 0000 FIRST STL RETADR 17202D12 0003 LDB #LENGTH 69202D13 BASE LENGTH 15 0006 CLOOP +JSUB RDREC 4B10103620 000A LDA LENGTH 03202625 000D COMP #0 29000030 0010 JEQ ENDFIL 33200735 0013 +JSUB WRREC 4B10105D40 0017 J CLOOP 3F2FEC 45 001A ENDFIL LDA EOF 03201050 001D STA BUFFER 0F2016 55 0020 LDA #3 010003 60 0023 STA LENGTH 0F200D

Page 24: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Line Loc Sourse Statement Object Code65 0026 +JSUB WRREC 4B1O1O5D 70 002A J @RETADR 3E200380 002D EOF BYTE C’EOF’ 454F4695 0030 RETADR RESW 1 100 0033 LENGTH RESW 1105 0036 BUFFER RESB 4096110 . 115 . subroutine to read record in to buffer120 .125 1036 RDREC CLEAR X B410130 1038 CLEAR A B400132 103A CLEAR S B440133 103C +LDT #4096 75101000

Page 25: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Line Loc Source Statement Object Code 135 1040 RLOOP TD INPUT E32019140 1043 JEQ RLOOP 332FFA145 1046 RD INPUT DB2013150 1049 COMPR A,S A004155 104B JEQ EXIT 332008160 104E STCH BUFFER ,X 57C003165 1051 TIXR T B850170 1053 JLT RLOOP 3B2FEA175 1056 EXIT STX LENGTH 134000180 1059 RSUB 4F0000185 105C INPUT BYTE X ‘F1’ F1195 .200 . Subroutine to write record from buffer205 .210 105D WRREC CLEAR X B410

Page 26: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Line Loc Source Statement Object Code 195 .200 . Subroutine to write record from buffer205 .

210 2061 WRREC LDT LENGTH 774000215 2064 WLOOP TD OUTPUT E32011 220 2067 JEQ WLOOP 332FFA225 206A LDCH BUFFER X 53C003230 206D WD OUTPUT DF2008235 207D TIXR T 2C1026240 2073 JLT WLOOP 3B2FEF245 2076 RSUB 4F0000250 2079 OUTPUT BYTE X ‘05’ 05 255 END FIRST

Page 27: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Object program with relocation by Modification recordsH^COPY ^ OO1077T^001000^1D^17202D^69202D^001036^281030^

301015^482061^361003^00102A^OC1039^00102DT^00101E^15^OC1036^482061^081033^4C000A 454F46^000003^000000T^002039^1E^041030^001030^E0205D^30203F^

D8205D^281030^302057^549039^2C205E^38203FT^002057^1C^101036^4C0000^F1^001000^041030^

E02079^302064^509039^DC2079^2C1036T^002073^07^382064^4C0000^05M^000007^05 + COPYM^000014^05 + COPYM^000027^05 + COPYE^001000

Page 28: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

b) Relocation bits

Relocation bits grouped into “bit mask”

12-bit fixed format But the text record size is 10 to 69 = 60/2( 2columns / byte) =30 bytes => 10 words

Page 29: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Consider the COPY program:Line Loc Source statement Object code RB5 1000 COPY START 1000 ---10 1000 STL RETADR 141033 115 1003 CLOOP JSUB RDREC 482039 120 1006 LDA LENGTH 001036 125 1009 COMP ZERO 281030 130 100C JEQ ENDFIL 301015 135 100F JSUB WRREC 482061 140 1012 J CLOOP 3C1003 145 1015 ENDFIL LDA EOF 00102A 150 1018 STA BUFFER 0C1039 155 101B LDA THREE 00102D 1 60 101E STA LENGTH 0C1036 165 1021 JSUB WRREC 482061 1 70 1024 LDL RETADR 081033 1 1

Page 30: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Line Loc Sourse Statement Object Code RB

75 1027 RSUB 4C0000 080 102A EOF BYTE C’EOF’ 454F46 085 102D THREE WORD 3 000003 090 1030 ZERO WORD 0 000000 095 1033 RETADR RESW 1 0100 1036 LENGTH RESW 1 0 105 1039 BUFFER RESB 4096 0110 . 115 . subroutine to read record in to buffer120 .125 2039 RDREC LDX ZERO 041030130 203C LDA ZERO 001030

Page 31: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Line Loc Source Statement Object Code 135 203F RLOOP TD INPUT E0205D140 2042 JEQ RLOOP 30203F 145 2045 RD INPUT D8205D150 2048 COMP ZERO 281030155 204B JEQ EXIT 302057160 204E STCH BUFFER ,X 549039165 2051 TIX MAXLEN 2C205E170 2054 JLT RLOOP 38203F175 2057 EXIT STX LENGTH 101036180 205A R SUB 4C0000185 205D INPUT BYTE X ‘F1’ F1190 205E MAXLEN WORD 4096 001000

Page 32: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Line Loc Source Statement Object Code 195 .200 . Subroutine to write record from buffer205 .

210 2061 WRREC LDX ZERO 041030215 2064 WLOOP TD OUTPUT E02079 220 2067 JEQ WLOOP 302064225 206A LDCH BUFFER X 509039230 206D WD OUTPUT DC2079235 207D TIX LENGTH 2C1026240 2073 JLT WLOOP 382064245 2076 RSUB 4C0000250 2079 OUTPUT BYTE X ‘05’ 05 255 END FIRST

Page 33: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Object program with relocation by bit maskH^COPY ^ OO107A

T^001000^1E^FFC^141033^482039^001036^281030^

301015^482061^361003^00102A^OC1039^00102D

T^00101E^15^E00^OC1036^482061^081033^4C000A

454F46^000003^000000

T^002039^1E^FFC^041030^001030^E0205D^30203F^

D8205D^281030^302057^549039^2C205E^38203F

T^002057^1A^800^101036^4C0000^F1^001000^041030^

E02079^302064^509039^DC2079^2C1036T^002073^07^382064^4C0000^05E^001000

Page 34: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Program linking

Goal

Resolve the problems with EXTREF and EXTDEF from different control sections (sec 2.3.5)

A program is a logical entity that combines all of the

related control sections.

Control sections could be assembled together, or they could be assembled independently of one another. Control sections are to be linked, relocated, and loaded by loaders.

Page 35: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Program linking

Complex Example

Program in Figure 3.8 and object code in Figure 3.9 Use modification records for both relocation and linking Consider :

address constant

external reference

Page 36: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Program for Linking and Relocation

0000 PROGA +START +0 +EXTDEF +LISTA,ENDA +EXTREF +LISTB,ENDB,LISTC,ENDC +. +.

0020 REF1 +LDA +LISTA 03201D 0023 REF2 +LDT +LISTB+4 77100004 0027 REF3 +LDX #ENDA-LISTA 050014

+. +.

0040 LISTA +EQU +* +. +.

0054 ENDA +EQU +* 0054 REF4 +WORD 000014 +ENDA-LISTA+LISTC 0057 REF5 +WORD FFFFF6 +ENDC-LISTC-10 005A REF6 +WORD 00003F +ENDC-LISTC+LISTA-1 005D REF7 +WORD 000014 +ENDA-LISTA-(ENDB-LISTB) 0060 REF8 +WORD FFFFC0 +LISTB-LISTA

+END +REF1

Inst

ruct

ion

Op

eran

ds

{

{Data Operands

Page 37: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Program for Linking and Relocation

0000 PROGB +START +0 +EXTDEF +LISTB,ENDB +EXTREF +LISTA,ENDA,LISTC,ENDC +. +.

0036 REF1 +LDA +LISTA 03100000 003A REF2 +LDT +LISTB+4 772027 003D REF3 +LDX #ENDA-LISTA 05100000

+. +.

0060 LISTB +EQU +* +. +.

0070 ENDB +EQU +* 0070 REF4 +WORD 000000 +ENDA-LISTA+LISTC 0073 REF5 +WORD FFFFF6 +ENDC-LISTC-10 0076 REF6 +WORD FFFFFF +ENDC-LISTC+LISTA-1 0079 REF7 +WORD FFFFF0 +ENDA-LISTA-(ENDB-LISTB) 007C REF8 +WORD 000060 +LISTB-LISTA

+END +

Page 38: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Program for Linking and Relocation

0000 PROGC +START +0 +EXTDEF +LISTC,ENDC +EXTREF +LISTA,ENDA,LISTB,ENDB +. +.

0018 REF1 +LDA +LISTA 03100000 001C REF2 +LDT +LISTB+4 77100004 0020 REF3 +LDX #ENDA-LISTA 05100000

+. +.

0030 LISTC +EQU +* +. +.

0042 ENDC +EQU +* 0042 REF4 +WORD +ENDA-LISTA+LISTC 000030 0045 REF5 +WORD +ENDC-LISTC-10 000008 0048 REF6 +WORD +ENDC-LISTC+LISTA-1 000011 004B REF7 +WORD +ENDA-LISTA-(ENDB-LISTB) 000000 004E REF8 +WORD +LISTB-LISTA 000000

+END +

Page 39: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent
Page 40: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Algorithm and data structure for a linking loader

considers the following conditions:

Most instructions use relative addressing;

no relocation is necessary Modification records are used in this type of machine

Because some external symbols

are processed before read

A linking loader usually makes two passes over its input.

Page 41: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Two passes linking loader

Two Passes Logic

Pass 1: Assign addresses to all external symbols Pass 2: Perform the actual loading, relocation, and linking

Object Program (Control Sections)

Pass 1 Pass 2 Memory

Load Map CSADDR ESTAB

Page 42: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Linking loader -- Pass 1 Assign address to all external symbols

Only processes Header Record and Define

Record Builds an external symbol table (ESTTAB)

in which control section symbol is defined

Program Load Address (PROGADDR)

The beginning address in memory

where the linked program is to be loaded (supplied by OS).

Page 43: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Control Section Address (CSADDR) The starting address assigned to the

control section currently being scanned by

the loader.

CSADDR is added to all relative addresses

within the control section.

Page 44: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Linking loader -- Pass 1 (Cond.)

Add symbol to ESTAB Control section name: (name, CSADDR) ESTAB Get control section name from H record If the first control section

CSADDR = PROGADDR When E record is encountered, read the next control section

CSADDR = CSADDR + CSLTH (known from H record)

EXTDEF: (name, CSADDR+value in the record) ® ESTAB Get EXTDEF from D record

Page 45: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Linking loader -- Pass 1 (Cond.)

Print the load map if necessary (optional)

Control Symbol Address Length section name

PROGA 4000 0063 LISTA 4040 ENDA 4054

PROGB 4063 007F LISTB 40C3 ENDB 40D3

PROGC 40E2 0051 LISTC 4112 ENDC 4124

Page 46: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Algorithm for Pass 1 of a linking loader

Page 47: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Pass 1:

begin

get PROGADDR from operating system set CSSADDR to PROGADDR {for first control section}

while not end of input do

begin

read next input record

{Header record for control section }

set CSLTH to control section length

search ESTAB for control section name

Page 48: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

if found then

set error flag {duplicate external symbol}

else

enter control section name into ESTAB

with value CSADDR

Page 49: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

While record type ≠’E’ do begin read next input record if record type = ‘D’ then for each symbol in the record do begin search ESTAB for symbol name if found then set error flag {duplicate external symbol} else enter symbol into ESTAB with value (CSADDR + indicated address) end {for}end { while ≠ ‘E’ }

Page 50: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

add CSLTH to CSADDR

{starting address for next control section}

end {while not EOF}

End {Pass 1}

Page 51: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Algorithm for Pass 2 of a linking loader

Page 52: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Pass 2: begin get CSSADDR from PROGADDR set EXEADDR to PROGADDR while not end of input do begin read next input record {Header record} set CSLTH to control section length While record type ≠’E’ do begin read next input record if record type = ‘T’ then

Page 53: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

begin

{

if object code is in character form, convert

into internal representation }

move object code from record to location

{ CSADDR + specified address }

end {if ‘T’}

else if record type = ‘M’ then

begin

search ESTAB for modifying symbol name

Page 54: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

if found then

add or subtract symbol value at location

{CSADDR + specified address}else

set error flag {undefined external symbol}

end { if ‘M’}

end {While ≠’E’}

Page 55: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

if an address is specified {in End record} then

set EXECADDR to (CSADDR +specified address)

Add CSLTH to CSADDR

End {while not EOF}

Jump to location given by EXECADDR {to start execution of loaded program}

End {Pass 2}

Page 56: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

MACHINE-INDEPENDENT LOADER FEATURES

• The loader features that are not directly related to the machine architecture and design.

Page 57: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Two machine-independent features of the loaders:-

1) Automatic Library search

2) Loader Options

Page 58: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Automatic Library Search• It allows a programmer to use standard

subroutines without explicitly including them in the program to be loaded.

• In most cases there is a standard system.

• Subroutines called by the program being loaded are automatically retrieved from a library as they are needed during linking.

Page 59: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Implementation of Search

• Linking loader must keep track of external symbols that are referred to, but not defined.

• Enter symbol from each Refer record into the external symbol table (ESTAB)• At the end of Pass 1, the symbol in table

that remain undefined represents unresolved

external references

Page 60: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

• The loader searches the library or libraries specified for routines that contain the definitions of these symbols

• Subroutines fetched from a library in this form may themselves in turn contain external references.

Resolving the external references:-

Page 61: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

• The process allows the programmer to override the standard subroutines in the library by supplying his or her own routines

• The libraries to be searched by the loader ordinarily contain assembled or compiled versions of subroutines (i.e., object programs)

• for ex:-

SQRT library function written by the programmer

Override the standard subroutines

Page 62: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Directory:-• In most cases, a special file structure is

used for the libraries.

• This structure contains a directory.

• This directory gives the name of each routine and a pointer to its address within the file.

• some of the O.S’s keep the directory (commonly used libraries ) permanently in the memory

Page 63: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Loader Options• Many loaders allow the user to specify options that

modify the standard processing of :- Relocating and Program linking • It is done with a “special command language”

(job control language) that is used to specify the option and is processed by the Operating system.

• Some times there is a separate input file to the loader that contains control statements. or

• Some times the control statements can be embedded into a control section.

Page 64: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

One typical loader option allows the selection of alternative sources of input.

The following are some of the loader option commands :

1) INCLUDE program-name (library-name)

2) DELETE csect - name

3) CHANGE name1 name2

4) LIBRARY MYLIB

5) NOCALL STDDEV, PLOT, CORREL

Page 65: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

1) INCLUDE program-name (library-name)

It directs the loader to read the designated object program from a library and treats it as if it were part of the primary loader input.

Example:-

INCLUDE READ(UTLIB)

INCLUDE WRITE(UTLIB)

Page 66: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

It instructs the loader to delete the named control sections from the set of programs being loaded.

Example:-DELETE RDREC, WRREC

2) DELETE csect-name

Page 67: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

It causes the “external symbol” name1 to be changed to name2 whenever it appears in the object program

Example:-CHANGE RDREC, READ

CHANGE WRREC WRITE

3) CHANGE name1 name2

Page 68: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

• The LIBRARY command allows the user to use special versions of the standard routine.

• Usually user-specified libraries are normally searched before the standard system libraries.

4) LIBRARY MYLIB

Page 69: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

• It instructs the loader to “retain external references as unresolved”.

• Unresolved references may lead to error but this option is more useful when programs are to be linked but not executed immediately.

• This avoids the overhead of loading and linking the unnecessary routines and saves the memory space.

5) NOCALL STDDEV, PLOT, CORREL

Page 70: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Variety of other Loader options:-• An ability to specify the location at which

execution is to begin.

• Ability to control whether or not the loader should attempt to execute the program if errors are detected during the load.

Page 71: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

LOADER DESIGN OPTIONS

Some of the loader design options:-

1) Linkage Editor

2) Dynamic Linking

3) Bootstrap Loaders

Page 72: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Library

Object programs

Linking loader

Memory

Fig: Linking loader

Linking Loader

(a) Object programs

(b) Linking Loader

(c) Memory

Perform all linking and relocation at load time

Page 73: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

1) Linkage editors

• Perform linking prior to load time, and writes linked program (executable image) into file instead of being immediately loading into memory.

• Found on most systems in addition to linking loaders

Page 74: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Library

Object programs

Linkage editor

Memory

Relocating loader

Linked programs

Fig: Linkage editor

Page 75: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

• Object program:- The source program is first compiled or

assembled, producing an object program.

• Linkage Editor:-It produces an linked version of the object

program which is written to a file or library for later execution.

When the user is ready to run the linked program, a simple relocating loader is used to load the program into memory.

Linkage Editors

Page 76: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

The linkage editor performs relocation of all control sections relative to the start of the program.

The loading is accomplished in one pass, with no ESTAB requirement, reduces overhead

If a program is to be executed many time without being reassembled, the linkage editor comes in need.

Resolving the external references and library searching are performed only one.

Page 77: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

• Linked program:It is generally in a form suitable for processing by a

relocating loaderAll external references are resolved and relocating is

indicated by some mechanism, such as Modification record or bit mask

Even though all linking has been performed, information concerning external references is often retained in the linked program

Retaining of external references allows subsequent re-linking of the program to replace control sections, modify external references, etc.

If external symbols are not retained then the linked program cannot be reprocessed by the linkage editor.

Page 78: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Difference between linking & linkage loader

Library

Object programs

Linking loader

Memory

Fig(a) : Linking loader

Library

Object programs

Linkage editor

Memory

Relocating loader

Linked programs

Fig(

b) :

Lin

kage

edi

tor

Page 79: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Suitable Work Environments

• In an environment where program is to be executed many times without being reassembled

• use of a linkage editor -> reduces overhead• resolution of external refs and library

searching are performed only once (Compare to a linking loader!)

• In a development and testing environment a linking loader is more efficient.

Page 80: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Other Functions of Linkage• Editors1 ) when a change is made in the source code of a

subroutine, linkage editor can replace this subroutine in the linked version of the program with out recompiling/reassembling all code.

2) It can be used to build packages of CSs that are generally used together

• Ex: FORTRAN has a large number of subroutines for formatted i/o with lots of cross-refs between them

It is desirable to keep them as separate CSs for reasons of program modularity and maintainability

Page 81: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

The following is a typical sequence of linkage editor commands used to

accomplish the above task.• INCLUDE PLANNER ( PROGLIB )

• DELETE PROJECT

( Delete from existing planner )

• INCLUDE PROJECT ( NEWLIB )

(include new version)

• REPLACE PLANNER ( PROGLIB )

Page 82: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

2) Dynamic linking• A subroutine is loaded and linked to the rest of the

program when it is first called. This type of function is usually called dynamic linking, dynamic loading or load on cell.

• Dynamic linking is often used to allow several executing programs to share one copy of a subroutine or library. It is more used for reference in object oriented system.

• The implementation of the object & its methods to be determined at the time the program is running

• The implementation can be changed at any time without affecting the program that makes use of the object.

• Dynamic linking provides the ability to load the routines only when they are needed which save memory space and time.

Page 83: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

The figure below illustrate a method in which routines requested by the user program are dynamically loaded via an operating system service request.

Dynamic loader

( part of the OS )

User program

Load-and–call

ERRHANDL

Dynamic loader

( part of the OS )

User program

Library

ERRHANDL

-----

(a) (b)

Page 84: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Dynamic loader

( part of the OS )

User program

Dynamic loader

( part of the OS )

User program

ERRHANDL

(c ) (d)

ERRHANDL

Page 85: Chapter - 3 Loaders & Linkers. Overview of Chapter 3 Basic Loader Functions Design Issues of: An Absolute Loader Simple Bootstrap Loader Machine-Dependent

Dynamic loader

( part of the OS )

User program

Load-and–call

ERRHANDL

ERRHANDL

(e )