sysinit.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2006 by Florian Klaempfl and Pavel Ozerski
  4. member of the Free Pascal development team.
  5. Win32 pascal only startup code
  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 sysinit;
  13. interface
  14. implementation
  15. var
  16. SysInstance : LongInt;
  17. TlsKeyVar: DWord = $ffffffff;
  18. InitFinalTable : record end; external name 'INITFINAL';
  19. ThreadvarTablesTable : record end; external name 'FPC_THREADVARTABLES';
  20. WideInitTables : record end; external name 'FPC_WIDEINITTABLES';
  21. {$ifdef FPC_HAS_RESSTRINITS}
  22. ResStrInitTables : record end; external name 'FPC_RESSTRINITTABLES';
  23. {$endif FPC_HAS_RESSTRINITS}
  24. ResourceStringTables : record end; external name 'FPC_RESOURCESTRINGTABLES';
  25. valgrind_used : boolean;external name '__fpc_valgrind';
  26. {$if defined(FPC_USE_TLS_DIRECTORY) or defined(FPC_SECTION_THREADVARS)}
  27. var
  28. tlsdir: record end; external name '_tls_used';
  29. procedure LinkIn(p1,p2,p3: Pointer); inline;
  30. begin
  31. end;
  32. {$endif}
  33. {$ifdef FPC_USE_TLS_DIRECTORY}
  34. var
  35. tls_callback_end: pointer; external name '__FPC_end_of_tls_callbacks';
  36. tls_callback: pointer; external name '__FPC_tls_callbacks';
  37. {$endif FPC_USE_TLS_DIRECTORY}
  38. procedure EXE_Entry(constref info : TEntryInformation); external name '_FPC_EXE_Entry';
  39. function DLL_Entry(constref info : TEntryInformation) : longbool; external name '_FPC_DLL_Entry';
  40. procedure PascalMain;external name 'PASCALMAIN';
  41. function GetStdHandle(nStdHandle:DWORD) : THandle; stdcall; external 'kernel32' name 'GetStdHandle';
  42. function GetConsoleMode(hConsoleHandle: THandle; var lpMode: DWORD): Boolean; stdcall; external 'kernel32' name 'GetConsoleMode';
  43. const
  44. STD_INPUT_HANDLE = dword(-10);
  45. SysInitEntryInformation : TEntryInformation = (
  46. InitFinalTable : @InitFinalTable;
  47. ThreadvarTablesTable : @ThreadvarTablesTable;
  48. ResourceStringTables : @ResourceStringTables;
  49. {$ifdef FPC_HAS_RESSTRINITS}
  50. ResStrInitTables : @ResStrInitTables;
  51. {$else FPC_HAS_RESSTRINITS}
  52. ResStrInitTables : nil;
  53. {$endif FPC_HAS_RESSTRINITS}
  54. ResLocation : nil;
  55. PascalMain : @PascalMain;
  56. valgrind_used : false;
  57. OS : (
  58. TlsKeyAddr : @TlsKeyVar;
  59. SysInstance : @SysInstance;
  60. WideInitTables: @WideInitTables;
  61. );
  62. );
  63. procedure SetupEntryInformation;
  64. begin
  65. { valgind_used is the only thng that can change at startup
  66. EntryInformation.InitFinalTable:=@InitFinalTable;
  67. EntryInformation.ThreadvarTablesTable:=@ThreadvarTablesTable;
  68. EntryInformation.ResourceStringTables:=@ResourceStringTables;
  69. EntryInformation.ResStrInitTables:=@ResStrInitTables;
  70. EntryInformation.OS.asm_exit:=@asm_exit;
  71. EntryInformation.OS.TlsKeyAddr:=@TlsKeyVar;
  72. EntryInformation.OS.SysInstance:=@SysInstance;
  73. EntryInformation.OS.WideInitTables:=@WideInitTables;
  74. EntryInformation.PascalMain:=@PascalMain;}
  75. SysInitEntryInformation.valgrind_used:=valgrind_used;
  76. end;
  77. {$define FPC_INSSIDE_SYSINIT}
  78. {$include systlsdir.inc}
  79. procedure _FPC_mainCRTStartup;stdcall;public name '_mainCRTStartup';
  80. begin
  81. IsConsole:=true;
  82. { do it like it is necessary for the startup code linking against cygwin }
  83. GetConsoleMode(GetStdHandle((Std_Input_Handle)),StartupConsoleMode);
  84. {$ifdef FPC_USE_TLS_DIRECTORY}
  85. LinkIn(@tlsdir,@tls_callback_end,@tls_callback);
  86. {$endif}
  87. SetupEntryInformation;
  88. Exe_entry(SysInitEntryInformation);
  89. end;
  90. procedure _FPC_WinMainCRTStartup;stdcall;public name '_WinMainCRTStartup';
  91. begin
  92. IsConsole:=false;
  93. {$ifdef FPC_USE_TLS_DIRECTORY}
  94. LinkIn(@tlsdir,@tls_callback_end,@tls_callback);
  95. {$endif}
  96. SetupEntryInformation;
  97. Exe_entry(SysInitEntryInformation);
  98. end;
  99. procedure _FPC_DLLMainCRTStartup(_hinstance : qword;_dllreason : dword;_dllparam:Pointer);stdcall;public name '_DLLMainCRTStartup';
  100. begin
  101. IsConsole:=true;
  102. sysinstance:=_hinstance;
  103. dllreason:=_dllreason;
  104. dllparam:=PtrInt(_dllparam);
  105. SetupEntryInformation;
  106. DLL_Entry(SysInitEntryInformation);
  107. end;
  108. procedure _FPC_DLLWinMainCRTStartup(_hinstance : qword;_dllreason : dword;_dllparam:Pointer);stdcall;public name '_DLLWinMainCRTStartup';
  109. begin
  110. IsConsole:=false;
  111. sysinstance:=_hinstance;
  112. dllreason:=_dllreason;
  113. dllparam:=PtrInt(_dllparam);
  114. SetupEntryInformation;
  115. DLL_Entry(SysInitEntryInformation);
  116. end;
  117. end.