xref: /openbsd-src/libexec/ld.so/sh/rtld_machine.c (revision 875211e110347b0f9a5087f9d4691dc1184914c0)
1 /*	$OpenBSD: rtld_machine.c,v 1.20 2013/06/13 04:13:47 brad Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Dale Rahn
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #define _DYN_LOADER
30 #define LDSO_ARCH_IS_RELA_
31 
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 
35 #include <nlist.h>
36 #include <link.h>
37 #include <signal.h>
38 
39 #include "syscall.h"
40 #include "archdep.h"
41 #include "resolve.h"
42 
43 void _dl_bind_start(void); /* XXX */
44 Elf_Addr _dl_bind(elf_object_t *object, int reloff);
45 #define _RF_S		0x80000000		/* Resolve symbol */
46 #define _RF_A		0x40000000		/* Use addend */
47 #define _RF_P		0x20000000		/* Location relative */
48 #define _RF_G		0x10000000		/* GOT offset */
49 #define _RF_B		0x08000000		/* Load address relative */
50 #define _RF_U		0x04000000		/* Unaligned */
51 #define _RF_E		0x02000000		/* ERROR */
52 #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
53 #define _RF_RS(s)	((s) & 0xff)		/* right shift */
54 static int reloc_target_flags[] = {
55 	0,						/* 0	R_SH_NONE */
56 	_RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0), /* 1	R_SH_DIR32 */
57 	_RF_S|_RF_P|_RF_A|      _RF_SZ(32) | _RF_RS(0), /* 2  REL32 */
58 	_RF_E,						/* 3	R_SH_DIR8WPN */
59 	_RF_E,						/* 4	R_SH_IND12W */
60 	_RF_E,						/* 5	R_SH_DIR8WPL */
61 	_RF_E,						/* 6	R_SH_DIR8WPZ */
62 	_RF_E,						/* 7	R_SH_DIR8BP */
63 	_RF_E,						/* 8	R_SH_DIR8W */
64 	_RF_E,						/* 9	R_SH_DIR8L */
65 	_RF_E,						/* 10	R_SH_LOOP_START */
66 	_RF_E,						/* 11	R_SH_LOOP_END */
67 	_RF_E,						/* 12	Unused */
68 	_RF_E,						/* 13	Unused */
69 	_RF_E,						/* 14	Unused */
70 	_RF_E,						/* 15	Unused */
71 	_RF_E,						/* 16	Unused */
72 	_RF_E,						/* 17	Unused */
73 	_RF_E,						/* 18	Unused */
74 	_RF_E,						/* 19	Unused */
75 	_RF_E,						/* 20	Unused */
76 	_RF_E,						/* 21	Unused */
77 	_RF_E,						/* 22	R_SH_GNU_VTINHERIT */
78 	_RF_E,						/* 23	R_SH_GNU_VTENTRY */
79 	_RF_E,						/* 24	R_SH_SWITCH8 */
80 	_RF_E,						/* 25	R_SH_SWITCH16 */
81 	_RF_E,						/* 26	R_SH_SWITCH32 */
82 	_RF_E,						/* 27	R_SH_USES */
83 	_RF_E,						/* 28	R_SH_COUNT */
84 	_RF_E,						/* 29	R_SH_ALIGN */
85 	_RF_E,						/* 30	R_SH_CODE */
86 	_RF_E,						/* 31	R_SH_DATA */
87 	_RF_E,						/* 32	R_SH_LABEL */
88 	_RF_E,						/* 33	R_SH_DIR16 */
89 	_RF_E,						/* 34	R_SH_DIR8 */
90 	_RF_E,						/* 35	R_SH_DIR8UL */
91 	_RF_E,						/* 36	R_SH_DIR8UW */
92 	_RF_E,						/* 37	R_SH_DIR8U */
93 	_RF_E,						/* 38	R_SH_DIR8SW */
94 	_RF_E,						/* 39	R_SH_DIR8S */
95 	_RF_E,						/* 40	R_SH_DIR4UL */
96 	_RF_E,						/* 41	R_SH_DIR4UW */
97 	_RF_E,						/* 42	R_SH_DIR4U */
98 	_RF_E,						/* 43	R_SH_PSHA */
99 	_RF_E,						/* 44	R_SH_PSHL */
100 	_RF_E,						/* 45	R_SH_DIR5U */
101 	_RF_E,						/* 46	R_SH_DIR6U */
102 	_RF_E,						/* 47	R_SH_DIR6S */
103 	_RF_E,						/* 48	R_SH_DIR10S */
104 	_RF_E,						/* 49	R_SH_DIR10SW */
105 	_RF_E,						/* 50	R_SH_DIR10SL */
106 	_RF_E,						/* 51	R_SH_DIR10SQ */
107 	_RF_E,						/* 52	XXXX */
108 	_RF_E,						/* 53	R_SH_DIR16S */
109 	_RF_E,						/* 54	Unused */
110 	_RF_E,						/* 55	Unused */
111 	_RF_E,						/* 56	Unused */
112 	_RF_E,						/* 57	Unused */
113 	_RF_E,						/* 58	Unused */
114 	_RF_E,						/* 59	Unused */
115 	_RF_E,						/* 60	Unused */
116 	_RF_E,						/* 61	Unused */
117 	_RF_E,						/* 62	Unused */
118 	_RF_E,						/* 63	Unused */
119 	_RF_E,						/* 64	Unused */
120 	_RF_E,						/* 65	Unused */
121 	_RF_E,						/* 66	Unused */
122 	_RF_E,						/* 67	Unused */
123 	_RF_E,						/* 68	Unused */
124 	_RF_E,						/* 69	Unused */
125 	_RF_E,						/* 70	Unused */
126 	_RF_E,						/* 71	Unused */
127 	_RF_E,						/* 72	Unused */
128 	_RF_E,						/* 73	Unused */
129 	_RF_E,						/* 74	Unused */
130 	_RF_E,						/* 75	Unused */
131 	_RF_E,						/* 76	Unused */
132 	_RF_E,						/* 77	Unused */
133 	_RF_E,						/* 78	Unused */
134 	_RF_E,						/* 79	Unused */
135 	_RF_E,						/* 80	Unused */
136 	_RF_E,						/* 81	Unused */
137 	_RF_E,						/* 82	Unused */
138 	_RF_E,						/* 83	Unused */
139 	_RF_E,						/* 84	Unused */
140 	_RF_E,						/* 85	Unused */
141 	_RF_E,						/* 86	Unused */
142 	_RF_E,						/* 87	Unused */
143 	_RF_E,						/* 88	Unused */
144 	_RF_E,						/* 89	Unused */
145 	_RF_E,						/* 90	Unused */
146 	_RF_E,						/* 91	Unused */
147 	_RF_E,						/* 92	Unused */
148 	_RF_E,						/* 93	Unused */
149 	_RF_E,						/* 94	Unused */
150 	_RF_E,						/* 95	Unused */
151 	_RF_E,						/* 96	Unused */
152 	_RF_E,						/* 97	Unused */
153 	_RF_E,						/* 98	Unused */
154 	_RF_E,						/* 99	Unused */
155 	_RF_E,						/* 100	Unused */
156 	_RF_E,						/* 101	Unused */
157 	_RF_E,						/* 102	Unused */
158 	_RF_E,						/* 103	Unused */
159 	_RF_E,						/* 104	Unused */
160 	_RF_E,						/* 105	Unused */
161 	_RF_E,						/* 106	Unused */
162 	_RF_E,						/* 107	Unused */
163 	_RF_E,						/* 108	Unused */
164 	_RF_E,						/* 109	Unused */
165 	_RF_E,						/* 110	Unused */
166 	_RF_E,						/* 111	Unused */
167 	_RF_E,						/* 112	Unused */
168 	_RF_E,						/* 113	Unused */
169 	_RF_E,						/* 114	Unused */
170 	_RF_E,						/* 115	Unused */
171 	_RF_E,						/* 116	Unused */
172 	_RF_E,						/* 117	Unused */
173 	_RF_E,						/* 118	Unused */
174 	_RF_E,						/* 119	Unused */
175 	_RF_E,						/* 120	Unused */
176 	_RF_E,						/* 121	Unused */
177 	_RF_E,						/* 122	Unused */
178 	_RF_E,						/* 123	Unused */
179 	_RF_E,						/* 124	Unused */
180 	_RF_E,						/* 125	Unused */
181 	_RF_E,						/* 126	Unused */
182 	_RF_E,						/* 127	Unused */
183 	_RF_E,						/* 128	Unused */
184 	_RF_E,						/* 129	Unused */
185 	_RF_E,						/* 130	Unused */
186 	_RF_E,						/* 131	Unused */
187 	_RF_E,						/* 132	Unused */
188 	_RF_E,						/* 133	Unused */
189 	_RF_E,						/* 134	Unused */
190 	_RF_E,						/* 135	Unused */
191 	_RF_E,						/* 136	Unused */
192 	_RF_E,						/* 137	Unused */
193 	_RF_E,						/* 138	Unused */
194 	_RF_E,						/* 139	Unused */
195 	_RF_E,						/* 140	Unused */
196 	_RF_E,						/* 141	Unused */
197 	_RF_E,						/* 142	Unused */
198 	_RF_E,						/* 143	Unused */
199 	_RF_E,						/* 144	R_SH_TLS_GD_32 */
200 	_RF_E,						/* 145	R_SH_TLS_LD_32 */
201 	_RF_E,						/* 146	R_SH_TLS_LDO_32 */
202 	_RF_E,						/* 147	R_SH_TLS_IE_32 */
203 	_RF_E,						/* 148	R_SH_TLS_LE_32 */
204 	_RF_E,						/* 149	R_SH_TLS_DTPMOD32 */
205 	_RF_E,						/* 150	R_SH_TLS_DTPOFF32 */
206 	_RF_E,						/* 151	R_SH_TLS_TPOFF32 */
207 	_RF_E,						/* 152 Unused */
208 	_RF_E,						/* 153 Unused */
209 	_RF_E,						/* 154 Unused */
210 	_RF_E,						/* 155 Unused */
211 	_RF_E,						/* 156 Unused */
212 	_RF_E,						/* 157 Unused */
213 	_RF_E,						/* 158 Unused */
214 	_RF_E,						/* 159 Unused */
215 	_RF_E,						/* 160	R_SH_GOT32 */
216 	_RF_E,						/* 161	R_SH_PLT32 */
217 	_RF_S|			_RF_SZ(32) | _RF_RS(0),	/* 162	COPY */
218 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),	/* 163	GLOB_DAT */
219 	_RF_S|			_RF_SZ(32) | _RF_RS(0),	/* 164	JMP_SLOT */
220 	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),	/* 165 RELATIVE */
221 	_RF_E,						/* 166	R_SH_GOTOFF */
222 	_RF_E,						/* 167	R_SH_GOTPC */
223 	_RF_E,						/* 168	R_SH_GOTPLT32 */
224 	_RF_E,						/* 169	R_SH_GOT_LOW16 */
225 	_RF_E,						/* 170	R_SH_GOT_MEDLOW16 */
226 	_RF_E,						/* 171	R_SH_GOT_MEDHI16 */
227 	_RF_E,						/* 172	R_SH_GOT_HI16 */
228 	_RF_E,						/* 173	R_SH_GOTPLT_LOW16 */
229 	_RF_E,						/* 174	R_SH_GOTPLT_MEDLOW16 */
230 	_RF_E,						/* 175	R_SH_GOTPLT_MEDHI16 */
231 	_RF_E,						/* 176	R_SH_GOTPLT_HI16 */
232 	_RF_E,						/* 177	R_SH_PLT_LOW16 */
233 	_RF_E,						/* 178	R_SH_PLT_MEDLOW16 */
234 	_RF_E,						/* 179	R_SH_PLT_MEDHI16 */
235 	_RF_E,						/* 180	R_SH_PLT_HI16 */
236 	_RF_E,						/* 181	R_SH_GOTOFF_LOW16 */
237 	_RF_E,						/* 182	R_SH_GOTOFF_MEDLOW16 */
238 	_RF_E,						/* 183	R_SH_GOTOFF_MEDHI16 */
239 	_RF_E,						/* 184	R_SH_GOTOFF_HI16 */
240 	_RF_E,						/* 185	R_SH_GOTPC_LOW16 */
241 	_RF_E,						/* 186	R_SH_GOTPC_MEDLOW16 */
242 	_RF_E,						/* 187	R_SH_GOTPC_MEDHI16 */
243 	_RF_E,						/* 188	R_SH_GOTPC_HI16 */
244 	_RF_E,						/* 189	R_SH_GOT10BY4 */
245 	_RF_E,						/* 190	R_SH_GOTPLT10BY4 */
246 	_RF_E,						/* 191	R_SH_GOT10BY8 */
247 	_RF_E,						/* 192	R_SH_GOTPLT10BY8 */
248 #ifdef SH_SUPPORT_64_BIT
249 	_RF_E,						/* 193	R_SH_COPY64 */
250 	_RF_E,						/* 194	R_SH_GLOB_DAT64 */
251 	_RF_E,						/* 195	R_SH_JMP_SLOT64 */
252 	_RF_E,						/* 196	R_SH_RELATIVE64 */
253 	_RF_E,						/* 197	Unused */
254 	_RF_E,						/* 198	Unused */
255 	_RF_E,						/* 199	Unused */
256 	_RF_E,						/* 200	Unused */
257 	_RF_E,						/* 201	Unused */
258 	_RF_E,						/* 202	Unused */
259 	_RF_E,						/* 203	Unused */
260 	_RF_E,						/* 204	Unused */
261 	_RF_E,						/* 205	Unused */
262 	_RF_E,						/* 206	Unused */
263 	_RF_E,						/* 207	Unused */
264 	_RF_E,						/* 208	Unused */
265 	_RF_E,						/* 209	Unused */
266 	_RF_E,						/* 210	Unused */
267 	_RF_E,						/* 211	Unused */
268 	_RF_E,						/* 212	Unused */
269 	_RF_E,						/* 213	Unused */
270 	_RF_E,						/* 214	Unused */
271 	_RF_E,						/* 215	Unused */
272 	_RF_E,						/* 216	Unused */
273 	_RF_E,						/* 217	Unused */
274 	_RF_E,						/* 218	Unused */
275 	_RF_E,						/* 219	Unused */
276 	_RF_E,						/* 220	Unused */
277 	_RF_E,						/* 221	Unused */
278 	_RF_E,						/* 222	Unused */
279 	_RF_E,						/* 223	Unused */
280 	_RF_E,						/* 224	Unused */
281 	_RF_E,						/* 225	Unused */
282 	_RF_E,						/* 226	Unused */
283 	_RF_E,						/* 227	Unused */
284 	_RF_E,						/* 228	Unused */
285 	_RF_E,						/* 229	Unused */
286 	_RF_E,						/* 230	Unused */
287 	_RF_E,						/* 231	Unused */
288 	_RF_E,						/* 232	Unused */
289 	_RF_E,						/* 233	Unused */
290 	_RF_E,						/* 234	Unused */
291 	_RF_E,						/* 235	Unused */
292 	_RF_E,						/* 236	Unused */
293 	_RF_E,						/* 237	Unused */
294 	_RF_E,						/* 238	Unused */
295 	_RF_E,						/* 239	Unused */
296 	_RF_E,						/* 240	Unused */
297 	_RF_E,						/* 241	Unused */
298 	_RF_E,						/* 242	R_SH_SHMEDIA_CODE */
299 	_RF_E,						/* 243	R_SH_PT_16 */
300 	_RF_E,						/* 244	R_SH_IMMS16 */
301 	_RF_E,						/* 245	R_SH_IMMU16 */
302 	_RF_E,						/* 246	R_SH_IMM_LOW16 */
303 	_RF_E,						/* 247	R_SH_IMM_LOW16_PCREL */
304 	_RF_E,						/* 248	R_SH_IMM_MEDLOW16 */
305 	_RF_E,						/* 249	R_SH_IMM_MEDLOW16_PCREL */
306 	_RF_E,						/* 250	R_SH_IMM_MEDHI16 */
307 	_RF_E,						/* 251	R_SH_IMM_MEDHI16_PCREL */
308 	_RF_E,						/* 252	R_SH_IMM_HI16 */
309 	_RF_E,						/* 253	R_SH_IMM_HI16_PCREL */
310 	_RF_E,						/* 254	R_SH_64 */
311 	_RF_E,						/* 255	R_SH_64_PCREL */
312 	0
313 #endif
314 };
315 
316 #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
317 #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
318 #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
319 #define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
320 #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
321 #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
322 #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
323 static int reloc_target_bitmask[] = {
324 #define _BM(x)  (x == 32? ~0 : ~(-(1UL << (x))))
325 	_BM(0),		/* 0	R_SH_NONE */
326 	_BM(32),	/* 1	R_SH_DIR32 */
327 	_BM(32),	/* 2	R_SH_REL32 */
328 	_BM(8),		/* 3	R_SH_DIR8WPN */
329 	_BM(12),	/* 4	R_SH_IND12W */
330 	_BM(8),		/* 5	R_SH_DIR8WPL */
331 	_BM(8),		/* 6	R_SH_DIR8WPZ */
332 	_BM(8),		/* 7	R_SH_DIR8BP */
333 	_BM(8),		/* 8	R_SH_DIR8W */
334 	_BM(8),		/* 9	R_SH_DIR8L */
335 	_BM(0),		/* 10	R_SH_LOOP_START */
336 	_BM(0),		/* 11	R_SH_LOOP_END */
337 	_BM(0),		/* 12	Unused */
338 	_BM(0),		/* 13	Unused */
339 	_BM(0),		/* 14	Unused */
340 	_BM(0),		/* 15	Unused */
341 	_BM(0),		/* 16	Unused */
342 	_BM(0),		/* 17	Unused */
343 	_BM(0),		/* 18	Unused */
344 	_BM(0),		/* 19	Unused */
345 	_BM(0),		/* 20	Unused */
346 	_BM(0),		/* 21	Unused */
347 	_BM(0),		/* 22	R_SH_GNU_VTINHERIT */
348 	_BM(0),		/* 23	R_SH_GNU_VTENTRY */
349 	_BM(0),		/* 24	R_SH_SWITCH8 */
350 	_BM(0),		/* 25	R_SH_SWITCH16 */
351 	_BM(0),		/* 26	R_SH_SWITCH32 */
352 	_BM(0),		/* 27	R_SH_USES */
353 	_BM(0),		/* 28	R_SH_COUNT */
354 	_BM(0),		/* 29	R_SH_ALIGN */
355 	_BM(0),		/* 30	R_SH_CODE */
356 	_BM(0),		/* 31	R_SH_DATA */
357 	_BM(0),		/* 32	R_SH_LABEL */
358 	_BM(0),		/* 33	R_SH_DIR16 */
359 	_BM(0),		/* 34	R_SH_DIR8 */
360 	_BM(0),		/* 35	R_SH_DIR8UL */
361 	_BM(0),		/* 36	R_SH_DIR8UW */
362 	_BM(0),		/* 37	R_SH_DIR8U */
363 	_BM(0),		/* 38	R_SH_DIR8SW */
364 	_BM(0),		/* 39	R_SH_DIR8S */
365 	_BM(0),		/* 40	R_SH_DIR4UL */
366 	_BM(0),		/* 41	R_SH_DIR4UW */
367 	_BM(0),		/* 42	R_SH_DIR4U */
368 	_BM(0),		/* 43	R_SH_PSHA */
369 	_BM(0),		/* 44	R_SH_PSHL */
370 	_BM(0),		/* 45	R_SH_DIR5U */
371 	_BM(0),		/* 46	R_SH_DIR6U */
372 	_BM(0),		/* 47	R_SH_DIR6S */
373 	_BM(0),		/* 48	R_SH_DIR10S */
374 	_BM(0),		/* 49	R_SH_DIR10SW */
375 	_BM(0),		/* 50	R_SH_DIR10SL */
376 	_BM(0),		/* 51	R_SH_DIR10SQ */
377 	_BM(0),		/* 52	xxx */
378 	_BM(0),		/* 53	R_SH_DIR16S */
379 	_BM(0),		/* 54	Unused */
380 	_BM(0),		/* 55	Unused */
381 	_BM(0),		/* 56	Unused */
382 	_BM(0),		/* 57	Unused */
383 	_BM(0),		/* 58	Unused */
384 	_BM(0),		/* 59	Unused */
385 	_BM(0),		/* 60	Unused */
386 	_BM(0),		/* 61	Unused */
387 	_BM(0),		/* 62	Unused */
388 	_BM(0),		/* 63	Unused */
389 	_BM(0),		/* 64	Unused */
390 	_BM(0),		/* 65	Unused */
391 	_BM(0),		/* 66	Unused */
392 	_BM(0),		/* 67	Unused */
393 	_BM(0),		/* 68	Unused */
394 	_BM(0),		/* 69	Unused */
395 	_BM(0),		/* 70	Unused */
396 	_BM(0),		/* 71	Unused */
397 	_BM(0),		/* 72	Unused */
398 	_BM(0),		/* 73	Unused */
399 	_BM(0),		/* 74	Unused */
400 	_BM(0),		/* 75	Unused */
401 	_BM(0),		/* 76	Unused */
402 	_BM(0),		/* 77	Unused */
403 	_BM(0),		/* 78	Unused */
404 	_BM(0),		/* 79	Unused */
405 	_BM(0),		/* 80	Unused */
406 	_BM(0),		/* 81	Unused */
407 	_BM(0),		/* 82	Unused */
408 	_BM(0),		/* 83	Unused */
409 	_BM(0),		/* 84	Unused */
410 	_BM(0),		/* 85	Unused */
411 	_BM(0),		/* 86	Unused */
412 	_BM(0),		/* 87	Unused */
413 	_BM(0),		/* 88	Unused */
414 	_BM(0),		/* 89	Unused */
415 	_BM(0),		/* 90	Unused */
416 	_BM(0),		/* 91	Unused */
417 	_BM(0),		/* 92	Unused */
418 	_BM(0),		/* 93	Unused */
419 	_BM(0),		/* 94	Unused */
420 	_BM(0),		/* 95	Unused */
421 	_BM(0),		/* 96	Unused */
422 	_BM(0),		/* 97	Unused */
423 	_BM(0),		/* 98	Unused */
424 	_BM(0),		/* 99	Unused */
425 	_BM(0),		/* 100	Unused */
426 	_BM(0),		/* 101	Unused */
427 	_BM(0),		/* 102	Unused */
428 	_BM(0),		/* 103	Unused */
429 	_BM(0),		/* 104	Unused */
430 	_BM(0),		/* 105	Unused */
431 	_BM(0),		/* 106	Unused */
432 	_BM(0),		/* 107	Unused */
433 	_BM(0),		/* 108	Unused */
434 	_BM(0),		/* 109	Unused */
435 	_BM(0),		/* 110	Unused */
436 	_BM(0),		/* 111	Unused */
437 	_BM(0),		/* 112	Unused */
438 	_BM(0),		/* 113	Unused */
439 	_BM(0),		/* 114	Unused */
440 	_BM(0),		/* 115	Unused */
441 	_BM(0),		/* 116	Unused */
442 	_BM(0),		/* 117	Unused */
443 	_BM(0),		/* 118	Unused */
444 	_BM(0),		/* 119	Unused */
445 	_BM(0),		/* 120	Unused */
446 	_BM(0),		/* 121	Unused */
447 	_BM(0),		/* 122	Unused */
448 	_BM(0),		/* 123	Unused */
449 	_BM(0),		/* 124	Unused */
450 	_BM(0),		/* 125	Unused */
451 	_BM(0),		/* 126	Unused */
452 	_BM(0),		/* 127	Unused */
453 	_BM(0),		/* 128	Unused */
454 	_BM(0),		/* 129	Unused */
455 	_BM(0),		/* 130	Unused */
456 	_BM(0),		/* 131	Unused */
457 	_BM(0),		/* 132	Unused */
458 	_BM(0),		/* 133	Unused */
459 	_BM(0),		/* 134	Unused */
460 	_BM(0),		/* 135	Unused */
461 	_BM(0),		/* 136	Unused */
462 	_BM(0),		/* 137	Unused */
463 	_BM(0),		/* 138	Unused */
464 	_BM(0),		/* 139	Unused */
465 	_BM(0),		/* 140	Unused */
466 	_BM(0),		/* 141	Unused */
467 	_BM(0),		/* 142	Unused */
468 	_BM(0),		/* 143	Unused */
469 	_BM(0),		/* 144	R_SH_TLS_GD_32 */
470 	_BM(0),		/* 145	R_SH_TLS_LD_32 */
471 	_BM(0),		/* 146	R_SH_TLS_LDO_32 */
472 	_BM(0),		/* 147	R_SH_TLS_IE_32 */
473 	_BM(0),		/* 148	R_SH_TLS_LE_32 */
474 	_BM(0),		/* 149	R_SH_TLS_DTPMOD32 */
475 	_BM(0),		/* 150	R_SH_TLS_DTPOFF32 */
476 	_BM(0),		/* 151	R_SH_TLS_TPOFF32 */
477 	_BM(0),		/* 152  xxx */
478 	_BM(0),		/* 153  xxx */
479 	_BM(0),		/* 154  xxx */
480 	_BM(0),		/* 155  xxx */
481 	_BM(0),		/* 156  xxx */
482 	_BM(0),		/* 157  xxx */
483 	_BM(0),		/* 158  xxx */
484 	_BM(0),		/* 159  xxx */
485 	_BM(0),		/* 160	R_SH_GOT32 */
486 	_BM(0),		/* 161	R_SH_PLT32 */
487 	_BM(0),		/* 162	R_SH_COPY */
488 	_BM(32),	/* 163	R_SH_GLOB_DAT */
489 	_BM(0),		/* 164	R_SH_JMP_SLOT */
490 	_BM(32),	/* 165	R_SH_RELATIVE */
491 	_BM(0),		/* 166	R_SH_GOTOFF */
492 	_BM(0),		/* 167	R_SH_GOTPC */
493 	_BM(0),		/* 168	R_SH_GOTPLT32 */
494 	_BM(0),		/* 169	R_SH_GOT_LOW16 */
495 	_BM(0),		/* 170	R_SH_GOT_MEDLOW16 */
496 	_BM(0),		/* 171	R_SH_GOT_MEDHI16 */
497 	_BM(0),		/* 172	R_SH_GOT_HI16 */
498 	_BM(0),		/* 173	R_SH_GOTPLT_LOW16 */
499 	_BM(0),		/* 174	R_SH_GOTPLT_MEDLOW16 */
500 	_BM(0),		/* 175	R_SH_GOTPLT_MEDHI16 */
501 	_BM(0),		/* 176	R_SH_GOTPLT_HI16 */
502 	_BM(0),		/* 177	R_SH_PLT_LOW16 */
503 	_BM(0),		/* 178	R_SH_PLT_MEDLOW16 */
504 	_BM(0),		/* 179	R_SH_PLT_MEDHI16 */
505 	_BM(0),		/* 180	R_SH_PLT_HI16 */
506 	_BM(0),		/* 181	R_SH_GOTOFF_LOW16 */
507 	_BM(0),		/* 182	R_SH_GOTOFF_MEDLOW16 */
508 	_BM(0),		/* 183	R_SH_GOTOFF_MEDHI16 */
509 	_BM(0),		/* 184	R_SH_GOTOFF_HI16 */
510 	_BM(0),		/* 185	R_SH_GOTPC_LOW16 */
511 	_BM(0),		/* 186	R_SH_GOTPC_MEDLOW16 */
512 	_BM(0),		/* 187	R_SH_GOTPC_MEDHI16 */
513 	_BM(0),		/* 188	R_SH_GOTPC_HI16 */
514 	_BM(0),		/* 189	R_SH_GOT10BY4 */
515 	_BM(0),		/* 190	R_SH_GOTPLT10BY4 */
516 	_BM(0),		/* 191	R_SH_GOT10BY8 */
517 	_BM(0),		/* 192	R_SH_GOTPLT10BY8 */
518 #ifdef SH_SUPPORT_64_BIT
519 	_BM(0),		/* 193	R_SH_COPY64 */
520 	_BM(0),		/* 194	R_SH_GLOB_DAT64 */
521 	_BM(0),		/* 195	R_SH_JMP_SLOT64 */
522 	_BM(0),		/* 196	R_SH_RELATIVE64 */
523 	_BM(0),		/* 197 xxx */
524 	_BM(0),		/* 198 xxx */
525 	_BM(0),		/* 199 xxx */
526 	_BM(0),		/* 200 xxx */
527 	_BM(0),		/* 201 xxx */
528 	_BM(0),		/* 202 xxx */
529 	_BM(0),		/* 203 xxx */
530 	_BM(0),		/* 204 xxx */
531 	_BM(0),		/* 205 xxx */
532 	_BM(0),		/* 206 xxx */
533 	_BM(0),		/* 207 xxx */
534 	_BM(0),		/* 208 xxx */
535 	_BM(0),		/* 209 xxx */
536 	_BM(0),		/* 210 xxx */
537 	_BM(0),		/* 211 xxx */
538 	_BM(0),		/* 212 xxx */
539 	_BM(0),		/* 213 xxx */
540 	_BM(0),		/* 214 xxx */
541 	_BM(0),		/* 215 xxx */
542 	_BM(0),		/* 216 xxx */
543 	_BM(0),		/* 217 xxx */
544 	_BM(0),		/* 218 xxx */
545 	_BM(0),		/* 219 xxx */
546 	_BM(0),		/* 220 xxx */
547 	_BM(0),		/* 221 xxx */
548 	_BM(0),		/* 222 xxx */
549 	_BM(0),		/* 223 xxx */
550 	_BM(0),		/* 224 xxx */
551 	_BM(0),		/* 225 xxx */
552 	_BM(0),		/* 226 xxx */
553 	_BM(0),		/* 227 xxx */
554 	_BM(0),		/* 228 xxx */
555 	_BM(0),		/* 229  xxx */
556 	_BM(0),		/* 230 xxx */
557 	_BM(0),		/* 231 xxx */
558 	_BM(0),		/* 232 xxx */
559 	_BM(0),		/* 233 xxx */
560 	_BM(0),		/* 234 xxx */
561 	_BM(0),		/* 235 xxx */
562 	_BM(0),		/* 236 xxx */
563 	_BM(0),		/* 237 xxx */
564 	_BM(0),		/* 238 xxx */
565 	_BM(0),		/* 239 xxx */
566 	_BM(0),		/* 240 xxx */
567 	_BM(0),		/* 241 xxx */
568 	_BM(0),		/* 242	R_SH_SHMEDIA_CODE */
569 	_BM(0),		/* 243	R_SH_PT_16 */
570 	_BM(0),		/* 244	R_SH_IMMS16 */
571 	_BM(0),		/* 245	R_SH_IMMU16 */
572 	_BM(0),		/* 246	R_SH_IMM_LOW16 */
573 	_BM(0),		/* 247	R_SH_IMM_LOW16_PCREL */
574 	_BM(0),		/* 248	R_SH_IMM_MEDLOW16 */
575 	_BM(0),		/* 249	R_SH_IMM_MEDLOW16_PCREL */
576 	_BM(0),		/* 250	R_SH_IMM_MEDHI16 */
577 	_BM(0),		/* 251	R_SH_IMM_MEDHI16_PCREL */
578 	_BM(0),		/* 252	R_SH_IMM_HI16 */
579 	_BM(0),		/* 253	R_SH_IMM_HI16_PCREL */
580 	_BM(0),		/* 254	R_SH_64 */
581 	_BM(0),		/* 255	R_SH_64_PCREL */
582 #endif
583 #undef _BM
584 };
585 #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
586 
587 #define R_TYPE(x) R_SH_ ## x
588 
589 void _dl_reloc_plt(Elf_Word *where, Elf_Addr value, Elf_RelA *rel);
590 
591 void
592 _dl_reloc_plt(Elf_Word *where, Elf_Addr value, Elf_RelA *rel)
593 {
594 	*where = value + rel->r_addend;
595 }
596 
597 int
598 _dl_md_reloc(elf_object_t *object, int rel, int relasz)
599 {
600 	long	i;
601 	long	numrela;
602 	long	relrel;
603 	int	fails = 0;
604 	Elf_Addr loff;
605 	Elf_Addr prev_value = 0;
606 	const Elf_Sym *prev_sym = NULL;
607 	Elf_RelA *rels;
608 	struct load_list *llist;
609 
610 	loff = object->obj_base;
611 	numrela = object->Dyn.info[relasz] / sizeof(Elf_RelA);
612 	relrel = rel == DT_RELA ? object->relacount : 0;
613 	rels = (Elf_RelA *)(object->Dyn.info[rel]);
614 
615 	if (rels == NULL)
616 		return(0);
617 
618 	if (relrel > numrela) {
619 		_dl_printf("relacount > numrel: %ld > %ld\n", relrel, numrela);
620 		_dl_exit(20);
621 	}
622 
623 	/*
624 	 * unprotect some segments if we need it.
625 	 */
626 	if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) {
627 		for (llist = object->load_list;
628 		    llist != NULL;
629 		    llist = llist->next) {
630 			if (!(llist->prot & PROT_WRITE))
631 				_dl_mprotect(llist->start, llist->size,
632 				    llist->prot|PROT_WRITE);
633 		}
634 	}
635 
636 	/* tight loop for leading RELATIVE relocs */
637 	for (i = 0; i < relrel; i++, rels++) {
638 		Elf_Addr *where;
639 
640 #ifdef DEBUG
641 		if (ELF_R_TYPE(rels->r_info) != R_TYPE(RELATIVE)) {
642 			_dl_printf("RELACOUNT wrong\n");
643 			_dl_exit(20);
644 		}
645 #endif
646 		where = (Elf_Addr *)(rels->r_offset + loff);
647 		*where = rels->r_addend + loff;
648 	}
649 	for (; i < numrela; i++, rels++) {
650 		Elf_Addr *where, value, ooff, mask;
651 		Elf_Word type;
652 		const Elf_Sym *sym, *this;
653 		const char *symn;
654 
655 		type = ELF_R_TYPE(rels->r_info);
656 
657 		if (reloc_target_flags[type] & _RF_E) {
658 			_dl_printf(" bad relocation obj %s %d %d\n", object->load_name, i, type);
659 			_dl_exit(1);
660 		}
661 		if (type == R_TYPE(NONE))
662 			continue;
663 
664 		if (type == R_TYPE(JMP_SLOT) && rel != DT_JMPREL)
665 			continue;
666 
667 		where = (Elf_Addr *)(rels->r_offset + loff);
668 
669 		if (RELOC_USE_ADDEND(type))
670 #ifdef LDSO_ARCH_IS_RELA_
671 			value = rels->r_addend;
672 #else
673 			value = *where & RELOC_VALUE_BITMASK(type);
674 #endif
675 		else
676 			value = 0;
677 
678 
679 		sym = NULL;
680 		symn = NULL;
681 		if (RELOC_RESOLVE_SYMBOL(type)) {
682 			sym = object->dyn.symtab;
683 			sym += ELF_R_SYM(rels->r_info);
684 			symn = object->dyn.strtab + sym->st_name;
685 
686 			if (sym->st_shndx != SHN_UNDEF &&
687 			    ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
688 				value += loff;
689 			} else if (sym == prev_sym) {
690 				value += prev_value;
691 			} else {
692 				this = NULL;
693 #if 1
694 				ooff = _dl_find_symbol_bysym(object,
695 				    ELF_R_SYM(rels->r_info), &this,
696 				    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
697 				    ((type == R_TYPE(JMP_SLOT)) ?
698 					SYM_PLT : SYM_NOTPLT),
699 				    sym, NULL);
700 #else
701 				ooff = _dl_find_symbol_bysym(object,
702 				    ELF_R_SYM(rels->r_info), &this,
703 				    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
704 				    SYM_PLT,
705 				    sym, NULL);
706 #endif
707 				if (this == NULL) {
708 resolve_failed:
709 					if (ELF_ST_BIND(sym->st_info) !=
710 					    STB_WEAK)
711 						fails++;
712 					continue;
713 				}
714 				prev_sym = sym;
715 				prev_value = (Elf_Addr)(ooff + this->st_value);
716 				value += prev_value;
717 			}
718 		}
719 
720 		if (type == R_TYPE(JMP_SLOT)) {
721 			_dl_reloc_plt((Elf_Word *)where, value, rels);
722 			continue;
723 		}
724 
725 		if (type == R_TYPE(COPY)) {
726 			void *dstaddr = where;
727 			const void *srcaddr;
728 			const Elf_Sym *dstsym = sym, *srcsym = NULL;
729 			Elf_Addr soff;
730 
731 			soff = _dl_find_symbol(symn, &srcsym,
732 			    SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT,
733 			    dstsym, object, NULL);
734 			if (srcsym == NULL)
735 				goto resolve_failed;
736 
737 			srcaddr = (void *)(soff + srcsym->st_value);
738 			_dl_bcopy(srcaddr, dstaddr, dstsym->st_size);
739 			continue;
740 		}
741 
742 		if (RELOC_PC_RELATIVE(type))
743 			value -= (Elf_Addr)where;
744 		if (RELOC_BASE_RELATIVE(type))
745 			value += loff;
746 
747 		mask = RELOC_VALUE_BITMASK(type);
748 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
749 		value &= mask;
750 
751 		if (RELOC_UNALIGNED(type)) {
752 			/* Handle unaligned relocations. */
753 			Elf_Addr tmp = 0;
754 			char *ptr = (char *)where;
755 			int i, size = RELOC_TARGET_SIZE(type)/8;
756 
757 			/* Read it in one byte at a time. */
758 			for (i=0; i<size; i++)
759 				tmp = (tmp << 8) | ptr[i];
760 
761 			tmp &= ~mask;
762 			tmp |= value;
763 
764 			/* Write it back out. */
765 			for (i=0; i<size; i++)
766 				ptr[i] = ((tmp >> (8*i)) & 0xff);
767 		} else {
768 			*where &= ~mask;
769 			*where |= value;
770 		}
771 	}
772 
773 	/* reprotect the unprotected segments */
774 	if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) {
775 		for (llist = object->load_list;
776 		    llist != NULL;
777 		    llist = llist->next) {
778 			if (!(llist->prot & PROT_WRITE))
779 				_dl_mprotect(llist->start, llist->size,
780 				    llist->prot);
781 		}
782 	}
783 
784 	return (fails);
785 }
786 
787 /*
788  *	Relocate the Global Offset Table (GOT).
789  *	This is done by calling _dl_md_reloc on DT_JUMPREL for DL_BIND_NOW,
790  *	otherwise the lazy binding plt initialization is performed.
791  */
792 int
793 _dl_md_reloc_got(elf_object_t *object, int lazy)
794 {
795 	int	fails = 0;
796 	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
797 	Elf_Addr ooff;
798 	const Elf_Sym *this;
799 	int i, num;
800 	Elf_RelA *rel;
801 
802 	if (object->Dyn.info[DT_PLTREL] != DT_RELA)
803 		return (0);
804 
805 	object->got_addr = 0;
806 	object->got_size = 0;
807 	this = NULL;
808 	ooff = _dl_find_symbol("__got_start", &this,
809 	    SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, object, NULL);
810 	if (this != NULL)
811 		object->got_addr = ooff + this->st_value;
812 
813 	this = NULL;
814 	ooff = _dl_find_symbol("__got_end", &this,
815 	    SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, object, NULL);
816 	if (this != NULL)
817 		object->got_size = ooff + this->st_value  - object->got_addr;
818 
819 	if (object->got_addr == 0)
820 		object->got_start = 0;
821 	else {
822 		object->got_start = ELF_TRUNC(object->got_addr, _dl_pagesz);
823 		object->got_size += object->got_addr - object->got_start;
824 		object->got_size = ELF_ROUND(object->got_size, _dl_pagesz);
825 	}
826 	object->plt_start = 0;
827 	object->plt_size = 0;
828 
829 	if (object->traced)
830 		lazy = 1;
831 
832 	if (!lazy) {
833 		fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
834 	} else {
835 		rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]);
836 		num = (object->Dyn.info[DT_PLTRELSZ]) / sizeof(Elf_RelA);
837 
838 		for (i = 0; i < num; i++, rel++) {
839 			Elf_Addr *where, value;
840 			Elf_Word type;
841 
842 			where = (Elf_Addr *)(rel->r_offset + object->obj_base);
843 			type = ELF_R_TYPE(rel->r_info);
844 			if (RELOC_USE_ADDEND(type))
845 				value = rel->r_addend;
846 			else
847 				value = 0;
848 			*where += object->obj_base + value;
849 		}
850 
851 		pltgot[1] = (Elf_Addr)object;
852 		pltgot[2] = (Elf_Addr)_dl_bind_start;
853 	}
854 	if (object->got_size != 0)
855 		_dl_mprotect((void*)object->got_start, object->got_size,
856 		    PROT_READ);
857 
858 	return (fails);
859 }
860 
861 Elf_Addr
862 _dl_bind(elf_object_t *object, int reloff)
863 {
864 	Elf_RelA *rel;
865 	Elf_Word *addr;
866 	const Elf_Sym *sym, *this;
867 	const char *symn;
868 	const elf_object_t *sobj;
869 	Elf_Addr ooff, newval;
870 	sigset_t savedmask;
871 
872 	rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL] + reloff);
873 
874 	sym = object->dyn.symtab;
875 	sym += ELF_R_SYM(rel->r_info);
876 	symn = object->dyn.strtab + sym->st_name;
877 
878 	this = NULL;
879 	ooff = _dl_find_symbol(symn,  &this,
880 	    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, sym, object, &sobj);
881 	if (this == NULL) {
882 		_dl_printf("lazy binding failed!\n");
883 		*(volatile int *)0 = 0;		/* XXX */
884 	}
885 
886 	newval = ooff + this->st_value;
887 
888 	if (sobj->traced && _dl_trace_plt(sobj, symn))
889 		return newval;
890 
891 	addr = (Elf_Addr *)(object->obj_base + rel->r_offset);
892 
893 	/* if GOT is protected, allow the write */
894 	if (object->got_size != 0) {
895 		_dl_thread_bind_lock(0, &savedmask);
896 		_dl_mprotect((void*)object->got_start, object->got_size,
897 		    PROT_READ|PROT_WRITE);
898 	}
899 
900 	if (*addr != newval)
901 		*addr = newval;
902 
903 	/* put the GOT back to RO */
904 	if (object->got_size != 0) {
905 		_dl_mprotect((void*)object->got_start, object->got_size,
906 		    PROT_READ);
907 		_dl_thread_bind_lock(1, &savedmask);
908 	}
909 	return newval;
910 }
911