Loaders and Linkers
CS 230
이준원
2
Overview
• assembler
– generates an object code in a predefined format
» COFF (common object file format)
» ELF (executable and linking format)
– assigns addresses to instructions and data
» assuming the program starts at location 0
• loader
– loads a program into memory(or AS) for execution
– also performs relocation and linking
• relocation
– modifies the absolute addresses in the object program
» assembler provides necessary information
• linking
– combines several object programs
– these programs are developed independently
– a program may use a symbol defined in another program
3
Bootstrap Loader
• bootstrapping
– actions taken when a computer is first powered on
– the hardware logic reads a program from address 0 of ROM (Read
Only Memory)
» ROM is installed by the manufacturer
» ROM contains bootstrapping program and some other routines
that controls hardware (e.g. BIOS)
• bootstrapping is a loader
– loads OS from disk into memory and makes it run
– the location of OS on disk (or floppy) usually starts at the first sector
– starting address in memory is usually fixed to 0
» no need of relocation
– this kind of loader is simple
» no relocation
» no linking
» called “absolute loader”
4
Relocation
• assembler review
– assembler generates an object code assuming that the program starts
at memory address 0
– loader decides the starting address of a program
– assembler generates modification record
• limits of modification record
– record format
» (address, length)
– it can be huge when direct addressing is frequently used
– if instruction format is fixed for absolute addressing, the length part
can be removed
– instead of address field, bit-vector can be used
» 1101100..... means instruction 1 2 4 5. .. need to be modified
• hardware support for relocation
– base register
» assembler and loader do not need to worry about relocation
5
Linking
• background
– a large problem is better broken into several small pieces
– each piece is better implemented independently
» assembled (compiled) independently
– there are many data structures shared among those pieces
» variables and procedures
– some programs are used by many different programs
» print(), file operations, exp(), sin(), ...
» these are usually provided as library functions
6
Linking (cont’d)
• requirements for linking
– each module defines
» which symbols are used by other modules
» symbols undefined in a module are assumed to be defined in
other modules
• if these symbols are declared explicitly, it helps linker to resolve
• principles
– assembler evaluates as much as possible
» expressions
– if some cannot be resolved,
» provide the modification records
7
Linking Example
• modification record from module A
– for statement 20, assembler
» leaves 4 in the operand2 field
» prepares modification record as
• add LISTB to operand2 field of statement 20
– for statement 24, modification record will be
» put LISTB-ENDB in operand2 field of statement 24
• in module B
– assembler prepares a symbol table containing external symbols name and value
(LISTB,60) (ENDB,80)
00 PROGA START 0
EXTRN LISTA, ENDA
EXTREF LISTB, ENDB
20 REF1 LD A, LISTB+4
24 REF2 LD B, LISTB-ENDB
48 LISTA EQU *
52
80 ENDA EQU *
00 PROGB START 0
EXTRN LISTB, ENDB
60 LISTB EQU *
64
80 ENDB EQU *
8
Linking Loader
• linking
– combine all the programs assembled independently
– resolves external symbols
» actual value of a symbol is known only at loading time
• linking loader: pass 1
– decides where each module will be located
» this information may be given by OS
– modify absolute addresses as defined in modification record in the
code itself
– prepare an external symbol table
» (control section, symbol name, value)
• linking loader: pass 2
– for a modification record (say the first one of A)
» search all the external symbol tables for LISTB
» add 60 to the operand2 field of statement 20
– transfers the control to the program loaded
9
Library
• a collection of popular routines
– provided as object modules
– linked together with user program
– programmers just use a function in the library as if it is a language
feature
– contains a table of symbols exported
• when a user program is compiled (assembled) all the undefined
symbols are kept in the external symbol table
• linking loader
– execute modification records
– resolve symbols in EST with standard libraries and user-specified
libraries
– if there remain symbols in EST unresolved, report ERROR
10
Library (cont’d)
• there may be thousands of routines(functions) in a library
– directory is needed
– (routine name, address)
– hashing to locate the routine name fast
• searching all the symbol definitions of library takes long time
– each program defines which library is used
» or as an option at compile(assemble) time or link time
11
Linkage Editor
• linking loader
– link object modules into a single module and load it into memory and
starts the program
• linkage editor
– just link and does some relocation
– store the result on disk instead of running
– programs once finished, are stored in this form
– these programs are run by relocating loader later
12
Linkage Editor (illustration)
object
program(s)
linking
loader
memory
object
program(s)
linkage
editor
library
linked
program
relocating
loader
memory
library
13
Comparison of Linking Loader and Linkage Editor
• what it does
– linking loader
» get an starting address from OS (zero for VM)
» processing modification record
» resolution of external symbols
» produce output into memory (or disk for VM)
» transfer control to the program
– linkage editor
» resolution of external symbols
• absolute addresses are not processed yet
» produce output on disk
14
Comparison of Linking Loader and Linkage Editor
• when to use
– linking loader
» when a program is in a development cycle
• modify the program
• assemble and run
– linkage editor
» when a program development is finished
» when a library is built
• some linking is done here
15
More about Linking
• Static Linking
– all code modules are copied into a single executable file
» the same shared module may exist in many files
– a location in this file implies the location in the memory image
– target address for cross-module reference can be determined before
run time
• Dynamic Linking
– needs help from OS ( that means the scheme varies depending on
OS)
– library modules are usually linked dynamically
– inserts symbolic references in the executable file
– these symbols are resolved at run time
16
Dynamic Linking of Unix
• GOT (Global Offset Table)
– a linker allocates GOT for each library module
– contains the addresses of all dynamically linked external symbols
(functions and variables) referenced by the module
• Steps
– a program referencing a library module is loaded for execution
– the imported modules are loaded (unless already resident)
– a region in the AS is allocated to map the module
– the loader initializes the module’s GOT (which may require loading of
other library modules)
• lazy loading
– load module only when it is accessed
– the entry in the GOT points to stub code
– the stub invokes dynamic loader which loads the referenced module
and replaces the respective entries in GOT
17
Dynamic Linking
• against static linking
– saves disk space
» do not duplicate shared modules in each executable file
– saves memory space
» by sharing binary code
– new versions of library code are immediately usable
– a module in memory can be linked immediately
• drawbacks
– extra work needs to be done to set up GOT (but much less modules
need to be loaded since some are already in memory)
– the location of module in AS is not determined until run time
» the code of the module should be position-independent
• relative addresses only
» indirect call via the GOT
18
MD DOS Linker
• object file (.OBJ)
– generated by assembler (or compiler)
– format
THEADR name of this object module
PUBDEF external symbols defined in this module
EXTDEF external symbols used here
TYPDEF data types for pubdef and extdef
SEGDEF describes segments in this module
GRPDEF segment grouping
LNAMES name list indexed by segdef and grpdef
LEDATA binary image of code
LIDATA repeated data
FIXUPP modification record
MODEND end
19
MS DOS Linker (cont’d)
• LINK
– pass 1:
» allocates segments defined in SEGDEF
» resolve external symbols
– pass 2:
» prepare memory image
• if needed, disk space is also used
» expand LIDATA
» relocations within segment
» write .EXE file
20
SunOS Linkers
• SunOS is UNIX
• two different linkers
• link-editor (ld) produces several types of output
– relocatable object module
» need to be link-edited later
– static executable
– dynamic executable
– shared object
» can be bounded at run time
• run-time linker
– bind dynamic executables and shared object at execution time

