Home | History | Annotate | Line # | Download | only in mips
mips_reloc.c revision 1.4
      1 /*	$NetBSD: mips_reloc.c,v 1.4 2001/10/14 23:13:22 rafal 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  * Relocate a MIPS GOT
     40  */
     41 
     42 void
     43 _rtld_relocate_mips_got(obj)
     44 	Obj_Entry *obj;
     45 {
     46 	Elf_Addr *got = obj->pltgot;
     47 	const Elf_Sym *sym = obj->symtab;
     48 	const Elf_Sym *def;
     49 	const Obj_Entry *defobj;
     50 	Elf_Word info;
     51 	int i;
     52 
     53 	i = (got[1] & 0x80000000) ? 2 : 1;
     54 	/* Relocate the local GOT entries */
     55 	while (i < obj->local_gotno)
     56 		got[i++] += (Elf_Word)obj->relocbase;
     57 	got += obj->local_gotno;
     58 	i = obj->symtabno - obj->gotsym;
     59 	sym += obj->gotsym;
     60 	/* Now do the global GOT entries */
     61 	while (i--) {
     62 		rdbg(1, (" doing got %d sym %p (%s, %x)",
     63 				obj->symtabno - obj->gotsym - i - 1,
     64 				sym, sym->st_name + obj->strtab, *got));
     65 
     66 		info = ELF32_R_INFO(obj->symtabno - i - 1, sym->st_info);
     67 		def = _rtld_find_symdef(_rtld_objlist, info, NULL, obj,
     68 					&defobj, true);
     69 
     70 		if (def == NULL)
     71 			_rtld_error(
     72 	    "%s: Undefined PLT symbol \"%s\" (reloc type = %ld, symnum = %ld)",
     73 			    obj->path, sym->st_name + obj->strtab,
     74 			    (u_long) ELF_R_TYPE(info),
     75 			    (u_long) obj->symtabno - i - 1);
     76 
     77 		if (sym->st_shndx == SHN_UNDEF) {
     78 #if 0	/* These don't seem to work? */
     79 
     80 			if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
     81 			    STT_FUNC) {
     82 				if (sym->st_value)
     83 					*got = sym->st_value +
     84 					    (Elf_Word)obj->relocbase;
     85 				else
     86 					*got = def->st_value +
     87 					    (Elf_Word)defobj->relocbase;
     88 			} else
     89 #endif
     90 				*got = def->st_value +
     91 				    (Elf_Word)defobj->relocbase;
     92 		} else if (sym->st_shndx == SHN_COMMON) {
     93 			*got = def->st_value +
     94 			    (Elf_Word)defobj->relocbase;
     95 		} else if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
     96 		    STT_FUNC &&
     97 		    *got != sym->st_value) {
     98 			*got += (Elf_Word)obj->relocbase;
     99 		} else if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
    100 		    STT_SECTION && ELFDEFNNAME(ST_BIND)(sym->st_info) ==
    101 		    STB_GLOBAL) {
    102 			if (sym->st_shndx == SHN_ABS)
    103 				*got = sym->st_value +
    104 				    (Elf_Word)obj->relocbase;
    105 			/* else SGI stuff ignored */
    106 		} else
    107 			*got = def->st_value +
    108 			    (Elf_Word)defobj->relocbase;
    109 
    110 		++sym;
    111 		++got;
    112 	}
    113 }
    114 
    115 /*
    116  * _rtld_bind_mips(symbol_index, return_address, old_gp, stub_return_addr)
    117  */
    118 
    119 caddr_t
    120 _rtld_bind_mips(a0, a1, a2, a3)
    121 	Elf_Word a0;
    122 	Elf_Addr a1, a2, a3;
    123 {
    124 	Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
    125 	Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
    126 	const Elf_Sym *def;
    127 	const Obj_Entry *defobj;
    128 
    129 	def = _rtld_find_symdef(_rtld_objlist, a0 << 8, NULL, obj, &defobj,
    130 	    true);
    131 	if (def) {
    132 		u[obj->local_gotno + a0 - obj->gotsym] = (Elf_Addr)
    133 		    (def->st_value + defobj->relocbase);
    134 		return((caddr_t)(def->st_value + defobj->relocbase));
    135 	}
    136 
    137 	return(NULL);	/* XXX */
    138 }
    139