update from main archive 960818
[glibc/history.git] / csu / initfini.c
blobf816e081698995e9ae9af4bcbe2287f4f1cf3189
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 /* Extract a `.end' if one is produced by the compiler. */
61 asm ("fgrep .end >/dev/null 2>&1 <<\\EOF.end && need_end=yes");
62 void
63 useless_function (void)
65 return;
67 asm ("\nEOF.end\n");
69 /* Find out how much alignment is produced by the compiler. */
70 asm ("align=`awk '$1==\".align\" { if ($2>max) max=$2; } END { print max; }' \
71 <<\\EOF.align");
72 void
73 useless_function2 (void (*foo) (void))
75 if (foo)
76 (*foo) ();
78 asm ("\nEOF.align\n`\n");
80 /* Append the .init prologue to crti.s-new. */
81 asm ("cat >> crti.s-new <<\\EOF.crti.init");
83 SECTION (".init")
84 void
85 _init (void)
87 /* We cannot use the normal constructor mechanism in gcrt1.o because it
88 appears before crtbegin.o in the link, so the header elt of .ctors
89 would come after the elt for __gmon_start__. One approach is for
90 gcrt1.o to reference a symbol which would be defined by some library
91 module which has a constructor; but then user code's constructors
92 would come first, and not be profiled. */
93 extern void __gmon_start__ (void); weak_extern (__gmon_start__)
94 if (__gmon_start__)
95 __gmon_start__ ();
97 /* End the here document containing the .init 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 ("\n\
101 EOF.crti.init\n\
102 test -n \"$align\" && echo .align $align >> crti.s-new\n\
103 test -n \"$need_end\" && echo .end _init >> crti.s-new\n\
104 fgrep .init crti.s-new >>crtn.s-new\n\
105 fgrep -v .end >> crtn.s-new <<\\EOF.crtn.init");
108 /* End the here document containing the .init epilogue code.
109 Then append the .fini prologue to crti.s-new. */
110 asm ("\nEOF.crtn.init\
112 cat >> crti.s-new <<\\EOF.crti.fini");
114 SECTION (".fini")
115 void
116 _fini (void)
118 /* End the here document containing the .fini prologue code.
119 Then fetch the .section directive just written and append that
120 to crtn.s-new, followed by the function epilogue. */
121 asm ("\nEOF.crti.fini\n\
122 test -n \"$align\" && echo .align $align >> crti.s-new\n\
123 test -n \"$need_end\" && echo .end _fini >> crti.s-new\n\
124 cat > /dev/null <<\\EOF.fini.skip");
127 /* Let GCC know that _fini is not a leaf function by having a dummy
128 function call here. We arrange for this call to be omitted from
129 either crt file. */
130 extern void i_am_not_a_leaf (void);
131 i_am_not_a_leaf ();
134 asm ("\nEOF.fini.skip\
136 fgrep .fini crti.s-new >>crtn.s-new\n\
137 fgrep -v .end >> crtn.s-new <<\\EOF.crtn.fini");
140 /* End the here document containing the .fini epilogue code.
141 Finally, put the remainder of the generated assembly into crtcommon.tmp. */
142 asm ("\nEOF.crtn.fini\
144 cat > crtcommon.tmp <<\\EOF_common");