system.pp 19 KB

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