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