mdreloc.c revision 1.11 1 /* $NetBSD: mdreloc.c,v 1.11 2018/11/23 10:59:20 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 2014-2015 The FreeBSD Foundation
34 * All rights reserved.
35 *
36 * Portions of this software were developed by Andrew Turner
37 * under sponsorship from the FreeBSD Foundation.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 #include <sys/cdefs.h>
62 #ifndef lint
63 __RCSID("$NetBSD: mdreloc.c,v 1.11 2018/11/23 10:59:20 skrll Exp $");
64 #endif /* not lint */
65
66 #include <sys/types.h>
67 #include <string.h>
68
69 #include "debug.h"
70 #include "rtld.h"
71
72 struct tls_data {
73 int64_t index;
74 Obj_Entry *obj;
75 const Elf_Rela *rela;
76 };
77
78 void _rtld_bind_start(void);
79 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
80 Elf_Addr _rtld_bind(const Obj_Entry *, Elf_Word);
81 void *_rtld_tlsdesc(void *);
82 void *_rtld_tlsdesc_dynamic(void *);
83 int64_t _rtld_tlsdesc_handle(struct tls_data *, u_int);
84
85 /*
86 * AARCH64 PLT looks like this;
87 *
88 * PLT HEADER <8 instructions>
89 * PLT ENTRY #0 <4 instructions>
90 * PLT ENTRY #1 <4 instructions>
91 * .
92 * .
93 * PLT ENTRY #n <4 instructions>
94 *
95 * PLT HEADER
96 * stp x16, x30, [sp, #-16]!
97 * adrp x16, (GOT+16)
98 * ldr x17, [x16, #PLT_GOT+0x10]
99 * add x16, x16, #PLT_GOT+0x10
100 * br x17
101 * nop
102 * nop
103 * nop
104 *
105 * PLT ENTRY #n
106 * adrp x16, PLTGOT + n * 8
107 * ldr x17, [x16, PLTGOT + n * 8]
108 * add x16, x16, :lo12:PLTGOT + n * 8
109 * br x17
110 */
111 void
112 _rtld_setup_pltgot(const Obj_Entry *obj)
113 {
114
115 obj->pltgot[1] = (Elf_Addr) obj;
116 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
117 }
118
119 static struct tls_data *
120 _rtld_tlsdesc_alloc(Obj_Entry *obj, const Elf_Rela *rela)
121 {
122 struct tls_data *tlsdesc;
123
124 tlsdesc = xmalloc(sizeof(*tlsdesc));
125 tlsdesc->index = -1;
126 tlsdesc->obj = obj;
127 tlsdesc->rela = rela;
128
129 return tlsdesc;
130 }
131
132 static int64_t
133 _rtld_tlsdesc_handle_locked(struct tls_data *tlsdesc, u_int flags)
134 {
135 const Elf_Rela *rela;
136 const Elf_Sym *def;
137 const Obj_Entry *defobj;
138 Obj_Entry *obj;
139
140 rela = tlsdesc->rela;
141 obj = tlsdesc->obj;
142
143 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, flags);
144 if (def == NULL)
145 _rtld_die();
146
147 tlsdesc->index = defobj->tlsoffset + def->st_value + rela->r_addend +
148 sizeof(struct tls_tcb);
149
150 return tlsdesc->index;
151 }
152
153 int64_t
154 _rtld_tlsdesc_handle(struct tls_data *tlsdesc, u_int flags)
155 {
156 sigset_t mask;
157
158 /* We have already found the index, return it */
159 if (tlsdesc->index >= 0)
160 return tlsdesc->index;
161
162 _rtld_exclusive_enter(&mask);
163 /* tlsdesc->index may have been set by another thread */
164 if (tlsdesc->index == -1)
165 _rtld_tlsdesc_handle_locked(tlsdesc, flags);
166 _rtld_exclusive_exit(&mask);
167
168 return tlsdesc->index;
169 }
170
171 static void
172 _rtld_tlsdesc_fill(Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *where)
173 {
174 if (ELF_R_SYM(rela->r_info) == 0) {
175 where[0] = (Elf_Addr)_rtld_tlsdesc;
176 where[1] = obj->tlsoffset + rela->r_addend +
177 sizeof(struct tls_tcb);
178 } else {
179 where[0] = (Elf_Addr)_rtld_tlsdesc_dynamic;
180 where[1] = (Elf_Addr)_rtld_tlsdesc_alloc(obj, rela);
181 }
182 }
183
184 void
185 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
186 {
187 const Elf_Rela *rela = 0, *relalim;
188 Elf_Addr relasz = 0;
189 Elf_Addr *where;
190
191 for (; dynp->d_tag != DT_NULL; dynp++) {
192 switch (dynp->d_tag) {
193 case DT_RELA:
194 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
195 break;
196 case DT_RELASZ:
197 relasz = dynp->d_un.d_val;
198 break;
199 }
200 }
201 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
202 for (; rela < relalim; rela++) {
203 where = (Elf_Addr *)(relocbase + rela->r_offset);
204 *where += (Elf_Addr)relocbase;
205 }
206 }
207
208 int
209 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
210 {
211 const Elf_Sym *def = NULL;
212 const Obj_Entry *defobj = NULL;
213 unsigned long last_symnum = ULONG_MAX;
214
215 for (const Elf_Rela *rela = obj->rela; rela < obj->relalim; rela++) {
216 Elf_Addr *where;
217 Elf_Addr tmp;
218 unsigned long symnum;
219
220 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
221
222 switch (ELF_R_TYPE(rela->r_info)) {
223 case R_TYPE(ABS64): /* word S + A */
224 case R_TYPE(GLOB_DAT): /* word S + A */
225 case R_TLS_TYPE(TLS_DTPREL):
226 case R_TLS_TYPE(TLS_DTPMOD):
227 case R_TLS_TYPE(TLS_TPREL):
228 symnum = ELF_R_SYM(rela->r_info);
229 if (last_symnum != symnum) {
230 last_symnum = symnum;
231 def = _rtld_find_symdef(symnum, obj, &defobj,
232 false);
233 if (def == NULL)
234 return -1;
235 }
236
237 default:
238 break;
239 }
240
241 switch (ELF_R_TYPE(rela->r_info)) {
242 case R_TYPE(NONE):
243 break;
244
245 case R_TYPE(ABS64): /* word S + A */
246 case R_TYPE(GLOB_DAT): /* word S + A */
247 tmp = (Elf_Addr)defobj->relocbase + def->st_value +
248 rela->r_addend;
249 if (*where != tmp)
250 *where = tmp;
251 rdbg(("ABS64/GLOB_DAT %s in %s --> %p @ %p in %s",
252 obj->strtab + obj->symtab[symnum].st_name,
253 obj->path, (void *)tmp, where, defobj->path));
254 break;
255
256 case R_TYPE(RELATIVE): /* word B + A */
257 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
258 rdbg(("RELATIVE in %s --> %p", obj->path,
259 (void *)*where));
260 break;
261
262 case R_TYPE(COPY):
263 /*
264 * These are deferred until all other relocations have
265 * been done. All we do here is make sure that the
266 * COPY relocation is not in a shared library. They
267 * are allowed only in executable files.
268 */
269 if (obj->isdynamic) {
270 _rtld_error(
271 "%s: Unexpected R_COPY relocation in shared library",
272 obj->path);
273 return -1;
274 }
275 rdbg(("COPY (avoid in main)"));
276 break;
277
278 case R_TYPE(TLSDESC):
279 _rtld_tlsdesc_fill(obj, rela, where);
280 break;
281
282 case R_TLS_TYPE(TLS_DTPREL):
283 *where = (Elf_Addr)(def->st_value + rela->r_addend);
284
285 rdbg(("TLS_DTPREL %s in %s --> %p",
286 obj->strtab + obj->symtab[symnum].st_name,
287 obj->path, (void *)*where));
288
289 break;
290 case R_TLS_TYPE(TLS_DTPMOD):
291 *where = (Elf_Addr)(defobj->tlsindex);
292
293 rdbg(("TLS_DTPMOD %s in %s --> %p",
294 obj->strtab + obj->symtab[symnum].st_name,
295 obj->path, (void *)*where));
296
297 break;
298
299 case R_TLS_TYPE(TLS_TPREL):
300 if (!defobj->tls_done &&
301 _rtld_tls_offset_allocate(obj))
302 return -1;
303
304 *where = (Elf_Addr)def->st_value + defobj->tlsoffset +
305 sizeof(struct tls_tcb);
306 rdbg(("TLS_TPREL %s in %s --> %p in %s",
307 obj->strtab + obj->symtab[symnum].st_name,
308 obj->path, (void *)*where, defobj->path));
309 break;
310
311 default:
312 rdbg(("sym = %lu, type = %lu, offset = %p, "
313 "addend = %p, contents = %p, symbol = %s",
314 (u_long)ELF_R_SYM(rela->r_info),
315 (u_long)ELF_R_TYPE(rela->r_info),
316 (void *)rela->r_offset, (void *)rela->r_addend,
317 (void *)*where,
318 obj->strtab + obj->symtab[symnum].st_name));
319 _rtld_error("%s: Unsupported relocation type %ld "
320 "in non-PLT relocations",
321 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
322 return -1;
323 }
324 }
325 return 0;
326 }
327
328 int
329 _rtld_relocate_plt_lazy(Obj_Entry *obj)
330 {
331
332 if (!obj->relocbase)
333 return 0;
334
335 for (const Elf_Rela *rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
336 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
337
338 assert((ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT)) ||
339 (ELF_R_TYPE(rela->r_info) == R_TYPE(TLSDESC)));
340
341 switch (ELF_R_TYPE(rela->r_info)) {
342 case R_TYPE(JUMP_SLOT):
343 /* Just relocate the GOT slots pointing into the PLT */
344 *where += (Elf_Addr)obj->relocbase;
345 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
346 break;
347 case R_TYPE(TLSDESC):
348 _rtld_tlsdesc_fill(obj, rela, where);
349 break;
350 }
351 }
352
353 return 0;
354 }
355
356 void
357 _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
358 {
359 const Elf_Rela *rela;
360 Elf_Addr *where, target;
361
362 while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
363 rela = obj->pltrelalim - obj->ifunc_remaining;
364 --obj->ifunc_remaining;
365 if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE)) {
366 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
367 target = (Elf_Addr)(obj->relocbase + rela->r_addend);
368 _rtld_exclusive_exit(mask);
369 target = _rtld_resolve_ifunc2(obj, target);
370 _rtld_exclusive_enter(mask);
371 if (*where != target)
372 *where = target;
373 }
374 }
375 }
376
377 static int
378 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
379 Elf_Addr *tp)
380 {
381 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
382 Elf_Addr new_value;
383 const Elf_Sym *def;
384 const Obj_Entry *defobj;
385
386 switch (ELF_R_TYPE(rela->r_info)) {
387 case R_TYPE(JUMP_SLOT):
388 def = _rtld_find_plt_symdef(ELF_R_SYM(rela->r_info), obj,
389 &defobj, tp != NULL);
390 if (__predict_false(def == NULL))
391 return -1;
392 if (__predict_false(def == &_rtld_sym_zero))
393 return 0;
394
395 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
396 if (tp == NULL)
397 return 0;
398 new_value = _rtld_resolve_ifunc(defobj, def);
399 } else {
400 new_value = (Elf_Addr)(defobj->relocbase +
401 def->st_value);
402 }
403 rdbg(("bind now/fixup in %s --> old=%p new=%p",
404 defobj->strtab + def->st_name, (void *)*where,
405 (void *)new_value));
406 if (*where != new_value)
407 *where = new_value;
408 if (tp)
409 *tp = new_value;
410 break;
411 case R_TYPE(TLSDESC):
412 if (ELF_R_SYM(rela->r_info) != 0) {
413 struct tls_data *tlsdesc = (struct tls_data *)where[1];
414 if (tlsdesc->index == -1)
415 _rtld_tlsdesc_handle_locked(tlsdesc, SYMLOOK_IN_PLT);
416 }
417 break;
418 }
419
420 return 0;
421 }
422
423 Elf_Addr
424 _rtld_bind(const Obj_Entry *obj, Elf_Word relaidx)
425 {
426 const Elf_Rela *rela = obj->pltrela + relaidx;
427 Elf_Addr new_value = 0;
428
429 _rtld_shared_enter();
430 int err = _rtld_relocate_plt_object(obj, rela, &new_value);
431 if (err)
432 _rtld_die();
433 _rtld_shared_exit();
434
435 return new_value;
436 }
437 int
438 _rtld_relocate_plt_objects(const Obj_Entry *obj)
439 {
440 const Elf_Rela *rela;
441 int err = 0;
442
443 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
444 err = _rtld_relocate_plt_object(obj, rela, NULL);
445 if (err)
446 break;
447 }
448
449 return err;
450 }
451