xref: /onnv-gate/usr/src/cmd/sgs/rtld/sparc/common_sparc.c (revision 4734:a4708faa3e85)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211618Srie 
220Sstevel@tonic-gate /*
23*4734Sab196087  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include	<stdio.h>
300Sstevel@tonic-gate #include	<strings.h>
310Sstevel@tonic-gate #include	<sys/elf.h>
320Sstevel@tonic-gate #include	<sys/elf_SPARC.h>
330Sstevel@tonic-gate #include	<alloca.h>
340Sstevel@tonic-gate #include	"_rtld.h"
350Sstevel@tonic-gate #include	"_elf.h"
360Sstevel@tonic-gate #include	"msg.h"
370Sstevel@tonic-gate #include	"conv.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  *  Matrix of legal combinations of usage of a given register:
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  *	Obj1\Obj2       Scratch Named
440Sstevel@tonic-gate  *	Scratch          OK      NO
450Sstevel@tonic-gate  *	Named            NO      *
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  *  * OK if the symbols are identical, NO if they are not.  Two symbols
480Sstevel@tonic-gate  *  are identical if and only if one of the following is true:
490Sstevel@tonic-gate  *        A. They are both global and have the same name.
500Sstevel@tonic-gate  *        B. They are both local, have the same name, and are defined in
510Sstevel@tonic-gate  *        the same object.  (Note that a local symbol in one object is
520Sstevel@tonic-gate  *        never identical to a local symbol in another object, even if the
530Sstevel@tonic-gate  *        name is the same.)
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  *  Matrix of legal combinations of st_shndx for the same register symbol:
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  *	Obj1\Obj2       UNDEF   ABS
580Sstevel@tonic-gate  *	UNDEF            OK      OK
590Sstevel@tonic-gate  *	ABS              OK      NO
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * Test the compatiblity of two register symbols, 0 pass, >0 fail
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static uintptr_t
661618Srie check_regsyms(Sym *sym1, const char *name1, Sym *sym2, const char *name2)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	if ((sym1->st_name == 0) && (sym2->st_name == 0))
690Sstevel@tonic-gate 		return (0);	/* scratches are always compatible */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if ((ELF_ST_BIND(sym1->st_info) == STB_LOCAL) ||
720Sstevel@tonic-gate 	    (ELF_ST_BIND(sym2->st_info) == STB_LOCAL)) {
730Sstevel@tonic-gate 		if (sym1->st_value == sym2->st_value)
740Sstevel@tonic-gate 			return (1);	/* local symbol incompat */
750Sstevel@tonic-gate 		return (0);		/* no other prob from locals */
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if (sym1->st_value == sym2->st_value) {
790Sstevel@tonic-gate 		/* NOTE this just avoids strcmp */
800Sstevel@tonic-gate 		if ((sym1->st_name == 0) || (sym2->st_name == 0))
810Sstevel@tonic-gate 			return (2);	/* can't match scratch to named */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 		if (strcmp(name1, name2) != 0)
840Sstevel@tonic-gate 			return (4);	/* diff name, same register value */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 		if ((sym1->st_shndx == SHN_ABS) && (sym2->st_shndx == SHN_ABS))
870Sstevel@tonic-gate 			return (3);	/* multiply defined */
880Sstevel@tonic-gate 	} else if (strcmp(name1, name2) == 0)
890Sstevel@tonic-gate 		return (5);	/* same name, diff register value */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	return (0);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate int
950Sstevel@tonic-gate elf_regsyms(Rt_map * lmp)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	Dyn *	dyn;
980Sstevel@tonic-gate 	Sym *	symdef;
990Sstevel@tonic-gate 	ulong_t	rsymndx;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/*
1020Sstevel@tonic-gate 	 * Scan through the .dynamic section of this object looking for all
1030Sstevel@tonic-gate 	 * DT_REGISTER entries.  For each DT_REGISTER entry found identify the
1040Sstevel@tonic-gate 	 * register symbol it identifies and confirm that it doesn't conflict
1050Sstevel@tonic-gate 	 * with any other register symbols.
1060Sstevel@tonic-gate 	 */
1070Sstevel@tonic-gate 	for (dyn = DYN(lmp); dyn->d_tag != DT_NULL; dyn++) {
1080Sstevel@tonic-gate 		Reglist *	rp;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 		if ((dyn->d_tag != DT_SPARC_REGISTER) &&
1110Sstevel@tonic-gate 		    (dyn->d_tag != DT_DEPRECATED_SPARC_REGISTER))
1120Sstevel@tonic-gate 			continue;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		/*
1150Sstevel@tonic-gate 		 * Get the local symbol table entry.
1160Sstevel@tonic-gate 		 */
1170Sstevel@tonic-gate 		rsymndx = dyn->d_un.d_val;
1180Sstevel@tonic-gate 		symdef = (Sym *)((unsigned long)SYMTAB(lmp) +
119*4734Sab196087 		    (rsymndx * SYMENT(lmp)));
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 		for (rp = reglist; rp; rp = rp->rl_next) {
122*4734Sab196087 			Conv_inv_buf_t	inv_buf;
1231618Srie 			const char	*str, *sym1, *sym2;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 			if (rp->rl_sym == symdef) {
1260Sstevel@tonic-gate 				/*
1270Sstevel@tonic-gate 				 * Same symbol definition - everything is a-ok.
1280Sstevel@tonic-gate 				 */
1290Sstevel@tonic-gate 				return (1);
1300Sstevel@tonic-gate 			}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 			sym1 = (STRTAB(rp->rl_lmp) + rp->rl_sym->st_name);
1330Sstevel@tonic-gate 			sym2 = (STRTAB(lmp) + symdef->st_name);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 			if (check_regsyms(rp->rl_sym, sym1, symdef, sym2) == 0)
1360Sstevel@tonic-gate 				continue;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 			if ((str = demangle(sym1)) != sym1) {
1391618Srie 				char	*_str = alloca(strlen(str) + 1);
1400Sstevel@tonic-gate 				(void) strcpy(_str, str);
1410Sstevel@tonic-gate 				sym1 = (const char *)_str;
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 			sym2 = demangle(sym2);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 			if (LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) {
1460Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_REG_SYMCONF),
147*4734Sab196087 				    conv_sym_SPARC_value(symdef->st_value,
148*4734Sab196087 				    0, &inv_buf), NAME(rp->rl_lmp),
149*4734Sab196087 				    sym1, NAME(lmp), sym2);
1500Sstevel@tonic-gate 			} else {
1511618Srie 				eprintf(LIST(lmp), ERR_FATAL,
1521618Srie 				    MSG_INTL(MSG_REG_SYMCONF),
153*4734Sab196087 				    conv_sym_SPARC_value(symdef->st_value,
154*4734Sab196087 				    0, &inv_buf), NAME(rp->rl_lmp),
155*4734Sab196087 				    sym1, NAME(lmp), sym2);
1560Sstevel@tonic-gate 				return (0);
1570Sstevel@tonic-gate 			}
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 		if ((rp = calloc(sizeof (Reglist), 1)) == (Reglist *)0)
1600Sstevel@tonic-gate 			return (0);
1610Sstevel@tonic-gate 		rp->rl_lmp = lmp;
1620Sstevel@tonic-gate 		rp->rl_sym = symdef;
1630Sstevel@tonic-gate 		rp->rl_next = reglist;
1640Sstevel@tonic-gate 		reglist = rp;
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 	return (1);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * When the relocation loop realizes that it's dealing with relative
1720Sstevel@tonic-gate  * relocations in a shared object, it breaks into this tighter loop
1730Sstevel@tonic-gate  * as an optimization.
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate ulong_t
1760Sstevel@tonic-gate elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz,
1770Sstevel@tonic-gate     ulong_t basebgn, ulong_t etext, ulong_t emap)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	ulong_t roffset = ((Rela *) relbgn)->r_offset;
1800Sstevel@tonic-gate 	Byte rtype;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	do {
1830Sstevel@tonic-gate 		roffset += basebgn;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 		/*
1860Sstevel@tonic-gate 		 * If this relocation is against an address not mapped in,
1870Sstevel@tonic-gate 		 * then break out of the relative relocation loop, falling
1880Sstevel@tonic-gate 		 * back on the main relocation loop.
1890Sstevel@tonic-gate 		 */
1900Sstevel@tonic-gate 		if (roffset < etext || roffset > emap)
1910Sstevel@tonic-gate 			break;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		/*
1940Sstevel@tonic-gate 		 * Perform the actual relocation.
1950Sstevel@tonic-gate 		 */
1960Sstevel@tonic-gate 		*((ulong_t *)roffset) +=
1970Sstevel@tonic-gate 		    basebgn + (long)(((Rela *)relbgn)->r_addend);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		relbgn += relsiz;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		if (relbgn >= relend)
2020Sstevel@tonic-gate 			break;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		rtype = (Byte)ELF_R_TYPE(((Rela *)relbgn)->r_info);
2050Sstevel@tonic-gate 		roffset = ((Rela *)relbgn)->r_offset;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	} while (rtype == R_SPARC_RELATIVE);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	return (relbgn);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  * This is the tightest loop for RELATIVE relocations for those
2140Sstevel@tonic-gate  * objects built with the DT_RELACOUNT .dynamic entry.
2150Sstevel@tonic-gate  */
2160Sstevel@tonic-gate ulong_t
2170Sstevel@tonic-gate elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz,
2180Sstevel@tonic-gate     ulong_t basebgn)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	ulong_t roffset = ((Rela *) relbgn)->r_offset;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	for (; relacount; relacount--) {
2230Sstevel@tonic-gate 		roffset += basebgn;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		/*
2260Sstevel@tonic-gate 		 * Perform the actual relocation.
2270Sstevel@tonic-gate 		 */
2280Sstevel@tonic-gate 		*((ulong_t *)roffset) =
2290Sstevel@tonic-gate 		    basebgn + (long)(((Rela *)relbgn)->r_addend);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		relbgn += relsiz;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		roffset = ((Rela *)relbgn)->r_offset;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	return (relbgn);
2370Sstevel@tonic-gate }
238