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