system.pp 19 KB

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