chapter 3 loaders and linkerswatis/courses/188231/sp3-2.pdf · chapter 3 loaders and...

30
Chapter 3 Loaders and Linkers -- Machine-Dependent Loader Feature

Upload: others

Post on 18-Oct-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

Chapter 3 Loaders and Linkers-- Machine-Dependent Loader Feature

Page 2: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� �

Motivation

� Shortcoming of an absolute loader� Programmer needs to specify the actual address at

which it will be loaded into memory.

� It is difficult to run several programs concurrently, sharing memory between them.

� It is difficult to use subroutine libraries.

� Solution: � A more complex loader that provides

� Program relocation

� Program linking

Page 3: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� �

Relocation

� Loaders that allow for program relocation are called relocating or relative loaders.

� Two methods for specifying relocation as part of the object program� Modification records

� For a small number of relocations required when relative or immediate addressing modes are extensively used

� Relocation bits� For a large number of relocations required when only direct addressing mode can be used in a machine with fixed instruction format (e.g., the standard SIC machine)

Page 4: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� �

Object program with modification record-- Figure 3.5, pp. 132

One modification record for each relocation

Modification recordCol 1: MCol 2-7: relocation address Col 8-9: length (halfbyte)Col 10: flag (+/-)Col 11-17: segment name

�� �� � � � � � � � � �� �� �� � �� � � � � � � � � �� �� ��� �� � � � � � � � � �� �� �� � �� � � � � � � � � �� �� �

� � � � � � �� � � � � � � �� �� �� � �� � � � � � � � � � � �� � � � � �� � � �� � � � � � � � � � �� � � � � � � �� �� �� � �� � � � � � � � � � � �� � � � � �� � � � � � � � � � � � � � �� � � � � � � �� �� �� � �� � � � � � � � � � � �� � � � � �� � � �� � � � � � � � � � �� � � � � � � �� �� �� � �� � � � � � � � � � � �� � � � � �� � � � � � � � �� ��� ��� ��� �

� � � � �� � � � � �� � � � � � � � � � � � � � � � �� � � � �� � � �� � � � �� � � � � �� � � � � � � � � � � � � � � � �� � � � �� � � �� � � � �� � � � � �� � � � � � � � � � � � � � � � �� � � � �� � � �� � � � �� � � � � �� � � � � � � � � � � � � � � � �� � � � �� � � �

� � �� �� � � � � � � � � � �� � � �� � � � � � � � � � � � �� � � � � � � � � � � �� � � � � � � �� � �� �� � � � � � � � � � �� � � �� � � � � � � � � � � � �� � � � � � � � � � � �� � � � � � � �� � �� �� � � � � � � � � � �� � � �� � � � � � � � � � � � �� � � � � � � � � � � �� � � � � � � �� � �� �� � � � � � � � � � �� � � �� � � � � � � � � � � � �� � � � � � � � � � � �� � � � � � � � � � �� � �� � �� � �

� � �� �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � �� �� � �� �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � �� �� � �� �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � �� �� � �� �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � �� � � � �� � �� � �� � �

� � �� �� � � � � � � � � � � � � � ��� � �� �� � � � � � � � � � � � � � ��� � �� �� � � � � � � � � � � � � � ��� � �� �� � � � � � � � � � � � � � ��

� � � � � � � �� �� � � �� � � � � � � �� �� � � �� � � � � � � �� �� � � �� � � � � � � �� �� � � �

� � � � �� �� �� � � �� � � � �� �� �� � � �� � � � �� �� �� � � �� � � � �� �� �� � � �

� � � � � � �� �� � � �� � � � � � �� �� � � �� � � � � � �� �� � � �� � � � � � �� �� � � �

� � � � � � �� � � � � � �� � � � � � �� � � � � � �

^ ^ ^^ ^ ^^ ^ ^ ^ ^ ^ ^ ^

^ ^ ^^ ^ ^ ^ ^ ^ ^ ^^ ^^ ^ ^^ ^ ^ ^ ^ ^ ^ ^^ ^

