2005-03-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / config / m32r / initfini.c
blob356311b7a6810c82f88cd5fabcf65584ef61b406
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) 1996, 1997, 1998 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
20 executable.)
22 GCC is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING. If not, write to
29 the Free Software Foundation, 59 Temple Place - Suite 330,
30 Boston, MA 02111-1307, USA. */
32 /* Declare a pointer to void function type. */
33 typedef void (*func_ptr) (void);
35 #ifdef CRT_INIT
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]
50 __attribute__ ((used, section (".ctors")))
51 = { (func_ptr) (-1) };
53 static func_ptr __DTOR_LIST__[1]
54 __attribute__ ((used, section (".dtors")))
55 = { (func_ptr) (-1) };
57 /* Run all the global destructors on exit from the program. */
59 /* Some systems place the number of pointers in the first word of the
60 table. On SVR4 however, that word is -1. In all cases, the table is
61 null-terminated. On SVR4, we start from the beginning of the list and
62 invoke each per-compilation-unit destructor routine in order
63 until we find that null.
65 Note that this function MUST be static. There will be one of these
66 functions in each root executable and one in each shared library, but
67 although they all have the same code, each one is unique in that it
68 refers to one particular associated `__DTOR_LIST__' which belongs to the
69 same particular root executable or shared library file. */
71 static void __do_global_dtors (void)
72 asm ("__do_global_dtors") __attribute__ ((used, section (".text")));
74 static void
75 __do_global_dtors ()
77 func_ptr *p;
79 for (p = __DTOR_LIST__ + 1; *p; p++)
80 (*p) ();
83 /* .init section start.
84 This must appear at the start of the .init section. */
86 asm ("\n\
87 .section .init,\"ax\",@progbits\n\
88 .balign 4\n\
89 .global __init\n\
90 __init:\n\
91 push fp\n\
92 push lr\n\
93 mv fp,sp\n\
94 seth r0, #shigh(__fini)\n\
95 add3 r0, r0, #low(__fini)\n\
96 bl atexit\n\
97 .fillinsn\n\
98 ");
100 /* .fini section start.
101 This must appear at the start of the .init section. */
103 asm ("\n\
104 .section .fini,\"ax\",@progbits\n\
105 .balign 4\n\
106 .global __fini\n\
107 __fini:\n\
108 push fp\n\
109 push lr\n\
110 mv fp,sp\n\
111 bl __do_global_dtors\n\
112 .fillinsn\n\
115 #endif /* CRT_INIT */
117 #ifdef CRT_FINI
119 /* Put a word containing zero at the end of each of our two lists of function
120 addresses. Note that the words defined here go into the .ctors and .dtors
121 sections of the crtend.o file, and since that file is always linked in
122 last, these words naturally end up at the very ends of the two lists
123 contained in these two sections. */
125 static func_ptr __CTOR_END__[1]
126 __attribute__ ((used, section (".ctors")))
127 = { (func_ptr) 0 };
129 static func_ptr __DTOR_END__[1]
130 __attribute__ ((used, section (".dtors")))
131 = { (func_ptr) 0 };
133 /* Run all global constructors for the program.
134 Note that they are run in reverse order. */
136 static void __do_global_ctors (void)
137 asm ("__do_global_ctors") __attribute__ ((used, section (".text")));
139 static void
140 __do_global_ctors ()
142 func_ptr *p;
144 for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)
145 (*p) ();
148 /* .init section end.
149 This must live at the end of the .init section. */
151 asm ("\n\
152 .section .init,\"ax\",@progbits\n\
153 bl __do_global_ctors\n\
154 mv sp,fp\n\
155 pop lr\n\
156 pop fp\n\
157 jmp lr\n\
158 .fillinsn\n\
161 /* .fini section end.
162 This must live at the end of the .fini section. */
164 asm ("\n\
165 .section .fini,\"ax\",@progbits\n\
166 mv sp,fp\n\
167 pop lr\n\
168 pop fp\n\
169 jmp lr\n\
170 .fillinsn\n\
173 #endif /* CRT_FINI */