System Programming Chapter4 Loaders and Linkers 1 3

  • Slides: 81
Download presentation
System Programming Chapter-4 Loaders and Linkers 1

System Programming Chapter-4 Loaders and Linkers 1

3. 0 Introduction u The succeeding processes should be conducted after translation of source

3. 0 Introduction u The succeeding processes should be conducted after translation of source program Loading : brings the object program into memory for execution ¤ Relocation : modifies the object program so that it can be loaded at an address different from the original location ¤ Linking : combines two or more separate object programs and supplies the information needed to allow references between them ¤ 2

3. 0 Introduction-cont’d u The tasks of a loader performed ¤ Loading ¤ Relocation

3. 0 Introduction-cont’d u The tasks of a loader performed ¤ Loading ¤ Relocation (for most loaders) ¤ Linking (for most loader) : generally performed by a linker or a linkage editor 3

3. 1 Basic Loader Functions Brings an object program into memory and starts its

3. 1 Basic Loader Functions Brings an object program into memory and starts its execution u Introduce an absolute loader – a loader might be found in a simple (e. g. SIC) machine that uses the absolute addressing scheme u Linking loader – also support relocation and linking u Linkage editor – perform linking before loading u 4

3. 1. 1 Design of an Absolute Loader u Object program H COPY 001000

3. 1. 1 Design of an Absolute Loader u Object program H COPY 001000 00107 A T 001000 1 E 141033 482039 001036 281030 301015… T 00101 E 15 0 C 1036 482061 081033 4 C 0000 454 F 46 …. . . T 002073 07 382064 4 C 0000 05 E 001000 Fig. 3. 1 (a) 5

3. 1. 1 Design of an Absolute Loader-cont’d Memory Address 0 FF 0 Memory

3. 1. 1 Design of an Absolute Loader-cont’d Memory Address 0 FF 0 Memory Contents xxxxxxxx 1000 14103348 20390010 36281030 30101548 1010 20613 C 10 0300102 A 0 C 103900 102 D 0 C 10 1020 36482061 0810334 C …… 2070 2 C 103638 20644 C 00 0005 xxxxxxx Fig. 3. 1 (b) 6

3. 1. 1 Design of an Absolute Loader-cont’d u Algorithm for an absolute loader

3. 1. 1 Design of an Absolute Loader-cont’d u Algorithm for an absolute loader Begin read Header record verify program name and length read first Text record while record type ‘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 record end 7

3. 1. 2 A Simple Bootstrap Loader u The source code for SIC/XE bootstrap

3. 1. 2 A Simple Bootstrap Loader u The source code for SIC/XE bootstrap loader BOOT START 0. This bootstrap reads object code from device F 1 and enters it into memory starting address 80 (Hex. ). After all of the code has been seen entered into memory, the bootstrap executes a jump to address 80 to begin execution. Register X contains the next address to be loaded. CLEAR A LDX #128 initialize register X to hex 80 LOOP JSUB GETC read Hex. digit RMO A, S save in register S SHIFTL S, 4 move to high-order 4 bits JSUB GETC get next Hex. digit 8

3. 1. 2 A Simple Bootstrap Loadercont’d u The source code for SIC/XE bootstrap

3. 1. 2 A Simple Bootstrap Loadercont’d u The source code for SIC/XE bootstrap loader ADDR STCH TIXR J S, A 0, X X, X LOOP combine digits to form 1 byte store at address in register X add 1 to memory address . Subroutine to read one char from input device and convert it from ASCII code to Hex. Digit value. The converted digit value is returned in register A. when end-of-file is read, control is transferred to the starting address (Hex. 80). GETC TD INPUT test input device JEQ GETC 9

3. 1. 2 A Simple Bootstrap Loadercont’d u The source code for SIC/XE bootstrap

3. 1. 2 A Simple Bootstrap Loadercont’d u The source code for SIC/XE bootstrap loader GETC RD INPUT COMP #4 JEQ 80 COMP #48 JLT GETC SUB #48 COMP #10 JLT RETURN SUB #7 RETURN RSUB INPUT BYTE END read character if end-of-file (Hex. 04) jump to starting address compare to Hex. 30 (‘ 0’) skip char less than ‘ 0’ subtract Hex. 30 from ASCII if result is less than 10, conversion is complete, otherwise subtract 7 more (‘A’ through ‘F’ return to caller X’F 1’ 10

