Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.46
      1 /*	$NetBSD: reloc.c,v 1.46 2001/12/16 08:23:25 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 int
    153 _rtld_relocate_nonplt_object(obj, rela, dodebug)
    154 	Obj_Entry *obj;
    155 	const Elf_Rela *rela;
    156 	bool dodebug;
    157 {
    158 	Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    159 	const Elf_Sym   *def;
    160 	const Obj_Entry *defobj;
    161 #if defined(__alpha__) || defined(__arm__) || defined(__i386__) || \
    162     defined(__m68k__) || defined(__powerpc__) || defined(__vax__)
    163 	Elf_Addr         tmp;
    164 #endif
    165 
    166 	switch (ELF_R_TYPE(rela->r_info)) {
    167 
    168 	case R_TYPE(NONE):
    169 		break;
    170 
    171 #if defined(__i386__)
    172 	case R_TYPE(GOT32):
    173 
    174 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    175 		    &defobj, false);
    176 		if (def == NULL)
    177 			return -1;
    178 
    179 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
    180 		if (*where != tmp)
    181 			*where = tmp;
    182 		rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
    183 		    defobj->strtab + def->st_name, obj->path,
    184 		    (void *)*where, defobj->path));
    185 		break;
    186 
    187 	case R_TYPE(PC32):
    188 		/*
    189 		 * I don't think the dynamic linker should ever see this
    190 		 * type of relocation.  But the binutils-2.6 tools sometimes
    191 		 * generate it.
    192 		 */
    193 
    194 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    195 		    &defobj, false);
    196 		if (def == NULL)
    197 			return -1;
    198 
    199 		*where += (Elf_Addr)(defobj->relocbase + def->st_value) -
    200 		    (Elf_Addr)where;
    201 		rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
    202 		    defobj->strtab + def->st_name, obj->path,
    203 		    (void *)*where, defobj->path));
    204 		break;
    205 
    206 	case R_TYPE(32):
    207 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    208 		    &defobj, false);
    209 		if (def == NULL)
    210 			return -1;
    211 
    212 		*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    213 		rdbg(dodebug, ("32 %s in %s --> %p in %s",
    214 		    defobj->strtab + def->st_name, obj->path,
    215 		    (void *)*where, defobj->path));
    216 		break;
    217 #endif /* __i386__ */
    218 
    219 #if defined(__m68k__)
    220 	case R_TYPE(GOT32):
    221 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    222 		    &defobj, false);
    223 		if (def == NULL)
    224 			return -1;
    225 
    226 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    227 		    rela->r_addend);
    228 		if (*where != tmp)
    229 			*where = tmp;
    230 		rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
    231 		    defobj->strtab + def->st_name, obj->path,
    232 		    (void *)*where, defobj->path));
    233 		break;
    234 
    235 	case R_TYPE(PC32):
    236 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    237 		    &defobj, false);
    238 		if (def == NULL)
    239 			return -1;
    240 
    241 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    242 		    rela->r_addend) - (Elf_Addr)where;
    243 		if (*where != tmp)
    244 			*where = tmp;
    245 		rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
    246 		    defobj->strtab + def->st_name, obj->path,
    247 		    (void *)*where, defobj->path));
    248 		break;
    249 
    250 	case R_TYPE(32):
    251 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    252 		    &defobj, false);
    253 		if (def == NULL)
    254 			return -1;
    255 
    256 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    257 		    rela->r_addend);
    258 		if (*where != tmp)
    259 			*where = tmp;
    260 		rdbg(dodebug, ("32 %s in %s --> %p in %s",
    261 		    defobj->strtab + def->st_name, obj->path,
    262 		    (void *)*where, defobj->path));
    263 		break;
    264 #endif /* __m68k__ */
    265 
    266 #if defined(__alpha__)
    267 	case R_TYPE(REFQUAD):
    268 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    269 		    &defobj, false);
    270 		if (def == NULL)
    271 			return -1;
    272 
    273 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
    274 		    *where + rela->r_addend;
    275 		if (*where != tmp)
    276 			*where = tmp;
    277 		rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
    278 		    defobj->strtab + def->st_name, obj->path,
    279 		    (void *)*where, defobj->path));
    280 		break;
    281 
    282 	case R_TYPE(RELATIVE):
    283 	    {
    284 		extern Elf_Addr	_GLOBAL_OFFSET_TABLE_[];
    285 		extern Elf_Addr	_GOT_END_[];
    286 
    287 		/* This is the ...iffy hueristic. */
    288 		if (!dodebug ||
    289 		    (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
    290 		    (caddr_t)where >= (caddr_t)_GOT_END_) {
    291 			*where += (Elf_Addr)(obj->relocbase + rela->r_addend);
    292 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    293 			    (void *)*where));
    294 		} else
    295 			rdbg(dodebug, ("RELATIVE in %s stays at %p",
    296 			    obj->path, (void *)*where));
    297 		break;
    298 	    }
    299 #endif /* __alpha__ */
    300 
    301 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
    302 	case R_TYPE(GLOB_DAT):
    303 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    304 		    &defobj, false);
    305 		if (def == NULL)
    306 			return -1;
    307 
    308 		if (*where != (Elf_Addr)(defobj->relocbase + def->st_value))
    309 			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
    310 		rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
    311 		    defobj->strtab + def->st_name, obj->path,
    312 		    (void *)*where, defobj->path));
    313 		break;
    314 
    315 #if !defined(__alpha__)
    316 	case R_TYPE(RELATIVE):
    317 		*where += (Elf_Addr)obj->relocbase;
    318 		rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    319 		    (void *)*where));
    320 		break;
    321 #endif /* ! __alpha__ */
    322 
    323 	case R_TYPE(COPY):
    324 		/*
    325 		 * These are deferred until all other relocations have
    326 		 * been done.  All we do here is make sure that the COPY
    327 		 * relocation is not in a shared library.  They are allowed
    328 		 * only in executable files.
    329 		 */
    330 		if (!obj->mainprog) {
    331 			_rtld_error(
    332 			"%s: Unexpected R_COPY relocation in shared library",
    333 			    obj->path);
    334 			return -1;
    335 		}
    336 		rdbg(dodebug, ("COPY (avoid in main)"));
    337 		break;
    338 #endif /* __i386__ || __alpha__ || __m68k__ */
    339 
    340 #if defined(__mips__)
    341 	case R_TYPE(REL32):
    342 		/* 32-bit PC-relative reference */
    343 		def = obj->symtab + ELF_R_SYM(rela->r_info);
    344 
    345 		if (ELFDEFNNAME(ST_BIND)(def->st_info) == STB_LOCAL &&
    346 		  (ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_SECTION ||
    347 		   ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_NOTYPE)) {
    348 			/*
    349 			 * XXX: ABI DIFFERENCE!
    350 			 *
    351 			 * Old NetBSD binutils would generate shared libs
    352 			 * with section-relative relocations being already
    353 			 * adjusted for the start address of the section.
    354 			 *
    355 			 * New binutils, OTOH, generate shared libs with
    356 			 * the same relocations being based at zero, so we
    357 			 * need to add in the start address of the section.
    358 			 *
    359 			 * We assume that all section-relative relocs with
    360 			 * contents less than the start of the section need
    361 			 * to be adjusted; this should work with both old
    362 			 * and new shlibs.
    363 			 *
    364 			 * --rkb, Oct 6, 2001
    365 			 */
    366 			if (def->st_info == STT_SECTION &&
    367 				    (*where < def->st_value))
    368 			    *where += (Elf_Addr) def->st_value;
    369 
    370 			*where += (Elf_Addr)obj->relocbase;
    371 
    372 			rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    373 			    obj->strtab + def->st_name, obj->path,
    374 			    (void *)*where, obj->path));
    375 		} else {
    376 			/* XXX maybe do something re: bootstrapping? */
    377 			def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
    378 			    NULL, obj, &defobj, false);
    379 			if (def == NULL)
    380 				return -1;
    381 			*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    382 			rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    383 			    defobj->strtab + def->st_name, obj->path,
    384 			    (void *)*where, defobj->path));
    385 		}
    386 		break;
    387 
    388 #endif /* __mips__ */
    389 
    390 #if defined(__powerpc__) || defined(__vax__)
    391 	case R_TYPE(32):	/* word32 S + A */
    392 	case R_TYPE(GLOB_DAT):	/* word32 S + A */
    393 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    394 		    &defobj, false);
    395 		if (def == NULL)
    396 			return -1;
    397 
    398 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    399 		    rela->r_addend);
    400 
    401 		if (*where != tmp)
    402 			*where = tmp;
    403 		rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
    404 		    defobj->strtab + def->st_name, obj->path,
    405 		    (void *)*where, defobj->path));
    406 		break;
    407 
    408 	case R_TYPE(COPY):
    409 		rdbg(dodebug, ("COPY"));
    410 		break;
    411 
    412 	case R_TYPE(JMP_SLOT):
    413 		rdbg(dodebug, ("JMP_SLOT"));
    414 		break;
    415 
    416 	case R_TYPE(RELATIVE):	/* word32 B + A */
    417 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    418 		if (*where != tmp)
    419 			*where = tmp;
    420 		rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    421 		    (void *)*where));
    422 		break;
    423 #endif /* __powerpc__ || __vax__ */
    424 
    425 #if defined(__powerpc__)
    426 	case R_TYPE(16_LO):	/* #lo(S + A) */
    427 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    428 		if (*(Elf32_Half *)where != tmp)
    429 			*(Elf32_Half *)where = tmp;
    430 		rdbg(dodebug, ("16_LO in %s --> %p", obj->path,
    431 		    (void *)*where));
    432 		break;
    433 
    434 	case R_TYPE(16_HI):	/* #hi(S + A) */
    435 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend) >> 16;
    436 		if (*(Elf32_Half *)where != tmp)
    437 			*(Elf32_Half *)where = tmp;
    438 		rdbg(dodebug, ("16_HI in %s --> %p", obj->path,
    439 		    (void *)*where));
    440 		break;
    441 
    442 	case R_TYPE(16_HA):	/* #ha(S + A) */
    443 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend + 0x8000)
    444 		    >> 16;
    445 		if (*(Elf32_Half *)where != tmp)
    446 			*(Elf32_Half *)where = tmp;
    447 		rdbg(dodebug, ("16_HA in %s --> %p", obj->path,
    448 		    (void *)*where));
    449 		break;
    450 #endif /* __powerpc__ */
    451 
    452 #if defined(__arm__)
    453 	case R_TYPE(GLOB_DAT):	/* word32 B + S */
    454 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    455 		    &defobj, false);
    456 		if (def == NULL)
    457 			return -1;
    458 		*where = (Elf_Addr)(defobj->relocbase + def->st_value);
    459 		rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
    460 		    defobj->strtab + def->st_name, obj->path,
    461 		    (void *)*where, where, defobj->path));
    462 		break;
    463 
    464 	case R_TYPE(COPY):
    465 		rdbg(dodebug, ("COPY"));
    466 		break;
    467 
    468 	case R_TYPE(JUMP_SLOT):
    469 		rdbg(dodebug, ("JUMP_SLOT"));
    470 		break;
    471 
    472 	case R_TYPE(ABS32):	/* word32 B + S + A */
    473 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    474 		    &defobj, false);
    475 		if (def == NULL)
    476 			return -1;
    477 		*where += (Elf_Addr)defobj->relocbase + def->st_value;
    478 		rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
    479 		    defobj->strtab + def->st_name, obj->path,
    480 		    (void *)*where, where, defobj->path));
    481 		break;
    482 
    483 	case R_TYPE(RELATIVE):	/* word32 B + A */
    484 		*where += (Elf_Addr)obj->relocbase;
    485 		rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
    486 		    (void *)*where, where));
    487 		break;
    488 
    489 	case R_TYPE(PC24): {	/* word32 S - P + A */
    490 		Elf32_Sword addend;
    491 
    492 		/*
    493 		 * Extract addend and sign-extend if needed.
    494 		 */
    495 		addend = *where;
    496 		if (addend & 0x00800000)
    497 			addend |= 0xff000000;
    498 
    499 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    500 		    &defobj, false);
    501 		if (def == NULL)
    502 			return -1;
    503 		tmp = (Elf_Addr)obj->relocbase + def->st_value
    504 		    - (Elf_Addr)where + (addend << 2);
    505 		if ((tmp & 0xfe000000) != 0xfe000000 && (tmp & 0xfe000000) != 0) {
    506 			_rtld_error(
    507 			"%s: R_ARM_PC24 relocation @ %p to %s failed "
    508 			"(displacement %ld (%#lx) out of range)",
    509 			    obj->path, where, defobj->strtab + def->st_name,
    510 			    (long) tmp, (long) tmp);
    511 			return -1;
    512 		}
    513 		tmp >>= 2;
    514 		*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
    515 		rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
    516 		    defobj->strtab + def->st_name, obj->path,
    517 		    (void *)*where, where, defobj->path));
    518 		break;
    519 	}
    520 #endif
    521 
    522 	default:
    523 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    524 		    &defobj, true);
    525 		rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    526 		    "addend = %p, contents = %p, symbol = %s",
    527 		    (u_long)ELF_R_SYM(rela->r_info),
    528 		    (u_long)ELF_R_TYPE(rela->r_info),
    529 		    (void *)rela->r_offset, (void *)rela->r_addend,
    530 		    (void *)*where,
    531 		    def ? defobj->strtab + def->st_name : "??"));
    532 		_rtld_error("%s: Unsupported relocation type %ld "
    533 		    "in non-PLT relocations\n",
    534 		    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    535 		return -1;
    536 	}
    537 	return 0;
    538 }
    539 
    540 
    541 #if !defined(__powerpc__)
    542 
    543 int
    544 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
    545 	Obj_Entry *obj;
    546 	const Elf_Rela *rela;
    547 	caddr_t *addrp;
    548 	bool bind_now;
    549 	bool dodebug;
    550 {
    551 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    552 	Elf_Addr new_value;
    553 
    554 	/* Fully resolve procedure addresses now */
    555 
    556 #if defined(__alpha__) || defined(__arm__) || defined(__i386__) || \
    557     defined(__m68k__) || defined(__vax__)
    558 	if (bind_now || obj->pltgot == NULL) {
    559 		const Elf_Sym  *def;
    560 		const Obj_Entry *defobj;
    561 
    562 #if !defined(__arm__)
    563 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    564 #else
    565 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    566 #endif
    567 
    568 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    569 		    &defobj, true);
    570 		if (def == NULL)
    571 			return -1;
    572 
    573 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    574 #if defined(__vax__)
    575 		new_value += rela->r_addend;
    576 #endif
    577 		rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
    578 		    (int)bind_now,
    579 		    defobj->strtab + def->st_name,
    580 		    (void *)*where, (void *)new_value));
    581 	} else
    582 #endif /* __alpha__ || __i386__ || __m68k__ || __vax__ */
    583 	if (!obj->mainprog) {
    584 		/* Just relocate the GOT slots pointing into the PLT */
    585 		new_value = *where + (Elf_Addr)(obj->relocbase);
    586 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    587 		    (void *)*where));
    588 	} else {
    589 		return 0;
    590 	}
    591 	/*
    592          * Since this page is probably copy-on-write, let's not write
    593          * it unless we really really have to.
    594          */
    595 	if (*where != new_value)
    596 		*where = new_value;
    597 	if (addrp != NULL) {
    598 		*addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
    599 #if defined(__vax__)
    600 		*addrp -= rela->r_addend;
    601 #endif
    602 	}
    603 	return 0;
    604 }
    605 
    606 #endif /* __powerpc__ */
    607 
    608 #endif /* __sparc__ || __x86_64__ */
    609 
    610 caddr_t
    611 _rtld_bind(obj, reloff)
    612 	Obj_Entry *obj;
    613 	Elf_Word reloff;
    614 {
    615 	const Elf_Rela *rela;
    616 	Elf_Rela        ourrela;
    617 	caddr_t		addr;
    618 
    619 	if (obj->pltrel != NULL) {
    620 		const Elf_Rel *rel;
    621 
    622 		rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
    623 		ourrela.r_info = rel->r_info;
    624 		ourrela.r_offset = rel->r_offset;
    625 		ourrela.r_addend = 0;
    626 		rela = &ourrela;
    627 	} else {
    628 		rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
    629 #ifdef __sparc64__
    630 		if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
    631 			/*
    632 			 * XXXX
    633 			 *
    634 			 * The first for PLT entries are reserved.  There
    635 			 * is some disagreement whether they should have
    636 			 * associated relocation entries.  Both the SPARC
    637 			 * 32-bit and 64-bit ELF specifications say that
    638 			 * they should have relocation entries, but the
    639 			 * 32-bit SPARC binutils do not generate them,
    640 			 * and now the 64-bit SPARC binutils have stopped
    641 			 * generating them too.
    642 			 *
    643 			 * So, to provide binary compatibility, we will
    644 			 * check the first entry, if it is reserved it
    645 			 * should not be of the type JMP_SLOT.  If it
    646 			 * is JMP_SLOT, then the 4 reserved entries were
    647 			 * not generated and our index is 4 entries too far.
    648 			 */
    649 			rela -= 4;
    650 		}
    651 #endif
    652 	}
    653 
    654 	if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
    655 		_rtld_die();
    656 
    657 	return addr;
    658 }
    659 
    660 /*
    661  * Relocate newly-loaded shared objects.  The argument is a pointer to
    662  * the Obj_Entry for the first such object.  All objects from the first
    663  * to the end of the list of objects are relocated.  Returns 0 on success,
    664  * or -1 on failure.
    665  */
    666 int
    667 _rtld_relocate_objects(first, bind_now, dodebug)
    668 	Obj_Entry *first;
    669 	bool bind_now;
    670 	bool dodebug;
    671 {
    672 	Obj_Entry *obj;
    673 	int ok = 1;
    674 
    675 	for (obj = first; obj != NULL; obj = obj->next) {
    676 		if (obj->nbuckets == 0 || obj->nchains == 0 ||
    677 		    obj->buckets == NULL || obj->symtab == NULL ||
    678 		    obj->strtab == NULL) {
    679 			_rtld_error("%s: Shared object has no run-time"
    680 			    " symbol table", obj->path);
    681 			return -1;
    682 		}
    683 		rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
    684 		    "%ld/%ld plt rel/rela)",
    685 		    obj->path,
    686 		    (long)(obj->rellim - obj->rel),
    687 		    (long)(obj->relalim - obj->rela),
    688 		    (long)(obj->pltrellim - obj->pltrel),
    689 		    (long)(obj->pltrelalim - obj->pltrela)));
    690 
    691 		if (obj->textrel) {
    692 			/*
    693 			 * There are relocations to the write-protected text
    694 			 * segment.
    695 			 */
    696 			if (mprotect(obj->mapbase, obj->textsize,
    697 				PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
    698 				_rtld_error("%s: Cannot write-enable text "
    699 				    "segment: %s", obj->path, xstrerror(errno));
    700 				return -1;
    701 			}
    702 		}
    703 		if (obj->rel != NULL) {
    704 			/* Process the non-PLT relocations. */
    705 			const Elf_Rel  *rel;
    706 			for (rel = obj->rel; rel < obj->rellim; ++rel) {
    707 				Elf_Rela        ourrela;
    708 				ourrela.r_info = rel->r_info;
    709 				ourrela.r_offset = rel->r_offset;
    710 #if defined(__mips__)
    711 				/* rel->r_offset is not valid on mips? */
    712 				if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
    713 					ourrela.r_addend = 0;
    714 				else
    715 #endif
    716 					ourrela.r_addend =
    717 					    *(Elf_Word *)(obj->relocbase +
    718 					    rel->r_offset);
    719 
    720 				if (_rtld_relocate_nonplt_object(obj, &ourrela,
    721 				    dodebug) < 0)
    722 					ok = 0;
    723 			}
    724 		}
    725 		if (obj->rela != NULL) {
    726 			/* Process the non-PLT relocations. */
    727 			const Elf_Rela *rela;
    728 			for (rela = obj->rela; rela < obj->relalim; ++rela) {
    729 				if (_rtld_relocate_nonplt_object(obj, rela,
    730 				    dodebug) < 0)
    731 					ok = 0;
    732 			}
    733 		}
    734 		if (obj->textrel) {	/* Re-protected the text segment. */
    735 			if (mprotect(obj->mapbase, obj->textsize,
    736 				     PROT_READ | PROT_EXEC) == -1) {
    737 				_rtld_error("%s: Cannot write-protect text "
    738 				    "segment: %s", obj->path, xstrerror(errno));
    739 				return -1;
    740 			}
    741 		}
    742 		/* Process the PLT relocations. */
    743 		if (obj->pltrel != NULL) {
    744 			const Elf_Rel  *rel;
    745 			for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
    746 				Elf_Rela        ourrela;
    747 				ourrela.r_info = rel->r_info;
    748 				ourrela.r_offset = rel->r_offset;
    749 				ourrela.r_addend =
    750 				    *(Elf_Word *)(obj->relocbase +
    751 				    rel->r_offset);
    752 				if (_rtld_relocate_plt_object(obj, &ourrela,
    753 				    NULL, bind_now, dodebug) < 0)
    754 					ok = 0;
    755 			}
    756 		}
    757 		if (obj->pltrela != NULL) {
    758 			const Elf_Rela *rela;
    759 			for (rela = obj->pltrela; rela < obj->pltrelalim;
    760 			    ++rela) {
    761 				if (_rtld_relocate_plt_object(obj, rela,
    762 				    NULL, bind_now, dodebug) < 0)
    763 					ok = 0;
    764 			}
    765 		}
    766 		if (!ok)
    767 			return -1;
    768 
    769 
    770 		/* Set some sanity-checking numbers in the Obj_Entry. */
    771 		obj->magic = RTLD_MAGIC;
    772 		obj->version = RTLD_VERSION;
    773 
    774 		/* Fill in the dynamic linker entry points. */
    775 		obj->dlopen = _rtld_dlopen;
    776 		obj->dlsym = _rtld_dlsym;
    777 		obj->dlerror = _rtld_dlerror;
    778 		obj->dlclose = _rtld_dlclose;
    779 		obj->dladdr = _rtld_dladdr;
    780 
    781 		/* Set the special PLTGOT entries. */
    782 		if (obj->pltgot != NULL) {
    783 #if defined(__arm__) || defined(__i386__) || defined(__m68k__) || \
    784     defined(__x86_64__)
    785 			obj->pltgot[1] = (Elf_Addr) obj;
    786 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    787 #endif
    788 #if defined(__alpha__)
    789 			_rtld_setup_alpha_pltgot(obj, dodebug);
    790 #endif
    791 #if defined(__mips__)
    792 			_rtld_relocate_mips_got(obj);
    793 
    794 			obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
    795 			/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
    796 			obj->pltgot[1] |= (Elf_Addr) obj;
    797 #endif
    798 #if defined(__powerpc__)
    799 			_rtld_setup_powerpc_plt(obj);
    800 #endif
    801 #if defined(__sparc__)
    802 #if defined(__arch64__) || defined(__sparc_v9__)
    803 			/*
    804 			 * On sparc64 we got troubles.
    805 			 *
    806 			 * Instructions are 4 bytes long.
    807 			 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
    808 			 * array entries.
    809 			 * Each PLT entry jumps to PLT0 to enter the dynamic
    810 			 * linker.
    811 			 * Loading an arbitrary 64-bit pointer takes 6
    812 			 * instructions and 2 registers.
    813 			 *
    814 			 * Somehow we need to issue a save to get a new stack
    815 			 * frame, load the address of the dynamic linker, and
    816 			 * jump there, in 8 instructions or less.
    817 			 *
    818 			 * Oh, we need to fill out both PLT0 and PLT1.
    819 			 */
    820 			{
    821 				Elf_Word *entry = (Elf_Word *)obj->pltgot;
    822 				extern void _rtld_install_plt __P((Elf_Word *,
    823 					Elf_Addr));
    824 				extern void _rtld_bind_start_0 __P((long,
    825 					long));
    826 				extern void _rtld_bind_start_1 __P((long,
    827 					long));
    828 
    829 				/* Install in entries 0 and 1 */
    830 				_rtld_install_plt(&entry[0],
    831 					(Elf_Addr)&_rtld_bind_start_0);
    832 				_rtld_install_plt(&entry[8],
    833 					(Elf_Addr)&_rtld_bind_start_1);
    834 
    835 				/*
    836 				 * Install the object reference in first slot
    837 				 * of entry 2.
    838 				 */
    839 				obj->pltgot[8] = (Elf_Addr) obj;
    840 			}
    841 #else
    842 			/*
    843 			 * PLTGOT is the PLT on the sparc.
    844 			 * The first entry holds the call the dynamic linker.
    845 			 * We construct a `call' sequence that transfers
    846 			 * to `_rtld_bind_start()'.
    847 			 * The second entry holds the object identification.
    848 			 * Note: each PLT entry is three words long.
    849 			 */
    850 #define SAVE	0x9de3bfc0	/* i.e. `save %sp,-64,%sp' */
    851 #define CALL	0x40000000
    852 #define NOP	0x01000000
    853 			obj->pltgot[0] = SAVE;
    854 			obj->pltgot[1] = CALL |
    855 			    ((Elf_Addr)&_rtld_bind_start -
    856 			     (Elf_Addr)&obj->pltgot[1]) >> 2;
    857 			obj->pltgot[2] = NOP;
    858 
    859 			obj->pltgot[3] = (Elf_Addr) obj;
    860 #endif /* __arch64__ */
    861 #endif /* __sparc__ */
    862 #if defined(__vax__)
    863 			obj->pltgot[1] = (Elf_Addr) obj;
    864 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    865 #endif
    866 		}
    867 	}
    868 
    869 	return 0;
    870 }
    871