810 likes | 842 Vues
System Programming. Chapter-4 Loaders and Linkers. 3.0 Introduction. The succeeding processes should be conducted after translation of source program Loading : brings the object program into memory for execution
E N D
System Programming Chapter-4 Loaders and Linkers
3.0 Introduction • 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
3.0 Introduction-cont’d • 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.1 Basic Loader Functions • Brings an object program into memory and starts its execution • Introduce an absolute loader – a loadermight be found in a simple (e.g. SIC) machine that uses the absolute addressing scheme • Linking loader – also support relocation and linking • Linkage editor – perform linking before loading
3.1.1 Design of an Absolute Loader • Object program H COPY 001000 00107A T 001000 1E 141033 482039 001036 281030 301015… T 00101E 15 0C1036 482061 081033 4C0000 454F46 …. .... T 002073 07 382064 4C0000 05 E 001000 Fig. 3.1 (a)
3.1.1 Design of an Absolute Loader-cont’d Memory Address Memory Contents • 0FF0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx • 1000 14103348 20390010 36281030 30101548 • 1010 20613C10 0300102A 0C103900 102D0C10 • 36482061 0810334C • …… • 2070 2C103638 20644C00 0005xxxx xxxxxxx Fig. 3.1 (b)
3.1.1 Design of an Absolute Loader-cont’d • 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
3.1.2 A Simple Bootstrap Loader • The source code for SIC/XE bootstrap loader BOOT START 0.This bootstrap reads object code from device F1 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 address80 to begin execution. Register X contains the next address to be loaded. CLEAR A LDX #128 initialize register X to hex 80LOOP 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
3.1.2 A Simple Bootstrap Loader-cont’d • The source code for SIC/XE bootstrap loader ADDR S, A combine digits to form 1 byte STCH 0, X store at address in register X TIXR X, X add 1 to memory address J LOOP.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
3.1.2 A Simple Bootstrap Loader-cont’d • The source code for SIC/XE bootstrap loader GETC RD INPUT read character COMP #4 if end-of-file (Hex. 04) jump JEQ 80 to starting address COMP #48 compare to Hex. 30 (‘0’) JLT GETC skip char less than ‘0’ SUB #48 subtract Hex. 30 from ASCII COMP #10 if result is less than 10, conversion JLT RETURN is complete, otherwise subtract SUB #7 7 more (‘A’ through ‘F’ RETURN RSUB return to callerINPUT BYTE X’F1’ END
3.2 Machine-Dependent Loader Features • 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. • Actually, linking usually involves relocation
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
3.2.1 Relocation-cont’d • SIC/XE Assembly Program (Fig. 3.4/Fig. 2.6)
3.2.1 Relocation-cont’d • SIC/XE Assembly Program (Fig. 3.4)
3.2.1 Relocation-cont’d • Fig. 3.4 Program-cont’d
3.2.1 Relocation-cont’d • Fig. 3.4 Program-cont’d
3.2.1 Relocation-cont’d • Fig. 3.4 Program-cont’d
3.2.1 Relocation-cont’d • Fig. 3.4 Program-cont’d
3.2.1 Relocation-cont’d • Fig. 3.4 Program-cont’d
3.2.1 Relocation-cont’d • Fig. 3.4 Program-cont’d
For format-4 instructions only 3.2.1 Relocation-cont’d • Relocation example with modification records use H COPY 000000 001077T 0000001D 17202D 69202D ….…M 000007 05 +COPYM 000014 05 +COPYM 000027 05 +COPY Fig. 3.5 object program with relocation by modification records of Fig. 3.4
3.2.1 Relocation-cont’d • Standard SIC relocatable program (Fig. 3.6)
3.2.1 Relocation-cont’d • Standard SIC relocatable program (Fig. 3.6)
3.2.1 Relocation-cont’d • Fig. 3.6 Program-cont’d
3.2.1 Relocation-cont’d • Fig. 3.6 Program-cont’d
3.2.1 Relocation-cont’d • Fig. 3.6 Program-cont’d
3.2.1 Relocation-cont’d • Fig. 3.6 Program-cont’d
3.2.1 Relocation-cont’d • Fig. 3.6 Program-cont’d
3.2.1 Relocation-cont’d • 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
3.2.1 Relocation-cont’d 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 H COPY 000000 000107AT 000000 1E FFC140033 481039 000036 280030 …..T 00001E 15 E00 0C0036 481061 080033 4C0000 454F46 000003….T 001039 1EFFC 040030 000030 ….T 001057 0A 800 100036 4C0000 F1 001000T 001061 19 FE0040030 E01079 …..E 000000 Fig. 3.7 Relocation bit mask
3.2.2 Program Linking • 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)
3.2.2 Program Linking-cont’d Fig.3.8
3.2.2 Program Linking-cont’d Fig.3.8
3.2.2 Program Linking-cont’d Fig.3.8
3.2.2 Program Linking-cont’d Fig.3.8
3.2.2 Program Linking-cont’d Fig.3.8
3.2.2 Program Linking-cont’d Fig.3.8
3.2.2 Program Linking-cont’d • Comments for Fig.3.8 • Consider the reference marked REF1 • 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 REF2 is processed in a similar manner
3.2.2 Program Linking-cont’d • Comments for Fig.3.8 • Consider the reference marked REF3 • 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 REF4 • 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
3.2.2 Program Linking-cont’d • 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
3.2.2 Program Linking-cont’d • Object programs corresponding to Fig.3.8 HPROGA 000000000063DLISTA 000040ENDA 000054RLISTB ENDB LISTC ENDC… T0000200A03201D77100004050014…T0000540F000014FFFFF6….M…M00005406+LISTCM00005706+ENDC… Fig.3.9
3.2.2 Program Linking-cont’d • Object programs corresponding to Fig.3.8 HPROGB 00000000007FDLISTB 000060ENDB 000070RLISTA ENDA LISTC ENDC… T0000360B0310000077202705100000…T0000700F000000FFFFF6….M…M00007006+ENDAM00007006-LISTAM00007006+LISTCM00007306+ENDC… Fig.3.9
3.2.2 Program Linking-cont’d • Object programs corresponding to Fig.3.8 HPROGC 000000000051DLISTC 000030ENDC 000042RLISTA ENDA LISTB ENDB… T0000180C031000007710000005100000…T0000420F000030000008000011….M…M00004206+ENDA M00004206-LISTAM00004206+PROGCM00004806+LISTA… Fig.3.9
Fig. 3.10 (a) Address Contents 4020 03201D77 1010C705 0014….4030 ….4050 ………… 00412600 0080040 510000044060 000083…4090 ………… ………… ..031040 4077202740A0 05100014 …………40D0 ………00 41260000 08004051 0000040040E0 0083…...40F0 …………. ………… …...0310 404077104100 40C70510 0014…… 4120 ………… 00412600 00080040 510000044130 000083… PROGA PROGB PROGC 3.2.2 Program Linking-cont’d • Memory contents from Fig.3.8 after linking and loading
3.2.2 Program Linking-cont’d • Relocation and linking operations performed on REF4 from PROGA (Fig.3.10(b)) PROGA Memory contents HPROGA....T0000540F000014…M00005406+LISTC 0000 ..4050 …..004126….… (REF4) (4112+0014=4126) + (REF4) PROGC + (Actual address of LISTC = 0030+40E2) 4112 HPROGC.....DLISTC000030… Load addressesPROGA 004000PROGB 004063PROGC 0040E2
3.2.3 Algorithm and Data Structures for a Linking Loader • 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
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) • 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)
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. • CSADDR contains the starting address assigned to the control section currently being scanned by the loader
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)) Beginget PROGADDR from operating systemset 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
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}