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