Home | History | Annotate | Line # | Download | only in arm
mdreloc.c revision 1.38.8.1
      1  1.38.8.1    martin /*	$NetBSD: mdreloc.c,v 1.38.8.1 2017/07/04 12:47:58 martin Exp $	*/
      2      1.26     skrll 
      3      1.26     skrll #include <sys/cdefs.h>
      4      1.26     skrll #ifndef lint
      5  1.38.8.1    martin __RCSID("$NetBSD: mdreloc.c,v 1.38.8.1 2017/07/04 12:47:58 martin Exp $");
      6      1.26     skrll #endif /* not lint */
      7      1.11  junyoung 
      8       1.1   mycroft #include <sys/types.h>
      9      1.23       mrg #include <string.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.22     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.22     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.29     lukem 	rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
     43      1.12   mycroft 	for (; rel < rellim; rel++) {
     44      1.12   mycroft 		where = (Elf_Addr *)(relocbase + rel->r_offset);
     45      1.12   mycroft 		*where += (Elf_Addr)relocbase;
     46      1.12   mycroft 	}
     47      1.12   mycroft }
     48      1.12   mycroft 
     49      1.16   thorpej /*
     50      1.16   thorpej  * It is possible for the compiler to emit relocations for unaligned data.
     51      1.16   thorpej  * We handle this situation with these inlines.
     52      1.16   thorpej  */
     53      1.16   thorpej #define	RELOC_ALIGNED_P(x) \
     54      1.16   thorpej 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
     55      1.16   thorpej 
     56      1.27     perry static inline Elf_Addr
     57      1.16   thorpej load_ptr(void *where)
     58      1.16   thorpej {
     59      1.16   thorpej 	Elf_Addr res;
     60      1.16   thorpej 
     61      1.16   thorpej 	memcpy(&res, where, sizeof(res));
     62      1.16   thorpej 
     63      1.16   thorpej 	return (res);
     64      1.16   thorpej }
     65      1.16   thorpej 
     66      1.27     perry static inline void
     67      1.16   thorpej store_ptr(void *where, Elf_Addr val)
     68      1.16   thorpej {
     69      1.16   thorpej 
     70      1.16   thorpej 	memcpy(where, &val, sizeof(val));
     71      1.16   thorpej }
     72      1.16   thorpej 
     73       1.2   mycroft int
     74      1.34     joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
     75       1.2   mycroft {
     76       1.3   mycroft 	const Elf_Rel *rel;
     77  1.38.8.1    martin 	const Elf_Sym *def = NULL;
     78  1.38.8.1    martin 	const Obj_Entry *defobj = NULL;
     79  1.38.8.1    martin 	unsigned long last_symnum = ULONG_MAX;
     80      1.12   mycroft 
     81       1.3   mycroft 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     82       1.3   mycroft 		Elf_Addr        *where;
     83       1.3   mycroft 		Elf_Addr         tmp;
     84       1.4   mycroft 		unsigned long	 symnum;
     85       1.3   mycroft 
     86       1.3   mycroft 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     87  1.38.8.1    martin 
     88  1.38.8.1    martin 		switch (ELF_R_TYPE(rel->r_info)) {
     89  1.38.8.1    martin 		case R_TYPE(PC24):	/* word32 S - P + A */
     90  1.38.8.1    martin 		case R_TYPE(ABS32):	/* word32 B + S + A */
     91  1.38.8.1    martin 		case R_TYPE(GLOB_DAT):	/* word32 B + S */
     92  1.38.8.1    martin 		case R_TYPE(TLS_DTPOFF32):
     93  1.38.8.1    martin 		case R_TYPE(TLS_DTPMOD32):
     94  1.38.8.1    martin 		case R_TYPE(TLS_TPOFF32):
     95  1.38.8.1    martin 			symnum = ELF_R_SYM(rel->r_info);
     96  1.38.8.1    martin 			if (last_symnum != symnum) {
     97  1.38.8.1    martin 				last_symnum = symnum;
     98  1.38.8.1    martin 				def = _rtld_find_symdef(symnum, obj, &defobj,
     99  1.38.8.1    martin 				    false);
    100  1.38.8.1    martin 				if (def == NULL)
    101  1.38.8.1    martin 					return -1;
    102  1.38.8.1    martin 			}
    103  1.38.8.1    martin 			break;
    104  1.38.8.1    martin 
    105  1.38.8.1    martin 		default:
    106  1.38.8.1    martin 			break;
    107  1.38.8.1    martin 		}
    108       1.3   mycroft 
    109       1.3   mycroft 		switch (ELF_R_TYPE(rel->r_info)) {
    110       1.3   mycroft 		case R_TYPE(NONE):
    111       1.3   mycroft 			break;
    112       1.2   mycroft 
    113       1.2   mycroft #if 1 /* XXX should not occur */
    114       1.3   mycroft 		case R_TYPE(PC24): {	/* word32 S - P + A */
    115       1.3   mycroft 			Elf32_Sword addend;
    116       1.2   mycroft 
    117       1.3   mycroft 			/*
    118       1.3   mycroft 			 * Extract addend and sign-extend if needed.
    119       1.3   mycroft 			 */
    120      1.17   thorpej 			addend = *where;
    121       1.3   mycroft 			if (addend & 0x00800000)
    122       1.3   mycroft 				addend |= 0xff000000;
    123       1.3   mycroft 			tmp = (Elf_Addr)obj->relocbase + def->st_value
    124       1.3   mycroft 			    - (Elf_Addr)where + (addend << 2);
    125       1.3   mycroft 			if ((tmp & 0xfe000000) != 0xfe000000 &&
    126       1.3   mycroft 			    (tmp & 0xfe000000) != 0) {
    127       1.3   mycroft 				_rtld_error(
    128       1.3   mycroft 				"%s: R_ARM_PC24 relocation @ %p to %s failed "
    129       1.3   mycroft 				"(displacement %ld (%#lx) out of range)",
    130       1.3   mycroft 				    obj->path, where,
    131  1.38.8.1    martin 				    obj->strtab + obj->symtab[
    132  1.38.8.1    martin 				        ELF_R_SYM(rel->r_info)].st_name,
    133       1.5   mycroft 				    (long) tmp, (long) tmp);
    134       1.3   mycroft 				return -1;
    135       1.3   mycroft 			}
    136       1.3   mycroft 			tmp >>= 2;
    137      1.17   thorpej 			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
    138      1.14   mycroft 			rdbg(("PC24 %s in %s --> %p @ %p in %s",
    139  1.38.8.1    martin 			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
    140  1.38.8.1    martin 			    .st_name, obj->path, (void *)*where, where,
    141  1.38.8.1    martin 			    defobj->path));
    142       1.3   mycroft 			break;
    143       1.2   mycroft 		}
    144       1.2   mycroft #endif
    145       1.2   mycroft 
    146       1.3   mycroft 		case R_TYPE(ABS32):	/* word32 B + S + A */
    147      1.15   mycroft 		case R_TYPE(GLOB_DAT):	/* word32 B + S */
    148      1.16   thorpej 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    149      1.16   thorpej 				tmp = *where + (Elf_Addr)defobj->relocbase +
    150      1.16   thorpej 				    def->st_value;
    151      1.24  rearnsha 				/* Set the Thumb bit, if needed.  */
    152      1.25   mycroft 				if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
    153      1.24  rearnsha 				    tmp |= 1;
    154      1.16   thorpej 				*where = tmp;
    155      1.16   thorpej 			} else {
    156      1.16   thorpej 				tmp = load_ptr(where) +
    157      1.16   thorpej 				    (Elf_Addr)defobj->relocbase +
    158      1.16   thorpej 				    def->st_value;
    159      1.24  rearnsha 				/* Set the Thumb bit, if needed.  */
    160      1.25   mycroft 				if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
    161      1.24  rearnsha 				    tmp |= 1;
    162      1.16   thorpej 				store_ptr(where, tmp);
    163      1.16   thorpej 			}
    164      1.15   mycroft 			rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
    165  1.38.8.1    martin 			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
    166  1.38.8.1    martin 			    .st_name, obj->path, (void *)tmp, where,
    167  1.38.8.1    martin 			    defobj->path));
    168       1.3   mycroft 			break;
    169       1.3   mycroft 
    170       1.3   mycroft 		case R_TYPE(RELATIVE):	/* word32 B + A */
    171      1.16   thorpej 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    172      1.16   thorpej 				tmp = *where + (Elf_Addr)obj->relocbase;
    173      1.16   thorpej 				*where = tmp;
    174      1.16   thorpej 			} else {
    175      1.16   thorpej 				tmp = load_ptr(where) +
    176      1.16   thorpej 				    (Elf_Addr)obj->relocbase;
    177      1.16   thorpej 				store_ptr(where, tmp);
    178      1.16   thorpej 			}
    179      1.14   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    180      1.16   thorpej 			    (void *)tmp));
    181       1.3   mycroft 			break;
    182       1.3   mycroft 
    183       1.3   mycroft 		case R_TYPE(COPY):
    184       1.3   mycroft 			/*
    185       1.3   mycroft 			 * These are deferred until all other relocations have
    186       1.3   mycroft 			 * been done.  All we do here is make sure that the
    187       1.3   mycroft 			 * COPY relocation is not in a shared library.  They
    188       1.3   mycroft 			 * are allowed only in executable files.
    189       1.3   mycroft 			 */
    190       1.8   mycroft 			if (obj->isdynamic) {
    191       1.3   mycroft 				_rtld_error(
    192       1.2   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    193       1.3   mycroft 				    obj->path);
    194       1.3   mycroft 				return -1;
    195       1.3   mycroft 			}
    196      1.14   mycroft 			rdbg(("COPY (avoid in main)"));
    197       1.3   mycroft 			break;
    198       1.3   mycroft 
    199      1.36      matt 		case R_TYPE(TLS_DTPOFF32):
    200      1.36      matt 			tmp = (Elf_Addr)(def->st_value);
    201      1.36      matt 			if (__predict_true(RELOC_ALIGNED_P(where)))
    202      1.36      matt 				*where = tmp;
    203      1.36      matt 			else
    204      1.36      matt 				store_ptr(where, tmp);
    205      1.36      matt 
    206      1.36      matt 			rdbg(("TLS_DTPOFF32 %s in %s --> %p",
    207  1.38.8.1    martin 			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
    208  1.38.8.1    martin 			    .st_name, obj->path, (void *)tmp));
    209      1.36      matt 
    210      1.36      matt 			break;
    211      1.36      matt 		case R_TYPE(TLS_DTPMOD32):
    212      1.36      matt 			tmp = (Elf_Addr)(defobj->tlsindex);
    213      1.36      matt 			if (__predict_true(RELOC_ALIGNED_P(where)))
    214      1.36      matt 				*where = tmp;
    215      1.36      matt 			else
    216      1.36      matt 				store_ptr(where, tmp);
    217      1.36      matt 
    218      1.36      matt 			rdbg(("TLS_DTPMOD32 %s in %s --> %p",
    219  1.38.8.1    martin 			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
    220  1.38.8.1    martin 			    .st_name, obj->path, (void *)tmp));
    221      1.36      matt 
    222      1.36      matt 			break;
    223      1.36      matt 
    224      1.36      matt 		case R_TYPE(TLS_TPOFF32):
    225      1.36      matt 			if (!defobj->tls_done &&
    226      1.36      matt 			    _rtld_tls_offset_allocate(obj))
    227      1.36      matt 				return -1;
    228      1.36      matt 
    229      1.36      matt 			tmp = (Elf_Addr)def->st_value + defobj->tlsoffset +
    230      1.36      matt 			    sizeof(struct tls_tcb);
    231      1.36      matt 			if (__predict_true(RELOC_ALIGNED_P(where)))
    232      1.36      matt 				*where = tmp;
    233      1.36      matt 			else
    234      1.36      matt 				store_ptr(where, tmp);
    235      1.36      matt 			rdbg(("TLS_TPOFF32 %s in %s --> %p",
    236  1.38.8.1    martin 			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
    237  1.38.8.1    martin 			    .st_name, obj->path, (void *)tmp));
    238      1.36      matt 			break;
    239      1.36      matt 
    240       1.3   mycroft 		default:
    241      1.14   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    242  1.38.8.1    martin 			    "contents = %p",
    243  1.38.8.1    martin 			    (u_long)ELF_R_SYM(rel->r_info),
    244  1.38.8.1    martin 			    (u_long)ELF_R_TYPE(rel->r_info),
    245  1.38.8.1    martin 			    (void *)rel->r_offset, (void *)load_ptr(where)));
    246       1.3   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    247      1.30      jmmv 			    "in non-PLT relocations",
    248       1.3   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    249       1.2   mycroft 			return -1;
    250       1.2   mycroft 		}
    251       1.2   mycroft 	}
    252       1.2   mycroft 	return 0;
    253       1.2   mycroft }
    254       1.6   mycroft 
    255       1.6   mycroft int
    256      1.22     skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    257       1.6   mycroft {
    258       1.6   mycroft 	const Elf_Rel *rel;
    259       1.6   mycroft 
    260      1.20   mycroft 	if (!obj->relocbase)
    261       1.6   mycroft 		return 0;
    262       1.6   mycroft 
    263       1.6   mycroft 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    264       1.6   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    265       1.6   mycroft 
    266       1.6   mycroft 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    267       1.6   mycroft 
    268       1.6   mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    269       1.6   mycroft 		*where += (Elf_Addr)obj->relocbase;
    270      1.14   mycroft 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    271       1.6   mycroft 	}
    272       1.6   mycroft 
    273       1.6   mycroft 	return 0;
    274       1.6   mycroft }
    275       1.6   mycroft 
    276      1.28      matt static int
    277      1.28      matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
    278      1.28      matt 	Elf_Addr *tp)
    279       1.6   mycroft {
    280      1.18   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    281       1.6   mycroft 	Elf_Addr new_value;
    282       1.6   mycroft 	const Elf_Sym  *def;
    283       1.6   mycroft 	const Obj_Entry *defobj;
    284      1.31  christos 	unsigned long info = rel->r_info;
    285       1.6   mycroft 
    286      1.33     skrll 	assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
    287       1.6   mycroft 
    288      1.31  christos 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    289      1.31  christos 	if (__predict_false(def == NULL))
    290      1.28      matt 		return -1;
    291      1.31  christos 	if (__predict_false(def == &_rtld_sym_zero))
    292      1.31  christos 		return 0;
    293       1.6   mycroft 
    294      1.38     joerg 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    295      1.38     joerg 		if (tp == NULL)
    296      1.38     joerg 			return 0;
    297      1.38     joerg 		new_value = _rtld_resolve_ifunc(defobj, def);
    298      1.38     joerg 	} else {
    299      1.38     joerg 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    300      1.38     joerg 	}
    301      1.24  rearnsha 	/* Set the Thumb bit, if needed.  */
    302      1.25   mycroft 	if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
    303      1.24  rearnsha 		new_value |= 1;
    304      1.14   mycroft 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    305       1.6   mycroft 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    306       1.6   mycroft 	if (*where != new_value)
    307       1.6   mycroft 		*where = new_value;
    308      1.28      matt 	if (tp)
    309      1.28      matt 		*tp = new_value;
    310      1.28      matt 
    311      1.28      matt 	return 0;
    312      1.28      matt }
    313      1.28      matt 
    314      1.28      matt caddr_t
    315      1.28      matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    316      1.28      matt {
    317      1.29     lukem 	const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel + reloff);
    318      1.32     skrll 	Elf_Addr new_value = 0;	/* XXX gcc */
    319      1.28      matt 	int err;
    320      1.28      matt 
    321      1.35     joerg 	_rtld_shared_enter();
    322      1.28      matt 	err = _rtld_relocate_plt_object(obj, rel, &new_value);
    323      1.31  christos 	if (err)
    324      1.28      matt 		_rtld_die();
    325      1.35     joerg 	_rtld_shared_exit();
    326       1.6   mycroft 
    327      1.18   mycroft 	return (caddr_t)new_value;
    328      1.21     skrll }
    329      1.21     skrll int
    330      1.21     skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
    331      1.21     skrll {
    332      1.21     skrll 	const Elf_Rel *rel;
    333      1.28      matt 	int err = 0;
    334      1.21     skrll 
    335      1.21     skrll 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    336      1.28      matt 		err = _rtld_relocate_plt_object(obj, rel, NULL);
    337      1.28      matt 		if (err)
    338      1.28      matt 			break;
    339      1.21     skrll 	}
    340      1.28      matt 
    341      1.28      matt 	return err;
    342       1.6   mycroft }
    343