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 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
39 #include <sys/imgact.h>
40 #include <sys/linker.h>
43 #include <sys/sysent.h>
44 #include <sys/imgact_elf.h>
45 #include <sys/syscall.h>
46 #include <sys/signalvar.h>
47 #include <sys/vnode.h>
50 #include <vm/vm_param.h>
52 #include <machine/elf.h>
53 #include <machine/md_var.h>
55 #include "linker_if.h"
57 u_long __read_frequently elf_hwcap;
58 u_long __read_frequently elf_hwcap2;
60 struct arm64_addr_mask elf64_addr_mask;
62 static struct sysentvec elf64_freebsd_sysvec = {
63 .sv_size = SYS_MAXSYSCALL,
65 .sv_fixup = __elfN(freebsd_fixup),
66 .sv_sendsig = sendsig,
67 .sv_sigcode = sigcode,
68 .sv_szsigcode = &szsigcode,
69 .sv_name = "FreeBSD ELF64",
70 .sv_coredump = __elfN(coredump),
71 .sv_elf_core_osabi = ELFOSABI_FREEBSD,
72 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
73 .sv_elf_core_prepare_notes = __elfN(prepare_notes),
74 .sv_minsigstksz = MINSIGSTKSZ,
75 .sv_minuser = VM_MIN_ADDRESS,
76 .sv_maxuser = VM_MAXUSER_ADDRESS,
77 .sv_usrstack = USRSTACK,
78 .sv_psstrings = PS_STRINGS,
79 .sv_psstringssz = sizeof(struct ps_strings),
80 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
81 .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
82 .sv_copyout_strings = exec_copyout_strings,
83 .sv_setregs = exec_setregs,
86 .sv_flags = SV_SHP | SV_TIMEKEEP | SV_ABI_FREEBSD | SV_LP64 |
87 SV_ASLR | SV_RNG_SEED_VER,
88 .sv_set_syscall_retval = cpu_set_syscall_retval,
89 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
90 .sv_syscallnames = syscallnames,
91 .sv_shared_page_base = SHAREDPAGE,
92 .sv_shared_page_len = PAGE_SIZE,
94 .sv_thread_detach = NULL,
96 .sv_hwcap = &elf_hwcap,
97 .sv_hwcap2 = &elf_hwcap2,
98 .sv_onexec_old = exec_onexec_old,
99 .sv_onexit = exit_onexit,
100 .sv_regset_begin = SET_BEGIN(__elfN(regset)),
101 .sv_regset_end = SET_LIMIT(__elfN(regset)),
103 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
105 static Elf64_Brandinfo freebsd_brand_info = {
106 .brand = ELFOSABI_FREEBSD,
107 .machine = EM_AARCH64,
108 .compat_3_brand = "FreeBSD",
109 .interp_path = "/libexec/ld-elf.so.1",
110 .sysvec = &elf64_freebsd_sysvec,
111 .interp_newpath = NULL,
112 .brand_note = &elf64_freebsd_brandnote,
113 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
116 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
117 (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
120 get_arm64_addr_mask(struct regset *rs, struct thread *td, void *buf,
124 KASSERT(*sizep == sizeof(elf64_addr_mask),
125 ("%s: invalid size", __func__));
126 memcpy(buf, &elf64_addr_mask, sizeof(elf64_addr_mask));
128 *sizep = sizeof(elf64_addr_mask);
133 static struct regset regset_arm64_addr_mask = {
134 .note = NT_ARM_ADDR_MASK,
135 .size = sizeof(struct arm64_addr_mask),
136 .get = get_arm64_addr_mask,
138 ELF_REGSET(regset_arm64_addr_mask);
141 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
142 size_t *off __unused)
147 elf_is_ifunc_reloc(Elf_Size r_info __unused)
150 return (ELF_R_TYPE(r_info) == R_AARCH64_IRELATIVE);
154 reloc_instr_imm(Elf32_Addr *where, Elf_Addr val, u_int msb, u_int lsb)
157 /* Check bounds: upper bits must be all ones or all zeros. */
158 if ((uint64_t)((int64_t)val >> (msb + 1)) + 1 > 1)
161 val &= (1 << (msb - lsb + 1)) - 1;
162 *where |= (Elf32_Addr)val;
167 * Process a relocation. Support for some static relocations is required
168 * in order for the -zifunc-noplt optimization to work.
171 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
172 int type, int flags, elf_lookup_fn lookup)
174 #define ARM64_ELF_RELOC_LOCAL (1 << 0)
175 #define ARM64_ELF_RELOC_LATE_IFUNC (1 << 1)
176 Elf_Addr *where, addr, addend, val;
177 Elf_Word rtype, symidx;
179 const Elf_Rela *rela;
184 rel = (const Elf_Rel *)data;
185 where = (Elf_Addr *) (relocbase + rel->r_offset);
187 rtype = ELF_R_TYPE(rel->r_info);
188 symidx = ELF_R_SYM(rel->r_info);
191 rela = (const Elf_Rela *)data;
192 where = (Elf_Addr *) (relocbase + rela->r_offset);
193 addend = rela->r_addend;
194 rtype = ELF_R_TYPE(rela->r_info);
195 symidx = ELF_R_SYM(rela->r_info);
198 panic("unknown reloc type %d\n", type);
201 if ((flags & ARM64_ELF_RELOC_LATE_IFUNC) != 0) {
202 KASSERT(type == ELF_RELOC_RELA,
203 ("Only RELA ifunc relocations are supported"));
204 if (rtype != R_AARCH64_IRELATIVE)
208 if ((flags & ARM64_ELF_RELOC_LOCAL) != 0) {
209 if (rtype == R_AARCH64_RELATIVE)
210 *where = elf_relocaddr(lf, relocbase + addend);
217 case R_AARCH64_RELATIVE:
219 case R_AARCH64_TSTBR14:
220 error = lookup(lf, symidx, 1, &addr);
223 error = reloc_instr_imm((Elf32_Addr *)where,
224 addr + addend - (Elf_Addr)where, 15, 2);
226 case R_AARCH64_CONDBR19:
227 error = lookup(lf, symidx, 1, &addr);
230 error = reloc_instr_imm((Elf32_Addr *)where,
231 addr + addend - (Elf_Addr)where, 20, 2);
233 case R_AARCH64_JUMP26:
234 case R_AARCH64_CALL26:
235 error = lookup(lf, symidx, 1, &addr);
238 error = reloc_instr_imm((Elf32_Addr *)where,
239 addr + addend - (Elf_Addr)where, 27, 2);
241 case R_AARCH64_ABS64:
242 case R_AARCH64_GLOB_DAT:
243 case R_AARCH64_JUMP_SLOT:
244 error = lookup(lf, symidx, 1, &addr);
247 *where = addr + addend;
249 case R_AARCH64_IRELATIVE:
250 addr = relocbase + addend;
251 val = ((Elf64_Addr (*)(void))addr)();
256 printf("kldload: unexpected relocation type %d, "
257 "symbol index %d\n", rtype, symidx);
264 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
265 int type, elf_lookup_fn lookup)
268 return (elf_reloc_internal(lf, relocbase, data, type,
269 ARM64_ELF_RELOC_LOCAL, lookup));
272 /* Process one elf relocation with addend. */
274 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
275 elf_lookup_fn lookup)
278 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
282 elf_reloc_late(linker_file_t lf, Elf_Addr relocbase, const void *data,
283 int type, elf_lookup_fn lookup)
286 return (elf_reloc_internal(lf, relocbase, data, type,
287 ARM64_ELF_RELOC_LATE_IFUNC, lookup));
291 elf_cpu_load_file(linker_file_t lf)
295 cpu_icache_sync_range((vm_offset_t)lf->address, lf->size);
300 elf_cpu_unload_file(linker_file_t lf __unused)
307 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)