systlsdir.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2008 by Florian Klaempfl and Pavel Ozerski
  4. member of the Free Pascal development team.
  5. FPC Pascal system unit part shared by win32/win64.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { TLS Directory code }
  13. {$ifdef FPC_USE_TLS_DIRECTORY}
  14. { Process TLS callback function }
  15. { This is only useful for executables
  16. for DLLs, DLL_Entry gets called. PM }
  17. { The consts are the same as for DDL_Entry,
  18. but as this file can be either in system unit or sysinitXXX
  19. we need to rename them with EXEC prefix
  20. to avoid duplicate entries. }
  21. Const
  22. EXEC_PROCESS_ATTACH = 1;
  23. EXEC_THREAD_ATTACH = 2;
  24. EXEC_PROCESS_DETACH = 0;
  25. EXEC_THREAD_DETACH = 3;
  26. {$ifdef FPC_INSSIDE_SYSINIT}
  27. var
  28. TlsKey : dword; external name '_FPC_TlsKey';
  29. type
  30. TTlsDirectory=packed record
  31. data_start, data_end : pointer;
  32. index_pointer, callbacks_pointer : pointer;
  33. zero_fill_size : dword;
  34. flags : dword;
  35. end;
  36. function TlsGetValue(dwTlsIndex : DWord) : pointer; stdcall;
  37. external 'kernel32' name 'TlsGetValue';
  38. procedure InitSystemThreads; external name '_FPC_InitSystemThreads';
  39. procedure SysAllocateThreadVars; external name '_FPC_SysAllocateThreadVars';
  40. procedure InitHeap; external name '_FPC_InitHeap';
  41. {$endif FPC_INSSIDE_SYSINIT}
  42. procedure Exec_Tls_callback(Handle : pointer; reason : Dword; Reserved : pointer);
  43. stdcall; [public,alias:'_FPC_Tls_Callback'];
  44. begin
  45. if IsLibrary then
  46. Exit;
  47. case reason of
  48. { For executables, EXEC_PROCESS_ATTACH is called *before* the entry point,
  49. and EXEC_PROCESS_DETACH is called *after* RTL shuts down and calls ExitProcess.
  50. It isn't a good idea to handle resources of the main thread at these points.
  51. InitSystemThreads is necessary however, because if some statically loaded
  52. DLL creates a thread, it will invoke EXEC_THREAD_ATTACH before anything else is
  53. initialized.
  54. TODO: The problem is that InitSystemThreads depends (in case of Win32)
  55. on EntryInformation which is not available at this point.
  56. Solving it properly needs to move this routine
  57. to sysinit unit or something like that.
  58. Exec_Tls_Callback is now part of sysinit unit for win32
  59. and the EntryInformation is a constant which sholud prevent troubles }
  60. EXEC_PROCESS_ATTACH:
  61. begin
  62. InitHeap;
  63. InitSystemThreads;
  64. end;
  65. EXEC_THREAD_ATTACH :
  66. begin
  67. { !!! SysInitMultithreading must NOT be called here. Windows guarantees that
  68. the main thread invokes PROCESS_ATTACH, not THREAD_ATTACH. So this always
  69. executes in non-main thread. SysInitMultithreading() here will cause
  70. initial threadvars to be copied to TLS of non-main thread, and threadvars
  71. of the main thread will be reinitialized upon the next access with zeroes,
  72. ending up in a delayed failure which is very hard to debug.
  73. Fortunately this nasty scenario can happen only when the first non-main thread
  74. was created outside of RTL (Sergei).
  75. }
  76. { Allocate Threadvars }
  77. SysAllocateThreadVars;
  78. { NS : no idea what is correct to pass here - pass dummy value for now }
  79. { passing a dummy is ok, the correct value is read from the coff header of SysInstance (FK) }
  80. InitThread($1000000); { Assume everything is idempotent there, as the thread could have been created with BeginThread... }
  81. end;
  82. EXEC_THREAD_DETACH :
  83. begin
  84. if TlsGetValue(TLSKey)<>nil then
  85. DoneThread; { Assume everything is idempotent there }
  86. end;
  87. end;
  88. end;
  89. { Mingw tlssup.c source code has
  90. _CRTALLOC(".CRT$XLA") PIMAGE_TLS_CALLBACK __xl_a = 0;
  91. _CRTALLOC(".CRT$XLZ") PIMAGE_TLS_CALLBACK __xl_z = 0;
  92. and the callback pointer is set to:
  93. (&__xl_a+1), (+1 meaning =+sizeof(pointer))
  94. I am not sure this can be compatible with
  95. }
  96. const
  97. FreePascal_TLS_callback : pointer = @Exec_Tls_callback;
  98. public name '__FPC_tls_callbacks' section '.CRT$XLFPC';
  99. FreePascal_end_of_TLS_callback : pointer = nil;
  100. public name '__FPC_end_of_tls_callbacks' section '.CRT$XLZZZ';
  101. var
  102. tls_callbacks : pointer; external name '___crt_xl_start__';
  103. tls_data_start : pointer; external name '___tls_start__';
  104. tls_data_end : pointer; external name '___tls_end__';
  105. _tls_index : dword; cvar; external;
  106. const
  107. _tls_used : TTlsDirectory = (
  108. data_start : @tls_data_start;
  109. data_end : @tls_data_end;
  110. index_pointer : @_tls_index;
  111. callbacks_pointer : @tls_callbacks;
  112. zero_fill_size : 0;
  113. flags : 0;
  114. ); cvar; public;
  115. {$ifdef win64}
  116. { This is a hack to support external linking.
  117. All released win64 versions of GNU binutils miss proper prefix handling
  118. when searching for _tls_used and expect two leading underscores.
  119. The issue has been fixed in binutils snapshots, but not released yet.
  120. TODO: This should be removed as soon as next version of binutils (>2.21) is
  121. released and we upgrade to it. }
  122. __tls_used : TTlsDirectory = (
  123. data_start : @tls_data_start;
  124. data_end : @tls_data_end;
  125. index_pointer : @_tls_index;
  126. callbacks_pointer : @tls_callbacks;
  127. zero_fill_size : 0;
  128. flags : 0;
  129. ); cvar; public;
  130. {$endif win64}
  131. {$endif FPC_USE_TLS_DIRECTORY}