sysinit.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 : QWord;
  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. function LinkIn(p1,p2,p3: Pointer): pointer;
  30. begin
  31. LinkIn:=p1;
  32. end;
  33. {$endif}
  34. {$ifdef FPC_USE_TLS_DIRECTORY}
  35. var
  36. tls_callback_end: pointer; external name '__FPC_end_of_tls_callbacks';
  37. tls_callback: pointer; external name '__FPC_tls_callbacks';
  38. {$endif FPC_USE_TLS_DIRECTORY}
  39. procedure EXE_Entry(constref info : TEntryInformation); external name '_FPC_EXE_Entry';
  40. function DLL_Entry(constref info : TEntryInformation) : longbool; external name '_FPC_DLL_Entry';
  41. procedure PascalMain;external name 'PASCALMAIN';
  42. function GetStdHandle(nStdHandle:DWORD) : THandle; stdcall; external 'kernel32' name 'GetStdHandle';
  43. function GetConsoleMode(hConsoleHandle: THandle; var lpMode: DWORD): Boolean; stdcall; external 'kernel32' name 'GetConsoleMode';
  44. const
  45. STD_INPUT_HANDLE = dword(-10);
  46. SysInitEntryInformation : TEntryInformation = (
  47. InitFinalTable : @InitFinalTable;
  48. ThreadvarTablesTable : @ThreadvarTablesTable;
  49. ResourceStringTables : @ResourceStringTables;
  50. {$ifdef FPC_HAS_RESSTRINITS}
  51. ResStrInitTables : @ResStrInitTables;
  52. {$else FPC_HAS_RESSTRINITS}
  53. ResStrInitTables : nil;
  54. {$endif FPC_HAS_RESSTRINITS}
  55. ResLocation : nil;
  56. PascalMain : @PascalMain;
  57. valgrind_used : false;
  58. OS : (
  59. TlsKeyAddr : @TlsKeyVar;
  60. SysInstance : @SysInstance;
  61. WideInitTables: @WideInitTables;
  62. );
  63. );
  64. procedure SetupEntryInformation;
  65. begin
  66. { valgind_used is the only thng that can change at startup
  67. EntryInformation.InitFinalTable:=@InitFinalTable;
  68. EntryInformation.ThreadvarTablesTable:=@ThreadvarTablesTable;
  69. EntryInformation.ResourceStringTables:=@ResourceStringTables;
  70. EntryInformation.ResStrInitTables:=@ResStrInitTables;
  71. EntryInformation.OS.asm_exit:=@asm_exit;
  72. EntryInformation.OS.TlsKeyAddr:=@TlsKeyVar;
  73. EntryInformation.OS.SysInstance:=@SysInstance;
  74. EntryInformation.OS.WideInitTables:=@WideInitTables;
  75. EntryInformation.PascalMain:=@PascalMain;}
  76. SysInitEntryInformation.valgrind_used:=valgrind_used;
  77. end;
  78. {$define FPC_INSSIDE_SYSINIT}
  79. {$include systlsdir.inc}
  80. procedure _FPC_mainCRTStartup;stdcall;public name '_mainCRTStartup';
  81. begin
  82. IsConsole:=true;
  83. { do it like it is necessary for the startup code linking against cygwin }
  84. GetConsoleMode(GetStdHandle((Std_Input_Handle)),StartupConsoleMode);
  85. {$ifdef FPC_USE_TLS_DIRECTORY}
  86. LinkIn(@tlsdir,@tls_callback_end,@tls_callback);
  87. {$endif}
  88. SetupEntryInformation;
  89. Exe_entry(SysInitEntryInformation);
  90. end;
  91. procedure _FPC_WinMainCRTStartup;stdcall;public name '_WinMainCRTStartup';
  92. begin
  93. IsConsole:=false;
  94. {$ifdef FPC_USE_TLS_DIRECTORY}
  95. LinkIn(@tlsdir,@tls_callback_end,@tls_callback);
  96. {$endif}
  97. SetupEntryInformation;
  98. Exe_entry(SysInitEntryInformation);
  99. end;
  100. procedure _FPC_DLLMainCRTStartup(_hinstance : qword;_dllreason : dword;_dllparam:Pointer);stdcall;public name '_DLLMainCRTStartup';
  101. begin
  102. IsConsole:=true;
  103. sysinstance:=_hinstance;
  104. dllreason:=_dllreason;
  105. dllparam:=PtrInt(_dllparam);
  106. SetupEntryInformation;
  107. DLL_Entry(SysInitEntryInformation);
  108. end;
  109. procedure _FPC_DLLWinMainCRTStartup(_hinstance : qword;_dllreason : dword;_dllparam:Pointer);stdcall;public name '_DLLWinMainCRTStartup';
  110. begin
  111. IsConsole:=false;
  112. sysinstance:=_hinstance;
  113. dllreason:=_dllreason;
  114. dllparam:=PtrInt(_dllparam);
  115. SetupEntryInformation;
  116. DLL_Entry(SysInitEntryInformation);
  117. end;
  118. end.