system.pp 18 KB

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