Home | History | Annotate | Line # | Download | only in x86_64
mdreloc.c revision 1.32.4.1
      1  1.32.4.1    bouyer /*	$NetBSD: mdreloc.c,v 1.32.4.1 2012/03/17 18:28:35 bouyer 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.26     skrll #include <sys/cdefs.h>
     70      1.26     skrll #ifndef lint
     71  1.32.4.1    bouyer __RCSID("$NetBSD: mdreloc.c,v 1.32.4.1 2012/03/17 18:28:35 bouyer Exp $");
     72      1.26     skrll #endif /* not lint */
     73      1.26     skrll 
     74       1.1      fvdl #include <sys/types.h>
     75       1.1      fvdl #include <sys/mman.h>
     76       1.1      fvdl #include <err.h>
     77       1.1      fvdl #include <errno.h>
     78       1.1      fvdl #include <fcntl.h>
     79       1.1      fvdl #include <stdarg.h>
     80       1.1      fvdl #include <stdio.h>
     81       1.1      fvdl #include <stdlib.h>
     82       1.1      fvdl #include <string.h>
     83       1.1      fvdl #include <unistd.h>
     84       1.1      fvdl #include <elf.h>
     85       1.1      fvdl 
     86       1.1      fvdl #include "debug.h"
     87       1.1      fvdl #include "rtld.h"
     88      1.16   mycroft 
     89      1.16   mycroft void _rtld_bind_start(void);
     90      1.21      fvdl void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     91      1.23     skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     92      1.25     skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
     93      1.25     skrll     const Elf_Rela *, Elf_Addr *);
     94       1.1      fvdl 
     95       1.8   mycroft void
     96       1.8   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
     97       1.8   mycroft {
     98       1.8   mycroft 	obj->pltgot[1] = (Elf_Addr) obj;
     99       1.8   mycroft 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
    100      1.21      fvdl }
    101      1.21      fvdl 
    102      1.21      fvdl void
    103      1.23     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    104      1.21      fvdl {
    105      1.21      fvdl 	const Elf_Rela *rela = 0, *relalim;
    106      1.21      fvdl 	Elf_Addr relasz = 0;
    107      1.21      fvdl 	Elf_Addr *where;
    108      1.21      fvdl 
    109      1.21      fvdl 	for (; dynp->d_tag != DT_NULL; dynp++) {
    110      1.21      fvdl 		switch (dynp->d_tag) {
    111      1.21      fvdl 		case DT_RELA:
    112      1.21      fvdl 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    113      1.21      fvdl 			break;
    114      1.21      fvdl 		case DT_RELASZ:
    115      1.21      fvdl 			relasz = dynp->d_un.d_val;
    116      1.21      fvdl 			break;
    117      1.21      fvdl 		}
    118      1.21      fvdl 	}
    119      1.21      fvdl 	/*
    120      1.21      fvdl 	 * Assume only 64-bit relocations here, which should always
    121      1.21      fvdl 	 * be true for the dynamic linker.
    122      1.21      fvdl 	 */
    123      1.21      fvdl 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
    124      1.21      fvdl 	for (; rela < relalim; rela++) {
    125      1.21      fvdl 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    126      1.21      fvdl 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    127      1.21      fvdl 	}
    128       1.8   mycroft }
    129       1.8   mycroft 
    130       1.8   mycroft int
    131      1.23     skrll _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
    132       1.1      fvdl {
    133       1.9   mycroft 	const Elf_Rela *rela;
    134      1.28     skrll 	const Elf_Sym *def = NULL;
    135      1.28     skrll 	const Obj_Entry *defobj =NULL;
    136       1.1      fvdl 
    137       1.9   mycroft 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    138       1.9   mycroft 		Elf64_Addr *where64;
    139       1.9   mycroft 		Elf32_Addr *where32;
    140       1.9   mycroft 		Elf64_Addr tmp64;
    141       1.9   mycroft 		Elf32_Addr tmp32;
    142      1.10   mycroft 		unsigned long	 symnum;
    143       1.1      fvdl 
    144       1.9   mycroft 		where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
    145       1.9   mycroft 		where32 = (Elf32_Addr *)where64;
    146      1.10   mycroft 		symnum = ELF_R_SYM(rela->r_info);
    147       1.1      fvdl 
    148       1.9   mycroft 		switch (ELF_R_TYPE(rela->r_info)) {
    149       1.9   mycroft 		case R_TYPE(NONE):
    150       1.9   mycroft 			break;
    151       1.9   mycroft 
    152       1.9   mycroft 		case R_TYPE(32):	/* word32 S + A, truncate */
    153       1.9   mycroft 		case R_TYPE(32S):	/* word32 S + A, signed truncate */
    154       1.9   mycroft 		case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
    155      1.29      matt 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    156      1.29      matt 			if (def == NULL)
    157      1.29      matt 				return -1;
    158       1.9   mycroft 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    159       1.9   mycroft 			    def->st_value + rela->r_addend);
    160       1.9   mycroft 
    161       1.9   mycroft 			if (*where32 != tmp32)
    162       1.9   mycroft 				*where32 = tmp32;
    163      1.17   mycroft 			rdbg(("32/32S %s in %s --> %p in %s",
    164      1.11   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    165      1.11   mycroft 			    obj->path, (void *)(unsigned long)*where32,
    166      1.11   mycroft 			    defobj->path));
    167       1.9   mycroft 			break;
    168       1.9   mycroft 		case R_TYPE(64):	/* word64 S + A */
    169      1.29      matt 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    170      1.29      matt 			if (def == NULL)
    171      1.29      matt 				return -1;
    172       1.9   mycroft 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
    173       1.9   mycroft 			    rela->r_addend);
    174       1.9   mycroft 
    175       1.9   mycroft 			if (*where64 != tmp64)
    176       1.9   mycroft 				*where64 = tmp64;
    177      1.17   mycroft 			rdbg(("64 %s in %s --> %p in %s",
    178      1.11   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    179      1.11   mycroft 			    obj->path, (void *)*where64, defobj->path));
    180       1.9   mycroft 			break;
    181       1.9   mycroft 		case R_TYPE(PC32):	/* word32 S + A - P */
    182      1.29      matt 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    183      1.29      matt 			if (def == NULL)
    184      1.29      matt 				return -1;
    185       1.9   mycroft 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    186       1.9   mycroft 			    def->st_value + rela->r_addend -
    187       1.9   mycroft 			    (Elf64_Addr)where64);
    188       1.9   mycroft 			if (*where32 != tmp32)
    189       1.9   mycroft 				*where32 = tmp32;
    190      1.17   mycroft 			rdbg(("PC32 %s in %s --> %p in %s",
    191      1.11   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    192      1.11   mycroft 			    obj->path, (void *)(unsigned long)*where32,
    193      1.11   mycroft 			    defobj->path));
    194       1.9   mycroft 			break;
    195       1.9   mycroft 		case R_TYPE(GLOB_DAT):	/* word64 S */
    196      1.29      matt 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    197      1.29      matt 			if (def == NULL)
    198      1.29      matt 				return -1;
    199       1.9   mycroft 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
    200       1.9   mycroft 
    201       1.9   mycroft 			if (*where64 != tmp64)
    202       1.9   mycroft 				*where64 = tmp64;
    203      1.17   mycroft 			rdbg(("64 %s in %s --> %p in %s",
    204      1.11   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    205      1.11   mycroft 			    obj->path, (void *)*where64, defobj->path));
    206       1.9   mycroft 			break;
    207       1.9   mycroft 		case R_TYPE(RELATIVE):  /* word64 B + A */
    208       1.9   mycroft 			tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
    209       1.9   mycroft 			if (*where64 != tmp64)
    210       1.9   mycroft 				*where64 = tmp64;
    211      1.17   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    212       1.9   mycroft 			    (void *)*where64));
    213       1.9   mycroft        			break;
    214       1.9   mycroft 
    215       1.9   mycroft 		case R_TYPE(COPY):
    216      1.17   mycroft 			rdbg(("COPY"));
    217       1.9   mycroft 			break;
    218       1.9   mycroft 
    219       1.9   mycroft 		default:
    220      1.17   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    221       1.9   mycroft 			    "addend = %p, contents = %p, symbol = %s",
    222      1.10   mycroft 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    223       1.9   mycroft 			    (void *)rela->r_offset, (void *)rela->r_addend,
    224       1.9   mycroft 			    (void *)*where64,
    225      1.10   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    226       1.9   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    227       1.9   mycroft 			    "in non-PLT relocations\n",
    228       1.9   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    229       1.1      fvdl 			return -1;
    230       1.9   mycroft 		}
    231       1.1      fvdl 	}
    232      1.12   mycroft 	return 0;
    233      1.12   mycroft }
    234      1.12   mycroft 
    235      1.12   mycroft int
    236      1.23     skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    237      1.12   mycroft {
    238      1.12   mycroft 	const Elf_Rela *rela;
    239      1.12   mycroft 
    240      1.20   mycroft 	if (!obj->relocbase)
    241      1.12   mycroft 		return 0;
    242      1.12   mycroft 
    243      1.12   mycroft 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    244      1.12   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    245      1.12   mycroft 
    246      1.12   mycroft 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    247      1.12   mycroft 
    248      1.12   mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    249      1.12   mycroft 		*where += (Elf_Addr)obj->relocbase;
    250      1.17   mycroft 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    251      1.12   mycroft 	}
    252      1.12   mycroft 
    253      1.12   mycroft 	return 0;
    254      1.12   mycroft }
    255      1.12   mycroft 
    256      1.24     skrll static inline int
    257      1.24     skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
    258      1.12   mycroft {
    259      1.12   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    260      1.12   mycroft 	Elf_Addr new_value;
    261      1.12   mycroft 	const Elf_Sym  *def;
    262      1.12   mycroft 	const Obj_Entry *defobj;
    263  1.32.4.1    bouyer 	unsigned long info = rela->r_info;
    264      1.12   mycroft 
    265  1.32.4.1    bouyer 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    266      1.12   mycroft 
    267  1.32.4.1    bouyer 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    268  1.32.4.1    bouyer 	if (__predict_false(def == NULL))
    269      1.24     skrll 		return -1;
    270  1.32.4.1    bouyer 	if (__predict_false(def == &_rtld_sym_zero))
    271  1.32.4.1    bouyer 		return 0;
    272  1.32.4.1    bouyer 
    273      1.12   mycroft 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
    274      1.12   mycroft 	    rela->r_addend);
    275      1.32      matt 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    276      1.31  christos 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    277      1.12   mycroft 	if (*where != new_value)
    278      1.12   mycroft 		*where = new_value;
    279      1.12   mycroft 
    280      1.24     skrll 	if (tp)
    281      1.24     skrll 		*tp = new_value - rela->r_addend;
    282      1.24     skrll 
    283      1.24     skrll 	return 0;
    284      1.24     skrll }
    285      1.24     skrll 
    286      1.24     skrll caddr_t
    287      1.24     skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    288      1.24     skrll {
    289      1.24     skrll 	const Elf_Rela *rela = obj->pltrela + reloff;
    290      1.24     skrll 	Elf_Addr new_value;
    291      1.24     skrll 	int err;
    292      1.24     skrll 
    293      1.27       dan 	new_value = 0; /* XXX GCC4 */
    294      1.27       dan 
    295      1.24     skrll 	err = _rtld_relocate_plt_object(obj, rela, &new_value);
    296  1.32.4.1    bouyer 	if (err)
    297      1.24     skrll 		_rtld_die();
    298      1.24     skrll 
    299      1.24     skrll 	return (caddr_t)new_value;
    300      1.24     skrll }
    301      1.24     skrll 
    302      1.24     skrll int
    303      1.24     skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
    304      1.24     skrll {
    305      1.24     skrll 	const Elf_Rela *rela;
    306      1.24     skrll 
    307      1.24     skrll 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
    308      1.24     skrll 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    309      1.24     skrll 			return -1;
    310      1.24     skrll 
    311      1.24     skrll 	return 0;
    312       1.1      fvdl }
    313