Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.6
      1 #include <sys/types.h>
      2 #include <sys/stat.h>
      3 
      4 #include "debug.h"
      5 #include "rtld.h"
      6 
      7 void
      8 _rtld_setup_pltgot(const Obj_Entry *obj)
      9 {
     10 	obj->pltgot[1] = (Elf_Addr) obj;
     11 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     12 }
     13 
     14 int
     15 _rtld_relocate_nonplt_objects(obj, dodebug)
     16 	Obj_Entry *obj;
     17 	bool dodebug;
     18 {
     19 	const Elf_Rel *rel;
     20 #define COMBRELOC
     21 #ifdef COMBRELOC
     22 	unsigned long lastsym = -1;
     23 #endif
     24 	Elf_Addr target;
     25 
     26 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     27 		Elf_Addr        *where;
     28 		const Elf_Sym   *def;
     29 		const Obj_Entry *defobj;
     30 		Elf_Addr         tmp;
     31 		unsigned long	 symnum;
     32 
     33 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     34 		symnum = ELF_R_SYM(rel->r_info);
     35 
     36 		switch (ELF_R_TYPE(rel->r_info)) {
     37 		case R_TYPE(NONE):
     38 			break;
     39 
     40 #if 1 /* XXX should not occur */
     41 		case R_TYPE(PC32):
     42 #ifdef COMBRELOC
     43 			if (symnum != lastsym) {
     44 #endif
     45 				def = _rtld_find_symdef(symnum, obj, &defobj,
     46 				    false);
     47 				if (def == NULL)
     48 					return -1;
     49 				target = (Elf_Addr)(defobj->relocbase +
     50 				    def->st_value);
     51 #ifdef COMBRELOC
     52 				lastsym = symnum;
     53 			}
     54 #endif
     55 
     56 			*where += target - (Elf_Addr)where;
     57 			rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
     58 			    obj->strtab + obj->symtab[symnum].st_name,
     59 			    obj->path, (void *)*where, defobj->path));
     60 			break;
     61 
     62 		case R_TYPE(GOT32):
     63 #endif
     64 		case R_TYPE(32):
     65 		case R_TYPE(GLOB_DAT):
     66 #ifdef COMBRELOC
     67 			if (symnum != lastsym) {
     68 #endif
     69 				def = _rtld_find_symdef(symnum, obj, &defobj,
     70 				    false);
     71 				if (def == NULL)
     72 					return -1;
     73 				target = (Elf_Addr)(defobj->relocbase +
     74 				    def->st_value);
     75 #ifdef COMBRELOC
     76 				lastsym = symnum;
     77 			}
     78 #endif
     79 
     80 			tmp = target + *where;
     81 			if (*where != tmp)
     82 				*where = tmp;
     83 			rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
     84 			    obj->strtab + obj->symtab[symnum].st_name,
     85 			    obj->path, (void *)*where, defobj->path));
     86 			break;
     87 
     88 		case R_TYPE(RELATIVE):
     89 			*where += (Elf_Addr)obj->relocbase;
     90 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
     91 			    (void *)*where));
     92 			break;
     93 
     94 		case R_TYPE(COPY):
     95 			/*
     96 			 * These are deferred until all other relocations have
     97 			 * been done.  All we do here is make sure that the
     98 			 * COPY relocation is not in a shared library.  They
     99 			 * are allowed only in executable files.
    100 			 */
    101 			if (!obj->mainprog) {
    102 				_rtld_error(
    103 			"%s: Unexpected R_COPY relocation in shared library",
    104 				    obj->path);
    105 				return -1;
    106 			}
    107 			rdbg(dodebug, ("COPY (avoid in main)"));
    108 			break;
    109 
    110 		default:
    111 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    112 			    "contents = %p, symbol = %s",
    113 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    114 			    (void *)rel->r_offset, (void *)*where,
    115 			    obj->strtab + obj->symtab[symnum].st_name));
    116 			_rtld_error("%s: Unsupported relocation type %ld "
    117 			    "in non-PLT relocations\n",
    118 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    119 			return -1;
    120 		}
    121 	}
    122 	return 0;
    123 }
    124 
    125 int
    126 _rtld_relocate_plt_lazy(obj, dodebug)
    127 	Obj_Entry *obj;
    128 	bool dodebug;
    129 {
    130 	const Elf_Rel *rel;
    131 
    132 	if (obj->mainprog)
    133 		return 0;
    134 
    135 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    136 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    137 
    138 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    139 
    140 		/* Just relocate the GOT slots pointing into the PLT */
    141 		*where += (Elf_Addr)obj->relocbase;
    142 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    143 		    (void *)*where));
    144 	}
    145 
    146 	return 0;
    147 }
    148 
    149 int
    150 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
    151 	Obj_Entry *obj;
    152 	const Elf_Rela *rela;
    153 	caddr_t *addrp;
    154 	bool dodebug;
    155 {
    156 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    157 	Elf_Addr new_value;
    158 	const Elf_Sym  *def;
    159 	const Obj_Entry *defobj;
    160 
    161 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    162 
    163 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    164 	if (def == NULL)
    165 		return -1;
    166 
    167 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    168 	rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
    169 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    170 	if (*where != new_value)
    171 		*where = new_value;
    172 
    173 	*addrp = (caddr_t)new_value;
    174 	return 0;
    175 }
    176