More Related Content

PPT
wk 4 -- linking.ppt
PPT
assem.ppt
PPT
Mod 5.3 - Linkers & Loaderss Summary.ppt
PPTX
Pune-Cocoa: Blocks and GCD
PPT
Intro reverse engineering
PPT
Advanced c programming in Linux
PDF
Compilation and Execution
PPT
Assembler design option
wk 4 -- linking.ppt
assem.ppt
Mod 5.3 - Linkers & Loaderss Summary.ppt
Pune-Cocoa: Blocks and GCD
Intro reverse engineering
Advanced c programming in Linux
Compilation and Execution
Assembler design option

Similar to loaderfffffffffffffffffffffffffffflinker.ppt (20)

PPT
Purdue CS354 Operating Systems 2008
PDF
Puppet camp london nov 2014 slides (1)
PDF
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
PDF
Yocto: Training in English
PPTX
A Comparison of .NET Framework vs. Java Virtual Machine
PPTX
Spark 1.0
PPTX
6-9-2017-slides-vFinal.pptx
PDF
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
PDF
L06 a versioning_system_overview
PPT
Embedded system - embedded system programming
PDF
muja osjkkhkhkkhkkkfdxfdfddkhvjlbjljlhgg
PDF
Ganesh naik linux_kernel_internals
PDF
Ganesh naik linux_kernel_internals
PDF
Part 02 Linux Kernel Module Programming
PDF
Linux Kernel Live Patching
PPTX
Apache maven and its impact on java 9 (Java One 2017)
PPTX
UNIT -5 EMBEDDED DRIVERS AND APPLICATION PORTING.pptx
PPT
嵌入式Linux課程-GNU Toolchain
PPTX
JavaScript Module Loaders
PPTX
Oracle applications r12.2, ebr, online patching means lot of work for devel...
Purdue CS354 Operating Systems 2008
Puppet camp london nov 2014 slides (1)
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
Yocto: Training in English
A Comparison of .NET Framework vs. Java Virtual Machine
Spark 1.0
6-9-2017-slides-vFinal.pptx
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
L06 a versioning_system_overview
Embedded system - embedded system programming
muja osjkkhkhkkhkkkfdxfdfddkhvjlbjljlhgg
Ganesh naik linux_kernel_internals
Ganesh naik linux_kernel_internals
Part 02 Linux Kernel Module Programming
Linux Kernel Live Patching
Apache maven and its impact on java 9 (Java One 2017)
UNIT -5 EMBEDDED DRIVERS AND APPLICATION PORTING.pptx
嵌入式Linux課程-GNU Toolchain
JavaScript Module Loaders
Oracle applications r12.2, ebr, online patching means lot of work for devel...
Ad

