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