3. 2 Machine-Dependent Loader Features u Disadvantages of absolute loader ¤ One of the

3. 2 Machine-Dependent Loader Features u Disadvantages of absolute loader ¤ One of the most obvious is the need for the programmer to specify the actual address at which it will be loaded into memory Writing absolute programs also makes it difficult to use subroutine libraries efficiently. This could not be done effectively if all of the subroutines had pre-assigned absolute addresses. u Actually, linking usually involves relocation u 11

3. 2. 1 Relocation Two methods for specifying relocation as part of the object

3. 2. 1 Relocation Two methods for specifying relocation as part of the object program ¤ The first method: A Modification record (The format is given in Section 2. 3. 5. ) is used to describe each part of the object code that must be changed when the program is relocated ¤ Fig 3. 4 shows a SIC/XE program we use to illustrate this first method of specifying relocation 12

3. 2. 1 Relocation-cont’d u SIC/XE Assembly Program (Fig. 3. 4/Fig. 2. 6) Line

3. 2. 1 Relocation-cont’d u SIC/XE Assembly Program (Fig. 3. 4/Fig. 2. 6) Line Loc 5 0000 COPY START 0 10 0000 FIRST STL RETADR 17202 D 12 0003 LDB #LENGTH 69202 D BASE LENGTH +JSUB RDREC 4 B 101036 Source statement 13 CLOOP Object code 15 0006 20 000 A LDA LENGTH 032026 25 000 D COMP #0 290000 30 0010 JEQ ENDFIL 332007 35 0013 +JSUB WRREC 4 B 10105 D 13

3. 2. 1 Relocation-cont’d u SIC/XE Assembly Program (Fig. 3. 4) Line Loc Source

3. 2. 1 Relocation-cont’d u SIC/XE Assembly Program (Fig. 3. 4) Line Loc Source statement Object code 40 0017 J CLOOP 3 F 2 FEC 45 001 A ENDFIL LDA EOF 032010 50 001 D STA BUFFER 0 F 2016 14

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 55 0020 LDA

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 55 0020 LDA #3 010003 60 0023 STA LENGTH 0 F 200 D 65 0026 +JSUB WRREC 4 B 10105 D 70 002 A J @RETADR 3 E 2003 80 002 D EOF BYTE C’EOF’ 454 F 46 95 0030 RETADR RESW 1 100 0033 LENGTH RESW 1 Source statement Object code 15

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 105 0036 110

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 105 0036 110 ~ 120 Object code Source statement BUFFER RESB 4096 . . SUBROUTINE TO READ RECORD INTO BUFFER. 125 1036 130 RDREC CLEAR X B 410 1038 CLEAR A B 400 132 103 A CLEAR S B 440 133 103 C +LDT #4096 75101000 135 1040 TD INPUT E 32019 RLOOP 16

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc Object code Source

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc Object code Source statement 140 1043 JEQ RLOOP 332 FFA 145 1046 RD INPUT DB 2013 150 1049 COMPR A, S A 004 17

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 155 104 B

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 155 104 B JEQ EXIT 332008 160 104 E STCH BUFFER, X 57 C 003 165 1051 TIXR T B 850 170 1053 JLT RLOOP 3 B 2 FEA 175 1056 STX LENGTH 134000 180 1059 185 105 C 195 ~ 205 Source statement EXIT RSUB INPUT BYTE Object code 4 F 0000 X’F 1’ F 1 . . SUBROUTINE TO WRITE RECORD FROM BUFFER 18

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 210 105 D

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 210 105 D WRREC CLEAR X B 410 212 105 F LDT LENGTH 774000 215 1062 TD OUTPUT E 32011 220 1065 JEQ WLOOP 332 FFA 225 1068 LDCH BUFFER, X 53 C 003 230 106 B WD OUTPUT DF 2008 235 106 E TIXR T B 850 240 1070 JLT WLOOP 3 B 2 FEF 245 1073 RSUB Source statement WLOOP Object code 4 F 0000 19

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 250 1076 255

