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