system.pp 19 KB

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