.
[glibc.git] / sysdeps / generic / initfini.c
blobd5ef778367f567bd4814ac956794ddc17095f141
1 /* Special .init and .fini section support.
2 Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 In addition to the permissions in the GNU Lesser General Public
11 License, the Free Software Foundation gives you unlimited
12 permission to link the compiled version of this file with other
13 programs, and to distribute those programs without any restriction
14 coming from the use of this file. (The GNU Lesser General Public
15 License restrictions do apply in other respects; for example, they
16 cover modification of the file, and distribution when not linked
17 into another program.)
19 Note that people who make modified versions of this file are not
20 obligated to grant this special exception for their modified
21 versions; it is their choice whether to do so. The GNU Lesser
22 General Public License gives permission to release a modified
23 version without this exception; this exception also makes it
24 possible to release a modified version which carries forward this
25 exception.
27 The GNU C Library is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 Lesser General Public License for more details.
32 You should have received a copy of the GNU Lesser General Public
33 License along with the GNU C Library; if not, write to the Free
34 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
35 02111-1307 USA. */
37 /* This file is compiled into assembly code which is then munged by a sed
38 script into two files: crti.s and crtn.s.
40 * crti.s puts a function prologue at the beginning of the
41 .init and .fini sections and defines global symbols for
42 those addresses, so they can be called as functions.
44 * crtn.s puts the corresponding function epilogues
45 in the .init and .fini sections. */
47 #include <stdlib.h>
49 /* We use embedded asm for .section unconditionally, as this makes it
50 easier to insert the necessary directives into crtn.S. */
51 #define SECTION(x) asm (".section " x )
53 /* Embed an #include to pull in the alignment and .end directives. */
54 asm ("\n#include \"defs.h\"");
56 /* The initial common code ends here. */
57 asm ("\n/*@HEADER_ENDS*/");
59 /* To determine whether we need .end and .align: */
60 asm ("\n/*@TESTS_BEGIN*/");
61 extern void dummy (void (*foo) (void));
62 void
63 dummy (void (*foo) (void))
65 if (foo)
66 (*foo) ();
68 asm ("\n/*@TESTS_END*/");
70 /* The beginning of _init: */
71 asm ("\n/*@_init_PROLOG_BEGINS*/");
73 static void
74 call_gmon_start(void)
76 extern void __gmon_start__ (void) __attribute__ ((weak)); /*weak_extern (__gmon_start__);*/
77 void (*gmon_start) (void) = __gmon_start__;
79 if (gmon_start)
80 gmon_start ();
83 SECTION (".init");
84 extern void __attribute__ ((section (".init"))) _init (void);
85 void
86 _init (void)
88 /* We cannot use the normal constructor mechanism in gcrt1.o because it
89 appears before crtbegin.o in the link, so the header elt of .ctors
90 would come after the elt for __gmon_start__. One approach is for
91 gcrt1.o to reference a symbol which would be defined by some library
92 module which has a constructor; but then user code's constructors
93 would come first, and not be profiled. */
94 call_gmon_start ();
96 asm ("ALIGN");
97 asm("END_INIT");
98 /* Now the epilog. */
99 asm ("\n/*@_init_PROLOG_ENDS*/");
100 asm ("\n/*@_init_EPILOG_BEGINS*/");
101 SECTION(".init");
103 asm ("END_INIT");
105 /* End of the _init epilog, beginning of the _fini prolog. */
106 asm ("\n/*@_init_EPILOG_ENDS*/");
107 asm ("\n/*@_fini_PROLOG_BEGINS*/");
109 SECTION (".fini");
110 extern void __attribute__ ((section (".fini"))) _fini (void);
111 void
112 _fini (void)
115 /* End of the _fini prolog. */
116 asm ("ALIGN");
117 asm ("END_FINI");
118 asm ("\n/*@_fini_PROLOG_ENDS*/");
121 /* Let GCC know that _fini is not a leaf function by having a dummy
122 function call here. We arrange for this call to be omitted from
123 either crt file. */
124 extern void i_am_not_a_leaf (void);
125 i_am_not_a_leaf ();
128 /* Beginning of the _fini epilog. */
129 asm ("\n/*@_fini_EPILOG_BEGINS*/");
130 SECTION (".fini");
132 asm ("END_FINI");
134 /* End of the _fini epilog. Any further generated assembly (e.g. .ident)
135 is shared between both crt files. */
136 asm ("\n/*@_fini_EPILOG_ENDS*/");
137 asm ("\n/*@TRAILER_BEGINS*/");
139 /* End of file. */