system.pp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl and Pavel Ozerski
  4. member of the Free Pascal development team.
  5. FPC Pascal system unit for the Win32 API.
  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. unit System;
  13. interface
  14. {$define FPC_IS_SYSTEM}
  15. {$ifdef SYSTEMDEBUG}
  16. {$define SYSTEMEXCEPTIONDEBUG}
  17. {$endif SYSTEMDEBUG}
  18. {$ifdef cpui386}
  19. {$define Set_i386_Exception_handler}
  20. {$endif cpui386}
  21. {$define DISABLE_NO_THREAD_MANAGER}
  22. {$define HAS_WIDESTRINGMANAGER}
  23. {$define DISABLE_NO_DYNLIBS_MANAGER}
  24. {$define FPC_SYSTEM_HAS_SYSDLH}
  25. {$define FPC_HAS_SETCTRLBREAKHANDLER}
  26. {$ifdef FPC_USE_WIN32_SEH}
  27. {$define FPC_SYSTEM_HAS_RAISEEXCEPTION}
  28. {$define FPC_SYSTEM_HAS_RERAISE}
  29. {$define FPC_SYSTEM_HAS_DONEEXCEPTION}
  30. {$define FPC_SYSTEM_HAS_SAFECALLHANDLER}
  31. {$endif FPC_USE_WIN32_SEH}
  32. { include system-independent routine headers }
  33. {$I systemh.inc}
  34. { include common windows headers }
  35. {$I syswinh.inc}
  36. var
  37. MainInstance : longint;
  38. implementation
  39. var
  40. FPCSysInstance : PLongint;public name '_FPC_SysInstance';
  41. {$define FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
  42. procedure OsSetupEntryInformation(constref info: TEntryInformation); forward;
  43. {$ifdef FPC_USE_WIN32_SEH}
  44. function main_wrapper(arg: Pointer; proc: Pointer): ptrint; forward;
  45. procedure OutermostHandler; external name '__FPC_DEFAULT_HANDLER';
  46. {$endif FPC_USE_WIN32_SEH}
  47. {$define FPC_SYSTEM_HAS_STACKTOP}
  48. function StackTop: pointer; assembler;nostackframe;
  49. asm
  50. movl %fs:(4),%eax
  51. end;
  52. { include system independent routines }
  53. {$I system.inc}
  54. { include code common with win64 }
  55. {$I syswin.inc}
  56. procedure OsSetupEntryInformation(constref info: TEntryInformation);
  57. begin
  58. TlsKey := info.OS.TlsKeyAddr;
  59. FPCSysInstance := info.OS.SysInstance;
  60. WStrInitTablesTable := info.OS.WideInitTables;
  61. end;
  62. {*****************************************************************************
  63. System Dependent Exit code
  64. *****************************************************************************}
  65. {$ifndef FPC_USE_WIN32_SEH}
  66. procedure install_exception_handlers;forward;
  67. procedure remove_exception_handlers;forward;
  68. {$endif FPC_USE_WIN32_SEH}
  69. Procedure system_exit;
  70. begin
  71. if IsLibrary then
  72. begin
  73. { If exiting from DLL_PROCESS_ATTACH/DETACH, unwind to DllMain context. }
  74. if DllInitState in [DLL_PROCESS_ATTACH,DLL_PROCESS_DETACH] then
  75. LongJmp(DLLBuf,1)
  76. else
  77. { Abnormal termination, Halt has been called from DLL function,
  78. put down the entire process (DLL_PROCESS_DETACH will still
  79. occur). At this point RTL has been already finalized in InternalExit
  80. and shouldn't be finalized another time in DLL_PROCESS_DETACH.
  81. Indicate this by resetting DllProcessAttachPerformed. }
  82. DllProcessAttachPerformed:=false;
  83. end;
  84. if not IsConsole then
  85. begin
  86. Close(stderr);
  87. Close(stdout);
  88. Close(erroutput);
  89. Close(Input);
  90. Close(Output);
  91. { what about Input and Output ?? PM }
  92. { now handled, FPK }
  93. end;
  94. if Ole32Dll <> 0 then
  95. begin
  96. WinFreeLibrary(Ole32Dll); { Careful, FreeLibrary should not be called from DllMain. }
  97. Ole32Dll := 0;
  98. end;
  99. if OleAut32Dll <> 0 then
  100. begin
  101. WinFreeLibrary(OleAut32Dll);
  102. OleAut32Dll := 0;
  103. end;
  104. {$ifndef FPC_USE_WIN32_SEH}
  105. if not IsLibrary then
  106. remove_exception_handlers;
  107. {$endif FPC_USE_WIN32_SEH}
  108. { do cleanup required by the startup code }
  109. EntryInformation.OS.asm_exit();
  110. { call exitprocess, with cleanup as required }
  111. ExitProcess(exitcode);
  112. end;
  113. var
  114. { value of the stack segment
  115. to check if the call stack can be written on exceptions }
  116. _SS : Cardinal;
  117. procedure Exe_entry(constref info : TEntryInformation);[public,alias:'_FPC_EXE_Entry'];
  118. var
  119. xframe: TEXCEPTION_FRAME;
  120. begin
  121. SetupEntryInformation(info);
  122. IsLibrary:=false;
  123. { install the handlers for exe only ?
  124. or should we install them for DLL also ? (PM) }
  125. {$ifndef FPC_USE_WIN32_SEH}
  126. install_exception_handlers;
  127. {$endif FPC_USE_WIN32_SEH}
  128. { This strange construction is needed to solve the _SS problem
  129. with a smartlinked syswin32 (PFV) }
  130. asm
  131. { movl %esp,%fs:(0)
  132. but don't insert it as it doesn't
  133. point to anything yet
  134. this will be used in signals unit }
  135. leal xframe,%eax
  136. movl %fs:(0),%ecx
  137. movl %ecx,TException_Frame.next(%eax)
  138. movl %eax,System_exception_frame
  139. {$ifndef FPC_USE_WIN32_SEH}
  140. movl $0,TException_Frame.handler(%eax)
  141. {$else}
  142. movl $OutermostHandler,TException_Frame.handler(%eax)
  143. movl %eax,%fs:(0)
  144. {$endif FPC_USE_WIN32_SEH}
  145. pushl %ebp
  146. xorl %eax,%eax
  147. movw %ss,%ax
  148. movl %eax,_SS
  149. xorl %ebp,%ebp
  150. end;
  151. EntryInformation.PascalMain();
  152. asm
  153. popl %ebp
  154. end;
  155. { if we pass here there was no error ! }
  156. system_exit;
  157. end;
  158. function is_prefetch(p : pointer) : boolean;
  159. var
  160. a : array[0..15] of byte;
  161. doagain : boolean;
  162. instrlo,instrhi,opcode : byte;
  163. i : longint;
  164. begin
  165. result:=false;
  166. { read memory safely without causing another exeception }
  167. if not(ReadProcessMemory(GetCurrentProcess,p,@a,sizeof(a),nil)) then
  168. exit;
  169. i:=0;
  170. doagain:=true;
  171. while doagain and (i<15) do
  172. begin
  173. opcode:=a[i];
  174. instrlo:=opcode and $f;
  175. instrhi:=opcode and $f0;
  176. case instrhi of
  177. { prefix? }
  178. $20,$30:
  179. doagain:=(instrlo and 7)=6;
  180. $60:
  181. doagain:=(instrlo and $c)=4;
  182. $f0:
  183. doagain:=instrlo in [0,2,3];
  184. $0:
  185. begin
  186. result:=(instrlo=$f) and (a[i+1] in [$d,$18]);
  187. exit;
  188. end;
  189. else
  190. doagain:=false;
  191. end;
  192. inc(i);
  193. end;
  194. end;
  195. //
  196. // Hardware exception handling
  197. //
  198. {$ifdef Set_i386_Exception_handler}
  199. type
  200. PFloatingSaveArea = ^TFloatingSaveArea;
  201. TFloatingSaveArea = packed record
  202. ControlWord : Cardinal;
  203. StatusWord : Cardinal;
  204. TagWord : Cardinal;
  205. ErrorOffset : Cardinal;
  206. ErrorSelector : Cardinal;
  207. DataOffset : Cardinal;
  208. DataSelector : Cardinal;
  209. RegisterArea : array[0..79] of Byte;
  210. Cr0NpxState : Cardinal;
  211. end;
  212. PContext = ^TContext;
  213. TContext = packed record
  214. //
  215. // The flags values within this flag control the contents of
  216. // a CONTEXT record.
  217. //
  218. ContextFlags : Cardinal;
  219. //
  220. // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  221. // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
  222. // included in CONTEXT_FULL.
  223. //
  224. Dr0, Dr1, Dr2,
  225. Dr3, Dr6, Dr7 : Cardinal;
  226. //
  227. // This section is specified/returned if the
  228. // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  229. //
  230. FloatSave : TFloatingSaveArea;
  231. //
  232. // This section is specified/returned if the
  233. // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  234. //
  235. SegGs, SegFs,
  236. SegEs, SegDs : Cardinal;
  237. //
  238. // This section is specified/returned if the
  239. // ContextFlags word contains the flag CONTEXT_INTEGER.
  240. //
  241. Edi, Esi, Ebx,
  242. Edx, Ecx, Eax : Cardinal;
  243. //
  244. // This section is specified/returned if the
  245. // ContextFlags word contains the flag CONTEXT_CONTROL.
  246. //
  247. Ebp : Cardinal;
  248. Eip : Cardinal;
  249. SegCs : Cardinal;
  250. EFlags, Esp, SegSs : Cardinal;
  251. //
  252. // This section is specified/returned if the ContextFlags word
  253. // contains the flag CONTEXT_EXTENDED_REGISTERS.
  254. // The format and contexts are processor specific
  255. //
  256. ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  257. end;
  258. PExceptionPointers = ^TExceptionPointers;
  259. TExceptionPointers = packed record
  260. ExceptionRecord : PExceptionRecord;
  261. ContextRecord : PContext;
  262. end;
  263. { type of functions that should be used for exception handling }
  264. TTopLevelExceptionFilter = function (excep : PExceptionPointers) : Longint;stdcall;
  265. {$i seh32.inc}
  266. {$ifndef FPC_USE_WIN32_SEH}
  267. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : TTopLevelExceptionFilter) : TTopLevelExceptionFilter;
  268. stdcall;external 'kernel32' name 'SetUnhandledExceptionFilter';
  269. const
  270. MaxExceptionLevel = 16;
  271. exceptLevel : Byte = 0;
  272. var
  273. exceptEip : array[0..MaxExceptionLevel-1] of Longint;
  274. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  275. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  276. {$ifdef SYSTEMEXCEPTIONDEBUG}
  277. procedure DebugHandleErrorAddrFrame(error : longint; addr, frame : pointer);
  278. begin
  279. if IsConsole then
  280. begin
  281. write(stderr,'HandleErrorAddrFrame(error=',error);
  282. write(stderr,',addr=',hexstr(ptruint(addr),8));
  283. writeln(stderr,',frame=',hexstr(ptruint(frame),8),')');
  284. end;
  285. HandleErrorAddrFrame(error,addr,frame);
  286. end;
  287. {$endif SYSTEMEXCEPTIONDEBUG}
  288. procedure JumpToHandleErrorFrame;
  289. var
  290. eip, ebp, error : Longint;
  291. begin
  292. // save ebp
  293. asm
  294. movl (%ebp),%eax
  295. movl %eax,ebp
  296. end;
  297. if (exceptLevel > 0) then
  298. dec(exceptLevel);
  299. eip:=exceptEip[exceptLevel];
  300. error:=exceptError[exceptLevel];
  301. {$ifdef SYSTEMEXCEPTIONDEBUG}
  302. if IsConsole then
  303. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  304. {$endif SYSTEMEXCEPTIONDEBUG}
  305. if resetFPU[exceptLevel] then
  306. SysResetFPU;
  307. { build a fake stack }
  308. asm
  309. movl ebp,%ecx
  310. movl eip,%edx
  311. movl error,%eax
  312. pushl eip
  313. movl ebp,%ebp // Change frame pointer
  314. {$ifdef SYSTEMEXCEPTIONDEBUG}
  315. jmpl DebugHandleErrorAddrFrame
  316. {$else not SYSTEMEXCEPTIONDEBUG}
  317. jmpl HandleErrorAddrFrame
  318. {$endif SYSTEMEXCEPTIONDEBUG}
  319. end;
  320. end;
  321. function syswin32_i386_exception_handler(excep : PExceptionPointers) : Longint;stdcall;
  322. var
  323. res,ssecode: longint;
  324. err: byte;
  325. must_reset_fpu: boolean;
  326. begin
  327. res := EXCEPTION_CONTINUE_SEARCH;
  328. if excep^.ContextRecord^.SegSs=_SS then begin
  329. err := 0;
  330. must_reset_fpu := true;
  331. {$ifdef SYSTEMEXCEPTIONDEBUG}
  332. if IsConsole then Writeln(stderr,'Exception ',
  333. hexstr(excep^.ExceptionRecord^.ExceptionCode, 8));
  334. {$endif SYSTEMEXCEPTIONDEBUG}
  335. case excep^.ExceptionRecord^.ExceptionCode of
  336. STATUS_INTEGER_DIVIDE_BY_ZERO :
  337. err := 200;
  338. STATUS_FLOAT_DIVIDE_BY_ZERO :
  339. err := 208;
  340. STATUS_ARRAY_BOUNDS_EXCEEDED :
  341. begin
  342. err := 201;
  343. must_reset_fpu := false;
  344. end;
  345. STATUS_STACK_OVERFLOW :
  346. begin
  347. err := 202;
  348. must_reset_fpu := false;
  349. end;
  350. STATUS_FLOAT_OVERFLOW :
  351. err := 205;
  352. STATUS_FLOAT_DENORMAL_OPERAND,
  353. STATUS_FLOAT_UNDERFLOW :
  354. err := 206;
  355. {excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  356. STATUS_FLOAT_INEXACT_RESULT,
  357. STATUS_FLOAT_INVALID_OPERATION,
  358. STATUS_FLOAT_STACK_CHECK :
  359. err := 207;
  360. STATUS_INTEGER_OVERFLOW :
  361. begin
  362. err := 215;
  363. must_reset_fpu := false;
  364. end;
  365. STATUS_ILLEGAL_INSTRUCTION:
  366. { if we're testing sse support, simply set the flag and continue }
  367. if sse_check then
  368. begin
  369. os_supports_sse:=false;
  370. { skip the offending movaps %xmm7, %xmm6 instruction }
  371. inc(excep^.ContextRecord^.Eip,3);
  372. excep^.ExceptionRecord^.ExceptionCode := 0;
  373. res:=EXCEPTION_CONTINUE_EXECUTION;
  374. end
  375. else
  376. err := 216;
  377. STATUS_ACCESS_VIOLATION:
  378. { Athlon prefetch bug? }
  379. if is_prefetch(pointer(excep^.ContextRecord^.Eip)) then
  380. begin
  381. { if yes, then retry }
  382. excep^.ExceptionRecord^.ExceptionCode := 0;
  383. res:=EXCEPTION_CONTINUE_EXECUTION;
  384. end
  385. else
  386. err := 216;
  387. STATUS_CONTROL_C_EXIT:
  388. err := 217;
  389. STATUS_PRIVILEGED_INSTRUCTION:
  390. begin
  391. err := 218;
  392. must_reset_fpu := false;
  393. end;
  394. STATUS_FLOAT_MULTIPLE_FAULTS,
  395. STATUS_FLOAT_MULTIPLE_TRAPS:
  396. begin
  397. { dumping ExtendedRegisters and comparing with the actually value of mxcsr revealed 24 }
  398. TranslateMxcsr(excep^.ContextRecord^.ExtendedRegisters[24],ssecode);
  399. {$ifdef SYSTEMEXCEPTIONDEBUG}
  400. if IsConsole then
  401. Writeln(stderr,'MXSR: ',hexstr(excep^.ContextRecord^.ExtendedRegisters[24], 2),' SSECODE: ',ssecode);
  402. {$endif SYSTEMEXCEPTIONDEBUG}
  403. err:=-ssecode;
  404. end;
  405. else
  406. begin
  407. if ((excep^.ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  408. err := 217
  409. else
  410. err := 255;
  411. end;
  412. end;
  413. if (err <> 0) and (exceptLevel < MaxExceptionLevel) then begin
  414. exceptEip[exceptLevel] := excep^.ContextRecord^.Eip;
  415. exceptError[exceptLevel] := err;
  416. resetFPU[exceptLevel] := must_reset_fpu;
  417. inc(exceptLevel);
  418. excep^.ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  419. excep^.ExceptionRecord^.ExceptionCode := 0;
  420. res := EXCEPTION_CONTINUE_EXECUTION;
  421. {$ifdef SYSTEMEXCEPTIONDEBUG}
  422. if IsConsole then begin
  423. writeln(stderr,'Exception Continue Exception set at ',
  424. hexstr(exceptEip[exceptLevel],8));
  425. writeln(stderr,'Eip changed to ',
  426. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', err);
  427. end;
  428. {$endif SYSTEMEXCEPTIONDEBUG}
  429. end;
  430. end;
  431. syswin32_i386_exception_handler := res;
  432. end;
  433. procedure install_exception_handlers;
  434. begin
  435. SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  436. end;
  437. procedure remove_exception_handlers;
  438. begin
  439. SetUnhandledExceptionFilter(nil);
  440. end;
  441. {$endif not FPC_USE_WIN32_SEH}
  442. {$else not cpui386 (Processor specific !!)}
  443. procedure install_exception_handlers;
  444. begin
  445. end;
  446. procedure remove_exception_handlers;
  447. begin
  448. end;
  449. {$endif Set_i386_Exception_handler}
  450. {$ifdef FPC_SECTION_THREADVARS}
  451. function fpc_tls_add(addr: pointer): pointer; assembler; nostackframe;
  452. [public,alias: 'FPC_TLS_ADD']; compilerproc;
  453. asm
  454. sub $tls_data_start,%eax
  455. cmpb $0,IsLibrary
  456. mov _tls_index,%ecx
  457. jnz .L1
  458. mov %fs:(0x2c),%edx
  459. add (%edx,%ecx,4),%eax
  460. ret
  461. .L1:
  462. push %ebx
  463. mov %eax,%ebx
  464. call GetLastError
  465. push %eax { save LastError }
  466. push _tls_index
  467. call TlsGetValue
  468. test %eax,%eax
  469. jnz .L2
  470. { This can happen when a thread existed before DLL was loaded,
  471. or if DisableThreadLibraryCalls was called. }
  472. call SysAllocateThreadVars
  473. mov $0x1000000,%eax
  474. call InitThread
  475. push _tls_index
  476. call TlsGetValue
  477. .L2:
  478. add %eax,%ebx
  479. call SetLastError { restore (value is on stack) }
  480. mov %ebx,%eax
  481. pop %ebx
  482. end;
  483. {$endif FPC_SECTION_THREADVARS}
  484. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  485. type
  486. tdosheader = packed record
  487. e_magic : word;
  488. e_cblp : word;
  489. e_cp : word;
  490. e_crlc : word;
  491. e_cparhdr : word;
  492. e_minalloc : word;
  493. e_maxalloc : word;
  494. e_ss : word;
  495. e_sp : word;
  496. e_csum : word;
  497. e_ip : word;
  498. e_cs : word;
  499. e_lfarlc : word;
  500. e_ovno : word;
  501. e_res : array[0..3] of word;
  502. e_oemid : word;
  503. e_oeminfo : word;
  504. e_res2 : array[0..9] of word;
  505. e_lfanew : longint;
  506. end;
  507. tpeheader = packed record
  508. PEMagic : longint;
  509. Machine : word;
  510. NumberOfSections : word;
  511. TimeDateStamp : longint;
  512. PointerToSymbolTable : longint;
  513. NumberOfSymbols : longint;
  514. SizeOfOptionalHeader : word;
  515. Characteristics : word;
  516. Magic : word;
  517. MajorLinkerVersion : byte;
  518. MinorLinkerVersion : byte;
  519. SizeOfCode : longint;
  520. SizeOfInitializedData : longint;
  521. SizeOfUninitializedData : longint;
  522. AddressOfEntryPoint : longint;
  523. BaseOfCode : longint;
  524. BaseOfData : longint;
  525. ImageBase : longint;
  526. SectionAlignment : longint;
  527. FileAlignment : longint;
  528. MajorOperatingSystemVersion : word;
  529. MinorOperatingSystemVersion : word;
  530. MajorImageVersion : word;
  531. MinorImageVersion : word;
  532. MajorSubsystemVersion : word;
  533. MinorSubsystemVersion : word;
  534. Reserved1 : longint;
  535. SizeOfImage : longint;
  536. SizeOfHeaders : longint;
  537. CheckSum : longint;
  538. Subsystem : word;
  539. DllCharacteristics : word;
  540. SizeOfStackReserve : longint;
  541. SizeOfStackCommit : longint;
  542. SizeOfHeapReserve : longint;
  543. SizeOfHeapCommit : longint;
  544. LoaderFlags : longint;
  545. NumberOfRvaAndSizes : longint;
  546. DataDirectory : array[1..$80] of byte;
  547. end;
  548. begin
  549. result:=tpeheader((pointer(getmodulehandle(nil))+(tdosheader(pointer(getmodulehandle(nil))^).e_lfanew))^).SizeOfStackReserve;
  550. end;
  551. initialization
  552. { get some helpful informations }
  553. GetStartupInfo(@startupinfo);
  554. { some misc Win32 stuff }
  555. if not IsLibrary then
  556. FPCSysInstance^:=getmodulehandle(nil);
  557. MainInstance:=FPCSysInstance^;
  558. { pass dummy value }
  559. StackLength := CheckInitialStkLen($1000000);
  560. StackBottom := StackTop - StackLength;
  561. CodePointer(SetThreadStackGuarantee) := WinGetProcAddress(WinGetModuleHandleW(KernelDLL), 'SetThreadStackGuarantee');
  562. if Assigned(SetThreadStackGuarantee) then
  563. SetThreadStackGuaranteeTo(StackMargin);
  564. cmdshow:=startupinfo.wshowwindow;
  565. { Setup heap and threading, these may be already initialized from TLS callback }
  566. if not Assigned(CurrentTM.BeginThread) then
  567. begin
  568. InitHeap;
  569. InitSystemThreads;
  570. end;
  571. SysInitExceptions;
  572. { setup fastmove stuff }
  573. fpc_cpucodeinit;
  574. initunicodestringmanager;
  575. InitWin32Widestrings;
  576. SysInitStdIO;
  577. { Arguments }
  578. setup_arguments;
  579. InitSystemDynLibs;
  580. { Reset IO Error }
  581. InOutRes:=0;
  582. DispCallByIDProc:=@DoDispCallByIDError;
  583. finalization
  584. WinFinalizeSystem;
  585. end.