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