Home | History | Annotate | Line # | Download | only in arm
mdreloc.c revision 1.14
      1 /*	$NetBSD: mdreloc.c,v 1.14 2002/09/12 22:56:29 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)
     47 	const Obj_Entry *obj;
     48 	bool self;
     49 {
     50 	const Elf_Rel *rel;
     51 
     52 	if (self)
     53 		return 0;
     54 
     55 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     56 		Elf_Addr        *where;
     57 		const Elf_Sym   *def;
     58 		const Obj_Entry *defobj;
     59 		Elf_Addr         tmp;
     60 		unsigned long	 symnum;
     61 
     62 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     63 		symnum = ELF_R_SYM(rel->r_info);
     64 
     65 		switch (ELF_R_TYPE(rel->r_info)) {
     66 		case R_TYPE(NONE):
     67 			break;
     68 
     69 #if 1 /* XXX should not occur */
     70 		case R_TYPE(PC24): {	/* word32 S - P + A */
     71 			Elf32_Sword addend;
     72 
     73 			/*
     74 			 * Extract addend and sign-extend if needed.
     75 			 */
     76 			addend = *where;
     77 			if (addend & 0x00800000)
     78 				addend |= 0xff000000;
     79 
     80 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     81 			if (def == NULL)
     82 				return -1;
     83 			tmp = (Elf_Addr)obj->relocbase + def->st_value
     84 			    - (Elf_Addr)where + (addend << 2);
     85 			if ((tmp & 0xfe000000) != 0xfe000000 &&
     86 			    (tmp & 0xfe000000) != 0) {
     87 				_rtld_error(
     88 				"%s: R_ARM_PC24 relocation @ %p to %s failed "
     89 				"(displacement %ld (%#lx) out of range)",
     90 				    obj->path, where,
     91 				    obj->strtab + obj->symtab[symnum].st_name,
     92 				    (long) tmp, (long) tmp);
     93 				return -1;
     94 			}
     95 			tmp >>= 2;
     96 			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
     97 			rdbg(("PC24 %s in %s --> %p @ %p in %s",
     98 			    obj->strtab + obj->symtab[symnum].st_name,
     99 			    obj->path, (void *)*where, where, defobj->path));
    100 			break;
    101 		}
    102 #endif
    103 
    104 		case R_TYPE(ABS32):	/* word32 B + S + A */
    105 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    106 			if (def == NULL)
    107 				return -1;
    108 			*where += (Elf_Addr)defobj->relocbase + def->st_value;
    109 			rdbg(("ABS32 %s in %s --> %p @ %p in %s",
    110 			    obj->strtab + obj->symtab[symnum].st_name,
    111 			    obj->path, (void *)*where, where, defobj->path));
    112 			break;
    113 
    114 		case R_TYPE(GLOB_DAT):	/* word32 B + S */
    115 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    116 			if (def == NULL)
    117 				return -1;
    118 			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
    119 			rdbg(("GLOB_DAT %s in %s --> %p @ %p in %s",
    120 			    obj->strtab + obj->symtab[symnum].st_name,
    121 			    obj->path, (void *)*where, where, defobj->path));
    122 			break;
    123 
    124 		case R_TYPE(RELATIVE):	/* word32 B + A */
    125 			*where += (Elf_Addr)obj->relocbase;
    126 			rdbg(("RELATIVE in %s --> %p", obj->path,
    127 			    (void *)*where));
    128 			break;
    129 
    130 		case R_TYPE(COPY):
    131 			/*
    132 			 * These are deferred until all other relocations have
    133 			 * been done.  All we do here is make sure that the
    134 			 * COPY relocation is not in a shared library.  They
    135 			 * are allowed only in executable files.
    136 			 */
    137 			if (obj->isdynamic) {
    138 				_rtld_error(
    139 			"%s: Unexpected R_COPY relocation in shared library",
    140 				    obj->path);
    141 				return -1;
    142 			}
    143 			rdbg(("COPY (avoid in main)"));
    144 			break;
    145 
    146 		default:
    147 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    148 			    "contents = %p, symbol = %s",
    149 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    150 			    (void *)rel->r_offset, (void *)*where,
    151 			    obj->strtab + obj->symtab[symnum].st_name));
    152 			_rtld_error("%s: Unsupported relocation type %ld "
    153 			    "in non-PLT relocations\n",
    154 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    155 			return -1;
    156 		}
    157 	}
    158 	return 0;
    159 }
    160 
    161 int
    162 _rtld_relocate_plt_lazy(obj)
    163 	const Obj_Entry *obj;
    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(JUMP_SLOT));
    174 
    175 		/* Just relocate the GOT slots pointing into the PLT */
    176 		*where += (Elf_Addr)obj->relocbase;
    177 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    178 	}
    179 
    180 	return 0;
    181 }
    182 
    183 int
    184 _rtld_relocate_plt_object(obj, rela, addrp)
    185 	const Obj_Entry *obj;
    186 	const Elf_Rela *rela;
    187 	caddr_t *addrp;
    188 {
    189 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    190 	Elf_Addr new_value;
    191 	const Elf_Sym  *def;
    192 	const Obj_Entry *defobj;
    193 
    194 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    195 
    196 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    197 	if (def == NULL)
    198 		return -1;
    199 
    200 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    201 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    202 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    203 	if (*where != new_value)
    204 		*where = new_value;
    205 
    206 	*addrp = (caddr_t)new_value;
    207 	return 0;
    208 }
    209