* elf/Makefile (lib-noranlib): Move rule adding deps for ld.so et al
[glibc.git] / csu / initfini.c
blob8fe2368e360e3853b956e46baed0bf84bbc9b5e4
1 /* Special .init and .fini section support.
2 Copyright (C) 1995, 1996 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 /* This file is compiled into assembly code which is then surrounded by the
21 lines `cat > crtcommon.tmp <<\EOF_common' and `EOF_common' and thus
22 becomes a shell script which creates three files of assembly code.
24 * The first file is crti.s-new; this puts a function prologue at the
25 beginning of the .init and .fini sections and defines global symbols for
26 those addresses, so they can be called as functions.
28 * The second file is crtn.s-new; this puts the corresponding function
29 epilogues in the .init and .fini sections.
31 * The third file is crtcommon.tmp, which is whatever miscellaneous cruft
32 the compiler generated at the end; it should be appended to both crti.s-new
33 and crtn.s-new. */
35 #include <stdlib.h>
38 #ifdef HAVE_ELF
39 /* These declarations make the functions go in the right sections when
40 we define them below. GCC syntax does not allow the attribute
41 specifications to be in the function definitions themselves. */
42 void _init (void) __attribute__ ((section (".init")));
43 void _fini (void) __attribute__ ((section (".fini")));
45 #define SECTION(x) /* Put nothing extra before the defn. */
47 #else
48 /* Some non-ELF systems support .init and .fini sections,
49 but the __attribute__ syntax only works for ELF. */
50 #define SECTION(x) asm (".section " x);
51 #endif
53 /* End the here document containing the initial common code.
54 Then move the output file crtcommon.tmp to crti.s-new and crtn.s-new. */
55 asm ("\nEOF_common\n\
56 rm -f crti.s-new crtn.s-new\n\
57 mv crtcommon.tmp crti.s-new\n\
58 cp crti.s-new crtn.s-new");
60 /* Append the .init prologue to crti.s-new. */
61 asm ("cat >> crti.s-new <<\\EOF.crti.init");
63 SECTION (".init")
64 void
65 _init (void)
67 /* We cannot use the normal constructor mechanism in gcrt1.o because it
68 appears before crtbegin.o in the link, so the header elt of .ctors
69 would come after the elt for __gmon_start__. One approach is for
70 gcrt1.o to reference a symbol which would be defined by some library
71 module which has a constructor; but then user code's constructors
72 would come first, and not be profiled. */
73 extern void __gmon_start__ (void) __attribute__ ((weak));
74 weak_symbol (__gmon_start__)
75 if (__gmon_start__)
76 __gmon_start__ ();
78 /* End the here document containing the .init prologue code.
79 Then fetch the .section directive just written and append that
80 to crtn.s-new, followed by the function epilogue. */
81 asm ("\nEOF.crti.init
82 \n\
83 fgrep .init crti.s-new >>crtn.s-new\n\
84 cat >> crtn.s-new <<\\EOF.crtn.init");
87 /* End the here document containing the .init epilogue code.
88 Then append the .fini prologue to crti.s-new. */
89 asm ("\nEOF.crtn.init\
90 \n\
91 cat >> crti.s-new <<\\EOF.crti.fini");
93 SECTION (".fini")
94 void
95 _fini (void)
97 /* End the here document containing the .fini prologue code.
98 Then fetch the .section directive just written and append that
99 to crtn.s-new, followed by the function epilogue. */
100 asm ("\nEOF.crti.fini\
102 cat > /dev/null <<\\EOF.fini.skip");
105 /* Let GCC know that _fini is not a leaf function by having a dummy
106 function call here. We arrange for this call to be omitted from
107 either crt file. */
108 extern void i_am_not_a_leaf (void);
109 i_am_not_a_leaf ();
112 asm ("\nEOF.fini.skip\
114 fgrep .fini crti.s-new >>crtn.s-new\n\
115 cat >> crtn.s-new <<\\EOF.crtn.fini");
118 /* End the here document containing the .fini epilogue code.
119 Finally, put the remainder of the generated assembly into crtcommon.tmp. */
120 asm ("\nEOF.crtn.fini\
122 cat > crtcommon.tmp <<\\EOF_common");