system.pp 19 KB

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