3. 2. 1 Relocation-cont’d u Fig. 3. 4 Program-cont’d Line Loc 250 1076 255 Source statement OUTPUT BYTE X’ 05’ END FIRST Object code 05 20

3. 2. 1 Relocation-cont’d u Relocation example with modification records use H COPY 000000

3. 2. 1 Relocation-cont’d u Relocation example with modification records use H COPY 000000 001077 T 0000001 D 17202 D 69202 D …. … M 000007 05 +COPY For format-4 M 000014 05 +COPY instructions only M 000027 05 +COPY Fig. 3. 5 object program with relocation by modification records of Fig. 3. 4 21

3. 2. 1 Relocation-cont’d u Standard SIC relocatable program (Fig. 3. 6) Line Loc

3. 2. 1 Relocation-cont’d u Standard SIC relocatable program (Fig. 3. 6) Line Loc 5 0000 COPY START 0 10 0000 FIRST STL RETADR 140033 15 0003 CLOOP JSUB RDREC 481039 20 0006 LDA LENGTH 000036 25 0009 COMP ZERO 280030 30 000 C JEQ ENDFIL 300015 35 000 F JSUB WRREC 481061 Source statement Object code 22

3. 2. 1 Relocation-cont’d u Standard SIC relocatable program (Fig. 3. 6) Line Loc

3. 2. 1 Relocation-cont’d u Standard SIC relocatable program (Fig. 3. 6) Line Loc Source statement Object code 40 0012 J CLOOP 3 C 0003 45 0015 ENDFIL LDA EOF 00002 A 50 0018 STA BUFFER 0 C 0039 23

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc 55 001 B

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc 55 001 B LDA THREE 00002 D 60 001 E STA LENGTH 0 C 0036 65 0021 JSUB WRREC 481061 70 0024 LDL RETADR 080033 75 0027 RSUB 80 002 A EOF BYTE C’EOF’ 454 F 46 85 002 D THREE WORD 3 000003 90 0030 ZERO WORD 0 000000 95 0033 RETADR RESW 1 100 0036 LENGTH RESW 1 Source statement Object code 4 C 0000 24

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc 105 0039 110

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc 105 0039 110 ~ 120 Object code Source statement BUFFER RESB 4096 . . SUBROUTINE TO READ RECORD INTO BUFFER. 125 1039 130 103 C 135 103 F RDREC RLOOP LDX ZERO 040030 LDA ZERO 000030 TD INPUT E 0105 D 25

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc Object code Source

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc Object code Source statement 140 1042 JEQ RLOOP 30103 F 145 1045 RD INPUT D 8105 D 150 1048 COMPR ZERO 280030 26

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc 155 104 B

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc 155 104 B JEQ EXIT 301057 160 104 E STCH BUFFER, X 548039 165 1051 TIXR MAXLEN 2 C 105 E 170 1054 JLT RLOOP 38103 F 175 1057 STX LENGTH 100036 180 105 A 185 105 D INPUT BYTE X’F 1’ F 1 190 105 E MAXLEN WORD 4096 001000 195 ~ 205 Source statement EXIT RSUB Object code 4 C 0000 . . SUBROUTINE TO WRITE RECORD FROM BUFFER 27

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc 210 1061 WRREC

3. 2. 1 Relocation-cont’d u Fig. 3. 6 Program-cont’d Line Loc 210 1061 WRREC LDX ZERO 040030 215 1064 WLOOP TD OUTPUT E 01079 220 1067 JEQ WLOOP 301064 225 106 A LDCH BUFFER, X 508039 230 106 D WD OUTPUT DC 1079 235 1070 TIX LENGTH 2 C 0036 240 1073 JLT LOOP 381064 245 1076 RSUB 250 1079 255 Source statement OUTPUT Object code 4 C 0000 BYTE X’ 05’ END FIRST 05 28

3. 2. 1 Relocation-cont’d u Comments for Fig. 3. 6 ¤ The standard SIC

