The document discusses loaders, linkers, and the linking process. It describes how loaders perform loading, relocation, and sometimes linking. Linkers specifically perform linking. Absolute and relocating loaders are described. Relocating loaders use modification records or relocation bit masks to modify addresses during relocation. Program linking combines object programs and resolves external references after loading. Examples demonstrate how external references are handled differently before and after loading and linking. The implementation of a linking loader is outlined, including its use of symbol tables and addresses to assign locations during two passes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
41 views50 pages
Loader and Linker Se Ncit
The document discusses loaders, linkers, and the linking process. It describes how loaders perform loading, relocation, and sometimes linking. Linkers specifically perform linking. Absolute and relocating loaders are described. Relocating loaders use modification records or relocation bit masks to modify addresses during relocation. Program linking combines object programs and resolves external references after loading. Examples demonstrate how external references are handled differently before and after loading and linking. The implementation of a linking loader is outlined, including its use of symbol tables and addresses to assign locations during two passes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50
Loader and Linker
Three Working Items
• Loading: loading an object program into memory for execution. • Relocation: modify the object program so that it can be loaded at an address different from the location originally specified. • Linking: combines two or more separate object programs and supplies the information needed to allow references between them. • A loader is a system program that performs the loading function. • Many loaders also support relocation and linking. • Some systems have a linker to perform the linking and a separate loader to handle relocation and loading. Absolute Loader • An object program is loaded at the address specified on the START directive. • No relocation or linking is needed • Thus is very simple • All functions are accomplished in single pass. Absolute Loader • The Header record of object program is checked to verify that the correct program has been presented for loading. • As each Text record is read, the object code it contains is moved to indicated address in memory. • When the End record is encountered the loader jumps to the specified address to begin execution of the loaded program. No text record corresponds here. XXX indicates that the previous contents of these locations remain unchanged. Relocating Loader • Two methods to describe where in the object program to modify the address (add the program starting address) – Use modification records • Suitable for a small number of changes – Use relocation bit mask • Suitable for a large number of changes Program Written in SIC/XE
PC-relative
Only these three lines need
to be modified. Base-relative Base-relative
This program is written in SIC/XE instructions. Program counter-
relative and base-relative addressing are extensively used to avoid the need for many address modification records. The Object Program
Only lines 15, 35, and 65 need to be modified.
The Same Program Written in SIC
Direct addressing Direct addressing Direct addressing
This program is written in SIC instructions. Only direct addressing
can be used. As such, we need many modification records. This not only makes the object program bigger, it also slows down the loading process. Relocation Bit Mask • If an object needs too many modification records, it would be more efficient to use a relocation bit mask to indicate where in the object program should be modified when the object program is loaded. • A relocation bit is associated with each word of object code. Since all SIC instructions occupy one word, this means that there is one relocation bit for each possible instruction. • 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. Relocation Bit Mask Example
This one-byte “F1” makes the LDX instruction on line 210
begins a new text record. This is because each relocation bit should be associated with a three-byte word. However, this data item occupies only one byte, which violates the Alignment rule. Program Linking • A program may be composed of many control sections. • These control sections may be assembled separately. • These control sections may be loaded at different addresses in memory. • External references to symbol defined in other control sections can only be resolved (calculating their addresses in memory) after these control sections are loaded into memory. Program Linking Example Object Program Example Program Linking Example • Notice that program A defines LISTA and ENDA, program B defines LISTB and ENDB, and program defines LISTC and ENDC. • Notice that the definitions of REF1, REF2, .., to REF7 in all of these three control sections are the same. • Therefore, after these three control sections are loaded, no matter where they are loaded, the values of REF1 to REF7 in all of these programs should be the same. REF1 • Program A – LISTA is defined in its own program and its address is immediately available. Therefore, we can simply use program counter-relative addressing • Program B – Because LISTA is an external reference, its address is not available now. Therefore an extended-format instruction with address field set to 00000 is used. A modification record in inserted into the object code so that once LISTA’s address is known, it can be added to this field. • Program C – The same as that processed in Program B. REF2 • Program A – Because LISTB is an external reference, its address is not available now. Therefore an extended-format instruction with address field set to 00004 is used. A modification record is inserted into the object code so that once LISTB’s address is available, it can be added to this field. • Program B – LISTB is defined in its own program and its address is immediately available. Therefore, we can simply use program counter-relative addressing • Program C – The same as that processed in Program A. REF3 • Program A – The difference between ENDA and LISTA (14) is immediately available during assembly. • Program B – Because the values of ENDA and LISTA are unknown during assembly, we need to use an extended-format instruction with its address field set to 0. – Two modification records are inserted to the object program – one for +ENDA and the other for –LISTA. • Program C – The same as that processed in Program B. REF4 • Program A – The difference between ENDA and LISTA can be known now. Only the value of LISTC is unknown. Therefore, an initial value of 000014 is stored with one modification record for LISTC. • Program B – Because none of ENDA, LISTA, and LISTC’s values can be known now, an initial value of 000000 is stored with three modification records for all of them. • Program C – The value of LISTC is known now. However, the values for ENDA and LISTA are unknown. An initial value of 000030 is stored with two modification records for ENDA and LISTA. After Loading into Memory Suppose that program A is loaded at 004000, program B at 004063, and program C at 0040E2.
Notice that REF4,
REF5, REF6, and REF7 in all of these three programs have the same values. REF4 after Linking • Program A – The address of REF4 is 4054 (4000 + 54) because program A is loaded at 4000 and the relative address of REF4 within program A is 54. – The value of REF4 is 004126 because • The address of LISTC is 0040E2 (the loaded address of program C) + 000030 (the relative address of LISTC in program C) • 0040E2 + 000014 (constant already calculated) = 004126. REF4 after Linking • Program B – The address of REF4 is 40D3 (4063 + 70) because program B is loaded at 4063 and the relative address of REF4 within program A is 70. – The value of REF4 is 004126 because • The address of LISTC is 004112 • The address of ENDA is 004054 • The address of LISTA is 004040 • 004054 + 004112 – 004040 = 004126 Instruction Operands • For references that are instruction operands, the calculated values after loading do no always appear to be equal. • This is because there is an additional address calculation step involved for program-counter (base) relative instructions. • In such cases, it is the target addresses that are the same. • For example, in program A, the reference REF1 is a program-counter relative instruction with displacement 1D. When this instruction is executed, the PC contains the value 4023. Therefore the resulting address is 4040. In program B, because direct addressing is used, 4040 (4000 + 40) is stored in the loaded program for REF1. The Implementation of a Linking Loader • A linking loader makes two passes over its input – In pass 1: assign addresses to external references – In pass 2: perform the actually loading, relocation, and linking • Very similar to what a two-pass assembler does. Data Structures • External symbol tables (ESTAB) – Like SYMTAB, store the name and address of each external symbol in the set of control sections being loaded. – It needs to indicate in which control section the symbol is defined. • PROGADDR – The beginning address in memory where the linked program is to be loaded. (given by the OS) • CSADDR – It contains the starting address assigned to the control section currently being scanned by the loader. – This value is added to all relative addresses within the control sections. Algorithm • During pass 1, the loader is concerned only with HEADER and DEFINE record types in the control sections to build ESTAB. • PROGADDR is obtained from OS. • This becomes the starting address (CSADDR) for the first control section. • The control section name from the header record is entered into ESTAB, with value given by CSADDR. Algorithm (Cont’d) • All external symbols appearing in the DEFINE records for the current control section are also entered into ESTAB. • Their addresses are obtained by adding the value (offset) specified in the DEFINE to CSADDR. • At the end, ESTAB contains all external symbols defined in the set of control sections together with the addresses assigned to each. • A Load Map can be generated to show these symbols and their addresses. A Load Map Algorithm (Cont’d) • During pass 2, the loader performs the actual loading, relocation, and linking. • CSADDR is used in the same way as it was used in pass 1 – It always contains the actual starting address of the control section being loaded. • As each text record is read, the object code is moved to the specified address (plus CSADDR) • When a modification record is encountered, the symbol whose value is to be used for modification is looked up in ESTAB. • This value is then added to or subtracted from the indicated location in memory. Machine Independent Features Loader Options • Many loaders allow the user to specify options that modify the standard processing. • For example: – Include program-name (library name) • Direct the loader to read the designated object program from a library – Delete csect-name • Instruct the loader to delete the named control sections from the set of programs being loaded – Change name1, name2 • Cause the external symbol name1 to be changed to name2 wherever it appears in the program Loader Options Application – In the COPY program, we write two subroutines RDREC and WRREC to perform read records and write records. – Suppose that the computer system provides READ and WRITE subroutines which has similar but advanced functions. – Without modifying the source program and reassembling it, we can use the following loader options to make the COPY object program use READ rather than RDREC and WRITE rather than WRREC.
Include READ (Util)
Include WRITE (Util) Delete RDREC, WRREC Change RDREC, READ Change WRREC, WRITE Loader Design Options Linkage Editor • The difference between a linkage editor and a linking loader: – A linking loader performs all linking and relocation operations, including automatic library search, and loads the linked program into memory for execution. – A linkage editor produces a linked version of the program, which is normally written to a file for later execution. Linkage Editor • When the user is ready to run the linked program, a simple relocating loader can be used to load the program into memory. • The only object code modification necessary is the addition of an actual address to relative values within the program. • The linkage editor performs relocation of all control sections relative to the start of the linked program. Linkage Editor • All items that need to be modified at load time have values that are relative to the start of the linked program. • This means that the loading can be accomplished in one pass with no external symbol table required. • Thus, if a program is to be executed many times without being reassembled, the use of a linkage editor can substantially reduces the overhead required. – Resolution of external references and library searching are only performed once. Dynamic Linking • Linkage editors perform linking before the program is loaded for execution. • Linking loaders perform these same operations at load time. • Dynamic linking postpones the linking function until execution time. – A subroutine is loaded and linked to the test of the program when it is first called. Dynamic Linking Application • Dynamic linking is often used to allow several executing programs to share one copy of a subroutine or library. • For example, a single copy of the standard C library can be loaded into memory. • All C programs currently in execution can be linked to this one copy, instead of linking a separate copy into each object program. Dynamic Linking Application • In an object-oriented system, dynamic linking is often used for references to software object. • This allows the implementation of the object and its method to be determined at the time the program is run. (e.g., C++) • The implementation can be changed at any time, without affecting the program that makes use of the object. Dynamic Linking Advantage • The subroutines that diagnose errors may never need to be called at all. • However, without using dynamic linking, these subroutines must be loaded and linked every time the program is run. • Using dynamic linking can save both space for storing the object program on disk and in memory, and time for loading the bigger object program. On PC Windows or UNIX operating systems, normally you are using (e.g., ld) a linkage editor to generate an executable program.