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-2016 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/>. */
28 /* Declare a pointer to void function type. */
29 typedef void (*func_ptr
) (void);
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. */
45 static func_ptr __CTOR_LIST__
[1] __attribute__ ((section (".ctors")))
46 = { (func_ptr
) (-1) };
48 static func_ptr __DTOR_LIST__
[1] __attribute__ ((section (".dtors")))
49 = { (func_ptr
) (-1) };
51 /* Run all the global destructors on exit from the program. */
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.
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. */
65 static void __do_global_dtors (void)
66 asm ("__do_global_dtors") __attribute__ ((section (".text")));
69 __do_global_dtors (void)
72 for (p
= __DTOR_LIST__
+ 1; *p
; p
++)
76 /* .init section start.
77 This must appear at the start of the .init section. */
82 .type _init, @function\n\
85 ! 2. adjust $fp by $sp\n\
89 /* .fini section start.
90 This must appear at the start of the .fini section. */
95 .type _fini, @function\n\
98 ! 2. adjust $fp by $sp\n\
100 ! 4. call __do_global_dtors\n\
101 j __do_global_dtors\n\
104 #endif /* CRT_BEGIN */
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;
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. */
119 static func_ptr __CTOR_END__
[1] __attribute__ ((section (".ctors")))
122 static func_ptr __DTOR_END__
[1] __attribute__ ((section (".dtors")))
125 /* Run all global constructors for the program.
126 Note that they are run in reverse order. */
128 static void __do_global_ctors (void)
129 asm ("__do_global_ctors") __attribute__ ((section (".text")));
132 __do_global_ctors (void)
135 for (p
= __CTOR_END__
- 1; *p
!= (func_ptr
) -1; p
--)
139 /* .init section end.
140 This must live at the end of the .init section. */
144 ! 1. call __do_global_ctors\n\
145 ! 2. adjust back $sp\n\
147 j __do_global_ctors\n\
150 /* .fini section end.
151 This must live at the end of the .fini section. */
155 ! 1. adjust back $sp\n\