Chapter 4 Loader and Linker
Chapter 4 Loader and Linker
Loaders
Loaders
• A loader is a system program that performs the
loading function.
– many also support relocation & linking
– others have a separate linker and loader
• Basic Functions
– bringing an object program into memory
– starting its execution
Input
• Object program:
– contains translated instructions and data from the
source program.
– specifies addresses in memory where these items
are to be loaded.
Basic Functions
• Allocation: allocate space in memory for the
programs
• Linking: Resolve symbolic references between
object files
– combines two or more separate object programs
– supplies the information needed to allow
references between them
Basic Functions
• Relocation: Adjust all address dependent
locations, such as address constants, to
correspond to the allocated space
– modifies the object program so that it can be
loaded at an address different from the location
originally specified
• Loading: Physically place the machine
instructions and data into memory
Basic Functions
Figure- Role of Linker and Loader
Loader Schemes
Segment is a unit of information treated as an entity.
It can be a program or data.
1. Compile and Go
– The assembler run in one part of memory
– place the assembled machine instructions and data,
as they are assembled, directly into their assigned
memory locations
– When the assembly is completed, the assembler
causes a transfer to the starting instruction of the
program
• Advantage
– simple
• Disadvantage
– Portion of memory wasted
– whenever the assembly program is to be
executed, it has to be assembled again
– Difficult to handle multiple segments
– programs have to be coded in the same
language
2. General Loader Scheme
- The loader accepts assembled machine
instructions, data & other information present in
object format
- Places machine instructions, data in core in an
executable form.
Advantage
- Saves memory in core.
- No need of retranslation
3. Absolute Loader
• The operation of absolute loader is very simple.
• The object code is loaded to specified locations
in the memory.
• At the end the loader jumps to the specified
address to begin execution of the loaded program.
Absolute Loader
Disadvantages
• If changes were made to MAIN that increased
its length to more than 300 bytes
– the end of MAIN (at 100 + 300 = 400) would
overlap the start of SQRT (at 400)
– It would then be necessary to assign SQRT to a
new location
• changing its START and re-assembling it?!
• Furthermore, it would also be necessarily to
modify all other subroutines that referred to
the address of SQRT.
Disadvantages of Absolute Loaders
• Actual load address must be specified
• The programmer must be careful not to assign
two subroutines to the same or overlapping
locations
• Difficult to use subroutine libraries (scientific
and mathematical) efficiently
– important to be able to select and load exactly
those routines that are needed
Loader functions are accomplished as follows
in the absolute loader
• Allocation - by programmer
• Linking - by programmer
• Relocation - by assembler
• Loading - by loader
4. Subroutine Linkages
• The main program A wishes to transfer to
subprogram B.
• The programmer, in program A, could write a
transfer instruction (e g, BAL 14,B) to
subprogram B.
• The assembler does not know the value of this
symbol reference and will declare it as an
error.
Externals and Entries
• The assembler pseudo-op EXTRN followed by
a list of symbols indicates that the symbols are
defined in other programs but referenced in the
present program
• If a symbol is defined in one program and
referenced in others,
– insert it into a symbol list following the pseudo-op
ENTRY.
5. Relocating Loaders
Relocating loaders or relative loaders:
– loaders that allow for program relocation.
– efficient sharing of the machine with larger
memory and when several independent
programs are to be run together
– support the use of subroutine libraries
efficiently
Two methods for specifying relocation as part of the
object program:
• 1. A Modification record
– describe each part of the object code that must be
changed when the program is relocated
• 2. Bit mask: A relocation bit/byte associated with
each word of object code
– S for Absolute: does not need modification
– R for Relative: needs relocation
– X for external
Design of Absolute Loader
2 Count=0
6-72 Empty
A B C
20K 20K 30K
D E
10K 20K
Dynamic Linking
• The loading and linking of external references
are postponed until execution time.
• The loader loads only the main program
• If the main program should
– execute a branch to an external address,
– reference an external variable
• The loader is called
– Only then has the segment containing the external
reference loaded.