initfini.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Special .init and .fini section support.
  2. Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. In addition to the permissions in the GNU Lesser General Public
  9. License, the Free Software Foundation gives you unlimited
  10. permission to link the compiled version of this file with other
  11. programs, and to distribute those programs without any restriction
  12. coming from the use of this file. (The GNU Lesser General Public
  13. License restrictions do apply in other respects; for example, they
  14. cover modification of the file, and distribution when not linked
  15. into another program.)
  16. The GNU C Library is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. Lesser General Public License for more details.
  20. You should have received a copy of the GNU Lesser General Public
  21. License along with the GNU C Library; if not, write to the Free
  22. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. 02111-1307 USA. */
  24. /* This file is compiled into assembly code which is then munged by a sed
  25. script into two files: crti.s and crtn.s.
  26. * crti.s puts a function prologue at the beginning of the
  27. .init and .fini sections and defines global symbols for
  28. those addresses, so they can be called as functions.
  29. * crtn.s puts the corresponding function epilogues
  30. in the .init and .fini sections. */
  31. #include <features.h>
  32. /* We use embedded asm for .section unconditionally, as this makes it
  33. easier to insert the necessary directives into crtn.S. */
  34. #define SECTION(x) asm (".section " x );
  35. /* Declare symbols as hidden. Hidden symbols are only seen by
  36. * the link editor and not by the dynamic loader. */
  37. #ifdef __HAVE_DOT_HIDDEN__
  38. # define HIDDEN(func) asm (".hidden " #func );
  39. #else
  40. # define HIDDEN(func)
  41. #endif
  42. #if defined(__sh__)
  43. /* The macro insert this sh specific stuff:
  44. @_SH_GLB_BEGINS
  45. bra 1f
  46. nop
  47. ALIGN
  48. @func_SH_GLB_LABEL
  49. 1:
  50. @_SH_GLB_ENDS
  51. */
  52. #define GLB_STUFF(func) asm ("\n/*@_SH_GLB_BEGINS*/"); \
  53. asm ("\n\tbra\t1f\n\tnop\n\tALIGN\n/*@" #func"_SH_GLB_LABEL*/\n1:"); \
  54. asm ("\n/*@_SH_GLB_ENDS*/");
  55. #else
  56. #define GLB_STUFF(func)
  57. #endif
  58. /* The initial common code ends here. */
  59. asm ("\n/*@HEADER_ENDS*/");
  60. /* To determine whether we need .end and .align: */
  61. //asm ("\n/*@TESTS_BEGIN*/");
  62. extern void dummy (void (*foo) (void));
  63. void
  64. dummy (void (*foo) (void))
  65. {
  66. if (foo)
  67. (*foo) ();
  68. }
  69. //asm ("\n/*@TESTS_END*/");
  70. /* The beginning of _init: */
  71. asm ("\n/*@_init_PROLOG_BEGINS*/");
  72. #ifdef GMON_SUPPORT
  73. asm ("\n/*@_init_GMON_STUFF_BEGINS*/");
  74. static void
  75. call_gmon_start(void)
  76. {
  77. extern void __gmon_start__ (void) __attribute__ ((weak)); /*weak_extern (__gmon_start__);*/
  78. void (*gmon_start) (void) = __gmon_start__;
  79. if (gmon_start)
  80. gmon_start ();
  81. }
  82. asm ("\n/*@_init_GMON_STUFF_PAUSES*/");
  83. #endif
  84. SECTION (".init")
  85. HIDDEN(_init)
  86. extern void i_am_not_a_leaf (void);
  87. extern void _init (void);
  88. void _init (void)
  89. {
  90. #ifdef GMON_SUPPORT
  91. asm ("\n/*@_init_GMON_STUFF_UNPAUSES*/");
  92. /* We cannot use the normal constructor mechanism in gcrt1.o because it
  93. appears before crtbegin.o in the link, so the header elt of .ctors
  94. would come after the elt for __gmon_start__. One approach is for
  95. gcrt1.o to reference a symbol which would be defined by some library
  96. module which has a constructor; but then user code's constructors
  97. would come first, and not be profiled. */
  98. call_gmon_start ();
  99. asm ("\n/*@_init_GMON_STUFF_ENDS*/");
  100. #else
  101. asm ("\n/*@_init_PROLOG_PAUSES*/");
  102. {
  103. /* Let GCC know that _init is not a leaf function by having a dummy
  104. * function call here. We arrange for this call to be omitted from
  105. * either crt file. */
  106. i_am_not_a_leaf ();
  107. }
  108. asm ("\n/*@_init_PROLOG_UNPAUSES*/");
  109. #endif
  110. GLB_STUFF(_init)
  111. asm ("ALIGN");
  112. asm("END_INIT");
  113. /* Now the epilog. */
  114. asm ("\n/*@_init_PROLOG_ENDS*/");
  115. asm ("\n/*@_init_EPILOG_BEGINS*/");
  116. }
  117. /* End of the _init epilog, beginning of the _fini prolog. */
  118. asm ("\n/*@_init_EPILOG_ENDS*/");
  119. asm ("\n/*@_fini_PROLOG_BEGINS*/");
  120. SECTION (".fini")
  121. HIDDEN(_fini)
  122. extern void i_am_not_a_leaf2 (void);
  123. extern void _fini (void);
  124. void _fini (void)
  125. {
  126. /* End of the _fini prolog. */
  127. GLB_STUFF(_fini)
  128. asm ("ALIGN");
  129. asm ("END_FINI");
  130. asm ("\n/*@_fini_PROLOG_ENDS*/");
  131. {
  132. /* Let GCC know that _fini is not a leaf function by having a dummy
  133. function call here. We arrange for this call to be omitted from
  134. either crt file. */
  135. i_am_not_a_leaf2 ();
  136. }
  137. /* Beginning of the _fini epilog. */
  138. asm ("\n/*@_fini_EPILOG_BEGINS*/");
  139. }
  140. /* End of the _fini epilog. Any further generated assembly (e.g. .ident)
  141. is shared between both crt files. */
  142. asm ("\n/*@_fini_EPILOG_ENDS*/");
  143. asm ("\n/*@TRAILER_BEGINS*/");
  144. /* End of file. */