xref: /freebsd-13-stable/lib/csu/powerpc64/crt1_c.c (revision 3d497e17ebd33fe0f58d773e35ab994d750258d6)
1 /* LINTLIBRARY */
2 /*-
3  * SPDX-License-Identifier: BSD-4-Clause
4  *
5  * Copyright 2001 David E. O'Brien.
6  * All rights reserved.
7  * Copyright 1996-1998 John D. Polstra.
8  * All rights reserved.
9  * Copyright (c) 1997 Jason R. Thorpe.
10  * Copyright (c) 1995 Christopher G. Demetriou
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *          This product includes software developed for the
24  *          FreeBSD Project.  See https://www.freebsd.org/ for
25  *          information about FreeBSD.
26  *          This product includes software developed for the
27  *          NetBSD Project.  See http://www.netbsd.org/ for
28  *          information about NetBSD.
29  * 4. The name of the author may not be used to endorse or promote products
30  *    derived from this software without specific prior written permission
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  */
43 
44 #include <sys/cdefs.h>
45 #include <stdlib.h>
46 #include <stdint.h>
47 #include <sys/elf.h>
48 
49 static uint32_t cpu_features;
50 static uint32_t cpu_features2;
51 
52 #include "libc_private.h"
53 #include "ignore_init.c"
54 
55 struct Struct_Obj_Entry;
56 struct ps_strings;
57 
58 extern void _start(int, char **, char **, const struct Struct_Obj_Entry *,
59     void (*)(void), struct ps_strings *);
60 
61 #ifdef GCRT
62 extern void _mcleanup(void);
63 extern void monstartup(void *, void *);
64 extern int eprol;
65 extern int etext;
66 #endif
67 
68 struct ps_strings *__ps_strings;
69 
70 static void
init_cpu_features(char ** env)71 init_cpu_features(char **env)
72 {
73 	const Elf_Auxinfo *aux;
74 
75 	/* Find the auxiliary vector on the stack. */
76 	while (*env++ != 0)	/* Skip over environment, and NULL terminator */
77 		;
78 	aux = (const Elf_Auxinfo *)env;
79 
80 	/* Digest the auxiliary vector. */
81 	for (;  aux->a_type != AT_NULL; aux++) {
82 		switch (aux->a_type) {
83 		case AT_HWCAP:
84 			cpu_features = (uint32_t)aux->a_un.a_val;
85 			break;
86 		case AT_HWCAP2:
87 			cpu_features2 = (uint32_t)aux->a_un.a_val;
88 			break;
89 		}
90 	}
91 }
92 
93 
94 /* The entry function. */
95 /*
96  * First 5 arguments are specified by the PowerPC SVR4 ABI.
97  * The last argument, ps_strings, is a BSD extension.
98  */
99 /* ARGSUSED */
100 void
_start(int argc,char ** argv,char ** env,const struct Struct_Obj_Entry * obj __unused,void (* cleanup)(void),struct ps_strings * ps_strings)101 _start(int argc, char **argv, char **env,
102     const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void),
103     struct ps_strings *ps_strings)
104 {
105 
106 	handle_argv(argc, argv, env);
107 
108 	if (ps_strings != (struct ps_strings *)0)
109 		__ps_strings = ps_strings;
110 
111 	if (&_DYNAMIC != NULL)
112 		atexit(cleanup);
113 	else {
114 		init_cpu_features(env);
115 		process_irelocs();
116 		_init_tls();
117 	}
118 
119 #ifdef GCRT
120 	atexit(_mcleanup);
121 	monstartup(&eprol, &etext);
122 #endif
123 
124 	handle_static_init(argc, argv, env);
125 	exit(main(argc, argv, env));
126 }
127 
128 #ifdef GCRT
129 __asm__(".text");
130 __asm__("eprol:");
131 __asm__(".previous");
132 #endif
133