Home | History | Annotate | Line # | Download | only in mips
mips_reloc.c revision 1.5
      1  1.5    rafal /*	$NetBSD: mips_reloc.c,v 1.5 2001/11/24 18:07:04 rafal Exp $	*/
      2  1.1   mhitch 
      3  1.1   mhitch /*
      4  1.1   mhitch  * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
      5  1.1   mhitch  * All rights reserved.
      6  1.1   mhitch  *
      7  1.1   mhitch  * Redistribution and use in source and binary forms, with or without
      8  1.1   mhitch  * modification, are permitted provided that the following conditions
      9  1.1   mhitch  * are met:
     10  1.1   mhitch  * 1. Redistributions of source code must retain the above copyright
     11  1.1   mhitch  *    notice, this list of conditions and the following disclaimer.
     12  1.1   mhitch  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   mhitch  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   mhitch  *    documentation and/or other materials provided with the distribution.
     15  1.1   mhitch  * 3. The name of the author may not be used to endorse or promote products
     16  1.1   mhitch  *    derived from this software without specific prior written permission.
     17  1.1   mhitch  *
     18  1.1   mhitch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1   mhitch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1   mhitch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1   mhitch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1   mhitch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1   mhitch  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1   mhitch  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1   mhitch  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1   mhitch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1   mhitch  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1   mhitch  */
     29  1.1   mhitch 
     30  1.1   mhitch 
     31  1.1   mhitch #include <stdarg.h>
     32  1.1   mhitch #include <sys/types.h>
     33  1.3  mycroft #include <sys/stat.h>
     34  1.1   mhitch 
     35  1.1   mhitch #include "debug.h"
     36  1.1   mhitch #include "rtld.h"
     37  1.1   mhitch 
     38  1.1   mhitch /*
     39  1.1   mhitch  * Relocate a MIPS GOT
     40  1.1   mhitch  */
     41  1.1   mhitch 
     42  1.1   mhitch void
     43  1.1   mhitch _rtld_relocate_mips_got(obj)
     44  1.1   mhitch 	Obj_Entry *obj;
     45  1.1   mhitch {
     46  1.1   mhitch 	Elf_Addr *got = obj->pltgot;
     47  1.1   mhitch 	const Elf_Sym *sym = obj->symtab;
     48  1.1   mhitch 	const Elf_Sym *def;
     49  1.1   mhitch 	const Obj_Entry *defobj;
     50  1.4    rafal 	Elf_Word info;
     51  1.1   mhitch 	int i;
     52  1.1   mhitch 
     53  1.1   mhitch 	i = (got[1] & 0x80000000) ? 2 : 1;
     54  1.1   mhitch 	/* Relocate the local GOT entries */
     55  1.1   mhitch 	while (i < obj->local_gotno)
     56  1.1   mhitch 		got[i++] += (Elf_Word)obj->relocbase;
     57  1.1   mhitch 	got += obj->local_gotno;
     58  1.1   mhitch 	i = obj->symtabno - obj->gotsym;
     59  1.1   mhitch 	sym += obj->gotsym;
     60  1.1   mhitch 	/* Now do the global GOT entries */
     61  1.1   mhitch 	while (i--) {
     62  1.4    rafal 		rdbg(1, (" doing got %d sym %p (%s, %x)",
     63  1.4    rafal 				obj->symtabno - obj->gotsym - i - 1,
     64  1.4    rafal 				sym, sym->st_name + obj->strtab, *got));
     65  1.4    rafal 
     66  1.4    rafal 		info = ELF32_R_INFO(obj->symtabno - i - 1, sym->st_info);
     67  1.4    rafal 		def = _rtld_find_symdef(_rtld_objlist, info, NULL, obj,
     68  1.4    rafal 					&defobj, true);
     69  1.4    rafal 
     70  1.4    rafal 		if (def == NULL)
     71  1.4    rafal 			_rtld_error(
     72  1.4    rafal 	    "%s: Undefined PLT symbol \"%s\" (reloc type = %ld, symnum = %ld)",
     73  1.4    rafal 			    obj->path, sym->st_name + obj->strtab,
     74  1.4    rafal 			    (u_long) ELF_R_TYPE(info),
     75  1.4    rafal 			    (u_long) obj->symtabno - i - 1);
     76  1.5    rafal 		else {
     77  1.4    rafal 
     78  1.5    rafal 			if (sym->st_shndx == SHN_UNDEF) {
     79  1.1   mhitch #if 0	/* These don't seem to work? */
     80  1.1   mhitch 
     81  1.5    rafal 				if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
     82  1.5    rafal 				    STT_FUNC) {
     83  1.5    rafal 					if (sym->st_value)
     84  1.5    rafal 						*got = sym->st_value +
     85  1.5    rafal 						    (Elf_Word)obj->relocbase;
     86  1.5    rafal 					else
     87  1.5    rafal 						*got = def->st_value +
     88  1.5    rafal 						    (Elf_Word)defobj->relocbase;
     89  1.5    rafal 				} else
     90  1.5    rafal #endif
     91  1.5    rafal 					*got = def->st_value +
     92  1.5    rafal 					    (Elf_Word)defobj->relocbase;
     93  1.5    rafal 			} else if (sym->st_shndx == SHN_COMMON) {
     94  1.5    rafal 				*got = def->st_value +
     95  1.5    rafal 				    (Elf_Word)defobj->relocbase;
     96  1.5    rafal 			} else if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
     97  1.5    rafal 			    STT_FUNC &&
     98  1.5    rafal 			    *got != sym->st_value) {
     99  1.5    rafal 				*got += (Elf_Word)obj->relocbase;
    100  1.5    rafal 			} else if (ELFDEFNNAME(ST_TYPE)(sym->st_info) ==
    101  1.5    rafal 			    STT_SECTION && ELFDEFNNAME(ST_BIND)(sym->st_info) ==
    102  1.5    rafal 			    STB_GLOBAL) {
    103  1.5    rafal 				if (sym->st_shndx == SHN_ABS)
    104  1.4    rafal 					*got = sym->st_value +
    105  1.4    rafal 					    (Elf_Word)obj->relocbase;
    106  1.5    rafal 				/* else SGI stuff ignored */
    107  1.1   mhitch 			} else
    108  1.1   mhitch 				*got = def->st_value +
    109  1.1   mhitch 				    (Elf_Word)defobj->relocbase;
    110  1.5    rafal 		}
    111  1.4    rafal 
    112  1.1   mhitch 		++sym;
    113  1.1   mhitch 		++got;
    114  1.1   mhitch 	}
    115  1.1   mhitch }
    116  1.1   mhitch 
    117  1.1   mhitch /*
    118  1.1   mhitch  * _rtld_bind_mips(symbol_index, return_address, old_gp, stub_return_addr)
    119  1.1   mhitch  */
    120  1.1   mhitch 
    121  1.1   mhitch caddr_t
    122  1.1   mhitch _rtld_bind_mips(a0, a1, a2, a3)
    123  1.1   mhitch 	Elf_Word a0;
    124  1.1   mhitch 	Elf_Addr a1, a2, a3;
    125  1.1   mhitch {
    126  1.1   mhitch 	Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
    127  1.1   mhitch 	Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
    128  1.1   mhitch 	const Elf_Sym *def;
    129  1.1   mhitch 	const Obj_Entry *defobj;
    130  1.1   mhitch 
    131  1.1   mhitch 	def = _rtld_find_symdef(_rtld_objlist, a0 << 8, NULL, obj, &defobj,
    132  1.1   mhitch 	    true);
    133  1.1   mhitch 	if (def) {
    134  1.1   mhitch 		u[obj->local_gotno + a0 - obj->gotsym] = (Elf_Addr)
    135  1.1   mhitch 		    (def->st_value + defobj->relocbase);
    136  1.1   mhitch 		return((caddr_t)(def->st_value + defobj->relocbase));
    137  1.1   mhitch 	}
    138  1.1   mhitch 
    139  1.1   mhitch 	return(NULL);	/* XXX */
    140  1.1   mhitch }
    141