Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.45
      1 /*	$NetBSD: reloc.c,v 1.45 2001/12/14 00:53:06 thorpej Exp $	 */
      2 
      3 /*
      4  * Copyright 1996 John D. Polstra.
      5  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      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. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by John Polstra.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Dynamic linker for ELF.
     36  *
     37  * John Polstra <jdp (at) polstra.com>.
     38  */
     39 
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <stdarg.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 #include <sys/types.h>
     49 #include <sys/mman.h>
     50 #include <dirent.h>
     51 
     52 #include "debug.h"
     53 #include "rtld.h"
     54 
     55 #ifndef RTLD_INHIBIT_COPY_RELOCS
     56 static int _rtld_do_copy_relocation __P((const Obj_Entry *, const Elf_Rela *,
     57     bool));
     58 
     59 /*
     60  * XXX: These don't work for the alpha and i386; don't know about powerpc
     61  *	The alpha and the i386 avoid the problem by compiling everything PIC.
     62  *	These relocation are supposed to be writing the address of the
     63  *	function to be called on the bss.rel or bss.rela segment, but:
     64  *		- st_size == 0
     65  *		- on the i386 at least the call instruction is a direct call
     66  *		  not an indirect call.
     67  */
     68 static int
     69 _rtld_do_copy_relocation(dstobj, rela, dodebug)
     70 	const Obj_Entry *dstobj;
     71 	const Elf_Rela *rela;
     72 	bool dodebug;
     73 {
     74 	void           *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
     75 	const Elf_Sym  *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
     76 	const char     *name = dstobj->strtab + dstsym->st_name;
     77 	unsigned long   hash = _rtld_elf_hash(name);
     78 	size_t          size = dstsym->st_size;
     79 	const void     *srcaddr;
     80 	const Elf_Sym  *srcsym = NULL;
     81 	Obj_Entry      *srcobj;
     82 
     83 	for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
     84 		if ((srcsym = _rtld_symlook_obj(name, hash, srcobj,
     85 		    false)) != NULL)
     86 			break;
     87 
     88 	if (srcobj == NULL) {
     89 		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
     90 		    " relocation in %s", name, dstobj->path);
     91 		return (-1);
     92 	}
     93 	srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
     94 	(void)memcpy(dstaddr, srcaddr, size);
     95 	rdbg(dodebug, ("COPY %s %s %s --> src=%p dst=%p *dst= %p size %ld",
     96 	    dstobj->path, srcobj->path, name, (void *)srcaddr,
     97 	    (void *)dstaddr, (void *)*(long *)dstaddr, (long)size));
     98 	return (0);
     99 }
    100 #endif /* RTLD_INHIBIT_COPY_RELOCS */
    101 
    102 
    103 /*
    104  * Process the special R_xxx_COPY relocations in the main program.  These
    105  * copy data from a shared object into a region in the main program's BSS
    106  * segment.
    107  *
    108  * Returns 0 on success, -1 on failure.
    109  */
    110 int
    111 _rtld_do_copy_relocations(dstobj, dodebug)
    112 	const Obj_Entry *dstobj;
    113 	bool dodebug;
    114 {
    115 #ifndef RTLD_INHIBIT_COPY_RELOCS
    116 
    117 	/* COPY relocations are invalid elsewhere */
    118 	assert(dstobj->mainprog);
    119 
    120 	if (dstobj->rel != NULL) {
    121 		const Elf_Rel  *rel;
    122 		for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
    123 			if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
    124 				Elf_Rela        ourrela;
    125 				ourrela.r_info = rel->r_info;
    126 				ourrela.r_offset = rel->r_offset;
    127 				ourrela.r_addend = 0;
    128 				if (_rtld_do_copy_relocation(dstobj,
    129 				    &ourrela, dodebug) < 0)
    130 					return (-1);
    131 			}
    132 		}
    133 	}
    134 	if (dstobj->rela != NULL) {
    135 		const Elf_Rela *rela;
    136 		for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
    137 			if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
    138 				if (_rtld_do_copy_relocation(dstobj, rela,
    139 				    dodebug) < 0)
    140 					return (-1);
    141 			}
    142 		}
    143 	}
    144 #endif /* RTLD_INHIBIT_COPY_RELOCS */
    145 
    146 	return (0);
    147 }
    148 
    149 
    150 #if !defined(__sparc__) && !defined(__x86_64__)
    151 
    152 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
    153 extern Elf_Addr  _GLOBAL_OFFSET_TABLE_[];
    154 extern Elf_Dyn   _DYNAMIC;
    155 #endif
    156 
    157 int
    158 _rtld_relocate_nonplt_object(obj, rela, dodebug)
    159 	Obj_Entry *obj;
    160 	const Elf_Rela *rela;
    161 	bool dodebug;
    162 {
    163 	Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    164 	const Elf_Sym   *def;
    165 	const Obj_Entry *defobj;
    166 #if defined(__alpha__) || defined(__arm__) || defined(__i386__) || \
    167     defined(__m68k__) || defined(__powerpc__) || defined(__vax__)
    168 	Elf_Addr         tmp;
    169 #endif
    170 
    171 	switch (ELF_R_TYPE(rela->r_info)) {
    172 
    173 	case R_TYPE(NONE):
    174 		break;
    175 
    176 #if defined(__i386__)
    177 	case R_TYPE(GOT32):
    178 
    179 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    180 		    &defobj, false);
    181 		if (def == NULL)
    182 			return -1;
    183 
    184 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
    185 		if (*where != tmp)
    186 			*where = tmp;
    187 		rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
    188 		    defobj->strtab + def->st_name, obj->path,
    189 		    (void *)*where, defobj->path));
    190 		break;
    191 
    192 	case R_TYPE(PC32):
    193 		/*
    194 		 * I don't think the dynamic linker should ever see this
    195 		 * type of relocation.  But the binutils-2.6 tools sometimes
    196 		 * generate it.
    197 		 */
    198 
    199 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    200 		    &defobj, false);
    201 		if (def == NULL)
    202 			return -1;
    203 
    204 		*where += (Elf_Addr)(defobj->relocbase + def->st_value) -
    205 		    (Elf_Addr)where;
    206 		rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
    207 		    defobj->strtab + def->st_name, obj->path,
    208 		    (void *)*where, defobj->path));
    209 		break;
    210 
    211 	case R_TYPE(32):
    212 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    213 		    &defobj, false);
    214 		if (def == NULL)
    215 			return -1;
    216 
    217 		*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    218 		rdbg(dodebug, ("32 %s in %s --> %p in %s",
    219 		    defobj->strtab + def->st_name, obj->path,
    220 		    (void *)*where, defobj->path));
    221 		break;
    222 #endif /* __i386__ */
    223 
    224 #if defined(__m68k__)
    225 	case R_TYPE(GOT32):
    226 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    227 		    &defobj, false);
    228 		if (def == NULL)
    229 			return -1;
    230 
    231 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    232 		    rela->r_addend);
    233 		if (*where != tmp)
    234 			*where = tmp;
    235 		rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
    236 		    defobj->strtab + def->st_name, obj->path,
    237 		    (void *)*where, defobj->path));
    238 		break;
    239 
    240 	case R_TYPE(PC32):
    241 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    242 		    &defobj, false);
    243 		if (def == NULL)
    244 			return -1;
    245 
    246 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    247 		    rela->r_addend) - (Elf_Addr)where;
    248 		if (*where != tmp)
    249 			*where = tmp;
    250 		rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
    251 		    defobj->strtab + def->st_name, obj->path,
    252 		    (void *)*where, defobj->path));
    253 		break;
    254 
    255 	case R_TYPE(32):
    256 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    257 		    &defobj, false);
    258 		if (def == NULL)
    259 			return -1;
    260 
    261 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    262 		    rela->r_addend);
    263 		if (*where != tmp)
    264 			*where = tmp;
    265 		rdbg(dodebug, ("32 %s in %s --> %p in %s",
    266 		    defobj->strtab + def->st_name, obj->path,
    267 		    (void *)*where, defobj->path));
    268 		break;
    269 #endif /* __m68k__ */
    270 
    271 #if defined(__alpha__)
    272 	case R_TYPE(REFQUAD):
    273 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    274 		    &defobj, false);
    275 		if (def == NULL)
    276 			return -1;
    277 
    278 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
    279 		    *where + rela->r_addend;
    280 		if (*where != tmp)
    281 			*where = tmp;
    282 		rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
    283 		    defobj->strtab + def->st_name, obj->path,
    284 		    (void *)*where, defobj->path));
    285 		break;
    286 #endif /* __alpha__ */
    287 
    288 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
    289 	case R_TYPE(GLOB_DAT):
    290 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    291 		    &defobj, false);
    292 		if (def == NULL)
    293 			return -1;
    294 
    295 		if (*where != (Elf_Addr)(defobj->relocbase + def->st_value))
    296 			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
    297 		rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
    298 		    defobj->strtab + def->st_name, obj->path,
    299 		    (void *)*where, defobj->path));
    300 		break;
    301 
    302 	case R_TYPE(RELATIVE):
    303 		if (!dodebug ||
    304 		    (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
    305 		    (caddr_t)where >= (caddr_t)&_DYNAMIC) {
    306 			*where += (Elf_Addr)obj->relocbase;
    307 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    308 			    (void *)*where));
    309 		}
    310 		else
    311 			rdbg(dodebug, ("RELATIVE in %s stays at %p",
    312 			    obj->path, (void *)*where));
    313 		break;
    314 
    315 	case R_TYPE(COPY):
    316 		/*
    317 		 * These are deferred until all other relocations have
    318 		 * been done.  All we do here is make sure that the COPY
    319 		 * relocation is not in a shared library.  They are allowed
    320 		 * only in executable files.
    321 		 */
    322 		if (!obj->mainprog) {
    323 			_rtld_error(
    324 			"%s: Unexpected R_COPY relocation in shared library",
    325 			    obj->path);
    326 			return -1;
    327 		}
    328 		rdbg(dodebug, ("COPY (avoid in main)"));
    329 		break;
    330 #endif /* __i386__ || __alpha__ || __m68k__ */
    331 
    332 #if defined(__mips__)
    333 	case R_TYPE(REL32):
    334 		/* 32-bit PC-relative reference */
    335 		def = obj->symtab + ELF_R_SYM(rela->r_info);
    336 
    337 		if (ELFDEFNNAME(ST_BIND)(def->st_info) == STB_LOCAL &&
    338 		  (ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_SECTION ||
    339 		   ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_NOTYPE)) {
    340 			/*
    341 			 * XXX: ABI DIFFERENCE!
    342 			 *
    343 			 * Old NetBSD binutils would generate shared libs
    344 			 * with section-relative relocations being already
    345 			 * adjusted for the start address of the section.
    346 			 *
    347 			 * New binutils, OTOH, generate shared libs with
    348 			 * the same relocations being based at zero, so we
    349 			 * need to add in the start address of the section.
    350 			 *
    351 			 * We assume that all section-relative relocs with
    352 			 * contents less than the start of the section need
    353 			 * to be adjusted; this should work with both old
    354 			 * and new shlibs.
    355 			 *
    356 			 * --rkb, Oct 6, 2001
    357 			 */
    358 			if (def->st_info == STT_SECTION &&
    359 				    (*where < def->st_value))
    360 			    *where += (Elf_Addr) def->st_value;
    361 
    362 			*where += (Elf_Addr)obj->relocbase;
    363 
    364 			rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    365 			    obj->strtab + def->st_name, obj->path,
    366 			    (void *)*where, obj->path));
    367 		} else {
    368 			/* XXX maybe do something re: bootstrapping? */
    369 			def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
    370 			    NULL, obj, &defobj, false);
    371 			if (def == NULL)
    372 				return -1;
    373 			*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    374 			rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    375 			    defobj->strtab + def->st_name, obj->path,
    376 			    (void *)*where, defobj->path));
    377 		}
    378 		break;
    379 
    380 #endif /* __mips__ */
    381 
    382 #if defined(__powerpc__) || defined(__vax__)
    383 	case R_TYPE(32):	/* word32 S + A */
    384 	case R_TYPE(GLOB_DAT):	/* word32 S + A */
    385 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    386 		    &defobj, false);
    387 		if (def == NULL)
    388 			return -1;
    389 
    390 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    391 		    rela->r_addend);
    392 
    393 		if (*where != tmp)
    394 			*where = tmp;
    395 		rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
    396 		    defobj->strtab + def->st_name, obj->path,
    397 		    (void *)*where, defobj->path));
    398 		break;
    399 
    400 	case R_TYPE(COPY):
    401 		rdbg(dodebug, ("COPY"));
    402 		break;
    403 
    404 	case R_TYPE(JMP_SLOT):
    405 		rdbg(dodebug, ("JMP_SLOT"));
    406 		break;
    407 
    408 	case R_TYPE(RELATIVE):	/* word32 B + A */
    409 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    410 		if (*where != tmp)
    411 			*where = tmp;
    412 		rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    413 		    (void *)*where));
    414 		break;
    415 #endif /* __powerpc__ || __vax__ */
    416 
    417 #if defined(__powerpc__)
    418 	case R_TYPE(16_LO):	/* #lo(S + A) */
    419 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    420 		if (*(Elf32_Half *)where != tmp)
    421 			*(Elf32_Half *)where = tmp;
    422 		rdbg(dodebug, ("16_LO in %s --> %p", obj->path,
    423 		    (void *)*where));
    424 		break;
    425 
    426 	case R_TYPE(16_HI):	/* #hi(S + A) */
    427 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend) >> 16;
    428 		if (*(Elf32_Half *)where != tmp)
    429 			*(Elf32_Half *)where = tmp;
    430 		rdbg(dodebug, ("16_HI in %s --> %p", obj->path,
    431 		    (void *)*where));
    432 		break;
    433 
    434 	case R_TYPE(16_HA):	/* #ha(S + A) */
    435 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend + 0x8000)
    436 		    >> 16;
    437 		if (*(Elf32_Half *)where != tmp)
    438 			*(Elf32_Half *)where = tmp;
    439 		rdbg(dodebug, ("16_HA in %s --> %p", obj->path,
    440 		    (void *)*where));
    441 		break;
    442 #endif /* __powerpc__ */
    443 
    444 #if defined(__arm__)
    445 	case R_TYPE(GLOB_DAT):	/* word32 B + S */
    446 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    447 		    &defobj, false);
    448 		if (def == NULL)
    449 			return -1;
    450 		*where = (Elf_Addr)(defobj->relocbase + def->st_value);
    451 		rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
    452 		    defobj->strtab + def->st_name, obj->path,
    453 		    (void *)*where, where, defobj->path));
    454 		break;
    455 
    456 	case R_TYPE(COPY):
    457 		rdbg(dodebug, ("COPY"));
    458 		break;
    459 
    460 	case R_TYPE(JUMP_SLOT):
    461 		rdbg(dodebug, ("JUMP_SLOT"));
    462 		break;
    463 
    464 	case R_TYPE(ABS32):	/* word32 B + S + A */
    465 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    466 		    &defobj, false);
    467 		if (def == NULL)
    468 			return -1;
    469 		*where += (Elf_Addr)defobj->relocbase + def->st_value;
    470 		rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
    471 		    defobj->strtab + def->st_name, obj->path,
    472 		    (void *)*where, where, defobj->path));
    473 		break;
    474 
    475 	case R_TYPE(RELATIVE):	/* word32 B + A */
    476 		*where += (Elf_Addr)obj->relocbase;
    477 		rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
    478 		    (void *)*where, where));
    479 		break;
    480 
    481 	case R_TYPE(PC24): {	/* word32 S - P + A */
    482 		Elf32_Sword addend;
    483 
    484 		/*
    485 		 * Extract addend and sign-extend if needed.
    486 		 */
    487 		addend = *where;
    488 		if (addend & 0x00800000)
    489 			addend |= 0xff000000;
    490 
    491 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    492 		    &defobj, false);
    493 		if (def == NULL)
    494 			return -1;
    495 		tmp = (Elf_Addr)obj->relocbase + def->st_value
    496 		    - (Elf_Addr)where + (addend << 2);
    497 		if ((tmp & 0xfe000000) != 0xfe000000 && (tmp & 0xfe000000) != 0) {
    498 			_rtld_error(
    499 			"%s: R_ARM_PC24 relocation @ %p to %s failed "
    500 			"(displacement %ld (%#lx) out of range)",
    501 			    obj->path, where, defobj->strtab + def->st_name,
    502 			    (long) tmp, (long) tmp);
    503 			return -1;
    504 		}
    505 		tmp >>= 2;
    506 		*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
    507 		rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
    508 		    defobj->strtab + def->st_name, obj->path,
    509 		    (void *)*where, where, defobj->path));
    510 		break;
    511 	}
    512 #endif
    513 
    514 	default:
    515 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    516 		    &defobj, true);
    517 		rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    518 		    "addend = %p, contents = %p, symbol = %s",
    519 		    (u_long)ELF_R_SYM(rela->r_info),
    520 		    (u_long)ELF_R_TYPE(rela->r_info),
    521 		    (void *)rela->r_offset, (void *)rela->r_addend,
    522 		    (void *)*where,
    523 		    def ? defobj->strtab + def->st_name : "??"));
    524 		_rtld_error("%s: Unsupported relocation type %ld "
    525 		    "in non-PLT relocations\n",
    526 		    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    527 		return -1;
    528 	}
    529 	return 0;
    530 }
    531 
    532 
    533 #if !defined(__powerpc__)
    534 
    535 int
    536 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
    537 	Obj_Entry *obj;
    538 	const Elf_Rela *rela;
    539 	caddr_t *addrp;
    540 	bool bind_now;
    541 	bool dodebug;
    542 {
    543 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    544 	Elf_Addr new_value;
    545 
    546 	/* Fully resolve procedure addresses now */
    547 
    548 #if defined(__alpha__) || defined(__arm__) || defined(__i386__) || \
    549     defined(__m68k__) || defined(__vax__)
    550 	if (bind_now || obj->pltgot == NULL) {
    551 		const Elf_Sym  *def;
    552 		const Obj_Entry *defobj;
    553 
    554 #if !defined(__arm__)
    555 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    556 #else
    557 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    558 #endif
    559 
    560 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    561 		    &defobj, true);
    562 		if (def == NULL)
    563 			return -1;
    564 
    565 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    566 #if defined(__vax__)
    567 		new_value += rela->r_addend;
    568 #endif
    569 		rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
    570 		    (int)bind_now,
    571 		    defobj->strtab + def->st_name,
    572 		    (void *)*where, (void *)new_value));
    573 	} else
    574 #endif /* __alpha__ || __i386__ || __m68k__ || __vax__ */
    575 	if (!obj->mainprog) {
    576 		/* Just relocate the GOT slots pointing into the PLT */
    577 		new_value = *where + (Elf_Addr)(obj->relocbase);
    578 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    579 		    (void *)*where));
    580 	} else {
    581 		return 0;
    582 	}
    583 	/*
    584          * Since this page is probably copy-on-write, let's not write
    585          * it unless we really really have to.
    586          */
    587 	if (*where != new_value)
    588 		*where = new_value;
    589 	if (addrp != NULL) {
    590 		*addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
    591 #if defined(__vax__)
    592 		*addrp -= rela->r_addend;
    593 #endif
    594 	}
    595 	return 0;
    596 }
    597 
    598 #endif /* __powerpc__ */
    599 
    600 #endif /* __sparc__ || __x86_64__ */
    601 
    602 caddr_t
    603 _rtld_bind(obj, reloff)
    604 	Obj_Entry *obj;
    605 	Elf_Word reloff;
    606 {
    607 	const Elf_Rela *rela;
    608 	Elf_Rela        ourrela;
    609 	caddr_t		addr;
    610 
    611 	if (obj->pltrel != NULL) {
    612 		const Elf_Rel *rel;
    613 
    614 		rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
    615 		ourrela.r_info = rel->r_info;
    616 		ourrela.r_offset = rel->r_offset;
    617 		ourrela.r_addend = 0;
    618 		rela = &ourrela;
    619 	} else {
    620 		rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
    621 #ifdef __sparc64__
    622 		if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
    623 			/*
    624 			 * XXXX
    625 			 *
    626 			 * The first for PLT entries are reserved.  There
    627 			 * is some disagreement whether they should have
    628 			 * associated relocation entries.  Both the SPARC
    629 			 * 32-bit and 64-bit ELF specifications say that
    630 			 * they should have relocation entries, but the
    631 			 * 32-bit SPARC binutils do not generate them,
    632 			 * and now the 64-bit SPARC binutils have stopped
    633 			 * generating them too.
    634 			 *
    635 			 * So, to provide binary compatibility, we will
    636 			 * check the first entry, if it is reserved it
    637 			 * should not be of the type JMP_SLOT.  If it
    638 			 * is JMP_SLOT, then the 4 reserved entries were
    639 			 * not generated and our index is 4 entries too far.
    640 			 */
    641 			rela -= 4;
    642 		}
    643 #endif
    644 	}
    645 
    646 	if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
    647 		_rtld_die();
    648 
    649 	return addr;
    650 }
    651 
    652 /*
    653  * Relocate newly-loaded shared objects.  The argument is a pointer to
    654  * the Obj_Entry for the first such object.  All objects from the first
    655  * to the end of the list of objects are relocated.  Returns 0 on success,
    656  * or -1 on failure.
    657  */
    658 int
    659 _rtld_relocate_objects(first, bind_now, dodebug)
    660 	Obj_Entry *first;
    661 	bool bind_now;
    662 	bool dodebug;
    663 {
    664 	Obj_Entry *obj;
    665 	int ok = 1;
    666 
    667 	for (obj = first; obj != NULL; obj = obj->next) {
    668 		if (obj->nbuckets == 0 || obj->nchains == 0 ||
    669 		    obj->buckets == NULL || obj->symtab == NULL ||
    670 		    obj->strtab == NULL) {
    671 			_rtld_error("%s: Shared object has no run-time"
    672 			    " symbol table", obj->path);
    673 			return -1;
    674 		}
    675 		rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
    676 		    "%ld/%ld plt rel/rela)",
    677 		    obj->path,
    678 		    (long)(obj->rellim - obj->rel),
    679 		    (long)(obj->relalim - obj->rela),
    680 		    (long)(obj->pltrellim - obj->pltrel),
    681 		    (long)(obj->pltrelalim - obj->pltrela)));
    682 
    683 		if (obj->textrel) {
    684 			/*
    685 			 * There are relocations to the write-protected text
    686 			 * segment.
    687 			 */
    688 			if (mprotect(obj->mapbase, obj->textsize,
    689 				PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
    690 				_rtld_error("%s: Cannot write-enable text "
    691 				    "segment: %s", obj->path, xstrerror(errno));
    692 				return -1;
    693 			}
    694 		}
    695 		if (obj->rel != NULL) {
    696 			/* Process the non-PLT relocations. */
    697 			const Elf_Rel  *rel;
    698 			for (rel = obj->rel; rel < obj->rellim; ++rel) {
    699 				Elf_Rela        ourrela;
    700 				ourrela.r_info = rel->r_info;
    701 				ourrela.r_offset = rel->r_offset;
    702 #if defined(__mips__)
    703 				/* rel->r_offset is not valid on mips? */
    704 				if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
    705 					ourrela.r_addend = 0;
    706 				else
    707 #endif
    708 					ourrela.r_addend =
    709 					    *(Elf_Word *)(obj->relocbase +
    710 					    rel->r_offset);
    711 
    712 				if (_rtld_relocate_nonplt_object(obj, &ourrela,
    713 				    dodebug) < 0)
    714 					ok = 0;
    715 			}
    716 		}
    717 		if (obj->rela != NULL) {
    718 			/* Process the non-PLT relocations. */
    719 			const Elf_Rela *rela;
    720 			for (rela = obj->rela; rela < obj->relalim; ++rela) {
    721 				if (_rtld_relocate_nonplt_object(obj, rela,
    722 				    dodebug) < 0)
    723 					ok = 0;
    724 			}
    725 		}
    726 		if (obj->textrel) {	/* Re-protected the text segment. */
    727 			if (mprotect(obj->mapbase, obj->textsize,
    728 				     PROT_READ | PROT_EXEC) == -1) {
    729 				_rtld_error("%s: Cannot write-protect text "
    730 				    "segment: %s", obj->path, xstrerror(errno));
    731 				return -1;
    732 			}
    733 		}
    734 		/* Process the PLT relocations. */
    735 		if (obj->pltrel != NULL) {
    736 			const Elf_Rel  *rel;
    737 			for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
    738 				Elf_Rela        ourrela;
    739 				ourrela.r_info = rel->r_info;
    740 				ourrela.r_offset = rel->r_offset;
    741 				ourrela.r_addend =
    742 				    *(Elf_Word *)(obj->relocbase +
    743 				    rel->r_offset);
    744 				if (_rtld_relocate_plt_object(obj, &ourrela,
    745 				    NULL, bind_now, dodebug) < 0)
    746 					ok = 0;
    747 			}
    748 		}
    749 		if (obj->pltrela != NULL) {
    750 			const Elf_Rela *rela;
    751 			for (rela = obj->pltrela; rela < obj->pltrelalim;
    752 			    ++rela) {
    753 				if (_rtld_relocate_plt_object(obj, rela,
    754 				    NULL, bind_now, dodebug) < 0)
    755 					ok = 0;
    756 			}
    757 		}
    758 		if (!ok)
    759 			return -1;
    760 
    761 
    762 		/* Set some sanity-checking numbers in the Obj_Entry. */
    763 		obj->magic = RTLD_MAGIC;
    764 		obj->version = RTLD_VERSION;
    765 
    766 		/* Fill in the dynamic linker entry points. */
    767 		obj->dlopen = _rtld_dlopen;
    768 		obj->dlsym = _rtld_dlsym;
    769 		obj->dlerror = _rtld_dlerror;
    770 		obj->dlclose = _rtld_dlclose;
    771 		obj->dladdr = _rtld_dladdr;
    772 
    773 		/* Set the special PLTGOT entries. */
    774 		if (obj->pltgot != NULL) {
    775 #if defined(__arm__) || defined(__i386__) || defined(__m68k__) || \
    776     defined(__x86_64__)
    777 			obj->pltgot[1] = (Elf_Addr) obj;
    778 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    779 #endif
    780 #if defined(__alpha__)
    781 			_rtld_setup_alpha_pltgot(obj, dodebug);
    782 #endif
    783 #if defined(__mips__)
    784 			_rtld_relocate_mips_got(obj);
    785 
    786 			obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
    787 			/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
    788 			obj->pltgot[1] |= (Elf_Addr) obj;
    789 #endif
    790 #if defined(__powerpc__)
    791 			_rtld_setup_powerpc_plt(obj);
    792 #endif
    793 #if defined(__sparc__)
    794 #if defined(__arch64__) || defined(__sparc_v9__)
    795 			/*
    796 			 * On sparc64 we got troubles.
    797 			 *
    798 			 * Instructions are 4 bytes long.
    799 			 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
    800 			 * array entries.
    801 			 * Each PLT entry jumps to PLT0 to enter the dynamic
    802 			 * linker.
    803 			 * Loading an arbitrary 64-bit pointer takes 6
    804 			 * instructions and 2 registers.
    805 			 *
    806 			 * Somehow we need to issue a save to get a new stack
    807 			 * frame, load the address of the dynamic linker, and
    808 			 * jump there, in 8 instructions or less.
    809 			 *
    810 			 * Oh, we need to fill out both PLT0 and PLT1.
    811 			 */
    812 			{
    813 				Elf_Word *entry = (Elf_Word *)obj->pltgot;
    814 				extern void _rtld_install_plt __P((Elf_Word *,
    815 					Elf_Addr));
    816 				extern void _rtld_bind_start_0 __P((long,
    817 					long));
    818 				extern void _rtld_bind_start_1 __P((long,
    819 					long));
    820 
    821 				/* Install in entries 0 and 1 */
    822 				_rtld_install_plt(&entry[0],
    823 					(Elf_Addr)&_rtld_bind_start_0);
    824 				_rtld_install_plt(&entry[8],
    825 					(Elf_Addr)&_rtld_bind_start_1);
    826 
    827 				/*
    828 				 * Install the object reference in first slot
    829 				 * of entry 2.
    830 				 */
    831 				obj->pltgot[8] = (Elf_Addr) obj;
    832 			}
    833 #else
    834 			/*
    835 			 * PLTGOT is the PLT on the sparc.
    836 			 * The first entry holds the call the dynamic linker.
    837 			 * We construct a `call' sequence that transfers
    838 			 * to `_rtld_bind_start()'.
    839 			 * The second entry holds the object identification.
    840 			 * Note: each PLT entry is three words long.
    841 			 */
    842 #define SAVE	0x9de3bfc0	/* i.e. `save %sp,-64,%sp' */
    843 #define CALL	0x40000000
    844 #define NOP	0x01000000
    845 			obj->pltgot[0] = SAVE;
    846 			obj->pltgot[1] = CALL |
    847 			    ((Elf_Addr)&_rtld_bind_start -
    848 			     (Elf_Addr)&obj->pltgot[1]) >> 2;
    849 			obj->pltgot[2] = NOP;
    850 
    851 			obj->pltgot[3] = (Elf_Addr) obj;
    852 #endif /* __arch64__ */
    853 #endif /* __sparc__ */
    854 #if defined(__vax__)
    855 			obj->pltgot[1] = (Elf_Addr) obj;
    856 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    857 #endif
    858 		}
    859 	}
    860 
    861 	return 0;
    862 }
    863