Home | History | Annotate | Line # | Download | only in x86_64
mdreloc.c revision 1.28
      1 /*	$NetBSD: mdreloc.c,v 1.28 2007/02/15 15:44:28 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.28 2007/02/15 15:44:28 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 #define COMBRELOC
    135 #ifdef COMBRELOC
    136 	unsigned long lastsym = -1;
    137 #endif
    138 	const Elf_Sym *def = NULL;
    139 	const Obj_Entry *defobj =NULL;
    140 
    141 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    142 		Elf64_Addr *where64;
    143 		Elf32_Addr *where32;
    144 		Elf64_Addr tmp64;
    145 		Elf32_Addr tmp32;
    146 		unsigned long	 symnum;
    147 
    148 		where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
    149 		where32 = (Elf32_Addr *)where64;
    150 		symnum = ELF_R_SYM(rela->r_info);
    151 
    152 		switch (ELF_R_TYPE(rela->r_info)) {
    153 		case R_TYPE(NONE):
    154 			break;
    155 
    156 		case R_TYPE(32):	/* word32 S + A, truncate */
    157 		case R_TYPE(32S):	/* word32 S + A, signed truncate */
    158 		case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
    159 #ifdef COMBRELOC
    160 			if (symnum != lastsym) {
    161 #endif
    162 				def = _rtld_find_symdef(symnum, obj, &defobj,
    163 				    false);
    164 				if (def == NULL)
    165 					return -1;
    166 #ifdef COMBRELOC
    167 				lastsym = symnum;
    168 			}
    169 #endif
    170 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    171 			    def->st_value + rela->r_addend);
    172 
    173 			if (*where32 != tmp32)
    174 				*where32 = tmp32;
    175 			rdbg(("32/32S %s in %s --> %p in %s",
    176 			    obj->strtab + obj->symtab[symnum].st_name,
    177 			    obj->path, (void *)(unsigned long)*where32,
    178 			    defobj->path));
    179 			break;
    180 		case R_TYPE(64):	/* word64 S + A */
    181 #ifdef COMBRELOC
    182 			if (symnum != lastsym) {
    183 #endif
    184 				def = _rtld_find_symdef(symnum, obj, &defobj,
    185 				    false);
    186 				if (def == NULL)
    187 					return -1;
    188 #ifdef COMBRELOC
    189 				lastsym = symnum;
    190 			}
    191 #endif
    192 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
    193 			    rela->r_addend);
    194 
    195 			if (*where64 != tmp64)
    196 				*where64 = tmp64;
    197 			rdbg(("64 %s in %s --> %p in %s",
    198 			    obj->strtab + obj->symtab[symnum].st_name,
    199 			    obj->path, (void *)*where64, defobj->path));
    200 			break;
    201 		case R_TYPE(PC32):	/* word32 S + A - P */
    202 #ifdef COMBRELOC
    203 			if (symnum != lastsym) {
    204 #endif
    205 				def = _rtld_find_symdef(symnum, obj, &defobj,
    206 				    false);
    207 				if (def == NULL)
    208 					return -1;
    209 #ifdef COMBRELOC
    210 				lastsym = symnum;
    211 			}
    212 #endif
    213 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    214 			    def->st_value + rela->r_addend -
    215 			    (Elf64_Addr)where64);
    216 			if (*where32 != tmp32)
    217 				*where32 = tmp32;
    218 			rdbg(("PC32 %s in %s --> %p in %s",
    219 			    obj->strtab + obj->symtab[symnum].st_name,
    220 			    obj->path, (void *)(unsigned long)*where32,
    221 			    defobj->path));
    222 			break;
    223 		case R_TYPE(GLOB_DAT):	/* word64 S */
    224 #ifdef COMBRELOC
    225 			if (symnum != lastsym) {
    226 #endif
    227 				def = _rtld_find_symdef(symnum, obj, &defobj,
    228 				    false);
    229 				if (def == NULL)
    230 					return -1;
    231 #ifdef COMBRELOC
    232 				lastsym = symnum;
    233 			}
    234 #endif
    235 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
    236 
    237 			if (*where64 != tmp64)
    238 				*where64 = tmp64;
    239 			rdbg(("64 %s in %s --> %p in %s",
    240 			    obj->strtab + obj->symtab[symnum].st_name,
    241 			    obj->path, (void *)*where64, defobj->path));
    242 			break;
    243 		case R_TYPE(RELATIVE):  /* word64 B + A */
    244 			tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
    245 			if (*where64 != tmp64)
    246 				*where64 = tmp64;
    247 			rdbg(("RELATIVE in %s --> %p", obj->path,
    248 			    (void *)*where64));
    249        			break;
    250 
    251 		case R_TYPE(COPY):
    252 			rdbg(("COPY"));
    253 			break;
    254 
    255 		default:
    256 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    257 			    "addend = %p, contents = %p, symbol = %s",
    258 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    259 			    (void *)rela->r_offset, (void *)rela->r_addend,
    260 			    (void *)*where64,
    261 			    obj->strtab + obj->symtab[symnum].st_name));
    262 			_rtld_error("%s: Unsupported relocation type %ld "
    263 			    "in non-PLT relocations\n",
    264 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    265 			return -1;
    266 		}
    267 	}
    268 	return 0;
    269 }
    270 
    271 int
    272 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    273 {
    274 	const Elf_Rela *rela;
    275 
    276 	if (!obj->relocbase)
    277 		return 0;
    278 
    279 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    280 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    281 
    282 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    283 
    284 		/* Just relocate the GOT slots pointing into the PLT */
    285 		*where += (Elf_Addr)obj->relocbase;
    286 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    287 	}
    288 
    289 	return 0;
    290 }
    291 
    292 static inline int
    293 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
    294 {
    295 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    296 	Elf_Addr new_value;
    297 	const Elf_Sym  *def;
    298 	const Obj_Entry *defobj;
    299 
    300 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    301 
    302 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    303 	if (def == NULL)
    304 		return -1;
    305 
    306 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
    307 	    rela->r_addend);
    308 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    309 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    310 	if (*where != new_value)
    311 		*where = new_value;
    312 
    313 	if (tp)
    314 		*tp = new_value - rela->r_addend;
    315 
    316 	return 0;
    317 }
    318 
    319 caddr_t
    320 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    321 {
    322 	const Elf_Rela *rela = obj->pltrela + reloff;
    323 	Elf_Addr new_value;
    324 	int err;
    325 
    326 	new_value = 0; /* XXX GCC4 */
    327 
    328 	err = _rtld_relocate_plt_object(obj, rela, &new_value);
    329 	if (err)
    330 		_rtld_die();
    331 
    332 	return (caddr_t)new_value;
    333 }
    334 
    335 int
    336 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    337 {
    338 	const Elf_Rela *rela;
    339 
    340 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
    341 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    342 			return -1;
    343 
    344 	return 0;
    345 }
    346