crt1.c revision 300323
1/* LINTLIBRARY */
2/*-
3 * Copyright 2001 David E. O'Brien.
4 * All rights reserved.
5 * Copyright 1996-1998 John D. Polstra.
6 * All rights reserved.
7 * Copyright (c) 1997 Jason R. Thorpe.
8 * Copyright (c) 1995 Christopher G. Demetriou
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *          This product includes software developed for the
22 *          FreeBSD Project.  See http://www.freebsd.org/ for
23 *          information about FreeBSD.
24 *          This product includes software developed for the
25 *          NetBSD Project.  See http://www.netbsd.org/ for
26 *          information about NetBSD.
27 * 4. The name of the author may not be used to endorse or promote products
28 *    derived from this software without specific prior written permission
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: stable/10/lib/csu/arm/crt1.c 300323 2016-05-20 19:14:15Z emaste $");
44
45#include <stdlib.h>
46
47#include "libc_private.h"
48#include "crtbrand.c"
49#include "ignore_init.c"
50
51struct Struct_Obj_Entry;
52struct ps_strings;
53
54extern void _start(int, char **, char **, const struct Struct_Obj_Entry *,
55    void (*)(void), struct ps_strings *);
56
57#ifdef GCRT
58extern void _mcleanup(void);
59extern void monstartup(void *, void *);
60extern int eprol;
61extern int etext;
62#endif
63
64struct ps_strings *__ps_strings;
65
66void __start(int, char **, char **, struct ps_strings *,
67    const struct Struct_Obj_Entry *, void (*)(void));
68
69/* The entry function. */
70__asm("	.text			\n"
71"	.align	0		\n"
72"	.globl	_start		\n"
73"	_start:			\n"
74"	mov	r5, r2		/* cleanup */		\n"
75"	mov	r4, r1		/* obj_main */		\n"
76"	mov	r3, r0		/* ps_strings */	\n"
77"	/* Get argc, argv, and envp from stack */	\n"
78"	ldr	r0, [sp, #0x0000]	\n"
79"	add	r1, sp, #0x0004		\n"
80"	add	r2, r1, r0, lsl #2	\n"
81"	add	r2, r2, #0x0004		\n"
82"	/* Ensure the stack is properly aligned before calling C code. */\n"
83"	bic	sp, sp, #7	\n"
84"	sub	sp, sp, #8	\n"
85"	str	r5, [sp, #4]	\n"
86"	str	r4, [sp, #0]	\n"
87"\n"
88"	b	 __start  ");
89/* ARGSUSED */
90void
91__start(int argc, char **argv, char **env, struct ps_strings *ps_strings,
92    const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void))
93{
94
95	handle_argv(argc, argv, env);
96
97	if (ps_strings != (struct ps_strings *)0)
98		__ps_strings = ps_strings;
99
100	if (&_DYNAMIC != NULL)
101		atexit(cleanup);
102	else
103		_init_tls();
104#ifdef GCRT
105	atexit(_mcleanup);
106	monstartup(&eprol, &etext);
107#endif
108	handle_static_init(argc, argv, env);
109	exit(main(argc, argv, env));
110}
111
112static const struct {
113	int32_t	namesz;
114	int32_t	descsz;
115	int32_t	type;
116	char	name[sizeof(NOTE_FREEBSD_VENDOR)];
117	char	desc[sizeof(MACHINE_ARCH)];
118} archtag __attribute__ ((section (NOTE_SECTION), aligned(4))) __used = {
119	.namesz = sizeof(NOTE_FREEBSD_VENDOR),
120	.descsz = sizeof(MACHINE_ARCH),
121	.type = ARCH_NOTETYPE,
122	.name = NOTE_FREEBSD_VENDOR,
123	.desc = MACHINE_ARCH
124};
125
126#ifdef GCRT
127__asm__(".text");
128__asm__("eprol:");
129__asm__(".previous");
130#endif
131