Update.
[glibc.git] / csu / initfini.c
blob1f74f203a30acc9e8f058acd8cc37725384e5952
1 /* Special .init and .fini section support.
2 Copyright (C) 1995, 1996, 1997 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
6 and/or modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 In addition to the permissions in the GNU Library 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 Library 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 The GNU C Library is distributed in the hope that it will be
20 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Library General Public License for more details.
24 You should have received a copy of the GNU Library General Public
25 License along with the GNU C Library; see the file COPYING.LIB. If not,
26 write to the Free Software Foundation, 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. */
29 /* This file is compiled into assembly code which is then munged by a sed
30 script into two files: crti.s and crtn.s.
32 * crti.s puts a function prologue at the beginning of the
33 .init and .fini sections and defines global symbols for
34 those addresses, so they can be called as functions.
36 * crtn.s puts the corresponding function epilogues
37 in the .init and .fini sections. */
39 #include <stdlib.h>
41 /* We use embedded asm for .section unconditionally, as this makes it
42 easier to insert the necessary directives into crtn.S. */
43 #define SECTION(x) asm (".section " x );
45 /* Embed an #include to pull in the alignment and .end directives. */
46 asm ("\n#include \"defs.h\"");
48 /* The initial common code ends here. */
49 asm ("\n/*@HEADER_ENDS*/");
51 /* To determine whether we need .end and .align: */
52 asm ("\n/*@TESTS_BEGIN*/");
53 void
54 dummy (void (*foo) (void))
56 if (foo)
57 (*foo) ();
59 asm ("\n/*@TESTS_END*/");
61 /* The beginning of _init: */
62 asm ("\n/*@_init_PROLOG_BEGINS*/");
64 SECTION (".init")
65 void
66 _init (void)
68 /* We cannot use the normal constructor mechanism in gcrt1.o because it
69 appears before crtbegin.o in the link, so the header elt of .ctors
70 would come after the elt for __gmon_start__. One approach is for
71 gcrt1.o to reference a symbol which would be defined by some library
72 module which has a constructor; but then user code's constructors
73 would come first, and not be profiled. */
74 extern void __gmon_start__ (void) __attribute__ ((weak)); /*weak_extern (__gmon_start__);*/
76 __gmon_start__ ();
78 asm ("ALIGN");
79 asm("END_INIT");
80 /* Now the epilog. */
81 asm ("\n/*@_init_PROLOG_ENDS*/");
82 asm ("\n/*@_init_EPILOG_BEGINS*/");
83 SECTION(".init");
85 asm ("END_INIT");
86 SECTION(".text");
88 /* This version of __gmon_start__ is used if no other is found. By providing
89 a default function we avoid the need to test whether the pointer is NULL,
90 which can be painful on some machines. */
92 void __attribute__ ((weak))
93 __gmon_start__ (void)
95 /* do nothing */
98 /* End of the _init epilog, beginning of the _fini prolog. */
99 asm ("\n/*@_init_EPILOG_ENDS*/");
100 asm ("\n/*@_fini_PROLOG_BEGINS*/");
102 SECTION (".fini")
103 void
104 _fini (void)
107 /* End of the _fini prolog. */
108 asm ("ALIGN");
109 asm ("END_FINI");
110 asm ("\n/*@_fini_PROLOG_ENDS*/");
113 /* Let GCC know that _fini is not a leaf function by having a dummy
114 function call here. We arrange for this call to be omitted from
115 either crt file. */
116 extern void i_am_not_a_leaf (void);
117 i_am_not_a_leaf ();
120 /* Beginning of the _fini epilog. */
121 asm ("\n/*@_fini_EPILOG_BEGINS*/");
122 SECTION (".fini");
124 asm ("END_FINI");
126 /* End of the _fini epilog. Any further generated assembly (e.g. .ident)
127 is shared between both crt files. */
128 asm ("\n/*@_fini_EPILOG_ENDS*/");
129 asm ("\n/*@TRAILER_BEGINS*/");
131 /* End of file. */