Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.13
      1 /*	$NetBSD: mdreloc.c,v 1.13 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_Rel *rel = 0, *rellim;
     25 	Elf_Addr relsz = 0;
     26 	Elf_Addr *where;
     27 
     28 	for (; dynp->d_tag != DT_NULL; dynp++) {
     29 		switch (dynp->d_tag) {
     30 		case DT_REL:
     31 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     32 			break;
     33 		case DT_RELSZ:
     34 			relsz = dynp->d_un.d_val;
     35 			break;
     36 		}
     37 	}
     38 	rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
     39 	for (; rel < rellim; rel++) {
     40 		where = (Elf_Addr *)(relocbase + rel->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_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(dodebug, ("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(dodebug, ("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(dodebug, ("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(dodebug, ("COPY (avoid in main)"));
    143 			break;
    144 
    145 		default:
    146 			rdbg(dodebug, ("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, dodebug)
    162 	const Obj_Entry *obj;
    163 	bool dodebug;
    164 {
    165 	const Elf_Rel *rel;
    166 
    167 	if (!obj->isdynamic)
    168 		return 0;
    169 
    170 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    171 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    172 
    173 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    174 
    175 		/* Just relocate the GOT slots pointing into the PLT */
    176 		*where += (Elf_Addr)obj->relocbase;
    177 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    178 		    (void *)*where));
    179 	}
    180 
    181 	return 0;
    182 }
    183 
    184 int
    185 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
    186 	const Obj_Entry *obj;
    187 	const Elf_Rela *rela;
    188 	caddr_t *addrp;
    189 	bool dodebug;
    190 {
    191 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    192 	Elf_Addr new_value;
    193 	const Elf_Sym  *def;
    194 	const Obj_Entry *defobj;
    195 
    196 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    197 
    198 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    199 	if (def == NULL)
    200 		return -1;
    201 
    202 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    203 	rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
    204 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    205 	if (*where != new_value)
    206 		*where = new_value;
    207 
    208 	*addrp = (caddr_t)new_value;
    209 	return 0;
    210 }
    211