Professional Documents
Culture Documents
Linking and Loading
Linking and Loading
Linking and Loading
1
Contents
Introduction to Compilation
Compilation steps
Linking & Loading
Static Linking
Dynamic Linking
Libraries
DLC functions
2
Introduction
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
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
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
11
Static Linking
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
15
Libraries
16
Static Libraries or Static Archives
17
Dynamic Libraries or Shared Objects
18
Static vs. Dynamic Linking
19
Dynamic Loading
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
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