2 * Copyright (c) 2014-2015 The FreeBSD Foundation
5 * Portions of this software were developed by Andrew Turner
6 * under sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
39 #include "rtld_printf.h"
42 * It is possible for the compiler to emit relocations for unaligned data.
43 * We handle this situation with these inlines.
45 #define RELOC_ALIGNED_P(x) \
46 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
49 * This is not the correct prototype, but we only need it for
50 * a function pointer to a simple asm function.
52 void *_rtld_tlsdesc(void *);
53 void *_rtld_tlsdesc_dynamic(void *);
58 init_pltgot(Obj_Entry *obj)
61 if (obj->pltgot != NULL) {
62 obj->pltgot[1] = (Elf_Addr) obj;
63 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
68 do_copy_relocations(Obj_Entry *dstobj)
70 const Obj_Entry *srcobj, *defobj;
71 const Elf_Rela *relalim;
73 const Elf_Sym *srcsym;
74 const Elf_Sym *dstsym;
83 * COPY relocs are invalid outside of the main program
85 assert(dstobj->mainprog);
87 relalim = (const Elf_Rela *)((char *)dstobj->rela +
89 for (rela = dstobj->rela; rela < relalim; rela++) {
90 if (ELF_R_TYPE(rela->r_info) != R_AARCH64_COPY)
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;
98 symlook_init(&req, name);
99 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
100 req.flags = SYMLOOK_EARLY;
102 for (srcobj = globallist_next(dstobj); srcobj != NULL;
103 srcobj = globallist_next(srcobj)) {
104 res = symlook_obj(&req, srcobj);
106 srcsym = req.sym_out;
107 defobj = req.defobj_out;
111 if (srcobj == NULL) {
112 _rtld_error("Undefined symbol \"%s\" referenced from "
113 "COPY relocation in %s", name, dstobj->path);
117 srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
118 memcpy(dstaddr, srcaddr, size);
127 const Elf_Rela *rela;
130 static struct tls_data *
131 reloc_tlsdesc_alloc(Obj_Entry *obj, const Elf_Rela *rela)
133 struct tls_data *tlsdesc;
135 tlsdesc = xmalloc(sizeof(struct tls_data));
138 tlsdesc->rela = rela;
144 * Look up the symbol to find its tls index
147 rtld_tlsdesc_handle_locked(struct tls_data *tlsdesc, int flags,
148 RtldLockState *lockstate)
150 const Elf_Rela *rela;
152 const Obj_Entry *defobj;
155 rela = tlsdesc->rela;
158 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, flags, NULL,
163 tlsdesc->index = defobj->tlsoffset + def->st_value + rela->r_addend;
165 return (tlsdesc->index);
169 rtld_tlsdesc_handle(struct tls_data *tlsdesc, int flags)
171 RtldLockState lockstate;
173 /* We have already found the index, return it */
174 if (tlsdesc->index >= 0)
175 return (tlsdesc->index);
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);
183 return (tlsdesc->index);
187 reloc_tlsdesc(Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *where)
189 if (ELF_R_SYM(rela->r_info) == 0) {
190 where[0] = (Elf_Addr)_rtld_tlsdesc;
191 where[1] = obj->tlsoffset + rela->r_addend;
193 where[0] = (Elf_Addr)_rtld_tlsdesc_dynamic;
194 where[1] = (Elf_Addr)reloc_tlsdesc_alloc(obj, rela);
199 * Process the PLT relocations.
202 reloc_plt(Obj_Entry *obj)
204 const Elf_Rela *relalim;
205 const Elf_Rela *rela;
207 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
208 for (rela = obj->pltrela; rela < relalim; rela++) {
211 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
213 switch(ELF_R_TYPE(rela->r_info)) {
214 case R_AARCH64_JUMP_SLOT:
215 *where += (Elf_Addr)obj->relocbase;
217 case R_AARCH64_TLSDESC:
218 reloc_tlsdesc(obj, rela, where);
221 _rtld_error("Unknown relocation type %u in PLT",
222 (unsigned int)ELF_R_TYPE(rela->r_info));
231 * LD_BIND_NOW was set - force relocation for all jump slots
234 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
236 const Obj_Entry *defobj;
237 const Elf_Rela *relalim;
238 const Elf_Rela *rela;
241 if (obj->jmpslots_done)
244 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
245 for (rela = obj->pltrela; rela < relalim; rela++) {
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);
254 dbg("reloc_jmpslots: sym not found");
258 *where = (Elf_Addr)(defobj->relocbase + def->st_value);
262 obj->jmpslots_done = true;
268 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
271 /* XXX not implemented */
276 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
277 struct Struct_RtldLockState *lockstate)
280 /* XXX not implemented */
285 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
286 const Obj_Entry *obj, const Elf_Rel *rel)
289 assert(ELF_R_TYPE(rel->r_info) == R_AARCH64_JUMP_SLOT);
291 if (*where != target && !ld_bind_not)
297 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
309 * Process non-PLT relocations
312 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
313 RtldLockState *lockstate)
315 const Obj_Entry *defobj;
316 const Elf_Rela *relalim;
317 const Elf_Rela *rela;
321 unsigned long symnum;
323 if ((flags & SYMLOOK_IFUNC) != 0)
324 /* XXX not implemented */
328 * The dynamic loader may be called from a thread, we have
329 * limited amounts of stack available so we cannot use alloca().
334 cache = calloc(obj->dynsymcount, sizeof(SymCache));
335 /* No need to check for NULL here */
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);
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,
350 *where = (Elf_Addr)defobj->relocbase + def->st_value +
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.
360 if (!obj->mainprog) {
361 _rtld_error("%s: Unexpected R_AARCH64_COPY "
362 "relocation in shared library", obj->path);
366 case R_AARCH64_TLSDESC:
367 reloc_tlsdesc(obj, rela, where);
369 case R_AARCH64_TLS_TPREL64:
370 def = find_symdef(symnum, obj, &defobj, flags, cache,
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
383 if (!defobj->tls_done) {
384 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
386 "%s: No space available for static "
387 "Thread Local Storage", obj->path);
392 *where = def->st_value + rela->r_addend +
395 case R_AARCH64_RELATIVE:
396 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
399 rtld_printf("%s: Unhandled relocation %lu\n",
400 obj->path, ELF_R_TYPE(rela->r_info));
409 allocate_initial_tls(Obj_Entry *objs)
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
418 tls_static_space = tls_last_offset + tls_last_size +
419 RTLD_STATIC_TLS_EXTRA;
421 tp = (Elf_Addr **) allocate_tls(objs, NULL, TLS_TCB_SIZE, 16);
423 asm volatile("msr tpidr_el0, %0" : : "r"(tp));
427 __tls_get_addr(tls_index* ti)
432 __asm __volatile("mrs %0, tpidr_el0" : "=r" (_tp));
433 p = tls_get_addr_common((Elf_Addr **)(_tp), ti->ti_module, ti->ti_offset);