system.pp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2006 by Florian Klaempfl and Pavel Ozerski
  4. member of the Free Pascal development team.
  5. FPC Pascal system unit for the Win64 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 SYSTEMEXCEPTIONDEBUG}
  15. {$ifdef SYSTEMDEBUG}
  16. {$define SYSTEMEXCEPTIONDEBUG}
  17. {$endif SYSTEMDEBUG}
  18. {$define DISABLE_NO_THREAD_MANAGER}
  19. {$define HAS_WIDESTRINGMANAGER}
  20. {$define DISABLE_NO_DYNLIBS_MANAGER}
  21. {$define FPC_SYSTEM_HAS_SYSDLH}
  22. {$define FPC_HAS_SETCTRLBREAKHANDLER}
  23. {$ifdef FPC_USE_WIN64_SEH}
  24. {$define FPC_SYSTEM_HAS_RAISEEXCEPTION}
  25. {$define FPC_SYSTEM_HAS_RERAISE}
  26. {$define FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  27. {$endif FPC_USE_WIN64_SEH}
  28. { include system-independent routine headers }
  29. {$I systemh.inc}
  30. const
  31. LineEnding = #13#10;
  32. LFNSupport = true;
  33. DirectorySeparator = '\';
  34. DriveSeparator = ':';
  35. ExtensionSeparator = '.';
  36. PathSeparator = ';';
  37. AllowDirectorySeparators : set of char = ['\','/'];
  38. AllowDriveSeparators : set of char = [':'];
  39. { FileNameCaseSensitive and FileNameCasePreserving are defined separately below!!! }
  40. maxExitCode = 65535;
  41. MaxPathLen = 260;
  42. AllFilesMask = '*';
  43. type
  44. PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  45. TEXCEPTION_FRAME = record
  46. next : PEXCEPTION_FRAME;
  47. handler : pointer;
  48. end;
  49. const
  50. { Default filehandles }
  51. UnusedHandle : THandle = THandle(-1);
  52. StdInputHandle : THandle = 0;
  53. StdOutputHandle : THandle = 0;
  54. StdErrorHandle : THandle = 0;
  55. System_exception_frame : PEXCEPTION_FRAME =nil;
  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. var
  62. { C compatible arguments }
  63. argc : longint;
  64. argv : ppchar;
  65. { Win32 Info }
  66. startupinfo : tstartupinfo deprecated; // Delphi does not have one in interface
  67. StartupConsoleMode : dword;
  68. MainInstance : qword;
  69. cmdshow : longint;
  70. DLLreason : dword;
  71. DLLparam : PtrInt;
  72. const
  73. hprevinst: qword=0;
  74. type
  75. TDLL_Entry_Hook = procedure (dllparam : PtrInt);
  76. const
  77. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  78. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  79. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  80. Const
  81. { it can be discussed whether fmShareDenyNone means read and write or read, write and delete, see
  82. also http://bugs.freepascal.org/view.php?id=8898, this allows users to configure the used
  83. value
  84. }
  85. fmShareDenyNoneFlags : DWord = 3;
  86. implementation
  87. {$asmmode att}
  88. var
  89. {$ifdef VER3_0}
  90. SysInstance : qword;
  91. FPCSysInstance: PQWord = @SysInstance; public name '_FPC_SysInstance';
  92. {$else VER3_0}
  93. FPCSysInstance : PQWord;public name '_FPC_SysInstance';
  94. {$endif VER3_0}
  95. {$define FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
  96. procedure OsSetupEntryInformation(constref info: TEntryInformation); forward;
  97. {$ifdef FPC_USE_WIN64_SEH}
  98. function main_wrapper(arg: Pointer; proc: Pointer): ptrint; assembler; nostackframe;
  99. asm
  100. subq $40, %rsp
  101. .seh_stackalloc 40
  102. .seh_endprologue
  103. call %rdx { "arg" is passed in %rcx }
  104. nop { this nop is critical for exception handling }
  105. addq $40, %rsp
  106. .seh_handler __FPC_default_handler,@except,@unwind
  107. end;
  108. {$endif FPC_USE_WIN64_SEH}
  109. {$define FPC_SYSTEM_HAS_STACKTOP}
  110. function StackTop: pointer; assembler;nostackframe;
  111. asm
  112. movq %gs:(8),%rax
  113. end;
  114. { include system independent routines }
  115. {$I system.inc}
  116. {*****************************************************************************
  117. System Dependent Exit code
  118. *****************************************************************************}
  119. {$ifndef FPC_USE_WIN64_SEH}
  120. procedure install_exception_handlers;forward;
  121. {$endif FPC_USE_WIN64_SEH}
  122. {$ifdef VER3_0}
  123. procedure PascalMain;external name 'PASCALMAIN';
  124. {$endif VER3_0}
  125. { include code common with win32 }
  126. {$I syswin.inc}
  127. {$ifdef VER3_0}
  128. { TLS directory code }
  129. {$I systlsdir.inc}
  130. {$endif VER3_0}
  131. procedure OsSetupEntryInformation(constref info: TEntryInformation);
  132. begin
  133. TlsKey := info.OS.TlsKeyAddr;
  134. FPCSysInstance := info.OS.SysInstance;
  135. WStrInitTablesTable := info.OS.WideInitTables;
  136. end;
  137. Procedure system_exit;
  138. begin
  139. { see comments in win32/system.pp about this logic }
  140. if IsLibrary then
  141. begin
  142. if DllInitState in [DLL_PROCESS_ATTACH,DLL_PROCESS_DETACH] then
  143. LongJmp(DLLBuf,1)
  144. else
  145. MainThreadIDWin32:=0;
  146. end;
  147. if not IsConsole then
  148. begin
  149. Close(stderr);
  150. Close(stdout);
  151. Close(erroutput);
  152. Close(Input);
  153. Close(Output);
  154. { what about Input and Output ?? PM }
  155. { now handled, FPK }
  156. end;
  157. { call exitprocess, with cleanup as required }
  158. ExitProcess(exitcode);
  159. end;
  160. var
  161. { old compilers emitted a reference to _fltused if a module contains
  162. floating type code so the linker could leave away floating point
  163. libraries or not. VC does this as well so we need to define this
  164. symbol as well (FK)
  165. }
  166. _fltused : int64;cvar;public;
  167. { value of the stack segment
  168. to check if the call stack can be written on exceptions }
  169. _SS : Cardinal;
  170. {$ifdef VER3_0}
  171. procedure Exe_entry;[public,alias:'_FPC_EXE_Entry'];
  172. {$else VER3_0}
  173. procedure Exe_entry(constref info: TEntryInformation);[public,alias:'_FPC_EXE_Entry'];
  174. {$endif VER3_0}
  175. begin
  176. {$ifndef VER3_0}
  177. SetupEntryInformation(info);
  178. {$endif VER3_0}
  179. IsLibrary:=false;
  180. { install the handlers for exe only ?
  181. or should we install them for DLL also ? (PM) }
  182. {$ifndef FPC_USE_WIN64_SEH}
  183. install_exception_handlers;
  184. {$endif FPC_USE_WIN64_SEH}
  185. ExitCode:=0;
  186. asm
  187. xorq %rax,%rax
  188. movw %ss,%ax
  189. movl %eax,_SS(%rip)
  190. movq %rbp,%rsi
  191. xorq %rbp,%rbp
  192. {$ifdef VER3_0}
  193. {$ifdef FPC_USE_WIN64_SEH}
  194. xor %rcx,%rcx
  195. lea PASCALMAIN(%rip),%rdx
  196. call main_wrapper
  197. {$else FPC_USE_WIN64_SEH}
  198. call PASCALMAIN
  199. {$endif FPC_USE_WIN64_SEH}
  200. {$else VER3_0}
  201. {$ifdef FPC_USE_WIN64_SEH}
  202. xor %rcx,%rcx
  203. lea EntryInformation(%rip),%rdx
  204. movq TEntryInformation.PascalMain(%rdx),%rdx
  205. call main_wrapper
  206. {$else FPC_USE_WIN64_SEH}
  207. lea EntryInformation(%rip),%rdx
  208. call TEntryInformation.PascalMain(%rdx)
  209. {$endif FPC_USE_WIN64_SEH}
  210. {$endif VER3_0}
  211. movq %rsi,%rbp
  212. end ['RSI','RBP']; { <-- specifying RSI allows compiler to save/restore it properly }
  213. { if we pass here there was no error ! }
  214. system_exit;
  215. end;
  216. {$ifdef VER3_0}
  217. procedure _FPC_DLLMainCRTStartup(_hinstance : qword;_dllreason : dword;_dllparam:Pointer);stdcall;public name '_DLLMainCRTStartup';
  218. begin
  219. IsConsole:=true;
  220. sysinstance:=_hinstance;
  221. dllreason:=_dllreason;
  222. dllparam:=PtrInt(_dllparam);
  223. DLL_Entry;
  224. end;
  225. procedure _FPC_DLLWinMainCRTStartup(_hinstance : qword;_dllreason : dword;_dllparam:Pointer);stdcall;public name '_DLLWinMainCRTStartup';
  226. begin
  227. IsConsole:=false;
  228. sysinstance:=_hinstance;
  229. dllreason:=_dllreason;
  230. dllparam:=PtrInt(_dllparam);
  231. DLL_Entry;
  232. end;
  233. {$endif VER3_0}
  234. function is_prefetch(p : pointer) : boolean;
  235. var
  236. a : array[0..15] of byte;
  237. doagain : boolean;
  238. instrlo,instrhi,opcode : byte;
  239. i : longint;
  240. begin
  241. result:=false;
  242. { read memory savely without causing another exeception }
  243. if not(ReadProcessMemory(GetCurrentProcess,p,@a,sizeof(a),nil)) then
  244. exit;
  245. i:=0;
  246. doagain:=true;
  247. while doagain and (i<15) do
  248. begin
  249. opcode:=a[i];
  250. instrlo:=opcode and $f;
  251. instrhi:=opcode and $f0;
  252. case instrhi of
  253. { prefix? }
  254. $20,$30:
  255. doagain:=(instrlo and 7)=6;
  256. $60:
  257. doagain:=(instrlo and $c)=4;
  258. $f0:
  259. doagain:=instrlo in [0,2,3];
  260. $0:
  261. begin
  262. result:=(instrlo=$f) and (a[i+1] in [$d,$18]);
  263. exit;
  264. end;
  265. else
  266. doagain:=false;
  267. end;
  268. inc(i);
  269. end;
  270. end;
  271. //
  272. // Hardware exception handling
  273. //
  274. {$I seh64.inc}
  275. type
  276. TVectoredExceptionHandler = function (excep : PExceptionPointers) : Longint;
  277. function AddVectoredExceptionHandler(FirstHandler : DWORD;VectoredHandler : TVectoredExceptionHandler) : longint;
  278. external 'kernel32' name 'AddVectoredExceptionHandler';
  279. {$ifndef FPC_USE_WIN64_SEH}
  280. const
  281. MaxExceptionLevel = 16;
  282. exceptLevel : Byte = 0;
  283. var
  284. exceptRip : array[0..MaxExceptionLevel-1] of Int64;
  285. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  286. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  287. {$ifdef SYSTEMEXCEPTIONDEBUG}
  288. procedure DebugHandleErrorAddrFrame(error : longint; addr, frame : pointer);
  289. begin
  290. if IsConsole then
  291. begin
  292. write(stderr,'HandleErrorAddrFrame(error=',error);
  293. write(stderr,',addr=',hexstr(int64(addr),16));
  294. writeln(stderr,',frame=',hexstr(int64(frame),16),')');
  295. end;
  296. HandleErrorAddrFrame(error,addr,frame);
  297. end;
  298. {$endif SYSTEMEXCEPTIONDEBUG}
  299. procedure JumpToHandleErrorFrame;
  300. var
  301. rip, rbp : int64;
  302. error : longint;
  303. begin
  304. // save ebp
  305. asm
  306. movq (%rbp),%rax
  307. movq %rax,rbp
  308. end;
  309. if exceptLevel>0 then
  310. dec(exceptLevel);
  311. rip:=exceptRip[exceptLevel];
  312. error:=exceptError[exceptLevel];
  313. {$ifdef SYSTEMEXCEPTIONDEBUG}
  314. if IsConsole then
  315. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  316. {$endif SYSTEMEXCEPTIONDEBUG}
  317. if resetFPU[exceptLevel] then
  318. SysResetFPU;
  319. { build a fake stack }
  320. asm
  321. movq rbp,%r8
  322. movq rip,%rdx
  323. movl error,%ecx
  324. pushq rip
  325. movq rbp,%rbp // Change frame pointer
  326. {$ifdef SYSTEMEXCEPTIONDEBUG}
  327. jmpl DebugHandleErrorAddrFrame
  328. {$else not SYSTEMEXCEPTIONDEBUG}
  329. jmpl HandleErrorAddrFrame
  330. {$endif SYSTEMEXCEPTIONDEBUG}
  331. end;
  332. end;
  333. function syswin64_x86_64_exception_handler(excep : PExceptionPointers) : Longint;public;
  334. var
  335. res: longint;
  336. err: byte;
  337. must_reset_fpu: boolean;
  338. begin
  339. res:=EXCEPTION_CONTINUE_SEARCH;
  340. {$ifdef SYSTEMEXCEPTIONDEBUG}
  341. if IsConsole then
  342. Writeln(stderr,'syswin64_x86_64_exception_handler called');
  343. {$endif SYSTEMEXCEPTIONDEBUG}
  344. if excep^.ContextRecord^.SegSs=_SS then
  345. begin
  346. err := 0;
  347. must_reset_fpu := true;
  348. {$ifdef SYSTEMEXCEPTIONDEBUG}
  349. if IsConsole then Writeln(stderr,'Exception ',
  350. hexstr(excep^.ExceptionRecord^.ExceptionCode,8));
  351. {$endif SYSTEMEXCEPTIONDEBUG}
  352. case cardinal(excep^.ExceptionRecord^.ExceptionCode) of
  353. STATUS_INTEGER_DIVIDE_BY_ZERO,
  354. STATUS_FLOAT_DIVIDE_BY_ZERO :
  355. err := 200;
  356. STATUS_ARRAY_BOUNDS_EXCEEDED :
  357. begin
  358. err := 201;
  359. must_reset_fpu := false;
  360. end;
  361. STATUS_STACK_OVERFLOW :
  362. begin
  363. err := 202;
  364. must_reset_fpu := false;
  365. end;
  366. STATUS_FLOAT_OVERFLOW :
  367. err := 205;
  368. STATUS_FLOAT_DENORMAL_OPERAND,
  369. STATUS_FLOAT_UNDERFLOW :
  370. err := 206;
  371. { excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  372. STATUS_FLOAT_INEXACT_RESULT,
  373. STATUS_FLOAT_INVALID_OPERATION,
  374. STATUS_FLOAT_STACK_CHECK :
  375. err := 207;
  376. STATUS_INTEGER_OVERFLOW :
  377. begin
  378. err := 215;
  379. must_reset_fpu := false;
  380. end;
  381. STATUS_ILLEGAL_INSTRUCTION:
  382. err := 216;
  383. STATUS_ACCESS_VIOLATION:
  384. { Athlon prefetch bug? }
  385. if is_prefetch(pointer(excep^.ContextRecord^.rip)) then
  386. begin
  387. { if yes, then retry }
  388. excep^.ExceptionRecord^.ExceptionCode := 0;
  389. res:=EXCEPTION_CONTINUE_EXECUTION;
  390. end
  391. else
  392. err := 216;
  393. STATUS_CONTROL_C_EXIT:
  394. err := 217;
  395. STATUS_PRIVILEGED_INSTRUCTION:
  396. begin
  397. err := 218;
  398. must_reset_fpu := false;
  399. end;
  400. else
  401. begin
  402. if ((excep^.ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  403. err := 217
  404. else
  405. { pass through exceptions which aren't an error. The problem is that vectored handlers
  406. always are called before structured ones so we see also internal exceptions of libraries.
  407. I wonder if there is a better solution (FK)
  408. }
  409. res:=EXCEPTION_CONTINUE_SEARCH;
  410. end;
  411. end;
  412. if (err <> 0) and (exceptLevel < MaxExceptionLevel) then
  413. begin
  414. exceptRip[exceptLevel] := excep^.ContextRecord^.Rip;
  415. exceptError[exceptLevel] := err;
  416. resetFPU[exceptLevel] := must_reset_fpu;
  417. inc(exceptLevel);
  418. excep^.ContextRecord^.Rip := Int64(@JumpToHandleErrorFrame);
  419. excep^.ExceptionRecord^.ExceptionCode := 0;
  420. res := EXCEPTION_CONTINUE_EXECUTION;
  421. {$ifdef SYSTEMEXCEPTIONDEBUG}
  422. if IsConsole then begin
  423. writeln(stderr,'Exception Continue Exception set at ',
  424. hexstr(exceptRip[exceptLevel-1],16));
  425. writeln(stderr,'Rip changed to ',
  426. hexstr(int64(@JumpToHandleErrorFrame),16), ' error=', err);
  427. end;
  428. {$endif SYSTEMEXCEPTIONDEBUG}
  429. end;
  430. end;
  431. syswin64_x86_64_exception_handler := res;
  432. end;
  433. procedure install_exception_handlers;
  434. begin
  435. AddVectoredExceptionHandler(1,@syswin64_x86_64_exception_handler);
  436. end;
  437. {$endif ndef FPC_USE_WIN64_SEH}
  438. {$ifdef VER3_0}
  439. procedure LinkIn(p1,p2,p3: Pointer); inline;
  440. begin
  441. end;
  442. procedure _FPC_mainCRTStartup;stdcall;public name '_mainCRTStartup';
  443. begin
  444. IsConsole:=true;
  445. GetConsoleMode(GetStdHandle((Std_Input_Handle)),@StartupConsoleMode);
  446. {$ifdef FPC_USE_TLS_DIRECTORY}
  447. LinkIn(@_tls_used,@FreePascal_TLS_callback,@FreePascal_end_of_TLS_callback);
  448. {$endif FPC_USE_TLS_DIRECTORY}
  449. Exe_entry;
  450. end;
  451. procedure _FPC_WinMainCRTStartup;stdcall;public name '_WinMainCRTStartup';
  452. begin
  453. IsConsole:=false;
  454. {$ifdef FPC_USE_TLS_DIRECTORY}
  455. LinkIn(@_tls_used,@FreePascal_TLS_callback,@FreePascal_end_of_TLS_callback);
  456. {$endif FPC_USE_TLS_DIRECTORY}
  457. Exe_entry;
  458. end;
  459. {$endif VER3_0}
  460. {$ifdef FPC_SECTION_THREADVARS}
  461. function fpc_tls_add(addr: pointer): pointer; assembler; nostackframe;
  462. [public,alias: 'FPC_TLS_ADD']; compilerproc;
  463. asm
  464. sub $56,%rsp { 32 spill area + 16 local vars + 8 misalignment }
  465. .seh_stackalloc 56
  466. .seh_endprologue
  467. lea tls_data_start(%rip),%rax
  468. sub %rax,%rcx
  469. cmpb $0,IsLibrary(%rip)
  470. mov _tls_index(%rip),%eax
  471. jnz .L1
  472. mov %gs:(88),%rdx
  473. add (%rdx,%rax,8),%rcx
  474. mov %rcx,%rax
  475. jmp .L3
  476. .L1:
  477. mov %rcx,32(%rsp)
  478. call GetLastError
  479. mov %rax,40(%rsp) { save LastError }
  480. mov _tls_index(%rip),%ecx
  481. call TlsGetValue
  482. test %rax,%rax
  483. jnz .L2
  484. { This can happen when a thread existed before DLL was loaded,
  485. or if DisableThreadLibraryCalls was called. }
  486. call SysAllocateThreadVars
  487. mov $0x1000000,%rcx
  488. call InitThread
  489. mov _tls_index(%rip),%ecx
  490. call TlsGetValue
  491. .L2:
  492. add %rax,32(%rsp)
  493. mov 40(%rsp),%rcx
  494. call SetLastError
  495. mov 32(%rsp),%rax
  496. .L3:
  497. add $56,%rsp
  498. end;
  499. {$endif FPC_SECTION_THREADVARS}
  500. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  501. type
  502. tdosheader = packed record
  503. e_magic : word;
  504. e_cblp : word;
  505. e_cp : word;
  506. e_crlc : word;
  507. e_cparhdr : word;
  508. e_minalloc : word;
  509. e_maxalloc : word;
  510. e_ss : word;
  511. e_sp : word;
  512. e_csum : word;
  513. e_ip : word;
  514. e_cs : word;
  515. e_lfarlc : word;
  516. e_ovno : word;
  517. e_res : array[0..3] of word;
  518. e_oemid : word;
  519. e_oeminfo : word;
  520. e_res2 : array[0..9] of word;
  521. e_lfanew : longint;
  522. end;
  523. tpeheader = packed record
  524. PEMagic : longint;
  525. Machine : word;
  526. NumberOfSections : word;
  527. TimeDateStamp : longint;
  528. PointerToSymbolTable : longint;
  529. NumberOfSymbols : longint;
  530. SizeOfOptionalHeader : word;
  531. Characteristics : word;
  532. Magic : word;
  533. MajorLinkerVersion : byte;
  534. MinorLinkerVersion : byte;
  535. SizeOfCode : longint;
  536. SizeOfInitializedData : longint;
  537. SizeOfUninitializedData : longint;
  538. AddressOfEntryPoint : longint;
  539. BaseOfCode : longint;
  540. {$ifdef win32}
  541. BaseOfData : longint;
  542. {$endif win32}
  543. ImageBase : PtrInt;
  544. SectionAlignment : longint;
  545. FileAlignment : longint;
  546. MajorOperatingSystemVersion : word;
  547. MinorOperatingSystemVersion : word;
  548. MajorImageVersion : word;
  549. MinorImageVersion : word;
  550. MajorSubsystemVersion : word;
  551. MinorSubsystemVersion : word;
  552. Reserved1 : longint;
  553. SizeOfImage : longint;
  554. SizeOfHeaders : longint;
  555. CheckSum : longint;
  556. Subsystem : word;
  557. DllCharacteristics : word;
  558. SizeOfStackReserve : PtrInt;
  559. SizeOfStackCommit : PtrInt;
  560. SizeOfHeapReserve : PtrInt;
  561. SizeOfHeapCommit : PtrInt;
  562. LoaderFlags : longint;
  563. NumberOfRvaAndSizes : longint;
  564. DataDirectory : array[1..$80] of byte;
  565. end;
  566. begin
  567. result:=tpeheader((pointer(getmodulehandle(nil))+(tdosheader(pointer(getmodulehandle(nil))^).e_lfanew))^).SizeOfStackReserve;
  568. end;
  569. begin
  570. { pass dummy value }
  571. StackLength := CheckInitialStkLen($1000000);
  572. StackBottom := StackTop - StackLength;
  573. { get some helpful informations }
  574. GetStartupInfo(@startupinfo);
  575. { some misc Win32 stuff }
  576. if not IsLibrary then
  577. FPCSysInstance^:=getmodulehandle(nil);
  578. MainInstance:=FPCSysInstance^;
  579. cmdshow:=startupinfo.wshowwindow;
  580. { Setup heap and threading, these may be already initialized from TLS callback }
  581. if not Assigned(CurrentTM.BeginThread) then
  582. begin
  583. InitHeap;
  584. InitSystemThreads;
  585. end;
  586. SysInitExceptions;
  587. initunicodestringmanager;
  588. InitWin32Widestrings;
  589. SysInitStdIO;
  590. { Arguments }
  591. setup_arguments;
  592. InitSystemDynLibs;
  593. { Reset IO Error }
  594. InOutRes:=0;
  595. ProcessID := GetCurrentProcessID;
  596. DispCallByIDProc:=@DoDispCallByIDError;
  597. end.