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