fpmingw.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. var _imp__atexit : TAtExitFunction; Cvar; external; // "true" atexit in mingw libs.
  26. function atexit(p:TCFunction):longint;cdecl; [public, alias : '_atexit'];
  27. begin
  28. atexit:=_imp__atexit(p); // simply route to "true" atexit
  29. end;
  30. procedure __cpu_features_init; cdecl; external;
  31. procedure _pei386_runtime_relocator; cdecl; external;
  32. procedure __main; cdecl;external;
  33. procedure doinit;
  34. // other mingw initialization. Sequence from crt1.c
  35. begin
  36. // not (yet) done: set mingw exception handlers:
  37. // SetUnhandledExceptionFilter (_gnu_exception_handler);
  38. __cpu_features_init; // load CPU features. Might be useful for debugger :-)
  39. // fpreset; // don't do this, we init our own fp mask
  40. // _mingw32_init_mainargs (); // mingw doesn't handle arguments not necessary.
  41. // _mingw32_init_fmode (); // Set default filemode. Is not done for libraries, so we don't.
  42. // Adust references to dllimported data that have non-zero offsets.
  43. _pei386_runtime_relocator; //
  44. // aligns stack here to 16 bytes
  45. {From libgcc.a, __main calls global class constructors via
  46. __do_global_ctors, This in turn registers __do_global_dtors
  47. as the first entry of the app's atexit table. We do this
  48. explicitly at app startup rather than rely on gcc to generate
  49. the call in main's prologue, since main may be imported from a dll
  50. which has its own __do_global_ctors. }
  51. // __main; // should be libgcc initialization but this causes infinite loop.
  52. end;
  53. procedure _cexit; cdecl; external;
  54. procedure doatexit;
  55. begin
  56. {
  57. * Perform exit processing for the C library. This means
  58. * flushing output and calling 'atexit' registered functions.
  59. }
  60. _cexit ();
  61. end;
  62. initialization
  63. doinit;
  64. finalization
  65. doatexit;
  66. end.