system.pp 18 KB

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