Geoff Chappell - Software Analyst
The LDR_DATA_TABLE_ENTRY structure is NTDLL’s record of how a DLL is loaded into a process. In early Windows versions, this structure is similarly the kernel’s record of each module that is loaded for kernel-mode execution. The different demands of kernel and user modes eventually led to the separate definition of a KLDR_DATA_TABLE_ENTRY. For simplicity, though not historical completeness, this note on the LDR_DATA_TABLE_ENTRY is specialised to its user-mode handling.
Each process has its own list of loaded modules. In some sense, it has three lists: although there is only the one LDR_DATA_TABLE_ENTRY structure for each module, each is linked in three different orders. The way to find the lists is well known, including to malware. The Ldr member of the process’s PEB points to the process’s PEB_LDR_DATA which contains the list heads as its members named InLoadOrderModuleList, InMemoryOrderModuleList and InInitializationOrderModuleList.
Less well known—or less well respected in real-world practice, even by programmers who aren’t writing malware—is that the links in these lists are not safe to follow while modules might be loaded and unloaded. That this can’t happen at the time can be hard enough to ensure even for the current process. Sadly, the preceding sentence is much too subtle for many hackers and even for many commentators who would not imagine that they might be thought of dismissively as hackers.
In an ideal world, the LDR_DATA_TABLE_ENTRY might be opaque outside NTDLL. But various high-level modules supplied with Windows over the years have used at least one member of the LDR_DATA_TABLE_ENTRY, which eventually had to be disclosed. A new header, named WINTERNL.H, for previously internal APIs was added to the Software Development Kit (SDK) apparently in 2002, and remains to this day. Starting with the SDK for Windows 7, WINTERNL.H presents a modified LDR_DATA_TABLE_ENTRY that has just the InMemoryOrderLinks, DllBase, FullDllName, CheckSum and TimeDateStamp members, plus padding that gets these members to the same offsets as in the true structure. It seems unlikely that Microsoft will change the LDR_DATA_TABLE_ENTRY in any way that moves these members.
Indeed, given that LDR_DATA_TABLE_ENTRY at least started as an undocumented structure for internal use, it is surprisingly stable across Windows versions. Until a significant reworking for Windows 8, the structure grew only by extension and many of the original members—which happen to be the most useful in practice—keep their same positions through the whole history. The following table shows the changing sizes:
Version | Size (x86) | Size (x64) |
---|---|---|
3.10 to 3.51 | 0x44 | |
4.0 to 5.0 | 0x48 | |
early 5.1 (before SP2) | 0x4C | |
late 5.1 to 5.2 | 0x50 | 0x98 |
6.0 | 0x68 | 0xC8 |
6.1 | 0x78 | 0xE0 |
6.2 | 0x98 | 0x0110 |
6.3 to 1511 | 0xA0 | 0x0118 |
1607 to 2004 | 0xA8 | 0x0120 |
These sizes, and the offsets, types and names in the tables that follow, are from Microsoft’s symbol files for the kernel starting with Windows 2000 SP3 and for NTDLL starting with Windows XP. Symbol files for earlier versions do not contain type information for the LDR_DATA_TABLE_ENTRY, but type information for a smattering of earlier versions was published in statically linked libraries: GDISRVL.LIB from the Device Driver Kit (DDK) for Windows NT 3.51; and SHELL32.LIB from the DDK for Windows NT 4.0. Inspection of binaries anyway confirms that all but one member had near enough the same usage as far back as Windows NT 3.10.
Offset (x86) | Offset (x64) | Definition | Versions |
---|---|---|---|
0x00 | 0x00 |
LIST_ENTRY InLoadOrderLinks; |
all |
0x08 | 0x10 |
LIST_ENTRY InMemoryOrderLinks; |
all |
0x10 | 0x20 |
LIST_ENTRY InInitializationOrderLinks; |
3.10 to 6.1 |
union { LIST_ENTRY InInitializationOrderLinks; LIST_ENTRY InProgressLinks; }; |
6.2 and higher | ||
0x18 | 0x30 |
PVOID DllBase; |
all |
0x1C | 0x38 |
PVOID EntryPoint; |
all |
0x20 | 0x40 |
ULONG SizeOfImage; |
all |
0x24 | 0x48 |
UNICODE_STRING FullDllName; |
all |
0x2C | 0x58 |
UNICODE_STRING BaseDllName; |
all |
0x34 | 0x68 |
ULONG Flags; |
3.10 to 6.1 |
union { UCHAR FlagGroup [4]; ULONG Flags; struct { /* bit fields, follow link */ }; }; |
6.2 and higher | ||
0x38 | 0x6C |
USHORT LoadCount; |
3.10 to 6.1 |
USHORT ObsoleteLoadCount; |
6.2 and higher | ||
0x3A | 0x6E |
USHORT TlsIndex; |
all |
0x3C | 0x70 |
union { LIST_ENTRY HashLinks; struct { PVOID SectionPointer; ULONG CheckSum; }; }; |
3.10 to 6.1 |
LIST_ENTRY HashLinks; |
6.2 and higher |
A practical reason to know of this structure is for the debugging exercise of finding why a DLL did not get unloaded when expected or did get unloaded but by surprise. Both are questions of DLL reference counting. Before Windows 8, the LoadCount member of this structure is the reference count. The LDR_DATA_TABLE_ENTRY for the DLL in question is most easily found when the DLL has just loaded. A program’s loading and unloading of the DLL can then be tracked easily by setting a write-memory breakpoint on the LoadCount member. At each break to the debugger, look at what the count has changed to and look at a stack dump to see who made the change.
Offset (x86) | Offset (x64) | Definition | Versions |
---|---|---|---|
0x44 | 0x80 |
ULONG TimeDateStamp; |
4.0 only |
union { ULONG TimeDateStamp; PVOID LoadedImports; }; |
5.0 to 6.1 | ||
ULONG TimeDateStamp; |
6.2 and higher |
Offset (x86) | Offset (x64) | Definition | Versions |
---|---|---|---|
0x48 | 0x88 |
PVOID EntryPointActivationContext; |
5.1 and higher |
0x4C | 0x90 |
PVOID PatchInformation; |
late 5.1 to 6.2 |
PVOID Spare; |
6.3 only | ||
PVOID Lock; |
10.0 and higher |
Insertion of the LDR_DATA_TABLE_ENTRY into three more lists for Windows Vista soon enough got undone when Windows 8 greatly reworked the tracking of DLLs as they get loaded and unloaded. These members’ positions have an entirely different use in Windows 8 and higher.
Offset (x86) | Offset (x64) | Definition | Versions |
---|---|---|---|
0x50 (6.0 to 6.1) | 0x98 (6.0 to 6.1) |
LIST_ENTRY ForwarderLinks; |
6.0 to 6.1 |
0x58 (6.0 to 6.1) | 0xA8 (6.0 to 6.1) |
LIST_ENTRY ServiceTagLinks; |
6.0 to 6.1 |
0x60 (6.0 to 6.1) | 0xB8 (6.0 to 6.1) |
LIST_ENTRY StaticLinks; |
6.0 to 6.1 |
Offset (x86) | Offset (x64) | Definition | Versions |
---|---|---|---|
0x50 | 0x98 |
LDR_DDAG_NODE *DdagNode; |
6.2 and higher |
0x54 | 0xA0 |
LIST_ENTRY NodeModuleLink; |
6.2 and higher |
0x5C | 0xB0 |
LDRP_DLL_SNAP_CONTEXT *SnapContext; |
6.2 to 6.3 |
LDRP_LOAD_CONTEXT *LoadContext; |
10.0 and higher | ||
0x60 | 0xB8 |
PVOID ParentDllBase; |
6.2 and higher |
0x64 | 0xC0 |
PVOID SwitchBackContext; |
6.2 and higher |
0x68 | 0xC8 |
RTL_BALANCED_NODE BaseAddressIndexNode; |
6.2 and higher |
0x74 | 0xE0 |
RTL_BALANCED_NODE MappingInfoIndexNode; |
6.2 and higher |
One addition for Windows 7 also got caught up in the reorganisation for Windows 8. Others are retained but shifted.
Offset (x86) | Offset (x64) | Definition | Versions |
---|---|---|---|
0x68 (6.1) | 0xC8 (6.1) |
PVOID ContextInformation; |
6.1 only |
0x6C (6.1); 0x80 |
0xD0 (6.1); 0xF8 |
ULONG_PTR OriginalBase; |
6.1 and higher |
0x70 (6.1); 0x88 |
0xD8 (6.1); 0x0100 |
LARGE_INTEGER LoadTime; |
6.1 and higher |
Offset (x86) | Offset (x64) | Definition | Versions |
---|---|---|---|
0x90 | 0x0108 |
ULONG BaseNameHashValue; |
6.2 and higher |
0x94 | 0x010C |
LDR_DLL_LOAD_REASON LoadReason; |
6.2 and higher |
Offset (x86) | Offset (x64) | Definition | Versions |
---|---|---|---|
0x98 | 0x0110 |
ULONG ImplicitPathOptions; |
6.3 and higher |
When Windows 8 extended the LoadCount from its old 16 bits and moved it to the new LDR_DDAG_NODE, it defined a ReferenceCount, distinct from the LoadCount, and placed it in the LDR_DDAG_NODE with the new LoadCount. Windows 10 leaves the LoadCount there but moves the ReferenceCount here.
Offset (x86) | Offset (x64) | Definition | Versions |
---|---|---|---|
0x9C | 0x0114 |
ULONG ReferenceCount; |
10.0 and higher |
0xA0 | 0x0118 |
ULONG DependentLoadFlags; |
1607 and higher |
0xA4 | 0x011C |
SE_SIGNING_LEVEL SigningLevel; |
1703 and higher |