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