system.pp 19 KB

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