xref: /titanic_44/usr/src/cmd/sgs/rtld/sparc/common_sparc.c (revision 0523b0a4ec1f5ec136a264130012c7bb0ede01f2)
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 2006 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(symdef->st_value),
147 				    NAME(rp->rl_lmp), sym1, NAME(lmp), sym2);
148 			} else {
149 				eprintf(LIST(lmp), ERR_FATAL,
150 				    MSG_INTL(MSG_REG_SYMCONF),
151 				    conv_sym_SPARC_value(symdef->st_value),
152 				    NAME(rp->rl_lmp), sym1, NAME(lmp), sym2);
153 				return (0);
154 			}
155 		}
156 		if ((rp = calloc(sizeof (Reglist), 1)) == (Reglist *)0)
157 			return (0);
158 		rp->rl_lmp = lmp;
159 		rp->rl_sym = symdef;
160 		rp->rl_next = reglist;
161 		reglist = rp;
162 	}
163 	return (1);
164 }
165 
166 
167 /*
168  * When the relocation loop realizes that it's dealing with relative
169  * relocations in a shared object, it breaks into this tighter loop
170  * as an optimization.
171  */
172 ulong_t
173 elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz,
174     ulong_t basebgn, ulong_t etext, ulong_t emap)
175 {
176 	ulong_t roffset = ((Rela *) relbgn)->r_offset;
177 	Byte rtype;
178 
179 	do {
180 		roffset += basebgn;
181 
182 		/*
183 		 * If this relocation is against an address not mapped in,
184 		 * then break out of the relative relocation loop, falling
185 		 * back on the main relocation loop.
186 		 */
187 		if (roffset < etext || roffset > emap)
188 			break;
189 
190 		/*
191 		 * Perform the actual relocation.
192 		 */
193 		*((ulong_t *)roffset) +=
194 		    basebgn + (long)(((Rela *)relbgn)->r_addend);
195 
196 		relbgn += relsiz;
197 
198 		if (relbgn >= relend)
199 			break;
200 
201 		rtype = (Byte)ELF_R_TYPE(((Rela *)relbgn)->r_info);
202 		roffset = ((Rela *)relbgn)->r_offset;
203 
204 	} while (rtype == R_SPARC_RELATIVE);
205 
206 	return (relbgn);
207 }
208 
209 /*
210  * This is the tightest loop for RELATIVE relocations for those
211  * objects built with the DT_RELACOUNT .dynamic entry.
212  */
213 ulong_t
214 elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz,
215     ulong_t basebgn)
216 {
217 	ulong_t roffset = ((Rela *) relbgn)->r_offset;
218 
219 	for (; relacount; relacount--) {
220 		roffset += basebgn;
221 
222 		/*
223 		 * Perform the actual relocation.
224 		 */
225 		*((ulong_t *)roffset) =
226 		    basebgn + (long)(((Rela *)relbgn)->r_addend);
227 
228 		relbgn += relsiz;
229 
230 		roffset = ((Rela *)relbgn)->r_offset;
231 	}
232 
233 	return (relbgn);
234 }
235