More from ATHMARANJANBhandary (8)

PPT
lecture 18 (Loader-Machine Dependent Features).ppt
PPT
MODULE_4_part1_Intro_image-segzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...
PPTX
Module 2.pptxwwwwwwwwwwwwwwwwwwwwwwwwwww
PPTX
Module 5.pptxsssssssssssssssssssssssssssssssssssssss
PPTX
Module 3-DCT.pptxssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...
PPT
MODULE_4_part1_Intro_image-segmentation.ppt AAAAAAAAAAAAAAAAAAAAAAA
PPTX
Module 4-Image segmentation.pptx aaaaaaaaaaaaaaaaaaaaaaaaaaa
PPTX
Digital Image Processing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
lecture 18 (Loader-Machine Dependent Features).ppt
MODULE_4_part1_Intro_image-segzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...
Module 2.pptxwwwwwwwwwwwwwwwwwwwwwwwwwww
Module 5.pptxsssssssssssssssssssssssssssssssssssssss
Module 3-DCT.pptxssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...
MODULE_4_part1_Intro_image-segmentation.ppt AAAAAAAAAAAAAAAAAAAAAAA
Module 4-Image segmentation.pptx aaaaaaaaaaaaaaaaaaaaaaaaaaa
Digital Image Processing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
Ad

Recently uploaded (20)

