Loaders & Linkers. Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
Loaders Dr. Monther Aldwairi 10/21/2007 Dr. Monther Aldwairi.
1 Loaders and Linkers Chapter 3 System Software An introduction to systems programming Leland L. Beck.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
Chapter 3 Loaders and Linkers
Linkage Editors Difference between a linkage editor and a linking loader: Linking loader performs all linking and relocation operations, including automatic.
3. Loaders & Linkers1 Chapter III: Loaders and Linkers Chapter goal: r To realize how a source program be loaded into memory m Loading m Relocation m Linking.
Chapter 3 Loaders and Linkers
Chapter 3 Loaders and Linkers. Purpose and Function Places object program in memory Linking – Combines 2 or more obj programs Relocation – Allows loading.
Loaders and Linkers CS 230 이준원. 2 Overview assembler –generates an object code in a predefined format »COFF (common object file format) »ELF (executable.
SYSTEM PROGRAMMING & SYSTEM ADMINISTRATION
Mr. D. J. Patel, AITS, Rajkot 1 Operating Systems, by Dhananjay Dhamdhere1 Static and Dynamic Memory Allocation Memory allocation is an aspect of a more.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Loaders and Linkers Object Program contains the information:
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
W EEK S IX O PERATING S YSTEM S TRUCTURE. T HE C ONCEPT OF L OADING AND L INKING Loader – Loader is a utility program which takes object code as input,
Memory Management Chapter 7.
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
1 Machine-Independent Features Automatic Library Search automatically incorporate routines from a subprogram library Loading Options.
Loader- Machine Independent Loader Features
CS 104 Introduction to Computer Science and Graphics Problems
Memory Management 2010.
An introduction to systems programming
1 Chapter 3 Loaders and Linkers Source Program Assembler Object Code Loader Executable Code Linker.
A. Frank - P. Weisberg Operating Systems Real Memory Management.
Topic 1: Introduction to Computers and Programming
03/05/2008CSCI 315 Operating Systems Design1 Memory Management Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Software Development and Software Loading in Embedded Systems.
Chapter 91 Memory Management Chapter 9   Review of process from source to executable (linking, loading, addressing)   General discussion of memory.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
MIPS coding. SPIM Some links can be found such as:
Chapter 4 Storage Management (Memory Management).
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
CS6502 Operating Systems - Dr. J. Garrido Memory Management – Part 1 Class Will Start Momentarily… Lecture 8b CS6502 Operating Systems Dr. Jose M. Garrido.
Loader and Linker.
LINKERS Execution of a program written in a language L involves the following steps: 1.Translation of the program: Performed by the translator for language.
The Assembly Process Computer Organization and Assembly Language: Module 10.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 10 – Loaders.
Loaders and Linkers T 李俊葦. 1. Loader Accepts the object programs , prepares these programs for execution by the computer , and indicates the execution.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
THE SOFTWARE Computers need clear-cut instructions to tell them what to do, how to do, and when to do. A set of instructions to carry out these functions.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 10 –Linkers.
Lecture 3 Translation.
Machine dependent Assembler Features
Computer Science 210 Computer Organization
System Programming and administration
Linking & Loading.
Chapter 8 Main Memory.
CS-3013 Operating Systems C-term 2008
Computer Science 210 Computer Organization
Chapter 11: File System Implementation
Loaders and Linkers: Features
Machine Independent Features
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Loaders and Linkers.
Memory Management Tasks
Computer Organization and Design Assembly & Compilation
Linking & Loading CS-502 Operating Systems
Chapter 1 Computer architecture Languages: machine, assembly, high
Linking & Loading CS-502 Operating Systems
An introduction to systems programming
Chapter 3 Loaders and Linkers
CSE 542: Operating Systems
Presentation transcript:

Loaders & Linkers

Introduction A “Loader” is a program which accepts the object program as input, makes them executable by the computer and initiates execution.

object file In computer science, object code, or an object file, is the representation of code that a compiler or assembler generates by processing a source code file.computer sciencecompilerassemblersource code Object files contain compact code, often called "binaries". A linker is typically used to generate an executable file by linking object files together. The only essential element in an object file is machine codelinkerexecutablemachine code Object files often also contain data for use by the code at runtime, relocation information, program symbols (names of variables and functions) for linking and/or debugging purposes, and other debugging information.relocationsymbolsdebugging

Basic Functions of the Loader ALLOCATION: Allocates space in memory for the programs( calculation program size) RELOCATION: Adjustment of addresses of all address sensitive entities. LINKING:Resolve inter-segment (inter-program) symbolic reference in object program. LOADING: Physically place the machine instructions and data into memory and initiate execution.

Linking In general,a program can contain : – External definitions – Public definitions Linker is the system software which performs : –Resolution of inter-segment symbolic references which is called as “Linking”. –Establish correspondence between external definitions and public definitions –To determine relocatability of symbols attributes. –Substitute relocatable addresses.

Functions of a Linker Establish communication between external symbol references and definitions and public definitions To determine the relocatability of symbol attributes and expressions. To substitute for these relocatable addresses.

In actual practice, a complete program is built from many smaller routines possibly by many people. All these routines have to be connected logically and linked to form a single program. A linker is a systems program that accounts for and reconciles all address references within and among modules and replaces those references with a single consistent scheme of relative addresses. Linking is done after the code is generated and is closely associated with a loader.

Compilers and translators basically translate one procedure at a time and put the translated output on the disk. All the translated procedures have to be located and linked together to be run as a unit called an executable binary program. In MS-DOS, Windows 95/98 etc object modules have extension.obj and the executable binary programs have.exe extension. In UNIX, object modules have.o extension and executable programs have no extension

Loading schemes 1.Assemble-and-go loader: The assembler simply places the code into memory and the loader executes a single instruction that transfers control to the starting instruction of the assembled program. 2.In this scheme, some portion of the memory is used by the assembler itself which would otherwise have been available for the object program.

Compile and Go Loader Assembling/compiling, linking, loading etc. goes in one shot. programmer or assembler must know the starting address in memory to load the program. External references of any kind are not allowed in this loader scheme.

Compile and go Loader The assembler runs in one part of the memory and place the assembled machine instructions and data as they are assembled directly into their assigned memory locations. When assembly is completed, the assembler causes a transfer to the starting instruction of the program. Assemble and Go Loader Code for Execution In main memory Source Program.ASM

Algorithm 1.Read pgm. Line by line till EOF, if EOF goto step 4 2.Analyze statement, find M/c code and dump it to memory at pre known location 3.Increment memory location counter appropriately and goto step 1 4.Load PC with starting address and issue EXEC call with the help of OS.

Compile and go loader Advantages: It is very simple to understand. It is simple to implement because loader is just an extension to the assembler design.

Disadvantages: Portion of the memory is wasted because the translator resides in the memory. It is necessary to assemble every time even though no modifications are being done. Can not handle multiple program segments especially if they are written in different languages. Scheme is good for small program not for bigger one Execution time = assemble time + load time

Absolute Loader Here assembler produces absolute object code which can be stored in secondary storage for use in future. No relocation information is needed, which saves space. Locations are being absolute, either programmer or assembler must know the memory management issue. Linking of different modules which are inter dependent is done by programmer. In short: Allocation and linking is by programmer Resolution is by assembler Loading is done by loader.

Absolute Loader MAIN SQRT Absolute Loader SQRT MAIN Absolute Loader Main Memory ~ ~ ~ ~

Operation The loader simply accepts the machine language and places it into the core as prescribe by the assembler. The loader then initiates execution by transferring control to the starting of the program.

Segment 1 Segment 2 Segment N AssemblerObject 1 length Start Address Absolute Loader Main Memory Object 2 length Start Address Object N length Start Address ~~ General Schematic of Absolute Loader.

Algorithm of an Absolute Loader. INPUT:Object modules consisting of machine code and data,starting address and length. OUTPUT:Executable code to be placed in the main memory. Begin for each program module begin 1) read the header to get the information about starting address and length. 2)mem_loc=starting addrs. 3)present_byte_counter=1 4)max_byte_counter=length while(present _byte_counter<max_byte_counter) for current object code do begin 1)read present_byte_conter byte 2)put it at location mem_locn 3)increment mem_locn & increment present_byte_counter end;

