system.pp 19 KB

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