mingw.pas 2.9 KB

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