^ ^ ^^ ^ ^ ^ ^

^ ^ ^^ ^^ ^^ ^^ ^^

Page 5: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� �

Relocatable program for SIC-- Figure 3.6, pp. 133

Fixed instruction formatDirect addressing mode

1400334810390000362800303000154810613C000300002A0C003900002D0C00364810610800334C0000454F46000003000000

0RETADRRDRECLENGTHZEROENDFILWRRECCLOOPEOFBUFFERTHREELENGTHWRRECRETADR

C’EOF’30114096

STARTSTLJSUBLDACOMPJEQJSUBJLDASTALDASTAJSUBLDLRSUBBYTEWORDWORDRESWRESWRESB

COPYFIRSTCLOOP

ENDFIL

EOFTHREEZERORETADRLENGTHBUFFER

00000000000300060009000C000F001200150018001B001E002100240027002A002D0030003300360039

Page 6: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� �

Relocatable program for SIC-- Figure 3.6, pp. 133

Fixed instruction formatDirect addressing mode

040030000030E0105D30103FD8105D2800303010575480392C105E38103F1000364C0000F1001000

ZEROZEROINPUTRLOOPINPUTZEROEXITBUFFER,XMAXLENRLOOPLENGTH

X’F1’4096

LDXLDATDJEQRDCOMPJEQSTCHTIXJLTSTXRSUBBYTEWORD

RDREC

RLOOP

EXIT

INPUTMAXLEN

1039103C103F104210451048104B104E105110541057105A105D105E

SUBROUTINE TO READ RECORD INTO BUFFER..

Page 7: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� �

Relocatable program for SIC-- Figure 3.6, pp. 133

Fixed instruction formatDirect addressing mode

The standard SIC machine does not use relative addressing (PC-relative, Base-relative)All instructions expect RSUB in Figure 3.6 need relocation � too many modification records

040030E01079301064508039DC10792C00363810644C000005

ZEROOUTPUTWLOOPBUFFER,XOUTPUTLENGTHLOOP

X’05’FIRST

LDXTDJEQLDCHWDTIXJLTRSUBBYTEEND

WRRECWLOOP

OUTPUT

106110641067106A106D1070107310761079

SUBROUTINE TO WRITE RECORD FROM BUFFER..

Page 8: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� �

Relocation Bits

� If there are many addresses needed to be modified, it is more efficient to use a relocation bit, instead of a Modification record, to specify every relocation.� When the instruction format is fixed

� There is a relocation bit for each word of the object program

� Relocation bits are put together into a bit mask

� If the relocation bit corresponding to a word of object code is set to 1, the program’s starting address will be added to this word when the program is relocated

Page 9: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� �

Relocation Bits-- Figure 3.7, pp. 134

� A bit value of 0 indicates that no modification is necessary or unused words

� A new Text record is created for proper alignment

�� �� � � � � � � � � �� �� ��� �� � � � � � � � � �� �� ��� �� � � � � � � � � �� �� ��� �� � � � � � � � � �� �� �

� � � � � � �� � � �� � � � � � � � � � � � � � � � � � � �� �� � � �� � � � �� � � � � � � � � � � � � �� � �� � � � � � �� � � �� � � � � � � � � � � � � � � � � � � �� �� � � �� � � � �� � � � � � � � � � � � � �� � �� � � � � � �� � � �� � � � � � � � � � � � � � � � � � � �� �� � � �� � � � �� � � � � � � � � � � � � �� � �� � � � � � �� � � �� � � � � � � � � � � � � � � � � � � �� �� � � �� � � � �� � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � � � �

� � � � �� � � � � � � �� � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � �� � � � �� � � � � � � �� � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � �� � � � �� � � � � � � �� � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � �� � � � �� � � � � � � �� � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � �

