Does the NT DLL Loader load DLLs in the order of the import section of the executable?
Asked Answered
G

1

2

If you have an executable on Windows, you can view its import section with the DUMPBIN utility (included e.g. in Visual Studio).

To get a list of all imported DLLs you can run something like this (just an arbitrary example):

C:\Programme\GIMP-2.0\bin>dumpbin /IMPORTS gimp-2.4.exe | grep -i \.dll
    libgimpcolor-2.0-0.dll
    libgimpmath-2.0-0.dll
    libgimpmodule-2.0-0.dll
    libgimpthumb-2.0-0.dll
    libgimpwidgets-2.0-0.dll
    libart_lgpl_2-2.dll
    libfontconfig-1.dll
    freetype6.dll
    libgdk-win32-2.0-0.dll
    libgdk_pixbuf-2.0-0.dll
    libglib-2.0-0.dll
    libgobject-2.0-0.dll
    libgthread-2.0-0.dll
    libgtk-win32-2.0-0.dll
    intl.dll
    libpango-1.0-0.dll
    libpangoft2-1.0-0.dll
    libgimpbase-2.0-0.dll
    libgimpconfig-2.0-0.dll
    KERNEL32.dll
    msvcrt.dll
    msvcrt.dll
    USER32.dll

I have now speculated in another question that, for independent DLLs the Loader (the component that maps the DLLs into the address space and calls their DllMain function) will load the DLLs in the order in which they appear in the import section.

Note: This can obviously only apply to independent DLLs because the loader will have to resolve dependencies so any DLL that is dependent on any other will always be loader after the other. So this question can only apply to independent (non-system) DLLs.

To stay with my (arbitrarily chosen) example above,

C:\Programme\GIMP-2.0\bin>dumpbin /IMPORTS libgimpcolor-2.0-0.dll | grep -i \.dll
Dump of file libgimpcolor-2.0-0.dll
    libglib-2.0-0.dll
    libgobject-2.0-0.dll
    msvcrt.dll

C:\Programme\GIMP-2.0\bin>dumpbin /IMPORTS libgimpmath-2.0-0.dll | grep -i \.dll
Dump of file libgimpmath-2.0-0.dll
    libglib-2.0-0.dll
    libgobject-2.0-0.dll
    msvcrt.dll

C:\Programme\GIMP-2.0\bin>dumpbin /IMPORTS libgobject-2.0-0.dll | grep -i \.dll
Dump of file libgobject-2.0-0.dll
    libglib-2.0-0.dll
    KERNEL32.dll
    msvcrt.dll

C:\Programme\GIMP-2.0\bin>dumpbin /IMPORTS libglib-2.0-0.dll | grep -i \.dll
Dump of file libglib-2.0-0.dll
    iconv.dll
    intl.dll
    ADVAPI32.DLL
    KERNEL32.dll
    msvcrt.dll
    msvcrt.dll
    OLE32.dll
    SHELL32.DLL
    USER32.dll
    WS2_32.DLL

libgimpmath and libgimpcolor are independent DLLs in that sense. So here the question would be: Will the Loader always load libgimpcolor before libgimpmath because it comes first in the import section?

Gus answered 17/6, 2011 at 7:17 Comment(1)
Ah! How I love those anonymous downvoters ...Gus
J
5

For independent DLLs, the load order is indeed the same as the order of the IAT.

From Michael Grier's MSDN Blog

The implementation is linear/sequential. Therefore even the order of the imports in your static import tables matters. [...] If the linker for some reason reverses the order of the static imports, you'll see the opposite.

Jangro answered 17/6, 2011 at 9:2 Comment(1)
Excellent article/link. Thanks!Gus

© 2022 - 2024 — McMap. All rights reserved.