crtstuff.c revision 146895
1226584Sdim/* Specialized bits of code needed to support construction and 2226584Sdim destruction of file-scope objects in C++ code. 3226584Sdim Copyright (C) 1991, 1994, 1995, 1996, 1997, 1998, 4226584Sdim 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. 5226584Sdim Contributed by Ron Guilmette (rfg@monkeys.com). 6226584Sdim 7226584SdimThis file is part of GCC. 8226584Sdim 9226584SdimGCC is free software; you can redistribute it and/or modify it under 10226584Sdimthe terms of the GNU General Public License as published by the Free 11226584SdimSoftware Foundation; either version 2, or (at your option) any later 12226584Sdimversion. 13226584Sdim 14226584SdimIn addition to the permissions in the GNU General Public License, the 15226584SdimFree Software Foundation gives you unlimited permission to link the 16226584Sdimcompiled version of this file into combinations with other programs, 17226584Sdimand to distribute those combinations without any restriction coming 18251662Sdimfrom the use of this file. (The General Public License restrictions 19251662Sdimdo apply in other respects; for example, they cover modification of 20251662Sdimthe file, and distribution when not linked into a combine 21226584Sdimexecutable.) 22226584Sdim 23226584SdimGCC is distributed in the hope that it will be useful, but WITHOUT ANY 24226584SdimWARRANTY; without even the implied warranty of MERCHANTABILITY or 25226584SdimFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 26226584Sdimfor more details. 27226584Sdim 28226584SdimYou should have received a copy of the GNU General Public License 29226584Sdimalong with GCC; see the file COPYING. If not, write to the Free 30226584SdimSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA 31226584Sdim02111-1307, USA. */ 32226584Sdim 33276479Sdim/* This file is a bit like libgcc2.c in that it is compiled 34276479Sdim multiple times and yields multiple .o files. 35276479Sdim 36276479Sdim This file is useful on target machines where the object file format 37234353Sdim supports multiple "user-defined" sections (e.g. COFF, ELF, ROSE). On 38234353Sdim such systems, this file allows us to avoid running collect (or any 39234353Sdim other such slow and painful kludge). Additionally, if the target 40234353Sdim system supports a .init section, this file allows us to support the 41234353Sdim linking of C++ code with a non-C++ main program. 42234353Sdim 43234353Sdim Note that if INIT_SECTION_ASM_OP is defined in the tm.h file, then 44234353Sdim this file *will* make use of the .init section. If that symbol is 45234353Sdim not defined however, then the .init section will not be used. 46234353Sdim 47234353Sdim Currently, only ELF and COFF are supported. It is likely however that 48234353Sdim ROSE could also be supported, if someone was willing to do the work to 49234353Sdim make whatever (small?) adaptations are needed. (Some work may be 50234353Sdim needed on the ROSE assembler and linker also.) 51234353Sdim 52234353Sdim This file must be compiled with gcc. */ 53234353Sdim 54234353Sdim/* It is incorrect to include config.h here, because this file is being 55234353Sdim compiled for the target, and hence definitions concerning only the host 56234353Sdim do not apply. */ 57251662Sdim 58251662Sdim/* We include auto-host.h here to get HAVE_GAS_HIDDEN. This is 59251662Sdim supposedly valid even though this is a "target" file. */ 60251662Sdim#include "auto-host.h" 61251662Sdim#include "tconfig.h" 62251662Sdim#include "tsystem.h" 63251662Sdim#include "coretypes.h" 64251662Sdim#include "tm.h" 65251662Sdim#include "unwind-dw2-fde.h" 66251662Sdim 67251662Sdim#ifndef FORCE_CODE_SECTION_ALIGN 68251662Sdim# define FORCE_CODE_SECTION_ALIGN 69251662Sdim#endif 70251662Sdim 71251662Sdim#ifndef CRT_CALL_STATIC_FUNCTION 72251662Sdim# define CRT_CALL_STATIC_FUNCTION(SECTION_OP, FUNC) \ 73251662Sdimstatic void __attribute__((__used__)) \ 74251662Sdimcall_ ## FUNC (void) \ 75251662Sdim{ \ 76251662Sdim asm (SECTION_OP); \ 77251662Sdim FUNC (); \ 78251662Sdim FORCE_CODE_SECTION_ALIGN \ 79251662Sdim asm (TEXT_SECTION_ASM_OP); \ 80251662Sdim} 81251662Sdim#endif 82251662Sdim 83251662Sdim#if defined(OBJECT_FORMAT_ELF) && defined(HAVE_LD_EH_FRAME_HDR) \ 84251662Sdim && !defined(inhibit_libc) && !defined(CRTSTUFFT_O) \ 85251662Sdim && defined(__GLIBC__) && __GLIBC__ >= 2 86251662Sdim#include <link.h> 87251662Sdim# if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \ 88251662Sdim || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG))) 89251662Sdim# define USE_PT_GNU_EH_FRAME 90251662Sdim# endif 91251662Sdim#endif 92251662Sdim#if defined(EH_FRAME_SECTION_NAME) && !defined(USE_PT_GNU_EH_FRAME) 93226584Sdim# define USE_EH_FRAME_REGISTRY 94226584Sdim#endif 95226584Sdim#if defined(EH_FRAME_SECTION_NAME) && defined(HAVE_LD_RO_RW_SECTION_MIXING) 96# define EH_FRAME_SECTION_CONST const 97#else 98# define EH_FRAME_SECTION_CONST 99#endif 100 101/* We do not want to add the weak attribute to the declarations of these 102 routines in unwind-dw2-fde.h because that will cause the definition of 103 these symbols to be weak as well. 104 105 This exposes a core issue, how to handle creating weak references vs 106 how to create weak definitions. Either we have to have the definition 107 of TARGET_WEAK_ATTRIBUTE be conditional in the shared header files or 108 have a second declaration if we want a function's references to be weak, 109 but not its definition. 110 111 Making TARGET_WEAK_ATTRIBUTE conditional seems like a good solution until 112 one thinks about scaling to larger problems -- ie, the condition under 113 which TARGET_WEAK_ATTRIBUTE is active will eventually get far too 114 complicated. 115 116 So, we take an approach similar to #pragma weak -- we have a second 117 declaration for functions that we want to have weak references. 118 119 Neither way is particularly good. */ 120 121/* References to __register_frame_info and __deregister_frame_info should 122 be weak in this file if at all possible. */ 123extern void __register_frame_info (const void *, struct object *) 124 TARGET_ATTRIBUTE_WEAK; 125extern void __register_frame_info_bases (const void *, struct object *, 126 void *, void *) 127 TARGET_ATTRIBUTE_WEAK; 128extern void *__deregister_frame_info (const void *) 129 TARGET_ATTRIBUTE_WEAK; 130extern void *__deregister_frame_info_bases (const void *) 131 TARGET_ATTRIBUTE_WEAK; 132extern void __do_global_ctors_1 (void); 133 134/* Likewise for _Jv_RegisterClasses. */ 135extern void _Jv_RegisterClasses (void *) TARGET_ATTRIBUTE_WEAK; 136 137#ifdef OBJECT_FORMAT_ELF 138 139/* Declare a pointer to void function type. */ 140typedef void (*func_ptr) (void); 141#define STATIC static 142 143#else /* OBJECT_FORMAT_ELF */ 144 145#include "gbl-ctors.h" 146 147#define STATIC 148 149#endif /* OBJECT_FORMAT_ELF */ 150 151#ifdef CRT_BEGIN 152 153/* NOTE: In order to be able to support SVR4 shared libraries, we arrange 154 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__, 155 __DTOR_END__ } per root executable and also one set of these symbols 156 per shared library. So in any given whole process image, we may have 157 multiple definitions of each of these symbols. In order to prevent 158 these definitions from conflicting with one another, and in order to 159 ensure that the proper lists are used for the initialization/finalization 160 of each individual shared library (respectively), we give these symbols 161 only internal (i.e. `static') linkage, and we also make it a point to 162 refer to only the __CTOR_END__ symbol in crtend.o and the __DTOR_LIST__ 163 symbol in crtbegin.o, where they are defined. */ 164 165/* The -1 is a flag to __do_global_[cd]tors indicating that this table 166 does not start with a count of elements. */ 167#ifdef CTOR_LIST_BEGIN 168CTOR_LIST_BEGIN; 169#elif defined(CTORS_SECTION_ASM_OP) 170/* Hack: force cc1 to switch to .data section early, so that assembling 171 __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */ 172static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { }; 173asm (CTORS_SECTION_ASM_OP); 174STATIC func_ptr __CTOR_LIST__[1] 175 __attribute__ ((__unused__, aligned(sizeof(func_ptr)))) 176 = { (func_ptr) (-1) }; 177#else 178STATIC func_ptr __CTOR_LIST__[1] 179 __attribute__ ((__unused__, section(".ctors"), aligned(sizeof(func_ptr)))) 180 = { (func_ptr) (-1) }; 181#endif /* __CTOR_LIST__ alternatives */ 182 183#ifdef DTOR_LIST_BEGIN 184DTOR_LIST_BEGIN; 185#elif defined(DTORS_SECTION_ASM_OP) 186asm (DTORS_SECTION_ASM_OP); 187STATIC func_ptr __DTOR_LIST__[1] 188 __attribute__ ((aligned(sizeof(func_ptr)))) 189 = { (func_ptr) (-1) }; 190#else 191STATIC func_ptr __DTOR_LIST__[1] 192 __attribute__((section(".dtors"), aligned(sizeof(func_ptr)))) 193 = { (func_ptr) (-1) }; 194#endif /* __DTOR_LIST__ alternatives */ 195 196#ifdef USE_EH_FRAME_REGISTRY 197/* Stick a label at the beginning of the frame unwind info so we can register 198 and deregister it with the exception handling library code. */ 199STATIC EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[] 200 __attribute__((section(EH_FRAME_SECTION_NAME), aligned(4))) 201 = { }; 202#endif /* USE_EH_FRAME_REGISTRY */ 203 204#ifdef JCR_SECTION_NAME 205/* Stick a label at the beginning of the java class registration info 206 so we can register them properly. */ 207STATIC void *__JCR_LIST__[] 208 __attribute__ ((unused, section(JCR_SECTION_NAME), aligned(sizeof(void*)))) 209 = { }; 210#endif /* JCR_SECTION_NAME */ 211 212#ifdef INIT_SECTION_ASM_OP 213 214#ifdef OBJECT_FORMAT_ELF 215 216/* Declare the __dso_handle variable. It should have a unique value 217 in every shared-object; in a main program its value is zero. The 218 object should in any case be protected. This means the instance 219 in one DSO or the main program is not used in another object. The 220 dynamic linker takes care of this. */ 221 222#ifdef HAVE_GAS_HIDDEN 223extern void *__dso_handle __attribute__ ((__visibility__ ("hidden"))); 224#endif 225#ifdef CRTSTUFFS_O 226void *__dso_handle = &__dso_handle; 227#else 228void *__dso_handle = 0; 229#endif 230 231/* The __cxa_finalize function may not be available so we use only a 232 weak declaration. */ 233extern void __cxa_finalize (void *) TARGET_ATTRIBUTE_WEAK; 234 235/* Run all the global destructors on exit from the program. */ 236 237/* Some systems place the number of pointers in the first word of the 238 table. On SVR4 however, that word is -1. In all cases, the table is 239 null-terminated. On SVR4, we start from the beginning of the list and 240 invoke each per-compilation-unit destructor routine in order 241 until we find that null. 242 243 Note that this function MUST be static. There will be one of these 244 functions in each root executable and one in each shared library, but 245 although they all have the same code, each one is unique in that it 246 refers to one particular associated `__DTOR_LIST__' which belongs to the 247 same particular root executable or shared library file. 248 249 On some systems, this routine is run more than once from the .fini, 250 when exit is called recursively, so we arrange to remember where in 251 the list we left off processing, and we resume at that point, 252 should we be re-invoked. */ 253 254static void __attribute__((used)) 255__do_global_dtors_aux (void) 256{ 257 static func_ptr *p = __DTOR_LIST__ + 1; 258 static _Bool completed; 259 func_ptr f; 260 261 if (__builtin_expect (completed, 0)) 262 return; 263 264#ifdef CRTSTUFFS_O 265 if (__cxa_finalize) 266 __cxa_finalize (__dso_handle); 267#endif 268 269 while ((f = *p)) 270 { 271 p++; 272 f (); 273 } 274 275#ifdef USE_EH_FRAME_REGISTRY 276#ifdef CRT_GET_RFIB_DATA 277 /* If we used the new __register_frame_info_bases interface, 278 make sure that we deregister from the same place. */ 279 if (__deregister_frame_info_bases) 280 __deregister_frame_info_bases (__EH_FRAME_BEGIN__); 281#else 282 if (__deregister_frame_info) 283 __deregister_frame_info (__EH_FRAME_BEGIN__); 284#endif 285#endif 286 287 completed = 1; 288} 289 290/* Stick a call to __do_global_dtors_aux into the .fini section. */ 291CRT_CALL_STATIC_FUNCTION (FINI_SECTION_ASM_OP, __do_global_dtors_aux) 292 293#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME) 294/* Stick a call to __register_frame_info into the .init section. For some 295 reason calls with no arguments work more reliably in .init, so stick the 296 call in another function. */ 297 298static void __attribute__((used)) 299frame_dummy (void) 300{ 301#ifdef USE_EH_FRAME_REGISTRY 302 static struct object object; 303#ifdef CRT_GET_RFIB_DATA 304 void *tbase, *dbase; 305 tbase = 0; 306 CRT_GET_RFIB_DATA (dbase); 307 if (__register_frame_info_bases) 308 __register_frame_info_bases (__EH_FRAME_BEGIN__, &object, tbase, dbase); 309#else 310 if (__register_frame_info) 311 __register_frame_info (__EH_FRAME_BEGIN__, &object); 312#endif /* CRT_GET_RFIB_DATA */ 313#endif /* USE_EH_FRAME_REGISTRY */ 314#ifdef JCR_SECTION_NAME 315 if (__JCR_LIST__[0] && _Jv_RegisterClasses) 316 _Jv_RegisterClasses (__JCR_LIST__); 317#endif /* JCR_SECTION_NAME */ 318} 319 320CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, frame_dummy) 321#endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */ 322 323#else /* OBJECT_FORMAT_ELF */ 324 325/* The function __do_global_ctors_aux is compiled twice (once in crtbegin.o 326 and once in crtend.o). It must be declared static to avoid a link 327 error. Here, we define __do_global_ctors as an externally callable 328 function. It is externally callable so that __main can invoke it when 329 INVOKE__main is defined. This has the additional effect of forcing cc1 330 to switch to the .text section. */ 331 332static void __do_global_ctors_aux (void); 333void 334__do_global_ctors (void) 335{ 336#ifdef INVOKE__main 337 /* If __main won't actually call __do_global_ctors then it doesn't matter 338 what's inside the function. The inside of __do_global_ctors_aux is 339 called automatically in that case. And the Alliant fx2800 linker 340 crashes on this reference. So prevent the crash. */ 341 __do_global_ctors_aux (); 342#endif 343} 344 345asm (INIT_SECTION_ASM_OP); /* cc1 doesn't know that we are switching! */ 346 347/* A routine to invoke all of the global constructors upon entry to the 348 program. We put this into the .init section (for systems that have 349 such a thing) so that we can properly perform the construction of 350 file-scope static-storage C++ objects within shared libraries. */ 351 352static void __attribute__((used)) 353__do_global_ctors_aux (void) /* prologue goes in .init section */ 354{ 355 FORCE_CODE_SECTION_ALIGN /* explicit align before switch to .text */ 356 asm (TEXT_SECTION_ASM_OP); /* don't put epilogue and body in .init */ 357 DO_GLOBAL_CTORS_BODY; 358 atexit (__do_global_dtors); 359} 360 361#endif /* OBJECT_FORMAT_ELF */ 362 363#elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */ 364 365extern void __do_global_dtors (void); 366 367/* This case is used by the Irix 6 port, which supports named sections but 368 not an SVR4-style .fini section. __do_global_dtors can be non-static 369 in this case because we protect it with -hidden_symbol. */ 370 371void 372__do_global_dtors (void) 373{ 374 func_ptr *p, f; 375 for (p = __DTOR_LIST__ + 1; (f = *p); p++) 376 f (); 377 378#ifdef USE_EH_FRAME_REGISTRY 379 if (__deregister_frame_info) 380 __deregister_frame_info (__EH_FRAME_BEGIN__); 381#endif 382} 383 384#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME) 385/* A helper function for __do_global_ctors, which is in crtend.o. Here 386 in crtbegin.o, we can reference a couple of symbols not visible there. 387 Plus, since we're before libgcc.a, we have no problems referencing 388 functions from there. */ 389void 390__do_global_ctors_1(void) 391{ 392#ifdef USE_EH_FRAME_REGISTRY 393 static struct object object; 394 if (__register_frame_info) 395 __register_frame_info (__EH_FRAME_BEGIN__, &object); 396#endif 397#ifdef JCR_SECTION_NAME 398 if (__JCR_LIST__[0] && _Jv_RegisterClasses) 399 _Jv_RegisterClasses (__JCR_LIST__); 400#endif 401} 402#endif /* USE_EH_FRAME_REGISTRY || JCR_SECTION_NAME */ 403 404#else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */ 405#error "What are you doing with crtstuff.c, then?" 406#endif 407 408#elif defined(CRT_END) /* ! CRT_BEGIN */ 409 410/* Put a word containing zero at the end of each of our two lists of function 411 addresses. Note that the words defined here go into the .ctors and .dtors 412 sections of the crtend.o file, and since that file is always linked in 413 last, these words naturally end up at the very ends of the two lists 414 contained in these two sections. */ 415 416#ifdef CTOR_LIST_END 417CTOR_LIST_END; 418#elif defined(CTORS_SECTION_ASM_OP) 419/* Hack: force cc1 to switch to .data section early, so that assembling 420 __CTOR_LIST__ does not undo our behind-the-back change to .ctors. */ 421static func_ptr force_to_data[1] __attribute__ ((__unused__)) = { }; 422asm (CTORS_SECTION_ASM_OP); 423STATIC func_ptr __CTOR_END__[1] 424 __attribute__((aligned(sizeof(func_ptr)))) 425 = { (func_ptr) 0 }; 426#else 427STATIC func_ptr __CTOR_END__[1] 428 __attribute__((section(".ctors"), aligned(sizeof(func_ptr)))) 429 = { (func_ptr) 0 }; 430#endif 431 432#ifdef DTOR_LIST_END 433DTOR_LIST_END; 434#elif defined(DTORS_SECTION_ASM_OP) 435asm (DTORS_SECTION_ASM_OP); 436STATIC func_ptr __DTOR_END__[1] 437 __attribute__ ((unused, aligned(sizeof(func_ptr)))) 438 = { (func_ptr) 0 }; 439#else 440STATIC func_ptr __DTOR_END__[1] 441 __attribute__((unused, section(".dtors"), aligned(sizeof(func_ptr)))) 442 = { (func_ptr) 0 }; 443#endif 444 445#ifdef EH_FRAME_SECTION_NAME 446/* Terminate the frame unwind info section with a 4byte 0 as a sentinel; 447 this would be the 'length' field in a real FDE. */ 448# if __INT_MAX__ == 2147483647 449typedef int int32; 450# elif __LONG_MAX__ == 2147483647 451typedef long int32; 452# elif __SHRT_MAX__ == 2147483647 453typedef short int32; 454# else 455# error "Missing a 4 byte integer" 456# endif 457STATIC EH_FRAME_SECTION_CONST int32 __FRAME_END__[] 458 __attribute__ ((unused, section(EH_FRAME_SECTION_NAME), 459 aligned(sizeof(int32)))) 460 = { 0 }; 461#endif /* EH_FRAME_SECTION_NAME */ 462 463#ifdef JCR_SECTION_NAME 464/* Null terminate the .jcr section array. */ 465STATIC void *__JCR_END__[1] 466 __attribute__ ((unused, section(JCR_SECTION_NAME), 467 aligned(sizeof(void *)))) 468 = { 0 }; 469#endif /* JCR_SECTION_NAME */ 470 471#ifdef INIT_SECTION_ASM_OP 472 473#ifdef OBJECT_FORMAT_ELF 474static void __attribute__((used)) 475__do_global_ctors_aux (void) 476{ 477 func_ptr *p; 478 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) 479 (*p) (); 480} 481 482/* Stick a call to __do_global_ctors_aux into the .init section. */ 483CRT_CALL_STATIC_FUNCTION (INIT_SECTION_ASM_OP, __do_global_ctors_aux) 484#else /* OBJECT_FORMAT_ELF */ 485 486/* Stick the real initialization code, followed by a normal sort of 487 function epilogue at the very end of the .init section for this 488 entire root executable file or for this entire shared library file. 489 490 Note that we use some tricks here to get *just* the body and just 491 a function epilogue (but no function prologue) into the .init 492 section of the crtend.o file. Specifically, we switch to the .text 493 section, start to define a function, and then we switch to the .init 494 section just before the body code. 495 496 Earlier on, we put the corresponding function prologue into the .init 497 section of the crtbegin.o file (which will be linked in first). 498 499 Note that we want to invoke all constructors for C++ file-scope static- 500 storage objects AFTER any other possible initialization actions which 501 may be performed by the code in the .init section contributions made by 502 other libraries, etc. That's because those other initializations may 503 include setup operations for very primitive things (e.g. initializing 504 the state of the floating-point coprocessor, etc.) which should be done 505 before we start to execute any of the user's code. */ 506 507static void 508__do_global_ctors_aux (void) /* prologue goes in .text section */ 509{ 510 asm (INIT_SECTION_ASM_OP); 511 DO_GLOBAL_CTORS_BODY; 512 atexit (__do_global_dtors); 513} /* epilogue and body go in .init section */ 514 515FORCE_CODE_SECTION_ALIGN 516asm (TEXT_SECTION_ASM_OP); 517 518#endif /* OBJECT_FORMAT_ELF */ 519 520#elif defined(HAS_INIT_SECTION) /* ! INIT_SECTION_ASM_OP */ 521 522extern void __do_global_ctors (void); 523 524/* This case is used by the Irix 6 port, which supports named sections but 525 not an SVR4-style .init section. __do_global_ctors can be non-static 526 in this case because we protect it with -hidden_symbol. */ 527void 528__do_global_ctors (void) 529{ 530 func_ptr *p; 531#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME) 532 __do_global_ctors_1(); 533#endif 534 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) 535 (*p) (); 536} 537 538#else /* ! INIT_SECTION_ASM_OP && ! HAS_INIT_SECTION */ 539#error "What are you doing with crtstuff.c, then?" 540#endif 541 542#else /* ! CRT_BEGIN && ! CRT_END */ 543#error "One of CRT_BEGIN or CRT_END must be defined." 544#endif 545