Home | History | Annotate | Line # | Download | only in hppa
hppa_reloc.c revision 1.23
      1  1.23     skrll /*	$NetBSD: hppa_reloc.c,v 1.23 2005/08/20 19:01:17 skrll 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  * 3. All advertising materials mentioning features or use of this software
     19   1.1  fredette  *    must display the following acknowledgement:
     20   1.1  fredette  *        This product includes software developed by the NetBSD
     21   1.1  fredette  *        Foundation, Inc. and its contributors.
     22   1.1  fredette  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1  fredette  *    contributors may be used to endorse or promote products derived
     24   1.1  fredette  *    from this software without specific prior written permission.
     25   1.1  fredette  *
     26   1.1  fredette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1  fredette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1  fredette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1  fredette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1  fredette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1  fredette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1  fredette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1  fredette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1  fredette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1  fredette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1  fredette  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1  fredette  */
     38   1.1  fredette 
     39  1.23     skrll #include <sys/cdefs.h>
     40  1.23     skrll #ifndef lint
     41  1.23     skrll __RCSID("$NetBSD: hppa_reloc.c,v 1.23 2005/08/20 19:01:17 skrll Exp $");
     42  1.23     skrll #endif /* not lint */
     43  1.23     skrll 
     44   1.1  fredette #include <stdlib.h>
     45   1.1  fredette #include <sys/types.h>
     46   1.1  fredette #include <sys/stat.h>
     47   1.1  fredette #include <sys/queue.h>
     48   1.1  fredette 
     49   1.1  fredette #include "rtld.h"
     50   1.1  fredette #include "debug.h"
     51   1.1  fredette 
     52   1.1  fredette #ifdef RTLD_DEBUG_HPPA
     53  1.14   mycroft #define	hdbg(x)		xprintf x
     54   1.1  fredette #else
     55   1.1  fredette #define	hdbg(x)		/* nothing */
     56   1.1  fredette #endif
     57  1.12   mycroft 
     58  1.20     skrll caddr_t _rtld_bind(const Obj_Entry *, const Elf_Addr);
     59  1.12   mycroft void _rtld_bind_start(void);
     60  1.13   mycroft void __rtld_setup_hppa_pltgot(const Obj_Entry *, Elf_Addr *);
     61   1.1  fredette 
     62   1.1  fredette /*
     63   1.1  fredette  * In the runtime architecture (ABI), PLABEL function
     64   1.1  fredette  * pointers are distinguished from normal function
     65   1.1  fredette  * pointers by having the next-least-significant bit
     66   1.1  fredette  * set.  (This bit is referred to as the L field in
     67   1.1  fredette  * HP documentation).  The $$dyncall millicode is
     68   1.1  fredette  * aware of this.
     69   1.1  fredette  */
     70   1.1  fredette #define	RTLD_MAKE_PLABEL(plabel)	(((Elf_Addr)(plabel)) | (1 << 1))
     71   1.1  fredette #define RTLD_IS_PLABEL(addr)		(((Elf_Addr)(addr)) & (1 << 1))
     72   1.1  fredette #define	RTLD_GET_PLABEL(addr)	((hppa_plabel *) (((Elf_Addr)addr) & ~3))
     73   1.1  fredette 
     74   1.1  fredette /*
     75   1.1  fredette  * This is the PLABEL structure.  The function PC and
     76   1.1  fredette  * shared linkage members must come first, as they are
     77   1.1  fredette  * the actual PLABEL.
     78   1.1  fredette  */
     79   1.1  fredette typedef struct _hppa_plabel {
     80   1.1  fredette 	Elf_Addr	hppa_plabel_pc;
     81   1.1  fredette 	Elf_Addr	hppa_plabel_sl;
     82   1.1  fredette 	SLIST_ENTRY(_hppa_plabel)	hppa_plabel_next;
     83   1.1  fredette } hppa_plabel;
     84   1.1  fredette 
     85   1.1  fredette /*
     86   1.1  fredette  * For now allocated PLABEL structures are tracked on a
     87   1.1  fredette  * singly linked list.  This maybe should be revisited.
     88   1.1  fredette  */
     89   1.1  fredette static SLIST_HEAD(hppa_plabel_head, _hppa_plabel) hppa_plabel_list
     90   1.1  fredette     = SLIST_HEAD_INITIALIZER(hppa_plabel_list);
     91   1.1  fredette 
     92   1.1  fredette /*
     93   1.1  fredette  * Because I'm hesitant to use NEW while relocating self,
     94   1.1  fredette  * this is a small pool of preallocated PLABELs.
     95   1.1  fredette  */
     96  1.20     skrll #define	HPPA_PLABEL_PRE	(12)
     97   1.1  fredette static hppa_plabel hppa_plabel_pre[HPPA_PLABEL_PRE];
     98   1.1  fredette static int hppa_plabel_pre_next = 0;
     99   1.1  fredette 
    100  1.20     skrll void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
    101  1.20     skrll int _rtld_relocate_plt_objects(const Obj_Entry *);
    102   1.1  fredette /*
    103   1.1  fredette  * This bootstraps the dynamic linker by relocating its GOT.
    104   1.1  fredette  * On the hppa, unlike on other architectures, static strings
    105   1.1  fredette  * are found through the GOT.  Static strings are essential
    106   1.1  fredette  * for RTLD_DEBUG, and I suspect they're used early even when
    107   1.1  fredette  * !defined(RTLD_DEBUG), making relocating the GOT essential.
    108   1.1  fredette  *
    109   1.1  fredette  * It gets worse.  Relocating the GOT doesn't mean just walking
    110   1.1  fredette  * it and adding the relocbase to all of the entries.  You must
    111   1.1  fredette  * find and use the GOT relocations, since those RELA relocations
    112   1.1  fredette  * have the necessary addends - the GOT comes initialized as
    113   1.1  fredette  * zeroes.
    114   1.1  fredette  */
    115   1.1  fredette void
    116  1.20     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    117   1.1  fredette {
    118   1.1  fredette 	const Elf_Rela	*relafirst, *rela, *relalim;
    119  1.22       chs 	Elf_Addr        relasz;
    120   1.1  fredette 	Elf_Addr	where;
    121  1.20     skrll 	Elf_Addr	*pltgot;
    122  1.20     skrll 	const Elf_Rela	*plabel_relocs[HPPA_PLABEL_PRE];
    123  1.20     skrll 	int		nplabel_relocs = 0;
    124  1.20     skrll 	int		i;
    125  1.20     skrll 	const Elf_Sym	*symtab, *sym;
    126  1.20     skrll 	unsigned long	symnum;
    127  1.20     skrll 	hppa_plabel	*plabel;
    128   1.1  fredette 
    129  1.22       chs 	/*
    130  1.22       chs 	 * Process the DYNAMIC section, looking for the non-PLT relocations.
    131   1.1  fredette 	 */
    132   1.1  fredette 	relafirst = NULL;
    133  1.22       chs 	relasz = 0;
    134  1.22       chs 	symtab = NULL;
    135  1.22       chs 	pltgot = NULL;
    136   1.1  fredette 	for (; dynp->d_tag != DT_NULL; ++dynp) {
    137   1.1  fredette 		switch (dynp->d_tag) {
    138   1.1  fredette 
    139   1.1  fredette 		case DT_RELA:
    140   1.1  fredette 			relafirst = (const Elf_Rela *)
    141   1.1  fredette 			    (relocbase + dynp->d_un.d_ptr);
    142   1.1  fredette 			break;
    143   1.1  fredette 
    144   1.1  fredette 		case DT_RELASZ:
    145   1.1  fredette 			relasz = dynp->d_un.d_val;
    146   1.1  fredette 			break;
    147  1.20     skrll 
    148  1.20     skrll 		case DT_SYMTAB:
    149  1.20     skrll 			symtab = (const Elf_Sym *)
    150  1.20     skrll 			    (relocbase + dynp->d_un.d_ptr);
    151  1.20     skrll 			break;
    152  1.20     skrll 
    153  1.20     skrll 		case DT_PLTGOT:
    154  1.20     skrll 			pltgot = (Elf_Addr *)
    155  1.20     skrll 			    (relocbase + dynp->d_un.d_ptr);
    156  1.20     skrll                         break;
    157   1.1  fredette 		}
    158   1.1  fredette 	}
    159   1.1  fredette 	relalim = (const Elf_Rela *)((caddr_t)relafirst + relasz);
    160   1.1  fredette 
    161  1.18     skrll 	for (rela = relafirst; rela < relalim; rela++) {
    162  1.20     skrll 		symnum = ELF_R_SYM(rela->r_info);
    163   1.1  fredette 		where = (Elf_Addr)(relocbase + rela->r_offset);
    164  1.20     skrll 
    165  1.20     skrll 		switch (ELF_R_TYPE(rela->r_info)) {
    166  1.20     skrll 		case R_TYPE(DIR32):
    167  1.20     skrll 			if (symnum == 0)
    168  1.20     skrll 				*((Elf_Addr *)where) = relocbase + rela->r_addend;
    169  1.20     skrll 			else {
    170  1.20     skrll 				sym = symtab + symnum;
    171  1.20     skrll 				*((Elf_Addr *)where) =
    172  1.20     skrll 				    relocbase + rela->r_addend + sym->st_value;
    173  1.20     skrll 			}
    174  1.20     skrll 			break;
    175  1.20     skrll 
    176  1.20     skrll 		case R_TYPE(PLABEL32):
    177  1.20     skrll 			/*
    178  1.20     skrll 			 * PLABEL32 relocation processing is done in two phases
    179  1.20     skrll 			 *
    180  1.20     skrll 			 *  i) local function relocations (symbol number == 0)
    181  1.20     skrll 			 *     can be resolved immediately.
    182  1.20     skrll 			 *
    183  1.20     skrll 			 * ii) external function relocations are deferred until
    184  1.20     skrll 			 *     we finish all other relocations so that global
    185  1.20     skrll 			 *     data isn't accessed until all other non-PLT
    186  1.20     skrll 			 *     relocations have been done.
    187  1.20     skrll 			 */
    188  1.20     skrll 			if (symnum == 0)
    189  1.20     skrll 				*((Elf_Addr *)where) =
    190  1.20     skrll 				    relocbase + rela->r_addend;
    191  1.20     skrll 			else
    192  1.20     skrll 				plabel_relocs[nplabel_relocs++] = rela;
    193  1.20     skrll 			break;
    194  1.20     skrll 
    195  1.20     skrll 		default:
    196  1.20     skrll 			break;
    197  1.20     skrll 		}
    198   1.1  fredette 	}
    199   1.1  fredette 
    200  1.20     skrll 	assert(nplabel_relocs < HPPA_PLABEL_PRE);
    201  1.20     skrll 	for (i = 0; i < nplabel_relocs; i++) {
    202  1.20     skrll 		rela = plabel_relocs[i];
    203  1.20     skrll 		where = (Elf_Addr)(relocbase + rela->r_offset);
    204  1.20     skrll 		sym = symtab + ELF_R_SYM(rela->r_info);
    205  1.20     skrll 
    206  1.20     skrll 		plabel = &hppa_plabel_pre[hppa_plabel_pre_next++];
    207  1.20     skrll 
    208  1.20     skrll         	plabel->hppa_plabel_pc = (Elf_Addr)
    209  1.20     skrll 		    (relocbase + sym->st_value + rela->r_addend);
    210  1.20     skrll         	plabel->hppa_plabel_sl = (Elf_Addr)pltgot;
    211  1.20     skrll 
    212  1.20     skrll         	SLIST_INSERT_HEAD(&hppa_plabel_list, plabel, hppa_plabel_next);
    213  1.20     skrll 		*((Elf_Addr *)where) = (Elf_Addr)(RTLD_MAKE_PLABEL(plabel));
    214  1.20     skrll 	}
    215  1.20     skrll 
    216   1.1  fredette #if defined(RTLD_DEBUG_HPPA)
    217  1.18     skrll 	for (rela = relafirst; rela < relalim; rela++) {
    218   1.1  fredette 		where = (Elf_Addr)(relocbase + rela->r_offset);
    219  1.20     skrll 
    220  1.20     skrll 		switch (ELF_R_TYPE(rela->r_info)) {
    221  1.20     skrll 		case R_TYPE(DIR32):
    222  1.20     skrll 			hdbg(("DIR32 rela @%p(%p) -> %p(%p)\n",
    223   1.1  fredette 			    (void *)rela->r_offset,
    224   1.1  fredette 			    (void *)where,
    225   1.1  fredette 			    (void *)rela->r_addend,
    226  1.20     skrll 			    (void *)*((Elf_Addr *)where) ));
    227  1.20     skrll 			break;
    228  1.20     skrll 
    229  1.20     skrll 		case R_TYPE(PLABEL32):
    230  1.20     skrll 			symnum = ELF_R_SYM(rela->r_info);
    231  1.20     skrll 			if (symnum == 0) {
    232  1.20     skrll 				hdbg(("PLABEL rela @%p(%p) -> %p(%p)\n",
    233  1.20     skrll 		    		    (void *)rela->r_offset,
    234  1.20     skrll 		    		    (void *)where,
    235  1.20     skrll 		    		    (void *)rela->r_addend,
    236  1.20     skrll 		    		    (void *)*((Elf_Addr *)where) ));
    237  1.20     skrll 			} else {
    238  1.20     skrll 				sym = symtab + symnum;
    239  1.20     skrll 
    240  1.20     skrll 				hdbg(("PLABEL32 rela @%p(%p), symnum=%ld(%p) -> %p(%p)\n",
    241  1.20     skrll 			    	    (void *)rela->r_offset,
    242  1.20     skrll 				    (void *)where,
    243  1.20     skrll 				    symnum,
    244  1.20     skrll 				    (void *)sym->st_value,
    245  1.20     skrll 			    	    (void *)rela->r_addend,
    246  1.20     skrll 				    (void *)*((Elf_Addr *)where) ));
    247  1.20     skrll 			}
    248  1.20     skrll 			break;
    249  1.20     skrll 		default:
    250  1.20     skrll 			hdbg(("rela XXX reloc\n"));
    251  1.20     skrll 			break;
    252  1.20     skrll 		}
    253   1.1  fredette 	}
    254   1.1  fredette #endif /* RTLD_DEBUG_HPPA */
    255   1.1  fredette }
    256   1.1  fredette 
    257   1.1  fredette /*
    258   1.1  fredette  * This allocates a PLABEL.  If called with a non-NULL def, the
    259   1.1  fredette  * plabel is for the function associated with that definition
    260   1.1  fredette  * in the defining object defobj, plus the given addend.  If
    261   1.1  fredette  * called with a NULL def, the plabel is for the function at
    262   1.1  fredette  * the (unrelocated) address in addend in the object defobj.
    263   1.1  fredette  */
    264   1.1  fredette Elf_Addr
    265   1.1  fredette _rtld_function_descriptor_alloc(const Obj_Entry *defobj, const Elf_Sym *def,
    266   1.1  fredette     Elf_Addr addend)
    267   1.1  fredette {
    268   1.1  fredette 	Elf_Addr	func_pc, func_sl;
    269   1.1  fredette 	hppa_plabel	*plabel;
    270   1.1  fredette 
    271   1.1  fredette 	if (def != NULL) {
    272   1.1  fredette 
    273   1.1  fredette 		/*
    274   1.1  fredette 		 * We assume that symbols of type STT_NOTYPE
    275   1.1  fredette 		 * are undefined.  Return NULL for these.
    276   1.1  fredette 		 */
    277   1.1  fredette 		if (ELF_ST_TYPE(def->st_info) == STT_NOTYPE)
    278   1.1  fredette 			return (Elf_Addr)NULL;
    279   1.1  fredette 
    280   1.1  fredette 		/* Otherwise assert that this symbol must be a function. */
    281   1.1  fredette 		assert(ELF_ST_TYPE(def->st_info) == STT_FUNC);
    282   1.1  fredette 
    283   1.1  fredette 		func_pc = (Elf_Addr)(defobj->relocbase + def->st_value +
    284   1.1  fredette 		    addend);
    285   1.1  fredette 	} else
    286   1.1  fredette 		func_pc = (Elf_Addr)(defobj->relocbase + addend);
    287   1.1  fredette 
    288   1.1  fredette 	/*
    289   1.1  fredette 	 * Search the existing PLABELs for one matching
    290   1.1  fredette 	 * this function.  If there is one, return it.
    291   1.1  fredette 	 */
    292  1.20     skrll 	func_sl = (Elf_Addr)(defobj->pltgot);
    293   1.1  fredette 	SLIST_FOREACH(plabel, &hppa_plabel_list, hppa_plabel_next)
    294   1.1  fredette 		if (plabel->hppa_plabel_pc == func_pc &&
    295   1.1  fredette 		    plabel->hppa_plabel_sl == func_sl)
    296   1.1  fredette 			return RTLD_MAKE_PLABEL(plabel);
    297   1.1  fredette 
    298   1.1  fredette 	/*
    299   1.1  fredette 	 * Once we've used up the preallocated set, we start
    300   1.1  fredette 	 * using NEW to allocate plabels.
    301   1.1  fredette 	 */
    302   1.1  fredette 	if (hppa_plabel_pre_next < HPPA_PLABEL_PRE)
    303   1.1  fredette 		plabel = &hppa_plabel_pre[hppa_plabel_pre_next++];
    304   1.1  fredette 	else {
    305   1.1  fredette 		plabel = NEW(hppa_plabel);
    306   1.1  fredette 		if (plabel == NULL)
    307   1.1  fredette 			return (Elf_Addr)-1;
    308   1.1  fredette 	}
    309   1.1  fredette 
    310   1.1  fredette 	/* Fill the new entry and insert it on the list. */
    311   1.1  fredette 	plabel->hppa_plabel_pc = func_pc;
    312   1.1  fredette 	plabel->hppa_plabel_sl = func_sl;
    313   1.1  fredette 	SLIST_INSERT_HEAD(&hppa_plabel_list, plabel, hppa_plabel_next);
    314   1.1  fredette 
    315   1.1  fredette 	return RTLD_MAKE_PLABEL(plabel);
    316   1.1  fredette }
    317   1.1  fredette 
    318   1.1  fredette /*
    319   1.1  fredette  * If a pointer is a PLABEL, this unwraps it.
    320   1.1  fredette  */
    321   1.1  fredette const void *
    322   1.1  fredette _rtld_function_descriptor_function(const void *addr)
    323   1.1  fredette {
    324   1.1  fredette 	return (RTLD_IS_PLABEL(addr) ?
    325   1.1  fredette 	    (const void *) RTLD_GET_PLABEL(addr)->hppa_plabel_pc :
    326   1.1  fredette 	    addr);
    327   1.1  fredette }
    328   1.1  fredette 
    329   1.2   mycroft /* This sets up an object's GOT. */
    330   1.2   mycroft void
    331   1.2   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
    332   1.2   mycroft {
    333  1.20     skrll 	__rtld_setup_hppa_pltgot(obj, obj->pltgot);
    334   1.4   mycroft }
    335   1.4   mycroft 
    336   1.4   mycroft int
    337  1.16     skrll _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
    338   1.4   mycroft {
    339   1.5   mycroft 	const Elf_Rela *rela;
    340   1.5   mycroft 
    341   1.5   mycroft 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    342   1.5   mycroft 		Elf_Addr        *where;
    343   1.5   mycroft 		const Elf_Sym   *def;
    344   1.5   mycroft 		const Obj_Entry *defobj;
    345   1.5   mycroft 		Elf_Addr         tmp;
    346   1.6   mycroft 		unsigned long	 symnum;
    347   1.5   mycroft 
    348   1.5   mycroft 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    349   1.6   mycroft 		symnum = ELF_R_SYM(rela->r_info);
    350   1.5   mycroft 
    351   1.5   mycroft 		switch (ELF_R_TYPE(rela->r_info)) {
    352   1.5   mycroft 		case R_TYPE(NONE):
    353   1.5   mycroft 			break;
    354   1.4   mycroft 
    355   1.5   mycroft 		case R_TYPE(DIR32):
    356   1.6   mycroft 			if (symnum) {
    357   1.5   mycroft 				/*
    358   1.5   mycroft 				 * This is either a DIR32 against a symbol
    359   1.5   mycroft 				 * (def->st_name != 0), or against a local
    360   1.5   mycroft 				 * section (def->st_name == 0).
    361   1.5   mycroft 				 */
    362   1.6   mycroft 				def = obj->symtab + symnum;
    363   1.5   mycroft 				defobj = obj;
    364   1.5   mycroft 				if (def->st_name != 0)
    365   1.6   mycroft 					def = _rtld_find_symdef(symnum, obj,
    366   1.6   mycroft 					    &defobj, false);
    367   1.5   mycroft 				if (def == NULL)
    368   1.5   mycroft 					return -1;
    369   1.4   mycroft 
    370   1.5   mycroft 				tmp = (Elf_Addr)(defobj->relocbase +
    371   1.5   mycroft 				    def->st_value + rela->r_addend);
    372   1.4   mycroft 
    373   1.5   mycroft 				if (*where != tmp)
    374   1.5   mycroft 					*where = tmp;
    375  1.14   mycroft 				rdbg(("DIR32 %s in %s --> %p in %s",
    376   1.7   mycroft 				    obj->strtab + obj->symtab[symnum].st_name,
    377   1.7   mycroft 				    obj->path, (void *)*where, defobj->path));
    378   1.5   mycroft 			} else {
    379   1.5   mycroft 				tmp = (Elf_Addr)(obj->relocbase +
    380   1.5   mycroft 				    rela->r_addend);
    381   1.5   mycroft 
    382  1.20     skrll 				if (*where != tmp)
    383  1.20     skrll 					*where = tmp;
    384  1.20     skrll 				rdbg(("DIR32 in %s --> %p", obj->path,
    385  1.14   mycroft 					    (void *)*where));
    386   1.5   mycroft 			}
    387   1.5   mycroft 			break;
    388   1.5   mycroft 
    389   1.5   mycroft 		case R_TYPE(PLABEL32):
    390   1.6   mycroft 			if (symnum) {
    391   1.6   mycroft 				def = _rtld_find_symdef(symnum, obj, &defobj,
    392   1.6   mycroft 				    false);
    393   1.5   mycroft 				if (def == NULL)
    394   1.5   mycroft 					return -1;
    395   1.4   mycroft 
    396  1.19     skrll 				tmp = _rtld_function_descriptor_alloc(defobj,
    397  1.19     skrll 				    def, rela->r_addend);
    398   1.5   mycroft 				if (tmp == (Elf_Addr)-1)
    399   1.5   mycroft 					return -1;
    400   1.4   mycroft 
    401   1.4   mycroft 				if (*where != tmp)
    402   1.4   mycroft 					*where = tmp;
    403  1.14   mycroft 				rdbg(("PLABEL32 %s in %s --> %p in %s",
    404   1.7   mycroft 				    obj->strtab + obj->symtab[symnum].st_name,
    405   1.7   mycroft 				    obj->path, (void *)*where, defobj->path));
    406   1.5   mycroft 			} else {
    407   1.5   mycroft 				/*
    408   1.5   mycroft 				 * This is a PLABEL for a static function, and
    409   1.5   mycroft 				 * the dynamic linker has both allocated a PLT
    410   1.5   mycroft 				 * entry for this function and told us where it
    411   1.5   mycroft 				 * is.  We can safely use the PLT entry as the
    412   1.5   mycroft 				 * PLABEL because there should be no other
    413   1.5   mycroft 				 * PLABEL reloc referencing this function.
    414   1.5   mycroft 				 * This object should also have an IPLT
    415   1.5   mycroft 				 * relocation to initialize the PLT entry.
    416   1.5   mycroft 				 *
    417   1.5   mycroft 				 * The dynamic linker should also have ensured
    418   1.5   mycroft 				 * that the addend has the
    419   1.5   mycroft 				 * next-least-significant bit set; the
    420   1.5   mycroft 				 * $$dyncall millicode uses this to distinguish
    421   1.5   mycroft 				 * a PLABEL pointer from a plain function
    422   1.5   mycroft 				 * pointer.
    423   1.5   mycroft 				 */
    424  1.19     skrll 				tmp = (Elf_Addr)
    425  1.19     skrll 				    (obj->relocbase + rela->r_addend);
    426   1.5   mycroft 
    427   1.5   mycroft 				if (*where != tmp)
    428   1.5   mycroft 					*where = tmp;
    429  1.14   mycroft 				rdbg(("PLABEL32 in %s --> %p", obj->path,
    430  1.14   mycroft 				    (void *)*where));
    431   1.5   mycroft 			}
    432   1.5   mycroft 			break;
    433   1.4   mycroft 
    434   1.5   mycroft 		case R_TYPE(COPY):
    435   1.4   mycroft 			/*
    436   1.5   mycroft 			 * These are deferred until all other relocations have
    437   1.5   mycroft 			 * been done.  All we do here is make sure that the
    438   1.5   mycroft 			 * COPY relocation is not in a shared library.  They
    439   1.5   mycroft 			 * are allowed only in executable files.
    440   1.4   mycroft 			 */
    441  1.10   mycroft 			if (obj->isdynamic) {
    442   1.5   mycroft 				_rtld_error(
    443   1.5   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    444   1.5   mycroft 				    obj->path);
    445   1.4   mycroft 				return -1;
    446   1.5   mycroft 			}
    447  1.14   mycroft 			rdbg(("COPY (avoid in main)"));
    448   1.5   mycroft 			break;
    449   1.4   mycroft 
    450   1.5   mycroft 		default:
    451  1.14   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    452   1.5   mycroft 			    "addend = %p, contents = %p, symbol = %s",
    453   1.6   mycroft 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    454   1.5   mycroft 			    (void *)rela->r_offset, (void *)rela->r_addend,
    455   1.5   mycroft 			    (void *)*where,
    456   1.6   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    457   1.5   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    458   1.5   mycroft 			    "in non-PLT relocations\n",
    459   1.5   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    460   1.4   mycroft 			return -1;
    461   1.4   mycroft 		}
    462   1.8   mycroft 	}
    463   1.8   mycroft 	return 0;
    464   1.8   mycroft }
    465   1.8   mycroft 
    466   1.8   mycroft int
    467  1.16     skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    468   1.8   mycroft {
    469   1.8   mycroft 	const Elf_Rela *rela;
    470   1.8   mycroft 
    471   1.8   mycroft 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    472   1.8   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    473   1.8   mycroft 		Elf_Addr func_pc, func_sl;
    474   1.8   mycroft 
    475   1.8   mycroft 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(IPLT));
    476   1.8   mycroft 
    477   1.8   mycroft 		/*
    478   1.8   mycroft 		 * If this is an IPLT reloc for a static function,
    479   1.8   mycroft 		 * fully resolve the PLT entry now.
    480   1.8   mycroft 		 */
    481   1.8   mycroft 		if (ELF_R_SYM(rela->r_info) == 0) {
    482   1.8   mycroft 			func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
    483  1.20     skrll 			func_sl = (Elf_Addr)(obj->pltgot);
    484   1.8   mycroft 		}
    485   1.8   mycroft 
    486   1.8   mycroft 		/*
    487   1.8   mycroft 		 * Otherwise set up for lazy binding.
    488   1.8   mycroft 		 */
    489   1.8   mycroft 		else {
    490   1.8   mycroft 			/*
    491   1.8   mycroft 			 * This function pointer points to the PLT
    492   1.8   mycroft 			 * stub added by the linker, and instead of
    493   1.8   mycroft 			 * a shared linkage value, we stash this
    494   1.8   mycroft 			 * relocation's offset.  The PLT stub has
    495   1.8   mycroft 			 * already been set up to transfer to
    496   1.8   mycroft 			 * _rtld_bind_start.
    497   1.8   mycroft 			 */
    498  1.20     skrll 			func_pc = ((Elf_Addr)(obj->pltgot)) - 16;
    499  1.19     skrll 			func_sl = (Elf_Addr)
    500  1.20     skrll 			    ((caddr_t)rela - (caddr_t)(obj->pltrela));
    501   1.8   mycroft 		}
    502  1.20     skrll 		rdbg(("lazy bind %s(%p) --> old=(%p,%p) new=(%p,%p)",
    503  1.20     skrll 		    obj->path,
    504  1.20     skrll 		    (void *)where,
    505  1.20     skrll 		    (void *)where[0], (void *)where[1],
    506  1.20     skrll 		    (void *)func_pc, (void *)func_sl));
    507   1.8   mycroft 
    508   1.8   mycroft 		/*
    509   1.8   mycroft 		 * Fill this PLT entry and return.
    510   1.8   mycroft 		 */
    511   1.8   mycroft 		where[0] = func_pc;
    512   1.8   mycroft 		where[1] = func_sl;
    513   1.4   mycroft 	}
    514   1.4   mycroft 	return 0;
    515   1.1  fredette }
    516  1.20     skrll 
    517  1.20     skrll caddr_t
    518  1.20     skrll _rtld_bind(const Obj_Entry *obj, Elf_Addr reloff)
    519  1.20     skrll {
    520  1.20     skrll 	const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
    521  1.20     skrll 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    522  1.20     skrll 	const Elf_Sym *def;
    523  1.20     skrll 	const Obj_Entry *defobj;
    524  1.20     skrll 	Elf_Addr	func_pc, func_sl;
    525  1.20     skrll 
    526  1.20     skrll 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(IPLT));
    527  1.20     skrll 	assert(ELF_R_SYM(rela->r_info) != 0);
    528  1.20     skrll 
    529  1.20     skrll 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    530  1.20     skrll 	if (def == NULL)
    531  1.20     skrll 		_rtld_die();
    532  1.20     skrll 
    533  1.20     skrll 	func_pc = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
    534  1.20     skrll 	func_sl = (Elf_Addr)(defobj->pltgot);
    535  1.20     skrll 
    536  1.20     skrll 	rdbg(("bind now/fixup in %s --> old=(%p,%p) new=(%p,%p)",
    537  1.20     skrll 	    defobj->strtab + def->st_name,
    538  1.20     skrll 	    (void *)where[0], (void *)where[1],
    539  1.20     skrll 	    (void *)func_pc, (void *)func_sl));
    540  1.20     skrll 	/*
    541  1.20     skrll 	 * Fill this PLT entry and return.
    542  1.20     skrll 	 */
    543  1.20     skrll 	if (where[0] != func_pc)
    544  1.20     skrll 		where[0] = func_pc;
    545  1.20     skrll 	if (where[1] != func_sl)
    546  1.20     skrll 		where[1] = func_sl;
    547  1.20     skrll 
    548  1.20     skrll 	return (caddr_t)where;
    549  1.20     skrll }
    550  1.20     skrll 
    551  1.20     skrll int
    552  1.20     skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
    553  1.20     skrll {
    554  1.20     skrll 	const Elf_Rela *rela;
    555  1.20     skrll 
    556  1.20     skrll 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    557  1.20     skrll 		Elf_Addr	*where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    558  1.20     skrll 		const Elf_Sym	*def;
    559  1.20     skrll 		const Obj_Entry	*defobj;
    560  1.20     skrll 		Elf_Addr	func_pc, func_sl;
    561  1.20     skrll 
    562  1.20     skrll 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(IPLT));
    563  1.20     skrll 
    564  1.20     skrll 		if (ELF_R_SYM(rela->r_info) == 0) {
    565  1.20     skrll 			func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
    566  1.20     skrll 			func_sl = (Elf_Addr)(obj->pltgot);
    567  1.20     skrll 		} else {
    568  1.20     skrll 			def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
    569  1.20     skrll 			    true);
    570  1.20     skrll 			if (def == NULL)
    571  1.20     skrll 				return -1;
    572  1.20     skrll 			func_pc = (Elf_Addr)(defobj->relocbase + def->st_value +
    573  1.20     skrll 			    rela->r_addend);
    574  1.20     skrll 			func_sl = (Elf_Addr)(defobj->pltgot);
    575  1.20     skrll 			rdbg(("bind now/fixup in %s --> old=(%p,%p) new=(%p,%p)",
    576  1.20     skrll 			    defobj->strtab + def->st_name,
    577  1.20     skrll 			    (void *)where[0], (void *)where[1],
    578  1.20     skrll 			    (void *)func_pc, (void *)func_sl));
    579  1.20     skrll 		}
    580  1.20     skrll 
    581  1.20     skrll 		/*
    582  1.20     skrll 		 * Fill this PLT entry and return.
    583  1.20     skrll 		 */
    584  1.20     skrll 		if (where[0] != func_pc)
    585  1.20     skrll 			where[0] = func_pc;
    586  1.20     skrll 		if (where[1] != func_sl)
    587  1.20     skrll 			where[1] = func_sl;
    588  1.20     skrll 	}
    589  1.20     skrll 	return 0;
    590  1.20     skrll }
    591