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