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

一笑奈何 提交于 2019-12-11 04:39:47

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6382560/does-the-nt-dll-loader-load-dlls-in-the-order-of-the-import-section-of-the-execu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!