SlideShare una empresa de Scribd logo
1 de 19
Linking and Loading 1
CS502 Spring 2006
Linking & Loading
CS-502 Operating Systems
{This topic is not covered in Tannenbaum.
Silbershatz et al devote about 2 pages to it in §8.1}
Linking and Loading 2
CS502 Spring 2006
What happens to your program …
…after it is compiled, but before it
can be run?
Linking and Loading 3
CS502 Spring 2006
Executable files
• The OS expects executable files to have a
specific format
– Header info
• Code locations
• Data locations
– Code & data
– Symbol Table
• List of names of things defined in your program and
where they are defined
• List of names of things defined elsewhere that are
used by your program, and where they are used.
Linking and Loading 4
CS502 Spring 2006
Example
#include <stdio.h>
int main () {
printf (“hello,
worldn”)
}
• Symbol defined in
your program and
used elsewhere
•main
• Symbol defined
elsewhere and used by
your program
•printf
Linking and Loading 5
CS502 Spring 2006
Example
#include <stdio.h>
extern int errno;
int main () {
printf (“hello,
worldn”)
<check errno for
errors>
}
• Symbol defined in
your program and
used elsewhere
•main
• Symbol defined
elsewhere and used by
your program
•printf
•errno
Linking and Loading 6
CS502 Spring 2006
Two-step operation (in most systems)
• Linking: Combining a set of programs, including
library routines, to create a loadable image
a) Resolving symbols defined within the set
b) Listing symbols needing to be resolved by loader
• Loading: Copying the loadable image into
memory, connecting it with any other programs
already loaded, and updating addresses as needed
– (In Unix) interpreting file to initialize the process
address space
– (in all systems) kernel image is special (own format)
Linking and Loading 7
CS502 Spring 2006
From source code to a process
• Binding is the process of mapping
names to addresses
• Most compilers produce relocatable
object code
• Addresses relative to zero
• The linker combines multiple object
files and library modules into a single
executable file
• Addresses also relative to zero
• The Loader reads the executable file
– Allocates memory
– Maps addresses within file to physical
memory addresses
– Resolves names of dynamic library
items
Source
(.c, .cc)
Object
(.o)
Executable
In-memory Image
Compiler
Linker
Loader
Other Objects
(.o)
Dynamic libraries
(.dll)
Static libraries
(.a)
Linking and Loading 8
CS502 Spring 2006
Static Linking and Loading
Printf.c
Printf.o
Static
Library
gcc
ar
Linke
r
Memory
HelloWorld.c
gcc
HelloWorld.o
Loader
a.Out
(or name of
your command)
Linking and Loading 9
CS502 Spring 2006
Classic Unix
• Linker is inside of gcc command
• Loader is part of exec system call
• Executable image contains all object and library
modules needed by program
• Entire image is loaded at once
• Every image contains copy of common library
routines
• Every loaded program contain duplicate copy of
library routines
Linking and Loading 10
CS502 Spring 2006
Dynamic Loading
• Routine is not loaded until it is called
• Better memory-space utilization; unused
routine is never loaded.
• Useful when large amounts of code are
needed to handle infrequently occurring
cases.
• Silbershatz says [incorrectly]
• No special support from the operating system is
required
• Must be implemented through program design
Linking and Loading 11
CS502 Spring 2006
Program-controlled Dynamic Loading
• Requires:
– A load system call to invoke loader (not in Unix/Linux)
– ability to leave symbols unresolved and resolve at run time (not in
Unix/Linux)
• E.g.,
void myPrintf (**arg) {
static int loaded = 0;
if (!loaded ) {
load (“printf”);
loaded = 1;
printf(arg);
}
}
Linking and Loading 12
CS502 Spring 2006
Linker-assisted Dynamic Loading
• Programmer marks modules as “dynamic”
to linker
• For function call to a dynamic function
• Call is indirect through a link table
• Each link table initialized with address small stub of
code to locate and load module.
• When loaded, loader replaces link table entry with
address of loaded function
• When unloaded, loader replaces table entry with
stub address
• Static data cannot be made dynamic
Linking and Loading 13
CS502 Spring 2006
Shared Libraries
• Observation – “everyone” links to standard
libraries (libc.a, etc.)
• Consume space in
• every executable image
• every process memory at runtime
• Would it be possible to share the common
libraries?
– Automatically load at runtime?
Linking and Loading 14
CS502 Spring 2006
Shared libraries (continued)
• Libraries designated as “shared”
• .so, .dll, etc.
• Supported by corresponding “.a” libraries containing
symbol information
• Linker sets up symbols to be resolved at
runtime
• Loader: Is library already in memory?
– If so, map into new process space
• “map,” an operation to be defined
– If not, load and then map
Linking and Loading 15
CS502 Spring 2006
Run-time Linking/Loading
Printf.c
Printf.o
Shared
Library
gcc
ar
Linker
Memory
gcc
Loader
Save disk space.
Startup faster.
Might not need all.
Run-time
Loader
HelloWorld.c
HelloWorld.o
a.Out
(or name of
your command)
Linking and Loading 16
CS502 Spring 2006
Dynamic Linking
• Complete linking postponed until execution time.
• Stub used to locate the appropriate memory-
resident library routine.
• Stub replaces itself with the address of the routine,
and executes the routine.
• Operating system needs to check if routine is in
processes’ memory address space.
• Dynamic linking is particularly useful for
libraries.
Linking and Loading 17
CS502 Spring 2006
Dynamic Shared Libraries
• Static shared libraries requires address
space pre-allocation
• Dynamic shared libraries – address binding
at runtime
– Code must be position independent
– At runtime, references are resolved as
• Library_relative_address + library_base_address
Linking and Loading 18
CS502 Spring 2006
Overlays
(primarily of historical interest)
• Keep in memory only those instructions and data
that are needed at any given time.
• Needed when process is larger than amount of
memory allocated to it.
• Can be implemented by user
– no special support needed from operating system, but
– programming design of overlay structure is complex
• Can be done with OS help – think about Unix exec
system call
Linking and Loading 19
CS502 Spring 2006
Linking - Summary
• Linker – key part of OS – not in kernel
– Combines object files and libraries into a
“standard” format that the OS loader can
interpret
– Resolves references and does static relocation
of addresses
– Creates information for loader to complete
binding process
– Supports dynamic shared libraries

