system.pp 18 KB

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