Home | History | Annotate | Line # | Download | only in aarch64
mdreloc.c revision 1.12
      1  1.12     skrll /* $NetBSD: mdreloc.c,v 1.12 2018/11/23 11:26:05 skrll 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.9  jakllsch /*-
     33   1.9  jakllsch  * Copyright (c) 2014-2015 The FreeBSD Foundation
     34   1.9  jakllsch  * All rights reserved.
     35   1.9  jakllsch  *
     36   1.9  jakllsch  * Portions of this software were developed by Andrew Turner
     37   1.9  jakllsch  * under sponsorship from the FreeBSD Foundation.
     38   1.9  jakllsch  *
     39   1.9  jakllsch  * Redistribution and use in source and binary forms, with or without
     40   1.9  jakllsch  * modification, are permitted provided that the following conditions
     41   1.9  jakllsch  * are met:
     42   1.9  jakllsch  * 1. Redistributions of source code must retain the above copyright
     43   1.9  jakllsch  *    notice, this list of conditions and the following disclaimer.
     44   1.9  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     45   1.9  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     46   1.9  jakllsch  *    documentation and/or other materials provided with the distribution.
     47   1.9  jakllsch  *
     48   1.9  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     49   1.9  jakllsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50   1.9  jakllsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51   1.9  jakllsch  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     52   1.9  jakllsch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53   1.9  jakllsch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54   1.9  jakllsch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55   1.9  jakllsch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56   1.9  jakllsch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57   1.9  jakllsch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58   1.9  jakllsch  * SUCH DAMAGE.
     59   1.9  jakllsch  */
     60   1.9  jakllsch 
     61   1.1      matt #include <sys/cdefs.h>
     62   1.1      matt #ifndef lint
     63  1.12     skrll __RCSID("$NetBSD: mdreloc.c,v 1.12 2018/11/23 11:26:05 skrll 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.9  jakllsch struct tls_data {
     73   1.9  jakllsch 	int64_t index;
     74   1.9  jakllsch 	Obj_Entry *obj;
     75   1.9  jakllsch 	const Elf_Rela *rela;
     76   1.9  jakllsch };
     77   1.9  jakllsch 
     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.9  jakllsch void *_rtld_tlsdesc_dynamic(void *);
     83   1.9  jakllsch 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.9  jakllsch static struct tls_data *
    120   1.9  jakllsch _rtld_tlsdesc_alloc(Obj_Entry *obj, const Elf_Rela *rela)
    121   1.9  jakllsch {
    122   1.9  jakllsch 	struct tls_data *tlsdesc;
    123   1.9  jakllsch 
    124   1.9  jakllsch 	tlsdesc = xmalloc(sizeof(*tlsdesc));
    125   1.9  jakllsch 	tlsdesc->index = -1;
    126   1.9  jakllsch 	tlsdesc->obj = obj;
    127   1.9  jakllsch 	tlsdesc->rela = rela;
    128   1.9  jakllsch 
    129   1.9  jakllsch 	return tlsdesc;
    130   1.9  jakllsch }
    131   1.9  jakllsch 
    132   1.9  jakllsch static int64_t
    133   1.9  jakllsch _rtld_tlsdesc_handle_locked(struct tls_data *tlsdesc, u_int flags)
    134   1.9  jakllsch {
    135   1.9  jakllsch 	const Elf_Rela *rela;
    136   1.9  jakllsch 	const Elf_Sym *def;
    137   1.9  jakllsch 	const Obj_Entry *defobj;
    138   1.9  jakllsch 	Obj_Entry *obj;
    139   1.9  jakllsch 
    140   1.9  jakllsch 	rela = tlsdesc->rela;
    141   1.9  jakllsch 	obj = tlsdesc->obj;
    142   1.9  jakllsch 
    143   1.9  jakllsch 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, flags);
    144   1.9  jakllsch 	if (def == NULL)
    145   1.9  jakllsch 		_rtld_die();
    146   1.9  jakllsch 
    147   1.9  jakllsch 	tlsdesc->index = defobj->tlsoffset + def->st_value + rela->r_addend +
    148   1.9  jakllsch 	    sizeof(struct tls_tcb);
    149   1.9  jakllsch 
    150   1.9  jakllsch 	return tlsdesc->index;
    151   1.9  jakllsch }
    152   1.9  jakllsch 
    153   1.9  jakllsch int64_t
    154   1.9  jakllsch _rtld_tlsdesc_handle(struct tls_data *tlsdesc, u_int flags)
    155   1.9  jakllsch {
    156   1.9  jakllsch 	sigset_t mask;
    157   1.9  jakllsch 
    158   1.9  jakllsch 	/* We have already found the index, return it */
    159   1.9  jakllsch 	if (tlsdesc->index >= 0)
    160   1.9  jakllsch 		return tlsdesc->index;
    161   1.9  jakllsch 
    162   1.9  jakllsch 	_rtld_exclusive_enter(&mask);
    163   1.9  jakllsch 	/* tlsdesc->index may have been set by another thread */
    164   1.9  jakllsch 	if (tlsdesc->index == -1)
    165   1.9  jakllsch 		_rtld_tlsdesc_handle_locked(tlsdesc, flags);
    166   1.9  jakllsch 	_rtld_exclusive_exit(&mask);
    167   1.9  jakllsch 
    168   1.9  jakllsch 	return tlsdesc->index;
    169   1.9  jakllsch }
    170   1.9  jakllsch 
    171   1.9  jakllsch static void
    172   1.9  jakllsch _rtld_tlsdesc_fill(Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *where)
    173   1.9  jakllsch {
    174   1.9  jakllsch 	if (ELF_R_SYM(rela->r_info) == 0) {
    175   1.9  jakllsch 		where[0] = (Elf_Addr)_rtld_tlsdesc;
    176   1.9  jakllsch 		where[1] = obj->tlsoffset + rela->r_addend +
    177   1.9  jakllsch 		    sizeof(struct tls_tcb);
    178   1.9  jakllsch 	} else {
    179   1.9  jakllsch 		where[0] = (Elf_Addr)_rtld_tlsdesc_dynamic;
    180   1.9  jakllsch 		where[1] = (Elf_Addr)_rtld_tlsdesc_alloc(obj, rela);
    181   1.9  jakllsch 	}
    182   1.9  jakllsch }
    183   1.9  jakllsch 
    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.12     skrll 		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.9  jakllsch 		case R_TYPE(TLSDESC):
    279   1.9  jakllsch 			_rtld_tlsdesc_fill(obj, rela, where);
    280   1.9  jakllsch 			break;
    281   1.9  jakllsch 
    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.12     skrll 			    "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.12     skrll 			    (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.9  jakllsch 			_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.9  jakllsch 	switch (ELF_R_TYPE(rela->r_info)) {
    386   1.9  jakllsch 	case R_TYPE(JUMP_SLOT):
    387   1.9  jakllsch 		def = _rtld_find_plt_symdef(ELF_R_SYM(rela->r_info), obj,
    388   1.9  jakllsch 		    &defobj, tp != NULL);
    389   1.9  jakllsch 		if (__predict_false(def == NULL))
    390   1.9  jakllsch 			return -1;
    391   1.9  jakllsch 		if (__predict_false(def == &_rtld_sym_zero))
    392   1.9  jakllsch 			return 0;
    393   1.1      matt 
    394   1.9  jakllsch 		if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    395   1.9  jakllsch 			if (tp == NULL)
    396   1.9  jakllsch 				return 0;
    397   1.9  jakllsch 			new_value = _rtld_resolve_ifunc(defobj, def);
    398   1.9  jakllsch 		} else {
    399   1.9  jakllsch 			new_value = (Elf_Addr)(defobj->relocbase +
    400   1.9  jakllsch 			     def->st_value);
    401   1.9  jakllsch 		}
    402   1.9  jakllsch 		rdbg(("bind now/fixup in %s --> old=%p new=%p",
    403   1.9  jakllsch 		    defobj->strtab + def->st_name, (void *)*where,
    404   1.9  jakllsch 		    (void *)new_value));
    405   1.9  jakllsch 		if (*where != new_value)
    406   1.9  jakllsch 			*where = new_value;
    407   1.9  jakllsch 		if (tp)
    408   1.9  jakllsch 			*tp = new_value;
    409   1.9  jakllsch 		break;
    410   1.9  jakllsch 	case R_TYPE(TLSDESC):
    411   1.9  jakllsch 		if (ELF_R_SYM(rela->r_info) != 0) {
    412   1.9  jakllsch 			struct tls_data *tlsdesc = (struct tls_data *)where[1];
    413   1.9  jakllsch 			if (tlsdesc->index == -1)
    414  1.10  jakllsch 				_rtld_tlsdesc_handle_locked(tlsdesc, SYMLOOK_IN_PLT);
    415   1.9  jakllsch 		}
    416   1.9  jakllsch 		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.8  christos 	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.11     skrll 
    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