crtstuff.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /* Specialized bits of code needed to support construction and
  2. destruction of file-scope objects in C++ code.
  3. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  4. Contributed by Ron Guilmette (rfg@monkeys.com).
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. /* This file is a bit like libgcc2.c in that it is compiled
  22. multiple times and yields multiple .o files.
  23. This file is useful on target machines where the object file format
  24. supports multiple "user-defined" sections (e.g. COFF, ELF, ROSE). On
  25. such systems, this file allows us to avoid running collect (or any
  26. other such slow and painful kludge). Additionally, if the target
  27. system supports a .init section, this file allows us to support the
  28. linking of C++ code with a non-C++ main program.
  29. Note that if INIT_SECTION_ASM_OP is defined in the tm.h file, then
  30. this file *will* make use of the .init section. If that symbol is
  31. not defined however, then the .init section will not be used.
  32. Currently, only ELF and COFF are supported. It is likely however that
  33. ROSE could also be supported, if someone was willing to do the work to
  34. make whatever (small?) adaptations are needed. (Some work may be
  35. needed on the ROSE assembler and linker also.)
  36. This file must be compiled with gcc. */
  37. /* Target machine header files require this define. */
  38. #define IN_LIBGCC2
  39. /* FIXME: Including auto-host is incorrect, but until we have
  40. identified the set of defines that need to go into auto-target.h,
  41. this will have to do. */
  42. #include "auto-host.h"
  43. #undef caddr_t
  44. #undef pid_t
  45. #undef rlim_t
  46. #undef ssize_t
  47. #undef vfork
  48. #include "tconfig.h"
  49. #include "tsystem.h"
  50. #include "coretypes.h"
  51. #include "tm.h"
  52. #include "libgcc_tm.h"
  53. #include "unwind-dw2-fde.h"
  54. #ifndef FORCE_CODE_SECTION_ALIGN
  55. # define FORCE_CODE_SECTION_ALIGN
  56. #endif
  57. #ifndef CRT_CALL_STATIC_FUNCTION
  58. # define CRT_CALL_STATIC_FUNCTION(SECTION_OP, FUNC) \
  59. static void __attribute__((__used__)) \
  60. call_ ## FUNC (void) \
  61. { \
  62. asm (SECTION_OP); \
  63. FUNC (); \
  64. FORCE_CODE_SECTION_ALIGN \
  65. asm (__LIBGCC_TEXT_SECTION_ASM_OP__); \
  66. }
  67. #endif
  68. #if defined(TARGET_DL_ITERATE_PHDR) && \
  69. (defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__))
  70. #define BSD_DL_ITERATE_PHDR_AVAILABLE
  71. #endif
  72. #if defined(OBJECT_FORMAT_ELF) \
  73. && !defined(OBJECT_FORMAT_FLAT) \
  74. && defined(HAVE_LD_EH_FRAME_HDR) \
  75. && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \
  76. && defined(BSD_DL_ITERATE_PHDR_AVAILABLE)
  77. #include <link.h>
  78. # define USE_PT_GNU_EH_FRAME
  79. #endif
  80. #if defined(OBJECT_FORMAT_ELF) \
  81. && !defined(OBJECT_FORMAT_FLAT) \
  82. && defined(HAVE_LD_EH_FRAME_HDR) && defined(TARGET_DL_ITERATE_PHDR) \
  83. && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \
  84. && defined(__sun__) && defined(__svr4__)
  85. #include <link.h>
  86. # define USE_PT_GNU_EH_FRAME
  87. #endif
  88. #if defined(OBJECT_FORMAT_ELF) \
  89. && !defined(OBJECT_FORMAT_FLAT) \
  90. && defined(HAVE_LD_EH_FRAME_HDR) \
  91. && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \
  92. && defined(__GLIBC__) && __GLIBC__ >= 2
  93. #include <link.h>
  94. /* uClibc pretends to be glibc 2.2 and DT_CONFIG is defined in its link.h.
  95. But it doesn't use PT_GNU_EH_FRAME ELF segment currently. */
  96. # if !defined(__UCLIBC__) \
  97. && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
  98. || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
  99. # define USE_PT_GNU_EH_FRAME
  100. # endif
  101. #endif
  102. #if defined(OBJECT_FORMAT_ELF) \
  103. && !defined(OBJECT_FORMAT_FLAT) \
  104. && defined(HAVE_LD_EH_FRAME_HDR) \
  105. && !defined(CRTSTUFFT_O) \
  106. && defined(inhibit_libc) \
  107. && (defined(__GLIBC__) || defined(__gnu_linux__) || defined(__GNU__))
  108. /* On systems using glibc, an inhibit_libc build of libgcc is only
  109. part of a bootstrap process. Build the same crt*.o as would be
  110. built with headers present, so that it is not necessary to build
  111. glibc more than once for the bootstrap to converge. */
  112. # define USE_PT_GNU_EH_FRAME
  113. #endif
  114. #ifdef USE_EH_FRAME_REGISTRY_ALWAYS
  115. # ifndef __LIBGCC_EH_FRAME_SECTION_NAME__
  116. # error "Can't use explicit exception-frame-registration without __LIBGCC_EH_FRAME_SECTION_NAME__"
  117. # endif
  118. #endif
  119. #if defined(__LIBGCC_EH_FRAME_SECTION_NAME__) && (!defined(USE_PT_GNU_EH_FRAME) || defined(USE_EH_FRAME_REGISTRY_ALWAYS))
  120. # define USE_EH_FRAME_REGISTRY
  121. #endif
  122. #if defined(__LIBGCC_EH_FRAME_SECTION_NAME__) \
  123. && __LIBGCC_EH_TABLES_CAN_BE_READ_ONLY__
  124. # define EH_FRAME_SECTION_CONST const
  125. #else
  126. # define EH_FRAME_SECTION_CONST
  127. #endif
  128. #if !defined(DTOR_LIST_END) && defined(OBJECT_FORMAT_ELF) \
  129. && defined(HAVE_GAS_HIDDEN) && !defined(FINI_ARRAY_SECTION_ASM_OP)
  130. # define HIDDEN_DTOR_LIST_END
  131. #endif
  132. #if !defined(USE_TM_CLONE_REGISTRY) && defined(OBJECT_FORMAT_ELF)
  133. # define USE_TM_CLONE_REGISTRY 1
  134. #elif !defined(USE_TM_CLONE_REGISTRY)
  135. # define USE_TM_CLONE_REGISTRY 0
  136. #endif
  137. /* We do not want to add the weak attribute to the declarations of these
  138. routines in unwind-dw2-fde.h because that will cause the definition of
  139. these symbols to be weak as well.
  140. This exposes a core issue, how to handle creating weak references vs
  141. how to create weak definitions. Either we have to have the definition
  142. of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or
  143. have a second declaration if we want a function's references to be weak,
  144. but not its definition.
  145. Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until
  146. one thinks about scaling to larger problems -- i.e., the condition under
  147. which TARGET_WEAK_ATTRIBUTE is active will eventually get far too
  148. complicated.
  149. So, we take an approach similar to #pragma weak -- we have a second
  150. declaration for functions that we want to have weak references.
  151. Neither way is particularly good. */
  152. /* References to __register_frame_info and __deregister_frame_info should
  153. be weak in this file if at all possible. */
  154. extern void __register_frame_info (const void *, struct object *)
  155. TARGET_ATTRIBUTE_WEAK;
  156. extern void __register_frame_info_bases (const void *, struct object *,
  157. void *, void *)
  158. TARGET_ATTRIBUTE_WEAK;
  159. extern void *__deregister_frame_info (const void *)
  160. TARGET_ATTRIBUTE_WEAK;
  161. extern void *__deregister_frame_info_bases (const void *)
  162. TARGET_ATTRIBUTE_WEAK;
  163. extern void __do_global_ctors_1 (void);
  164. /* Likewise for transactional memory clone tables. */
  165. extern void _ITM_registerTMCloneTable (void *, size_t) TARGET_ATTRIBUTE_WEAK;
  166. extern void _ITM_deregisterTMCloneTable (void *) TARGET_ATTRIBUTE_WEAK;
  167. #ifdef OBJECT_FORMAT_ELF
  168. /* Declare a pointer to void function type. */
  169. typedef void (*func_ptr) (void);
  170. #define STATIC static
  171. #else /* OBJECT_FORMAT_ELF */
  172. #include "gbl-ctors.h"
  173. #define STATIC
  174. #endif /* OBJECT_FORMAT_ELF */
  175. #ifdef CRT_BEGIN
  176. /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
  177. to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
  178. __DTOR_END__ } per root executable and also one set of these symbols
  179. per shared library. So in any given whole process image, we may have
  180. multiple definitions of each of these symbols. In order to prevent
  181. these definitions from conflicting with one another, and in order to
  182. ensure that the proper lists are used for the initialization/finalization
  183. of each individual shared library (respectively), we give these symbols
  184. only internal (i.e. `static') linkage, and we also make it a point to
  185. refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__
  186. symbol in crtbegin.o, where they are defined. */
  187. /* No need for .ctors/.dtors section if linker can place them in
  188. .init_array/.fini_array section. */
  189. #ifndef USE_INITFINI_ARRAY
  190. /* The -1 is a flag to __do_global_[cd]tors indicating that this table
  191. does not start with a count of elements. */
  192. #ifdef CTOR_LIST_BEGIN
  193. CTOR_LIST_BEGIN;
  194. #elif defined(__LIBGCC_CTORS_SECTION_ASM_OP__)
  195. /* Hack: force cc1 to switch to .data section early, so that assembling
  196. __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */
  197. static func_ptr force_to_data[1] __attribute__ ((__used__)) = { };
  198. asm (__LIBGCC_CTORS_SECTION_ASM_OP__);
  199. STATIC func_ptr __CTOR_LIST__[1]
  200. __attribute__ ((__used__, aligned(__alignof__(func_ptr))))
  201. = { (func_ptr) (-1) };
  202. #else
  203. STATIC func_ptr __CTOR_LIST__[1]
  204. __attribute__ ((__used__, section(".ctors"), aligned(__alignof__(func_ptr))))
  205. = { (func_ptr) (-1) };
  206. #endif /* __CTOR_LIST__ alternatives */
  207. #ifdef DTOR_LIST_BEGIN
  208. DTOR_LIST_BEGIN;
  209. #elif defined(__LIBGCC_DTORS_SECTION_ASM_OP__)
  210. asm (__LIBGCC_DTORS_SECTION_ASM_OP__);
  211. STATIC func_ptr __DTOR_LIST__[1]
  212. __attribute__ ((aligned(__alignof__(func_ptr))))
  213. = { (func_ptr) (-1) };
  214. #else
  215. STATIC func_ptr __DTOR_LIST__[1]
  216. __attribute__((section(".dtors"), aligned(__alignof__(func_ptr))))
  217. = { (func_ptr) (-1) };
  218. #endif /* __DTOR_LIST__ alternatives */
  219. #endif /* USE_INITFINI_ARRAY */
  220. #ifdef USE_EH_FRAME_REGISTRY
  221. /* Stick a label at the beginning of the frame unwind info so we can register
  222. and deregister it with the exception handling library code. */
  223. STATIC EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[]
  224. __attribute__((section(__LIBGCC_EH_FRAME_SECTION_NAME__), aligned(4)))
  225. = { };
  226. #endif /* USE_EH_FRAME_REGISTRY */
  227. #if USE_TM_CLONE_REGISTRY
  228. STATIC func_ptr __TMC_LIST__[]
  229. __attribute__((used, section(".tm_clone_table"), aligned(__alignof__(void*))))
  230. = { };
  231. # ifdef HAVE_GAS_HIDDEN
  232. extern func_ptr __TMC_END__[] __attribute__((__visibility__ ("hidden")));
  233. # endif
  234. static inline void
  235. deregister_tm_clones (void)
  236. {
  237. void (*fn) (void *);
  238. #ifdef HAVE_GAS_HIDDEN
  239. func_ptr *end = __TMC_END__;
  240. // Do not optimize the comparison to false.
  241. __asm ("" : "+g" (end));
  242. if (__TMC_LIST__ == end)
  243. return;
  244. #else
  245. if (__TMC_LIST__[0] == NULL)
  246. return;
  247. #endif
  248. fn = _ITM_deregisterTMCloneTable;
  249. __asm ("" : "+r" (fn));
  250. if (fn)
  251. fn (__TMC_LIST__);
  252. }
  253. static inline void
  254. register_tm_clones (void)
  255. {
  256. void (*fn) (void *, size_t);
  257. size_t size;
  258. #ifdef HAVE_GAS_HIDDEN
  259. func_ptr *end = __TMC_END__;
  260. // Do not optimize the comparison to false.
  261. __asm ("" : "+g" (end));
  262. size = (end - __TMC_LIST__) / 2;
  263. #else
  264. for (size = 0; __TMC_LIST__[size * 2] != NULL; size++)
  265. continue;
  266. #endif
  267. if (size == 0)
  268. return;
  269. fn = _ITM_registerTMCloneTable;
  270. __asm ("" : "+r" (fn));
  271. if (fn)
  272. fn (__TMC_LIST__, size);
  273. }
  274. #endif /* USE_TM_CLONE_REGISTRY */
  275. #if defined(__LIBGCC_INIT_SECTION_ASM_OP__) \
  276. || defined(__LIBGCC_INIT_ARRAY_SECTION_ASM_OP__)
  277. #ifdef OBJECT_FORMAT_ELF
  278. #if DEFAULT_USE_CXA_ATEXIT
  279. /* Declare the __dso_handle variable. It should have a unique value
  280. in every shared-object; in a main program its value is zero. The
  281. object should in any case be protected. This means the instance
  282. in one DSO or the main program is not used in another object. The
  283. dynamic linker takes care of this.
  284. If __cxa_atexit is not being used, __dso_handle will not be used and
  285. doesn't need to be defined. */
  286. #ifdef TARGET_LIBGCC_SDATA_SECTION
  287. extern void *__dso_handle __attribute__ ((__section__ (TARGET_LIBGCC_SDATA_SECTION)));
  288. #endif
  289. #ifdef HAVE_GAS_HIDDEN
  290. extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
  291. #endif
  292. #ifdef CRTSTUFFS_O
  293. void *__dso_handle = &__dso_handle;
  294. #else
  295. void *__dso_handle = 0;
  296. #endif
  297. #endif /* DEFAULT_USE_CXA_ATEXIT */
  298. /* The __cxa_finalize function may not be available so we use only a
  299. weak declaration. */
  300. extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK;
  301. /* Run all the global destructors on exit from the program. */
  302. /* Some systems place the number of pointers in the first word of the
  303. table. On SVR4 however, that word is -1. In all cases, the table is
  304. null-terminated. On SVR4, we start from the beginning of the list and
  305. invoke each per-compilation-unit destructor routine in order
  306. until we find that null.
  307. Note that this function MUST be static. There will be one of these
  308. functions in each root executable and one in each shared library, but
  309. although they all have the same code, each one is unique in that it
  310. refers to one particular associated `__DTOR_LIST__' which belongs to the
  311. same particular root executable or shared library file.
  312. On some systems, this routine is run more than once from the .fini,
  313. when exit is called recursively, so we arrange to remember where in
  314. the list we left off processing, and we resume at that point,
  315. should we be re-invoked.
  316. This routine does not need to be run if none of the following clauses are
  317. true, as it will not do anything, so can be removed. */
  318. #if defined(CRTSTUFFS_O) || !defined(FINI_ARRAY_SECTION_ASM_OP) \
  319. || USE_TM_CLONE_REGISTRY || defined(USE_EH_FRAME_REGISTRY)
  320. static void __attribute__((used))
  321. __do_global_dtors_aux (void)
  322. {
  323. static _Bool completed;
  324. if (__builtin_expect (completed, 0))
  325. return;
  326. #if DEFAULT_USE_CXA_ATEXIT
  327. #ifdef CRTSTUFFS_O
  328. if (__cxa_finalize)
  329. __cxa_finalize (__dso_handle);
  330. #endif
  331. #endif
  332. #ifdef FINI_ARRAY_SECTION_ASM_OP
  333. /* If we are using .fini_array then destructors will be run via that
  334. mechanism. */
  335. #elif defined(HIDDEN_DTOR_LIST_END)
  336. {
  337. /* Safer version that makes sure only .dtors function pointers are
  338. called even if the static variable is maliciously changed. */
  339. extern func_ptr __DTOR_END__[] __attribute__((visibility ("hidden")));
  340. static size_t dtor_idx;
  341. const size_t max_idx = __DTOR_END__ - __DTOR_LIST__ - 1;
  342. func_ptr *dtor_list;
  343. __asm ("" : "=g" (dtor_list) : "0" (__DTOR_LIST__));
  344. while (dtor_idx < max_idx)
  345. dtor_list[++dtor_idx] ();
  346. }
  347. #else /* !defined (FINI_ARRAY_SECTION_ASM_OP) */
  348. {
  349. static func_ptr *p = __DTOR_LIST__ + 1;
  350. func_ptr f;
  351. while ((f = *p))
  352. {
  353. p++;
  354. f ();
  355. }
  356. }
  357. #endif /* !defined(FINI_ARRAY_SECTION_ASM_OP) */
  358. #if USE_TM_CLONE_REGISTRY
  359. deregister_tm_clones ();
  360. #endif /* USE_TM_CLONE_REGISTRY */
  361. #ifdef USE_EH_FRAME_REGISTRY
  362. #ifdef CRT_GET_RFIB_DATA
  363. /* If we used the new __register_frame_info_bases interface,
  364. make sure that we deregister from the same place. */
  365. if (__deregister_frame_info_bases)
  366. __deregister_frame_info_bases (__EH_FRAME_BEGIN__);
  367. #else
  368. if (__deregister_frame_info)
  369. __deregister_frame_info (__EH_FRAME_BEGIN__);
  370. #endif
  371. #endif
  372. completed = 1;
  373. }
  374. /* Stick a call to __do_global_dtors_aux into the .fini section. */
  375. #ifdef FINI_SECTION_ASM_OP
  376. CRT_CALL_STATIC_FUNCTION (FINI_SECTION_ASM_OP, __do_global_dtors_aux)
  377. #elif defined (FINI_ARRAY_SECTION_ASM_OP)
  378. #if defined(__FDPIC__)
  379. __asm__("\t.equ\t__do_global_dtors_aux_alias, __do_global_dtors_aux\n");
  380. extern char __do_global_dtors_aux_alias;
  381. static void *__do_global_dtors_aux_fini_array_entry[]
  382. __attribute__ ((__used__, section(".fini_array"), aligned(sizeof(void *))))
  383. = { &__do_global_dtors_aux_alias };
  384. #else /* defined(__FDPIC__) */
  385. static func_ptr __do_global_dtors_aux_fini_array_entry[]
  386. __attribute__ ((__used__, section(".fini_array"),
  387. aligned(__alignof__(func_ptr)))) = { __do_global_dtors_aux };
  388. #endif /* defined(__FDPIC__) */
  389. #else /* !FINI_SECTION_ASM_OP && !FINI_ARRAY_SECTION_ASM_OP */
  390. static void __attribute__((used))
  391. __do_global_dtors_aux_1 (void)
  392. {
  393. atexit (__do_global_dtors_aux);
  394. }
  395. CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__,
  396. __do_global_dtors_aux_1)
  397. #endif
  398. #endif /* defined(CRTSTUFFS_O) || !defined(FINI_ARRAY_SECTION_ASM_OP)
  399. || USE_TM_CLONE_REGISTRY || defined(USE_EH_FRAME_REGISTRY) */
  400. #if defined(USE_EH_FRAME_REGISTRY) || USE_TM_CLONE_REGISTRY
  401. /* Stick a call to __register_frame_info into the .init section. For some
  402. reason calls with no arguments work more reliably in .init, so stick the
  403. call in another function. */
  404. static void __attribute__((used))
  405. frame_dummy (void)
  406. {
  407. #ifdef USE_EH_FRAME_REGISTRY
  408. static struct object object;
  409. #ifdef CRT_GET_RFIB_DATA
  410. void *tbase, *dbase;
  411. tbase = 0;
  412. CRT_GET_RFIB_DATA (dbase);
  413. if (__register_frame_info_bases)
  414. __register_frame_info_bases (__EH_FRAME_BEGIN__, &object, tbase, dbase);
  415. #else
  416. if (__register_frame_info)
  417. __register_frame_info (__EH_FRAME_BEGIN__, &object);
  418. #endif /* CRT_GET_RFIB_DATA */
  419. #endif /* USE_EH_FRAME_REGISTRY */
  420. #if USE_TM_CLONE_REGISTRY
  421. register_tm_clones ();
  422. #endif /* USE_TM_CLONE_REGISTRY */
  423. }
  424. #ifdef __LIBGCC_INIT_SECTION_ASM_OP__
  425. CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__, frame_dummy)
  426. #else /* defined(__LIBGCC_INIT_SECTION_ASM_OP__) */
  427. #if defined(__FDPIC__)
  428. __asm__("\t.equ\t__frame_dummy_alias, frame_dummy\n");
  429. extern char __frame_dummy_alias;
  430. static void *__frame_dummy_init_array_entry[]
  431. __attribute__ ((__used__, section(".init_array"), aligned(sizeof(void *))))
  432. = { &__frame_dummy_alias };
  433. #else /* defined(__FDPIC__) */
  434. static func_ptr __frame_dummy_init_array_entry[]
  435. __attribute__ ((__used__, section(".init_array"),
  436. aligned(__alignof__(func_ptr)))) = { frame_dummy };
  437. #endif /* defined(__FDPIC__) */
  438. #endif /* !defined(__LIBGCC_INIT_SECTION_ASM_OP__) */
  439. #endif /* USE_EH_FRAME_REGISTRY || USE_TM_CLONE_REGISTRY */
  440. #else /* OBJECT_FORMAT_ELF */
  441. /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
  442. and once in crtend.o). It must be declared static to avoid a link
  443. error. Here, we define __do_global_ctors as an externally callable
  444. function. It is externally callable so that __main can invoke it when
  445. INVOKE__main is defined. This has the additional effect of forcing cc1
  446. to switch to the .text section. */
  447. static void __do_global_ctors_aux (void);
  448. void
  449. __do_global_ctors (void)
  450. {
  451. #ifdef INVOKE__main
  452. /* If __main won't actually call __do_global_ctors then it doesn't matter
  453. what's inside the function. The inside of __do_global_ctors_aux is
  454. called automatically in that case. And the Alliant fx2800 linker
  455. crashes on this reference. So prevent the crash. */
  456. __do_global_ctors_aux ();
  457. #endif
  458. }
  459. asm (__LIBGCC_INIT_SECTION_ASM_OP__); /* cc1 doesn't know that we are switching! */
  460. /* A routine to invoke all of the global constructors upon entry to the
  461. program. We put this into the .init section (for systems that have
  462. such a thing) so that we can properly perform the construction of
  463. file-scope static-storage C++ objects within shared libraries. */
  464. static void __attribute__((used))
  465. __do_global_ctors_aux (void) /* prologue goes in .init section */
  466. {
  467. FORCE_CODE_SECTION_ALIGN /* explicit align before switch to .text */
  468. asm (__LIBGCC_TEXT_SECTION_ASM_OP__); /* don't put epilogue and body in .init */
  469. DO_GLOBAL_CTORS_BODY;
  470. atexit (__do_global_dtors);
  471. }
  472. #endif /* OBJECT_FORMAT_ELF */
  473. #elif defined(HAS_INIT_SECTION) /* ! __LIBGCC_INIT_SECTION_ASM_OP__ */
  474. extern void __do_global_dtors (void);
  475. /* This case is used by the Irix 6 port, which supports named sections but
  476. not an SVR4-style .fini section. __do_global_dtors can be non-static
  477. in this case because we protect it with -hidden_symbol. */
  478. void
  479. __do_global_dtors (void)
  480. {
  481. func_ptr *p, f;
  482. for (p = __DTOR_LIST__ + 1; (f = *p); p++)
  483. f ();
  484. #if USE_TM_CLONE_REGISTRY
  485. deregister_tm_clones ();
  486. #endif /* USE_TM_CLONE_REGISTRY */
  487. #ifdef USE_EH_FRAME_REGISTRY
  488. if (__deregister_frame_info)
  489. __deregister_frame_info (__EH_FRAME_BEGIN__);
  490. #endif
  491. }
  492. #if defined(USE_EH_FRAME_REGISTRY) || USE_TM_CLONE_REGISTRY
  493. /* A helper function for __do_global_ctors, which is in crtend.o. Here
  494. in crtbegin.o, we can reference a couple of symbols not visible there.
  495. Plus, since we're before libgcc.a, we have no problems referencing
  496. functions from there. */
  497. void
  498. __do_global_ctors_1(void)
  499. {
  500. #ifdef USE_EH_FRAME_REGISTRY
  501. static struct object object;
  502. if (__register_frame_info)
  503. __register_frame_info (__EH_FRAME_BEGIN__, &object);
  504. #endif
  505. #if USE_TM_CLONE_REGISTRY
  506. register_tm_clones ();
  507. #endif /* USE_TM_CLONE_REGISTRY */
  508. }
  509. #endif /* USE_EH_FRAME_REGISTRY || USE_TM_CLONE_REGISTRY */
  510. #else /* ! __LIBGCC_INIT_SECTION_ASM_OP__ && ! HAS_INIT_SECTION */
  511. #error "What are you doing with crtstuff.c, then?"
  512. #endif
  513. #elif defined(CRT_END) /* ! CRT_BEGIN */
  514. /* No need for .ctors/.dtors section if linker can place them in
  515. .init_array/.fini_array section. */
  516. #ifndef USE_INITFINI_ARRAY
  517. /* Put a word containing zero at the end of each of our two lists of function
  518. addresses. Note that the words defined here go into the .ctors and .dtors
  519. sections of the crtend.o file, and since that file is always linked in
  520. last, these words naturally end up at the very ends of the two lists
  521. contained in these two sections. */
  522. #ifdef CTOR_LIST_END
  523. CTOR_LIST_END;
  524. #elif defined(__LIBGCC_CTORS_SECTION_ASM_OP__)
  525. /* Hack: force cc1 to switch to .data section early, so that assembling
  526. __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */
  527. static func_ptr force_to_data[1] __attribute__ ((__used__)) = { };
  528. asm (__LIBGCC_CTORS_SECTION_ASM_OP__);
  529. STATIC func_ptr __CTOR_END__[1]
  530. __attribute__((aligned(__alignof__(func_ptr))))
  531. = { (func_ptr) 0 };
  532. #else
  533. STATIC func_ptr __CTOR_END__[1]
  534. __attribute__((section(".ctors"), aligned(__alignof__(func_ptr))))
  535. = { (func_ptr) 0 };
  536. #endif
  537. #ifdef DTOR_LIST_END
  538. DTOR_LIST_END;
  539. #elif defined(HIDDEN_DTOR_LIST_END)
  540. #ifdef __LIBGCC_DTORS_SECTION_ASM_OP__
  541. asm (__LIBGCC_DTORS_SECTION_ASM_OP__);
  542. #endif
  543. func_ptr __DTOR_END__[1]
  544. __attribute__ ((used,
  545. #ifndef __LIBGCC_DTORS_SECTION_ASM_OP__
  546. section(".dtors"),
  547. #endif
  548. aligned(__alignof__(func_ptr)), visibility ("hidden")))
  549. = { (func_ptr) 0 };
  550. #elif defined(__LIBGCC_DTORS_SECTION_ASM_OP__)
  551. asm (__LIBGCC_DTORS_SECTION_ASM_OP__);
  552. STATIC func_ptr __DTOR_END__[1]
  553. __attribute__ ((used, aligned(__alignof__(func_ptr))))
  554. = { (func_ptr) 0 };
  555. #else
  556. STATIC func_ptr __DTOR_END__[1]
  557. __attribute__((used, section(".dtors"), aligned(__alignof__(func_ptr))))
  558. = { (func_ptr) 0 };
  559. #endif
  560. #endif /* USE_INITFINI_ARRAY */
  561. #ifdef __LIBGCC_EH_FRAME_SECTION_NAME__
  562. /* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
  563. this would be the 'length' field in a real FDE. */
  564. # if __INT_MAX__ == 2147483647
  565. typedef int int32;
  566. # elif __LONG_MAX__ == 2147483647
  567. typedef long int32;
  568. # elif __SHRT_MAX__ == 2147483647
  569. typedef short int32;
  570. # else
  571. # error "Missing a 4 byte integer"
  572. # endif
  573. STATIC EH_FRAME_SECTION_CONST int32 __FRAME_END__[]
  574. __attribute__ ((used, section(__LIBGCC_EH_FRAME_SECTION_NAME__),
  575. aligned(__alignof__(int32))))
  576. = { 0 };
  577. #endif /* __LIBGCC_EH_FRAME_SECTION_NAME__ */
  578. #if USE_TM_CLONE_REGISTRY
  579. # ifndef HAVE_GAS_HIDDEN
  580. static
  581. # endif
  582. func_ptr __TMC_END__[]
  583. __attribute__((used, section(".tm_clone_table"),
  584. aligned(__alignof__(void *))))
  585. # ifdef HAVE_GAS_HIDDEN
  586. __attribute__((__visibility__ ("hidden"))) = { };
  587. # else
  588. = { 0, 0 };
  589. # endif
  590. #endif /* USE_TM_CLONE_REGISTRY */
  591. #ifdef __LIBGCC_INIT_ARRAY_SECTION_ASM_OP__
  592. /* If we are using .init_array, there is nothing to do. */
  593. #elif defined(__LIBGCC_INIT_SECTION_ASM_OP__)
  594. #ifdef OBJECT_FORMAT_ELF
  595. static void __attribute__((used))
  596. __do_global_ctors_aux (void)
  597. {
  598. func_ptr *p;
  599. for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
  600. (*p) ();
  601. }
  602. /* Stick a call to __do_global_ctors_aux into the .init section. */
  603. CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__, __do_global_ctors_aux)
  604. #else /* OBJECT_FORMAT_ELF */
  605. /* Stick the real initialization code, followed by a normal sort of
  606. function epilogue at the very end of the .init section for this
  607. entire root executable file or for this entire shared library file.
  608. Note that we use some tricks here to get *just* the body and just
  609. a function epilogue (but no function prologue) into the .init
  610. section of the crtend.o file. Specifically, we switch to the .text
  611. section, start to define a function, and then we switch to the .init
  612. section just before the body code.
  613. Earlier on, we put the corresponding function prologue into the .init
  614. section of the crtbegin.o file (which will be linked in first).
  615. Note that we want to invoke all constructors for C++ file-scope static-
  616. storage objects AFTER any other possible initialization actions which
  617. may be performed by the code in the .init section contributions made by
  618. other libraries, etc. That's because those other initializations may
  619. include setup operations for very primitive things (e.g. initializing
  620. the state of the floating-point coprocessor, etc.) which should be done
  621. before we start to execute any of the user's code. */
  622. static void
  623. __do_global_ctors_aux (void) /* prologue goes in .text section */
  624. {
  625. asm (__LIBGCC_INIT_SECTION_ASM_OP__);
  626. DO_GLOBAL_CTORS_BODY;
  627. atexit (__do_global_dtors);
  628. } /* epilogue and body go in .init section */
  629. FORCE_CODE_SECTION_ALIGN
  630. asm (__LIBGCC_TEXT_SECTION_ASM_OP__);
  631. #endif /* OBJECT_FORMAT_ELF */
  632. #elif defined(HAS_INIT_SECTION) /* ! __LIBGCC_INIT_SECTION_ASM_OP__ */
  633. extern void __do_global_ctors (void);
  634. /* This case is used by the Irix 6 port, which supports named sections but
  635. not an SVR4-style .init section. __do_global_ctors can be non-static
  636. in this case because we protect it with -hidden_symbol. */
  637. void
  638. __do_global_ctors (void)
  639. {
  640. func_ptr *p;
  641. #if defined(USE_EH_FRAME_REGISTRY) || USE_TM_CLONE_REGISTRY
  642. __do_global_ctors_1();
  643. #endif
  644. for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
  645. (*p) ();
  646. }
  647. #else /* ! __LIBGCC_INIT_SECTION_ASM_OP__ && ! HAS_INIT_SECTION */
  648. #error "What are you doing with crtstuff.c, then?"
  649. #endif
  650. #else /* ! CRT_BEGIN && ! CRT_END */
  651. #error "One of CRT_BEGIN or CRT_END must be defined."
  652. #endif