system.pp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. { include system-independent routine headers }
  23. {$I systemh.inc}
  24. const
  25. LineEnding = #13#10;
  26. LFNSupport = true;
  27. DirectorySeparator = '\';
  28. DriveSeparator = ':';
  29. ExtensionSeparator = '.';
  30. PathSeparator = ';';
  31. AllowDirectorySeparators : set of char = ['\','/'];
  32. AllowDriveSeparators : set of char = [':'];
  33. { FileNameCaseSensitive is defined separately below!!! }
  34. maxExitCode = 65535;
  35. MaxPathLen = 260;
  36. AllFilesMask = '*';
  37. type
  38. PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  39. TEXCEPTION_FRAME = record
  40. next : PEXCEPTION_FRAME;
  41. handler : pointer;
  42. end;
  43. const
  44. { Default filehandles }
  45. UnusedHandle : THandle = THandle(-1);
  46. StdInputHandle : THandle = 0;
  47. StdOutputHandle : THandle = 0;
  48. StdErrorHandle : THandle = 0;
  49. FileNameCaseSensitive : boolean = true;
  50. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  51. sLineBreak = LineEnding;
  52. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  53. System_exception_frame : PEXCEPTION_FRAME =nil;
  54. type
  55. TStartupInfo=packed record
  56. cb : longint;
  57. lpReserved : Pointer;
  58. lpDesktop : Pointer;
  59. lpTitle : Pointer;
  60. dwX : longint;
  61. dwY : longint;
  62. dwXSize : longint;
  63. dwYSize : longint;
  64. dwXCountChars : longint;
  65. dwYCountChars : longint;
  66. dwFillAttribute : longint;
  67. dwFlags : longint;
  68. wShowWindow : Word;
  69. cbReserved2 : Word;
  70. lpReserved2 : Pointer;
  71. hStdInput : longint;
  72. hStdOutput : longint;
  73. hStdError : longint;
  74. end;
  75. var
  76. { C compatible arguments }
  77. argc : longint;
  78. argv : ppchar;
  79. { Win32 Info }
  80. startupinfo : tstartupinfo;
  81. hprevinst,
  82. MainInstance,
  83. cmdshow : longint;
  84. DLLreason,DLLparam:longint;
  85. StartupConsoleMode : DWORD;
  86. type
  87. TDLL_Entry_Hook = procedure (dllparam : longint);
  88. const
  89. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  90. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  91. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  92. Const
  93. { it can be discussed whether fmShareDenyNone means read and write or read, write and delete, see
  94. also http://bugs.freepascal.org/view.php?id=8898, this allows users to configure the used
  95. value
  96. }
  97. fmShareDenyNoneFlags : DWord = 3;
  98. implementation
  99. var
  100. EntryInformation : TEntryInformation;
  101. SysInstance : Longint;public name '_FPC_SysInstance';
  102. { used by wstrings.inc because wstrings.inc is included before sysos.inc
  103. this is put here (FK) }
  104. function SysAllocStringLen(psz:pointer;len:dword):pointer;stdcall;
  105. external 'oleaut32.dll' name 'SysAllocStringLen';
  106. procedure SysFreeString(bstr:pointer);stdcall;
  107. external 'oleaut32.dll' name 'SysFreeString';
  108. function SysReAllocStringLen(var bstr:pointer;psz: pointer;
  109. len:dword): Integer; stdcall;external 'oleaut32.dll' name 'SysReAllocStringLen';
  110. { include system independent routines }
  111. {$I system.inc}
  112. {*****************************************************************************
  113. Parameter Handling
  114. *****************************************************************************}
  115. procedure setup_arguments;
  116. var
  117. arglen,
  118. count : longint;
  119. argstart,
  120. pc,arg : pchar;
  121. quote : Boolean;
  122. argvlen : longint;
  123. buf: array[0..259] of char; // need MAX_PATH bytes, not 256!
  124. procedure allocarg(idx,len:longint);
  125. var
  126. oldargvlen : longint;
  127. begin
  128. if idx>=argvlen then
  129. begin
  130. oldargvlen:=argvlen;
  131. argvlen:=(idx+8) and (not 7);
  132. sysreallocmem(argv,argvlen*sizeof(pointer));
  133. fillchar(argv[oldargvlen],(argvlen-oldargvlen)*sizeof(pointer),0);
  134. end;
  135. { use realloc to reuse already existing memory }
  136. { always allocate, even if length is zero, since }
  137. { the arg. is still present! }
  138. sysreallocmem(argv[idx],len+1);
  139. end;
  140. begin
  141. { create commandline, it starts with the executed filename which is argv[0] }
  142. { Win32 passes the command NOT via the args, but via getmodulefilename}
  143. count:=0;
  144. argv:=nil;
  145. argvlen:=0;
  146. ArgLen := GetModuleFileName(0, @buf[0], sizeof(buf));
  147. buf[ArgLen] := #0; // be safe
  148. allocarg(0,arglen);
  149. move(buf,argv[0]^,arglen+1);
  150. { Setup cmdline variable }
  151. cmdline:=GetCommandLine;
  152. { process arguments }
  153. pc:=cmdline;
  154. {$IfDef SYSTEM_DEBUG_STARTUP}
  155. Writeln(stderr,'Win32 GetCommandLine is #',pc,'#');
  156. {$EndIf }
  157. while pc^<>#0 do
  158. begin
  159. { skip leading spaces }
  160. while pc^ in [#1..#32] do
  161. inc(pc);
  162. if pc^=#0 then
  163. break;
  164. { calc argument length }
  165. quote:=False;
  166. argstart:=pc;
  167. arglen:=0;
  168. while (pc^<>#0) do
  169. begin
  170. case pc^ of
  171. #1..#32 :
  172. begin
  173. if quote then
  174. inc(arglen)
  175. else
  176. break;
  177. end;
  178. '"' :
  179. if pc[1]<>'"' then
  180. quote := not quote
  181. else
  182. inc(pc);
  183. else
  184. inc(arglen);
  185. end;
  186. inc(pc);
  187. end;
  188. { copy argument }
  189. { Don't copy the first one, it is already there.}
  190. If Count<>0 then
  191. begin
  192. allocarg(count,arglen);
  193. quote:=False;
  194. pc:=argstart;
  195. arg:=argv[count];
  196. while (pc^<>#0) do
  197. begin
  198. case pc^ of
  199. #1..#32 :
  200. begin
  201. if quote then
  202. begin
  203. arg^:=pc^;
  204. inc(arg);
  205. end
  206. else
  207. break;
  208. end;
  209. '"' :
  210. if pc[1]<>'"' then
  211. quote := not quote
  212. else
  213. inc(pc);
  214. else
  215. begin
  216. arg^:=pc^;
  217. inc(arg);
  218. end;
  219. end;
  220. inc(pc);
  221. end;
  222. arg^:=#0;
  223. end;
  224. {$IfDef SYSTEM_DEBUG_STARTUP}
  225. Writeln(stderr,'dos arg ',count,' #',arglen,'#',argv[count],'#');
  226. {$EndIf SYSTEM_DEBUG_STARTUP}
  227. inc(count);
  228. end;
  229. { get argc }
  230. argc:=count;
  231. { free unused memory, leaving a nil entry at the end }
  232. sysreallocmem(argv,(count+1)*sizeof(pointer));
  233. argv[count] := nil;
  234. end;
  235. function paramcount : longint;
  236. begin
  237. paramcount := argc - 1;
  238. end;
  239. function paramstr(l : longint) : string;
  240. begin
  241. if (l>=0) and (l<argc) then
  242. paramstr:=strpas(argv[l])
  243. else
  244. paramstr:='';
  245. end;
  246. procedure randomize;
  247. begin
  248. randseed:=GetTickCount;
  249. end;
  250. {*****************************************************************************
  251. System Dependent Exit code
  252. *****************************************************************************}
  253. procedure install_exception_handlers;forward;
  254. procedure remove_exception_handlers;forward;
  255. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  256. procedure PascalMain;stdcall;external name 'PASCALMAIN';
  257. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  258. procedure fpc_do_exit;stdcall;external name 'FPC_DO_EXIT';
  259. Procedure ExitDLL(Exitcode : longint); forward;
  260. procedure asm_exit;stdcall;external name 'asm_exit';
  261. Procedure system_exit;
  262. begin
  263. { don't call ExitProcess inside
  264. the DLL exit code !!
  265. This crashes Win95 at least PM }
  266. if IsLibrary then
  267. ExitDLL(ExitCode);
  268. FiniSystemThreads;
  269. if not IsConsole then
  270. begin
  271. Close(stderr);
  272. Close(stdout);
  273. Close(erroutput);
  274. Close(Input);
  275. Close(Output);
  276. { what about Input and Output ?? PM }
  277. { now handled, FPK }
  278. end;
  279. remove_exception_handlers;
  280. { in 2.0 asm_exit does an exitprocess }
  281. {$ifndef ver2_0}
  282. { do cleanup required by the startup code }
  283. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  284. EntryInformation.asm_exit();
  285. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  286. asm_exit;
  287. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  288. {$endif ver2_0}
  289. { call exitprocess, with cleanup as required }
  290. ExitProcess(exitcode);
  291. end;
  292. var
  293. { value of the stack segment
  294. to check if the call stack can be written on exceptions }
  295. _SS : Cardinal;
  296. procedure Exe_entry(const info : TEntryInformation);[public,alias:'_FPC_EXE_Entry'];
  297. var
  298. ST : pointer;
  299. begin
  300. EntryInformation:=info;
  301. IsLibrary:=false;
  302. { install the handlers for exe only ?
  303. or should we install them for DLL also ? (PM) }
  304. install_exception_handlers;
  305. { This strange construction is needed to solve the _SS problem
  306. with a smartlinked syswin32 (PFV) }
  307. asm
  308. { allocate space for an exception frame }
  309. pushl $0
  310. pushl %fs:(0)
  311. { movl %esp,%fs:(0)
  312. but don't insert it as it doesn't
  313. point to anything yet
  314. this will be used in signals unit }
  315. movl %esp,%eax
  316. movl %eax,System_exception_frame
  317. pushl %ebp
  318. movl %esp,%eax
  319. movl %eax,st
  320. end;
  321. StackTop:=st;
  322. asm
  323. xorl %eax,%eax
  324. movw %ss,%ax
  325. movl %eax,_SS
  326. xorl %ebp,%ebp
  327. end;
  328. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  329. EntryInformation.PascalMain();
  330. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  331. PascalMain;
  332. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  333. asm
  334. popl %ebp
  335. end;
  336. { if we pass here there was no error ! }
  337. system_exit;
  338. end;
  339. function ReadProcessMemory(process : dword;address : pointer;dest : pointer;size : dword;bytesread : pdword) : longbool;
  340. stdcall;external 'kernel32' name 'ReadProcessMemory';
  341. function is_prefetch(p : pointer) : boolean;
  342. var
  343. a : array[0..15] of byte;
  344. doagain : boolean;
  345. instrlo,instrhi,opcode : byte;
  346. i : longint;
  347. begin
  348. result:=false;
  349. { read memory savely without causing another exeception }
  350. if not(ReadProcessMemory(GetCurrentProcess,p,@a,sizeof(a),nil)) then
  351. exit;
  352. i:=0;
  353. doagain:=true;
  354. while doagain and (i<15) do
  355. begin
  356. opcode:=a[i];
  357. instrlo:=opcode and $f;
  358. instrhi:=opcode and $f0;
  359. case instrhi of
  360. { prefix? }
  361. $20,$30:
  362. doagain:=(instrlo and 7)=6;
  363. $60:
  364. doagain:=(instrlo and $c)=4;
  365. $f0:
  366. doagain:=instrlo in [0,2,3];
  367. $0:
  368. begin
  369. result:=(instrlo=$f) and (a[i+1] in [$d,$18]);
  370. exit;
  371. end;
  372. else
  373. doagain:=false;
  374. end;
  375. inc(i);
  376. end;
  377. end;
  378. //
  379. // Hardware exception handling
  380. //
  381. {$ifdef Set_i386_Exception_handler}
  382. {
  383. Error code definitions for the Win32 API functions
  384. Values are 32 bit values layed out as follows:
  385. 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  386. 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  387. +---+-+-+-----------------------+-------------------------------+
  388. |Sev|C|R| Facility | Code |
  389. +---+-+-+-----------------------+-------------------------------+
  390. where
  391. Sev - is the severity code
  392. 00 - Success
  393. 01 - Informational
  394. 10 - Warning
  395. 11 - Error
  396. C - is the Customer code flag
  397. R - is a reserved bit
  398. Facility - is the facility code
  399. Code - is the facility's status code
  400. }
  401. const
  402. SEVERITY_SUCCESS = $00000000;
  403. SEVERITY_INFORMATIONAL = $40000000;
  404. SEVERITY_WARNING = $80000000;
  405. SEVERITY_ERROR = $C0000000;
  406. const
  407. STATUS_SEGMENT_NOTIFICATION = $40000005;
  408. DBG_TERMINATE_THREAD = $40010003;
  409. DBG_TERMINATE_PROCESS = $40010004;
  410. DBG_CONTROL_C = $40010005;
  411. DBG_CONTROL_BREAK = $40010008;
  412. STATUS_GUARD_PAGE_VIOLATION = $80000001;
  413. STATUS_DATATYPE_MISALIGNMENT = $80000002;
  414. STATUS_BREAKPOINT = $80000003;
  415. STATUS_SINGLE_STEP = $80000004;
  416. DBG_EXCEPTION_NOT_HANDLED = $80010001;
  417. STATUS_ACCESS_VIOLATION = $C0000005;
  418. STATUS_IN_PAGE_ERROR = $C0000006;
  419. STATUS_INVALID_HANDLE = $C0000008;
  420. STATUS_NO_MEMORY = $C0000017;
  421. STATUS_ILLEGAL_INSTRUCTION = $C000001D;
  422. STATUS_NONCONTINUABLE_EXCEPTION = $C0000025;
  423. STATUS_INVALID_DISPOSITION = $C0000026;
  424. STATUS_ARRAY_BOUNDS_EXCEEDED = $C000008C;
  425. STATUS_FLOAT_DENORMAL_OPERAND = $C000008D;
  426. STATUS_FLOAT_DIVIDE_BY_ZERO = $C000008E;
  427. STATUS_FLOAT_INEXACT_RESULT = $C000008F;
  428. STATUS_FLOAT_INVALID_OPERATION = $C0000090;
  429. STATUS_FLOAT_OVERFLOW = $C0000091;
  430. STATUS_FLOAT_STACK_CHECK = $C0000092;
  431. STATUS_FLOAT_UNDERFLOW = $C0000093;
  432. STATUS_INTEGER_DIVIDE_BY_ZERO = $C0000094;
  433. STATUS_INTEGER_OVERFLOW = $C0000095;
  434. STATUS_PRIVILEGED_INSTRUCTION = $C0000096;
  435. STATUS_STACK_OVERFLOW = $C00000FD;
  436. STATUS_CONTROL_C_EXIT = $C000013A;
  437. STATUS_FLOAT_MULTIPLE_FAULTS = $C00002B4;
  438. STATUS_FLOAT_MULTIPLE_TRAPS = $C00002B5;
  439. STATUS_REG_NAT_CONSUMPTION = $C00002C9;
  440. EXCEPTION_EXECUTE_HANDLER = 1;
  441. EXCEPTION_CONTINUE_EXECUTION = -1;
  442. EXCEPTION_CONTINUE_SEARCH = 0;
  443. EXCEPTION_MAXIMUM_PARAMETERS = 15;
  444. CONTEXT_X86 = $00010000;
  445. CONTEXT_CONTROL = CONTEXT_X86 or $00000001;
  446. CONTEXT_INTEGER = CONTEXT_X86 or $00000002;
  447. CONTEXT_SEGMENTS = CONTEXT_X86 or $00000004;
  448. CONTEXT_FLOATING_POINT = CONTEXT_X86 or $00000008;
  449. CONTEXT_DEBUG_REGISTERS = CONTEXT_X86 or $00000010;
  450. CONTEXT_EXTENDED_REGISTERS = CONTEXT_X86 or $00000020;
  451. CONTEXT_FULL = CONTEXT_CONTROL or CONTEXT_INTEGER or CONTEXT_SEGMENTS;
  452. MAXIMUM_SUPPORTED_EXTENSION = 512;
  453. type
  454. PFloatingSaveArea = ^TFloatingSaveArea;
  455. TFloatingSaveArea = packed record
  456. ControlWord : Cardinal;
  457. StatusWord : Cardinal;
  458. TagWord : Cardinal;
  459. ErrorOffset : Cardinal;
  460. ErrorSelector : Cardinal;
  461. DataOffset : Cardinal;
  462. DataSelector : Cardinal;
  463. RegisterArea : array[0..79] of Byte;
  464. Cr0NpxState : Cardinal;
  465. end;
  466. PContext = ^TContext;
  467. TContext = packed record
  468. //
  469. // The flags values within this flag control the contents of
  470. // a CONTEXT record.
  471. //
  472. ContextFlags : Cardinal;
  473. //
  474. // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  475. // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
  476. // included in CONTEXT_FULL.
  477. //
  478. Dr0, Dr1, Dr2,
  479. Dr3, Dr6, Dr7 : Cardinal;
  480. //
  481. // This section is specified/returned if the
  482. // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  483. //
  484. FloatSave : TFloatingSaveArea;
  485. //
  486. // This section is specified/returned if the
  487. // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  488. //
  489. SegGs, SegFs,
  490. SegEs, SegDs : Cardinal;
  491. //
  492. // This section is specified/returned if the
  493. // ContextFlags word contains the flag CONTEXT_INTEGER.
  494. //
  495. Edi, Esi, Ebx,
  496. Edx, Ecx, Eax : Cardinal;
  497. //
  498. // This section is specified/returned if the
  499. // ContextFlags word contains the flag CONTEXT_CONTROL.
  500. //
  501. Ebp : Cardinal;
  502. Eip : Cardinal;
  503. SegCs : Cardinal;
  504. EFlags, Esp, SegSs : Cardinal;
  505. //
  506. // This section is specified/returned if the ContextFlags word
  507. // contains the flag CONTEXT_EXTENDED_REGISTERS.
  508. // The format and contexts are processor specific
  509. //
  510. ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  511. end;
  512. type
  513. PExceptionRecord = ^TExceptionRecord;
  514. TExceptionRecord = packed record
  515. ExceptionCode : cardinal;
  516. ExceptionFlags : Longint;
  517. ExceptionRecord : PExceptionRecord;
  518. ExceptionAddress : Pointer;
  519. NumberParameters : Longint;
  520. ExceptionInformation : array[0..EXCEPTION_MAXIMUM_PARAMETERS-1] of Pointer;
  521. end;
  522. PExceptionPointers = ^TExceptionPointers;
  523. TExceptionPointers = packed record
  524. ExceptionRecord : PExceptionRecord;
  525. ContextRecord : PContext;
  526. end;
  527. { type of functions that should be used for exception handling }
  528. TTopLevelExceptionFilter = function (excep : PExceptionPointers) : Longint;stdcall;
  529. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : TTopLevelExceptionFilter) : TTopLevelExceptionFilter;
  530. stdcall;external 'kernel32' name 'SetUnhandledExceptionFilter';
  531. const
  532. MaxExceptionLevel = 16;
  533. exceptLevel : Byte = 0;
  534. var
  535. exceptEip : array[0..MaxExceptionLevel-1] of Longint;
  536. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  537. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  538. {$ifdef SYSTEMEXCEPTIONDEBUG}
  539. procedure DebugHandleErrorAddrFrame(error : longint; addr, frame : pointer);
  540. begin
  541. if IsConsole then
  542. begin
  543. write(stderr,'HandleErrorAddrFrame(error=',error);
  544. write(stderr,',addr=',hexstr(ptruint(addr),8));
  545. writeln(stderr,',frame=',hexstr(ptruint(frame),8),')');
  546. end;
  547. HandleErrorAddrFrame(error,addr,frame);
  548. end;
  549. {$endif SYSTEMEXCEPTIONDEBUG}
  550. procedure JumpToHandleErrorFrame;
  551. var
  552. eip, ebp, error : Longint;
  553. begin
  554. // save ebp
  555. asm
  556. movl (%ebp),%eax
  557. movl %eax,ebp
  558. end;
  559. if (exceptLevel > 0) then
  560. dec(exceptLevel);
  561. eip:=exceptEip[exceptLevel];
  562. error:=exceptError[exceptLevel];
  563. {$ifdef SYSTEMEXCEPTIONDEBUG}
  564. if IsConsole then
  565. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  566. {$endif SYSTEMEXCEPTIONDEBUG}
  567. if resetFPU[exceptLevel] then
  568. SysResetFPU;
  569. { build a fake stack }
  570. asm
  571. {$ifdef REGCALL}
  572. movl ebp,%ecx
  573. movl eip,%edx
  574. movl error,%eax
  575. pushl eip
  576. movl ebp,%ebp // Change frame pointer
  577. {$else}
  578. movl ebp,%eax
  579. pushl %eax
  580. movl eip,%eax
  581. pushl %eax
  582. movl error,%eax
  583. pushl %eax
  584. movl eip,%eax
  585. pushl %eax
  586. movl ebp,%ebp // Change frame pointer
  587. {$endif}
  588. {$ifdef SYSTEMEXCEPTIONDEBUG}
  589. jmpl DebugHandleErrorAddrFrame
  590. {$else not SYSTEMEXCEPTIONDEBUG}
  591. jmpl HandleErrorAddrFrame
  592. {$endif SYSTEMEXCEPTIONDEBUG}
  593. end;
  594. end;
  595. function syswin32_i386_exception_handler(excep : PExceptionPointers) : Longint;stdcall;
  596. var
  597. res: longint;
  598. err: byte;
  599. must_reset_fpu: boolean;
  600. begin
  601. res := EXCEPTION_CONTINUE_SEARCH;
  602. if excep^.ContextRecord^.SegSs=_SS then begin
  603. err := 0;
  604. must_reset_fpu := true;
  605. {$ifdef SYSTEMEXCEPTIONDEBUG}
  606. if IsConsole then Writeln(stderr,'Exception ',
  607. hexstr(excep^.ExceptionRecord^.ExceptionCode, 8));
  608. {$endif SYSTEMEXCEPTIONDEBUG}
  609. case excep^.ExceptionRecord^.ExceptionCode of
  610. STATUS_INTEGER_DIVIDE_BY_ZERO,
  611. STATUS_FLOAT_DIVIDE_BY_ZERO :
  612. err := 200;
  613. STATUS_ARRAY_BOUNDS_EXCEEDED :
  614. begin
  615. err := 201;
  616. must_reset_fpu := false;
  617. end;
  618. STATUS_STACK_OVERFLOW :
  619. begin
  620. err := 202;
  621. must_reset_fpu := false;
  622. end;
  623. STATUS_FLOAT_OVERFLOW :
  624. err := 205;
  625. STATUS_FLOAT_DENORMAL_OPERAND,
  626. STATUS_FLOAT_UNDERFLOW :
  627. err := 206;
  628. {excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  629. STATUS_FLOAT_INEXACT_RESULT,
  630. STATUS_FLOAT_INVALID_OPERATION,
  631. STATUS_FLOAT_STACK_CHECK :
  632. err := 207;
  633. STATUS_INTEGER_OVERFLOW :
  634. begin
  635. err := 215;
  636. must_reset_fpu := false;
  637. end;
  638. STATUS_ILLEGAL_INSTRUCTION:
  639. { if we're testing sse support, simply set the flag and continue }
  640. if sse_check then
  641. begin
  642. os_supports_sse:=false;
  643. { skip the offending movaps %xmm7, %xmm6 instruction }
  644. inc(excep^.ContextRecord^.Eip,3);
  645. excep^.ExceptionRecord^.ExceptionCode := 0;
  646. res:=EXCEPTION_CONTINUE_EXECUTION;
  647. end
  648. else
  649. err := 216;
  650. STATUS_ACCESS_VIOLATION:
  651. { Athlon prefetch bug? }
  652. if is_prefetch(pointer(excep^.ContextRecord^.Eip)) then
  653. begin
  654. { if yes, then retry }
  655. excep^.ExceptionRecord^.ExceptionCode := 0;
  656. res:=EXCEPTION_CONTINUE_EXECUTION;
  657. end
  658. else
  659. err := 216;
  660. STATUS_CONTROL_C_EXIT:
  661. err := 217;
  662. STATUS_PRIVILEGED_INSTRUCTION:
  663. begin
  664. err := 218;
  665. must_reset_fpu := false;
  666. end;
  667. else
  668. begin
  669. if ((excep^.ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  670. err := 217
  671. else
  672. err := 255;
  673. end;
  674. end;
  675. if (err <> 0) and (exceptLevel < MaxExceptionLevel) then begin
  676. exceptEip[exceptLevel] := excep^.ContextRecord^.Eip;
  677. exceptError[exceptLevel] := err;
  678. resetFPU[exceptLevel] := must_reset_fpu;
  679. inc(exceptLevel);
  680. excep^.ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  681. excep^.ExceptionRecord^.ExceptionCode := 0;
  682. res := EXCEPTION_CONTINUE_EXECUTION;
  683. {$ifdef SYSTEMEXCEPTIONDEBUG}
  684. if IsConsole then begin
  685. writeln(stderr,'Exception Continue Exception set at ',
  686. hexstr(exceptEip[exceptLevel],8));
  687. writeln(stderr,'Eip changed to ',
  688. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', err);
  689. end;
  690. {$endif SYSTEMEXCEPTIONDEBUG}
  691. end;
  692. end;
  693. syswin32_i386_exception_handler := res;
  694. end;
  695. procedure install_exception_handlers;
  696. {$ifdef SYSTEMEXCEPTIONDEBUG}
  697. var
  698. oldexceptaddr,
  699. newexceptaddr : Longint;
  700. {$endif SYSTEMEXCEPTIONDEBUG}
  701. begin
  702. {$ifdef SYSTEMEXCEPTIONDEBUG}
  703. asm
  704. movl $0,%eax
  705. movl %fs:(%eax),%eax
  706. movl %eax,oldexceptaddr
  707. end;
  708. {$endif SYSTEMEXCEPTIONDEBUG}
  709. SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  710. {$ifdef SYSTEMEXCEPTIONDEBUG}
  711. asm
  712. movl $0,%eax
  713. movl %fs:(%eax),%eax
  714. movl %eax,newexceptaddr
  715. end;
  716. if IsConsole then
  717. writeln(stderr,'Old exception ',hexstr(oldexceptaddr,8),
  718. ' new exception ',hexstr(newexceptaddr,8));
  719. {$endif SYSTEMEXCEPTIONDEBUG}
  720. end;
  721. procedure remove_exception_handlers;
  722. begin
  723. SetUnhandledExceptionFilter(nil);
  724. end;
  725. {$else not cpui386 (Processor specific !!)}
  726. procedure install_exception_handlers;
  727. begin
  728. end;
  729. procedure remove_exception_handlers;
  730. begin
  731. end;
  732. {$endif Set_i386_Exception_handler}
  733. const
  734. { MultiByteToWideChar }
  735. MB_PRECOMPOSED = 1;
  736. CP_ACP = 0;
  737. WC_NO_BEST_FIT_CHARS = $400;
  738. function MultiByteToWideChar(CodePage:UINT; dwFlags:DWORD; lpMultiByteStr:PChar; cchMultiByte:longint; lpWideCharStr:PWideChar;cchWideChar:longint):longint;
  739. stdcall; external 'kernel32' name 'MultiByteToWideChar';
  740. function WideCharToMultiByte(CodePage:UINT; dwFlags:DWORD; lpWideCharStr:PWideChar; cchWideChar:longint; lpMultiByteStr:PChar;cchMultiByte:longint; lpDefaultChar:PChar; lpUsedDefaultChar:pointer):longint;
  741. stdcall; external 'kernel32' name 'WideCharToMultiByte';
  742. function CharUpperBuff(lpsz:LPWSTR; cchLength:DWORD):DWORD;
  743. stdcall; external 'user32' name 'CharUpperBuffW';
  744. function CharLowerBuff(lpsz:LPWSTR; cchLength:DWORD):DWORD;
  745. stdcall; external 'user32' name 'CharLowerBuffW';
  746. {******************************************************************************
  747. Widestring
  748. ******************************************************************************}
  749. procedure Win32Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  750. var
  751. destlen: SizeInt;
  752. begin
  753. // retrieve length including trailing #0
  754. // not anymore, because this must also be usable for single characters
  755. destlen:=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, source, len, nil, 0);
  756. // this will null-terminate
  757. setlength(dest, destlen);
  758. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, source, len, @dest[1], destlen);
  759. end;
  760. function Win32WideUpper(const s : WideString) : WideString;
  761. begin
  762. result:=s;
  763. if length(result)>0 then
  764. CharUpperBuff(LPWSTR(result),length(result));
  765. end;
  766. function Win32WideLower(const s : WideString) : WideString;
  767. begin
  768. result:=s;
  769. if length(result)>0 then
  770. CharLowerBuff(LPWSTR(result),length(result));
  771. end;
  772. {******************************************************************************}
  773. { include code common with win64 }
  774. {$I syswin.inc}
  775. {******************************************************************************}
  776. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  777. type
  778. tdosheader = packed record
  779. e_magic : word;
  780. e_cblp : word;
  781. e_cp : word;
  782. e_crlc : word;
  783. e_cparhdr : word;
  784. e_minalloc : word;
  785. e_maxalloc : word;
  786. e_ss : word;
  787. e_sp : word;
  788. e_csum : word;
  789. e_ip : word;
  790. e_cs : word;
  791. e_lfarlc : word;
  792. e_ovno : word;
  793. e_res : array[0..3] of word;
  794. e_oemid : word;
  795. e_oeminfo : word;
  796. e_res2 : array[0..9] of word;
  797. e_lfanew : longint;
  798. end;
  799. tpeheader = packed record
  800. PEMagic : longint;
  801. Machine : word;
  802. NumberOfSections : word;
  803. TimeDateStamp : longint;
  804. PointerToSymbolTable : longint;
  805. NumberOfSymbols : longint;
  806. SizeOfOptionalHeader : word;
  807. Characteristics : word;
  808. Magic : word;
  809. MajorLinkerVersion : byte;
  810. MinorLinkerVersion : byte;
  811. SizeOfCode : longint;
  812. SizeOfInitializedData : longint;
  813. SizeOfUninitializedData : longint;
  814. AddressOfEntryPoint : longint;
  815. BaseOfCode : longint;
  816. BaseOfData : longint;
  817. ImageBase : longint;
  818. SectionAlignment : longint;
  819. FileAlignment : longint;
  820. MajorOperatingSystemVersion : word;
  821. MinorOperatingSystemVersion : word;
  822. MajorImageVersion : word;
  823. MinorImageVersion : word;
  824. MajorSubsystemVersion : word;
  825. MinorSubsystemVersion : word;
  826. Reserved1 : longint;
  827. SizeOfImage : longint;
  828. SizeOfHeaders : longint;
  829. CheckSum : longint;
  830. Subsystem : word;
  831. DllCharacteristics : word;
  832. SizeOfStackReserve : longint;
  833. SizeOfStackCommit : longint;
  834. SizeOfHeapReserve : longint;
  835. SizeOfHeapCommit : longint;
  836. LoaderFlags : longint;
  837. NumberOfRvaAndSizes : longint;
  838. DataDirectory : array[1..$80] of byte;
  839. end;
  840. begin
  841. result:=tpeheader((pointer(SysInstance)+(tdosheader(pointer(SysInstance)^).e_lfanew))^).SizeOfStackReserve;
  842. end;
  843. begin
  844. { get some helpful informations }
  845. GetStartupInfo(@startupinfo);
  846. SysResetFPU;
  847. if not(IsLibrary) then
  848. SysInitFPU;
  849. { some misc Win32 stuff }
  850. hprevinst:=0;
  851. if not IsLibrary then
  852. SysInstance:=getmodulehandle(nil);
  853. MainInstance:=SysInstance;
  854. { pass dummy value }
  855. StackLength := CheckInitialStkLen($1000000);
  856. StackBottom := StackTop - StackLength;
  857. cmdshow:=startupinfo.wshowwindow;
  858. { Setup heap }
  859. InitHeap;
  860. SysInitExceptions;
  861. { setup fastmove stuff }
  862. fpc_cpucodeinit;
  863. SysInitStdIO;
  864. { Arguments }
  865. setup_arguments;
  866. { Reset IO Error }
  867. InOutRes:=0;
  868. ProcessID := GetCurrentProcessID;
  869. { threading }
  870. InitSystemThreads;
  871. { Reset internal error variable }
  872. errno:=0;
  873. initvariantmanager;
  874. initwidestringmanager;
  875. {$ifndef VER2_2}
  876. initunicodestringmanager;
  877. {$endif VER2_2}
  878. InitWin32Widestrings;
  879. DispCallByIDProc:=@DoDispCallByIDError;
  880. end.