1 /* .init/.fini section handling + C++ global constructor/destructor handling.
2 This file is based on crtstuff.c, sol2-crti.asm, sol2-crtn.asm.
4 Copyright (C) 1995-2017 Free Software Foundation, Inc.
5 Contributor: Joern Rennecke <joern.rennecke@embecosm.com>
6 on behalf of Synopsys Inc.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 Under Section 7 of GPL version 3, you are granted additional
21 permissions described in the GCC Runtime Library Exception, version
22 3.1, as published by the Free Software Foundation.
24 You should have received a copy of the GNU General Public License and
25 a copy of the GCC Runtime Library Exception along with this program;
26 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
27 <http://www.gnu.org/licenses/>. */
29 /* Declare a pointer to void function type. */
30 typedef void (*func_ptr
) (void);
34 /* NOTE: In order to be able to support SVR4 shared libraries, we arrange
35 to have one set of symbols { __CTOR_LIST__, __DTOR_LIST__, __CTOR_END__,
36 __DTOR_END__ } per root executable and also one set of these symbols
37 per shared library. So in any given whole process image, we may have
38 multiple definitions of each of these symbols. In order to prevent
39 these definitions from conflicting with one another, and in order to
40 ensure that the proper lists are used for the initialization/finalization
41 of each individual shared library (respectively), we give these symbols
42 only internal (i.e. `static') linkage, and we also make it a point to
43 refer to only the __CTOR_END__ symbol in crtfini.o and the __DTOR_LIST__
44 symbol in crtinit.o, where they are defined. */
46 static func_ptr __CTOR_LIST__
[1] __attribute__ ((section (".ctors")))
47 = { (func_ptr
) (-1) };
49 static func_ptr __DTOR_LIST__
[1] __attribute__ ((section (".dtors")))
50 = { (func_ptr
) (-1) };
52 /* Run all the global destructors on exit from the program. */
54 /* Some systems place the number of pointers in the first word of the
55 table. On SVR4 however, that word is -1. In all cases, the table is
56 null-terminated. On SVR4, we start from the beginning of the list and
57 invoke each per-compilation-unit destructor routine in order
58 until we find that null.
60 Note that this function MUST be static. There will be one of these
61 functions in each root executable and one in each shared library, but
62 although they all have the same code, each one is unique in that it
63 refers to one particular associated `__DTOR_LIST__' which belongs to the
64 same particular root executable or shared library file. */
66 static void __do_global_dtors (void)
67 asm ("__do_global_dtors") __attribute__ ((section (".text")));
70 __do_global_dtors (void)
73 for (p
= __DTOR_LIST__
+ 1; *p
; p
++)
77 /* .init section start.
78 This must appear at the start of the .init section. */
91 /* .fini section start.
92 This must appear at the start of the .init section. */
103 bl.nd __do_global_dtors\n\
106 #endif /* CRT_INIT */
110 /* Put a word containing zero at the end of each of our two lists of function
111 addresses. Note that the words defined here go into the .ctors and .dtors
112 sections of the crtend.o file, and since that file is always linked in
113 last, these words naturally end up at the very ends of the two lists
114 contained in these two sections. */
116 static func_ptr __CTOR_END__
[1] __attribute__ ((section (".ctors")))
119 static func_ptr __DTOR_END__
[1] __attribute__ ((section (".dtors")))
122 /* Run all global constructors for the program.
123 Note that they are run in reverse order. */
125 static void __do_global_ctors (void)
126 asm ("__do_global_ctors") __attribute__ ((section (".text")));
129 __do_global_ctors (void)
132 for (p
= __CTOR_END__
- 1; *p
!= (func_ptr
) -1; p
--)
136 /* .init section end.
137 This must live at the end of the .init section. */
141 bl.nd __do_global_ctors\n\
147 /* .fini section end.
148 This must live at the end of the .fini section. */
157 #endif /* CRT_FINI */