� � �� �� � � � � �� � � � � � � � � � � � � � � � � � �� �� � �� �� � � �� �� �� �� � � � � � � � � �� � �� �� � � � � �� � � � � � � � � � � � � � � �� � �� �� � �� �� � � �� �� �� �� � � � � � � � � �� � �� �� � � � � �� � � � � � � � � � � � � � � � � � �� �� � �� �� � � �� �� �� �� � � � � � � � � �� � �� �� � � � � �� � � � � � � � � � � � � � � �� � �� �� � �� �� � � �� �� �� �� � � � � � � � � � � � � � � � � �� � � � � � � �� � � � � � � �� � � � � � � �

� � �� �� � � � � � � � � � � � � � � � � � �� � � � � � �� � �� �� � � � � � � � � � � � � � � � � � �� � � � � � �� � �� �� � � � � � � � � � � � � � � � � � �� � � � � � �� � �� �� � � � � � � � � � � � � � � � � � �� � � � � � �

� � �� �� � � � � � � � � � � � � � � � � � � � � � � � �� �� � � � �� � � � �� � � � � �� � � � � � ��� � �� �� � � � � � � � � � � � � � � � � � � � � � � � �� �� � � � �� � � � �� � � � � �� � � � � � ��� � �� �� � � � � � � � � � � � � � � � � � � � � � � � �� �� � � � �� � � � �� � � � � �� � � � � � ��� � �� �� � � � � � � � � � � � � � � � � � � � � � � � �� �� � � � �� � � � �� � � � � �� � � � � � ��

� � � � � � �� � � � � � �� � � � � � �� � � � � � �

� � � � � � � � � �

� � � � � � �

� � � � � � � � � �

� � �

� � � � � � � �

Bit mask

����^ ^ ^^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^^ ^ ^ ^ ^ ^ ^ ^ ^ ^

^ ^ ^ ^ ^ ^ ^^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^^

Page 10: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

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.

� Example� Program in Figure 3.8 and object code in Figure 3.9� Use modification records for both relocation and linking

� address constant

� external reference

Page 11: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Program for Linking and Relocation-- Figure 3.8, pp.136

03201D77100004050014

000014FFFFF600003F000014FFFFC0

+0+LISTA,ENDA+LISTB,ENDB,LISTC,ENDC

+LISTA+LISTB+4#ENDA-LISTA

+*

+*+ENDA-LISTA+LISTC+ENDC-LISTC-10+ENDC-LISTC+LISTA-1+ENDA-LISTA-(ENDB-LISTB)+LISTB-LISTA+REF1

+START+EXTDEF+EXTREF+.+.+LDA+LDT+LDX+.+.+EQU+.+.+EQU+WORD+WORD+WORD+WORD+WORD+END

PROGA

REF1REF2REF3

LISTA

ENDAREF4REF5REF6REF7REF8

0000

002000230027

0040

005400540057005A005D0060

Page 12: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Object programs of Figure 3.8-- Figure 3.9, pp.137

� �� �� � � � � � � � � � � �� �

� � � � � � �� �� � � � � � �� �

� � � � � � � � � � � � �

��

� � � �� � � � � � � �� � � � � � � � � �� � �� �

��

� � � �� � �� � � � �� � � � � � � � � � � � � � � � � �� � � � � � � �

� � � � �� � �� � � � �

� � � � �� � �� � � � �

� � � � �� � �� �� � �

� � � � �� � �� � � � �

� � � � �� � �� �� � �

� � � � �� � �� � � � �

� � � � �� � �� � �� �� �

� � � � �� �� �� � �

� � � � �� �� � � � �

� � � � �� � �� � � � �

� � � � �� � �� � �� �� �

� � � � �� �

��

��

0023 REF2 +LDT LISTB+4 77100004

0054 REF4 WORD ENDA-LISTA+LISTC 0000140057 REF5 WORD ENDC-LISTC-10 FFFFE6005A REF6 WORD ENDC-LISTC+LISTA-1 00003F005D REF7 WORD ENDA-LISTA-(ENDB-LISTB) 0000140060 REF8 WORD LISTB-LISTA FFFFC0

Page 13: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Program for Linking and Relocation-- Figure 3.8, pp.136

0310000077202705100000

