xref: /titanic_52/usr/src/cmd/sgs/rtld/sparc/common_sparc.c (revision 06e46062ef4f5f4b687cbafb4518fb123fe23920)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2000-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 
29 #include	<stdio.h>
30 #include	<strings.h>
31 #include	<sys/elf.h>
32 #include	<sys/elf_SPARC.h>
33 #include	<alloca.h>
34 #include	"_rtld.h"
35 #include	"_elf.h"
36 #include	"msg.h"
37 #include	"conv.h"
38 
39 /*
40  *
41  *  Matrix of legal combinations of usage of a given register:
42  *
43  *	Obj1\Obj2       Scratch Named
44  *	Scratch          OK      NO
45  *	Named            NO      *
46  *
47  *  * OK if the symbols are identical, NO if they are not.  Two symbols
48  *  are identical if and only if one of the following is true:
49  *        A. They are both global and have the same name.
50  *        B. They are both local, have the same name, and are defined in
51  *        the same object.  (Note that a local symbol in one object is
52  *        never identical to a local symbol in another object, even if the
53  *        name is the same.)
54  *
55  *  Matrix of legal combinations of st_shndx for the same register symbol:
56  *
57  *	Obj1\Obj2       UNDEF   ABS
58  *	UNDEF            OK      OK
59  *	ABS              OK      NO
60  */
61 
62 /*
63  * Test the compatiblity of two register symbols, 0 pass, >0 fail
64  */
65 static uintptr_t
66 check_regsyms(Sym * sym1, const char * name1, Sym * sym2, const char * name2)
67 {
68 	if ((sym1->st_name == 0) && (sym2->st_name == 0))
69 		return (0);	/* scratches are always compatible */
70 
71 	if ((ELF_ST_BIND(sym1->st_info) == STB_LOCAL) ||
72 	    (ELF_ST_BIND(sym2->st_info) == STB_LOCAL)) {
73 		if (sym1->st_value == sym2->st_value)
74 			return (1);	/* local symbol incompat */
75 		return (0);		/* no other prob from locals */
76 	}
77 
78 	if (sym1->st_value == sym2->st_value) {
79 		/* NOTE this just avoids strcmp */
80 		if ((sym1->st_name == 0) || (sym2->st_name == 0))
81 			return (2);	/* can't match scratch to named */
82 
83 		if (strcmp(name1, name2) != 0)
84 			return (4);	/* diff name, same register value */
85 
86 		if ((sym1->st_shndx == SHN_ABS) && (sym2->st_shndx == SHN_ABS))
87 			return (3);	/* multiply defined */
88 	} else if (strcmp(name1, name2) == 0)
89 		return (5);	/* same name, diff register value */
90 
91 	return (0);
92 }
93 
94 int
95 elf_regsyms(Rt_map * lmp)
96 {
97 	Dyn *	dyn;
98 	Sym *	symdef;
99 	ulong_t	rsymndx;
100 
101 	/*
102 	 * Scan through the .dynamic section of this object looking for all
103 	 * DT_REGISTER entries.  For each DT_REGISTER entry found identify the
104 	 * register symbol it identifies and confirm that it doesn't conflict
105 	 * with any other register symbols.
106 	 */
107 	for (dyn = DYN(lmp); dyn->d_tag != DT_NULL; dyn++) {
108 		Reglist *	rp;
109 
110 		if ((dyn->d_tag != DT_SPARC_REGISTER) &&
111 		    (dyn->d_tag != DT_DEPRECATED_SPARC_REGISTER))
112 			continue;
113 
114 		/*
115 		 * Get the local symbol table entry.
116 		 */
117 		rsymndx = dyn->d_un.d_val;
118 		symdef = (Sym *)((unsigned long)SYMTAB(lmp) +
119 				(rsymndx * SYMENT(lmp)));
120 
121 		for (rp = reglist; rp; rp = rp->rl_next) {
122 			const char *	str, * sym1, * sym2;
123 
124 			if (rp->rl_sym == symdef) {
125 				/*
126 				 * Same symbol definition - everything is a-ok.
127 				 */
128 				return (1);
129 			}
130 
131 			sym1 = (STRTAB(rp->rl_lmp) + rp->rl_sym->st_name);
132 			sym2 = (STRTAB(lmp) + symdef->st_name);
133 
134 			if (check_regsyms(rp->rl_sym, sym1, symdef, sym2) == 0)
135 				continue;
136 
137 			if ((str = demangle(sym1)) != sym1) {
138 				char *	_str = alloca(strlen(str) + 1);
139 				(void) strcpy(_str, str);
140 				sym1 = (const char *)_str;
141 			}
142 			sym2 = demangle(sym2);
143 
144 			if (LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) {
145 				(void) printf(MSG_INTL(MSG_LDD_REG_SYMCONF),
146 				    conv_sym_SPARC_value_str(symdef->st_value),
147 				    NAME(rp->rl_lmp), sym1, NAME(lmp), sym2);
148 			} else {
149 				eprintf(ERR_FATAL, MSG_INTL(MSG_REG_SYMCONF),
150 				    conv_sym_SPARC_value_str(symdef->st_value),
151 				    NAME(rp->rl_lmp), sym1, NAME(lmp), sym2);
152 				return (0);
153 			}
154 		}
155 		if ((rp = calloc(sizeof (Reglist), 1)) == (Reglist *)0)
156 			return (0);
157 		rp->rl_lmp = lmp;
158 		rp->rl_sym = symdef;
159 		rp->rl_next = reglist;
160 		reglist = rp;
161 	}
162 	return (1);
163 }
164 
165 
166 /*
167  * When the relocation loop realizes that it's dealing with relative
168  * relocations in a shared object, it breaks into this tighter loop
169  * as an optimization.
170  */
171 ulong_t
172 elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz,
173     ulong_t basebgn, ulong_t etext, ulong_t emap)
174 {
175 	ulong_t roffset = ((Rela *) relbgn)->r_offset;
176 	Byte rtype;
177 
178 	do {
179 		roffset += basebgn;
180 
181 		/*
182 		 * If this relocation is against an address not mapped in,
183 		 * then break out of the relative relocation loop, falling
184 		 * back on the main relocation loop.
185 		 */
186 		if (roffset < etext || roffset > emap)
187 			break;
188 
189 		/*
190 		 * Perform the actual relocation.
191 		 */
192 		*((ulong_t *)roffset) +=
193 		    basebgn + (long)(((Rela *)relbgn)->r_addend);
194 
195 		relbgn += relsiz;
196 
197 		if (relbgn >= relend)
198 			break;
199 
200 		rtype = (Byte)ELF_R_TYPE(((Rela *)relbgn)->r_info);
201 		roffset = ((Rela *)relbgn)->r_offset;
202 
203 	} while (rtype == R_SPARC_RELATIVE);
204 
205 	return (relbgn);
206 }
207 
208 /*
209  * This is the tightest loop for RELATIVE relocations for those
210  * objects built with the DT_RELACOUNT .dynamic entry.
211  */
212 ulong_t
213 elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz,
214     ulong_t basebgn)
215 {
216 	ulong_t roffset = ((Rela *) relbgn)->r_offset;
217 
218 	for (; relacount; relacount--) {
219 		roffset += basebgn;
220 
221 		/*
222 		 * Perform the actual relocation.
223 		 */
224 		*((ulong_t *)roffset) =
225 		    basebgn + (long)(((Rela *)relbgn)->r_addend);
226 
227 		relbgn += relsiz;
228 
229 		roffset = ((Rela *)relbgn)->r_offset;
230 	}
231 
232 	return (relbgn);
233 }
234