0% found this document useful (0 votes)
23 views85 pages

SS Mod 4

System software module 4

Uploaded by

amayavk118
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)
23 views85 pages

SS Mod 4

System software module 4

Uploaded by

amayavk118
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/ 85

Loader and Linker

Loaders and Linkers


• An object program contains translated instructions
and data values from the source program and
specifies addresses in memory where these items are
to be loaded.
• 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
Basic Loader Functions
• 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
► Absolute Loader is a primitive type of loader, which
does only the loading function.
► It does not perform linking and program relocation.

► All functions are accomplished in a single pass.


► The Header record of object programs 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 the indicated address in memory.
► When the End record is encountered, the loader jumps to
the specified address to begin execution of the loaded
program.
Example – COPY program
No text record corresponds here.
XXX indicates that the previous
contents of these locations remain
unchanged.
Absolute Loader Implementation
“14” occupies two bytes if
it is represented in char form.

When loaded into


memory, “14” should
occupy only one byte.
Absolute Loader Implementation
• In the object program, each byte of the assembled code is
given in its hexadecimal representation in character form.
• Eg: opcode for STL - a pair of characters “1” and “4”.
• When read by a loader, it occupy 2 bytes of memory.
• When loaded for execution, this opcode must be stored in a
single byte with hexadecimal no. 14.
• Each pair of bytes from the object program record must be
packed in to a single byte during loading.
• In the object code, each character represents one byte.
• In the loaded program, each character represents one
hexadecimal digit in memory(ie. half byte)
Bootstrap Loader
• When a computer is first turned on or restarted, a special
type of absolute loader, bootstrap loader is executed
(stored in ROM on a PC).
• The bootstrap loader loads the first program to be run by
the computer – usually the operating system, from the
boot disk (e.g., a hard disk or a floppy disk)
• It then jumps to the just loaded program to execute it.
• Normally, the just loaded program is very small (e.g., a
disk sector’s size, 512 bytes) and is a loader itself.
• The just loaded loader will continue to load another larger
loader and jump to it.
• This process repeats another the entire large operating
system is loaded.
Bootstrap Loader Example

Convert “14” in char form


to “14” in byte form
Bootstrap Loader Example

Convert “1” in char form


to “1” in its ASCII code
• Bootstrap loader begins at address 0 in memory.
• It loads OS starting at address 80.
• Subroutine GETC reads on character from device F1.
• Converts it from the ASCII code to the hexadecimal
representation.
• GETC jumps to address 80 when EOF read from F1.
GETC Subroutine
• Read one character from F1 and convert it from the ASCII
character code to the hexadecimal digit of the character.
• Eg: ASCII code “0” (hexadecimal 30) converted to numeric 0.
• ASCII “1” through “9”(hexadecimal 31 through 39) converted to
numeric values 1 through 9.
• ASCII “A” through “F”(hexadecimal 41 through 46) converted to
numeric values 10 through 15.
• Subtract 48(hexadecimal 30) from character codes “0” through
“9”.
• Subtract 55(hexadecimal 37) from character codes “A” through
“F”.
• GETC jumps to address 80 when EOF(hexadecimal 04) is read.
• Skips all input characters less than hexadecimal 30, causes
bootstrap to ignore any control bytes.
• Main loop of bootstrap loads address of next memory
location in register X,
• GETC read and convert a pair of characters from F1.
• These two hexadecimal values are combined into a single
byte by shifting the first one left 4 bits and adding the
second to it.
• The resulting byte is stored at address currently in register
X, using STCH instruction.
• TIXR add 1 to the value of X.
Absolute Loader - Drawbacks
• Programmer need to specify the actual address where the
object code will be loaded in memory.
• Not an issue with a simple machine with a small memory,
run only one program at a time.
• Advanced machines need to run several independent
programs together, sharing memory and other resources
between them.
• We do not know in advance where a program will be
loaded.
• Efficient sharing of resources require relocatable programs.
• Efficient use of subroutine libraries require relocatable
programs.
Relocating Loader
• Loaders that perform program Relocation.
• 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.
Modification Record
• Describe part of the object code that must be changed
when the program is relocated.
• Most instructions in the COPY program use relative or
immediate addressing.
• The portions of the assembled program that contain actual
addresses are extended format instructions on lines 15,35
and 65. – values affected by relocation.
The Object Program

