SPOS
SPOS
UNIT-3
Linkers and Loaders
Introduc on:
A linker is a special program that combines the object files, generated by the
compiler/assembler and other pieces of code to originate an executable file
that has a .exe extension.
A loader is a special program that takes input of executable files from linker,
loads it to main memory, and prepares this code for execu on by computer.
Func ons of loader: Assemblers and compilers are used to convert source
code to object code. The loader will accept that object code, make it ready
for execu on, and helps to execute. Loader performs its task via four
func ons, these are as follows:
1. Allocation: It allocates memory for the program in the main
memory.
2. Linking: It combines two or more separate object programs or
modules and supplies necessary information.
3. Relocation: It modifies the object program so that it can be
loaded at an address different from the location.
4. Loading: It brings the object program into the main memory for
execution.
System programming and opera ng system
Disadvantages:
1. Complexity: Loaders can be complex to implement and maintain, as they
need to perform a variety of tasks such as memory management, symbol
resolu on, and reloca on.
2. Security: A poorly designed or implemented loader can introduce
security vulnerabilities, such as buffer overflows or other types of
memory corruption.
3. Overlapping Memory: Due to the use of dynamic loading and
relocation, the same memory location may be used by multiple
programs leading to overlapping memory.
System programming and opera ng system
Loading schemes:
Compile and go:
The "Compile and Go" loader is a technique used in system programming to
load and execute programs. It is also called as “assemble and go and load and
go”.
It is a loading scheme in which the assembler itself does the compila on and
then loads the assembled instruc ons in the designated memory loca on.
Examples of compile-and-go systems are WATFOR, PL/C, and Dartmouth
BASIC
Working:
The instruc ons are read line by line, its machine code is obtained and it is
directly put in the main memory at some known address.
The assembler runs in one part of the memory and assembled machine
instruc ons and data is directly put into their assigned memory loca ons.
Here no object code is generated, source program is directly converted into
exe (Executable file) code.
Advantages:
1. It is very simple to implement.
2. The translator is enough to do the task, no subroutines are needed.
3. The use of a compiler and loader can result in faster and more efficient
code execu on.
4. Compilers and loaders can automate many aspects of the so ware
development process, making it easier and faster to develop, test, and
deploy so ware.
5. No object files are required.
Disadvantages:
1. There is no use of the assembler but it is s ll there so a wastage of
memory takes place.
2. When source code runs multiple times, the translation is also done
every time. so, re-translation is happening.
System programming and opera ng system
Advantages:
1. Smaller than Assembler.
2. No reassembly is needed.
3. There is no wastage of memory because assembler is not needed and
loader is also present at the same loca on.
4. Avoids drawbacks of compile and go.
Disadvantages:
1. Some por on of memory is occupied by loader.
System programming and opera ng system
Example:
In the example there are two programs, i.e. MAIN starts at memory loca on
100. F1 starts at memory loca on 400. These loca ons are predefined
addresses where the program's code will be loaded.
The absolute loader takes the compiled program and loads it directly into
memory at the predefined addresses. The MAIN program is loaded star ng
at address 100, and F1 is loaded star ng at address 400.
System programming and opera ng system
The absolute loader directly loads the compiled code into these fixed
memory loca ons without any adjustments or reloca ons.
Advantages:
1. It is simple to implement.
2. No reloca on is required.
3. The task of loader becomes simpler as it simply obeys the instruc on
regarding where to place the object code to the main memory.
Disadvantages:
1. Programmer must specify the star ng address to the assembler for the
program where it should be loaded.
2. The programmer must know memory management as well as memory
status at any me.
3. It is very difficult to relocate in case of mul ple subrou nes.
Reloca ng loaders:
A reloca ng loader is a type of loader that is used in system so ware to
load programs into memory and adjust their addresses. It is responsible for
dynamic loading, address transla on, and using a symbol table to adjust the
addresses of a program's instruc ons and data.
The term "reloca ng" refers to the ability of the loader to adjust, or
"relocate", the absolute addresses in a program so that it can be loaded at
an address different from the one originally specified for it.
It achieves this by modifying the object code of a program based on the
offset address, which is the star ng address where the program is loaded in
main memory.
Example:
o The best examples of reloca ng loaders are found in early opera ng
systems. Here's a simple hypothe cal scenario to illustrate how a
reloca ng loader would work:
o Imagine an opera ng system that starts each program at memory
address 0. A programmer writes a program that uses memory addresses
0 through 999, and compiles it. The compiled program includes
instruc ons that refer to these memory addresses.
o However, when the program is loaded into memory, another program is
already using memory addresses 0 through 999. So, the opera ng
system decides to load the new program star ng at memory address
1000(Offset address).
System programming and opera ng system
Design:
Design of direct linking loader is more complicated than absolute loader.
Input to loader is object module and this is divided into 4 sec on ESD, TXT,
RLD, END.
It requires two passes to complete the linking process: Pass I: assigns
addresses to all external symbols (Uses Global EST: GEST). Pass II: performs
actual loading, reloca on and linking.
Advantages:
1. Allow mul ple procedure and data segment.
2. Reloca on Facility.
Disadvantages:
1. Because the loader performs linking and reloca on during the loading phase,
it can be slower than loaders that perform these tasks separately before
loading.
2. The loader must be designed to handle both linking and reloca on
dynamically, making it more complex than simpler loaders that only load the
program.
System programming and opera ng system
Self-reloca ng programs:
Program reloca on means the ability to load and run a program at any
memory loca on, instead of needing a fixed loca on specified during its
crea on. This depends on how and when the program’s virtual memory
addresses are mapped to physical memory addresses. In simple terms, a
relocatable program can be placed anywhere in memory and s ll work
correctly, as its memory references are adjusted as needed.
A self-reloca ng program is a program which can perform the reloca on
itself. Self-reloca ng program contain the reloca ng logic, so no need of a
linker in that.
1. Sta c linking:
It is the process of copying all library modules used in the program into the
final executable code.
The sta cally linked file includes both the calling program and the called
program.
Sta c linking is done by linker as the final step of compila on. When the
program is loaded, the OS places only a single file to the memory containing
both the source code and the reference libraries.
It has fast execu on but more disk storage.
2. Dynamic linking:
It is the process of using only the name of the shared library module in
program.
This dynamically link file includes only the name of calling program.
Dynamic linking is done by the linker at run me. Dynamic linking allows
many programs to use single copy of library module.
Execu on is slower but task takes less disk storage.
Overlays:
The loader can load programs in chunks called overlays, which allows
programs to run in a smaller memory space by only loading the needed
parts of the program at a me.
Overlay is a part of program that has same load origin as some other parts
of the program and the program which contains overlays is called as overlay
structured program.
System programming and opera ng system
The concept of overlays is that it will not use the complete program
simultaneously whenever a process is running. It will use only some part of
it. Then overlays concept says that whatever part you require, you load it,
and once the part is done, you just unload it, which means pull it back and
get the new part you required and run it.
System programming and opera ng system