1 /* .init/.fini section handling + C++ global constructor/destructor 2 handling of Andes NDS32 cpu for GNU compiler. 3 This file is based on crtstuff.c, sol2-crti.asm, sol2-crtn.asm. 4 Copyright (C) 2012-2018 Free Software Foundation, Inc. 5 Contributed by Andes Technology Corporation. 6 7 This file is part of GCC. 8 9 GCC is free software; you can redistribute it and/or modify it 10 under the terms of the GNU General Public License as published 11 by the Free Software Foundation; either version 3, or (at your 12 option) any later version. 13 14 GCC is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 License for more details. 18 19 Under Section 7 of GPL version 3, you are granted additional 20 permissions described in the GCC Runtime Library Exception, version 21 3.1, as published by the Free Software Foundation. 22 23 You should have received a copy of the GNU General Public License and 24 a copy of the GCC Runtime Library Exception along with this program; 25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 26 <http://www.gnu.org/licenses/>. */ 27 28 /* Declare a pointer to void function type. */ 29 typedef void (*func_ptr) (void); 30 31 #ifdef CRT_BEGIN 32 33 /* NOTE: In order to be able to support SVR4 shared libraries, we arrange 34 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__, 35 __DTOR_END__ } per root executable and also one set of these symbols 36 per shared library. So in any given whole process image, we may have 37 multiple definitions of each of these symbols. In order to prevent 38 these definitions from conflicting with one another, and in order to 39 ensure that the proper lists are used for the initialization/finalization 40 of each individual shared library (respectively), we give these symbols 41 only internal (i.e. `static') linkage, and we also make it a point to 42 refer to only the __CTOR_END__ symbol in crtfini.o and the __DTOR_LIST__ 43 symbol in crtinit.o, where they are defined. */ 44 45 static func_ptr __CTOR_LIST__[1] __attribute__ ((section (".ctors"))) 46 = { (func_ptr) (-1) }; 47 48 static func_ptr __DTOR_LIST__[1] __attribute__ ((section (".dtors"))) 49 = { (func_ptr) (-1) }; 50 51 /* Run all the global destructors on exit from the program. */ 52 53 /* Some systems place the number of pointers in the first word of the 54 table. On SVR4 however, that word is -1. In all cases, the table is 55 null-terminated. On SVR4, we start from the beginning of the list and 56 invoke each per-compilation-unit destructor routine in order 57 until we find that null. 58 59 Note that this function MUST be static. There will be one of these 60 functions in each root executable and one in each shared library, but 61 although they all have the same code, each one is unique in that it 62 refers to one particular associated `__DTOR_LIST__' which belongs to the 63 same particular root executable or shared library file. */ 64 65 static void __do_global_dtors (void) 66 asm ("__do_global_dtors") __attribute__ ((section (".text"))); 67 68 static void 69 __do_global_dtors (void) 70 { 71 func_ptr *p; 72 for (p = __DTOR_LIST__ + 1; *p; p++) 73 (*p) (); 74 } 75 76 /* .init section start. 77 This must appear at the start of the .init section. */ 78 79 asm ("\n\ 80 .section .init\n\ 81 .global _init\n\ 82 .type _init, @function\n\ 83 _init:\n\ 84 ! 1. store $fp\n\ 85 ! 2. adjust $fp by $sp\n\ 86 ! 3. adjust $sp\n\ 87 "); 88 89 /* .fini section start. 90 This must appear at the start of the .fini section. */ 91 92 asm ("\n\ 93 .section .fini\n\ 94 .global _fini\n\ 95 .type _fini, @function\n\ 96 _fini:\n\ 97 ! 1. store $fp\n\ 98 ! 2. adjust $fp by $sp\n\ 99 ! 3. adjust $sp\n\ 100 ! 4. call __do_global_dtors\n\ 101 j __do_global_dtors\n\ 102 "); 103 104 #endif /* CRT_BEGIN */ 105 106 #ifdef CRT_END 107 108 /* Define __dso_handle which would be needed for C++ library. 109 Since our elf-toolchain only builds programs with static link, 110 we can directly define 'void *__dso_handle = 0'. */ 111 void *__dso_handle = 0; 112 113 /* Put a word containing zero at the end of each of our two lists of function 114 addresses. Note that the words defined here go into the .ctors and .dtors 115 sections of the crtend.o file, and since that file is always linked in 116 last, these words naturally end up at the very ends of the two lists 117 contained in these two sections. */ 118 119 static func_ptr __CTOR_END__[1] __attribute__ ((section (".ctors"))) 120 = { (func_ptr) 0 }; 121 122 static func_ptr __DTOR_END__[1] __attribute__ ((section (".dtors"))) 123 = { (func_ptr) 0 }; 124 125 /* Run all global constructors for the program. 126 Note that they are run in reverse order. */ 127 128 static void __do_global_ctors (void) 129 asm ("__do_global_ctors") __attribute__ ((section (".text"))); 130 131 static void 132 __do_global_ctors (void) 133 { 134 func_ptr *p; 135 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--) 136 (*p) (); 137 } 138 139 /* .init section end. 140 This must live at the end of the .init section. */ 141 142 asm ("\n\ 143 .section .init\n\ 144 ! 1. call __do_global_ctors\n\ 145 ! 2. adjust back $sp\n\ 146 ! 3. restore $fp\n\ 147 j __do_global_ctors\n\ 148 "); 149 150 /* .fini section end. 151 This must live at the end of the .fini section. */ 152 153 asm ("\n\ 154 .section .fini\n\ 155 ! 1. adjust back $sp\n\ 156 ! 2. restore $fp\n\ 157 "); 158 159 #endif /* CRT_END */ 160