Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.72
      1 /*	$NetBSD: reloc.c,v 1.72 2002/09/24 20:23:11 mycroft 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 <err.h>
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <stdarg.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 #include <sys/types.h>
     49 #include <sys/mman.h>
     50 #include <dirent.h>
     51 
     52 #include "debug.h"
     53 #include "rtld.h"
     54 
     55 #ifndef RTLD_INHIBIT_COPY_RELOCS
     56 static int _rtld_do_copy_relocation __P((const Obj_Entry *, const Elf_Rela *));
     57 
     58 /*
     59  * XXX: These don't work for the alpha and i386; don't know about powerpc
     60  *	The alpha and the i386 avoid the problem by compiling everything PIC.
     61  *	These relocation are supposed to be writing the address of the
     62  *	function to be called on the bss.rel or bss.rela segment, but:
     63  *		- st_size == 0
     64  *		- on the i386 at least the call instruction is a direct call
     65  *		  not an indirect call.
     66  */
     67 static int
     68 _rtld_do_copy_relocation(dstobj, rela)
     69 	const Obj_Entry *dstobj;
     70 	const Elf_Rela *rela;
     71 {
     72 	void           *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
     73 	const Elf_Sym  *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
     74 	const char     *name = dstobj->strtab + dstsym->st_name;
     75 	unsigned long   hash = _rtld_elf_hash(name);
     76 	size_t          size = dstsym->st_size;
     77 	const void     *srcaddr;
     78 	const Elf_Sym  *srcsym = NULL;
     79 	Obj_Entry      *srcobj;
     80 
     81 	for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
     82 		if ((srcsym = _rtld_symlook_obj(name, hash, srcobj, false)) != NULL)
     83 			break;
     84 
     85 	if (srcobj == NULL) {
     86 		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
     87 		    " relocation in %s", name, dstobj->path);
     88 		return (-1);
     89 	}
     90 	srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
     91 	(void)memcpy(dstaddr, srcaddr, size);
     92 	rdbg(("COPY %s %s %s --> src=%p dst=%p *dst= %p size %ld",
     93 	    dstobj->path, srcobj->path, name, (void *)srcaddr,
     94 	    (void *)dstaddr, (void *)*(long *)dstaddr, (long)size));
     95 	return (0);
     96 }
     97 #endif /* RTLD_INHIBIT_COPY_RELOCS */
     98 
     99 
    100 /*
    101  * Process the special R_xxx_COPY relocations in the main program.  These
    102  * copy data from a shared object into a region in the main program's BSS
    103  * segment.
    104  *
    105  * Returns 0 on success, -1 on failure.
    106  */
    107 int
    108 _rtld_do_copy_relocations(dstobj)
    109 	const Obj_Entry *dstobj;
    110 {
    111 #ifndef RTLD_INHIBIT_COPY_RELOCS
    112 
    113 	/* COPY relocations are invalid elsewhere */
    114 	assert(!dstobj->isdynamic);
    115 
    116 	if (dstobj->rel != NULL) {
    117 		const Elf_Rel  *rel;
    118 		for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
    119 			if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
    120 				Elf_Rela        ourrela;
    121 				ourrela.r_info = rel->r_info;
    122 				ourrela.r_offset = rel->r_offset;
    123 				ourrela.r_addend = 0;
    124 				if (_rtld_do_copy_relocation(dstobj,
    125 				    &ourrela) < 0)
    126 					return (-1);
    127 			}
    128 		}
    129 	}
    130 	if (dstobj->rela != NULL) {
    131 		const Elf_Rela *rela;
    132 		for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
    133 			if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
    134 				if (_rtld_do_copy_relocation(dstobj, rela) < 0)
    135 					return (-1);
    136 			}
    137 		}
    138 	}
    139 #endif /* RTLD_INHIBIT_COPY_RELOCS */
    140 
    141 	return (0);
    142 }
    143 
    144 #if !defined(__mips__)
    145 caddr_t
    146 _rtld_bind(obj, reloff)
    147 	const Obj_Entry *obj;
    148 	Elf_Word reloff;
    149 {
    150 	const Elf_Rela *rela;
    151 	Elf_Rela        ourrela;
    152 	caddr_t		addr;
    153 
    154 	if (obj->pltrel != NULL) {
    155 		const Elf_Rel *rel;
    156 
    157 		rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
    158 		ourrela.r_info = rel->r_info;
    159 		ourrela.r_offset = rel->r_offset;
    160 		ourrela.r_addend = 0;
    161 		rela = &ourrela;
    162 	} else {
    163 		rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
    164 #ifdef __sparc64__
    165 		if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
    166 			/*
    167 			 * XXXX
    168 			 *
    169 			 * The first four PLT entries are reserved.  There
    170 			 * is some disagreement whether they should have
    171 			 * associated relocation entries.  Both the SPARC
    172 			 * 32-bit and 64-bit ELF specifications say that
    173 			 * they should have relocation entries, but the
    174 			 * 32-bit SPARC binutils do not generate them,
    175 			 * and now the 64-bit SPARC binutils have stopped
    176 			 * generating them too.
    177 			 *
    178 			 * So, to provide binary compatibility, we will
    179 			 * check the first entry, if it is reserved it
    180 			 * should not be of the type JMP_SLOT.  If it
    181 			 * is JMP_SLOT, then the 4 reserved entries were
    182 			 * not generated and our index is 4 entries too far.
    183 			 */
    184 			rela -= 4;
    185 		}
    186 #endif
    187 	}
    188 
    189 	if (_rtld_relocate_plt_object(obj, rela, &addr) < 0)
    190 		_rtld_die();
    191 
    192 	return addr;
    193 }
    194 #endif
    195 
    196 /*
    197  * Relocate newly-loaded shared objects.  The argument is a pointer to
    198  * the Obj_Entry for the first such object.  All objects from the first
    199  * to the end of the list of objects are relocated.  Returns 0 on success,
    200  * or -1 on failure.
    201  */
    202 int
    203 _rtld_relocate_objects(first, bind_now, self)
    204 	Obj_Entry *first;
    205 	bool bind_now;
    206 	bool self;
    207 {
    208 	Obj_Entry *obj;
    209 	int ok = 1;
    210 
    211 	for (obj = first; obj != NULL; obj = obj->next) {
    212 		if (obj->nbuckets == 0 || obj->nchains == 0 ||
    213 		    obj->buckets == NULL || obj->symtab == NULL ||
    214 		    obj->strtab == NULL) {
    215 			_rtld_error("%s: Shared object has no run-time"
    216 			    " symbol table", obj->path);
    217 			return -1;
    218 		}
    219 		rdbg((" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)",
    220 		    obj->path,
    221 		    (long)(obj->rellim - obj->rel),
    222 		    (long)(obj->relalim - obj->rela),
    223 		    (long)(obj->pltrellim - obj->pltrel),
    224 		    (long)(obj->pltrelalim - obj->pltrela)));
    225 
    226 		if (obj->textrel) {
    227 			/*
    228 			 * There are relocations to the write-protected text
    229 			 * segment.
    230 			 */
    231 			if (mprotect(obj->mapbase, obj->textsize,
    232 				PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
    233 				_rtld_error("%s: Cannot write-enable text "
    234 				    "segment: %s", obj->path, xstrerror(errno));
    235 				return -1;
    236 			}
    237 		}
    238 		dbg(("doing non-PLT relocations"));
    239 		if (_rtld_relocate_nonplt_objects(obj, self) < 0)
    240 			ok = 0;
    241 		if (obj->textrel) {	/* Re-protected the text segment. */
    242 			if (mprotect(obj->mapbase, obj->textsize,
    243 				     PROT_READ | PROT_EXEC) == -1) {
    244 				_rtld_error("%s: Cannot write-protect text "
    245 				    "segment: %s", obj->path, xstrerror(errno));
    246 				return -1;
    247 			}
    248 		}
    249 		dbg(("doing lazy PLT binding"));
    250 		if (_rtld_relocate_plt_lazy(obj) < 0)
    251 			ok = 0;
    252 #if defined(__i386__)
    253 		if (bind_now)
    254 			if (_rtld_relocate_plt_objects(obj) < 0)
    255 				ok = 0;
    256 #endif
    257 		if (!ok)
    258 			return -1;
    259 
    260 
    261 		/* Set some sanity-checking numbers in the Obj_Entry. */
    262 		obj->magic = RTLD_MAGIC;
    263 		obj->version = RTLD_VERSION;
    264 
    265 		/* Fill in the dynamic linker entry points. */
    266 		obj->dlopen = _rtld_dlopen;
    267 		obj->dlsym = _rtld_dlsym;
    268 		obj->dlerror = _rtld_dlerror;
    269 		obj->dlclose = _rtld_dlclose;
    270 		obj->dladdr = _rtld_dladdr;
    271 
    272 		dbg(("fixing up PLTGOT"));
    273 		/* Set the special PLTGOT entries. */
    274 		if (obj->pltgot != NULL)
    275 			_rtld_setup_pltgot(obj);
    276 	}
    277 
    278 	return 0;
    279 }
    280