Home | History | Annotate | Line # | Download | only in m68k
mdreloc.c revision 1.14
      1 /*	$NetBSD: mdreloc.c,v 1.14 2002/09/25 07:27:52 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_Rela *rela = 0, *relalim;
     26 	Elf_Addr relasz = 0;
     27 	Elf_Addr *where;
     28 
     29 	for (; dynp->d_tag != DT_NULL; dynp++) {
     30 		switch (dynp->d_tag) {
     31 		case DT_RELA:
     32 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
     33 			break;
     34 		case DT_RELASZ:
     35 			relasz = dynp->d_un.d_val;
     36 			break;
     37 		}
     38 	}
     39 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
     40 	for (; rela < relalim; rela++) {
     41 		where = (Elf_Addr *)(relocbase + rela->r_offset);
     42 		*where += (Elf_Addr)relocbase;
     43 	}
     44 }
     45 
     46 int
     47 _rtld_relocate_nonplt_objects(obj, self)
     48 	const Obj_Entry *obj;
     49 	bool self;
     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(("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(("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(("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(("COPY (avoid in main)"));
    122 			break;
    123 
    124 		default:
    125 			rdbg(("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)
    142 	const Obj_Entry *obj;
    143 {
    144 	const Elf_Rela *rela;
    145 
    146 	if (!obj->isdynamic)
    147 		return 0;
    148 
    149 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    150 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    151 
    152 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    153 
    154 		/* Just relocate the GOT slots pointing into the PLT */
    155 		*where += (Elf_Addr)obj->relocbase;
    156 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    157 	}
    158 
    159 	return 0;
    160 }
    161 
    162 caddr_t
    163 _rtld_bind(obj, reloff)
    164 	const Obj_Entry *obj;
    165 	Elf_Word reloff;
    166 {
    167 	const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
    168 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    169 	Elf_Addr new_value;
    170 	const Elf_Sym  *def;
    171 	const Obj_Entry *defobj;
    172 
    173 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    174 
    175 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    176 	if (def == NULL)
    177 		_rtld_die();
    178 
    179 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    180 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    181 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    182 	if (*where != new_value)
    183 		*where = new_value;
    184 
    185 	return (caddr_t)new_value;
    186 }
    187