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