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