3. 2. 1 Relocation-cont’d u Comments for Fig. 3. 6 ¤ The standard SIC does not use relative address, the addresses in all instructions must be modified with 31 modification records (too large) ¤ A different technique (is well suitable for machine that uses direct addressing and has a fixed instruction format) • A relocation bit associated with each word of object code • All bits are gathered together into a bit mask following the length indicator in each Text record 29

3. 2. 1 Relocation-cont’d H COPY 000000 000107 A T 000000 1 E FFC

3. 2. 1 Relocation-cont’d H COPY 000000 000107 A T 000000 1 E FFC 140033 481039 000036 280030 …. . T 00001 E 15 E 00 0 C 0036 481061 080033 4 C 0000 454 F 46 000003…. T 001039 1 E FFC 040030 000030 …. T 001057 0 A 800 100036 4 C 0000 F 1 001000 T 001061 19 FE 0 040030 E 01079 …. . E 000000 Fig. 3. 7 Relocation bit mask If the relocation bit is set to 1, the program’s starting address is to be added when the program is relocated, a bit value of 0 indicates that no modification is necessary 30

3. 2. 2 Program Linking u Control section review ¤ Could be assembled together

3. 2. 2 Program Linking u Control section review ¤ Could be assembled together or assembled independently ¤ Appear as separate segments of object code after assembly ¤ EXTDEF (external definition) & EXTREF (external reference) 31

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0000 PROGA START

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0000 PROGA START Object code 0 EXTDEF LISTA, ENDA EXTREF LISTB, ENDB, LISTC, ENDC …. 0020 REF 1 LDA LISTA 03201 D 0023 REF 2 +LDT LISTB+4 77100004 0027 REF 3 LDX #ENDA-LISTA 050014 …. 0040 LISTA EQU * …. 32

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0054 ENDA EQU

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0054 ENDA EQU Object code * 0054 REF 4 WORD ENDA-LISTA+LISTC 000014 0057 REF 5 WORD ENDC-LISTC-10 FFFFF 6 005 A REF 6 WORD ENDC-LISTC+LISTA-1 00003 F 005 D REF 7 WORD ENDA-LISTA-(ENDB-LISTB) 000014 0060 REF 8 WORD LISTB-LISTA END FFFFC 0 REF 1 33

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0000 PROGB START

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0000 PROGB START Object code 0 EXTDEF LISTB, ENDB EXTREF LISTA, ENDA, LISTC, ENDC …. 0036 REF 1 +LDA LISTA 03100000 003 A REF 2 LDT LISTB+4 772027 003 D REF 3 +LDX #ENDA-LISTA 05100000 …. 0060 LISTB EQU * …. 34

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0070 ENDB EQU

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0070 ENDB EQU Object code * 0070 REF 4 WORD ENDA-LISTA+LISTC 000000 0073 REF 5 WORD ENDC-LISTC-10 FFFFF 6 0076 REF 6 WORD ENDC-LISTC+LISTA-1 FFFFFF 0079 REF 7 WORD ENDA-LISTA-(ENDB-LISTB) FFFFF 0 007 C REF 8 WORD LISTB-LISTA END 000060 REF 1 35

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0000 PROGC START

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0000 PROGC START Object code 0 EXTDEF LISTC, ENDC EXTREF LISTA, ENDA, LISTB, ENDB …. 0018 REF 1 +LDA LISTA 03100000 001 C REF 2 +LDT LISTB+4 77100004 0020 REF 3 +LDX #ENDA-LISTA 05100000 …. 0030 LISTC EQU * …. 36

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0042 ENDC EQU

3. 2. 2 Program Linking-cont’d Fig. 3. 8 Loc Source statement 0042 ENDC EQU Object code * 0042 REF 4 WORD ENDA-LISTA+LISTC 000030 0045 REF 5 WORD ENDC-LISTC-10 000008 0048 REF 6 WORD ENDC-LISTC+LISTA-1 000011 004 B REF 7 WORD ENDA-LISTA-(ENDB-LISTB) 000000 004 E REF 8 WORD LISTB-LISTA END 000000 REF 1 37

3. 2. 2 Program Linking-cont’d u Comments for Fig. 3. 8 ¤ Consider the

