System Programming (SENG 3044)
Lecture 03: Loaders and Linkers
Walelign A. ([Link].)
Debre Berhan University
Faculty of Computing
Department of Software Engineering
January 4, 2021
Lecture 03 Loaders and Linkers January 4, 2021 1 / 28
Outline
Basic Loader Functions
Machine-Dependent Loader Features
Machine-Independent Loader Features
Loader Design Options
Implementation Examples
Lecture 03 Loaders and Linkers January 4, 2021 2 / 28
Introduction
Loading: loading an object program into memory for execution.
Relocation: modify the object program so that it can be loaded at
an address 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.
Lecture 03 Loaders and Linkers January 4, 2021 3 / 28
Type of loaders
Absolute loader
Bootstrap loader
Relocating loader (relative loader)
Design Options
Linking Loader
Linkage editors
Dynamic linking
Basic Loader Functions
The most fundamental functions of a loader:
:Bringing an object program into memory and starting its execution
Lecture 03 Loaders and Linkers January 4, 2021 4 / 28
Absolute Loader
An object program is loaded at the address specified on the START
directive.
Absolute Program (e.g. SIC programs)
Advantage
I Simple and efficient
Disadvantages
I The need for programmer to specify the actual address at which it will
be loaded into memory
I Difficult to use subroutine libraries efficiently
Absolute loader only performs loading function
I Does not need to perform linking and program relocation.
I All functions are accomplished in a single pass
Lecture 03 Loaders and Linkers January 4, 2021 5 / 28
Absolute Loader
In a Single Pass
I Check the Header record for program name, starting address, and
length
I Bring the object program contained in the Text record to the indicated
address
I No need to perform program linking and relocation
I Start the execution by jumping to the address specified in the End
record
Lecture 03 Loaders and Linkers January 4, 2021 6 / 28
Absolute Loader
In a Single Pass
I Object Program Contains
F H record
F T record
F E record
Lecture 03 Loaders and Linkers January 4, 2021 7 / 28
Type of loaders
Loading an absolute program
Lecture 03 Loaders and Linkers January 4, 2021 8 / 28
Bootstrap Loader
Bootstrap Loader
I When a computer is first turned on or restarted, a special type of
absolute loader, called a bootstrap loader is executed
In PC, BIOS acts as a bootstrap loader
I This bootstrap loads the first program to be run by the computer –
usually an operating system
I It then jumps to the just loaded program to execute it.
I Normally, the just loaded program is very small (e.g., a disk sector’s
size, 512 bytes) and is a loader itself.
I The just loaded loader will continue to load another larger loader and
jump to it.
I This process repeats another until the entire large operating system is
loaded.
Lecture 03 Loaders and Linkers January 4, 2021 9 / 28
Program Relocation
An object program that contains the information necessary to perform
address modification for relocation
The assembler can identify for the loader those parts of object
program that need modification.
No instruction modification is needed for
I immediate addressing (not a memory address)
I PC-relative, Base-relative addressing
The only parts of the program that require modification at load time
are those that specify direct addresses
Lecture 03 Loaders and Linkers January 4, 2021 10 / 28
Instruction format vs, Relocatable Loader
In SIC/XE
I Relative and immediate addressing
Do not need to modify their object code after relocation
I Extended format
Whose values are affected by relocation
Need to modify when relocation
In SIC
I Format 3 with address field
Should be modified
SIC does not support PC-relative and base-relative addressing
Lecture 03 Loaders and Linkers January 4, 2021 11 / 28
Relocation
Loaders that allow for program relocation are called relocating loaders
or relative loaders.
Two methods for specifying relocation as part of the object program
I Modification Records
F Suitable for a small number of relocations required
When relative or immediate addressing modes are extensively used
I Relocation bits
F Suitable for a large number of relocations required
When only direct addressing mode can be used in a machine with fixed
instruction format (e.g., the standard SIC machine)
A Modification record is used to describe each part of the object
code that must be changed when the program is relocated.
Lecture 03 Loaders and Linkers January 4, 2021 12 / 28
Example of a SIC/XE Program
Lecture 03 Loaders and Linkers January 4, 2021 13 / 28
Example of a SIC/XE Program
Lecture 03 Loaders and Linkers January 4, 2021 14 / 28
Example of a SIC/XE Program
Lecture 03 Loaders and Linkers January 4, 2021 15 / 28
Relocatable Program
Lecture 03 Loaders and Linkers January 4, 2021 16 / 28
Object Program with Relocation by Modification Records
Lecture 03 Loaders and Linkers January 4, 2021 17 / 28
Algorithm and Data Structure for a Linking Loader
Algorithm for a linking (and relocating) loader
I Modification records are used for relocation
F Not use the modification bits
F So that linking and relocation functions are performed using the same
mechanism.
Lecture 03 Loaders and Linkers January 4, 2021 18 / 28
Implementation of An Assembler
Lecture 03 Loaders and Linkers January 4, 2021 19 / 28
Implementation of a Linking Loader
Lecture 03 Loaders and Linkers January 4, 2021 20 / 28
Data Structures
Lecture 03 Loaders and Linkers January 4, 2021 21 / 28
Algorithm
Pass 1
I Assign addresses to all external symbols
F Loader is concerned only with Header and Define records in the control
sections
I To build up ESTAB
F Add control section name into ESTAB
F Add all external symbols in the Define record into ESTAB
Pass 2
I Perform the actual loading, relocation, and linking
I When Text record is encountered
F Read into the specified address (+CSADDR)
I When Modification record is encountered
F Lookup the symbol in ESTAB
F This value is then added to or subtracted from the indicated location in
memory
Lecture 03 Loaders and Linkers January 4, 2021 22 / 28
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
Lecture 03 Loaders and Linkers January 4, 2021 23 / 28
Loader Design Options
Linkage Editor
The difference between a linkage editor and a linking loader:
I A linking loader performs all linking and relocation operations,
including automatic library search, and loads the linked program into
memory for execution.
I A linkage editor produces a linked version of the program, which is
normally written to a file for later execution.
Lecture 03 Loaders and Linkers January 4, 2021 24 / 28
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.
I A subroutine is loaded and linked to the test 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.
For example, a single copy of the standard C library can be loaded
into memory.
Lecture 03 Loaders and Linkers January 4, 2021 25 / 28
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.
Lecture 03 Loaders and Linkers January 4, 2021 26 / 28
End
Question
Many Thanks!
Lecture 03 Loaders and Linkers January 4, 2021 27 / 28