system.pp 20 KB

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