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