system.pp 20 KB

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