000000FFFFF6FFFFFFFFFFF0000060

+0+LISTB,ENDB+LISTA,ENDA,LISTC,ENDC

+LISTA+LISTB+4#ENDA-LISTA

+*

+*+ENDA-LISTA+LISTC+ENDC-LISTC-10+ENDC-LISTC+LISTA-1+ENDA-LISTA-(ENDB-LISTB)+LISTB-LISTA+

+START+EXTDEF+EXTREF+.+.+LDA+LDT+LDX+.+.+EQU+.+.+EQU+WORD+WORD+WORD+WORD+WORD+END

PROGB

REF1REF2REF3

LISTB

ENDBREF4REF5REF6REF7REF8

0000

0036003A003D

0060

00700070007300760079007C

Page 14: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Object programs of Figure 3.8-- Figure 3.9, pp.138

� �� �� � � � � � � � � � � �� �

� � � � � � �� �� � � � � � �� �

� � � � � � � � � � � � �

��

� � � �� � � � � � � � � � � �� � � � � � �� � � � � � �

��

� � � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �

� � � � �� � � � � � � �

� � � � �� � � � �� � �

� � � � �� � � � � � � �

� � � � �� � � � �� � �

� � � � �� � � � � � � �

� � � � �� � � � � � � �

� � � � �� � � � �� � �

� � � � �� � � � � � � �

� � � � �� � � � �� � �

� � � � �� � � � � � � �

� � � � �� � � � � � � �

� � � � �� � � � �� � �

� � � � �� � � � � � � �

� � � � �� � � � � �� �� �

� � � � �� � � � � � � �

0036 REF1 +LDA #LISTA 03100000003A REF2 +LDT #LISTB+4 772027003D REF3 +LDX #ENDA-LISTA 05100000

0070 REF4 WORD ENDA-LISTA+LISTC 0000000073 REF5 WORD ENDC-LISTC-10 FFFFF60076 REF6 WORD ENDC-LISTC+LISTA-1 FFFFFF0079 REF7 WORD ENDA-LISTA-(ENDB-LISTB) FFFFF0007C REF8 WORD LISTB-LISTA 000060

Page 15: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Program for Linking and Relocation-- Figure 3.8, pp.137

031000007710000405100000

000030000008000011000000000000

+0+LISTC,ENDC+LISTA,ENDA,LISTB,ENDB

+LISTA+LISTB+4#ENDA-LISTA

+*

+*+ENDA-LISTA+LISTC+ENDC-LISTC-10+ENDC-LISTC+LISTA-1+ENDA-LISTA-(ENDB-LISTB)+LISTB-LISTA+

+START+EXTDEF+EXTREF+.+.+LDA+LDT+LDX+.+.+EQU+.+.+EQU+WORD+WORD+WORD+WORD+WORD+END

PROGC

REF1REF2REF3

LISTC

ENDCREF4REF5REF6REF7REF8

0000

0018001C0020

0030

0042004200450048004B004E

Page 16: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Object programs of Figure 3.8-- Figure 3.9, pp.138

� �� �� � � � � � � � � � � �� �

� � � � � � � � �� � � � � � �� �

� � � � � � � � � � � � �

��

� � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � �

��

� � � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �

� � � � � � � � � � � � �

� � � � � � � � � � � �

� � � � � � � � � �� � �

� � � � � � � � � � � � �

� � � � �� � � � �� � �

� � � � �� � � � � � � �

� � � � �� � � � � �� �� �

� � � � �� � � � � � � �

� � � � �� � � � �� � �

� � � � �� � � � � � � �

� � � � �� � � � �� � �

� � � � �� � � � � � � �

� � � � �� � � � � � � �

� � � � �� � � � � � � �

0018 REF1 +LDA #LISTA 03100000001C REF2 +LDT #LISTB+4 771000040020 REF3 +LDX #ENDA-LISTA 05100000

