initfini.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* .init/.fini section handling + C++ global constructor/destructor handling.
  2. This file is based on crtstuff.c, sol2-crti.S, sol2-crtn.S.
  3. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC 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. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* Declare a pointer to void function type. */
  21. typedef void (*func_ptr) (void);
  22. #ifdef CRT_INIT
  23. /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
  24. to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
  25. __DTOR_END__ } per root executable and also one set of these symbols
  26. per shared library. So in any given whole process image, we may have
  27. multiple definitions of each of these symbols. In order to prevent
  28. these definitions from conflicting with one another, and in order to
  29. ensure that the proper lists are used for the initialization/finalization
  30. of each individual shared library (respectively), we give these symbols
  31. only internal (i.e. `static') linkage, and we also make it a point to
  32. refer to only the __CTOR_END__ symbol in crtfini.o and the __DTOR_LIST__
  33. symbol in crtinit.o, where they are defined. */
  34. static func_ptr __CTOR_LIST__[1]
  35. __attribute__ ((used, section (".ctors")))
  36. = { (func_ptr) (-1) };
  37. static func_ptr __DTOR_LIST__[1]
  38. __attribute__ ((used, 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__ ((used, 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,\"ax\",@progbits\n\
  64. .balign 4\n\
  65. .global __init\n\
  66. __init:\n\
  67. push fp\n\
  68. push lr\n\
  69. mv fp,sp\n\
  70. seth r0, #shigh(__fini)\n\
  71. add3 r0, r0, #low(__fini)\n\
  72. bl atexit\n\
  73. .fillinsn\n\
  74. ");
  75. /* .fini section start.
  76. This must appear at the start of the .init section. */
  77. asm ("\n\
  78. .section .fini,\"ax\",@progbits\n\
  79. .balign 4\n\
  80. .global __fini\n\
  81. __fini:\n\
  82. push fp\n\
  83. push lr\n\
  84. mv fp,sp\n\
  85. bl __do_global_dtors\n\
  86. .fillinsn\n\
  87. ");
  88. #endif /* CRT_INIT */
  89. #ifdef CRT_FINI
  90. /* Put a word containing zero at the end of each of our two lists of function
  91. addresses. Note that the words defined here go into the .ctors and .dtors
  92. sections of the crtend.o file, and since that file is always linked in
  93. last, these words naturally end up at the very ends of the two lists
  94. contained in these two sections. */
  95. static func_ptr __CTOR_END__[1]
  96. __attribute__ ((used, section (".ctors")))
  97. = { (func_ptr) 0 };
  98. static func_ptr __DTOR_END__[1]
  99. __attribute__ ((used, section (".dtors")))
  100. = { (func_ptr) 0 };
  101. /* Run all global constructors for the program.
  102. Note that they are run in reverse order. */
  103. static void __do_global_ctors (void)
  104. asm ("__do_global_ctors") __attribute__ ((used, section (".text")));
  105. static void
  106. __do_global_ctors (void)
  107. {
  108. func_ptr *p;
  109. for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
  110. (*p) ();
  111. }
  112. /* .init section end.
  113. This must live at the end of the .init section. */
  114. asm ("\n\
  115. .section .init,\"ax\",@progbits\n\
  116. bl __do_global_ctors\n\
  117. mv sp,fp\n\
  118. pop lr\n\
  119. pop fp\n\
  120. jmp lr\n\
  121. .fillinsn\n\
  122. ");
  123. /* .fini section end.
  124. This must live at the end of the .fini section. */
  125. asm ("\n\
  126. .section .fini,\"ax\",@progbits\n\
  127. mv sp,fp\n\
  128. pop lr\n\
  129. pop fp\n\
  130. jmp lr\n\
  131. .fillinsn\n\
  132. ");
  133. #endif /* CRT_FINI */