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-2024 Free Software Foundation, Inc.
5 Contributed by Andes Technology Corporation.
7 This file is part of GCC.
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.
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.
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.
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/>. */
29 /* Need header file for `struct object' type. */
30 #include "../libgcc/unwind-dw2-fde.h"
32 /* Declare a pointer to void function type. */
33 typedef void (*func_ptr
) (void);
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. */
49 static func_ptr __CTOR_LIST__
[1] __attribute__ ((section (".ctors"), used
))
52 static func_ptr __DTOR_LIST__
[1] __attribute__ ((section (".dtors"), used
))
56 #ifdef SUPPORT_UNWINDING_DWARF2
57 /* Preparation of exception handling with dwar2 mechanism registration. */
60 .section .eh_frame,\"aw\",@progbits\n\
61 .global __EH_FRAME_BEGIN__\n\
62 .type __EH_FRAME_BEGIN__, @object\n\
64 __EH_FRAME_BEGIN__:\n\
65 ! Beginning location of eh_frame section\n\
69 extern func_ptr __EH_FRAME_BEGIN__
[];
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
));
78 /* Register the exception handling table as the first constructor. */
80 __nds32_register_eh (void)
82 static struct object object
;
83 if (__register_frame_info
)
84 __register_frame_info (__EH_FRAME_BEGIN__
, &object
);
87 /* Unregister the exception handling table as a deconstructor. */
89 __nds32_deregister_eh (void)
91 static int completed
= 0;
96 if (__deregister_frame_info
)
97 __deregister_frame_info (__EH_FRAME_BEGIN__
);
103 /* Run all the global destructors on exit from the program. */
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.
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. */
117 static void __do_global_dtors (void)
118 asm ("__do_global_dtors") __attribute__ ((section (".text"), used
));
121 __do_global_dtors (void)
124 for (p
= __DTOR_LIST__
+ 1; *p
; p
++)
128 /* .init section start.
129 This must appear at the start of the .init section. */
134 .type _init, @function\n\
137 ! 2. adjust $fp by $sp\n\
141 /* .fini section start.
142 This must appear at the start of the .fini section. */
147 .type _fini, @function\n\
150 ! 2. adjust $fp by $sp\n\
152 ! 4. call __do_global_dtors\n\
153 j __do_global_dtors\n\
156 #endif /* CRT_BEGIN */
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;
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. */
171 static func_ptr __CTOR_END__
[1] __attribute__ ((section (".ctors"), used
))
174 static func_ptr __DTOR_END__
[1] __attribute__ ((section (".dtors"), used
))
177 #ifdef SUPPORT_UNWINDING_DWARF2
178 /* ZERO terminator in .eh_frame section. */
180 .section .eh_frame,\"aw\",@progbits\n\
181 .global __EH_FRAME_END__\n\
182 .type __EH_FRAME_END__, @object\n\
185 ! End location of eh_frame section with ZERO terminator\n\
191 /* Run all global constructors for the program.
192 Note that they are run in reverse order. */
194 static void __do_global_ctors (void)
195 asm ("__do_global_ctors") __attribute__ ((section (".text"), used
));
198 __do_global_ctors (void)
201 for (p
= __CTOR_END__
- 1; *p
; p
--)
205 /* .init section end.
206 This must live at the end of the .init section. */
210 ! 1. call __do_global_ctors\n\
211 ! 2. adjust back $sp\n\
213 j __do_global_ctors\n\
216 /* .fini section end.
217 This must live at the end of the .fini section. */
221 ! 1. adjust back $sp\n\