Chapter - 3 Loaders & Linkers
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
Loader Design options –Linkage Editors –Dynamic Linking –Bootstrap Loaders – Implementation Examples Overview of Chapter 3 (contd…)
Basic loader functions: The loader brings an object program into memory and starts its executions.
There are 2 type of loader:- 1) Absolute loader. 2) A simple bootstrap loader. Types of loaders:
Absolute loader need not perform the functions like:- Program Linking and Program Relocation All 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.
Consider the COPY program: Line Loc Source statement Object code COPY START STL RETADR CLOOP JSUB RDREC LDA LENGTH COMP ZERO C JEQ ENDFIL F JSUB WRREC J CLOOP 3C ENDFIL LDA EOF 00102A STA BUFFER 0C B LDA THREE 00102D E STA LENGTH 0C JSUB WRREC LDL RETADR
Line Loc Sourse StatementObject Code RSUB 4C A EOF BYTE C’EOF’ 454F D THREE WORD ZERO WORD RETADR RESW LENGTH RESW BUFFER RESB subroutine to read record in to buffer RDREC LDX ZERO C LDA ZERO
Line Loc Source Statement Object Code F RLOOP TD INPUT E0205D JEQ RLOOP 30203F RD INPUT D8205D COMP ZERO B JEQ EXIT E STCH BUFFER,X TIX MAXLEN 2C205E JLT RLOOP 38203F EXIT STX LENGTH A R SUB 4C D INPUT BYTE X ‘F1’ F E MAXLEN WORD
Line Loc Source Statement Object Code Subroutine to write record from buffer WRREC LDX ZERO WLOOP TD OUTPUT E JEQ WLOOP A LDCH BUFFER X D WD OUTPUT DC D TIX LENGTH 2C JLT WLOOP RSUB 4C OUTPUT BYTE X ‘05’ END FIRST
Object program H ^ COPY ^ OO107A T ^ ^ 1E ^ ^ ^ ^ ^ ^ ^ ^ 00102A ^ OC1039 ^ 00102D T ^ 00101E ^ 15 ^ OC1036 ^ ^ ^ 4C000A 454F46 ^ ^ T ^ ^ 1E ^ ^ ^ E0205D ^ 30203F ^ D8205D ^ ^ ^ ^ 2C205E ^ 38203F T ^ ^ 1C ^ ^ 4C0000 ^ F1 ^ ^ ^ E02079 ^ ^ ^ DC2079 ^ 2C1036 T ^ ^ 07 ^ ^ 4C0000 ^ 05 E ^
Memory Address Contents 0000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 0010 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx ………………………… C A OC D0C C F xx xxxxxxxx xxxxxxxx xxxxxxxx …………………………… 2030 xxxxxxxx xxxxxxxx XX E C205E D0C 045B B C C C123 0C C C xxxx xxxxxxxx Absolute Program loaded in memory 0123 COPY
Algorithm for an absolute loader begin read Header record verify program name and length read 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 record end
Asimple 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.
O.S “F1” device Bootstrap loader O.S 0 80 Example (SIC bootstrap loader) The bootstrap itself begins at address 0 It loads the OS starting address 0x80 After loading the OS, No header record or control information, The object code is consecutive bytes of memory the control is transferred to the instruction at address 80.
Bootstrap loader for SIC/XE 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
SUBROUTINE GETC Begin 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
Bootstrap loader for SIC/XE BOOT 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
SUBROUTINE GETC GETC 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 CALLER INPUT BYTE X’F1’ CODE FOR INPUT DEVICE END LOOP
Machine - Dependent Loader features There are three feature:- 1) Relocation 2) Program Linking 3) Algorithm and Data Structures for a Linking Loader.
1) Relocation The loaders that allow for program relocation are called relocating loaders or relative loaders Definition:-
a) Modification Records Extended Instruction formats
Example of a SIC/XE program Line Loc Source statement Object code COPY START FIRST STL RETADR 17202D LDB #LENGTH 69202D 13 BASE LENGTH CLOOP +JSUB RDREC 4B A LDA LENGTH D COMP # JEQ ENDFIL JSUB WRREC 4B10105D J CLOOP 3F2FEC A ENDFIL LDA EOF D STA BUFFER 0F LDA # STA LENGTH 0F200D
Line Loc Sourse Statement Object Code JSUB WRREC 4B1O1O5D A 3E D EOF BYTE C’EOF’ 454F RETADR RESW LENGTH RESW BUFFER RESB subroutine to read record in to buffer RDREC CLEAR X B CLEAR A B A CLEAR S B C +LDT #
Line Loc Source Statement Object Code RLOOP TD INPUT E JEQ RLOOP 332FFA RD INPUT DB COMPR A,S A B JEQ EXIT E STCH BUFFER,X 57C TIXR T B JLT RLOOP 3B2FEA EXIT STX LENGTH RSUB 4F C INPUT BYTE X ‘F1’ F Subroutine to write record from buffer D WRREC CLEAR X B410
Line Loc Source Statement Object Code Subroutine to write record from buffer WRREC LDT LENGTH WLOOP TD OUTPUT E JEQ WLOOP 332FFA A LDCH BUFFER X 53C D WD OUTPUT DF D TIXR T 2C JLT WLOOP 3B2FEF RSUB 4F OUTPUT BYTE X ‘05’ END FIRST
Object program with relocation by Modification records H ^ COPY ^ OO1077 T ^ ^ 1D ^ 17202D ^ 69202D ^ ^ ^ ^ ^ ^ 00102A ^ OC1039 ^ 00102D T ^ 00101E ^ 15 ^ OC1036 ^ ^ ^ 4C000A 454F46 ^ ^ T ^ ^ 1E ^ ^ ^ E0205D ^ 30203F ^ D8205D ^ ^ ^ ^ 2C205E ^ 38203F T ^ ^ 1C ^ ^ 4C0000 ^ F1 ^ ^ ^ E02079 ^ ^ ^ DC2079 ^ 2C1036 T ^ ^ 07 ^ ^ 4C0000 ^ 05 M ^ ^ 05 + COPY M ^ ^ 05 + COPY M ^ ^ 05 + COPY E ^
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
Consider the COPY program: Line Loc Source statement Object code RB COPY START STL RETADR CLOOP JSUB RDREC LDA LENGTH COMP ZERO C JEQ ENDFIL F JSUB WRREC J CLOOP 3C ENDFIL LDA EOF 00102A STA BUFFER 0C B LDA THREE 00102D E STA LENGTH 0C JSUB WRREC LDL RETADR
Line Loc Sourse Statement Object Code RB RSUB 4C A EOF BYTE C’EOF’ 454F D THREE WORD ZERO WORD RETADR RESW LENGTH RESW BUFFER RESB subroutine to read record in to buffer RDREC LDX ZERO C LDA ZERO
Line Loc Source Statement Object Code F RLOOP TD INPUT E0205D JEQ RLOOP 30203F RD INPUT D8205D COMP ZERO B JEQ EXIT E STCH BUFFER,X TIX MAXLEN 2C205E JLT RLOOP 38203F EXIT STX LENGTH A R SUB 4C D INPUT BYTE X ‘F1’ F E MAXLEN WORD
Line Loc Source Statement Object Code Subroutine to write record from buffer WRREC LDX ZERO WLOOP TD OUTPUT E JEQ WLOOP A LDCH BUFFER X D WD OUTPUT DC D TIX LENGTH 2C JLT WLOOP RSUB 4C OUTPUT BYTE X ‘05’ END FIRST
Object program with relocation by bit mask H ^ COPY ^ OO107A T ^ ^ 1E ^ FFC ^ ^ ^ ^ ^ ^ ^ ^ 00102A ^ OC1039 ^ 00102D T ^ 00101E ^ 15 ^ E00 ^ OC1036 ^ ^ ^ 4C000A 454F46 ^ ^ T ^ ^ 1E ^ FFC ^ ^ ^ E0205D ^ 30203F ^ D8205D ^ ^ ^ ^ 2C205E ^ 38203F T ^ ^ 1A ^ 800 ^ ^ 4C0000 ^ F1 ^ ^ ^ E02079 ^ ^ ^ DC2079 ^ 2C1036 T ^ ^ 07 ^ ^ 4C0000 ^ 05 E ^
Program linking Goal R R esolve 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. C ontrol sections could be assembled together, or they could be assembled independently of one another. C C ontrol sections are to be linked, relocated, and loaded by loaders.
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
Program for Linking and Relocation 0000PROGA+START+0+0 +EXTDEF+LISTA,ENDA +EXTREF+LISTB,ENDB,LISTC,ENDC REF1+LDA+LISTA03201D 0023REF2+LDT+LISTB REF3+LDX#ENDA-LISTA LISTA+EQU +*+* ENDA+EQU +*+* 0054REF4+WORD ENDA-LISTA+LISTC 0057REF5+WORDFFFFF6 +ENDC-LISTC AREF6+WORD00003F +ENDC-LISTC+LISTA-1 005DREF7+WORD ENDA-LISTA-(ENDB-LISTB) 0060REF8 +WORD FFFFC0 +LISTB-LISTA +END +REF1 Instruction Operands { { Data Operands
Program for Linking and Relocation 0000PROGB+START+0+0 +EXTDEF+LISTB,ENDB +EXTREF+LISTA,ENDA,LISTC,ENDC REF1+LDA+LISTA AREF2+LDT+LISTB DREF3+LDX#ENDA-LISTA LISTB+EQU +*+* ENDB+EQU +*+* 0070REF4+WORD ENDA-LISTA+LISTC 0073REF5+WORDFFFFF6 +ENDC-LISTC REF6+WORDFFFFFF +ENDC-LISTC+LISTA REF7+WORDFFFFF0 +ENDA-LISTA-(ENDB-LISTB) 007CREF8 +WORD LISTB-LISTA +END +
Program for Linking and Relocation 0000PROGC+START+0+0 +EXTDEF+LISTC,ENDC +EXTREF+LISTA,ENDA,LISTB,ENDB REF1+LDA+LISTA CREF2+LDT+LISTB REF3+LDX#ENDA-LISTA LISTC+EQU+*+* ENDC+EQU+*+* 0042REF4+WORD+ENDA-LISTA+LISTC REF5+WORD+ENDC-LISTC REF6+WORD+ENDC-LISTC+LISTA BREF7+WORD+ENDA-LISTA-(ENDB-LISTB) EREF8 +WORD+LISTB-LISTA END+
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.
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 2Memory Load Map CSADDRESTAB
Linking loader -- Pass 1 A ssign address to all external symbols O nly processes Header Record and Define Record B uilds 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).
Control Section Address (CSADDR) T he starting address assigned to the control section currently being scanned by the loader. C SADDR is added to all relative addresses within the control section.
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
Print the load map if necessary (optional) ControlSymbolAddressLength sectionname PROGA LISTA4040 ENDA4054 PROGB F LISTB40C3 ENDB40D3 PROGC40E20051 LISTC4112 ENDC4124
Algorithm for Pass 1 of a linking loader
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
if found then set error flag {duplicate external symbol} else enter control section name into ESTAB with value CSADDR
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’ }
add CSLTH to CSADDR {starting address for next control section} end {while not EOF} End {Pass 1}
Algorithm for Pass 2 of a linking loader
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
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
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 {to start execution of loaded program} End {Pass 2}
MACHINE-INDEPENDENT LOADER FEATURES The loader features that are not directly related to the machine architecture and design.
Two machine-independent features of the loaders:- 1) Automatic Library search 2) Loader Options
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.
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
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:-
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
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
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.
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
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)
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
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
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
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
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.
LOADER DESIGN OPTIONS Some of the loader design options:- 1) Linkage Editor 2) Dynamic Linking 3) Bootstrap Loaders
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
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
Library Object programs Linkage editor Memory Relocating loader Linked programs Fig: Linkage editor
Object program:- T he source program is first compiled or assembled, producing an object program. Linkage Editor:- II t produces an linked version of the object program which is written to a file or library for later execution. WW hen the user is ready to run the linked program, a simple relocating loader is used to load the program into memory. Linkage Editors
TT he linkage editor performs relocation of all control sections relative to the start of the program. TT he loading is accomplished in one pass, with no ESTAB requirement, reduces overhead II f a program is to be executed many time without being reassembled, the linkage editor comes in need. RR esolving the external references and library searching are performed only one.
Linked program: II t is generally in a form suitable for processing by a relocating loader AA ll external references are resolved and relocating is indicated by some mechanism, such as Modification record or bit mask EE ven though all linking has been performed, information concerning external references is often retained in the linked program RR etaining of external references allows subsequent re-linking of the program to replace control sections, modify external references, etc. I f external symbols are not retained then the linked program cannot be reprocessed by the linkage editor.
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) : Linkage editor
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.
Other Functions of Linkage Editors 1 ) 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
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 )
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.
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)
Dynamic loader ( part of the OS ) User program Dynamic loader ( part of the OS ) User program ERRHANDL (c ) (d) ERRHANDL
Dynamic loader ( part of the OS ) User program Load-and–call ERRHANDL (e )