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