system.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. { include system-independent routine headers }
  24. {$I systemh.inc}
  25. const
  26. LineEnding = #13#10;
  27. LFNSupport = true;
  28. DirectorySeparator = '\';
  29. DriveSeparator = ':';
  30. ExtensionSeparator = '.';
  31. PathSeparator = ';';
  32. AllowDirectorySeparators : set of char = ['\','/'];
  33. AllowDriveSeparators : set of char = [':'];
  34. { FileNameCaseSensitive and FileNameCasePreserving are defined separately below!!! }
  35. maxExitCode = 65535;
  36. MaxPathLen = 260;
  37. AllFilesMask = '*';
  38. type
  39. PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  40. TEXCEPTION_FRAME = record
  41. next : PEXCEPTION_FRAME;
  42. handler : pointer;
  43. end;
  44. const
  45. { Default filehandles }
  46. UnusedHandle : THandle = THandle(-1);
  47. StdInputHandle : THandle = 0;
  48. StdOutputHandle : THandle = 0;
  49. StdErrorHandle : THandle = 0;
  50. FileNameCaseSensitive : boolean = false;
  51. FileNameCasePreserving: boolean = true;
  52. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  53. sLineBreak = LineEnding;
  54. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  55. System_exception_frame : PEXCEPTION_FRAME =nil;
  56. type
  57. TStartupInfo=packed record
  58. cb : longint;
  59. lpReserved : Pointer;
  60. lpDesktop : Pointer;
  61. lpTitle : Pointer;
  62. dwX : longint;
  63. dwY : longint;
  64. dwXSize : longint;
  65. dwYSize : longint;
  66. dwXCountChars : longint;
  67. dwYCountChars : longint;
  68. dwFillAttribute : longint;
  69. dwFlags : longint;
  70. wShowWindow : Word;
  71. cbReserved2 : Word;
  72. lpReserved2 : Pointer;
  73. hStdInput : longint;
  74. hStdOutput : longint;
  75. hStdError : longint;
  76. end;
  77. var
  78. { C compatible arguments }
  79. argc : longint; public name 'operatingsystem_parameter_argc';
  80. argv : ppchar; public name 'operatingsystem_parameter_argv';
  81. { Win32 Info }
  82. startupinfo : tstartupinfo;
  83. MainInstance,
  84. cmdshow : longint;
  85. DLLreason : dword; public name 'operatingsystem_dllreason';
  86. DLLparam : PtrInt; public name 'operatingsystem_dllparam';
  87. StartupConsoleMode : DWORD;
  88. const
  89. hprevinst: longint=0;
  90. type
  91. TDLL_Entry_Hook = procedure (dllparam : PtrInt);
  92. const
  93. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  94. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  95. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  96. Const
  97. { it can be discussed whether fmShareDenyNone means read and write or read, write and delete, see
  98. also http://bugs.freepascal.org/view.php?id=8898, this allows users to configure the used
  99. value
  100. }
  101. fmShareDenyNoneFlags : DWord = 3;
  102. implementation
  103. var
  104. SysInstance : Longint;public name '_FPC_SysInstance';
  105. InitFinalTable : record end; external name 'INITFINAL';
  106. ThreadvarTablesTable : record end; external name 'FPC_THREADVARTABLES';
  107. procedure PascalMain;stdcall;external name 'PASCALMAIN';
  108. procedure asm_exit;stdcall;external name 'asm_exit';
  109. const
  110. EntryInformation : TEntryInformation = (
  111. InitFinalTable : @InitFinalTable;
  112. ThreadvarTablesTable : @ThreadvarTablesTable;
  113. asm_exit : @asm_exit;
  114. PascalMain : @PascalMain;
  115. valgrind_used : false;
  116. );
  117. { include system independent routines }
  118. {$I system.inc}
  119. { include code common with win64 }
  120. {$I syswin.inc}
  121. {*****************************************************************************
  122. System Dependent Exit code
  123. *****************************************************************************}
  124. procedure install_exception_handlers;forward;
  125. procedure remove_exception_handlers;forward;
  126. Procedure system_exit;
  127. begin
  128. if IsLibrary then
  129. begin
  130. { If exiting from DLL_PROCESS_ATTACH/DETACH, unwind to DllMain context. }
  131. if DllInitState in [DLL_PROCESS_ATTACH,DLL_PROCESS_DETACH] then
  132. LongJmp(DLLBuf,1)
  133. else
  134. { Abnormal termination, Halt has been called from DLL function,
  135. put down the entire process (DLL_PROCESS_DETACH will still
  136. occur). At this point RTL has been already finalized in InternalExit
  137. and shouldn't be finalized another time in DLL_PROCESS_DETACH.
  138. Indicate this by resetting MainThreadIdWin32. }
  139. MainThreadIDWin32:=0;
  140. end;
  141. if not IsConsole then
  142. begin
  143. Close(stderr);
  144. Close(stdout);
  145. Close(erroutput);
  146. Close(Input);
  147. Close(Output);
  148. { what about Input and Output ?? PM }
  149. { now handled, FPK }
  150. end;
  151. if not IsLibrary then
  152. remove_exception_handlers;
  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. ST : pointer;
  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. install_exception_handlers;
  171. { This strange construction is needed to solve the _SS problem
  172. with a smartlinked syswin32 (PFV) }
  173. asm
  174. { allocate space for an exception frame }
  175. pushl $0
  176. pushl %fs:(0)
  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. movl %esp,%eax
  182. movl %eax,System_exception_frame
  183. pushl %ebp
  184. xorl %eax,%eax
  185. movw %ss,%ax
  186. movl %eax,_SS
  187. xorl %ebp,%ebp
  188. end;
  189. EntryInformation.PascalMain();
  190. asm
  191. popl %ebp
  192. end;
  193. { if we pass here there was no error ! }
  194. system_exit;
  195. end;
  196. function is_prefetch(p : pointer) : boolean;
  197. var
  198. a : array[0..15] of byte;
  199. doagain : boolean;
  200. instrlo,instrhi,opcode : byte;
  201. i : longint;
  202. begin
  203. result:=false;
  204. { read memory savely without causing another exeception }
  205. if not(ReadProcessMemory(GetCurrentProcess,p,@a,sizeof(a),nil)) then
  206. exit;
  207. i:=0;
  208. doagain:=true;
  209. while doagain and (i<15) do
  210. begin
  211. opcode:=a[i];
  212. instrlo:=opcode and $f;
  213. instrhi:=opcode and $f0;
  214. case instrhi of
  215. { prefix? }
  216. $20,$30:
  217. doagain:=(instrlo and 7)=6;
  218. $60:
  219. doagain:=(instrlo and $c)=4;
  220. $f0:
  221. doagain:=instrlo in [0,2,3];
  222. $0:
  223. begin
  224. result:=(instrlo=$f) and (a[i+1] in [$d,$18]);
  225. exit;
  226. end;
  227. else
  228. doagain:=false;
  229. end;
  230. inc(i);
  231. end;
  232. end;
  233. //
  234. // Hardware exception handling
  235. //
  236. {$ifdef Set_i386_Exception_handler}
  237. type
  238. PFloatingSaveArea = ^TFloatingSaveArea;
  239. TFloatingSaveArea = packed record
  240. ControlWord : Cardinal;
  241. StatusWord : Cardinal;
  242. TagWord : Cardinal;
  243. ErrorOffset : Cardinal;
  244. ErrorSelector : Cardinal;
  245. DataOffset : Cardinal;
  246. DataSelector : Cardinal;
  247. RegisterArea : array[0..79] of Byte;
  248. Cr0NpxState : Cardinal;
  249. end;
  250. PContext = ^TContext;
  251. TContext = packed record
  252. //
  253. // The flags values within this flag control the contents of
  254. // a CONTEXT record.
  255. //
  256. ContextFlags : Cardinal;
  257. //
  258. // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  259. // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
  260. // included in CONTEXT_FULL.
  261. //
  262. Dr0, Dr1, Dr2,
  263. Dr3, Dr6, Dr7 : Cardinal;
  264. //
  265. // This section is specified/returned if the
  266. // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  267. //
  268. FloatSave : TFloatingSaveArea;
  269. //
  270. // This section is specified/returned if the
  271. // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  272. //
  273. SegGs, SegFs,
  274. SegEs, SegDs : Cardinal;
  275. //
  276. // This section is specified/returned if the
  277. // ContextFlags word contains the flag CONTEXT_INTEGER.
  278. //
  279. Edi, Esi, Ebx,
  280. Edx, Ecx, Eax : Cardinal;
  281. //
  282. // This section is specified/returned if the
  283. // ContextFlags word contains the flag CONTEXT_CONTROL.
  284. //
  285. Ebp : Cardinal;
  286. Eip : Cardinal;
  287. SegCs : Cardinal;
  288. EFlags, Esp, SegSs : Cardinal;
  289. //
  290. // This section is specified/returned if the ContextFlags word
  291. // contains the flag CONTEXT_EXTENDED_REGISTERS.
  292. // The format and contexts are processor specific
  293. //
  294. ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  295. end;
  296. PExceptionPointers = ^TExceptionPointers;
  297. TExceptionPointers = packed record
  298. ExceptionRecord : PExceptionRecord;
  299. ContextRecord : PContext;
  300. end;
  301. { type of functions that should be used for exception handling }
  302. TTopLevelExceptionFilter = function (excep : PExceptionPointers) : Longint;stdcall;
  303. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : TTopLevelExceptionFilter) : TTopLevelExceptionFilter;
  304. stdcall;external 'kernel32' name 'SetUnhandledExceptionFilter';
  305. const
  306. MaxExceptionLevel = 16;
  307. exceptLevel : Byte = 0;
  308. var
  309. exceptEip : array[0..MaxExceptionLevel-1] of Longint;
  310. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  311. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  312. {$ifdef SYSTEMEXCEPTIONDEBUG}
  313. procedure DebugHandleErrorAddrFrame(error : longint; addr, frame : pointer);
  314. begin
  315. if IsConsole then
  316. begin
  317. write(stderr,'HandleErrorAddrFrame(error=',error);
  318. write(stderr,',addr=',hexstr(ptruint(addr),8));
  319. writeln(stderr,',frame=',hexstr(ptruint(frame),8),')');
  320. end;
  321. HandleErrorAddrFrame(error,addr,frame);
  322. end;
  323. {$endif SYSTEMEXCEPTIONDEBUG}
  324. procedure JumpToHandleErrorFrame;
  325. var
  326. eip, ebp, error : Longint;
  327. begin
  328. // save ebp
  329. asm
  330. movl (%ebp),%eax
  331. movl %eax,ebp
  332. end;
  333. if (exceptLevel > 0) then
  334. dec(exceptLevel);
  335. eip:=exceptEip[exceptLevel];
  336. error:=exceptError[exceptLevel];
  337. {$ifdef SYSTEMEXCEPTIONDEBUG}
  338. if IsConsole then
  339. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  340. {$endif SYSTEMEXCEPTIONDEBUG}
  341. if resetFPU[exceptLevel] then
  342. SysResetFPU;
  343. { build a fake stack }
  344. asm
  345. movl ebp,%ecx
  346. movl eip,%edx
  347. movl error,%eax
  348. pushl eip
  349. movl ebp,%ebp // Change frame pointer
  350. {$ifdef SYSTEMEXCEPTIONDEBUG}
  351. jmpl DebugHandleErrorAddrFrame
  352. {$else not SYSTEMEXCEPTIONDEBUG}
  353. jmpl HandleErrorAddrFrame
  354. {$endif SYSTEMEXCEPTIONDEBUG}
  355. end;
  356. end;
  357. function syswin32_i386_exception_handler(excep : PExceptionPointers) : Longint;stdcall;
  358. var
  359. res: longint;
  360. err: byte;
  361. must_reset_fpu: boolean;
  362. begin
  363. res := EXCEPTION_CONTINUE_SEARCH;
  364. if excep^.ContextRecord^.SegSs=_SS then begin
  365. err := 0;
  366. must_reset_fpu := true;
  367. {$ifdef SYSTEMEXCEPTIONDEBUG}
  368. if IsConsole then Writeln(stderr,'Exception ',
  369. hexstr(excep^.ExceptionRecord^.ExceptionCode, 8));
  370. {$endif SYSTEMEXCEPTIONDEBUG}
  371. case excep^.ExceptionRecord^.ExceptionCode of
  372. STATUS_INTEGER_DIVIDE_BY_ZERO,
  373. STATUS_FLOAT_DIVIDE_BY_ZERO :
  374. err := 200;
  375. STATUS_ARRAY_BOUNDS_EXCEEDED :
  376. begin
  377. err := 201;
  378. must_reset_fpu := false;
  379. end;
  380. STATUS_STACK_OVERFLOW :
  381. begin
  382. err := 202;
  383. must_reset_fpu := false;
  384. end;
  385. STATUS_FLOAT_OVERFLOW :
  386. err := 205;
  387. STATUS_FLOAT_DENORMAL_OPERAND,
  388. STATUS_FLOAT_UNDERFLOW :
  389. err := 206;
  390. {excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  391. STATUS_FLOAT_INEXACT_RESULT,
  392. STATUS_FLOAT_INVALID_OPERATION,
  393. STATUS_FLOAT_STACK_CHECK :
  394. err := 207;
  395. STATUS_INTEGER_OVERFLOW :
  396. begin
  397. err := 215;
  398. must_reset_fpu := false;
  399. end;
  400. STATUS_ILLEGAL_INSTRUCTION:
  401. { if we're testing sse support, simply set the flag and continue }
  402. if sse_check then
  403. begin
  404. os_supports_sse:=false;
  405. { skip the offending movaps %xmm7, %xmm6 instruction }
  406. inc(excep^.ContextRecord^.Eip,3);
  407. excep^.ExceptionRecord^.ExceptionCode := 0;
  408. res:=EXCEPTION_CONTINUE_EXECUTION;
  409. end
  410. else
  411. err := 216;
  412. STATUS_ACCESS_VIOLATION:
  413. { Athlon prefetch bug? }
  414. if is_prefetch(pointer(excep^.ContextRecord^.Eip)) then
  415. begin
  416. { if yes, then retry }
  417. excep^.ExceptionRecord^.ExceptionCode := 0;
  418. res:=EXCEPTION_CONTINUE_EXECUTION;
  419. end
  420. else
  421. err := 216;
  422. STATUS_CONTROL_C_EXIT:
  423. err := 217;
  424. STATUS_PRIVILEGED_INSTRUCTION:
  425. begin
  426. err := 218;
  427. must_reset_fpu := false;
  428. end;
  429. else
  430. begin
  431. if ((excep^.ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  432. err := 217
  433. else
  434. err := 255;
  435. end;
  436. end;
  437. if (err <> 0) and (exceptLevel < MaxExceptionLevel) then begin
  438. exceptEip[exceptLevel] := excep^.ContextRecord^.Eip;
  439. exceptError[exceptLevel] := err;
  440. resetFPU[exceptLevel] := must_reset_fpu;
  441. inc(exceptLevel);
  442. excep^.ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  443. excep^.ExceptionRecord^.ExceptionCode := 0;
  444. res := EXCEPTION_CONTINUE_EXECUTION;
  445. {$ifdef SYSTEMEXCEPTIONDEBUG}
  446. if IsConsole then begin
  447. writeln(stderr,'Exception Continue Exception set at ',
  448. hexstr(exceptEip[exceptLevel],8));
  449. writeln(stderr,'Eip changed to ',
  450. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', err);
  451. end;
  452. {$endif SYSTEMEXCEPTIONDEBUG}
  453. end;
  454. end;
  455. syswin32_i386_exception_handler := res;
  456. end;
  457. procedure install_exception_handlers;
  458. {$ifdef SYSTEMEXCEPTIONDEBUG}
  459. var
  460. oldexceptaddr,
  461. newexceptaddr : Longint;
  462. {$endif SYSTEMEXCEPTIONDEBUG}
  463. begin
  464. {$ifdef SYSTEMEXCEPTIONDEBUG}
  465. asm
  466. movl $0,%eax
  467. movl %fs:(%eax),%eax
  468. movl %eax,oldexceptaddr
  469. end;
  470. {$endif SYSTEMEXCEPTIONDEBUG}
  471. SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  472. {$ifdef SYSTEMEXCEPTIONDEBUG}
  473. asm
  474. movl $0,%eax
  475. movl %fs:(%eax),%eax
  476. movl %eax,newexceptaddr
  477. end;
  478. if IsConsole then
  479. writeln(stderr,'Old exception ',hexstr(oldexceptaddr,8),
  480. ' new exception ',hexstr(newexceptaddr,8));
  481. {$endif SYSTEMEXCEPTIONDEBUG}
  482. end;
  483. procedure remove_exception_handlers;
  484. begin
  485. SetUnhandledExceptionFilter(nil);
  486. end;
  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. var
  597. st : Pointer;
  598. begin
  599. { get some helpful informations }
  600. GetStartupInfo(@startupinfo);
  601. { some misc Win32 stuff }
  602. if not IsLibrary then
  603. SysInstance:=getmodulehandle(nil);
  604. MainInstance:=SysInstance;
  605. asm
  606. movl %fs:(4),%eax
  607. movl %eax,st
  608. end;
  609. StackTop:=st;
  610. { pass dummy value }
  611. StackLength := CheckInitialStkLen($1000000);
  612. StackBottom := StackTop - StackLength;
  613. cmdshow:=startupinfo.wshowwindow;
  614. { Setup heap and threading, these may be already initialized from TLS callback }
  615. if not Assigned(CurrentTM.BeginThread) then
  616. begin
  617. InitHeap;
  618. InitSystemThreads;
  619. end;
  620. SysInitExceptions;
  621. { setup fastmove stuff }
  622. fpc_cpucodeinit;
  623. initwidestringmanager;
  624. initunicodestringmanager;
  625. InitWin32Widestrings;
  626. SysInitStdIO;
  627. { Arguments }
  628. setup_arguments;
  629. { Reset IO Error }
  630. InOutRes:=0;
  631. ProcessID := GetCurrentProcessID;
  632. { Reset internal error variable }
  633. errno:=0;
  634. initvariantmanager;
  635. DispCallByIDProc:=@DoDispCallByIDError;
  636. end.