Home | History | Annotate | Line # | Download | only in common
crt0-common.c revision 1.18
      1 /* $NetBSD: crt0-common.c,v 1.18 2018/07/12 21:36:45 joerg 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.18 2018/07/12 21:36:45 joerg Exp $");
     40 
     41 #include <sys/types.h>
     42 #include <sys/exec.h>
     43 #include <sys/syscall.h>
     44 #include <machine/profile.h>
     45 #include <stdlib.h>
     46 #include <unistd.h>
     47 
     48 #include "rtld.h"
     49 
     50 extern int main(int, char **, char **);
     51 
     52 #ifndef HAVE_INITFINI_ARRAY
     53 extern void	_init(void);
     54 extern void	_fini(void);
     55 #endif
     56 extern void	_libc_init(void);
     57 
     58 /*
     59  * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
     60  * as being at address zero, unless something else defines it).  That way,
     61  * if we happen to be compiling without -static but with without any
     62  * shared libs present, things will still work.
     63  */
     64 
     65 __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
     66 
     67 #ifdef MCRT0
     68 extern void	monstartup(u_long, u_long);
     69 extern void	_mcleanup(void);
     70 extern unsigned char __etext, __eprol;
     71 #endif /* MCRT0 */
     72 
     73 char		**environ;
     74 struct ps_strings *__ps_strings = 0;
     75 
     76 static char	 empty_string[] = "";
     77 char		*__progname = empty_string;
     78 
     79 __dead __dso_hidden void ___start(void (*)(void), const Obj_Entry *,
     80 			 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 #ifdef HAVE_INITFINI_ARRAY
     91 /*
     92  * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically,
     93  * we have to process these instead of relying on RTLD to do it for us.
     94  *
     95  * Since we don't need .init or .fini sections, just code them in C
     96  * to make life easier.
     97  */
     98 extern const fptr_t __preinit_array_start[] __dso_hidden;
     99 extern const fptr_t __preinit_array_end[] __dso_hidden __weak;
    100 extern const fptr_t __init_array_start[] __dso_hidden;
    101 extern const fptr_t __init_array_end[] __dso_hidden __weak;
    102 extern const fptr_t __fini_array_start[] __dso_hidden;
    103 extern const fptr_t __fini_array_end[] __dso_hidden __weak;
    104 
    105 static inline void
    106 _preinit(void)
    107 {
    108 	for (const fptr_t *f = __preinit_array_start; f < __preinit_array_end; f++) {
    109 		(*f)();
    110 	}
    111 }
    112 
    113 static inline void
    114 _init(void)
    115 {
    116 	for (const fptr_t *f = __init_array_start; f < __init_array_end; f++) {
    117 		(*f)();
    118 	}
    119 }
    120 
    121 static void
    122 _fini(void)
    123 {
    124 	for (const fptr_t *f = __fini_array_start; f < __fini_array_end; f++) {
    125 		(*f)();
    126 	}
    127 }
    128 #endif /* HAVE_INITFINI_ARRAY */
    129 
    130 #if defined(__x86_64__) || defined(__powerpc__) || defined(__sparc__)
    131 #define HAS_IPLTA
    132 static void fix_iplta(void) __noinline;
    133 #elif defined(__i386__) || defined(__arm__)
    134 #define HAS_IPLT
    135 static void fix_iplt(void) __noinline;
    136 #endif
    137 
    138 
    139 #ifdef HAS_IPLTA
    140 #include <stdio.h>
    141 extern const Elf_Rela __rela_iplt_start[] __dso_hidden __weak;
    142 extern const Elf_Rela __rela_iplt_end[] __dso_hidden __weak;
    143 #ifdef __sparc__
    144 #define IFUNC_RELOCATION R_TYPE(JMP_IREL)
    145 #include <machine/elf_support.h>
    146 #define write_plt(where, value) sparc_write_branch((void *)where, (void *)value)
    147 #else
    148 #define IFUNC_RELOCATION R_TYPE(IRELATIVE)
    149 #define write_plt(where, value) *where = value
    150 #endif
    151 
    152 static void
    153 fix_iplta(void)
    154 {
    155 	const Elf_Rela *rela, *relalim;
    156 	uintptr_t relocbase = 0;
    157 	Elf_Addr *where, target;
    158 
    159 	rela = __rela_iplt_start;
    160 	relalim = __rela_iplt_end;
    161 	for (; rela < relalim; ++rela) {
    162 		if (ELF_R_TYPE(rela->r_info) != IFUNC_RELOCATION)
    163 			abort();
    164 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    165 		target = (Elf_Addr)(relocbase + rela->r_addend);
    166 		target = ((Elf_Addr(*)(void))target)();
    167 		write_plt(where, target);
    168 	}
    169 }
    170 #endif
    171 #ifdef HAS_IPLT
    172 extern const Elf_Rel __rel_iplt_start[] __dso_hidden __weak;
    173 extern const Elf_Rel __rel_iplt_end[] __dso_hidden __weak;
    174 #define IFUNC_RELOCATION R_TYPE(IRELATIVE)
    175 
    176 static void
    177 fix_iplt(void)
    178 {
    179 	const Elf_Rel *rel, *rellim;
    180 	uintptr_t relocbase = 0;
    181 	Elf_Addr *where, target;
    182 
    183 	rel = __rel_iplt_start;
    184 	rellim = __rel_iplt_end;
    185 	for (; rel < rellim; ++rel) {
    186 		if (ELF_R_TYPE(rel->r_info) != IFUNC_RELOCATION)
    187 			abort();
    188 		where = (Elf_Addr *)(relocbase + rel->r_offset);
    189 		target = ((Elf_Addr(*)(void))*where)();
    190 		*where = target;
    191 	}
    192 }
    193 #endif
    194 
    195 #if defined(__x86_64__) || defined(__i386__)
    196 #  define HAS_RELOCATE_SELF
    197 #  if defined(__x86_64__)
    198 #  define RELA
    199 #  define REL_TAG DT_RELA
    200 #  define RELSZ_TAG DT_RELASZ
    201 #  define REL_TYPE Elf_Rela
    202 #  else
    203 #  define REL_TAG DT_REL
    204 #  define RELSZ_TAG DT_RELSZ
    205 #  define REL_TYPE Elf_Rel
    206 #  endif
    207 
    208 #include <elf.h>
    209 
    210 static void relocate_self(struct ps_strings *) __noinline;
    211 
    212 static void
    213 relocate_self(struct ps_strings *ps_strings)
    214 {
    215 	AuxInfo *aux = (AuxInfo *)(ps_strings->ps_argvstr + ps_strings->ps_nargvstr +
    216 	    ps_strings->ps_nenvstr + 2);
    217 	uintptr_t relocbase;
    218 	const Elf_Phdr *phdr;
    219 	Elf_Half phnum;
    220 
    221 	for (; aux->a_type != AT_NULL; ++aux) {
    222 		switch (aux->a_type) {
    223 		case AT_BASE:
    224 			if (aux->a_v)
    225 				return;
    226 			break;
    227 		case AT_PHDR:
    228 			phdr = (void *)aux->a_v;
    229 			break;
    230 		case AT_PHNUM:
    231 			phnum = (Elf_Half)aux->a_v;
    232 			break;
    233 		}
    234 	}
    235 	const Elf_Phdr *phlimit = phdr + phnum, *dynphdr;
    236 
    237 	for (; phdr < phlimit; ++phdr) {
    238 		if (phdr->p_type == PT_DYNAMIC)
    239 			dynphdr = phdr;
    240 		if (phdr->p_type == PT_PHDR)
    241 			relocbase = (uintptr_t)phdr - phdr->p_vaddr;
    242 	}
    243 	Elf_Dyn *dynp = (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase);
    244 
    245 	const REL_TYPE *relocs = 0, *relocslim;
    246 	Elf_Addr relocssz = 0;
    247 
    248 	for (; dynp->d_tag != DT_NULL; dynp++) {
    249 		switch (dynp->d_tag) {
    250 		case REL_TAG:
    251 			relocs =
    252 			    (const REL_TYPE *)(relocbase + dynp->d_un.d_ptr);
    253 			break;
    254 		case RELSZ_TAG:
    255 			relocssz = dynp->d_un.d_val;
    256 			break;
    257 		}
    258 	}
    259 	relocslim = (const REL_TYPE *)((const uint8_t *)relocs + relocssz);
    260 	for (; relocs < relocslim; ++relocs) {
    261 		Elf_Addr *where;
    262 
    263 		where = (Elf_Addr *)(relocbase + relocs->r_offset);
    264 
    265 		switch (ELF_R_TYPE(relocs->r_info)) {
    266 		case R_TYPE(RELATIVE):  /* word64 B + A */
    267 #ifdef RELA
    268 			*where = (Elf_Addr)(relocbase + relocs->r_addend);
    269 #else
    270 			*where += (Elf_Addr)relocbase;
    271 #endif
    272 			break;
    273 #ifdef IFUNC_RELOCATION
    274 		case IFUNC_RELOCATION:
    275 			break;
    276 #endif
    277 		default:
    278 			abort();
    279 		}
    280 	}
    281 }
    282 #endif
    283 
    284 void
    285 ___start(void (*cleanup)(void),			/* from shared loader */
    286     const Obj_Entry *obj,			/* from shared loader */
    287     struct ps_strings *ps_strings)
    288 {
    289 #if defined(HAS_RELOCATE_SELF)
    290 	relocate_self(ps_strings);
    291 #endif
    292 
    293 	if (ps_strings == NULL)
    294 		_FATAL("ps_strings missing\n");
    295 	__ps_strings = ps_strings;
    296 
    297 	environ = ps_strings->ps_envstr;
    298 
    299 	if (ps_strings->ps_argvstr[0] != NULL) {
    300 		char *c;
    301 		__progname = ps_strings->ps_argvstr[0];
    302 		for (c = ps_strings->ps_argvstr[0]; *c; ++c) {
    303 			if (*c == '/')
    304 				__progname = c + 1;
    305 		}
    306 	} else {
    307 		__progname = empty_string;
    308 	}
    309 
    310 	if (&rtld_DYNAMIC != NULL && obj != NULL) {
    311 		if (obj->magic != RTLD_MAGIC)
    312 			_FATAL("Corrupt Obj_Entry pointer in GOT\n");
    313 		if (obj->version != RTLD_VERSION)
    314 			_FATAL("Dynamic linker version mismatch\n");
    315 		atexit(cleanup);
    316 	}
    317 
    318 	_libc_init();
    319 
    320 	if (&rtld_DYNAMIC == NULL) {
    321 #ifdef HAS_IPLTA
    322 		fix_iplta();
    323 #endif
    324 #ifdef HAS_IPLT
    325 		fix_iplt();
    326 #endif
    327 	}
    328 
    329 #ifdef HAVE_INITFINI_ARRAY
    330 	_preinit();
    331 #endif
    332 
    333 #ifdef MCRT0
    334 	atexit(_mcleanup);
    335 	monstartup((u_long)&__eprol, (u_long)&__etext);
    336 #endif
    337 
    338 	atexit(_fini);
    339 	_init();
    340 
    341 	exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ));
    342 }
    343