Home | History | Annotate | Line # | Download | only in mips
mips_reloc.c revision 1.20
      1 /*	$NetBSD: mips_reloc.c,v 1.20 2002/09/12 18:36:43 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 
     32 #include <stdarg.h>
     33 #include <sys/types.h>
     34 #include <sys/stat.h>
     35 
     36 #include "debug.h"
     37 #include "rtld.h"
     38 
     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 
     52 	def = _rtld_find_symdef(a0, obj, &defobj, true);
     53 	if (def) {
     54 		u[obj->local_gotno + a0 - obj->gotsym] = (Elf_Addr)
     55 		    (def->st_value + defobj->relocbase);
     56 		return((caddr_t)(def->st_value + defobj->relocbase));
     57 	}
     58 
     59 	return(NULL);	/* XXX */
     60 }
     61 
     62 void
     63 _rtld_setup_pltgot(obj)
     64 	const Obj_Entry *obj;
     65 {
     66 	obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
     67 	/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
     68 	obj->pltgot[1] |= (Elf_Addr) obj;
     69 }
     70 
     71 void
     72 _rtld_relocate_nonplt_self(dynp, relocbase)
     73 	Elf_Dyn *dynp;
     74 	Elf_Addr relocbase;
     75 {
     76 	const Elf_Rel *rel = 0, *rellim;
     77 	Elf_Addr relsz = 0;
     78 	Elf_Addr *where;
     79 	const Elf_Sym *symtab, *sym;
     80 	Elf_Addr *got;
     81 	Elf_Word local_gotno, symtabno, gotsym;
     82 	int i;
     83 
     84 	for (; dynp->d_tag != DT_NULL; dynp++) {
     85 		switch (dynp->d_tag) {
     86 		case DT_REL:
     87 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     88 			break;
     89 		case DT_RELSZ:
     90 			relsz = dynp->d_un.d_val;
     91 			break;
     92 		case DT_SYMTAB:
     93 			symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
     94 			break;
     95 		case DT_PLTGOT:
     96 			got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
     97 			break;
     98 		case DT_MIPS_LOCAL_GOTNO:
     99 			local_gotno = dynp->d_un.d_val;
    100 			break;
    101 		case DT_MIPS_SYMTABNO:
    102 			symtabno = dynp->d_un.d_val;
    103 			break;
    104 		case DT_MIPS_GOTSYM:
    105 			gotsym = dynp->d_un.d_val;
    106 			break;
    107 		}
    108 	}
    109 	rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
    110 	for (; rel < rellim; rel++) {
    111 		where = (Elf_Addr *)(relocbase + rel->r_offset);
    112 
    113 		switch (ELF_R_TYPE(rel->r_info)) {
    114 		case R_TYPE(NONE):
    115 			break;
    116 
    117 		case R_TYPE(REL32):
    118 			sym = symtab + ELF_R_SYM(rel->r_info);
    119 			if (ELF_ST_BIND(sym->st_info) == STB_LOCAL &&
    120 			    ELF_ST_TYPE(sym->st_info) == STT_SECTION)
    121 				*where += (Elf_Addr)(sym->st_value + relocbase);
    122 			else
    123 				abort();
    124 			break;
    125 
    126 		default:
    127 			abort();
    128 		}
    129 	}
    130 
    131 	i = (got[1] & 0x80000000) ? 2 : 1;
    132 	/* Relocate the local GOT entries */
    133 	while (i < local_gotno)
    134 		got[i++] += relocbase;
    135 	got += local_gotno;
    136 	sym = symtab + gotsym;
    137 	/* Now do the global GOT entries */
    138 	for (i = gotsym; i < symtabno; i++) {
    139 		if (sym->st_shndx == SHN_UNDEF ||
    140 		    sym->st_shndx == SHN_COMMON)
    141 			*got = sym->st_value + relocbase;
    142 		else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    143 		    *got != sym->st_value)
    144 			*got += relocbase;
    145 		else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
    146 		    ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
    147 			if (sym->st_shndx == SHN_ABS)
    148 				*got = sym->st_value + relocbase;
    149 			/* else SGI stuff ignored */
    150 		} else
    151 			*got = sym->st_value + relocbase;
    152 		++sym;
    153 		++got;
    154 	}
    155 }
    156 
    157 int
    158 _rtld_relocate_nonplt_objects(obj, self, dodebug)
    159 	const Obj_Entry *obj;
    160 	bool self;
    161 	bool dodebug;
    162 {
    163 	const Elf_Rel *rel;
    164 	Elf_Addr *got = obj->pltgot;
    165 	const Elf_Sym *sym, *def;
    166 	const Obj_Entry *defobj;
    167 	int i;
    168 
    169 	if (self)
    170 		return 0;
    171 
    172 	for (rel = obj->rel; rel < obj->rellim; rel++) {
    173 		Elf_Addr        *where;
    174 		unsigned long	 symnum;
    175 
    176 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    177 		symnum = ELF_R_SYM(rel->r_info);
    178 
    179 		switch (ELF_R_TYPE(rel->r_info)) {
    180 		case R_TYPE(NONE):
    181 			break;
    182 
    183 		case R_TYPE(REL32):
    184 			/* 32-bit PC-relative reference */
    185 			def = obj->symtab + symnum;
    186 
    187 			if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
    188 			  (ELF_ST_TYPE(def->st_info) == STT_SECTION ||
    189 			   ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
    190 				/*
    191 				 * XXX: ABI DIFFERENCE!
    192 				 *
    193 				 * Old NetBSD binutils would generate shared
    194 				 * libs with section-relative relocations being
    195 				 * already adjusted for the start address of
    196 				 * the section.
    197 				 *
    198 				 * New binutils, OTOH, generate shared libs
    199 				 * with the same relocations being based at
    200 				 * zero, so we need to add in the start address
    201 				 * of the section.
    202 				 *
    203 				 * We assume that all section-relative relocs
    204 				 * with contents less than the start of the
    205 				 * section need to be adjusted; this should
    206 				 * work with both old and new shlibs.
    207 				 *
    208 				 * --rkb, Oct 6, 2001
    209 				 */
    210 				if (def->st_info == STT_SECTION &&
    211 				    *where < def->st_value)
    212 					*where += (Elf_Addr)def->st_value;
    213 
    214 				*where += (Elf_Addr)obj->relocbase;
    215 
    216 				rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    217 				    obj->strtab + def->st_name, obj->path,
    218 				    (void *)*where, obj->path));
    219 			} else {
    220 				/* XXX maybe do something re: bootstrapping? */
    221 				def = _rtld_find_symdef(symnum, obj, &defobj,
    222 				    false);
    223 				if (def == NULL)
    224 					return -1;
    225 				*where += (Elf_Addr)(defobj->relocbase +
    226 				    def->st_value);
    227 				rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    228 				    obj->strtab + obj->symtab[symnum].st_name,
    229 				    obj->path, (void *)*where, defobj->path));
    230 			}
    231 			break;
    232 
    233 		default:
    234 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    235 			    "contents = %p, symbol = %s",
    236 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    237 			    (void *)rel->r_offset, (void *)*where,
    238 			    obj->strtab + obj->symtab[symnum].st_name));
    239 			_rtld_error("%s: Unsupported relocation type %ld "
    240 			    "in non-PLT relocations\n",
    241 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    242 			return -1;
    243 		}
    244 	}
    245 
    246 	i = (got[1] & 0x80000000) ? 2 : 1;
    247 	/* Relocate the local GOT entries */
    248 	while (i < obj->local_gotno)
    249 		got[i++] += (Elf_Addr)obj->relocbase;
    250 	got += obj->local_gotno;
    251 	sym = obj->symtab + obj->gotsym;
    252 	/* Now do the global GOT entries */
    253 	for (i = obj->gotsym; i < obj->symtabno; i++) {
    254 		rdbg(1, (" doing got %d sym %p (%s, %x)", i - obj->gotsym,
    255 		    sym, sym->st_name + obj->strtab, *got));
    256 
    257 		def = _rtld_find_symdef(i, obj, &defobj, true);
    258 		if (def == NULL)
    259 			return -1;
    260 		if (sym->st_shndx == SHN_UNDEF ||
    261 		    sym->st_shndx == SHN_COMMON)
    262 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
    263 		else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    264 		    *got != sym->st_value)
    265 			*got += (Elf_Addr)obj->relocbase;
    266 		else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
    267 		    ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
    268 			if (sym->st_shndx == SHN_ABS)
    269 				*got = sym->st_value +
    270 				    (Elf_Addr)obj->relocbase;
    271 			/* else SGI stuff ignored */
    272 		} else
    273 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
    274 		++sym;
    275 		++got;
    276 	}
    277 
    278 	return 0;
    279 }
    280 
    281 int
    282 _rtld_relocate_plt_lazy(obj, dodebug)
    283 	const Obj_Entry *obj;
    284 	bool dodebug;
    285 {
    286 	const Elf_Rel *rel;
    287 
    288 	if (!obj->isdynamic)
    289 		return 0;
    290 
    291 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    292 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    293 
    294 		/* Just relocate the GOT slots pointing into the PLT */
    295 		*where += (Elf_Addr)obj->relocbase;
    296 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    297 		    (void *)*where));
    298 	}
    299 
    300 	return 0;
    301 }
    302 
    303 int
    304 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
    305 	const Obj_Entry *obj;
    306 	const Elf_Rela *rela;
    307 	caddr_t *addrp;
    308 	bool dodebug;
    309 {
    310 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    311 	Elf_Addr new_value;
    312 
    313 	/* Fully resolve procedure addresses now */
    314 
    315 	if (obj->isdynamic) {
    316 		/* Just relocate the GOT slots pointing into the PLT */
    317 		new_value = *where + (Elf_Addr)(obj->relocbase);
    318 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    319 		    (void *)*where));
    320 	} else {
    321 		return 0;
    322 	}
    323 	if (*where != new_value)
    324 		*where = new_value;
    325 
    326 	*addrp = (caddr_t)new_value;
    327 	return 0;
    328 }
    329