Home | History | Annotate | Line # | Download | only in arm
mdreloc.c revision 1.9
      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, self, dodebug)
     16 	const Obj_Entry *obj;
     17 	bool self;
     18 	bool dodebug;
     19 {
     20 	const Elf_Rel *rel;
     21 
     22 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     23 		Elf_Addr        *where;
     24 		const Elf_Sym   *def;
     25 		const Obj_Entry *defobj;
     26 		Elf_Addr         tmp;
     27 		unsigned long	 symnum;
     28 
     29 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     30 		symnum = ELF_R_SYM(rel->r_info);
     31 
     32 		switch (ELF_R_TYPE(rel->r_info)) {
     33 		case R_TYPE(NONE):
     34 			break;
     35 
     36 #if 1 /* XXX should not occur */
     37 		case R_TYPE(PC24): {	/* word32 S - P + A */
     38 			Elf32_Sword addend;
     39 
     40 			/*
     41 			 * Extract addend and sign-extend if needed.
     42 			 */
     43 			addend = *where;
     44 			if (addend & 0x00800000)
     45 				addend |= 0xff000000;
     46 
     47 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     48 			if (def == NULL)
     49 				return -1;
     50 			tmp = (Elf_Addr)obj->relocbase + def->st_value
     51 			    - (Elf_Addr)where + (addend << 2);
     52 			if ((tmp & 0xfe000000) != 0xfe000000 &&
     53 			    (tmp & 0xfe000000) != 0) {
     54 				_rtld_error(
     55 				"%s: R_ARM_PC24 relocation @ %p to %s failed "
     56 				"(displacement %ld (%#lx) out of range)",
     57 				    obj->path, where,
     58 				    obj->strtab + obj->symtab[symnum].st_name,
     59 				    (long) tmp, (long) tmp);
     60 				return -1;
     61 			}
     62 			tmp >>= 2;
     63 			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
     64 			rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
     65 			    obj->strtab + obj->symtab[symnum].st_name,
     66 			    obj->path, (void *)*where, where, defobj->path));
     67 			break;
     68 		}
     69 #endif
     70 
     71 		case R_TYPE(ABS32):	/* word32 B + S + A */
     72 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     73 			if (def == NULL)
     74 				return -1;
     75 			*where += (Elf_Addr)defobj->relocbase + def->st_value;
     76 			rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
     77 			    obj->strtab + obj->symtab[symnum].st_name,
     78 			    obj->path, (void *)*where, where, defobj->path));
     79 			break;
     80 
     81 		case R_TYPE(GLOB_DAT):	/* word32 B + S */
     82 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     83 			if (def == NULL)
     84 				return -1;
     85 			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
     86 			rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
     87 			    obj->strtab + obj->symtab[symnum].st_name,
     88 			    obj->path, (void *)*where, where, defobj->path));
     89 			break;
     90 
     91 		case R_TYPE(RELATIVE):	/* word32 B + A */
     92 			*where += (Elf_Addr)obj->relocbase;
     93 			rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
     94 			    (void *)*where, 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->isdynamic) {
    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 
    128 int
    129 _rtld_relocate_plt_lazy(obj, dodebug)
    130 	const Obj_Entry *obj;
    131 	bool dodebug;
    132 {
    133 	const Elf_Rel *rel;
    134 
    135 	if (!obj->isdynamic)
    136 		return 0;
    137 
    138 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    139 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    140 
    141 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    142 
    143 		/* Just relocate the GOT slots pointing into the PLT */
    144 		*where += (Elf_Addr)obj->relocbase;
    145 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    146 		    (void *)*where));
    147 	}
    148 
    149 	return 0;
    150 }
    151 
    152 int
    153 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
    154 	const Obj_Entry *obj;
    155 	const Elf_Rela *rela;
    156 	caddr_t *addrp;
    157 	bool dodebug;
    158 {
    159 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    160 	Elf_Addr new_value;
    161 	const Elf_Sym  *def;
    162 	const Obj_Entry *defobj;
    163 
    164 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    165 
    166 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    167 	if (def == NULL)
    168 		return -1;
    169 
    170 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    171 	rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
    172 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    173 	if (*where != new_value)
    174 		*where = new_value;
    175 
    176 	*addrp = (caddr_t)new_value;
    177 	return 0;
    178 }
    179