3. 2. 2 Program Linking-cont’d u Comments for Fig. 3. 8 ¤ Consider the reference marked REF 1 • For program PROGA, no modification is necessary (program-counter relative instruction) • For program PROGB, the same operand refers to an external symbol, an extended-format instruction is necessary (initially set address to 00000). But a modification record is needed to instruct the loader to add the value of the symbol LISTA to this address • The same way is handled for program PROGC ¤ Reference manner REF 2 is processed in a similar 38

3. 2. 2 Program Linking-cont’d u Comments for Fig. 3. 8 ¤ Consider the

3. 2. 2 Program Linking-cont’d u Comments for Fig. 3. 8 ¤ Consider the reference marked REF 3 • In PROGA, the assembler has all of information necessary to compute this immediate value • In PROGB and PROGC, the values of the labels are unknown, so the initial value of the address field is set to 00000, and two modification records in object program are necessary for relocation update ¤ Consider the reference marked REF 4 • In PROGA, the assembler can evaluate all of the expression except for the value of LISTC, this results in an initial value of 000014 and one modification record 39

3. 2. 2 Program Linking-cont’d u Comments for Fig. 3. 8 • In PROGB,

3. 2. 2 Program Linking-cont’d u Comments for Fig. 3. 8 • In PROGB, no terms can be evaluated by the assembler, the object code contains an initial value of 000000 and three modification records • In PROGC, the assembler can supply the value of LISTC (but not the actual address, the actual address is not known until the program is loaded), modification records instruct the loader to add the beginning address of the program PROGC, to add the value of ENDA, and to subtract the value of LISTA 40

3. 2. 2 Program Linking-cont’d u Object programs corresponding to Fig. 3. 8 HPROGA

3. 2. 2 Program Linking-cont’d u Object programs corresponding to Fig. 3. 8 HPROGA 0000063 DLISTA 000040 ENDA 000054 RLISTB ENDB LISTC ENDC … T 0000200 A 03201 D 77100004050014 … T 0000540 F 000014 FFFFF 6…. M… M 00005406+LISTC M 00005706+ENDC … Fig. 3. 9 41

3. 2. 2 Program Linking-cont’d u Object programs corresponding to Fig. 3. 8 Fig.

3. 2. 2 Program Linking-cont’d u Object programs corresponding to Fig. 3. 8 Fig. 3. 9 HPROGB 000007 F DLISTB 000060 ENDB 000070 RLISTA ENDA LISTC ENDC … T 0000360 B 0310000077202705100000 … T 0000700 F 000000 FFFFF 6…. M… M 00007006+ENDA M 00007006 -LISTA M 00007006+LISTC M 00007306+ENDC … 42

3. 2. 2 Program Linking-cont’d u Object programs corresponding to Fig. 3. 8 Fig.

3. 2. 2 Program Linking-cont’d u Object programs corresponding to Fig. 3. 8 Fig. 3. 9 HPROGC 0000051 DLISTC 000030 ENDC 000042 RLISTA ENDA LISTB ENDB … T 0000180 C 031000007710000005100000 … T 0000420 F 000030000008000011…. M… M 00004206+ENDA M 00004206 -LISTA M 00004206+PROGC M 00004806+LISTA … 43

3. 2. 2 Program Linking-cont’d u Memory contents from Fig. 3. 8 after linking

3. 2. 2 Program Linking-cont’d u Memory contents from Fig. 3. 8 after linking and loading Fig. 3. 10 (a) 4020 03201 D 77 1010 C 705 0014…. Address 4030 4050 4060 4090 40 A 0 40 D 0 40 E 0 40 F 0 4100 4120 4130 Contents …. ………… 000083… ………… 05100014 ……… 00 0083…. . . …………. 40 C 70510 ………… 000083… 00412600 0080040 51000004 …………. . 031040 40772027 ………… 41260000 08004051 00000400 ………… …. . . 0310 40407710 0014…… 00412600 00080040 51000004 PROGA PROGB PROGC 44

3. 2. 2 Program Linking-cont’d u Relocation and linking operations performed on REF 4

