2 * SPDX-License-Identifier: BSD-1-Clause
4 * Copyright 2012 Konstantin Belousov <kib@FreeBSD.org>
5 * Copyright (c) 2018 The FreeBSD Foundation
7 * Parts of this software was developed by Konstantin Belousov
8 * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/elf_common.h>
37 extern int main(int, char **, char **);
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;
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;
62 for (r = &__rela_iplt_start[0]; r < &__rela_iplt_end[0]; r++)
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;
76 for (r = &__rel_iplt_start[0]; r < &__rel_iplt_end[0]; r++)
79 #elif defined(CRT_IRELOC_SUPPRESS)
81 #error "Define platform reloc type"
85 const char *__progname = "";
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)
103 handle_static_init(int argc, char **argv, char **env)
105 void (*fn)(int, char **, char **);
106 size_t array_size, n;
108 if (&_DYNAMIC != NULL)
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)
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)
129 handle_argv(int argc, char *argv[], char **env)
135 if (argc > 0 && argv[0] != NULL) {
136 __progname = argv[0];
137 for (s = __progname; *s != '\0'; s++) {
144 static const struct {
148 char name[sizeof(NOTE_FREEBSD_VENDOR)];
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,