Linker and Loaders

41
Linkers and Loader Loader is program which accepts the object program and prepares it for executions (Loads into Memory) Function of Loader 1. Allocation Allocates space in Memory for Program 2. Linking Resolves symbolic references between objects 2. Linking Resolves symbolic references between objects codes 3. Relocation Adjust all the address dependent locations 4. Loading Physically place M/C instructions and data into memory

Transcript of Linker and Loaders

Page 1: 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

Page 2: Linker and Loaders
Page 3: Linker and Loaders

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

Page 4: Linker and 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.

Page 5: Linker and Loaders

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.

Page 6: Linker and Loaders

Assemble and Go Loader

Page 7: Linker and Loaders

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.

Page 8: Linker and Loaders

General Loader Scheme

Page 9: Linker and Loaders

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

Page 10: Linker and Loaders

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

Page 11: Linker and Loaders

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.

Page 12: Linker and Loaders

Absolute Loader

Page 13: Linker and Loaders

Absolute Loader

Page 14: Linker and Loaders

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

Page 15: Linker and Loaders

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.

Page 16: Linker and 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

Page 17: Linker and Loaders

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

Page 18: Linker and Loaders

Subroutine Linkages

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

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

Page 19: Linker and Loaders

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

Page 20: Linker and Loaders

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.

Page 21: Linker and Loaders

Relocating Loaders

Page 22: Linker and Loaders

Relocating Loaders

Page 23: Linker and 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.

Page 24: Linker and Loaders

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

Page 25: Linker and Loaders

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.

Page 26: Linker and Loaders

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.

Page 27: Linker and Loaders

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

Page 28: Linker and Loaders

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.

Page 29: Linker and Loaders

Direct Linking Loader

Page 30: Linker and Loaders

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.

Page 31: Linker and Loaders

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)

Page 32: Linker and Loaders

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

Page 33: Linker and Loaders

Direct Linking Loader -ESD

Page 34: Linker and Loaders

Direct Linking Loader - TXT

Page 35: Linker and Loaders

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.

Page 36: Linker and Loaders

Direct Linking Loader - RLD

Page 37: Linker and Loaders

Other Loading Schemes – Dynamic Loading

Page 38: Linker and Loaders

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.

Page 39: Linker and Loaders

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

Page 40: Linker and Loaders

Design of Absolute Loader

Page 41: Linker and Loaders

THANK YOU!!THANK YOU!!