OSDN Git Service

Ok, this commit is _huge_ and its gonna change the world. I've
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / common / initfini.c
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.
4
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.
9
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.)
18
19    The GNU C Library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with the GNU C Library; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27    02111-1307 USA.  */
28
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.
31
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.
35
36    * crtn.s puts the corresponding function epilogues
37    in the .init and .fini sections. */
38
39 #include <features.h>
40
41 #undef GMON_SUPPORT
42
43 /* We use embedded asm for .section unconditionally, as this makes it
44    easier to insert the necessary directives into crtn.S. */
45 #define SECTION(x) asm (".section " x );
46
47 /* Declare symbols as hidden. Hidden symbols are only seen by
48  * the link editor and not by the dynamic loader. */
49 #ifdef __HAVE_DOT_HIDDEN__
50 #  define HIDDEN(func)  asm (".hidden  " #func );
51 #else
52 #  define HIDDEN(func)
53 #endif
54
55 /* The initial common code ends here. */
56 asm ("\n/*@HEADER_ENDS*/");
57
58 /* To determine whether we need .end and .align: */
59 //asm ("\n/*@TESTS_BEGIN*/");
60 extern void dummy (void (*foo) (void));
61 void
62 dummy (void (*foo) (void))
63 {
64   if (foo)
65     (*foo) ();
66 }
67 //asm ("\n/*@TESTS_END*/");
68
69 /* The beginning of _init:  */
70 asm ("\n/*@_init_PROLOG_BEGINS*/");
71
72 #ifdef GMON_SUPPORT
73 static void
74 call_gmon_start(void)
75 {
76   extern void __gmon_start__ (void) __attribute__ ((weak)); /*weak_extern (__gmon_start__);*/
77   void (*gmon_start) (void) = __gmon_start__;
78
79   if (gmon_start)
80     gmon_start ();
81 }
82 #endif
83
84 SECTION (".init")
85 HIDDEN(_init)
86
87 extern void i_am_not_a_leaf (void);
88 extern void _init (void);
89 void _init (void)
90 {
91 #ifdef GMON_SUPPORT
92   /* We cannot use the normal constructor mechanism in gcrt1.o because it
93      appears before crtbegin.o in the link, so the header elt of .ctors
94      would come after the elt for __gmon_start__.  One approach is for
95      gcrt1.o to reference a symbol which would be defined by some library
96      module which has a constructor; but then user code's constructors
97      would come first, and not be profiled.  */
98   call_gmon_start ();
99 #else
100   asm ("\n/*@_init_PROLOG_PAUSES*/");
101   {
102     /* Let GCC know that _init is not a leaf function by having a dummy
103      * function call here.  We arrange for this call to be omitted from
104      * either crt file.  */
105     i_am_not_a_leaf ();
106   }
107   asm ("\n/*@_init_PROLOG_UNPAUSES*/");
108 #endif
109
110   asm ("ALIGN");
111   asm("END_INIT");
112   /* Now the epilog. */
113   asm ("\n/*@_init_PROLOG_ENDS*/");
114   asm ("\n/*@_init_EPILOG_BEGINS*/");
115 }
116
117 /* End of the _init epilog, beginning of the _fini prolog. */
118 asm ("\n/*@_init_EPILOG_ENDS*/");
119 asm ("\n/*@_fini_PROLOG_BEGINS*/");
120
121 SECTION (".fini")
122 HIDDEN(_fini)
123
124 extern void i_am_not_a_leaf2 (void);
125 extern void _fini (void);
126 void _fini (void)
127 {
128
129   /* End of the _fini prolog. */
130   asm ("ALIGN");
131   asm ("END_FINI");
132   asm ("\n/*@_fini_PROLOG_ENDS*/");
133
134   {
135     /* Let GCC know that _fini is not a leaf function by having a dummy
136        function call here.  We arrange for this call to be omitted from
137        either crt file.  */
138     i_am_not_a_leaf2 ();
139   }
140
141   /* Beginning of the _fini epilog. */
142   asm ("\n/*@_fini_EPILOG_BEGINS*/");
143 }
144
145 /* End of the _fini epilog.  Any further generated assembly (e.g. .ident)
146    is shared between both crt files. */
147 asm ("\n/*@_fini_EPILOG_ENDS*/");
148 asm ("\n/*@TRAILER_BEGINS*/");
149
150 /* End of file. */