]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/csu/common/ignore_init.c
Import DTS files from Linux 5.0
[FreeBSD/FreeBSD.git] / lib / csu / common / ignore_init.c
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 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/elf.h>
33 #include <sys/elf_common.h>
34
35 #include "notes.h"
36
37 extern int main(int, char **, char **);
38
39 extern void (*__preinit_array_start[])(int, char **, char **) __hidden;
40 extern void (*__preinit_array_end[])(int, char **, char **) __hidden;
41 extern void (*__init_array_start[])(int, char **, char **) __hidden;
42 extern void (*__init_array_end[])(int, char **, char **) __hidden;
43 extern void (*__fini_array_start[])(void) __hidden;
44 extern void (*__fini_array_end[])(void) __hidden;
45 extern void _fini(void) __hidden;
46 extern void _init(void) __hidden;
47
48 extern int _DYNAMIC;
49 #pragma weak _DYNAMIC
50
51 #if defined(CRT_IRELOC_RELA)
52 extern const Elf_Rela __rela_iplt_start[] __weak_symbol __hidden;
53 extern const Elf_Rela __rela_iplt_end[] __weak_symbol __hidden;
54
55 #include "reloc.c"
56
57 static void
58 process_irelocs(void)
59 {
60         const Elf_Rela *r;
61
62         for (r = &__rela_iplt_start[0]; r < &__rela_iplt_end[0]; r++)
63                 crt1_handle_rela(r);
64 }
65 #elif defined(CRT_IRELOC_REL)
66 extern const Elf_Rel __rel_iplt_start[] __weak_symbol __hidden;
67 extern const Elf_Rel __rel_iplt_end[] __weak_symbol __hidden;
68
69 #include "reloc.c"
70
71 static void
72 process_irelocs(void)
73 {
74         const Elf_Rel *r;
75
76         for (r = &__rel_iplt_start[0]; r < &__rel_iplt_end[0]; r++)
77                 crt1_handle_rel(r);
78 }
79 #elif defined(CRT_IRELOC_SUPPRESS)
80 #else
81 #error "Define platform reloc type"
82 #endif
83
84 char **environ;
85 const char *__progname = "";
86
87 static void
88 finalizer(void)
89 {
90         void (*fn)(void);
91         size_t array_size, n;
92
93         array_size = __fini_array_end - __fini_array_start;
94         for (n = array_size; n > 0; n--) {
95                 fn = __fini_array_start[n - 1];
96                 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
97                         (fn)();
98         }
99         _fini();
100 }
101
102 static inline void
103 handle_static_init(int argc, char **argv, char **env)
104 {
105         void (*fn)(int, char **, char **);
106         size_t array_size, n;
107
108         if (&_DYNAMIC != NULL)
109                 return;
110
111         atexit(finalizer);
112
113         array_size = __preinit_array_end - __preinit_array_start;
114         for (n = 0; n < array_size; n++) {
115                 fn = __preinit_array_start[n];
116                 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
117                         fn(argc, argv, env);
118         }
119         _init();
120         array_size = __init_array_end - __init_array_start;
121         for (n = 0; n < array_size; n++) {
122                 fn = __init_array_start[n];
123                 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
124                         fn(argc, argv, env);
125         }
126 }
127
128 static inline void
129 handle_argv(int argc, char *argv[], char **env)
130 {
131         const char *s;
132
133         if (environ == NULL)
134                 environ = env;
135         if (argc > 0 && argv[0] != NULL) {
136                 __progname = argv[0];
137                 for (s = __progname; *s != '\0'; s++) {
138                         if (*s == '/')
139                                 __progname = s + 1;
140                 }
141         }
142 }
143
144 static const struct {
145         int32_t namesz;
146         int32_t descsz;
147         int32_t type;
148         char    name[sizeof(NOTE_FREEBSD_VENDOR)];
149         uint32_t desc;
150 } crt_noinit_tag __attribute__ ((section (NOTE_SECTION),
151     aligned(4))) __used = {
152         .namesz = sizeof(NOTE_FREEBSD_VENDOR),
153         .descsz = sizeof(uint32_t),
154         .type = NT_FREEBSD_NOINIT_TAG,
155         .name = NOTE_FREEBSD_VENDOR,
156         .desc = 0
157 };