Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.39
      1  1.39     joerg /*	$NetBSD: mdreloc.c,v 1.39 2017/08/10 19:03:26 joerg Exp $	*/
      2  1.21     skrll 
      3  1.21     skrll #include <sys/cdefs.h>
      4  1.21     skrll #ifndef lint
      5  1.39     joerg __RCSID("$NetBSD: mdreloc.c,v 1.39 2017/08/10 19:03:26 joerg Exp $");
      6  1.21     skrll #endif /* not lint */
      7  1.11  junyoung 
      8   1.1   mycroft #include <sys/types.h>
      9  1.33     joerg #include <sys/ucontext.h>
     10   1.1   mycroft 
     11   1.1   mycroft #include "debug.h"
     12   1.1   mycroft #include "rtld.h"
     13   1.1   mycroft 
     14  1.13   mycroft void _rtld_bind_start(void);
     15  1.12   mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     16  1.19     skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     17  1.12   mycroft 
     18   1.1   mycroft void
     19   1.1   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
     20   1.1   mycroft {
     21   1.1   mycroft 	obj->pltgot[1] = (Elf_Addr) obj;
     22   1.1   mycroft 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     23   1.1   mycroft }
     24   1.2   mycroft 
     25  1.12   mycroft void
     26  1.19     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     27  1.12   mycroft {
     28  1.12   mycroft 	const Elf_Rel *rel = 0, *rellim;
     29  1.12   mycroft 	Elf_Addr relsz = 0;
     30  1.12   mycroft 	Elf_Addr *where;
     31  1.12   mycroft 
     32  1.12   mycroft 	for (; dynp->d_tag != DT_NULL; dynp++) {
     33  1.12   mycroft 		switch (dynp->d_tag) {
     34  1.12   mycroft 		case DT_REL:
     35  1.12   mycroft 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     36  1.12   mycroft 			break;
     37  1.12   mycroft 		case DT_RELSZ:
     38  1.12   mycroft 			relsz = dynp->d_un.d_val;
     39  1.12   mycroft 			break;
     40  1.12   mycroft 		}
     41  1.12   mycroft 	}
     42  1.22  christos 	if (rel == 0 || relsz == 0)
     43  1.22  christos 		return;
     44  1.27     lukem 	rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
     45  1.12   mycroft 	for (; rel < rellim; rel++) {
     46  1.12   mycroft 		where = (Elf_Addr *)(relocbase + rel->r_offset);
     47  1.12   mycroft 		*where += (Elf_Addr)relocbase;
     48  1.12   mycroft 	}
     49  1.12   mycroft }
     50  1.12   mycroft 
     51   1.2   mycroft int
     52  1.32     joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
     53   1.2   mycroft {
     54   1.3   mycroft 	const Elf_Rel *rel;
     55  1.20     lukem 	Elf_Addr target = 0;
     56  1.38     joerg 	const Elf_Sym   *def = NULL;
     57  1.38     joerg 	const Obj_Entry *defobj = NULL;
     58  1.38     joerg 	unsigned long last_symnum = ULONG_MAX;
     59  1.12   mycroft 
     60   1.3   mycroft 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     61   1.3   mycroft 		Elf_Addr        *where;
     62   1.3   mycroft 		Elf_Addr         tmp;
     63   1.4   mycroft 		unsigned long	 symnum;
     64   1.3   mycroft 
     65   1.3   mycroft 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     66  1.38     joerg 
     67  1.38     joerg 		switch (ELF_R_TYPE(rel->r_info)) {
     68  1.38     joerg 		case R_TYPE(PC32):
     69  1.38     joerg 		case R_TYPE(GOT32):
     70  1.38     joerg 		case R_TYPE(32):
     71  1.38     joerg 		case R_TYPE(GLOB_DAT):
     72  1.38     joerg 		case R_TYPE(TLS_TPOFF):
     73  1.38     joerg 		case R_TYPE(TLS_TPOFF32):
     74  1.38     joerg 		case R_TYPE(TLS_DTPMOD32):
     75  1.38     joerg 		case R_TYPE(TLS_DTPOFF32):
     76  1.38     joerg 			symnum = ELF_R_SYM(rel->r_info);
     77  1.38     joerg 			if (symnum != last_symnum) {
     78  1.38     joerg 				last_symnum = symnum;
     79  1.38     joerg 				def = _rtld_find_symdef(symnum, obj, &defobj,
     80  1.38     joerg 				    false);
     81  1.38     joerg 				if (def == NULL)
     82  1.38     joerg 					return -1;
     83  1.38     joerg 			}
     84  1.38     joerg 			break;
     85  1.38     joerg 		default:
     86  1.38     joerg 			break;
     87  1.38     joerg 		}
     88  1.38     joerg 
     89   1.3   mycroft 
     90   1.3   mycroft 		switch (ELF_R_TYPE(rel->r_info)) {
     91   1.3   mycroft 		case R_TYPE(NONE):
     92   1.3   mycroft 			break;
     93   1.2   mycroft 
     94   1.2   mycroft #if 1 /* XXX should not occur */
     95   1.3   mycroft 		case R_TYPE(PC32):
     96  1.23      matt 			target = (Elf_Addr)(defobj->relocbase + def->st_value);
     97   1.3   mycroft 
     98   1.4   mycroft 			*where += target - (Elf_Addr)where;
     99  1.14   mycroft 			rdbg(("PC32 %s in %s --> %p in %s",
    100   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    101   1.5   mycroft 			    obj->path, (void *)*where, defobj->path));
    102   1.3   mycroft 			break;
    103   1.2   mycroft 
    104   1.3   mycroft 		case R_TYPE(GOT32):
    105   1.2   mycroft #endif
    106   1.3   mycroft 		case R_TYPE(32):
    107   1.3   mycroft 		case R_TYPE(GLOB_DAT):
    108  1.23      matt 			target = (Elf_Addr)(defobj->relocbase + def->st_value);
    109   1.3   mycroft 
    110   1.4   mycroft 			tmp = target + *where;
    111   1.3   mycroft 			if (*where != tmp)
    112   1.3   mycroft 				*where = tmp;
    113  1.14   mycroft 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    114   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    115   1.5   mycroft 			    obj->path, (void *)*where, defobj->path));
    116   1.3   mycroft 			break;
    117   1.3   mycroft 
    118   1.3   mycroft 		case R_TYPE(RELATIVE):
    119  1.12   mycroft 			*where += (Elf_Addr)obj->relocbase;
    120  1.14   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    121  1.12   mycroft 			    (void *)*where));
    122   1.3   mycroft 			break;
    123   1.3   mycroft 
    124   1.3   mycroft 		case R_TYPE(COPY):
    125   1.3   mycroft 			/*
    126   1.3   mycroft 			 * These are deferred until all other relocations have
    127   1.3   mycroft 			 * been done.  All we do here is make sure that the
    128   1.3   mycroft 			 * COPY relocation is not in a shared library.  They
    129   1.3   mycroft 			 * are allowed only in executable files.
    130   1.3   mycroft 			 */
    131   1.8   mycroft 			if (obj->isdynamic) {
    132   1.3   mycroft 				_rtld_error(
    133   1.2   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    134   1.3   mycroft 				    obj->path);
    135   1.3   mycroft 				return -1;
    136   1.3   mycroft 			}
    137  1.14   mycroft 			rdbg(("COPY (avoid in main)"));
    138   1.3   mycroft 			break;
    139   1.3   mycroft 
    140  1.33     joerg 		case R_TYPE(TLS_TPOFF):
    141  1.33     joerg 			if (!defobj->tls_done &&
    142  1.33     joerg 			    _rtld_tls_offset_allocate(obj))
    143  1.33     joerg 				return -1;
    144  1.33     joerg 
    145  1.35       apb 			*where += (Elf_Addr)(def->st_value - defobj->tlsoffset);
    146  1.33     joerg 
    147  1.33     joerg 			rdbg(("TLS_TPOFF %s in %s --> %p",
    148  1.33     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    149  1.33     joerg 			    obj->path, (void *)*where));
    150  1.33     joerg 			break;
    151  1.33     joerg 
    152  1.35       apb 		case R_TYPE(TLS_TPOFF32):
    153  1.35       apb 			if (!defobj->tls_done &&
    154  1.35       apb 			    _rtld_tls_offset_allocate(obj))
    155  1.35       apb 				return -1;
    156  1.35       apb 
    157  1.35       apb 			*where += (Elf_Addr)(defobj->tlsoffset - def->st_value);
    158  1.35       apb 			rdbg(("TLS_TPOFF32 %s in %s --> %p",
    159  1.35       apb 			    obj->strtab + obj->symtab[symnum].st_name,
    160  1.35       apb 			    obj->path, (void *)*where));
    161  1.35       apb 			break;
    162  1.35       apb 
    163  1.33     joerg 		case R_TYPE(TLS_DTPMOD32):
    164  1.33     joerg 			*where = (Elf_Addr)(defobj->tlsindex);
    165  1.33     joerg 
    166  1.33     joerg 			rdbg(("TLS_DTPMOD32 %s in %s --> %p",
    167  1.33     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    168  1.33     joerg 			    obj->path, (void *)*where));
    169  1.33     joerg 			break;
    170  1.33     joerg 
    171  1.33     joerg 		case R_TYPE(TLS_DTPOFF32):
    172  1.33     joerg 			*where = (Elf_Addr)(def->st_value);
    173  1.33     joerg 
    174  1.33     joerg 			rdbg(("TLS_DTPOFF32 %s in %s --> %p",
    175  1.33     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    176  1.33     joerg 			    obj->path, (void *)*where));
    177  1.33     joerg 
    178  1.33     joerg 			break;
    179  1.33     joerg 
    180   1.3   mycroft 		default:
    181  1.14   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    182   1.3   mycroft 			    "contents = %p, symbol = %s",
    183  1.38     joerg 			    (u_long)ELF_R_SYM(rel->r_info),
    184  1.38     joerg 			    (u_long)ELF_R_TYPE(rel->r_info),
    185   1.3   mycroft 			    (void *)rel->r_offset, (void *)*where,
    186   1.4   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    187   1.3   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    188  1.28      jmmv 			    "in non-PLT relocations",
    189   1.3   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    190   1.2   mycroft 			return -1;
    191   1.2   mycroft 		}
    192   1.2   mycroft 	}
    193   1.6   mycroft 	return 0;
    194   1.6   mycroft }
    195   1.6   mycroft 
    196   1.6   mycroft int
    197  1.39     joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
    198   1.6   mycroft {
    199   1.6   mycroft 	const Elf_Rel *rel;
    200   1.6   mycroft 
    201  1.18   mycroft 	if (!obj->relocbase)
    202   1.6   mycroft 		return 0;
    203   1.6   mycroft 
    204  1.39     joerg 	for (rel = obj->pltrellim; rel-- > obj->pltrel; ) {
    205   1.6   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    206   1.6   mycroft 
    207  1.39     joerg 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT) ||
    208  1.39     joerg 		       ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE));
    209  1.39     joerg 
    210  1.39     joerg 		if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE))
    211  1.39     joerg 			obj->ifunc_remaining = obj->pltrellim - rel;
    212   1.6   mycroft 
    213   1.6   mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    214   1.6   mycroft 		*where += (Elf_Addr)obj->relocbase;
    215  1.14   mycroft 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    216   1.6   mycroft 	}
    217   1.6   mycroft 
    218   1.6   mycroft 	return 0;
    219   1.6   mycroft }
    220   1.6   mycroft 
    221  1.39     joerg void
    222  1.39     joerg _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
    223  1.39     joerg {
    224  1.39     joerg 	const Elf_Rel *rel;
    225  1.39     joerg 	Elf_Addr *where, target;
    226  1.39     joerg 
    227  1.39     joerg 	while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
    228  1.39     joerg 		rel = obj->pltrellim - obj->ifunc_remaining;
    229  1.39     joerg 		--obj->ifunc_remaining;
    230  1.39     joerg 		if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE)) {
    231  1.39     joerg 			where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    232  1.39     joerg 			_rtld_exclusive_exit(mask);
    233  1.39     joerg 			target = _rtld_resolve_ifunc2(obj, *where);
    234  1.39     joerg 			_rtld_exclusive_enter(mask);
    235  1.39     joerg 			if (*where != target)
    236  1.39     joerg 				*where = target;
    237  1.39     joerg 		}
    238  1.39     joerg 	}
    239  1.39     joerg }
    240  1.39     joerg 
    241  1.26      matt static inline int
    242  1.26      matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
    243  1.26      matt 	Elf_Addr *tp)
    244   1.6   mycroft {
    245  1.16   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    246  1.26      matt 	Elf_Addr target;
    247  1.26      matt 	const Elf_Sym *def;
    248   1.6   mycroft 	const Obj_Entry *defobj;
    249  1.29  christos 	unsigned long info = rel->r_info;
    250   1.6   mycroft 
    251  1.39     joerg 	if (ELF_R_TYPE(info) == R_TYPE(IRELATIVE))
    252  1.39     joerg 		return 0;
    253  1.39     joerg 
    254  1.29  christos 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    255   1.6   mycroft 
    256  1.29  christos 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    257  1.29  christos 	if (__predict_false(def == NULL))
    258  1.26      matt 		return -1;
    259  1.29  christos 	if (__predict_false(def == &_rtld_sym_zero))
    260  1.29  christos 		return 0;
    261  1.29  christos 
    262  1.36     joerg 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    263  1.36     joerg 		if (tp == NULL)
    264  1.36     joerg 			return 0;
    265  1.36     joerg 		target = _rtld_resolve_ifunc(defobj, def);
    266  1.36     joerg 	} else {
    267  1.36     joerg 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
    268  1.36     joerg 	}
    269  1.36     joerg 
    270  1.26      matt 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    271  1.26      matt 	    defobj->strtab + def->st_name, (void *)*where,
    272  1.26      matt 	    (void *)target));
    273  1.26      matt 	if (*where != target)
    274  1.26      matt 		*where = target;
    275  1.26      matt 	if (tp)
    276  1.26      matt 		*tp = target;
    277  1.26      matt 	return 0;
    278  1.26      matt }
    279  1.26      matt 
    280  1.26      matt caddr_t
    281  1.26      matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    282  1.26      matt {
    283  1.31     skrll 	const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel
    284  1.31     skrll 	    + reloff);
    285  1.26      matt 	Elf_Addr new_value;
    286  1.26      matt 	int err;
    287  1.26      matt 
    288  1.30     skrll 	new_value = 0;	/* XXX gcc */
    289  1.30     skrll 
    290  1.34     joerg 	_rtld_shared_enter();
    291  1.26      matt 	err = _rtld_relocate_plt_object(obj, rel, &new_value);
    292  1.29  christos 	if (err)
    293  1.16   mycroft 		_rtld_die();
    294  1.34     joerg 	_rtld_shared_exit();
    295   1.6   mycroft 
    296  1.16   mycroft 	return (caddr_t)new_value;
    297  1.15  junyoung }
    298  1.15  junyoung 
    299  1.15  junyoung int
    300  1.15  junyoung _rtld_relocate_plt_objects(const Obj_Entry *obj)
    301  1.15  junyoung {
    302  1.15  junyoung 	const Elf_Rel *rel;
    303  1.26      matt 	int err = 0;
    304  1.15  junyoung 
    305  1.15  junyoung 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    306  1.26      matt 		err = _rtld_relocate_plt_object(obj, rel, NULL);
    307  1.26      matt 		if (err)
    308  1.26      matt 			break;
    309  1.15  junyoung 	}
    310  1.26      matt 	return err;
    311   1.2   mycroft }
    312  1.33     joerg 
    313  1.33     joerg /*
    314  1.33     joerg  * i386 specific GNU variant of __tls_get_addr using register based
    315  1.33     joerg  * argument passing.
    316  1.33     joerg  */
    317  1.33     joerg #define	DTV_MAX_INDEX(dtv)	((size_t)((dtv)[-1]))
    318  1.33     joerg 
    319  1.33     joerg __dso_public __attribute__((__regparm__(1))) void *
    320  1.33     joerg ___tls_get_addr(void *arg_)
    321  1.33     joerg {
    322  1.33     joerg 	size_t *arg = (size_t *)arg_;
    323  1.33     joerg 	void **dtv;
    324  1.33     joerg 	struct tls_tcb *tcb = __lwp_getprivate_fast();
    325  1.33     joerg 	size_t idx = arg[0], offset = arg[1];
    326  1.33     joerg 
    327  1.33     joerg 	dtv = tcb->tcb_dtv;
    328  1.33     joerg 
    329  1.33     joerg 	if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL))
    330  1.33     joerg 		return (uint8_t *)dtv[idx] + offset;
    331  1.33     joerg 
    332  1.33     joerg 	return _rtld_tls_get_addr(tcb, idx, offset);
    333  1.33     joerg }
    334