Home | History | Annotate | Line # | Download | only in mips
mips_reloc.c revision 1.12
      1 /*	$NetBSD: mips_reloc.c,v 1.12 2002/09/06 03:05:36 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 
     31 #include <stdarg.h>
     32 #include <sys/types.h>
     33 #include <sys/stat.h>
     34 
     35 #include "debug.h"
     36 #include "rtld.h"
     37 
     38 /*
     39  * _rtld_bind_mips(symbol_index, return_address, old_gp, stub_return_addr)
     40  */
     41 caddr_t _rtld_bind_mips(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
     42 
     43 caddr_t
     44 _rtld_bind_mips(a0, a1, a2, a3)
     45 	Elf_Word a0;
     46 	Elf_Addr a1, a2, a3;
     47 {
     48 	Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
     49 	Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
     50 	const Elf_Sym *def;
     51 	const Obj_Entry *defobj;
     52 
     53 	def = _rtld_find_symdef(a0, obj, &defobj, true);
     54 	if (def) {
     55 		u[obj->local_gotno + a0 - obj->gotsym] = (Elf_Addr)
     56 		    (def->st_value + defobj->relocbase);
     57 		return((caddr_t)(def->st_value + defobj->relocbase));
     58 	}
     59 
     60 	return(NULL);	/* XXX */
     61 }
     62 
     63 void
     64 _rtld_setup_pltgot(obj)
     65 	const Obj_Entry *obj;
     66 {
     67 	Elf_Addr *got = obj->pltgot;
     68 	const Elf_Sym *sym = obj->symtab;
     69 	const Elf_Sym *def;
     70 	const Obj_Entry *defobj;
     71 	int i;
     72 
     73 	i = (got[1] & 0x80000000) ? 2 : 1;
     74 	/* Relocate the local GOT entries */
     75 	while (i < obj->local_gotno)
     76 		got[i++] += (Elf_Word)obj->relocbase;
     77 	got += obj->local_gotno;
     78 	i = obj->symtabno - obj->gotsym;
     79 	sym += obj->gotsym;
     80 	/* Now do the global GOT entries */
     81 	while (i--) {
     82 		rdbg(1, (" doing got %d sym %p (%s, %x)",
     83 				obj->symtabno - obj->gotsym - i - 1,
     84 				sym, sym->st_name + obj->strtab, *got));
     85 
     86 		def = _rtld_find_symdef(obj->symtabno - i - 1, obj, &defobj,
     87 		    true);
     88 		if (def == NULL)
     89 			_rtld_error(
     90 	    "%s: Undefined PLT symbol \"%s\" (section type = %ld, symnum = %ld)",
     91 			    obj->path, sym->st_name + obj->strtab,
     92 			    (u_long) ELF_ST_TYPE(sym->st_info),
     93 			    (u_long) obj->symtabno - i - 1);
     94 		else {
     95 
     96 			if (sym->st_shndx == SHN_UNDEF) {
     97 #if 0	/* These don't seem to work? */
     98 
     99 				if (ELF_ST_TYPE(sym->st_info) == STT_FUNC) {
    100 					if (sym->st_value)
    101 						*got = sym->st_value +
    102 						    (Elf_Word)obj->relocbase;
    103 					else
    104 						*got = def->st_value +
    105 						    (Elf_Word)defobj->relocbase;
    106 				} else
    107 #endif
    108 					*got = def->st_value +
    109 					    (Elf_Word)defobj->relocbase;
    110 			} else if (sym->st_shndx == SHN_COMMON) {
    111 				*got = def->st_value +
    112 				    (Elf_Word)defobj->relocbase;
    113 			} else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    114 			    *got != sym->st_value) {
    115 				*got += (Elf_Word)obj->relocbase;
    116 			} else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
    117 			    ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
    118 				if (sym->st_shndx == SHN_ABS)
    119 					*got = sym->st_value +
    120 					    (Elf_Word)obj->relocbase;
    121 				/* else SGI stuff ignored */
    122 			} else
    123 				*got = def->st_value +
    124 				    (Elf_Word)defobj->relocbase;
    125 		}
    126 
    127 		++sym;
    128 		++got;
    129 	}
    130 
    131 	obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
    132 	/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
    133 	obj->pltgot[1] |= (Elf_Addr) obj;
    134 }
    135 
    136 int
    137 _rtld_relocate_nonplt_objects(obj, dodebug)
    138 	Obj_Entry *obj;
    139 	bool dodebug;
    140 {
    141 	const Elf_Rel *rel;
    142 
    143 	for (rel = obj->rel; rel < obj->rellim; rel++) {
    144 		Elf_Addr        *where;
    145 		const Elf_Sym   *def;
    146 		const Obj_Entry *defobj;
    147 		unsigned long	 symnum;
    148 
    149 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    150 		symnum = ELF_R_SYM(rel->r_info);
    151 
    152 		switch (ELF_R_TYPE(rel->r_info)) {
    153 		case R_TYPE(NONE):
    154 			break;
    155 
    156 		case R_TYPE(REL32):
    157 			/* 32-bit PC-relative reference */
    158 			def = obj->symtab + symnum;
    159 
    160 			if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
    161 			  (ELF_ST_TYPE(def->st_info) == STT_SECTION ||
    162 			   ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
    163 				/*
    164 				 * XXX: ABI DIFFERENCE!
    165 				 *
    166 				 * Old NetBSD binutils would generate shared
    167 				 * libs with section-relative relocations being
    168 				 * already adjusted for the start address of
    169 				 * the section.
    170 				 *
    171 				 * New binutils, OTOH, generate shared libs
    172 				 * with the same relocations being based at
    173 				 * zero, so we need to add in the start address
    174 				 * of the section.
    175 				 *
    176 				 * We assume that all section-relative relocs
    177 				 * with contents less than the start of the
    178 				 * section need to be adjusted; this should
    179 				 * work with both old and new shlibs.
    180 				 *
    181 				 * --rkb, Oct 6, 2001
    182 				 */
    183 				if (def->st_info == STT_SECTION &&
    184 					    (*where < def->st_value))
    185 				    *where += (Elf_Addr) def->st_value;
    186 
    187 				*where += (Elf_Addr)obj->relocbase;
    188 
    189 				rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    190 				    obj->strtab + def->st_name, obj->path,
    191 				    (void *)*where, obj->path));
    192 			} else {
    193 				/* XXX maybe do something re: bootstrapping? */
    194 				def = _rtld_find_symdef(symnum, obj, &defobj,
    195 				    false);
    196 				if (def == NULL)
    197 					return -1;
    198 				*where += (Elf_Addr)(defobj->relocbase +
    199 				    def->st_value);
    200 				rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    201 				    obj->strtab + obj->symtab[symnum].st_name,
    202 				    obj->path, (void *)*where, defobj->path));
    203 			}
    204 			break;
    205 
    206 		default:
    207 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    208 			    "contents = %p, symbol = %s",
    209 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    210 			    (void *)rel->r_offset, (void *)*where,
    211 			    obj->strtab + obj->symtab[symnum].st_name));
    212 			_rtld_error("%s: Unsupported relocation type %ld "
    213 			    "in non-PLT relocations\n",
    214 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    215 			return -1;
    216 		}
    217 	}
    218 	return 0;
    219 }
    220 
    221 int
    222 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
    223 	Obj_Entry *obj;
    224 	const Elf_Rela *rela;
    225 	caddr_t *addrp;
    226 	bool dodebug;
    227 {
    228 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    229 	Elf_Addr new_value;
    230 
    231 	/* Fully resolve procedure addresses now */
    232 
    233 	if (!obj->mainprog) {
    234 		/* Just relocate the GOT slots pointing into the PLT */
    235 		new_value = *where + (Elf_Addr)(obj->relocbase);
    236 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    237 		    (void *)*where));
    238 	} else {
    239 		return 0;
    240 	}
    241 	if (*where != new_value)
    242 		*where = new_value;
    243 
    244 	*addrp = (caddr_t)new_value;
    245 	return 0;
    246 }
    247 
    248 int
    249 _rtld_relocate_plt_lazy(obj, dodebug)
    250 	Obj_Entry *obj;
    251 	bool dodebug;
    252 {
    253 	const Elf_Rel *rel;
    254 
    255 	if (obj->mainprog)
    256 		return 0;
    257 
    258 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    259 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    260 
    261 		/* Just relocate the GOT slots pointing into the PLT */
    262 		*where += (Elf_Addr)obj->relocbase;
    263 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    264 		    (void *)*where));
    265 	}
    266 
    267 	return 0;
    268 }
    269