Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.37
      1 /*	$NetBSD: reloc.c,v 1.37 2001/07/15 01:44:10 matt 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 			*where += (Elf_Addr)obj->relocbase;
    341 			rdbg(dodebug, ("REL32 in %s --> %p", obj->path,
    342 			    (void *)*where));
    343 		} else {
    344 			/* XXX maybe do something re: bootstrapping? */
    345 			def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
    346 			    NULL, obj, &defobj, false);
    347 			if (def == NULL)
    348 				return -1;
    349 			*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    350 			rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    351 			    defobj->strtab + def->st_name, obj->path,
    352 			    (void *)*where, defobj->path));
    353 		}
    354 		break;
    355 
    356 #endif /* __mips__ */
    357 
    358 #if defined(__powerpc__) || defined(__vax__)
    359 	case R_TYPE(32):	/* word32 S + A */
    360 	case R_TYPE(GLOB_DAT):	/* word32 S + A */
    361 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    362 		    &defobj, false);
    363 		if (def == NULL)
    364 			return -1;
    365 
    366 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    367 		    rela->r_addend);
    368 
    369 		if (*where != tmp)
    370 			*where = tmp;
    371 		rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
    372 		    defobj->strtab + def->st_name, obj->path,
    373 		    (void *)*where, defobj->path));
    374 		break;
    375 
    376 	case R_TYPE(COPY):
    377 		rdbg(dodebug, ("COPY"));
    378 		break;
    379 
    380 	case R_TYPE(JMP_SLOT):
    381 		rdbg(dodebug, ("JMP_SLOT"));
    382 		break;
    383 
    384 	case R_TYPE(RELATIVE):	/* word32 B + A */
    385 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    386 		if (*where != tmp)
    387 			*where = tmp;
    388 		rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    389 		    (void *)*where));
    390 		break;
    391 #endif /* __powerpc__ || __vax__ */
    392 
    393 #if defined(__powerpc__)
    394 	case R_TYPE(16_LO):	/* #lo(S + A) */
    395 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    396 		if (*(Elf32_Half *)where != tmp)
    397 			*(Elf32_Half *)where = tmp;
    398 		rdbg(dodebug, ("16_LO in %s --> %p", obj->path,
    399 		    (void *)*where));
    400 		break;
    401 
    402 	case R_TYPE(16_HI):	/* #hi(S + A) */
    403 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend) >> 16;
    404 		if (*(Elf32_Half *)where != tmp)
    405 			*(Elf32_Half *)where = tmp;
    406 		rdbg(dodebug, ("16_HI in %s --> %p", obj->path,
    407 		    (void *)*where));
    408 		break;
    409 
    410 	case R_TYPE(16_HA):	/* #ha(S + A) */
    411 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend + 0x8000)
    412 		    >> 16;
    413 		if (*(Elf32_Half *)where != tmp)
    414 			*(Elf32_Half *)where = tmp;
    415 		rdbg(dodebug, ("16_HA in %s --> %p", obj->path,
    416 		    (void *)*where));
    417 		break;
    418 #endif /* __powerpc__ */
    419 
    420 #if defined(__arm__)
    421 	case R_TYPE(GLOB_DAT):	/* word32 B + S */
    422 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    423 		    &defobj, false);
    424 		if (def == NULL)
    425 			return -1;
    426 		*where = (Elf_Addr)(defobj->relocbase + def->st_value);
    427 		rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
    428 		    defobj->strtab + def->st_name, obj->path,
    429 		    (void *)*where, defobj->path));
    430 		break;
    431 
    432 	case R_TYPE(COPY):
    433 		rdbg(dodebug, ("COPY"));
    434 		break;
    435 
    436 	case R_TYPE(JUMP_SLOT):
    437 		rdbg(dodebug, ("JUMP_SLOT"));
    438 		break;
    439 
    440 	case R_TYPE(ABS32):	/* word32 B + S + A */
    441 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    442 		    &defobj, false);
    443 		if (def == NULL)
    444 			return -1;
    445 		*where += (Elf_Addr)obj->relocbase + def->st_value;
    446 		rdbg(dodebug, ("ABS32 %s in %s --> %p in %s",
    447 		    defobj->strtab + def->st_name, obj->path,
    448 		    (void *)*where, defobj->path));
    449 		break;
    450 
    451 	case R_TYPE(RELATIVE):	/* word32 B + A */
    452 		*where += (Elf_Addr)obj->relocbase;
    453 		rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    454 		    (void *)*where));
    455 		break;
    456 
    457 	case R_TYPE(PC24): {	/* word32 S - P + A */
    458 		Elf32_Sword addend;
    459 
    460 		/*
    461 		 * Extract addend and sign-extend if needed.
    462 		 */
    463 		addend = *where;
    464 		if (addend & 0x00800000)
    465 			addend |= 0xff000000;
    466 
    467 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    468 		    &defobj, false);
    469 		if (def == NULL)
    470 			return -1;
    471 		tmp = (Elf_Addr)obj->relocbase + def->st_value
    472 		    - (Elf_Addr)where + (addend << 2);
    473 		if ((tmp & 0xfe000000) != 0xfe000000 && (tmp & 0xfe000000) != 0) {
    474 			_rtld_error(
    475 			"%s: R_ARM_PC24 relocation @ %p to %s failed "
    476 			"(displacement %ld (%#lx) out of range)",
    477 			    obj->path, where, defobj->strtab + def->st_name,
    478 			    (long) tmp, (long) tmp);
    479 			return -1;
    480 		}
    481 		tmp >>= 2;
    482 		*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
    483 		rdbg(dodebug, ("PC24 %s in %s --> %p in %s",
    484 		    defobj->strtab + def->st_name, obj->path,
    485 		    (void *)*where, defobj->path));
    486 		break;
    487 	}
    488 #endif
    489 
    490 	default:
    491 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    492 		    &defobj, true);
    493 		rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    494 		    "addend = %p, contents = %p, symbol = %s",
    495 		    (u_long)ELF_R_SYM(rela->r_info),
    496 		    (u_long)ELF_R_TYPE(rela->r_info),
    497 		    (void *)rela->r_offset, (void *)rela->r_addend,
    498 		    (void *)*where,
    499 		    def ? defobj->strtab + def->st_name : "??"));
    500 		_rtld_error("%s: Unsupported relocation type %ld "
    501 		    "in non-PLT relocations\n",
    502 		    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    503 		return -1;
    504 	}
    505 	return 0;
    506 }
    507 
    508 
    509 
    510 int
    511 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
    512 	Obj_Entry *obj;
    513 	const Elf_Rela *rela;
    514 	caddr_t *addrp;
    515 	bool bind_now;
    516 	bool dodebug;
    517 {
    518 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    519 	Elf_Addr new_value;
    520 
    521 	/* Fully resolve procedure addresses now */
    522 
    523 #if defined(__powerpc__)
    524 	return _rtld_reloc_powerpc_plt(obj, rela, bind_now);
    525 #endif
    526 
    527 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
    528     defined(__vax__)
    529 	if (bind_now || obj->pltgot == NULL) {
    530 		const Elf_Sym  *def;
    531 		const Obj_Entry *defobj;
    532 
    533 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    534 
    535 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    536 		    &defobj, true);
    537 		if (def == NULL)
    538 			return -1;
    539 
    540 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    541 #if defined(__vax__)
    542 		new_value += rela->r_addend;
    543 #endif
    544 		rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
    545 		    (int)bind_now,
    546 		    defobj->strtab + def->st_name,
    547 		    (void *)*where, (void *)new_value));
    548 	} else
    549 #endif /* __alpha__ || __i386__ || __m68k__ || __vax__ */
    550 	if (!obj->mainprog) {
    551 		/* Just relocate the GOT slots pointing into the PLT */
    552 		new_value = *where + (Elf_Addr)(obj->relocbase);
    553 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    554 		    (void *)*where));
    555 	} else {
    556 		return 0;
    557 	}
    558 	/*
    559          * Since this page is probably copy-on-write, let's not write
    560          * it unless we really really have to.
    561          */
    562 	if (*where != new_value)
    563 		*where = new_value;
    564 	if (addrp != NULL) {
    565 		*addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
    566 #if defined(__vax__)
    567 		*addrp -= rela->r_addend;
    568 #endif
    569 	}
    570 	return 0;
    571 }
    572 #endif /* __sparc__ */
    573 
    574 caddr_t
    575 _rtld_bind(obj, reloff)
    576 	Obj_Entry *obj;
    577 	Elf_Word reloff;
    578 {
    579 	const Elf_Rela *rela;
    580 	Elf_Rela        ourrela;
    581 	caddr_t		addr;
    582 
    583 	if (obj->pltrel != NULL) {
    584 		const Elf_Rel *rel;
    585 
    586 		rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
    587 		ourrela.r_info = rel->r_info;
    588 		ourrela.r_offset = rel->r_offset;
    589 		ourrela.r_addend = 0;
    590 		rela = &ourrela;
    591 	} else {
    592 		rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
    593 	}
    594 
    595 	if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
    596 		_rtld_die();
    597 
    598 	return addr;
    599 }
    600 
    601 /*
    602  * Relocate newly-loaded shared objects.  The argument is a pointer to
    603  * the Obj_Entry for the first such object.  All objects from the first
    604  * to the end of the list of objects are relocated.  Returns 0 on success,
    605  * or -1 on failure.
    606  */
    607 int
    608 _rtld_relocate_objects(first, bind_now, dodebug)
    609 	Obj_Entry *first;
    610 	bool bind_now;
    611 	bool dodebug;
    612 {
    613 	Obj_Entry *obj;
    614 	int ok = 1;
    615 
    616 	for (obj = first; obj != NULL; obj = obj->next) {
    617 		if (obj->nbuckets == 0 || obj->nchains == 0 ||
    618 		    obj->buckets == NULL || obj->symtab == NULL ||
    619 		    obj->strtab == NULL) {
    620 			_rtld_error("%s: Shared object has no run-time"
    621 			    " symbol table", obj->path);
    622 			return -1;
    623 		}
    624 		rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
    625 		    "%ld/%ld plt rel/rela)",
    626 		    obj->path,
    627 		    (long)(obj->rellim - obj->rel),
    628 		    (long)(obj->relalim - obj->rela),
    629 		    (long)(obj->pltrellim - obj->pltrel),
    630 		    (long)(obj->pltrelalim - obj->pltrela)));
    631 
    632 		if (obj->textrel) {
    633 			/*
    634 			 * There are relocations to the write-protected text
    635 			 * segment.
    636 			 */
    637 			if (mprotect(obj->mapbase, obj->textsize,
    638 				PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
    639 				_rtld_error("%s: Cannot write-enable text "
    640 				    "segment: %s", obj->path, xstrerror(errno));
    641 				return -1;
    642 			}
    643 		}
    644 		if (obj->rel != NULL) {
    645 			/* Process the non-PLT relocations. */
    646 			const Elf_Rel  *rel;
    647 			for (rel = obj->rel; rel < obj->rellim; ++rel) {
    648 				Elf_Rela        ourrela;
    649 				ourrela.r_info = rel->r_info;
    650 				ourrela.r_offset = rel->r_offset;
    651 #if defined(__mips__)
    652 				/* rel->r_offset is not valid on mips? */
    653 				if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
    654 					ourrela.r_addend = 0;
    655 				else
    656 #endif
    657 					ourrela.r_addend =
    658 					    *(Elf_Word *)(obj->relocbase +
    659 					    rel->r_offset);
    660 
    661 				if (_rtld_relocate_nonplt_object(obj, &ourrela,
    662 				    dodebug) < 0)
    663 					ok = 0;
    664 			}
    665 		}
    666 		if (obj->rela != NULL) {
    667 			/* Process the non-PLT relocations. */
    668 			const Elf_Rela *rela;
    669 			for (rela = obj->rela; rela < obj->relalim; ++rela) {
    670 				if (_rtld_relocate_nonplt_object(obj, rela,
    671 				    dodebug) < 0)
    672 					ok = 0;
    673 			}
    674 		}
    675 		if (obj->textrel) {	/* Re-protected the text segment. */
    676 			if (mprotect(obj->mapbase, obj->textsize,
    677 				     PROT_READ | PROT_EXEC) == -1) {
    678 				_rtld_error("%s: Cannot write-protect text "
    679 				    "segment: %s", obj->path, xstrerror(errno));
    680 				return -1;
    681 			}
    682 		}
    683 		/* Process the PLT relocations. */
    684 		if (obj->pltrel != NULL) {
    685 			const Elf_Rel  *rel;
    686 			for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
    687 				Elf_Rela        ourrela;
    688 				ourrela.r_info = rel->r_info;
    689 				ourrela.r_offset = rel->r_offset;
    690 				ourrela.r_addend =
    691 				    *(Elf_Word *)(obj->relocbase +
    692 				    rel->r_offset);
    693 				if (_rtld_relocate_plt_object(obj, &ourrela,
    694 				    NULL, bind_now, dodebug) < 0)
    695 					ok = 0;
    696 			}
    697 		}
    698 		if (obj->pltrela != NULL) {
    699 			const Elf_Rela *rela;
    700 			for (rela = obj->pltrela; rela < obj->pltrelalim;
    701 			    ++rela) {
    702 				if (_rtld_relocate_plt_object(obj, rela,
    703 				    NULL, bind_now, dodebug) < 0)
    704 					ok = 0;
    705 			}
    706 		}
    707 		if (!ok)
    708 			return -1;
    709 
    710 
    711 		/* Set some sanity-checking numbers in the Obj_Entry. */
    712 		obj->magic = RTLD_MAGIC;
    713 		obj->version = RTLD_VERSION;
    714 
    715 		/* Fill in the dynamic linker entry points. */
    716 		obj->dlopen = _rtld_dlopen;
    717 		obj->dlsym = _rtld_dlsym;
    718 		obj->dlerror = _rtld_dlerror;
    719 		obj->dlclose = _rtld_dlclose;
    720 		obj->dladdr = _rtld_dladdr;
    721 
    722 		/* Set the special PLTGOT entries. */
    723 		if (obj->pltgot != NULL) {
    724 #if defined(__i386__) || defined(__m68k__) || defined(__x86_64__)
    725 			obj->pltgot[1] = (Elf_Addr) obj;
    726 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    727 #endif
    728 #if defined(__alpha__)
    729 			/*
    730 			 * This function will be called to perform the
    731 			 * relocation.
    732 			 */
    733 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    734 			/* Identify this shared object */
    735 			obj->pltgot[3] = (Elf_Addr) obj;
    736 #endif
    737 #if defined(__mips__)
    738 			_rtld_relocate_mips_got(obj);
    739 
    740 			obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
    741 			/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
    742 			obj->pltgot[1] |= (Elf_Addr) obj;
    743 #endif
    744 #if defined(__powerpc__)
    745 			_rtld_setup_powerpc_plt(obj);
    746 #endif
    747 #if defined(__sparc__)
    748 #if defined(__arch64__) || defined(__sparc_v9__)
    749 			/*
    750 			 * On sparc64 we got troubles.
    751 			 *
    752 			 * Instructions are 4 bytes long.
    753 			 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
    754 			 * array entries.
    755 			 * Each PLT entry jumps to PLT0 to enter the dynamic
    756 			 * linker.
    757 			 * Loading an arbitrary 64-bit pointer takes 6
    758 			 * instructions and 2 registers.
    759 			 *
    760 			 * Somehow we need to issue a save to get a new stack
    761 			 * frame, load the address of the dynamic linker, and
    762 			 * jump there, in 8 instructions or less.
    763 			 *
    764 			 * Oh, we need to fill out both PLT0 and PLT1.
    765 			 */
    766 			{
    767 				Elf_Word *entry = (Elf_Word *)obj->pltgot;
    768 				extern void _rtld_install_plt __P((Elf_Word *,
    769 					Elf_Addr));
    770 				extern void _rtld_bind_start_0 __P((long,
    771 					long));
    772 				extern void _rtld_bind_start_1 __P((long,
    773 					long));
    774 
    775 				/* Install in entries 0 and 1 */
    776 				_rtld_install_plt(&entry[0],
    777 					(Elf_Addr)&_rtld_bind_start_0);
    778 				_rtld_install_plt(&entry[8],
    779 					(Elf_Addr)&_rtld_bind_start_1);
    780 
    781 				/*
    782 				 * Install the object reference in first slot
    783 				 * of entry 2.
    784 				 */
    785 				obj->pltgot[8] = (Elf_Addr) obj;
    786 			}
    787 #else
    788 			/*
    789 			 * PLTGOT is the PLT on the sparc.
    790 			 * The first entry holds the call the dynamic linker.
    791 			 * We construct a `call' sequence that transfers
    792 			 * to `_rtld_bind_start()'.
    793 			 * The second entry holds the object identification.
    794 			 * Note: each PLT entry is three words long.
    795 			 */
    796 #define SAVE	0x9de3bfc0	/* i.e. `save %sp,-64,%sp' */
    797 #define CALL	0x40000000
    798 #define NOP	0x01000000
    799 			obj->pltgot[0] = SAVE;
    800 			obj->pltgot[1] = CALL |
    801 			    ((Elf_Addr)&_rtld_bind_start -
    802 			     (Elf_Addr)&obj->pltgot[1]) >> 2;
    803 			obj->pltgot[2] = NOP;
    804 
    805 			obj->pltgot[3] = (Elf_Addr) obj;
    806 #endif /* __arch64__ */
    807 #endif /* __sparc__ */
    808 #if defined(__vax__)
    809 			obj->pltgot[1] = (Elf_Addr) obj;
    810 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    811 #endif
    812 		}
    813 	}
    814 
    815 	return 0;
    816 }
    817