Home | History | Annotate | Line # | Download | only in arm
mdreloc.c revision 1.29
      1  1.29     lukem /*	$NetBSD: mdreloc.c,v 1.29 2009/03/16 02:46:47 lukem Exp $	*/
      2  1.26     skrll 
      3  1.26     skrll #include <sys/cdefs.h>
      4  1.26     skrll #ifndef lint
      5  1.29     lukem __RCSID("$NetBSD: mdreloc.c,v 1.29 2009/03/16 02:46:47 lukem Exp $");
      6  1.26     skrll #endif /* not lint */
      7  1.11  junyoung 
      8   1.1   mycroft #include <sys/types.h>
      9   1.1   mycroft #include <sys/stat.h>
     10  1.23       mrg 
     11  1.23       mrg #include <string.h>
     12   1.1   mycroft 
     13   1.1   mycroft #include "debug.h"
     14   1.1   mycroft #include "rtld.h"
     15   1.1   mycroft 
     16  1.13   mycroft void _rtld_bind_start(void);
     17  1.12   mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     18  1.22     skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     19  1.12   mycroft 
     20   1.1   mycroft void
     21   1.1   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
     22   1.1   mycroft {
     23   1.1   mycroft 	obj->pltgot[1] = (Elf_Addr) obj;
     24   1.1   mycroft 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     25   1.1   mycroft }
     26   1.2   mycroft 
     27  1.12   mycroft void
     28  1.22     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     29  1.12   mycroft {
     30  1.12   mycroft 	const Elf_Rel *rel = 0, *rellim;
     31  1.12   mycroft 	Elf_Addr relsz = 0;
     32  1.12   mycroft 	Elf_Addr *where;
     33  1.12   mycroft 
     34  1.12   mycroft 	for (; dynp->d_tag != DT_NULL; dynp++) {
     35  1.12   mycroft 		switch (dynp->d_tag) {
     36  1.12   mycroft 		case DT_REL:
     37  1.12   mycroft 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     38  1.12   mycroft 			break;
     39  1.12   mycroft 		case DT_RELSZ:
     40  1.12   mycroft 			relsz = dynp->d_un.d_val;
     41  1.12   mycroft 			break;
     42  1.12   mycroft 		}
     43  1.12   mycroft 	}
     44  1.29     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.16   thorpej /*
     52  1.16   thorpej  * It is possible for the compiler to emit relocations for unaligned data.
     53  1.16   thorpej  * We handle this situation with these inlines.
     54  1.16   thorpej  */
     55  1.16   thorpej #define	RELOC_ALIGNED_P(x) \
     56  1.16   thorpej 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
     57  1.16   thorpej 
     58  1.27     perry static inline Elf_Addr
     59  1.16   thorpej load_ptr(void *where)
     60  1.16   thorpej {
     61  1.16   thorpej 	Elf_Addr res;
     62  1.16   thorpej 
     63  1.16   thorpej 	memcpy(&res, where, sizeof(res));
     64  1.16   thorpej 
     65  1.16   thorpej 	return (res);
     66  1.16   thorpej }
     67  1.16   thorpej 
     68  1.27     perry static inline void
     69  1.16   thorpej store_ptr(void *where, Elf_Addr val)
     70  1.16   thorpej {
     71  1.16   thorpej 
     72  1.16   thorpej 	memcpy(where, &val, sizeof(val));
     73  1.16   thorpej }
     74  1.16   thorpej 
     75   1.2   mycroft int
     76  1.22     skrll _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
     77   1.2   mycroft {
     78   1.3   mycroft 	const Elf_Rel *rel;
     79  1.12   mycroft 
     80   1.3   mycroft 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     81   1.3   mycroft 		Elf_Addr        *where;
     82   1.3   mycroft 		const Elf_Sym   *def;
     83   1.3   mycroft 		const Obj_Entry *defobj;
     84   1.3   mycroft 		Elf_Addr         tmp;
     85   1.4   mycroft 		unsigned long	 symnum;
     86   1.3   mycroft 
     87   1.3   mycroft 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     88   1.4   mycroft 		symnum = ELF_R_SYM(rel->r_info);
     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(PC24): {	/* word32 S - P + A */
     96   1.3   mycroft 			Elf32_Sword addend;
     97   1.2   mycroft 
     98   1.3   mycroft 			/*
     99   1.3   mycroft 			 * Extract addend and sign-extend if needed.
    100   1.3   mycroft 			 */
    101  1.17   thorpej 			addend = *where;
    102   1.3   mycroft 			if (addend & 0x00800000)
    103   1.3   mycroft 				addend |= 0xff000000;
    104   1.3   mycroft 
    105   1.4   mycroft 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    106   1.3   mycroft 			if (def == NULL)
    107   1.3   mycroft 				return -1;
    108   1.3   mycroft 			tmp = (Elf_Addr)obj->relocbase + def->st_value
    109   1.3   mycroft 			    - (Elf_Addr)where + (addend << 2);
    110   1.3   mycroft 			if ((tmp & 0xfe000000) != 0xfe000000 &&
    111   1.3   mycroft 			    (tmp & 0xfe000000) != 0) {
    112   1.3   mycroft 				_rtld_error(
    113   1.3   mycroft 				"%s: R_ARM_PC24 relocation @ %p to %s failed "
    114   1.3   mycroft 				"(displacement %ld (%#lx) out of range)",
    115   1.3   mycroft 				    obj->path, where,
    116   1.5   mycroft 				    obj->strtab + obj->symtab[symnum].st_name,
    117   1.5   mycroft 				    (long) tmp, (long) tmp);
    118   1.3   mycroft 				return -1;
    119   1.3   mycroft 			}
    120   1.3   mycroft 			tmp >>= 2;
    121  1.17   thorpej 			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
    122  1.14   mycroft 			rdbg(("PC24 %s in %s --> %p @ %p in %s",
    123   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    124  1.17   thorpej 			    obj->path, (void *)*where, where, defobj->path));
    125   1.3   mycroft 			break;
    126   1.2   mycroft 		}
    127   1.2   mycroft #endif
    128   1.2   mycroft 
    129   1.3   mycroft 		case R_TYPE(ABS32):	/* word32 B + S + A */
    130  1.15   mycroft 		case R_TYPE(GLOB_DAT):	/* word32 B + S */
    131   1.4   mycroft 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    132   1.3   mycroft 			if (def == NULL)
    133   1.3   mycroft 				return -1;
    134  1.16   thorpej 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    135  1.16   thorpej 				tmp = *where + (Elf_Addr)defobj->relocbase +
    136  1.16   thorpej 				    def->st_value;
    137  1.24  rearnsha 				/* Set the Thumb bit, if needed.  */
    138  1.25   mycroft 				if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
    139  1.24  rearnsha 				    tmp |= 1;
    140  1.16   thorpej 				*where = tmp;
    141  1.16   thorpej 			} else {
    142  1.16   thorpej 				tmp = load_ptr(where) +
    143  1.16   thorpej 				    (Elf_Addr)defobj->relocbase +
    144  1.16   thorpej 				    def->st_value;
    145  1.24  rearnsha 				/* Set the Thumb bit, if needed.  */
    146  1.25   mycroft 				if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
    147  1.24  rearnsha 				    tmp |= 1;
    148  1.16   thorpej 				store_ptr(where, tmp);
    149  1.16   thorpej 			}
    150  1.15   mycroft 			rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
    151   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    152  1.16   thorpej 			    obj->path, (void *)tmp, where, defobj->path));
    153   1.3   mycroft 			break;
    154   1.3   mycroft 
    155   1.3   mycroft 		case R_TYPE(RELATIVE):	/* word32 B + A */
    156  1.16   thorpej 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    157  1.16   thorpej 				tmp = *where + (Elf_Addr)obj->relocbase;
    158  1.16   thorpej 				*where = tmp;
    159  1.16   thorpej 			} else {
    160  1.16   thorpej 				tmp = load_ptr(where) +
    161  1.16   thorpej 				    (Elf_Addr)obj->relocbase;
    162  1.16   thorpej 				store_ptr(where, tmp);
    163  1.16   thorpej 			}
    164  1.14   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    165  1.16   thorpej 			    (void *)tmp));
    166   1.3   mycroft 			break;
    167   1.3   mycroft 
    168   1.3   mycroft 		case R_TYPE(COPY):
    169   1.3   mycroft 			/*
    170   1.3   mycroft 			 * These are deferred until all other relocations have
    171   1.3   mycroft 			 * been done.  All we do here is make sure that the
    172   1.3   mycroft 			 * COPY relocation is not in a shared library.  They
    173   1.3   mycroft 			 * are allowed only in executable files.
    174   1.3   mycroft 			 */
    175   1.8   mycroft 			if (obj->isdynamic) {
    176   1.3   mycroft 				_rtld_error(
    177   1.2   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    178   1.3   mycroft 				    obj->path);
    179   1.3   mycroft 				return -1;
    180   1.3   mycroft 			}
    181  1.14   mycroft 			rdbg(("COPY (avoid in main)"));
    182   1.3   mycroft 			break;
    183   1.3   mycroft 
    184   1.3   mycroft 		default:
    185  1.14   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    186   1.3   mycroft 			    "contents = %p, symbol = %s",
    187   1.4   mycroft 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    188  1.16   thorpej 			    (void *)rel->r_offset, (void *)load_ptr(where),
    189   1.4   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    190   1.3   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    191   1.3   mycroft 			    "in non-PLT relocations\n",
    192   1.3   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    193   1.2   mycroft 			return -1;
    194   1.2   mycroft 		}
    195   1.2   mycroft 	}
    196   1.2   mycroft 	return 0;
    197   1.2   mycroft }
    198   1.6   mycroft 
    199   1.6   mycroft int
    200  1.22     skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    201   1.6   mycroft {
    202   1.6   mycroft 	const Elf_Rel *rel;
    203   1.6   mycroft 
    204  1.20   mycroft 	if (!obj->relocbase)
    205   1.6   mycroft 		return 0;
    206   1.6   mycroft 
    207   1.6   mycroft 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    208   1.6   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    209   1.6   mycroft 
    210   1.6   mycroft 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    211   1.6   mycroft 
    212   1.6   mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    213   1.6   mycroft 		*where += (Elf_Addr)obj->relocbase;
    214  1.14   mycroft 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    215   1.6   mycroft 	}
    216   1.6   mycroft 
    217   1.6   mycroft 	return 0;
    218   1.6   mycroft }
    219   1.6   mycroft 
    220  1.28      matt static int
    221  1.28      matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
    222  1.28      matt 	Elf_Addr *tp)
    223   1.6   mycroft {
    224  1.18   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    225   1.6   mycroft 	Elf_Addr new_value;
    226   1.6   mycroft 	const Elf_Sym  *def;
    227   1.6   mycroft 	const Obj_Entry *defobj;
    228   1.6   mycroft 
    229  1.18   mycroft 	assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    230   1.6   mycroft 
    231  1.18   mycroft 	def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
    232   1.6   mycroft 	if (def == NULL)
    233  1.28      matt 		return -1;
    234   1.6   mycroft 
    235   1.6   mycroft 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    236  1.24  rearnsha 	/* Set the Thumb bit, if needed.  */
    237  1.25   mycroft 	if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
    238  1.24  rearnsha 		new_value |= 1;
    239  1.14   mycroft 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    240   1.6   mycroft 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    241   1.6   mycroft 	if (*where != new_value)
    242   1.6   mycroft 		*where = new_value;
    243  1.28      matt 	if (tp)
    244  1.28      matt 		*tp = new_value;
    245  1.28      matt 
    246  1.28      matt 	return 0;
    247  1.28      matt }
    248  1.28      matt 
    249  1.28      matt caddr_t
    250  1.28      matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    251  1.28      matt {
    252  1.29     lukem 	const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel + reloff);
    253  1.28      matt 	Elf_Addr new_value;
    254  1.28      matt 	int err;
    255  1.28      matt 
    256  1.28      matt 	err = _rtld_relocate_plt_object(obj, rel, &new_value);
    257  1.28      matt 	if (err || new_value == 0)
    258  1.28      matt 		_rtld_die();
    259   1.6   mycroft 
    260  1.18   mycroft 	return (caddr_t)new_value;
    261  1.21     skrll }
    262  1.21     skrll int
    263  1.21     skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
    264  1.21     skrll {
    265  1.21     skrll 	const Elf_Rel *rel;
    266  1.28      matt 	int err = 0;
    267  1.21     skrll 
    268  1.21     skrll 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    269  1.28      matt 		err = _rtld_relocate_plt_object(obj, rel, NULL);
    270  1.28      matt 		if (err)
    271  1.28      matt 			break;
    272  1.21     skrll 	}
    273  1.28      matt 
    274  1.28      matt 	return err;
    275   1.6   mycroft }
    276