Linker and Loader
Linker and Loader
Slide 2
MACHINE DEPENDENT LOADER FEATURES
1. Program Relocation
2. Program Linking
Slide 3
Program Relocation (Relocating Loader)
The absolute loader has several disadvantages. One of the most obvious is the need for
the programmer to specify the actual address at which it will be loaded into memory.
On a simple computer with a small memory the actual address at which the program
will be loaded can be specified easily.
On a larger and more advanced machine, we often like to run several independent
programs together, sharing memory between them. We do not know in advance where
a program will be loaded. Hence we write relocatable programs instead of absolute
ones.
Slide 4
Writing absolute programs also makes it difficult to use subroutine libraries
efficiently. This could not be done effectively if all of the subroutines had
preassigned absolute addresses.
Slide 5
Sometimes it is required to load and run several programs at the same time. The system
must be able to load these programs wherever there is place in the memory. Therefore
the exact starting address is not known until the load time.
In an absolute program the starting address to which the program has to be loaded is
mentioned in the program itself using the START directive. So the address of every
instruction and labels are known while assembling itself.
Loaders that has the capability to perform relocation are called relocating loaders
or relative loaders.
Slide 6
Modification Record
A Modification record is used to describe each part of the object code that must be
changed when the program is relocated.
Slide 7
Each Modification record specifies the starting address and length of the field whose
value is to be altered. It then describes the modification to be performed.
Slide 8
Consider the following object program, here the records starting with M represents the
modification record.
In this example, the record M 000007 05 + COPY is the modification suggested for the
statement at location 000007 and requires modification of 5-half bytes and the
modification to be performed is add the value of the symbol COPY, which represents
the starting address of the program.(means add the starting address of program to the
statement at 000007).
Slide 9
The Modification record is not well suited for certain cases.
In some programs the addresses in majority of instructions need to be modified when
the program is relocated. This would require large number of Modification records,
which results in an object program more than twice as large as the normal.
In such cases, the second method called relocation bit is used.
Slide 10
Relocation Bit
Slide 11
If the relocation bit corresponding to a word of object code is set to 1, the programs
starting address is to be added to this word when the program is relocated.
A bit value of 0 indicates that no modification is necessary.
If a Text record contains fewer than 12 words of object code, the bits corresponding to
unused words are set to 0.
In the following object code, the bit mask FFC (representing the bit string
111111111100) in the first Text record specifies that all 10 words of object code are to
be modified during relocation.
Slide 12
Program Linking (Linking Loader)
A Linking Loader is a program that has the capability to perform relocation, linking
and loading. Linking and relocation is performed at load time.
Slide 13
Slide 14
Algorithm and Data Structures for a Linking Loader
The algorithm for a linking loader is considerably more complicated than the absolute
loader algorithm.
A linking loader usually makes two passes over its input, just as an assembler does.
In terms of general function, the two passes of a linking loader are quite similar to the
two passes of an assembler:
Pass 1 assigns addresses to all external symbols.
Pass 2 performs the actual loading, relocation, and
linking.
The main data structure needed for our linking loader is an external symbol table
ESTAB. This table, which is analogous to SYMTAB in our assembler algorithm, is
used to store the name and address of each external symbol in the set of control
sections being loaded.
Slide 15
Two other important variables are PROGADDR (program load address) and
CSADDR (control section address).
(1) PROGADDR is the beginning address in memory where the linked program is to
be loaded. Its value is supplied to the loader by the OS.
(2) CSADDR 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 section to convert them to actual addresses.
Slide 16
Linking loader PASS 1
Slide 17
How Pass 1 Algorithm work
The beginning load address for the linked program (PROGADDR) is obtained from the
OS. This becomes the starting address (CSADDR) for the first control section in the
input sequence.
The control section name from Header record is entered into ESTAB, with value given
by CSADDR.
All external symbols appearing in the Define record for the control section are also
entered into ESTAB. Their addresses are obtained by adding the value specified in the
Define record to CSADDR.
When the End record is read, the control section length CSLTH (which was saved from
the End record) is added to CSADDR. This calculation gives the starting address for
the next control section in sequence.
At the end of Pass 1, ESTAB contains all external symbols defined in the set of control
sections together with the address assigned to each.
Slide 18
Slide 19
Slide 20
Linking loader PASS 2
Pass 2 of linking loader performs the actual loading, relocation, and linking of the
program.
Slide 21
How Pass 2 Algorithm work
As each Text record is read, the object code is moved to the specified address (plus the
current value of 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.
The last step performed by the loader is usually the transferring of control to the loaded
program to begin execution. The End record for each control section may contain the
address of the first instruction in that control section to be executed. Loader takes this
as the transfer point to begin execution.
Slide 22
Slide 23
MACHINE INDEPENDENT LOADER FEATURES
The features of loader that doesn’t depends the architecture of machine are called
machine independent loader features. It includes:
Automatic Library search
Loader Options that can be selected at the time of loading and linking
Slide 24
Automatic Library Search
Many linking loaders can automatically incorporate routines from a subprogram
library into the program being loaded.
In most cases there is a standard system library that is used in this way. Other
libraries may be specified by control statements or by parameters to the loader.
The subroutines called by the program being loaded are automatically fetched
from the library linked with the main library, program, and loaded.
Linking loaders that support automatic library search must keep track of
external symbols that are referred to, but not defined, in the primary input to
the loader.
At the end of Pass 1, the symbols in ESTAB that remain undefined represent
unresolved external references.
Slide 25
The loader searches the library or libraries specified for routines that contain
the definitions of these symbols, and processes the subroutines found by this
search exactly as if they had been part of the primary input stream.
The process just described allows the programmer to override the standard
subroutines in the library by supplying his or her own routines.
This structure contains a directory that gives the name of each routine and a
pointer to its address within the file.
Some operating systems can keep the directory for commonly used libraries
permanently in memory. This can expedite the search process if a large
number of external references are to be resolved.
Slide 26
Slide 27
Loader Options
Many loaders allow the user to specify options that modify the standard
processing.
Slide 28
Command Language
Slide 29
If we would like to use the utility routines READ and WRITE instead of RDREC and
WRREC in our programs, for a temporary measure, we use the following loader
commands
INCLUDE READ(UTLIB)
INCLUDE WRITE(UTILB)
DELETE RDREC, WRREC
CHANGE RDREC, READ
CHANGE WRREC, WRITE
These commands would ask the loader to include control sections READ and WRITE
from the library UTLIB and to delete the control sections WRREC and RDREC. The
first CHANGE command would change all the external references to the symbol
RDREC to be changed to refer to READ and second CHANGE will cause references
to WRREC to be changed to WRITE.
Slide 30
Loader Design Options
Slide 31
Linkage Editors
Linking loaders perform all linking and relocation at load time. There are two
alternatives: Linkage editors, which perform linking prior to load time, and dynamic
linking, in which the linking function is performed at execution time.
Difference between linkage editor and linking loader is explained below:
A linking loader performs all linking and relocation operations, including automatic
library search if specified, and loads the linked program directly into memory for
execution.
A linkage editor produces a linked version of the program (load module or executable
image), which is written to a file or library for later execution.
When the user is ready to run the linked program, a simple relocating loader
can be used to load the program into memory.
This means that the loading can be accomplished in one pass with no external
symbol table required.
This approach reduces the overhead when the program is executed. All that is
required at load time is a very simple form of relocation.
Slide 32
Slide 33
A linkage editor produces a linked version of the program (load module or executable
image), which is written to a file or library for later execution. 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
load address to relative values within the program.
The Linkage Editor(LE) performs relocation of all control sections relative to the start
of the linked program. Thus, 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.
Slide 34
Dynamic Linking
Linkage editor
Perform linking operations before the program is loaded for execution
Linking loader
Perform linking operations at load time
Slide 35
Dynamic linking is often used to allow several executing programs to share one copy
of a subroutine or library (eg. run-time support routines for a high-level language like
C.)
With a program that allows its user to interactively call any of the subroutines of a
large mathematical and statistical library, all of the library subroutines could
potentially be needed, but only a few will actually be used in any one execution.
Dynamic linking can avoid the necessity of loading the entire library for each
execution except those necessary subroutines.
Slide 36
Whenever the user program
needs a subroutine for its
execution, the program makes a
load-and-call service request to
OS(instead of executing a JSUB
instruction referreing to an
external symbol) . The parameter
of this request is the symbolic
name(ERRHANDL) of the
routine to be called.
Slide 37
OS examines its internal
tables to determine
whether or not the
routine is already loaded.
If necessary, the routine
is loaded from the
specified user or system
libraries.
Slide 38
Control is then passed from OS to
the routine being called.
Slide 39
When the called subroutine completes it
processing, it returns to its caller (i.e.,
OS). OS then returns control to the
program that issued the request.
Slide 40
Control may simply be passed from the dynamic loader to the called routine.
Slide 41
Bootstrap Loader
Slide 42
Given an idle computer with no program in memory, how do we get things started? Two solutions
are there.
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.
On some computers, there’s a built-in hardware which read a fixed-length record from some
device into memory at a fixed location. After the read operation, control is automatically
transferred to the address in memory. If the loading process requires more instructions than
can be read in a single record, this first record causes the reading of others, and these in turn
can cause the reading of more records
When a computer is first turned on or restarted, a special type of absolute loader, called a
bootstrap loader, is executed. This bootstrap loader loads the first program to be run by the
computer – usually an operating system
Slide 43