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