Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.4
      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 #ifdef COMBRELOC
     21 	unsigned long lastsym = -1;
     22 #endif
     23 	Elf_Addr target;
     24 
     25 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     26 		Elf_Addr        *where;
     27 		Elf_Addr         tmp;
     28 		unsigned long	 symnum;
     29 
     30 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     31 		symnum = ELF_R_SYM(rel->r_info);
     32 
     33 		switch (ELF_R_TYPE(rel->r_info)) {
     34 		case R_TYPE(NONE):
     35 			break;
     36 
     37 #if 1 /* XXX should not occur */
     38 		case R_TYPE(PC32):
     39 #ifdef COMBRELOC
     40 			if (symnum != lastsym)
     41 #endif
     42 			{
     43 				const Elf_Sym   *def;
     44 				const Obj_Entry *defobj;
     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 #endif
     54 			}
     55 
     56 			*where += target - (Elf_Addr)where;
     57 			rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
     58 			    defobj->strtab + def->st_name, obj->path,
     59 			    (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 			{
     70 				const Elf_Sym   *def;
     71 				const Obj_Entry *defobj;
     72 				def = _rtld_find_symdef(symnum, obj, &defobj,
     73 				    false);
     74 				if (def == NULL)
     75 					return -1;
     76 				target = (Elf_Addr)(defobj->relocbase +
     77 				    def->st_value);
     78 #ifdef COMBRELOC
     79 				lastsym = symnum;
     80 #endif
     81 			}
     82 
     83 			tmp = target + *where;
     84 			if (*where != tmp)
     85 				*where = tmp;
     86 			rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
     87 			    defobj->strtab + def->st_name, obj->path,
     88 			    (void *)*where, defobj->path));
     89 			break;
     90 
     91 		case R_TYPE(RELATIVE):
     92 			*where += (Elf_Addr)obj->relocbase;
     93 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
     94 			    (void *)*where));
     95 			break;
     96 
     97 		case R_TYPE(COPY):
     98 			/*
     99 			 * These are deferred until all other relocations have
    100 			 * been done.  All we do here is make sure that the
    101 			 * COPY relocation is not in a shared library.  They
    102 			 * are allowed only in executable files.
    103 			 */
    104 			if (!obj->mainprog) {
    105 				_rtld_error(
    106 			"%s: Unexpected R_COPY relocation in shared library",
    107 				    obj->path);
    108 				return -1;
    109 			}
    110 			rdbg(dodebug, ("COPY (avoid in main)"));
    111 			break;
    112 
    113 		default:
    114 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    115 			    "contents = %p, symbol = %s",
    116 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    117 			    (void *)rel->r_offset, (void *)*where,
    118 			    obj->strtab + obj->symtab[symnum].st_name));
    119 			_rtld_error("%s: Unsupported relocation type %ld "
    120 			    "in non-PLT relocations\n",
    121 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    122 			return -1;
    123 		}
    124 	}
    125 	return 0;
    126 }
    127