Loaders and Linkers

18
Loaders and Linkers Name: Kunj Desai Branch: CSE A Semester: 5 th Year: 2016

Transcript of Loaders and Linkers

Page 1: Loaders and Linkers

Loaders and Linkers Name: Kunj Desai Branch: CSE A Semester: 5th Year: 2016

Page 2: Loaders and Linkers

Translation Hierarchy Compiler : Translates high-level language program intoassembly language. Assembler: Converts assembly language programs intoobject files. Object files contain a combination of machine instructions, data, and information needed to place instructions properly in memory.

Page 3: Loaders and Linkers

Flow of the Hierarchy

Page 4: Loaders and Linkers

Flow of ExecutionTranslator

Linker

Loader

Program executio

n

.Obj

.exe

Page 5: Loaders and Linkers

Process for producing an executable file

Page 6: Loaders and Linkers

Explanation to Linker Definition: Tool that merges the object files produced byseparate compilation or assembly and creates anexecutable file. Three tasks done by LINKER: Searches the program to find library routines used byprogram, e.g. printf(),sqrt(),strcat() and various other. Determines the memory locations that code from eachmodule will occupy and relocates its instructions byadjusting absolute references.

Page 7: Loaders and Linkers

Explanation to Linker Relocation, which modifies the object program so that it can be loaded at an address different from the location originally specified.

It combines two or more separate object programs and supplies the information needed to allow references between them .

Page 8: Loaders and Linkers

Linking Concepts Computer programs typically comprise several

parts or modules; all these parts/modules need not be contained within a single object file, and in such case refer to each other by means of symbols. Typically, an object file can contain three kinds of symbols:

Publicly defined symbols, which allow it to be called by other modules , also called as public definition .

Externally defined symbols(undefined symbols), which calls the other modules where these symbols are defined, also called as external reference.

Local symbols, used internally within the object file to facilitate relocation.

Page 9: Loaders and Linkers

Static Linking Static linking occurs when a calling program is

linked to a called program in a single executable module. When the program is loaded, the operating system places into memory a single file that contains the executable code and data.

The result of statically linking programs is an .EXE file or dynamic link library (DLL) subprogram that contains the executable code for multiple programs. This file includes both the calling program and the called program.

Page 10: Loaders and Linkers

Advantages and Disadvantages

Advantages The advantage of static linking is that you can

create self-contained, independent programs. In other words, the executable program consists of one part (the .EXE file) that you need to keep track of.

Disadvantages: You cannot change the behavior of executable

files without re-linking them. External called programs cannot be shared,

requiring that duplicate copies of programs be loaded in memory if more than one calling program needs to access them.

Page 11: Loaders and Linkers

Dynamic linking Many operating system environments allow

dynamic linking, that is the postponing of the resolving of some undefined symbols until a program is run.

That means that the executable code still contains undefined symbols, plus a list of objects or libraries that will provide definitions for these. Loading the program will load these objects/libraries as well, and perform a final linking.

Page 12: Loaders and Linkers

Advantages Advantages : Often-used libraries (for example the standard

system libraries) need to be stored in only one location, not duplicated in every single binary.

If an error in a library function is corrected by replacing the library, all programs using it dynamically will benefit from the correction after restarting them. Programs that included this function by static linking would have to be re-linked first.

Page 13: Loaders and Linkers

Disadvantages Disadvantages: Known on the Windows platform as "DLL Hell",

an incompatible updated DLL will break executables that depended on the behavior of the previous DLL.

A program, together with the libraries it uses, might be certified (e.g. as to correctness, documentation requirements, or performance) as a package, but not if components can be replaced.

Page 14: Loaders and Linkers

Definition of Loader Loader is utility program which takes object

code as input prepares it for execution and loads the executable code into the memory. Thus loader is actually responsible for initiating the execution process.

It is a SYSTEM PROGRAM that brings an executable file residing on disk into memory and starts it running.

Page 15: Loaders and Linkers

Functions of Loader The loader is responsible for the activities such as

allocation, linking , relocation and loading:1) It allocates the space for program in the memory, by

calculating the size of the program. This activity is called allocation.

2) It resolves the symbolic references (code/data) between the object modules by assigning all the user subroutine and library subroutine addresses. This activity is called linking.

3) There are some address dependent locations in the program, such address constants must be adjusted according to allocated space, such activity done by loader is called relocation.

Page 16: Loaders and Linkers

Functions of Loader

4) Finally it places all the machine instructions and data of corresponding programs and subroutines into the memory. Thus program now becomes ready for execution, this activity is called loading.

Steps:1. Read executable file’s header to determine the

size of text and data segments. 2. Create a new address space for the program.

Copies instructions and data into address space. Copies arguments passed to the program on the stack. Initializes the machine registers including the stack pointer.

3. Jumps to a startup routine that copies the program’s arguments from the stack to registers and calls the program’s main routine.

Page 17: Loaders and Linkers

Types of Loaders Compile/Assemble and Go loader:

In compile and go loader is a link editor/program loader in which the assembler itself places the assembled instruction directly into the designated memory locations for execution.

General Scheme Loader: In this loader scheme, the source program is converted to object program by some translator (assembler). The loader accepts these object modules and puts machine instruction and data in an executable form at their assigned memory. The loader occupies some portion of main memory.

Page 18: Loaders and Linkers

Types of Loaders Absolute Loader: An absolute loader is simple loader which

performs loading process and need not to perform linking and program relocation processes.

Relocating Loader : When a single subroutine is changed then all the subroutines need to be reassembled. The task of allocation and linking must be carried out once again. To avoid this rework a new class of loaders is introduced which is called relocating loader.

Direct Linking Loader: A Direct linking loader is a general relocating loader and is the most popular loading scheme presently used. This scheme has an advantage that it allows the programmer to use multiple procedure and multiple data segments.