Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

435 lines
16 KiB

  1. /***
  2. *dllcrt0.c - C runtime initialization routine for a DLL with linked-in C R-T
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * This the startup routine for a DLL which is linked with its own
  8. * C run-time code. It is similar to the routine _mainCRTStartup()
  9. * in the file CRT0.C, except that there is no main() in a DLL.
  10. *
  11. *Revision History:
  12. * 05-04-92 SKS Based on CRT0.C (start-up code for EXE's)
  13. * 08-26-92 SKS Add _osver, _winver, _winmajor, _winminor
  14. * 09-16-92 SKS This module used to be enabled only in LIBCMT.LIB,
  15. * but it is now enabled for LIBC.LIB as well!
  16. * 09-29-92 SKS _CRT_INIT needs to be WINAPI, not cdecl
  17. * 10-16-92 SKS Call _heap_init before _mtinit (fix copied from CRT0.C)
  18. * 10-24-92 SKS Call to _mtdeletelocks() must be under #ifdef MTHREAD!
  19. * 03-20-93 SKS Remove obsolete variables _osmode, _cpumode, etc.
  20. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  21. * 04-14-93 SKS _DllMainCRTStartup replaces _CRT_INIT. Also, call
  22. * _mtterm instead of _mtdeletelocks on PROCESS_DETACH
  23. * to do all multi-thread cleanup (e.g. free up TLS index)
  24. * 04-19-93 SKS Remove obsolete variable _atopsp
  25. * 04-20-93 SKS Call _cexit on DLL detach
  26. * 04-20-93 SKS Restore _CRT_INIT, must co-exist with DllMainCRTStartup
  27. * 04-26-93 SKS _mtinit now returns 0 or 1, no longer calls _amsg_exit
  28. * 04-27-93 GJF Removed support for _RT_STACK, _RT_INTDIV,
  29. * _RT_INVALDISP and _RT_NONCONT.
  30. * 05-06-93 SKS Add call to _heap_term to free up all allocated memory
  31. * *and* address space. This must be the last thing done.
  32. * 06-08-93 GJF Added __proc_attached flag.
  33. * 06-08-93 SKS Clean up failure handling in _CRT_INIT
  34. * 11-05-93 CFW Undefine GetEnviromentStrings.
  35. * 11-09-93 GJF Added call to __initmbctable (must happen before args
  36. * and env strings are processed).
  37. * 11-09-93 GJF Added (restored) support for NT SDK builds. Also,
  38. * replaced MTHREAD with _MT.
  39. * 11-20-93 CFW Wide char enable.
  40. * 11-23-93 CFW GetEnviromentStrings undef moved to internal.h.
  41. * 11-29-93 CFW Wide environment.
  42. * 12-06-93 CFW Wide char enable.
  43. * 12-13-93 SKS Free up per-thread CRT data on DLL_THREAD_DETACH
  44. * using a call to _freeptd() in _CRT_INIT()
  45. * 03-30-93 CFW Use __crtXXX calls for Unicode model.
  46. * 04-08-93 CFW Move __crtXXX calls past initialization.
  47. * 06-08-94 SKS Add functn pointer _pRawDllMain, called around DllMain.
  48. * 09-06-94 CFW Remove _MBCS_OS switch.
  49. * 09-06-94 GJF Added __error_mode and __app_type.
  50. * 09-15-94 SKS Move #ifndef directives to after file header comment
  51. * 12-27-94 CFW Remove unused _wDll support.
  52. * 01-16-95 CFW Set default debug output for console.
  53. * 02-22-95 JCF Spliced _WIN32 & Mac versions.
  54. * 03-10-95 JCF Return the user error from DllInit instead of noErr.
  55. * 03-28-95 BWT Fail if unable to retrieve cmdline or envptr (fixes
  56. * stress bug).
  57. * 04-06-95 CFW Set default debug output for Mac, dump leaks for user
  58. * DLL.
  59. * 04-06-95 CFW Use __crtGetEnvironmentStringsA.
  60. * 04-12-95 CFW __crtGetEnvironmentStringsA must be after mtinit().
  61. * 04-14-95 CFW env, arg test must also be.
  62. * 04-26-95 CFW Change default debug output for Mac to debugger.
  63. * 05-23-95 CFW Dump memory leaks before mtterm().
  64. * 06-27-95 CFW Always set __app_type.
  65. * 06-28-95 GJF Added call to _ioterm() to clean up lowio at DLL
  66. * unload time.
  67. * 07-04-95 GJF Interface to __crtGetEnvironmentStrings and _setenvp
  68. * changes slightly.
  69. * 07-07-95 CFW Simplify default report mode scheme.
  70. * 04-23-96 GJF Check for failure of heap initialization.
  71. * 05-14-96 GJF Changed how failure during process attach is handled.
  72. * 02-18-97 GJF Removed obsolete Win32s and PharLap TNT support. Also,
  73. * some cosmetic changes.
  74. * 07-24-97 GJF Minor changes to support stubbing out arg and env
  75. * processing, and our heap manager.
  76. * 10-02-98 GJF Use GetVersionEx instead of GetVersion and store OS ID
  77. * in _osplatform.
  78. * 11-13-98 KBF Moved RTC_Initialize from _cinit to after _heap_init.
  79. * 05-11-99 KBF Wrap RTC support in #ifdef.
  80. * 05-17-99 PML Remove all Macintosh support.
  81. * 02-02-00 GB Added ATTACH_THREAD support for _CRT_INIT where we
  82. * initialise per thread data so that in case where we
  83. * are short of memory, we don't have to kill the whole
  84. * process for inavailablity of space.
  85. * 08-22-00 GB Fixed potentia leak of ptd in CRT_INIT
  86. * 03-16-01 PML _alloca the OSVERSIONINFO so /GS can work (vs7#224261)
  87. * 03-26-01 PML Use GetVersionExA, not GetVersionEx (vs7#230286)
  88. * 03-27-01 PML Fail DLL load instead of calling _amsg_exit (vs7#231220)
  89. *
  90. *******************************************************************************/
  91. #ifndef _POSIX_ /* not built for POSIX */
  92. #ifndef CRTDLL /* not built for CRTDLL */
  93. #include <cruntime.h>
  94. #include <dos.h>
  95. #include <internal.h>
  96. #include <mtdll.h>
  97. #include <stdlib.h>
  98. #include <string.h>
  99. #include <rterr.h>
  100. #include <oscalls.h>
  101. #define _DECL_DLLMAIN /* enable prototypes for DllMain and _CRT_INIT */
  102. #include <process.h>
  103. #include <awint.h>
  104. #include <tchar.h>
  105. #include <dbgint.h>
  106. #include <rtcapi.h>
  107. /*
  108. * flag set iff _CRTDLL_INIT was called with DLL_PROCESS_ATTACH
  109. */
  110. static int __proc_attached = 0;
  111. /*
  112. * command line, environment, and a few other globals
  113. */
  114. char *_acmdln; /* points to command line */
  115. char *_aenvptr = NULL; /* points to environment block */
  116. wchar_t *_wenvptr = NULL; /* points to wide environment block */
  117. void (__cdecl * _aexit_rtn)(int) = _exit; /* RT message return procedure */
  118. /*
  119. * _error_mode and _apptype, together, determine how error messages are
  120. * written out.
  121. */
  122. int __error_mode = _OUT_TO_DEFAULT;
  123. int __app_type = _UNKNOWN_APP;
  124. /*
  125. * User routine DllMain is called on all notifications
  126. */
  127. extern BOOL WINAPI DllMain(
  128. HANDLE hDllHandle,
  129. DWORD dwReason,
  130. LPVOID lpreserved
  131. ) ;
  132. /* _pRawDllMain MUST be a common variable, not extern nor initialized! */
  133. BOOL (WINAPI *_pRawDllMain)(HANDLE, DWORD, LPVOID);
  134. /***
  135. *BOOL WINAPI _CRT_INIT(hDllHandle, dwReason, lpreserved) -
  136. * C Run-Time initialization for a DLL linked with a C run-time library.
  137. *
  138. *Purpose:
  139. * This routine does the C run-time initialization or termination.
  140. * For the multi-threaded run-time library, it also cleans up the
  141. * multi-threading locks on DLL termination.
  142. *
  143. *Entry:
  144. *
  145. *Exit:
  146. *
  147. *NOTES:
  148. * This routine must be the entry point for the DLL.
  149. *
  150. *******************************************************************************/
  151. BOOL WINAPI _CRT_INIT(
  152. HANDLE hDllHandle,
  153. DWORD dwReason,
  154. LPVOID lpreserved
  155. )
  156. {
  157. /*
  158. * Start-up code only gets executed when the process is initialized
  159. */
  160. if ( dwReason == DLL_PROCESS_ATTACH )
  161. {
  162. /*
  163. * Dynamically allocate the OSVERSIONINFOA buffer, so we avoid
  164. * triggering the /GS buffer overrun detection. That can't be
  165. * used here, since the guard cookie isn't available until we
  166. * initialize it from here!
  167. */
  168. OSVERSIONINFOA *posvi =
  169. (OSVERSIONINFOA *)_alloca(sizeof(OSVERSIONINFOA));
  170. /*
  171. * Get the full Win32 version
  172. */
  173. posvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
  174. if ( !GetVersionExA(posvi) )
  175. return FALSE;
  176. _osplatform = posvi->dwPlatformId;
  177. _winmajor = posvi->dwMajorVersion;
  178. _winminor = posvi->dwMinorVersion;
  179. /*
  180. * The somewhat bizarre calculations of _osver and _winver are
  181. * required for backward compatibility (used to use GetVersion)
  182. */
  183. _osver = (posvi->dwBuildNumber) & 0x07fff;
  184. if ( _osplatform != VER_PLATFORM_WIN32_NT )
  185. _osver |= 0x08000;
  186. _winver = (_winmajor << 8) + _winminor;
  187. #ifdef _MT
  188. if ( !_heap_init(1) ) /* initialize heap */
  189. #else
  190. if ( !_heap_init(0) ) /* initialize heap */
  191. #endif
  192. return FALSE; /* fail to load DLL */
  193. #ifdef _MT
  194. if(!_mtinit()) /* initialize multi-thread */
  195. {
  196. _heap_term(); /* heap is now invalid! */
  197. return FALSE; /* fail to load DLL */
  198. }
  199. #endif /* _MT */
  200. /*
  201. * Initialize the Runtime Checks stuff
  202. */
  203. #ifdef _RTC
  204. _RTC_Initialize();
  205. #endif
  206. _acmdln = (char *)GetCommandLineA();
  207. _aenvptr = (char *)__crtGetEnvironmentStringsA();
  208. if (_ioinit() < 0) { /* initialize lowio */
  209. #ifdef _MT
  210. _mtterm(); /* free TLS index, call _mtdeletelocks() */
  211. #endif /* _MT */
  212. _heap_term(); /* heap is now invalid! */
  213. return FALSE; /* fail to load DLL */
  214. }
  215. if (_setargv() < 0 || /* get cmd line info */
  216. _setenvp() < 0 || /* get environ info */
  217. _cinit() != 0) /* do C data initialize */
  218. {
  219. _ioterm(); /* shut down lowio */
  220. #ifdef _MT
  221. _mtterm(); /* free TLS index, call _mtdeletelocks() */
  222. #endif /* _MT */
  223. _heap_term(); /* heap is now invalid! */
  224. return FALSE; /* fail to load DLL */
  225. }
  226. /*
  227. * increment flag to indicate process attach notification
  228. * has been received
  229. */
  230. __proc_attached++;
  231. }
  232. else if ( dwReason == DLL_PROCESS_DETACH )
  233. {
  234. if ( __proc_attached > 0 )
  235. {
  236. __proc_attached--;
  237. /*
  238. * Any basic clean-up code that goes here must be duplicated
  239. * below in _DllMainCRTStartup for the case where the user's
  240. * DllMain() routine fails on a Process Attach notification.
  241. * This does not include calling user C++ destructors, etc.
  242. */
  243. if ( _C_Termination_Done == FALSE )
  244. _cexit();
  245. #ifdef _DEBUG
  246. /* Dump all memory leaks */
  247. if (_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & _CRTDBG_LEAK_CHECK_DF)
  248. _CrtDumpMemoryLeaks();
  249. #endif
  250. /* Shut down lowio */
  251. _ioterm();
  252. #ifdef _MT
  253. _mtterm();
  254. #endif
  255. /* This should be the last thing the C run-time does */
  256. _heap_term(); /* heap is now invalid! */
  257. }
  258. else
  259. /* no prior process attach, just return */
  260. return FALSE;
  261. }
  262. #ifdef _MT
  263. else if ( dwReason == DLL_THREAD_ATTACH )
  264. {
  265. _ptiddata ptd;
  266. if ( ((ptd = _calloc_crt(1, sizeof(struct _tiddata))) != NULL))
  267. {
  268. if (TlsSetValue(__tlsindex, (LPVOID)ptd) ) {
  269. /*
  270. * Initialize of per-thread data
  271. */
  272. _initptd(ptd);
  273. ptd->_tid = GetCurrentThreadId();
  274. ptd->_thandle = (uintptr_t)(-1);
  275. } else
  276. {
  277. _free_crt(ptd);
  278. return FALSE;
  279. }
  280. } else
  281. {
  282. return FALSE;
  283. }
  284. }
  285. else if ( dwReason == DLL_THREAD_DETACH )
  286. {
  287. _freeptd(NULL); /* free up per-thread CRT data */
  288. }
  289. #endif
  290. return TRUE ;
  291. }
  292. /***
  293. *BOOL WINAPI _DllMainCRTStartup(hDllHandle, dwReason, lpreserved) -
  294. * C Run-Time initialization for a DLL linked with a C run-time library.
  295. *
  296. *Purpose:
  297. * This routine does the C run-time initialization or termination
  298. * and then calls the user code notification handler "DllMain".
  299. * For the multi-threaded run-time library, it also cleans up the
  300. * multi-threading locks on DLL termination.
  301. *
  302. *Entry:
  303. *
  304. *Exit:
  305. *
  306. *NOTES:
  307. * This routine is the preferred entry point. _CRT_INIT may also be
  308. * used, or the user may supply his/her own entry and call _CRT_INIT
  309. * from within it, but this is not the preferred method.
  310. *
  311. *******************************************************************************/
  312. BOOL WINAPI _DllMainCRTStartup(
  313. HANDLE hDllHandle,
  314. DWORD dwReason,
  315. LPVOID lpreserved
  316. )
  317. {
  318. BOOL retcode = TRUE;
  319. /*
  320. * If this is a process detach notification, check that there has
  321. * has been a prior process attach notification.
  322. */
  323. if ( (dwReason == DLL_PROCESS_DETACH) && (__proc_attached == 0) )
  324. /*
  325. * no prior process attach notification. just return
  326. * without doing anything.
  327. */
  328. return FALSE;
  329. if ( dwReason == DLL_PROCESS_ATTACH || dwReason == DLL_THREAD_ATTACH )
  330. {
  331. if ( _pRawDllMain )
  332. retcode = (*_pRawDllMain)(hDllHandle, dwReason, lpreserved);
  333. if ( retcode )
  334. retcode = _CRT_INIT(hDllHandle, dwReason, lpreserved);
  335. if ( !retcode )
  336. return FALSE;
  337. }
  338. retcode = DllMain(hDllHandle, dwReason, lpreserved);
  339. if ( (dwReason == DLL_PROCESS_ATTACH) && !retcode )
  340. /*
  341. * The user's DllMain routine returned failure, the C runtime
  342. * needs to be cleaned up. Do this by calling _CRT_INIT again,
  343. * this time imitating DLL_PROCESS_DETACH. Note this will also
  344. * clear the __proc_attached flag so the cleanup will not be
  345. * repeated upon receiving the real process detach notification.
  346. */
  347. _CRT_INIT(hDllHandle, DLL_PROCESS_DETACH, lpreserved);
  348. if ( (dwReason == DLL_PROCESS_DETACH) ||
  349. (dwReason == DLL_THREAD_DETACH) )
  350. {
  351. if ( _CRT_INIT(hDllHandle, dwReason, lpreserved) == FALSE )
  352. retcode = FALSE ;
  353. if ( retcode && _pRawDllMain )
  354. retcode = (*_pRawDllMain)(hDllHandle, dwReason, lpreserved);
  355. }
  356. return retcode ;
  357. }
  358. /***
  359. *_amsg_exit(rterrnum) - Fast exit fatal errors
  360. *
  361. *Purpose:
  362. * Exit the program with error code of 255 and appropriate error
  363. * message.
  364. *
  365. *Entry:
  366. * int rterrnum - error message number (amsg_exit only).
  367. *
  368. *Exit:
  369. * Calls exit() (for integer divide-by-0) or _exit() indirectly
  370. * through _aexit_rtn [amsg_exit].
  371. * For multi-thread: calls _exit() function
  372. *
  373. *Exceptions:
  374. *
  375. *******************************************************************************/
  376. void __cdecl _amsg_exit (
  377. int rterrnum
  378. )
  379. {
  380. if ( (__error_mode == _OUT_TO_STDERR) || ((__error_mode ==
  381. _OUT_TO_DEFAULT) && (__app_type == _CONSOLE_APP)) )
  382. _FF_MSGBANNER(); /* write run-time error banner */
  383. _NMSG_WRITE(rterrnum); /* write message */
  384. _aexit_rtn(255); /* normally _exit(255) */
  385. }
  386. #endif /* CRTDLL */
  387. #endif /* _POSIX_ */