Dynamic loading

Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory. Unlike static linking and loadtime linking, this mechanism allows a computer program to startup in the absence of these libraries, to discover available libraries, and to potentially gain additional functionality.[1][2]

Contents

History

Dynamic loading was a common technique for IBM/360 Operating systems (1960s to, the - still extant - Z/Architecture), particularly for I/O subroutines, and for COBOL and PL/1 runtime libraries. As far as the application programmer is concerned, the loading is largely transparent, since it is mostly handled by the operating system (or its I/O subsystem). The main advantages are:

  • Fixes (patches) to the subsystems fixed all programs at once, without the need to relink them
  • Libraries could be protected from unauthorized modification

IBM's strategic transaction processing system, CICS (1970s onwards) uses dynamic loading extensively both for its kernel and for normal application program loading. Corrections to application programs could be made offline and new copies of changed programs loaded dynamically without needing to restart CICS [3][4](that can, and frequently does, run 24/7).

Uses

Dynamic loading is most frequently used in implementing software plugins.[1] For example, the Apache Web Server's *.dso "dynamic shared object" plugin files are libraries which are loaded at runtime with dynamic loading.[5] Dynamic loading is also used in implementing computer programs where multiple different libraries may supply the requisite functionality and where the user has the option to select which library or libraries to provide.

In C/C++

Not all systems support dynamic loading. UNIX-like operating systems such as Mac OS X, Linux, and Solaris provide dynamic loading with the C programming language "dl" library. The Windows operating system provides dynamic loading through the Windows API.

Summary

Name Standard POSIX/UNIX API Microsoft Windows API
Header file inclusion #include <dlfcn.h> #include <windows.h>
Definitions for header dl

(libdl.so, libdl.dylib, etc. depending on the OS)

Kernel32.dll
Loading the library dlopen LoadLibrary
LoadLibraryEx
Extracting contents dlsym GetProcAddress
Unloading the library dlclose FreeLibrary

Loading the Library

Loading the library is accomplished with LoadLibrary or LoadLibraryEx on Windows and with dlopen on UNIX-like operating systems. Examples follow:

Most UNIX-like operating systems (Linux, *BSD, Solaris, etc.)

void* sdl_library = dlopen("libSDL.so", RTLD_LAZY);
if(sdl_library == NULL) {
   // report error ...
} else {
   // use the result in a call to dlsym
}

Mac OS X

As a UNIX library:

void* sdl_library = dlopen("libsdl.dylib", RTLD_LAZY);
if(sdl_library == NULL) {
   // report error ...
} else {
   // use the result in a call to dlsym
}

As an OS X Framework:

void* sdl_library = dlopen("/Library/Frameworks/SDL.framework/SDL", RTLD_LAZY);
if(sdl_library == NULL) {
   // report error ...
} else {
   // use the result in a call to dlsym
}

Windows

HMODULE sdl_library = LoadLibrary("SDL.dll");
if( sdl_library == NULL) {
   // report error ...
} else {
   // use the result in a call to GetProcAddress
}

Extracting Library Contents

Extracting the contents of a dynamically loaded library is achieved with GetProcAddress on Windows and with dlsym on UNIX-like operating systems.

UNIX-like operating systems (Linux, *BSD, Mac OS X, Solaris, etc.)

void* initializer = dlsym(sdl_library,"SDL_Init");
if(initializer == NULL) {
   // report error ...
} else {
   // cast initializer to its proper type and use
}

Windows

FARPROC initializer = GetProcAddress(sdl_library,"SDL_Init");
if(initializer == NULL) {
   // report error ...
} else {
   // cast initializer to its proper type and use
}

Converting Extracted Library Contents

The result of dlsym() or GetProcAddress() has to be converted to the desired destination before it can be used.

Windows

In the Windows case, the conversion is straightforward, since FARPROC is essentially already a function pointer:

typedef INT_PTR (*FARPROC)(void);

This can be problematic when the address of an object is to be retrieved rather than a function. However, usually one wants to extract functions anyway, so this is normally not a problem.

typedef void (*sdl_init_function_type)(void);
sdl_init_function_type init_func = (sdl_init_function_type) initializer;

UNIX (POSIX)

According to the POSIX specification, the result of dlsym() is a void pointer. Therefore, the specification actually contains a defect, since both ISO C and the C++ programming language prohibit conversion between object pointers and function pointers (in fact, a function pointer is not required to even have the same size as an object pointer). Therefore, strictly speaking, a legal conversion between type void* and a pointer to a function cannot exist.

On most systems in use today, function and object pointers are de facto convertible. The following code snippet demonstrates one workaround which allows to perform the conversion anyway on many systems:

typedef void (*sdl_init_function_type)(void);
sdl_init_function_type init_func = (sdl_init_function_type)initializer;

The above snippet will give a warning on some compilers: warning: dereferencing type-punned pointer will break strict-aliasing rules. Another workaround is:

typedef void (*sdl_init_function_type)(void);
union { sdl_init_function_type func; void * obj; } alias;
alias.obj = initializer;
sdl_init_function_type init_func = alias.func;

which disables the warning even if strict aliasing is in effect. This makes use of the fact that reading from a different union member than the one most recently written to (called "type punning") is common, and explicitly allowed even if strict aliasing is in force, provided the memory is accessed through the union type directly.[6] However, this is not strictly the case here, since the function pointer is copied to be used outside the union.

