system.pp 19 KB

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