CRT initialization and DLLMain

自作多情 提交于 2019-12-04 12:57:51

You are working from the assumption that the entrypoint for a DLL is always _DllMainCRTStartup. This is not the case, it is merely the linker's default. It can be anything a programmer wants it to be, swiftly and easily changed with the linker's /ENTRYPOINT option. There is nothing that Microsoft can do to prevent this. Not a very good practice, pointing that out is was the point of that document.

So the [2] mishap is easily invoked if such a custom entrypoint doesn't also ensure to initialize the CRT explicitly. Which doesn't just involve initializing the CRT runtime state, it also involves initializing global state of the DLL, like calling C initializers, the constructors of static C++ objects and allocating thread-local variables. Something the DLL version of the CRT cannot do. Keep in mind that _DllMainCRTStartup and _CRT_INIT are linked into the DLL itself, that code is not in the DLL version of the CRT.

The dynamic CRT's own runtime state is initialized by the CRT DLL's own entrypoint, the Windows loader ensures it runs first.

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