Linking and Loading

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

Static and Dynamic Linking

1
Contents

 Introduction to Compilation
 Compilation steps
 Linking & Loading
 Static Linking

 Dynamic Linking

 Libraries
 DLC functions

2
Introduction

 Compilation in Unix Systems:-


cc sample.c (or cc sample.c -o sample)
 Running the executable:-
sample (or ./sample)

3
Compilation steps

 Preprocessor
 C-Compiler
 Assembler
 Linker-Loader

4
Compilation steps

 Preprocessor
 C-Compiler
 Assembler
 Linker-Loader

5
Compilation steps
 Preprocessor:
Normally called "cpp". It takes a C source file, and handles all the
pre-processor definitions (#include files, #define macros, conditional
source code inclusion with #ifdef, etc.) Can be invoked separately on
a program, usually with a command like:

cpp single_source.c

 C-Compiler
 Assembler
 Linker-Loader

6
Compilation steps
 Preprocessor
 C-Compiler:
This is the actual compiler, that translates the input file into assembly
language. As you saw, we used the "-c" flag to invoke it, along with
the C Pre-Processor, (and possibly the optimizer too, read on), and
the assembler.
 Assembler
 Linker-Loader

7
Compilation steps
 Preprocessor
 C-Compiler
 Assembler:
sometimes called "as". This takes the assembly code generated by the
compiler, and translates it into machine language code kept in object
files. Only Assembly code can be generated with a command like:

cc -S single_source.c

 Linker-Loader
8
Compilation steps
 Preprocessor
 C-Compiler
 Optimizer
 Assembler
 Linker-Loader
This is the tool that takes all the object files (and C
libraries), links and loads them together, to form one
executable file, in a format the operating system supports.

9
Linking vs. Loading

 Linker :- The process of linking involves scanning the user's compiled


object code for references to routines which have not been defined by
the user, then attempting to find those routines in libraries.

 Loading:- Then the code for the library routines is loaded into the
executable file during the linking phase/executing phase.
 Static Linking:- if loaded in the linking phase
 Dynamic Linking:- if loaded at runtime

 A considerable overlap exists between the functions of linkers and


loaders. One way to think of them is: the linker does the symbol
resolution; the loader does the program loading;
10
Static Linking

 Under static linking, copies of the archive library object files that
satisfy still unresolved external references in your program are
incorporated in the executable at link time

 External references in your program are connected with their


definitions when the executable is created.

 This is more portable as it does not require the presence of the


library on the system where it is executed.
executed

11
Static Linking

The code for various library routines is


redundantly stored in the executable files of
many different programs wasting a lot of
disk space

12
Dynamic Linking
 Dynamic linking is accomplished by placing the name of a
sharable library in the executable image. The link editor records in
your executable only the name of the shared object and a small
amount of bookkeeping information for use by the dynamic linker
at run time.
 External references in your program are connected with their
definitions when the program is executed.
 It saves disk space, but the viability of dynamically linked
programs depends on being able to find the library files not only
at compile time, but again at run time.
 Various complications arise if libraries are updated or moved
between compile and run time, or if the program is compiled and
linked on one system and then moved to another where the
libraries are not stored in the same locations

13
Dynamic Linking
An advantage of dynamic linking is that multiple
programs can share a single copy of the library

14
Libraries & Header files

 The standard libraries supplied by the C compilation system


contain functions that you can use in your program to perform
input/output, string handling, and other high-level operations that
are not explicitly provided by the C language.

 Header files contain definitions and declarations that your


program will need if it calls a library function. They also contain
function-like macros that you can use in your program as you
would a function.

15
Libraries

 Different versions of the libraries are required for static and


dynamic linking.
 Static libraries are called archives and have suffix .a, shared
object libraries used for dynamic linking have suffix .so -- usually
both are available.
For example the math library in /usr/lib:
 libm.a
 libm.so

 Static or dynamic linking can be selected with compiler options


such as cc -dy for dynamic and cc -dn for static linking.
linking

16
Static Libraries or Static Archives

 Static libraries are collections or archives of object


files.

 During static linking, the link editor makes available


copies the object files in these archives that contain a
function that has been called in the program.

17
Dynamic Libraries or Shared Objects

 A shared object is a single object file that contains the


code for every function in a given library.

 When function in that library is called, and dynamically


link with the program, the entire contents of the shared
object are mapped into the virtual address space of the
process at run time.

18
Static vs. Dynamic Linking

 Effectively Portable  Not efficient in portability


 Non-efficient use of memory  Effective use of physical
 Standalone binaries, thus less memory
dependent on shared libraries  Small binaries but heavily
 In case of disk corruption or dependent on shared libraries
hardware errors a static binary  Dynamically linked executable
is not likely to be affected as it has more dependencies thus is
has less dependencies within effected.
the system.

19
Dynamic Loading

 Dynamic loading is the process in which one can attach a shared


library to the address space of the process during execution, look
up the address of a function in the library, call that function and
then detach the shared library when it is no longer needed.

 Using dynamic libraries this way speeds up the launch process.

 This can be achieved by using the functions that interact with the
dynamic loader, in the application.
 These functions are Dynamic loader compatibility (DLC)
functions

20
DLC Functions

 The DLC functions are declared in /usr/include/dlfcn.h. There are


five of them:
 dlopen: Opens a dynamic library and returns a

handle(dynamic library handle) to be used for further calls


 dlsym: Returns the address of a symbol exported by a

dynamically loaded library. One can then access the function


or data through the address returned from dlsym.
 dladdr: Returns information on the address provided

 dlerror: Returns a string that describes an error condition

encountered by the last call to dlopen, dlsym, or dlclose.


 dlclose:Closes a dynamically loaded library.

21
Example using DLC Functions
#include<stdio.h>
#include<stdlib.h>
#include<dlfcn.h> Shared Object -- libbar.so:
int main() …………………..
{ void print ()
void * handle = NULL; {
void (*bar)(); fprintf ( stderr, "Hello, Shared World!\n" );
handle = dlopen ( "./bar.so", RTLD_NOW ); }
if ( handle == NULL ) fprintf ( stderr, "fail 1: %s\n", dlerror() ); …………………………
else
{
fprintf ( stderr, "%x\n", handle ); Compiler commands:
bar = (void(*)(void))(dlsym ( handle, "print" ));
if ( bar == NULL ) fprintf ( stderr, "fail 2: %s\n", dlerror() );
else
{ fprintf ( stderr, "%d\n", bar );
cc foo.c -o foo -lbar
bar ( ); }
}
} 22
23

You might also like