Home | History | Annotate | Line # | Download | only in sh3
mdreloc.c revision 1.21
      1 /*	$NetBSD: mdreloc.c,v 1.21 2005/08/20 19:01:17 skrll Exp $	*/
      2 
      3 #include <sys/cdefs.h>
      4 #ifndef lint
      5 __RCSID("$NetBSD: mdreloc.c,v 1.21 2005/08/20 19:01:17 skrll Exp $");
      6 #endif /* not lint */
      7 
      8 #include <sys/types.h>
      9 #include <sys/stat.h>
     10 
     11 #include "debug.h"
     12 #include "rtld.h"
     13 
     14 void _rtld_bind_start(void);
     15 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     16 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     17 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
     18     const Elf_Rela *, Elf_Addr *);
     19 
     20 void
     21 _rtld_setup_pltgot(const Obj_Entry *obj)
     22 {
     23 	obj->pltgot[1] = (Elf_Addr) obj;
     24 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     25 }
     26 
     27 void
     28 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     29 {
     30 	const Elf_Rela *rela = 0, *relalim;
     31 	Elf_Addr relasz = 0;
     32 	Elf_Addr *where;
     33 
     34 	for (; dynp->d_tag != DT_NULL; dynp++) {
     35 		switch (dynp->d_tag) {
     36 		case DT_RELA:
     37 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
     38 			break;
     39 		case DT_RELASZ:
     40 			relasz = dynp->d_un.d_val;
     41 			break;
     42 		}
     43 	}
     44 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
     45 	for (; rela < relalim; rela++) {
     46 		where = (Elf_Addr *)(relocbase + rela->r_offset);
     47 		*where = (Elf_Addr)(relocbase + rela->r_addend);
     48 	}
     49 }
     50 
     51 int
     52 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
     53 {
     54 	const Elf_Rela *rela;
     55 
     56 	for (rela = obj->rela; rela < obj->relalim; rela++) {
     57 		Elf_Addr        *where;
     58 		const Elf_Sym   *def;
     59 		const Obj_Entry *defobj;
     60 		Elf_Addr         tmp;
     61 		unsigned long	 symnum;
     62 
     63 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
     64 		symnum = ELF_R_SYM(rela->r_info);
     65 
     66 		switch (ELF_R_TYPE(rela->r_info)) {
     67 		case R_TYPE(NONE):
     68 			break;
     69 
     70 #if 1 /* XXX should not occur */
     71 		case R_TYPE(GOT32):
     72 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     73 			if (def == NULL)
     74 				return -1;
     75 
     76 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
     77 			    rela->r_addend);
     78 			if (*where != tmp)
     79 				*where = tmp;
     80 			rdbg(("GOT32 %s in %s --> %p in %s",
     81 			    obj->strtab + obj->symtab[symnum].st_name,
     82 			    obj->path, (void *)*where, defobj->path));
     83 			break;
     84 
     85 		case R_TYPE(REL32):
     86 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     87 			if (def == NULL)
     88 				return -1;
     89 
     90 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
     91 			    rela->r_addend) - (Elf_Addr)where;
     92 			if (*where != tmp)
     93 				*where = tmp;
     94 			rdbg(("PC32 %s in %s --> %p in %s",
     95 			    obj->strtab + obj->symtab[symnum].st_name,
     96 			    obj->path, (void *)*where, defobj->path));
     97 			break;
     98 #endif
     99 
    100 		case R_TYPE(DIR32):
    101 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    102 			if (def == NULL)
    103 				return -1;
    104 
    105 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    106 			    rela->r_addend);
    107 			if (*where != tmp)
    108 				*where = tmp;
    109 			rdbg(("32 %s in %s --> %p in %s",
    110 			    obj->strtab + obj->symtab[symnum].st_name,
    111 			    obj->path, (void *)*where, defobj->path));
    112 			break;
    113 
    114 		case R_TYPE(GLOB_DAT):
    115 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    116 			if (def == NULL)
    117 				return -1;
    118 
    119 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
    120 			    rela->r_addend;
    121 			if (*where != tmp)
    122 				*where = tmp;
    123 			rdbg(("GLOB_DAT %s in %s --> %p in %s",
    124 			    obj->strtab + obj->symtab[symnum].st_name,
    125 			    obj->path, (void *)*where, defobj->path));
    126 			break;
    127 
    128 		case R_TYPE(RELATIVE):
    129 			if (rela->r_addend)
    130 				*where = (Elf_Addr)obj->relocbase + rela->r_addend;
    131 			else
    132 				*where += (Elf_Addr)obj->relocbase;
    133 			rdbg(("RELATIVE in %s --> %p", obj->path,
    134 			    (void *)*where));
    135 			break;
    136 
    137 		case R_TYPE(COPY):
    138 			/*
    139 			 * These are deferred until all other relocations have
    140 			 * been done.  All we do here is make sure that the
    141 			 * COPY relocation is not in a shared library.  They
    142 			 * are allowed only in executable files.
    143 			 */
    144 			if (obj->isdynamic) {
    145 				_rtld_error(
    146 			"%s: Unexpected R_COPY relocation in shared library",
    147 				    obj->path);
    148 				return -1;
    149 			}
    150 			rdbg(("COPY (avoid in main)"));
    151 			break;
    152 
    153 		default:
    154 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    155 			    "addend = %p, contents = %p, symbol = %s",
    156 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    157 			    (void *)rela->r_offset, (void *)rela->r_addend,
    158 			    (void *)*where,
    159 			    obj->strtab + obj->symtab[symnum].st_name));
    160 			_rtld_error("%s: Unsupported relocation type %ld "
    161 			    "in non-PLT relocations\n",
    162 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    163 			return -1;
    164 		}
    165 	}
    166 	return 0;
    167 }
    168 
    169 int
    170 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    171 {
    172 	const Elf_Rela *rela;
    173 
    174 	if (!obj->relocbase)
    175 		return 0;
    176 
    177 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    178 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    179 
    180 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    181 
    182 		/* Just relocate the GOT slots pointing into the PLT */
    183 		*where += (Elf_Addr)obj->relocbase;
    184 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    185 	}
    186 
    187 	return 0;
    188 }
    189 
    190 caddr_t
    191 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    192 {
    193 	const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
    194 	Elf_Addr new_value;
    195 	int err;
    196 
    197 	err = _rtld_relocate_plt_object(obj, rela, &new_value);
    198 	if (err)
    199 		_rtld_die();
    200 
    201 	return (caddr_t)new_value;
    202 }
    203 
    204 int
    205 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    206 {
    207 	const Elf_Rela *rela = obj->pltrela;
    208 
    209 	for (; rela < obj->pltrelalim; rela++)
    210 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    211 			return -1;
    212 
    213 	return 0;
    214 }
    215 
    216 static inline int
    217 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
    218 {
    219 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    220 	Elf_Addr new_value;
    221 	const Elf_Sym  *def;
    222 	const Obj_Entry *defobj;
    223 
    224 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    225 
    226 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    227 	if (def == NULL)
    228 		return -1;
    229 
    230 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    231 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    232 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    233 	if (*where != new_value)
    234 		*where = new_value;
    235 
    236 	if (tp)
    237 		*tp = new_value;
    238 
    239 	return 0;
    240 }
    241