0042 REF4 WORD ENDA-LISTA+LISTC 0000300045 REF5 WORD ENDC-LISTC-10 0000080048 REF6 WORD ENDC-LISTC+LISTA-1 000011004B REF7 WORD ENDA-LISTA-(ENDB-LISTB) 000000004E REF8 WORD LISTB-LISTA 000000

Page 17: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Programs after linking and loading-- Figure 3.10(a), pp.140

������� �� ������������� �� ������������� �� ���� �

� � � � � � � �

���

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

�� � � � � ��

� � � � � � � �

� � � � � � � �

� � � � � � � �

� �� � � � � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � � � �� � �

� � � � � � � �

� �� �� � � �

� � � � � � � �

� � � � � � � �

�� � � � � ��

� � � � � � � �

� � � � � � � �

���

� � � � � � � �

���

� � � � � � � �

� � � � � � � �

� � � � � � � �

� �� �� � � �

� � � � � � � �

� � � � � � � �

� � � � �� �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� �� � �� �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � �� � ��

� � � � � � � �

� � � �� � �

� � � � � � � �

� � � � � � � �

� � � � �� �

� � � � � � � �

� � � � � � � �

���

� � � � � � � �

���

� � � � � � � �

� � � � � � � �

� � � � � � � �

� �� �� � � �

� � � � � � � �

� � � � � � � �

� �� � �� � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � �� � � � �

� � � � � � � �

� � � � � � � �

� �� �� � � �

� � � � � � � �

� �� � �� � �

� � � � � � � �

� � � � � � � �

���

� � � � � � � �

���

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � �� � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� � � � � �

� � � � � � � �

� � � � � � � �

� � � � � � � �

� �� � � �� �

� � � � � � � �

� � � � � � � �

� � � � � �� �

� � � � � �

� � � � � � � �

� �� � � �� �

� � � � � � � �

� � � � � � � �

� � � � � �

� � � � � � � �

���

� � � ���� � � �

� � � �

� �� �

� � � �

� � �

� �� �

� � � �

� ��

� �� �

� � �

� �� �

� �� �

� �� �

� �� �

� � �

� �� �

� � � �

� � � �

� � � �

� � � �

� � �

� � � ����

ContentsMemoryaddress

� !� � !� � !�

PROGA

PROGB

PROGC

Page 18: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Relocation and linking operations performed on REF4

REF4=ENDA-LISTA+LISTC

LISTC is 40E2+0030=4112REF4=0014+4112=4126

HPROGC…….DLISTC000030.

.

.

……004126……

0000..

4050..

HPROGA…….T0000540F000014…….M00005406+LISTC.

PROGA

PROGC

Object programs Memory contents

Load addressesPROGA 004000PROGB 004063PROGC 0040E2

+

+ 4112(Actual addressof LISTC)

Page 19: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Algorithm and data structurefor a linking loader

� Section 3.2.3 considers the following conditions:� Most instructions use relative addressing; no relocation

is necessary

� Modification records are used in this type of machine

� A linking loader usually makes two passes over its input:� Because some external symbols are processed before

read

Page 20: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

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

CSADDR ESTAB Load Map

Page 21: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Linking loader-- Pass 1

� Assign address to all external symbols� Only processes Header Record and Define Record� Builds an external symbol table (ESTTAB)

� its name

� its address

� in which control section the symbol is defined

� Program Load Address (PROGADDR)� The beginning address in memory where the linked program is to be loaded (supplied by OS).

� 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 22: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

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 23: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Linking loader-- Pass 1 (Cond.)

� Print the load map if necessary (optional)

0063

007F

0051

400040404054406340C340D340E241124124

LISTAENDA

LISTBENDB

LISTCENDC

PROGA

PROGB

PROGC

LengthAddressSymbolname

Controlsection

Page 24: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

beginget PROGADDR from operating systemset CSADDR to PROGADDR {for first control section}while not end of input do

beginread next input record {Header record for control section}set CSLTH to control section lengthsearch ESTAB for control section name if found then

set error flag {duplicate external symbol}else

enter control section name into ESTAB with value CSADDRwhile record type != ‘E’ do

