2
0

mingw.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. unit mingw;
  2. {
  3. This file is part of the Free Pascal Integrated Development Environment
  4. Copyright (c) 2009 by Marco van de Voort
  5. Mingw helpers. Currently mostly atexit.
  6. Copied from fpmingw unit from ide directory.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. interface
  14. // mingw put atexit in binaries, so that it can have one atexit, and call it from
  15. // dll and .exe startup code.
  16. // This unit provides a similar service for when mingw code (read: libgdb and friends) are statically
  17. // linked to FPC.
  18. Type
  19. TCFunction = function:longint cdecl; // prototype of an handler to be registered with atexit
  20. function atexit(p:TCFunction):longint; cdecl; // export our own atexit handler
  21. implementation
  22. uses gdbint; // force dependancies that hopefully make it execute at the right moment.
  23. // prototype of atexit:
  24. Type
  25. TAtexitFunction = function(p:TCFUnction):longint cdecl;
  26. {$ifdef win64}
  27. var __imp_atexit : TAtExitFunction; Cvar; external; // "true" atexit in mingw libs.
  28. {$else not win64}
  29. var _imp__atexit : TAtExitFunction; Cvar; external; // "true" atexit in mingw libs.
  30. {$endif not win64}
  31. function atexit(p:TCFunction):longint;cdecl; [public, alias : '_atexit'];
  32. begin
  33. {$ifdef win64}
  34. atexit:=__imp_atexit(p); // simply route to "true" atexit
  35. {$else not win64}
  36. atexit:=_imp__atexit(p); // simply route to "true" atexit
  37. {$endif not win64}
  38. end;
  39. {$ifdef win32}
  40. procedure __cpu_features_init; cdecl; external;
  41. {$endif win32}
  42. procedure _pei386_runtime_relocator; cdecl; external;
  43. procedure __main; cdecl;external;
  44. procedure doinit;
  45. // other mingw initialization. Sequence from crt1.c
  46. begin
  47. // not (yet) done: set mingw exception handlers:
  48. // SetUnhandledExceptionFilter (_gnu_exception_handler);
  49. {$ifdef win32}
  50. __cpu_features_init; // load CPU features. Might be useful for debugger :-)
  51. {$endif win32}
  52. // fpreset; // don't do this, we init our own fp mask
  53. // _mingw32_init_mainargs (); // mingw doesn't handle arguments not necessary.
  54. // _mingw32_init_fmode (); // Set default filemode. Is not done for libraries, so we don't.
  55. // Adust references to dllimported data that have non-zero offsets.
  56. _pei386_runtime_relocator; //
  57. // aligns stack here to 16 bytes
  58. {From libgcc.a, __main calls global class constructors via
  59. __do_global_ctors, This in turn registers __do_global_dtors
  60. as the first entry of the app's atexit table. We do this
  61. explicitly at app startup rather than rely on gcc to generate
  62. the call in main's prologue, since main may be imported from a dll
  63. which has its own __do_global_ctors. }
  64. // __main; // should be libgcc initialization but this causes infinite loop.
  65. end;
  66. procedure _cexit; cdecl; external;
  67. procedure doatexit;
  68. begin
  69. {
  70. * Perform exit processing for the C library. This means
  71. * flushing output and calling 'atexit' registered functions.
  72. }
  73. _cexit ();
  74. end;
  75. initialization
  76. doinit;
  77. finalization
  78. doatexit;
  79. end.