Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.12
      1  1.12        tv /*	$NetBSD: reloc.c,v 1.12 1999/02/25 21:49:04 tv 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.11  christos 	const Obj_Entry *dstobj,
     68  1.11  christos 	const Elf_RelA *rela,
     69  1.11  christos 	bool dodebug)
     70   1.1       cgd {
     71  1.11  christos 	void           *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
     72  1.11  christos 	const Elf_Sym  *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
     73  1.11  christos 	const char     *name = dstobj->strtab + dstsym->st_name;
     74  1.11  christos 	unsigned long   hash = _rtld_elf_hash(name);
     75  1.11  christos 	size_t          size = dstsym->st_size;
     76  1.11  christos 	const void     *srcaddr;
     77  1.11  christos 	const Elf_Sym  *srcsym;
     78  1.11  christos 	Obj_Entry      *srcobj;
     79  1.11  christos 
     80  1.11  christos 	for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
     81  1.11  christos 		if ((srcsym = _rtld_symlook_obj(name, hash, srcobj,
     82  1.11  christos 		    false)) != NULL)
     83  1.11  christos 			break;
     84  1.11  christos 
     85  1.11  christos 	if (srcobj == NULL) {
     86  1.11  christos 		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
     87  1.11  christos 		    " relocation in %s", name, dstobj->path);
     88  1.11  christos 		return -1;
     89  1.11  christos 	}
     90  1.11  christos 	srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
     91  1.11  christos 	(void)memcpy(dstaddr, srcaddr, size);
     92  1.11  christos 	rdbg(dodebug, "COPY %s %s %s --> src=%p dst=%p *dst= %p size %d",
     93  1.11  christos 	    dstobj->path, srcobj->path, name, (void *)srcaddr,
     94  1.11  christos 	    (void *)dstaddr, (void *)*(long *)dstaddr, size);
     95  1.11  christos 	return 0;
     96   1.1       cgd }
     97   1.4  christos #endif /* __alpha__ || __powerpc__ || __i386__ */
     98  1.11  christos 
     99  1.11  christos 
    100   1.1       cgd /*
    101   1.1       cgd  * Process the special R_xxx_COPY relocations in the main program.  These
    102   1.1       cgd  * copy data from a shared object into a region in the main program's BSS
    103   1.1       cgd  * segment.
    104   1.1       cgd  *
    105   1.1       cgd  * Returns 0 on success, -1 on failure.
    106   1.1       cgd  */
    107   1.1       cgd int
    108   1.1       cgd _rtld_do_copy_relocations(
    109  1.11  christos 	const Obj_Entry *dstobj,
    110  1.11  christos 	bool dodebug)
    111   1.1       cgd {
    112  1.11  christos 	assert(dstobj->mainprog);	/* COPY relocations are invalid
    113  1.11  christos 					 * elsewhere */
    114   1.1       cgd 
    115   1.4  christos #if defined(__alpha__) || defined(__powerpc__) || defined(__i386__)
    116  1.11  christos 	if (dstobj->rel != NULL) {
    117  1.11  christos 		const Elf_Rel  *rel;
    118  1.11  christos 		for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
    119  1.11  christos 			if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
    120  1.11  christos 				Elf_RelA        ourrela;
    121  1.11  christos 				ourrela.r_info = rel->r_info;
    122  1.11  christos 				ourrela.r_offset = rel->r_offset;
    123  1.11  christos 				ourrela.r_addend = 0;
    124  1.11  christos 				if (_rtld_do_copy_relocation(dstobj,
    125  1.11  christos 				    &ourrela, dodebug) < 0)
    126  1.11  christos 					return -1;
    127  1.11  christos 			}
    128  1.11  christos 		}
    129  1.11  christos 	}
    130  1.11  christos 	if (dstobj->rela != NULL) {
    131  1.11  christos 		const Elf_RelA *rela;
    132  1.11  christos 		for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
    133  1.11  christos 			if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
    134  1.11  christos 				if (_rtld_do_copy_relocation(dstobj, rela,
    135  1.11  christos 				    dodebug) < 0)
    136  1.11  christos 					return -1;
    137  1.11  christos 			}
    138  1.11  christos 		}
    139   1.1       cgd 	}
    140   1.4  christos #endif /* __alpha__ || __powerpc__ || __i386__ */
    141   1.1       cgd 
    142  1.11  christos 	return 0;
    143   1.1       cgd }
    144   1.9        pk 
    145   1.9        pk 
    146  1.11  christos #ifndef __sparc__
    147  1.11  christos int
    148   1.9        pk _rtld_relocate_nonplt_object(
    149  1.11  christos 	const Obj_Entry * obj,
    150  1.11  christos 	const Elf_RelA * rela,
    151  1.11  christos 	bool dodebug)
    152   1.9        pk {
    153  1.11  christos 	Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    154  1.11  christos 	const Elf_Sym   *def;
    155   1.9        pk 	const Obj_Entry *defobj;
    156  1.11  christos 	extern Elf_Addr  _GLOBAL_OFFSET_TABLE_[];
    157  1.11  christos 	extern Elf_Dyn   _DYNAMIC;
    158  1.11  christos 	Elf_Addr         tmp;
    159   1.9        pk 
    160  1.11  christos 	switch (ELF_R_TYPE(rela->r_info)) {
    161   1.9        pk 
    162  1.11  christos 	case R_TYPE(NONE):
    163  1.11  christos 		break;
    164   1.9        pk 
    165  1.11  christos #ifdef __i386__
    166  1.11  christos 	case R_TYPE(GOT32):
    167   1.9        pk 
    168  1.11  christos 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    169  1.11  christos 		    &defobj, false);
    170  1.11  christos 		if (def == NULL)
    171  1.11  christos 			return -1;
    172   1.9        pk 
    173  1.11  christos 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
    174  1.11  christos 		if (*where != tmp)
    175  1.11  christos 			*where = tmp;
    176  1.11  christos 		rdbg(dodebug, "GOT32 %s in %s --> %p in %s",
    177  1.11  christos 		    defobj->strtab + def->st_name, obj->path,
    178  1.11  christos 		    (void *)*where, defobj->path);
    179  1.11  christos 		break;
    180   1.9        pk 
    181  1.11  christos 	case R_TYPE(PC32):
    182  1.11  christos 		/*
    183  1.11  christos 		 * I don't think the dynamic linker should ever see this
    184  1.11  christos 		 * type of relocation.  But the binutils-2.6 tools sometimes
    185  1.11  christos 		 * generate it.
    186  1.11  christos 		 */
    187   1.9        pk 
    188  1.11  christos 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    189  1.11  christos 		    &defobj, false);
    190  1.11  christos 		if (def == NULL)
    191  1.11  christos 			return -1;
    192   1.9        pk 
    193  1.11  christos 		*where += (Elf_Addr)(defobj->relocbase + def->st_value) -
    194  1.11  christos 		    (Elf_Addr)where;
    195  1.11  christos 		rdbg(dodebug, "PC32 %s in %s --> %p in %s",
    196  1.11  christos 		    defobj->strtab + def->st_name, obj->path,
    197  1.11  christos 		    (void *)*where, defobj->path);
    198  1.11  christos 		break;
    199  1.11  christos 
    200  1.11  christos 	case R_TYPE(32):
    201  1.11  christos 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    202  1.11  christos 		    &defobj, false);
    203  1.11  christos 		if (def == NULL)
    204  1.11  christos 			return -1;
    205   1.9        pk 
    206  1.11  christos 		*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    207  1.11  christos 		rdbg(dodebug, "32 %s in %s --> %p in %s",
    208  1.11  christos 		    defobj->strtab + def->st_name, obj->path,
    209  1.11  christos 		    (void *)*where, defobj->path);
    210  1.11  christos 		break;
    211   1.4  christos #endif /* __i386__ */
    212   1.4  christos 
    213   1.1       cgd #ifdef __alpha__
    214  1.11  christos 	case R_TYPE(REFQUAD):
    215  1.11  christos 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    216  1.11  christos 		    &defobj, false);
    217  1.11  christos 		if (def == NULL)
    218  1.11  christos 			return -1;
    219   1.1       cgd 
    220  1.11  christos 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
    221  1.11  christos 		    *where + rela->r_addend;
    222  1.12        tv 		if (*where != tmp)
    223  1.12        tv 			*where = tmp;
    224  1.11  christos 		rdbg(dodebug, "REFQUAD %s in %s --> %p in %s",
    225  1.11  christos 		    defobj->strtab + def->st_name, obj->path,
    226  1.11  christos 		    (void *)*where, defobj->path);
    227  1.11  christos 		break;
    228   1.4  christos #endif /* __alpha__ */
    229   1.1       cgd 
    230   1.4  christos #if defined(__i386__) || defined(__alpha__)
    231  1.11  christos 	case R_TYPE(GLOB_DAT):
    232  1.11  christos 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    233  1.11  christos 		    &defobj, false);
    234  1.11  christos 		if (def == NULL)
    235  1.11  christos 			return -1;
    236   1.1       cgd 
    237  1.11  christos 		if (*where != (Elf_Addr)(defobj->relocbase + def->st_value))
    238  1.11  christos 			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
    239  1.11  christos 		rdbg(dodebug, "GLOB_DAT %s in %s --> %p in %s",
    240  1.11  christos 		    defobj->strtab + def->st_name, obj->path,
    241  1.11  christos 		    (void *)*where, defobj->path);
    242  1.11  christos 		break;
    243  1.11  christos 
    244  1.11  christos 	case R_TYPE(RELATIVE):
    245  1.12        tv 		if ((caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
    246  1.11  christos 		    (caddr_t)where >= (caddr_t)&_DYNAMIC) {
    247  1.11  christos 			*where += (Elf_Addr)obj->relocbase;
    248  1.11  christos 			rdbg(dodebug, "RELATIVE in %s --> %p", obj->path,
    249  1.11  christos 			    (void *)*where);
    250  1.11  christos 		}
    251  1.11  christos 		else
    252  1.11  christos 			rdbg(dodebug, "RELATIVE in %s stays at %p",
    253  1.11  christos 			    obj->path, (void *)*where);
    254  1.11  christos 		break;
    255   1.1       cgd 
    256  1.11  christos 	case R_TYPE(COPY):
    257  1.11  christos 		/*
    258  1.11  christos 		 * These are deferred until all other relocations have
    259  1.11  christos 		 * been done.  All we do here is make sure that the COPY
    260  1.11  christos 		 * relocation is not in a shared library.  They are allowed
    261  1.11  christos 		 * only in executable files.
    262  1.11  christos 		 */
    263  1.11  christos 		if (!obj->mainprog) {
    264  1.11  christos 			_rtld_error(
    265  1.11  christos 			"%s: Unexpected R_COPY relocation in shared library",
    266  1.11  christos 			    obj->path);
    267  1.11  christos 			return -1;
    268  1.11  christos 		}
    269  1.11  christos 		rdbg(dodebug, "COPY (avoid in main)");
    270  1.11  christos 		break;
    271   1.4  christos #endif /* __i386__ || __alpha__ */
    272   1.2    mhitch 
    273   1.2    mhitch #ifdef __mips__
    274  1.11  christos 	case R_TYPE(REL32):
    275  1.11  christos 		/* 32-bit PC-relative reference */
    276  1.11  christos 		def = obj->symtab + ELF_R_SYM(rela->r_info);
    277  1.11  christos 
    278  1.11  christos 		if (ELF_SYM_BIND(def->st_info) == Elf_estb_local &&
    279  1.11  christos 		  (ELF_SYM_TYPE(def->st_info) == Elf_estt_section ||
    280  1.11  christos 		   ELF_SYM_TYPE(def->st_info) == Elf_estt_notype)) {
    281  1.11  christos 			*where += (Elf_Addr)obj->relocbase;
    282  1.11  christos 			rdbg(dodebug, "REL32 in %s --> %p", obj->path,
    283  1.11  christos 			    (void *)*where);
    284  1.11  christos 		} else {
    285  1.11  christos 			/* XXX maybe do something re: bootstrapping? */
    286  1.11  christos 			def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
    287  1.11  christos 			    NULL, obj, &defobj, false);
    288  1.11  christos 			if (def == NULL)
    289  1.11  christos 				return -1;
    290  1.11  christos 			*where += (Elf_Addr)(defobj->relocbase + def->st_value);
    291  1.11  christos 			rdbg(dodebug, "REL32 %s in %s --> %p in %s",
    292  1.11  christos 			    defobj->strtab + def->st_name, obj->path,
    293  1.11  christos 			    (void *)*where, defobj->path);
    294  1.11  christos 		}
    295  1.11  christos 		break;
    296   1.2    mhitch 
    297  1.11  christos #endif /* __mips__ */
    298   1.1       cgd 
    299   1.3    tsubai #ifdef __powerpc__
    300  1.11  christos 	case R_TYPE(32):	/* word32 S + A */
    301  1.11  christos 	case R_TYPE(GLOB_DAT):	/* word32 S + A */
    302  1.11  christos 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    303  1.11  christos 		    &defobj, false);
    304  1.11  christos 		if (def == NULL)
    305  1.11  christos 			return -1;
    306   1.3    tsubai 
    307  1.11  christos 		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    308  1.11  christos 		    rela->r_addend);
    309   1.3    tsubai 
    310  1.11  christos 		if (*where != tmp)
    311  1.11  christos 			*where = tmp;
    312  1.11  christos 		rdbg(dodebug, "32/GLOB_DAT %s in %s --> %p in %s",
    313  1.11  christos 		    defobj->strtab + def->st_name, obj->path,
    314  1.11  christos 		    (void *)*where, defobj->path);
    315  1.11  christos 		break;
    316  1.11  christos 
    317  1.11  christos 	case R_TYPE(COPY):
    318  1.11  christos 		rdbg(dodebug, "COPY");
    319  1.11  christos 		break;
    320  1.11  christos 
    321  1.11  christos 	case R_TYPE(JMP_SLOT):
    322  1.11  christos 		rdbg(dodebug, "JMP_SLOT");
    323  1.11  christos 		break;
    324  1.11  christos 
    325  1.11  christos 	case R_TYPE(RELATIVE):	/* word32 B + A */
    326  1.11  christos 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    327  1.11  christos 		if (obj == &_rtld_objself && *where == tmp;
    328  1.11  christos 			break;	/* GOT - already done */
    329  1.11  christos 
    330  1.11  christos 		*where = tmp;
    331  1.11  christos 		rdbg(dodebug, "RELATIVE in %s --> %p", obj->path,
    332  1.11  christos 		    (void *)*where);
    333  1.11  christos 		break;
    334   1.4  christos #endif /* __powerpc__ */
    335   1.3    tsubai 
    336  1.11  christos 	default:
    337  1.11  christos 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    338  1.11  christos 		    &defobj, true);
    339  1.11  christos 		rdbg(dodebug, "sym = %lu, type = %lu, offset = %p, "
    340  1.11  christos 		    "addend = %p, contents = %p, symbol = %s",
    341  1.11  christos 		    (u_long)ELF_R_SYM(rela->r_info),
    342  1.11  christos 		    (u_long)ELF_R_TYPE(rela->r_info),
    343  1.11  christos 		    (void *)rela->r_offset, (void *)rela->r_addend,
    344  1.11  christos 		    (void *)*where,
    345  1.11  christos 		    def ? defobj->strtab + def->st_name : "??");
    346  1.11  christos 		_rtld_error("%s: Unsupported relocation type %d"
    347  1.11  christos 		    "in non-PLT relocations\n",
    348  1.11  christos 		    obj->path, ELF_R_TYPE(rela->r_info));
    349  1.11  christos 		return -1;
    350  1.11  christos 	}
    351  1.11  christos 	return 0;
    352   1.1       cgd }
    353   1.9        pk 
    354   1.9        pk 
    355   1.9        pk 
    356  1.11  christos int
    357   1.1       cgd _rtld_relocate_plt_object(
    358  1.11  christos 	const Obj_Entry * obj,
    359  1.11  christos 	const Elf_RelA * rela,
    360  1.11  christos 	caddr_t *addrp,
    361  1.11  christos 	bool bind_now,
    362  1.11  christos 	bool dodebug)
    363   1.1       cgd {
    364  1.11  christos 	Elf_Addr       *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    365  1.11  christos 	Elf_Addr        new_value;
    366   1.1       cgd 
    367  1.11  christos 	/* Fully resolve procedure addresses now */
    368   1.2    mhitch 
    369   1.3    tsubai #if defined(__powerpc__)
    370  1.11  christos 	return _rtld_reloc_powerpc_plt(obj, rela, bind_now);
    371   1.3    tsubai #endif
    372   1.3    tsubai 
    373  1.11  christos #if defined(__alpha__)	|| defined(__i386__)
    374  1.11  christos 	if (bind_now || obj->pltgot == NULL) {
    375  1.11  christos 		const Elf_Sym  *def;
    376  1.11  christos 		const Obj_Entry *defobj;
    377  1.11  christos 
    378  1.11  christos 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    379   1.1       cgd 
    380  1.11  christos 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    381  1.11  christos 		    &defobj, true);
    382  1.11  christos 		if (def == NULL)
    383  1.11  christos 			return -1;
    384   1.1       cgd 
    385  1.11  christos 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    386  1.11  christos 		rdbg(dodebug, "bind now %d/fixup in %s --> old=%p new=%p",
    387  1.11  christos 		    (int)bind_now,
    388  1.11  christos 		    defobj->strtab + def->st_name,
    389  1.11  christos 		    (void *)*where, (void *)new_value);
    390  1.11  christos 	} else
    391  1.11  christos #endif /* __alpha__ || __i386__ */
    392  1.11  christos 	if (!obj->mainprog) {
    393  1.11  christos 		/* Just relocate the GOT slots pointing into the PLT */
    394  1.11  christos 		new_value = *where + (Elf_Addr)(obj->relocbase);
    395  1.11  christos 		rdbg(dodebug, "fixup !main in %s --> %p", obj->path,
    396  1.11  christos 		    (void *)*where);
    397  1.11  christos 	} else {
    398  1.11  christos 		return 0;
    399  1.11  christos 	}
    400  1.11  christos 	/*
    401  1.11  christos          * Since this page is probably copy-on-write, let's not write
    402  1.11  christos          * it unless we really really have to.
    403  1.11  christos          */
    404  1.11  christos 	if (*where != new_value)
    405  1.11  christos 		*where = new_value;
    406  1.11  christos 	if (addrp != NULL)
    407  1.11  christos 		*addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
    408   1.7        tv 	return 0;
    409   1.1       cgd }
    410  1.11  christos #endif /* __sparc__ */
    411   1.9        pk 
    412   1.1       cgd caddr_t
    413   1.1       cgd _rtld_bind(
    414  1.11  christos 	const Obj_Entry *obj,
    415  1.11  christos 	Elf_Word reloff)
    416   1.1       cgd {
    417  1.11  christos 	const Elf_RelA *rela;
    418  1.11  christos 	Elf_RelA        ourrela;
    419  1.11  christos 	caddr_t		addr;
    420   1.1       cgd 
    421  1.11  christos 	if (obj->pltrel != NULL) {
    422  1.11  christos 		const Elf_Rel *rel;
    423   1.1       cgd 
    424  1.11  christos 		rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
    425  1.11  christos 		ourrela.r_info = rel->r_info;
    426  1.11  christos 		ourrela.r_offset = rel->r_offset;
    427  1.11  christos 		rela = &ourrela;
    428  1.11  christos 	} else {
    429  1.11  christos 		rela = (const Elf_RelA *)((caddr_t) obj->pltrela + reloff);
    430  1.11  christos 	}
    431  1.11  christos 
    432  1.11  christos 	if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
    433  1.11  christos 		_rtld_die();
    434   1.1       cgd 
    435  1.11  christos 	return addr;
    436   1.1       cgd }
    437   1.9        pk 
    438   1.1       cgd /*
    439   1.1       cgd  * Relocate newly-loaded shared objects.  The argument is a pointer to
    440   1.1       cgd  * the Obj_Entry for the first such object.  All objects from the first
    441   1.1       cgd  * to the end of the list of objects are relocated.  Returns 0 on success,
    442   1.1       cgd  * or -1 on failure.
    443   1.1       cgd  */
    444   1.1       cgd int
    445   1.1       cgd _rtld_relocate_objects(
    446  1.11  christos 	Obj_Entry * first,
    447  1.11  christos 	bool bind_now,
    448  1.11  christos 	bool dodebug)
    449   1.1       cgd {
    450  1.11  christos 	Obj_Entry      *obj;
    451  1.11  christos 	int             ok = 1;
    452   1.1       cgd 
    453  1.11  christos 	for (obj = first; obj != NULL; obj = obj->next) {
    454  1.11  christos 		if (obj->nbuckets == 0 || obj->nchains == 0
    455  1.11  christos 		    || obj->buckets == NULL || obj->symtab == NULL
    456  1.11  christos 		    || obj->strtab == NULL) {
    457  1.11  christos 			_rtld_error("%s: Shared object has no run-time"
    458  1.11  christos 			    " symbol table", obj->path);
    459  1.11  christos 			return -1;
    460  1.11  christos 		}
    461  1.11  christos 		rdbg(dodebug, " relocating %s (%ld/%ld rel/rela, "
    462  1.11  christos 		    "%ld/%ld plt rel/rela)",
    463  1.11  christos 		    obj->path,
    464  1.11  christos 		    (long)(obj->rellim - obj->rel),
    465  1.11  christos 		    (long)(obj->relalim - obj->rela),
    466  1.11  christos 		    (long)(obj->pltrellim - obj->pltrel),
    467  1.11  christos 		    (long)(obj->pltrelalim - obj->pltrela));
    468  1.11  christos 
    469  1.11  christos 		if (obj->textrel) {
    470  1.11  christos 			/*
    471  1.11  christos 			 * There are relocations to the write-protected text
    472  1.11  christos 			 * segment.
    473  1.11  christos 			 */
    474  1.11  christos 			if (mprotect(obj->mapbase, obj->textsize,
    475  1.11  christos 				PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
    476  1.11  christos 				_rtld_error("%s: Cannot write-enable text "
    477  1.11  christos 				    "segment: %s", obj->path, xstrerror(errno));
    478  1.11  christos 				return -1;
    479  1.11  christos 			}
    480  1.11  christos 		}
    481  1.11  christos 		if (obj->rel != NULL) {
    482  1.11  christos 			/* Process the non-PLT relocations. */
    483  1.11  christos 			const Elf_Rel  *rel;
    484  1.11  christos 			for (rel = obj->rel; rel < obj->rellim; ++rel) {
    485  1.11  christos 				Elf_RelA        ourrela;
    486  1.11  christos 				ourrela.r_info = rel->r_info;
    487  1.11  christos 				ourrela.r_offset = rel->r_offset;
    488   1.2    mhitch #if defined(__mips__)
    489  1.11  christos 				/* rel->r_offset is not valid on mips? */
    490  1.11  christos 				if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
    491  1.11  christos 					ourrela.r_addend = 0;
    492  1.11  christos 				else
    493  1.11  christos #endif
    494  1.11  christos 					ourrela.r_addend =
    495  1.11  christos 					    *(Elf_Word *)(obj->relocbase +
    496  1.11  christos 					    rel->r_offset);
    497  1.11  christos 
    498  1.11  christos 				if (_rtld_relocate_nonplt_object(obj, &ourrela,
    499  1.11  christos 				    dodebug) < 0)
    500  1.11  christos 					ok = 0;
    501  1.11  christos 			}
    502  1.11  christos 		}
    503  1.11  christos 		if (obj->rela != NULL) {
    504  1.11  christos 			/* Process the non-PLT relocations. */
    505  1.11  christos 			const Elf_RelA *rela;
    506  1.11  christos 			for (rela = obj->rela; rela < obj->relalim; ++rela) {
    507  1.11  christos 				if (_rtld_relocate_nonplt_object(obj, rela,
    508  1.11  christos 				    dodebug) < 0)
    509  1.11  christos 					ok = 0;
    510  1.11  christos 			}
    511  1.11  christos 		}
    512  1.11  christos 		if (obj->textrel) {	/* Re-protected the text segment. */
    513  1.11  christos 			if (mprotect(obj->mapbase, obj->textsize,
    514  1.11  christos 				     PROT_READ | PROT_EXEC) == -1) {
    515  1.11  christos 				_rtld_error("%s: Cannot write-protect text "
    516  1.11  christos 				    "segment: %s", obj->path, xstrerror(errno));
    517  1.11  christos 				return -1;
    518  1.11  christos 			}
    519  1.11  christos 		}
    520  1.11  christos 		/* Process the PLT relocations. */
    521  1.11  christos 		if (obj->pltrel != NULL) {
    522  1.11  christos 			const Elf_Rel  *rel;
    523  1.11  christos 			for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
    524  1.11  christos 				Elf_RelA        ourrela;
    525  1.11  christos 				ourrela.r_info = rel->r_info;
    526  1.11  christos 				ourrela.r_offset = rel->r_offset;
    527  1.11  christos 				ourrela.r_addend =
    528  1.11  christos 				    *(Elf_Word *)(obj->relocbase +
    529  1.11  christos 				    rel->r_offset);
    530  1.11  christos 				if (_rtld_relocate_plt_object(obj, &ourrela,
    531  1.11  christos 				    NULL, bind_now, dodebug) < 0)
    532  1.11  christos 					ok = 0;
    533  1.11  christos 			}
    534  1.11  christos 		}
    535  1.11  christos 		if (obj->pltrela != NULL) {
    536  1.11  christos 			const Elf_RelA *rela;
    537  1.11  christos 			for (rela = obj->pltrela; rela < obj->pltrelalim;
    538  1.11  christos 			    ++rela) {
    539  1.11  christos 				if (_rtld_relocate_plt_object(obj, rela,
    540  1.11  christos 				    NULL, bind_now, dodebug) < 0)
    541  1.11  christos 					ok = 0;
    542  1.11  christos 			}
    543  1.11  christos 		}
    544  1.11  christos 		if (!ok)
    545  1.11  christos 			return -1;
    546  1.11  christos 
    547  1.11  christos 
    548  1.11  christos 		/* Set some sanity-checking numbers in the Obj_Entry. */
    549  1.11  christos 		obj->magic = RTLD_MAGIC;
    550  1.11  christos 		obj->version = RTLD_VERSION;
    551  1.11  christos 
    552  1.11  christos 		/* Fill in the dynamic linker entry points. */
    553  1.11  christos 		obj->dlopen = _rtld_dlopen;
    554  1.11  christos 		obj->dlsym = _rtld_dlsym;
    555  1.11  christos 		obj->dlerror = _rtld_dlerror;
    556  1.11  christos 		obj->dlclose = _rtld_dlclose;
    557   1.1       cgd 
    558  1.11  christos 		/* Set the special PLTGOT entries. */
    559  1.11  christos 		if (obj->pltgot != NULL) {
    560   1.1       cgd #if defined(__i386__)
    561  1.11  christos 			obj->pltgot[1] = (Elf_Addr) obj;
    562  1.11  christos 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    563   1.1       cgd #endif
    564   1.1       cgd #if defined(__alpha__)
    565  1.11  christos 			/*
    566  1.11  christos 			 * This function will be called to perform the
    567  1.11  christos 			 * relocation.
    568  1.11  christos 			 */
    569  1.11  christos 			obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
    570  1.11  christos 			/* Identify this shared object */
    571  1.11  christos 			obj->pltgot[3] = (Elf_Addr) obj;
    572   1.2    mhitch #endif
    573   1.2    mhitch #if defined(__mips__)
    574  1.11  christos 			_rtld_relocate_mips_got(obj);
    575   1.2    mhitch 
    576  1.11  christos 			obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
    577  1.11  christos 			/* XXX only if obj->pltgot[1] & 0x80000000 ?? */
    578  1.11  christos 			obj->pltgot[1] |= (Elf_Addr) obj;
    579   1.3    tsubai #endif
    580   1.3    tsubai #if defined(__powerpc__)
    581  1.11  christos 			_rtld_setup_powerpc_plt(obj);
    582   1.9        pk #endif
    583   1.9        pk #if defined(__sparc__)
    584  1.11  christos 			/*
    585  1.11  christos 			 * PLTGOT is the PLT on the sparc.
    586  1.11  christos 			 * The first entry holds the call the dynamic linker.
    587  1.11  christos 			 * We construct a `call' instruction that transfers
    588  1.11  christos 			 * to `_rtld_bind_start()'.
    589  1.11  christos 			 * The second entry holds the object identification.
    590  1.11  christos 			 * Note: each PLT entry is three words long.
    591  1.11  christos 			 */
    592  1.11  christos 			obj->pltgot[1] = 0x40000000 |
    593  1.11  christos 			    ((Elf_Addr)&_rtld_bind_start -
    594  1.11  christos 			    (Elf_Addr)&obj->pltgot[1]);
    595  1.11  christos 			obj->pltgot[3] = (Elf_Addr) obj;
    596   1.1       cgd #endif
    597  1.11  christos 		}
    598   1.1       cgd 	}
    599   1.1       cgd 
    600  1.11  christos 	return 0;
    601   1.1       cgd }
    602