]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/aarch64/reloc.c
Copy ^/vendor/NetBSD/tests/dist/lib/libc/hash/t_hmac.c to
[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(
113 "Undefined symbol \"%s\" referenced from COPY relocation in %s",
114                             name, dstobj->path);
115                         return (-1);
116                 }
117
118                 srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
119                 memcpy(dstaddr, srcaddr, size);
120         }
121
122         return (0);
123 }
124
125 struct tls_data {
126         int64_t index;
127         Obj_Entry *obj;
128         const Elf_Rela *rela;
129 };
130
131 static struct tls_data *
132 reloc_tlsdesc_alloc(Obj_Entry *obj, const Elf_Rela *rela)
133 {
134         struct tls_data *tlsdesc;
135
136         tlsdesc = xmalloc(sizeof(struct tls_data));
137         tlsdesc->index = -1;
138         tlsdesc->obj = obj;
139         tlsdesc->rela = rela;
140
141         return (tlsdesc);
142 }
143
144 /*
145  * Look up the symbol to find its tls index
146  */
147 static int64_t
148 rtld_tlsdesc_handle_locked(struct tls_data *tlsdesc, int flags,
149     RtldLockState *lockstate)
150 {
151         const Elf_Rela *rela;
152         const Elf_Sym *def;
153         const Obj_Entry *defobj;
154         Obj_Entry *obj;
155
156         rela = tlsdesc->rela;
157         obj = tlsdesc->obj;
158
159         def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, flags, NULL,
160             lockstate);
161         if (def == NULL)
162                 rtld_die();
163
164         tlsdesc->index = defobj->tlsoffset + def->st_value + rela->r_addend;
165
166         return (tlsdesc->index);
167 }
168
169 int64_t
170 rtld_tlsdesc_handle(struct tls_data *tlsdesc, int flags)
171 {
172         RtldLockState lockstate;
173
174         /* We have already found the index, return it */
175         if (tlsdesc->index >= 0)
176                 return (tlsdesc->index);
177
178         wlock_acquire(rtld_bind_lock, &lockstate);
179         /* tlsdesc->index may have been set by another thread */
180         if (tlsdesc->index == -1)
181                 rtld_tlsdesc_handle_locked(tlsdesc, flags, &lockstate);
182         lock_release(rtld_bind_lock, &lockstate);
183
184         return (tlsdesc->index);
185 }
186
187 /*
188  * Process the PLT relocations.
189  */
190 int
191 reloc_plt(Obj_Entry *obj)
192 {
193         const Elf_Rela *relalim;
194         const Elf_Rela *rela;
195
196         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
197         for (rela = obj->pltrela; rela < relalim; rela++) {
198                 Elf_Addr *where;
199
200                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
201
202                 switch(ELF_R_TYPE(rela->r_info)) {
203                 case R_AARCH64_JUMP_SLOT:
204                         *where += (Elf_Addr)obj->relocbase;
205                         break;
206                 case R_AARCH64_TLSDESC:
207                         if (ELF_R_SYM(rela->r_info) == 0) {
208                                 where[0] = (Elf_Addr)_rtld_tlsdesc;
209                                 where[1] = obj->tlsoffset + rela->r_addend;
210                         } else {
211                                 where[0] = (Elf_Addr)_rtld_tlsdesc_dynamic;
212                                 where[1] = (Elf_Addr)reloc_tlsdesc_alloc(obj,
213                                     rela);
214                         }
215                         break;
216                 default:
217                         _rtld_error("Unknown relocation type %u in PLT",
218                             (unsigned int)ELF_R_TYPE(rela->r_info));
219                         return (-1);
220                 }
221         }
222
223         return (0);
224 }
225
226 /*
227  * LD_BIND_NOW was set - force relocation for all jump slots
228  */
229 int
230 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
231 {
232         const Obj_Entry *defobj;
233         const Elf_Rela *relalim;
234         const Elf_Rela *rela;
235         const Elf_Sym *def;
236         struct tls_data *tlsdesc;
237
238         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
239         for (rela = obj->pltrela; rela < relalim; rela++) {
240                 Elf_Addr *where;
241
242                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
243                 switch(ELF_R_TYPE(rela->r_info)) {
244                 case R_AARCH64_JUMP_SLOT:
245                         def = find_symdef(ELF_R_SYM(rela->r_info), obj,
246                             &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate);
247                         if (def == NULL) {
248                                 dbg("reloc_jmpslots: sym not found");
249                                 return (-1);
250                         }
251
252                         *where = (Elf_Addr)(defobj->relocbase + def->st_value);
253                         break;
254                 case R_AARCH64_TLSDESC:
255                         if (ELF_R_SYM(rela->r_info) != 0) {
256                                 tlsdesc = (struct tls_data *)where[1];
257                                 if (tlsdesc->index == -1)
258                                         rtld_tlsdesc_handle_locked(tlsdesc,
259                                             SYMLOOK_IN_PLT | flags, lockstate);
260                         }
261                         break;
262                 default:
263                         _rtld_error("Unknown relocation type %x in jmpslot",
264                             (unsigned int)ELF_R_TYPE(rela->r_info));
265                         return (-1);
266                 }
267         }
268
269         return (0);
270 }
271
272 int
273 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
274 {
275
276         /* XXX not implemented */
277         return (0);
278 }
279
280 int
281 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
282    struct Struct_RtldLockState *lockstate)
283 {
284
285         /* XXX not implemented */
286         return (0);
287 }
288
289 Elf_Addr
290 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
291     const Obj_Entry *obj, const Elf_Rel *rel)
292 {
293
294         assert(ELF_R_TYPE(rel->r_info) == R_AARCH64_JUMP_SLOT);
295
296         if (*where != target)
297                 *where = target;
298
299         return target;
300 }
301
302 void
303 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
304 {
305 }
306
307 /*
308  * Process non-PLT relocations
309  */
310 int
311 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
312     RtldLockState *lockstate)
313 {
314         const Obj_Entry *defobj;
315         const Elf_Rela *relalim;
316         const Elf_Rela *rela;
317         const Elf_Sym *def;
318         SymCache *cache;
319         Elf_Addr *where;
320         unsigned long symnum;
321
322         if ((flags & SYMLOOK_IFUNC) != 0)
323                 /* XXX not implemented */
324                 return (0);
325
326         /*
327          * The dynamic loader may be called from a thread, we have
328          * limited amounts of stack available so we cannot use alloca().
329          */
330         if (obj == obj_rtld)
331                 cache = NULL;
332         else
333                 cache = calloc(obj->dynsymcount, sizeof(SymCache));
334                 /* No need to check for NULL here */
335
336         relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
337         for (rela = obj->rela; rela < relalim; rela++) {
338                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
339                 symnum = ELF_R_SYM(rela->r_info);
340
341                 switch (ELF_R_TYPE(rela->r_info)) {
342                 case R_AARCH64_ABS64:
343                 case R_AARCH64_GLOB_DAT:
344                         def = find_symdef(symnum, obj, &defobj, flags, cache,
345                             lockstate);
346                         if (def == NULL)
347                                 return (-1);
348
349                         *where = (Elf_Addr)defobj->relocbase + def->st_value +
350                             rela->r_addend;
351                         break;
352                 case R_AARCH64_COPY:
353                         /*
354                          * These are deferred until all other relocations have
355                          * been done. All we do here is make sure that the
356                          * COPY relocation is not in a shared library. They
357                          * are allowed only in executable files.
358                          */
359                         if (!obj->mainprog) {
360                                 _rtld_error("%s: Unexpected R_AARCH64_COPY "
361                                     "relocation in shared library", obj->path);
362                                 return (-1);
363                         }
364                         break;
365                 case R_AARCH64_TLS_TPREL64:
366                         def = find_symdef(symnum, obj, &defobj, flags, cache,
367                             lockstate);
368                         if (def == NULL)
369                                 return (-1);
370
371                         /*
372                          * We lazily allocate offsets for static TLS as we
373                          * see the first relocation that references the
374                          * TLS block. This allows us to support (small
375                          * amounts of) static TLS in dynamically loaded
376                          * modules. If we run out of space, we generate an
377                          * error.
378                          */
379                         if (!defobj->tls_done) {
380                                 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
381                                         _rtld_error(
382                                             "%s: No space available for static "
383                                             "Thread Local Storage", obj->path);
384                                         return (-1);
385                                 }
386                         }
387
388                         *where = def->st_value + rela->r_addend +
389                             defobj->tlsoffset;
390                         break;
391                 case R_AARCH64_RELATIVE:
392                         *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
393                         break;
394                 default:
395                         rtld_printf("%s: Unhandled relocation %lu\n",
396                             obj->path, ELF_R_TYPE(rela->r_info));
397                         return (-1);
398                 }
399         }
400
401         return (0);
402 }
403
404 void
405 allocate_initial_tls(Obj_Entry *objs)
406 {
407         Elf_Addr **tp;
408
409         /*
410         * Fix the size of the static TLS block by using the maximum
411         * offset allocated so far and adding a bit for dynamic modules to
412         * use.
413         */
414         tls_static_space = tls_last_offset + tls_last_size +
415             RTLD_STATIC_TLS_EXTRA;
416
417         tp = (Elf_Addr **) allocate_tls(objs, NULL, TLS_TCB_SIZE, 16);
418
419         asm volatile("msr       tpidr_el0, %0" : : "r"(tp));
420 }