system.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl and Pavel Ozerski
  4. member of the Free Pascal development team.
  5. FPC Pascal system unit for the Win32 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. {$ifdef SYSTEMDEBUG}
  15. {$define SYSTEMEXCEPTIONDEBUG}
  16. {$endif SYSTEMDEBUG}
  17. {$define FPC_HAS_INDIRECT_MAIN_INFORMATION}
  18. {$ifdef cpui386}
  19. {$define Set_i386_Exception_handler}
  20. {$endif cpui386}
  21. {$define DISABLE_NO_THREAD_MANAGER}
  22. {$define HAS_WIDESTRINGMANAGER}
  23. {$define DISABLE_NO_DYNLIBS_MANAGER}
  24. {$define FPC_SYSTEM_HAS_SYSDLH}
  25. {$ifdef FPC_USE_WIN32_SEH}
  26. {$define FPC_SYSTEM_HAS_RAISEEXCEPTION}
  27. {$define FPC_SYSTEM_HAS_RERAISE}
  28. {$define FPC_SYSTEM_HAS_DONEEXCEPTION}
  29. {$define FPC_SYSTEM_HAS_SAFECALLHANDLER}
  30. {$endif FPC_USE_WIN32_SEH}
  31. { include system-independent routine headers }
  32. {$I systemh.inc}
  33. const
  34. LineEnding = #13#10;
  35. LFNSupport = true;
  36. DirectorySeparator = '\';
  37. DriveSeparator = ':';
  38. ExtensionSeparator = '.';
  39. PathSeparator = ';';
  40. AllowDirectorySeparators : set of char = ['\','/'];
  41. AllowDriveSeparators : set of char = [':'];
  42. { FileNameCaseSensitive and FileNameCasePreserving are defined separately below!!! }
  43. maxExitCode = 65535;
  44. MaxPathLen = 260;
  45. AllFilesMask = '*';
  46. type
  47. PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  48. TEXCEPTION_FRAME = record
  49. next : PEXCEPTION_FRAME;
  50. handler : pointer;
  51. end;
  52. const
  53. { Default filehandles }
  54. UnusedHandle : THandle = THandle(-1);
  55. StdInputHandle : THandle = 0;
  56. StdOutputHandle : THandle = 0;
  57. StdErrorHandle : THandle = 0;
  58. FileNameCaseSensitive : boolean = false;
  59. FileNameCasePreserving: boolean = true;
  60. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  61. sLineBreak = LineEnding;
  62. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  63. System_exception_frame : PEXCEPTION_FRAME =nil;
  64. var
  65. { C compatible arguments }
  66. argc : longint; public name 'operatingsystem_parameter_argc';
  67. argv : ppchar; public name 'operatingsystem_parameter_argv';
  68. { Win32 Info }
  69. startupinfo : tstartupinfo deprecated; // Delphi does not have one in interface
  70. MainInstance,
  71. cmdshow : longint;
  72. DLLreason : dword; public name 'operatingsystem_dllreason';
  73. DLLparam : PtrInt; public name 'operatingsystem_dllparam';
  74. StartupConsoleMode : DWORD;
  75. const
  76. hprevinst: longint=0;
  77. type
  78. TDLL_Entry_Hook = procedure (dllparam : PtrInt);
  79. const
  80. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  81. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  82. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  83. Const
  84. { it can be discussed whether fmShareDenyNone means read and write or read, write and delete, see
  85. also http://bugs.freepascal.org/view.php?id=8898, this allows users to configure the used
  86. value
  87. }
  88. fmShareDenyNoneFlags : DWord = 3;
  89. implementation
  90. var
  91. SysInstance : Longint;public name '_FPC_SysInstance';
  92. {$ifdef FPC_USE_WIN32_SEH}
  93. function main_wrapper(arg: Pointer; proc: Pointer): ptrint; forward;
  94. procedure OutermostHandler; external name '__FPC_DEFAULT_HANDLER';
  95. {$endif FPC_USE_WIN32_SEH}
  96. {$define FPC_SYSTEM_HAS_STACKTOP}
  97. function StackTop: pointer; assembler;nostackframe;
  98. asm
  99. movl %fs:(4),%eax
  100. end;
  101. { include system independent routines }
  102. {$I system.inc}
  103. { include code common with win64 }
  104. {$I syswin.inc}
  105. {*****************************************************************************
  106. System Dependent Exit code
  107. *****************************************************************************}
  108. {$ifndef FPC_USE_WIN32_SEH}
  109. procedure install_exception_handlers;forward;
  110. procedure remove_exception_handlers;forward;
  111. {$endif FPC_USE_WIN32_SEH}
  112. Procedure system_exit;
  113. begin
  114. if IsLibrary then
  115. begin
  116. { If exiting from DLL_PROCESS_ATTACH/DETACH, unwind to DllMain context. }
  117. if DllInitState in [DLL_PROCESS_ATTACH,DLL_PROCESS_DETACH] then
  118. LongJmp(DLLBuf,1)
  119. else
  120. { Abnormal termination, Halt has been called from DLL function,
  121. put down the entire process (DLL_PROCESS_DETACH will still
  122. occur). At this point RTL has been already finalized in InternalExit
  123. and shouldn't be finalized another time in DLL_PROCESS_DETACH.
  124. Indicate this by resetting MainThreadIdWin32. }
  125. MainThreadIDWin32:=0;
  126. end;
  127. if not IsConsole then
  128. begin
  129. Close(stderr);
  130. Close(stdout);
  131. Close(erroutput);
  132. Close(Input);
  133. Close(Output);
  134. { what about Input and Output ?? PM }
  135. { now handled, FPK }
  136. end;
  137. {$ifndef FPC_USE_WIN32_SEH}
  138. if not IsLibrary then
  139. remove_exception_handlers;
  140. {$endif FPC_USE_WIN32_SEH}
  141. { do cleanup required by the startup code }
  142. EntryInformation.OS.asm_exit();
  143. { call exitprocess, with cleanup as required }
  144. ExitProcess(exitcode);
  145. end;
  146. var
  147. { value of the stack segment
  148. to check if the call stack can be written on exceptions }
  149. _SS : Cardinal;
  150. procedure Exe_entry(const info : TEntryInformation);[public,alias:'_FPC_EXE_Entry'];
  151. var
  152. xframe: TEXCEPTION_FRAME;
  153. begin
  154. SetupEntryInformation(info);
  155. IsLibrary:=false;
  156. { install the handlers for exe only ?
  157. or should we install them for DLL also ? (PM) }
  158. {$ifndef FPC_USE_WIN32_SEH}
  159. install_exception_handlers;
  160. {$endif FPC_USE_WIN32_SEH}
  161. { This strange construction is needed to solve the _SS problem
  162. with a smartlinked syswin32 (PFV) }
  163. asm
  164. { movl %esp,%fs:(0)
  165. but don't insert it as it doesn't
  166. point to anything yet
  167. this will be used in signals unit }
  168. leal xframe,%eax
  169. movl %fs:(0),%ecx
  170. movl %ecx,TException_Frame.next(%eax)
  171. movl %eax,System_exception_frame
  172. {$ifndef FPC_USE_WIN32_SEH}
  173. movl $0,TException_Frame.handler(%eax)
  174. {$else}
  175. movl $OutermostHandler,TException_Frame.handler(%eax)
  176. movl %eax,%fs:(0)
  177. {$endif FPC_USE_WIN32_SEH}
  178. pushl %ebp
  179. xorl %eax,%eax
  180. movw %ss,%ax
  181. movl %eax,_SS
  182. xorl %ebp,%ebp
  183. end;
  184. EntryInformation.PascalMain();
  185. asm
  186. popl %ebp
  187. end;
  188. { if we pass here there was no error ! }
  189. system_exit;
  190. end;
  191. function is_prefetch(p : pointer) : boolean;
  192. var
  193. a : array[0..15] of byte;
  194. doagain : boolean;
  195. instrlo,instrhi,opcode : byte;
  196. i : longint;
  197. begin
  198. result:=false;
  199. { read memory savely without causing another exeception }
  200. if not(ReadProcessMemory(GetCurrentProcess,p,@a,sizeof(a),nil)) then
  201. exit;
  202. i:=0;
  203. doagain:=true;
  204. while doagain and (i<15) do
  205. begin
  206. opcode:=a[i];
  207. instrlo:=opcode and $f;
  208. instrhi:=opcode and $f0;
  209. case instrhi of
  210. { prefix? }
  211. $20,$30:
  212. doagain:=(instrlo and 7)=6;
  213. $60:
  214. doagain:=(instrlo and $c)=4;
  215. $f0:
  216. doagain:=instrlo in [0,2,3];
  217. $0:
  218. begin
  219. result:=(instrlo=$f) and (a[i+1] in [$d,$18]);
  220. exit;
  221. end;
  222. else
  223. doagain:=false;
  224. end;
  225. inc(i);
  226. end;
  227. end;
  228. //
  229. // Hardware exception handling
  230. //
  231. {$ifdef Set_i386_Exception_handler}
  232. type
  233. PFloatingSaveArea = ^TFloatingSaveArea;
  234. TFloatingSaveArea = packed record
  235. ControlWord : Cardinal;
  236. StatusWord : Cardinal;
  237. TagWord : Cardinal;
  238. ErrorOffset : Cardinal;
  239. ErrorSelector : Cardinal;
  240. DataOffset : Cardinal;
  241. DataSelector : Cardinal;
  242. RegisterArea : array[0..79] of Byte;
  243. Cr0NpxState : Cardinal;
  244. end;
  245. PContext = ^TContext;
  246. TContext = packed record
  247. //
  248. // The flags values within this flag control the contents of
  249. // a CONTEXT record.
  250. //
  251. ContextFlags : Cardinal;
  252. //
  253. // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  254. // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
  255. // included in CONTEXT_FULL.
  256. //
  257. Dr0, Dr1, Dr2,
  258. Dr3, Dr6, Dr7 : Cardinal;
  259. //
  260. // This section is specified/returned if the
  261. // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  262. //
  263. FloatSave : TFloatingSaveArea;
  264. //
  265. // This section is specified/returned if the
  266. // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  267. //
  268. SegGs, SegFs,
  269. SegEs, SegDs : Cardinal;
  270. //
  271. // This section is specified/returned if the
  272. // ContextFlags word contains the flag CONTEXT_INTEGER.
  273. //
  274. Edi, Esi, Ebx,
  275. Edx, Ecx, Eax : Cardinal;
  276. //
  277. // This section is specified/returned if the
  278. // ContextFlags word contains the flag CONTEXT_CONTROL.
  279. //
  280. Ebp : Cardinal;
  281. Eip : Cardinal;
  282. SegCs : Cardinal;
  283. EFlags, Esp, SegSs : Cardinal;
  284. //
  285. // This section is specified/returned if the ContextFlags word
  286. // contains the flag CONTEXT_EXTENDED_REGISTERS.
  287. // The format and contexts are processor specific
  288. //
  289. ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  290. end;
  291. PExceptionPointers = ^TExceptionPointers;
  292. TExceptionPointers = packed record
  293. ExceptionRecord : PExceptionRecord;
  294. ContextRecord : PContext;
  295. end;
  296. { type of functions that should be used for exception handling }
  297. TTopLevelExceptionFilter = function (excep : PExceptionPointers) : Longint;stdcall;
  298. {$i seh32.inc}
  299. {$ifndef FPC_USE_WIN32_SEH}
  300. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : TTopLevelExceptionFilter) : TTopLevelExceptionFilter;
  301. stdcall;external 'kernel32' name 'SetUnhandledExceptionFilter';
  302. const
  303. MaxExceptionLevel = 16;
  304. exceptLevel : Byte = 0;
  305. var
  306. exceptEip : array[0..MaxExceptionLevel-1] of Longint;
  307. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  308. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  309. {$ifdef SYSTEMEXCEPTIONDEBUG}
  310. procedure DebugHandleErrorAddrFrame(error : longint; addr, frame : pointer);
  311. begin
  312. if IsConsole then
  313. begin
  314. write(stderr,'HandleErrorAddrFrame(error=',error);
  315. write(stderr,',addr=',hexstr(ptruint(addr),8));
  316. writeln(stderr,',frame=',hexstr(ptruint(frame),8),')');
  317. end;
  318. HandleErrorAddrFrame(error,addr,frame);
  319. end;
  320. {$endif SYSTEMEXCEPTIONDEBUG}
  321. procedure JumpToHandleErrorFrame;
  322. var
  323. eip, ebp, error : Longint;
  324. begin
  325. // save ebp
  326. asm
  327. movl (%ebp),%eax
  328. movl %eax,ebp
  329. end;
  330. if (exceptLevel > 0) then
  331. dec(exceptLevel);
  332. eip:=exceptEip[exceptLevel];
  333. error:=exceptError[exceptLevel];
  334. {$ifdef SYSTEMEXCEPTIONDEBUG}
  335. if IsConsole then
  336. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  337. {$endif SYSTEMEXCEPTIONDEBUG}
  338. if resetFPU[exceptLevel] then
  339. SysResetFPU;
  340. { build a fake stack }
  341. asm
  342. movl ebp,%ecx
  343. movl eip,%edx
  344. movl error,%eax
  345. pushl eip
  346. movl ebp,%ebp // Change frame pointer
  347. {$ifdef SYSTEMEXCEPTIONDEBUG}
  348. jmpl DebugHandleErrorAddrFrame
  349. {$else not SYSTEMEXCEPTIONDEBUG}
  350. jmpl HandleErrorAddrFrame
  351. {$endif SYSTEMEXCEPTIONDEBUG}
  352. end;
  353. end;
  354. function syswin32_i386_exception_handler(excep : PExceptionPointers) : Longint;stdcall;
  355. var
  356. res: longint;
  357. err: byte;
  358. must_reset_fpu: boolean;
  359. begin
  360. res := EXCEPTION_CONTINUE_SEARCH;
  361. if excep^.ContextRecord^.SegSs=_SS then begin
  362. err := 0;
  363. must_reset_fpu := true;
  364. {$ifdef SYSTEMEXCEPTIONDEBUG}
  365. if IsConsole then Writeln(stderr,'Exception ',
  366. hexstr(excep^.ExceptionRecord^.ExceptionCode, 8));
  367. {$endif SYSTEMEXCEPTIONDEBUG}
  368. case excep^.ExceptionRecord^.ExceptionCode of
  369. STATUS_INTEGER_DIVIDE_BY_ZERO,
  370. STATUS_FLOAT_DIVIDE_BY_ZERO :
  371. err := 200;
  372. STATUS_ARRAY_BOUNDS_EXCEEDED :
  373. begin
  374. err := 201;
  375. must_reset_fpu := false;
  376. end;
  377. STATUS_STACK_OVERFLOW :
  378. begin
  379. err := 202;
  380. must_reset_fpu := false;
  381. end;
  382. STATUS_FLOAT_OVERFLOW :
  383. err := 205;
  384. STATUS_FLOAT_DENORMAL_OPERAND,
  385. STATUS_FLOAT_UNDERFLOW :
  386. err := 206;
  387. {excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  388. STATUS_FLOAT_INEXACT_RESULT,
  389. STATUS_FLOAT_INVALID_OPERATION,
  390. STATUS_FLOAT_STACK_CHECK :
  391. err := 207;
  392. STATUS_INTEGER_OVERFLOW :
  393. begin
  394. err := 215;
  395. must_reset_fpu := false;
  396. end;
  397. STATUS_ILLEGAL_INSTRUCTION:
  398. { if we're testing sse support, simply set the flag and continue }
  399. if sse_check then
  400. begin
  401. os_supports_sse:=false;
  402. { skip the offending movaps %xmm7, %xmm6 instruction }
  403. inc(excep^.ContextRecord^.Eip,3);
  404. excep^.ExceptionRecord^.ExceptionCode := 0;
  405. res:=EXCEPTION_CONTINUE_EXECUTION;
  406. end
  407. else
  408. err := 216;
  409. STATUS_ACCESS_VIOLATION:
  410. { Athlon prefetch bug? }
  411. if is_prefetch(pointer(excep^.ContextRecord^.Eip)) then
  412. begin
  413. { if yes, then retry }
  414. excep^.ExceptionRecord^.ExceptionCode := 0;
  415. res:=EXCEPTION_CONTINUE_EXECUTION;
  416. end
  417. else
  418. err := 216;
  419. STATUS_CONTROL_C_EXIT:
  420. err := 217;
  421. STATUS_PRIVILEGED_INSTRUCTION:
  422. begin
  423. err := 218;
  424. must_reset_fpu := false;
  425. end;
  426. else
  427. begin
  428. if ((excep^.ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  429. err := 217
  430. else
  431. err := 255;
  432. end;
  433. end;
  434. if (err <> 0) and (exceptLevel < MaxExceptionLevel) then begin
  435. exceptEip[exceptLevel] := excep^.ContextRecord^.Eip;
  436. exceptError[exceptLevel] := err;
  437. resetFPU[exceptLevel] := must_reset_fpu;
  438. inc(exceptLevel);
  439. excep^.ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  440. excep^.ExceptionRecord^.ExceptionCode := 0;
  441. res := EXCEPTION_CONTINUE_EXECUTION;
  442. {$ifdef SYSTEMEXCEPTIONDEBUG}
  443. if IsConsole then begin
  444. writeln(stderr,'Exception Continue Exception set at ',
  445. hexstr(exceptEip[exceptLevel],8));
  446. writeln(stderr,'Eip changed to ',
  447. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', err);
  448. end;
  449. {$endif SYSTEMEXCEPTIONDEBUG}
  450. end;
  451. end;
  452. syswin32_i386_exception_handler := res;
  453. end;
  454. procedure install_exception_handlers;
  455. begin
  456. SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  457. end;
  458. procedure remove_exception_handlers;
  459. begin
  460. SetUnhandledExceptionFilter(nil);
  461. end;
  462. {$endif not FPC_USE_WIN32_SEH}
  463. {$else not cpui386 (Processor specific !!)}
  464. procedure install_exception_handlers;
  465. begin
  466. end;
  467. procedure remove_exception_handlers;
  468. begin
  469. end;
  470. {$endif Set_i386_Exception_handler}
  471. {$ifdef FPC_SECTION_THREADVARS}
  472. function fpc_tls_add(addr: pointer): pointer; assembler; nostackframe;
  473. [public,alias: 'FPC_TLS_ADD']; compilerproc;
  474. asm
  475. sub $tls_data_start,%eax
  476. cmpb $0,IsLibrary
  477. mov _tls_index,%ecx
  478. jnz .L1
  479. mov %fs:(0x2c),%edx
  480. add (%edx,%ecx,4),%eax
  481. ret
  482. .L1:
  483. push %ebx
  484. mov %eax,%ebx
  485. call GetLastError
  486. push %eax { save LastError }
  487. push _tls_index
  488. call TlsGetValue
  489. test %eax,%eax
  490. jnz .L2
  491. { This can happen when a thread existed before DLL was loaded,
  492. or if DisableThreadLibraryCalls was called. }
  493. call SysAllocateThreadVars
  494. mov $0x1000000,%eax
  495. call InitThread
  496. push _tls_index
  497. call TlsGetValue
  498. .L2:
  499. add %eax,%ebx
  500. call SetLastError { restore (value is on stack) }
  501. mov %ebx,%eax
  502. pop %ebx
  503. end;
  504. {$endif FPC_SECTION_THREADVARS}
  505. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  506. type
  507. tdosheader = packed record
  508. e_magic : word;
  509. e_cblp : word;
  510. e_cp : word;
  511. e_crlc : word;
  512. e_cparhdr : word;
  513. e_minalloc : word;
  514. e_maxalloc : word;
  515. e_ss : word;
  516. e_sp : word;
  517. e_csum : word;
  518. e_ip : word;
  519. e_cs : word;
  520. e_lfarlc : word;
  521. e_ovno : word;
  522. e_res : array[0..3] of word;
  523. e_oemid : word;
  524. e_oeminfo : word;
  525. e_res2 : array[0..9] of word;
  526. e_lfanew : longint;
  527. end;
  528. tpeheader = packed record
  529. PEMagic : longint;
  530. Machine : word;
  531. NumberOfSections : word;
  532. TimeDateStamp : longint;
  533. PointerToSymbolTable : longint;
  534. NumberOfSymbols : longint;
  535. SizeOfOptionalHeader : word;
  536. Characteristics : word;
  537. Magic : word;
  538. MajorLinkerVersion : byte;
  539. MinorLinkerVersion : byte;
  540. SizeOfCode : longint;
  541. SizeOfInitializedData : longint;
  542. SizeOfUninitializedData : longint;
  543. AddressOfEntryPoint : longint;
  544. BaseOfCode : longint;
  545. BaseOfData : longint;
  546. ImageBase : longint;
  547. SectionAlignment : longint;
  548. FileAlignment : longint;
  549. MajorOperatingSystemVersion : word;
  550. MinorOperatingSystemVersion : word;
  551. MajorImageVersion : word;
  552. MinorImageVersion : word;
  553. MajorSubsystemVersion : word;
  554. MinorSubsystemVersion : word;
  555. Reserved1 : longint;
  556. SizeOfImage : longint;
  557. SizeOfHeaders : longint;
  558. CheckSum : longint;
  559. Subsystem : word;
  560. DllCharacteristics : word;
  561. SizeOfStackReserve : longint;
  562. SizeOfStackCommit : longint;
  563. SizeOfHeapReserve : longint;
  564. SizeOfHeapCommit : longint;
  565. LoaderFlags : longint;
  566. NumberOfRvaAndSizes : longint;
  567. DataDirectory : array[1..$80] of byte;
  568. end;
  569. begin
  570. result:=tpeheader((pointer(getmodulehandle(nil))+(tdosheader(pointer(getmodulehandle(nil))^).e_lfanew))^).SizeOfStackReserve;
  571. end;
  572. begin
  573. { get some helpful informations }
  574. GetStartupInfo(@startupinfo);
  575. { some misc Win32 stuff }
  576. if not IsLibrary then
  577. SysInstance:=getmodulehandle(nil);
  578. MainInstance:=SysInstance;
  579. { pass dummy value }
  580. StackLength := CheckInitialStkLen($1000000);
  581. StackBottom := StackTop - StackLength;
  582. cmdshow:=startupinfo.wshowwindow;
  583. { Setup heap and threading, these may be already initialized from TLS callback }
  584. if not Assigned(CurrentTM.BeginThread) then
  585. begin
  586. InitHeap;
  587. InitSystemThreads;
  588. end;
  589. SysInitExceptions;
  590. { setup fastmove stuff }
  591. fpc_cpucodeinit;
  592. initunicodestringmanager;
  593. InitWin32Widestrings;
  594. SysInitStdIO;
  595. { Arguments }
  596. setup_arguments;
  597. InitSystemDynLibs;
  598. { Reset IO Error }
  599. InOutRes:=0;
  600. ProcessID := GetCurrentProcessID;
  601. DispCallByIDProc:=@DoDispCallByIDError;
  602. end.