]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/aarch64/reloc.c
Merge branch 'releng/11.3' into releng-CDN/11.3
[FreeBSD/FreeBSD.git] / libexec / rtld-elf / aarch64 / reloc.c
1 /*-
2  * Copyright (c) 2014-2015 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * Portions of this software were developed by Andrew Turner
6  * under sponsorship from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/types.h>
34
35 #include <stdlib.h>
36
37 #include "debug.h"
38 #include "rtld.h"
39 #include "rtld_printf.h"
40
41 /*
42  * It is possible for the compiler to emit relocations for unaligned data.
43  * We handle this situation with these inlines.
44  */
45 #define RELOC_ALIGNED_P(x) \
46         (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
47
48 /*
49  * This is not the correct prototype, but we only need it for
50  * a function pointer to a simple asm function.
51  */
52 void *_rtld_tlsdesc(void *);
53 void *_rtld_tlsdesc_dynamic(void *);
54
55 void _exit(int);
56
57 void
58 init_pltgot(Obj_Entry *obj)
59 {
60
61         if (obj->pltgot != NULL) {
62                 obj->pltgot[1] = (Elf_Addr) obj;
63                 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
64         }
65 }
66
67 int
68 do_copy_relocations(Obj_Entry *dstobj)
69 {
70         const Obj_Entry *srcobj, *defobj;
71         const Elf_Rela *relalim;
72         const Elf_Rela *rela;
73         const Elf_Sym *srcsym;
74         const Elf_Sym *dstsym;
75         const void *srcaddr;
76         const char *name;
77         void *dstaddr;
78         SymLook req;
79         size_t size;
80         int res;
81
82         /*
83          * COPY relocs are invalid outside of the main program
84          */
85         assert(dstobj->mainprog);
86
87         relalim = (const Elf_Rela *)((char *)dstobj->rela +
88             dstobj->relasize);
89         for (rela = dstobj->rela; rela < relalim; rela++) {
90                 if (ELF_R_TYPE(rela->r_info) != R_AARCH64_COPY)
91                         continue;
92
93                 dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
94                 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
95                 name = dstobj->strtab + dstsym->st_name;
96                 size = dstsym->st_size;
97
98                 symlook_init(&req, name);
99                 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
100                 req.flags = SYMLOOK_EARLY;
101
102                 for (srcobj = globallist_next(dstobj); srcobj != NULL;
103                      srcobj = globallist_next(srcobj)) {
104                         res = symlook_obj(&req, srcobj);
105                         if (res == 0) {
106                                 srcsym = req.sym_out;
107                                 defobj = req.defobj_out;
108                                 break;
109                         }
110                 }
111                 if (srcobj == NULL) {
112                         _rtld_error("Undefined symbol \"%s\" referenced from "
113                             "COPY relocation in %s", name, dstobj->path);
114                         return (-1);
115                 }
116
117                 srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
118                 memcpy(dstaddr, srcaddr, size);
119         }
120
121         return (0);
122 }
123
124 struct tls_data {
125         int64_t index;
126         Obj_Entry *obj;
127         const Elf_Rela *rela;
128 };
129
130 static struct tls_data *
131 reloc_tlsdesc_alloc(Obj_Entry *obj, const Elf_Rela *rela)
132 {
133         struct tls_data *tlsdesc;
134
135         tlsdesc = xmalloc(sizeof(struct tls_data));
136         tlsdesc->index = -1;
137         tlsdesc->obj = obj;
138         tlsdesc->rela = rela;
139
140         return (tlsdesc);
141 }
142
143 /*
144  * Look up the symbol to find its tls index
145  */
146 static int64_t
147 rtld_tlsdesc_handle_locked(struct tls_data *tlsdesc, int flags,
148     RtldLockState *lockstate)
149 {
150         const Elf_Rela *rela;
151         const Elf_Sym *def;
152         const Obj_Entry *defobj;
153         Obj_Entry *obj;
154
155         rela = tlsdesc->rela;
156         obj = tlsdesc->obj;
157
158         def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, flags, NULL,
159             lockstate);
160         if (def == NULL)
161                 rtld_die();
162
163         tlsdesc->index = defobj->tlsoffset + def->st_value + rela->r_addend;
164
165         return (tlsdesc->index);
166 }
167
168 int64_t
169 rtld_tlsdesc_handle(struct tls_data *tlsdesc, int flags)
170 {
171         RtldLockState lockstate;
172
173         /* We have already found the index, return it */
174         if (tlsdesc->index >= 0)
175                 return (tlsdesc->index);
176
177         wlock_acquire(rtld_bind_lock, &lockstate);
178         /* tlsdesc->index may have been set by another thread */
179         if (tlsdesc->index == -1)
180                 rtld_tlsdesc_handle_locked(tlsdesc, flags, &lockstate);
181         lock_release(rtld_bind_lock, &lockstate);
182
183         return (tlsdesc->index);
184 }
185
186 static void
187 reloc_tlsdesc(Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *where)
188 {
189         if (ELF_R_SYM(rela->r_info) == 0) {
190                 where[0] = (Elf_Addr)_rtld_tlsdesc;
191                 where[1] = obj->tlsoffset + rela->r_addend;
192         } else {
193                 where[0] = (Elf_Addr)_rtld_tlsdesc_dynamic;
194                 where[1] = (Elf_Addr)reloc_tlsdesc_alloc(obj, rela);
195         }
196 }
197
198 /*
199  * Process the PLT relocations.
200  */
201 int
202 reloc_plt(Obj_Entry *obj)
203 {
204         const Elf_Rela *relalim;
205         const Elf_Rela *rela;
206
207         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
208         for (rela = obj->pltrela; rela < relalim; rela++) {
209                 Elf_Addr *where;
210
211                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
212
213                 switch(ELF_R_TYPE(rela->r_info)) {
214                 case R_AARCH64_JUMP_SLOT:
215                         *where += (Elf_Addr)obj->relocbase;
216                         break;
217                 case R_AARCH64_TLSDESC:
218                         reloc_tlsdesc(obj, rela, where);
219                         break;
220                 case R_AARCH64_NONE:
221                         break;
222                 default:
223                         _rtld_error("Unknown relocation type %u in PLT",
224                             (unsigned int)ELF_R_TYPE(rela->r_info));
225                         return (-1);
226                 }
227         }
228
229         return (0);
230 }
231
232 /*
233  * LD_BIND_NOW was set - force relocation for all jump slots
234  */
235 int
236 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
237 {
238         const Obj_Entry *defobj;
239         const Elf_Rela *relalim;
240         const Elf_Rela *rela;
241         const Elf_Sym *def;
242
243         if (obj->jmpslots_done)
244                 return (0);
245
246         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
247         for (rela = obj->pltrela; rela < relalim; rela++) {
248                 Elf_Addr *where;
249
250                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
251                 switch(ELF_R_TYPE(rela->r_info)) {
252                 case R_AARCH64_JUMP_SLOT:
253                         def = find_symdef(ELF_R_SYM(rela->r_info), obj,
254                             &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate);
255                         if (def == NULL) {
256                                 dbg("reloc_jmpslots: sym not found");
257                                 return (-1);
258                         }
259
260                         *where = (Elf_Addr)(defobj->relocbase + def->st_value);
261                         break;
262                 }
263         }
264         obj->jmpslots_done = true;
265
266         return (0);
267 }
268
269 int
270 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
271 {
272
273         /* XXX not implemented */
274         return (0);
275 }
276
277 int
278 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
279    struct Struct_RtldLockState *lockstate)
280 {
281
282         /* XXX not implemented */
283         return (0);
284 }
285
286 Elf_Addr
287 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
288     const Obj_Entry *obj, const Elf_Rel *rel)
289 {
290
291         assert(ELF_R_TYPE(rel->r_info) == R_AARCH64_JUMP_SLOT);
292
293         if (*where != target && !ld_bind_not)
294                 *where = target;
295         return (target);
296 }
297
298 void
299 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
300 {
301
302 }
303
304 void
305 pre_init(void)
306 {
307
308 }
309
310 /*
311  * Process non-PLT relocations
312  */
313 int
314 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
315     RtldLockState *lockstate)
316 {
317         const Obj_Entry *defobj;
318         const Elf_Rela *relalim;
319         const Elf_Rela *rela;
320         const Elf_Sym *def;
321         SymCache *cache;
322         Elf_Addr *where;
323         unsigned long symnum;
324
325         if ((flags & SYMLOOK_IFUNC) != 0)
326                 /* XXX not implemented */
327                 return (0);
328
329         /*
330          * The dynamic loader may be called from a thread, we have
331          * limited amounts of stack available so we cannot use alloca().
332          */
333         if (obj == obj_rtld)
334                 cache = NULL;
335         else
336                 cache = calloc(obj->dynsymcount, sizeof(SymCache));
337                 /* No need to check for NULL here */
338
339         relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
340         for (rela = obj->rela; rela < relalim; rela++) {
341                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
342                 symnum = ELF_R_SYM(rela->r_info);
343
344                 switch (ELF_R_TYPE(rela->r_info)) {
345                 case R_AARCH64_ABS64:
346                 case R_AARCH64_GLOB_DAT:
347                         def = find_symdef(symnum, obj, &defobj, flags, cache,
348                             lockstate);
349                         if (def == NULL)
350                                 return (-1);
351
352                         *where = (Elf_Addr)defobj->relocbase + def->st_value +
353                             rela->r_addend;
354                         break;
355                 case R_AARCH64_COPY:
356                         /*
357                          * These are deferred until all other relocations have
358                          * been done. All we do here is make sure that the
359                          * COPY relocation is not in a shared library. They
360                          * are allowed only in executable files.
361                          */
362                         if (!obj->mainprog) {
363                                 _rtld_error("%s: Unexpected R_AARCH64_COPY "
364                                     "relocation in shared library", obj->path);
365                                 return (-1);
366                         }
367                         break;
368                 case R_AARCH64_TLSDESC:
369                         reloc_tlsdesc(obj, rela, where);
370                         break;
371                 case R_AARCH64_TLS_TPREL64:
372                         def = find_symdef(symnum, obj, &defobj, flags, cache,
373                             lockstate);
374                         if (def == NULL)
375                                 return (-1);
376
377                         /*
378                          * We lazily allocate offsets for static TLS as we
379                          * see the first relocation that references the
380                          * TLS block. This allows us to support (small
381                          * amounts of) static TLS in dynamically loaded
382                          * modules. If we run out of space, we generate an
383                          * error.
384                          */
385                         if (!defobj->tls_done) {
386                                 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
387                                         _rtld_error(
388                                             "%s: No space available for static "
389                                             "Thread Local Storage", obj->path);
390                                         return (-1);
391                                 }
392                         }
393
394                         *where = def->st_value + rela->r_addend +
395                             defobj->tlsoffset;
396                         break;
397
398                 /*
399                  * !!! BEWARE !!!
400                  * ARM ELF ABI defines TLS_DTPMOD64 as 1029, and TLS_DTPREL64
401                  * as 1028. But actual bfd linker and the glibc RTLD linker
402                  * treats TLS_DTPMOD64 as 1028 and TLS_DTPREL64 1029.
403                  */
404                 case R_AARCH64_TLS_DTPREL64: /* efectively is TLS_DTPMOD64 */
405                         def = find_symdef(symnum, obj, &defobj, flags, cache,
406                             lockstate);
407                         if (def == NULL)
408                                 return (-1);
409
410                         *where += (Elf_Addr)defobj->tlsindex;
411                         break;
412                 case R_AARCH64_TLS_DTPMOD64: /* efectively is TLS_DTPREL64 */
413                         def = find_symdef(symnum, obj, &defobj, flags, cache,
414                             lockstate);
415                         if (def == NULL)
416                                 return (-1);
417
418                         *where += (Elf_Addr)(def->st_value + rela->r_addend);
419                         break;
420                 case R_AARCH64_RELATIVE:
421                         *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
422                         break;
423                 case R_AARCH64_NONE:
424                         break;
425                 default:
426                         rtld_printf("%s: Unhandled relocation %lu\n",
427                             obj->path, ELF_R_TYPE(rela->r_info));
428                         return (-1);
429                 }
430         }
431
432         return (0);
433 }
434
435 void
436 allocate_initial_tls(Obj_Entry *objs)
437 {
438         Elf_Addr **tp;
439
440         /*
441         * Fix the size of the static TLS block by using the maximum
442         * offset allocated so far and adding a bit for dynamic modules to
443         * use.
444         */
445         tls_static_space = tls_last_offset + tls_last_size +
446             RTLD_STATIC_TLS_EXTRA;
447
448         tp = (Elf_Addr **) allocate_tls(objs, NULL, TLS_TCB_SIZE, 16);
449
450         asm volatile("msr       tpidr_el0, %0" : : "r"(tp));
451 }
452
453 void *
454 __tls_get_addr(tls_index* ti)
455 {
456       char *p;
457       void *_tp;
458
459       __asm __volatile("mrs     %0, tpidr_el0"  : "=r" (_tp));
460       p = tls_get_addr_common((Elf_Addr **)(_tp), ti->ti_module, ti->ti_offset);
461
462       return (p);
463 }