Más contenido relacionado

Similar a wk 4 -- linking.ppt

Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)Ahmed El-Arabawy
 
Apache Spark Tutorial
Apache Spark TutorialApache Spark Tutorial
Apache Spark TutorialAhmet Bulut
 
Transactional writes to cloud storage with Eric Liang
Transactional writes to cloud storage with Eric LiangTransactional writes to cloud storage with Eric Liang
Transactional writes to cloud storage with Eric LiangDatabricks
 
Robust and Scalable ETL over Cloud Storage with Apache Spark
Robust and Scalable ETL over Cloud Storage with Apache SparkRobust and Scalable ETL over Cloud Storage with Apache Spark
Robust and Scalable ETL over Cloud Storage with Apache SparkDatabricks
 
Ch1Ch2Sept10.pdf
Ch1Ch2Sept10.pdfCh1Ch2Sept10.pdf
Ch1Ch2Sept10.pdfSamSami69
 
APEX Application Lifecycle and Deployment 20220714.pdf
APEX Application Lifecycle and Deployment 20220714.pdfAPEX Application Lifecycle and Deployment 20220714.pdf
APEX Application Lifecycle and Deployment 20220714.pdfRichard Martens
 
Os7 2
Os7 2Os7 2
Os7 2issbp
 
Address Binding Scheme
Address Binding SchemeAddress Binding Scheme
Address Binding SchemeRajesh Piryani
 
Introduction to Apache Spark :: Lagos Scala Meetup session 2
Introduction to Apache Spark :: Lagos Scala Meetup session 2 Introduction to Apache Spark :: Lagos Scala Meetup session 2
Introduction to Apache Spark :: Lagos Scala Meetup session 2 Olalekan Fuad Elesin
 
Ingesting hdfs intosolrusingsparktrimmed
Ingesting hdfs intosolrusingsparktrimmedIngesting hdfs intosolrusingsparktrimmed
Ingesting hdfs intosolrusingsparktrimmedwhoschek
 
bh-europe-01-clowes
bh-europe-01-clowesbh-europe-01-clowes
bh-europe-01-clowesguest3e5046
 
Hm system programming class 1
Hm system programming class 1Hm system programming class 1
Hm system programming class 1Hitesh Mohapatra
 
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...Jose Quesada (hiring)
 
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a serviceCOMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a serviceAntonio García-Domínguez
 

