crtstuff.c revision 146895
151973Smsmith/* Specialized bits of code needed to support construction and 251973Smsmith destruction of file-scope objects in C++ code. 351973Smsmith Copyright (C) 1991, 1994, 1995, 1996, 1997, 1998, 451973Smsmith 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. 551973Smsmith Contributed by Ron Guilmette (rfg@monkeys.com). 651973Smsmith 751973SmsmithThis file is part of GCC. 851973Smsmith 951973SmsmithGCC is free software; you can redistribute it and/or modify it under 1051973Smsmiththe terms of the GNU General Public License as published by the Free 1151973SmsmithSoftware Foundation; either version 2, or (at your option) any later 1251973Smsmithversion. 1351973Smsmith 1451973SmsmithIn addition to the permissions in the GNU General Public License, the 1551973SmsmithFree Software Foundation gives you unlimited permission to link the 1651973Smsmithcompiled version of this file into combinations with other programs, 1751973Smsmithand to distribute those combinations without any restriction coming 1851973Smsmithfrom the use of this file. (The General Public License restrictions 1951973Smsmithdo apply in other respects; for example, they cover modification of 2051973Smsmiththe file, and distribution when not linked into a combine 2151973Smsmithexecutable.) 2251973Smsmith 2351973SmsmithGCC is distributed in the hope that it will be useful, but WITHOUT ANY 2451973SmsmithWARRANTY; without even the implied warranty of MERCHANTABILITY or 2551973SmsmithFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2651973Smsmithfor more details. 2751973Smsmith 2851973SmsmithYou should have received a copy of the GNU General Public License 2952544Smsmithalong with GCC; see the file COPYING. If not, write to the Free 30138090SscottlSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA 3152544Smsmith02111-1307, USA. */ 3251973Smsmith 3351973Smsmith/* This file is a bit like libgcc2.c in that it is compiled 3451973Smsmith multiple times and yields multiple .o files. 3558188Smsmith 3651973Smsmith This file is useful on target machines where the object file format 3751973Smsmith supports multiple "user-defined" sections (e.g. COFF, ELF, ROSE). On 3851973Smsmith such systems, this file allows us to avoid running collect (or any 3958188Smsmith other such slow and painful kludge). Additionally, if the target 4058188Smsmith system supports a .init section, this file allows us to support the 4158188Smsmith linking of C++ code with a non-C++ main program. 4258188Smsmith 4351973Smsmith Note that if INIT_SECTION_ASM_OP is defined in the tm.h file, then 4451973Smsmith this file *will* make use of the .init section. If that symbol is 4551973Smsmith not defined however, then the .init section will not be used. 4651973Smsmith 4751973Smsmith Currently, only ELF and COFF are supported. It is likely however that 4851973Smsmith ROSE could also be supported, if someone was willing to do the work to 4951973Smsmith make whatever (small?) adaptations are needed. (Some work may be 5058188Smsmith needed on the ROSE assembler and linker also.) 5158188Smsmith 5259136Smsmith This file must be compiled with gcc. */ 5351973Smsmith 5458188Smsmith/* It is incorrect to include config.h here, because this file is being 5558188Smsmith compiled for the target, and hence definitions concerning only the host 5658188Smsmith do not apply. */ 5758188Smsmith 5858188Smsmith/* We include auto-host.h here to get HAVE_GAS_HIDDEN. This is 5951973Smsmith supposedly valid even though this is a "target" file. */ 6051973Smsmith#include "auto-host.h" 6151973Smsmith#include "tconfig.h" 6251973Smsmith#include "tsystem.h" 6351973Smsmith#include "coretypes.h" 6451973Smsmith#include "tm.h" 6554419Smsmith#include "unwind-dw2-fde.h" 6651973Smsmith 6751973Smsmith#ifndef FORCE_CODE_SECTION_ALIGN 6851973Smsmith# define FORCE_CODE_SECTION_ALIGN 6952225Smsmith#endif 7052225Smsmith 7152225Smsmith#ifndef CRT_CALL_STATIC_FUNCTION 7252225Smsmith# define CRT_CALL_STATIC_FUNCTION(SECTION_OP, FUNC) \ 7352225Smsmithstatic void __attribute__((__used__)) \ 7452225Smsmithcall_ ## FUNC (void) \ 7552225Smsmith{ \ 7652225Smsmith asm (SECTION_OP); \ 7758188Smsmith FUNC (); \ 7858188Smsmith FORCE_CODE_SECTION_ALIGN \ 7958188Smsmith asm (TEXT_SECTION_ASM_OP); \ 8052225Smsmith} 81240608Sjhb#endif 82240608Sjhb 83240608Sjhb#if defined(OBJECT_FORMAT_ELF) && defined(HAVE_LD_EH_FRAME_HDR) \ 84240608Sjhb && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \ 85240608Sjhb && defined(__GLIBC__) && __GLIBC__ >= 2 86240608Sjhb#include <link.h> 87240608Sjhb# if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \ 88240608Sjhb || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG))) 89240608Sjhb# define USE_PT_GNU_EH_FRAME 90240608Sjhb# endif 91240608Sjhb#endif 92240608Sjhb#if defined(EH_FRAME_SECTION_NAME) && !defined(USE_PT_GNU_EH_FRAME) 9352225Smsmith# define USE_EH_FRAME_REGISTRY 9452225Smsmith#endif 9558188Smsmith#if defined(EH_FRAME_SECTION_NAME) && defined(HAVE_LD_RO_RW_SECTION_MIXING) 9658188Smsmith# define EH_FRAME_SECTION_CONST const 9752225Smsmith#else 9852225Smsmith# define EH_FRAME_SECTION_CONST 9952225Smsmith#endif 10052225Smsmith 10158188Smsmith/* We do not want to add the weak attribute to the declarations of these 10258188Smsmith routines in unwind-dw2-fde.h because that will cause the definition of 10352225Smsmith these symbols to be weak as well. 10452225Smsmith 10552225Smsmith This exposes a core issue, how to handle creating weak references vs 10652225Smsmith how to create weak definitions. Either we have to have the definition 10758188Smsmith of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or 10858188Smsmith have a second declaration if we want a function's references to be weak, 10952225Smsmith but not its definition. 11052225Smsmith 11152225Smsmith Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until 11252225Smsmith one thinks about scaling to larger problems -- ie, the condition under 11358188Smsmith which TARGET_WEAK_ATTRIBUTE is active will eventually get far too 11458188Smsmith complicated. 11558188Smsmith 11652225Smsmith So, we take an approach similar to #pragma weak -- we have a second 11752225Smsmith declaration for functions that we want to have weak references. 118240608Sjhb 119240608Sjhb Neither way is particularly good. */ 120240608Sjhb 121240608Sjhb/* References to __register_frame_info and __deregister_frame_info should 122240608Sjhb be weak in this file if at all possible. */ 123240608Sjhbextern void __register_frame_info (const void *, struct object *) 124240608Sjhb TARGET_ATTRIBUTE_WEAK; 125240608Sjhbextern void __register_frame_info_bases (const void *, struct object *, 126240608Sjhb void *, void *) 127240608Sjhb TARGET_ATTRIBUTE_WEAK; 128240608Sjhbextern void *__deregister_frame_info (const void *) 129240608Sjhb TARGET_ATTRIBUTE_WEAK; 13052225Smsmithextern void *__deregister_frame_info_bases (const void *) 13152225Smsmith TARGET_ATTRIBUTE_WEAK; 13258188Smsmithextern void __do_global_ctors_1 (void); 13352225Smsmith 13452225Smsmith/* Likewise for _Jv_RegisterClasses. */ 13552225Smsmithextern void _Jv_RegisterClasses (void *) TARGET_ATTRIBUTE_WEAK; 13652225Smsmith 13752225Smsmith#ifdef OBJECT_FORMAT_ELF 13852225Smsmith 13952225Smsmith/* Declare a pointer to void function type. */ 14052225Smsmithtypedef void (*func_ptr) (void); 14152225Smsmith#define STATIC static 14252225Smsmith 14352225Smsmith#else /* OBJECT_FORMAT_ELF */ 14452225Smsmith 14552225Smsmith#include "gbl-ctors.h" 14652225Smsmith 14758188Smsmith#define STATIC 14858188Smsmith 14952225Smsmith#endif /* OBJECT_FORMAT_ELF */ 15054419Smsmith 15154419Smsmith#ifdef CRT_BEGIN 15254419Smsmith 15358188Smsmith/* NOTE: In order to be able to support SVR4 shared libraries, we arrange 15454419Smsmith to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__, 15554419Smsmith __DTOR_END__ } per root executable and also one set of these symbols 15654419Smsmith per shared library. So in any given whole process image, we may have 15754419Smsmith multiple definitions of each of these symbols. In order to prevent 15854419Smsmith these definitions from conflicting with one another, and in order to 15958188Smsmith ensure that the proper lists are used for the initialization/finalization 16058188Smsmith of each individual shared library (respectively), we give these symbols 16158188Smsmith only internal (i.e. `static') linkage, and we also make it a point to 16254419Smsmith refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__ 163240608Sjhb symbol in crtbegin.o, where they are defined. */ 164240608Sjhb 165240608Sjhb/* The -1 is a flag to __do_global_[cd]tors indicating that this table 166240608Sjhb does not start with a count of elements. */ 167240608Sjhb#ifdef CTOR_LIST_BEGIN 168240608SjhbCTOR_LIST_BEGIN; 169240608Sjhb#elif defined(CTORS_SECTION_ASM_OP) 170240608Sjhb/* Hack: force cc1 to switch to .data section early, so that assembling 171240608Sjhb __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */ 172240608Sjhbstatic func_ptr force_to_data[1] __attribute__ ((__unused__)) = { }; 173240608Sjhbasm (CTORS_SECTION_ASM_OP); 174240608SjhbSTATIC func_ptr __CTOR_LIST__[1] 17554419Smsmith __attribute__ ((__unused__, aligned(sizeof(func_ptr)))) 17654419Smsmith = { (func_ptr) (-1) }; 17758188Smsmith#else 17854419SmsmithSTATIC func_ptr __CTOR_LIST__[1] 17954419Smsmith __attribute__ ((__unused__, section(".ctors"), aligned(sizeof(func_ptr)))) 18054419Smsmith = { (func_ptr) (-1) }; 18158188Smsmith#endif /* __CTOR_LIST__ alternatives */ 18254419Smsmith 18354419Smsmith#ifdef DTOR_LIST_BEGIN 18454419SmsmithDTOR_LIST_BEGIN; 18554419Smsmith#elif defined(DTORS_SECTION_ASM_OP) 18654419Smsmithasm (DTORS_SECTION_ASM_OP); 18754419SmsmithSTATIC func_ptr __DTOR_LIST__[1] 18854419Smsmith __attribute__ ((aligned(sizeof(func_ptr)))) 18954419Smsmith = { (func_ptr) (-1) }; 19054419Smsmith#else 19154419SmsmithSTATIC func_ptr __DTOR_LIST__[1] 19258188Smsmith __attribute__((section(".dtors"), aligned(sizeof(func_ptr)))) 19354419Smsmith = { (func_ptr) (-1) }; 19458188Smsmith#endif /* __DTOR_LIST__ alternatives */ 19558188Smsmith 19654419Smsmith#ifdef USE_EH_FRAME_REGISTRY 19751973Smsmith/* Stick a label at the beginning of the frame unwind info so we can register 19851973Smsmith and deregister it with the exception handling library code. */ 19951973SmsmithSTATIC EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[] 20051973Smsmith __attribute__((section(EH_FRAME_SECTION_NAME), aligned(4))) 20151973Smsmith = { }; 20251973Smsmith#endif /* USE_EH_FRAME_REGISTRY */ 203103870Salfred 20451973Smsmith#ifdef JCR_SECTION_NAME 20551973Smsmith/* Stick a label at the beginning of the java class registration info 20651973Smsmith so we can register them properly. */ 20751973SmsmithSTATIC void *__JCR_LIST__[] 20858188Smsmith __attribute__ ((unused, section(JCR_SECTION_NAME), aligned(sizeof(void*)))) 20958188Smsmith = { }; 21058188Smsmith#endif /* JCR_SECTION_NAME */ 21158188Smsmith 21258188Smsmith#ifdef INIT_SECTION_ASM_OP 21358188Smsmith 21458188Smsmith#ifdef OBJECT_FORMAT_ELF 21558188Smsmith 21658188Smsmith/* Declare the __dso_handle variable. It should have a unique value 21758188Smsmith in every shared-object; in a main program its value is zero. The 21858188Smsmith object should in any case be protected. This means the instance 21958188Smsmith in one DSO or the main program is not used in another object. The 22058188Smsmith dynamic linker takes care of this. */ 22158188Smsmith 22258188Smsmith#ifdef HAVE_GAS_HIDDEN 22358188Smsmithextern void *__dso_handle __attribute__ ((__visibility__ ("hidden"))); 22458188Smsmith#endif 22558188Smsmith#ifdef CRTSTUFFS_O 22658188Smsmithvoid *__dso_handle = &__dso_handle; 22758188Smsmith#else 22858188Smsmithvoid *__dso_handle = 0; 22958188Smsmith#endif 23058188Smsmith 23158188Smsmith/* The __cxa_finalize function may not be available so we use only a 23258188Smsmith weak declaration. */ 233103870Salfredextern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK; 234103870Salfred 23558188Smsmith/* Run all the global destructors on exit from the program. */ 23651973Smsmith 23751973Smsmith/* Some systems place the number of pointers in the first word of the 23851973Smsmith table. On SVR4 however, that word is -1. In all cases, the table is 23951973Smsmith null-terminated. On SVR4, we start from the beginning of the list and 24051973Smsmith invoke each per-compilation-unit destructor routine in order 24151973Smsmith until we find that null. 24251973Smsmith 24351973Smsmith Note that this function MUST be static. There will be one of these 24451973Smsmith functions in each root executable and one in each shared library, but 24551973Smsmith although they all have the same code, each one is unique in that it 24651973Smsmith refers to one particular associated `__DTOR_LIST__' which belongs to the 24751973Smsmith same particular root executable or shared library file. 24851973Smsmith 24951973Smsmith On some systems, this routine is run more than once from the .fini, 25051973Smsmith when exit is called recursively, so we arrange to remember where in 25151973Smsmith the list we left off processing, and we resume at that point, 25251973Smsmith should we be re-invoked. */ 25351973Smsmith 25451973Smsmithstatic void __attribute__((used)) 25551973Smsmith__do_global_dtors_aux (void) 25651973Smsmith{ 25751973Smsmith static func_ptr *p = __DTOR_LIST__ + 1; 25851973Smsmith static _Bool completed; 25951973Smsmith func_ptr f; 26051973Smsmith 26151973Smsmith if (__builtin_expect (completed, 0)) 26251973Smsmith return; 26351973Smsmith 264103870Salfred#ifdef CRTSTUFFS_O 265103870Salfred if (__cxa_finalize) 26651973Smsmith __cxa_finalize (__dso_handle); 26751973Smsmith#endif 26851973Smsmith 26951973Smsmith while ((f = *p)) 27051973Smsmith { 27151973Smsmith p++; 27251973Smsmith f (); 27351973Smsmith } 27451973Smsmith 27551973Smsmith#ifdef USE_EH_FRAME_REGISTRY 27651973Smsmith#ifdef CRT_GET_RFIB_DATA 27751973Smsmith /* If we used the new __register_frame_info_bases interface, 27851973Smsmith make sure that we deregister from the same place. */ 27951973Smsmith if (__deregister_frame_info_bases) 28051973Smsmith __deregister_frame_info_bases (__EH_FRAME_BEGIN__); 28151973Smsmith#else 28251973Smsmith if (__deregister_frame_info) 28351973Smsmith __deregister_frame_info (__EH_FRAME_BEGIN__); 28451973Smsmith#endif 28551973Smsmith#endif 28651973Smsmith 28751973Smsmith completed = 1; 28851973Smsmith} 28954979Smsmith 29051973Smsmith/* Stick a call to __do_global_dtors_aux into the .fini section. */ 29151973SmsmithCRT_CALL_STATIC_FUNCTION (FINI_SECTION_ASM_OP, __do_global_dtors_aux) 29251973Smsmith 29351973Smsmith#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME) 29451973Smsmith/* Stick a call to __register_frame_info into the .init section. For some 29551973Smsmith reason calls with no arguments work more reliably in .init, so stick the 29651973Smsmith call in another function. */ 29751973Smsmith 29851973Smsmithstatic void __attribute__((used)) 29951973Smsmithframe_dummy (void) 30051973Smsmith{ 30151973Smsmith#ifdef USE_EH_FRAME_REGISTRY 30251973Smsmith static struct object object; 30351973Smsmith#ifdef CRT_GET_RFIB_DATA 30451973Smsmith void *tbase, *dbase; 30551973Smsmith tbase = 0; 30651973Smsmith CRT_GET_RFIB_DATA (dbase); 30751973Smsmith if (__register_frame_info_bases) 30851973Smsmith __register_frame_info_bases (__EH_FRAME_BEGIN__, &object, tbase, dbase); 30951973Smsmith#else 31054979Smsmith if (__register_frame_info) 31151973Smsmith __register_frame_info (__EH_FRAME_BEGIN__, &object); 31251973Smsmith#endif /* CRT_GET_RFIB_DATA */ 31351973Smsmith#endif /* USE_EH_FRAME_REGISTRY */ 31451973Smsmith#ifdef JCR_SECTION_NAME 315103870Salfred if (__JCR_LIST__[0] && _Jv_RegisterClasses) 31651973Smsmith _Jv_RegisterClasses (__JCR_LIST__); 31751973Smsmith#endif /* JCR_SECTION_NAME */ 31851973Smsmith} 31951973Smsmith 32051973SmsmithCRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, frame_dummy) 32151973Smsmith#endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */ 32251973Smsmith 323103870Salfred#else /* OBJECT_FORMAT_ELF */ 32451973Smsmith 32551973Smsmith/* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o 32651973Smsmith and once in crtend.o). It must be declared static to avoid a link 32751973Smsmith error. Here, we define __do_global_ctors as an externally callable 32851973Smsmith function. It is externally callable so that __main can invoke it when 32951973Smsmith INVOKE__main is defined. This has the additional effect of forcing cc1 33051973Smsmith to switch to the .text section. */ 33151973Smsmith 33251973Smsmithstatic void __do_global_ctors_aux (void); 33351973Smsmithvoid 33451973Smsmith__do_global_ctors (void) 33551973Smsmith{ 33651973Smsmith#ifdef INVOKE__main 33751973Smsmith /* If __main won't actually call __do_global_ctors then it doesn't matter 33851973Smsmith what's inside the function. The inside of __do_global_ctors_aux is 33951973Smsmith called automatically in that case. And the Alliant fx2800 linker 34051973Smsmith crashes on this reference. So prevent the crash. */ 34151973Smsmith __do_global_ctors_aux (); 34251973Smsmith#endif 34351973Smsmith} 34451973Smsmith 34551973Smsmithasm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */ 34651973Smsmith 34751973Smsmith/* A routine to invoke all of the global constructors upon entry to the 348103870Salfred program. We put this into the .init section (for systems that have 34951973Smsmith such a thing) so that we can properly perform the construction of 35051973Smsmith file-scope static-storage C++ objects within shared libraries. */ 35151973Smsmith 35251973Smsmithstatic void __attribute__((used)) 35351973Smsmith__do_global_ctors_aux (void) /* prologue goes in .init section */ 35451973Smsmith{ 35551973Smsmith FORCE_CODE_SECTION_ALIGN /* explicit align before switch to .text */ 35651973Smsmith asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */ 35751973Smsmith DO_GLOBAL_CTORS_BODY; 358103870Salfred atexit (__do_global_dtors); 35951973Smsmith} 36058188Smsmith 36158188Smsmith#endif /* OBJECT_FORMAT_ELF */ 36258188Smsmith 36358188Smsmith#elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */ 36458188Smsmith 36558188Smsmithextern void __do_global_dtors (void); 36658188Smsmith 36758188Smsmith/* This case is used by the Irix 6 port, which supports named sections but 36858188Smsmith not an SVR4-style .fini section. __do_global_dtors can be non-static 36958188Smsmith in this case because we protect it with -hidden_symbol. */ 37058188Smsmith 37158188Smsmithvoid 37258188Smsmith__do_global_dtors (void) 37358188Smsmith{ 37458188Smsmith func_ptr *p, f; 37558188Smsmith for (p = __DTOR_LIST__ + 1; (f = *p); p++) 37658188Smsmith f (); 37758188Smsmith 37858188Smsmith#ifdef USE_EH_FRAME_REGISTRY 37958188Smsmith if (__deregister_frame_info) 38058188Smsmith __deregister_frame_info (__EH_FRAME_BEGIN__); 38158188Smsmith#endif 38258188Smsmith} 38358188Smsmith 38458188Smsmith#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME) 38558188Smsmith/* A helper function for __do_global_ctors, which is in crtend.o. Here 38658188Smsmith in crtbegin.o, we can reference a couple of symbols not visible there. 38758188Smsmith Plus, since we're before libgcc.a, we have no problems referencing 38858188Smsmith functions from there. */ 38958188Smsmithvoid 39058188Smsmith__do_global_ctors_1(void) 39158188Smsmith{ 39258188Smsmith#ifdef USE_EH_FRAME_REGISTRY 39358188Smsmith static struct object object; 39458188Smsmith if (__register_frame_info) 39558188Smsmith __register_frame_info (__EH_FRAME_BEGIN__, &object); 39658188Smsmith#endif 39758188Smsmith#ifdef JCR_SECTION_NAME 39858188Smsmith if (__JCR_LIST__[0] && _Jv_RegisterClasses) 399103870Salfred _Jv_RegisterClasses (__JCR_LIST__); 40058188Smsmith#endif 40158188Smsmith} 40258188Smsmith#endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */ 40358188Smsmith 40458188Smsmith#else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */ 40558188Smsmith#error "What are you doing with crtstuff.c, then?" 406103870Salfred#endif 40758188Smsmith 40858188Smsmith#elif defined(CRT_END) /* ! CRT_BEGIN */ 40958188Smsmith 41058188Smsmith/* Put a word containing zero at the end of each of our two lists of function 41158188Smsmith addresses. Note that the words defined here go into the .ctors and .dtors 41258188Smsmith sections of the crtend.o file, and since that file is always linked in 41358188Smsmith last, these words naturally end up at the very ends of the two lists 41458188Smsmith contained in these two sections. */ 41558188Smsmith 41658188Smsmith#ifdef CTOR_LIST_END 41758188SmsmithCTOR_LIST_END; 41858188Smsmith#elif defined(CTORS_SECTION_ASM_OP) 41958188Smsmith/* Hack: force cc1 to switch to .data section early, so that assembling 42058188Smsmith __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */ 42158188Smsmithstatic func_ptr force_to_data[1] __attribute__ ((__unused__)) = { }; 42258188Smsmithasm (CTORS_SECTION_ASM_OP); 42358188SmsmithSTATIC func_ptr __CTOR_END__[1] 42458188Smsmith __attribute__((aligned(sizeof(func_ptr)))) 42558188Smsmith = { (func_ptr) 0 }; 42658188Smsmith#else 427103870SalfredSTATIC func_ptr __CTOR_END__[1] 42858188Smsmith __attribute__((section(".ctors"), aligned(sizeof(func_ptr)))) 42958188Smsmith = { (func_ptr) 0 }; 43058188Smsmith#endif 43158188Smsmith 43258188Smsmith#ifdef DTOR_LIST_END 43358188SmsmithDTOR_LIST_END; 43458188Smsmith#elif defined(DTORS_SECTION_ASM_OP) 43558188Smsmithasm (DTORS_SECTION_ASM_OP); 43658188SmsmithSTATIC func_ptr __DTOR_END__[1] 43758188Smsmith __attribute__ ((unused, aligned(sizeof(func_ptr)))) 43858188Smsmith = { (func_ptr) 0 }; 43958188Smsmith#else 44058188SmsmithSTATIC func_ptr __DTOR_END__[1] 44158188Smsmith __attribute__((unused, section(".dtors"), aligned(sizeof(func_ptr)))) 44258188Smsmith = { (func_ptr) 0 }; 44358188Smsmith#endif 44458188Smsmith 44558188Smsmith#ifdef EH_FRAME_SECTION_NAME 44658188Smsmith/* Terminate the frame unwind info section with a 4byte 0 as a sentinel; 44758188Smsmith this would be the 'length' field in a real FDE. */ 44858188Smsmith# if __INT_MAX__ == 2147483647 44958188Smsmithtypedef int int32; 45058188Smsmith# elif __LONG_MAX__ == 2147483647 45158188Smsmithtypedef long int32; 452103870Salfred# elif __SHRT_MAX__ == 2147483647 45358188Smsmithtypedef short int32; 45458188Smsmith# else 45558188Smsmith# error "Missing a 4 byte integer" 45658188Smsmith# endif 45758188SmsmithSTATIC EH_FRAME_SECTION_CONST int32 __FRAME_END__[] 45858188Smsmith __attribute__ ((unused, section(EH_FRAME_SECTION_NAME), 45958188Smsmith aligned(sizeof(int32)))) 460103870Salfred = { 0 }; 46158188Smsmith#endif /* EH_FRAME_SECTION_NAME */ 46258188Smsmith 46358188Smsmith#ifdef JCR_SECTION_NAME 46458188Smsmith/* Null terminate the .jcr section array. */ 46558188SmsmithSTATIC void *__JCR_END__[1] 46658188Smsmith __attribute__ ((unused, section(JCR_SECTION_NAME), 46758188Smsmith aligned(sizeof(void *)))) 46858188Smsmith = { 0 }; 46958188Smsmith#endif /* JCR_SECTION_NAME */ 47058188Smsmith 47158188Smsmith#ifdef INIT_SECTION_ASM_OP 47258188Smsmith 47358188Smsmith#ifdef OBJECT_FORMAT_ELF 47458188Smsmithstatic void __attribute__((used)) 47558188Smsmith__do_global_ctors_aux (void) 47658188Smsmith{ 47758188Smsmith func_ptr *p; 47858188Smsmith for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) 47958188Smsmith (*p) (); 48058188Smsmith} 48158188Smsmith 48258188Smsmith/* Stick a call to __do_global_ctors_aux into the .init section. */ 48358188SmsmithCRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, __do_global_ctors_aux) 48458188Smsmith#else /* OBJECT_FORMAT_ELF */ 48558188Smsmith 486103870Salfred/* Stick the real initialization code, followed by a normal sort of 48758188Smsmith function epilogue at the very end of the .init section for this 48859136Smsmith entire root executable file or for this entire shared library file. 48959136Smsmith 49059136Smsmith Note that we use some tricks here to get *just* the body and just 49159136Smsmith a function epilogue (but no function prologue) into the .init 49259136Smsmith section of the crtend.o file. Specifically, we switch to the .text 49359136Smsmith section, start to define a function, and then we switch to the .init 49459136Smsmith section just before the body code. 49559136Smsmith 496103870Salfred Earlier on, we put the corresponding function prologue into the .init 49759136Smsmith section of the crtbegin.o file (which will be linked in first). 49858188Smsmith 49952544Smsmith Note that we want to invoke all constructors for C++ file-scope static- 50052544Smsmith storage objects AFTER any other possible initialization actions which 50152544Smsmith may be performed by the code in the .init section contributions made by 50252544Smsmith other libraries, etc. That's because those other initializations may 50352544Smsmith include setup operations for very primitive things (e.g. initializing 50452544Smsmith the state of the floating-point coprocessor, etc.) which should be done 50552544Smsmith before we start to execute any of the user's code. */ 50652544Smsmith 50752544Smsmithstatic void 50852544Smsmith__do_global_ctors_aux (void) /* prologue goes in .text section */ 50952544Smsmith{ 51052544Smsmith asm (INIT_SECTION_ASM_OP); 51152544Smsmith DO_GLOBAL_CTORS_BODY; 51252544Smsmith atexit (__do_global_dtors); 51352544Smsmith} /* epilogue and body go in .init section */ 51452544Smsmith 51552544SmsmithFORCE_CODE_SECTION_ALIGN 51652544Smsmithasm (TEXT_SECTION_ASM_OP); 51752544Smsmith 51852544Smsmith#endif /* OBJECT_FORMAT_ELF */ 51952544Smsmith 52052544Smsmith#elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */ 52152544Smsmith 52252544Smsmithextern void __do_global_ctors (void); 52352544Smsmith 52452544Smsmith/* This case is used by the Irix 6 port, which supports named sections but 52552544Smsmith not an SVR4-style .init section. __do_global_ctors can be non-static 52652544Smsmith in this case because we protect it with -hidden_symbol. */ 52752544Smsmithvoid 52852544Smsmith__do_global_ctors (void) 52952544Smsmith{ 53052544Smsmith func_ptr *p; 53152544Smsmith#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME) 53252544Smsmith __do_global_ctors_1(); 53352544Smsmith#endif 53452544Smsmith for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) 53552544Smsmith (*p) (); 53652544Smsmith} 53752544Smsmith 53852544Smsmith#else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */ 53952544Smsmith#error "What are you doing with crtstuff.c, then?" 54052544Smsmith#endif 54152544Smsmith 54252544Smsmith#else /* ! CRT_BEGIN && ! CRT_END */ 54352544Smsmith#error "One of CRT_BEGIN or CRT_END must be defined." 54452544Smsmith#endif 54552544Smsmith