Home | History | Annotate | Line # | Download | only in x86_64
mdreloc.c revision 1.5.2.1
      1  1.5.2.1     tron /*	$NetBSD: mdreloc.c,v 1.5.2.1 2004/05/28 08:31:23 tron Exp $	*/
      2      1.4  thorpej 
      3      1.4  thorpej /*
      4      1.4  thorpej  * Copyright (c) 2001 Wasabi Systems, Inc.
      5      1.4  thorpej  * All rights reserved.
      6      1.4  thorpej  *
      7      1.4  thorpej  * Written by Frank van der Linden for Wasabi Systems, Inc.
      8      1.4  thorpej  *
      9      1.4  thorpej  * Redistribution and use in source and binary forms, with or without
     10      1.4  thorpej  * modification, are permitted provided that the following conditions
     11      1.4  thorpej  * are met:
     12      1.4  thorpej  * 1. Redistributions of source code must retain the above copyright
     13      1.4  thorpej  *    notice, this list of conditions and the following disclaimer.
     14      1.4  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.4  thorpej  *    notice, this list of conditions and the following disclaimer in the
     16      1.4  thorpej  *    documentation and/or other materials provided with the distribution.
     17      1.4  thorpej  * 3. All advertising materials mentioning features or use of this software
     18      1.4  thorpej  *    must display the following acknowledgement:
     19      1.4  thorpej  *      This product includes software developed for the NetBSD Project by
     20      1.4  thorpej  *      Wasabi Systems, Inc.
     21      1.4  thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22      1.4  thorpej  *    or promote products derived from this software without specific prior
     23      1.4  thorpej  *    written permission.
     24      1.4  thorpej  *
     25      1.4  thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26      1.4  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27      1.4  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28      1.4  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29      1.4  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30      1.4  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31      1.4  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32      1.4  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33      1.4  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34      1.4  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35      1.4  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36      1.5  thorpej  */
     37      1.5  thorpej 
     38      1.5  thorpej /*
     39      1.5  thorpej  * Copyright 1996 John D. Polstra.
     40      1.5  thorpej  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
     41      1.5  thorpej  * All rights reserved.
     42      1.5  thorpej  *
     43      1.5  thorpej  * Redistribution and use in source and binary forms, with or without
     44      1.5  thorpej  * modification, are permitted provided that the following conditions
     45      1.5  thorpej  * are met:
     46      1.5  thorpej  * 1. Redistributions of source code must retain the above copyright
     47      1.5  thorpej  *    notice, this list of conditions and the following disclaimer.
     48      1.5  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     49      1.5  thorpej  *    notice, this list of conditions and the following disclaimer in the
     50      1.5  thorpej  *    documentation and/or other materials provided with the distribution.
     51      1.5  thorpej  * 3. All advertising materials mentioning features or use of this software
     52      1.5  thorpej  *    must display the following acknowledgement:
     53      1.5  thorpej  *      This product includes software developed by John Polstra.
     54      1.5  thorpej  * 4. The name of the author may not be used to endorse or promote products
     55      1.5  thorpej  *    derived from this software without specific prior written permission.
     56      1.5  thorpej  *
     57      1.5  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58      1.5  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59      1.5  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60      1.5  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61      1.5  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62      1.5  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63      1.5  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64      1.5  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65      1.5  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66      1.5  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67      1.4  thorpej  */
     68      1.4  thorpej 
     69      1.1     fvdl #include <sys/types.h>
     70      1.1     fvdl #include <sys/mman.h>
     71      1.1     fvdl #include <err.h>
     72      1.1     fvdl #include <errno.h>
     73      1.1     fvdl #include <fcntl.h>
     74      1.1     fvdl #include <stdarg.h>
     75      1.1     fvdl #include <stdio.h>
     76      1.1     fvdl #include <stdlib.h>
     77      1.1     fvdl #include <string.h>
     78      1.1     fvdl #include <unistd.h>
     79      1.1     fvdl #include <elf.h>
     80      1.1     fvdl 
     81      1.1     fvdl #include "debug.h"
     82      1.1     fvdl #include "rtld.h"
     83      1.1     fvdl 
     84  1.5.2.1     tron void _rtld_bind_start(void);
     85  1.5.2.1     tron void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     86  1.5.2.1     tron caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     87      1.1     fvdl 
     88  1.5.2.1     tron void
     89  1.5.2.1     tron _rtld_setup_pltgot(const Obj_Entry *obj)
     90      1.1     fvdl {
     91  1.5.2.1     tron 	obj->pltgot[1] = (Elf_Addr) obj;
     92  1.5.2.1     tron 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     93  1.5.2.1     tron }
     94      1.1     fvdl 
     95  1.5.2.1     tron void
     96  1.5.2.1     tron _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     97  1.5.2.1     tron {
     98  1.5.2.1     tron 	const Elf_Rela *rela = 0, *relalim;
     99  1.5.2.1     tron 	Elf_Addr relasz = 0;
    100  1.5.2.1     tron 	Elf_Addr *where;
    101  1.5.2.1     tron 
    102  1.5.2.1     tron 	for (; dynp->d_tag != DT_NULL; dynp++) {
    103  1.5.2.1     tron 		switch (dynp->d_tag) {
    104  1.5.2.1     tron 		case DT_RELA:
    105  1.5.2.1     tron 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    106  1.5.2.1     tron 			break;
    107  1.5.2.1     tron 		case DT_RELASZ:
    108  1.5.2.1     tron 			relasz = dynp->d_un.d_val;
    109  1.5.2.1     tron 			break;
    110  1.5.2.1     tron 		}
    111  1.5.2.1     tron 	}
    112  1.5.2.1     tron 	/*
    113  1.5.2.1     tron 	 * Assume only 64-bit relocations here, which should always
    114  1.5.2.1     tron 	 * be true for the dynamic linker.
    115  1.5.2.1     tron 	 */
    116  1.5.2.1     tron 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
    117  1.5.2.1     tron 	for (; rela < relalim; rela++) {
    118  1.5.2.1     tron 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    119  1.5.2.1     tron 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    120  1.5.2.1     tron 	}
    121  1.5.2.1     tron }
    122      1.1     fvdl 
    123  1.5.2.1     tron int
    124  1.5.2.1     tron _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
    125  1.5.2.1     tron {
    126  1.5.2.1     tron 	const Elf_Rela *rela;
    127      1.1     fvdl 
    128  1.5.2.1     tron 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    129  1.5.2.1     tron 		Elf64_Addr *where64;
    130  1.5.2.1     tron 		Elf32_Addr *where32;
    131  1.5.2.1     tron 		const Elf_Sym *def;
    132  1.5.2.1     tron 		const Obj_Entry *defobj;
    133  1.5.2.1     tron 		Elf64_Addr tmp64;
    134  1.5.2.1     tron 		Elf32_Addr tmp32;
    135  1.5.2.1     tron 		unsigned long	 symnum;
    136  1.5.2.1     tron 
    137  1.5.2.1     tron 		where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
    138  1.5.2.1     tron 		where32 = (Elf32_Addr *)where64;
    139  1.5.2.1     tron 		symnum = ELF_R_SYM(rela->r_info);
    140  1.5.2.1     tron 
    141  1.5.2.1     tron 		switch (ELF_R_TYPE(rela->r_info)) {
    142  1.5.2.1     tron 		case R_TYPE(NONE):
    143  1.5.2.1     tron 			break;
    144  1.5.2.1     tron 
    145  1.5.2.1     tron 		case R_TYPE(32):	/* word32 S + A, truncate */
    146  1.5.2.1     tron 		case R_TYPE(32S):	/* word32 S + A, signed truncate */
    147  1.5.2.1     tron 		case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
    148  1.5.2.1     tron 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    149  1.5.2.1     tron 			if (def == NULL)
    150  1.5.2.1     tron 				return -1;
    151  1.5.2.1     tron 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    152  1.5.2.1     tron 			    def->st_value + rela->r_addend);
    153  1.5.2.1     tron 
    154  1.5.2.1     tron 			if (*where32 != tmp32)
    155  1.5.2.1     tron 				*where32 = tmp32;
    156  1.5.2.1     tron 			rdbg(("32/32S %s in %s --> %p in %s",
    157  1.5.2.1     tron 			    obj->strtab + obj->symtab[symnum].st_name,
    158  1.5.2.1     tron 			    obj->path, (void *)(unsigned long)*where32,
    159  1.5.2.1     tron 			    defobj->path));
    160  1.5.2.1     tron 			break;
    161  1.5.2.1     tron 		case R_TYPE(64):	/* word64 S + A */
    162  1.5.2.1     tron 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    163  1.5.2.1     tron 			if (def == NULL)
    164  1.5.2.1     tron 				return -1;
    165  1.5.2.1     tron 
    166  1.5.2.1     tron 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
    167  1.5.2.1     tron 			    rela->r_addend);
    168  1.5.2.1     tron 
    169  1.5.2.1     tron 			if (*where64 != tmp64)
    170  1.5.2.1     tron 				*where64 = tmp64;
    171  1.5.2.1     tron 			rdbg(("64 %s in %s --> %p in %s",
    172  1.5.2.1     tron 			    obj->strtab + obj->symtab[symnum].st_name,
    173  1.5.2.1     tron 			    obj->path, (void *)*where64, defobj->path));
    174  1.5.2.1     tron 			break;
    175  1.5.2.1     tron 		case R_TYPE(PC32):	/* word32 S + A - P */
    176  1.5.2.1     tron 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    177  1.5.2.1     tron 			if (def == NULL)
    178  1.5.2.1     tron 				return -1;
    179  1.5.2.1     tron 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    180  1.5.2.1     tron 			    def->st_value + rela->r_addend -
    181  1.5.2.1     tron 			    (Elf64_Addr)where64);
    182  1.5.2.1     tron 			if (*where32 != tmp32)
    183  1.5.2.1     tron 				*where32 = tmp32;
    184  1.5.2.1     tron 			rdbg(("PC32 %s in %s --> %p in %s",
    185  1.5.2.1     tron 			    obj->strtab + obj->symtab[symnum].st_name,
    186  1.5.2.1     tron 			    obj->path, (void *)(unsigned long)*where32,
    187  1.5.2.1     tron 			    defobj->path));
    188  1.5.2.1     tron 			break;
    189  1.5.2.1     tron 		case R_TYPE(GLOB_DAT):	/* word64 S */
    190  1.5.2.1     tron 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    191  1.5.2.1     tron 			if (def == NULL)
    192  1.5.2.1     tron 				return -1;
    193  1.5.2.1     tron 
    194  1.5.2.1     tron 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
    195  1.5.2.1     tron 
    196  1.5.2.1     tron 			if (*where64 != tmp64)
    197  1.5.2.1     tron 				*where64 = tmp64;
    198  1.5.2.1     tron 			rdbg(("64 %s in %s --> %p in %s",
    199  1.5.2.1     tron 			    obj->strtab + obj->symtab[symnum].st_name,
    200  1.5.2.1     tron 			    obj->path, (void *)*where64, defobj->path));
    201  1.5.2.1     tron 			break;
    202  1.5.2.1     tron 		case R_TYPE(RELATIVE):  /* word64 B + A */
    203  1.5.2.1     tron 			tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
    204  1.5.2.1     tron 			if (*where64 != tmp64)
    205  1.5.2.1     tron 				*where64 = tmp64;
    206  1.5.2.1     tron 			rdbg(("RELATIVE in %s --> %p", obj->path,
    207  1.5.2.1     tron 			    (void *)*where64));
    208  1.5.2.1     tron        			break;
    209  1.5.2.1     tron 
    210  1.5.2.1     tron 		case R_TYPE(COPY):
    211  1.5.2.1     tron 			rdbg(("COPY"));
    212  1.5.2.1     tron 			break;
    213  1.5.2.1     tron 
    214  1.5.2.1     tron 		default:
    215  1.5.2.1     tron 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    216  1.5.2.1     tron 			    "addend = %p, contents = %p, symbol = %s",
    217  1.5.2.1     tron 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    218  1.5.2.1     tron 			    (void *)rela->r_offset, (void *)rela->r_addend,
    219  1.5.2.1     tron 			    (void *)*where64,
    220  1.5.2.1     tron 			    obj->strtab + obj->symtab[symnum].st_name));
    221  1.5.2.1     tron 			_rtld_error("%s: Unsupported relocation type %ld "
    222  1.5.2.1     tron 			    "in non-PLT relocations\n",
    223  1.5.2.1     tron 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    224      1.1     fvdl 			return -1;
    225  1.5.2.1     tron 		}
    226  1.5.2.1     tron 	}
    227  1.5.2.1     tron 	return 0;
    228  1.5.2.1     tron }
    229      1.1     fvdl 
    230  1.5.2.1     tron int
    231  1.5.2.1     tron _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    232  1.5.2.1     tron {
    233  1.5.2.1     tron 	const Elf_Rela *rela;
    234      1.1     fvdl 
    235  1.5.2.1     tron 	if (!obj->relocbase)
    236  1.5.2.1     tron 		return 0;
    237      1.1     fvdl 
    238  1.5.2.1     tron 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    239  1.5.2.1     tron 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    240      1.1     fvdl 
    241  1.5.2.1     tron 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    242      1.1     fvdl 
    243  1.5.2.1     tron 		/* Just relocate the GOT slots pointing into the PLT */
    244  1.5.2.1     tron 		*where += (Elf_Addr)obj->relocbase;
    245  1.5.2.1     tron 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    246      1.1     fvdl 	}
    247  1.5.2.1     tron 
    248      1.1     fvdl 	return 0;
    249      1.1     fvdl }
    250      1.1     fvdl 
    251  1.5.2.1     tron caddr_t
    252  1.5.2.1     tron _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    253      1.1     fvdl {
    254  1.5.2.1     tron 	const Elf_Rela *rela = obj->pltrela + reloff;
    255      1.3     fvdl 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    256      1.3     fvdl 	Elf_Addr new_value;
    257  1.5.2.1     tron 	const Elf_Sym  *def;
    258  1.5.2.1     tron 	const Obj_Entry *defobj;
    259      1.1     fvdl 
    260  1.5.2.1     tron 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    261      1.1     fvdl 
    262  1.5.2.1     tron 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    263  1.5.2.1     tron 	if (def == NULL)
    264  1.5.2.1     tron 		_rtld_die();
    265  1.5.2.1     tron 
    266  1.5.2.1     tron 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
    267  1.5.2.1     tron 	    rela->r_addend);
    268  1.5.2.1     tron 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    269  1.5.2.1     tron 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    270      1.1     fvdl 	if (*where != new_value)
    271      1.1     fvdl 		*where = new_value;
    272  1.5.2.1     tron 
    273  1.5.2.1     tron 	return (caddr_t)(new_value - rela->r_addend);
    274      1.1     fvdl }
    275