PPT
System Unit Components and its Functions
PPTX
AI_ML_Internship_WReport_Template_v2.pptx
PDF
GENERATOR AND IMPROVED COIL THEREFOR HAVINGELECTRODYNAMIC PROPERTIES
PPTX
AIR BAG SYStYEM mechanical enginweering.pptx
PDF
ICT grade for 8. MATATAG curriculum .P2.pdf
PDF
PakistanCoinageAct-906.pdfdbnsshsjjsbsbb
PDF
20A LG INR18650HJ2 3.6V 2900mAh Battery cells for Power Tools Vacuum Cleaner
PDF
Dozuki_Solution-hardware minimalization.
PDF
Printing Presentation to show beginners.
PPTX
Unit-1.pptxgeyeuueueu7r7r7r77r7r7r7uriruru
PPTX
dDifference Beetween Saving slides And Investment Slides.pptx
PPTX
Chapter no 8 output devices dpart 2.pptx
DOCX
Copy-OT LIST 12.8.25.docxjdjfufufufufuuffuf
PPTX
Pin configuration and project related to
PDF
Topic-1-Main-Features-of-Data-Processing.pdf
PPTX
RTS MASTER DECK_Household Convergence Scorecards. Use this file copy.pptx
PPTX
Clauses_Part1.hshshpjzjxnznxnxnndndndndndndndnndptx
PPTX
vortex flow measurement in instrumentation
PPTX
Group 4 [BSIT-1C] Computer Network (1).pptx
PDF
Presented by ATHUL KRISHNA.S_20250813_191657_0000.pdf
System Unit Components and its Functions
AI_ML_Internship_WReport_Template_v2.pptx
GENERATOR AND IMPROVED COIL THEREFOR HAVINGELECTRODYNAMIC PROPERTIES
AIR BAG SYStYEM mechanical enginweering.pptx
ICT grade for 8. MATATAG curriculum .P2.pdf
PakistanCoinageAct-906.pdfdbnsshsjjsbsbb
20A LG INR18650HJ2 3.6V 2900mAh Battery cells for Power Tools Vacuum Cleaner
Dozuki_Solution-hardware minimalization.
Printing Presentation to show beginners.
Unit-1.pptxgeyeuueueu7r7r7r77r7r7r7uriruru
dDifference Beetween Saving slides And Investment Slides.pptx
Chapter no 8 output devices dpart 2.pptx
Copy-OT LIST 12.8.25.docxjdjfufufufufuuffuf
Pin configuration and project related to
Topic-1-Main-Features-of-Data-Processing.pdf
RTS MASTER DECK_Household Convergence Scorecards. Use this file copy.pptx
Clauses_Part1.hshshpjzjxnznxnxnndndndndndndndnndptx
vortex flow measurement in instrumentation
Group 4 [BSIT-1C] Computer Network (1).pptx
Presented by ATHUL KRISHNA.S_20250813_191657_0000.pdf

