1 /* $OpenBSD: rtld_machine.c,v 1.36 2023/01/29 20:30:21 gnezdo 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/exec_elf.h>
34 #include <sys/syscall.h>
35 #include <sys/unistd.h>
36
37 #include <machine/reloc.h>
38
39 #include "util.h"
40 #include "resolve.h"
41
42 int64_t pcookie __attribute__((section(".openbsd.randomdata"))) __dso_hidden;
43
44 void _dl_bind_start(void); /* XXX */
45 Elf_Addr _dl_bind(elf_object_t *object, int reloff);
46 #define _RF_S 0x80000000 /* Resolve symbol */
47 #define _RF_A 0x40000000 /* Use addend */
48 #define _RF_P 0x20000000 /* Location relative */
49 #define _RF_G 0x10000000 /* GOT offset */
50 #define _RF_B 0x08000000 /* Load address relative */
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 const 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 };
222
223 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
224 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
225 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
226 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0)
227 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
228 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
229 #define RELOC_ERROR(t) \
230 ((t) >= nitems(reloc_target_flags) || (reloc_target_flags[t] & _RF_E))
231
232 static const int reloc_target_bitmask[] = {
233 #define _BM(x) (x == 32? ~0 : ~(-(1UL << (x))))
234 _BM(0), /* 0 R_SH_NONE */
235 _BM(32), /* 1 R_SH_DIR32 */
236 _BM(32), /* 2 R_SH_REL32 */
237 _BM(8), /* 3 R_SH_DIR8WPN */
238 _BM(12), /* 4 R_SH_IND12W */
239 _BM(8), /* 5 R_SH_DIR8WPL */
240 _BM(8), /* 6 R_SH_DIR8WPZ */
241 _BM(8), /* 7 R_SH_DIR8BP */
242 _BM(8), /* 8 R_SH_DIR8W */
243 _BM(8), /* 9 R_SH_DIR8L */
244 _BM(0), /* 10 R_SH_LOOP_START */
245 _BM(0), /* 11 R_SH_LOOP_END */
246 _BM(0), /* 12 Unused */
247 _BM(0), /* 13 Unused */
248 _BM(0), /* 14 Unused */
249 _BM(0), /* 15 Unused */
250 _BM(0), /* 16 Unused */
251 _BM(0), /* 17 Unused */
252 _BM(0), /* 18 Unused */
253 _BM(0), /* 19 Unused */
254 _BM(0), /* 20 Unused */
255 _BM(0), /* 21 Unused */
256 _BM(0), /* 22 R_SH_GNU_VTINHERIT */
257 _BM(0), /* 23 R_SH_GNU_VTENTRY */
258 _BM(0), /* 24 R_SH_SWITCH8 */
259 _BM(0), /* 25 R_SH_SWITCH16 */
260 _BM(0), /* 26 R_SH_SWITCH32 */
261 _BM(0), /* 27 R_SH_USES */
262 _BM(0), /* 28 R_SH_COUNT */
263 _BM(0), /* 29 R_SH_ALIGN */
264 _BM(0), /* 30 R_SH_CODE */
265 _BM(0), /* 31 R_SH_DATA */
266 _BM(0), /* 32 R_SH_LABEL */
267 _BM(0), /* 33 R_SH_DIR16 */
268 _BM(0), /* 34 R_SH_DIR8 */
269 _BM(0), /* 35 R_SH_DIR8UL */
270 _BM(0), /* 36 R_SH_DIR8UW */
271 _BM(0), /* 37 R_SH_DIR8U */
272 _BM(0), /* 38 R_SH_DIR8SW */
273 _BM(0), /* 39 R_SH_DIR8S */
274 _BM(0), /* 40 R_SH_DIR4UL */
275 _BM(0), /* 41 R_SH_DIR4UW */
276 _BM(0), /* 42 R_SH_DIR4U */
277 _BM(0), /* 43 R_SH_PSHA */
278 _BM(0), /* 44 R_SH_PSHL */
279 _BM(0), /* 45 R_SH_DIR5U */
280 _BM(0), /* 46 R_SH_DIR6U */
281 _BM(0), /* 47 R_SH_DIR6S */
282 _BM(0), /* 48 R_SH_DIR10S */
283 _BM(0), /* 49 R_SH_DIR10SW */
284 _BM(0), /* 50 R_SH_DIR10SL */
285 _BM(0), /* 51 R_SH_DIR10SQ */
286 _BM(0), /* 52 xxx */
287 _BM(0), /* 53 R_SH_DIR16S */
288 _BM(0), /* 54 Unused */
289 _BM(0), /* 55 Unused */
290 _BM(0), /* 56 Unused */
291 _BM(0), /* 57 Unused */
292 _BM(0), /* 58 Unused */
293 _BM(0), /* 59 Unused */
294 _BM(0), /* 60 Unused */
295 _BM(0), /* 61 Unused */
296 _BM(0), /* 62 Unused */
297 _BM(0), /* 63 Unused */
298 _BM(0), /* 64 Unused */
299 _BM(0), /* 65 Unused */
300 _BM(0), /* 66 Unused */
301 _BM(0), /* 67 Unused */
302 _BM(0), /* 68 Unused */
303 _BM(0), /* 69 Unused */
304 _BM(0), /* 70 Unused */
305 _BM(0), /* 71 Unused */
306 _BM(0), /* 72 Unused */
307 _BM(0), /* 73 Unused */
308 _BM(0), /* 74 Unused */
309 _BM(0), /* 75 Unused */
310 _BM(0), /* 76 Unused */
311 _BM(0), /* 77 Unused */
312 _BM(0), /* 78 Unused */
313 _BM(0), /* 79 Unused */
314 _BM(0), /* 80 Unused */
315 _BM(0), /* 81 Unused */
316 _BM(0), /* 82 Unused */
317 _BM(0), /* 83 Unused */
318 _BM(0), /* 84 Unused */
319 _BM(0), /* 85 Unused */
320 _BM(0), /* 86 Unused */
321 _BM(0), /* 87 Unused */
322 _BM(0), /* 88 Unused */
323 _BM(0), /* 89 Unused */
324 _BM(0), /* 90 Unused */
325 _BM(0), /* 91 Unused */
326 _BM(0), /* 92 Unused */
327 _BM(0), /* 93 Unused */
328 _BM(0), /* 94 Unused */
329 _BM(0), /* 95 Unused */
330 _BM(0), /* 96 Unused */
331 _BM(0), /* 97 Unused */
332 _BM(0), /* 98 Unused */
333 _BM(0), /* 99 Unused */
334 _BM(0), /* 100 Unused */
335 _BM(0), /* 101 Unused */
336 _BM(0), /* 102 Unused */
337 _BM(0), /* 103 Unused */
338 _BM(0), /* 104 Unused */
339 _BM(0), /* 105 Unused */
340 _BM(0), /* 106 Unused */
341 _BM(0), /* 107 Unused */
342 _BM(0), /* 108 Unused */
343 _BM(0), /* 109 Unused */
344 _BM(0), /* 110 Unused */
345 _BM(0), /* 111 Unused */
346 _BM(0), /* 112 Unused */
347 _BM(0), /* 113 Unused */
348 _BM(0), /* 114 Unused */
349 _BM(0), /* 115 Unused */
350 _BM(0), /* 116 Unused */
351 _BM(0), /* 117 Unused */
352 _BM(0), /* 118 Unused */
353 _BM(0), /* 119 Unused */
354 _BM(0), /* 120 Unused */
355 _BM(0), /* 121 Unused */
356 _BM(0), /* 122 Unused */
357 _BM(0), /* 123 Unused */
358 _BM(0), /* 124 Unused */
359 _BM(0), /* 125 Unused */
360 _BM(0), /* 126 Unused */
361 _BM(0), /* 127 Unused */
362 _BM(0), /* 128 Unused */
363 _BM(0), /* 129 Unused */
364 _BM(0), /* 130 Unused */
365 _BM(0), /* 131 Unused */
366 _BM(0), /* 132 Unused */
367 _BM(0), /* 133 Unused */
368 _BM(0), /* 134 Unused */
369 _BM(0), /* 135 Unused */
370 _BM(0), /* 136 Unused */
371 _BM(0), /* 137 Unused */
372 _BM(0), /* 138 Unused */
373 _BM(0), /* 139 Unused */
374 _BM(0), /* 140 Unused */
375 _BM(0), /* 141 Unused */
376 _BM(0), /* 142 Unused */
377 _BM(0), /* 143 Unused */
378 _BM(0), /* 144 R_SH_TLS_GD_32 */
379 _BM(0), /* 145 R_SH_TLS_LD_32 */
380 _BM(0), /* 146 R_SH_TLS_LDO_32 */
381 _BM(0), /* 147 R_SH_TLS_IE_32 */
382 _BM(0), /* 148 R_SH_TLS_LE_32 */
383 _BM(0), /* 149 R_SH_TLS_DTPMOD32 */
384 _BM(0), /* 150 R_SH_TLS_DTPOFF32 */
385 _BM(0), /* 151 R_SH_TLS_TPOFF32 */
386 _BM(0), /* 152 xxx */
387 _BM(0), /* 153 xxx */
388 _BM(0), /* 154 xxx */
389 _BM(0), /* 155 xxx */
390 _BM(0), /* 156 xxx */
391 _BM(0), /* 157 xxx */
392 _BM(0), /* 158 xxx */
393 _BM(0), /* 159 xxx */
394 _BM(0), /* 160 R_SH_GOT32 */
395 _BM(0), /* 161 R_SH_PLT32 */
396 _BM(0), /* 162 R_SH_COPY */
397 _BM(32), /* 163 R_SH_GLOB_DAT */
398 _BM(0), /* 164 R_SH_JMP_SLOT */
399 _BM(32), /* 165 R_SH_RELATIVE */
400 #undef _BM
401 };
402 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
403
404 #define R_TYPE(x) R_SH_ ## x
405
406 void _dl_reloc_plt(Elf_Word *where, Elf_Addr value, Elf_RelA *rel);
407
408 void
_dl_reloc_plt(Elf_Word * where,Elf_Addr value,Elf_RelA * rel)409 _dl_reloc_plt(Elf_Word *where, Elf_Addr value, Elf_RelA *rel)
410 {
411 *where = value + rel->r_addend;
412 }
413
414 int
_dl_md_reloc(elf_object_t * object,int rel,int relasz)415 _dl_md_reloc(elf_object_t *object, int rel, int relasz)
416 {
417 long i;
418 long numrela;
419 long relrel;
420 int fails = 0;
421 Elf_Addr loff;
422 Elf_Addr prev_value = 0;
423 const Elf_Sym *prev_sym = NULL;
424 Elf_RelA *rels;
425
426 loff = object->obj_base;
427 numrela = object->Dyn.info[relasz] / sizeof(Elf_RelA);
428 relrel = rel == DT_RELA ? object->relacount : 0;
429 rels = (Elf_RelA *)(object->Dyn.info[rel]);
430
431 if (rels == NULL)
432 return 0;
433
434 if (relrel > numrela)
435 _dl_die("relacount > numrel: %ld > %ld", relrel, numrela);
436
437 /* tight loop for leading RELATIVE relocs */
438 for (i = 0; i < relrel; i++, rels++) {
439 Elf_Addr *where;
440
441 where = (Elf_Addr *)(rels->r_offset + loff);
442 *where = rels->r_addend + loff;
443 }
444 for (; i < numrela; i++, rels++) {
445 Elf_Addr *where, value, mask;
446 Elf_Word type;
447 const Elf_Sym *sym;
448 const char *symn;
449
450 type = ELF_R_TYPE(rels->r_info);
451
452 if (RELOC_ERROR(type))
453 _dl_die("bad relocation obj %s %ld %d",
454 object->load_name, i, type);
455
456 if (type == R_TYPE(NONE))
457 continue;
458
459 if (type == R_TYPE(JMP_SLOT) && rel != DT_JMPREL)
460 continue;
461
462 where = (Elf_Addr *)(rels->r_offset + loff);
463
464 if (RELOC_USE_ADDEND(type))
465 #ifdef LDSO_ARCH_IS_RELA_
466 value = rels->r_addend;
467 #else
468 value = *where & RELOC_VALUE_BITMASK(type);
469 #endif
470 else
471 value = 0;
472
473
474 sym = NULL;
475 symn = NULL;
476 if (RELOC_RESOLVE_SYMBOL(type)) {
477 sym = object->dyn.symtab;
478 sym += ELF_R_SYM(rels->r_info);
479 symn = object->dyn.strtab + sym->st_name;
480
481 if (sym->st_shndx != SHN_UNDEF &&
482 ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
483 value += loff;
484 } else if (sym == prev_sym) {
485 value += prev_value;
486 } else {
487 struct sym_res sr;
488
489 sr = _dl_find_symbol(symn,
490 SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
491 ((type == R_TYPE(JMP_SLOT)) ?
492 SYM_PLT : SYM_NOTPLT),
493 sym, object);
494 if (sr.sym == NULL) {
495 resolve_failed:
496 if (ELF_ST_BIND(sym->st_info) !=
497 STB_WEAK)
498 fails++;
499 continue;
500 }
501 prev_sym = sym;
502 prev_value = (Elf_Addr)(sr.obj->obj_base +
503 sr.sym->st_value);
504 value += prev_value;
505 }
506 }
507
508 if (type == R_TYPE(JMP_SLOT)) {
509 _dl_reloc_plt((Elf_Word *)where, value, rels);
510 continue;
511 }
512
513 if (type == R_TYPE(COPY)) {
514 void *dstaddr = where;
515 const void *srcaddr;
516 const Elf_Sym *dstsym = sym;
517 struct sym_res sr;
518
519 sr = _dl_find_symbol(symn,
520 SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT,
521 dstsym, object);
522 if (sr.sym == NULL)
523 goto resolve_failed;
524
525 srcaddr = (void *)(sr.obj->obj_base + sr.sym->st_value);
526 _dl_bcopy(srcaddr, dstaddr, dstsym->st_size);
527 continue;
528 }
529
530 if (RELOC_PC_RELATIVE(type))
531 value -= (Elf_Addr)where;
532 if (RELOC_BASE_RELATIVE(type))
533 value += loff;
534
535 mask = RELOC_VALUE_BITMASK(type);
536 value >>= RELOC_VALUE_RIGHTSHIFT(type);
537 value &= mask;
538
539 *where &= ~mask;
540 *where |= value;
541 }
542
543 return fails;
544 }
545
546 /*
547 * Relocate the Global Offset Table (GOT).
548 * This is done by calling _dl_md_reloc on DT_JMPREL for DL_BIND_NOW,
549 * otherwise the lazy binding plt initialization is performed.
550 */
551 int
_dl_md_reloc_got(elf_object_t * object,int lazy)552 _dl_md_reloc_got(elf_object_t *object, int lazy)
553 {
554 int fails = 0;
555 Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
556 int i, num;
557 Elf_RelA *rel;
558
559 if (object->Dyn.info[DT_PLTREL] != DT_RELA)
560 return 0;
561
562 if (!lazy) {
563 fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
564 } else {
565 rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]);
566 num = (object->Dyn.info[DT_PLTRELSZ]) / sizeof(Elf_RelA);
567
568 for (i = 0; i < num; i++, rel++) {
569 Elf_Addr *where, value;
570 Elf_Word type;
571
572 where = (Elf_Addr *)(rel->r_offset + object->obj_base);
573 type = ELF_R_TYPE(rel->r_info);
574 if (RELOC_USE_ADDEND(type))
575 value = rel->r_addend;
576 else
577 value = 0;
578 *where += object->obj_base + value;
579 }
580
581 pltgot[1] = (Elf_Addr)object;
582 pltgot[2] = (Elf_Addr)_dl_bind_start;
583 }
584
585 return fails;
586 }
587
588 Elf_Addr
_dl_bind(elf_object_t * object,int reloff)589 _dl_bind(elf_object_t *object, int reloff)
590 {
591 Elf_RelA *rel;
592 const Elf_Sym *sym;
593 const char *symn;
594 struct sym_res sr;
595 uint64_t cookie = pcookie;
596 struct {
597 struct __kbind param;
598 Elf_Addr newval;
599 } buf;
600
601 rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL] + reloff);
602
603 sym = object->dyn.symtab;
604 sym += ELF_R_SYM(rel->r_info);
605 symn = object->dyn.strtab + sym->st_name;
606
607 sr = _dl_find_symbol(symn, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT,
608 sym, object);
609 if (sr.sym == NULL)
610 _dl_die("lazy binding failed!");
611
612 buf.newval = sr.obj->obj_base + sr.sym->st_value;
613
614 if (__predict_false(sr.obj->traced) && _dl_trace_plt(sr.obj, symn))
615 return buf.newval;
616
617 buf.param.kb_addr = (Elf_Addr *)(object->obj_base + rel->r_offset);
618 buf.param.kb_size = sizeof(Elf_Addr);
619
620 /* directly code the syscall, so that it's actually inline here */
621 {
622 register long syscall_num __asm("r0") = SYS_kbind;
623 register void *arg1 __asm("r4") = &buf;
624 register long arg2 __asm("r5") = sizeof(buf);
625 register long arg3 __asm("r6") = 0xffffffff & cookie;
626 register long arg4 __asm("r7") = 0xffffffff & (cookie >> 32);
627
628 __asm volatile("trapa #0x80" : "+r" (syscall_num)
629 : "r" (arg1), "r" (arg2), "r" (arg3), "r" (arg4)
630 : "r1", "cc", "memory");
631 }
632
633 return buf.newval;
634 }
635