vxcrtstuff.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* This file is part of GCC.
  2. GCC is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU General Public License as published by the Free
  4. Software Foundation; either version 3, or (at your option) any later
  5. version.
  6. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9. for more details.
  10. Under Section 7 of GPL version 3, you are granted additional
  11. permissions described in the GCC Runtime Library Exception, version
  12. 3.1, as published by the Free Software Foundation.
  13. You should have received a copy of the GNU General Public License and
  14. a copy of the GCC Runtime Library Exception along with this program;
  15. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  16. <http://www.gnu.org/licenses/>. */
  17. /* The essential point of the crtbegin/crtend files on VxWorks is to handle
  18. the eh frames registration thanks to dedicated constructors and
  19. destructors. What needs to be done depends on the VxWorks version and the
  20. kind of module (rtp, dkm, ...) one is building. */
  21. #define IN_LIBGCC2
  22. /* FIXME: Including auto-host is incorrect, but until we have
  23. identified the set of defines that need to go into auto-target.h,
  24. this will have to do. */
  25. #include "auto-host.h"
  26. #undef caddr_t
  27. #undef pid_t
  28. #undef rlim_t
  29. #undef ssize_t
  30. #undef vfork
  31. #include "tconfig.h"
  32. #include "tsystem.h"
  33. #include "coretypes.h"
  34. #include "tm.h"
  35. #include "libgcc_tm.h"
  36. #include "unwind-dw2-fde.h"
  37. /* If we are entitled/requested to use init/fini arrays, we'll rely on that.
  38. Otherwise, we may rely on ctors/dtors sections for RTPs or expect munch to
  39. be involved for kernel modules. */
  40. #if !defined(USE_INITFINI_ARRAY) && defined(__RTP__)
  41. #define USE_CDTORS_SECTIONS
  42. #endif
  43. #if DWARF2_UNWIND_INFO && !defined(__USING_SJLJ_EXCEPTIONS__)
  44. #define USE_EH_FRAME_REGISTRY
  45. #endif
  46. /* ------------------------------ crtbegin ------------------------------- */
  47. #ifdef CRT_BEGIN
  48. #if DEFAULT_USE_CXA_ATEXIT && defined(__RTP__)
  49. /* This mimics the crtstuff.c behavior. dso_handle should be NULL for the
  50. main program (in vx_crtbegin.o) and a unique value for the shared libraries
  51. (in vx_crtbeginS.o). */
  52. extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
  53. #ifdef CRTSTUFFS_O
  54. void *__dso_handle = &__dso_handle;
  55. #else
  56. void *__dso_handle = 0;
  57. #endif
  58. #endif /* DEFAULT_USE_CXA_ATEXIT */
  59. /* Determine what names to use for the constructor/destructor functions. */
  60. #if defined(USE_CDTORS_SECTIONS) || defined(USE_INITFINI_ARRAY)
  61. #define EH_CTOR_NAME _crtbe_register_frame
  62. #define EH_DTOR_NAME _ctrbe_deregister_frame
  63. #define EH_LINKAGE static
  64. #else
  65. /* No specific sections for constructors or destructors: we thus use a
  66. symbol naming convention so that the constructors are then recognized
  67. by munch or whatever tool is used for the final link phase. Since the
  68. pointers to the constructor/destructor functions are not created in this
  69. translation unit, they must have external linkage. */
  70. #define EH_CTOR_NAME _GLOBAL__I_00101_0__crtbe_register_frame
  71. #define EH_DTOR_NAME _GLOBAL__D_00101_1__crtbe_deregister_frame
  72. #define EH_LINKAGE
  73. #endif
  74. #ifdef USE_INITFINI_ARRAY
  75. /* .init_array and .fini_array is supported starting VxWorks 7.2 in all
  76. cases. The compiler is then configured to always support priorities in
  77. constructors, so we can rely on the constructor and destructor attributes
  78. to generate the proper sections. */
  79. #define EH_CTOR_ATTRIBUTE __attribute__((constructor (101)))
  80. #define EH_DTOR_ATTRIBUTE __attribute__((destructor (101)))
  81. /* Provide the init/fini array support functions for shared libraries,
  82. where we don't want to drag libc_internal contents blindly and which
  83. provides functions with a slightly different name anyway. */
  84. #if HAVE_INITFINI_ARRAY_SUPPORT && defined(CRTSTUFFS_O)
  85. /* Run through the .init_array, .fini_array sections. The linker script
  86. *must* provide __init_array_start, __init_array_end, __fini_array_start,
  87. __fini_array_end symbols. */
  88. typedef void (*initfini_ptr) (void);
  89. extern initfini_ptr __init_array_start[];
  90. extern initfini_ptr __init_array_end[];
  91. extern initfini_ptr __fini_array_start[];
  92. extern initfini_ptr __fini_array_end[];
  93. /* Provide the actual code through static functions, which don't need
  94. to be exposed in the shared lib interface. */
  95. static void __exec_init_array(void)
  96. {
  97. initfini_ptr *fn;
  98. for (fn = __init_array_start; fn < __init_array_end; ++fn)
  99. (*fn)();
  100. }
  101. static void __exec_fini_array(void)
  102. {
  103. initfini_ptr *fn;
  104. for (fn = __fini_array_end - 1; fn >= __fini_array_start; --fn)
  105. (*fn)();
  106. }
  107. /* Reference the two above functions as the init / fini function. */
  108. void __attribute__ ((__section__ (".init"))) _init()
  109. {
  110. __exec_init_array();
  111. }
  112. void __attribute__ ((__section__ (".fini"))) _fini()
  113. {
  114. __exec_fini_array();
  115. }
  116. #endif /* __CRTSTUFFS_O__ */
  117. #else /* !USE_INITFINI_ARRAY */
  118. /* Note: Even in case of .ctors/.dtors sections, we can't use the attribute
  119. (constructor (15)) here as gcc may have been configured with constructors
  120. priority disabled. We will instead craft an explicit section name for this
  121. purpose. */
  122. #define EH_CTOR_ATTRIBUTE
  123. #define EH_DTOR_ATTRIBUTE
  124. #endif /* USE_INITFINI_ARRAY */
  125. #ifdef USE_EH_FRAME_REGISTRY
  126. /* Stick a label at the beginning of the frame unwind info so we can register
  127. and deregister it with the exception handling library code. */
  128. static const char __EH_FRAME_BEGIN__[]
  129. __attribute__((section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4)))
  130. = { };
  131. EH_LINKAGE EH_CTOR_ATTRIBUTE void EH_CTOR_NAME (void)
  132. {
  133. static struct object object;
  134. __register_frame_info (__EH_FRAME_BEGIN__, &object);
  135. }
  136. EH_LINKAGE EH_DTOR_ATTRIBUTE void EH_DTOR_NAME (void)
  137. {
  138. __deregister_frame_info (__EH_FRAME_BEGIN__);
  139. }
  140. #endif /* USE_EH_FRAME_REGISTRY */
  141. #ifdef USE_CDTORS_SECTIONS
  142. /* As explained above, we need to manually build the sections here as the
  143. compiler may not have support for constructors priority enabled. */
  144. static void (* volatile eh_registration_ctors[])()
  145. __attribute__((section (".ctors.101")))
  146. = { &EH_CTOR_NAME };
  147. static void (* volatile eh_registration_dtors[])()
  148. __attribute__((section (".dtors.65434")))
  149. = { &EH_DTOR_NAME };
  150. #endif
  151. /* ------------------------------ crtend --------------------------------- */
  152. #elif defined (CRT_END) /* ! CRT_BEGIN */
  153. #ifdef USE_EH_FRAME_REGISTRY
  154. /* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
  155. this would be the 'length' field in a real FDE. */
  156. static const char __FRAME_END__[]
  157. __attribute__ ((used, section(__LIBGCC_EH_FRAME_SECTION_NAME__),
  158. aligned(4)))
  159. = { 0, 0, 0, 0 };
  160. #endif /* USE_EH_FRAME_REGISTRY */
  161. #else /* ! CRT_BEGIN & ! CRT_END */
  162. #error "One of CRT_BEGIN or CRT_END must be defined."
  163. #endif