beginread next input recordif record type = ‘D’ then

for each symbol in the record dobegin

search ESTAB for symbol nameif found then

set error flag (duplicate external symbol)else

enter symbol into ESTAB with value(CSADR + indicated address)

end {for}end {while != ‘E’}

add CSLTH to CSADDR {starting address for next control section}end {while not EOF}

end {Pass 1}

Linking loader-- Pass 1 algorithm

(Only processes Header Record and Define Record)

Page 25: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Linking loader-- Pass 2

� Perform the actual loading, relocation, and linking� Only processes Text Record and Modification Record

� Get address of external symbol from ESTAB

� When read T record� Moving object code to the specified address

� When read M record� (+/-) EXTREF in M records are handled

� Last step: transfer control to the address in E� If more than one transfer address: use the last one

� If no transfer: transfer control to the first instruction (PROGADDR)

Page 26: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

beginset CSADDR to PROGADDRset EXECADDR to PROGADDRwhile not end of input do"����� #���$����%&���'�#�()� #���'�#*������+,)����'����-���'�����-����.

/.�-���'�#���%��01�‘ ’ #�"����� #���$����%&���'�#�2��'�#���%��1�‘,’ �.��"����

(�2��"3�'��'�#��������'. '���2��4�'��5������������� -��%����� ����*��5���"3�'��'�#��2����'�#����-�' �����(���66��7��%�'�2��#� ##���*

��#�(�2�‘,’*�-����2��'�#�1�‘8’ �.��"����

�� '.� �,���2����#�2��������"�-�� ���2�2�&�#��.�� ##����&"� '�����"�-�5 -&�� ��-�' �����(���66��7��%�'�2��#� ##���*

�-���������2- ��9&�#�2���#��$��� -����"�-:

��#�(�2�‘8’*��#�(/.�-��01�‘ ’*

�2� �� ##��������%�'�2��#�(��� �#��'�#*��.������ ; ��66�����9���66��7��%�'�2��#� ##���:

##���+,)�������66���#�(/.�-������ �!*

3&�%����-�' �������5���"�� ; ��66��(����� ���$�'&������2�-� #�#%�� �*��#�( ����*

Linking loader-- Pass 2 algorithm

(Only processes Text Record and Modification Record)

Page 27: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Linking loader-- Improve efficiency

� We can make the linking loader algorithm more efficient by� Assigning a reference number to each external symbol referred to

in a control section� 01: control section name

� 02~: external reference symbols

� Using this reference number (instead of the symbol name) in Modification records

� Avoiding multiple searches of ESTAB for the same symbol during the loading of a control section.

� Search of ESTAB for each external symbol can be performed once and the result is stored in a table indexed by the reference number.

� The values for code modification can then be obtained by simply indexing into the table.

Page 28: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Examples of Using Reference Numbers-- Figure 3.12, pp. 145

Ref No. Symbol Address

1 PROGA 4000

2 LISTB 40C3

3 ENDB 40D3

4 LISTC 4112

5 ENDC 4124

PROGA

Ref No. Symbol Address

1 PROGB 4063

2 LISTA 4040

3 ENDA 4054

4 LISTC 4112

5 ENDC 4124

PROGB

Ref No. Symbol Address

1 PROGC 4063

2 LISTA 4040

3 ENDA 4054

4 LISTB 40C3

5 ENDB 40D3

PROGC

Page 29: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Examples of Using Reference Numbers-- Figure 3.12, pp. 145 (Cond.)

�� �� �� � � � � � � � � � �� ��� �� � � � � � � � � � � � � � ��� �� �� � � � � � � � � � �� ��� �� � � � � � � � � � � � � � �

� � � � � � �� �� � � � � � �� � � � � � � � � � �� � � � � � � � � � � � � � � �� �� � � � � � �� � � � � � � � � � �� � � � � � � � �

� �� � � � � �� � � �� � � � �� � � �� � � � � � � �� � � �� � � � � � � � �� �� � � � � �� � � �� � � � �� � � �� � � � � � � �� � � �� � � � � � � � �

�� ���� ��

� � � �� � �� � � � �� � � � � � � � � �� � �� � � � � � � � � � � � � �� � � � � � � � � �� � �� � � � � �� � �� � � � �� � � � � � � � � �� � �� � � � � � � � � � � � � �� � � � � � � � � �� � �� �

�� ���� ��

� � � �� � �� � � � �� � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � �� � � � � � � � � � � �� � �� � � � �� � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � �� � � � � � � �

� � � � �� � �� � ��� � � � � � � � � � � �� � � � �� � �� � ��� � � � � � � � � � � �

� � � � �� � �� � ��� � � � � � � � � � � �� � � � �� � �� � ��� � � � � � � � � � � �

� � � � �� � �� � ��� � � � � � � � � � � �� � � � �� � �� � ��� � � � � � � � � � � �

� � � � �� � ��� � � � � � � � �� � � � �� � ��� � � � � � � � � �� �� ��� � ��� �

� � � � �� � �� � ��� � � � � � � � � � � �� � � � �� � �� � ��� � � � � � � � � � � �

� � � � �� � ��� � � � � � � � �� � � � �� � ��� � � � � � � � � �� �� ��� � ��� �

� � � � �� � �� � ��� � � � � � � � � � ��� � � � �� � �� � ��� � � � � � � � � � ��

� � � � �� ��� � � � � � � �� � � � �� ��� � � � � � � � �� �� � �� �� �� �

� � � � �� �� � ��� � � � � � � � � � �� � � � �� �� � ��� � � � � � � � � � �

� � � � �� � �� � ��� � � � � � � � � � � �� � � � �� � �� � ��� � � � � � � � � � � �

� � � � �� � ��� � � � � � � � �� � � � �� � ��� � � � � � � � � �� �� ���� ����

� � � � �� �� � � � � � �� � � � �� �� � � � � � �

� � �

� � � �

� � � �

� � � � �

� � � � � � �

� � �

� � �

� � �

� � �

� � �

� � �

� � �

� � �

� � �

� � �

� � �

Page 30: Chapter 3 Loaders and Linkerswatis/courses/188231/sp3-2.pdf · Chapter 3 Loaders and Linkers--Machine-Dependent Loader Feature Motivation Shortcoming of an absolute loader Programmer

��������� ����� ��

Examples of Using Reference Numbers-- Figure 3.12, pp. 145 (Cond.)

� �� �� � � � � � � � � � � �� �

� � � � � � �� �� � � � � � �� �

� � � � � � � � � � � � � � � � � � � � �

��

� � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � �

��

� � � �� � �� � � � �� � � � � � � � � � � � � � � � � � � � � � � � � �

� � � � � � � � � � � �

� � � � � � � � � ��

� � � � � � � � � � ��

� � � � � � � � � � � �

� � � � �� � �� � ��

� � � � �� � �� � � �

� � � � �� � �� � � �

� � � � �� � �� � � �

� � � � �� � �� � ��

� � � � �� � �� � � �

� � � � �� � �� � ��

� � � � �� � �� � ��

� � � � �� � �� � ��

� � � � �� � �� � � �

� �� �� � � � � � � � � � � �� �

� � � � � � �� �� � � � � � �� �

� � � � � � � � � � � � � � � � � � � � �

��

� � � �� � � � � � � � � � � �� � � � � � � � � � � � � �

��

� � � �� � �� � � � � � �� � � � � � � � � � � � � � � � � � � � � �� �

� � � � �� � � � � � �

� � � � �� � � � � ��

� � � � �� � � � � � �

� � � � �� � �� � ��

� � � � �� � �� � � �

� � � � �� � �� � ��

� � � � �� � �� � ��

� � � � �� � �� � ��

� � � � �� � �� � ��

� � � � �� � �� � ��

� � � � �� � �� � � �

� � � � �� � �� � ��

� � � � �� � �� � � �

� � � � �� � �� � � �

� � � � �� � �� � � �