misc
[glibc.git] / csu / initfini.c
blobbfd120b7ef729ffc3c21bcff3e9b457f66ec77c8
1 /* Special .init and .fini section support.
2 Copyright (C) 1995 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>
37 /* We are compiled with -DGLOBAL=static to generate the versions used for
38 shared libraries' .init and .fini sections, which do not have entry
39 point symbols. */
40 #ifndef GLOBAL
41 #define GLOBAL
42 #endif
44 /* These declarations make the functions go in the right sections when
45 we define them below. GCC syntax does not allow the attribute
46 specifications to be in the function definitions themselves. */
47 GLOBAL void _init (void) __attribute__ ((section (".init")));
48 GLOBAL void _fini (void) __attribute__ ((section (".fini")));
50 /* End the here document containing the initial common code.
51 Then move the output file crtcommon.tmp to crti.s-new and crtn.s-new. */
52 asm ("\nEOF_common\n\
53 mv -f crtcommon.tmp crti.s-new\n\
54 cp -f crti.s-new crtn.s-new");
56 /* Append the .init prologue to crti.s-new. */
57 asm ("cat >> crti.s-new <<\\EOF.crti.init");
58 GLOBAL void
59 _init (void)
61 (void) &_init; /* Don't optimize out the function! */
62 /* End the here document containing the .init prologue code.
63 Then fetch the .section directive just written and append that
64 to crtn.s-new, followed by the function epilogue. */
65 asm ("\nEOF.crti.init
66 \n\
67 fgrep .init crti.s-new >>crtn.s-new\n\
68 cat >> crtn.s-new <<\\EOF.crtn.init");
71 /* End the here document containing the .init epilogue code.
72 Then append the .fini prologue to crti.s-new. */
73 asm ("\nEOF.crtn.init\
74 \n\
75 cat >> crti.s-new <<\\EOF.crti.fini");
77 GLOBAL void
78 _fini (void)
80 (void) &_fini; /* Don't optimize out the function! */
81 /* End the here document containing the .fini prologue code.
82 Then fetch the .section directive just written and append that
83 to crtn.s-new, followed by the function epilogue. */
84 asm ("\nEOF.crti.fini\
85 \n\
86 fgrep .fini crti.s-new >>crtn.s-new\n\
87 cat >> crtn.s-new <<\\EOF.crtn.fini");
90 /* End the here document containing the .fini epilogue code.
91 Finally, put the remainder of the generated assembly into crtcommon.tmp. */
92 asm ("\nEOF.crtn.fini\
93 \n\
94 cat > crtcommon.tmp <<\\EOF_common");