Linking & Loading

Linking & Loading

CSC501 Operating SystemsPrinciples Linking & Loading 1 Previous Lectures q Midterm q Today Q Linking and Loading 2 Question: What happened to your program after it is compiled, but before it can be run? 3 Background: Executable Files q The OS expects executable files to have a specific format Q Header info v Code locations v Data locations Q Code & data Q Symbol Table v List of names of things defined in your program and where they are defined v List of names of things defined elsewhere that are used by your program, and where they are used. 4 Example: ELF Files (x86/Linux) Linkable sections Executable segments ELF Header Program Header (optional, ignored) describes sections Table sections segments Section Header describes sections (optional, ignored) Table 5 Example #include <stdio.h> q Symbol defined in your program and used elsewhere int main () { v main printf (“hello, q Symbol defined world\n”) elsewhere and used by your program v printf } 6 Example #include <stdio.h> q Symbol defined in extern interrno; your program and used elsewhere intmain () { v main printf(“hello, q Symbol defined world\n”) elsewhere and used by your program <check errnofor v errors> printf v } errno 7 From source code to a process Source (.c, .cc) Compiler Object Other Objects (.o) (.o) Linker Static libraries (.a) Executable Dynamic libraries Loader (.dll) In-memory Image 8 From source code to a process q Most compilers produce relocatableobject code Q Addresses relative to zero or a prefixed location q The linker combines multiple object files and library modules into a single executable file Q Addresses also relative to zero or a prefixed location Q Resolving symbols defined within these files Q Listing symbols needing to be resolved by loader q The Loader reads the executable file Q Allocates memory Q Maps addresses within file to physical memory addresses Q Resolves names of dynamic library items 9 Compiling q Why isn't everything written and compiled as just one big program, saving the necessity of linking? Q Efficiency: if just one function is changed in a 100K line program, why recompile the whole program? Just recompile the one function and relink Q Multiple-language programs Q Other reasons? 10 Linking q Linker collects procedures and links them together object modules into one executable program q Two approaches Q Static linking Q Dynamic dinking 11 Static Linking Printf.c HelloWorld.c gcc Static gcc Library Printf.o ar HelloWorld.o Linker a.out (or name of your command) Loader Memory 12 Static Linking --Classic Unix q Linker is inside of gcc command q Loader is part of exec system call q Executable image contains all object and library modules needed by program q Entire image is loaded at once q Every image contains copy of common library routines q Every loaded program contain duplicate copy of library routines 13 Dynamic Linking q Complete linking postponed until execution time. q Stub used to locate the appropriate memory-resident library routine. q Stub replaces itself with the address of the routine, and executes the routine. q Operating system needs to check if routine is in processes’ memory address space. 14 Dynamic Linking q Dynamic vs. static linking $ gcc-static hello.c-o hello-static $ gcchello.c-o hello-dynamic $ ls-l hello 80 hello.c 13724 hello-dynamic 383 hello.s 1688756 hello-static q If you are the sys admin, which do you prefer? 15 Advantages of Dynamic Linking q The executable is smaller (it not include the library information explicitly), q When the library is changed, the code that references it does not usually need to be recompiled. q The executable accesses the .DLL at run time; therefore, multiple codes can access the same .DLL at the same time (saves memory) 16 Disadvantages of Dynamic Linking q Performance hit ~10% Q Need to load shared objects (once) Q Need to resolve addresses (once or every time) q What if the necessary dynamic library is missing? q Could have the library, but wrong version 17 Unix Dynamic Objects (.so) q Compiler Options (cont) Q -static link only to static (.a=archive) libraries Q -shared if possible, prefer shared libraries over static Q -nostartfilesskip linking of standard start files, like /usr/lib/crt[0,1].o, /usr/lib/crti.o, etc q Linker Options (gccgives unknown options to linker) Q -l lib (default naming convention liblib.a) Q -L lib path (in addition to default /usr/lib and /lib) Q -s strip final executable code of symbol and relocation tables 18 Loading q It loads a program file for execution q Two approaches Q Static loading Q Dynamic loading q Advantages of dynamic loading Q Better memory-space utilization; unused routine is never loaded. Q Useful when large amounts of code are needed to handle infrequently occurring cases 19 Dynamic Loading q Program-controlled dynamic loading q Linker-assisted dynamic loading 20 Program-controlled Dynamic Loading q Requires: Q A load system call to invoke loader Q ability to leave symbols unresolved and resolve at run time q E.g., void myPrintf(**arg) { static intloaded = 0; if (!loaded ) { load (“printf”); loaded = 1; printf(arg); } } 21 Linker-assisted Dynamic Loading q Programmer marks modules as “dynamic” to linker q For function call to a dynamic function v Call is indirect through a link table v Each link table initialized with address small stub of code to locate and load module. v When loaded, loader replaces link table entry with address of loaded function v When unloaded, loader replaces table entry with stub address v Static data cannot be made dynamic 22 Shared Libraries q Observation –“everyone” links to standard libraries (libc.a, etc.) q Consume space in v every executable image v every process memory at runtime q Would it be possible to share the common libraries? Q Automatically load at runtime? 23 Shared libraries (continued) q Libraries designated as “shared” v .so, .dll, etc. v Supported by corresponding “.a” libraries containing symbol information q Linker sets up symbols to be resolved at runtime q Loader: Is library already in memory? Q If so, map into new process space v “map,” an operation to be defined Q If not, load and then map 24 Dynamic Shared Libraries q Static shared libraries requires address space pre-allocation q Dynamic shared libraries –address binding at runtime Q Code must be position independent Q At runtime, references are resolved as v Library_relative_address + library_base_address 25 Run-time Linking/Loading Printf.c HelloWorld.c gcc gcc Printf.o HelloWorld.o Linker ar Save disk space. Startup faster. a.out Might not need all. Shared (or name of Library your command) Run-time Loader Loader Memory 26 Next Lecture q Enjoy the Spring Break q Memory Management 27.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    27 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us