Content deleted Content added
→Platforms without dynamic loading: Rephrase sentence Tags: Mobile edit Mobile web edit |
|||
(33 intermediate revisions by 24 users not shown) | |||
Line 1:
{{Short description|Mechanism by which a computer program can load a library (or other binary) into memory}}
{{distinguish|text=[[active load|dynamic load]]}}
{{Redirect-distinguish2|Dynamically loaded library|[[dynamic-link library|dynamically linked library]]}}
{{Use dmy dates|date=July 2019|cs1-dates=y}}
'''Dynamic loading''' is a mechanism by which a [[computer program]] can, at [[Run time (program lifecycle phase)|run time]], load a [[Library (computing)|library]] (or other [[Executable file|binary]]) into memory, retrieve the addresses of functions and variables contained in the library, execute those [[library function|functions]] or access those variables, and unload the library from memory. It is one of the
==History==
Line 10 ⟶ 11:
* Libraries could be protected from unauthorized modification
[[IBM]]'s strategic [[transaction processing]] system, [[CICS]] (1970s onwards) uses dynamic loading extensively both for its [[
[[Shared libraries]] were added to Unix in the 1980s, but initially without the ability to let a program load additional libraries after startup.<ref name="spe"/>
Line 18 ⟶ 19:
==In C/C++==
Not all systems support dynamic loading. [[
===Summary===
Line 24 ⟶ 25:
|-
! Name
! [[
! [[Windows API|Microsoft Windows API]]
|-
Line 50 ⟶ 51:
===Loading the library===
Loading the library is accomplished with <code>LoadLibrary</code> or <code>LoadLibraryEx</code> on [[Microsoft Windows|Windows]] and with <code>dlopen</code> on [[
====Most
<syntaxhighlight lang="c">
void* sdl_library = dlopen("libSDL.so", RTLD_LAZY);
if (!sdl_library
// report error ...
} else {
Line 63 ⟶ 64:
====macOS====
As a [[
<syntaxhighlight lang="c">
void* sdl_library = dlopen("
if (!sdl_library
// report error ...
} else {
Line 78 ⟶ 79:
<syntaxhighlight lang="c">
void* sdl_library = dlopen("/Library/Frameworks/SDL.framework/SDL", RTLD_LAZY);
if (!sdl_library
// report error ...
} else {
Line 103 ⟶ 104:
<syntaxhighlight lang="c">
HMODULE sdl_library = LoadLibrary(TEXT("SDL.dll"));
if (!sdl_library
// report error ...
} else {
Line 111 ⟶ 112:
===Extracting library contents===
Extracting the contents of a dynamically loaded library is achieved with <code>GetProcAddress</code> on [[Microsoft Windows|Windows]] and with <code>dlsym</code> on [[
====
<syntaxhighlight lang="c">
void* initializer = dlsym(sdl_library, "SDL_Init");
if (!initializer
// report error ...
} else {
Line 137 ⟶ 138:
====Windows====
<syntaxhighlight lang="c">
FARPROC initializer = GetProcAddress(sdl_library, "SDL_Init");
if (!initializer
// report error ...
} else {
Line 145 ⟶ 146:
</syntaxhighlight>
===Converting
The result of <code>dlsym()</code> or <code>GetProcAddress()</code> has to be converted to a pointer of the
====Windows====
In
<syntaxhighlight lang="c">
Line 162 ⟶ 163:
</syntaxhighlight>
====
According to the POSIX specification, the result of <code>dlsym()</code> is a <code>void</code> pointer. However, a function pointer is not required to even have the same size as a data object pointer, and therefore a valid conversion between type <code>void*</code> and a pointer to a function may not be easy to implement on all platforms.
Line 174 ⟶ 175:
<syntaxhighlight lang="c">
typedef void (*sdl_init_function_type)(void);
union { sdl_init_function_type func; void
alias.obj = initializer;
sdl_init_function_type init_func = alias.func;
Line 191 ⟶ 192:
===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 [[computer file|file]] which contains the library. Unloading the library is accomplished with <code>FreeLibrary</code> on [[Microsoft Windows|Windows]] and with <code>dlclose</code> on
====
<syntaxhighlight lang="c">
dlclose(sdl_library);
Line 204 ⟶ 205:
===Special library===
The implementations of dynamic loading on [[
[[Microsoft Windows|Windows]] allows programmers to access symbols exported by the main executable. Windows does not use a global symbol table
====
<syntaxhighlight lang="c">
void* this_process = dlopen(NULL, 0);
</syntaxhighlight>
Line 220 ⟶ 221:
HMODULE this_process_again;
GetModuleHandleEx(0, 0, &this_process_again);
</syntaxhighlight>
==In Java==
{{further|Java
In the [[Java programming language]], [[Java class|classes]] can be dynamically loaded using the '''{{Javadoc:SE|java/lang|ClassLoader}}''' object. For example:
Line 246 ⟶ 247:
==Platforms without dynamic loading==
Despite its promulgation in the 1980s through
==See also==
Line 262 ⟶ 263:
* [[FlexOS]]<!-- might at a later stage be incorporated in the article body as example of a system supporting dynamic loading/unloading of modular subdrivers -->
* [[GNU linker]]
* [[gold (linker)]]▼
* [[Lazy loading]]
* [[Library (computing)]]
* [[Linker (computing)]]
Line 270 ⟶ 273:
* [[Relocation (computer science)]]
* [[Relocation table]]
* [[Resident System Extension]] (RSX)<!-- dynamic (but not delayed) loading/unloading of modules under CP/M-related systems -->
* [[Static library]]
* [[Terminate-and-stay-resident program]] (TSR)<!-- dynamic (but not delayed) loading of system extensions under DOS-related systenms -->
▲* [[gold (linker)]]
{{div col end}}
Line 281 ⟶ 284:
<ref name="CICS">{{Cite web |url=http://publib.boulder.ibm.com/infocenter/cicsts/v3r1/index.jsp?topic=/com.ibm.cics.ts31.doc/dfhp3/dfhp3oq.htm |title=Using the CICS-supplied procedures to install application programs}}</ref>
<ref name="CEMT">{{Cite web |url=http://www-01.ibm.com/support/docview.wss?uid=swg21031546 |title=IBM CEMT NEWCOPY or PHASEIN request fails with NOT FOR HOLD PROG - United States |date=2013-03-15}}</ref>
<ref name="spe">{{cite journal |title=An approach to genuine dynamic linking |author-first1=W. Wilson |author-last1=Ho |author-first2=Ronald A. |author-last2=Olsson |date=1991 |journal=
<ref name="apache">
<ref name="gcc-strict-aliasing">[https://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Optimize-Options.html#index-fstrict_002daliasing-721 GCC 4.3.2 Optimize Options: -fstrict-aliasing]</ref>
<ref name="POSIX_dlopen">[http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html POSIX documentation on <code>dlopen()</code>] (issues 6 and 7).</ref>
Line 299 ⟶ 302:
** [https://github.com/danfuzz/dl-example Dynamic Library Loading Example] (complete but concise working example)
** [https://developer.apple.com/library/mac/#DOCUMENTATION/DeveloperTools/Conceptual/DynamicLibraries/000-Introduction/Introduction.html#//apple_ref/doc/uid/TP40001908-SW1 Dynamic Library Programming Topics from Apple Developer Connection (targeted to macOS)]
* C/C++
** [http://www.opengroup.org/onlinepubs/009695399/functions/dlopen.html dlopen]
** [http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html dlsym]
|