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