A dynamic library is a
library
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
that contains
functions and data that can be consumed by a
computer program
A computer program is a sequence or set of instructions in a programming language for a computer to Execution (computing), execute. It is one component of software, which also includes software documentation, documentation and other intangibl ...
at
run-time as loaded from a file separate from the program
executable
In computer science, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes a computer "to perform indicated tasks according to encoded instruction (computer science), in ...
.
Dynamic linking
In computing, a dynamic linker is the part of an operating system that loads and links the shared libraries needed by an executable when it is executed (at " run time"), by copying the content of libraries from persistent storage to RAM, fill ...
or
late binding allows for using a dynamic library by linking program library references with the associated objects in the library either at
load-time or
run-time. At program build-time, the
linker
Linker or linkers may refer to:
Computing
* Linker (computing), a computer program that takes one or more object files generated by a compiler or generated by an assembler and links them with libraries, generating an executable program or shar ...
records what library objects the program uses. When the program is run, a ''dynamic linker'' or ''linking loader'' associates program library references with the associated objects in the library.
A dynamic library can be linked at build-time to a stub for each library resource that is resolved at run-time. Alternatively, a dynamic library can be loaded without linking to stubs.
Most modern operating systems use the same format for both a dynamic library and an executable
[Some older systems, e.g., Burroughs MCP, ]Multics
Multics ("MULTiplexed Information and Computing Service") is an influential early time-sharing operating system based on the concept of a single-level memory.Dennis M. Ritchie, "The Evolution of the Unix Time-sharing System", Communications of t ...
, also have a single format which affords two main advantages: it necessitates only one loader, and it allows an executable file to be used as a
shared library
In computing, a library is a collection of System resource, resources that can be leveraged during software development to implement a computer program. Commonly, a library consists of executable code such as compiled function (computer scienc ...
. Examples of file formats use for both dynamic library and executable files include
ELF
An elf (: elves) is a type of humanoid supernatural being in Germanic peoples, Germanic folklore. Elves appear especially in Norse mythology, North Germanic mythology, being mentioned in the Icelandic ''Poetic Edda'' and the ''Prose Edda'' ...
,
Mach-O, and
PE.
A dynamic library is called by different names in different contexts. In
Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
and
OS/2
OS/2 is a Proprietary software, proprietary computer operating system for x86 and PowerPC based personal computers. It was created and initially developed jointly by IBM and Microsoft, under the leadership of IBM software designer Ed Iacobucci, ...
the technology is called
dynamic-link library. In
Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
user space, it's called ''dynamic shared object'' (DSO), or usually just ''shared object'' (SO). In
Linux
Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
kernel it's called
loadable kernel module (LKM). In
OpenVMS, it's called ''shareable image''.
As an alternative to dynamic linking, a
static library
A static library or statically linked library contains functions and data that can be included in a consuming computer program at build-time such that the library does not need to be accessible in a separate file at run-time. If all libraries a ...
is included into the program executable so that the library is not required at run-time.
Dynamic loading
Dynamic loading is the process of loading a dynamic library at run-time and also unloading a library. A load can be initiated implicitly or explicitly by a program. An implicit request occurs if the program is configured to automatically load the dynamic library and this is setup at link-time. Explicit requests are made by a program via operating system API calls. For instance, Windows provides
LoadLibrary
,
LoadLibraryEx
,
FreeLibrary
and
GetProcAddress
.
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
-based systems, including most UNIX and UNIX-like systems, use
dlopen
,
dlclose
and
dlsym
.
A dynamic loader locates a dynamic library on request and implementation varies. Some loaders depend on the executable storing a full path to the library. Any change to the library name or location results in a run-time failure. More commonly, the library name without path information is stored in the executable, and the loader applies a search algorithm to find the file.
If a dynamic library that an program depends is unavailable (deleted, moved, renamed) or replaced with an incompatible version, the executable will fail at run-time. This is called
dependency hell
Dependency hell is a colloquial term for the frustration of some software users who have installed software packages which have dependencies on specific versions of other software packages.
The dependency issue arises when several packages ha ...
and on Windows
DLL hell. Issues can be minimized by naming library files with a version number and by following versioning rules that require changing the version when an incompatible change is made.
Windows
For a custom DLL
Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
checks the directory where it loaded the program; directories configured via
SetDllDirectory()
; the System32, System, and Windows directories; then the current working directory; and finally the directories specified by the PATH
environment variable
An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the va ...
. Applications written for the
.NET Framework (since 2002), also check the
Global Assembly Cache as the primary store of shared DLL files to remove the issue of
DLL hell.
Originally, for
COM,
Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
would only query the
registry to locate a DLL that provides an object class. Later, Windows allowed for loading from a DLL file co-located with the program executable.
To address "DLL hell", Windows applications are typically installed with private DLL files and the system prevents replacement of shared system DLLs with earlier versions.
OpenStep
OpenStep
OpenStep is an object-oriented application programming interface (API) specification developed by NeXT. It provides a framework for building graphical user interfaces (GUIs) and developing software applications. OpenStep was designed to be plat ...
used a more flexible system, collecting a list of libraries from a number of known locations (similar to the PATH concept) when the system first starts. Moving libraries around causes no problems at all, although users incur a time cost when first starting the system.
Unix-like systems
Most
Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
systems have a "search path" specifying file-system
directories in which to look for dynamic libraries. Some systems specify the default path in a
configuration file
A configuration file, a.k.a. config file, is a computer file, file that stores computer data, data used to configure a software system such as an application software, application, a server (computing), server or an operating system.
Some applic ...
, others hard-code it into the dynamic loader. Some
executable file formats can specify additional directories in which to search for libraries for a particular program. This can usually be overridden with an
environment variable
An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the va ...
, although it is disabled for
setuid and setgid programs, so that a user can't force such a program to run arbitrary code with root permissions. Developers of libraries are encouraged to place their dynamic libraries in places in the default search path. On the downside, this can make installation of new libraries problematic, and these "known" locations quickly become home to an increasing number of library files, making management more complex.
Prebinding
As an optimization, systems can compute the likely load address for each dynamic library on the system to minimize load-time when the library is needed. This optimization is known as
prebinding and prelinking on macOS and Linux, respectively. IBM
z/VM uses a similar technique, called ''Discontinuous Saved Segments'' (DCSS). Disadvantages of this technique include the time required to compute the addresses each time a dynamic library changes, the inability to use
address space layout randomization, and the requirement of sufficient virtual address space for use.
History
Dynamic linking was originally developed for the
Multics
Multics ("MULTiplexed Information and Computing Service") is an influential early time-sharing operating system based on the concept of a single-level memory.Dennis M. Ritchie, "The Evolution of the Unix Time-sharing System", Communications of t ...
operating system, starting in 1964, and the MTS (
Michigan Terminal System), built in the late 1960s.
Notes
References
Computer libraries
Operating system technology
Sources
* {{cite book, last=Levine, first=J.R., title=Linkers and Loaders, publisher=Elsevier Science, series=Operating Systems Series, year=2000, isbn=978-1-55860-496-4, url=https://books.google.com/books?id=Id9cYsIdjIwC, access-date=2025-02-02