system.pp 19 KB

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