1 /*-
2 * SPDX-License-Identifier: BSD-1-Clause
3 *
4 * Copyright 2012 Konstantin Belousov <[email protected]>
5 * Copyright (c) 2018, 2023 The FreeBSD Foundation
6 *
7 * Parts of this software was developed by Konstantin Belousov
8 * <[email protected]> under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/elf.h>
30 #include <sys/elf_common.h>
31 #include <stdlib.h>
32 #include "libc_private.h"
33
34 extern void (*__preinit_array_start[])(int, char **, char **) __hidden;
35 extern void (*__preinit_array_end[])(int, char **, char **) __hidden;
36 extern void (*__init_array_start[])(int, char **, char **) __hidden;
37 extern void (*__init_array_end[])(int, char **, char **) __hidden;
38 extern void (*__fini_array_start[])(void) __hidden;
39 extern void (*__fini_array_end[])(void) __hidden;
40 extern void _fini(void) __hidden;
41 extern void _init(void) __hidden;
42
43 extern int _DYNAMIC;
44 #pragma weak _DYNAMIC
45
46 #if defined(CRT_IRELOC_RELA)
47 extern const Elf_Rela __rela_iplt_start[] __weak_symbol __hidden;
48 extern const Elf_Rela __rela_iplt_end[] __weak_symbol __hidden;
49
50 #include "reloc.c"
51
52 static void
process_irelocs(void)53 process_irelocs(void)
54 {
55 const Elf_Rela *r;
56
57 for (r = &__rela_iplt_start[0]; r < &__rela_iplt_end[0]; r++)
58 crt1_handle_rela(r);
59 }
60 #elif defined(CRT_IRELOC_REL)
61 extern const Elf_Rel __rel_iplt_start[] __weak_symbol __hidden;
62 extern const Elf_Rel __rel_iplt_end[] __weak_symbol __hidden;
63
64 #include "reloc.c"
65
66 static void
process_irelocs(void)67 process_irelocs(void)
68 {
69 const Elf_Rel *r;
70
71 for (r = &__rel_iplt_start[0]; r < &__rel_iplt_end[0]; r++)
72 crt1_handle_rel(r);
73 }
74 #elif defined(CRT_IRELOC_SUPPRESS)
75 #else
76 #error "Define platform reloc type"
77 #endif
78
79 #ifndef PIC
80 static void
finalizer(void)81 finalizer(void)
82 {
83 void (*fn)(void);
84 size_t array_size, n;
85
86 array_size = __fini_array_end - __fini_array_start;
87 for (n = array_size; n > 0; n--) {
88 fn = __fini_array_start[n - 1];
89 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
90 (fn)();
91 }
92 _fini();
93 }
94 #endif
95
96 static void
handle_static_init(int argc,char ** argv,char ** env)97 handle_static_init(int argc, char **argv, char **env)
98 {
99 #ifndef PIC
100 void (*fn)(int, char **, char **);
101 size_t array_size, n;
102
103 if (&_DYNAMIC != NULL)
104 return;
105
106 atexit(finalizer);
107
108 array_size = __preinit_array_end - __preinit_array_start;
109 for (n = 0; n < array_size; n++) {
110 fn = __preinit_array_start[n];
111 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
112 fn(argc, argv, env);
113 }
114 _init();
115 array_size = __init_array_end - __init_array_start;
116 for (n = 0; n < array_size; n++) {
117 fn = __init_array_start[n];
118 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
119 fn(argc, argv, env);
120 }
121 #endif
122 }
123
124 static void
handle_argv(int argc,char * argv[],char ** env)125 handle_argv(int argc, char *argv[], char **env)
126 {
127 const char *s;
128
129 if (environ == NULL)
130 environ = env;
131 if (argc > 0 && argv[0] != NULL) {
132 __progname = argv[0];
133 for (s = __progname; *s != '\0'; s++) {
134 if (*s == '/')
135 __progname = s + 1;
136 }
137 }
138 }
139
140 void
__libc_start1(int argc,char * argv[],char * env[],void (* cleanup)(void),int (* mainX)(int,char * [],char * []))141 __libc_start1(int argc, char *argv[], char *env[], void (*cleanup)(void),
142 int (*mainX)(int, char *[], char *[]))
143 {
144 handle_argv(argc, argv, env);
145
146 if (&_DYNAMIC != NULL) {
147 atexit(cleanup);
148 } else {
149 #ifndef CRT_IRELOC_SUPPRESS
150 INIT_IRELOCS;
151 process_irelocs();
152 #endif
153 _init_tls();
154 }
155
156 handle_static_init(argc, argv, env);
157 exit(mainX(argc, argv, env));
158 }
159
160 /* XXXKIB _mcleanup and monstartup defs */
161 extern void _mcleanup(void);
162 extern void monstartup(void *, void *);
163
164 void
__libc_start1_gcrt(int argc,char * argv[],char * env[],void (* cleanup)(void),int (* mainX)(int,char * [],char * []),int * eprolp,int * etextp)165 __libc_start1_gcrt(int argc, char *argv[], char *env[],
166 void (*cleanup)(void), int (*mainX)(int, char *[], char *[]),
167 int *eprolp, int *etextp)
168 {
169 handle_argv(argc, argv, env);
170
171 if (&_DYNAMIC != NULL) {
172 atexit(cleanup);
173 } else {
174 #ifndef CRT_IRELOC_SUPPRESS
175 INIT_IRELOCS;
176 process_irelocs();
177 #endif
178 _init_tls();
179 }
180
181 atexit(_mcleanup);
182 monstartup(eprolp, etextp);
183
184 handle_static_init(argc, argv, env);
185 exit(mainX(argc, argv, env));
186 }
187