xref: /titanic_41/usr/src/cmd/sgs/rtld/sparc/common_sparc.c (revision ed5289f91b9bf164dccd6c75398362be77a4478d)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 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 			Conv_inv_buf_t	inv_buf;
123 			const char	*str, *sym1, *sym2;
124 
125 			if (rp->rl_sym == symdef) {
126 				/*
127 				 * Same symbol definition - everything is a-ok.
128 				 */
129 				return (1);
130 			}
131 
132 			sym1 = (STRTAB(rp->rl_lmp) + rp->rl_sym->st_name);
133 			sym2 = (STRTAB(lmp) + symdef->st_name);
134 
135 			if (check_regsyms(rp->rl_sym, sym1, symdef, sym2) == 0)
136 				continue;
137 
138 			if ((str = demangle(sym1)) != sym1) {
139 				char	*_str = alloca(strlen(str) + 1);
140 				(void) strcpy(_str, str);
141 				sym1 = (const char *)_str;
142 			}
143 			sym2 = demangle(sym2);
144 
145 			if (LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) {
146 				(void) printf(MSG_INTL(MSG_LDD_REG_SYMCONF),
147 				    conv_sym_SPARC_value(symdef->st_value,
148 				    0, &inv_buf), NAME(rp->rl_lmp),
149 				    sym1, NAME(lmp), sym2);
150 			} else {
151 				eprintf(LIST(lmp), ERR_FATAL,
152 				    MSG_INTL(MSG_REG_SYMCONF),
153 				    conv_sym_SPARC_value(symdef->st_value,
154 				    0, &inv_buf), NAME(rp->rl_lmp),
155 				    sym1, NAME(lmp), sym2);
156 				return (0);
157 			}
158 		}
159 		if ((rp = calloc(sizeof (Reglist), 1)) == (Reglist *)0)
160 			return (0);
161 		rp->rl_lmp = lmp;
162 		rp->rl_sym = symdef;
163 		rp->rl_next = reglist;
164 		reglist = rp;
165 	}
166 	return (1);
167 }
168 
169 
170 /*
171  * When the relocation loop realizes that it's dealing with relative
172  * relocations in a shared object, it breaks into this tighter loop
173  * as an optimization.
174  */
175 ulong_t
176 elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz,
177     ulong_t basebgn, ulong_t etext, ulong_t emap)
178 {
179 	ulong_t roffset = ((Rela *) relbgn)->r_offset;
180 	Byte rtype;
181 
182 	do {
183 		roffset += basebgn;
184 
185 		/*
186 		 * If this relocation is against an address not mapped in,
187 		 * then break out of the relative relocation loop, falling
188 		 * back on the main relocation loop.
189 		 */
190 		if (roffset < etext || roffset > emap)
191 			break;
192 
193 		/*
194 		 * Perform the actual relocation.
195 		 */
196 		*((ulong_t *)roffset) +=
197 		    basebgn + (long)(((Rela *)relbgn)->r_addend);
198 
199 		relbgn += relsiz;
200 
201 		if (relbgn >= relend)
202 			break;
203 
204 		rtype = (Byte)ELF_R_TYPE(((Rela *)relbgn)->r_info, M_MACH);
205 		roffset = ((Rela *)relbgn)->r_offset;
206 
207 	} while (rtype == R_SPARC_RELATIVE);
208 
209 	return (relbgn);
210 }
211 
212 /*
213  * This is the tightest loop for RELATIVE relocations for those
214  * objects built with the DT_RELACOUNT .dynamic entry.
215  */
216 ulong_t
217 elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz,
218     ulong_t basebgn)
219 {
220 	ulong_t roffset = ((Rela *) relbgn)->r_offset;
221 
222 	for (; relacount; relacount--) {
223 		roffset += basebgn;
224 
225 		/*
226 		 * Perform the actual relocation.
227 		 */
228 		*((ulong_t *)roffset) =
229 		    basebgn + (long)(((Rela *)relbgn)->r_addend);
230 
231 		relbgn += relsiz;
232 
233 		roffset = ((Rela *)relbgn)->r_offset;
234 	}
235 
236 	return (relbgn);
237 }
238