]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/csu/common/ignore_init.c
crt: switch to standard note type definitions from elf_common.h
[FreeBSD/FreeBSD.git] / lib / csu / common / ignore_init.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 2012 Konstantin Belousov <kib@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
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_common.h>
33 #include "notes.h"
34
35 extern int main(int, char **, char **);
36
37 extern void (*__preinit_array_start[])(int, char **, char **) __hidden;
38 extern void (*__preinit_array_end[])(int, char **, char **) __hidden;
39 extern void (*__init_array_start[])(int, char **, char **) __hidden;
40 extern void (*__init_array_end[])(int, char **, char **) __hidden;
41 extern void (*__fini_array_start[])(void) __hidden;
42 extern void (*__fini_array_end[])(void) __hidden;
43 extern void _fini(void) __hidden;
44 extern void _init(void) __hidden;
45
46 extern int _DYNAMIC;
47 #pragma weak _DYNAMIC
48
49 char **environ;
50 const char *__progname = "";
51
52 static void
53 finalizer(void)
54 {
55         void (*fn)(void);
56         size_t array_size, n;
57
58         array_size = __fini_array_end - __fini_array_start;
59         for (n = array_size; n > 0; n--) {
60                 fn = __fini_array_start[n - 1];
61                 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
62                         (fn)();
63         }
64         _fini();
65 }
66
67 static inline void
68 handle_static_init(int argc, char **argv, char **env)
69 {
70         void (*fn)(int, char **, char **);
71         size_t array_size, n;
72
73         if (&_DYNAMIC != NULL)
74                 return;
75
76         atexit(finalizer);
77
78         array_size = __preinit_array_end - __preinit_array_start;
79         for (n = 0; n < array_size; n++) {
80                 fn = __preinit_array_start[n];
81                 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
82                         fn(argc, argv, env);
83         }
84         _init();
85         array_size = __init_array_end - __init_array_start;
86         for (n = 0; n < array_size; n++) {
87                 fn = __init_array_start[n];
88                 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
89                         fn(argc, argv, env);
90         }
91 }
92
93 static inline void
94 handle_argv(int argc, char *argv[], char **env)
95 {
96         const char *s;
97
98         if (environ == NULL)
99                 environ = env;
100         if (argc > 0 && argv[0] != NULL) {
101                 __progname = argv[0];
102                 for (s = __progname; *s != '\0'; s++) {
103                         if (*s == '/')
104                                 __progname = s + 1;
105                 }
106         }
107 }
108
109 static const struct {
110         int32_t namesz;
111         int32_t descsz;
112         int32_t type;
113         char    name[sizeof(NOTE_FREEBSD_VENDOR)];
114         uint32_t desc;
115 } crt_noinit_tag __attribute__ ((section (NOTE_SECTION),
116     aligned(4))) __used = {
117         .namesz = sizeof(NOTE_FREEBSD_VENDOR),
118         .descsz = sizeof(uint32_t),
119         .type = NT_FREEBSD_NOINIT_TAG,
120         .name = NOTE_FREEBSD_VENDOR,
121         .desc = 0
122 };