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