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