Similar a wk 4 -- linking.ppt (20)

Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
 
Apache Spark Tutorial
Apache Spark TutorialApache Spark Tutorial
Apache Spark Tutorial
 
Linkers And Loaders
Linkers And LoadersLinkers And Loaders
Linkers And Loaders
 
Transactional writes to cloud storage with Eric Liang
Transactional writes to cloud storage with Eric LiangTransactional writes to cloud storage with Eric Liang
Transactional writes to cloud storage with Eric Liang
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
Robust and Scalable ETL over Cloud Storage with Apache Spark
Robust and Scalable ETL over Cloud Storage with Apache SparkRobust and Scalable ETL over Cloud Storage with Apache Spark
Robust and Scalable ETL over Cloud Storage with Apache Spark
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 
Ch1Ch2Sept10.pdf
Ch1Ch2Sept10.pdfCh1Ch2Sept10.pdf
Ch1Ch2Sept10.pdf
 
APEX Application Lifecycle and Deployment 20220714.pdf
APEX Application Lifecycle and Deployment 20220714.pdfAPEX Application Lifecycle and Deployment 20220714.pdf
APEX Application Lifecycle and Deployment 20220714.pdf
 
Os7 2
Os7 2Os7 2
Os7 2
 
Address Binding Scheme
Address Binding SchemeAddress Binding Scheme
Address Binding Scheme
 
Introduction to Apache Spark :: Lagos Scala Meetup session 2
Introduction to Apache Spark :: Lagos Scala Meetup session 2 Introduction to Apache Spark :: Lagos Scala Meetup session 2
Introduction to Apache Spark :: Lagos Scala Meetup session 2
 
Ingesting hdfs intosolrusingsparktrimmed
Ingesting hdfs intosolrusingsparktrimmedIngesting hdfs intosolrusingsparktrimmed
Ingesting hdfs intosolrusingsparktrimmed
 
bh-europe-01-clowes
bh-europe-01-clowesbh-europe-01-clowes
bh-europe-01-clowes
 
Hm system programming class 1
Hm system programming class 1Hm system programming class 1
Hm system programming class 1
 
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
 
Eusecwest
EusecwestEusecwest
Eusecwest
 
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a serviceCOMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
 
Flexible Indexing in Lucene 4.0
Flexible Indexing in Lucene 4.0Flexible Indexing in Lucene 4.0
Flexible Indexing in Lucene 4.0
 
Linkers
LinkersLinkers
Linkers
 