3. 2. 2 Program Linking-cont’d u Relocation and linking operations performed on REF 4 from PROGA (Fig. 3. 10(b)) PROGA HPROGA. . (REF 4). . T 0000540 F 000014 … M 00005406+LISTC PROGC HPROGC. . . DLISTC 000030 … Memory contents 0000 (4112+0014=4126). . 4050 …. . 004126…. … (REF 4) + + 4112 Load addresses PROGA 004000 PROGB 004063 PROGC 0040 E 2 (Actual address of LISTC = 0030+40 E 2) 45

3. 2. 3 Algorithm and Data Structures for a Linking Loader u u The

3. 2. 3 Algorithm and Data Structures for a Linking Loader u u The input to a linking loader consists of a set of object programs (i. e. control sections) that are to be linked together It is possible for a control section to make an external reference to a symbol whose definition does not appear until later in this input stream – so thus a two-pass linking loader is required ¤ Pass-1 : assigns addresses to all external symbols ¤ Pass-2 : perform the actual loading, relocation, and linking 46

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d The main data

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d The main data structure needed for linking loader is an external symbol table ESTAB (analogous to SYMBOL, contains the name and address of each external symbol) u Two other important variables are PRGOADDR (program load address) and CSADDR (control section address); this value is added to all relative addresses within the control section) u 47

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d PROGADDR is the

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d PROGADDR is the beginning address in memory where the linked program is to be loaded. Its value is supplied to the loader by the operating system. u CSADDR contains the starting address assigned to the control section currently being scanned by the loader u 48

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d Algorithm for pass-1

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d Algorithm for pass-1 of a linking loader (Fig. 3. 11(a)) Begin u get PROGADDR from operating system set CSADDR to PROGADDR {first control section} while not end of input do begin read next input record {Header record for C. S. } set CSLTH to control section length search ESTAB for control section name if found then set error flag {duplicate external symbol} else enter control section name into ESTAB with value 49

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d while record type

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d 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 else enter symbol into ESTAB with value { CSADDR + indicated address) end {for} 50

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d end {while ≠

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d end {while ≠ ‘E’ } add CSLTH to CSADDR {starting address for next C. S. } end {while not EOF} end {Pass 1} 51

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u Algorithm for

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u Algorithm for pass-2 of a linking loader (Fig. 3. 11(a)) Begin set CSADDR to PROGADDR {first control section} set EXECADDR 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 52

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d if record type

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d if record type = ‘T’ then begin {if object code is in char 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 53

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d if found then

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d 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’ } 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 end {Pass 2} 54

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u Example of

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u Example of external symbol table (ESTAB) Control section Symbol name PROGA Address Length 4000 0063 LISTA 4040 ENDA 4054 PROGB 4063 LISTB 40 C 3 ENDB 40 D 3 PROGC 40 E 2 LISTC 4112 ENDC 4124 007 F 0051 55

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u u u

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u u u During the first pass, the loader is concerned only with Header and Define record types in the control sections. Pass-2 performs the actual loading, relocation, and linking of the program The last step performed by the loader is usually the transferring of control to the loaded program to begin execution. 56

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u In some

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u In some implementations, the reference number mechanism is used (instead of the symbol name) in modification records for algorithm efficiency improvement. For example HPROGA 0000063 DLISTA 00004 ENDA 000054 R 02 LISTB 03 ENDB 04 LISTC 05 ENDC … T. . M 00002405+02 M 00005406+04 … 57

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u u The

3. 2. 3 Algorithm and Data Structures for a Linking Loader-cont’d u u The reference number 01 is always assigned to the control section name The main advantage of this reference-number mechanism is that it avoids multiple searches of ESTAB for the same symbol during the loading of a control section. The reference number can be used as an index 58

3. 3 Machine-Independent Loader Features u u Automatic library search for handling external references

3. 3 Machine-Independent Loader Features u u Automatic library search for handling external references Loader options allow the user to specify options that modify the standard processing ¤ A special command language is used to specify options. Three methods can be applied 59

3. 3. 1 Automatic Library Search u u In most cases, the subroutines called

3. 3. 1 Automatic Library Search u u In most cases, the subroutines called by the program are automatically fetched from the system library (referred to as automatic library call) A special file structure directory is used to contain the name of each subroutine and a pointer to its address Other libraries may be specified by the programmer to the loader, with control statements or parameters If conflict, the programmer-specified subroutine names override the standard ones 60

3. 3. 2 Loader Options u Typical loader options ¤ Allows the selection of

3. 3. 2 Loader Options u Typical loader options ¤ Allows the selection of alternative sources of input. For example • INCLUDE program-name (library-name) direct the loader to read the designated object program from a library ¤ Allows the user to delete external symbol • DELETE csect-name instruct the loader to delete the named control section from the set of programs being loaded 61

3. 3. 2 Loader Options-cont’d ¤ Change external references within the programs being loaded

3. 3. 2 Loader Options-cont’d ¤ Change external references within the programs being loaded and linked • CHANGE name 1, name 2 Cause the name 1 to be changed to name 2 u Example INCLUDE READ(UTLIB) INCLUDE WRITE(UTLIB) DELETE RDREC, WRREC CHANGE RDREC, READ CHANGE WRREC, WRITE These commands direct the loader : 62

3. 3. 2 Loader Options-cont’d 1) 2) 3) ¤ To include control sections read

