Home | History | Annotate | Line # | Download | only in mips
mips_reloc.c revision 1.8
      1 /*	$NetBSD: mips_reloc.c,v 1.8 2002/09/05 17:58:04 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 
     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 	Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
     49 	const Elf_Sym *def;
     50 	const Obj_Entry *defobj;
     51 
     52 	def = _rtld_find_symdef(ELF_R_INFO(a0, 0), 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 	Obj_Entry *obj;
     65 {
     66 	Elf_Addr *got = obj->pltgot;
     67 	const Elf_Sym *sym = obj->symtab;
     68 	const Elf_Sym *def;
     69 	const Obj_Entry *defobj;
     70 	int i;
     71 
     72 	i = (got[1] & 0x80000000) ? 2 : 1;
     73 	/* Relocate the local GOT entries */
     74 	while (i < obj->local_gotno)
     75 		got[i++] += (Elf_Word)obj->relocbase;
     76 	got += obj->local_gotno;
     77 	i = obj->symtabno - obj->gotsym;
     78 	sym += obj->gotsym;
     79 	/* Now do the global GOT entries */
     80 	while (i--) {
     81 		rdbg(1, (" doing got %d sym %p (%s, %x)",
     82 				obj->symtabno - obj->gotsym - i - 1,
     83 				sym, sym->st_name + obj->strtab, *got));
     84 
     85 		def = _rtld_find_symdef(ELF_R_INFO(obj->symtabno - i - 1, 0),
     86 		    obj, &defobj, true);
     87 		if (def == NULL)
     88 			_rtld_error(
     89 	    "%s: Undefined PLT symbol \"%s\" (section type = %ld, symnum = %ld)",
     90 			    obj->path, sym->st_name + obj->strtab,
     91 			    (u_long) ELF_ST_TYPE(sym->st_info),
     92 			    (u_long) obj->symtabno - i - 1);
     93 		else {
     94 
     95 			if (sym->st_shndx == SHN_UNDEF) {
     96 #if 0	/* These don't seem to work? */
     97 
     98 				if (ELF_ST_TYPE(sym->st_info) == STT_FUNC) {
     99 					if (sym->st_value)
    100 						*got = sym->st_value +
    101 						    (Elf_Word)obj->relocbase;
    102 					else
    103 						*got = def->st_value +
    104 						    (Elf_Word)defobj->relocbase;
    105 				} else
    106 #endif
    107 					*got = def->st_value +
    108 					    (Elf_Word)defobj->relocbase;
    109 			} else if (sym->st_shndx == SHN_COMMON) {
    110 				*got = def->st_value +
    111 				    (Elf_Word)defobj->relocbase;
    112 			} else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    113 			    *got != sym->st_value) {
    114 				*got += (Elf_Word)obj->relocbase;
    115 			} else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
    116 			    ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
    117 				if (sym->st_shndx == SHN_ABS)
    118 					*got = sym->st_value +
    119 					    (Elf_Word)obj->relocbase;
    120 				/* else SGI stuff ignored */
    121 			} else
    122 				*got = def->st_value +
    123 				    (Elf_Word)defobj->relocbase;
    124 		}
    125 
    126 		++sym;
    127 		++got;
    128 	}
    129 
    130 	obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
    131 	/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
    132 	obj->pltgot[1] |= (Elf_Addr) obj;
    133 }
    134 
    135 int
    136 _rtld_relocate_nonplt_object(obj, rela, dodebug)
    137 	Obj_Entry *obj;
    138 	const Elf_Rela *rela;
    139 	bool dodebug;
    140 {
    141 	Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    142 	const Elf_Sym   *def;
    143 	const Obj_Entry *defobj;
    144 
    145 	switch (ELF_R_TYPE(rela->r_info)) {
    146 
    147 	case R_TYPE(NONE):
    148 		break;
    149 
    150 	case R_TYPE(REL32):
    151 		/* 32-bit PC-relative reference */
    152 		def = obj->symtab + ELF_R_SYM(rela->r_info);
    153 
    154 		if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
    155 		  (ELF_ST_TYPE(def->st_info) == STT_SECTION ||
    156 		   ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
    157 			/*
    158 			 * XXX: ABI DIFFERENCE!
    159 			 *
    160 			 * Old NetBSD binutils would generate shared libs
    161 			 * with section-relative relocations being already
    162 			 * adjusted for the start address of the section.
    163 			 *
    164 			 * New binutils, OTOH, generate shared libs with
    165 			 * the same relocations being based at zero, so we
    166 			 * need to add in the start address of the section.
    167 			 *
    168 			 * We assume that all section-relative relocs with
    169 			 * contents less than the start of the section need
    170 			 * to be adjusted; this should work with both old
    171 			 * and new shlibs.
    172 			 *
    173 			 * --rkb, Oct 6, 2001
    174 			 */
    175 			if (def->st_info == STT_SECTION &&
    176 				    (*where < def->st_value))
    177 			    *where += (Elf_Addr) def->st_value;
    178 
    179 			*where += (Elf_Addr)obj->relocbase;
    180 
    181 			rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    182 			    obj->strtab + def->st_name, obj->path,
    183 			    (void *)*where, obj->path));
    184 		} else {
    185 			/* XXX maybe do something re: bootstrapping? */
    186 			def = _rtld_find_symdef(rela->r_info, obj, &defobj,
    187 			    false);
    188 			if (def == NULL)
    189 				return -1;
    190 			*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    191 			rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    192 			    defobj->strtab + def->st_name, obj->path,
    193 			    (void *)*where, defobj->path));
    194 		}
    195 		break;
    196 
    197 	default:
    198 		def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
    199 		rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    200 		    "addend = %p, contents = %p, symbol = %s",
    201 		    (u_long)ELF_R_SYM(rela->r_info),
    202 		    (u_long)ELF_R_TYPE(rela->r_info),
    203 		    (void *)rela->r_offset, (void *)rela->r_addend,
    204 		    (void *)*where,
    205 		    def ? defobj->strtab + def->st_name : "??"));
    206 		_rtld_error("%s: Unsupported relocation type %ld "
    207 		    "in non-PLT relocations\n",
    208 		    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    209 		return -1;
    210 	}
    211 	return 0;
    212 }
    213 
    214 int
    215 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
    216 	Obj_Entry *obj;
    217 	const Elf_Rela *rela;
    218 	caddr_t *addrp;
    219 	bool bind_now;
    220 	bool dodebug;
    221 {
    222 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    223 	Elf_Addr new_value;
    224 
    225 	/* Fully resolve procedure addresses now */
    226 
    227 	if (!obj->mainprog) {
    228 		/* Just relocate the GOT slots pointing into the PLT */
    229 		new_value = *where + (Elf_Addr)(obj->relocbase);
    230 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    231 		    (void *)*where));
    232 	} else {
    233 		return 0;
    234 	}
    235 	/*
    236          * Since this page is probably copy-on-write, let's not write
    237          * it unless we really really have to.
    238          */
    239 	if (*where != new_value)
    240 		*where = new_value;
    241 	if (addrp != NULL) {
    242 		*addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
    243 #if defined(__vax__)
    244 		*addrp -= rela->r_addend;
    245 #endif
    246 	}
    247 	return 0;
    248 }
    249