Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.9
      1  1.9        pk /*	$NetBSD: reloc.c,v 1.9 1999/02/22 17:06:11 pk Exp $	*/
      2  1.1       cgd 
      3  1.1       cgd /*
      4  1.1       cgd  * Copyright 1996 John D. Polstra.
      5  1.1       cgd  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  1.1       cgd  * All rights reserved.
      7  1.1       cgd  *
      8  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1       cgd  * modification, are permitted provided that the following conditions
     10  1.1       cgd  * are met:
     11  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1       cgd  *    must display the following acknowledgement:
     18  1.1       cgd  *      This product includes software developed by John Polstra.
     19  1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20  1.1       cgd  *    derived from this software without specific prior written permission.
     21  1.1       cgd  *
     22  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1       cgd  */
     33  1.1       cgd 
     34  1.1       cgd /*
     35  1.1       cgd  * Dynamic linker for ELF.
     36  1.1       cgd  *
     37  1.1       cgd  * John Polstra <jdp (at) polstra.com>.
     38  1.1       cgd  */
     39  1.1       cgd 
     40  1.1       cgd #include <err.h>
     41  1.1       cgd #include <errno.h>
     42  1.1       cgd #include <fcntl.h>
     43  1.1       cgd #include <stdarg.h>
     44  1.1       cgd #include <stdio.h>
     45  1.1       cgd #include <stdlib.h>
     46  1.1       cgd #include <string.h>
     47  1.1       cgd #include <unistd.h>
     48  1.1       cgd #include <sys/types.h>
     49  1.1       cgd #include <sys/mman.h>
     50  1.1       cgd #include <dirent.h>
     51  1.1       cgd 
     52  1.1       cgd #include "debug.h"
     53  1.1       cgd #include "rtld.h"
     54  1.1       cgd 
     55  1.4  christos #if defined(__alpha__) || defined(__powerpc__) || defined(__i386__)
     56  1.4  christos /*
     57  1.4  christos  * XXX: These don't work for the alpha and i386; don't know about powerpc
     58  1.4  christos  *	The alpha and the i386 avoid the problem by compiling everything PIC.
     59  1.4  christos  *	These relocation are supposed to be writing the address of the
     60  1.4  christos  *	function to be called on the bss.rel or bss.rela segment, but:
     61  1.4  christos  *		- st_size == 0
     62  1.4  christos  *		- on the i386 at least the call instruction is a direct call
     63  1.4  christos  *		  not an indirect call.
     64  1.4  christos  */
     65  1.1       cgd static int
     66  1.1       cgd _rtld_do_copy_relocation(
     67  1.1       cgd     const Obj_Entry *dstobj,
     68  1.1       cgd     const Elf_RelA *rela)
     69  1.1       cgd {
     70  1.1       cgd     void *dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
     71  1.1       cgd     const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
     72  1.1       cgd     const char *name = dstobj->strtab + dstsym->st_name;
     73  1.1       cgd     unsigned long hash = _rtld_elf_hash(name);
     74  1.1       cgd     size_t size = dstsym->st_size;
     75  1.1       cgd     const void *srcaddr;
     76  1.1       cgd     const Elf_Sym *srcsym;
     77  1.1       cgd     Obj_Entry *srcobj;
     78  1.1       cgd 
     79  1.1       cgd     for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next)
     80  1.1       cgd 	if ((srcsym = _rtld_symlook_obj(name, hash, srcobj, false)) != NULL)
     81  1.1       cgd 	    break;
     82  1.1       cgd 
     83  1.1       cgd     if (srcobj == NULL) {
     84  1.1       cgd 	_rtld_error("Undefined symbol \"%s\" referenced from COPY"
     85  1.1       cgd 	      " relocation in %s", name, dstobj->path);
     86  1.1       cgd 	return -1;
     87  1.1       cgd     }
     88  1.1       cgd 
     89  1.1       cgd     srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
     90  1.1       cgd     memcpy(dstaddr, srcaddr, size);
     91  1.6  christos #ifdef RTLD_DEBUG_RELOC
     92  1.6  christos     dbg("COPY %s %s %s --> src=%p dst=%p *dst= %p size %d",
     93  1.6  christos 	dstobj->path, srcobj->path, name, (void *)srcaddr, (void *)dstaddr,
     94  1.6  christos 	(void *)*(long *)dstaddr, size);
     95  1.6  christos #endif
     96  1.1       cgd     return 0;
     97  1.1       cgd }
     98  1.4  christos #endif /* __alpha__ || __powerpc__ || __i386__ */
     99  1.1       cgd 
    100  1.1       cgd /*
    102  1.1       cgd  * Process the special R_xxx_COPY relocations in the main program.  These
    103  1.1       cgd  * copy data from a shared object into a region in the main program's BSS
    104  1.1       cgd  * segment.
    105  1.1       cgd  *
    106  1.1       cgd  * Returns 0 on success, -1 on failure.
    107  1.1       cgd  */
    108  1.1       cgd int
    109  1.1       cgd _rtld_do_copy_relocations(
    110  1.1       cgd     const Obj_Entry *dstobj)
    111  1.1       cgd {
    112  1.1       cgd     assert(dstobj->mainprog);	/* COPY relocations are invalid elsewhere */
    113  1.4  christos 
    114  1.1       cgd #if defined(__alpha__) || defined(__powerpc__) || defined(__i386__)
    115  1.1       cgd     if (dstobj->rel != NULL) {
    116  1.1       cgd 	const Elf_Rel *rel;
    117  1.1       cgd 	for (rel = dstobj->rel;  rel < dstobj->rellim;  ++rel) {
    118  1.1       cgd 	    if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
    119  1.1       cgd 		Elf_RelA ourrela;
    120  1.1       cgd 		ourrela.r_info = rel->r_info;
    121  1.1       cgd 		ourrela.r_offset = rel->r_offset;
    122  1.1       cgd 		ourrela.r_addend = 0;
    123  1.1       cgd 		if (_rtld_do_copy_relocation(dstobj, &ourrela) < 0)
    124  1.1       cgd 		    return -1;
    125  1.1       cgd 	    }
    126  1.1       cgd 	}
    127  1.1       cgd     }
    128  1.1       cgd 
    129  1.1       cgd     if (dstobj->rela != NULL) {
    130  1.1       cgd 	const Elf_RelA *rela;
    131  1.1       cgd 	for (rela = dstobj->rela;  rela < dstobj->relalim;  ++rela) {
    132  1.1       cgd 	    if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
    133  1.1       cgd 		if (_rtld_do_copy_relocation(dstobj, rela) < 0)
    134  1.1       cgd 		    return -1;
    135  1.1       cgd 	    }
    136  1.1       cgd 	}
    137  1.4  christos     }
    138  1.1       cgd #endif /* __alpha__ || __powerpc__ || __i386__ */
    139  1.1       cgd 
    140  1.1       cgd     return 0;
    141  1.9        pk }
    142  1.9        pk 
    143  1.9        pk #ifdef __sparc__
    144  1.9        pk /*
    145  1.9        pk  * The following table holds for each relocation type:
    146  1.9        pk  *	- the width in bits of the memory location the relocation
    147  1.9        pk  *	  applies to (not currently used)
    148  1.9        pk  *	- the number of bits the relocation value must be shifted to the
    149  1.9        pk  *	  right (i.e. discard least significant bits) to fit into
    150  1.9        pk  *	  the appropriate field in the instruction word.
    151  1.9        pk  *	- flags indicating whether
    152  1.9        pk  *		* the relocation involves a symbol
    153  1.9        pk  *		* the relocation is relative to the current position
    154  1.9        pk  *		* the relocation is for a GOT entry
    155  1.9        pk  *		* the relocation is relative to the load address
    156  1.9        pk  *
    157  1.9        pk  */
    158  1.9        pk #define _RF_S		0x80000000		/* Resolve symbol */
    159  1.9        pk #define _RF_A		0x40000000		/* Use addend */
    160  1.9        pk #define _RF_P		0x20000000		/* Location relative */
    161  1.9        pk #define _RF_G		0x10000000		/* GOT offset */
    162  1.9        pk #define _RF_B		0x08000000		/* Load address relative */
    163  1.9        pk #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
    164  1.9        pk #define _RF_RS(s)	( (s) & 0xff)		/* right shift */
    165  1.9        pk static int reloc_target_flags[] = {
    166  1.9        pk 	0,							/* NONE */
    167  1.9        pk 	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
    168  1.9        pk 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
    169  1.9        pk 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
    170  1.9        pk 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
    171  1.9        pk 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
    172  1.9        pk 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
    173  1.9        pk 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
    174  1.9        pk 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
    175  1.9        pk 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
    176  1.9        pk 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
    177  1.9        pk 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
    178  1.9        pk 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
    179  1.9        pk 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
    180  1.9        pk 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
    181  1.9        pk 	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
    182  1.9        pk 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
    183  1.9        pk 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
    184  1.9        pk 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
    185  1.9        pk 				_RF_SZ(32) | _RF_RS(0),		/* COPY */
    186  1.9        pk 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_DAT */
    187  1.9        pk 				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
    188  1.9        pk 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELATIVE */
    189  1.9        pk 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
    190  1.9        pk 
    191  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
    192  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* HIPLT22 */
    193  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
    194  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
    195  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* PCPLT22 */
    196  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
    197  1.9        pk 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* 10 */
    198  1.9        pk 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* 11 */
    199  1.9        pk 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* 64 */
    200  1.9        pk 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
    201  1.9        pk 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* HH22 */
    202  1.9        pk 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* HM10 */
    203  1.9        pk 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* LM22 */
    204  1.9        pk 	_RF_S|_RF_A|_RF_P|/*unknown*/	_RF_SZ(32) | _RF_RS(0),	/* WDISP16 */
    205  1.9        pk 	_RF_S|_RF_A|_RF_P|/*unknown*/	_RF_SZ(32) | _RF_RS(0),	/* WDISP19 */
    206  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
    207  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* 7 */
    208  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* 5 */
    209  1.9        pk 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* 6 */
    210  1.9        pk };
    211  1.9        pk 
    212  1.9        pk #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
    213  1.9        pk #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
    214  1.9        pk #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
    215  1.9        pk #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
    216  1.9        pk 
    217  1.9        pk static int reloc_target_bitmask[] = {
    218  1.9        pk #define _BM(x)	(~(-(1ULL << (x))))
    219  1.9        pk 	0,				/* NONE */
    220  1.9        pk 	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
    221  1.9        pk 	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
    222  1.9        pk 	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
    223  1.9        pk 	_BM(22), _BM(22),		/* HI22, _22 */
    224  1.9        pk 	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
    225  1.9        pk 	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
    226  1.9        pk 	_BM(10), _BM(22),		/* _PC10, _PC22 */
    227  1.9        pk 	_BM(30), 0,			/* _WPLT30, _COPY */
    228  1.9        pk 	-1, -1, _BM(22),		/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
    229  1.9        pk 	_BM(32), _BM(32),		/* _UA32, PLT32 */
    230  1.9        pk 	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
    231  1.9        pk 	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
    232  1.9        pk 	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
    233  1.9        pk 	_BM(10), _BM(22),		/* _OLO10, _HH22 */
    234  1.9        pk 	_BM(10), _BM(22),		/* _HM10, _LM22 */
    235  1.9        pk 	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
    236  1.9        pk 	-1,				/* GLOB_JMP */
    237  1.9        pk 	_BM(7), _BM(5), _BM(6)		/* _7, _5, _6 */
    238  1.9        pk #undef _BM
    239  1.9        pk };
    240  1.9        pk #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
    241  1.9        pk 
    242  1.9        pk static int
    243  1.9        pk _rtld_relocate_nonplt_object(
    244  1.9        pk 	const Obj_Entry *obj,
    245  1.9        pk 	const Elf_RelA *rela)
    246  1.9        pk {
    247  1.9        pk 	Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    248  1.9        pk 	Elf_Word type, value, mask;
    249  1.9        pk 
    250  1.9        pk 	type = ELF_R_TYPE(rela->r_info);
    251  1.9        pk 	if (type == R_TYPE(NONE))
    252  1.9        pk 		return (0);
    253  1.9        pk 
    254  1.9        pk 	/*
    255  1.9        pk 	 * We use the fact that relocation types are an `enum'
    256  1.9        pk 	 * Note: R_SPARC_6 is currently numerically largest.
    257  1.9        pk 	 */
    258  1.9        pk 	if (type > R_TYPE(6))
    259  1.9        pk 		return (-1);
    260  1.9        pk 
    261  1.9        pk 	value = rela->r_addend;
    262  1.9        pk 	if (RELOC_RESOLVE_SYMBOL(type)) {
    263  1.9        pk 		const Elf_Sym *def;
    264  1.9        pk 		const Obj_Entry *defobj;
    265  1.9        pk 
    266  1.9        pk 		/* Find the symbol */
    267  1.9        pk 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
    268  1.9        pk 					NULL, obj, &defobj, false);
    269  1.9        pk 		if (def == NULL)
    270  1.9        pk 			return (-1);
    271  1.9        pk 
    272  1.9        pk 		/* Add in the symbol's absolute address */
    273  1.9        pk 		value += (Elf_Word)(defobj->relocbase + def->st_value);
    274  1.9        pk 	}
    275  1.9        pk 
    276  1.9        pk 	if (RELOC_PC_RELATIVE(type)) {
    277  1.9        pk 		value -= (Elf_Word)where;
    278  1.9        pk 	}
    279  1.9        pk 
    280  1.9        pk 	mask = RELOC_VALUE_BITMASK(type);
    281  1.9        pk 	value >>= RELOC_VALUE_RIGHTSHIFT(type);
    282  1.9        pk 	value &= mask;
    283  1.9        pk 
    284  1.9        pk 	/* We ignore alignment restrictions here */
    285  1.9        pk 	*where &= ~mask;
    286  1.9        pk 	*where |= value;
    287  1.9        pk 	return (0);
    288  1.9        pk }
    289  1.9        pk 
    290  1.9        pk static int
    291  1.9        pk __rtld_relocate_plt_object(
    292  1.9        pk 	const Obj_Entry *obj,
    293  1.9        pk 	const Elf_RelA *rela,
    294  1.9        pk 	bool bind_now,
    295  1.9        pk 	caddr_t *addrp)
    296  1.9        pk {
    297  1.9        pk 	const Elf_Sym *def;
    298  1.9        pk 	const Obj_Entry *defobj;
    299  1.9        pk 	Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    300  1.9        pk 	Elf_Addr value;
    301  1.9        pk 
    302  1.9        pk 	if (bind_now == 0 && obj->pltgot != NULL)
    303  1.9        pk 		return (0);
    304  1.9        pk 
    305  1.9        pk 	/* Fully resolve procedure addresses now */
    306  1.9        pk 
    307  1.9        pk 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    308  1.9        pk 
    309  1.9        pk 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
    310  1.9        pk 				NULL, obj, &defobj, true);
    311  1.9        pk 	if (def == NULL)
    312  1.9        pk 		return (-1);
    313  1.9        pk 
    314  1.9        pk 	value = (Elf_Addr) (defobj->relocbase + def->st_value);
    315  1.9        pk 
    316  1.9        pk #ifdef RTLD_DEBUG_RELOC
    317  1.9        pk 	dbg("bind now %d/fixup in %s --> old=%p new=%p",
    318  1.9        pk 		(int)bind_now,
    319  1.9        pk 		defobj->strtab + def->st_name,
    320  1.9        pk 		(void *)*where, (void *)value);
    321  1.9        pk #endif
    322  1.9        pk 
    323  1.9        pk 	/*
    324  1.9        pk 	 * At the PLT entry pointed at by `where', we now construct
    325  1.9        pk 	 * a direct transfer to the now fully resolved function
    326  1.9        pk 	 * address.  The resulting code in the jump slot is:
    327  1.9        pk 	 *
    328  1.9        pk 	 *	sethi	%hi(addr), %g1
    329  1.9        pk 	 *	jmp	%g1+%lo(addr)
    330  1.9        pk 	 *	nop	! delay slot
    331  1.9        pk 	 */
    332  1.9        pk #define SETHI	0x03000000
    333  1.9        pk #define JMP	0x81c06000
    334  1.9        pk #define NOP	0x01000000
    335  1.9        pk 	where[0] = SETHI | ((value >> 10) & 0x003fffff);
    336  1.9        pk 	where[1] = JMP   | (value & 0x000003ff);
    337  1.9        pk 	where[2] = NOP;
    338  1.9        pk 
    339  1.9        pk 	if (addrp != NULL)
    340  1.9        pk 		*addrp = (caddr_t)value;
    341  1.9        pk 
    342  1.9        pk 	return (0);
    343  1.9        pk }
    344  1.9        pk 
    345  1.9        pk #define _rtld_relocate_plt_object(obj, rela, bind_now) \
    346  1.9        pk 	__rtld_relocate_plt_object(obj, rela, bind_now, NULL)
    347  1.9        pk 
    348  1.9        pk caddr_t
    349  1.9        pk _rtld_bind(
    350  1.9        pk 	const Obj_Entry *obj,
    351  1.9        pk 	Elf_Word reloff)
    352  1.9        pk {
    353  1.9        pk 	const Elf_RelA *rela;
    354  1.9        pk 	Elf_RelA ourrela;
    355  1.9        pk 	caddr_t addr;
    356  1.9        pk 
    357  1.9        pk 	if (obj->pltrel != NULL) {
    358  1.9        pk 		const Elf_Rel *rel;
    359  1.9        pk 
    360  1.9        pk 		rel = (const Elf_Rel *) ((caddr_t) obj->pltrel + reloff);
    361  1.9        pk 		ourrela.r_info = rel->r_info;
    362  1.9        pk 		ourrela.r_offset = rel->r_offset;
    363  1.9        pk 		rela = &ourrela;
    364  1.9        pk 	} else {
    365  1.9        pk 		rela = (const Elf_RelA *) ((caddr_t) obj->pltrela + reloff);
    366  1.9        pk 	}
    367  1.9        pk 
    368  1.9        pk 	if (__rtld_relocate_plt_object(obj, rela, true, &addr) < 0)
    369  1.9        pk 		_rtld_die();
    370  1.9        pk 
    371  1.9        pk 	return (addr);
    372  1.9        pk 	return *(caddr_t *)(obj->relocbase + rela->r_offset);
    373  1.9        pk }
    374  1.9        pk 
    375  1.9        pk #else /* __sparc__ */
    376  1.1       cgd 
    377  1.1       cgd static int
    378  1.1       cgd _rtld_relocate_nonplt_object(
    379  1.1       cgd     const Obj_Entry *obj,
    380  1.1       cgd     const Elf_RelA *rela)
    381  1.1       cgd {
    382  1.1       cgd     Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    383  1.1       cgd 
    384  1.1       cgd     switch (ELF_R_TYPE(rela->r_info)) {
    385  1.1       cgd 
    386  1.1       cgd     case R_TYPE(NONE):
    387  1.1       cgd 	break;
    388  1.1       cgd 
    389  1.4  christos #ifdef __i386__
    390  1.1       cgd     case R_TYPE(GOT32): {
    391  1.1       cgd 	const Elf_Sym *def;
    392  1.1       cgd 	const Obj_Entry *defobj;
    393  1.1       cgd 
    394  1.1       cgd 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, false);
    395  1.1       cgd 	if (def == NULL)
    396  1.1       cgd 	    return -1;
    397  1.6  christos 
    398  1.6  christos 	if (*where != (Elf_Addr) (defobj->relocbase + def->st_value))
    399  1.6  christos 	    *where = (Elf_Addr) (defobj->relocbase + def->st_value);
    400  1.6  christos #ifdef RTLD_DEBUG_RELOC
    401  1.6  christos 	dbg("GOT32 %s in %s --> %p in %s",
    402  1.6  christos 	    defobj->strtab + def->st_name, obj->path,
    403  1.6  christos 	    (void *)*where, defobj->path);
    404  1.1       cgd #endif
    405  1.1       cgd 	break;
    406  1.1       cgd     }
    407  1.4  christos 
    408  1.1       cgd     case R_TYPE(PC32):
    409  1.1       cgd 	/*
    410  1.1       cgd 	 * I don't think the dynamic linker should ever see this
    411  1.1       cgd 	 * type of relocation.  But the binutils-2.6 tools sometimes
    412  1.1       cgd 	 * generate it.
    413  1.1       cgd 	 */
    414  1.1       cgd     {
    415  1.1       cgd 	const Elf_Sym *def;
    416  1.1       cgd 	const Obj_Entry *defobj;
    417  1.1       cgd 
    418  1.1       cgd 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, false);
    419  1.1       cgd 	if (def == NULL)
    420  1.1       cgd 	    return -1;
    421  1.1       cgd 
    422  1.1       cgd 	*where += (Elf_Addr) (defobj->relocbase + def->st_value)
    423  1.6  christos 	    - (Elf_Addr) where;
    424  1.6  christos #ifdef RTLD_DEBUG_RELOC
    425  1.6  christos 	dbg("PC32 %s in %s --> %p in %s",
    426  1.6  christos 	    defobj->strtab + def->st_name, obj->path,
    427  1.6  christos 	    (void *)*where, defobj->path);
    428  1.1       cgd #endif
    429  1.1       cgd 	break;
    430  1.4  christos     }
    431  1.4  christos 
    432  1.4  christos     case R_TYPE(32): {
    433  1.4  christos 	const Elf_Sym *def;
    434  1.4  christos 	const Obj_Entry *defobj;
    435  1.4  christos 
    436  1.4  christos 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, false);
    437  1.4  christos 	if (def == NULL)
    438  1.4  christos 	    return -1;
    439  1.4  christos 
    440  1.6  christos 	*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    441  1.6  christos #ifdef RTLD_DEBUG_RELOC
    442  1.6  christos 	dbg("32 %s in %s --> %p in %s",
    443  1.6  christos 	    defobj->strtab + def->st_name, obj->path,
    444  1.6  christos 	    (void *)*where, defobj->path);
    445  1.4  christos #endif
    446  1.4  christos 	break;
    447  1.4  christos     }
    448  1.4  christos #endif /* __i386__ */
    449  1.1       cgd 
    450  1.1       cgd #ifdef __alpha__
    451  1.1       cgd     case R_ALPHA_REFQUAD: {
    452  1.1       cgd 	const Elf_Sym *def;
    453  1.1       cgd 	const Obj_Entry *defobj;
    454  1.1       cgd 	Elf_Addr tmp_value;
    455  1.1       cgd 
    456  1.1       cgd 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, false);
    457  1.1       cgd 	if (def == NULL)
    458  1.1       cgd 	    return -1;
    459  1.1       cgd 
    460  1.1       cgd 	tmp_value = (Elf_Addr) (defobj->relocbase + def->st_value)
    461  1.1       cgd 	    + *where + rela->r_addend;
    462  1.1       cgd 	if (*where != tmp_value)
    463  1.6  christos 	    *where = tmp_value;
    464  1.6  christos #ifdef RTLD_DEBUG_RELOC
    465  1.6  christos 	dbg("REFQUAD %s in %s --> %p in %s",
    466  1.6  christos 	    defobj->strtab + def->st_name, obj->path,
    467  1.6  christos 	    (void *)*where, defobj->path);
    468  1.1       cgd #endif
    469  1.1       cgd 	break;
    470  1.4  christos     }
    471  1.1       cgd #endif /* __alpha__ */
    472  1.4  christos 
    473  1.1       cgd #if defined(__i386__) || defined(__alpha__)
    474  1.1       cgd     case R_TYPE(GLOB_DAT):
    475  1.1       cgd     {
    476  1.1       cgd 	const Elf_Sym *def;
    477  1.1       cgd 	const Obj_Entry *defobj;
    478  1.1       cgd 
    479  1.1       cgd 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, false);
    480  1.1       cgd 	if (def == NULL)
    481  1.1       cgd 	    return -1;
    482  1.1       cgd 
    483  1.1       cgd 	if (*where != (Elf_Addr) (defobj->relocbase + def->st_value))
    484  1.6  christos 	    *where = (Elf_Addr) (defobj->relocbase + def->st_value);
    485  1.6  christos #ifdef RTLD_DEBUG_RELOC
    486  1.6  christos 	dbg("GLOB_DAT %s in %s --> %p in %s",
    487  1.6  christos 	    defobj->strtab + def->st_name, obj->path,
    488  1.6  christos 	    (void *)*where, defobj->path);
    489  1.1       cgd #endif
    490  1.1       cgd 	break;
    491  1.1       cgd     }
    492  1.1       cgd 
    493  1.1       cgd     case R_TYPE(RELATIVE): {
    494  1.1       cgd 	extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
    495  1.1       cgd 	extern Elf_Dyn _DYNAMIC;
    496  1.1       cgd 
    497  1.1       cgd 	if (obj != &_rtld_objself ||
    498  1.6  christos 	    (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
    499  1.1       cgd 	    (caddr_t)where >= (caddr_t)&_DYNAMIC) {
    500  1.6  christos 	    *where += (Elf_Addr) obj->relocbase;
    501  1.6  christos #ifdef RTLD_DEBUG_RELOC
    502  1.6  christos 	    dbg("RELATIVE in %s --> %p", obj->path, (void *)*where);
    503  1.6  christos #endif
    504  1.6  christos 	}
    505  1.6  christos #ifdef RTLD_DEBUG_RELOC
    506  1.6  christos 	else
    507  1.6  christos 	    dbg("RELATIVE in %s stays at %p", obj->path, (void *)*where);
    508  1.1       cgd #endif
    509  1.1       cgd 	break;
    510  1.1       cgd     }
    511  1.1       cgd 
    512  1.1       cgd     case R_TYPE(COPY): {
    513  1.1       cgd 	/*
    514  1.1       cgd 	 * These are deferred until all other relocations have
    515  1.1       cgd 	 * been done.  All we do here is make sure that the COPY
    516  1.1       cgd 	 * relocation is not in a shared library.  They are allowed
    517  1.1       cgd 	 * only in executable files.
    518  1.1       cgd 	 */
    519  1.1       cgd 	if (!obj->mainprog) {
    520  1.1       cgd 	    _rtld_error("%s: Unexpected R_COPY relocation in shared library",
    521  1.1       cgd 		  obj->path);
    522  1.1       cgd 	    return -1;
    523  1.6  christos 	}
    524  1.6  christos #ifdef RTLD_DEBUG_RELOC
    525  1.6  christos 	dbg("COPY (avoid in main)");
    526  1.1       cgd #endif
    527  1.1       cgd 	break;
    528  1.4  christos     }
    529  1.2    mhitch #endif /* __i386__ || __alpha__ */
    530  1.2    mhitch 
    531  1.2    mhitch #ifdef __mips__
    532  1.2    mhitch     case R_TYPE(REL32): {
    533  1.2    mhitch     		/* 32-bit PC-relative reference */
    534  1.2    mhitch 
    535  1.2    mhitch         const Elf_Sym *def;
    536  1.2    mhitch         const Obj_Entry *defobj;
    537  1.2    mhitch 
    538  1.2    mhitch 	def = obj->symtab + ELF_R_SYM(rela->r_info);
    539  1.2    mhitch 
    540  1.2    mhitch         if (ELF_SYM_BIND(def->st_info) == Elf_estb_local &&
    541  1.2    mhitch           (ELF_SYM_TYPE(def->st_info) == Elf_estt_section ||
    542  1.2    mhitch            ELF_SYM_TYPE(def->st_info) == Elf_estt_notype)) {
    543  1.6  christos             *where += (Elf_Addr) obj->relocbase;
    544  1.6  christos #ifdef RTLD_DEBUG_RELOC
    545  1.6  christos 	    dbg("REL32 in %s --> %p", obj->path, (void *)*where);
    546  1.2    mhitch #endif
    547  1.2    mhitch         } else {
    548  1.2    mhitch /* XXX maybe do something re: bootstrapping? */
    549  1.2    mhitch             def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    550  1.2    mhitch 	        &defobj, false);
    551  1.2    mhitch             if (def == NULL)
    552  1.2    mhitch                 return -1;
    553  1.6  christos 	    *where += (Elf_Addr)(defobj->relocbase + def->st_value);
    554  1.6  christos #ifdef RTLD_DEBUG_RELOC
    555  1.6  christos 	    dbg("REL32 %s in %s --> %p in %s",
    556  1.6  christos 		defobj->strtab + def->st_name, obj->path,
    557  1.6  christos 		(void *)*where, defobj->path);
    558  1.2    mhitch #endif
    559  1.2    mhitch         }
    560  1.2    mhitch         break;
    561  1.2    mhitch     }
    562  1.2    mhitch 
    563  1.1       cgd #endif /* mips */
    564  1.3    tsubai 
    565  1.3    tsubai #ifdef __powerpc__
    566  1.3    tsubai     case R_TYPE(32):		/* word32 S + A */
    567  1.3    tsubai     case R_TYPE(GLOB_DAT): {	/* word32 S + A */
    568  1.3    tsubai 	const Elf_Sym *def;
    569  1.3    tsubai 	const Obj_Entry *defobj;
    570  1.3    tsubai 	Elf_Addr x;
    571  1.3    tsubai 
    572  1.3    tsubai 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, false);
    573  1.3    tsubai 	if (def == NULL)
    574  1.3    tsubai 	    return -1;
    575  1.3    tsubai 
    576  1.3    tsubai 	x = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
    577  1.3    tsubai 
    578  1.3    tsubai 	if (*where != x)
    579  1.6  christos 	    *where = x;
    580  1.6  christos #ifdef RTLD_DEBUG_RELOC
    581  1.6  christos 	dbg("32/GLOB_DAT %s in %s --> %p in %s",
    582  1.6  christos 	    defobj->strtab + def->st_name, obj->path,
    583  1.6  christos 	    (void *)*where, defobj->path);
    584  1.3    tsubai #endif
    585  1.3    tsubai 	break;
    586  1.3    tsubai     }
    587  1.3    tsubai 
    588  1.6  christos     case R_TYPE(COPY):
    589  1.6  christos #ifdef RTLD_DEBUG_RELOC
    590  1.6  christos 	dbg("COPY");
    591  1.3    tsubai #endif
    592  1.3    tsubai 	break;
    593  1.3    tsubai 
    594  1.6  christos     case R_TYPE(JMP_SLOT):
    595  1.6  christos #ifdef RTLD_DEBUG_RELOC
    596  1.6  christos 	dbg("JMP_SLOT");
    597  1.3    tsubai #endif
    598  1.3    tsubai 	break;
    599  1.3    tsubai 
    600  1.3    tsubai     case R_TYPE(RELATIVE): {	/* word32 B + A */
    601  1.3    tsubai 	if (obj == &_rtld_objself &&
    602  1.3    tsubai 	    *where == (Elf_Addr)obj->relocbase + rela->r_addend)
    603  1.3    tsubai 	    break;	/* GOT - already done */
    604  1.3    tsubai 
    605  1.6  christos 	*where = (Elf_Addr)obj->relocbase + rela->r_addend;
    606  1.6  christos #ifdef RTLD_DEBUG_RELOC
    607  1.6  christos 	dbg("RELATIVE in %s --> %p", obj->path, (void *)*where);
    608  1.3    tsubai #endif
    609  1.3    tsubai 	break;
    610  1.4  christos     }
    611  1.3    tsubai #endif /* __powerpc__ */
    612  1.1       cgd 
    613  1.1       cgd     default: {
    614  1.1       cgd 	const Elf_Sym *def;
    615  1.1       cgd 	const Obj_Entry *defobj;
    616  1.1       cgd 
    617  1.5   thorpej 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, true);
    618  1.5   thorpej 	dbg("sym = %lu, type = %lu, offset = %p, addend = %p, contents = %p, symbol = %s",
    619  1.4  christos 	    (u_long)ELF_R_SYM(rela->r_info), (u_long)ELF_R_TYPE(rela->r_info),
    620  1.1       cgd 	    (void *)rela->r_offset, (void *)rela->r_addend, (void *)*where,
    621  1.1       cgd 	    def ? defobj->strtab + def->st_name : "??");
    622  1.1       cgd 	_rtld_error("%s: Unsupported relocation type %d in non-PLT relocations\n",
    623  1.1       cgd 	      obj->path, ELF_R_TYPE(rela->r_info));
    624  1.1       cgd 	return -1;
    625  1.1       cgd     }
    626  1.1       cgd     }
    627  1.1       cgd     return 0;
    628  1.9        pk }
    629  1.9        pk 
    630  1.9        pk 
    631  1.1       cgd 
    632  1.1       cgd static int
    633  1.1       cgd _rtld_relocate_plt_object(
    634  1.1       cgd     const Obj_Entry *obj,
    635  1.1       cgd     const Elf_RelA *rela,
    636  1.1       cgd     bool bind_now)
    637  1.1       cgd {
    638  1.1       cgd     Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    639  1.1       cgd     Elf_Addr new_value;
    640  1.1       cgd 
    641  1.2    mhitch     /* Fully resolve procedure addresses now */
    642  1.3    tsubai 
    643  1.3    tsubai #if defined(__powerpc__)
    644  1.3    tsubai     return _rtld_reloc_powerpc_plt(obj, rela, bind_now);
    645  1.3    tsubai #endif
    646  1.4  christos 
    647  1.1       cgd #if defined(__alpha__)	|| defined(__i386__) /* (jrs) */
    648  1.1       cgd     if (bind_now || obj->pltgot == NULL) {
    649  1.1       cgd 	const Elf_Sym *def;
    650  1.1       cgd 	const Obj_Entry *defobj;
    651  1.1       cgd 
    652  1.1       cgd 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    653  1.1       cgd 
    654  1.1       cgd 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, true);
    655  1.1       cgd 	if (def == NULL)
    656  1.1       cgd 	    return -1;
    657  1.1       cgd 
    658  1.6  christos 	new_value = (Elf_Addr) (defobj->relocbase + def->st_value);
    659  1.6  christos #ifdef RTLD_DEBUG_RELOC
    660  1.6  christos 	dbg("bind now %d/fixup in %s --> old=%p new=%p",
    661  1.6  christos 	    (int)bind_now,
    662  1.6  christos 	    defobj->strtab + def->st_name,
    663  1.1       cgd 	    (void *)*where, (void *)new_value);
    664  1.2    mhitch #endif
    665  1.2    mhitch     } else
    666  1.4  christos #endif	/* __alpha__ (jrs) */
    667  1.1       cgd     if (!obj->mainprog) {
    668  1.1       cgd 	/* Just relocate the GOT slots pointing into the PLT */
    669  1.6  christos 	new_value = *where + (Elf_Addr) (obj->relocbase);
    670  1.6  christos #ifdef RTLD_DEBUG_RELOC
    671  1.4  christos 	dbg("fixup !main in %s --> %p", obj->path, (void *)*where);
    672  1.1       cgd #endif
    673  1.7        tv     } else {
    674  1.1       cgd 	return 0;
    675  1.1       cgd     }
    676  1.1       cgd     /*
    677  1.1       cgd      * Since this page is probably copy-on-write, let's not write
    678  1.1       cgd      * it unless we really really have to.
    679  1.1       cgd      */
    680  1.1       cgd     if (*where != new_value)
    681  1.1       cgd 	*where = new_value;
    682  1.1       cgd     return 0;
    683  1.9        pk }
    684  1.1       cgd 
    685  1.1       cgd caddr_t
    686  1.1       cgd _rtld_bind(
    687  1.1       cgd     const Obj_Entry *obj,
    688  1.1       cgd     Elf_Word reloff)
    689  1.1       cgd {
    690  1.1       cgd     const Elf_RelA *rela;
    691  1.1       cgd     Elf_RelA ourrela;
    692  1.1       cgd 
    693  1.1       cgd     if (obj->pltrel != NULL) {
    694  1.1       cgd 	ourrela.r_info =   ((const Elf_Rel *) ((caddr_t) obj->pltrel + reloff))->r_info;
    695  1.1       cgd 	ourrela.r_offset = ((const Elf_Rel *) ((caddr_t) obj->pltrel + reloff))->r_offset;
    696  1.1       cgd 	rela = &ourrela;
    697  1.1       cgd     } else {
    698  1.1       cgd 	rela = (const Elf_RelA *) ((caddr_t) obj->pltrela + reloff);
    699  1.1       cgd     }
    700  1.9        pk 
    701  1.1       cgd     if (__rtld_relocate_plt_object(obj, rela, true) < 0)
    702  1.1       cgd 	_rtld_die();
    703  1.1       cgd 
    704  1.1       cgd     return *(caddr_t *)(obj->relocbase + rela->r_offset);
    705  1.9        pk }
    706  1.9        pk #endif /* __sparc__ */
    707  1.1       cgd 
    708  1.1       cgd /*
    709  1.1       cgd  * Relocate newly-loaded shared objects.  The argument is a pointer to
    710  1.1       cgd  * the Obj_Entry for the first such object.  All objects from the first
    711  1.1       cgd  * to the end of the list of objects are relocated.  Returns 0 on success,
    712  1.1       cgd  * or -1 on failure.
    713  1.1       cgd  */
    714  1.1       cgd int
    715  1.1       cgd _rtld_relocate_objects(
    716  1.1       cgd     Obj_Entry *first,
    717  1.1       cgd     bool bind_now)
    718  1.1       cgd {
    719  1.1       cgd     Obj_Entry *obj;
    720  1.1       cgd     int ok = 1;
    721  1.1       cgd 
    722  1.1       cgd     for (obj = first;  obj != NULL;  obj = obj->next) {
    723  1.1       cgd 
    724  1.1       cgd 	if (obj->nbuckets == 0 || obj->nchains == 0
    725  1.1       cgd 	        || obj->buckets == NULL || obj->symtab == NULL
    726  1.1       cgd 	        || obj->strtab == NULL) {
    727  1.1       cgd 	    _rtld_error("%s: Shared object has no run-time symbol table",
    728  1.1       cgd 			obj->path);
    729  1.1       cgd 	    return -1;
    730  1.1       cgd 	}
    731  1.5   thorpej 
    732  1.1       cgd 	dbg(" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)",
    733  1.5   thorpej 	    obj->path,
    734  1.5   thorpej 	    (long)(obj->rellim - obj->rel), (long)(obj->relalim - obj->rela),
    735  1.5   thorpej 	    (long)(obj->pltrellim - obj->pltrel),
    736  1.1       cgd 	    (long)(obj->pltrelalim - obj->pltrela));
    737  1.1       cgd 
    738  1.1       cgd 	if (obj->textrel) {
    739  1.1       cgd 	    /* There are relocations to the write-protected text segment. */
    740  1.1       cgd 	    if (mprotect(obj->mapbase, obj->textsize,
    741  1.1       cgd 			 PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
    742  1.1       cgd 		_rtld_error("%s: Cannot write-enable text segment: %s",
    743  1.1       cgd 			    obj->path, xstrerror(errno));
    744  1.1       cgd 		return -1;
    745  1.1       cgd 	    }
    746  1.1       cgd 	}
    747  1.1       cgd 
    748  1.1       cgd 	if (obj->rel != NULL) {
    749  1.1       cgd 	    /* Process the non-PLT relocations. */
    750  1.1       cgd 	    const Elf_Rel *rel;
    751  1.1       cgd 	    for (rel = obj->rel;  rel < obj->rellim;  ++rel) {
    752  1.1       cgd 		Elf_RelA ourrela;
    753  1.1       cgd 		ourrela.r_info   = rel->r_info;
    754  1.2    mhitch 		ourrela.r_offset = rel->r_offset;
    755  1.2    mhitch #if defined(__mips__)
    756  1.2    mhitch 		/* rel->r_offset is not valid on mips? */
    757  1.2    mhitch 		if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
    758  1.2    mhitch 		    ourrela.r_addend = 0;
    759  1.2    mhitch 		else
    760  1.1       cgd #endif
    761  1.1       cgd 		ourrela.r_addend = *(Elf_Word *) (obj->relocbase + rel->r_offset);
    762  1.1       cgd 
    763  1.1       cgd 		if (_rtld_relocate_nonplt_object(obj, &ourrela) < 0)
    764  1.1       cgd 		    ok = 0;
    765  1.1       cgd 	    }
    766  1.1       cgd 	}
    767  1.1       cgd 
    768  1.1       cgd 	if (obj->rela != NULL) {
    769  1.1       cgd 	    /* Process the non-PLT relocations. */
    770  1.1       cgd 	    const Elf_RelA *rela;
    771  1.1       cgd 	    for (rela = obj->rela;  rela < obj->relalim;  ++rela) {
    772  1.1       cgd 		if (_rtld_relocate_nonplt_object(obj, rela) < 0)
    773  1.1       cgd 		    ok = 0;
    774  1.1       cgd 	    }
    775  1.1       cgd 	}
    776  1.1       cgd 
    777  1.1       cgd 	if (obj->textrel) {	/* Re-protected the text segment. */
    778  1.1       cgd 	    if (mprotect(obj->mapbase, obj->textsize,
    779  1.1       cgd 			 PROT_READ|PROT_EXEC) == -1) {
    780  1.1       cgd 		_rtld_error("%s: Cannot write-protect text segment: %s",
    781  1.1       cgd 			    obj->path, xstrerror(errno));
    782  1.1       cgd 		return -1;
    783  1.1       cgd 	    }
    784  1.1       cgd 	}
    785  1.1       cgd 
    786  1.1       cgd 	/* Process the PLT relocations. */
    787  1.1       cgd 	if (obj->pltrel != NULL) {
    788  1.1       cgd 	    const Elf_Rel *rel;
    789  1.1       cgd 	    for (rel = obj->pltrel; rel < obj->pltrellim;  ++rel) {
    790  1.1       cgd 		Elf_RelA ourrela;
    791  1.1       cgd 		ourrela.r_info   = rel->r_info;
    792  1.1       cgd 		ourrela.r_offset = rel->r_offset;
    793  1.1       cgd 		ourrela.r_addend = *(Elf_Word *) (obj->relocbase + rel->r_offset);
    794  1.1       cgd 		if (_rtld_relocate_plt_object(obj, &ourrela, bind_now) < 0)
    795  1.1       cgd 		    ok = 0;
    796  1.1       cgd 	    }
    797  1.1       cgd 	}
    798  1.1       cgd 
    799  1.1       cgd 	if (obj->pltrela != NULL) {
    800  1.1       cgd 	    const Elf_RelA *rela;
    801  1.1       cgd 	    for (rela = obj->pltrela;  rela < obj->pltrelalim;  ++rela) {
    802  1.1       cgd 		if (_rtld_relocate_plt_object(obj, rela, bind_now) < 0)
    803  1.1       cgd 		    ok = 0;
    804  1.1       cgd 	    }
    805  1.1       cgd 	}
    806  1.1       cgd 
    807  1.1       cgd 	if (!ok)
    808  1.1       cgd 	    return -1;
    809  1.1       cgd 
    810  1.1       cgd 
    811  1.1       cgd 	/* Set some sanity-checking numbers in the Obj_Entry. */
    812  1.1       cgd 	obj->magic = RTLD_MAGIC;
    813  1.1       cgd 	obj->version = RTLD_VERSION;
    814  1.1       cgd 
    815  1.1       cgd 	/* Fill in the dynamic linker entry points. */
    816  1.1       cgd 	obj->dlopen  = _rtld_dlopen;
    817  1.1       cgd 	obj->dlsym   = _rtld_dlsym;
    818  1.1       cgd 	obj->dlerror = _rtld_dlerror;
    819  1.1       cgd 	obj->dlclose = _rtld_dlclose;
    820  1.1       cgd 
    821  1.1       cgd 	/* Set the special PLTGOT entries. */
    822  1.1       cgd 	if (obj->pltgot != NULL) {
    823  1.1       cgd #if defined(__i386__)
    824  1.1       cgd 	    obj->pltgot[1] = (Elf_Addr) obj;
    825  1.1       cgd 	    obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
    826  1.1       cgd #endif
    827  1.1       cgd #if defined(__alpha__)
    828  1.1       cgd 	    /* This function will be called to perform the relocation.  */
    829  1.1       cgd 	    obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
    830  1.1       cgd 	    /* Identify this shared object */
    831  1.2    mhitch 	    obj->pltgot[3] = (Elf_Addr) obj;
    832  1.2    mhitch #endif
    833  1.2    mhitch #if defined(__mips__)
    834  1.2    mhitch 	    _rtld_relocate_mips_got(obj);
    835  1.2    mhitch 
    836  1.2    mhitch 	    obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
    837  1.2    mhitch 	    /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
    838  1.3    tsubai 	    obj->pltgot[1] |= (Elf_Addr) obj;
    839  1.3    tsubai #endif
    840  1.3    tsubai #if defined(__powerpc__)
    841  1.9        pk 	    _rtld_setup_powerpc_plt(obj);
    842  1.9        pk #endif
    843  1.9        pk #if defined(__sparc__)
    844  1.9        pk 		/*
    845  1.9        pk 		 * PLTGOT is the PLT on the sparc.
    846  1.9        pk 		 * The first entry holds the call the dynamic linker.
    847  1.9        pk 		 * We construct a `call' instruction that transfers
    848  1.9        pk 		 * to `_rtld_bind_start()'.
    849  1.9        pk 		 * The second entry holds the object identification.
    850  1.9        pk 		 * Note: each PLT entry is three words long.
    851  1.9        pk 		 */
    852  1.9        pk 		obj->pltgot[1] = 0x40000000 |
    853  1.9        pk 		    ((Elf_Addr)&_rtld_bind_start - (Elf_Addr)&obj->pltgot[1]);
    854  1.1       cgd 		obj->pltgot[3] = (Elf_Addr) obj;
    855  1.1       cgd #endif
    856  1.1       cgd 	}
    857  1.1       cgd     }
    858  1.1       cgd 
    859  1.1       cgd     return 0;
    860                }
    861