Último

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Último (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

wk 4 -- linking.ppt

  • 1. Linking and Loading 1 CS502 Spring 2006 Linking & Loading CS-502 Operating Systems {This topic is not covered in Tannenbaum. Silbershatz et al devote about 2 pages to it in §8.1}
  • 2. Linking and Loading 2 CS502 Spring 2006 What happens to your program … …after it is compiled, but before it can be run?
  • 3. Linking and Loading 3 CS502 Spring 2006 Executable files • The OS expects executable files to have a specific format – Header info • Code locations • Data locations – Code & data – Symbol Table • List of names of things defined in your program and where they are defined • List of names of things defined elsewhere that are used by your program, and where they are used.
  • 4. Linking and Loading 4 CS502 Spring 2006 Example #include <stdio.h> int main () { printf (“hello, worldn”) } • Symbol defined in your program and used elsewhere •main • Symbol defined elsewhere and used by your program •printf
  • 5. Linking and Loading 5 CS502 Spring 2006 Example #include <stdio.h> extern int errno; int main () { printf (“hello, worldn”) <check errno for errors> } • Symbol defined in your program and used elsewhere •main • Symbol defined elsewhere and used by your program •printf •errno
  • 6. Linking and Loading 6 CS502 Spring 2006 Two-step operation (in most systems) • Linking: Combining a set of programs, including library routines, to create a loadable image a) Resolving symbols defined within the set b) Listing symbols needing to be resolved by loader • Loading: Copying the loadable image into memory, connecting it with any other programs already loaded, and updating addresses as needed – (In Unix) interpreting file to initialize the process address space – (in all systems) kernel image is special (own format)
  • 7. Linking and Loading 7 CS502 Spring 2006 From source code to a process • Binding is the process of mapping names to addresses • Most compilers produce relocatable object code • Addresses relative to zero • The linker combines multiple object files and library modules into a single executable file • Addresses also relative to zero • The Loader reads the executable file – Allocates memory – Maps addresses within file to physical memory addresses – Resolves names of dynamic library items Source (.c, .cc) Object (.o) Executable In-memory Image Compiler Linker Loader Other Objects (.o) Dynamic libraries (.dll) Static libraries (.a)
  • 8. Linking and Loading 8 CS502 Spring 2006 Static Linking and Loading Printf.c Printf.o Static Library gcc ar Linke r Memory HelloWorld.c gcc HelloWorld.o Loader a.Out (or name of your command)
  • 9. Linking and Loading 9 CS502 Spring 2006 Classic Unix • Linker is inside of gcc command • Loader is part of exec system call • Executable image contains all object and library modules needed by program • Entire image is loaded at once • Every image contains copy of common library routines • Every loaded program contain duplicate copy of library routines
  • 10. Linking and Loading 10 CS502 Spring 2006 Dynamic Loading • Routine is not loaded until it is called • Better memory-space utilization; unused routine is never loaded. • Useful when large amounts of code are needed to handle infrequently occurring cases. • Silbershatz says [incorrectly] • No special support from the operating system is required • Must be implemented through program design
  • 11. Linking and Loading 11 CS502 Spring 2006 Program-controlled Dynamic Loading • Requires: – A load system call to invoke loader (not in Unix/Linux) – ability to leave symbols unresolved and resolve at run time (not in Unix/Linux) • E.g., void myPrintf (**arg) { static int loaded = 0; if (!loaded ) { load (“printf”); loaded = 1; printf(arg); } }
  • 12. Linking and Loading 12 CS502 Spring 2006 Linker-assisted Dynamic Loading • Programmer marks modules as “dynamic” to linker • For function call to a dynamic function • Call is indirect through a link table • Each link table initialized with address small stub of code to locate and load module. • When loaded, loader replaces link table entry with address of loaded function • When unloaded, loader replaces table entry with stub address • Static data cannot be made dynamic
  • 13. Linking and Loading 13 CS502 Spring 2006 Shared Libraries • Observation – “everyone” links to standard libraries (libc.a, etc.) • Consume space in • every executable image • every process memory at runtime • Would it be possible to share the common libraries? – Automatically load at runtime?
  • 14. Linking and Loading 14 CS502 Spring 2006 Shared libraries (continued) • Libraries designated as “shared” • .so, .dll, etc. • Supported by corresponding “.a” libraries containing symbol information • Linker sets up symbols to be resolved at runtime • Loader: Is library already in memory? – If so, map into new process space • “map,” an operation to be defined – If not, load and then map
  • 15. Linking and Loading 15 CS502 Spring 2006 Run-time Linking/Loading Printf.c Printf.o Shared Library gcc ar Linker Memory gcc Loader Save disk space. Startup faster. Might not need all. Run-time Loader HelloWorld.c HelloWorld.o a.Out (or name of your command)
  • 16. Linking and Loading 16 CS502 Spring 2006 Dynamic Linking • Complete linking postponed until execution time. • Stub used to locate the appropriate memory- resident library routine. • Stub replaces itself with the address of the routine, and executes the routine. • Operating system needs to check if routine is in processes’ memory address space. • Dynamic linking is particularly useful for libraries.
  • 17. Linking and Loading 17 CS502 Spring 2006 Dynamic Shared Libraries • Static shared libraries requires address space pre-allocation • Dynamic shared libraries – address binding at runtime – Code must be position independent – At runtime, references are resolved as • Library_relative_address + library_base_address
  • 18. Linking and Loading 18 CS502 Spring 2006 Overlays (primarily of historical interest) • Keep in memory only those instructions and data that are needed at any given time. • Needed when process is larger than amount of memory allocated to it. • Can be implemented by user – no special support needed from operating system, but – programming design of overlay structure is complex • Can be done with OS help – think about Unix exec system call
  • 19. Linking and Loading 19 CS502 Spring 2006 Linking - Summary • Linker – key part of OS – not in kernel – Combines object files and libraries into a “standard” format that the OS loader can interpret – Resolves references and does static relocation of addresses – Creates information for loader to complete binding process – Supports dynamic shared libraries