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