Home | History | Annotate | Line # | Download | only in mips
mips_reloc.c revision 1.40
      1 /*	$NetBSD: mips_reloc.c,v 1.40 2003/07/26 15:04:41 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
      5  * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net>
      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 #include <sys/types.h>
     32 #include <sys/stat.h>
     33 
     34 #include <stdlib.h>
     35 #include <string.h>
     36 
     37 #include "debug.h"
     38 #include "rtld.h"
     39 
     40 #define SUPPORT_OLD_BROKEN_LD
     41 
     42 void _rtld_bind_start(void);
     43 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     44 caddr_t _rtld_bind(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
     45 
     46 void
     47 _rtld_setup_pltgot(const Obj_Entry *obj)
     48 {
     49 	obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
     50 	/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
     51 	obj->pltgot[1] |= (Elf_Addr) obj;
     52 }
     53 
     54 void
     55 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     56 {
     57 	const Elf_Rel *rel = 0, *rellim;
     58 	Elf_Addr relsz = 0;
     59 	Elf_Addr *where;
     60 	const Elf_Sym *symtab, *sym;
     61 	Elf_Addr *got;
     62 	Elf_Word local_gotno, symtabno, gotsym;
     63 	int i;
     64 
     65 	for (; dynp->d_tag != DT_NULL; dynp++) {
     66 		switch (dynp->d_tag) {
     67 		case DT_REL:
     68 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     69 			break;
     70 		case DT_RELSZ:
     71 			relsz = dynp->d_un.d_val;
     72 			break;
     73 		case DT_SYMTAB:
     74 			symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
     75 			break;
     76 		case DT_PLTGOT:
     77 			got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
     78 			break;
     79 		case DT_MIPS_LOCAL_GOTNO:
     80 			local_gotno = dynp->d_un.d_val;
     81 			break;
     82 		case DT_MIPS_SYMTABNO:
     83 			symtabno = dynp->d_un.d_val;
     84 			break;
     85 		case DT_MIPS_GOTSYM:
     86 			gotsym = dynp->d_un.d_val;
     87 			break;
     88 		}
     89 	}
     90 
     91 	i = (got[1] & 0x80000000) ? 2 : 1;
     92 	/* Relocate the local GOT entries */
     93 	got += i;
     94 	for (; i < local_gotno; i++)
     95 		*got++ += relocbase;
     96 	sym = symtab + gotsym;
     97 	/* Now do the global GOT entries */
     98 	for (i = gotsym; i < symtabno; i++) {
     99 		*got = sym->st_value + relocbase;
    100 		++sym;
    101 		++got;
    102 	}
    103 
    104 	rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
    105 	for (; rel < rellim; rel++) {
    106 		where = (Elf_Addr *)(relocbase + rel->r_offset);
    107 
    108 		switch (ELF_R_TYPE(rel->r_info)) {
    109 		case R_TYPE(NONE):
    110 			break;
    111 
    112 		case R_TYPE(REL32):
    113 			assert(ELF_R_SYM(rel->r_info) < gotsym);
    114 			sym = symtab + ELF_R_SYM(rel->r_info);
    115 			assert(sym->st_info ==
    116 			    ELF_ST_INFO(STB_LOCAL, STT_SECTION));
    117 			*where += (Elf_Addr)(sym->st_value + relocbase);
    118 			break;
    119 
    120 		default:
    121 			abort();
    122 		}
    123 	}
    124 }
    125 
    126 /*
    127  * It is possible for the compiler to emit relocations for unaligned data.
    128  * We handle this situation with these inlines.
    129  */
    130 #define	RELOC_ALIGNED_P(x) \
    131 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
    132 
    133 static __inline Elf_Addr
    134 load_ptr(void *where)
    135 {
    136 	Elf_Addr res;
    137 
    138 	memcpy(&res, where, sizeof(res));
    139 
    140 	return (res);
    141 }
    142 
    143 static __inline void
    144 store_ptr(void *where, Elf_Addr val)
    145 {
    146 
    147 	memcpy(where, &val, sizeof(val));
    148 }
    149 
    150 int
    151 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
    152 {
    153 	const Elf_Rel *rel;
    154 	Elf_Addr *got = obj->pltgot;
    155 	const Elf_Sym *sym, *def;
    156 	const Obj_Entry *defobj;
    157 	int i;
    158 #ifdef SUPPORT_OLD_BROKEN_LD
    159 	int broken;
    160 #endif
    161 
    162 #ifdef SUPPORT_OLD_BROKEN_LD
    163 	broken = 0;
    164 	sym = obj->symtab;
    165 	for (i = 1; i < 12; i++)
    166 		if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
    167 			broken = 1;
    168 	dbg(("%s: broken=%d", obj->path, broken));
    169 #endif
    170 
    171 	i = (got[1] & 0x80000000) ? 2 : 1;
    172 	/* Relocate the local GOT entries */
    173 	got += i;
    174 	for (; i < obj->local_gotno; i++)
    175 		*got++ += (Elf_Addr)obj->relocbase;
    176 	sym = obj->symtab + obj->gotsym;
    177 	/* Now do the global GOT entries */
    178 	for (i = obj->gotsym; i < obj->symtabno; i++) {
    179 		rdbg((" doing got %d sym %p (%s, %x)", i - obj->gotsym, sym,
    180 		    sym->st_name + obj->strtab, *got));
    181 
    182 #ifdef SUPPORT_OLD_BROKEN_LD
    183 		if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    184 		    broken && sym->st_shndx == SHN_UNDEF) {
    185 			/*
    186 			 * XXX DANGER WILL ROBINSON!
    187 			 * You might think this is stupid, as it intentionally
    188 			 * defeats lazy binding -- and you'd be right.
    189 			 * Unfortunately, for lazy binding to work right, we
    190 			 * need to a way to force the GOT slots used for
    191 			 * function pointers to be resolved immediately.  This
    192 			 * is supposed to be done automatically by the linker,
    193 			 * by not outputting a PLT slot and setting st_value
    194 			 * to 0 if there are non-PLT references, but older
    195 			 * versions of GNU ld do not do this.
    196 			 */
    197 			def = _rtld_find_symdef(i, obj, &defobj, false);
    198 			if (def == NULL)
    199 				return -1;
    200 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
    201 		} else
    202 #endif
    203 		if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    204 		    sym->st_value != 0) {
    205 			/*
    206 			 * If there are non-PLT references to the function,
    207 			 * st_value should be 0, forcing us to resolve the
    208 			 * address immediately.
    209 			 */
    210 			*got = sym->st_value + (Elf_Addr)obj->relocbase;
    211 		} else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
    212 			/* Symbols with index SHN_ABS are not relocated. */
    213 			if (sym->st_shndx != SHN_ABS)
    214 				*got = sym->st_value +
    215 				    (Elf_Addr)obj->relocbase;
    216 		} else {
    217 			def = _rtld_find_symdef(i, obj, &defobj, false);
    218 			if (def == NULL)
    219 				return -1;
    220 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
    221 		}
    222 
    223 		rdbg(("  --> now %x", *got));
    224 		++sym;
    225 		++got;
    226 	}
    227 
    228 	got = obj->pltgot;
    229 	for (rel = obj->rel; rel < obj->rellim; rel++) {
    230 		Elf_Addr        *where, tmp;
    231 		unsigned long	 symnum;
    232 
    233 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    234 		symnum = ELF_R_SYM(rel->r_info);
    235 
    236 		switch (ELF_R_TYPE(rel->r_info)) {
    237 		case R_TYPE(NONE):
    238 			break;
    239 
    240 		case R_TYPE(REL32):
    241 			/* 32-bit PC-relative reference */
    242 			def = obj->symtab + symnum;
    243 
    244 			if (symnum >= obj->gotsym) {
    245 				tmp = *where;
    246 				tmp += got[obj->local_gotno + symnum - obj->gotsym];
    247 				*where = tmp;
    248 
    249 				rdbg(("REL32/G %s in %s --> %p in %s",
    250 				    obj->strtab + def->st_name, obj->path,
    251 				    (void *)tmp, obj->path));
    252 				break;
    253 			} else {
    254 				/*
    255 				 * XXX: ABI DIFFERENCE!
    256 				 *
    257 				 * Old NetBSD binutils would generate shared
    258 				 * libs with section-relative relocations being
    259 				 * already adjusted for the start address of
    260 				 * the section.
    261 				 *
    262 				 * New binutils, OTOH, generate shared libs
    263 				 * with the same relocations being based at
    264 				 * zero, so we need to add in the start address
    265 				 * of the section.
    266 				 *
    267 				 * --rkb, Oct 6, 2001
    268 				 */
    269 				tmp = *where;
    270 
    271 				if (def->st_info ==
    272 				    ELF_ST_INFO(STB_LOCAL, STT_SECTION)
    273 #ifdef SUPPORT_OLD_BROKEN_LD
    274 				    && !broken
    275 #endif
    276 				    )
    277 					tmp += (Elf_Addr)def->st_value;
    278 
    279 				tmp += (Elf_Addr)obj->relocbase;
    280 				*where = tmp;
    281 
    282 				rdbg(("REL32/L %s in %s --> %p in %s",
    283 				    obj->strtab + def->st_name, obj->path,
    284 				    (void *)tmp, obj->path));
    285 			}
    286 			break;
    287 
    288 		default:
    289 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    290 			    "contents = %p, symbol = %s",
    291 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    292 			    (void *)rel->r_offset, (void *)load_ptr(where),
    293 			    obj->strtab + obj->symtab[symnum].st_name));
    294 			_rtld_error("%s: Unsupported relocation type %ld "
    295 			    "in non-PLT relocations\n",
    296 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    297 			return -1;
    298 		}
    299 	}
    300 
    301 	return 0;
    302 }
    303 
    304 int
    305 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    306 {
    307 	/* PLT fixups were done above in the GOT relocation. */
    308 	return 0;
    309 }
    310 
    311 caddr_t
    312 _rtld_bind(Elf_Word a0, Elf_Addr a1, Elf_Addr a2, Elf_Addr a3)
    313 {
    314 	Elf_Addr *got = (Elf_Addr *)(a2 - 0x7ff0);
    315 	const Obj_Entry *obj = (Obj_Entry *)(got[1] & 0x7fffffff);
    316 	const Elf_Sym *def;
    317 	const Obj_Entry *defobj;
    318 	Elf_Addr new_value;
    319 
    320 	def = _rtld_find_symdef(a0, obj, &defobj, true);
    321 	if (def == NULL)
    322 		_rtld_die();
    323 
    324 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    325 	rdbg(("bind now/fixup in %s --> new=%p",
    326 	    defobj->strtab + def->st_name, (void *)new_value));
    327 	got[obj->local_gotno + a0 - obj->gotsym] = new_value;
    328 	return ((caddr_t)new_value);
    329 }
    330