Only lines 15, 35, and 65 need to be modified.


Each modification record specify the starting
address, the length of the field whose value is
to be altered and the modification to be performed.
Eg: all modifications add the value of symbol
COPY, which is the starting address of the program
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(No relative addressing). 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 code 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, there is one
relocation bit for each possible instruction.
• The relocation bits are gathered together in to a bit mask
following the length indicator in each text record.
Relocation Bit Mask Example

Bit mask is represented as three hexadecimal digits


(underlined for easier identification)
Relocation Bit Mask
• 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.
• A bit value 0 indicates no modification,
• If a text record has fewer than 12 words of object code, the
bits corresponding to the unused words are set to 0.
• Eg:- bit mask FFC( representing bit string 111111111100)
specifies that the first 10 words of the object code are to be
modified during relocation.
• E00(111000000000) specifies that first three words are to
be modified.
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(line 185), which violates the
Alignment rule.
Program Linking
• A program may be composed of many control
sections.
• These control sections may be assembled together
or independently of one another.
• They appear as separate segments of object code
after assembly.
• 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
Program Linking
• Each program contains a list of items. Eg LISTA
• Ends of these lists are marked by labels ENDA …
• Each program contains same set of references to
external symbols.
• REF1 through REF3 are instruction operands.
• REF4 through REF8 are values of data words.
Object Program Example
Program Linking Example
• Program A defines LISTA and ENDA, program B
defines LISTB and ENDB, and program C 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. We can simply use program
counter-relative addressing. No modification for relocation
or linking is required.
• Program B
– LISTA is an external reference, its address is not available.
– 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
– LISTB is an external reference, its address is not
available now.
– 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+000030 (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.
Linking Loader
• A linking loader consists of a set of object
programs that are to be liked together.
• A control section may refer to an external
symbol whose definition appear later .
• Linking operation cannot be performed until
an address is assigned to the external
symbol.
• Hence, a linking loader usually makes two
passes over its input.
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 Structure and Variables
• External symbol tables (ESTAB)
– Like SYMTAB, store the name and address of each external
symbol in the set of control sections being loaded.
– It also indicate in which control section the symbol is defined.
– Hashed Organization.
• PROGADDR(Program load Address)
– The beginning address in memory where the linked program
is to be loaded. (given by the OS)
• CSADDR(Control Section Address)
– 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 to convert them to actual address.
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.
• When the END record is read, the CSLTH is added to
CSADDR to get the starting address for the next control
section.
• At the end of pass 1, 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(useful in program debugging).
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.
Reference Number
• The linking loader algorithm can be made more
efficient if we assign a reference number to each
external symbol referred to in a control section.
• This reference number is used (instead of the symbol
name) in modification record.
• This simple technique avoid multiple searches of
ESTAB for the same symbol during the loading of a
control section.
– After the first search for a symbol (the REFER records), we
put the found entries into an array.
– Later in the same control section, we can just use the
reference number as an index into the array to quickly fetch a
symbol’s value.
Reference Number Example

Reference number 01 is reserved


for the current control section name.
All other reference numbers start
from 02.
Machine Independent Features
Automatic Library Search
• Many linking loaders can automatically
incorporate routines from a subprogram library
into the program being loaded. (E.g., the standard
C library)
• The subroutines called by the program are
automatically fetched from the library, linked with
the main program, and loaded.
• The programmer does not need to take any action
beyond mentioning the subroutine names as
external references in the source program
Automatic Library Search
• Linking loader 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.
• Enter symbols from the Refer record into the ESTAB.
• At the end of pass 1, the symbols in ESTAB that
remain undefined represent unresolved external
references.
• The loader searches the library for routines that
contain the definitions of these symbols, and processes
the subroutines found by this search process exactly as
if they had been part of the primary input stream.
Automatic Library Search
• The subroutines fetched from a library in this way
may themselves contain external references. It is
necessary to repeat the library search process until
all references are resolved.
• If unresolved references remain after the library
search is completed, they are treated as errors.
• If a symbol (or a subroutine name) is defined both
in the source program and in the library, the one in
the source program is used first.
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.
– INCLUDE program_name(library name)

– 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
• Alternatives for loading, linking and
relocation:
– Linking loaders perform all linking and
relocation at load time.
– Linkage editors perform linking prior to load
time.
– Dynamic linking perform linking at execution
time.
Linkage editor and linking loader
• Source program is first assembled(compiled)
to produce the object program.
– A linking loader performs all linking and
relocation operations, including automatic
library search. The linked program is loaded
directly into memory for execution.
– A linkage editor produces a linked version of
the program(load module), which is normally
written to a file for later execution.
Linking Loader

Linkage Editor
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 required is the
addition of an actual load 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.
• 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 reduces the
overhead required.
– Resolution of external references and library searching
are only performed once.
– A linking loader searches libraries and resolves external
references every time the program is executed.
• Linking loader is used when
- the program is reassembled for every execution.
- may occur in a program development and testing
environment(student programs)
- when a program is used so infrequently that it is not
worthwhile to store the assembled version in a library.
• The linked program produced by a linkage editor
is suitable for processing by a relocating loader.
• All external references are resolved.
• Relocation is indicated by modification record or
bit mask.
• Information of external references are retained in
the linked program, allows subsequent relinking of
the program to replace control sections, modify
external references etc.
• If a subroutine is modified, after the new version of the
subroutine is assembled or compiled, the linkage editor
can replace this subroutine in the linked version of the
program. It is not necessary to go back to the original
versions of all other subroutines.

INCLUDE PLANNER(PROGLIB)
DELETE PROJECT
INCLUDE PROJECT(NEWLIB)
REPLACE PLANNER(PROGLIB)

• Linkage editor can be used to build packages of


subroutines or other control sections that are used together.
- useful when dealing with subroutine libraries.
Dynamic Linking
• Linkage editors perform linking before the
program is loaded for execution.
• Linking loaders perform these linking
operations at load time.
• Dynamic linking 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 Application
• Dynamic linking allows 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 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 makes it possible for one object to be
shared by several programs.
Dynamic Linking Advantage
• Eg: If the program contains subroutines that diagnose
errors in the input data during execution. If such errors are
rare, these routines may never need to be called at all.
• However, without using dynamic linking, (if the program
were completely linked before execution)these subroutines
must be loaded and linked every time the program is run.
• Dynamic linking loads the routines only when they are
needed.
• If the subroutines are large or have many external
references, dynamic linking can save both space for storing
the object program on disk and in memory, and time for
loading the bigger object program.
Dynamic Linking Advantage
• Suppose, one execution a program uses only a few out of a
large number of possible subroutines, but the exact
routines needed cannot be predicted until the program
examines its input.
• In this case all of the subroutines could be needed but only
a few will actually be used in one execution.
• Dynamic linking avoids the necessity of loading the entire
library for each execution.
• Program need no know the possible set of subroutines that
might be used. The subroutine name may be treated as
another input item.
Dynamic Linking Implementation
• A subroutine that is to be dynamically loaded must
be called via an operating system service request.
– This method can also be thought of as a request to a
part of the loader that is kept in memory during
execution of the program
• Instead of executing a JSUB instruction that refers
to an external symbol, the program makes a load-
and-call service request to the OS.
• The parameter of this request is the symbolic name
of the routine to be called. (fig.(a))
Implementation Example

Load the called


Issue a load-and-call subroutine into
service request memory
Dynamic Linking Implementation
• The OS examines its internal tables to determine whether the
subroutine is already loaded.
• If needed, the subroutine is loaded from the library. (fig(b))
• Then control is passed from the OS to the subroutine being
called. (fig©)
• When the called subroutine completes its processing, it
returns to its caller (operating system).
• The OS then returns control to the program that issues the
request. (fig(d))
• After the subroutine is completed, the memory that was
allocated to load it may be released and used for other
purposes.
Control is returned The called subroutine
Control is passed
to the loader and
to the loaded this time is already loaded.
later returned to
subroutine.
the user program
Dynamic Linking Implementation
• However, often this is not done immediately.
• Sometimes it is desirable to retain the routine in memory
for later use as long as the storage space is not needed for
other processing.
• If the subroutine is retained in memory, it can be used by
later calls to the same subroutine without loading the same
subroutine multiple times.
• Control can simply pass from the dynamic loader to the
called routine directly. (fig(e))
• When dynamic linking is used, the association of the actual
address with the symbolic name of the called routine is not
made until the called statement is executed.
• Binding of the name to an actual address is delayed from
load time until execution time.
• Delayed binding offer more capabilities at higher cost.

You might also like