1 /*-
2 * SPDX-License-Identifier: BSD-1-Clause
3 *
4 * Copyright 2012 Konstantin Belousov <kib@FreeBSD.org>
5 * Copyright (c) 2018 The FreeBSD Foundation
6 *
7 * Parts of this software was developed by Konstantin Belousov
8 * <kib@FreeBSD.org> 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/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/elf.h>
31 #include <sys/elf_common.h>
32
33 extern int main(int, char **, char **);
34
35 extern void (*__preinit_array_start[])(int, char **, char **) __hidden;
36 extern void (*__preinit_array_end[])(int, char **, char **) __hidden;
37 extern void (*__init_array_start[])(int, char **, char **) __hidden;
38 extern void (*__init_array_end[])(int, char **, char **) __hidden;
39 extern void (*__fini_array_start[])(void) __hidden;
40 extern void (*__fini_array_end[])(void) __hidden;
41 extern void _fini(void) __hidden;
42 extern void _init(void) __hidden;
43
44 extern int _DYNAMIC;
45 #pragma weak _DYNAMIC
46
47 #if defined(CRT_IRELOC_RELA)
48 extern const Elf_Rela __rela_iplt_start[] __weak_symbol __hidden;
49 extern const Elf_Rela __rela_iplt_end[] __weak_symbol __hidden;
50
51 #include "reloc.c"
52
53 static void
process_irelocs(void)54 process_irelocs(void)
55 {
56 const Elf_Rela *r;
57
58 for (r = &__rela_iplt_start[0]; r < &__rela_iplt_end[0]; r++)
59 crt1_handle_rela(r);
60 }
61 #elif defined(CRT_IRELOC_REL)
62 extern const Elf_Rel __rel_iplt_start[] __weak_symbol __hidden;
63 extern const Elf_Rel __rel_iplt_end[] __weak_symbol __hidden;
64
65 #include "reloc.c"
66
67 static void
process_irelocs(void)68 process_irelocs(void)
69 {
70 const Elf_Rel *r;
71
72 for (r = &__rel_iplt_start[0]; r < &__rel_iplt_end[0]; r++)
73 crt1_handle_rel(r);
74 }
75 #elif defined(CRT_IRELOC_SUPPRESS)
76 #else
77 #error "Define platform reloc type"
78 #endif
79
80 char **environ;
81 const char *__progname = "";
82
83 static void
finalizer(void)84 finalizer(void)
85 {
86 void (*fn)(void);
87 size_t array_size, n;
88
89 array_size = __fini_array_end - __fini_array_start;
90 for (n = array_size; n > 0; n--) {
91 fn = __fini_array_start[n - 1];
92 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
93 (fn)();
94 }
95 _fini();
96 }
97
98 static inline void
handle_static_init(int argc,char ** argv,char ** env)99 handle_static_init(int argc, char **argv, char **env)
100 {
101 void (*fn)(int, char **, char **);
102 size_t array_size, n;
103
104 if (&_DYNAMIC != NULL)
105 return;
106
107 atexit(finalizer);
108
109 array_size = __preinit_array_end - __preinit_array_start;
110 for (n = 0; n < array_size; n++) {
111 fn = __preinit_array_start[n];
112 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
113 fn(argc, argv, env);
114 }
115 _init();
116 array_size = __init_array_end - __init_array_start;
117 for (n = 0; n < array_size; n++) {
118 fn = __init_array_start[n];
119 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
120 fn(argc, argv, env);
121 }
122 }
123
124 static inline 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