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