Home | History | Annotate | Line # | Download | only in hppa
hppa_reloc.c revision 1.14
      1  1.14   mycroft /*	$NetBSD: hppa_reloc.c,v 1.14 2002/09/12 22:56:29 mycroft Exp $	*/
      2   1.1  fredette 
      3   1.1  fredette /*-
      4   1.1  fredette  * Copyright (c) 2002 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.1  fredette  * by Matt Fredette.
      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.1  fredette #include <stdlib.h>
     40   1.1  fredette #include <sys/types.h>
     41   1.1  fredette #include <sys/stat.h>
     42   1.1  fredette #include <sys/queue.h>
     43   1.1  fredette 
     44   1.1  fredette #include "rtld.h"
     45   1.1  fredette #include "debug.h"
     46   1.1  fredette 
     47   1.1  fredette #ifdef RTLD_DEBUG_HPPA
     48  1.14   mycroft #define	hdbg(x)		xprintf x
     49   1.1  fredette #else
     50   1.1  fredette #define	hdbg(x)		/* nothing */
     51   1.1  fredette #endif
     52  1.12   mycroft 
     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.1  fredette  * In the runtime architecture (ABI), PLABEL function
     58   1.1  fredette  * pointers are distinguished from normal function
     59   1.1  fredette  * pointers by having the next-least-significant bit
     60   1.1  fredette  * set.  (This bit is referred to as the L field in
     61   1.1  fredette  * HP documentation).  The $$dyncall millicode is
     62   1.1  fredette  * aware of this.
     63   1.1  fredette  */
     64   1.1  fredette #define	RTLD_MAKE_PLABEL(plabel)	(((Elf_Addr)(plabel)) | (1 << 1))
     65   1.1  fredette #define RTLD_IS_PLABEL(addr)		(((Elf_Addr)(addr)) & (1 << 1))
     66   1.1  fredette #define	RTLD_GET_PLABEL(addr)	((hppa_plabel *) (((Elf_Addr)addr) & ~3))
     67   1.1  fredette 
     68   1.1  fredette /*
     69   1.1  fredette  * This is the PLABEL structure.  The function PC and
     70   1.1  fredette  * shared linkage members must come first, as they are
     71   1.1  fredette  * the actual PLABEL.
     72   1.1  fredette  */
     73   1.1  fredette typedef struct _hppa_plabel {
     74   1.1  fredette 	Elf_Addr	hppa_plabel_pc;
     75   1.1  fredette 	Elf_Addr	hppa_plabel_sl;
     76   1.1  fredette 	SLIST_ENTRY(_hppa_plabel)	hppa_plabel_next;
     77   1.1  fredette } hppa_plabel;
     78   1.1  fredette 
     79   1.1  fredette /*
     80   1.1  fredette  * For now allocated PLABEL structures are tracked on a
     81   1.1  fredette  * singly linked list.  This maybe should be revisited.
     82   1.1  fredette  */
     83   1.1  fredette static SLIST_HEAD(hppa_plabel_head, _hppa_plabel) hppa_plabel_list
     84   1.1  fredette     = SLIST_HEAD_INITIALIZER(hppa_plabel_list);
     85   1.1  fredette 
     86   1.1  fredette /*
     87   1.1  fredette  * Because I'm hesitant to use NEW while relocating self,
     88   1.1  fredette  * this is a small pool of preallocated PLABELs.
     89   1.1  fredette  */
     90   1.1  fredette #define	HPPA_PLABEL_PRE	(10)
     91   1.1  fredette static hppa_plabel hppa_plabel_pre[HPPA_PLABEL_PRE];
     92   1.1  fredette static int hppa_plabel_pre_next = 0;
     93   1.1  fredette 
     94   1.1  fredette /*
     95   1.1  fredette  * The DT_PLTGOT _DYNAMIC entry always gives the linkage table
     96   1.1  fredette  * pointer for an object.  This is often, but not always, the
     97   1.1  fredette  * same as the object's value for _GLOBAL_OFFSET_TABLE_.  We
     98   1.1  fredette  * cache one object's GOT value, otherwise we look it up.
     99   1.1  fredette  * XXX it would be nice to be able to keep this in the Obj_Entry.
    100   1.1  fredette  */
    101   1.1  fredette static const Obj_Entry *hppa_got_cache_obj = NULL;
    102   1.1  fredette static Elf_Addr *hppa_got_cache_got;
    103   1.1  fredette #define HPPA_OBJ_SL(obj)	((obj)->pltgot)
    104   1.1  fredette #define	HPPA_OBJ_GOT(obj)	((obj) == hppa_got_cache_obj ?		\
    105   1.1  fredette 				  hppa_got_cache_got :			\
    106   1.1  fredette 				  _rtld_fill_hppa_got_cache(obj))
    107   1.1  fredette static Elf_Addr *_rtld_fill_hppa_got_cache __P((const Obj_Entry *));
    108   1.1  fredette 
    109   1.1  fredette /*
    110   1.1  fredette  * This bootstraps the dynamic linker by relocating its GOT.
    111   1.1  fredette  * On the hppa, unlike on other architectures, static strings
    112   1.1  fredette  * are found through the GOT.  Static strings are essential
    113   1.1  fredette  * for RTLD_DEBUG, and I suspect they're used early even when
    114   1.1  fredette  * !defined(RTLD_DEBUG), making relocating the GOT essential.
    115   1.1  fredette  *
    116   1.1  fredette  * It gets worse.  Relocating the GOT doesn't mean just walking
    117   1.1  fredette  * it and adding the relocbase to all of the entries.  You must
    118   1.1  fredette  * find and use the GOT relocations, since those RELA relocations
    119   1.1  fredette  * have the necessary addends - the GOT comes initialized as
    120   1.1  fredette  * zeroes.
    121   1.1  fredette  */
    122   1.1  fredette void
    123   1.1  fredette _rtld_bootstrap_hppa_got(Elf_Dyn *dynp, Elf_Addr relocbase,
    124   1.1  fredette     Elf_Addr got_begin, Elf_Addr got_end)
    125   1.1  fredette {
    126   1.1  fredette 	const Elf_Rela	*relafirst, *rela, *relalim;
    127   1.1  fredette 	Elf_Addr        relasz = 0;
    128   1.1  fredette 	Elf_Addr	where;
    129   1.1  fredette 
    130   1.1  fredette 	/*
    131   1.1  fredette 	 * Process the DYNAMIC section, looking for the non-PLT
    132   1.1  fredette 	 * relocations.
    133   1.1  fredette 	 */
    134   1.1  fredette 	relafirst = NULL;
    135   1.1  fredette 	for (; dynp->d_tag != DT_NULL; ++dynp) {
    136   1.1  fredette 		switch (dynp->d_tag) {
    137   1.1  fredette 
    138   1.1  fredette 		case DT_RELA:
    139   1.1  fredette 			relafirst = (const Elf_Rela *)
    140   1.1  fredette 			    (relocbase + dynp->d_un.d_ptr);
    141   1.1  fredette 			break;
    142   1.1  fredette 
    143   1.1  fredette 		case DT_RELASZ:
    144   1.1  fredette 			relasz = dynp->d_un.d_val;
    145   1.1  fredette 			break;
    146   1.1  fredette 		}
    147   1.1  fredette 	}
    148   1.1  fredette 	relalim = (const Elf_Rela *)((caddr_t)relafirst + relasz);
    149   1.1  fredette 
    150   1.1  fredette 	/*
    151   1.1  fredette 	 * Process all relocations that look like they're in
    152   1.1  fredette 	 * the GOT.
    153   1.1  fredette 	 */
    154   1.1  fredette 	for(rela = relafirst; rela < relalim; rela++) {
    155   1.1  fredette 		where = (Elf_Addr)(relocbase + rela->r_offset);
    156   1.1  fredette 		if (where >= got_begin && where < got_end)
    157   1.1  fredette 			*((Elf_Addr *)where) = relocbase + rela->r_addend;
    158   1.1  fredette 	}
    159   1.1  fredette 
    160   1.1  fredette #if defined(RTLD_DEBUG_HPPA)
    161   1.1  fredette 	for(rela = relafirst; rela < relalim; rela++) {
    162   1.1  fredette 		where = (Elf_Addr)(relocbase + rela->r_offset);
    163   1.1  fredette 		if (where >= got_begin && where < got_end)
    164   1.1  fredette 			xprintf("GOT rela @%p(%p) -> %p(%p)\n",
    165   1.1  fredette 			    (void *)rela->r_offset,
    166   1.1  fredette 			    (void *)where,
    167   1.1  fredette 			    (void *)rela->r_addend,
    168   1.1  fredette 			    (void *)*((Elf_Addr *)where));
    169   1.1  fredette 	}
    170   1.1  fredette #endif /* RTLD_DEBUG_HPPA */
    171   1.1  fredette }
    172   1.1  fredette 
    173   1.1  fredette /*
    174   1.1  fredette  * This looks up the object's _GLOBAL_OFFSET_TABLE_
    175   1.1  fredette  * and caches the result.
    176   1.1  fredette  */
    177   1.1  fredette static Elf_Addr *
    178   1.1  fredette _rtld_fill_hppa_got_cache(const Obj_Entry *obj)
    179   1.1  fredette {
    180   1.1  fredette 	const char *name = "_GLOBAL_OFFSET_TABLE_";
    181   1.1  fredette 	unsigned long hash;
    182   1.1  fredette 	const Elf_Sym *def;
    183   1.1  fredette 
    184   1.1  fredette 	hash = _rtld_elf_hash(name);
    185   1.1  fredette 	def = _rtld_symlook_obj(name, hash, obj, true);
    186   1.1  fredette 	assert(def != NULL);
    187   1.1  fredette 	hppa_got_cache_obj = obj;
    188   1.1  fredette 	return hppa_got_cache_got =
    189   1.1  fredette 	    (Elf_Addr *)(obj->relocbase + def->st_value);
    190   1.1  fredette }
    191   1.1  fredette 
    192   1.1  fredette /*
    193   1.1  fredette  * This allocates a PLABEL.  If called with a non-NULL def, the
    194   1.1  fredette  * plabel is for the function associated with that definition
    195   1.1  fredette  * in the defining object defobj, plus the given addend.  If
    196   1.1  fredette  * called with a NULL def, the plabel is for the function at
    197   1.1  fredette  * the (unrelocated) address in addend in the object defobj.
    198   1.1  fredette  */
    199   1.1  fredette Elf_Addr
    200   1.1  fredette _rtld_function_descriptor_alloc(const Obj_Entry *defobj, const Elf_Sym *def,
    201   1.1  fredette     Elf_Addr addend)
    202   1.1  fredette {
    203   1.1  fredette 	Elf_Addr	func_pc, func_sl;
    204   1.1  fredette 	hppa_plabel	*plabel;
    205   1.1  fredette 
    206   1.1  fredette 	if (def != NULL) {
    207   1.1  fredette 
    208   1.1  fredette 		/*
    209   1.1  fredette 		 * We assume that symbols of type STT_NOTYPE
    210   1.1  fredette 		 * are undefined.  Return NULL for these.
    211   1.1  fredette 		 */
    212   1.1  fredette 		if (ELF_ST_TYPE(def->st_info) == STT_NOTYPE)
    213   1.1  fredette 			return (Elf_Addr)NULL;
    214   1.1  fredette 
    215   1.1  fredette 		/* Otherwise assert that this symbol must be a function. */
    216   1.1  fredette 		assert(ELF_ST_TYPE(def->st_info) == STT_FUNC);
    217   1.1  fredette 
    218   1.1  fredette 		func_pc = (Elf_Addr)(defobj->relocbase + def->st_value +
    219   1.1  fredette 		    addend);
    220   1.1  fredette 	} else
    221   1.1  fredette 		func_pc = (Elf_Addr)(defobj->relocbase + addend);
    222   1.1  fredette 
    223   1.1  fredette 	/*
    224   1.1  fredette 	 * Search the existing PLABELs for one matching
    225   1.1  fredette 	 * this function.  If there is one, return it.
    226   1.1  fredette 	 */
    227   1.1  fredette 	func_sl = (Elf_Addr)HPPA_OBJ_SL(defobj);
    228   1.1  fredette 	SLIST_FOREACH(plabel, &hppa_plabel_list, hppa_plabel_next)
    229   1.1  fredette 		if (plabel->hppa_plabel_pc == func_pc &&
    230   1.1  fredette 		    plabel->hppa_plabel_sl == func_sl)
    231   1.1  fredette 			return RTLD_MAKE_PLABEL(plabel);
    232   1.1  fredette 
    233   1.1  fredette 	/*
    234   1.1  fredette 	 * XXX - this assumes that the dynamic linker doesn't
    235   1.1  fredette 	 * have more than HPPA_PLABEL_PRE PLABEL relocations.
    236   1.1  fredette 	 * Once we've used up the preallocated set, we start
    237   1.1  fredette 	 * using NEW to allocate plabels.
    238   1.1  fredette 	 */
    239   1.1  fredette 	if (hppa_plabel_pre_next < HPPA_PLABEL_PRE)
    240   1.1  fredette 		plabel = &hppa_plabel_pre[hppa_plabel_pre_next++];
    241   1.1  fredette 	else {
    242   1.1  fredette 		plabel = NEW(hppa_plabel);
    243   1.1  fredette 		if (plabel == NULL)
    244   1.1  fredette 			return (Elf_Addr)-1;
    245   1.1  fredette 	}
    246   1.1  fredette 
    247   1.1  fredette 	/* Fill the new entry and insert it on the list. */
    248   1.1  fredette 	plabel->hppa_plabel_pc = func_pc;
    249   1.1  fredette 	plabel->hppa_plabel_sl = func_sl;
    250   1.1  fredette 	SLIST_INSERT_HEAD(&hppa_plabel_list, plabel, hppa_plabel_next);
    251   1.1  fredette 
    252   1.1  fredette 	return RTLD_MAKE_PLABEL(plabel);
    253   1.1  fredette }
    254   1.1  fredette 
    255   1.1  fredette /*
    256   1.1  fredette  * If a pointer is a PLABEL, this unwraps it.
    257   1.1  fredette  */
    258   1.1  fredette const void *
    259   1.1  fredette _rtld_function_descriptor_function(const void *addr)
    260   1.1  fredette {
    261   1.1  fredette 	return (RTLD_IS_PLABEL(addr) ?
    262   1.1  fredette 	    (const void *) RTLD_GET_PLABEL(addr)->hppa_plabel_pc :
    263   1.1  fredette 	    addr);
    264   1.1  fredette }
    265   1.1  fredette 
    266   1.1  fredette /*
    267   1.1  fredette  * This handles an IPLT relocation, with or without a symbol.
    268   1.1  fredette  */
    269   1.1  fredette int
    270  1.14   mycroft _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, caddr_t *addrp)
    271   1.1  fredette {
    272   1.1  fredette 	Elf_Addr	*where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    273   1.1  fredette 	const Elf_Sym	*def;
    274   1.1  fredette 	const Obj_Entry	*defobj;
    275   1.1  fredette 	Elf_Addr	func_pc, func_sl;
    276   1.1  fredette 
    277   1.1  fredette 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(IPLT));
    278   1.1  fredette 
    279   1.1  fredette 	/*
    280   1.1  fredette 	 * If this is an IPLT reloc for a static function,
    281   1.1  fredette 	 * fully resolve the PLT entry now.
    282   1.1  fredette 	 */
    283   1.1  fredette 	if (ELF_R_SYM(rela->r_info) == 0) {
    284   1.1  fredette 		func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
    285   1.1  fredette 		func_sl = (Elf_Addr)HPPA_OBJ_SL(obj);
    286   1.1  fredette 	}
    287   1.1  fredette 
    288   1.1  fredette 	/*
    289   1.1  fredette 	 * If we must bind now, fully resolve the PLT entry.
    290   1.1  fredette 	 */
    291   1.8   mycroft 	else {
    292   1.1  fredette 
    293   1.1  fredette 		/*
    294   1.1  fredette 		 * Look up the symbol.  While we're relocating self,
    295   1.1  fredette 		 * _rtld_objlist is NULL, so just pass in self.
    296   1.1  fredette 		 */
    297   1.6   mycroft 		def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
    298   1.6   mycroft 		    false);
    299   1.1  fredette 		if (def == NULL)
    300   1.1  fredette 			return -1;
    301   1.1  fredette 		func_pc = (Elf_Addr)(defobj->relocbase + def->st_value +
    302   1.1  fredette 		    rela->r_addend);
    303   1.1  fredette 		func_sl = (Elf_Addr)HPPA_OBJ_SL(defobj);
    304   1.1  fredette 	}
    305   1.1  fredette 
    306   1.1  fredette 	/*
    307   1.1  fredette 	 * Fill this PLT entry and return.
    308   1.1  fredette 	 */
    309   1.1  fredette 	where[0] = func_pc;
    310   1.1  fredette 	where[1] = func_sl;
    311   1.8   mycroft 
    312   1.8   mycroft 	*addrp = (caddr_t)where;
    313   1.1  fredette 	return 0;
    314   1.2   mycroft }
    315   1.2   mycroft 
    316   1.2   mycroft /* This sets up an object's GOT. */
    317   1.2   mycroft void
    318   1.2   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
    319   1.2   mycroft {
    320   1.2   mycroft 	__rtld_setup_hppa_pltgot(obj, HPPA_OBJ_GOT(obj));
    321   1.4   mycroft }
    322   1.4   mycroft 
    323   1.4   mycroft int
    324  1.14   mycroft _rtld_relocate_nonplt_objects(obj, self)
    325   1.9   mycroft 	const Obj_Entry *obj;
    326  1.11   mycroft 	bool self;
    327   1.4   mycroft {
    328   1.5   mycroft 	const Elf_Rela *rela;
    329   1.5   mycroft 
    330   1.5   mycroft 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    331   1.5   mycroft 		Elf_Addr        *where;
    332   1.5   mycroft 		const Elf_Sym   *def;
    333   1.5   mycroft 		const Obj_Entry *defobj;
    334   1.5   mycroft 		Elf_Addr         tmp;
    335   1.6   mycroft 		unsigned long	 symnum;
    336   1.5   mycroft 
    337   1.5   mycroft 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    338   1.6   mycroft 		symnum = ELF_R_SYM(rela->r_info);
    339   1.5   mycroft 
    340   1.5   mycroft 		switch (ELF_R_TYPE(rela->r_info)) {
    341   1.5   mycroft 		case R_TYPE(NONE):
    342   1.5   mycroft 			break;
    343   1.4   mycroft 
    344   1.5   mycroft 		case R_TYPE(DIR32):
    345   1.6   mycroft 			if (symnum) {
    346   1.5   mycroft 				/*
    347   1.5   mycroft 				 * This is either a DIR32 against a symbol
    348   1.5   mycroft 				 * (def->st_name != 0), or against a local
    349   1.5   mycroft 				 * section (def->st_name == 0).
    350   1.5   mycroft 				 */
    351   1.6   mycroft 				def = obj->symtab + symnum;
    352   1.5   mycroft 				defobj = obj;
    353   1.5   mycroft 				if (def->st_name != 0)
    354   1.5   mycroft 					/*
    355   1.5   mycroft 			 		 * While we're relocating self,
    356   1.5   mycroft 					 * _rtld_objlist is NULL, so we just
    357   1.5   mycroft 					 * pass in self.
    358   1.5   mycroft 					 */
    359   1.6   mycroft 					def = _rtld_find_symdef(symnum, obj,
    360   1.6   mycroft 					    &defobj, false);
    361   1.5   mycroft 				if (def == NULL)
    362   1.5   mycroft 					return -1;
    363   1.4   mycroft 
    364   1.5   mycroft 				tmp = (Elf_Addr)(defobj->relocbase +
    365   1.5   mycroft 				    def->st_value + rela->r_addend);
    366   1.4   mycroft 
    367   1.5   mycroft 				if (*where != tmp)
    368   1.5   mycroft 					*where = tmp;
    369  1.14   mycroft 				rdbg(("DIR32 %s in %s --> %p in %s",
    370   1.7   mycroft 				    obj->strtab + obj->symtab[symnum].st_name,
    371   1.7   mycroft 				    obj->path, (void *)*where, defobj->path));
    372   1.5   mycroft 			} else {
    373   1.5   mycroft 				extern Elf_Addr	_GLOBAL_OFFSET_TABLE_[];
    374   1.5   mycroft 				extern Elf_Addr	_GOT_END_[];
    375   1.5   mycroft 
    376   1.5   mycroft 				tmp = (Elf_Addr)(obj->relocbase +
    377   1.5   mycroft 				    rela->r_addend);
    378   1.5   mycroft 
    379   1.5   mycroft 				/* This is the ...iffy hueristic. */
    380  1.11   mycroft 				if (!self ||
    381   1.5   mycroft 				    (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
    382   1.5   mycroft 				    (caddr_t)where >= (caddr_t)_GOT_END_) {
    383   1.5   mycroft 					if (*where != tmp)
    384   1.5   mycroft 						*where = tmp;
    385  1.14   mycroft 					rdbg(("DIR32 in %s --> %p", obj->path,
    386  1.14   mycroft 					    (void *)*where));
    387   1.5   mycroft 				} else
    388  1.14   mycroft 					rdbg(("DIR32 in %s stays at %p",
    389   1.5   mycroft 					    obj->path, (void *)*where));
    390   1.5   mycroft 			}
    391   1.5   mycroft 			break;
    392   1.5   mycroft 
    393   1.5   mycroft 		case R_TYPE(PLABEL32):
    394   1.6   mycroft 			if (symnum) {
    395   1.4   mycroft 				/*
    396   1.4   mycroft 		 		 * While we're relocating self, _rtld_objlist
    397   1.4   mycroft 				 * is NULL, so we just pass in self.
    398   1.4   mycroft 				 */
    399   1.6   mycroft 				def = _rtld_find_symdef(symnum, obj, &defobj,
    400   1.6   mycroft 				    false);
    401   1.5   mycroft 				if (def == NULL)
    402   1.5   mycroft 					return -1;
    403   1.4   mycroft 
    404   1.5   mycroft 				tmp = _rtld_function_descriptor_alloc(defobj, def,
    405   1.5   mycroft 				    rela->r_addend);
    406   1.5   mycroft 				if (tmp == (Elf_Addr)-1)
    407   1.5   mycroft 					return -1;
    408   1.4   mycroft 
    409   1.4   mycroft 				if (*where != tmp)
    410   1.4   mycroft 					*where = tmp;
    411  1.14   mycroft 				rdbg(("PLABEL32 %s in %s --> %p in %s",
    412   1.7   mycroft 				    obj->strtab + obj->symtab[symnum].st_name,
    413   1.7   mycroft 				    obj->path, (void *)*where, defobj->path));
    414   1.5   mycroft 			} else {
    415   1.5   mycroft 				/*
    416   1.5   mycroft 				 * This is a PLABEL for a static function, and
    417   1.5   mycroft 				 * the dynamic linker has both allocated a PLT
    418   1.5   mycroft 				 * entry for this function and told us where it
    419   1.5   mycroft 				 * is.  We can safely use the PLT entry as the
    420   1.5   mycroft 				 * PLABEL because there should be no other
    421   1.5   mycroft 				 * PLABEL reloc referencing this function.
    422   1.5   mycroft 				 * This object should also have an IPLT
    423   1.5   mycroft 				 * relocation to initialize the PLT entry.
    424   1.5   mycroft 				 *
    425   1.5   mycroft 				 * The dynamic linker should also have ensured
    426   1.5   mycroft 				 * that the addend has the
    427   1.5   mycroft 				 * next-least-significant bit set; the
    428   1.5   mycroft 				 * $$dyncall millicode uses this to distinguish
    429   1.5   mycroft 				 * a PLABEL pointer from a plain function
    430   1.5   mycroft 				 * pointer.
    431   1.5   mycroft 				 */
    432   1.5   mycroft 				tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
    433   1.5   mycroft 
    434   1.5   mycroft 				if (*where != tmp)
    435   1.5   mycroft 					*where = tmp;
    436  1.14   mycroft 				rdbg(("PLABEL32 in %s --> %p", obj->path,
    437  1.14   mycroft 				    (void *)*where));
    438   1.5   mycroft 			}
    439   1.5   mycroft 			break;
    440   1.4   mycroft 
    441   1.5   mycroft 		case R_TYPE(COPY):
    442   1.4   mycroft 			/*
    443   1.5   mycroft 			 * These are deferred until all other relocations have
    444   1.5   mycroft 			 * been done.  All we do here is make sure that the
    445   1.5   mycroft 			 * COPY relocation is not in a shared library.  They
    446   1.5   mycroft 			 * are allowed only in executable files.
    447   1.4   mycroft 			 */
    448  1.10   mycroft 			if (obj->isdynamic) {
    449   1.5   mycroft 				_rtld_error(
    450   1.5   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    451   1.5   mycroft 				    obj->path);
    452   1.4   mycroft 				return -1;
    453   1.5   mycroft 			}
    454  1.14   mycroft 			rdbg(("COPY (avoid in main)"));
    455   1.5   mycroft 			break;
    456   1.4   mycroft 
    457   1.5   mycroft 		default:
    458  1.14   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    459   1.5   mycroft 			    "addend = %p, contents = %p, symbol = %s",
    460   1.6   mycroft 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
    461   1.5   mycroft 			    (void *)rela->r_offset, (void *)rela->r_addend,
    462   1.5   mycroft 			    (void *)*where,
    463   1.6   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    464   1.5   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    465   1.5   mycroft 			    "in non-PLT relocations\n",
    466   1.5   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    467   1.4   mycroft 			return -1;
    468   1.4   mycroft 		}
    469   1.8   mycroft 	}
    470   1.8   mycroft 	return 0;
    471   1.8   mycroft }
    472   1.8   mycroft 
    473   1.8   mycroft int
    474  1.14   mycroft _rtld_relocate_plt_lazy(obj)
    475   1.9   mycroft 	const Obj_Entry *obj;
    476   1.8   mycroft {
    477   1.8   mycroft 	const Elf_Rela *rela;
    478   1.8   mycroft 
    479   1.8   mycroft 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    480   1.8   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    481   1.8   mycroft 		Elf_Addr func_pc, func_sl;
    482   1.8   mycroft 
    483   1.8   mycroft 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(IPLT));
    484   1.8   mycroft 
    485   1.8   mycroft 		/*
    486   1.8   mycroft 		 * If this is an IPLT reloc for a static function,
    487   1.8   mycroft 		 * fully resolve the PLT entry now.
    488   1.8   mycroft 		 */
    489   1.8   mycroft 		if (ELF_R_SYM(rela->r_info) == 0) {
    490   1.8   mycroft 			func_pc = (Elf_Addr)(obj->relocbase + rela->r_addend);
    491   1.8   mycroft 			func_sl = (Elf_Addr)HPPA_OBJ_SL(obj);
    492   1.8   mycroft 		}
    493   1.8   mycroft 
    494   1.8   mycroft 		/*
    495   1.8   mycroft 		 * Otherwise set up for lazy binding.
    496   1.8   mycroft 		 */
    497   1.8   mycroft 		else {
    498   1.8   mycroft 			/*
    499   1.8   mycroft 			 * This function pointer points to the PLT
    500   1.8   mycroft 			 * stub added by the linker, and instead of
    501   1.8   mycroft 			 * a shared linkage value, we stash this
    502   1.8   mycroft 			 * relocation's offset.  The PLT stub has
    503   1.8   mycroft 			 * already been set up to transfer to
    504   1.8   mycroft 			 * _rtld_bind_start.
    505   1.8   mycroft 			 */
    506   1.8   mycroft 			func_pc = ((Elf_Addr)HPPA_OBJ_GOT(obj)) - 16;
    507   1.8   mycroft 			func_sl = (Elf_Addr)((caddr_t)rela - (caddr_t)obj->pltrela);
    508   1.8   mycroft 		}
    509   1.8   mycroft 
    510   1.8   mycroft 		/*
    511   1.8   mycroft 		 * Fill this PLT entry and return.
    512   1.8   mycroft 		 */
    513   1.8   mycroft 		where[0] = func_pc;
    514   1.8   mycroft 		where[1] = func_sl;
    515   1.4   mycroft 	}
    516   1.4   mycroft 	return 0;
    517   1.1  fredette }
    518