system.pp 18 KB

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