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);
223 _rtld_error("Unknown relocation type %u in PLT",
224 (unsigned int)ELF_R_TYPE(rela->r_info));
233 * LD_BIND_NOW was set - force relocation for all jump slots
236 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
238 const Obj_Entry *defobj;
239 const Elf_Rela *relalim;
240 const Elf_Rela *rela;
243 if (obj->jmpslots_done)
246 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
247 for (rela = obj->pltrela; rela < relalim; rela++) {
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);
256 dbg("reloc_jmpslots: sym not found");
260 *where = (Elf_Addr)(defobj->relocbase + def->st_value);
264 obj->jmpslots_done = true;
270 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
273 /* XXX not implemented */
278 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
279 struct Struct_RtldLockState *lockstate)
282 /* XXX not implemented */
287 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
288 const Obj_Entry *obj, const Elf_Rel *rel)
291 assert(ELF_R_TYPE(rel->r_info) == R_AARCH64_JUMP_SLOT);
293 if (*where != target && !ld_bind_not)
299 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
311 * Process non-PLT relocations
314 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
315 RtldLockState *lockstate)
317 const Obj_Entry *defobj;
318 const Elf_Rela *relalim;
319 const Elf_Rela *rela;
323 unsigned long symnum;
325 if ((flags & SYMLOOK_IFUNC) != 0)
326 /* XXX not implemented */
330 * The dynamic loader may be called from a thread, we have
331 * limited amounts of stack available so we cannot use alloca().
336 cache = calloc(obj->dynsymcount, sizeof(SymCache));
337 /* No need to check for NULL here */
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);
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,
352 *where = (Elf_Addr)defobj->relocbase + def->st_value +
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.
362 if (!obj->mainprog) {
363 _rtld_error("%s: Unexpected R_AARCH64_COPY "
364 "relocation in shared library", obj->path);
368 case R_AARCH64_TLSDESC:
369 reloc_tlsdesc(obj, rela, where);
371 case R_AARCH64_TLS_TPREL64:
372 def = find_symdef(symnum, obj, &defobj, flags, cache,
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
385 if (!defobj->tls_done) {
386 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
388 "%s: No space available for static "
389 "Thread Local Storage", obj->path);
394 *where = def->st_value + rela->r_addend +
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.
404 case R_AARCH64_TLS_DTPREL64: /* efectively is TLS_DTPMOD64 */
405 def = find_symdef(symnum, obj, &defobj, flags, cache,
410 *where += (Elf_Addr)defobj->tlsindex;
412 case R_AARCH64_TLS_DTPMOD64: /* efectively is TLS_DTPREL64 */
413 def = find_symdef(symnum, obj, &defobj, flags, cache,
418 *where += (Elf_Addr)(def->st_value + rela->r_addend);
420 case R_AARCH64_RELATIVE:
421 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
426 rtld_printf("%s: Unhandled relocation %lu\n",
427 obj->path, ELF_R_TYPE(rela->r_info));
436 allocate_initial_tls(Obj_Entry *objs)
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
445 tls_static_space = tls_last_offset + tls_last_size +
446 RTLD_STATIC_TLS_EXTRA;
448 tp = (Elf_Addr **) allocate_tls(objs, NULL, TLS_TCB_SIZE, 16);
450 asm volatile("msr tpidr_el0, %0" : : "r"(tp));
454 __tls_get_addr(tls_index* ti)
459 __asm __volatile("mrs %0, tpidr_el0" : "=r" (_tp));
460 p = tls_get_addr_common((Elf_Addr **)(_tp), ti->ti_module, ti->ti_offset);