Home | History | Annotate | Line # | Download | only in common
crt0-common.c revision 1.3
      1  1.3  joerg /* $NetBSD: crt0-common.c,v 1.3 2011/02/18 23:37:36 joerg 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.3  joerg __RCSID("$NetBSD: crt0-common.c,v 1.3 2011/02/18 23:37:36 joerg Exp $");
     40  1.1  joerg 
     41  1.1  joerg #include <sys/types.h>
     42  1.1  joerg #include <sys/syscall.h>
     43  1.1  joerg #include <machine/profile.h>
     44  1.1  joerg #include <stdlib.h>
     45  1.1  joerg #include <unistd.h>
     46  1.1  joerg 
     47  1.1  joerg #include "rtld.h"
     48  1.1  joerg 
     49  1.1  joerg extern int main(int, char **, char **);
     50  1.1  joerg 
     51  1.1  joerg extern void	_init(void);
     52  1.1  joerg extern void	_fini(void);
     53  1.1  joerg 
     54  1.1  joerg /*
     55  1.1  joerg  * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
     56  1.1  joerg  * as being at address zero, unless something else defines it).  That way,
     57  1.1  joerg  * if we happen to be compiling without -static but with without any
     58  1.1  joerg  * shared libs present, things will still work.
     59  1.1  joerg  */
     60  1.3  joerg 
     61  1.3  joerg #if __GNUC_PREREQ__(4,2)
     62  1.3  joerg static int rtld_DYNAMIC __attribute__((__weakref__, __alias__("_DYNAMIC")));
     63  1.3  joerg #define	DYNAMIC_SYM	rtld_DYNAMIC
     64  1.3  joerg #else
     65  1.1  joerg extern int _DYNAMIC __weak_reference(_DYNAMIC);
     66  1.3  joerg #define	DYNAMIC_SYM	_DYNAMIC
     67  1.3  joerg #endif
     68  1.1  joerg 
     69  1.1  joerg #ifdef MCRT0
     70  1.1  joerg extern void	monstartup(u_long, u_long);
     71  1.1  joerg extern void	_mcleanup(void);
     72  1.1  joerg extern unsigned char __etext, __eprol;
     73  1.1  joerg #endif /* MCRT0 */
     74  1.1  joerg 
     75  1.1  joerg char		**environ;
     76  1.1  joerg struct ps_strings *__ps_strings = 0;
     77  1.1  joerg 
     78  1.1  joerg static char	 empty_string[] = "";
     79  1.1  joerg char		*__progname = empty_string;
     80  1.1  joerg 
     81  1.1  joerg void		___start(int, char **, char **, void (*)(void),
     82  1.1  joerg     const Obj_Entry *, struct ps_strings *);
     83  1.1  joerg 
     84  1.1  joerg #define	write(fd, s, n)	__syscall(SYS_write, (fd), (s), (n))
     85  1.1  joerg 
     86  1.1  joerg #define	_FATAL(str)				\
     87  1.1  joerg do {						\
     88  1.1  joerg 	write(2, str, sizeof(str)-1);		\
     89  1.1  joerg 	_exit(1);				\
     90  1.1  joerg } while (0)
     91  1.1  joerg 
     92  1.1  joerg void
     93  1.1  joerg ___start(int argc, char **argv, char **envp,
     94  1.1  joerg     void (*cleanup)(void),			/* from shared loader */
     95  1.1  joerg     const Obj_Entry *obj,			/* from shared loader */
     96  1.1  joerg     struct ps_strings *ps_strings)
     97  1.1  joerg {
     98  1.1  joerg 	environ = envp;
     99  1.1  joerg 
    100  1.1  joerg 	if (argv[0] != NULL) {
    101  1.1  joerg 		char *c;
    102  1.1  joerg 		__progname = argv[0];
    103  1.1  joerg 		for (c = argv[0]; *c; ++c) {
    104  1.1  joerg 			if (*c == '/')
    105  1.1  joerg 				__progname = c + 1;
    106  1.1  joerg 		}
    107  1.1  joerg 	} else {
    108  1.1  joerg 		__progname = empty_string;
    109  1.1  joerg 	}
    110  1.1  joerg 
    111  1.1  joerg 	if (ps_strings != NULL)
    112  1.1  joerg 		__ps_strings = ps_strings;
    113  1.1  joerg 
    114  1.3  joerg 	if (&DYNAMIC_SYM != NULL) {
    115  1.2   matt 		if (obj == NULL)
    116  1.2   matt 			_FATAL("NULL Obj_Entry pointer in GOT\n");
    117  1.2   matt 		if (obj->magic != RTLD_MAGIC)
    118  1.1  joerg 			_FATAL("Corrupt Obj_Entry pointer in GOT\n");
    119  1.1  joerg 		if (obj->version != RTLD_VERSION)
    120  1.1  joerg 			_FATAL("Dynamic linker version mismatch\n");
    121  1.1  joerg 		atexit(cleanup);
    122  1.1  joerg 	}
    123  1.1  joerg 
    124  1.1  joerg #ifdef MCRT0
    125  1.1  joerg 	atexit(_mcleanup);
    126  1.1  joerg 	monstartup((u_long)&__eprol, (u_long)&__etext);
    127  1.1  joerg #endif
    128  1.1  joerg 
    129  1.1  joerg 	atexit(_fini);
    130  1.1  joerg 	_init();
    131  1.1  joerg 
    132  1.1  joerg 	exit(main(argc, argv, environ));
    133  1.1  joerg }
    134