Home | History | Annotate | Line # | Download | only in common
crt0-common.c revision 1.13
      1  1.13   matt /* $NetBSD: crt0-common.c,v 1.13 2013/01/31 22:24:25 matt Exp $ */
      2   1.1  joerg 
      3   1.1  joerg /*
      4   1.1  joerg  * Copyright (c) 1998 Christos Zoulas
      5   1.1  joerg  * Copyright (c) 1995 Christopher G. Demetriou
      6   1.1  joerg  * All rights reserved.
      7   1.1  joerg  *
      8   1.1  joerg  * Redistribution and use in source and binary forms, with or without
      9   1.1  joerg  * modification, are permitted provided that the following conditions
     10   1.1  joerg  * are met:
     11   1.1  joerg  * 1. Redistributions of source code must retain the above copyright
     12   1.1  joerg  *    notice, this list of conditions and the following disclaimer.
     13   1.1  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  joerg  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  joerg  *    documentation and/or other materials provided with the distribution.
     16   1.1  joerg  * 3. All advertising materials mentioning features or use of this software
     17   1.1  joerg  *    must display the following acknowledgement:
     18   1.1  joerg  *          This product includes software developed for the
     19   1.1  joerg  *          NetBSD Project.  See http://www.NetBSD.org/ for
     20   1.1  joerg  *          information about NetBSD.
     21   1.1  joerg  * 4. The name of the author may not be used to endorse or promote products
     22   1.1  joerg  *    derived from this software without specific prior written permission.
     23   1.1  joerg  *
     24   1.1  joerg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25   1.1  joerg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26   1.1  joerg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27   1.1  joerg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28   1.1  joerg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29   1.1  joerg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30   1.1  joerg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31   1.1  joerg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32   1.1  joerg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33   1.1  joerg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34   1.1  joerg  *
     35   1.1  joerg  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     36   1.1  joerg  */
     37   1.1  joerg 
     38   1.1  joerg #include <sys/cdefs.h>
     39  1.13   matt __RCSID("$NetBSD: crt0-common.c,v 1.13 2013/01/31 22:24:25 matt Exp $");
     40   1.1  joerg 
     41   1.1  joerg #include <sys/types.h>
     42   1.6  joerg #include <sys/exec.h>
     43   1.1  joerg #include <sys/syscall.h>
     44   1.1  joerg #include <machine/profile.h>
     45   1.1  joerg #include <stdlib.h>
     46   1.1  joerg #include <unistd.h>
     47   1.1  joerg 
     48   1.1  joerg #include "rtld.h"
     49   1.1  joerg 
     50   1.1  joerg extern int main(int, char **, char **);
     51   1.1  joerg 
     52   1.9   matt #ifndef HAVE_INITFINI_ARRAY
     53   1.1  joerg extern void	_init(void);
     54   1.1  joerg extern void	_fini(void);
     55   1.9   matt #endif
     56   1.5  joerg extern void	_libc_init(void);
     57   1.1  joerg 
     58   1.1  joerg /*
     59   1.1  joerg  * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
     60   1.1  joerg  * as being at address zero, unless something else defines it).  That way,
     61   1.1  joerg  * if we happen to be compiling without -static but with without any
     62   1.1  joerg  * shared libs present, things will still work.
     63   1.1  joerg  */
     64   1.3  joerg 
     65   1.4  joerg __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
     66   1.1  joerg 
     67   1.1  joerg #ifdef MCRT0
     68   1.1  joerg extern void	monstartup(u_long, u_long);
     69   1.1  joerg extern void	_mcleanup(void);
     70   1.1  joerg extern unsigned char __etext, __eprol;
     71   1.1  joerg #endif /* MCRT0 */
     72   1.1  joerg 
     73   1.1  joerg char		**environ;
     74   1.1  joerg struct ps_strings *__ps_strings = 0;
     75   1.1  joerg 
     76   1.1  joerg static char	 empty_string[] = "";
     77   1.1  joerg char		*__progname = empty_string;
     78   1.1  joerg 
     79   1.8  joerg __dead __dso_hidden void ___start(void (*)(void), const Obj_Entry *,
     80   1.6  joerg 			 struct ps_strings *);
     81   1.1  joerg 
     82   1.1  joerg #define	write(fd, s, n)	__syscall(SYS_write, (fd), (s), (n))
     83   1.1  joerg 
     84   1.1  joerg #define	_FATAL(str)				\
     85   1.1  joerg do {						\
     86   1.1  joerg 	write(2, str, sizeof(str)-1);		\
     87   1.1  joerg 	_exit(1);				\
     88   1.1  joerg } while (0)
     89   1.1  joerg 
     90   1.9   matt #ifdef HAVE_INITFINI_ARRAY
     91   1.9   matt /*
     92   1.9   matt  * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically,
     93   1.9   matt  * we have to process these instead of relying on RTLD to do it for us.
     94   1.9   matt  *
     95   1.9   matt  * Since we don't need .init or .fini sections, just code them in C
     96   1.9   matt  * to make life easier.
     97   1.9   matt  */
     98  1.13   matt __weakref_visible const fptr_t preinit_array_start[1]
     99  1.13   matt     __weak_reference(__preinit_array_start);
    100  1.13   matt __weakref_visible const fptr_t preinit_array_end[1]
    101  1.13   matt     __weak_reference(__preinit_array_end);
    102  1.12   matt __weakref_visible const fptr_t init_array_start[1]
    103  1.12   matt     __weak_reference(__init_array_start);
    104  1.12   matt __weakref_visible const fptr_t init_array_end[1]
    105  1.12   matt     __weak_reference(__init_array_end);
    106  1.12   matt __weakref_visible const fptr_t fini_array_start[1]
    107  1.12   matt     __weak_reference(__fini_array_start);
    108  1.12   matt __weakref_visible const fptr_t fini_array_end[1]
    109  1.12   matt     __weak_reference(__fini_array_end);
    110   1.9   matt 
    111   1.9   matt static inline void
    112  1.13   matt _preinit(void)
    113  1.13   matt {
    114  1.13   matt 	for (const fptr_t *f = preinit_array_start; f < preinit_array_end; f++) {
    115  1.13   matt 		(*f)();
    116  1.13   matt 	}
    117  1.13   matt }
    118  1.13   matt 
    119  1.13   matt static inline void
    120   1.9   matt _init(void)
    121   1.9   matt {
    122   1.9   matt 	for (const fptr_t *f = init_array_start; f < init_array_end; f++) {
    123   1.9   matt 		(*f)();
    124   1.9   matt 	}
    125   1.9   matt }
    126   1.9   matt 
    127   1.9   matt static void
    128   1.9   matt _fini(void)
    129   1.9   matt {
    130   1.9   matt 	for (const fptr_t *f = fini_array_start; f < fini_array_end; f++) {
    131   1.9   matt 		(*f)();
    132   1.9   matt 	}
    133   1.9   matt }
    134   1.9   matt #endif /* HAVE_INITFINI_ARRAY */
    135   1.9   matt 
    136   1.1  joerg void
    137   1.6  joerg ___start(void (*cleanup)(void),			/* from shared loader */
    138   1.1  joerg     const Obj_Entry *obj,			/* from shared loader */
    139   1.1  joerg     struct ps_strings *ps_strings)
    140   1.1  joerg {
    141   1.1  joerg 
    142   1.6  joerg 	if (ps_strings == NULL)
    143   1.6  joerg 		_FATAL("ps_strings missing\n");
    144   1.6  joerg 	__ps_strings = ps_strings;
    145   1.6  joerg 
    146   1.6  joerg 	environ = ps_strings->ps_envstr;
    147   1.6  joerg 
    148   1.6  joerg 	if (ps_strings->ps_argvstr[0] != NULL) {
    149   1.1  joerg 		char *c;
    150   1.6  joerg 		__progname = ps_strings->ps_argvstr[0];
    151   1.6  joerg 		for (c = ps_strings->ps_argvstr[0]; *c; ++c) {
    152   1.1  joerg 			if (*c == '/')
    153   1.1  joerg 				__progname = c + 1;
    154   1.1  joerg 		}
    155   1.1  joerg 	} else {
    156   1.1  joerg 		__progname = empty_string;
    157   1.1  joerg 	}
    158   1.1  joerg 
    159   1.4  joerg 	if (&rtld_DYNAMIC != NULL) {
    160   1.2   matt 		if (obj == NULL)
    161   1.2   matt 			_FATAL("NULL Obj_Entry pointer in GOT\n");
    162   1.2   matt 		if (obj->magic != RTLD_MAGIC)
    163   1.1  joerg 			_FATAL("Corrupt Obj_Entry pointer in GOT\n");
    164   1.1  joerg 		if (obj->version != RTLD_VERSION)
    165   1.1  joerg 			_FATAL("Dynamic linker version mismatch\n");
    166   1.1  joerg 		atexit(cleanup);
    167   1.1  joerg 	}
    168   1.1  joerg 
    169   1.5  joerg 	_libc_init();
    170   1.5  joerg 
    171  1.13   matt #ifdef HAVE_INITFINI_ARRAY
    172  1.13   matt 	_preinit();
    173  1.13   matt #endif
    174  1.13   matt 
    175   1.1  joerg #ifdef MCRT0
    176   1.1  joerg 	atexit(_mcleanup);
    177   1.1  joerg 	monstartup((u_long)&__eprol, (u_long)&__etext);
    178   1.1  joerg #endif
    179   1.1  joerg 
    180   1.1  joerg 	atexit(_fini);
    181   1.1  joerg 	_init();
    182   1.1  joerg 
    183   1.6  joerg 	exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ));
    184   1.1  joerg }
    185