Home | History | Annotate | Line # | Download | only in ld.elf_so
reloc.c revision 1.114
      1  1.114  christos /*	$NetBSD: reloc.c,v 1.114 2018/12/30 01:48:37 christos Exp $	 */
      2    1.1       cgd 
      3    1.1       cgd /*
      4    1.1       cgd  * Copyright 1996 John D. Polstra.
      5    1.1       cgd  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6    1.1       cgd  * All rights reserved.
      7    1.1       cgd  *
      8    1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9    1.1       cgd  * modification, are permitted provided that the following conditions
     10    1.1       cgd  * are met:
     11    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17    1.1       cgd  *    must display the following acknowledgement:
     18    1.1       cgd  *      This product includes software developed by John Polstra.
     19    1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20    1.1       cgd  *    derived from this software without specific prior written permission.
     21    1.1       cgd  *
     22    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23    1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24    1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25    1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26    1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27    1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28    1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29    1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30    1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31    1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32    1.1       cgd  */
     33    1.1       cgd 
     34    1.1       cgd /*
     35    1.1       cgd  * Dynamic linker for ELF.
     36    1.1       cgd  *
     37    1.1       cgd  * John Polstra <jdp (at) polstra.com>.
     38    1.1       cgd  */
     39    1.1       cgd 
     40   1.85     skrll #include <sys/cdefs.h>
     41   1.85     skrll #ifndef lint
     42  1.114  christos __RCSID("$NetBSD: reloc.c,v 1.114 2018/12/30 01:48:37 christos Exp $");
     43   1.85     skrll #endif /* not lint */
     44   1.85     skrll 
     45    1.1       cgd #include <err.h>
     46    1.1       cgd #include <errno.h>
     47    1.1       cgd #include <fcntl.h>
     48    1.1       cgd #include <stdarg.h>
     49    1.1       cgd #include <stdio.h>
     50    1.1       cgd #include <stdlib.h>
     51    1.1       cgd #include <string.h>
     52    1.1       cgd #include <unistd.h>
     53    1.1       cgd #include <sys/types.h>
     54    1.1       cgd #include <sys/mman.h>
     55  1.102     joerg #include <sys/bitops.h>
     56    1.1       cgd #include <dirent.h>
     57    1.1       cgd 
     58    1.1       cgd #include "debug.h"
     59    1.1       cgd #include "rtld.h"
     60    1.1       cgd 
     61   1.14        pk #ifndef RTLD_INHIBIT_COPY_RELOCS
     62   1.80     skrll static int _rtld_do_copy_relocation(const Obj_Entry *, const Elf_Rela *);
     63   1.16  christos 
     64    1.1       cgd static int
     65   1.80     skrll _rtld_do_copy_relocation(const Obj_Entry *dstobj, const Elf_Rela *rela)
     66    1.1       cgd {
     67   1.11  christos 	void           *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
     68   1.11  christos 	const Elf_Sym  *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
     69   1.11  christos 	const char     *name = dstobj->strtab + dstsym->st_name;
     70   1.11  christos 	unsigned long   hash = _rtld_elf_hash(name);
     71   1.11  christos 	size_t          size = dstsym->st_size;
     72   1.11  christos 	const void     *srcaddr;
     73   1.37      matt 	const Elf_Sym  *srcsym = NULL;
     74   1.11  christos 	Obj_Entry      *srcobj;
     75   1.11  christos 
     76  1.110       uwe 	if (__predict_false(size == 0)) {
     77  1.110       uwe #if defined(__powerpc__) && !defined(__LP64) /* PR port-macppc/47464 */
     78  1.110       uwe 		if (strcmp(name, "_SDA_BASE_") == 0
     79  1.110       uwe 		    || strcmp(name, "_SDA2_BASE_") == 0)
     80  1.110       uwe 		{
     81  1.110       uwe 			rdbg(("COPY %s %s --> ignoring old binutils bug",
     82  1.110       uwe 			      dstobj->path, name));
     83  1.110       uwe 			return 0;
     84  1.110       uwe 		}
     85  1.110       uwe #endif
     86  1.110       uwe #if 0 /* shall we warn? */
     87  1.110       uwe 		xwarnx("%s: zero size COPY relocation for \"%s\"",
     88  1.110       uwe 		       dstobj->path, name);
     89  1.110       uwe #endif
     90  1.110       uwe 	}
     91  1.110       uwe 
     92  1.104    nonaka 	for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) {
     93  1.104    nonaka 		srcsym = _rtld_symlook_obj(name, hash, srcobj, 0,
     94  1.104    nonaka 		    _rtld_fetch_ventry(dstobj, ELF_R_SYM(rela->r_info)));
     95  1.104    nonaka 		if (srcsym != NULL)
     96   1.11  christos 			break;
     97  1.104    nonaka 	}
     98   1.11  christos 
     99   1.11  christos 	if (srcobj == NULL) {
    100   1.11  christos 		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
    101   1.11  christos 		    " relocation in %s", name, dstobj->path);
    102   1.14        pk 		return (-1);
    103   1.11  christos 	}
    104   1.11  christos 	srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
    105   1.84    petrov 	rdbg(("COPY %s %s %s --> src=%p dst=%p size %ld",
    106   1.98  christos 	    dstobj->path, srcobj->path, name, srcaddr,
    107   1.84    petrov 	    (void *)dstaddr, (long)size));
    108  1.114  christos 	(void)memcpy(dstaddr, srcaddr, size);
    109   1.14        pk 	return (0);
    110    1.1       cgd }
    111   1.14        pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
    112   1.11  christos 
    113   1.11  christos 
    114    1.1       cgd /*
    115    1.1       cgd  * Process the special R_xxx_COPY relocations in the main program.  These
    116    1.1       cgd  * copy data from a shared object into a region in the main program's BSS
    117    1.1       cgd  * segment.
    118    1.1       cgd  *
    119    1.1       cgd  * Returns 0 on success, -1 on failure.
    120    1.1       cgd  */
    121    1.1       cgd int
    122   1.80     skrll _rtld_do_copy_relocations(const Obj_Entry *dstobj)
    123    1.1       cgd {
    124   1.14        pk #ifndef RTLD_INHIBIT_COPY_RELOCS
    125   1.14        pk 
    126   1.14        pk 	/* COPY relocations are invalid elsewhere */
    127   1.64   mycroft 	assert(!dstobj->isdynamic);
    128    1.1       cgd 
    129   1.11  christos 	if (dstobj->rel != NULL) {
    130   1.11  christos 		const Elf_Rel  *rel;
    131   1.11  christos 		for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
    132   1.11  christos 			if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
    133   1.35    kleink 				Elf_Rela        ourrela;
    134   1.11  christos 				ourrela.r_info = rel->r_info;
    135   1.11  christos 				ourrela.r_offset = rel->r_offset;
    136   1.11  christos 				ourrela.r_addend = 0;
    137   1.11  christos 				if (_rtld_do_copy_relocation(dstobj,
    138   1.66   mycroft 				    &ourrela) < 0)
    139   1.14        pk 					return (-1);
    140   1.11  christos 			}
    141   1.11  christos 		}
    142   1.11  christos 	}
    143   1.11  christos 	if (dstobj->rela != NULL) {
    144   1.35    kleink 		const Elf_Rela *rela;
    145   1.11  christos 		for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
    146   1.11  christos 			if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
    147   1.66   mycroft 				if (_rtld_do_copy_relocation(dstobj, rela) < 0)
    148   1.14        pk 					return (-1);
    149   1.11  christos 			}
    150   1.11  christos 		}
    151    1.1       cgd 	}
    152  1.114  christos #ifdef GNU_RELRO
    153  1.114  christos 	if (_rtld_relro(dstobj, true) == -1)
    154  1.114  christos 		return -1;
    155  1.114  christos #endif
    156   1.14        pk #endif /* RTLD_INHIBIT_COPY_RELOCS */
    157    1.1       cgd 
    158   1.14        pk 	return (0);
    159    1.1       cgd }
    160    1.9        pk 
    161    1.1       cgd /*
    162    1.1       cgd  * Relocate newly-loaded shared objects.  The argument is a pointer to
    163    1.1       cgd  * the Obj_Entry for the first such object.  All objects from the first
    164    1.1       cgd  * to the end of the list of objects are relocated.  Returns 0 on success,
    165    1.1       cgd  * or -1 on failure.
    166    1.1       cgd  */
    167    1.1       cgd int
    168   1.80     skrll _rtld_relocate_objects(Obj_Entry *first, bool bind_now)
    169    1.1       cgd {
    170   1.16  christos 	Obj_Entry *obj;
    171   1.77   mycroft 	int ok = 1;
    172    1.1       cgd 
    173   1.11  christos 	for (obj = first; obj != NULL; obj = obj->next) {
    174   1.16  christos 		if (obj->nbuckets == 0 || obj->nchains == 0 ||
    175   1.16  christos 		    obj->buckets == NULL || obj->symtab == NULL ||
    176   1.16  christos 		    obj->strtab == NULL) {
    177   1.11  christos 			_rtld_error("%s: Shared object has no run-time"
    178   1.11  christos 			    " symbol table", obj->path);
    179   1.11  christos 			return -1;
    180   1.11  christos 		}
    181  1.102     joerg 		if (obj->nbuckets == UINT32_MAX) {
    182  1.102     joerg 			_rtld_error("%s: Symbol table too large", obj->path);
    183  1.102     joerg 			return -1;
    184  1.102     joerg 		}
    185   1.66   mycroft 		rdbg((" relocating %s (%ld/%ld rel/rela, %ld/%ld plt rel/rela)",
    186   1.11  christos 		    obj->path,
    187   1.11  christos 		    (long)(obj->rellim - obj->rel),
    188   1.11  christos 		    (long)(obj->relalim - obj->rela),
    189   1.11  christos 		    (long)(obj->pltrellim - obj->pltrel),
    190   1.16  christos 		    (long)(obj->pltrelalim - obj->pltrela)));
    191   1.11  christos 
    192   1.11  christos 		if (obj->textrel) {
    193  1.108  christos 			xwarnx("%s: text relocations", obj->path);
    194   1.11  christos 			/*
    195   1.11  christos 			 * There are relocations to the write-protected text
    196   1.11  christos 			 * segment.
    197   1.11  christos 			 */
    198   1.11  christos 			if (mprotect(obj->mapbase, obj->textsize,
    199  1.108  christos 				PROT_READ | PROT_WRITE) == -1) {
    200   1.11  christos 				_rtld_error("%s: Cannot write-enable text "
    201   1.11  christos 				    "segment: %s", obj->path, xstrerror(errno));
    202   1.11  christos 				return -1;
    203   1.11  christos 			}
    204   1.11  christos 		}
    205   1.71   mycroft 		dbg(("doing non-PLT relocations"));
    206   1.76  junyoung 		if (_rtld_relocate_nonplt_objects(obj) < 0)
    207   1.77   mycroft 			ok = 0;
    208   1.11  christos 		if (obj->textrel) {	/* Re-protected the text segment. */
    209   1.11  christos 			if (mprotect(obj->mapbase, obj->textsize,
    210   1.11  christos 				     PROT_READ | PROT_EXEC) == -1) {
    211   1.11  christos 				_rtld_error("%s: Cannot write-protect text "
    212   1.11  christos 				    "segment: %s", obj->path, xstrerror(errno));
    213   1.11  christos 				return -1;
    214   1.11  christos 			}
    215   1.11  christos 		}
    216   1.71   mycroft 		dbg(("doing lazy PLT binding"));
    217   1.76  junyoung 		if (_rtld_relocate_plt_lazy(obj) < 0)
    218   1.77   mycroft 			ok = 0;
    219  1.103     skrll 		if (obj->z_now || bind_now) {
    220   1.82     skrll 			dbg(("doing immediate PLT binding"));
    221   1.76  junyoung 			if (_rtld_relocate_plt_objects(obj) < 0)
    222   1.77   mycroft 				ok = 0;
    223   1.82     skrll 		}
    224   1.77   mycroft 		if (!ok)
    225   1.77   mycroft 			return -1;
    226   1.77   mycroft 
    227   1.71   mycroft 		dbg(("fixing up PLTGOT"));
    228   1.11  christos 		/* Set the special PLTGOT entries. */
    229   1.53   mycroft 		if (obj->pltgot != NULL)
    230   1.53   mycroft 			_rtld_setup_pltgot(obj);
    231  1.109  christos #ifdef GNU_RELRO
    232  1.114  christos 		if (_rtld_relro(obj, false) == -1)
    233  1.114  christos 			return -1;
    234  1.109  christos #endif
    235    1.1       cgd 	}
    236   1.11  christos 	return 0;
    237    1.1       cgd }
    238  1.107     joerg 
    239  1.107     joerg Elf_Addr
    240  1.107     joerg _rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def)
    241  1.107     joerg {
    242  1.107     joerg 	Elf_Addr target;
    243  1.107     joerg 
    244  1.107     joerg 	_rtld_shared_exit();
    245  1.111     joerg 	target = _rtld_resolve_ifunc2(obj,
    246  1.107     joerg 	    (Elf_Addr)obj->relocbase + def->st_value);
    247  1.107     joerg 	_rtld_shared_enter();
    248  1.111     joerg 	return target;
    249  1.111     joerg }
    250  1.111     joerg 
    251  1.111     joerg Elf_Addr
    252  1.111     joerg _rtld_resolve_ifunc2(const Obj_Entry *obj, Elf_Addr addr)
    253  1.111     joerg {
    254  1.111     joerg 	Elf_Addr target;
    255  1.111     joerg 
    256  1.111     joerg 	target = _rtld_call_function_addr(obj, addr);
    257  1.107     joerg 
    258  1.107     joerg 	return target;
    259  1.107     joerg }
    260  1.112     joerg 
    261  1.112     joerg #ifdef RTLD_COMMON_CALL_IFUNC_RELA
    262  1.112     joerg #  ifdef __sparc__
    263  1.112     joerg #  include <machine/elf_support.h>
    264  1.112     joerg #  endif
    265  1.112     joerg 
    266  1.112     joerg void
    267  1.112     joerg _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
    268  1.112     joerg {
    269  1.112     joerg 	const Elf_Rela *rela;
    270  1.112     joerg 	Elf_Addr *where;
    271  1.112     joerg #ifdef __sparc__
    272  1.112     joerg 	Elf_Word *where2;
    273  1.112     joerg #endif
    274  1.112     joerg 	Elf_Addr target;
    275  1.112     joerg 
    276  1.112     joerg 	while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
    277  1.112     joerg 		rela = obj->pltrelalim - obj->ifunc_remaining--;
    278  1.112     joerg #ifdef __sparc__
    279  1.112     joerg #define PLT_IRELATIVE R_TYPE(JMP_IREL)
    280  1.112     joerg #else
    281  1.112     joerg #define PLT_IRELATIVE R_TYPE(IRELATIVE)
    282  1.112     joerg #endif
    283  1.112     joerg 		if (ELF_R_TYPE(rela->r_info) != PLT_IRELATIVE)
    284  1.112     joerg 			continue;
    285  1.112     joerg #ifdef __sparc__
    286  1.112     joerg 		where2 = (Elf_Word *)(obj->relocbase + rela->r_offset);
    287  1.112     joerg #else
    288  1.112     joerg 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    289  1.112     joerg #endif
    290  1.112     joerg 		target = (Elf_Addr)(obj->relocbase + rela->r_addend);
    291  1.112     joerg 		_rtld_exclusive_exit(mask);
    292  1.112     joerg 		target = _rtld_resolve_ifunc2(obj, target);
    293  1.112     joerg 		_rtld_exclusive_enter(mask);
    294  1.112     joerg #ifdef __sparc__
    295  1.112     joerg 		sparc_write_branch(where2 + 1, (void *)target);
    296  1.112     joerg #else
    297  1.112     joerg 		if (*where != target)
    298  1.112     joerg 			*where = target;
    299  1.112     joerg #endif
    300  1.112     joerg 	}
    301  1.112     joerg 
    302  1.112     joerg 	while (obj->ifunc_remaining_nonplt > 0 && _rtld_objgen == cur_objgen) {
    303  1.112     joerg 		rela = obj->relalim - obj->ifunc_remaining_nonplt--;
    304  1.112     joerg 		if (ELF_R_TYPE(rela->r_info) != R_TYPE(IRELATIVE))
    305  1.112     joerg 			continue;
    306  1.112     joerg 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    307  1.112     joerg 		target = (Elf_Addr)(obj->relocbase + rela->r_addend);
    308  1.112     joerg 		_rtld_exclusive_exit(mask);
    309  1.112     joerg 		target = _rtld_resolve_ifunc2(obj, target);
    310  1.112     joerg 		_rtld_exclusive_enter(mask);
    311  1.112     joerg 		if (*where != target)
    312  1.112     joerg 			*where = target;
    313  1.112     joerg 	}
    314  1.112     joerg }
    315  1.112     joerg #endif
    316  1.112     joerg 
    317  1.112     joerg #ifdef RTLD_COMMON_CALL_IFUNC_REL
    318  1.112     joerg void
    319  1.112     joerg _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
    320  1.112     joerg {
    321  1.112     joerg 	const Elf_Rel *rel;
    322  1.112     joerg 	Elf_Addr *where, target;
    323  1.112     joerg 
    324  1.112     joerg 	while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
    325  1.112     joerg 		rel = obj->pltrellim - obj->ifunc_remaining;
    326  1.112     joerg 		--obj->ifunc_remaining;
    327  1.112     joerg 		if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE)) {
    328  1.112     joerg 			where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    329  1.112     joerg 			_rtld_exclusive_exit(mask);
    330  1.112     joerg 			target = _rtld_resolve_ifunc2(obj, *where);
    331  1.112     joerg 			_rtld_exclusive_enter(mask);
    332  1.112     joerg 			if (*where != target)
    333  1.112     joerg 				*where = target;
    334  1.112     joerg 		}
    335  1.112     joerg 	}
    336  1.112     joerg 
    337  1.112     joerg 	while (obj->ifunc_remaining_nonplt > 0 && _rtld_objgen == cur_objgen) {
    338  1.112     joerg 		rel = obj->rellim - obj->ifunc_remaining_nonplt--;
    339  1.112     joerg 		if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE)) {
    340  1.112     joerg 			where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    341  1.112     joerg 			_rtld_exclusive_exit(mask);
    342  1.112     joerg 			target = _rtld_resolve_ifunc2(obj, *where);
    343  1.112     joerg 			_rtld_exclusive_enter(mask);
    344  1.112     joerg 			if (*where != target)
    345  1.112     joerg 				*where = target;
    346  1.112     joerg 		}
    347  1.112     joerg 	}
    348  1.112     joerg }
    349  1.112     joerg #endif
    350