fpmingw.pas 3.2 KB

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