Home | History | Annotate | Line # | Download | only in x86_64
mdreloc.c revision 1.4
      1  1.4  thorpej /*	$NetBSD: mdreloc.c,v 1.4 2001/12/14 22:07:23 thorpej 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.4  thorpej  */
     37  1.4  thorpej 
     38  1.1     fvdl #include <sys/types.h>
     39  1.1     fvdl #include <sys/mman.h>
     40  1.1     fvdl #include <err.h>
     41  1.1     fvdl #include <errno.h>
     42  1.1     fvdl #include <fcntl.h>
     43  1.1     fvdl #include <stdarg.h>
     44  1.1     fvdl #include <stdio.h>
     45  1.1     fvdl #include <stdlib.h>
     46  1.1     fvdl #include <string.h>
     47  1.1     fvdl #include <unistd.h>
     48  1.1     fvdl #include <elf.h>
     49  1.1     fvdl 
     50  1.1     fvdl #include "debug.h"
     51  1.1     fvdl #include "rtld.h"
     52  1.1     fvdl 
     53  1.1     fvdl extern Elf64_Addr _GLOBAL_OFFSET_TABLE_[];
     54  1.1     fvdl 
     55  1.1     fvdl int
     56  1.2     fvdl _rtld_relocate_nonplt_object(Obj_Entry *obj, const Elf_Rela *rela, bool dodebug)
     57  1.1     fvdl {
     58  1.1     fvdl 	Elf64_Addr *where64;
     59  1.1     fvdl 	Elf32_Addr *where32;
     60  1.1     fvdl 	const Elf_Sym *def;
     61  1.1     fvdl 	const Obj_Entry *defobj;
     62  1.1     fvdl 	Elf64_Addr tmp64;
     63  1.1     fvdl 	Elf32_Addr tmp32;
     64  1.1     fvdl 
     65  1.1     fvdl 	where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
     66  1.1     fvdl 	where32 = (Elf32_Addr *)where64;
     67  1.1     fvdl 
     68  1.1     fvdl 	switch (ELF_R_TYPE(rela->r_info)) {
     69  1.1     fvdl 
     70  1.1     fvdl 	case R_TYPE(NONE):
     71  1.1     fvdl 		break;
     72  1.1     fvdl 
     73  1.1     fvdl 	case R_TYPE(32):	/* word32 S + A, truncate */
     74  1.1     fvdl 	case R_TYPE(32S):	/* word32 S + A, signed truncate */
     75  1.1     fvdl 	case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
     76  1.1     fvdl 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
     77  1.1     fvdl 		    &defobj, false);
     78  1.1     fvdl 		if (def == NULL)
     79  1.1     fvdl 			return -1;
     80  1.1     fvdl 		tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase + def->st_value +
     81  1.1     fvdl 		    rela->r_addend);
     82  1.1     fvdl 
     83  1.1     fvdl 		if (*where32 != tmp32)
     84  1.1     fvdl 			*where32 = tmp32;
     85  1.1     fvdl 		rdbg(dodebug, ("32/32S %s in %s --> %p in %s",
     86  1.1     fvdl 		    defobj->strtab + def->st_name, obj->path,
     87  1.1     fvdl 		    (void *)(unsigned long)*where32, defobj->path));
     88  1.1     fvdl 		break;
     89  1.1     fvdl 	case R_TYPE(64):	/* word64 S + A */
     90  1.1     fvdl 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
     91  1.1     fvdl 		    &defobj, false);
     92  1.1     fvdl 		if (def == NULL)
     93  1.1     fvdl 			return -1;
     94  1.1     fvdl 
     95  1.1     fvdl 		tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
     96  1.1     fvdl 		    rela->r_addend);
     97  1.1     fvdl 
     98  1.1     fvdl 		if (*where64 != tmp64)
     99  1.1     fvdl 			*where64 = tmp64;
    100  1.1     fvdl 		rdbg(dodebug, ("64 %s in %s --> %p in %s",
    101  1.1     fvdl 		    defobj->strtab + def->st_name, obj->path,
    102  1.1     fvdl 		    (void *)*where64, defobj->path));
    103  1.1     fvdl 		break;
    104  1.1     fvdl 	case R_TYPE(PC32):	/* word32 S + A - P */
    105  1.1     fvdl 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    106  1.1     fvdl 		    &defobj, false);
    107  1.1     fvdl 		if (def == NULL)
    108  1.1     fvdl 			return -1;
    109  1.1     fvdl 		tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase + def->st_value +
    110  1.1     fvdl 		    rela->r_addend - (Elf64_Addr)where64);
    111  1.1     fvdl 		if (*where32 != tmp32)
    112  1.1     fvdl 			*where32 = tmp32;
    113  1.1     fvdl 		rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
    114  1.1     fvdl 		    defobj->strtab + def->st_name, obj->path,
    115  1.1     fvdl 		    (void *)(unsigned long)*where32, defobj->path));
    116  1.1     fvdl 		break;
    117  1.1     fvdl 	case R_TYPE(GLOB_DAT):	/* word64 S */
    118  1.1     fvdl 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    119  1.1     fvdl 		    &defobj, false);
    120  1.1     fvdl 		if (def == NULL)
    121  1.1     fvdl 			return -1;
    122  1.1     fvdl 
    123  1.1     fvdl 		tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
    124  1.1     fvdl 
    125  1.1     fvdl 		if (*where64 != tmp64)
    126  1.1     fvdl 			*where64 = tmp64;
    127  1.1     fvdl 		rdbg(dodebug, ("64 %s in %s --> %p in %s",
    128  1.1     fvdl 		    defobj->strtab + def->st_name, obj->path,
    129  1.1     fvdl 		    (void *)*where64, defobj->path));
    130  1.1     fvdl 		break;
    131  1.1     fvdl 	case R_TYPE(RELATIVE):  /* word64 B + A */
    132  1.1     fvdl 		tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
    133  1.1     fvdl 		if (*where64 != tmp64)
    134  1.1     fvdl 			*where64 = tmp64;
    135  1.1     fvdl 		rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
    136  1.1     fvdl 		    (void *)*where64));
    137  1.1     fvdl                 break;
    138  1.1     fvdl 
    139  1.1     fvdl 	case R_TYPE(COPY):
    140  1.1     fvdl 		rdbg(dodebug, ("COPY"));
    141  1.1     fvdl 		break;
    142  1.1     fvdl 
    143  1.1     fvdl 	default:
    144  1.1     fvdl 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    145  1.1     fvdl 		    &defobj, true);
    146  1.1     fvdl 		rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    147  1.1     fvdl 		    "addend = %p, contents = %p, symbol = %s",
    148  1.1     fvdl 		    (u_long)ELF_R_SYM(rela->r_info),
    149  1.1     fvdl 		    (u_long)ELF_R_TYPE(rela->r_info),
    150  1.1     fvdl 		    (void *)rela->r_offset, (void *)rela->r_addend,
    151  1.1     fvdl 		    (void *)*where64,
    152  1.1     fvdl 		    def ? defobj->strtab + def->st_name : "??"));
    153  1.1     fvdl 		_rtld_error("%s: Unsupported relocation type %ld "
    154  1.1     fvdl 		    "in non-PLT relocations\n",
    155  1.1     fvdl 		    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    156  1.1     fvdl 		return -1;
    157  1.1     fvdl 	}
    158  1.1     fvdl 	return 0;
    159  1.1     fvdl }
    160  1.1     fvdl 
    161  1.1     fvdl 
    162  1.1     fvdl 
    163  1.1     fvdl int
    164  1.2     fvdl _rtld_relocate_plt_object(Obj_Entry *obj, const Elf_Rela *rela, caddr_t *addrp,
    165  1.1     fvdl 			  bool bind_now, bool dodebug)
    166  1.1     fvdl {
    167  1.3     fvdl 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    168  1.3     fvdl 	Elf_Addr new_value;
    169  1.1     fvdl 
    170  1.1     fvdl 	/* Fully resolve procedure addresses now */
    171  1.1     fvdl 
    172  1.1     fvdl 	if (bind_now || obj->pltgot == NULL) {
    173  1.1     fvdl 		const Elf_Sym  *def;
    174  1.1     fvdl 		const Obj_Entry *defobj;
    175  1.1     fvdl 
    176  1.1     fvdl 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    177  1.1     fvdl 
    178  1.1     fvdl 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
    179  1.1     fvdl 		    &defobj, true);
    180  1.1     fvdl 		if (def == NULL)
    181  1.1     fvdl 			return -1;
    182  1.1     fvdl 
    183  1.3     fvdl 		new_value = (Elf_Addr)
    184  1.1     fvdl 		    (defobj->relocbase + def->st_value + rela->r_addend);
    185  1.1     fvdl 		rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
    186  1.1     fvdl 		    (int)bind_now,
    187  1.1     fvdl 		    defobj->strtab + def->st_name,
    188  1.3     fvdl 		    (void *)*where, (void *)new_value));
    189  1.1     fvdl 	} else {
    190  1.1     fvdl 		if (!obj->mainprog) {
    191  1.1     fvdl 			/* Just relocate the GOT slots pointing into the PLT */
    192  1.3     fvdl 			new_value = *where + (Elf_Addr) obj->relocbase;
    193  1.1     fvdl 			rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    194  1.1     fvdl 			    (void *)(unsigned long)*where));
    195  1.1     fvdl 		} else {
    196  1.1     fvdl 			return 0;
    197  1.1     fvdl 		}
    198  1.1     fvdl 	}
    199  1.1     fvdl 	/*
    200  1.1     fvdl          * Since this page is probably copy-on-write, let's not write
    201  1.1     fvdl          * it unless we really really have to.
    202  1.1     fvdl          */
    203  1.1     fvdl 	if (*where != new_value)
    204  1.1     fvdl 		*where = new_value;
    205  1.1     fvdl 	if (addrp != NULL)
    206  1.1     fvdl 		*addrp = *(caddr_t *)(obj->relocbase + rela->r_offset) -
    207  1.1     fvdl 		    rela->r_addend;
    208  1.1     fvdl 	return 0;
    209  1.1     fvdl }
    210