Absolute loader. Advantages: More core is available to the user since the translator is not in the memory. No relocation info. Required so size of object module will be small. Absolute loader are simple to implement. Loader is being very small, it eats up very small region of memory Execution time = load time It is fast and efficient

Disadvantages: Programmer must specify the starting address of each module so programmer must know memory management as well as memory status at any time.

Relocation Linker will generally performs this activity by using relocation information, length, machine code and data from object file The.EXE file contains : –machine code and data w.r.t zero address –Relocation information –Length of file

Relocation In computer science, relocation is the process of replacing symbolic references or names of libraries with actual usable addresses in memory before running a program.computer sciencelibrariesmemory It is typically done by the linker during compilation (at compile time), although it can be done at runtime by a loaderlinker compilationcompile timeruntimeloader

Relocation table The relocation table is a list of pointers created by the linker and linked into the MSDOS.exe file.pointerslinker The relocation table tells the loader where these segments can be found in program image loaded into memory.memory

Ways to put Relocation information 1.Put relocation information after every byte of machine code and data 2.Put the relocation information at the end of object code.

Opcode operand Load A Add B Store C Stop Endp A DB 5 B DB 3 C DW ? Machine code xx Relocation bits Way1 to put relocation information Second way to put relocation information

Program Relocation Machine code w.r.t xx xx Machine code w.r.t xx xx

Program Relocatability Relocatable program Non- Relocatable Programs Self Relocatable Programs

Non- Relocatable Programs This type of program can not be executed in any area of memory other than one distinguished for it at the time of its coding.

Relocatable Programs Using relocation information, it is possible to relocate the program to execute from the area other than one which is distinguished for it at the time of translation. The agent which performs such relocation is called as Linkage Editors or Relocating Loaders

Self- Relocatable Programs This is the program which can itself perform the relocation of its address sensitive portions of the code.

