initc.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Pierre Muller
  4. Code to generate execution of all c functions
  5. with constructors attributes
  6. Based on .ctor and .dtor sections of DJGPP gcc compiler
  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. unit InitC;
  14. interface
  15. implementation
  16. { we need to include dpmiexcp unit
  17. to avoid getting troubles with _exit found both
  18. in libc and in v2prt0.as PM }
  19. uses
  20. dpmiexcp;
  21. type
  22. simple_proc = procedure;
  23. var
  24. first_ctor : longint;external name 'djgpp_first_ctor';
  25. ctor : array [0..maxlongint div sizeof(simple_proc)-1] of simple_proc;external name 'djgpp_first_ctor';
  26. last_ctor : longint;external name 'djgpp_last_ctor';
  27. first_dtor : longint;external name 'djgpp_first_dtor';
  28. dtor : array [0..maxlongint div sizeof(simple_proc)-1] of simple_proc;external name 'djgpp_first_dtor';
  29. last_dtor : longint;external name 'djgpp_last_dtor';
  30. bss_count : longint;external name '___bss_count';
  31. const
  32. save_exit : pointer = nil;
  33. procedure run_c_constructors;
  34. const
  35. already_done : longint = -1;
  36. var
  37. f : simple_proc;
  38. i,nb : longint;
  39. begin
  40. if already_done=bss_count then
  41. exit;
  42. already_done:=bss_count;
  43. f:=ctor[0];
  44. nb:=((cardinal(@last_ctor)-cardinal(@first_ctor)) div sizeof(pointer));
  45. for i:=1 to nb do
  46. begin
  47. f();
  48. f:=ctor[i];
  49. end;
  50. end;
  51. procedure run_c_destructors;
  52. const
  53. already_done : longint = -1;
  54. var
  55. f : simple_proc;
  56. i,nb : longint;
  57. begin
  58. exitproc:=save_exit;
  59. if already_done=bss_count then
  60. exit;
  61. already_done:=bss_count;
  62. f:=dtor[0];
  63. nb:=((cardinal(last_dtor)-cardinal(first_dtor)) div sizeof(pointer));
  64. for i:=1 to nb do
  65. begin
  66. f();
  67. f:=dtor[i];
  68. end;
  69. end;
  70. begin
  71. run_c_constructors;
  72. If cardinal(@first_dtor)<>cardinal(@last_dtor) then
  73. begin
  74. { can exitproc be allready non nil here ?
  75. you have to make really weird things to achieve
  76. that be lets suppose it is possible !! (PM) }
  77. save_exit:=exitproc;
  78. exitproc:=@run_c_destructors;
  79. end;
  80. end.