c28b73ca624ed08546daa9ec79bf7b55af67d835
[platform/upstream/glibc.git] / csu / elf-init.c
1 /* Startup support for ELF initializers/finalizers in the main executable.
2    Copyright (C) 2002 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    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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <stddef.h>
21
22 #ifdef HAVE_INITFINI_ARRAY
23 /* These magic symbols are provided by the linker.  */
24 extern void (*__preinit_array_start []) (void);
25 extern void (*__preinit_array_end []) (void);
26 extern void (*__init_array_start []) (void);
27 extern void (*__init_array_end []) (void);
28 extern void (*__fini_array_start []) (void);
29 extern void (*__fini_array_end []) (void);
30 #endif
31
32 /* These function symbols are provided for the .init/.fini section entry
33    points automagically by the linker.  */
34 extern void _init (void);
35 extern void _fini (void);
36
37 /* These functions are passed to __libc_start_main by the startup code.
38    These get statically linked into each program.  For dynamically linked
39    programs, this module will come from libc_nonshared.a and differs from
40    the libc.a module in that it doesn't call the preinit array.  */
41
42 void
43 __libc_csu_init (void)
44 {
45 #ifdef HAVE_INITFINI_ARRAY
46   /* For dynamically linked executables the preinit array is executed by
47      the dynamic linker (before initializing any shared object.  */
48
49 # ifndef LIBC_NONSHARED
50   /* For static executables, preinit happens rights before init.  */
51   {
52     const size_t size = __preinit_array_end - __preinit_array_start;
53     size_t i;
54     for (i = 0; i < size; i++)
55       (*__preinit_array_start [i]) ();
56   }
57 # endif
58 #endif
59
60   _init ();
61
62 #ifdef HAVE_INITFINI_ARRAY
63   {
64     const size_t size = __init_array_end - __init_array_start;
65     size_t i;
66     for (i = 0; i < size; i++)
67       (*__init_array_start [i]) ();
68   }
69 #endif
70 }
71
72 void
73 __libc_csu_fini (void)
74 {
75 #ifdef HAVE_INITFINI_ARRAY
76   size_t i = __fini_array_end - __fini_array_start;
77   while (i-- > 0)
78     (*__fini_array_start [i]) ();
79 #endif
80
81   _fini ();
82 }