1129205Scognet/* LINTLIBRARY */
2129205Scognet/*-
3129205Scognet * Copyright 2001 David E. O'Brien.
4129205Scognet * All rights reserved.
5129205Scognet * Copyright 1996-1998 John D. Polstra.
6129205Scognet * All rights reserved.
7129205Scognet * Copyright (c) 1997 Jason R. Thorpe.
8129205Scognet * Copyright (c) 1995 Christopher G. Demetriou
9129205Scognet * All rights reserved.
10129205Scognet *
11129205Scognet * Redistribution and use in source and binary forms, with or without
12129205Scognet * modification, are permitted provided that the following conditions
13129205Scognet * are met:
14129205Scognet * 1. Redistributions of source code must retain the above copyright
15129205Scognet *    notice, this list of conditions and the following disclaimer.
16129205Scognet * 2. Redistributions in binary form must reproduce the above copyright
17129205Scognet *    notice, this list of conditions and the following disclaimer in the
18129205Scognet *    documentation and/or other materials provided with the distribution.
19129205Scognet * 3. All advertising materials mentioning features or use of this software
20129205Scognet *    must display the following acknowledgement:
21129205Scognet *          This product includes software developed for the
22129205Scognet *          FreeBSD Project.  See http://www.freebsd.org/ for
23129205Scognet *          information about FreeBSD.
24129205Scognet *          This product includes software developed for the
25129205Scognet *          NetBSD Project.  See http://www.netbsd.org/ for
26129205Scognet *          information about NetBSD.
27129205Scognet * 4. The name of the author may not be used to endorse or promote products
28129205Scognet *    derived from this software without specific prior written permission
29129205Scognet *
30129205Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31129205Scognet * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32129205Scognet * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33129205Scognet * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34129205Scognet * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35129205Scognet * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36129205Scognet * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37129205Scognet * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38129205Scognet * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39129205Scognet * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40129205Scognet */
41129205Scognet
42216338Sdim#include <sys/cdefs.h>
43216338Sdim__FBSDID("$FreeBSD$");
44216338Sdim
45129205Scognet#ifndef lint
46129205Scognet#ifndef __GNUC__
47129205Scognet#error "GCC is needed to compile this file"
48129205Scognet#endif
49129205Scognet#endif /* lint */
50129205Scognet
51129205Scognet#include <stdlib.h>
52129205Scognet
53129205Scognet#include "libc_private.h"
54129205Scognet#include "crtbrand.c"
55129205Scognet
56129205Scognetstruct Struct_Obj_Entry;
57129205Scognetstruct ps_strings;
58129205Scognet
59129205Scognetextern int _DYNAMIC;
60129205Scognet#pragma weak _DYNAMIC
61129205Scognet
62129205Scognetextern void _fini(void);
63129205Scognetextern void _init(void);
64129205Scognetextern int main(int, char **, char **);
65129205Scognetextern void _start(int, char **, char **, const struct Struct_Obj_Entry *,
66129205Scognet    void (*)(void), struct ps_strings *);
67129205Scognet
68129205Scognet#ifdef GCRT
69129205Scognetextern void _mcleanup(void);
70129205Scognetextern void monstartup(void *, void *);
71129205Scognetextern int eprol;
72129205Scognetextern int etext;
73129205Scognet#endif
74129205Scognet
75129205Scognetchar **environ;
76129205Scognetconst char *__progname = "";
77129205Scognetstruct ps_strings *__ps_strings;
78129205Scognet
79204756Suqsvoid __start(int, char **, char **, struct ps_strings *,
80204756Suqs    const struct Struct_Obj_Entry *, void (*)(void));
81204756Suqs
82129205Scognet/* The entry function. */
83129205Scognet__asm("	.text			\n"
84129205Scognet"	.align	0		\n"
85129205Scognet"	.globl	_start		\n"
86129205Scognet"	_start:			\n"
87129205Scognet"	mov	r5, r2		/* cleanup */		\n"
88129205Scognet"	mov	r4, r1		/* obj_main */		\n"
89129205Scognet"	mov	r3, r0		/* ps_strings */	\n"
90129205Scognet"	/* Get argc, argv, and envp from stack */	\n"
91129205Scognet"	ldr	r0, [sp, #0x0000]	\n"
92129205Scognet"	add	r1, sp, #0x0004		\n"
93129205Scognet"	add	r2, r1, r0, lsl #2	\n"
94129205Scognet"	add	r2, r2, #0x0004		\n"
95129205Scognet"	/* Ensure the stack is properly aligned before calling C code. */\n"
96129205Scognet"	bic	sp, sp, #7	\n"
97129205Scognet"	sub	sp, sp, #8	\n"
98129205Scognet"	str	r5, [sp, #4]	\n"
99129205Scognet"	str	r4, [sp, #0]	\n"
100129205Scognet"\n"
101129205Scognet"	b	 __start  ");
102129205Scognet/* ARGSUSED */
103129205Scognetvoid
104135679Scognet__start(int argc, char **argv, char **env, struct ps_strings *ps_strings,
105135679Scognet    const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void))
106129205Scognet{
107129205Scognet	const char *s;
108129205Scognet
109245777Skib	if (environ == NULL)
110245777Skib		environ = env;
111129205Scognet
112129205Scognet	if (argc > 0 && argv[0] != NULL) {
113129205Scognet		__progname = argv[0];
114129205Scognet		for (s = __progname; *s != '\0'; s++)
115129205Scognet			if (*s == '/')
116129205Scognet				__progname = s + 1;
117129205Scognet	}
118129205Scognet
119129205Scognet	if (ps_strings != (struct ps_strings *)0)
120129205Scognet		__ps_strings = ps_strings;
121129205Scognet
122129205Scognet	if (&_DYNAMIC != NULL)
123129205Scognet		atexit(cleanup);
124133754Sdfr	else
125133754Sdfr		_init_tls();
126129205Scognet#ifdef GCRT
127129205Scognet	atexit(_mcleanup);
128129205Scognet#endif
129129205Scognet	atexit(_fini);
130129205Scognet#ifdef GCRT
131129205Scognet	monstartup(&eprol, &etext);
132129205Scognet#endif
133129205Scognet	_init();
134129205Scognet	exit( main(argc, argv, env) );
135129205Scognet}
136129205Scognet
137129205Scognet#ifdef GCRT
138129205Scognet__asm__(".text");
139129205Scognet__asm__("eprol:");
140129205Scognet__asm__(".previous");
141129205Scognet#endif
142