Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.16
      1 /*	$NetBSD: mdreloc.c,v 1.16 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_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, self)
     48 	const Obj_Entry *obj;
     49 	bool self;
     50 {
     51 	const Elf_Rel *rel;
     52 #define COMBRELOC
     53 #ifdef COMBRELOC
     54 	unsigned long lastsym = -1;
     55 #endif
     56 	Elf_Addr target;
     57 
     58 	if (self)
     59 		return 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(obj)
    162 	const Obj_Entry *obj;
    163 {
    164 	const Elf_Rel *rel;
    165 
    166 	if (!obj->isdynamic)
    167 		return 0;
    168 
    169 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    170 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    171 
    172 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    173 
    174 		/* Just relocate the GOT slots pointing into the PLT */
    175 		*where += (Elf_Addr)obj->relocbase;
    176 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    177 	}
    178 
    179 	return 0;
    180 }
    181 
    182 caddr_t
    183 _rtld_bind(obj, reloff)
    184 	const Obj_Entry *obj;
    185 	Elf_Word reloff;
    186 {
    187 	const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
    188 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    189 	Elf_Addr new_value;
    190 	const Elf_Sym  *def;
    191 	const Obj_Entry *defobj;
    192 
    193 	assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    194 
    195 	def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
    196 	if (def == NULL)
    197 		_rtld_die();
    198 
    199 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    200 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    201 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    202 	if (*where != new_value)
    203 		*where = new_value;
    204 
    205 	return (caddr_t)new_value;
    206 }
    207 
    208 int
    209 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    210 {
    211 	const Elf_Rel *rel;
    212 
    213 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    214 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    215 		Elf_Addr target;
    216 		const Elf_Sym *def;
    217 		const Obj_Entry *defobj;
    218 
    219 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    220 
    221 		def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
    222 		    true);
    223 		if (def == NULL)
    224 			return -1;
    225 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
    226 		rdbg(("bind now/fixup in %s --> old=%p new=%p",
    227 		    defobj->strtab + def->st_name, (void *)*where,
    228 		    (void *)target));
    229 		if (*where != target)
    230 			*where = target;
    231 	}
    232 	return 0;
    233 }
    234