loaderfffffffffffffffffffffffffffflinker.ppt

  • 1. Loaders and Linkers CS 230 이준원
  • 2. 2 Overview • assembler – generates an object code in a predefined format » COFF (common object file format) » ELF (executable and linking format) – assigns addresses to instructions and data » assuming the program starts at location 0 • loader – loads a program into memory(or AS) for execution – also performs relocation and linking • relocation – modifies the absolute addresses in the object program » assembler provides necessary information • linking – combines several object programs – these programs are developed independently – a program may use a symbol defined in another program
  • 3. 3 Bootstrap Loader • bootstrapping – actions taken when a computer is first powered on – the hardware logic reads a program from address 0 of ROM (Read Only Memory) » ROM is installed by the manufacturer » ROM contains bootstrapping program and some other routines that controls hardware (e.g. BIOS) • bootstrapping is a loader – loads OS from disk into memory and makes it run – the location of OS on disk (or floppy) usually starts at the first sector – starting address in memory is usually fixed to 0 » no need of relocation – this kind of loader is simple » no relocation » no linking » called “absolute loader”
  • 4. 4 Relocation • assembler review – assembler generates an object code assuming that the program starts at memory address 0 – loader decides the starting address of a program – assembler generates modification record • limits of modification record – record format » (address, length) – it can be huge when direct addressing is frequently used – if instruction format is fixed for absolute addressing, the length part can be removed – instead of address field, bit-vector can be used » 1101100..... means instruction 1 2 4 5. .. need to be modified • hardware support for relocation – base register » assembler and loader do not need to worry about relocation
  • 5. 5 Linking • background – a large problem is better broken into several small pieces – each piece is better implemented independently » assembled (compiled) independently – there are many data structures shared among those pieces » variables and procedures – some programs are used by many different programs » print(), file operations, exp(), sin(), ... » these are usually provided as library functions
  • 6. 6 Linking (cont’d) • requirements for linking – each module defines » which symbols are used by other modules » symbols undefined in a module are assumed to be defined in other modules • if these symbols are declared explicitly, it helps linker to resolve • principles – assembler evaluates as much as possible » expressions – if some cannot be resolved, » provide the modification records
  • 7. 7 Linking Example • modification record from module A – for statement 20, assembler » leaves 4 in the operand2 field » prepares modification record as • add LISTB to operand2 field of statement 20 – for statement 24, modification record will be » put LISTB-ENDB in operand2 field of statement 24 • in module B – assembler prepares a symbol table containing external symbols name and value (LISTB,60) (ENDB,80) 00 PROGA START 0 EXTRN LISTA, ENDA EXTREF LISTB, ENDB 20 REF1 LD A, LISTB+4 24 REF2 LD B, LISTB-ENDB 48 LISTA EQU * 52 80 ENDA EQU * 00 PROGB START 0 EXTRN LISTB, ENDB 60 LISTB EQU * 64 80 ENDB EQU *
  • 8. 8 Linking Loader • linking – combine all the programs assembled independently – resolves external symbols » actual value of a symbol is known only at loading time • linking loader: pass 1 – decides where each module will be located » this information may be given by OS – modify absolute addresses as defined in modification record in the code itself – prepare an external symbol table » (control section, symbol name, value) • linking loader: pass 2 – for a modification record (say the first one of A) » search all the external symbol tables for LISTB » add 60 to the operand2 field of statement 20 – transfers the control to the program loaded
  • 9. 9 Library • a collection of popular routines – provided as object modules – linked together with user program – programmers just use a function in the library as if it is a language feature – contains a table of symbols exported • when a user program is compiled (assembled) all the undefined symbols are kept in the external symbol table • linking loader – execute modification records – resolve symbols in EST with standard libraries and user-specified libraries – if there remain symbols in EST unresolved, report ERROR
  • 10. 10 Library (cont’d) • there may be thousands of routines(functions) in a library – directory is needed – (routine name, address) – hashing to locate the routine name fast • searching all the symbol definitions of library takes long time – each program defines which library is used » or as an option at compile(assemble) time or link time
  • 11. 11 Linkage Editor • linking loader – link object modules into a single module and load it into memory and starts the program • linkage editor – just link and does some relocation – store the result on disk instead of running – programs once finished, are stored in this form – these programs are run by relocating loader later
  • 13. 13 Comparison of Linking Loader and Linkage Editor • what it does – linking loader » get an starting address from OS (zero for VM) » processing modification record » resolution of external symbols » produce output into memory (or disk for VM) » transfer control to the program – linkage editor » resolution of external symbols • absolute addresses are not processed yet » produce output on disk
  • 14. 14 Comparison of Linking Loader and Linkage Editor • when to use – linking loader » when a program is in a development cycle • modify the program • assemble and run – linkage editor » when a program development is finished » when a library is built • some linking is done here
  • 15. 15 More about Linking • Static Linking – all code modules are copied into a single executable file » the same shared module may exist in many files – a location in this file implies the location in the memory image – target address for cross-module reference can be determined before run time • Dynamic Linking – needs help from OS ( that means the scheme varies depending on OS) – library modules are usually linked dynamically – inserts symbolic references in the executable file – these symbols are resolved at run time
  • 16. 16 Dynamic Linking of Unix • GOT (Global Offset Table) – a linker allocates GOT for each library module – contains the addresses of all dynamically linked external symbols (functions and variables) referenced by the module • Steps – a program referencing a library module is loaded for execution – the imported modules are loaded (unless already resident) – a region in the AS is allocated to map the module – the loader initializes the module’s GOT (which may require loading of other library modules) • lazy loading – load module only when it is accessed – the entry in the GOT points to stub code – the stub invokes dynamic loader which loads the referenced module and replaces the respective entries in GOT
  • 17. 17 Dynamic Linking • against static linking – saves disk space » do not duplicate shared modules in each executable file – saves memory space » by sharing binary code – new versions of library code are immediately usable – a module in memory can be linked immediately • drawbacks – extra work needs to be done to set up GOT (but much less modules need to be loaded since some are already in memory) – the location of module in AS is not determined until run time » the code of the module should be position-independent • relative addresses only » indirect call via the GOT
  • 18. 18 MD DOS Linker • object file (.OBJ) – generated by assembler (or compiler) – format THEADR name of this object module PUBDEF external symbols defined in this module EXTDEF external symbols used here TYPDEF data types for pubdef and extdef SEGDEF describes segments in this module GRPDEF segment grouping LNAMES name list indexed by segdef and grpdef LEDATA binary image of code LIDATA repeated data FIXUPP modification record MODEND end
  • 19. 19 MS DOS Linker (cont’d) • LINK – pass 1: » allocates segments defined in SEGDEF » resolve external symbols – pass 2: » prepare memory image • if needed, disk space is also used » expand LIDATA » relocations within segment » write .EXE file
  • 20. 20 SunOS Linkers • SunOS is UNIX • two different linkers • link-editor (ld) produces several types of output – relocatable object module » need to be link-edited later – static executable – dynamic executable – shared object » can be bounded at run time • run-time linker – bind dynamic executables and shared object at execution time