Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.24
      1 /*	$NetBSD: reloc.c,v 1.24 1999/12/08 08:47:10 itohy 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__ */
    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(__vax__)
    392 	case R_TYPE(REL32):
    393 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    394 		    &defobj, false);
    395 		if (def == NULL)
    396 			return -1;
    397 
    398 		*where += (Elf_Addr)(defobj->relocbase + def->st_value
    399 			+ rela->r_addend);
    400 		rdbg(dodebug, ("32 %s in %s --> %p in %s",
    401 		    defobj->strtab + def->st_name, obj->path,
    402 		    (void *)*where, defobj->path));
    403 		break;
    404 #endif /* __vax__ */
    405 
    406 	default:
    407 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    408 		    &defobj, true);
    409 		rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    410 		    "addend = %p, contents = %p, symbol = %s",
    411 		    (u_long)ELF_R_SYM(rela->r_info),
    412 		    (u_long)ELF_R_TYPE(rela->r_info),
    413 		    (void *)rela->r_offset, (void *)rela->r_addend,
    414 		    (void *)*where,
    415 		    def ? defobj->strtab + def->st_name : "??"));
    416 		_rtld_error("%s: Unsupported relocation type %d"
    417 		    "in non-PLT relocations\n",
    418 		    obj->path, ELF_R_TYPE(rela->r_info));
    419 		return -1;
    420 	}
    421 	return 0;
    422 }
    423 
    424 
    425 
    426 int
    427 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
    428 	Obj_Entry *obj;
    429 	const Elf_RelA *rela;
    430 	caddr_t *addrp;
    431 	bool bind_now;
    432 	bool dodebug;
    433 {
    434 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    435 	Elf_Addr new_value;
    436 
    437 	/* Fully resolve procedure addresses now */
    438 
    439 #if defined(__powerpc__)
    440 	return _rtld_reloc_powerpc_plt(obj, rela, bind_now);
    441 #endif
    442 
    443 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
    444     defined(__vax__)
    445 	if (bind_now || obj->pltgot == NULL) {
    446 		const Elf_Sym  *def;
    447 		const Obj_Entry *defobj;
    448 
    449 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    450 
    451 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    452 		    &defobj, true);
    453 		if (def == NULL)
    454 			return -1;
    455 
    456 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    457 		rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
    458 		    (int)bind_now,
    459 		    defobj->strtab + def->st_name,
    460 		    (void *)*where, (void *)new_value));
    461 	} else
    462 #endif /* __alpha__ || __i386__ || __m68k__ || __vax__ */
    463 	if (!obj->mainprog) {
    464 		/* Just relocate the GOT slots pointing into the PLT */
    465 		new_value = *where + (Elf_Addr)(obj->relocbase);
    466 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    467 		    (void *)*where));
    468 	} else {
    469 		return 0;
    470 	}
    471 	/*
    472          * Since this page is probably copy-on-write, let's not write
    473          * it unless we really really have to.
    474          */
    475 	if (*where != new_value)
    476 		*where = new_value;
    477 	if (addrp != NULL)
    478 		*addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
    479 	return 0;
    480 }
    481 #endif /* __sparc__ */
    482 
    483 caddr_t
    484 _rtld_bind(obj, reloff)
    485 	Obj_Entry *obj;
    486 	Elf_Word reloff;
    487 {
    488 	const Elf_RelA *rela;
    489 	Elf_RelA        ourrela;
    490 	caddr_t		addr;
    491 
    492 	if (obj->pltrel != NULL) {
    493 		const Elf_Rel *rel;
    494 
    495 		rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
    496 		ourrela.r_info = rel->r_info;
    497 		ourrela.r_offset = rel->r_offset;
    498 		rela = &ourrela;
    499 	} else {
    500 		rela = (const Elf_RelA *)((caddr_t) obj->pltrela + reloff);
    501 	}
    502 
    503 	if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
    504 		_rtld_die();
    505 
    506 	return addr;
    507 }
    508 
    509 /*
    510  * Relocate newly-loaded shared objects.  The argument is a pointer to
    511  * the Obj_Entry for the first such object.  All objects from the first
    512  * to the end of the list of objects are relocated.  Returns 0 on success,
    513  * or -1 on failure.
    514  */
    515 int
    516 _rtld_relocate_objects(first, bind_now, dodebug)
    517 	Obj_Entry *first;
    518 	bool bind_now;
    519 	bool dodebug;
    520 {
    521 	Obj_Entry *obj;
    522 	int ok = 1;
    523 
    524 	for (obj = first; obj != NULL; obj = obj->next) {
    525 		if (obj->nbuckets == 0 || obj->nchains == 0 ||
    526 		    obj->buckets == NULL || obj->symtab == NULL ||
    527 		    obj->strtab == NULL) {
    528 			_rtld_error("%s: Shared object has no run-time"
    529 			    " symbol table", obj->path);
    530 			return -1;
    531 		}
    532 		rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
    533 		    "%ld/%ld plt rel/rela)",
    534 		    obj->path,
    535 		    (long)(obj->rellim - obj->rel),
    536 		    (long)(obj->relalim - obj->rela),
    537 		    (long)(obj->pltrellim - obj->pltrel),
    538 		    (long)(obj->pltrelalim - obj->pltrela)));
    539 
    540 		if (obj->textrel) {
    541 			/*
    542 			 * There are relocations to the write-protected text
    543 			 * segment.
    544 			 */
    545 			if (mprotect(obj->mapbase, obj->textsize,
    546 				PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
    547 				_rtld_error("%s: Cannot write-enable text "
    548 				    "segment: %s", obj->path, xstrerror(errno));
    549 				return -1;
    550 			}
    551 		}
    552 		if (obj->rel != NULL) {
    553 			/* Process the non-PLT relocations. */
    554 			const Elf_Rel  *rel;
    555 			for (rel = obj->rel; rel < obj->rellim; ++rel) {
    556 				Elf_RelA        ourrela;
    557 				ourrela.r_info = rel->r_info;
    558 				ourrela.r_offset = rel->r_offset;
    559 #if defined(__mips__)
    560 				/* rel->r_offset is not valid on mips? */
    561 				if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
    562 					ourrela.r_addend = 0;
    563 				else
    564 #endif
    565 					ourrela.r_addend =
    566 					    *(Elf_Word *)(obj->relocbase +
    567 					    rel->r_offset);
    568 
    569 				if (_rtld_relocate_nonplt_object(obj, &ourrela,
    570 				    dodebug) < 0)
    571 					ok = 0;
    572 			}
    573 		}
    574 		if (obj->rela != NULL) {
    575 			/* Process the non-PLT relocations. */
    576 			const Elf_RelA *rela;
    577 			for (rela = obj->rela; rela < obj->relalim; ++rela) {
    578 				if (_rtld_relocate_nonplt_object(obj, rela,
    579 				    dodebug) < 0)
    580 					ok = 0;
    581 			}
    582 		}
    583 		if (obj->textrel) {	/* Re-protected the text segment. */
    584 			if (mprotect(obj->mapbase, obj->textsize,
    585 				     PROT_READ | PROT_EXEC) == -1) {
    586 				_rtld_error("%s: Cannot write-protect text "
    587 				    "segment: %s", obj->path, xstrerror(errno));
    588 				return -1;
    589 			}
    590 		}
    591 		/* Process the PLT relocations. */
    592 		if (obj->pltrel != NULL) {
    593 			const Elf_Rel  *rel;
    594 			for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
    595 				Elf_RelA        ourrela;
    596 				ourrela.r_info = rel->r_info;
    597 				ourrela.r_offset = rel->r_offset;
    598 				ourrela.r_addend =
    599 				    *(Elf_Word *)(obj->relocbase +
    600 				    rel->r_offset);
    601 				if (_rtld_relocate_plt_object(obj, &ourrela,
    602 				    NULL, bind_now, dodebug) < 0)
    603 					ok = 0;
    604 			}
    605 		}
    606 		if (obj->pltrela != NULL) {
    607 			const Elf_RelA *rela;
    608 			for (rela = obj->pltrela; rela < obj->pltrelalim;
    609 			    ++rela) {
    610 				if (_rtld_relocate_plt_object(obj, rela,
    611 				    NULL, bind_now, dodebug) < 0)
    612 					ok = 0;
    613 			}
    614 		}
    615 		if (!ok)
    616 			return -1;
    617 
    618 
    619 		/* Set some sanity-checking numbers in the Obj_Entry. */
    620 		obj->magic = RTLD_MAGIC;
    621 		obj->version = RTLD_VERSION;
    622 
    623 		/* Fill in the dynamic linker entry points. */
    624 		obj->dlopen = _rtld_dlopen;
    625 		obj->dlsym = _rtld_dlsym;
    626 		obj->dlerror = _rtld_dlerror;
    627 		obj->dlclose = _rtld_dlclose;
    628 
    629 		/* Set the special PLTGOT entries. */
    630 		if (obj->pltgot != NULL) {
    631 #if defined(__i386__) || defined(__m68k__)
    632 			obj->pltgot[1] = (Elf_Addr) obj;
    633 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    634 #endif
    635 #if defined(__alpha__)
    636 			/*
    637 			 * This function will be called to perform the
    638 			 * relocation.
    639 			 */
    640 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    641 			/* Identify this shared object */
    642 			obj->pltgot[3] = (Elf_Addr) obj;
    643 #endif
    644 #if defined(__mips__)
    645 			_rtld_relocate_mips_got(obj);
    646 
    647 			obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
    648 			/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
    649 			obj->pltgot[1] |= (Elf_Addr) obj;
    650 #endif
    651 #if defined(__powerpc__)
    652 			_rtld_setup_powerpc_plt(obj);
    653 #endif
    654 #if defined(__sparc__)
    655 			/*
    656 			 * PLTGOT is the PLT on the sparc.
    657 			 * The first entry holds the call the dynamic linker.
    658 			 * We construct a `call' sequence that transfers
    659 			 * to `_rtld_bind_start()'.
    660 			 * The second entry holds the object identification.
    661 			 * Note: each PLT entry is three words long.
    662 			 */
    663 #define SAVE	0x9de3bfc0	/* i.e. `save %sp,-64,%sp' */
    664 #define CALL	0x40000000
    665 #define NOP	0x01000000
    666 			obj->pltgot[0] = SAVE;
    667 			obj->pltgot[1] = CALL |
    668 			    ((Elf_Addr)&_rtld_bind_start -
    669 			     (Elf_Addr)&obj->pltgot[1]) >> 2;
    670 			obj->pltgot[2] = NOP;
    671 
    672 			obj->pltgot[3] = (Elf_Addr) obj;
    673 #endif
    674 #if defined(__vax__)
    675 			obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
    676 			obj->pltgot[1] = (Elf_Addr) obj;
    677 #endif
    678 		}
    679 	}
    680 
    681 	return 0;
    682 }
    683