system.pp 18 KB

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