Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.22
      1 /*	$NetBSD: reloc.c,v 1.22 1999/10/28 23:58:21 simonb 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 	const 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__) || defined(__m68k__)
    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__ || __m68k__ */
    221 
    222 #if defined(__alpha__)
    223 	case R_TYPE(REFQUAD):
    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 		    *where + rela->r_addend;
    231 		if (*where != tmp)
    232 			*where = tmp;
    233 		rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
    234 		    defobj->strtab + def->st_name, obj->path,
    235 		    (void *)*where, defobj->path));
    236 		break;
    237 #endif /* __alpha__ */
    238 
    239 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
    240 	case R_TYPE(GLOB_DAT):
    241 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    242 		    &defobj, false);
    243 		if (def == NULL)
    244 			return -1;
    245 
    246 		if (*where != (Elf_Addr)(defobj->relocbase + def->st_value))
    247 			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
    248 		rdbg(dodebug, ("GLOB_DAT %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(RELATIVE):
    254 		if (!dodebug ||
    255 		    (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
    256 		    (caddr_t)where >= (caddr_t)&_DYNAMIC) {
    257 			*where += (Elf_Addr)obj->relocbase;
    258 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    259 			    (void *)*where));
    260 		}
    261 		else
    262 			rdbg(dodebug, ("RELATIVE in %s stays at %p",
    263 			    obj->path, (void *)*where));
    264 		break;
    265 
    266 	case R_TYPE(COPY):
    267 		/*
    268 		 * These are deferred until all other relocations have
    269 		 * been done.  All we do here is make sure that the COPY
    270 		 * relocation is not in a shared library.  They are allowed
    271 		 * only in executable files.
    272 		 */
    273 		if (!obj->mainprog) {
    274 			_rtld_error(
    275 			"%s: Unexpected R_COPY relocation in shared library",
    276 			    obj->path);
    277 			return -1;
    278 		}
    279 		rdbg(dodebug, ("COPY (avoid in main)"));
    280 		break;
    281 #endif /* __i386__ || __alpha__ */
    282 
    283 #if defined(__mips__)
    284 	case R_TYPE(REL32):
    285 		/* 32-bit PC-relative reference */
    286 		def = obj->symtab + ELF_R_SYM(rela->r_info);
    287 
    288 		if (ELFDEFNNAME(ST_BIND)(def->st_info) == STB_LOCAL &&
    289 		  (ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_SECTION ||
    290 		   ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_NOTYPE)) {
    291 			*where += (Elf_Addr)obj->relocbase;
    292 			rdbg(dodebug, ("REL32 in %s --> %p", obj->path,
    293 			    (void *)*where));
    294 		} else {
    295 			/* XXX maybe do something re: bootstrapping? */
    296 			def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
    297 			    NULL, obj, &defobj, false);
    298 			if (def == NULL)
    299 				return -1;
    300 			*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    301 			rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
    302 			    defobj->strtab + def->st_name, obj->path,
    303 			    (void *)*where, defobj->path));
    304 		}
    305 		break;
    306 
    307 #endif /* __mips__ */
    308 
    309 #if defined(__powerpc__) || defined(__vax__)
    310 	case R_TYPE(32):	/* word32 S + A */
    311 	case R_TYPE(GLOB_DAT):	/* word32 S + A */
    312 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    313 		    &defobj, false);
    314 		if (def == NULL)
    315 			return -1;
    316 
    317 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    318 		    rela->r_addend);
    319 
    320 		if (*where != tmp)
    321 			*where = tmp;
    322 		rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
    323 		    defobj->strtab + def->st_name, obj->path,
    324 		    (void *)*where, defobj->path));
    325 		break;
    326 
    327 	case R_TYPE(COPY):
    328 		rdbg(dodebug, ("COPY"));
    329 		break;
    330 
    331 	case R_TYPE(JMP_SLOT):
    332 		rdbg(dodebug, ("JMP_SLOT"));
    333 		break;
    334 
    335 	case R_TYPE(RELATIVE):	/* word32 B + A */
    336 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    337 		if (*where != tmp)
    338 			*where = tmp;
    339 		rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    340 		    (void *)*where));
    341 		break;
    342 #endif /* __powerpc__ || __vax__ */
    343 
    344 #if defined(__vax__)
    345 	case R_TYPE(REL32):
    346 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    347 		    &defobj, false);
    348 		if (def == NULL)
    349 			return -1;
    350 
    351 		*where += (Elf_Addr)(defobj->relocbase + def->st_value
    352 			+ rela->r_addend);
    353 		rdbg(dodebug, ("32 %s in %s --> %p in %s",
    354 		    defobj->strtab + def->st_name, obj->path,
    355 		    (void *)*where, defobj->path));
    356 		break;
    357 #endif /* __vax__ */
    358 
    359 	default:
    360 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    361 		    &defobj, true);
    362 		rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    363 		    "addend = %p, contents = %p, symbol = %s",
    364 		    (u_long)ELF_R_SYM(rela->r_info),
    365 		    (u_long)ELF_R_TYPE(rela->r_info),
    366 		    (void *)rela->r_offset, (void *)rela->r_addend,
    367 		    (void *)*where,
    368 		    def ? defobj->strtab + def->st_name : "??"));
    369 		_rtld_error("%s: Unsupported relocation type %d"
    370 		    "in non-PLT relocations\n",
    371 		    obj->path, ELF_R_TYPE(rela->r_info));
    372 		return -1;
    373 	}
    374 	return 0;
    375 }
    376 
    377 
    378 
    379 int
    380 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
    381 	const Obj_Entry *obj;
    382 	const Elf_RelA *rela;
    383 	caddr_t *addrp;
    384 	bool bind_now;
    385 	bool dodebug;
    386 {
    387 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    388 	Elf_Addr new_value;
    389 
    390 	/* Fully resolve procedure addresses now */
    391 
    392 #if defined(__powerpc__)
    393 	return _rtld_reloc_powerpc_plt(obj, rela, bind_now);
    394 #endif
    395 
    396 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
    397     defined(__vax__)
    398 	if (bind_now || obj->pltgot == NULL) {
    399 		const Elf_Sym  *def;
    400 		const Obj_Entry *defobj;
    401 
    402 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    403 
    404 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    405 		    &defobj, true);
    406 		if (def == NULL)
    407 			return -1;
    408 
    409 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    410 		rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
    411 		    (int)bind_now,
    412 		    defobj->strtab + def->st_name,
    413 		    (void *)*where, (void *)new_value));
    414 	} else
    415 #endif /* __alpha__ || __i386__ || __m68k__ || __vax__ */
    416 	if (!obj->mainprog) {
    417 		/* Just relocate the GOT slots pointing into the PLT */
    418 		new_value = *where + (Elf_Addr)(obj->relocbase);
    419 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    420 		    (void *)*where));
    421 	} else {
    422 		return 0;
    423 	}
    424 	/*
    425          * Since this page is probably copy-on-write, let's not write
    426          * it unless we really really have to.
    427          */
    428 	if (*where != new_value)
    429 		*where = new_value;
    430 	if (addrp != NULL)
    431 		*addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
    432 	return 0;
    433 }
    434 #endif /* __sparc__ */
    435 
    436 caddr_t
    437 _rtld_bind(obj, reloff)
    438 	const Obj_Entry *obj;
    439 	Elf_Word reloff;
    440 {
    441 	const Elf_RelA *rela;
    442 	Elf_RelA        ourrela;
    443 	caddr_t		addr;
    444 
    445 	if (obj->pltrel != NULL) {
    446 		const Elf_Rel *rel;
    447 
    448 		rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
    449 		ourrela.r_info = rel->r_info;
    450 		ourrela.r_offset = rel->r_offset;
    451 		rela = &ourrela;
    452 	} else {
    453 		rela = (const Elf_RelA *)((caddr_t) obj->pltrela + reloff);
    454 	}
    455 
    456 	if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
    457 		_rtld_die();
    458 
    459 	return addr;
    460 }
    461 
    462 /*
    463  * Relocate newly-loaded shared objects.  The argument is a pointer to
    464  * the Obj_Entry for the first such object.  All objects from the first
    465  * to the end of the list of objects are relocated.  Returns 0 on success,
    466  * or -1 on failure.
    467  */
    468 int
    469 _rtld_relocate_objects(first, bind_now, dodebug)
    470 	Obj_Entry *first;
    471 	bool bind_now;
    472 	bool dodebug;
    473 {
    474 	Obj_Entry *obj;
    475 	int ok = 1;
    476 
    477 	for (obj = first; obj != NULL; obj = obj->next) {
    478 		if (obj->nbuckets == 0 || obj->nchains == 0 ||
    479 		    obj->buckets == NULL || obj->symtab == NULL ||
    480 		    obj->strtab == NULL) {
    481 			_rtld_error("%s: Shared object has no run-time"
    482 			    " symbol table", obj->path);
    483 			return -1;
    484 		}
    485 		rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
    486 		    "%ld/%ld plt rel/rela)",
    487 		    obj->path,
    488 		    (long)(obj->rellim - obj->rel),
    489 		    (long)(obj->relalim - obj->rela),
    490 		    (long)(obj->pltrellim - obj->pltrel),
    491 		    (long)(obj->pltrelalim - obj->pltrela)));
    492 
    493 		if (obj->textrel) {
    494 			/*
    495 			 * There are relocations to the write-protected text
    496 			 * segment.
    497 			 */
    498 			if (mprotect(obj->mapbase, obj->textsize,
    499 				PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
    500 				_rtld_error("%s: Cannot write-enable text "
    501 				    "segment: %s", obj->path, xstrerror(errno));
    502 				return -1;
    503 			}
    504 		}
    505 		if (obj->rel != NULL) {
    506 			/* Process the non-PLT relocations. */
    507 			const Elf_Rel  *rel;
    508 			for (rel = obj->rel; rel < obj->rellim; ++rel) {
    509 				Elf_RelA        ourrela;
    510 				ourrela.r_info = rel->r_info;
    511 				ourrela.r_offset = rel->r_offset;
    512 #if defined(__mips__)
    513 				/* rel->r_offset is not valid on mips? */
    514 				if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
    515 					ourrela.r_addend = 0;
    516 				else
    517 #endif
    518 					ourrela.r_addend =
    519 					    *(Elf_Word *)(obj->relocbase +
    520 					    rel->r_offset);
    521 
    522 				if (_rtld_relocate_nonplt_object(obj, &ourrela,
    523 				    dodebug) < 0)
    524 					ok = 0;
    525 			}
    526 		}
    527 		if (obj->rela != NULL) {
    528 			/* Process the non-PLT relocations. */
    529 			const Elf_RelA *rela;
    530 			for (rela = obj->rela; rela < obj->relalim; ++rela) {
    531 				if (_rtld_relocate_nonplt_object(obj, rela,
    532 				    dodebug) < 0)
    533 					ok = 0;
    534 			}
    535 		}
    536 		if (obj->textrel) {	/* Re-protected the text segment. */
    537 			if (mprotect(obj->mapbase, obj->textsize,
    538 				     PROT_READ | PROT_EXEC) == -1) {
    539 				_rtld_error("%s: Cannot write-protect text "
    540 				    "segment: %s", obj->path, xstrerror(errno));
    541 				return -1;
    542 			}
    543 		}
    544 		/* Process the PLT relocations. */
    545 		if (obj->pltrel != NULL) {
    546 			const Elf_Rel  *rel;
    547 			for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
    548 				Elf_RelA        ourrela;
    549 				ourrela.r_info = rel->r_info;
    550 				ourrela.r_offset = rel->r_offset;
    551 				ourrela.r_addend =
    552 				    *(Elf_Word *)(obj->relocbase +
    553 				    rel->r_offset);
    554 				if (_rtld_relocate_plt_object(obj, &ourrela,
    555 				    NULL, bind_now, dodebug) < 0)
    556 					ok = 0;
    557 			}
    558 		}
    559 		if (obj->pltrela != NULL) {
    560 			const Elf_RelA *rela;
    561 			for (rela = obj->pltrela; rela < obj->pltrelalim;
    562 			    ++rela) {
    563 				if (_rtld_relocate_plt_object(obj, rela,
    564 				    NULL, bind_now, dodebug) < 0)
    565 					ok = 0;
    566 			}
    567 		}
    568 		if (!ok)
    569 			return -1;
    570 
    571 
    572 		/* Set some sanity-checking numbers in the Obj_Entry. */
    573 		obj->magic = RTLD_MAGIC;
    574 		obj->version = RTLD_VERSION;
    575 
    576 		/* Fill in the dynamic linker entry points. */
    577 		obj->dlopen = _rtld_dlopen;
    578 		obj->dlsym = _rtld_dlsym;
    579 		obj->dlerror = _rtld_dlerror;
    580 		obj->dlclose = _rtld_dlclose;
    581 
    582 		/* Set the special PLTGOT entries. */
    583 		if (obj->pltgot != NULL) {
    584 #if defined(__i386__) || defined(__m68k__)
    585 			obj->pltgot[1] = (Elf_Addr) obj;
    586 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    587 #endif
    588 #if defined(__alpha__)
    589 			/*
    590 			 * This function will be called to perform the
    591 			 * relocation.
    592 			 */
    593 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    594 			/* Identify this shared object */
    595 			obj->pltgot[3] = (Elf_Addr) obj;
    596 #endif
    597 #if defined(__mips__)
    598 			_rtld_relocate_mips_got(obj);
    599 
    600 			obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
    601 			/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
    602 			obj->pltgot[1] |= (Elf_Addr) obj;
    603 #endif
    604 #if defined(__powerpc__)
    605 			_rtld_setup_powerpc_plt(obj);
    606 #endif
    607 #if defined(__sparc__)
    608 			/*
    609 			 * PLTGOT is the PLT on the sparc.
    610 			 * The first entry holds the call the dynamic linker.
    611 			 * We construct a `call' sequence that transfers
    612 			 * to `_rtld_bind_start()'.
    613 			 * The second entry holds the object identification.
    614 			 * Note: each PLT entry is three words long.
    615 			 */
    616 #define SAVE	0x9de3bfc0	/* i.e. `save %sp,-64,%sp' */
    617 #define CALL	0x40000000
    618 #define NOP	0x01000000
    619 			obj->pltgot[0] = SAVE;
    620 			obj->pltgot[1] = CALL |
    621 			    ((Elf_Addr)&_rtld_bind_start -
    622 			     (Elf_Addr)&obj->pltgot[1]) >> 2;
    623 			obj->pltgot[2] = NOP;
    624 
    625 			obj->pltgot[3] = (Elf_Addr) obj;
    626 #endif
    627 #if defined(__vax__)
    628 			obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
    629 			obj->pltgot[1] = (Elf_Addr) obj;
    630 #endif
    631 		}
    632 	}
    633 
    634 	return 0;
    635 }
    636