Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.41
      1  1.41     joerg /*	$NetBSD: mdreloc.c,v 1.41 2018/04/03 21:10:27 joerg Exp $	*/
      2  1.21     skrll 
      3  1.21     skrll #include <sys/cdefs.h>
      4  1.21     skrll #ifndef lint
      5  1.41     joerg __RCSID("$NetBSD: mdreloc.c,v 1.41 2018/04/03 21:10:27 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.41     joerg 
    119  1.41     joerg 		case R_TYPE(IRELATIVE):
    120  1.41     joerg 			/* IFUNC relocations are handled in _rtld_call_ifunc */
    121  1.41     joerg 			if (obj->ifunc_remaining_nonplt == 0) {
    122  1.41     joerg 				obj->ifunc_remaining_nonplt =
    123  1.41     joerg 				    obj->rellim - rel;
    124  1.41     joerg 			}
    125  1.41     joerg 			/* FALL-THROUGH */
    126  1.41     joerg 
    127   1.3   mycroft 		case R_TYPE(RELATIVE):
    128  1.12   mycroft 			*where += (Elf_Addr)obj->relocbase;
    129  1.14   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    130  1.12   mycroft 			    (void *)*where));
    131   1.3   mycroft 			break;
    132   1.3   mycroft 
    133   1.3   mycroft 		case R_TYPE(COPY):
    134   1.3   mycroft 			/*
    135   1.3   mycroft 			 * These are deferred until all other relocations have
    136   1.3   mycroft 			 * been done.  All we do here is make sure that the
    137   1.3   mycroft 			 * COPY relocation is not in a shared library.  They
    138   1.3   mycroft 			 * are allowed only in executable files.
    139   1.3   mycroft 			 */
    140   1.8   mycroft 			if (obj->isdynamic) {
    141   1.3   mycroft 				_rtld_error(
    142   1.2   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    143   1.3   mycroft 				    obj->path);
    144   1.3   mycroft 				return -1;
    145   1.3   mycroft 			}
    146  1.14   mycroft 			rdbg(("COPY (avoid in main)"));
    147   1.3   mycroft 			break;
    148   1.3   mycroft 
    149  1.33     joerg 		case R_TYPE(TLS_TPOFF):
    150  1.33     joerg 			if (!defobj->tls_done &&
    151  1.33     joerg 			    _rtld_tls_offset_allocate(obj))
    152  1.33     joerg 				return -1;
    153  1.33     joerg 
    154  1.35       apb 			*where += (Elf_Addr)(def->st_value - defobj->tlsoffset);
    155  1.33     joerg 
    156  1.33     joerg 			rdbg(("TLS_TPOFF %s in %s --> %p",
    157  1.33     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    158  1.33     joerg 			    obj->path, (void *)*where));
    159  1.33     joerg 			break;
    160  1.33     joerg 
    161  1.35       apb 		case R_TYPE(TLS_TPOFF32):
    162  1.35       apb 			if (!defobj->tls_done &&
    163  1.35       apb 			    _rtld_tls_offset_allocate(obj))
    164  1.35       apb 				return -1;
    165  1.35       apb 
    166  1.35       apb 			*where += (Elf_Addr)(defobj->tlsoffset - def->st_value);
    167  1.35       apb 			rdbg(("TLS_TPOFF32 %s in %s --> %p",
    168  1.35       apb 			    obj->strtab + obj->symtab[symnum].st_name,
    169  1.35       apb 			    obj->path, (void *)*where));
    170  1.35       apb 			break;
    171  1.35       apb 
    172  1.33     joerg 		case R_TYPE(TLS_DTPMOD32):
    173  1.33     joerg 			*where = (Elf_Addr)(defobj->tlsindex);
    174  1.33     joerg 
    175  1.33     joerg 			rdbg(("TLS_DTPMOD32 %s in %s --> %p",
    176  1.33     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    177  1.33     joerg 			    obj->path, (void *)*where));
    178  1.33     joerg 			break;
    179  1.33     joerg 
    180  1.33     joerg 		case R_TYPE(TLS_DTPOFF32):
    181  1.33     joerg 			*where = (Elf_Addr)(def->st_value);
    182  1.33     joerg 
    183  1.33     joerg 			rdbg(("TLS_DTPOFF32 %s in %s --> %p",
    184  1.33     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    185  1.33     joerg 			    obj->path, (void *)*where));
    186  1.33     joerg 
    187  1.33     joerg 			break;
    188  1.33     joerg 
    189   1.3   mycroft 		default:
    190  1.14   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    191   1.3   mycroft 			    "contents = %p, symbol = %s",
    192  1.38     joerg 			    (u_long)ELF_R_SYM(rel->r_info),
    193  1.38     joerg 			    (u_long)ELF_R_TYPE(rel->r_info),
    194   1.3   mycroft 			    (void *)rel->r_offset, (void *)*where,
    195   1.4   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    196   1.3   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    197  1.28      jmmv 			    "in non-PLT relocations",
    198   1.3   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    199   1.2   mycroft 			return -1;
    200   1.2   mycroft 		}
    201   1.2   mycroft 	}
    202   1.6   mycroft 	return 0;
    203   1.6   mycroft }
    204   1.6   mycroft 
    205   1.6   mycroft int
    206  1.39     joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
    207   1.6   mycroft {
    208   1.6   mycroft 	const Elf_Rel *rel;
    209   1.6   mycroft 
    210  1.39     joerg 	for (rel = obj->pltrellim; rel-- > obj->pltrel; ) {
    211   1.6   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    212   1.6   mycroft 
    213  1.39     joerg 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT) ||
    214  1.39     joerg 		       ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE));
    215  1.39     joerg 
    216  1.39     joerg 		if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE))
    217  1.39     joerg 			obj->ifunc_remaining = obj->pltrellim - rel;
    218   1.6   mycroft 
    219   1.6   mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    220   1.6   mycroft 		*where += (Elf_Addr)obj->relocbase;
    221  1.14   mycroft 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    222   1.6   mycroft 	}
    223   1.6   mycroft 
    224   1.6   mycroft 	return 0;
    225   1.6   mycroft }
    226   1.6   mycroft 
    227  1.26      matt static inline int
    228  1.26      matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
    229  1.26      matt 	Elf_Addr *tp)
    230   1.6   mycroft {
    231  1.16   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    232  1.26      matt 	Elf_Addr target;
    233  1.26      matt 	const Elf_Sym *def;
    234   1.6   mycroft 	const Obj_Entry *defobj;
    235  1.29  christos 	unsigned long info = rel->r_info;
    236   1.6   mycroft 
    237  1.39     joerg 	if (ELF_R_TYPE(info) == R_TYPE(IRELATIVE))
    238  1.39     joerg 		return 0;
    239  1.39     joerg 
    240  1.29  christos 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    241   1.6   mycroft 
    242  1.29  christos 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    243  1.29  christos 	if (__predict_false(def == NULL))
    244  1.26      matt 		return -1;
    245  1.29  christos 	if (__predict_false(def == &_rtld_sym_zero))
    246  1.29  christos 		return 0;
    247  1.29  christos 
    248  1.36     joerg 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    249  1.36     joerg 		if (tp == NULL)
    250  1.36     joerg 			return 0;
    251  1.36     joerg 		target = _rtld_resolve_ifunc(defobj, def);
    252  1.36     joerg 	} else {
    253  1.36     joerg 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
    254  1.36     joerg 	}
    255  1.36     joerg 
    256  1.26      matt 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    257  1.26      matt 	    defobj->strtab + def->st_name, (void *)*where,
    258  1.26      matt 	    (void *)target));
    259  1.26      matt 	if (*where != target)
    260  1.26      matt 		*where = target;
    261  1.26      matt 	if (tp)
    262  1.26      matt 		*tp = target;
    263  1.26      matt 	return 0;
    264  1.26      matt }
    265  1.26      matt 
    266  1.26      matt caddr_t
    267  1.26      matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    268  1.26      matt {
    269  1.31     skrll 	const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel
    270  1.31     skrll 	    + reloff);
    271  1.26      matt 	Elf_Addr new_value;
    272  1.26      matt 	int err;
    273  1.26      matt 
    274  1.30     skrll 	new_value = 0;	/* XXX gcc */
    275  1.30     skrll 
    276  1.34     joerg 	_rtld_shared_enter();
    277  1.26      matt 	err = _rtld_relocate_plt_object(obj, rel, &new_value);
    278  1.29  christos 	if (err)
    279  1.16   mycroft 		_rtld_die();
    280  1.34     joerg 	_rtld_shared_exit();
    281   1.6   mycroft 
    282  1.16   mycroft 	return (caddr_t)new_value;
    283  1.15  junyoung }
    284  1.15  junyoung 
    285  1.15  junyoung int
    286  1.15  junyoung _rtld_relocate_plt_objects(const Obj_Entry *obj)
    287  1.15  junyoung {
    288  1.15  junyoung 	const Elf_Rel *rel;
    289  1.26      matt 	int err = 0;
    290  1.15  junyoung 
    291  1.15  junyoung 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    292  1.26      matt 		err = _rtld_relocate_plt_object(obj, rel, NULL);
    293  1.26      matt 		if (err)
    294  1.26      matt 			break;
    295  1.15  junyoung 	}
    296  1.26      matt 	return err;
    297   1.2   mycroft }
    298  1.33     joerg 
    299  1.33     joerg /*
    300  1.33     joerg  * i386 specific GNU variant of __tls_get_addr using register based
    301  1.33     joerg  * argument passing.
    302  1.33     joerg  */
    303  1.33     joerg #define	DTV_MAX_INDEX(dtv)	((size_t)((dtv)[-1]))
    304  1.33     joerg 
    305  1.33     joerg __dso_public __attribute__((__regparm__(1))) void *
    306  1.33     joerg ___tls_get_addr(void *arg_)
    307  1.33     joerg {
    308  1.33     joerg 	size_t *arg = (size_t *)arg_;
    309  1.33     joerg 	void **dtv;
    310  1.33     joerg 	struct tls_tcb *tcb = __lwp_getprivate_fast();
    311  1.33     joerg 	size_t idx = arg[0], offset = arg[1];
    312  1.33     joerg 
    313  1.33     joerg 	dtv = tcb->tcb_dtv;
    314  1.33     joerg 
    315  1.33     joerg 	if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL))
    316  1.33     joerg 		return (uint8_t *)dtv[idx] + offset;
    317  1.33     joerg 
    318  1.33     joerg 	return _rtld_tls_get_addr(tcb, idx, offset);
    319  1.33     joerg }
    320