0% found this document useful (0 votes)
24 views29 pages

Module 3 Loaders and Linkers Design of An Absolute Loader

spos

Uploaded by

Sushma Borkar
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
0% found this document useful (0 votes)
24 views29 pages

Module 3 Loaders and Linkers Design of An Absolute Loader

spos

Uploaded by

Sushma Borkar
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/ 29

lOMoARcPSD|8074068

753567671 module 3 - Loaders and linkers. Design of an


absolute loader
engineering (Mahatma Gandhi University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by sushma borkar ([email protected])
lOMoARcPSD|8074068

UNIT III

LOADERS AND LINKERS

INTRODUCTION

  Loader is a system program that performs the loading function. 


  Many loaders also support relocation and linking. 
 Some systems have a linker (linkage editor) to perform the linking
 operations and a separate loader to handle relocation and loading. 
 One system loader or linker can be used regardless of the original source
 programming language. 
  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 location originally specified. 
 Linking Combines two or more separate object programs and supplies the
information needed to allow references between them. 


3.1 BASIC LOADER FUNCTIONS

Fundamental functions of a loader:


1. Bringing an object program into memory.
2. Starting its execution.

3.1.1 Design of an Absolute Loader

For a simple absolute loader, all functions are accomplished in a single pass as follows:

1) The Header record of object programs is checked to verify that the correct
program has been presented for loading.

2) As each Text record is read, the object code it contains is moved to the
indicated address in memory.

3) When the End record is encountered, the loader jumps to the specified
address to begin execution of the loaded program.

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

43

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

An example object program is shown in Fig (a).

Fig (b) shows a representation of the program from Fig (a) after loading.

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

44

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

Algorithm for Absolute Loader

 It is very important to realize that in Fig (a), each printed character


 represents one byte of the object program record. 
 In Fig (b), on the other hand, each printed character represents one
 hexadecimal digit in memory (a half-byte). 
 Therefore, to save space and execution time of loaders, most machines
store object programs in a binary form, with each byte of object code
 stored as a single byte in the object program. 
 In this type of representation a byte may contain any binary value. 

3.1.2 A Simple Bootstrap Loader

When a computer is first turned on or restarted, a special type of absolute


loader, called a bootstrap loader, is executed. This bootstrap loads the first
program to be run by the computer – usually an operating system.

Working of a simple Bootstrap loader

  The bootstrap begins at address 0 in the memory of the machine. 


  It loads the operating system at address 80. 
 Each byte of object code to be loaded is represented on device F1 as two
hexadecimal digits just as it is in a Text record of a SIC object program. 
5

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

 The object code from device F1 is always loaded into consecutive bytes
of memory, starting at address 80. The main loop of the bootstrap keeps
the address of the next memory location to be loaded in register X. 
 After all of the object code from device F1 has been loaded, the bootstrap jumps to
address 80, which begins the execution of the program that was loaded. 
  Much of the work of the bootstrap loader is performed by the subroutine GETC. 
 GETC is used to read and convert a pair of characters from device F1
representing 1 byte of object code to be loaded. For example, two bytes =
 C “D8” ‘4438’H converting to one byte ‘D8’H. 
 The resulting byte is stored at the address currently in register X, using
STCH instruction that refers to location 0 using indexed addressing. 
 The TIXR instruction is then used to add 1 to the value in X. 

Source code for bootstrap loader

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

3.2 MACHINE-DEPENDENT LOADER FEATURES

 The absolute loader has several potential 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. 
 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. 
 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. 
 Loaders that allow for program relocation are called relocating loaders or
relative loaders. 

3.2.1 Relocation

Two methods for specifying relocation as part of the object program:

The first method:


 A Modification is used to describe each part of the object code that
must be changed when the program is relocated. 

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

Fig(1) :Consider the program

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

  Most of the instructions in this program use relative or immediate addressing. 


 The only portions of the assembled program that contain actual addresses
are the extended format instructions on lines 15, 35, and 65. Thus these
are the only items whose values are affected by relocation. 

Object program

 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. 
 In this example, all modifications add the value of the symbol COPY,
which represents the starting address of the program. 

Fig(2) :Consider a Relocatable program for a Standard SIC machine

10

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

.
.
.

 The Modification record is not well suited for use with all machine
architectures.Consider, for example, the program in Fig (2) .This is a
 relocatable program written for standard version for SIC. 
 The important difference between this example and the one in Fig (1) is
 that the standard SIC machine does not use relative addressing. 
 In this program the addresses in all the instructions except RSUB must modified
when the program is relocated. This would require 31 Modification records, which
results in an object program more than twice as large as the one in Fig (1). 

The second method:


11

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

 There are no Modification records. 


 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. 

Fig (3): Object program with relocation by bit mask

 The relocation bits are gathered together into a bit mask following the