3. 3. 2 Loader Options-cont’d 1) 2) 3) ¤ To include control sections read and write from library UTLIB To delete the control sections RDREC and WRREC from the load To cause all external references to symbol RDREC to refer to symbol READ, and references to symbol WRREC to refer to symbol WRITE Allow the user to specify alternative libraries to be searched • LIBRARY MYLIB the user-specified libraries (e. g. MYLIB) are normally searched before the standard system libraries 63

3. 3. 2 Loader Options-cont’d ¤ Allow the user to specify that some external

3. 3. 2 Loader Options-cont’d ¤ Allow the user to specify that some external references not be solved in this way • NOCALL STDDEV, PLOT, CORREL instruct the loader that these external references are to remain unresolved (can avoid the overhead of loading and linking the unneeded routines) 64

3. 4 Loader Design Options u Linking loader (as discussed before) ¤ u Linkage

3. 4 Loader Design Options u Linking loader (as discussed before) ¤ u Linkage editors ¤ u Perform all linking and relocation at load time Perform linking prior to load time Dynamic linking ¤ The linking function is performed at execution time 65

3. 4. 1 Linkage Editors u u u Perform linking and some relocation operations

3. 4. 1 Linkage Editors u u u Perform linking and some relocation operations The linked program (often called a load module or an executable image) is written to a file or library instead of being immediately loaded into memory A simple relocating loader can be used to load the program into memory 66

3. 4. 1 Linkage Editors-cont’d u Processing of an object program using linking loader

3. 4. 1 Linkage Editors-cont’d u Processing of an object program using linking loader Object program Library Linking loader Memory 67

3. 4. 1 Linkage Editors-cont’d u Processing of an object program using linkage editor

3. 4. 1 Linkage Editors-cont’d u Processing of an object program using linkage editor Object program Library Linkage editor Linked program Relocating loader Memory 68

3. 4. 1 Linkage Editors-cont’d u Characteristics with linkage editor and linking loader ¤

3. 4. 1 Linkage Editors-cont’d u Characteristics with linkage editor and linking loader ¤ ¤ If a program is to be executed many times without being reassembled, the use of linkage editor is preferred (can reduce the overheads associated with resolution of external references and library searching) A linking loader searches libraries and resolves external references every time the program is executed 69

3. 4. 1 Linkage Editors-cont’d ¤ A linkage editor can easily accomplish the replacement

3. 4. 1 Linkage Editors-cont’d ¤ A linkage editor can easily accomplish the replacement of new subroutines by using the command language, for example INCLUDE DELETE INCLUDE REPLACE ¤ PLANNER (PROGLIB) PROJECT (NEWLIB) PLANNER (PROGLIB) A Linkage editor can also be used to build packages of subroutines or other control sections that are used together, for example 70

3. 4. 1 Linkage Editors-cont’d INCLUDE INCLUDE … SAVE READR(FTNLIB) WRITER(FTNLIB) BLOCK(FTNLIB) DEBLOCK(FTNLIB) ENCODE(FTNLIB)

