Home | History | Annotate | Line # | Download | only in common
crt0-common.c revision 1.29
      1 /* $NetBSD: crt0-common.c,v 1.29 2025/03/22 16:59:02 christos Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998 Christos Zoulas
      5  * Copyright (c) 1995 Christopher G. Demetriou
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *          This product includes software developed for the
     19  *          NetBSD Project.  See http://www.NetBSD.org/ for
     20  *          information about NetBSD.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  *
     35  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __RCSID("$NetBSD: crt0-common.c,v 1.29 2025/03/22 16:59:02 christos Exp $");
     40 
     41 #include <sys/types.h>
     42 #include <sys/exec.h>
     43 #include <sys/exec_elf.h>
     44 #include <sys/syscall.h>
     45 #include <machine/profile.h>
     46 #include <stdlib.h>
     47 #include <unistd.h>
     48 
     49 #include "csu-common.h"
     50 
     51 extern int main(int, char **, char **);
     52 
     53 typedef void (*fptr_t)(void);
     54 #ifndef HAVE_INITFINI_ARRAY
     55 extern void	_init(void);
     56 extern void	_fini(void);
     57 #endif
     58 
     59 /*
     60  * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
     61  * as being at address zero, unless something else defines it).  That way,
     62  * if we happen to be compiling without -static but with without any
     63  * shared libs present, things will still work.
     64  */
     65 
     66 __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
     67 
     68 #ifdef MCRT0
     69 extern void	monstartup(u_long, u_long);
     70 extern void	_mcleanup(void);
     71 extern unsigned char __etext, __eprol;
     72 #endif /* MCRT0 */
     73 
     74 static char	 empty_string[] = "";
     75 
     76 char		**environ __common;
     77 struct ps_strings *__ps_strings __common = 0;
     78 char		*__progname __common = empty_string;
     79 
     80 __dead __dso_hidden void ___start(void (*)(void), struct ps_strings *);
     81 
     82 #define	write(fd, s, n)	__syscall(SYS_write, (fd), (s), (n))
     83 
     84 #define	_FATAL(str)				\
     85 do {						\
     86 	write(2, str, sizeof(str)-1);		\
     87 	_exit(1);				\
     88 } while (0)
     89 
     90 /*
     91  * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically,
     92  * we have to process these instead of relying on RTLD to do it for us.
     93  *
     94  * Since we don't need .init or .fini sections, just code them in C
     95  * to make life easier.
     96  */
     97 extern const fptr_t __preinit_array_start[] __dso_hidden;
     98 extern const fptr_t __preinit_array_end[] __dso_hidden __weak;
     99 extern const fptr_t __init_array_start[] __dso_hidden;
    100 extern const fptr_t __init_array_end[] __dso_hidden __weak;
    101 extern const fptr_t __fini_array_start[] __dso_hidden;
    102 extern const fptr_t __fini_array_end[] __dso_hidden __weak;
    103 
    104 static inline void
    105 _preinit(void)
    106 {
    107 	for (const fptr_t *f = __preinit_array_start; f < __preinit_array_end; f++) {
    108 		(*f)();
    109 	}
    110 }
    111 
    112 static inline void
    113 _initarray(void)
    114 {
    115 	for (const fptr_t *f = __init_array_start; f < __init_array_end; f++) {
    116 		(*f)();
    117 	}
    118 }
    119 
    120 static void
    121 _finiarray(void)
    122 {
    123 	for (const fptr_t *f = __fini_array_start; f < __fini_array_end; f++) {
    124 		(*f)();
    125 	}
    126 }
    127 
    128 #if \
    129     defined(__aarch64__) || \
    130     defined(__powerpc__) || \
    131     defined(__sparc__) || \
    132     defined(__x86_64__)
    133 #define HAS_IPLTA
    134 static void fix_iplta(void) __noinline;
    135 #elif \
    136     defined(__arm__) || \
    137     defined(__i386__)
    138 #define HAS_IPLT
    139 static void fix_iplt(void) __noinline;
    140 #endif
    141 
    142 
    143 #ifdef HAS_IPLTA
    144 #include <stdio.h>
    145 extern const Elf_Rela __rela_iplt_start[] __dso_hidden __weak;
    146 extern const Elf_Rela __rela_iplt_end[] __dso_hidden __weak;
    147 #ifdef __sparc__
    148 #define IFUNC_RELOCATION R_TYPE(JMP_IREL)
    149 #include <machine/elf_support.h>
    150 #define write_plt(where, value) sparc_write_branch((void *)where, (void *)value)
    151 #else
    152 #define IFUNC_RELOCATION R_TYPE(IRELATIVE)
    153 #define write_plt(where, value) *where = value
    154 #endif
    155 
    156 static void
    157 fix_iplta(void)
    158 {
    159 	const Elf_Rela *rela, *relalim;
    160 	uintptr_t relocbase = 0;
    161 	Elf_Addr *where, target;
    162 
    163 	rela = __rela_iplt_start;
    164 	relalim = __rela_iplt_end;
    165 	for (; rela < relalim; ++rela) {
    166 		if (ELF_R_TYPE(rela->r_info) != IFUNC_RELOCATION)
    167 			abort();
    168 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    169 		target = (Elf_Addr)(relocbase + rela->r_addend);
    170 		target = ((Elf_Addr(*)(void))target)();
    171 		write_plt(where, target);
    172 	}
    173 }
    174 #endif
    175 #ifdef HAS_IPLT
    176 extern const Elf_Rel __rel_iplt_start[] __dso_hidden __weak;
    177 extern const Elf_Rel __rel_iplt_end[] __dso_hidden __weak;
    178 #define IFUNC_RELOCATION R_TYPE(IRELATIVE)
    179 
    180 static void
    181 fix_iplt(void)
    182 {
    183 	const Elf_Rel *rel, *rellim;
    184 	uintptr_t relocbase = 0;
    185 	Elf_Addr *where, target;
    186 
    187 	rel = __rel_iplt_start;
    188 	rellim = __rel_iplt_end;
    189 	for (; rel < rellim; ++rel) {
    190 		if (ELF_R_TYPE(rel->r_info) != IFUNC_RELOCATION)
    191 			abort();
    192 		where = (Elf_Addr *)(relocbase + rel->r_offset);
    193 		target = ((Elf_Addr(*)(void))*where)();
    194 		*where = target;
    195 	}
    196 }
    197 #endif
    198 
    199 #if defined(__x86_64__) || defined(__i386__)
    200 #  define HAS_RELOCATE_SELF
    201 #  if defined(__x86_64__)
    202 #  define RELA
    203 #  define REL_TAG DT_RELA
    204 #  define RELSZ_TAG DT_RELASZ
    205 #  define REL_TYPE Elf_Rela
    206 #  else
    207 #  define REL_TAG DT_REL
    208 #  define RELSZ_TAG DT_RELSZ
    209 #  define REL_TYPE Elf_Rel
    210 #  endif
    211 
    212 #include <elf.h>
    213 
    214 static void relocate_self(struct ps_strings *) __noinline;
    215 
    216 static void
    217 relocate_self(struct ps_strings *ps_strings)
    218 {
    219 	AuxInfo *aux = (AuxInfo *)(ps_strings->ps_argvstr + ps_strings->ps_nargvstr +
    220 	    ps_strings->ps_nenvstr + 2);
    221 	uintptr_t relocbase = (uintptr_t)~0U;
    222 	const Elf_Phdr *phdr = NULL;
    223 	Elf_Half phnum = (Elf_Half)~0;
    224 
    225 	for (; aux->a_type != AT_NULL; ++aux) {
    226 		switch (aux->a_type) {
    227 		case AT_BASE:
    228 			if (aux->a_v)
    229 				return;
    230 			break;
    231 		case AT_PHDR:
    232 			phdr = (void *)aux->a_v;
    233 			break;
    234 		case AT_PHNUM:
    235 			phnum = (Elf_Half)aux->a_v;
    236 			break;
    237 		}
    238 	}
    239 
    240 	if (phdr == NULL || phnum == (Elf_Half)~0)
    241 		return;
    242 
    243 	const Elf_Phdr *phlimit = phdr + phnum, *dynphdr = NULL;
    244 
    245 	for (; phdr < phlimit; ++phdr) {
    246 		if (phdr->p_type == PT_DYNAMIC)
    247 			dynphdr = phdr;
    248 		if (phdr->p_type == PT_PHDR)
    249 			relocbase = (uintptr_t)phdr - phdr->p_vaddr;
    250 	}
    251 	if (dynphdr == NULL || relocbase == (uintptr_t)~0U)
    252 		return;
    253 
    254 	Elf_Dyn *dynp = (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase);
    255 
    256 	const REL_TYPE *relocs = 0, *relocslim;
    257 	Elf_Addr relocssz = 0;
    258 
    259 	for (; dynp->d_tag != DT_NULL; dynp++) {
    260 		switch (dynp->d_tag) {
    261 		case REL_TAG:
    262 			relocs =
    263 			    (const REL_TYPE *)(relocbase + dynp->d_un.d_ptr);
    264 			break;
    265 		case RELSZ_TAG:
    266 			relocssz = dynp->d_un.d_val;
    267 			break;
    268 		}
    269 	}
    270 	relocslim = (const REL_TYPE *)((const uint8_t *)relocs + relocssz);
    271 	for (; relocs < relocslim; ++relocs) {
    272 		Elf_Addr *where;
    273 
    274 		where = (Elf_Addr *)(relocbase + relocs->r_offset);
    275 
    276 		switch (ELF_R_TYPE(relocs->r_info)) {
    277 		case R_TYPE(RELATIVE):  /* word64 B + A */
    278 #ifdef RELA
    279 			*where = (Elf_Addr)(relocbase + relocs->r_addend);
    280 #else
    281 			*where += (Elf_Addr)relocbase;
    282 #endif
    283 			break;
    284 #ifdef IFUNC_RELOCATION
    285 		case IFUNC_RELOCATION:
    286 			break;
    287 #endif
    288 		default:
    289 			abort();
    290 		}
    291 	}
    292 }
    293 #endif
    294 
    295 void
    296 ___start(void (*cleanup)(void),			/* from shared loader */
    297     struct ps_strings *ps_strings)
    298 {
    299 #if defined(HAS_RELOCATE_SELF)
    300 	relocate_self(ps_strings);
    301 #endif
    302 
    303 	if (ps_strings == NULL)
    304 		_FATAL("ps_strings missing\n");
    305 	__ps_strings = ps_strings;
    306 
    307 	environ = ps_strings->ps_envstr;
    308 
    309 	if (ps_strings->ps_argvstr[0] != NULL) {
    310 		char *c;
    311 		__progname = ps_strings->ps_argvstr[0];
    312 		for (c = ps_strings->ps_argvstr[0]; *c; ++c) {
    313 			if (*c == '/')
    314 				__progname = c + 1;
    315 		}
    316 	} else {
    317 		__progname = empty_string;
    318 	}
    319 
    320 	_libc_init();
    321 
    322 	if (&rtld_DYNAMIC == NULL) {
    323 #ifdef HAS_IPLTA
    324 		fix_iplta();
    325 #endif
    326 #ifdef HAS_IPLT
    327 		fix_iplt();
    328 #endif
    329 	}
    330 
    331 	_preinit();
    332 
    333 	if (cleanup != NULL)
    334 		atexit(cleanup);
    335 
    336 #ifdef MCRT0
    337 	atexit(_mcleanup);
    338 	monstartup((u_long)&__eprol, (u_long)&__etext);
    339 #endif
    340 
    341 	atexit(_finiarray);
    342 	_initarray();
    343 
    344 #ifndef HAVE_INITFINI_ARRAY
    345 	atexit(_fini);
    346 	_init();
    347 #endif
    348 
    349 	exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ));
    350 }
    351