system.pp 19 KB

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