system.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. {$ifdef VER3_0}
  18. { 3.1.1+ do not require this anymore }
  19. {$define FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  20. {$endif VER3_0}
  21. {$ifdef cpui386}
  22. {$define Set_i386_Exception_handler}
  23. {$endif cpui386}
  24. {$define DISABLE_NO_THREAD_MANAGER}
  25. {$define HAS_WIDESTRINGMANAGER}
  26. {$define DISABLE_NO_DYNLIBS_MANAGER}
  27. {$define FPC_SYSTEM_HAS_SYSDLH}
  28. {$define FPC_HAS_SETCTRLBREAKHANDLER}
  29. {$ifdef FPC_USE_WIN32_SEH}
  30. {$define FPC_SYSTEM_HAS_RAISEEXCEPTION}
  31. {$define FPC_SYSTEM_HAS_RERAISE}
  32. {$define FPC_SYSTEM_HAS_DONEEXCEPTION}
  33. {$define FPC_SYSTEM_HAS_SAFECALLHANDLER}
  34. {$endif FPC_USE_WIN32_SEH}
  35. { include system-independent routine headers }
  36. {$I systemh.inc}
  37. const
  38. LineEnding = #13#10;
  39. LFNSupport = true;
  40. DirectorySeparator = '\';
  41. DriveSeparator = ':';
  42. ExtensionSeparator = '.';
  43. PathSeparator = ';';
  44. AllowDirectorySeparators : set of char = ['\','/'];
  45. AllowDriveSeparators : set of char = [':'];
  46. { FileNameCaseSensitive and FileNameCasePreserving are defined separately below!!! }
  47. maxExitCode = 65535;
  48. MaxPathLen = 260;
  49. AllFilesMask = '*';
  50. type
  51. PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  52. TEXCEPTION_FRAME = record
  53. next : PEXCEPTION_FRAME;
  54. handler : pointer;
  55. end;
  56. const
  57. { Default filehandles }
  58. UnusedHandle : THandle = THandle(-1);
  59. StdInputHandle : THandle = 0;
  60. StdOutputHandle : THandle = 0;
  61. StdErrorHandle : THandle = 0;
  62. FileNameCaseSensitive : boolean = false;
  63. FileNameCasePreserving: boolean = true;
  64. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  65. sLineBreak = LineEnding;
  66. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  67. System_exception_frame : PEXCEPTION_FRAME =nil;
  68. var
  69. { C compatible arguments }
  70. argc : longint; public name 'operatingsystem_parameter_argc';
  71. argv : ppchar; public name 'operatingsystem_parameter_argv';
  72. { Win32 Info }
  73. startupinfo : tstartupinfo deprecated; // Delphi does not have one in interface
  74. MainInstance,
  75. cmdshow : longint;
  76. DLLreason : dword; public name 'operatingsystem_dllreason';
  77. DLLparam : PtrInt; public name 'operatingsystem_dllparam';
  78. StartupConsoleMode : DWORD;
  79. const
  80. hprevinst: longint=0;
  81. type
  82. TDLL_Entry_Hook = procedure (dllparam : PtrInt);
  83. const
  84. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  85. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  86. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  87. Const
  88. { it can be discussed whether fmShareDenyNone means read and write or read, write and delete, see
  89. also http://bugs.freepascal.org/view.php?id=8898, this allows users to configure the used
  90. value
  91. }
  92. fmShareDenyNoneFlags : DWord = 3;
  93. implementation
  94. var
  95. FPCSysInstance : PLongint;public name '_FPC_SysInstance';
  96. {$define FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
  97. procedure OsSetupEntryInformation(constref info: TEntryInformation); forward;
  98. {$ifdef FPC_USE_WIN32_SEH}
  99. function main_wrapper(arg: Pointer; proc: Pointer): ptrint; forward;
  100. procedure OutermostHandler; external name '__FPC_DEFAULT_HANDLER';
  101. {$endif FPC_USE_WIN32_SEH}
  102. {$define FPC_SYSTEM_HAS_STACKTOP}
  103. function StackTop: pointer; assembler;nostackframe;
  104. asm
  105. movl %fs:(4),%eax
  106. end;
  107. { include system independent routines }
  108. {$I system.inc}
  109. { include code common with win64 }
  110. {$I syswin.inc}
  111. procedure OsSetupEntryInformation(constref info: TEntryInformation);
  112. begin
  113. TlsKey := info.OS.TlsKeyAddr;
  114. FPCSysInstance := info.OS.SysInstance;
  115. WStrInitTablesTable := info.OS.WideInitTables;
  116. end;
  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.OS.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(constref info : TEntryInformation);[public,alias:'_FPC_EXE_Entry'];
  163. var
  164. xframe: TEXCEPTION_FRAME;
  165. begin
  166. SetupEntryInformation(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,ssecode: 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. STATUS_FLOAT_MULTIPLE_FAULTS,
  439. STATUS_FLOAT_MULTIPLE_TRAPS:
  440. begin
  441. { dumping ExtendedRegisters and comparing with the actually value of mxcsr revealed 24 }
  442. TranslateMxcsr(excep^.ContextRecord^.ExtendedRegisters[24],ssecode);
  443. {$ifdef SYSTEMEXCEPTIONDEBUG}
  444. if IsConsole then
  445. Writeln(stderr,'MXSR: ',hexstr(excep^.ContextRecord^.ExtendedRegisters[24], 2),' SSECODE: ',ssecode);
  446. {$endif SYSTEMEXCEPTIONDEBUG}
  447. err:=-ssecode;
  448. end;
  449. else
  450. begin
  451. if ((excep^.ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  452. err := 217
  453. else
  454. err := 255;
  455. end;
  456. end;
  457. if (err <> 0) and (exceptLevel < MaxExceptionLevel) then begin
  458. exceptEip[exceptLevel] := excep^.ContextRecord^.Eip;
  459. exceptError[exceptLevel] := err;
  460. resetFPU[exceptLevel] := must_reset_fpu;
  461. inc(exceptLevel);
  462. excep^.ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  463. excep^.ExceptionRecord^.ExceptionCode := 0;
  464. res := EXCEPTION_CONTINUE_EXECUTION;
  465. {$ifdef SYSTEMEXCEPTIONDEBUG}
  466. if IsConsole then begin
  467. writeln(stderr,'Exception Continue Exception set at ',
  468. hexstr(exceptEip[exceptLevel],8));
  469. writeln(stderr,'Eip changed to ',
  470. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', err);
  471. end;
  472. {$endif SYSTEMEXCEPTIONDEBUG}
  473. end;
  474. end;
  475. syswin32_i386_exception_handler := res;
  476. end;
  477. procedure install_exception_handlers;
  478. begin
  479. SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  480. end;
  481. procedure remove_exception_handlers;
  482. begin
  483. SetUnhandledExceptionFilter(nil);
  484. end;
  485. {$endif not FPC_USE_WIN32_SEH}
  486. {$else not cpui386 (Processor specific !!)}
  487. procedure install_exception_handlers;
  488. begin
  489. end;
  490. procedure remove_exception_handlers;
  491. begin
  492. end;
  493. {$endif Set_i386_Exception_handler}
  494. {$ifdef FPC_SECTION_THREADVARS}
  495. function fpc_tls_add(addr: pointer): pointer; assembler; nostackframe;
  496. [public,alias: 'FPC_TLS_ADD']; compilerproc;
  497. asm
  498. sub $tls_data_start,%eax
  499. cmpb $0,IsLibrary
  500. mov _tls_index,%ecx
  501. jnz .L1
  502. mov %fs:(0x2c),%edx
  503. add (%edx,%ecx,4),%eax
  504. ret
  505. .L1:
  506. push %ebx
  507. mov %eax,%ebx
  508. call GetLastError
  509. push %eax { save LastError }
  510. push _tls_index
  511. call TlsGetValue
  512. test %eax,%eax
  513. jnz .L2
  514. { This can happen when a thread existed before DLL was loaded,
  515. or if DisableThreadLibraryCalls was called. }
  516. call SysAllocateThreadVars
  517. mov $0x1000000,%eax
  518. call InitThread
  519. push _tls_index
  520. call TlsGetValue
  521. .L2:
  522. add %eax,%ebx
  523. call SetLastError { restore (value is on stack) }
  524. mov %ebx,%eax
  525. pop %ebx
  526. end;
  527. {$endif FPC_SECTION_THREADVARS}
  528. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  529. type
  530. tdosheader = packed record
  531. e_magic : word;
  532. e_cblp : word;
  533. e_cp : word;
  534. e_crlc : word;
  535. e_cparhdr : word;
  536. e_minalloc : word;
  537. e_maxalloc : word;
  538. e_ss : word;
  539. e_sp : word;
  540. e_csum : word;
  541. e_ip : word;
  542. e_cs : word;
  543. e_lfarlc : word;
  544. e_ovno : word;
  545. e_res : array[0..3] of word;
  546. e_oemid : word;
  547. e_oeminfo : word;
  548. e_res2 : array[0..9] of word;
  549. e_lfanew : longint;
  550. end;
  551. tpeheader = packed record
  552. PEMagic : longint;
  553. Machine : word;
  554. NumberOfSections : word;
  555. TimeDateStamp : longint;
  556. PointerToSymbolTable : longint;
  557. NumberOfSymbols : longint;
  558. SizeOfOptionalHeader : word;
  559. Characteristics : word;
  560. Magic : word;
  561. MajorLinkerVersion : byte;
  562. MinorLinkerVersion : byte;
  563. SizeOfCode : longint;
  564. SizeOfInitializedData : longint;
  565. SizeOfUninitializedData : longint;
  566. AddressOfEntryPoint : longint;
  567. BaseOfCode : longint;
  568. BaseOfData : longint;
  569. ImageBase : longint;
  570. SectionAlignment : longint;
  571. FileAlignment : longint;
  572. MajorOperatingSystemVersion : word;
  573. MinorOperatingSystemVersion : word;
  574. MajorImageVersion : word;
  575. MinorImageVersion : word;
  576. MajorSubsystemVersion : word;
  577. MinorSubsystemVersion : word;
  578. Reserved1 : longint;
  579. SizeOfImage : longint;
  580. SizeOfHeaders : longint;
  581. CheckSum : longint;
  582. Subsystem : word;
  583. DllCharacteristics : word;
  584. SizeOfStackReserve : longint;
  585. SizeOfStackCommit : longint;
  586. SizeOfHeapReserve : longint;
  587. SizeOfHeapCommit : longint;
  588. LoaderFlags : longint;
  589. NumberOfRvaAndSizes : longint;
  590. DataDirectory : array[1..$80] of byte;
  591. end;
  592. begin
  593. result:=tpeheader((pointer(getmodulehandle(nil))+(tdosheader(pointer(getmodulehandle(nil))^).e_lfanew))^).SizeOfStackReserve;
  594. end;
  595. begin
  596. { get some helpful informations }
  597. GetStartupInfo(@startupinfo);
  598. { some misc Win32 stuff }
  599. if not IsLibrary then
  600. FPCSysInstance^:=getmodulehandle(nil);
  601. MainInstance:=FPCSysInstance^;
  602. { pass dummy value }
  603. StackLength := CheckInitialStkLen($1000000);
  604. StackBottom := StackTop - StackLength;
  605. cmdshow:=startupinfo.wshowwindow;
  606. { Setup heap and threading, these may be already initialized from TLS callback }
  607. if not Assigned(CurrentTM.BeginThread) then
  608. begin
  609. InitHeap;
  610. InitSystemThreads;
  611. end;
  612. SysInitExceptions;
  613. { setup fastmove stuff }
  614. fpc_cpucodeinit;
  615. initunicodestringmanager;
  616. InitWin32Widestrings;
  617. SysInitStdIO;
  618. { Arguments }
  619. setup_arguments;
  620. InitSystemDynLibs;
  621. { Reset IO Error }
  622. InOutRes:=0;
  623. ProcessID := GetCurrentProcessID;
  624. DispCallByIDProc:=@DoDispCallByIDError;
  625. end.