Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.108
      1 /*	$NetBSD: reloc.c,v 1.108 2016/04/12 19:10:48 christos Exp $	 */
      2 
      3 /*
      4  * Copyright 1996 John D. Polstra.
      5  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by John Polstra.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Dynamic linker for ELF.
     36  *
     37  * John Polstra <jdp (at) polstra.com>.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 __RCSID("$NetBSD: reloc.c,v 1.108 2016/04/12 19:10:48 christos Exp $");
     43 #endif /* not lint */
     44 
     45 #include <err.h>
     46 #include <errno.h>
     47 #include <fcntl.h>
     48 #include <stdarg.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 #include <sys/types.h>
     54 #include <sys/mman.h>
     55 #include <sys/bitops.h>
     56 #include <dirent.h>
     57 
     58 #include "debug.h"
     59 #include "rtld.h"
     60 
     61 #ifndef RTLD_INHIBIT_COPY_RELOCS
     62 static int _rtld_do_copy_relocation(const Obj_Entry *, const Elf_Rela *);
     63 
     64 static int
     65 _rtld_do_copy_relocation(const Obj_Entry *dstobj, const Elf_Rela *rela)
     66 {
     67 	void           *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
     68 	const Elf_Sym  *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
     69 	const char     *name = dstobj->strtab + dstsym->st_name;
     70 	unsigned long   hash = _rtld_elf_hash(name);
     71 	size_t          size = dstsym->st_size;
     72 	const void     *srcaddr;
     73 	const Elf_Sym  *srcsym = NULL;
     74 	Obj_Entry      *srcobj;
     75 
     76 	for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) {
     77 		srcsym = _rtld_symlook_obj(name, hash, srcobj, 0,
     78 		    _rtld_fetch_ventry(dstobj, ELF_R_SYM(rela->r_info)));
     79 		if (srcsym != NULL)
     80 			break;
     81 	}
     82 
     83 	if (srcobj == NULL) {
     84 		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
     85 		    " relocation in %s", name, dstobj->path);
     86 		return (-1);
     87 	}
     88 	srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
     89 	(void)memcpy(dstaddr, srcaddr, size);
     90 	rdbg(("COPY %s %s %s --> src=%p dst=%p size %ld",
     91 	    dstobj->path, srcobj->path, name, srcaddr,
     92 	    (void *)dstaddr, (long)size));
     93 	return (0);
     94 }
     95 #endif /* RTLD_INHIBIT_COPY_RELOCS */
     96 
     97 
     98 /*
     99  * Process the special R_xxx_COPY relocations in the main program.  These
    100  * copy data from a shared object into a region in the main program's BSS
    101  * segment.
    102  *
    103  * Returns 0 on success, -1 on failure.
    104  */
    105 int
    106 _rtld_do_copy_relocations(const Obj_Entry *dstobj)
    107 {
    108 #ifndef RTLD_INHIBIT_COPY_RELOCS
    109 
    110 	/* COPY relocations are invalid elsewhere */
    111 	assert(!dstobj->isdynamic);
    112 
    113 	if (dstobj->rel != NULL) {
    114 		const Elf_Rel  *rel;
    115 		for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
    116 			if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
    117 				Elf_Rela        ourrela;
    118 				ourrela.r_info = rel->r_info;
    119 				ourrela.r_offset = rel->r_offset;
    120 				ourrela.r_addend = 0;
    121 				if (_rtld_do_copy_relocation(dstobj,
    122 				    &ourrela) < 0)
    123 					return (-1);
    124 			}
    125 		}
    126 	}
    127 	if (dstobj->rela != NULL) {
    128 		const Elf_Rela *rela;
    129 		for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
    130 			if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
    131 				if (_rtld_do_copy_relocation(dstobj, rela) < 0)
    132 					return (-1);
    133 			}
    134 		}
    135 	}
    136 #endif /* RTLD_INHIBIT_COPY_RELOCS */
    137 
    138 	return (0);
    139 }
    140 
    141 /*
    142  * Relocate newly-loaded shared objects.  The argument is a pointer to
    143  * the Obj_Entry for the first such object.  All objects from the first
    144  * to the end of the list of objects are relocated.  Returns 0 on success,
    145  * or -1 on failure.
    146  */
    147 int
    148 _rtld_relocate_objects(Obj_Entry *first, bool bind_now)
    149 {
    150 	Obj_Entry *obj;
    151 	int ok = 1;
    152 
    153 	for (obj = first; obj != NULL; obj = obj->next) {
    154 		if (obj->nbuckets == 0 || obj->nchains == 0 ||
    155 		    obj->buckets == NULL || obj->symtab == NULL ||
    156 		    obj->strtab == NULL) {
    157 			_rtld_error("%s: Shared object has no run-time"
    158 			    " symbol table", obj->path);
    159 			return -1;
    160 		}
    161 		if (obj->nbuckets == UINT32_MAX) {
    162 			_rtld_error("%s: Symbol table too large", obj->path);
    163 			return -1;
    164 		}
    165 		rdbg((" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)",
    166 		    obj->path,
    167 		    (long)(obj->rellim - obj->rel),
    168 		    (long)(obj->relalim - obj->rela),
    169 		    (long)(obj->pltrellim - obj->pltrel),
    170 		    (long)(obj->pltrelalim - obj->pltrela)));
    171 
    172 		if (obj->textrel) {
    173 			xwarnx("%s: text relocations", obj->path);
    174 			/*
    175 			 * There are relocations to the write-protected text
    176 			 * segment.
    177 			 */
    178 			if (mprotect(obj->mapbase, obj->textsize,
    179 				PROT_READ | PROT_WRITE) == -1) {
    180 				_rtld_error("%s: Cannot write-enable text "
    181 				    "segment: %s", obj->path, xstrerror(errno));
    182 				return -1;
    183 			}
    184 		}
    185 		dbg(("doing non-PLT relocations"));
    186 		if (_rtld_relocate_nonplt_objects(obj) < 0)
    187 			ok = 0;
    188 		if (obj->textrel) {	/* Re-protected the text segment. */
    189 			if (mprotect(obj->mapbase, obj->textsize,
    190 				     PROT_READ | PROT_EXEC) == -1) {
    191 				_rtld_error("%s: Cannot write-protect text "
    192 				    "segment: %s", obj->path, xstrerror(errno));
    193 				return -1;
    194 			}
    195 		}
    196 		dbg(("doing lazy PLT binding"));
    197 		if (_rtld_relocate_plt_lazy(obj) < 0)
    198 			ok = 0;
    199 		if (obj->z_now || bind_now) {
    200 			dbg(("doing immediate PLT binding"));
    201 			if (_rtld_relocate_plt_objects(obj) < 0)
    202 				ok = 0;
    203 		}
    204 		if (!ok)
    205 			return -1;
    206 
    207 		/* Set some sanity-checking numbers in the Obj_Entry. */
    208 		obj->magic = RTLD_MAGIC;
    209 		obj->version = RTLD_VERSION;
    210 
    211 		/*
    212 		 * Fill in the backwards compatibility dynamic linker entry points.
    213 		 *
    214 		 * DO NOT ADD TO THIS LIST
    215 		 */
    216 		obj->dlopen = dlopen;
    217 		obj->dlsym = dlsym;
    218 		obj->dlerror = dlerror;
    219 		obj->dlclose = dlclose;
    220 		obj->dladdr = dladdr;
    221 
    222 		dbg(("fixing up PLTGOT"));
    223 		/* Set the special PLTGOT entries. */
    224 		if (obj->pltgot != NULL)
    225 			_rtld_setup_pltgot(obj);
    226 	}
    227 
    228 	return 0;
    229 }
    230 
    231 Elf_Addr
    232 _rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def)
    233 {
    234 	Elf_Addr target;
    235 
    236 	_rtld_shared_exit();
    237 	target = _rtld_call_function_addr(obj,
    238 	    (Elf_Addr)obj->relocbase + def->st_value);
    239 	_rtld_shared_enter();
    240 
    241 	return target;
    242 }
    243