3. 4. 1 Linkage Editors-cont’d INCLUDE INCLUDE … SAVE READR(FTNLIB) WRITER(FTNLIB) BLOCK(FTNLIB) DEBLOCK(FTNLIB) ENCODE(FTNLIB) DECODE(FTNLIB) FTNIO(SUBLIB) The linked module named FTNIO which contains the subroutines named as READR, WRITER, BLOCK, …, and so on, will be saved into the library SUBLIB 71

3. 4. 1 Linkage Editors-cont’d ¤ ¤ A linkage editor often allow the user

3. 4. 1 Linkage Editors-cont’d ¤ ¤ A linkage editor often allow the user to specify that external references are not to be solved by automatic library search (NOCALL command. This means that this task can be postponed to the execution time) Linkage editors tend to offer more flexibility and control, with a increase in complexity and overhead, compared to the linking loader 72

3. 4. 2 Dynamic Linking u u A scheme that postpones the linking function

3. 4. 2 Dynamic Linking u u A scheme that postpones the linking function until execution time (a subroutine is loaded and linked to the rest of program when it is first called) Also called dynamic loading or load on call 73

3. 4. 2 Dynamic Linking-cont’d u The advantages of dynamic linking ¤ ¤ Allow

3. 4. 2 Dynamic Linking-cont’d u The advantages of dynamic linking ¤ ¤ Allow several executing programs to share one copy of a subroutine or library (instead of linking a separate copy into each object program: linkage editor) In an object-oriented system, dynamic linking is often used for references to software objects : Allows the implementation of the object and its method to be determined at the program execution time (i. e. object to be shared by several programs) 74

3. 4. 2 Dynamic Linking-cont’d ¤ ¤ Dynamic linking provides the ability to load

3. 4. 2 Dynamic Linking-cont’d ¤ ¤ Dynamic linking provides the ability to load the routines only when they are needed, so thus it can result in substantial savings of time and memory space Dynamic linking can avoid the necessity of loading the entire library for each execution (allow its user to interactively call any of the subroutines) 75

3. 4. 2 Dynamic Linking-cont’d u A loading and linking method used in dynamic

3. 4. 2 Dynamic Linking-cont’d u A loading and linking method used in dynamic linking Step-1: the program makes a load-and-call service request to the Operating System Load–and-call service request: ERRHANDLE Dynamic loader (a part of the O. S) User program 76

3. 4. 2 Dynamic Linking-cont’d Step-2: the O. S examines its internal tables to

3. 4. 2 Dynamic Linking-cont’d Step-2: the O. S examines its internal tables to determine whether or not the routine is already loaded. If necessary, the routine is loaded from the specified library Dynamic loader User program Library ERRHANDLE 77

3. 4. 2 Dynamic Linking-cont’d Step-3: after the subroutine is loaded, the control is

3. 4. 2 Dynamic Linking-cont’d Step-3: after the subroutine is loaded, the control is passed from the O. S to the subroutine Dynamic loader User program ERRHANDLE 78

3. 4. 2 Dynamic Linking-cont’d Step-4: when the called routine completes its processing, the

3. 4. 2 Dynamic Linking-cont’d Step-4: when the called routine completes its processing, the control is returned to the user program, by passing through the O. S Dynamic loader User program ERRHANDLE 79

3. 4. 2 Dynamic Linking-cont’d Step-5: after the subroutine is completed, the allocated memory

3. 4. 2 Dynamic Linking-cont’d Step-5: after the subroutine is completed, the allocated memory may not be released immediately. It is desirable to retain the routine in memory for later use or reclaimed by the O. S while memory space is not enough Load–and-call service request: ERRHANDLE Dynamic loader User program ERRHANDLE 80

3. 4. 3 Bootstrap Loader u u Given an idle computer with no program

3. 4. 3 Bootstrap Loader u u Given an idle computer with no program in memory, how do we get things started? Solutions On some computers, an absolute loader program is permanently resident in a read-only memory (ROM). When some hardware signal occurs, the machine begins to execute this ROM program. This is referred to as a bootstrap loader. 81