length indicator in each Text record. In Fig (3) this mask is represented (in
character form) as three hexadecimal digits. 
 If the relocation bit corresponding to a word of object code is set to 1, the
program’s 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. 
 For example, 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. 
 Example: Note that the LDX instruction on line 210 (Fig (2)) begins a new
Text record. If it were placed in the preceding Text record, it would not be
properly aligned to correspond to a relocation bit because of the 1-byte
data value generated from line 185. 

3.2.2 Program Linking

Consider the three (separately assembled) programs in the figure, each of which
consists of a single control section.

Program 1 (PROGA):

12

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

Program 2 (PROGB):

Program 3 (PROGC):

13

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

Consider first the reference marked REF1.

For the first program (PROGA),


  REF1 is simply a reference to a label within the program. 
  It is assembled in the usual way as a PC relative instruction. 
 No modification for relocation or linking is necessary. 

In PROGB, the same operand refers to an external symbol.


 The assembler uses an extended-format instruction with address field set
 to 00000. 
 The object program for PROGB contains a Modification record instructing
the loader to add the value of the symbol LISTA to this address field when
the program is linked. 

For PROGC, REF1 is handled in exactly the same way.

Corresponding object programs

PROGA:

14

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

PROGB:

PROGC:

15

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

 The reference marked REF2 is processed in a similar manner. 



 REF3 is an immediate operand whose value is to be the difference
 between ENDA and LISTA (that is, the length of the list in bytes). 
 In PROGA, the assembler has all of the information necessary to compute
this value. During the assembly of PROGB (and PROGC), the values of
the labels are unknown. 
 In these programs, the expression must be assembled as an external reference
(with two Modification records) even though the final result will be an absolute
value independent of the locations at which the programs are loaded. 

  Consider REF4. 
 The assembler for PROGA can evaluate all of the expression in REF4
except for the value of LISTC. This results in an initial value of ‘000014’H
 and one Modification record. 
 The same expression in PROGB contains no terms that can be evaluated
by the assembler. The object code therefore contains an initial value of
000000 and three Modification records. 
 For PROGC, the assembler can supply the value of LISTC relative to the
beginning of the program (but not the actual address, which is not known
 until the program is loaded). 
 The initial value of this data word contains the relative address of LISTC
(‘000030’H). Modification records instruct the loader to add the beginning
address of the program (i.e., the value of PROGC), to add the value of
ENDA, and to subtract the value of LISTA. 

Fig (4): The three programs as they might appear in memory after loading
and linking.

16

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

PROGA has been loaded starting at address 4000, with PROGB and PROGC
immediately following.

For example, the value for reference REF4 in PROGA is located at address 4054
(the beginning address of PROGA plus 0054).

Fig (5): Relocation and linking operations performed on REF4 in PROGA

17

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

The initial value (from the Text record) is 000014. To this is added the address
assigned to LISTC, which 4112 (the beginning address of PROGC plus 30).

3.2.3 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
18

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

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

(1) 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.

(2) A hashed organization is typically used for this table.

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

3.2.3.1 PASS 1

 During Pass 1, the loader is concerned only with Header and Define
record types in the control sections. 

Algorithm for Pass 1 of a Linking loader

19

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

1) 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.
2) 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.
3) 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. 

 Many loaders include as an option the ability to print a load map that
shows these symbols and their addresses. 

3.2.3.2 PASS 2

 Pass 2 performs the actual loading, relocation, and linking of the program. 

Algorithm for Pass 2 of a Linking loader

1) As each Text record is read, the object code is moved to the specified address
(plus the current value of CSADDR).
2) When a Modification record is encountered, the symbol whose value is to be
used for modification is looked up in ESTAB.
3) This value is then added to or subtracted from the indicated location in memory.
4) 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. Our loader takes this as the
transfer point to begin execution. If more than one control section specifies a
 transfer address, the loader arbitrarily uses the last one encountered. 
 If no control section contains a transfer address, the loader uses the
 beginning of the linked program (i.e., PROGADDR) as the transfer point. 
 Normally, a transfer address would be placed in the End record for a main
program, but not for a subroutine. 

20

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

This algorithm can be made more efficient. Assign a reference number, which is
used (instead of the symbol name) in Modification records, to each external
symbol referred to in a control section. Suppose we always assign the reference
number 01 to the control section name.

Fig (6): Object programs using reference numbers for code modification

21

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

22

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

3.3 MACHINE-INDEPENDENT LOADER FEATURES

 Loading and linking are often thought of as OS service functions. Therefore,


most loaders include fewer different features than are found in a typical
 assembler. 
 They include the use of an automatic library search process for
handling external reference and some common options that can be
selected at the time of loading and linking. 

3.3.1 Automatic Library Search

 Many linking loaders can automatically incorporate routines from a


 subprogram library into the program being 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. 
 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 subroutines fetched from a library in this way may themselves
