Home | History | Annotate | Line # | Download | only in mips
mips_reloc.c revision 1.35
      1 /*	$NetBSD: mips_reloc.c,v 1.35 2002/09/25 03:57:15 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
      5  * Portions copyright 2002 Charles M. Hannum.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/types.h>
     32 #include <sys/stat.h>
     33 #include <string.h>
     34 
     35 #include "debug.h"
     36 #include "rtld.h"
     37 
     38 void _rtld_bind_start(void);
     39 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     40 caddr_t _rtld_bind_mips(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
     41 
     42 caddr_t
     43 _rtld_bind_mips(a0, a1, a2, a3)
     44 	Elf_Word a0;
     45 	Elf_Addr a1, a2, a3;
     46 {
     47 	Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
     48 	const Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
     49 	const Elf_Sym *def;
     50 	const Obj_Entry *defobj;
     51 	Elf_Addr new_value;
     52 
     53 	def = _rtld_find_symdef(a0, obj, &defobj, true);
     54 	if (def == NULL)
     55 		_rtld_die();
     56 
     57 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
     58 	rdbg(("bind now/fixup in %s --> new=%p",
     59 	    defobj->strtab + def->st_name, (void *)new_value));
     60 	u[obj->local_gotno + a0 - obj->gotsym] = new_value;
     61 	return ((caddr_t)new_value);
     62 }
     63 
     64 void
     65 _rtld_setup_pltgot(obj)
     66 	const Obj_Entry *obj;
     67 {
     68 	obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
     69 	/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
     70 	obj->pltgot[1] |= (Elf_Addr) obj;
     71 }
     72 
     73 void
     74 _rtld_relocate_nonplt_self(dynp, relocbase)
     75 	Elf_Dyn *dynp;
     76 	Elf_Addr relocbase;
     77 {
     78 	const Elf_Rel *rel = 0, *rellim;
     79 	Elf_Addr relsz = 0;
     80 	Elf_Addr *where;
     81 	const Elf_Sym *symtab, *sym;
     82 	Elf_Addr *got;
     83 	Elf_Word local_gotno, symtabno, gotsym;
     84 	int i;
     85 
     86 	for (; dynp->d_tag != DT_NULL; dynp++) {
     87 		switch (dynp->d_tag) {
     88 		case DT_REL:
     89 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     90 			break;
     91 		case DT_RELSZ:
     92 			relsz = dynp->d_un.d_val;
     93 			break;
     94 		case DT_SYMTAB:
     95 			symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
     96 			break;
     97 		case DT_PLTGOT:
     98 			got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
     99 			break;
    100 		case DT_MIPS_LOCAL_GOTNO:
    101 			local_gotno = dynp->d_un.d_val;
    102 			break;
    103 		case DT_MIPS_SYMTABNO:
    104 			symtabno = dynp->d_un.d_val;
    105 			break;
    106 		case DT_MIPS_GOTSYM:
    107 			gotsym = dynp->d_un.d_val;
    108 			break;
    109 		}
    110 	}
    111 
    112 	i = (got[1] & 0x80000000) ? 2 : 1;
    113 	/* Relocate the local GOT entries */
    114 	got += i;
    115 	for (; i < local_gotno; i++)
    116 		*got++ += relocbase;
    117 	sym = symtab + gotsym;
    118 	/* Now do the global GOT entries */
    119 	for (i = gotsym; i < symtabno; i++) {
    120 		*got = sym->st_value + relocbase;
    121 		++sym;
    122 		++got;
    123 	}
    124 
    125 	rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
    126 	for (; rel < rellim; rel++) {
    127 		where = (Elf_Addr *)(relocbase + rel->r_offset);
    128 
    129 		switch (ELF_R_TYPE(rel->r_info)) {
    130 		case R_TYPE(NONE):
    131 			break;
    132 
    133 		case R_TYPE(REL32):
    134 			assert(ELF_R_SYM(rel->r_info) < gotsym);
    135 			sym = symtab + ELF_R_SYM(rel->r_info);
    136 			assert(sym->st_info ==
    137 			    ELF_ST_INFO(STB_LOCAL, STT_SECTION));
    138 			*where += (Elf_Addr)(sym->st_value + relocbase);
    139 			break;
    140 
    141 		default:
    142 			abort();
    143 		}
    144 	}
    145 }
    146 
    147 /*
    148  * It is possible for the compiler to emit relocations for unaligned data.
    149  * We handle this situation with these inlines.
    150  */
    151 #define	RELOC_ALIGNED_P(x) \
    152 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
    153 
    154 static __inline Elf_Addr
    155 load_ptr(void *where)
    156 {
    157 	Elf_Addr res;
    158 
    159 	memcpy(&res, where, sizeof(res));
    160 
    161 	return (res);
    162 }
    163 
    164 static __inline void
    165 store_ptr(void *where, Elf_Addr val)
    166 {
    167 
    168 	memcpy(where, &val, sizeof(val));
    169 }
    170 
    171 int
    172 _rtld_relocate_nonplt_objects(obj, self)
    173 	const Obj_Entry *obj;
    174 	bool self;
    175 {
    176 	const Elf_Rel *rel;
    177 	Elf_Addr *got = obj->pltgot;
    178 	const Elf_Sym *sym, *def;
    179 	const Obj_Entry *defobj;
    180 	int i;
    181 
    182 	if (self)
    183 		return 0;
    184 
    185 	i = (got[1] & 0x80000000) ? 2 : 1;
    186 	/* Relocate the local GOT entries */
    187 	got += i;
    188 	for (; i < obj->local_gotno; i++)
    189 		*got++ += (Elf_Addr)obj->relocbase;
    190 	sym = obj->symtab + obj->gotsym;
    191 	/* Now do the global GOT entries */
    192 	for (i = obj->gotsym; i < obj->symtabno; i++) {
    193 		rdbg((" doing got %d sym %p (%s, %x)", i - obj->gotsym, sym,
    194 		    sym->st_name + obj->strtab, *got));
    195 
    196 		if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    197 		    sym->st_shndx == SHN_UNDEF) {
    198 			/*
    199 			 * XXX DANGER WILL ROBINSON!
    200 			 * You might think this is stupid, as it intentionally
    201 			 * defeats lazy binding -- and you'd be right.
    202 			 * Unfortunately, for lazy binding to work right, we
    203 			 * need to a way to force the GOT slots used for
    204 			 * function pointers to be resolved immediately.  This
    205 			 * is supposed to be done automatically by the linker,
    206 			 * by not outputting a PLT slot and setting st_value
    207 			 * to 0, but GNU ld does not do so reliably.
    208 			 */
    209 			def = _rtld_find_symdef(i, obj, &defobj, true);
    210 			if (def == NULL)
    211 				return -1;
    212 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
    213 		} else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    214 		    sym->st_value != 0) {
    215 			/*
    216 			 * If there are non-PLT references to the function,
    217 			 * st_value should be 0, forcing us to resolve the
    218 			 * address immediately.
    219 			 */
    220 			*got = sym->st_value + (Elf_Addr)obj->relocbase;
    221 		} else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
    222 			/* Symbols with index SHN_ABS are not relocated. */
    223 			if (sym->st_shndx != SHN_ABS)
    224 				*got = sym->st_value +
    225 				    (Elf_Addr)obj->relocbase;
    226 		} else {
    227 			def = _rtld_find_symdef(i, obj, &defobj, true);
    228 			if (def == NULL)
    229 				return -1;
    230 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
    231 		}
    232 
    233 		rdbg(("  --> now %x", *got));
    234 		++sym;
    235 		++got;
    236 	}
    237 
    238 	got = obj->pltgot;
    239 	for (rel = obj->rel; rel < obj->rellim; rel++) {
    240 		Elf_Addr        *where, tmp;
    241 		unsigned long	 symnum;
    242 
    243 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    244 		symnum = ELF_R_SYM(rel->r_info);
    245 
    246 		switch (ELF_R_TYPE(rel->r_info)) {
    247 		case R_TYPE(NONE):
    248 			break;
    249 
    250 		case R_TYPE(REL32):
    251 			/* 32-bit PC-relative reference */
    252 			def = obj->symtab + symnum;
    253 
    254 			if (symnum >= obj->gotsym) {
    255 				tmp = *where;
    256 				tmp += got[obj->local_gotno + symnum - obj->gotsym];
    257 				*where = tmp;
    258 
    259 				rdbg(("REL32/G %s in %s --> %p in %s",
    260 				    obj->strtab + def->st_name, obj->path,
    261 				    (void *)tmp, obj->path));
    262 				break;
    263 			} else {
    264 				/*
    265 				 * XXX: ABI DIFFERENCE!
    266 				 *
    267 				 * Old NetBSD binutils would generate shared
    268 				 * libs with section-relative relocations being
    269 				 * already adjusted for the start address of
    270 				 * the section.
    271 				 *
    272 				 * New binutils, OTOH, generate shared libs
    273 				 * with the same relocations being based at
    274 				 * zero, so we need to add in the start address
    275 				 * of the section.
    276 				 *
    277 				 * We assume that all section-relative relocs
    278 				 * with contents less than the start of the
    279 				 * section need to be adjusted; this should
    280 				 * work with both old and new shlibs.
    281 				 *
    282 				 * --rkb, Oct 6, 2001
    283 				 */
    284 				tmp = *where;
    285 
    286 				if (def->st_info ==
    287 				    ELF_ST_INFO(STB_LOCAL, STT_SECTION) &&
    288 				    tmp < def->st_value)
    289 					tmp += (Elf_Addr)def->st_value;
    290 
    291 				tmp += (Elf_Addr)obj->relocbase;
    292 				*where = tmp;
    293 
    294 				rdbg(("REL32/L %s in %s --> %p in %s",
    295 				    obj->strtab + def->st_name, obj->path,
    296 				    (void *)tmp, obj->path));
    297 			}
    298 			break;
    299 
    300 		default:
    301 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    302 			    "contents = %p, symbol = %s",
    303 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    304 			    (void *)rel->r_offset, (void *)load_ptr(where),
    305 			    obj->strtab + obj->symtab[symnum].st_name));
    306 			_rtld_error("%s: Unsupported relocation type %ld "
    307 			    "in non-PLT relocations\n",
    308 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    309 			return -1;
    310 		}
    311 	}
    312 
    313 	return 0;
    314 }
    315 
    316 int
    317 _rtld_relocate_plt_lazy(obj)
    318 	const Obj_Entry *obj;
    319 {
    320 	/* PLT fixups were done above in the GOT relocation. */
    321 	return 0;
    322 }
    323