1 /*-
2 * Copyright (c) 2014, 2015 The FreeBSD Foundation.
3 * Copyright (c) 2014 Andrew Turner.
4 * All rights reserved.
5 *
6 * This software was developed by Andrew Turner under
7 * sponsorship from the FreeBSD Foundation.
8 *
9 * Portions of this software were developed by Konstantin Belousov
10 * under sponsorship from the FreeBSD Foundation.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/exec.h>
38 #include <sys/imgact.h>
39 #include <sys/linker.h>
40 #include <sys/proc.h>
41 #include <sys/reg.h>
42 #include <sys/sysent.h>
43 #include <sys/imgact_elf.h>
44 #include <sys/syscall.h>
45 #include <sys/signalvar.h>
46 #include <sys/vnode.h>
47
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_map.h>
52
53 #include <machine/elf.h>
54 #include <machine/md_var.h>
55
56 #include "linker_if.h"
57
58 u_long __read_frequently elf_hwcap;
59 u_long __read_frequently elf_hwcap2;
60 u_long __read_frequently elf_hwcap3;
61 u_long __read_frequently elf_hwcap4;
62 /* TODO: Move to a better location */
63 u_long __read_frequently linux_elf_hwcap;
64 u_long __read_frequently linux_elf_hwcap2;
65 u_long __read_frequently linux_elf_hwcap3;
66 u_long __read_frequently linux_elf_hwcap4;
67
68 struct arm64_addr_mask elf64_addr_mask = {
69 .code = TBI_ADDR_MASK,
70 .data = TBI_ADDR_MASK,
71 };
72 #ifdef COMPAT_FREEBSD14
73 struct arm64_addr_mask elf64_addr_mask_14;
74 #endif
75
76 static void arm64_exec_protect(struct image_params *, int);
77
78 static struct sysentvec elf64_freebsd_sysvec = {
79 .sv_size = SYS_MAXSYSCALL,
80 .sv_table = sysent,
81 .sv_fixup = __elfN(freebsd_fixup),
82 .sv_sendsig = sendsig,
83 .sv_sigcode = sigcode,
84 .sv_szsigcode = &szsigcode,
85 .sv_name = "FreeBSD ELF64",
86 .sv_coredump = __elfN(coredump),
87 .sv_elf_core_osabi = ELFOSABI_FREEBSD,
88 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
89 .sv_elf_core_prepare_notes = __elfN(prepare_notes),
90 .sv_minsigstksz = MINSIGSTKSZ,
91 .sv_minuser = VM_MIN_ADDRESS,
92 .sv_maxuser = VM_MAXUSER_ADDRESS,
93 .sv_usrstack = USRSTACK,
94 .sv_psstrings = PS_STRINGS,
95 .sv_psstringssz = sizeof(struct ps_strings),
96 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
97 .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
98 .sv_copyout_strings = exec_copyout_strings,
99 .sv_setregs = exec_setregs,
100 .sv_fixlimit = NULL,
101 .sv_maxssiz = NULL,
102 .sv_flags = SV_SHP | SV_TIMEKEEP | SV_ABI_FREEBSD | SV_LP64 |
103 SV_ASLR | SV_RNG_SEED_VER | SV_SIGSYS,
104 .sv_set_syscall_retval = cpu_set_syscall_retval,
105 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
106 .sv_syscallnames = syscallnames,
107 .sv_shared_page_base = SHAREDPAGE,
108 .sv_shared_page_len = PAGE_SIZE,
109 .sv_schedtail = NULL,
110 .sv_thread_detach = NULL,
111 .sv_trap = NULL,
112 .sv_hwcap = &elf_hwcap,
113 .sv_hwcap2 = &elf_hwcap2,
114 .sv_hwcap3 = &elf_hwcap3,
115 .sv_hwcap4 = &elf_hwcap4,
116 .sv_onexec_old = exec_onexec_old,
117 .sv_protect = arm64_exec_protect,
118 .sv_onexit = exit_onexit,
119 .sv_regset_begin = SET_BEGIN(__elfN(regset)),
120 .sv_regset_end = SET_LIMIT(__elfN(regset)),
121 };
122 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
123
124 static const Elf64_Brandinfo freebsd_brand_info = {
125 .brand = ELFOSABI_FREEBSD,
126 .machine = EM_AARCH64,
127 .compat_3_brand = "FreeBSD",
128 .interp_path = "/libexec/ld-elf.so.1",
129 .sysvec = &elf64_freebsd_sysvec,
130 .interp_newpath = NULL,
131 .brand_note = &elf64_freebsd_brandnote,
132 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
133 };
134 C_SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
135 (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
136
137 static bool
get_arm64_addr_mask(struct regset * rs,struct thread * td,void * buf,size_t * sizep)138 get_arm64_addr_mask(struct regset *rs, struct thread *td, void *buf,
139 size_t *sizep)
140 {
141 if (buf != NULL) {
142 KASSERT(*sizep == sizeof(elf64_addr_mask),
143 ("%s: invalid size", __func__));
144 #ifdef COMPAT_FREEBSD14
145 /* running an old binary use the old address mask */
146 if (td->td_proc->p_osrel < TBI_VERSION)
147 memcpy(buf, &elf64_addr_mask_14,
148 sizeof(elf64_addr_mask_14));
149 else
150 #endif
151 memcpy(buf, &elf64_addr_mask, sizeof(elf64_addr_mask));
152 }
153 *sizep = sizeof(elf64_addr_mask);
154
155 return (true);
156 }
157
158 static struct regset regset_arm64_addr_mask = {
159 .note = NT_ARM_ADDR_MASK,
160 .size = sizeof(struct arm64_addr_mask),
161 .get = get_arm64_addr_mask,
162 };
163 ELF_REGSET(regset_arm64_addr_mask);
164
165 void
elf64_dump_thread(struct thread * td __unused,void * dst __unused,size_t * off __unused)166 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
167 size_t *off __unused)
168 {
169 }
170
171 bool
elf_is_ifunc_reloc(Elf_Size r_info __unused)172 elf_is_ifunc_reloc(Elf_Size r_info __unused)
173 {
174
175 return (ELF_R_TYPE(r_info) == R_AARCH64_IRELATIVE);
176 }
177
178 static int
reloc_instr_imm(Elf32_Addr * where,Elf_Addr val,u_int msb,u_int lsb)179 reloc_instr_imm(Elf32_Addr *where, Elf_Addr val, u_int msb, u_int lsb)
180 {
181
182 /* Check bounds: upper bits must be all ones or all zeros. */
183 if ((uint64_t)((int64_t)val >> (msb + 1)) + 1 > 1)
184 return (-1);
185 val >>= lsb;
186 val &= (1 << (msb - lsb + 1)) - 1;
187 *where |= (Elf32_Addr)val;
188 return (0);
189 }
190
191 /*
192 * Process a relocation. Support for some static relocations is required
193 * in order for the -zifunc-noplt optimization to work.
194 */
195 static int
elf_reloc_internal(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,int flags,elf_lookup_fn lookup)196 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
197 int type, int flags, elf_lookup_fn lookup)
198 {
199 #define ARM64_ELF_RELOC_LOCAL (1 << 0)
200 #define ARM64_ELF_RELOC_LATE_IFUNC (1 << 1)
201 Elf_Addr *where, addr, addend, val;
202 Elf_Word rtype, symidx;
203 const Elf_Rel *rel;
204 const Elf_Rela *rela;
205 int error;
206
207 switch (type) {
208 case ELF_RELOC_REL:
209 rel = (const Elf_Rel *)data;
210 where = (Elf_Addr *) (relocbase + rel->r_offset);
211 addend = *where;
212 rtype = ELF_R_TYPE(rel->r_info);
213 symidx = ELF_R_SYM(rel->r_info);
214 break;
215 case ELF_RELOC_RELA:
216 rela = (const Elf_Rela *)data;
217 where = (Elf_Addr *) (relocbase + rela->r_offset);
218 addend = rela->r_addend;
219 rtype = ELF_R_TYPE(rela->r_info);
220 symidx = ELF_R_SYM(rela->r_info);
221 break;
222 default:
223 panic("unknown reloc type %d\n", type);
224 }
225
226 if ((flags & ARM64_ELF_RELOC_LATE_IFUNC) != 0) {
227 KASSERT(type == ELF_RELOC_RELA,
228 ("Only RELA ifunc relocations are supported"));
229 if (rtype != R_AARCH64_IRELATIVE)
230 return (0);
231 }
232
233 if ((flags & ARM64_ELF_RELOC_LOCAL) != 0) {
234 if (rtype == R_AARCH64_RELATIVE)
235 *where = elf_relocaddr(lf, relocbase + addend);
236 return (0);
237 }
238
239 error = 0;
240 switch (rtype) {
241 case R_AARCH64_NONE:
242 case R_AARCH64_RELATIVE:
243 break;
244 case R_AARCH64_TSTBR14:
245 error = lookup(lf, symidx, 1, &addr);
246 if (error != 0)
247 return (-1);
248 error = reloc_instr_imm((Elf32_Addr *)where,
249 addr + addend - (Elf_Addr)where, 15, 2);
250 break;
251 case R_AARCH64_CONDBR19:
252 error = lookup(lf, symidx, 1, &addr);
253 if (error != 0)
254 return (-1);
255 error = reloc_instr_imm((Elf32_Addr *)where,
256 addr + addend - (Elf_Addr)where, 20, 2);
257 break;
258 case R_AARCH64_JUMP26:
259 case R_AARCH64_CALL26:
260 error = lookup(lf, symidx, 1, &addr);
261 if (error != 0)
262 return (-1);
263 error = reloc_instr_imm((Elf32_Addr *)where,
264 addr + addend - (Elf_Addr)where, 27, 2);
265 break;
266 case R_AARCH64_ABS64:
267 case R_AARCH64_GLOB_DAT:
268 case R_AARCH64_JUMP_SLOT:
269 error = lookup(lf, symidx, 1, &addr);
270 if (error != 0)
271 return (-1);
272 *where = addr + addend;
273 break;
274 case R_AARCH64_IRELATIVE:
275 addr = relocbase + addend;
276 val = ((Elf64_Addr (*)(void))addr)();
277 if (*where != val)
278 *where = val;
279 break;
280 default:
281 printf("kldload: unexpected relocation type %d, "
282 "symbol index %d\n", rtype, symidx);
283 return (-1);
284 }
285 return (error);
286 }
287
288 int
elf_reloc_local(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)289 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
290 int type, elf_lookup_fn lookup)
291 {
292
293 return (elf_reloc_internal(lf, relocbase, data, type,
294 ARM64_ELF_RELOC_LOCAL, lookup));
295 }
296
297 /* Process one elf relocation with addend. */
298 int
elf_reloc(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)299 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
300 elf_lookup_fn lookup)
301 {
302
303 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
304 }
305
306 int
elf_reloc_late(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)307 elf_reloc_late(linker_file_t lf, Elf_Addr relocbase, const void *data,
308 int type, elf_lookup_fn lookup)
309 {
310
311 return (elf_reloc_internal(lf, relocbase, data, type,
312 ARM64_ELF_RELOC_LATE_IFUNC, lookup));
313 }
314
315 int
elf_cpu_load_file(linker_file_t lf)316 elf_cpu_load_file(linker_file_t lf)
317 {
318
319 if (lf->id != 1)
320 cpu_icache_sync_range(lf->address, lf->size);
321 return (0);
322 }
323
324 int
elf_cpu_unload_file(linker_file_t lf __unused)325 elf_cpu_unload_file(linker_file_t lf __unused)
326 {
327
328 return (0);
329 }
330
331 int
elf_cpu_parse_dynamic(caddr_t loadbase __unused,Elf_Dyn * dynamic __unused)332 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
333 {
334
335 return (0);
336 }
337
338 static const Elf_Note gnu_property_note = {
339 .n_namesz = sizeof(GNU_ABI_VENDOR),
340 .n_descsz = 16,
341 .n_type = NT_GNU_PROPERTY_TYPE_0,
342 };
343
344 static bool
gnu_property_cb(const Elf_Note * note,void * arg0,bool * res)345 gnu_property_cb(const Elf_Note *note, void *arg0, bool *res)
346 {
347 const uint32_t *data;
348 uintptr_t p;
349
350 *res = false;
351 p = (uintptr_t)(note + 1);
352 p += roundup2(note->n_namesz, 4);
353 data = (const uint32_t *)p;
354 if (data[0] != GNU_PROPERTY_AARCH64_FEATURE_1_AND)
355 return (false);
356 /*
357 * The data length should be at least the size of a uint32, and be
358 * a multiple of uint32_t's
359 */
360 if (data[1] < sizeof(uint32_t) || (data[1] % sizeof(uint32_t)) != 0)
361 return (false);
362 if ((data[2] & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) != 0)
363 *res = true;
364
365 return (true);
366 }
367
368 static void
arm64_exec_protect(struct image_params * imgp,int flags __unused)369 arm64_exec_protect(struct image_params *imgp, int flags __unused)
370 {
371 const Elf_Ehdr *hdr;
372 const Elf_Phdr *phdr;
373 vm_offset_t sva, eva;
374 int i;
375 bool found;
376
377 /* Skip if BTI is not supported */
378 if ((elf_hwcap2 & HWCAP2_BTI) == 0)
379 return;
380
381 hdr = (const Elf_Ehdr *)imgp->image_header;
382 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
383
384 found = false;
385 for (i = 0; i < hdr->e_phnum; i++) {
386 if (phdr[i].p_type == PT_NOTE && __elfN(parse_notes)(imgp,
387 &gnu_property_note, GNU_ABI_VENDOR, &phdr[i],
388 gnu_property_cb, NULL)) {
389 found = true;
390 break;
391 }
392 }
393 if (!found)
394 return;
395
396 for (i = 0; i < hdr->e_phnum; i++) {
397 if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0)
398 continue;
399
400 sva = phdr[i].p_vaddr + imgp->et_dyn_addr;
401 eva = sva + phdr[i].p_memsz;
402 pmap_bti_set(vmspace_pmap(imgp->proc->p_vmspace), sva, eva);
403 }
404 }
405