Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.17
      1 /*	$NetBSD: mdreloc.c,v 1.17 2002/09/26 20:42:11 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 int
     47 _rtld_relocate_nonplt_objects(obj)
     48 	const Obj_Entry *obj;
     49 {
     50 	const Elf_Rel *rel;
     51 #define COMBRELOC
     52 #ifdef COMBRELOC
     53 	unsigned long lastsym = -1;
     54 #endif
     55 	Elf_Addr target;
     56 
     57 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     58 		Elf_Addr        *where;
     59 		const Elf_Sym   *def;
     60 		const Obj_Entry *defobj;
     61 		Elf_Addr         tmp;
     62 		unsigned long	 symnum;
     63 
     64 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     65 		symnum = ELF_R_SYM(rel->r_info);
     66 
     67 		switch (ELF_R_TYPE(rel->r_info)) {
     68 		case R_TYPE(NONE):
     69 			break;
     70 
     71 #if 1 /* XXX should not occur */
     72 		case R_TYPE(PC32):
     73 #ifdef COMBRELOC
     74 			if (symnum != lastsym) {
     75 #endif
     76 				def = _rtld_find_symdef(symnum, obj, &defobj,
     77 				    false);
     78 				if (def == NULL)
     79 					return -1;
     80 				target = (Elf_Addr)(defobj->relocbase +
     81 				    def->st_value);
     82 #ifdef COMBRELOC
     83 				lastsym = symnum;
     84 			}
     85 #endif
     86 
     87 			*where += target - (Elf_Addr)where;
     88 			rdbg(("PC32 %s in %s --> %p in %s",
     89 			    obj->strtab + obj->symtab[symnum].st_name,
     90 			    obj->path, (void *)*where, defobj->path));
     91 			break;
     92 
     93 		case R_TYPE(GOT32):
     94 #endif
     95 		case R_TYPE(32):
     96 		case R_TYPE(GLOB_DAT):
     97 #ifdef COMBRELOC
     98 			if (symnum != lastsym) {
     99 #endif
    100 				def = _rtld_find_symdef(symnum, obj, &defobj,
    101 				    false);
    102 				if (def == NULL)
    103 					return -1;
    104 				target = (Elf_Addr)(defobj->relocbase +
    105 				    def->st_value);
    106 #ifdef COMBRELOC
    107 				lastsym = symnum;
    108 			}
    109 #endif
    110 
    111 			tmp = target + *where;
    112 			if (*where != tmp)
    113 				*where = tmp;
    114 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    115 			    obj->strtab + obj->symtab[symnum].st_name,
    116 			    obj->path, (void *)*where, defobj->path));
    117 			break;
    118 
    119 		case R_TYPE(RELATIVE):
    120 			*where += (Elf_Addr)obj->relocbase;
    121 			rdbg(("RELATIVE in %s --> %p", obj->path,
    122 			    (void *)*where));
    123 			break;
    124 
    125 		case R_TYPE(COPY):
    126 			/*
    127 			 * These are deferred until all other relocations have
    128 			 * been done.  All we do here is make sure that the
    129 			 * COPY relocation is not in a shared library.  They
    130 			 * are allowed only in executable files.
    131 			 */
    132 			if (obj->isdynamic) {
    133 				_rtld_error(
    134 			"%s: Unexpected R_COPY relocation in shared library",
    135 				    obj->path);
    136 				return -1;
    137 			}
    138 			rdbg(("COPY (avoid in main)"));
    139 			break;
    140 
    141 		default:
    142 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    143 			    "contents = %p, symbol = %s",
    144 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    145 			    (void *)rel->r_offset, (void *)*where,
    146 			    obj->strtab + obj->symtab[symnum].st_name));
    147 			_rtld_error("%s: Unsupported relocation type %ld "
    148 			    "in non-PLT relocations\n",
    149 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    150 			return -1;
    151 		}
    152 	}
    153 	return 0;
    154 }
    155 
    156 int
    157 _rtld_relocate_plt_lazy(obj)
    158 	const Obj_Entry *obj;
    159 {
    160 	const Elf_Rel *rel;
    161 
    162 	if (!obj->isdynamic)
    163 		return 0;
    164 
    165 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    166 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    167 
    168 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    169 
    170 		/* Just relocate the GOT slots pointing into the PLT */
    171 		*where += (Elf_Addr)obj->relocbase;
    172 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    173 	}
    174 
    175 	return 0;
    176 }
    177 
    178 caddr_t
    179 _rtld_bind(obj, reloff)
    180 	const Obj_Entry *obj;
    181 	Elf_Word reloff;
    182 {
    183 	const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
    184 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    185 	Elf_Addr new_value;
    186 	const Elf_Sym  *def;
    187 	const Obj_Entry *defobj;
    188 
    189 	assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    190 
    191 	def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
    192 	if (def == NULL)
    193 		_rtld_die();
    194 
    195 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    196 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    197 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    198 	if (*where != new_value)
    199 		*where = new_value;
    200 
    201 	return (caddr_t)new_value;
    202 }
    203 
    204 int
    205 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    206 {
    207 	const Elf_Rel *rel;
    208 
    209 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    210 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    211 		Elf_Addr target;
    212 		const Elf_Sym *def;
    213 		const Obj_Entry *defobj;
    214 
    215 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    216 
    217 		def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
    218 		    true);
    219 		if (def == NULL)
    220 			return -1;
    221 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
    222 		rdbg(("bind now/fixup in %s --> old=%p new=%p",
    223 		    defobj->strtab + def->st_name, (void *)*where,
    224 		    (void *)target));
    225 		if (*where != target)
    226 			*where = target;
    227 	}
    228 	return 0;
    229 }
    230