initfini.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* .init/.fini section handling + C++ global constructor/destructor handling.
  2. This file is based on crtstuff.c, sol2-crti.asm, sol2-crtn.asm.
  3. Copyright (C) 1995-2022 Free Software Foundation, Inc.
  4. Contributor: Joern Rennecke <joern.rennecke@embecosm.com>
  5. on behalf of Synopsys Inc.
  6. This file is part of GCC.
  7. GCC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3, or (at your option)
  10. any later version.
  11. GCC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. Under Section 7 of GPL version 3, you are granted additional
  16. permissions described in the GCC Runtime Library Exception, version
  17. 3.1, as published by the Free Software Foundation.
  18. You should have received a copy of the GNU General Public License and
  19. a copy of the GCC Runtime Library Exception along with this program;
  20. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. <http://www.gnu.org/licenses/>. */
  22. /* Declare a pointer to void function type. */
  23. typedef void (*func_ptr) (void);
  24. #ifdef CRT_INIT
  25. /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
  26. to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
  27. __DTOR_END__ } per root executable and also one set of these symbols
  28. per shared library. So in any given whole process image, we may have
  29. multiple definitions of each of these symbols. In order to prevent
  30. these definitions from conflicting with one another, and in order to
  31. ensure that the proper lists are used for the initialization/finalization
  32. of each individual shared library (respectively), we give these symbols
  33. only internal (i.e. `static') linkage, and we also make it a point to
  34. refer to only the __CTOR_END__ symbol in crtfini.o and the __DTOR_LIST__
  35. symbol in crtinit.o, where they are defined. */
  36. static func_ptr __CTOR_LIST__[1] __attribute__ ((section (".ctors")))
  37. = { (func_ptr) (-1) };
  38. static func_ptr __DTOR_LIST__[1] __attribute__ ((section (".dtors")))
  39. = { (func_ptr) (-1) };
  40. /* Run all the global destructors on exit from the program. */
  41. /* Some systems place the number of pointers in the first word of the
  42. table. On SVR4 however, that word is -1. In all cases, the table is
  43. null-terminated. On SVR4, we start from the beginning of the list and
  44. invoke each per-compilation-unit destructor routine in order
  45. until we find that null.
  46. Note that this function MUST be static. There will be one of these
  47. functions in each root executable and one in each shared library, but
  48. although they all have the same code, each one is unique in that it
  49. refers to one particular associated `__DTOR_LIST__' which belongs to the
  50. same particular root executable or shared library file. */
  51. static void __do_global_dtors (void)
  52. asm ("__do_global_dtors") __attribute__ ((section (".text")));
  53. static void
  54. __do_global_dtors (void)
  55. {
  56. func_ptr *p;
  57. for (p = __DTOR_LIST__ + 1; *p; p++)
  58. (*p) ();
  59. }
  60. /* .init section start.
  61. This must appear at the start of the .init section. */
  62. asm ("\n\
  63. .section .init\n\
  64. .global init\n\
  65. .word 0\n\
  66. init:\n\
  67. st blink,[sp,4]\n\
  68. st fp,[sp]\n\
  69. mov fp,sp\n\
  70. sub sp,sp,16\n\
  71. ");
  72. /* .fini section start.
  73. This must appear at the start of the .init section. */
  74. asm ("\n\
  75. .section .fini\n\
  76. .global fini\n\
  77. .word 0\n\
  78. fini:\n\
  79. st blink,[sp,4]\n\
  80. st fp,[sp]\n\
  81. mov fp,sp\n\
  82. sub sp,sp,16\n\
  83. bl.nd __do_global_dtors\n\
  84. ");
  85. #endif /* CRT_INIT */
  86. #ifdef CRT_FINI
  87. /* Put a word containing zero at the end of each of our two lists of function
  88. addresses. Note that the words defined here go into the .ctors and .dtors
  89. sections of the crtend.o file, and since that file is always linked in
  90. last, these words naturally end up at the very ends of the two lists
  91. contained in these two sections. */
  92. static func_ptr __CTOR_END__[1] __attribute__ ((section (".ctors")))
  93. = { (func_ptr) 0 };
  94. static func_ptr __DTOR_END__[1] __attribute__ ((section (".dtors")))
  95. = { (func_ptr) 0 };
  96. /* Run all global constructors for the program.
  97. Note that they are run in reverse order. */
  98. static void __do_global_ctors (void)
  99. asm ("__do_global_ctors") __attribute__ ((section (".text")));
  100. static void
  101. __do_global_ctors (void)
  102. {
  103. func_ptr *p;
  104. for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
  105. (*p) ();
  106. }
  107. /* .init section end.
  108. This must live at the end of the .init section. */
  109. asm ("\n\
  110. .section .init\n\
  111. bl.nd __do_global_ctors\n\
  112. ld blink,[fp,4]\n\
  113. j.d blink\n\
  114. ld.a fp,[sp,16]\n\
  115. ");
  116. /* .fini section end.
  117. This must live at the end of the .fini section. */
  118. asm ("\n\
  119. .section .fini\n\
  120. ld blink,[fp,4]\n\
  121. j.d blink\n\
  122. ld.a fp,[sp,16]\n\
  123. ");
  124. #endif /* CRT_FINI */