unit-iii-loader & linker - sri eshwar

Download Unit-III-Loader & Linker - Sri Eshwar

If you can't read please download the document

Upload: veningstonk-ageesh

Post on 26-May-2017

231 views

Category:

Documents


0 download

TRANSCRIPT

  • System SoftwareLoaders & Linkers

    Veningston .KDepartment of CSE

    Government College of Technology, [email protected]

    mailto:[email protected]
  • OutlineLoaders & Linkers

    Basic loader functions Design of an Absolute Loader A Simple Bootstrap Loader Machine dependent loader features

    Program Relocation Program Linking Algorithm and Data Structures for Linking Loader

    Machine-independent loader features Automatic Library Search Loader Options

    Loader design options Linkage Editors Dynamic Linking Bootstrap Loaders

    24/August/2013 2System Software - Sri Eshwar College of

    Engineering

  • Recap [1/3]Machine architectures

    Simplified Instructional Computer (SIC)

    SIC/XE [eXtra Equipment]

    AssemblerTranslate mnemonic operation codes to their machine language equivalents

    Assign machine addresses to symbolic labels used by the programmer

    Write the object program

    24/August/2013 3System Software - Sri Eshwar College of

    Engineering

  • Recap [2/3]Format of Object program

    24/August/2013 4System Software - Sri Eshwar College of

    Engineering

  • Recap [3/3]

    Object program contains translated instructions and data values from the source program, and specifies addresses in memory where these items are to be loaded

    24/August/2013 5System Software - Sri Eshwar College of

    Engineering

  • Basic loader functionsLoading

    Brings object program into memory

    RelocationModifies the object program so that it can be loaded at an address different from the location originally specified

    LinkingCombines two or more separate object programs and supplies information needed to allow references between them

    may be a single system programLoader: loading and relocation

    Linker: linking Linking Loader

    24/August/2013 6System Software - Sri Eshwar College of

    Engineering

  • Absolute loader

    No linking and relocation neededAll functions are accomplished in a single passRecords in object program perform

    Header recordCheck the Header record for program name, starting address, and length (available memory)

    Text recordBring the object program contained in the Text record to the indicated address

    End recordTransfer control to the address specified in the End record to begin execution of the loaded program

    24/August/2013 7System Software - Sri Eshwar College of

    Engineering

  • Loading of an absolute program [1/2]

    Object program

    24/August/2013 8System Software - Sri Eshwar College of

    Engineering

  • Loading of an absolute program [2/2]Program loaded in memory

    No text recordthese locations remain unchanged

    24/August/2013 9System Software - Sri Eshwar College of

    Engineering

  • Algorithm for an absolute loader

    24/August/2013 10System Software - Sri Eshwar College of

    Engineering

  • Object Code Representation

    Character formEach byte of assembled code is given using its hexadecimal representation in character form

    Easy to read by human beings

    Inefficient in terms of space and execution time

    Binary formEach byte of object code is stored as a single byte in the object program

    Most machine store object programs in a binary formLess space and loading time

    Not good for reading

    24/August/2013 11System Software - Sri Eshwar College of

    Engineering

  • A simple bootstrap loader

    Bootstrap Loader (usually in ROM)When a computer is first tuned on or restarted, a special type of absolute loader, the bootstrap loader loads the first program (usually O.S.) to be run into memory

    SIC bootstrap loaderThe bootstrap itself begins at address 0It loads the OS starting address 0x80No header record, end record or control informationThe object code is loaded into consecutive bytes of memory starting at address 80After loading the OS, the control is transferred to the instruction at address 80.Begins the execution of the program

    24/August/2013System Software - Sri Eshwar College of

    Engineering12

  • SIC bootstrap loader

    24/August/2013System Software - Sri Eshwar College of

    Engineering13

  • Algorithm for SIC/XE bootstrap loader

    24/August/2013System Software - Sri Eshwar College of

    Engineering14

  • Machine-Dependent Loader Feature

    Merits of an absolute loaderSimple and efficient

    limitation of an absolute loaderProgrammer needs to specify the actual address at which it will be loaded into memory.It is difficult to run several programs concurrently, sharing memory between them.It is difficult to use subroutine libraries efficiently.

    SolutionA more complex loader that provides

    Program relocationProgram linking

    24/August/2013System Software - Sri Eshwar College of

    Engineering15

  • (Recap) Program relocation

    Why?

    It is desirable to load and run several programs at the same time

    The system must be able to load programs into memory wherever there is room

    The exact starting address of the program is not known until load time

    24/August/2013System Software - Sri Eshwar College of

    Engineering16

    It is not practical to plan program execution (we do not know exactly when jobs will be submitted, exactly how long they will run)

  • (Recap) Example of program relocation

    24/August/2013System Software - Sri Eshwar College of

    Engineering17

  • (Recap) Program relocation

    Absolute program

    Program with starting address specified at assembly time

    The address may be invalid if the program is loaded into somewhere else.

    Example

    24/August/2013System Software - Sri Eshwar College of

    Engineering18

  • (Recap) Program relocation

    The only parts of the program that requiremodification at load time are those that specify direct addressesThe rest of the instructions need not be modified

    Not a memory address (immediate addressing)PC-relative, Base-relative

    From the object program, it is not possible to distinguish the address and constant

    The assembler must keep some information to tell the loaderThe object program that contains the modification record is called a relocatable program

    24/August/2013System Software - Sri Eshwar College of

    Engineering19

  • (Recap) The way to solve the relocation problem

    For an address label, its address is assigned relative to the start of the program (START 0)

    Produce a Modification record to store the starting location and the length of the address field to be modified.

    The command for the loader must also be a part of the object program

    24/August/2013System Software - Sri Eshwar College of

    Engineering20

  • (Recap) Modification record

    One modification record for each address to be modified

    The length is stored in half-bytes (4 bits)

    The starting location is the location of the byte containing the leftmost bits of the address field to be modified.

    If the field contains an odd number of half-bytes, the starting location begins in the middle of the first byte.

    24/August/2013System Software - Sri Eshwar College of

    Engineering21

  • (Recap) Modification record

    24/August/2013System Software - Sri Eshwar College of

    Engineering22

  • (Recap) Relocatable Object Program

    24/August/2013System Software - Sri Eshwar College of

    Engineering23

  • Relocation

    Loaders that allow for program relocation are called relocating loaders or relative loaders.

    Efficient sharing of the machine requires that we write relocatable programs instead of absolute ones.

    The way relocationis implemented in a loader is also dependent upon machine characteristics.

    Linkingis not a machine-dependent function

    24/August/2013System Software - Sri Eshwar College of

    Engineering24

  • Relocation

    Two methods for specifying relocation as part of the object programModification records

    For a small number of relocations required when relative or immediate addressing modes are extensively used

    Relocation bitsFor a large number of relocations required when only direct addressing mode can be used in a machine with fixed instruction format (e.g., the standard SIC machine)

    24/August/2013System Software - Sri Eshwar College of

    Engineering25

  • Object program with relocation by modification records

    24/August/2013System Software - Sri Eshwar College of

    Engineering26

  • Relocatable program for SIC

    24/August/2013System Software - Sri Eshwar College of

    Engineering27

  • Relocatable program for SIC

    24/August/2013System Software - Sri Eshwar College of

    Engineering28

  • Relocatable program for SIC

    24/August/2013System Software - Sri Eshwar College of

    Engineering29

  • Review on Relocatable program for SIC

    The standard SIC machine does not use relative addressing (PC-relative, Base-relative)

    All instructions except RSUB in this program need relocation [require 31 Modification records]

    The modification record scheme is a convenient means for specifying program relocation; however, it is not well suited for use with all machine architectures

    24/August/2013System Software - Sri Eshwar College of

    Engineering30

    Too many modification records

  • Relocation Bits

    If there are many addresses needed to be modified, it is more efficient to use a relocation bit, instead of a Modification record, to specify every relocation.

    When the instruction format is fixed

    There is a relocation bit for each word of the object program

    Relocation bits are put together into a bit mask

    If the relocation bit corresponding to a word of object code is set to 1, the added to this word when the program is relocated

    24/August/2013System Software - Sri Eshwar College of

    Engineering31

  • Relocation Bits

    24/August/2013System Software - Sri Eshwar College of

    Engineering32

  • Relocation Bits

    A bit value of 0 indicates that no modification is necessary [e.g. data constants and JSUB]

    If a text record contains fewer than 12 words of object code, the bits corresponding to unused words are set to 0

    A new Text record is created for proper alignment

    Relocation bits are generated by the Assembler and used by the Loader.

    24/August/2013System Software - Sri Eshwar College of

    Engineering33

  • Program linking

    Goal

    Resolve the problems with EXTREF and EXTDEF from different control sections

    A program is a logical entity that combines all of the related control sections.

    Control sections could be assembled together, or they could be assembled independently of one another.

    Control sections are to be linked, relocated, and loaded by loaders.

    24/August/2013System Software - Sri Eshwar College of

    Engineering34

  • (Recap)Expressionthe use of expressions as operand

    The assembler evaluates the expressions and produces a single operand address or valueExpressions consist of

    Operator+,-,*,/ (division is usually defined to produce an integer result)

    Individual termsConstantsUser-defined symbolsSpecial terms, e.g. *, the current value of LOCCTR

    ExamplesMAXLEN EQU BUFEND-BUFFERSTAB RESB (6+3+2)*MAXENTRIES

    24/August/2013System Software - Sri Eshwar College of

    Engineering35

  • (Recap)Relocation Problem in ExpressionsValues of terms can be

    Absolute (independent of program location)Constants

    Relative (to the beginning of the program)Address labels* (value of LOCCTR)

    Expressions can beAbsolute

    Only absolute termsMAXLEN EQU 1000

    Relative terms in pairs with opposite signs for each Pair [independent on program starting address]

    MAXLEN EQU BUFEND-BUFFER

    RelativeAll the relative terms except one can be paired as described in

    unpaired relative term must have a positive sign.

    STAB EQU OPTAB + (BUFEND BUFFER)

    24/August/2013System Software - Sri Eshwar College of

    Engineering36

  • (Recap) Program blocks vs. Control sections

    Program blocks (Assembler directive: USE)

    Segments of code that are rearrangedwithin a single object program unit

    Control sections(Assembler directive: CSECT)

    Segments of code that are translated into independent object program units

    24/August/2013System Software - Sri Eshwar College of

    Engineering37

  • (Recap) Control Sections and Program Linking

    Control sectionscan be loaded and relocated independently of the other control sections

    are most often used for subroutines or other logical subdivisions of a program

    the programmer can assemble, load, and manipulate each of these control sections separately

    because of this, there should be some means for linking control sections together

    24/August/2013System Software - Sri Eshwar College of

    Engineering38

  • (Recap) External Definition and Reference

    Instructions in one control section may need to refer to instructions or data located in another sectionExternal definition

    EXTDEF name [, name]EXTDEF names symbols that are defined in this control section and may be used by other sectionsE.g. EXTDEF BUFFER, BUFEND, LENGTH

    External referenceEXTREFname [,name]

    EXTREF names symbols that are used in this control section and are defined elsewhereE.g. EXTREF RDREC, WRREC

    To reference an external symbol, extended format instruction is needed

    24/August/2013System Software - Sri Eshwar College of

    Engineering39

  • (Recap) Records for Object Program

    The assembler must include information in the object program that will cause the loader to insert proper values where they are required

    24/August/2013System Software - Sri Eshwar College of

    Engineering40

  • (Recap) Records for Object Program

    24/August/2013System Software - Sri Eshwar College of

    Engineering41

  • Program linking

    Example

    Program for Linking and Relocation

    Use modification records for both relocation and linking

    address constant

    external reference

    24/August/2013System Software - Sri Eshwar College of

    Engineering42

  • Program for Linking and Relocation [1/3]

    24/August/2013System Software - Sri Eshwar College of

    Engineering43

  • Program for Linking and Relocation [2/3]

    24/August/2013System Software - Sri Eshwar College of

    Engineering44

  • Program for Linking and Relocation [3/3]

    24/August/2013System Software - Sri Eshwar College of

    Engineering45

  • About the assembly program

    3 control sections: PROGA, PROGB, PROGC

    List of items:LISTA, LISTB, LISTC

    Set of references to external symbols

    Instruction operands: REF1, REF2, REF3

    Values of data words:REF4 through REF8

    These programs are given to emphasize the relationship between the relocation and linking process.

    24/August/2013System Software - Sri Eshwar College of

    Engineering46

  • From the loader point of view

    Programmer think of a program as a logical entity that combines all of the related control sections

    However, from the loader point of view, there is no such thing as a program. There are only control sections that are to be linked, relocated, and loaded.

    The loader do not need to knowwhich control sections were assembled at the same time

    24/August/2013System Software - Sri Eshwar College of

    Engineering47

  • Object programs [1/3]

    24/August/2013System Software - Sri Eshwar College of

    Engineering48

  • Object programs [2/3]

    24/August/2013System Software - Sri Eshwar College of

    Engineering49

  • Object programs [3/3]

    24/August/2013System Software - Sri Eshwar College of

    Engineering50

  • About the object program

    Local reference is assembled using PC-relative instruction with no relocation or linking required

    Modification record instructing the loader to add the value of the external symbol to this address field when the program is linked

    24/August/2013System Software - Sri Eshwar College of

    Engineering51

  • Note

    The general approach taken is for the assembler to evaluate as much of the expression as it can

    The remaining terms are passed on to the loader via modification records

    24/August/2013System Software - Sri Eshwar College of

    Engineering52

  • Example - 1

    Consider REF4, the assembler for PROGAcan evaluate all of the expression in REF4except for the value of LISTC.

    This results in an initial value of 000014and one Modification record

    24/August/2013System Software - Sri Eshwar College of

    Engineering53

  • Example - 2

    Consider REF4, in PROGBcontains no terms that can be evaluated by the assembler

    The object code therefore contains an initial value of 000000and three Modification record

    24/August/2013System Software - Sri Eshwar College of

    Engineering54

  • Example - 3

    Consider REF4, for PROGCassembler can supply the value of LISTCrelative to the beginning or the program (but not the actual address which is not known until the program is loaded)

    The initial value of this data word contains the relative address of LISTC000030

    Modification records instruct the loader to add the beginning address of the program (i.e. the value of PROGC), to add the value of ENDA, and to subtract the value of LISTA

    24/August/2013System Software - Sri Eshwar College of

    Engineering55

  • Inference from the example 1, 2 & 3

    The expression in REF4represents

    a simple external reference for PROGA

    A more complicated external reference for PROGB

    A combination of relocation and external references for PROGC

    24/August/2013System Software - Sri Eshwar College of

    Engineering56

  • Exercise

    To work through references REF5through REF8by yourself to be sure that you have understood how the object code and modification records are generated

    24/August/2013System Software - Sri Eshwar College of

    Engineering57

  • Programs after linking and loading

    24/August/2013System Software - Sri Eshwar College of

    Engineering58

  • Inference from the previous Figure

    Note that each of REF4through REF8has resulted (after relocation and linking is performed) in the same value in each of the three programs, Since the same source expression appeared in each program

    24/August/2013System Software - Sri Eshwar College of

    Engineering59

  • Example

    The value for reference REF4in PROGAis located at address 4054(the beginning address of PROGA+ 0054, the relative address of REF4within PROGA)

    Next slide shows how this value is computed?

    24/August/2013System Software - Sri Eshwar College of

    Engineering60

  • Relocation and linking operationsperformed on REF4

    24/August/2013System Software - Sri Eshwar College of

    Engineering61

  • Note

    For the reference that are instruction operands, the calculated values after loading do not always appear to be equal

    This is because there is an additional address calculation step involved for PC-relativeor Base-relativeinstructions.

    24/August/2013System Software - Sri Eshwar College of

    Engineering62

  • Algorithm and data structurefor a linking loader

    Programmer do not need to worry about how these calculations are actually performed by the loader

    The algorithm and data structures does

    24/August/2013System Software - Sri Eshwar College of

    Engineering63

  • Algorithm and data structurefor a linking loader

    Considerations by SIC/XE:

    Most instructions uses relative addressing; no relocation is necessary

    Modification records are used in this type of machine

    A linking loader usually makes two passes over its input

    Because some external symbols are processed before read

    24/August/2013System Software - Sri Eshwar College of

    Engineering64

  • Two passes linking loader

    Two Passes Logic

    Pass 1: assign addresses to all external symbols

    Pass 2: perform the actual loading, relocation, and linking

    24/August/2013System Software - Sri Eshwar College of

    Engineering65

  • Linking loader - Pass 1 [1/3]

    Assign address to all external symbolsOnly processes Header Record and Define RecordBuilds an external symbol table (ESTTAB)

    its nameits addressin which control section the symbol is defined

    Program Load Address (PROGADDR)The beginning address in memory where the linked program is to be loaded (supplied by OS).

    Control Section Address (CSADDR)The starting address assigned to the control section currently being scanned by the loader.CSADDR is added to all relative addresses within the control section

    24/August/2013System Software - Sri Eshwar College of

    Engineering66

  • Linking loader - Pass 1 [2/3]

    Add symbol to ESTAB

    Control section name: (name, CSADDR) ESTAB

    Get control section name from H record

    If the first control sectionCSADDR = PROGADDR

    When E record is encountered, read the next control section

    CSADDR = CSADDR + CSLTH (known from H record)

    EXTDEF: (name, CSADDR + value in the record) ESTAB

    Get EXTDEF from D record

    24/August/2013System Software - Sri Eshwar College of

    Engineering67

  • Linking loader - Pass 1 [3/3]

    At the end of Pass 1: ESTAB contains all external symbols defined in the set of control sections together with the address assigned to each

    Print the load map if necessary (optional) useful in program debugging

    24/August/2013System Software - Sri Eshwar College of

    Engineering68

  • Linking loader - Pass 1 algorithm

    24/August/2013System Software - Sri Eshwar College of

    Engineering69

  • Linking loader - Pass 2Perform the actual loading, relocation, and linking

    Only processes Text Record and Modification RecordGet address of external symbol from ESTABWhen read T record

    Moving object code to the specified address

    When read M record(+/-) EXTREF in M to be used for modification is looked up in ESTABThis value is added/subtracted from the indicated location in memory

    Last step: transfer control to the address in EIf more than one control section specifies a transfer address: loader arbitrarily uses the last oneIf no control section contains a transfer address: transfer control to the first instruction (PROGADDR)

    24/August/2013System Software - Sri Eshwar College of

    Engineering70

  • Linking loader - Pass 2 algorithm

    24/August/2013System Software - Sri Eshwar College of

    Engineering71

  • Linking loader To improve efficiency

    We can make the linking loader algorithm more efficient by

    Assigning a reference number to each external symbol referred to in a control section

    01: control section name02~: external reference symbols

    This reference number is used (instead of the symbol name) in Modification recordsAvoids multiple searches of ESTABfor the same symbol during the loading of a control section.

    Search of ESTAB for each external symbol can be performed once and the result is stored in a table indexed by the reference number.The values for code modification can then be obtained by simply indexing into the table.

    24/August/2013System Software - Sri Eshwar College of

    Engineering72

  • Examples of Using Reference Numbers [1/3]

    24/August/2013System Software - Sri Eshwar College of

    Engineering73

  • Examples of Using Reference Numbers [2/3]

    24/August/2013System Software - Sri Eshwar College of

    Engineering74

    The reference numbers are underlined in the Refer and modification records

  • Examples of Using Reference Numbers [3/3]

    24/August/2013System Software - Sri Eshwar College of

    Engineering75