crtstuff.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /* Specialized bits of code needed to support construction and
  2. destruction of file-scope objects in C++ code.
  3. Copyright (C) 1991-2015 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__))
  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. #endif
  135. /* We do not want to add the weak attribute to the declarations of these
  136. routines in unwind-dw2-fde.h because that will cause the definition of
  137. these symbols to be weak as well.
  138. This exposes a core issue, how to handle creating weak references vs
  139. how to create weak definitions. Either we have to have the definition
  140. of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or
  141. have a second declaration if we want a function's references to be weak,
  142. but not its definition.
  143. Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until
  144. one thinks about scaling to larger problems -- i.e., the condition under
  145. which TARGET_WEAK_ATTRIBUTE is active will eventually get far too
  146. complicated.
  147. So, we take an approach similar to #pragma weak -- we have a second
  148. declaration for functions that we want to have weak references.
  149. Neither way is particularly good. */
  150. /* References to __register_frame_info and __deregister_frame_info should
  151. be weak in this file if at all possible. */
  152. extern void __register_frame_info (const void *, struct object *)
  153. TARGET_ATTRIBUTE_WEAK;
  154. extern void __register_frame_info_bases (const void *, struct object *,
  155. void *, void *)
  156. TARGET_ATTRIBUTE_WEAK;
  157. extern void *__deregister_frame_info (const void *)
  158. TARGET_ATTRIBUTE_WEAK;
  159. extern void *__deregister_frame_info_bases (const void *)
  160. TARGET_ATTRIBUTE_WEAK;
  161. extern void __do_global_ctors_1 (void);
  162. /* Likewise for _Jv_RegisterClasses. */
  163. extern void _Jv_RegisterClasses (void *) TARGET_ATTRIBUTE_WEAK;
  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(sizeof(func_ptr))))
  201. = { (func_ptr) (-1) };
  202. #else
  203. STATIC func_ptr __CTOR_LIST__[1]
  204. __attribute__ ((__used__, section(".ctors"), aligned(sizeof(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(sizeof(func_ptr))))
  213. = { (func_ptr) (-1) };
  214. #else
  215. STATIC func_ptr __DTOR_LIST__[1]
  216. __attribute__((section(".dtors"), aligned(sizeof(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. #ifdef __LIBGCC_JCR_SECTION_NAME__
  228. /* Stick a label at the beginning of the java class registration info
  229. so we can register them properly. */
  230. STATIC void *__JCR_LIST__[]
  231. __attribute__ ((used, section(__LIBGCC_JCR_SECTION_NAME__),
  232. aligned(sizeof(void*))))
  233. = { };
  234. #endif /* __LIBGCC_JCR_SECTION_NAME__ */
  235. #if USE_TM_CLONE_REGISTRY
  236. STATIC func_ptr __TMC_LIST__[]
  237. __attribute__((used, section(".tm_clone_table"), aligned(sizeof(void*))))
  238. = { };
  239. # ifdef HAVE_GAS_HIDDEN
  240. extern func_ptr __TMC_END__[] __attribute__((__visibility__ ("hidden")));
  241. # endif
  242. static inline void
  243. deregister_tm_clones (void)
  244. {
  245. void (*fn) (void *);
  246. #ifdef HAVE_GAS_HIDDEN
  247. if (__TMC_END__ - __TMC_LIST__ == 0)
  248. return;
  249. #else
  250. if (__TMC_LIST__[0] == NULL)
  251. return;
  252. #endif
  253. fn = _ITM_deregisterTMCloneTable;
  254. __asm ("" : "+r" (fn));
  255. if (fn)
  256. fn (__TMC_LIST__);
  257. }
  258. static inline void
  259. register_tm_clones (void)
  260. {
  261. void (*fn) (void *, size_t);
  262. size_t size;
  263. #ifdef HAVE_GAS_HIDDEN
  264. size = (__TMC_END__ - __TMC_LIST__) / 2;
  265. #else
  266. for (size = 0; __TMC_LIST__[size * 2] != NULL; size++)
  267. continue;
  268. #endif
  269. if (size == 0)
  270. return;
  271. fn = _ITM_registerTMCloneTable;
  272. __asm ("" : "+r" (fn));
  273. if (fn)
  274. fn (__TMC_LIST__, size);
  275. }
  276. #endif /* USE_TM_CLONE_REGISTRY */
  277. #if defined(__LIBGCC_INIT_SECTION_ASM_OP__) \
  278. || defined(__LIBGCC_INIT_ARRAY_SECTION_ASM_OP__)
  279. #ifdef OBJECT_FORMAT_ELF
  280. /* Declare the __dso_handle variable. It should have a unique value
  281. in every shared-object; in a main program its value is zero. The
  282. object should in any case be protected. This means the instance
  283. in one DSO or the main program is not used in another object. The
  284. dynamic linker takes care of this. */
  285. #ifdef TARGET_LIBGCC_SDATA_SECTION
  286. extern void *__dso_handle __attribute__ ((__section__ (TARGET_LIBGCC_SDATA_SECTION)));
  287. #endif
  288. #ifdef HAVE_GAS_HIDDEN
  289. extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
  290. #endif
  291. #ifdef CRTSTUFFS_O
  292. void *__dso_handle = &__dso_handle;
  293. #else
  294. void *__dso_handle = 0;
  295. #endif
  296. /* The __cxa_finalize function may not be available so we use only a
  297. weak declaration. */
  298. extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK;
  299. /* Run all the global destructors on exit from the program. */
  300. /* Some systems place the number of pointers in the first word of the
  301. table. On SVR4 however, that word is -1. In all cases, the table is
  302. null-terminated. On SVR4, we start from the beginning of the list and
  303. invoke each per-compilation-unit destructor routine in order
  304. until we find that null.
  305. Note that this function MUST be static. There will be one of these
  306. functions in each root executable and one in each shared library, but
  307. although they all have the same code, each one is unique in that it
  308. refers to one particular associated `__DTOR_LIST__' which belongs to the
  309. same particular root executable or shared library file.
  310. On some systems, this routine is run more than once from the .fini,
  311. when exit is called recursively, so we arrange to remember where in
  312. the list we left off processing, and we resume at that point,
  313. should we be re-invoked. */
  314. static void __attribute__((used))
  315. __do_global_dtors_aux (void)
  316. {
  317. static _Bool completed;
  318. if (__builtin_expect (completed, 0))
  319. return;
  320. #ifdef CRTSTUFFS_O
  321. if (__cxa_finalize)
  322. __cxa_finalize (__dso_handle);
  323. #endif
  324. #ifdef FINI_ARRAY_SECTION_ASM_OP
  325. /* If we are using .fini_array then destructors will be run via that
  326. mechanism. */
  327. #elif defined(HIDDEN_DTOR_LIST_END)
  328. {
  329. /* Safer version that makes sure only .dtors function pointers are
  330. called even if the static variable is maliciously changed. */
  331. extern func_ptr __DTOR_END__[] __attribute__((visibility ("hidden")));
  332. static size_t dtor_idx;
  333. const size_t max_idx = __DTOR_END__ - __DTOR_LIST__ - 1;
  334. func_ptr *dtor_list;
  335. __asm ("" : "=g" (dtor_list) : "0" (__DTOR_LIST__));
  336. while (dtor_idx < max_idx)
  337. dtor_list[++dtor_idx] ();
  338. }
  339. #else /* !defined (FINI_ARRAY_SECTION_ASM_OP) */
  340. {
  341. static func_ptr *p = __DTOR_LIST__ + 1;
  342. func_ptr f;
  343. while ((f = *p))
  344. {
  345. p++;
  346. f ();
  347. }
  348. }
  349. #endif /* !defined(FINI_ARRAY_SECTION_ASM_OP) */
  350. #if USE_TM_CLONE_REGISTRY
  351. deregister_tm_clones ();
  352. #endif /* USE_TM_CLONE_REGISTRY */
  353. #ifdef USE_EH_FRAME_REGISTRY
  354. #ifdef CRT_GET_RFIB_DATA
  355. /* If we used the new __register_frame_info_bases interface,
  356. make sure that we deregister from the same place. */
  357. if (__deregister_frame_info_bases)
  358. __deregister_frame_info_bases (__EH_FRAME_BEGIN__);
  359. #else
  360. if (__deregister_frame_info)
  361. __deregister_frame_info (__EH_FRAME_BEGIN__);
  362. #endif
  363. #endif
  364. completed = 1;
  365. }
  366. /* Stick a call to __do_global_dtors_aux into the .fini section. */
  367. #ifdef FINI_SECTION_ASM_OP
  368. CRT_CALL_STATIC_FUNCTION (FINI_SECTION_ASM_OP, __do_global_dtors_aux)
  369. #elif defined (FINI_ARRAY_SECTION_ASM_OP)
  370. static func_ptr __do_global_dtors_aux_fini_array_entry[]
  371. __attribute__ ((__used__, section(".fini_array"), aligned(sizeof(func_ptr))))
  372. = { __do_global_dtors_aux };
  373. #else /* !FINI_SECTION_ASM_OP && !FINI_ARRAY_SECTION_ASM_OP */
  374. static void __attribute__((used))
  375. __do_global_dtors_aux_1 (void)
  376. {
  377. atexit (__do_global_dtors_aux);
  378. }
  379. CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__,
  380. __do_global_dtors_aux_1)
  381. #endif
  382. #if defined(USE_EH_FRAME_REGISTRY) \
  383. || defined(__LIBGCC_JCR_SECTION_NAME__) \
  384. || defined(USE_TM_CLONE_REGISTRY)
  385. /* Stick a call to __register_frame_info into the .init section. For some
  386. reason calls with no arguments work more reliably in .init, so stick the
  387. call in another function. */
  388. static void __attribute__((used))
  389. frame_dummy (void)
  390. {
  391. #ifdef USE_EH_FRAME_REGISTRY
  392. static struct object object;
  393. #ifdef CRT_GET_RFIB_DATA
  394. void *tbase, *dbase;
  395. tbase = 0;
  396. CRT_GET_RFIB_DATA (dbase);
  397. if (__register_frame_info_bases)
  398. __register_frame_info_bases (__EH_FRAME_BEGIN__, &object, tbase, dbase);
  399. #else
  400. if (__register_frame_info)
  401. __register_frame_info (__EH_FRAME_BEGIN__, &object);
  402. #endif /* CRT_GET_RFIB_DATA */
  403. #endif /* USE_EH_FRAME_REGISTRY */
  404. #ifdef __LIBGCC_JCR_SECTION_NAME__
  405. void **jcr_list;
  406. __asm ("" : "=g" (jcr_list) : "0" (__JCR_LIST__));
  407. if (__builtin_expect (*jcr_list != NULL, 0))
  408. {
  409. void (*register_classes) (void *) = _Jv_RegisterClasses;
  410. __asm ("" : "+r" (register_classes));
  411. if (register_classes)
  412. register_classes (jcr_list);
  413. }
  414. #endif /* __LIBGCC_JCR_SECTION_NAME__ */
  415. #if USE_TM_CLONE_REGISTRY
  416. register_tm_clones ();
  417. #endif /* USE_TM_CLONE_REGISTRY */
  418. }
  419. #ifdef __LIBGCC_INIT_SECTION_ASM_OP__
  420. CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__, frame_dummy)
  421. #else /* defined(__LIBGCC_INIT_SECTION_ASM_OP__) */
  422. static func_ptr __frame_dummy_init_array_entry[]
  423. __attribute__ ((__used__, section(".init_array"), aligned(sizeof(func_ptr))))
  424. = { frame_dummy };
  425. #endif /* !defined(__LIBGCC_INIT_SECTION_ASM_OP__) */
  426. #endif /* USE_EH_FRAME_REGISTRY || __LIBGCC_JCR_SECTION_NAME__ || USE_TM_CLONE_REGISTRY */
  427. #else /* OBJECT_FORMAT_ELF */
  428. /* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o
  429. and once in crtend.o). It must be declared static to avoid a link
  430. error. Here, we define __do_global_ctors as an externally callable
  431. function. It is externally callable so that __main can invoke it when
  432. INVOKE__main is defined. This has the additional effect of forcing cc1
  433. to switch to the .text section. */
  434. static void __do_global_ctors_aux (void);
  435. void
  436. __do_global_ctors (void)
  437. {
  438. #ifdef INVOKE__main
  439. /* If __main won't actually call __do_global_ctors then it doesn't matter
  440. what's inside the function. The inside of __do_global_ctors_aux is
  441. called automatically in that case. And the Alliant fx2800 linker
  442. crashes on this reference. So prevent the crash. */
  443. __do_global_ctors_aux ();
  444. #endif
  445. }
  446. asm (__LIBGCC_INIT_SECTION_ASM_OP__); /* cc1 doesn't know that we are switching! */
  447. /* A routine to invoke all of the global constructors upon entry to the
  448. program. We put this into the .init section (for systems that have
  449. such a thing) so that we can properly perform the construction of
  450. file-scope static-storage C++ objects within shared libraries. */
  451. static void __attribute__((used))
  452. __do_global_ctors_aux (void) /* prologue goes in .init section */
  453. {
  454. FORCE_CODE_SECTION_ALIGN /* explicit align before switch to .text */
  455. asm (__LIBGCC_TEXT_SECTION_ASM_OP__); /* don't put epilogue and body in .init */
  456. DO_GLOBAL_CTORS_BODY;
  457. atexit (__do_global_dtors);
  458. }
  459. #endif /* OBJECT_FORMAT_ELF */
  460. #elif defined(HAS_INIT_SECTION) /* ! __LIBGCC_INIT_SECTION_ASM_OP__ */
  461. extern void __do_global_dtors (void);
  462. /* This case is used by the Irix 6 port, which supports named sections but
  463. not an SVR4-style .fini section. __do_global_dtors can be non-static
  464. in this case because we protect it with -hidden_symbol. */
  465. void
  466. __do_global_dtors (void)
  467. {
  468. func_ptr *p, f;
  469. for (p = __DTOR_LIST__ + 1; (f = *p); p++)
  470. f ();
  471. #if USE_TM_CLONE_REGISTRY
  472. deregister_tm_clones ();
  473. #endif /* USE_TM_CLONE_REGISTRY */
  474. #ifdef USE_EH_FRAME_REGISTRY
  475. if (__deregister_frame_info)
  476. __deregister_frame_info (__EH_FRAME_BEGIN__);
  477. #endif
  478. }
  479. #if defined(USE_EH_FRAME_REGISTRY) \
  480. || defined(__LIBGCC_JCR_SECTION_NAME__) \
  481. || defined(USE_TM_CLONE_REGISTRY)
  482. /* A helper function for __do_global_ctors, which is in crtend.o. Here
  483. in crtbegin.o, we can reference a couple of symbols not visible there.
  484. Plus, since we're before libgcc.a, we have no problems referencing
  485. functions from there. */
  486. void
  487. __do_global_ctors_1(void)
  488. {
  489. #ifdef USE_EH_FRAME_REGISTRY
  490. static struct object object;
  491. if (__register_frame_info)
  492. __register_frame_info (__EH_FRAME_BEGIN__, &object);
  493. #endif
  494. #ifdef __LIBGCC_JCR_SECTION_NAME__
  495. void **jcr_list;
  496. __asm ("" : "=g" (jcr_list) : "0" (__JCR_LIST__));
  497. if (__builtin_expect (*jcr_list != NULL, 0))
  498. {
  499. void (*register_classes) (void *) = _Jv_RegisterClasses;
  500. __asm ("" : "+r" (register_classes));
  501. if (register_classes)
  502. register_classes (jcr_list);
  503. }
  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 || __LIBGCC_JCR_SECTION_NAME__ || 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(sizeof(func_ptr))))
  531. = { (func_ptr) 0 };
  532. #else
  533. STATIC func_ptr __CTOR_END__[1]
  534. __attribute__((section(".ctors"), aligned(sizeof(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(sizeof(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(sizeof(func_ptr))))
  554. = { (func_ptr) 0 };
  555. #else
  556. STATIC func_ptr __DTOR_END__[1]
  557. __attribute__((used, section(".dtors"), aligned(sizeof(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(sizeof(int32))))
  576. = { 0 };
  577. #endif /* __LIBGCC_EH_FRAME_SECTION_NAME__ */
  578. #ifdef __LIBGCC_JCR_SECTION_NAME__
  579. /* Null terminate the .jcr section array. */
  580. STATIC void *__JCR_END__[1]
  581. __attribute__ ((used, section(__LIBGCC_JCR_SECTION_NAME__),
  582. aligned(sizeof(void *))))
  583. = { 0 };
  584. #endif /* __LIBGCC_JCR_SECTION_NAME__ */
  585. #if USE_TM_CLONE_REGISTRY
  586. # ifndef HAVE_GAS_HIDDEN
  587. static
  588. # endif
  589. func_ptr __TMC_END__[]
  590. __attribute__((used, section(".tm_clone_table"), aligned(sizeof(void *))))
  591. # ifdef HAVE_GAS_HIDDEN
  592. __attribute__((__visibility__ ("hidden"))) = { };
  593. # else
  594. = { 0, 0 };
  595. # endif
  596. #endif /* USE_TM_CLONE_REGISTRY */
  597. #ifdef __LIBGCC_INIT_ARRAY_SECTION_ASM_OP__
  598. /* If we are using .init_array, there is nothing to do. */
  599. #elif defined(__LIBGCC_INIT_SECTION_ASM_OP__)
  600. #ifdef OBJECT_FORMAT_ELF
  601. static void __attribute__((used))
  602. __do_global_ctors_aux (void)
  603. {
  604. func_ptr *p;
  605. for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
  606. (*p) ();
  607. }
  608. /* Stick a call to __do_global_ctors_aux into the .init section. */
  609. CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__, __do_global_ctors_aux)
  610. #else /* OBJECT_FORMAT_ELF */
  611. /* Stick the real initialization code, followed by a normal sort of
  612. function epilogue at the very end of the .init section for this
  613. entire root executable file or for this entire shared library file.
  614. Note that we use some tricks here to get *just* the body and just
  615. a function epilogue (but no function prologue) into the .init
  616. section of the crtend.o file. Specifically, we switch to the .text
  617. section, start to define a function, and then we switch to the .init
  618. section just before the body code.
  619. Earlier on, we put the corresponding function prologue into the .init
  620. section of the crtbegin.o file (which will be linked in first).
  621. Note that we want to invoke all constructors for C++ file-scope static-
  622. storage objects AFTER any other possible initialization actions which
  623. may be performed by the code in the .init section contributions made by
  624. other libraries, etc. That's because those other initializations may
  625. include setup operations for very primitive things (e.g. initializing
  626. the state of the floating-point coprocessor, etc.) which should be done
  627. before we start to execute any of the user's code. */
  628. static void
  629. __do_global_ctors_aux (void) /* prologue goes in .text section */
  630. {
  631. asm (__LIBGCC_INIT_SECTION_ASM_OP__);
  632. DO_GLOBAL_CTORS_BODY;
  633. atexit (__do_global_dtors);
  634. } /* epilogue and body go in .init section */
  635. FORCE_CODE_SECTION_ALIGN
  636. asm (__LIBGCC_TEXT_SECTION_ASM_OP__);
  637. #endif /* OBJECT_FORMAT_ELF */
  638. #elif defined(HAS_INIT_SECTION) /* ! __LIBGCC_INIT_SECTION_ASM_OP__ */
  639. extern void __do_global_ctors (void);
  640. /* This case is used by the Irix 6 port, which supports named sections but
  641. not an SVR4-style .init section. __do_global_ctors can be non-static
  642. in this case because we protect it with -hidden_symbol. */
  643. void
  644. __do_global_ctors (void)
  645. {
  646. func_ptr *p;
  647. #if defined(USE_EH_FRAME_REGISTRY) \
  648. || defined(__LIBGCC_JCR_SECTION_NAME__) \
  649. || defined(USE_TM_CLONE_REGISTRY)
  650. __do_global_ctors_1();
  651. #endif
  652. for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
  653. (*p) ();
  654. }
  655. #else /* ! __LIBGCC_INIT_SECTION_ASM_OP__ && ! HAS_INIT_SECTION */
  656. #error "What are you doing with crtstuff.c, then?"
  657. #endif
  658. #else /* ! CRT_BEGIN && ! CRT_END */
  659. #error "One of CRT_BEGIN or CRT_END must be defined."
  660. #endif