Linker and Loaders

Post on 28-Nov-2014

665 views 10 download

Transcript of Linker and Loaders

Linkers and Loader

• Loader is program which accepts the object programand prepares it for executions (Loads into Memory)

• Function of Loader1. Allocation Allocates space in Memory for Program2. Linking Resolves symbolic references between objects

codes3. Relocation Adjust all the address dependent locations4. Loading Physically place M/C instructions and data into

memory

• Loader is program which accepts the object programand prepares it for executions (Loads into Memory)

• Function of Loader1. Allocation Allocates space in Memory for Program2. Linking Resolves symbolic references between objects

codes3. Relocation Adjust all the address dependent locations4. Loading Physically place M/C instructions and data into

memory

Linkers and Loaders

Loading Schemes to be discussed

Compile / Assemble and go loader Absolute Loader Relocating Loaders Direct- Linking Loaders

Loading Schemes to be discussed

Compile / Assemble and go loader Absolute Loader Relocating Loaders Direct- Linking Loaders

Assemble and Go Loader

• Assembler runs in memory and loaded assembledM/C instructions and data as they are assembled.

• Directly placed into memory at assigned location.• At end, assembler executes transfer instruction to

transfer control of executions at first line of M/Ccode.

• Assembler runs in memory and loaded assembledM/C instructions and data as they are assembled.

• Directly placed into memory at assigned location.• At end, assembler executes transfer instruction to

transfer control of executions at first line of M/Ccode.

Assemble and Go Loader

• Advantages– Simple to design– Only one transfer instruction as a part of loader

• Disadvantages– Memory wasted for holding assembler– Necessary to re-translate code every time– Source program need to be in one language only.

• Advantages– Simple to design– Only one transfer instruction as a part of loader

• Disadvantages– Memory wasted for holding assembler– Necessary to re-translate code every time– Source program need to be in one language only.

Assemble and Go Loader

General Loader Scheme

• Object program is already created• Object code can be loaded whenever it is to be executed.• System program known as loader must be there in the

memory• It accepts all object programs and places them in

memory for execution.• Loader is smaller than assembler in size.

• Object program is already created• Object code can be loaded whenever it is to be executed.• System program known as loader must be there in the

memory• It accepts all object programs and places them in

memory for execution.• Loader is smaller than assembler in size.

General Loader Scheme

General Loader Scheme

• Advantages– No need of re-assembling of program at every execution– Assembler need not be in memory– Subroutines can be written in several languages

• Advantages– No need of re-assembling of program at every execution– Assembler need not be in memory– Subroutines can be written in several languages

Absolute Loader

• Simplest type of General Loader Scheme• The assembled code is stored on Secondary storage• The absolute loader simply accepts the object code

and places it in the main memory at the location pre-described by assembler.

• More memory is available to user as assembler isnot in memory

• Simplest type of General Loader Scheme• The assembled code is stored on Secondary storage• The absolute loader simply accepts the object code

and places it in the main memory at the location pre-described by assembler.

• More memory is available to user as assembler isnot in memory

Absolute Loader

• Advantage– Simple to implement

• Dis-advantage– Programmer has to specify the physical address to

assembler where object code is to be loaded.– Programmer should remember address of each subroutine– Programmer should use those addresses explicitly to call

those functions.– Programmer should take care of non-conflicting subroutine

address.

• Advantage– Simple to implement

• Dis-advantage– Programmer has to specify the physical address to

assembler where object code is to be loaded.– Programmer should remember address of each subroutine– Programmer should use those addresses explicitly to call

those functions.– Programmer should take care of non-conflicting subroutine

address.

Absolute Loader

Absolute Loader

Absolute Loader

• Four loader function accomplished by– Allocation programmer– Linking Programmer– Relocation Assembler– Loading :Loader

• Four loader function accomplished by– Allocation programmer– Linking Programmer– Relocation Assembler– Loading :Loader

Subroutine Linkages

• Main program A wants to transfer control tosubroutine B.

• Assembler does not understand call to B as B is notdefined in program A.

• To resolve such linking problems (symbolicreferences) pseudo opcodes EXTRN and ENTRYare used Relocating / DLLs loaders.

• Main program A wants to transfer control tosubroutine B.

• Assembler does not understand call to B as B is notdefined in program A.

• To resolve such linking problems (symbolicreferences) pseudo opcodes EXTRN and ENTRYare used Relocating / DLLs loaders.

Subroutine Linkages

• EXTRN indicates assembler that these symbols arereferenced in this program but defined in some otherprogram.

EXTRN list of symbols

• ENTRY informs the assembler that these symbolsare defined here but may be referenced in otherprograms.

ENTRY list of symbols

• EXTRN indicates assembler that these symbols arereferenced in this program but defined in some otherprogram.

EXTRN list of symbols

