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