Relocating loader 3. Relocating loader: This loader allows multiple procedure segments and one data segment which is common to all assembler assembles each segment independently and passes on to loader the following info: M/C code relocation information length of program inter segment reference

For each program, Assembler produces m/c code prefixed by “ Transfer vector “ Transfer vector consists of names of subroutines referenced by source program Transfer vector is kind of table that contains info. About external subroutines and their addresses which may be filled by loader. Transfer vector is a global table for object file.

Entities and their usage in R L Relocation bits : to solve the problem of relocation. Transfer vector: to solve problem of linking. Program length : to solve the problem of allocation.

RELOCATING LOADER ADVANTAGES: The relocating loader takes care of the program modification dynamically ( i.e change in program size) Using the transfer vectors,the required sub- routines can be brought into the main memory rather than keeping all routines in the memory all the time. DISADVANTAGES: Transfer vector linkage is useful only for subroutines but not for loading or storing external data. If Transfer vector itself increases, the size of object program in memory increases, as Transfer Vector has to be there in memory all the time

Linking loader vs. Linkage Editor Object program(s) Linking loader Memory Library Object program(s) Linkage editor Library Linked program Simple Loader Memory A linkage editor produces a linked version of the program (load module), which is written to a file or library for later execution.

Linkage editors Advantages: –One linking and the most of relocation for many executions. –Possibility to create subroutines libraries. –Actual loading is very simple. Disadvantages: –recompiling for nearly every execution (program development). - infrequently used programs (storage waste).

Direct linking loader: It belongs to the category of Re-locatable Loader. It is most widely used loader. It allows the programmer to use multiple procedure segments and multiple data segments. References to non local data and subroutine is allowed.

For Direct Linking Loader, assembler must provide: Length of each and every program segment PUBLIC TABLE which defines Name of the Symbol & address of it’s definition. EXTERN TABLE which defines Name of the symbol and address of its Usage.

Program 1 prog 1: public x public yMACHINE EQUIVALENT Ext A Ext Bafter pass1after pass2 | load A mult x add y | store B | store A endp x db ? 0050 xx0050 xx y db ? 0051 xx0051 xx

Public table for program 1 Name of symbolAddress of its definition x 0050 Y 0051

Extern table for program 1 Name of the symbolAddress of its usage A 0001,0041 B 0021

Program 2: Public A Public B Ext x Ext y | After pass 1 After pass 2 load A mult x add y | store B | endp store x – A db ? 0037 xx xx B db? 0038 xx 0038 xx end

Public table for program 2 Name of symbolAddress of its definition A 0037 B 0038

Extern table of program 2 Name of the symbolAddress of its usage X 0003,0036 Y 0005

Assume that the object code of pgm 2 will be loaded at the end of pgm1. Hence all relative addresses in the segment are to be adjusted by length of first program.( which is 52)

Modified pub table for program 2 Name of symbolAddress of its definition A 0089 B 0090

Modified ext table for program 2 Name of symbolAddress of its definition x 55,88 y 57

Use public table of program 2 with extern table of program 1 and vice versa to resolve everything

DLL : Algorithm( Pass 1) Purpose :To build global extern symbol table( GST) 1.Initialize ST, STPTR, LC 2.Get name of procedure from user and store it into ST. 3.Search for obj file, if not found, report “error” and stop. 4.Input record and determine its type. Case 1: procedure name 1. save module size 2. mark proc as found in GST. 3. STPTR++ 4. goto step 4

Case 2 : Public definition table 1. enter global data area name into ST and mark as found. 2. add offset to LC of each global variable. 3. STPTR++ 4. Goto step 4 Case 3 : Extern table 1.if extern symbols are not in ST enter them and mark them as not found 2. GOTO step 4. Case 4: End of module 1. Add the size of module to LC 2. if all entries in ST are marked ”FOUND” goto pass 2 3. else set STP to next entry with no address and goto step 3

DLL : Algorithm( Pass 2) Purpose :To generate load module Step 1 : initialize STPTR and LC Step 2: fetch object module pointed to in ST from file to memory Step 3: update all external reference using using public table and extern table. Step 4 : move all code and data records to load module and update STPTR and LC Step 5: if no more ST entry then stop else GOTO step 2.

Dynamic Linking Dynamic linking (dynamic loading, load on call) postpones the linking function until execution time; a subroutine is loaded and linked to the rest of the program when it first called.

Dynamic Linking Advantages: –Several executing programs can share one copy of a subroutine or library (Xwindows, C support routines). –The implementation of the method can be determined or even changed at the time the program is run. –The subroutine name might simply be treated as an input item (!) Additional advantages: –Applications with rarely used subroutines (f.e. errors handling) –Application with a lot of possible services (f.e. mathematic packages)

Dynamic loader (OS) User program ERRHANDL Library Dynamic loader (OS) User program ERRHANDL Dynamic loader (OS) User program ERRHANDL Dynamic loader (OS) User program ERRHANDL Load-and-call ERRHANDL Dynamic loader (OS) User program Load-and-call ERRHANDL

Bootstrap Loaders This absolute loader (bootstrap loader) is a system software, resident in ROM or “ primary loaded ” by some hardware function.