• ENTRY informs the assembler that these symbolsare defined here but may be referenced in otherprograms.

ENTRY list of symbols

Subroutine Linkages

• Example:MAIN START

EXTRN SUBROUT--------------------L 15, =A(SUBROUT)BALR 14, 15-----------END

• Example:MAIN START

EXTRN SUBROUT--------------------L 15, =A(SUBROUT)BALR 14, 15-----------END

Subroutine Linkages

SUBROUT STARTUSING *,15----------BR 14END

SUBROUT STARTUSING *,15----------BR 14END

Subroutine Linkages

• Example ENTRY

A STARTENTRY B1, B2, B3,….Bn

(entry points in subroutine)-----------

B1 -------B2 -------

END

• Example ENTRY

A STARTENTRY B1, B2, B3,….Bn

(entry points in subroutine)-----------

B1 -------B2 -------

END

Relocating Loaders• Purpose:

– To reduce efforts of programmer to allocate and link theobject codes.

– To avoid re-assembling of all subroutines even if one ismodified.

• Assembler assembles every routine independently.• Assembler has to provide object code and other

information to the loader to support the loaderfunctionalities.

• Binary Symbolic Subroutine is simple example ofrelocating loader.

• It is used with system having fixed-length directaddress instruction

• BSS allows many procedure segments and one datasegment.

• Purpose:– To reduce efforts of programmer to allocate and link the

object codes.– To avoid re-assembling of all subroutines even if one is

modified.• Assembler assembles every routine independently.• Assembler has to provide object code and other

information to the loader to support the loaderfunctionalities.

• Binary Symbolic Subroutine is simple example ofrelocating loader.

• It is used with system having fixed-length directaddress instruction

• BSS allows many procedure segments and one datasegment.

Relocating Loaders

Relocating Loaders

Relocating Loaders

• The Relocating assembler has to provide objectcode and relocating and linking information to theLoader.– Length of program for Allocation.– Transfer vector for Linking– Relocation Bits for Relocation– Object codes for Loading.

• Allocation:– Program Length, Transfer Vector length is determined by

Assembler.– All subroutines referenced in program must be allocated

space in memory.

• The Relocating assembler has to provide objectcode and relocating and linking information to theLoader.– Length of program for Allocation.– Transfer vector for Linking– Relocation Bits for Relocation– Object codes for Loading.

• Allocation:– Program Length, Transfer Vector length is determined by

Assembler.– All subroutines referenced in program must be allocated

space in memory.

Relocating Loaders

• Linking:– Transfer vector is part of object code generated by

assembler.– It contains symbolic name/references to all subroutines

referenced in the program.

• How linking is done?– Call to function becomes a transfer instruction that passes

control to Transfer vector.– Transfer vector contains actual address of subroutine and

control is transferred to that physical memory location.

• For eg.- SQRT function is called in MAIN

• Linking:– Transfer vector is part of object code generated by

assembler.– It contains symbolic name/references to all subroutines

referenced in the program.

• How linking is done?– Call to function becomes a transfer instruction that passes

control to Transfer vector.– Transfer vector contains actual address of subroutine and

control is transferred to that physical memory location.

• For eg.- SQRT function is called in MAIN

Relocating Loaders

• Relocation:– It is done with the help of RELOCATION BITS.– If the relocations bit = 1 then instruction or part of instruction

need to be relocated.– Relocation bits included as a part of object code for every

instruction.– In example for every half word the relocation bit is present.

• Check that EXTRN says that SQRT and ERR areexternal symbols and for every external symboltransfer vector entry is generated.

• Relocation:– It is done with the help of RELOCATION BITS.– If the relocations bit = 1 then instruction or part of instruction

need to be relocated.– Relocation bits included as a part of object code for every

instruction.– In example for every half word the relocation bit is present.

• Check that EXTRN says that SQRT and ERR areexternal symbols and for every external symboltransfer vector entry is generated.

Relocating Loaders

• Advantages:– All four functions are performed by Loader.

• Disadvantages:– Transfer Vector can be used only for linking not for storage

of external data.– Transfer vector increases the size of object code.– Common data segment so every subroutine has to access it

in synchronization.

• Advantages:– All four functions are performed by Loader.

• Disadvantages:– Transfer Vector can be used only for linking not for storage

of external data.– Transfer vector increases the size of object code.– Common data segment so every subroutine has to access it

in synchronization.

Direct Linking Loader

• A general relocatable loader.• Multiple procedure and data segments. Every

segment can be accessed by other segment.• Independent translation of program.• We are studying hypothetical loader supporting IBM

370 Model

• A general relocatable loader.• Multiple procedure and data segments. Every

segment can be accessed by other segment.• Independent translation of program.• We are studying hypothetical loader supporting IBM

370 Model

