100% found this document useful (1 vote)
73 views43 pages

Linker and Loader

The document discusses the features of a linker and loader that are dependent on machine architecture including program relocation and program linking. It describes how relocation is implemented using modification records or relocation bits in the object code. It also explains the two pass algorithm of a linking loader, where pass 1 assigns addresses to external symbols and pass 2 performs loading, relocation and linking by using data structures like the external symbol table.

Uploaded by

SHAH SMIT
Copyright
© © All Rights Reserved
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
100% found this document useful (1 vote)
73 views43 pages

Linker and Loader

The document discusses the features of a linker and loader that are dependent on machine architecture including program relocation and program linking. It describes how relocation is implemented using modification records or relocation bits in the object code. It also explains the two pass algorithm of a linking loader, where pass 1 assigns addresses to external symbols and pass 2 performs loading, relocation and linking by using data structures like the external symbol table.

Uploaded by

SHAH SMIT
Copyright
© © All Rights Reserved
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/ 43

CS202 – System Software

Dr. Manish Khare


Linker and Loader
Need for Linker and Loader

Slide 2
MACHINE DEPENDENT LOADER FEATURES

 The features of loader that depends on machine architecture


are called machine dependent loader features. It includes:

 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.

 The need for program relocation is an indirect consequence of the change to


larger and more powerful computers. The way relocation is implemented in a
loader is also dependent upon machine characteristics.

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.

 There are two methods for specifying relocation in object program


 1. Modification Record (for SIC/XE)
 2. Relocation Bit (for SIC)

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.

 The length is stored in half-bytes (4 bits)


 The starting location is the location of the byte containing the leftmost bits of the
address field to be modified.
 If the field contains an odd number of half-bytes, the starting location begins in the
middle of the first byte.

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

 To overcome the disadvantage of modification record, relocation bit is used.


 The Text records are the same as before except that there is a relocation bit 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.
 The relocation bits are gathered together into a bit mask following the length indicator
in each Text record.

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)

 Many programming languages allow us to write different pieces of code called


modules, separately. This simplifies the programming task because we can break a
large program into small, more manageable pieces. Eventually, though, we need to put
all the modules together. Apart from this, a user code often makes references to code
and data defined in some "libraries".

 Linking is the process in which references to "externally" defined symbols are


processed so as to make them operational.

 A linker or link editor is a program that combines object modules to form an


executable program.

 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

 During Pass 1, the loader is concerned only with Header and


Define records.
 Variables and Data structures used in PASS1
 PROGADDR (Program Load Address) from OS
 CSADDR (Control Section Address)
 CSLTH (Control Section Length)
 ESTAB (External Symbol Table)

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.

 In most cases a special file structure is used for the libraries

 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.

 Many loaders have a special command language that is used to specify


options.
 Sometimes there is a separate input file to the loader that contains such
control statements.
 Sometimes these same statements can also be embedded in the primary
input stream between object programs.
 On a few systems the programmer can even include loader control
statements in the source program.
 On some systems options are specified as a part of the job control
language that is processed by the O/S.

Slide 28
Command Language

 INCLUDE program-name (library-name)


 Include the designated objection program from a library as a part of input
 DELETE csect-name
 Delete the named control section from the set of programs being loaded
 CHANGE name1, name2
 Replace the external symbol name1 with name2
 LIBRARY library-name
 Specify alternative libraries to be searched
 NOCALL name1, name2 …
 Instruct the loader that these external references are to remain unresolved

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

 1. Linkage Editors – which perform linking prior to load time


 2. Dynamic Linking – which perform the linking function at execution time.
 3. Bootstrap Loaders – used to load operating system or the loader into the memory

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

 Dynamic linking dynamic loading, load on call)


 Postpone the linking function until execution time
 A subroutine is loaded and linked to the rest of the program when it is first
called

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

You might also like