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 35extern int main(int, char **, char **); 36 37extern void (*__preinit_array_start[])(int, char **, char **) __hidden; 38extern void (*__preinit_array_end[])(int, char **, char **) __hidden; 39extern void (*__init_array_start[])(int, char **, char **) __hidden; 40extern void (*__init_array_end[])(int, char **, char **) __hidden; 41extern void (*__fini_array_start[])(void) __hidden; 42extern void (*__fini_array_end[])(void) __hidden; 43extern void _fini(void) __hidden; 44extern void _init(void) __hidden; 45 46extern int _DYNAMIC; 47#pragma weak _DYNAMIC 48 49#if defined(CRT_IRELOC_RELA) 50extern const Elf_Rela __rela_iplt_start[] __weak_symbol __hidden; 51extern const Elf_Rela __rela_iplt_end[] __weak_symbol __hidden; 52 53#include "reloc.c" 54 55static void 56process_irelocs(void) 57{ 58 const Elf_Rela *r; 59 60 for (r = &__rela_iplt_start[0]; r < &__rela_iplt_end[0]; r++) 61 crt1_handle_rela(r); 62} 63#elif defined(CRT_IRELOC_REL) 64extern const Elf_Rel __rel_iplt_start[] __weak_symbol __hidden; 65extern const Elf_Rel __rel_iplt_end[] __weak_symbol __hidden; 66 67#include "reloc.c" 68 69static void 70process_irelocs(void) 71{ 72 const Elf_Rel *r; 73 74 for (r = &__rel_iplt_start[0]; r < &__rel_iplt_end[0]; r++) 75 crt1_handle_rel(r); 76} 77#elif defined(CRT_IRELOC_SUPPRESS) 78#else 79#error "Define platform reloc type" 80#endif 81 82char **environ; 83const char *__progname = ""; 84 85static void 86finalizer(void) 87{ 88 void (*fn)(void); 89 size_t array_size, n; 90 91 array_size = __fini_array_end - __fini_array_start; 92 for (n = array_size; n > 0; n--) { 93 fn = __fini_array_start[n - 1]; 94 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) 95 (fn)(); 96 } 97 _fini(); 98} 99 100static inline void 101handle_static_init(int argc, char **argv, char **env) 102{ 103 void (*fn)(int, char **, char **); 104 size_t array_size, n; 105 106 if (&_DYNAMIC != NULL) 107 return; 108 109 atexit(finalizer); 110 111 array_size = __preinit_array_end - __preinit_array_start; 112 for (n = 0; n < array_size; n++) { 113 fn = __preinit_array_start[n]; 114 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) 115 fn(argc, argv, env); 116 } 117 _init(); 118 array_size = __init_array_end - __init_array_start; 119 for (n = 0; n < array_size; n++) { 120 fn = __init_array_start[n]; 121 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) 122 fn(argc, argv, env); 123 } 124} 125 126static inline void 127handle_argv(int argc, char *argv[], char **env) 128{ 129 const char *s; 130 131 if (environ == NULL) 132 environ = env; 133 if (argc > 0 && argv[0] != NULL) { 134 __progname = argv[0]; 135 for (s = __progname; *s != '\0'; s++) { 136 if (*s == '/') 137 __progname = s + 1; 138 } 139 } 140} 141