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"
55232832Skib#include "ignore_init.c"
56129205Scognet
57129205Scognetstruct Struct_Obj_Entry;
58129205Scognetstruct ps_strings;
59129205Scognet
60129205Scognetextern void _start(int, char **, char **, const struct Struct_Obj_Entry *,
61129205Scognet    void (*)(void), struct ps_strings *);
62129205Scognet
63129205Scognet#ifdef GCRT
64129205Scognetextern void _mcleanup(void);
65129205Scognetextern void monstartup(void *, void *);
66129205Scognetextern int eprol;
67129205Scognetextern int etext;
68129205Scognet#endif
69129205Scognet
70129205Scognetstruct ps_strings *__ps_strings;
71129205Scognet
72204756Suqsvoid __start(int, char **, char **, struct ps_strings *,
73204756Suqs    const struct Struct_Obj_Entry *, void (*)(void));
74204756Suqs
75129205Scognet/* The entry function. */
76129205Scognet__asm("	.text			\n"
77129205Scognet"	.align	0		\n"
78129205Scognet"	.globl	_start		\n"
79129205Scognet"	_start:			\n"
80129205Scognet"	mov	r5, r2		/* cleanup */		\n"
81129205Scognet"	mov	r4, r1		/* obj_main */		\n"
82129205Scognet"	mov	r3, r0		/* ps_strings */	\n"
83129205Scognet"	/* Get argc, argv, and envp from stack */	\n"
84129205Scognet"	ldr	r0, [sp, #0x0000]	\n"
85129205Scognet"	add	r1, sp, #0x0004		\n"
86129205Scognet"	add	r2, r1, r0, lsl #2	\n"
87129205Scognet"	add	r2, r2, #0x0004		\n"
88129205Scognet"	/* Ensure the stack is properly aligned before calling C code. */\n"
89129205Scognet"	bic	sp, sp, #7	\n"
90129205Scognet"	sub	sp, sp, #8	\n"
91129205Scognet"	str	r5, [sp, #4]	\n"
92129205Scognet"	str	r4, [sp, #0]	\n"
93129205Scognet"\n"
94129205Scognet"	b	 __start  ");
95129205Scognet/* ARGSUSED */
96129205Scognetvoid
97135679Scognet__start(int argc, char **argv, char **env, struct ps_strings *ps_strings,
98135679Scognet    const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void))
99129205Scognet{
100129205Scognet
101245133Skib	handle_argv(argc, argv, env);
102129205Scognet
103129205Scognet	if (ps_strings != (struct ps_strings *)0)
104129205Scognet		__ps_strings = ps_strings;
105129205Scognet
106129205Scognet	if (&_DYNAMIC != NULL)
107129205Scognet		atexit(cleanup);
108133754Sdfr	else
109133754Sdfr		_init_tls();
110129205Scognet#ifdef GCRT
111129205Scognet	atexit(_mcleanup);
112129205Scognet	monstartup(&eprol, &etext);
113129205Scognet#endif
114232832Skib	handle_static_init(argc, argv, env);
115232832Skib	exit(main(argc, argv, env));
116129205Scognet}
117129205Scognet
118255874Sandrewstatic const struct {
119255874Sandrew	int32_t	namesz;
120255874Sandrew	int32_t	descsz;
121255874Sandrew	int32_t	type;
122255874Sandrew	char	name[sizeof(NOTE_FREEBSD_VENDOR)];
123255874Sandrew	char	desc[sizeof(MACHINE_ARCH)];
124255874Sandrew} archtag __attribute__ ((section (NOTE_SECTION), aligned(4))) __used = {
125255874Sandrew	.namesz = sizeof(NOTE_FREEBSD_VENDOR),
126266410Sian	.descsz = sizeof(MACHINE_ARCH),
127255874Sandrew	.type = ARCH_NOTETYPE,
128255874Sandrew	.name = NOTE_FREEBSD_VENDOR,
129255874Sandrew	.desc = MACHINE_ARCH
130255874Sandrew};
131255874Sandrew
132129205Scognet#ifdef GCRT
133129205Scognet__asm__(".text");
134129205Scognet__asm__("eprol:");
135129205Scognet__asm__(".previous");
136129205Scognet#endif
137