Direct Linking Loader• Following information assembler must

provide to the loader for every segment:– Length of segment.– List of symbols in segment referenced by other

segments.– A list of all symbols not defined in segment but

referenced in segment.– Information about address dependent values: their

location, and how to revise their addresses.– Translated code for every segment and their

relative addresses.

• Following information assembler mustprovide to the loader for every segment:– Length of segment.– List of symbols in segment referenced by other

segments.– A list of all symbols not defined in segment but

referenced in segment.– Information about address dependent values: their

location, and how to revise their addresses.– Translated code for every segment and their

relative addresses.

Direct Linking Loader

Direct Linking Loader

• Some Observations:– POINTER contains value 28 ie relative address of TABLE.– ASUM = A(SUM). SUM is not defined in segment so

absolute address of ASUM can not be mentionedimmediately.

– JOHN, RESULT may be referenced by other procedures somust be informed to the loader.

• Some Observations:– POINTER contains value 28 ie relative address of TABLE.– ASUM = A(SUM). SUM is not defined in segment so

absolute address of ASUM can not be mentionedimmediately.

– JOHN, RESULT may be referenced by other procedures somust be informed to the loader.

Direct Linking Loader

• In this scheme assembler produces fourtypes of cards as object deck (input to thedirect-linking loader) :– ESD (External Symbol Dictionary)– TXT (Object code for every segment)– RLD (Relocation and Linkage Directory)– END (Specifies end of object code)

• In this scheme assembler produces fourtypes of cards as object deck (input to thedirect-linking loader) :– ESD (External Symbol Dictionary)– TXT (Object code for every segment)– RLD (Relocation and Linkage Directory)– END (Specifies end of object code)

Direct Linking Loader -ESD

• ESD stores all symbols defined in program may bereferenced somewhere else and all symbolsreferenced in program but defined elsewhere.

• Types of symbols:– SD: Segment Definition.– LD: Local Definition.– ER: External Reference. ER symbols are used in

conjunction with RLD cards for resolving symbolicreferences

• ESD stores all symbols defined in program may bereferenced somewhere else and all symbolsreferenced in program but defined elsewhere.

• Types of symbols:– SD: Segment Definition.– LD: Local Definition.– ER: External Reference. ER symbols are used in

conjunction with RLD cards for resolving symbolicreferences

Direct Linking Loader -ESD

Direct Linking Loader - TXT

Direct Linking Loader - RLD

• RLD is Relocation and Linkage Directory.• Contains information about contents that depends on

address in core. (Relocation and linking).• RLD contains following information:

– Location of each constant that needs to be changed due torelocation. (relative address of that instruction)

– By what value it has to be changed.(segment symbol)– The operation to be performed ( add/subtract)

• Adjusting address constants of internal symbol isrelocation.

• Adjusting address constants of external symbol isLinking.

• RLD is Relocation and Linkage Directory.• Contains information about contents that depends on

address in core. (Relocation and linking).• RLD contains following information:

– Location of each constant that needs to be changed due torelocation. (relative address of that instruction)

– By what value it has to be changed.(segment symbol)– The operation to be performed ( add/subtract)

• Adjusting address constants of internal symbol isrelocation.

• Adjusting address constants of external symbol isLinking.

Direct Linking Loader - RLD

Other Loading Schemes – Dynamic Loading

Design of Absolute Loader• Allocation, Linking and Relocation performed by

Programmer and Assembler.• Absolute Loader has to read object card and load it

into the memory.• Assembler conveys through two cards:

– Object deck for where to load and what to load in memory.– Entry point for execution of code.

• Card types are:– Text card (type = 0) for instruction and data– Transfer card ( type = 1) to hold entry point to program.

• Allocation, Linking and Relocation performed byProgrammer and Assembler.

• Absolute Loader has to read object card and load itinto the memory.

• Assembler conveys through two cards:– Object deck for where to load and what to load in memory.– Entry point for execution of code.

• Card types are:– Text card (type = 0) for instruction and data– Transfer card ( type = 1) to hold entry point to program.

Design of Absolute Loader• Text Card:

Card column Contents1 Card type = 02 No. Of bytes of information.3-5 Address at which data on card to put.6-7 Empty8-72 Instruction and data to be loaded.73-80 Card sequence number

• Transfer Card:Card column Contents

1 Card type = 12 Count = 03-5 Address of entry point6-72 Empty73-80 Card sequence number

• Text Card:Card column Contents

1 Card type = 02 No. Of bytes of information.3-5 Address at which data on card to put.6-7 Empty8-72 Instruction and data to be loaded.73-80 Card sequence number

• Transfer Card:Card column Contents

1 Card type = 12 Count = 03-5 Address of entry point6-72 Empty73-80 Card sequence number

Design of Absolute Loader

THANK YOU!!THANK YOU!!