Home | History | Annotate | Line # | Download | only in aarch64
mdreloc.c revision 1.7.2.3
      1  1.7.2.3  pgoyette /* $NetBSD: mdreloc.c,v 1.7.2.3 2018/11/26 01:52:13 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.3  pgoyette __RCSID("$NetBSD: mdreloc.c,v 1.7.2.3 2018/11/26 01:52:13 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.2  pgoyette 	int64_t index;
     74  1.7.2.2  pgoyette 	Obj_Entry *obj;
     75  1.7.2.2  pgoyette 	const Elf_Rela *rela;
     76  1.7.2.2  pgoyette };
     77  1.7.2.2  pgoyette 
     78      1.1      matt void _rtld_bind_start(void);
     79      1.1      matt void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     80      1.5  nisimura Elf_Addr _rtld_bind(const Obj_Entry *, Elf_Word);
     81      1.7     skrll void *_rtld_tlsdesc(void *);
     82  1.7.2.2  pgoyette void *_rtld_tlsdesc_dynamic(void *);
     83  1.7.2.2  pgoyette int64_t _rtld_tlsdesc_handle(struct tls_data *, u_int);
     84      1.1      matt 
     85      1.6  nisimura /*
     86      1.6  nisimura  * AARCH64 PLT looks like this;
     87      1.6  nisimura  *
     88      1.6  nisimura  *	PLT HEADER <8 instructions>
     89      1.6  nisimura  *	PLT ENTRY #0 <4 instructions>
     90      1.6  nisimura  *	PLT ENTRY #1 <4 instructions>
     91      1.6  nisimura  *	.
     92      1.6  nisimura  *	.
     93      1.6  nisimura  *	PLT ENTRY #n <4 instructions>
     94      1.6  nisimura  *
     95      1.6  nisimura  * PLT HEADER
     96      1.6  nisimura  *	stp  x16, x30, [sp, #-16]!
     97      1.6  nisimura  *	adrp x16, (GOT+16)
     98      1.6  nisimura  *	ldr  x17, [x16, #PLT_GOT+0x10]
     99      1.6  nisimura  *	add  x16, x16, #PLT_GOT+0x10
    100      1.6  nisimura  *	br   x17
    101      1.6  nisimura  *	nop
    102      1.6  nisimura  *	nop
    103      1.6  nisimura  *	nop
    104      1.6  nisimura  *
    105      1.6  nisimura  * PLT ENTRY #n
    106      1.6  nisimura  *	adrp x16, PLTGOT + n * 8
    107      1.6  nisimura  *	ldr  x17, [x16, PLTGOT + n * 8]
    108      1.6  nisimura  *	add  x16, x16, :lo12:PLTGOT + n * 8
    109      1.6  nisimura  *	br   x17
    110      1.6  nisimura  */
    111      1.1      matt void
    112      1.1      matt _rtld_setup_pltgot(const Obj_Entry *obj)
    113      1.1      matt {
    114      1.6  nisimura 
    115      1.1      matt 	obj->pltgot[1] = (Elf_Addr) obj;
    116      1.1      matt 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
    117      1.1      matt }
    118      1.1      matt 
    119  1.7.2.2  pgoyette static struct tls_data *
    120  1.7.2.2  pgoyette _rtld_tlsdesc_alloc(Obj_Entry *obj, const Elf_Rela *rela)
    121  1.7.2.2  pgoyette {
    122  1.7.2.2  pgoyette 	struct tls_data *tlsdesc;
    123  1.7.2.2  pgoyette 
    124  1.7.2.2  pgoyette 	tlsdesc = xmalloc(sizeof(*tlsdesc));
    125  1.7.2.2  pgoyette 	tlsdesc->index = -1;
    126  1.7.2.2  pgoyette 	tlsdesc->obj = obj;
    127  1.7.2.2  pgoyette 	tlsdesc->rela = rela;
    128  1.7.2.2  pgoyette 
    129  1.7.2.2  pgoyette 	return tlsdesc;
    130  1.7.2.2  pgoyette }
    131  1.7.2.2  pgoyette 
    132  1.7.2.2  pgoyette static int64_t
    133  1.7.2.2  pgoyette _rtld_tlsdesc_handle_locked(struct tls_data *tlsdesc, u_int flags)
    134  1.7.2.2  pgoyette {
    135  1.7.2.2  pgoyette 	const Elf_Rela *rela;
    136  1.7.2.2  pgoyette 	const Elf_Sym *def;
    137  1.7.2.2  pgoyette 	const Obj_Entry *defobj;
    138  1.7.2.2  pgoyette 	Obj_Entry *obj;
    139  1.7.2.2  pgoyette 
    140  1.7.2.2  pgoyette 	rela = tlsdesc->rela;
    141  1.7.2.2  pgoyette 	obj = tlsdesc->obj;
    142  1.7.2.2  pgoyette 
    143  1.7.2.2  pgoyette 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, flags);
    144  1.7.2.2  pgoyette 	if (def == NULL)
    145  1.7.2.2  pgoyette 		_rtld_die();
    146  1.7.2.2  pgoyette 
    147  1.7.2.2  pgoyette 	tlsdesc->index = defobj->tlsoffset + def->st_value + rela->r_addend +
    148  1.7.2.2  pgoyette 	    sizeof(struct tls_tcb);
    149  1.7.2.2  pgoyette 
    150  1.7.2.2  pgoyette 	return tlsdesc->index;
    151  1.7.2.2  pgoyette }
    152  1.7.2.2  pgoyette 
    153  1.7.2.2  pgoyette int64_t
    154  1.7.2.2  pgoyette _rtld_tlsdesc_handle(struct tls_data *tlsdesc, u_int flags)
    155  1.7.2.2  pgoyette {
    156  1.7.2.2  pgoyette 	sigset_t mask;
    157  1.7.2.2  pgoyette 
    158  1.7.2.2  pgoyette 	/* We have already found the index, return it */
    159  1.7.2.2  pgoyette 	if (tlsdesc->index >= 0)
    160  1.7.2.2  pgoyette 		return tlsdesc->index;
    161  1.7.2.2  pgoyette 
    162  1.7.2.2  pgoyette 	_rtld_exclusive_enter(&mask);
    163  1.7.2.2  pgoyette 	/* tlsdesc->index may have been set by another thread */
    164  1.7.2.2  pgoyette 	if (tlsdesc->index == -1)
    165  1.7.2.2  pgoyette 		_rtld_tlsdesc_handle_locked(tlsdesc, flags);
    166  1.7.2.2  pgoyette 	_rtld_exclusive_exit(&mask);
    167  1.7.2.2  pgoyette 
    168  1.7.2.2  pgoyette 	return tlsdesc->index;
    169  1.7.2.2  pgoyette }
    170  1.7.2.2  pgoyette 
    171  1.7.2.2  pgoyette static void
    172  1.7.2.2  pgoyette _rtld_tlsdesc_fill(Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *where)
    173  1.7.2.2  pgoyette {
    174  1.7.2.2  pgoyette 	if (ELF_R_SYM(rela->r_info) == 0) {
    175  1.7.2.2  pgoyette 		where[0] = (Elf_Addr)_rtld_tlsdesc;
    176  1.7.2.2  pgoyette 		where[1] = obj->tlsoffset + rela->r_addend +
    177  1.7.2.2  pgoyette 		    sizeof(struct tls_tcb);
    178  1.7.2.2  pgoyette 	} else {
    179  1.7.2.2  pgoyette 		where[0] = (Elf_Addr)_rtld_tlsdesc_dynamic;
    180  1.7.2.2  pgoyette 		where[1] = (Elf_Addr)_rtld_tlsdesc_alloc(obj, rela);
    181  1.7.2.2  pgoyette 	}
    182  1.7.2.2  pgoyette }
    183  1.7.2.2  pgoyette 
    184      1.1      matt void
    185      1.1      matt _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    186      1.1      matt {
    187      1.7     skrll 	const Elf_Rela *rela = 0, *relalim;
    188      1.7     skrll 	Elf_Addr relasz = 0;
    189      1.1      matt 	Elf_Addr *where;
    190      1.1      matt 
    191      1.1      matt 	for (; dynp->d_tag != DT_NULL; dynp++) {
    192      1.1      matt 		switch (dynp->d_tag) {
    193      1.7     skrll 		case DT_RELA:
    194      1.7     skrll 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    195      1.1      matt 			break;
    196      1.7     skrll 		case DT_RELASZ:
    197      1.7     skrll 			relasz = dynp->d_un.d_val;
    198      1.1      matt 			break;
    199      1.1      matt 		}
    200      1.1      matt 	}
    201      1.7     skrll 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    202      1.7     skrll 	for (; rela < relalim; rela++) {
    203      1.7     skrll 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    204      1.1      matt 		*where += (Elf_Addr)relocbase;
    205      1.1      matt 	}
    206      1.1      matt }
    207      1.1      matt 
    208      1.1      matt int
    209      1.1      matt _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    210      1.1      matt {
    211      1.3     joerg 	const Elf_Sym *def = NULL;
    212      1.3     joerg 	const Obj_Entry *defobj = NULL;
    213      1.3     joerg 	unsigned long last_symnum = ULONG_MAX;
    214      1.3     joerg 
    215      1.1      matt 	for (const Elf_Rela *rela = obj->rela; rela < obj->relalim; rela++) {
    216      1.1      matt 		Elf_Addr        *where;
    217      1.5  nisimura 		Elf_Addr	tmp;
    218  1.7.2.3  pgoyette 		unsigned long	symnum = ULONG_MAX;
    219      1.1      matt 
    220      1.1      matt 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    221      1.1      matt 
    222      1.1      matt 		switch (ELF_R_TYPE(rela->r_info)) {
    223      1.7     skrll 		case R_TYPE(ABS64):	/* word S + A */
    224      1.7     skrll 		case R_TYPE(GLOB_DAT):	/* word S + A */
    225      1.3     joerg 		case R_TLS_TYPE(TLS_DTPREL):
    226      1.3     joerg 		case R_TLS_TYPE(TLS_DTPMOD):
    227      1.3     joerg 		case R_TLS_TYPE(TLS_TPREL):
    228      1.3     joerg 			symnum = ELF_R_SYM(rela->r_info);
    229      1.3     joerg 			if (last_symnum != symnum) {
    230      1.3     joerg 				last_symnum = symnum;
    231      1.3     joerg 				def = _rtld_find_symdef(symnum, obj, &defobj,
    232      1.3     joerg 				    false);
    233      1.3     joerg 				if (def == NULL)
    234      1.3     joerg 					return -1;
    235      1.3     joerg 			}
    236      1.3     joerg 
    237      1.3     joerg 		default:
    238      1.3     joerg 			break;
    239      1.3     joerg 		}
    240      1.3     joerg 
    241      1.3     joerg 		switch (ELF_R_TYPE(rela->r_info)) {
    242      1.1      matt 		case R_TYPE(NONE):
    243      1.1      matt 			break;
    244      1.1      matt 
    245      1.7     skrll 		case R_TYPE(ABS64):	/* word S + A */
    246      1.7     skrll 		case R_TYPE(GLOB_DAT):	/* word S + A */
    247      1.5  nisimura 			tmp = (Elf_Addr)defobj->relocbase + def->st_value +
    248      1.5  nisimura 			    rela->r_addend;
    249      1.5  nisimura 			if (*where != tmp)
    250      1.5  nisimura 				*where = tmp;
    251      1.1      matt 			rdbg(("ABS64/GLOB_DAT %s in %s --> %p @ %p in %s",
    252      1.1      matt 			    obj->strtab + obj->symtab[symnum].st_name,
    253      1.1      matt 			    obj->path, (void *)tmp, where, defobj->path));
    254      1.1      matt 			break;
    255      1.1      matt 
    256      1.1      matt 		case R_TYPE(RELATIVE):	/* word B + A */
    257      1.5  nisimura 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
    258      1.1      matt 			rdbg(("RELATIVE in %s --> %p", obj->path,
    259      1.5  nisimura 			    (void *)*where));
    260      1.1      matt 			break;
    261      1.1      matt 
    262      1.1      matt 		case R_TYPE(COPY):
    263      1.1      matt 			/*
    264      1.1      matt 			 * These are deferred until all other relocations have
    265      1.1      matt 			 * been done.  All we do here is make sure that the
    266      1.1      matt 			 * COPY relocation is not in a shared library.  They
    267      1.1      matt 			 * are allowed only in executable files.
    268      1.1      matt 			 */
    269      1.1      matt 			if (obj->isdynamic) {
    270      1.1      matt 				_rtld_error(
    271      1.1      matt 			"%s: Unexpected R_COPY relocation in shared library",
    272      1.1      matt 				    obj->path);
    273      1.1      matt 				return -1;
    274      1.1      matt 			}
    275      1.1      matt 			rdbg(("COPY (avoid in main)"));
    276      1.1      matt 			break;
    277      1.1      matt 
    278  1.7.2.2  pgoyette 		case R_TYPE(TLSDESC):
    279  1.7.2.2  pgoyette 			_rtld_tlsdesc_fill(obj, rela, where);
    280  1.7.2.2  pgoyette 			break;
    281  1.7.2.2  pgoyette 
    282      1.1      matt 		case R_TLS_TYPE(TLS_DTPREL):
    283      1.5  nisimura 			*where = (Elf_Addr)(def->st_value + rela->r_addend);
    284      1.1      matt 
    285      1.5  nisimura 			rdbg(("TLS_DTPREL %s in %s --> %p",
    286      1.1      matt 			    obj->strtab + obj->symtab[symnum].st_name,
    287      1.5  nisimura 			    obj->path, (void *)*where));
    288      1.1      matt 
    289      1.1      matt 			break;
    290      1.1      matt 		case R_TLS_TYPE(TLS_DTPMOD):
    291      1.1      matt 			*where = (Elf_Addr)(defobj->tlsindex);
    292      1.1      matt 
    293      1.1      matt 			rdbg(("TLS_DTPMOD %s in %s --> %p",
    294      1.1      matt 			    obj->strtab + obj->symtab[symnum].st_name,
    295      1.5  nisimura 			    obj->path, (void *)*where));
    296      1.1      matt 
    297      1.1      matt 			break;
    298      1.1      matt 
    299      1.1      matt 		case R_TLS_TYPE(TLS_TPREL):
    300      1.1      matt 			if (!defobj->tls_done &&
    301      1.1      matt 			    _rtld_tls_offset_allocate(obj))
    302      1.1      matt 				return -1;
    303      1.1      matt 
    304      1.1      matt 			*where = (Elf_Addr)def->st_value + defobj->tlsoffset +
    305      1.1      matt 			    sizeof(struct tls_tcb);
    306      1.5  nisimura 			rdbg(("TLS_TPREL %s in %s --> %p in %s",
    307      1.1      matt 			    obj->strtab + obj->symtab[symnum].st_name,
    308      1.5  nisimura 			    obj->path, (void *)*where, defobj->path));
    309      1.1      matt 			break;
    310      1.1      matt 
    311      1.1      matt 		default:
    312      1.1      matt 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    313  1.7.2.3  pgoyette 			    "addend = %p, contents = %p",
    314      1.3     joerg 			    (u_long)ELF_R_SYM(rela->r_info),
    315      1.3     joerg 			    (u_long)ELF_R_TYPE(rela->r_info),
    316      1.5  nisimura 			    (void *)rela->r_offset, (void *)rela->r_addend,
    317  1.7.2.3  pgoyette 			    (void *)*where));
    318      1.1      matt 			_rtld_error("%s: Unsupported relocation type %ld "
    319      1.1      matt 			    "in non-PLT relocations",
    320      1.1      matt 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    321      1.1      matt 			return -1;
    322      1.1      matt 		}
    323      1.1      matt 	}
    324      1.1      matt 	return 0;
    325      1.1      matt }
    326      1.1      matt 
    327      1.1      matt int
    328      1.4     joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
    329      1.1      matt {
    330      1.1      matt 
    331      1.1      matt 	if (!obj->relocbase)
    332      1.1      matt 		return 0;
    333      1.1      matt 
    334      1.7     skrll 	for (const Elf_Rela *rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    335      1.7     skrll 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    336      1.1      matt 
    337      1.7     skrll 		assert((ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT)) ||
    338      1.7     skrll 		    (ELF_R_TYPE(rela->r_info) == R_TYPE(TLSDESC)));
    339      1.1      matt 
    340      1.7     skrll 		switch (ELF_R_TYPE(rela->r_info)) {
    341      1.7     skrll 		case R_TYPE(JUMP_SLOT):
    342      1.7     skrll 			/* Just relocate the GOT slots pointing into the PLT */
    343      1.7     skrll 			*where += (Elf_Addr)obj->relocbase;
    344      1.7     skrll 			rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    345      1.7     skrll 			break;
    346      1.7     skrll 		case R_TYPE(TLSDESC):
    347  1.7.2.2  pgoyette 			_rtld_tlsdesc_fill(obj, rela, where);
    348      1.7     skrll 			break;
    349      1.7     skrll 		}
    350      1.1      matt 	}
    351      1.1      matt 
    352      1.1      matt 	return 0;
    353      1.1      matt }
    354      1.1      matt 
    355      1.7     skrll void
    356      1.7     skrll _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
    357      1.7     skrll {
    358      1.7     skrll 	const Elf_Rela *rela;
    359      1.7     skrll 	Elf_Addr *where, target;
    360      1.7     skrll 
    361      1.7     skrll 	while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
    362      1.7     skrll 		rela = obj->pltrelalim - obj->ifunc_remaining;
    363      1.7     skrll 		--obj->ifunc_remaining;
    364      1.7     skrll 		if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE)) {
    365      1.7     skrll 			where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    366      1.7     skrll 			target = (Elf_Addr)(obj->relocbase + rela->r_addend);
    367      1.7     skrll 			_rtld_exclusive_exit(mask);
    368      1.7     skrll 			target = _rtld_resolve_ifunc2(obj, target);
    369      1.7     skrll 			_rtld_exclusive_enter(mask);
    370      1.7     skrll 			if (*where != target)
    371      1.7     skrll 				*where = target;
    372      1.7     skrll 		}
    373      1.7     skrll 	}
    374      1.7     skrll }
    375      1.7     skrll 
    376      1.1      matt static int
    377      1.7     skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
    378      1.1      matt 	Elf_Addr *tp)
    379      1.1      matt {
    380      1.7     skrll 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    381      1.1      matt 	Elf_Addr new_value;
    382      1.1      matt 	const Elf_Sym  *def;
    383      1.1      matt 	const Obj_Entry *defobj;
    384      1.1      matt 
    385  1.7.2.2  pgoyette 	switch (ELF_R_TYPE(rela->r_info)) {
    386  1.7.2.2  pgoyette 	case R_TYPE(JUMP_SLOT):
    387  1.7.2.2  pgoyette 		def = _rtld_find_plt_symdef(ELF_R_SYM(rela->r_info), obj,
    388  1.7.2.2  pgoyette 		    &defobj, tp != NULL);
    389  1.7.2.2  pgoyette 		if (__predict_false(def == NULL))
    390  1.7.2.2  pgoyette 			return -1;
    391  1.7.2.2  pgoyette 		if (__predict_false(def == &_rtld_sym_zero))
    392      1.2     joerg 			return 0;
    393  1.7.2.2  pgoyette 
    394  1.7.2.2  pgoyette 		if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    395  1.7.2.2  pgoyette 			if (tp == NULL)
    396  1.7.2.2  pgoyette 				return 0;
    397  1.7.2.2  pgoyette 			new_value = _rtld_resolve_ifunc(defobj, def);
    398  1.7.2.2  pgoyette 		} else {
    399  1.7.2.2  pgoyette 			new_value = (Elf_Addr)(defobj->relocbase +
    400  1.7.2.2  pgoyette 			     def->st_value);
    401  1.7.2.2  pgoyette 		}
    402  1.7.2.2  pgoyette 		rdbg(("bind now/fixup in %s --> old=%p new=%p",
    403  1.7.2.2  pgoyette 		    defobj->strtab + def->st_name, (void *)*where,
    404  1.7.2.2  pgoyette 		    (void *)new_value));
    405  1.7.2.2  pgoyette 		if (*where != new_value)
    406  1.7.2.2  pgoyette 			*where = new_value;
    407  1.7.2.2  pgoyette 		if (tp)
    408  1.7.2.2  pgoyette 			*tp = new_value;
    409  1.7.2.2  pgoyette 		break;
    410  1.7.2.2  pgoyette 	case R_TYPE(TLSDESC):
    411  1.7.2.2  pgoyette 		if (ELF_R_SYM(rela->r_info) != 0) {
    412  1.7.2.2  pgoyette 			struct tls_data *tlsdesc = (struct tls_data *)where[1];
    413  1.7.2.2  pgoyette 			if (tlsdesc->index == -1)
    414  1.7.2.2  pgoyette 				_rtld_tlsdesc_handle_locked(tlsdesc, SYMLOOK_IN_PLT);
    415  1.7.2.2  pgoyette 		}
    416  1.7.2.2  pgoyette 		break;
    417      1.2     joerg 	}
    418      1.1      matt 
    419      1.1      matt 	return 0;
    420      1.1      matt }
    421      1.1      matt 
    422      1.5  nisimura Elf_Addr
    423      1.7     skrll _rtld_bind(const Obj_Entry *obj, Elf_Word relaidx)
    424      1.1      matt {
    425      1.7     skrll 	const Elf_Rela *rela = obj->pltrela + relaidx;
    426  1.7.2.1  pgoyette 	Elf_Addr new_value = 0;
    427      1.1      matt 
    428      1.1      matt 	_rtld_shared_enter();
    429      1.7     skrll 	int err = _rtld_relocate_plt_object(obj, rela, &new_value);
    430      1.1      matt 	if (err)
    431      1.1      matt 		_rtld_die();
    432      1.1      matt 	_rtld_shared_exit();
    433      1.1      matt 
    434      1.5  nisimura 	return new_value;
    435      1.1      matt }
    436      1.1      matt int
    437      1.1      matt _rtld_relocate_plt_objects(const Obj_Entry *obj)
    438      1.1      matt {
    439      1.7     skrll 	const Elf_Rela *rela;
    440      1.1      matt 	int err = 0;
    441  1.7.2.3  pgoyette 
    442      1.7     skrll 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    443      1.7     skrll 		err = _rtld_relocate_plt_object(obj, rela, NULL);
    444      1.1      matt 		if (err)
    445      1.1      matt 			break;
    446      1.1      matt 	}
    447      1.1      matt 
    448      1.1      matt 	return err;
    449      1.1      matt }
    450