Home | History | Annotate | Line # | Download | only in hppa
hppa_reloc.c revision 1.45
      1  1.45     joerg /*	$NetBSD: hppa_reloc.c,v 1.45 2017/08/10 19:03:26 joerg Exp $	*/
      2   1.1  fredette 
      3   1.1  fredette /*-
      4  1.21     skrll  * Copyright (c) 2002, 2004 The NetBSD Foundation, Inc.
      5   1.1  fredette  * All rights reserved.
      6   1.1  fredette  *
      7   1.1  fredette  * This code is derived from software contributed to The NetBSD Foundation
      8  1.21     skrll  * by Matt Fredette and Nick Hudson.
      9   1.1  fredette  *
     10   1.1  fredette  * Redistribution and use in source and binary forms, with or without
     11   1.1  fredette  * modification, are permitted provided that the following conditions
     12   1.1  fredette  * are met:
     13   1.1  fredette  * 1. Redistributions of source code must retain the above copyright
     14   1.1  fredette  *    notice, this list of conditions and the following disclaimer.
     15   1.1  fredette  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  fredette  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  fredette  *    documentation and/or other materials provided with the distribution.
     18   1.1  fredette  *
     19   1.1  fredette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  fredette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  fredette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  fredette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  fredette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  fredette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  fredette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  fredette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  fredette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  fredette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  fredette  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  fredette  */
     31   1.1  fredette 
     32  1.23     skrll #include <sys/cdefs.h>
     33  1.23     skrll #ifndef lint
     34  1.45     joerg __RCSID("$NetBSD: hppa_reloc.c,v 1.45 2017/08/10 19:03:26 joerg Exp $");
     35  1.23     skrll #endif /* not lint */
     36  1.23     skrll 
     37   1.1  fredette #include <stdlib.h>
     38   1.1  fredette #include <sys/types.h>
     39   1.1  fredette #include <sys/queue.h>
     40   1.1  fredette 
     41  1.24     skrll #include <string.h>
     42  1.24     skrll 
     43   1.1  fredette #include "rtld.h"
     44   1.1  fredette #include "debug.h"
     45   1.1  fredette 
     46   1.1  fredette #ifdef RTLD_DEBUG_HPPA
     47  1.14   mycroft #define	hdbg(x)		xprintf x
     48   1.1  fredette #else
     49   1.1  fredette #define	hdbg(x)		/* nothing */
     50   1.1  fredette #endif
     51  1.12   mycroft 
     52  1.20     skrll caddr_t _rtld_bind(const Obj_Entry *, const Elf_Addr);
     53  1.12   mycroft void _rtld_bind_start(void);
     54  1.13   mycroft void __rtld_setup_hppa_pltgot(const Obj_Entry *, Elf_Addr *);
     55   1.1  fredette 
     56   1.1  fredette /*
     57  1.24     skrll  * It is possible for the compiler to emit relocations for unaligned data.
     58  1.24     skrll  * We handle this situation with these inlines.
     59  1.24     skrll  */
     60  1.24     skrll #define	RELOC_ALIGNED_P(x) \
     61  1.24     skrll 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
     62  1.24     skrll 
     63  1.24     skrll static inline Elf_Addr
     64  1.24     skrll load_ptr(void *where)
     65  1.24     skrll {
     66  1.24     skrll 	if (__predict_true(RELOC_ALIGNED_P(where)))
     67  1.24     skrll 		return *(Elf_Addr *)where;
     68  1.24     skrll 	else {
     69  1.24     skrll 		Elf_Addr res;
     70  1.24     skrll 
     71  1.24     skrll 		(void)memcpy(&res, where, sizeof(res));
     72  1.24     skrll 		return res;
     73  1.24     skrll 	}
     74  1.24     skrll }
     75  1.24     skrll 
     76  1.24     skrll static inline void
     77  1.24     skrll store_ptr(void *where, Elf_Addr val)
     78  1.24     skrll {
     79  1.24     skrll 	if (__predict_true(RELOC_ALIGNED_P(where)))
     80  1.24     skrll 		*(Elf_Addr *)where = val;
     81  1.24     skrll 	else
     82  1.24     skrll 		(void)memcpy(where, &val, sizeof(val));
     83  1.24     skrll }
     84  1.24     skrll 
     85  1.42     skrll static __inline void
     86  1.42     skrll fdc(void *addr)
     87  1.42     skrll {
     88  1.42     skrll 	__asm volatile("fdc %%r0(%%sr0, %0)" : : "r" (addr));
     89  1.42     skrll }
     90  1.42     skrll 
     91  1.42     skrll static __inline void
     92  1.42     skrll fic(void *addr)
     93  1.42     skrll {
     94  1.42     skrll 	__asm volatile("fic %%r0(%%sr0,%0)" : : "r" (addr));
     95  1.42     skrll }
     96  1.42     skrll 
     97  1.42     skrll static __inline void
     98  1.42     skrll sync(void)
     99  1.42     skrll {
    100  1.42     skrll 	__asm volatile("sync" : : : "memory");
    101  1.42     skrll }
    102  1.42     skrll 
    103  1.42     skrll #define PLT_STUB_MAGIC1	0x00c0ffee
    104  1.42     skrll #define PLT_STUB_MAGIC2	0xdeadbeef
    105  1.42     skrll 
    106  1.42     skrll #define PLT_STUB_INSN1	0x0e801081	/* ldw	0(%r20), %r1 */
    107  1.42     skrll #define PLT_STUB_INSN2	0xe820c000	/* bv	%r0(%r1) */
    108  1.42     skrll 
    109  1.24     skrll /*
    110  1.34     skrll  * In the runtime architecture (ABI), PLABEL function pointers are
    111  1.34     skrll  * distinguished from normal function pointers by having the next-least-
    112  1.34     skrll  * significant bit set.  (This bit is referred to as the L field in HP
    113  1.34     skrll  * documentation).  The $$dyncall millicode is aware of this.
    114   1.1  fredette  */
    115   1.1  fredette #define	RTLD_MAKE_PLABEL(plabel)	(((Elf_Addr)(plabel)) | (1 << 1))
    116   1.1  fredette #define RTLD_IS_PLABEL(addr)		(((Elf_Addr)(addr)) & (1 << 1))
    117   1.1  fredette #define	RTLD_GET_PLABEL(addr)	((hppa_plabel *) (((Elf_Addr)addr) & ~3))
    118   1.1  fredette 
    119   1.1  fredette /*
    120   1.1  fredette  * This is the PLABEL structure.  The function PC and
    121   1.1  fredette  * shared linkage members must come first, as they are
    122   1.1  fredette  * the actual PLABEL.
    123   1.1  fredette  */
    124   1.1  fredette typedef struct _hppa_plabel {
    125   1.1  fredette 	Elf_Addr	hppa_plabel_pc;
    126   1.1  fredette 	Elf_Addr	hppa_plabel_sl;
    127   1.1  fredette 	SLIST_ENTRY(_hppa_plabel)	hppa_plabel_next;
    128   1.1  fredette } hppa_plabel;
    129   1.1  fredette 
    130   1.1  fredette /*
    131   1.1  fredette  * For now allocated PLABEL structures are tracked on a
    132   1.1  fredette  * singly linked list.  This maybe should be revisited.
    133   1.1  fredette  */
    134   1.1  fredette static SLIST_HEAD(hppa_plabel_head, _hppa_plabel) hppa_plabel_list
    135   1.1  fredette     = SLIST_HEAD_INITIALIZER(hppa_plabel_list);
    136   1.1  fredette 
    137   1.1  fredette /*
    138   1.1  fredette  * Because I'm hesitant to use NEW while relocating self,
    139   1.1  fredette  * this is a small pool of preallocated PLABELs.
    140   1.1  fredette  */
    141  1.40       chs #define	HPPA_PLABEL_PRE	(32)
    142   1.1  fredette static hppa_plabel hppa_plabel_pre[HPPA_PLABEL_PRE];
    143   1.1  fredette static int hppa_plabel_pre_next = 0;
    144   1.1  fredette 
    145  1.20     skrll void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
    146  1.20     skrll int _rtld_relocate_plt_objects(const Obj_Entry *);
    147  1.25     skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
    148  1.25     skrll     const Elf_Rela *, Elf_Addr *);
    149  1.25     skrll 
    150   1.1  fredette /*
    151   1.1  fredette  * This bootstraps the dynamic linker by relocating its GOT.
    152   1.1  fredette  * On the hppa, unlike on other architectures, static strings
    153   1.1  fredette  * are found through the GOT.  Static strings are essential
    154   1.1  fredette  * for RTLD_DEBUG, and I suspect they're used early even when
    155   1.1  fredette  * !defined(RTLD_DEBUG), making relocating the GOT essential.
    156   1.1  fredette  *
    157   1.1  fredette  * It gets worse.  Relocating the GOT doesn't mean just walking
    158   1.1  fredette  * it and adding the relocbase to all of the entries.  You must
    159   1.1  fredette  * find and use the GOT relocations, since those RELA relocations
    160   1.1  fredette  * have the necessary addends - the GOT comes initialized as
    161   1.1  fredette  * zeroes.
    162   1.1  fredette  */
    163   1.1  fredette void
    164  1.20     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    165   1.1  fredette {
    166   1.1  fredette 	const Elf_Rela	*relafirst, *rela, *relalim;
    167  1.22       chs 	Elf_Addr        relasz;
    168  1.24     skrll 	void		*where;
    169  1.20     skrll 	Elf_Addr	*pltgot;
    170  1.20     skrll 	const Elf_Rela	*plabel_relocs[HPPA_PLABEL_PRE];
    171  1.20     skrll 	int		nplabel_relocs = 0;
    172  1.20     skrll 	int		i;
    173  1.20     skrll 	const Elf_Sym	*symtab, *sym;
    174  1.20     skrll 	unsigned long	symnum;
    175  1.20     skrll 	hppa_plabel	*plabel;
    176   1.1  fredette 
    177  1.22       chs 	/*
    178  1.22       chs 	 * Process the DYNAMIC section, looking for the non-PLT relocations.
    179   1.1  fredette 	 */
    180   1.1  fredette 	relafirst = NULL;
    181  1.22       chs 	relasz = 0;
    182  1.22       chs 	symtab = NULL;
    183  1.22       chs 	pltgot = NULL;
    184   1.1  fredette 	for (; dynp->d_tag != DT_NULL; ++dynp) {
    185   1.1  fredette 		switch (dynp->d_tag) {
    186   1.1  fredette 
    187   1.1  fredette 		case DT_RELA:
    188   1.1  fredette 			relafirst = (const Elf_Rela *)
    189   1.1  fredette 			    (relocbase + dynp->d_un.d_ptr);
    190   1.1  fredette 			break;
    191   1.1  fredette 
    192   1.1  fredette 		case DT_RELASZ:
    193   1.1  fredette 			relasz = dynp->d_un.d_val;
    194   1.1  fredette 			break;
    195  1.20     skrll 
    196  1.20     skrll 		case DT_SYMTAB:
    197  1.20     skrll 			symtab = (const Elf_Sym *)
    198  1.20     skrll 			    (relocbase + dynp->d_un.d_ptr);
    199  1.20     skrll 			break;
    200  1.20     skrll 
    201  1.20     skrll 		case DT_PLTGOT:
    202  1.20     skrll 			pltgot = (Elf_Addr *)
    203  1.20     skrll 			    (relocbase + dynp->d_un.d_ptr);
    204  1.35     skrll 			break;
    205   1.1  fredette 		}
    206   1.1  fredette 	}
    207  1.28       mjf 	relalim = (const Elf_Rela *)((const char *)relafirst + relasz);
    208   1.1  fredette 
    209  1.18     skrll 	for (rela = relafirst; rela < relalim; rela++) {
    210  1.20     skrll 		symnum = ELF_R_SYM(rela->r_info);
    211  1.24     skrll 		where = (void *)(relocbase + rela->r_offset);
    212  1.20     skrll 
    213  1.20     skrll 		switch (ELF_R_TYPE(rela->r_info)) {
    214  1.20     skrll 		case R_TYPE(DIR32):
    215  1.20     skrll 			if (symnum == 0)
    216  1.24     skrll 				store_ptr(where,
    217  1.24     skrll 				    relocbase + rela->r_addend);
    218  1.20     skrll 			else {
    219  1.20     skrll 				sym = symtab + symnum;
    220  1.24     skrll 				store_ptr(where,
    221  1.24     skrll 				    relocbase + rela->r_addend + sym->st_value);
    222  1.20     skrll 			}
    223  1.20     skrll 			break;
    224  1.20     skrll 
    225  1.20     skrll 		case R_TYPE(PLABEL32):
    226  1.20     skrll 			/*
    227  1.20     skrll 			 * PLABEL32 relocation processing is done in two phases
    228  1.20     skrll 			 *
    229  1.20     skrll 			 *  i) local function relocations (symbol number == 0)
    230  1.20     skrll 			 *     can be resolved immediately.
    231  1.20     skrll 			 *
    232  1.20     skrll 			 * ii) external function relocations are deferred until
    233  1.20     skrll 			 *     we finish all other relocations so that global
    234  1.20     skrll 			 *     data isn't accessed until all other non-PLT
    235  1.20     skrll 			 *     relocations have been done.
    236  1.20     skrll 			 */
    237  1.20     skrll 			if (symnum == 0)
    238  1.20     skrll 				*((Elf_Addr *)where) =
    239  1.20     skrll 				    relocbase + rela->r_addend;
    240  1.20     skrll 			else
    241  1.20     skrll 				plabel_relocs[nplabel_relocs++] = rela;
    242  1.20     skrll 			break;
    243  1.20     skrll 
    244  1.20     skrll 		default:
    245  1.20     skrll 			break;
    246  1.20     skrll 		}
    247   1.1  fredette 	}
    248   1.1  fredette 
    249  1.20     skrll 	assert(nplabel_relocs < HPPA_PLABEL_PRE);
    250  1.20     skrll 	for (i = 0; i < nplabel_relocs; i++) {
    251  1.20     skrll 		rela = plabel_relocs[i];
    252  1.24     skrll 		where = (void *)(relocbase + rela->r_offset);
    253  1.20     skrll 		sym = symtab + ELF_R_SYM(rela->r_info);
    254  1.20     skrll 
    255  1.20     skrll 		plabel = &hppa_plabel_pre[hppa_plabel_pre_next++];
    256  1.20     skrll 
    257  1.35     skrll 		plabel->hppa_plabel_pc = (Elf_Addr)
    258  1.20     skrll 		    (relocbase + sym->st_value + rela->r_addend);
    259  1.35     skrll 		plabel->hppa_plabel_sl = (Elf_Addr)pltgot;
    260  1.20     skrll 
    261  1.35     skrll 		SLIST_INSERT_HEAD(&hppa_plabel_list, plabel, hppa_plabel_next);
    262  1.20     skrll 		*((Elf_Addr *)where) = (Elf_Addr)(RTLD_MAKE_PLABEL(plabel));
    263  1.20     skrll 	}
    264  1.20     skrll 
    265   1.1  fredette #if defined(RTLD_DEBUG_HPPA)
    266  1.18     skrll 	for (rela = relafirst; rela < relalim; rela++) {
    267  1.24     skrll 		where = (void *)(relocbase + rela->r_offset);
    268  1.20     skrll 
    269  1.20     skrll 		switch (ELF_R_TYPE(rela->r_info)) {
    270  1.20     skrll 		case R_TYPE(DIR32):
    271  1.20     skrll 			hdbg(("DIR32 rela @%p(%p) -> %p(%p)\n",
    272   1.1  fredette 			    (void *)rela->r_offset,
    273   1.1  fredette 			    (void *)where,
    274   1.1  fredette 			    (void *)rela->r_addend,
    275  1.20     skrll 			    (void *)*((Elf_Addr *)where) ));
    276  1.20     skrll 			break;
    277  1.20     skrll 
    278  1.20     skrll 		case R_TYPE(PLABEL32):
    279  1.20     skrll 			symnum = ELF_R_SYM(rela->r_info);
    280  1.20     skrll 			if (symnum == 0) {
    281  1.20     skrll 				hdbg(("PLABEL rela @%p(%p) -> %p(%p)\n",
    282  1.20     skrll 		    		    (void *)rela->r_offset,
    283  1.20     skrll 		    		    (void *)where,
    284  1.20     skrll 		    		    (void *)rela->r_addend,
    285  1.20     skrll 		    		    (void *)*((Elf_Addr *)where) ));
    286  1.20     skrll 			} else {
    287  1.20     skrll 				sym = symtab + symnum;
    288  1.20     skrll 
    289  1.20     skrll 				hdbg(("PLABEL32 rela @%p(%p), symnum=%ld(%p) -> %p(%p)\n",
    290  1.20     skrll 			    	    (void *)rela->r_offset,
    291  1.20     skrll 				    (void *)where,
    292  1.20     skrll 				    symnum,
    293  1.20     skrll 				    (void *)sym->st_value,
    294  1.20     skrll 			    	    (void *)rela->r_addend,
    295  1.20     skrll 				    (void *)*((Elf_Addr *)where) ));
    296  1.20     skrll 			}
    297  1.20     skrll 			break;
    298  1.20     skrll 		default:
    299  1.20     skrll 			hdbg(("rela XXX reloc\n"));
    300  1.20     skrll 			break;
    301  1.20     skrll 		}
    302   1.1  fredette 	}
    303   1.1  fredette #endif /* RTLD_DEBUG_HPPA */
    304   1.1  fredette }
    305   1.1  fredette 
    306   1.1  fredette /*
    307   1.1  fredette  * This allocates a PLABEL.  If called with a non-NULL def, the
    308   1.1  fredette  * plabel is for the function associated with that definition
    309   1.1  fredette  * in the defining object defobj, plus the given addend.  If
    310   1.1  fredette  * called with a NULL def, the plabel is for the function at
    311   1.1  fredette  * the (unrelocated) address in addend in the object defobj.
    312   1.1  fredette  */
    313   1.1  fredette Elf_Addr
    314   1.1  fredette _rtld_function_descriptor_alloc(const Obj_Entry *defobj, const Elf_Sym *def,
    315   1.1  fredette     Elf_Addr addend)
    316   1.1  fredette {
    317   1.1  fredette 	Elf_Addr	func_pc, func_sl;
    318   1.1  fredette 	hppa_plabel	*plabel;
    319   1.1  fredette 
    320   1.1  fredette 	if (def != NULL) {
    321   1.1  fredette 
    322   1.1  fredette 		/*
    323   1.1  fredette 		 * We assume that symbols of type STT_NOTYPE
    324   1.1  fredette 		 * are undefined.  Return NULL for these.
    325   1.1  fredette 		 */
    326   1.1  fredette 		if (ELF_ST_TYPE(def->st_info) == STT_NOTYPE)
    327   1.1  fredette 			return (Elf_Addr)NULL;
    328   1.1  fredette 
    329   1.1  fredette 		/* Otherwise assert that this symbol must be a function. */
    330   1.1  fredette 		assert(ELF_ST_TYPE(def->st_info) == STT_FUNC);
    331   1.1  fredette 
    332   1.1  fredette 		func_pc = (Elf_Addr)(defobj->relocbase + def->st_value +
    333   1.1  fredette 		    addend);
    334   1.1  fredette 	} else
    335   1.1  fredette 		func_pc = (Elf_Addr)(defobj->relocbase + addend);
    336   1.1  fredette 
    337   1.1  fredette 	/*
    338   1.1  fredette 	 * Search the existing PLABELs for one matching
    339   1.1  fredette 	 * this function.  If there is one, return it.
    340   1.1  fredette 	 */
    341  1.20     skrll 	func_sl = (Elf_Addr)(defobj->pltgot);
    342   1.1  fredette 	SLIST_FOREACH(plabel, &hppa_plabel_list, hppa_plabel_next)
    343   1.1  fredette 		if (plabel->hppa_plabel_pc == func_pc &&
    344   1.1  fredette 		    plabel->hppa_plabel_sl == func_sl)
    345   1.1  fredette 			return RTLD_MAKE_PLABEL(plabel);
    346   1.1  fredette 
    347   1.1  fredette 	/*
    348   1.1  fredette 	 * Once we've used up the preallocated set, we start
    349   1.1  fredette 	 * using NEW to allocate plabels.
    350   1.1  fredette 	 */
    351   1.1  fredette 	if (hppa_plabel_pre_next < HPPA_PLABEL_PRE)
    352   1.1  fredette 		plabel = &hppa_plabel_pre[hppa_plabel_pre_next++];
    353   1.1  fredette 	else {
    354   1.1  fredette 		plabel = NEW(hppa_plabel);
    355   1.1  fredette 		if (plabel == NULL)
    356   1.1  fredette 			return (Elf_Addr)-1;
    357   1.1  fredette 	}
    358   1.1  fredette 
    359   1.1  fredette 	/* Fill the new entry and insert it on the list. */
    360   1.1  fredette 	plabel->hppa_plabel_pc = func_pc;
    361   1.1  fredette 	plabel->hppa_plabel_sl = func_sl;
    362   1.1  fredette 	SLIST_INSERT_HEAD(&hppa_plabel_list, plabel, hppa_plabel_next);
    363   1.1  fredette 
    364   1.1  fredette 	return RTLD_MAKE_PLABEL(plabel);
    365   1.1  fredette }
    366   1.1  fredette 
    367   1.1  fredette /*
    368   1.1  fredette  * If a pointer is a PLABEL, this unwraps it.
    369   1.1  fredette  */
    370   1.1  fredette const void *
    371   1.1  fredette _rtld_function_descriptor_function(const void *addr)
    372   1.1  fredette {
    373   1.1  fredette 	return (RTLD_IS_PLABEL(addr) ?
    374   1.1  fredette 	    (const void *) RTLD_GET_PLABEL(addr)->hppa_plabel_pc :
    375   1.1  fredette 	    addr);
    376   1.1  fredette }
    377   1.1  fredette 
    378   1.2   mycroft /* This sets up an object's GOT. */
    379   1.2   mycroft void
    380   1.2   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
    381   1.2   mycroft {
    382  1.42     skrll 	Elf_Word *got = obj->pltgot;
    383  1.42     skrll 
    384  1.42     skrll 	assert(got[-2] == PLT_STUB_MAGIC1);
    385  1.42     skrll 	assert(got[-1] == PLT_STUB_MAGIC2);
    386  1.42     skrll 
    387  1.42     skrll 	__rtld_setup_hppa_pltgot(obj, got);
    388  1.42     skrll 
    389  1.42     skrll 	fdc(&got[-2]);
    390  1.42     skrll 	fdc(&got[-1]);
    391  1.42     skrll 	fdc(&got[1]);
    392  1.42     skrll 	sync();
    393  1.42     skrll 	fic(&got[-2]);
    394  1.42     skrll 	fic(&got[-1]);
    395  1.42     skrll 	fic(&got[1]);
    396  1.42     skrll 	sync();
    397  1.42     skrll 
    398  1.42     skrll 	/*
    399  1.42     skrll 	 * libc makes use of %t1 (%r22) to pass errno values to __cerror. Fixup
    400  1.42     skrll 	 * the PLT stub to not use %r22.
    401  1.42     skrll 	 */
    402  1.42     skrll 	got[-7] = PLT_STUB_INSN1;
    403  1.42     skrll 	got[-6] = PLT_STUB_INSN2;
    404  1.42     skrll 	fdc(&got[-7]);
    405  1.42     skrll 	fdc(&got[-6]);
    406  1.42     skrll 	sync();
    407  1.42     skrll 	fic(&got[-7]);
    408  1.42     skrll 	fic(&got[-6]);
    409  1.42     skrll 	sync();
    410   1.4   mycroft }
    411   1.4   mycroft 
    412   1.4   mycroft int
    413  1.33     joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    414   1.4   mycroft {
    415   1.5   mycroft 	const Elf_Rela *rela;
    416  1.44     joerg 	const Elf_Sym *def = NULL;
    417  1.44     joerg 	const Obj_Entry *defobj = NULL;
    418  1.44     joerg 	unsigned long last_symnum = ULONG_MAX;
    419   1.5   mycroft 
    420   1.5   mycroft 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    421   1.5   mycroft 		Elf_Addr        *where;
    422   1.5   mycroft 		Elf_Addr         tmp;
    423   1.6   mycroft 		unsigned long	 symnum;
    424   1.5   mycroft 
    425   1.5   mycroft 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    426   1.5   mycroft 
    427  1.44     joerg 		/* First, handle DIR32 and PLABEL32 without symbol. */
    428  1.44     joerg 		if (ELF_R_SYM(rela->r_info) == 0) {
    429  1.44     joerg 			switch (ELF_R_TYPE(rela->r_info)) {
    430  1.44     joerg 			default:
    431  1.44     joerg 				break;
    432  1.44     joerg 			case R_TYPE(DIR32):
    433   1.5   mycroft 				tmp = (Elf_Addr)(obj->relocbase +
    434   1.5   mycroft 				    rela->r_addend);
    435   1.5   mycroft 
    436  1.24     skrll 				if (load_ptr(where) != tmp)
    437  1.24     skrll 					store_ptr(where, tmp);
    438  1.20     skrll 				rdbg(("DIR32 in %s --> %p", obj->path,
    439  1.24     skrll 					    (void *)load_ptr(where)));
    440  1.44     joerg 				continue;
    441  1.44     joerg 			case R_TYPE(PLABEL32):
    442   1.5   mycroft 				/*
    443   1.5   mycroft 				 * This is a PLABEL for a static function, and
    444   1.5   mycroft 				 * the dynamic linker has both allocated a PLT
    445   1.5   mycroft 				 * entry for this function and told us where it
    446   1.5   mycroft 				 * is.  We can safely use the PLT entry as the
    447   1.5   mycroft 				 * PLABEL because there should be no other
    448   1.5   mycroft 				 * PLABEL reloc referencing this function.
    449   1.5   mycroft 				 * This object should also have an IPLT
    450   1.5   mycroft 				 * relocation to initialize the PLT entry.
    451   1.5   mycroft 				 *
    452   1.5   mycroft 				 * The dynamic linker should also have ensured
    453   1.5   mycroft 				 * that the addend has the
    454   1.5   mycroft 				 * next-least-significant bit set; the
    455   1.5   mycroft 				 * $$dyncall millicode uses this to distinguish
    456   1.5   mycroft 				 * a PLABEL pointer from a plain function
    457   1.5   mycroft 				 * pointer.
    458   1.5   mycroft 				 */
    459  1.19     skrll 				tmp = (Elf_Addr)
    460  1.19     skrll 				    (obj->relocbase + rela->r_addend);
    461   1.5   mycroft 
    462   1.5   mycroft 				if (*where != tmp)
    463   1.5   mycroft 					*where = tmp;
    464  1.14   mycroft 				rdbg(("PLABEL32 in %s --> %p", obj->path,
    465  1.14   mycroft 				    (void *)*where));
    466  1.44     joerg 				continue;
    467  1.44     joerg 			}
    468  1.44     joerg 		}
    469  1.44     joerg 
    470  1.44     joerg 		switch (ELF_R_TYPE(rela->r_info)) {
    471  1.44     joerg 		case R_TYPE(DIR32):
    472  1.44     joerg 		case R_TYPE(PLABEL32):
    473  1.44     joerg 		case R_TYPE(COPY):
    474  1.44     joerg 		case R_TYPE(TLS_TPREL32):
    475  1.44     joerg 		case R_TYPE(TLS_DTPMOD32):
    476  1.44     joerg 		case R_TYPE(TLS_DTPOFF32):
    477  1.44     joerg 			symnum = ELF_R_SYM(rela->r_info);
    478  1.44     joerg 			if (last_symnum != symnum) {
    479  1.44     joerg 				last_symnum = symnum;
    480  1.44     joerg 				if (ELF_R_TYPE(rela->r_info) == R_TYPE(DIR32)) {
    481  1.44     joerg 					/*
    482  1.44     joerg 					 * DIR32 relocation against local
    483  1.44     joerg 					 * symbols are special...
    484  1.44     joerg 					 */
    485  1.44     joerg 					def = obj->symtab + symnum;
    486  1.44     joerg 					defobj = obj;
    487  1.44     joerg 					if (def->st_name == 0)
    488  1.44     joerg 						break;
    489  1.44     joerg 				}
    490  1.44     joerg 				def = _rtld_find_symdef(symnum, obj, &defobj,
    491  1.44     joerg 				    false);
    492  1.44     joerg 				if (def == NULL)
    493  1.44     joerg 					return -1;
    494   1.5   mycroft 			}
    495   1.5   mycroft 			break;
    496  1.44     joerg 		default:
    497  1.44     joerg 			break;
    498  1.44     joerg 		}
    499  1.44     joerg 
    500  1.44     joerg 		switch (ELF_R_TYPE(rela->r_info)) {
    501  1.44     joerg 		case R_TYPE(NONE):
    502  1.44     joerg 			break;
    503  1.44     joerg 
    504  1.44     joerg 		case R_TYPE(DIR32):
    505  1.44     joerg 			tmp = (Elf_Addr)(defobj->relocbase +
    506  1.44     joerg 			    def->st_value + rela->r_addend);
    507  1.44     joerg 
    508  1.44     joerg 			if (load_ptr(where) != tmp)
    509  1.44     joerg 				store_ptr(where, tmp);
    510  1.44     joerg 			rdbg(("DIR32 %s in %s --> %p in %s",
    511  1.44     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    512  1.44     joerg 			    obj->path, (void *)load_ptr(where),
    513  1.44     joerg 			    defobj->path));
    514  1.44     joerg 			break;
    515  1.44     joerg 
    516  1.44     joerg 		case R_TYPE(PLABEL32):
    517  1.44     joerg 			tmp = _rtld_function_descriptor_alloc(defobj,
    518  1.44     joerg 			    def, rela->r_addend);
    519  1.44     joerg 			if (tmp == (Elf_Addr)-1)
    520  1.44     joerg 				return -1;
    521  1.44     joerg 
    522  1.44     joerg 			if (*where != tmp)
    523  1.44     joerg 				*where = tmp;
    524  1.44     joerg 			rdbg(("PLABEL32 %s in %s --> %p in %s",
    525  1.44     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    526  1.44     joerg 			    obj->path, (void *)*where, defobj->path));
    527  1.44     joerg 			break;
    528   1.4   mycroft 
    529   1.5   mycroft 		case R_TYPE(COPY):
    530   1.4   mycroft 			/*
    531   1.5   mycroft 			 * These are deferred until all other relocations have
    532   1.5   mycroft 			 * been done.  All we do here is make sure that the
    533   1.5   mycroft 			 * COPY relocation is not in a shared library.  They
    534   1.5   mycroft 			 * are allowed only in executable files.
    535   1.4   mycroft 			 */
    536  1.10   mycroft 			if (obj->isdynamic) {
    537   1.5   mycroft 				_rtld_error(
    538   1.5   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    539   1.5   mycroft 				    obj->path);
    540   1.4   mycroft 				return -1;
    541   1.5   mycroft 			}
    542  1.14   mycroft 			rdbg(("COPY (avoid in main)"));
    543   1.5   mycroft 			break;
    544   1.4   mycroft 
    545  1.38     skrll 		case R_TYPE(TLS_TPREL32):
    546  1.38     skrll 			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
    547  1.38     skrll 				return -1;
    548  1.38     skrll 
    549  1.41     skrll 			*where = (Elf_Addr)(defobj->tlsoffset + def->st_value +
    550  1.38     skrll 			    rela->r_addend + sizeof(struct tls_tcb));
    551  1.38     skrll 
    552  1.38     skrll 			rdbg(("TPREL32 %s in %s --> %p in %s",
    553  1.38     skrll 			    obj->strtab + obj->symtab[symnum].st_name,
    554  1.38     skrll 			    obj->path, (void *)*where, defobj->path));
    555  1.38     skrll 			break;
    556  1.38     skrll 
    557  1.36     skrll 		case R_TYPE(TLS_DTPMOD32):
    558  1.36     skrll 			*where = (Elf_Addr)(defobj->tlsindex);
    559  1.36     skrll 
    560  1.36     skrll 			rdbg(("TLS_DTPMOD32 %s in %s --> %p",
    561  1.36     skrll 			    obj->strtab + obj->symtab[symnum].st_name,
    562  1.36     skrll 			    obj->path, (void *)*where));
    563  1.36     skrll 
    564  1.36     skrll 			break;
    565  1.36     skrll 
    566  1.36     skrll 		case R_TYPE(TLS_DTPOFF32):
    567  1.36     skrll 			*where = (Elf_Addr)(def->st_value);
    568  1.36     skrll 
    569  1.36     skrll 			rdbg(("TLS_DTPOFF32 %s in %s --> %p",
    570  1.36     skrll 			    obj->strtab + obj->symtab[symnum].st_name,
    571  1.36     skrll 			    obj->path, (void *)*where));
    572  1.36     skrll 
    573  1.36     skrll 			break;
    574  1.36     skrll 
    575   1.5   mycroft 		default:
    576  1.14   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    577   1.5   mycroft 			    "addend = %p, contents = %p, symbol = %s",
    578   1.6   mycroft 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    579   1.5   mycroft 			    (void *)rela->r_offset, (void *)rela->r_addend,
    580  1.24     skrll 			    (void *)load_ptr(where),
    581   1.6   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    582   1.5   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    583  1.29      jmmv 			    "in non-PLT relocations",
    584   1.5   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    585   1.4   mycroft 			return -1;
    586   1.4   mycroft 		}
    587   1.8   mycroft 	}
    588   1.8   mycroft 	return 0;
    589   1.8   mycroft }
    590   1.8   mycroft 
    591   1.8   mycroft int
    592  1.45     joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
    593   1.8   mycroft {
    594   1.8   mycroft 	const Elf_Rela *rela;
    595   1.8   mycroft 
    596   1.8   mycroft 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    597   1.8   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    598   1.8   mycroft 		Elf_Addr func_pc, func_sl;
    599   1.8   mycroft 
    600   1.8   mycroft 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(IPLT));
    601   1.8   mycroft 
    602   1.8   mycroft 		/*
    603   1.8   mycroft 		 * If this is an IPLT reloc for a static function,
    604   1.8   mycroft 		 * fully resolve the PLT entry now.
    605   1.8   mycroft 		 */
    606   1.8   mycroft 		if (ELF_R_SYM(rela->r_info) == 0) {
    607   1.8   mycroft 			func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
    608  1.20     skrll 			func_sl = (Elf_Addr)(obj->pltgot);
    609   1.8   mycroft 		}
    610   1.8   mycroft 
    611   1.8   mycroft 		/*
    612   1.8   mycroft 		 * Otherwise set up for lazy binding.
    613   1.8   mycroft 		 */
    614   1.8   mycroft 		else {
    615   1.8   mycroft 			/*
    616   1.8   mycroft 			 * This function pointer points to the PLT
    617   1.8   mycroft 			 * stub added by the linker, and instead of
    618   1.8   mycroft 			 * a shared linkage value, we stash this
    619   1.8   mycroft 			 * relocation's offset.  The PLT stub has
    620   1.8   mycroft 			 * already been set up to transfer to
    621   1.8   mycroft 			 * _rtld_bind_start.
    622   1.8   mycroft 			 */
    623  1.20     skrll 			func_pc = ((Elf_Addr)(obj->pltgot)) - 16;
    624  1.19     skrll 			func_sl = (Elf_Addr)
    625  1.28       mjf 			    ((const char *)rela - (const char *)(obj->pltrela));
    626   1.8   mycroft 		}
    627  1.20     skrll 		rdbg(("lazy bind %s(%p) --> old=(%p,%p) new=(%p,%p)",
    628  1.20     skrll 		    obj->path,
    629  1.20     skrll 		    (void *)where,
    630  1.20     skrll 		    (void *)where[0], (void *)where[1],
    631  1.20     skrll 		    (void *)func_pc, (void *)func_sl));
    632   1.8   mycroft 
    633   1.8   mycroft 		/*
    634   1.8   mycroft 		 * Fill this PLT entry and return.
    635   1.8   mycroft 		 */
    636   1.8   mycroft 		where[0] = func_pc;
    637   1.8   mycroft 		where[1] = func_sl;
    638   1.4   mycroft 	}
    639   1.4   mycroft 	return 0;
    640   1.1  fredette }
    641  1.20     skrll 
    642  1.25     skrll static inline int
    643  1.34     skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
    644  1.34     skrll     Elf_Addr *tp)
    645  1.20     skrll {
    646  1.20     skrll 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    647  1.20     skrll 	const Elf_Sym *def;
    648  1.20     skrll 	const Obj_Entry *defobj;
    649  1.20     skrll 	Elf_Addr	func_pc, func_sl;
    650  1.31  christos 	unsigned long info = rela->r_info;
    651  1.20     skrll 
    652  1.31  christos 	assert(ELF_R_TYPE(info) == R_TYPE(IPLT));
    653  1.20     skrll 
    654  1.31  christos 	if (ELF_R_SYM(info) == 0) {
    655  1.25     skrll 		func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
    656  1.25     skrll 		func_sl = (Elf_Addr)(obj->pltgot);
    657  1.25     skrll 	} else {
    658  1.31  christos 		def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj,
    659  1.31  christos 		    tp != NULL);
    660  1.31  christos 		if (__predict_false(def == NULL))
    661  1.25     skrll 			return -1;
    662  1.31  christos 		if (__predict_false(def == &_rtld_sym_zero))
    663  1.31  christos 			return 0;
    664  1.20     skrll 
    665  1.43     joerg 		if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    666  1.43     joerg 			if (tp == NULL)
    667  1.43     joerg 				return 0;
    668  1.43     joerg 			Elf_Addr ptr = _rtld_resolve_ifunc(defobj, def);
    669  1.43     joerg 			assert(RTLD_IS_PLABEL(ptr));
    670  1.43     joerg 			hppa_plabel *label = RTLD_GET_PLABEL(ptr);
    671  1.43     joerg 			func_pc = label->hppa_plabel_pc;
    672  1.43     joerg 			func_sl = label->hppa_plabel_sl;
    673  1.43     joerg 		} else {
    674  1.43     joerg 			func_pc = (Elf_Addr)(defobj->relocbase + def->st_value +
    675  1.43     joerg 			    rela->r_addend);
    676  1.43     joerg 			func_sl = (Elf_Addr)(defobj->pltgot);
    677  1.43     joerg 		}
    678  1.20     skrll 
    679  1.25     skrll 		rdbg(("bind now/fixup in %s --> old=(%p,%p) new=(%p,%p)",
    680  1.25     skrll 		    defobj->strtab + def->st_name,
    681  1.25     skrll 		    (void *)where[0], (void *)where[1],
    682  1.25     skrll 		    (void *)func_pc, (void *)func_sl));
    683  1.25     skrll 	}
    684  1.20     skrll 	/*
    685  1.20     skrll 	 * Fill this PLT entry and return.
    686  1.20     skrll 	 */
    687  1.20     skrll 	if (where[0] != func_pc)
    688  1.20     skrll 		where[0] = func_pc;
    689  1.20     skrll 	if (where[1] != func_sl)
    690  1.20     skrll 		where[1] = func_sl;
    691  1.20     skrll 
    692  1.25     skrll 	if (tp)
    693  1.25     skrll 		*tp = (Elf_Addr)where;
    694  1.25     skrll 
    695  1.25     skrll 	return 0;
    696  1.25     skrll }
    697  1.25     skrll 
    698  1.25     skrll caddr_t
    699  1.25     skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    700  1.25     skrll {
    701  1.28       mjf 	const Elf_Rela *rela;
    702  1.32     skrll 	Elf_Addr new_value = 0;	/* XXX gcc */
    703  1.25     skrll 	int err;
    704  1.25     skrll 
    705  1.28       mjf 	rela = (const Elf_Rela *)((const char *)obj->pltrela + reloff);
    706  1.28       mjf 
    707  1.25     skrll 	assert(ELF_R_SYM(rela->r_info) != 0);
    708  1.25     skrll 
    709  1.39     joerg 	_rtld_shared_enter();
    710  1.25     skrll 	err = _rtld_relocate_plt_object(obj, rela, &new_value);
    711  1.31  christos 	if (err)
    712  1.25     skrll 		_rtld_die();
    713  1.39     joerg 	_rtld_shared_exit();
    714  1.25     skrll 
    715  1.25     skrll 	return (caddr_t)new_value;
    716  1.20     skrll }
    717  1.20     skrll 
    718  1.20     skrll int
    719  1.20     skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
    720  1.20     skrll {
    721  1.25     skrll 	const Elf_Rela *rela = obj->pltrela;
    722  1.20     skrll 
    723  1.25     skrll 	for (; rela < obj->pltrelalim; rela++) {
    724  1.25     skrll 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    725  1.25     skrll 			return -1;
    726  1.20     skrll 	}
    727  1.20     skrll 	return 0;
    728  1.20     skrll }
    729  1.43     joerg 
    730  1.43     joerg void
    731  1.43     joerg _rtld_call_function_void(const Obj_Entry *obj, Elf_Addr ptr)
    732  1.43     joerg {
    733  1.43     joerg 	volatile hppa_plabel plabel;
    734  1.43     joerg 	void (*f)(void);
    735  1.43     joerg 
    736  1.43     joerg 	plabel.hppa_plabel_pc = (Elf_Addr)ptr;
    737  1.43     joerg 	plabel.hppa_plabel_sl = (Elf_Addr)(obj->pltgot);
    738  1.43     joerg 	f = (void (*)(void))RTLD_MAKE_PLABEL(&plabel);
    739  1.43     joerg 
    740  1.43     joerg 	f();
    741  1.43     joerg }
    742  1.43     joerg 
    743  1.43     joerg Elf_Addr
    744  1.43     joerg _rtld_call_function_addr(const Obj_Entry *obj, Elf_Addr ptr)
    745  1.43     joerg {
    746  1.43     joerg 	volatile hppa_plabel plabel;
    747  1.43     joerg 	Elf_Addr (*f)(void);
    748  1.43     joerg 
    749  1.43     joerg 	plabel.hppa_plabel_pc = (Elf_Addr)ptr;
    750  1.43     joerg 	plabel.hppa_plabel_sl = (Elf_Addr)(obj->pltgot);
    751  1.43     joerg 	f = (Elf_Addr (*)(void))RTLD_MAKE_PLABEL(&plabel);
    752  1.43     joerg 
    753  1.43     joerg 	return f();
    754  1.43     joerg }
    755