Home | History | Annotate | Line # | Download | only in arm
mdreloc.c revision 1.18
      1 /*	$NetBSD: mdreloc.c,v 1.18 2002/09/25 07:27:51 mycroft 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 __P((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(dynp, relocbase)
     22 	Elf_Dyn *dynp;
     23 	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(obj, self)
     72 	const Obj_Entry *obj;
     73 	bool self;
     74 {
     75 	const Elf_Rel *rel;
     76 
     77 	if (self)
     78 		return 0;
     79 
     80 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     81 		Elf_Addr        *where;
     82 		const Elf_Sym   *def;
     83 		const Obj_Entry *defobj;
     84 		Elf_Addr         tmp;
     85 		unsigned long	 symnum;
     86 
     87 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     88 		symnum = ELF_R_SYM(rel->r_info);
     89 
     90 		switch (ELF_R_TYPE(rel->r_info)) {
     91 		case R_TYPE(NONE):
     92 			break;
     93 
     94 #if 1 /* XXX should not occur */
     95 		case R_TYPE(PC24): {	/* word32 S - P + A */
     96 			Elf32_Sword addend;
     97 
     98 			/*
     99 			 * Extract addend and sign-extend if needed.
    100 			 */
    101 			addend = *where;
    102 			if (addend & 0x00800000)
    103 				addend |= 0xff000000;
    104 
    105 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    106 			if (def == NULL)
    107 				return -1;
    108 			tmp = (Elf_Addr)obj->relocbase + def->st_value
    109 			    - (Elf_Addr)where + (addend << 2);
    110 			if ((tmp & 0xfe000000) != 0xfe000000 &&
    111 			    (tmp & 0xfe000000) != 0) {
    112 				_rtld_error(
    113 				"%s: R_ARM_PC24 relocation @ %p to %s failed "
    114 				"(displacement %ld (%#lx) out of range)",
    115 				    obj->path, where,
    116 				    obj->strtab + obj->symtab[symnum].st_name,
    117 				    (long) tmp, (long) tmp);
    118 				return -1;
    119 			}
    120 			tmp >>= 2;
    121 			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
    122 			rdbg(("PC24 %s in %s --> %p @ %p in %s",
    123 			    obj->strtab + obj->symtab[symnum].st_name,
    124 			    obj->path, (void *)*where, where, defobj->path));
    125 			break;
    126 		}
    127 #endif
    128 
    129 		case R_TYPE(ABS32):	/* word32 B + S + A */
    130 		case R_TYPE(GLOB_DAT):	/* word32 B + S */
    131 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    132 			if (def == NULL)
    133 				return -1;
    134 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    135 				tmp = *where + (Elf_Addr)defobj->relocbase +
    136 				    def->st_value;
    137 				*where = tmp;
    138 			} else {
    139 				tmp = load_ptr(where) +
    140 				    (Elf_Addr)defobj->relocbase +
    141 				    def->st_value;
    142 				store_ptr(where, tmp);
    143 			}
    144 			rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
    145 			    obj->strtab + obj->symtab[symnum].st_name,
    146 			    obj->path, (void *)tmp, where, defobj->path));
    147 			break;
    148 
    149 		case R_TYPE(RELATIVE):	/* word32 B + A */
    150 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    151 				tmp = *where + (Elf_Addr)obj->relocbase;
    152 				*where = tmp;
    153 			} else {
    154 				tmp = load_ptr(where) +
    155 				    (Elf_Addr)obj->relocbase;
    156 				store_ptr(where, tmp);
    157 			}
    158 			rdbg(("RELATIVE in %s --> %p", obj->path,
    159 			    (void *)tmp));
    160 			break;
    161 
    162 		case R_TYPE(COPY):
    163 			/*
    164 			 * These are deferred until all other relocations have
    165 			 * been done.  All we do here is make sure that the
    166 			 * COPY relocation is not in a shared library.  They
    167 			 * are allowed only in executable files.
    168 			 */
    169 			if (obj->isdynamic) {
    170 				_rtld_error(
    171 			"%s: Unexpected R_COPY relocation in shared library",
    172 				    obj->path);
    173 				return -1;
    174 			}
    175 			rdbg(("COPY (avoid in main)"));
    176 			break;
    177 
    178 		default:
    179 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    180 			    "contents = %p, symbol = %s",
    181 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    182 			    (void *)rel->r_offset, (void *)load_ptr(where),
    183 			    obj->strtab + obj->symtab[symnum].st_name));
    184 			_rtld_error("%s: Unsupported relocation type %ld "
    185 			    "in non-PLT relocations\n",
    186 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    187 			return -1;
    188 		}
    189 	}
    190 	return 0;
    191 }
    192 
    193 int
    194 _rtld_relocate_plt_lazy(obj)
    195 	const Obj_Entry *obj;
    196 {
    197 	const Elf_Rel *rel;
    198 
    199 	if (!obj->isdynamic)
    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(obj, reloff)
    217 	const Obj_Entry *obj;
    218 	Elf_Word reloff;
    219 {
    220 	const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
    221 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    222 	Elf_Addr new_value;
    223 	const Elf_Sym  *def;
    224 	const Obj_Entry *defobj;
    225 
    226 	assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    227 
    228 	def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
    229 	if (def == NULL)
    230 		_rtld_die();
    231 
    232 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    233 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    234 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    235 	if (*where != new_value)
    236 		*where = new_value;
    237 
    238 	return (caddr_t)new_value;
    239 }
    240