Introduction
A .dll is a library of code and data that a program loads when it needs
it, instead of carrying it. On Windows it is a
PE file with the same headers, sections and tables
as a program. Microsoft’s specification says that such files “are considered
executable files for almost all purposes, although they cannot be directly run”. The
Windows API itself is a set of DLLs. A 16-bit DLL of Windows 3.x is an
NE file; an OS/2 one is NE or
LX. This page covers the 32-bit and 64-bit
Windows form; the structure shared with programs is on the
.EXE page and its
PE32 and
PE32+ pages, and is not repeated here.
A DLL dropped on any page opens in the explorer as a PE file, with its structure, its resources and its code, exactly as a program does.
What makes a DLL different
The differences are a flag, an entry point that is optional and means something else, and the tables that matter most.
| Part | In a program | In a DLL |
|---|---|---|
| COFF characteristics | IMAGE_FILE_DLL (0x2000) clear | IMAGE_FILE_DLL set |
| Entry point | The starting address of the process | Optional; when there is none, AddressOfEntryPoint must be zero |
| Export table | Usually none | The point of the file (data directory 0) |
| Base relocations | The linker strips them by default | Kept, so that the DLL can move |
| How it runs | The loader creates a process | The loader maps it into a process that already exists or is starting |
The flag is in the COFF file header. Nothing else in the headers is specific to DLLs; Windows decides what to do from this flag and from the tables.
The entry point and DllMain
A program’s entry point starts the process. A DLL’s entry point, if it has one, is
called by the loader, never run on its own. Microsoft’s name for it is
DllMain; the real name does not matter, because the header holds only the
address. The loader calls it with a handle (the DLL’s base address), a reason and a
reserved pointer:
| Reason | Value | When |
|---|---|---|
DLL_PROCESS_ATTACH | 1 | The DLL is loaded into the process, at start-up or by LoadLibrary. Returning false fails the load. |
DLL_PROCESS_DETACH | 0 | The DLL is unloaded because the load failed, FreeLibrary brought the count to zero, or the process ends. |
DLL_THREAD_ATTACH | 2 | A thread is created in the process after the DLL was loaded. |
DLL_THREAD_DETACH | 3 | A thread exits cleanly. |
The reserved pointer says more. At attach it is null for a dynamic load and non-null
for a static one. At detach it is null after FreeLibrary or a failed load
and non-null when the process is ending; in that case the DLL should not free its
resources, because other threads may have been stopped in the middle of using them.
The calls are serialised, and the threads inside them hold the loader lock. So the
entry point may do only simple work. It must not load or free other DLLs, and calls
into User, Shell and COM can fail because those components may not be loaded yet or may
already be gone. Calls into kernel32.dll that do not load other DLLs are
safe, as it is always present. Most DLLs do real set-up in a function that the program
calls after loading. A DLL that has no need of notifications can have no entry point;
one that wants no thread notifications can ask for them to stop with
DisableThreadLibraryCalls.
Exports
The export directory, found by the first data directory, names the DLL and holds three tables: the export address table, the name pointer table and the ordinal table. Every export has an ordinal, which is its index in the address table plus the directory’s ordinal base (usually 1). A name is optional: a function can be exported by ordinal alone. The ordinal table holds 16-bit indexes into the address table, without the base added. The reference is on the Exports page.
Forwarders
An entry of the address table is normally the RVA of code or data. If the RVA lies
inside the export directory’s own range, it is a forwarder instead, an RVA of a text
string that names a symbol in another DLL, such as MYDLL.expfunc or, by
ordinal, MYDLL.#27. The symbol is then both imported and exported, and
a program that imports it from this DLL is sent on to the other one.
Loading, sharing and relocation
A program finds a DLL’s exports through the import directory, filled in by the
loader before the program runs, or through LoadLibrary and
GetProcAddress at any time. When several programs use a DLL at once, each
has its own copy of the DLL’s data, and they share its code.
A DLL is not always loaded at the address it prefers. The
optional header holds a preferred
image base; if another module is there, the loader must move the DLL, and for that it
needs the base relocations. Programs
have them stripped by default and DLLs do not. A file with
IMAGE_FILE_RELOCS_STRIPPED (0x0001) must be loaded at its
preferred base address, and if that is taken the loader reports an error. The
DYNAMIC_BASE flag (0x0040) of the optional header’s
DllCharacteristics says that the DLL can be relocated at load time.
Despite its name, DllCharacteristics is in programs too.
Delay-load imports
Delay-load imports delay the loading of a DLL until the first call into it. They have their own directory (index 13 of the data directories), laid out like the ordinary import directory, with a table of the imports that may be needed, a table the helper routine fills in, and a place for the module handle that the helper stores when it loads the DLL. A program that never calls the function never loads the DLL. See Delay-load imports.
Kinds of DLL
Resource-only DLLs
A DLL can hold no code, only resources such as icons, strings, dialogs and version information. It needs no entry point, so its entry point address is zero, and a program loads it to read the resources. Windows splits its own programs this way: the language-neutral file keeps the code, and a .mui file beside it holds the resources of one language.
.NET DLLs
A .NET library has the same .dll extension and the DLL flag, and its
code is intermediate language described by metadata, found through the CLR runtime
header (index 14 of the data directories). Native exports are not how its
functions are found. See the .NET assembly
page.
Manifests and side-by-side assemblies
A DLL, like a program, can carry an application manifest, an XML file naming the
side-by-side assemblies that it needs. Microsoft says to embed it as a resource of
the .dll if possible; otherwise it is a file beside the DLL, named like
example.dll.manifest, with the resource number in the name when it is
not 1.
Specific forms of a DLL
Several extensions are specialised files with one job, mostly described by Microsoft as DLLs, with a set of functions that the system calls; each page says what its sources do and do not state:
- .ocx: ActiveX and COM controls, with self-registration.
- .ax: DirectShow filters.
- .cpl: Control Panel items.
- .drv: installable drivers, with a
DriverProcfunction. - .acm: audio compression drivers.
- .tsp: telephony service providers.
- .mui: language resources.
- .sys: kernel-mode drivers, which are not loaded as DLLs but share the format.
- .scr: screen savers, which are renamed executables.
- .efi: images for the EFI subsystems.
Not described
Known, not yet covered
- Reference: API sets and the search order that Windows uses to find a DLL by name. They belong to the loader, not to the file.
Links
Software
Developers
Reference
- PE Format – Microsoft’s specification: the DLL flag, the entry point, the DLL characteristics, the export table and forwarders, delay-load imports, base relocations
- DllMain entry point – the reasons, the return value and the limits
- Dynamic-Link Libraries – Microsoft’s overview, including shared code
- Application manifests – embedding a manifest in a .exe or .dll