Anúncio

Linker and Loader

25 de Feb de 2021
Anúncio

Mais conteúdo relacionado

Anúncio

Linker and Loader

  1. 1
  2. 2 Introduction Linker Linker is a program that takes one or more objects generated by a compiler and combines/assembles them into a single executable program. Source program Executable program Object modules Loader Loader is a program that is responsible for loading programs from executables into memory, preparing them for execution and then executing them. Source program Executable program The execution of a program involves the following steps: Step 1: Translation Step 2: Linking Step 3: Relocation Step 4: Loading Compiler Linker Translator Linker Loader Memory Output Source Program Object modules Binary Programs Data Loader
  3. 3 Concept of Binding Binding is an association between an attribute and an entity or between an operation and a symbol. The time at which a binding takes place is called binding time. Binding can take place at language design time, language implementation time, compile time, link time, or run time. There are two types of binding: 1. Static binding: A static binding is a building before the execution of program begins. It is also called early binding. 2. Dynamic binding: A dynamic binding is a binding performed after the execution of a program begins. It is also called late binding. Dynamic linking involves the following steps: 1) A reference to an external module during run time causes the loader to find the target module and load it. 2) Perform relocation during run time Dynamic linking permits a program to load and unload routines at run time. Binding Static Binding Dynamic Binding
  4. 4 Difference between Static and Dynamic Binding Static Binding Dynamic Binding 1. Binding between names and objects can be determined at compile time. 1. Binding between names and objects can be determined at run time. 2. Binding variable depends on where the procedure is written. 2. Binding depends on who calls the procedure. 3. Output does not differ at runtime. 3. Output differs at runtime. 4. Mostly, compiler is needed. 4. Interpreter is needed. 5. Program is more efficient. 5. Program is more flexible. 6. Example: C language 6. Example: LISP(List Programming)
  5. 5 Translator, Link and Load Time Address Following are some terminology which is very important to understand: 1. Translator: A translator translates a program and convert it into target code. It also allocates memory to all variables defined in the program. 2. Translation time address: Address assigned by the translator. 3. Linked address: Address assigned by the linker. 4. Load-time address: Address assigned by the loader. 5. Translated origin: Address of the origin assumed by the translator. This is the address specified by the programmer in an ORIGIN statement. 6. Liked origin: Address of the origin assumed by the linker while producing a binary program. 7. Load origin: Address of the origin assigned by the loader while loading the program for execution.
  6. Example of Translator, Link and Load Time Address The translated time origin of a program is 200. the translation time address of LOOP is 201. If the program is loaded for execution in the memory area starting with the address 500, then the load time origin is 500 and load time address of LOOP is 501.  Translation time origin is 200.  Translation time address of symbol A is 224.  Link address of A is 524.  Link address of LOOP becomes 501
  7. 7 Types of Loader Function of Loader 1. Loading - which allocates memory location and brings the object program into memory for execution - (Loader) 2. Linking- which combines two or more separate object programs and supplies the information needed to allow references between them - (Linker) 3. Relocation - which modifies the object program so that it can be loaded at an address different from the location originally specified - (Linking Loader) Based on the above four functions the loader is divided into different types, they are 1. Compile and go loader or Assemble and go loader 2. General loader scheme 3. Absolute loader 4. Direct linking loader 5. Relocating loader 6. Dynamic linking loader
  8. 8 1. Compile and Go Loader  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.  The instruction are read line by line, its machine code is obtained and it is directly put in the main memory at some known address.  After completion of assembly process, it assigns the starting address of the program to the location counter. The assembler is first executed it & when it is finished, causes a branch straight to the first instruction of the program.  There is no stop between the compilation, link editing, loading, and execution of the program. It is also called an assemble-and-go or a load and-go system. Advantages of Compile-and-go Loaders  They are simple and easier to implement.  No additional routines are required to load the compiled code into the memory. Disadvantages of Compile-and-go Loaders  Portion of memory is wasted because combination of assembler and loader activities, this combination program occupies large block of memory  There is no production of .obj file  It cannot handle multiple source programs or multiple programs written in different languages  The execution time will be more in this scheme as every time program is assembled and then executed
  9. 9 2. General Loader Scheme  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.  Generally the size of loader is less than that of assembler. Advantage of General Loader Scheme  In this scheme the source program translators produce compatible object program deck formats and it is possible to write subroutines in several different languages since the object decks to be processed by the loader will all be in the same “language” that is in “machine language”.  The program need not be retranslated each time while running it  There is no wastage of memory, because assembler is not placed in the memory  It is possible to write source program with multiple programs and multiple languages
  10. 10 3. Absolute Loader  Absolute loader is a kind of loader in which relocated object files are created, loader accepts these files and places them at specified locations in the memory. This type of loader is called absolute because no relocation information is needed; rather it is obtained from the programmer or assembler.  The starting address of every module is known to the programmer, this corresponding starting address is stored in the object file, then task of loader becomes very simple and that is to simply place the executable form of the machine instructions at the locations mentioned in the object file.  In this scheme the programmer or assembler should have knowledge of memory management. The resolution of external references or linking of different subroutines are the issues which need to be handled by the programmer.  Thus the absolute loader is simple to implement in this scheme 1. Allocation is done by either programmer or assembler 2. Linking is done by the programmer or assembler 3. Resolution is done by assembler 4. Simply loading is done by the loader Disadvantages  The programmer must specify to the assembler the address where the program is to be loaded.  If there are multiple subroutines , the programmer must remember the address of each.
  11. 11 4. Direct Linking Loader  The direct linking loader is the most common type of loader. The loader cannot have the direct access to the source code. The assembler should give the following information to the loader i. The length of the object code segment ii. The list of all the symbols which are not defined in the current segment but can be used in the current segment. iii. The list of all the symbols which are defined in the current segment but can be referred by the other segments.  The list of symbols which are not defined in the current segment but can be used in the current segment are stored in a data structure called USE table. The list of symbols which are defined in the current segment and can be referred by the other segments are stored in a data structure called DEFINITION table.  There are 4 types of cards available in the direct linking loader. They are 1. ESD-External symbol dictionary 2. TXT-card 3. RLD-Relocation and linking dictionary 4. END-card 1. ESD card: It contains information about all symbols that are defined in the program but reference somewhere, It contains:  Reference number  Symbol name  Type Id  Relative location  Length
  12. 12 2. TXT Card: It contains the actual information are text which are already translated. 3. RLD Card: This card contains information about location in the program whose contexts depends on the address at which the program is placed. In this we are used ‘+’ and ‘–‘sign, when we are using the ‘+’ sign then no need of relocation, when we are using ‘-‘sign relocation is necessary. The format of RLD contains: i. Reference number ii. Symbol iii. Flag iv. Length v. Relative location 4. END Card: It indicates end of the object program. Disadvantage  Allocate, relocate, link, and load all the subroutines each time in order to execute a program.  Furthermore, even though loader program smaller than assembler, it absorb considerable amount of memory.
  13. 13 5. Relocating Loader  To avoid possible assembling of all subroutines when a single subroutine is change and to perform the tasks of allocation and linking for the programmer, the general class of relocating loader was introduced. The output of a relocating loader is the program and information about all other programs it references. In addition, there is information (relocation information) as to location in this program that must be change if it is to be loaded in an arbitrary location in memory. In the given program X variable will be at 30. But after loading it will be loaded at 530. The address of variable ‘X’ can be found as given below: 500 + 30 = 530 Segment register offset actual address
  14. 14 6. Dynamic Linking Loader  Dynamic Linking Loader is a general re-locatable loader  Allowing the programmer multiple procedure segments and multiple data segments and giving programmer complete freedom in referencing data or instruction contained in other segments.  The assembler must give the loader the following information with each procedure or data segment.  Dynamic linking defers much of the linking process until a program starts running. It provides a variety of benefits that are hard to get otherwise.  Dynamically linked shared libraries are easier to create than static linked shared libraries.  Dynamically linked shared libraries are easier to update than static linked shared libraries.  The semantics of dynamically linked shared libraries can be much closer to those of unshared libraries.  Dynamic linking permits a program to load and unload routines at runtime, a facility that can otherwise be very difficult to provide.
  15. 15 The object module of a program contain all information necessary to relocate and link the program with other program. Object module of a program P consists of 4 components: 1. Header: contain translated origin, size & execution start address of P. 2. Program: contain machine language program corresponding to P. 3. Relocation table: each RELOCTAB contain a single field. Translated address: of an address sensitive instruction. 4. Linking table(LINKTAB): contain information about public definition and external references in P. Each LINKTAB entry contain three fields: 1. Symbol: symbolic name 2. Type: PD/ EXT indicating whether public or external. 3. Translated address: for public: add. of first memory word allocated to the symbol. for external: add. Of memory word which is required to contain address of the symbol. Object Module
  16. 16 Program can be classified into 3 categories: 1. Non-relocatable program 2. Relocatable program 3. Self-relocatable program 1. Non-relocatable program: It is a program which cannot be executed in any memory area other than the area starting on its translated origin. Non-relocatability is the result of address-sensitivity of the code and lack of information regarding: which part of the program are address sensitive and in which manner. Example: .com files on DOS 2. Relocatable program: It is the one which consists of a program and relevant information for its relocation. These program contains information of address sensitive instruction. At relocation of the program, the relocation factor is added to address part of the address sensitive instruction. This process is generally done by operating system and the output which perform relocation is called linkage editor relocating loader. Example: .exe files on DOS Program Relocatability Program + Data Program + Data ___________________ Relocation information
  17. 3. Self-relocatable program: Self-relocating program is a program which can perform the relocation of its own address sensitive instruction. It contains the followings: 1. A table of information about address sensitive instructions in the program. 2. Relocating logic that can perform the relocation of the address sensitive instructions  Self relocating contain the relocating logic that can perform the relocation of the address sensitive instructions. So there is no need of a linker in self relocating program.  It uses the load address and the information concerning address sensitive instruction to perform its own relocation. Commonly used compilers like TC, FORTRAN, COBOL in multiuser system contain code for self-relocation. These programs can execute in any area of the memory.  Thus, if the non-relocatable compiler is used then it is necessary to keep ‘n’ copies of compiler for each user. It requires large storage.  If relocatable compiler is used then it is necessary to load linkage editor each time and relocate the program each time when program is execute. Thus, even if storage for n copies is saved by retaining one copy only, the storage is required for , linkage editor. Program Relocatability Relocating Logic Program + Data Relocation Information
Anúncio