Solving the function pointer problem on POSIX systems

The fact remains that any conversion between function and object pointers has to be regarded as an (inherently non-portable) implementation extension, and that no "correct" way for a direct conversion exists, since in this regard the POSIX and ISO standards contradict each other.

Because of this problem, the POSIX documentation on dlsym() (issue 6) stated that "a future version may either add a new function to return function pointers, or the current interface may be deprecated in favor of two new functions: one that returns data pointers and the other that returns function pointers".[7] However, the most current version of the standard (issue 7, 2008) simply states that function pointers have to be convertible to void* for POSIX compliance,[8] leaving compiler makers to choose which standard they adhere to.

If the contents of the library can be changed (i.e. in the case of a custom library), in addition to the function itself a pointer to it can be exported. Since a pointer to a function pointer is itself an object pointer, this pointer can always be legally retrieved by call to dlsym() and subsequent conversion. However, this approach requires maintaining separate pointers to all functions that are to be used externally, and the benefits are usually small.

Unloading the Library

Loading a library causes memory to be allocated; the library must be deallocated in order to avoid a memory leak. Additionally, failure to unload a library can prevent filesystem operations on the file which contains the library. Unloading the library is accomplished with FreeLibrary on Windows and with dlclose on UNIX-like operating systems. However, unloading a DLL can lead to program crashes if objects in the main application refer to memory allocated within the DLL. For example, if a DLL introduces a new class and the DLL is closed, further operations on instances of that class from the main application will likely cause a memory access violation. Likewise, if the DLL introduces a factory function for instantiating dynamically-loaded classes, calling or dereferencing that function after the DLL is closed leads to undefined behaviour.

UNIX-like operating systems (Linux, *BSD, Mac OS X, Solaris, etc.)

dlclose(sdl_library);

Windows

FreeLibrary(sdl_library);

Special Library

Both Windows and UNIX implementations of dynamic loading allow programmers to extract symbols from the currently executing process. In both of these APIs, the currently executing process can be "loaded" such that the result can be used in the same manner as the result from dynamically loading a library with LoadLibrary or dlopen.

UNIX-like operating systems (Linux, *BSD, Mac OS X, Solaris, etc.)

void* this_process = dlopen(NULL,0);

Windows

HMODULE this_process;
GetModuleHandleEx(0,0,&this_process);
 
HMODULE this_process_again = GetModuleHandle(0);

In Java

In the Java programming language, classes can be dynamically loaded using the ClassLoader object. For example:

Class type = ClassLoader.getSystemClassLoader().loadClass(name);
Object obj = type.newInstance();

See also

External links

References


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Loading (disambiguation) — Loading is the insertion of impedance into a circuit to change the characteristics of the circuit.Loading may also refer to:* Carbohydrate loading, a strategy employed by endurance athletes to maximize the storage of glycogen in the muscles *… …   Wikipedia

  • Dynamic linker — In computing, a dynamic linker is the part of an operating system (OS) that loads and links the shared libraries for an executable when it is executed. The specific operating system and executable format determine how the dynamic linker functions …   Wikipedia

  • Loading — In electrical transmission lines, the term loading means the insertion of impedance into a circuit to change the characteristics of the circuit. The loading of a communication channel may be expressed as (a) the equivalent mean power and the peak …   Wikipedia

  • Dynamic mechanical analysis — Dynamic Mechanical Analyzer Acronym DMA Classification Thermal analysis Manufacturers Bose Electroforce Group, Mettler Toledo, Netzsch Instruments, PerkinElmer, TA Instruments, Triton Technology Ltd, Gabo Other techniques Related Isothermal… …   Wikipedia

  • Dynamic spectrum management — (DSM), also referred to as dynamic spectrum access (DSA), is a set of techniques based on theoretical concepts in network information theory and game theory that is being researched and developed to improve the performance of a communication… …   Wikipedia

  • Dynamic-link library — This article is about the OS/2 and Windows implementation. For dynamic linking of libraries in general, see Dynamic linker. Dynamic link library Filename extension .dll Internet media type application/x msd …   Wikipedia

  • Loading gauge — The clearance between train and tunnel is often small. London Underground train at Hendon. A loading gauge defines the maximum height and width for railway vehicles and their loads to ensure safe passage through bridges, tunnels and other… …   Wikipedia

  • Dynamic logic (digital electronics) — For the subject in theoretical computer science, see dynamic logic (modal logic). In integrated circuit design, dynamic logic (or sometimes clocked logic) is a design methodology in combinatorial logic circuits, particularly those implemented in… …   Wikipedia

  • Dynamic logic (digital logic) — In integrated circuit design,dynamic logic (or sometimes clocked logic) is a design methodology logic family in digital logic that was popular in the 1970s and has seen a recent resurgence in the design of high speed digital electronics,… …   Wikipedia

  • Dynamic positioning — Offshore Support Vessel Toisa Perseus with, in the background, the fifth generation deepwater drillship Discoverer Enterprise, over the Thunder Horse Oil Field. Both are equipped with DP systems. Dynamic positioning (DP) is a computer controlled… …   Wikipedia


Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”

We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.