Home | History | Annotate | Line # | Download | only in aarch64
mdreloc.c revision 1.7.2.4
      1  1.7.2.4  pgoyette /* $NetBSD: mdreloc.c,v 1.7.2.4 2019/01/26 21:59:58 pgoyette Exp $ */
      2      1.1      matt 
      3      1.1      matt /*-
      4      1.1      matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5      1.1      matt  * All rights reserved.
      6      1.1      matt  *
      7      1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1      matt  * by Matt Thomas of 3am Software Foundry.
      9      1.1      matt  *
     10      1.1      matt  * Redistribution and use in source and binary forms, with or without
     11      1.1      matt  * modification, are permitted provided that the following conditions
     12      1.1      matt  * are met:
     13      1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14      1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15      1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17      1.1      matt  *    documentation and/or other materials provided with the distribution.
     18      1.1      matt  *
     19      1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1      matt  */
     31      1.1      matt 
     32  1.7.2.2  pgoyette /*-
     33  1.7.2.2  pgoyette  * Copyright (c) 2014-2015 The FreeBSD Foundation
     34  1.7.2.2  pgoyette  * All rights reserved.
     35  1.7.2.2  pgoyette  *
     36  1.7.2.2  pgoyette  * Portions of this software were developed by Andrew Turner
     37  1.7.2.2  pgoyette  * under sponsorship from the FreeBSD Foundation.
     38  1.7.2.2  pgoyette  *
     39  1.7.2.2  pgoyette  * Redistribution and use in source and binary forms, with or without
     40  1.7.2.2  pgoyette  * modification, are permitted provided that the following conditions
     41  1.7.2.2  pgoyette  * are met:
     42  1.7.2.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     43  1.7.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     44  1.7.2.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     45  1.7.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     46  1.7.2.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     47  1.7.2.2  pgoyette  *
     48  1.7.2.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     49  1.7.2.2  pgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  1.7.2.2  pgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  1.7.2.2  pgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     52  1.7.2.2  pgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  1.7.2.2  pgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  1.7.2.2  pgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  1.7.2.2  pgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  1.7.2.2  pgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  1.7.2.2  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  1.7.2.2  pgoyette  * SUCH DAMAGE.
     59  1.7.2.2  pgoyette  */
     60  1.7.2.2  pgoyette 
     61      1.1      matt #include <sys/cdefs.h>
     62      1.1      matt #ifndef lint
     63  1.7.2.4  pgoyette __RCSID("$NetBSD: mdreloc.c,v 1.7.2.4 2019/01/26 21:59:58 pgoyette Exp $");
     64      1.1      matt #endif /* not lint */
     65      1.1      matt 
     66      1.1      matt #include <sys/types.h>
     67      1.1      matt #include <string.h>
     68      1.1      matt 
     69      1.1      matt #include "debug.h"
     70      1.1      matt #include "rtld.h"
     71      1.1      matt 
     72  1.7.2.2  pgoyette struct tls_data {
     73  1.7.2.4  pgoyette 	size_t		td_tlsindex;
     74  1.7.2.4  pgoyette 	Elf_Addr	td_tlsoffs;
     75  1.7.2.2  pgoyette };
     76  1.7.2.2  pgoyette 
     77      1.1      matt void _rtld_bind_start(void);
     78      1.1      matt void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     79      1.5  nisimura Elf_Addr _rtld_bind(const Obj_Entry *, Elf_Word);
     80  1.7.2.4  pgoyette void *_rtld_tlsdesc_static(void *);
     81  1.7.2.4  pgoyette void *_rtld_tlsdesc_undef(void *);
     82  1.7.2.2  pgoyette void *_rtld_tlsdesc_dynamic(void *);
     83      1.1      matt 
     84      1.6  nisimura /*
     85      1.6  nisimura  * AARCH64 PLT looks like this;
     86      1.6  nisimura  *
     87      1.6  nisimura  *	PLT HEADER <8 instructions>
     88      1.6  nisimura  *	PLT ENTRY #0 <4 instructions>
     89      1.6  nisimura  *	PLT ENTRY #1 <4 instructions>
     90      1.6  nisimura  *	.
     91      1.6  nisimura  *	.
     92      1.6  nisimura  *	PLT ENTRY #n <4 instructions>
     93      1.6  nisimura  *
     94      1.6  nisimura  * PLT HEADER
     95      1.6  nisimura  *	stp  x16, x30, [sp, #-16]!
     96      1.6  nisimura  *	adrp x16, (GOT+16)
     97      1.6  nisimura  *	ldr  x17, [x16, #PLT_GOT+0x10]
     98      1.6  nisimura  *	add  x16, x16, #PLT_GOT+0x10
     99      1.6  nisimura  *	br   x17
    100      1.6  nisimura  *	nop
    101      1.6  nisimura  *	nop
    102      1.6  nisimura  *	nop
    103      1.6  nisimura  *
    104      1.6  nisimura  * PLT ENTRY #n
    105      1.6  nisimura  *	adrp x16, PLTGOT + n * 8
    106      1.6  nisimura  *	ldr  x17, [x16, PLTGOT + n * 8]
    107      1.6  nisimura  *	add  x16, x16, :lo12:PLTGOT + n * 8
    108      1.6  nisimura  *	br   x17
    109      1.6  nisimura  */
    110      1.1      matt void
    111      1.1      matt _rtld_setup_pltgot(const Obj_Entry *obj)
    112      1.1      matt {
    113      1.6  nisimura 
    114      1.1      matt 	obj->pltgot[1] = (Elf_Addr) obj;
    115      1.1      matt 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
    116      1.1      matt }
    117      1.1      matt 
    118  1.7.2.2  pgoyette static struct tls_data *
    119  1.7.2.4  pgoyette _rtld_tlsdesc_alloc(size_t tlsindex, Elf_Addr offs)
    120  1.7.2.2  pgoyette {
    121  1.7.2.2  pgoyette 	struct tls_data *tlsdesc;
    122  1.7.2.2  pgoyette 
    123  1.7.2.2  pgoyette 	tlsdesc = xmalloc(sizeof(*tlsdesc));
    124  1.7.2.4  pgoyette 	tlsdesc->td_tlsindex = tlsindex;
    125  1.7.2.4  pgoyette 	tlsdesc->td_tlsoffs = offs;
    126  1.7.2.2  pgoyette 
    127  1.7.2.2  pgoyette 	return tlsdesc;
    128  1.7.2.2  pgoyette }
    129  1.7.2.2  pgoyette 
    130  1.7.2.4  pgoyette static void
    131  1.7.2.4  pgoyette _rtld_tlsdesc_fill(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *where, u_int flags)
    132  1.7.2.2  pgoyette {
    133  1.7.2.2  pgoyette 	const Elf_Sym *def;
    134  1.7.2.2  pgoyette 	const Obj_Entry *defobj;
    135  1.7.2.4  pgoyette 	Elf_Addr offs = 0;
    136  1.7.2.4  pgoyette 	unsigned long symnum = ELF_R_SYM(rela->r_info);
    137  1.7.2.2  pgoyette 
    138  1.7.2.4  pgoyette 	if (symnum != 0) {
    139  1.7.2.4  pgoyette 		def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
    140  1.7.2.4  pgoyette 		    flags);
    141  1.7.2.4  pgoyette 		if (def == NULL)
    142  1.7.2.4  pgoyette 			_rtld_die();
    143  1.7.2.4  pgoyette 		if (def == &_rtld_sym_zero) {
    144  1.7.2.4  pgoyette 			/* Weak undefined thread variable */
    145  1.7.2.4  pgoyette 			where[0] = (Elf_Addr)_rtld_tlsdesc_undef;
    146  1.7.2.4  pgoyette 			where[1] = rela->r_addend;
    147  1.7.2.2  pgoyette 
    148  1.7.2.4  pgoyette 			rdbg(("TLSDESC %s (weak) in %s --> %p",
    149  1.7.2.4  pgoyette 			    obj->strtab + obj->symtab[symnum].st_name,
    150  1.7.2.4  pgoyette 			    obj->path, (void *)where[1]));
    151  1.7.2.2  pgoyette 
    152  1.7.2.4  pgoyette 			return;
    153  1.7.2.4  pgoyette 		}
    154  1.7.2.4  pgoyette 		offs = def->st_value;
    155  1.7.2.4  pgoyette 	} else {
    156  1.7.2.4  pgoyette 		defobj = obj;
    157  1.7.2.4  pgoyette 	}
    158  1.7.2.4  pgoyette 	offs += rela->r_addend;
    159  1.7.2.2  pgoyette 
    160  1.7.2.4  pgoyette 	if (defobj->tls_done) {
    161  1.7.2.4  pgoyette 		/* Variable is in initialy allocated TLS segment */
    162  1.7.2.4  pgoyette 		where[0] = (Elf_Addr)_rtld_tlsdesc_static;
    163  1.7.2.4  pgoyette 		where[1] = defobj->tlsoffset + offs +
    164  1.7.2.2  pgoyette 		    sizeof(struct tls_tcb);
    165  1.7.2.4  pgoyette 
    166  1.7.2.4  pgoyette 		rdbg(("TLSDESC %s --> %p static",
    167  1.7.2.4  pgoyette 		    obj->path, (void *)where[1]));
    168  1.7.2.2  pgoyette 	} else {
    169  1.7.2.4  pgoyette 		/* TLS offset is unknown at load time, use dynamic resolving */
    170  1.7.2.2  pgoyette 		where[0] = (Elf_Addr)_rtld_tlsdesc_dynamic;
    171  1.7.2.4  pgoyette 		where[1] = (Elf_Addr)_rtld_tlsdesc_alloc(defobj->tlsindex, offs);
    172  1.7.2.4  pgoyette 
    173  1.7.2.4  pgoyette 		rdbg(("TLSDESC %s in %s --> %p dynamic (%zu, %p)",
    174  1.7.2.4  pgoyette 		    obj->strtab + obj->symtab[symnum].st_name,
    175  1.7.2.4  pgoyette 		    obj->path, (void *)where[1], defobj->tlsindex, (void *)offs));
    176  1.7.2.2  pgoyette 	}
    177  1.7.2.2  pgoyette }
    178  1.7.2.2  pgoyette 
    179      1.1      matt void
    180      1.1      matt _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    181      1.1      matt {
    182      1.7     skrll 	const Elf_Rela *rela = 0, *relalim;
    183      1.7     skrll 	Elf_Addr relasz = 0;
    184      1.1      matt 	Elf_Addr *where;
    185      1.1      matt 
    186      1.1      matt 	for (; dynp->d_tag != DT_NULL; dynp++) {
    187      1.1      matt 		switch (dynp->d_tag) {
    188      1.7     skrll 		case DT_RELA:
    189      1.7     skrll 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    190      1.1      matt 			break;
    191      1.7     skrll 		case DT_RELASZ:
    192      1.7     skrll 			relasz = dynp->d_un.d_val;
    193      1.1      matt 			break;
    194      1.1      matt 		}
    195      1.1      matt 	}
    196      1.7     skrll 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    197      1.7     skrll 	for (; rela < relalim; rela++) {
    198      1.7     skrll 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    199      1.1      matt 		*where += (Elf_Addr)relocbase;
    200      1.1      matt 	}
    201      1.1      matt }
    202      1.1      matt 
    203      1.1      matt int
    204      1.1      matt _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    205      1.1      matt {
    206      1.3     joerg 	const Elf_Sym *def = NULL;
    207      1.3     joerg 	const Obj_Entry *defobj = NULL;
    208      1.3     joerg 	unsigned long last_symnum = ULONG_MAX;
    209      1.3     joerg 
    210      1.1      matt 	for (const Elf_Rela *rela = obj->rela; rela < obj->relalim; rela++) {
    211      1.1      matt 		Elf_Addr        *where;
    212      1.5  nisimura 		Elf_Addr	tmp;
    213  1.7.2.3  pgoyette 		unsigned long	symnum = ULONG_MAX;
    214      1.1      matt 
    215      1.1      matt 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    216      1.1      matt 
    217      1.1      matt 		switch (ELF_R_TYPE(rela->r_info)) {
    218      1.7     skrll 		case R_TYPE(ABS64):	/* word S + A */
    219      1.7     skrll 		case R_TYPE(GLOB_DAT):	/* word S + A */
    220      1.3     joerg 		case R_TLS_TYPE(TLS_DTPREL):
    221      1.3     joerg 		case R_TLS_TYPE(TLS_DTPMOD):
    222      1.3     joerg 		case R_TLS_TYPE(TLS_TPREL):
    223      1.3     joerg 			symnum = ELF_R_SYM(rela->r_info);
    224      1.3     joerg 			if (last_symnum != symnum) {
    225      1.3     joerg 				last_symnum = symnum;
    226      1.3     joerg 				def = _rtld_find_symdef(symnum, obj, &defobj,
    227      1.3     joerg 				    false);
    228      1.3     joerg 				if (def == NULL)
    229      1.3     joerg 					return -1;
    230      1.3     joerg 			}
    231      1.3     joerg 
    232      1.3     joerg 		default:
    233      1.3     joerg 			break;
    234      1.3     joerg 		}
    235      1.3     joerg 
    236      1.3     joerg 		switch (ELF_R_TYPE(rela->r_info)) {
    237      1.1      matt 		case R_TYPE(NONE):
    238      1.1      matt 			break;
    239      1.1      matt 
    240      1.7     skrll 		case R_TYPE(ABS64):	/* word S + A */
    241      1.7     skrll 		case R_TYPE(GLOB_DAT):	/* word S + A */
    242      1.5  nisimura 			tmp = (Elf_Addr)defobj->relocbase + def->st_value +
    243      1.5  nisimura 			    rela->r_addend;
    244      1.5  nisimura 			if (*where != tmp)
    245      1.5  nisimura 				*where = tmp;
    246      1.1      matt 			rdbg(("ABS64/GLOB_DAT %s in %s --> %p @ %p in %s",
    247      1.1      matt 			    obj->strtab + obj->symtab[symnum].st_name,
    248      1.1      matt 			    obj->path, (void *)tmp, where, defobj->path));
    249      1.1      matt 			break;
    250      1.1      matt 
    251      1.1      matt 		case R_TYPE(RELATIVE):	/* word B + A */
    252      1.5  nisimura 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
    253      1.1      matt 			rdbg(("RELATIVE in %s --> %p", obj->path,
    254      1.5  nisimura 			    (void *)*where));
    255      1.1      matt 			break;
    256      1.1      matt 
    257      1.1      matt 		case R_TYPE(COPY):
    258      1.1      matt 			/*
    259      1.1      matt 			 * These are deferred until all other relocations have
    260      1.1      matt 			 * been done.  All we do here is make sure that the
    261      1.1      matt 			 * COPY relocation is not in a shared library.  They
    262      1.1      matt 			 * are allowed only in executable files.
    263      1.1      matt 			 */
    264      1.1      matt 			if (obj->isdynamic) {
    265      1.1      matt 				_rtld_error(
    266      1.1      matt 			"%s: Unexpected R_COPY relocation in shared library",
    267      1.1      matt 				    obj->path);
    268      1.1      matt 				return -1;
    269      1.1      matt 			}
    270      1.1      matt 			rdbg(("COPY (avoid in main)"));
    271      1.1      matt 			break;
    272      1.1      matt 
    273  1.7.2.2  pgoyette 		case R_TYPE(TLSDESC):
    274  1.7.2.4  pgoyette 			_rtld_tlsdesc_fill(obj, rela, where, 0);
    275  1.7.2.2  pgoyette 			break;
    276  1.7.2.2  pgoyette 
    277      1.1      matt 		case R_TLS_TYPE(TLS_DTPREL):
    278      1.5  nisimura 			*where = (Elf_Addr)(def->st_value + rela->r_addend);
    279      1.1      matt 
    280      1.5  nisimura 			rdbg(("TLS_DTPREL %s in %s --> %p",
    281      1.1      matt 			    obj->strtab + obj->symtab[symnum].st_name,
    282      1.5  nisimura 			    obj->path, (void *)*where));
    283      1.1      matt 
    284      1.1      matt 			break;
    285      1.1      matt 		case R_TLS_TYPE(TLS_DTPMOD):
    286      1.1      matt 			*where = (Elf_Addr)(defobj->tlsindex);
    287      1.1      matt 
    288      1.1      matt 			rdbg(("TLS_DTPMOD %s in %s --> %p",
    289      1.1      matt 			    obj->strtab + obj->symtab[symnum].st_name,
    290      1.5  nisimura 			    obj->path, (void *)*where));
    291      1.1      matt 
    292      1.1      matt 			break;
    293      1.1      matt 
    294      1.1      matt 		case R_TLS_TYPE(TLS_TPREL):
    295      1.1      matt 			if (!defobj->tls_done &&
    296      1.1      matt 			    _rtld_tls_offset_allocate(obj))
    297      1.1      matt 				return -1;
    298      1.1      matt 
    299      1.1      matt 			*where = (Elf_Addr)def->st_value + defobj->tlsoffset +
    300      1.1      matt 			    sizeof(struct tls_tcb);
    301      1.5  nisimura 			rdbg(("TLS_TPREL %s in %s --> %p in %s",
    302      1.1      matt 			    obj->strtab + obj->symtab[symnum].st_name,
    303      1.5  nisimura 			    obj->path, (void *)*where, defobj->path));
    304      1.1      matt 			break;
    305      1.1      matt 
    306      1.1      matt 		default:
    307      1.1      matt 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    308  1.7.2.3  pgoyette 			    "addend = %p, contents = %p",
    309      1.3     joerg 			    (u_long)ELF_R_SYM(rela->r_info),
    310      1.3     joerg 			    (u_long)ELF_R_TYPE(rela->r_info),
    311      1.5  nisimura 			    (void *)rela->r_offset, (void *)rela->r_addend,
    312  1.7.2.3  pgoyette 			    (void *)*where));
    313      1.1      matt 			_rtld_error("%s: Unsupported relocation type %ld "
    314      1.1      matt 			    "in non-PLT relocations",
    315      1.1      matt 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    316      1.1      matt 			return -1;
    317      1.1      matt 		}
    318      1.1      matt 	}
    319      1.1      matt 	return 0;
    320      1.1      matt }
    321      1.1      matt 
    322      1.1      matt int
    323      1.4     joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
    324      1.1      matt {
    325      1.1      matt 
    326      1.1      matt 	if (!obj->relocbase)
    327      1.1      matt 		return 0;
    328      1.1      matt 
    329      1.7     skrll 	for (const Elf_Rela *rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    330      1.7     skrll 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    331      1.1      matt 
    332      1.7     skrll 		assert((ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT)) ||
    333      1.7     skrll 		    (ELF_R_TYPE(rela->r_info) == R_TYPE(TLSDESC)));
    334      1.1      matt 
    335      1.7     skrll 		switch (ELF_R_TYPE(rela->r_info)) {
    336      1.7     skrll 		case R_TYPE(JUMP_SLOT):
    337      1.7     skrll 			/* Just relocate the GOT slots pointing into the PLT */
    338      1.7     skrll 			*where += (Elf_Addr)obj->relocbase;
    339      1.7     skrll 			rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    340      1.7     skrll 			break;
    341      1.7     skrll 		case R_TYPE(TLSDESC):
    342  1.7.2.4  pgoyette 			_rtld_tlsdesc_fill(obj, rela, where, SYMLOOK_IN_PLT);
    343      1.7     skrll 			break;
    344      1.7     skrll 		}
    345      1.1      matt 	}
    346      1.1      matt 
    347      1.1      matt 	return 0;
    348      1.1      matt }
    349      1.1      matt 
    350      1.7     skrll void
    351      1.7     skrll _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
    352      1.7     skrll {
    353      1.7     skrll 	const Elf_Rela *rela;
    354      1.7     skrll 	Elf_Addr *where, target;
    355      1.7     skrll 
    356      1.7     skrll 	while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
    357      1.7     skrll 		rela = obj->pltrelalim - obj->ifunc_remaining;
    358      1.7     skrll 		--obj->ifunc_remaining;
    359      1.7     skrll 		if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE)) {
    360      1.7     skrll 			where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    361      1.7     skrll 			target = (Elf_Addr)(obj->relocbase + rela->r_addend);
    362      1.7     skrll 			_rtld_exclusive_exit(mask);
    363      1.7     skrll 			target = _rtld_resolve_ifunc2(obj, target);
    364      1.7     skrll 			_rtld_exclusive_enter(mask);
    365      1.7     skrll 			if (*where != target)
    366      1.7     skrll 				*where = target;
    367      1.7     skrll 		}
    368      1.7     skrll 	}
    369      1.7     skrll }
    370      1.7     skrll 
    371      1.1      matt static int
    372      1.7     skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
    373      1.1      matt 	Elf_Addr *tp)
    374      1.1      matt {
    375      1.7     skrll 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    376      1.1      matt 	Elf_Addr new_value;
    377      1.1      matt 	const Elf_Sym  *def;
    378      1.1      matt 	const Obj_Entry *defobj;
    379      1.1      matt 
    380  1.7.2.2  pgoyette 	switch (ELF_R_TYPE(rela->r_info)) {
    381  1.7.2.2  pgoyette 	case R_TYPE(JUMP_SLOT):
    382  1.7.2.2  pgoyette 		def = _rtld_find_plt_symdef(ELF_R_SYM(rela->r_info), obj,
    383  1.7.2.2  pgoyette 		    &defobj, tp != NULL);
    384  1.7.2.2  pgoyette 		if (__predict_false(def == NULL))
    385  1.7.2.2  pgoyette 			return -1;
    386  1.7.2.2  pgoyette 		if (__predict_false(def == &_rtld_sym_zero))
    387      1.2     joerg 			return 0;
    388  1.7.2.2  pgoyette 
    389  1.7.2.2  pgoyette 		if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    390  1.7.2.2  pgoyette 			if (tp == NULL)
    391  1.7.2.2  pgoyette 				return 0;
    392  1.7.2.2  pgoyette 			new_value = _rtld_resolve_ifunc(defobj, def);
    393  1.7.2.2  pgoyette 		} else {
    394  1.7.2.2  pgoyette 			new_value = (Elf_Addr)(defobj->relocbase +
    395  1.7.2.2  pgoyette 			     def->st_value);
    396  1.7.2.2  pgoyette 		}
    397  1.7.2.2  pgoyette 		rdbg(("bind now/fixup in %s --> old=%p new=%p",
    398  1.7.2.2  pgoyette 		    defobj->strtab + def->st_name, (void *)*where,
    399  1.7.2.2  pgoyette 		    (void *)new_value));
    400  1.7.2.2  pgoyette 		if (*where != new_value)
    401  1.7.2.2  pgoyette 			*where = new_value;
    402  1.7.2.2  pgoyette 		if (tp)
    403  1.7.2.2  pgoyette 			*tp = new_value;
    404  1.7.2.2  pgoyette 		break;
    405  1.7.2.2  pgoyette 	case R_TYPE(TLSDESC):
    406  1.7.2.4  pgoyette 		_rtld_tlsdesc_fill(obj, rela, where, SYMLOOK_IN_PLT);
    407  1.7.2.2  pgoyette 		break;
    408      1.2     joerg 	}
    409      1.1      matt 
    410      1.1      matt 	return 0;
    411      1.1      matt }
    412      1.1      matt 
    413      1.5  nisimura Elf_Addr
    414      1.7     skrll _rtld_bind(const Obj_Entry *obj, Elf_Word relaidx)
    415      1.1      matt {
    416      1.7     skrll 	const Elf_Rela *rela = obj->pltrela + relaidx;
    417  1.7.2.1  pgoyette 	Elf_Addr new_value = 0;
    418      1.1      matt 
    419      1.1      matt 	_rtld_shared_enter();
    420      1.7     skrll 	int err = _rtld_relocate_plt_object(obj, rela, &new_value);
    421      1.1      matt 	if (err)
    422      1.1      matt 		_rtld_die();
    423      1.1      matt 	_rtld_shared_exit();
    424      1.1      matt 
    425      1.5  nisimura 	return new_value;
    426      1.1      matt }
    427      1.1      matt int
    428      1.1      matt _rtld_relocate_plt_objects(const Obj_Entry *obj)
    429      1.1      matt {
    430      1.7     skrll 	const Elf_Rela *rela;
    431      1.1      matt 	int err = 0;
    432  1.7.2.3  pgoyette 
    433      1.7     skrll 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    434      1.7     skrll 		err = _rtld_relocate_plt_object(obj, rela, NULL);
    435      1.1      matt 		if (err)
    436      1.1      matt 			break;
    437      1.1      matt 	}
    438      1.1      matt 
    439      1.1      matt 	return err;
    440      1.1      matt }
    441