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