crt1.c revision 103721
1/*-
2 * Copyright 2001 David E. O'Brien.
3 * All rights reserved.
4 * Copyright (c) 1995, 1998 Berkeley Software Design, Inc.
5 * All rights reserved.
6 * Copyright 1996-1998 John D. Polstra.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. The name of the authors may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef lint
33#ifndef __GNUC__
34#error "GCC is needed to compile this file"
35#endif
36#endif /* lint */
37
38#include <stdlib.h>
39
40#include "libc_private.h"
41#include "crtbrand.c"
42
43struct Struct_Obj_Entry;
44struct ps_strings;
45
46extern int _DYNAMIC;
47#pragma weak _DYNAMIC
48
49typedef void (*fptr)(void);
50
51extern void _fini(void);
52extern void _init(void);
53extern int main(int, char **, char **);
54extern void _start(char **, void (*)(void), struct Struct_Obj_Entry *,
55    struct ps_strings *);
56extern void __sparc_utrap_setup(void);
57
58#ifdef GCRT
59extern void _mcleanup(void);
60extern void monstartup(void *, void *);
61extern int eprol;
62extern int etext;
63#endif
64
65char **environ;
66const char *__progname = "";
67
68/*
69 * Grab %g1 before it gets used for anything by the compiler.
70 * Sparc ELF psABI specifies a termination routine (if any) will be in
71 * %g1
72 */
73static __inline fptr
74get_term(void)
75{
76	fptr retval;
77
78#if 0
79#ifdef	__GNUC__
80	__asm__ volatile("mov %%g1,%0" : "=r"(retval));
81#else
82	retval = (fptr)0; /* XXXX Fix this for other compilers */
83#endif
84#else
85	retval = (fptr)0; /* XXXX temporary */
86#endif
87	return(retval);
88}
89
90/* The entry function. */
91/*
92 * %o0 holds ps_strings pointer.  For Solaris compat and/or shared
93 * libraries, if %g1 is not 0, it is a routine to pass to atexit().
94 * (By passing the pointer in the usual argument register, we avoid
95 * having to do any inline assembly, except to recover %g1.)
96 *
97 * Note: kernel may (is not set in stone yet) pass ELF aux vector in %o1,
98 * but for now we do not use it here.
99 */
100/* ARGSUSED */
101void
102_start(char **ap, void (*cleanup)(void), struct Struct_Obj_Entry *obj __unused,
103    struct ps_strings *ps_strings __unused)
104{
105	void (*term)(void);
106	int argc;
107	char **argv;
108	char **env;
109	const char *s;
110
111	term = get_term();
112
113	argc = *(long *)(void *)ap;
114	argv = ap + 1;
115	env  = ap + 2 + argc;
116	environ = env;
117	if (argc > 0 && argv[0] != NULL) {
118		__progname = argv[0];
119		for (s = __progname; *s != '\0'; s++)
120			if (*s == '/')
121				__progname = s + 1;
122	}
123
124	__sparc_utrap_setup();
125
126	/*
127	 * If the kernel or a shared library wants us to call
128	 * a termination function, arrange to do so.
129	 */
130	if (term)
131		atexit(term);
132
133	if (&_DYNAMIC != NULL)
134		atexit(cleanup);
135
136#ifdef GCRT
137	atexit(_mcleanup);
138#endif
139	atexit(_fini);
140#ifdef GCRT
141	monstartup(&eprol, &etext);
142#endif
143	_init();
144	exit( main(argc, argv, env) );
145}
146
147#ifdef GCRT
148__asm__(".text");
149__asm__("eprol:");
150__asm__(".previous");
151#endif
152
153__asm__(".ident\t\"$FreeBSD: head/lib/csu/sparc64/crt1.c 103721 2002-09-20 22:23:32Z markm $\"");
154