Home | History | Annotate | Line # | Download | only in common
crt0-common.c revision 1.1.2.2
      1 /* $NetBSD: crt0-common.c,v 1.1.2.2 2011/03/05 15:09:14 bouyer Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998 Christos Zoulas
      5  * Copyright (c) 1995 Christopher G. Demetriou
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *          This product includes software developed for the
     19  *          NetBSD Project.  See http://www.NetBSD.org/ for
     20  *          information about NetBSD.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  *
     35  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __RCSID("$NetBSD: crt0-common.c,v 1.1.2.2 2011/03/05 15:09:14 bouyer Exp $");
     40 
     41 #include <sys/types.h>
     42 #include <sys/syscall.h>
     43 #include <machine/profile.h>
     44 #include <stdlib.h>
     45 #include <unistd.h>
     46 
     47 #include "rtld.h"
     48 
     49 extern int main(int, char **, char **);
     50 
     51 extern void	_init(void);
     52 extern void	_fini(void);
     53 
     54 /*
     55  * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
     56  * as being at address zero, unless something else defines it).  That way,
     57  * if we happen to be compiling without -static but with without any
     58  * shared libs present, things will still work.
     59  */
     60 
     61 __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
     62 
     63 #ifdef MCRT0
     64 extern void	monstartup(u_long, u_long);
     65 extern void	_mcleanup(void);
     66 extern unsigned char __etext, __eprol;
     67 #endif /* MCRT0 */
     68 
     69 char		**environ;
     70 struct ps_strings *__ps_strings = 0;
     71 
     72 static char	 empty_string[] = "";
     73 char		*__progname = empty_string;
     74 
     75 void		___start(int, char **, char **, void (*)(void),
     76     const Obj_Entry *, struct ps_strings *);
     77 
     78 #define	write(fd, s, n)	__syscall(SYS_write, (fd), (s), (n))
     79 
     80 #define	_FATAL(str)				\
     81 do {						\
     82 	write(2, str, sizeof(str)-1);		\
     83 	_exit(1);				\
     84 } while (0)
     85 
     86 void
     87 ___start(int argc, char **argv, char **envp,
     88     void (*cleanup)(void),			/* from shared loader */
     89     const Obj_Entry *obj,			/* from shared loader */
     90     struct ps_strings *ps_strings)
     91 {
     92 	environ = envp;
     93 
     94 	if (argv[0] != NULL) {
     95 		char *c;
     96 		__progname = argv[0];
     97 		for (c = argv[0]; *c; ++c) {
     98 			if (*c == '/')
     99 				__progname = c + 1;
    100 		}
    101 	} else {
    102 		__progname = empty_string;
    103 	}
    104 
    105 	if (ps_strings != NULL)
    106 		__ps_strings = ps_strings;
    107 
    108 	if (&rtld_DYNAMIC != NULL) {
    109 		if (obj == NULL)
    110 			_FATAL("NULL Obj_Entry pointer in GOT\n");
    111 		if (obj->magic != RTLD_MAGIC)
    112 			_FATAL("Corrupt Obj_Entry pointer in GOT\n");
    113 		if (obj->version != RTLD_VERSION)
    114 			_FATAL("Dynamic linker version mismatch\n");
    115 		atexit(cleanup);
    116 	}
    117 
    118 #ifdef MCRT0
    119 	atexit(_mcleanup);
    120 	monstartup((u_long)&__eprol, (u_long)&__etext);
    121 #endif
    122 
    123 	atexit(_fini);
    124 	_init();
    125 
    126 	exit(main(argc, argv, environ));
    127 }
    128