2 * Copyright (c) 2014, 2015 The FreeBSD Foundation.
3 * Copyright (c) 2014 Andrew Turner.
6 * This software was developed by Andrew Turner under
7 * sponsorship from the FreeBSD Foundation.
9 * Portions of this software were developed by Konstantin Belousov
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
41 #include <sys/imgact.h>
42 #include <sys/linker.h>
44 #include <sys/sysent.h>
45 #include <sys/imgact_elf.h>
46 #include <sys/syscall.h>
47 #include <sys/signalvar.h>
48 #include <sys/vnode.h>
51 #include <vm/vm_param.h>
53 #include <machine/elf.h>
54 #include <machine/md_var.h>
56 #include "linker_if.h"
60 static struct sysentvec elf64_freebsd_sysvec = {
61 .sv_size = SYS_MAXSYSCALL,
66 .sv_fixup = __elfN(freebsd_fixup),
67 .sv_sendsig = sendsig,
68 .sv_sigcode = sigcode,
69 .sv_szsigcode = &szsigcode,
70 .sv_name = "FreeBSD ELF64",
71 .sv_coredump = __elfN(coredump),
72 .sv_imgact_try = NULL,
73 .sv_minsigstksz = MINSIGSTKSZ,
74 .sv_minuser = VM_MIN_ADDRESS,
75 .sv_maxuser = VM_MAXUSER_ADDRESS,
76 .sv_usrstack = USRSTACK,
77 .sv_psstrings = PS_STRINGS,
78 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
79 .sv_copyout_strings = exec_copyout_strings,
80 .sv_setregs = exec_setregs,
83 .sv_flags = SV_SHP | SV_TIMEKEEP | SV_ABI_FREEBSD | SV_LP64 |
85 .sv_set_syscall_retval = cpu_set_syscall_retval,
86 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
87 .sv_syscallnames = syscallnames,
88 .sv_shared_page_base = SHAREDPAGE,
89 .sv_shared_page_len = PAGE_SIZE,
91 .sv_thread_detach = NULL,
93 .sv_hwcap = &elf_hwcap,
95 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
97 static Elf64_Brandinfo freebsd_brand_info = {
98 .brand = ELFOSABI_FREEBSD,
99 .machine = EM_AARCH64,
100 .compat_3_brand = "FreeBSD",
102 .interp_path = "/libexec/ld-elf.so.1",
103 .sysvec = &elf64_freebsd_sysvec,
104 .interp_newpath = NULL,
105 .brand_note = &elf64_freebsd_brandnote,
106 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
109 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
110 (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
113 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
114 size_t *off __unused)
120 elf_is_ifunc_reloc(Elf_Size r_info __unused)
123 return (ELF_R_TYPE(r_info) == R_AARCH64_IRELATIVE);
127 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
128 int type, int local, elf_lookup_fn lookup)
130 Elf_Addr *where, addr, addend, val;
131 Elf_Word rtype, symidx;
133 const Elf_Rela *rela;
138 rel = (const Elf_Rel *)data;
139 where = (Elf_Addr *) (relocbase + rel->r_offset);
141 rtype = ELF_R_TYPE(rel->r_info);
142 symidx = ELF_R_SYM(rel->r_info);
145 rela = (const Elf_Rela *)data;
146 where = (Elf_Addr *) (relocbase + rela->r_offset);
147 addend = rela->r_addend;
148 rtype = ELF_R_TYPE(rela->r_info);
149 symidx = ELF_R_SYM(rela->r_info);
152 panic("unknown reloc type %d\n", type);
156 if (rtype == R_AARCH64_RELATIVE)
157 *where = elf_relocaddr(lf, relocbase + addend);
163 case R_AARCH64_RELATIVE:
165 case R_AARCH64_ABS64:
166 case R_AARCH64_GLOB_DAT:
167 case R_AARCH64_JUMP_SLOT:
168 error = lookup(lf, symidx, 1, &addr);
171 *where = addr + addend;
173 case R_AARCH64_IRELATIVE:
174 addr = relocbase + addend;
175 val = ((Elf64_Addr (*)(void))addr)();
180 printf("kldload: unexpected relocation type %d\n", rtype);
187 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
188 int type, elf_lookup_fn lookup)
191 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
194 /* Process one elf relocation with addend. */
196 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
197 elf_lookup_fn lookup)
200 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
204 elf_cpu_load_file(linker_file_t lf)
208 cpu_icache_sync_range((vm_offset_t)lf->address, lf->size);
213 elf_cpu_unload_file(linker_file_t lf __unused)