Back to index

glibc  2.9
elf-init.c
Go to the documentation of this file.
00001 /* Startup support for ELF initializers/finalizers in the main executable.
00002    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    In addition to the permissions in the GNU Lesser General Public
00011    License, the Free Software Foundation gives you unlimited
00012    permission to link the compiled version of this file with other
00013    programs, and to distribute those programs without any restriction
00014    coming from the use of this file. (The GNU Lesser General Public
00015    License restrictions do apply in other respects; for example, they
00016    cover modification of the file, and distribution when not linked
00017    into another program.)
00018 
00019    Note that people who make modified versions of this file are not
00020    obligated to grant this special exception for their modified
00021    versions; it is their choice whether to do so. The GNU Lesser
00022    General Public License gives permission to release a modified
00023    version without this exception; this exception also makes it
00024    possible to release a modified version which carries forward this
00025    exception.
00026 
00027    The GNU C Library is distributed in the hope that it will be useful,
00028    but WITHOUT ANY WARRANTY; without even the implied warranty of
00029    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00030    Lesser General Public License for more details.
00031 
00032    You should have received a copy of the GNU Lesser General Public
00033    License along with the GNU C Library; if not, write to the Free
00034    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00035    02111-1307 USA.  */
00036 
00037 #include <stddef.h>
00038 
00039 
00040 /* These magic symbols are provided by the linker.  */
00041 extern void (*__preinit_array_start []) (int, char **, char **)
00042   attribute_hidden;
00043 extern void (*__preinit_array_end []) (int, char **, char **)
00044   attribute_hidden;
00045 extern void (*__init_array_start []) (int, char **, char **)
00046   attribute_hidden;
00047 extern void (*__init_array_end []) (int, char **, char **)
00048   attribute_hidden;
00049 extern void (*__fini_array_start []) (void) attribute_hidden;
00050 extern void (*__fini_array_end []) (void) attribute_hidden;
00051 
00052 
00053 /* These function symbols are provided for the .init/.fini section entry
00054    points automagically by the linker.  */
00055 extern void _init (void);
00056 extern void _fini (void);
00057 
00058 /* These functions are passed to __libc_start_main by the startup code.
00059    These get statically linked into each program.  For dynamically linked
00060    programs, this module will come from libc_nonshared.a and differs from
00061    the libc.a module in that it doesn't call the preinit array.  */
00062 
00063 void
00064 __libc_csu_init (int argc, char **argv, char **envp)
00065 {
00066   /* For dynamically linked executables the preinit array is executed by
00067      the dynamic linker (before initializing any shared object.  */
00068 
00069 #ifndef LIBC_NONSHARED
00070   /* For static executables, preinit happens rights before init.  */
00071   {
00072     const size_t size = __preinit_array_end - __preinit_array_start;
00073     size_t i;
00074     for (i = 0; i < size; i++)
00075       (*__preinit_array_start [i]) (argc, argv, envp);
00076   }
00077 #endif
00078 
00079   _init ();
00080 
00081   const size_t size = __init_array_end - __init_array_start;
00082   for (size_t i = 0; i < size; i++)
00083       (*__init_array_start [i]) (argc, argv, envp);
00084 }
00085 
00086 /* This function should not be used anymore.  We run the executable's
00087    destructor now just like any other.  We cannot remove the function,
00088    though.  */
00089 void
00090 __libc_csu_fini (void)
00091 {
00092 #ifndef LIBC_NONSHARED
00093   size_t i = __fini_array_end - __fini_array_start;
00094   while (i-- > 0)
00095     (*__fini_array_start [i]) ();
00096 
00097   _fini ();
00098 #endif
00099 }