| 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-2025 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 |
#include <stddef.h> |
| 29 |
/* Need header file for `struct object' type. */ |
| 30 |
#include "../libgcc/unwind-dw2-fde.h" |
| 31 |
|
| 32 |
/* Declare a pointer to void function type. */ |
| 33 |
typedef void (*func_ptr) (void); |
| 34 |
|
| 35 |
#ifdef CRT_BEGIN |
| 36 |
|
| 37 |
/* NOTE: In order to be able to support SVR4 shared libraries, we arrange |
| 38 |
to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__, |
| 39 |
__DTOR_END__ } per root executable and also one set of these symbols |
| 40 |
per shared library. So in any given whole process image, we may have |
| 41 |
multiple definitions of each of these symbols. In order to prevent |
| 42 |
these definitions from conflicting with one another, and in order to |
| 43 |
ensure that the proper lists are used for the initialization/finalization |
| 44 |
of each individual shared library (respectively), we give these symbols |
| 45 |
only internal (i.e. `static') linkage, and we also make it a point to |
| 46 |
refer to only the __CTOR_END__ symbol in crtfini.o and the __DTOR_LIST__ |
| 47 |
symbol in crtinit.o, where they are defined. */ |
| 48 |
|
| 49 |
static func_ptr __CTOR_LIST__[1] __attribute__ ((section (".ctors"), used)) |
| 50 |
= { (func_ptr) 0 }; |
| 51 |
|
| 52 |
static func_ptr __DTOR_LIST__[1] __attribute__ ((section (".dtors"), used)) |
| 53 |
= { (func_ptr) 0 }; |
| 54 |
|
| 55 |
|
| 56 |
#ifdef SUPPORT_UNWINDING_DWARF2 |
| 57 |
/* Preparation of exception handling with dwar2 mechanism registration. */ |
| 58 |
|
| 59 |
asm ("\n\ |
| 60 |
.section .eh_frame,\"aw\",@progbits\n\ |
| 61 |
.global __EH_FRAME_BEGIN__\n\ |
| 62 |
.type __EH_FRAME_BEGIN__, @object\n\ |
| 63 |
.align 2\n\ |
| 64 |
__EH_FRAME_BEGIN__:\n\ |
| 65 |
! Beginning location of eh_frame section\n\ |
| 66 |
.previous\n\ |
| 67 |
"); |
| 68 |
|
| 69 |
extern func_ptr __EH_FRAME_BEGIN__[]; |
| 70 |
|
| 71 |
|
| 72 |
/* Note that the following two functions are going to be chained into |
| 73 |
constructor and destructor list, repectively. So these two declarations |
| 74 |
must be placed after __CTOR_LIST__ and __DTOR_LIST. */ |
| 75 |
extern void __nds32_register_eh(void) __attribute__((constructor, used)); |
| 76 |
extern void __nds32_deregister_eh(void) __attribute__((destructor, used)); |
| 77 |
|
| 78 |
/* Register the exception handling table as the first constructor. */ |
| 79 |
void |
| 80 |
__nds32_register_eh (void) |
| 81 |
{ |
| 82 |
static struct object object; |
| 83 |
if (__register_frame_info) |
| 84 |
__register_frame_info (__EH_FRAME_BEGIN__, &object); |
| 85 |
} |
| 86 |
|
| 87 |
/* Unregister the exception handling table as a deconstructor. */ |
| 88 |
void |
| 89 |
__nds32_deregister_eh (void) |
| 90 |
{ |
| 91 |
static int completed = 0; |
| 92 |
|
| 93 |
if (completed) |
| 94 |
return; |
| 95 |
|
| 96 |
if (__deregister_frame_info) |
| 97 |
__deregister_frame_info (__EH_FRAME_BEGIN__); |
| 98 |
|
| 99 |
completed = 1; |
| 100 |
} |
| 101 |
#endif |
| 102 |
|
| 103 |
/* Run all the global destructors on exit from the program. */ |
| 104 |
|
| 105 |
/* Some systems place the number of pointers in the first word of the |
| 106 |
table. On SVR4 however, that word is -1. In all cases, the table is |
| 107 |
null-terminated. On SVR4, we start from the beginning of the list and |
| 108 |
invoke each per-compilation-unit destructor routine in order |
| 109 |
until we find that null. |
| 110 |
|
| 111 |
Note that this function MUST be static. There will be one of these |
| 112 |
functions in each root executable and one in each shared library, but |
| 113 |
although they all have the same code, each one is unique in that it |
| 114 |
refers to one particular associated `__DTOR_LIST__' which belongs to the |
| 115 |
same particular root executable or shared library file. */ |
| 116 |
|
| 117 |
static void __do_global_dtors (void) |
| 118 |
asm ("__do_global_dtors") __attribute__ ((section (".text"), used)); |
| 119 |
|
| 120 |
static void |
| 121 |
__do_global_dtors (void) |
| 122 |
{ |
| 123 |
func_ptr *p; |
| 124 |
for (p = __DTOR_LIST__ + 1; *p; p++) |
| 125 |
(*p) (); |
| 126 |
} |
| 127 |
|
| 128 |
/* .init section start. |
| 129 |
This must appear at the start of the .init section. */ |
| 130 |
|
| 131 |
asm ("\n\ |
| 132 |
.section .init\n\ |
| 133 |
.global _init\n\ |
| 134 |
.type _init, @function\n\ |
| 135 |
_init:\n\ |
| 136 |
! 1. store $fp\n\ |
| 137 |
! 2. adjust $fp by $sp\n\ |
| 138 |
! 3. adjust $sp\n\ |
| 139 |
"); |
| 140 |
|
| 141 |
/* .fini section start. |
| 142 |
This must appear at the start of the .fini section. */ |
| 143 |
|
| 144 |
asm ("\n\ |
| 145 |
.section .fini\n\ |
| 146 |
.global _fini\n\ |
| 147 |
.type _fini, @function\n\ |
| 148 |
_fini:\n\ |
| 149 |
! 1. store $fp\n\ |
| 150 |
! 2. adjust $fp by $sp\n\ |
| 151 |
! 3. adjust $sp\n\ |
| 152 |
! 4. call __do_global_dtors\n\ |
| 153 |
j __do_global_dtors\n\ |
| 154 |
"); |
| 155 |
|
| 156 |
#endif /* CRT_BEGIN */ |
| 157 |
|
| 158 |
#ifdef CRT_END |
| 159 |
|
| 160 |
/* Define __dso_handle which would be needed for C++ library. |
| 161 |
Since our elf-toolchain only builds programs with static link, |
| 162 |
we can directly define 'void *__dso_handle = 0'. */ |
| 163 |
void *__dso_handle = 0; |
| 164 |
|
| 165 |
/* Put a word containing zero at the end of each of our two lists of function |
| 166 |
addresses. Note that the words defined here go into the .ctors and .dtors |
| 167 |
sections of the crtend.o file, and since that file is always linked in |
| 168 |
last, these words naturally end up at the very ends of the two lists |
| 169 |
contained in these two sections. */ |
| 170 |
|
| 171 |
static func_ptr __CTOR_END__[1] __attribute__ ((section (".ctors"), used)) |
| 172 |
= { (func_ptr) 0 }; |
| 173 |
|
| 174 |
static func_ptr __DTOR_END__[1] __attribute__ ((section (".dtors"), used)) |
| 175 |
= { (func_ptr) 0 }; |
| 176 |
|
| 177 |
#ifdef SUPPORT_UNWINDING_DWARF2 |
| 178 |
/* ZERO terminator in .eh_frame section. */ |
| 179 |
asm ("\n\ |
| 180 |
.section .eh_frame,\"aw\",@progbits\n\ |
| 181 |
.global __EH_FRAME_END__\n\ |
| 182 |
.type __EH_FRAME_END__, @object\n\ |
| 183 |
.align 2\n\ |
| 184 |
__EH_FRAME_END__:\n\ |
| 185 |
! End location of eh_frame section with ZERO terminator\n\ |
| 186 |
.word 0\n\ |
| 187 |
.previous\n\ |
| 188 |
"); |
| 189 |
#endif |
| 190 |
|
| 191 |
/* Run all global constructors for the program. |
| 192 |
Note that they are run in reverse order. */ |
| 193 |
|
| 194 |
static void __do_global_ctors (void) |
| 195 |
asm ("__do_global_ctors") __attribute__ ((section (".text"), used)); |
| 196 |
|
| 197 |
static void |
| 198 |
__do_global_ctors (void) |
| 199 |
{ |
| 200 |
func_ptr *p; |
| 201 |
for (p = __CTOR_END__ - 1; *p; p--) |
| 202 |
(*p) (); |
| 203 |
} |
| 204 |
|
| 205 |
/* .init section end. |
| 206 |
This must live at the end of the .init section. */ |
| 207 |
|
| 208 |
asm ("\n\ |
| 209 |
.section .init\n\ |
| 210 |
! 1. call __do_global_ctors\n\ |
| 211 |
! 2. adjust back $sp\n\ |
| 212 |
! 3. restore $fp\n\ |
| 213 |
j __do_global_ctors\n\ |
| 214 |
"); |
| 215 |
|
| 216 |
/* .fini section end. |
| 217 |
This must live at the end of the .fini section. */ |
| 218 |
|
| 219 |
asm ("\n\ |
| 220 |
.section .fini\n\ |
| 221 |
! 1. adjust back $sp\n\ |
| 222 |
! 2. restore $fp\n\ |
| 223 |
"); |
| 224 |
|
| 225 |
#endif /* CRT_END */ |