Home | History | Annotate | Line # | Download | only in m68k
mdreloc.c revision 1.12
      1 /*	$NetBSD: mdreloc.c,v 1.12 2002/09/12 20:21:00 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 
     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_Rela *rela = 0, *relalim;
     25 	Elf_Addr relasz = 0;
     26 	Elf_Addr *where;
     27 
     28 	for (; dynp->d_tag != DT_NULL; dynp++) {
     29 		switch (dynp->d_tag) {
     30 		case DT_RELA:
     31 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
     32 			break;
     33 		case DT_RELASZ:
     34 			relasz = dynp->d_un.d_val;
     35 			break;
     36 		}
     37 	}
     38 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
     39 	for (; rela < relalim; rela++) {
     40 		where = (Elf_Addr *)(relocbase + rela->r_offset);
     41 		*where += (Elf_Addr)relocbase;
     42 	}
     43 }
     44 
     45 int
     46 _rtld_relocate_nonplt_objects(obj, self, dodebug)
     47 	const Obj_Entry *obj;
     48 	bool self;
     49 	bool dodebug;
     50 {
     51 	const Elf_Rela *rela;
     52 
     53 	if (self)
     54 		return 0;
     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(PC32):
     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) - (Elf_Addr)where;
     78 			if (*where != tmp)
     79 				*where = tmp;
     80 			rdbg(dodebug, ("PC32 %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(GOT32):
     86 #endif
     87 		case R_TYPE(32):
     88 		case R_TYPE(GLOB_DAT):
     89 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     90 			if (def == NULL)
     91 				return -1;
     92 
     93 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
     94 			    rela->r_addend);
     95 			if (*where != tmp)
     96 				*where = tmp;
     97 			rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
     98 			    obj->strtab + obj->symtab[symnum].st_name,
     99 			    obj->path, (void *)*where, defobj->path));
    100 			break;
    101 
    102 		case R_TYPE(RELATIVE):
    103 			*where += (Elf_Addr)obj->relocbase;
    104 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    105 			    (void *)*where));
    106 			break;
    107 
    108 		case R_TYPE(COPY):
    109 			/*
    110 			 * These are deferred until all other relocations have
    111 			 * been done.  All we do here is make sure that the
    112 			 * COPY relocation is not in a shared library.  They
    113 			 * are allowed only in executable files.
    114 			 */
    115 			if (obj->isdynamic) {
    116 				_rtld_error(
    117 			"%s: Unexpected R_COPY relocation in shared library",
    118 				    obj->path);
    119 				return -1;
    120 			}
    121 			rdbg(dodebug, ("COPY (avoid in main)"));
    122 			break;
    123 
    124 		default:
    125 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    126 			    "addend = %p, contents = %p, symbol = %s",
    127 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    128 			    (void *)rela->r_offset, (void *)rela->r_addend,
    129 			    (void *)*where,
    130 			    obj->strtab + obj->symtab[symnum].st_name));
    131 			_rtld_error("%s: Unsupported relocation type %ld "
    132 			    "in non-PLT relocations\n",
    133 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    134 			return -1;
    135 		}
    136 	}
    137 	return 0;
    138 }
    139 
    140 int
    141 _rtld_relocate_plt_lazy(obj, dodebug)
    142 	const Obj_Entry *obj;
    143 	bool dodebug;
    144 {
    145 	const Elf_Rela *rela;
    146 
    147 	if (!obj->isdynamic)
    148 		return 0;
    149 
    150 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    151 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    152 
    153 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    154 
    155 		/* Just relocate the GOT slots pointing into the PLT */
    156 		*where += (Elf_Addr)obj->relocbase;
    157 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    158 		    (void *)*where));
    159 	}
    160 
    161 	return 0;
    162 }
    163 
    164 int
    165 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
    166 	const Obj_Entry *obj;
    167 	const Elf_Rela *rela;
    168 	caddr_t *addrp;
    169 	bool dodebug;
    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 
    176 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    177 
    178 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    179 	if (def == NULL)
    180 		return -1;
    181 
    182 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    183 	rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
    184 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    185 	if (*where != new_value)
    186 		*where = new_value;
    187 
    188 	*addrp = (caddr_t)new_value;
    189 	return 0;
    190 }
    191