contain external references. It is therefore necessary to repeat the
library search process until all references are resolved. 
 If unresolved external references remain after the library search is
completed, these must be treated as errors. 

3.3.2 Loader Options

 Many loaders allow the user to specify options that modify the standard
processing 

 Typical loader option 1: Allows the selection of alternative sources of
input. 

Ex : INCLUDE program-name (library-name) might direct the loader to
read the designated object program from a library and treat it as if it
were part of the primary loader input. 

 Loader option 2: Allows the user to delete external symbols or entire
control sections. 

23

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

Ex : DELETE csect-name might instruct the loader to delete the


named control section(s) from the set of programs being loaded. 

CHANGE name1, name2 might cause the external symbol name1 to


be changed to name2 wherever it appears in the object programs.

 Loader option 3: Involves the automatic inclusion of library routines to


satisfy external references.

Ex. : LIBRARY MYLIB


Such user-specified libraries are normally searched before the standard
system libraries. This allows the user to use special versions of the standard
routines.

NOCALL STDDEV, PLOT, CORREL

 To instruct the loader that these external references are to remain


unresolved. This avoids the overhead of loading and linking the
unneeded routines, and saves the memory space that would otherwise
be required. 


3.4 LOADER DESIGN OPTIONS

  Linking loaders perform all linking and relocation at load time. 


 There are two alternatives: 
1. Linkage editors, which perform linking prior to load time.
2. Dynamic linking, in which the linking function is performed at
execution time.
 Precondition: The source program is first assembled or compiled,
 producing an object program. 
 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. 

3.4.1 Linkage Editors

 The linkage editor performs relocation of all control sections relative to


the start of the linked program. Thus, all items that need to be modified
24

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

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. 
 If a program is to be executed many times without being reassembled,
the use of a linkage editor substantially reduces the overhead required. 

 Linkage editors can perform many useful functions besides simply
preparing an object program for execution. Ex., a typical sequence of
 linkage editor commands used: 
INCLUDE PLANNER (PROGLIB) 

DELETE PROJECT {delete from existing PLANNER}


INCLUDE PROJECT (NEWLIB) {include new version}
REPLACE PLANNER (PROGLIB)

 Linkage editors can also be used to build packages of subroutines or other


control sections that are generally used together. This can be useful when
dealing with subroutine libraries that support high-level programming
languages. 
 Linkage editors often include a variety of other options and commands
like those discussed for linking loaders. Compared to linking loaders,
linkage editors in general tend to offer more flexibility and control. 

Fig (7): Processing of an object program using (a) Linking loader and
(b) Linkage editor

25

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

3.4.2 Dynamic Linking

 Linkage editors perform linking operations before the program is loaded


 for execution. 
 Linking loaders perform these same operations at load time. 
 Dynamic linking, dynamic loading, or load on call postpones the linking
function until execution time: a subroutine is loaded and linked to the rest
of the program when it is first called. 
 Dynamic linking is often used to allow several executing programs to
share one copy of a subroutine or library, ex. 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. 

26

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

Fig (a): Instead of executing a JSUB instruction referring to an external symbol,


the program makes a load-and-call service request to OS. The parameter of this
request is the symbolic name of the routine to be called.
Fig (b): 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.

Fig (c): Control is then passed from OS to the routine being called

Fig (d): 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.

Fig (e): If a subroutine is still in memory, a second call to it may not require another load
operation. Control may simply be passed from the dynamic loader to the called routine.

3.4.3 Bootstrap Loaders

  With the machine empty and idle there is no need for program relocation. 
 We can specify the absolute address for whatever program is first loaded
 and this will be the OS, which occupies a predefined location in memory. 
27

Downloaded by sushma borkar ([email protected])


lOMoARcPSD|8074068

 We need some means of accomplishing the functions of an absolute loader. 


1. To have the operator enter into memory the object code for an
absolute loader, using switches on the computer console.
2. To have the absolute loader program permanently resident in a ROM.
3. To have a built –in hardware function that reads a fixed –length record
from some device into memory at a fixed location.

 When some hardware signal occurs, the machine begins to execute this
 ROM program. 
 On some computers, the program is executed directly in the ROM: on others,
the program is copied from ROM to main memory and executed there. 
  The particular device to be used can often be selected via console switches. 
 After the read operation is complete, control is automatically transferred to
the address in memory where the record was stored, which contains
machine where the record was stored, which contains machine
 instructions that load the absolute program that follow. 
 If the loading process requires more instructions that can be read in a
single record, this first record causes the reading of others, and these in
 turn can cause the reading of still more records – boots trap. 
  The first record is generally referred to as bootstrap loader: 
 Such a loader is added to the beginning of all object programs that are to
 be loaded into an empty and idle system. 
 This includes the OS itself and all stand-alone programs that are to be run
without an OS. 

28

Downloaded by sushma borkar ([email protected])

You might also like