]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/aarch64/reloc.c
MFC r341511,r341512,r341513:
[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                 default:
221                         _rtld_error("Unknown relocation type %u in PLT",
222                             (unsigned int)ELF_R_TYPE(rela->r_info));
223                         return (-1);
224                 }
225         }
226
227         return (0);
228 }
229
230 /*
231  * LD_BIND_NOW was set - force relocation for all jump slots
232  */
233 int
234 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
235 {
236         const Obj_Entry *defobj;
237         const Elf_Rela *relalim;
238         const Elf_Rela *rela;
239         const Elf_Sym *def;
240
241         if (obj->jmpslots_done)
242                 return (0);
243
244         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
245         for (rela = obj->pltrela; rela < relalim; rela++) {
246                 Elf_Addr *where;
247
248                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
249                 switch(ELF_R_TYPE(rela->r_info)) {
250                 case R_AARCH64_JUMP_SLOT:
251                         def = find_symdef(ELF_R_SYM(rela->r_info), obj,
252                             &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate);
253                         if (def == NULL) {
254                                 dbg("reloc_jmpslots: sym not found");
255                                 return (-1);
256                         }
257
258                         *where = (Elf_Addr)(defobj->relocbase + def->st_value);
259                         break;
260                 }
261         }
262         obj->jmpslots_done = true;
263
264         return (0);
265 }
266
267 int
268 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
269 {
270
271         /* XXX not implemented */
272         return (0);
273 }
274
275 int
276 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
277    struct Struct_RtldLockState *lockstate)
278 {
279
280         /* XXX not implemented */
281         return (0);
282 }
283
284 Elf_Addr
285 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
286     const Obj_Entry *obj, const Elf_Rel *rel)
287 {
288
289         assert(ELF_R_TYPE(rel->r_info) == R_AARCH64_JUMP_SLOT);
290
291         if (*where != target && !ld_bind_not)
292                 *where = target;
293         return (target);
294 }
295
296 void
297 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
298 {
299
300 }
301
302 void
303 pre_init(void)
304 {
305
306 }
307
308 /*
309  * Process non-PLT relocations
310  */
311 int
312 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
313     RtldLockState *lockstate)
314 {
315         const Obj_Entry *defobj;
316         const Elf_Rela *relalim;
317         const Elf_Rela *rela;
318         const Elf_Sym *def;
319         SymCache *cache;
320         Elf_Addr *where;
321         unsigned long symnum;
322
323         if ((flags & SYMLOOK_IFUNC) != 0)
324                 /* XXX not implemented */
325                 return (0);
326
327         /*
328          * The dynamic loader may be called from a thread, we have
329          * limited amounts of stack available so we cannot use alloca().
330          */
331         if (obj == obj_rtld)
332                 cache = NULL;
333         else
334                 cache = calloc(obj->dynsymcount, sizeof(SymCache));
335                 /* No need to check for NULL here */
336
337         relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
338         for (rela = obj->rela; rela < relalim; rela++) {
339                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
340                 symnum = ELF_R_SYM(rela->r_info);
341
342                 switch (ELF_R_TYPE(rela->r_info)) {
343                 case R_AARCH64_ABS64:
344                 case R_AARCH64_GLOB_DAT:
345                         def = find_symdef(symnum, obj, &defobj, flags, cache,
346                             lockstate);
347                         if (def == NULL)
348                                 return (-1);
349
350                         *where = (Elf_Addr)defobj->relocbase + def->st_value +
351                             rela->r_addend;
352                         break;
353                 case R_AARCH64_COPY:
354                         /*
355                          * These are deferred until all other relocations have
356                          * been done. All we do here is make sure that the
357                          * COPY relocation is not in a shared library. They
358                          * are allowed only in executable files.
359                          */
360                         if (!obj->mainprog) {
361                                 _rtld_error("%s: Unexpected R_AARCH64_COPY "
362                                     "relocation in shared library", obj->path);
363                                 return (-1);
364                         }
365                         break;
366                 case R_AARCH64_TLSDESC:
367                         reloc_tlsdesc(obj, rela, where);
368                         break;
369                 case R_AARCH64_TLS_TPREL64:
370                         def = find_symdef(symnum, obj, &defobj, flags, cache,
371                             lockstate);
372                         if (def == NULL)
373                                 return (-1);
374
375                         /*
376                          * We lazily allocate offsets for static TLS as we
377                          * see the first relocation that references the
378                          * TLS block. This allows us to support (small
379                          * amounts of) static TLS in dynamically loaded
380                          * modules. If we run out of space, we generate an
381                          * error.
382                          */
383                         if (!defobj->tls_done) {
384                                 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
385                                         _rtld_error(
386                                             "%s: No space available for static "
387                                             "Thread Local Storage", obj->path);
388                                         return (-1);
389                                 }
390                         }
391
392                         *where = def->st_value + rela->r_addend +
393                             defobj->tlsoffset;
394                         break;
395                 case R_AARCH64_RELATIVE:
396                         *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
397                         break;
398                 default:
399                         rtld_printf("%s: Unhandled relocation %lu\n",
400                             obj->path, ELF_R_TYPE(rela->r_info));
401                         return (-1);
402                 }
403         }
404
405         return (0);
406 }
407
408 void
409 allocate_initial_tls(Obj_Entry *objs)
410 {
411         Elf_Addr **tp;
412
413         /*
414         * Fix the size of the static TLS block by using the maximum
415         * offset allocated so far and adding a bit for dynamic modules to
416         * use.
417         */
418         tls_static_space = tls_last_offset + tls_last_size +
419             RTLD_STATIC_TLS_EXTRA;
420
421         tp = (Elf_Addr **) allocate_tls(objs, NULL, TLS_TCB_SIZE, 16);
422
423         asm volatile("msr       tpidr_el0, %0" : : "r"(tp));
424 }
425
426 void *
427 __tls_get_addr(tls_index* ti)
428 {
429       char *p;
430       void *_tp;
431
432       __asm __volatile("mrs     %0, tpidr_el0"  : "=r" (_tp));
433       p = tls_get_addr_common((Elf_Addr **)(_tp), ti->ti_module, ti->ti_offset);
434
435       return (p);
436 }