xref: /freebsd/sys/arm64/arm64/elf_machdep.c (revision 4c6c27d3fb4ad15931aae2eaf8e624aed99a3fd9)
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 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 
135 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
136     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
137 
138 static bool
get_arm64_addr_mask(struct regset * rs,struct thread * td,void * buf,size_t * sizep)139 get_arm64_addr_mask(struct regset *rs, struct thread *td, void *buf,
140     size_t *sizep)
141 {
142 	if (buf != NULL) {
143 		KASSERT(*sizep == sizeof(elf64_addr_mask),
144 		    ("%s: invalid size", __func__));
145 #ifdef COMPAT_FREEBSD14
146 		/* running an old binary use the old address mask */
147 		if (td->td_proc->p_osrel < TBI_VERSION)
148 			memcpy(buf, &elf64_addr_mask_14,
149 			    sizeof(elf64_addr_mask_14));
150 		else
151 #endif
152 			memcpy(buf, &elf64_addr_mask, sizeof(elf64_addr_mask));
153 	}
154 	*sizep = sizeof(elf64_addr_mask);
155 
156 	return (true);
157 }
158 
159 static struct regset regset_arm64_addr_mask = {
160 	.note = NT_ARM_ADDR_MASK,
161 	.size = sizeof(struct arm64_addr_mask),
162 	.get = get_arm64_addr_mask,
163 };
164 ELF_REGSET(regset_arm64_addr_mask);
165 
166 void
elf64_dump_thread(struct thread * td __unused,void * dst __unused,size_t * off __unused)167 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
168     size_t *off __unused)
169 {
170 }
171 
172 bool
elf_is_ifunc_reloc(Elf_Size r_info __unused)173 elf_is_ifunc_reloc(Elf_Size r_info __unused)
174 {
175 
176 	return (ELF_R_TYPE(r_info) == R_AARCH64_IRELATIVE);
177 }
178 
179 static int
reloc_instr_imm(Elf32_Addr * where,Elf_Addr val,u_int msb,u_int lsb)180 reloc_instr_imm(Elf32_Addr *where, Elf_Addr val, u_int msb, u_int lsb)
181 {
182 
183 	/* Check bounds: upper bits must be all ones or all zeros. */
184 	if ((uint64_t)((int64_t)val >> (msb + 1)) + 1 > 1)
185 		return (-1);
186 	val >>= lsb;
187 	val &= (1 << (msb - lsb + 1)) - 1;
188 	*where |= (Elf32_Addr)val;
189 	return (0);
190 }
191 
192 /*
193  * Process a relocation.  Support for some static relocations is required
194  * in order for the -zifunc-noplt optimization to work.
195  */
196 static int
elf_reloc_internal(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,int flags,elf_lookup_fn lookup)197 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
198     int type, int flags, elf_lookup_fn lookup)
199 {
200 #define	ARM64_ELF_RELOC_LOCAL		(1 << 0)
201 #define	ARM64_ELF_RELOC_LATE_IFUNC	(1 << 1)
202 	Elf_Addr *where, addr, addend, val;
203 	Elf_Word rtype, symidx;
204 	const Elf_Rel *rel;
205 	const Elf_Rela *rela;
206 	int error;
207 
208 	switch (type) {
209 	case ELF_RELOC_REL:
210 		rel = (const Elf_Rel *)data;
211 		where = (Elf_Addr *) (relocbase + rel->r_offset);
212 		addend = *where;
213 		rtype = ELF_R_TYPE(rel->r_info);
214 		symidx = ELF_R_SYM(rel->r_info);
215 		break;
216 	case ELF_RELOC_RELA:
217 		rela = (const Elf_Rela *)data;
218 		where = (Elf_Addr *) (relocbase + rela->r_offset);
219 		addend = rela->r_addend;
220 		rtype = ELF_R_TYPE(rela->r_info);
221 		symidx = ELF_R_SYM(rela->r_info);
222 		break;
223 	default:
224 		panic("unknown reloc type %d\n", type);
225 	}
226 
227 	if ((flags & ARM64_ELF_RELOC_LATE_IFUNC) != 0) {
228 		KASSERT(type == ELF_RELOC_RELA,
229 		    ("Only RELA ifunc relocations are supported"));
230 		if (rtype != R_AARCH64_IRELATIVE)
231 			return (0);
232 	}
233 
234 	if ((flags & ARM64_ELF_RELOC_LOCAL) != 0) {
235 		if (rtype == R_AARCH64_RELATIVE)
236 			*where = elf_relocaddr(lf, relocbase + addend);
237 		return (0);
238 	}
239 
240 	error = 0;
241 	switch (rtype) {
242 	case R_AARCH64_NONE:
243 	case R_AARCH64_RELATIVE:
244 		break;
245 	case R_AARCH64_TSTBR14:
246 		error = lookup(lf, symidx, 1, &addr);
247 		if (error != 0)
248 			return (-1);
249 		error = reloc_instr_imm((Elf32_Addr *)where,
250 		    addr + addend - (Elf_Addr)where, 15, 2);
251 		break;
252 	case R_AARCH64_CONDBR19:
253 		error = lookup(lf, symidx, 1, &addr);
254 		if (error != 0)
255 			return (-1);
256 		error = reloc_instr_imm((Elf32_Addr *)where,
257 		    addr + addend - (Elf_Addr)where, 20, 2);
258 		break;
259 	case R_AARCH64_JUMP26:
260 	case R_AARCH64_CALL26:
261 		error = lookup(lf, symidx, 1, &addr);
262 		if (error != 0)
263 			return (-1);
264 		error = reloc_instr_imm((Elf32_Addr *)where,
265 		    addr + addend - (Elf_Addr)where, 27, 2);
266 		break;
267 	case R_AARCH64_ABS64:
268 	case R_AARCH64_GLOB_DAT:
269 	case R_AARCH64_JUMP_SLOT:
270 		error = lookup(lf, symidx, 1, &addr);
271 		if (error != 0)
272 			return (-1);
273 		*where = addr + addend;
274 		break;
275 	case R_AARCH64_IRELATIVE:
276 		addr = relocbase + addend;
277 		val = ((Elf64_Addr (*)(void))addr)();
278 		if (*where != val)
279 			*where = val;
280 		break;
281 	default:
282 		printf("kldload: unexpected relocation type %d, "
283 		    "symbol index %d\n", rtype, symidx);
284 		return (-1);
285 	}
286 	return (error);
287 }
288 
289 int
elf_reloc_local(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)290 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
291     int type, elf_lookup_fn lookup)
292 {
293 
294 	return (elf_reloc_internal(lf, relocbase, data, type,
295 	    ARM64_ELF_RELOC_LOCAL, lookup));
296 }
297 
298 /* Process one elf relocation with addend. */
299 int
elf_reloc(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)300 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
301     elf_lookup_fn lookup)
302 {
303 
304 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
305 }
306 
307 int
elf_reloc_late(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)308 elf_reloc_late(linker_file_t lf, Elf_Addr relocbase, const void *data,
309     int type, elf_lookup_fn lookup)
310 {
311 
312 	return (elf_reloc_internal(lf, relocbase, data, type,
313 	    ARM64_ELF_RELOC_LATE_IFUNC, lookup));
314 }
315 
316 int
elf_cpu_load_file(linker_file_t lf)317 elf_cpu_load_file(linker_file_t lf)
318 {
319 
320 	if (lf->id != 1)
321 		cpu_icache_sync_range(lf->address, lf->size);
322 	return (0);
323 }
324 
325 int
elf_cpu_unload_file(linker_file_t lf __unused)326 elf_cpu_unload_file(linker_file_t lf __unused)
327 {
328 
329 	return (0);
330 }
331 
332 int
elf_cpu_parse_dynamic(caddr_t loadbase __unused,Elf_Dyn * dynamic __unused)333 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
334 {
335 
336 	return (0);
337 }
338 
339 static Elf_Note gnu_property_note = {
340 	.n_namesz = sizeof(GNU_ABI_VENDOR),
341 	.n_descsz = 16,
342 	.n_type = NT_GNU_PROPERTY_TYPE_0,
343 };
344 
345 static bool
gnu_property_cb(const Elf_Note * note,void * arg0,bool * res)346 gnu_property_cb(const Elf_Note *note, void *arg0, bool *res)
347 {
348 	const uint32_t *data;
349 	uintptr_t p;
350 
351 	*res = false;
352 	p = (uintptr_t)(note + 1);
353 	p += roundup2(note->n_namesz, 4);
354 	data = (const uint32_t *)p;
355 	if (data[0] != GNU_PROPERTY_AARCH64_FEATURE_1_AND)
356 		return (false);
357 	/*
358 	 * The data length should be at least the size of a uint32, and be
359 	 * a multiple of uint32_t's
360 	 */
361 	if (data[1] < sizeof(uint32_t) || (data[1] % sizeof(uint32_t)) != 0)
362 		return (false);
363 	if ((data[2] & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) != 0)
364 		*res = true;
365 
366 	return (true);
367 }
368 
369 static void
arm64_exec_protect(struct image_params * imgp,int flags __unused)370 arm64_exec_protect(struct image_params *imgp, int flags __unused)
371 {
372 	const Elf_Ehdr *hdr;
373 	const Elf_Phdr *phdr;
374 	vm_offset_t sva, eva;
375 	int i;
376 	bool found;
377 
378 	/* Skip if BTI is not supported */
379 	if ((elf_hwcap2 & HWCAP2_BTI) == 0)
380 		return;
381 
382 	hdr = (const Elf_Ehdr *)imgp->image_header;
383 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
384 
385 	found = false;
386 	for (i = 0; i < hdr->e_phnum; i++) {
387 		if (phdr[i].p_type == PT_NOTE && __elfN(parse_notes)(imgp,
388 		    &gnu_property_note, GNU_ABI_VENDOR, &phdr[i],
389 		    gnu_property_cb, NULL)) {
390 			found = true;
391 			break;
392 		}
393 	}
394 	if (!found)
395 		return;
396 
397 	for (i = 0; i < hdr->e_phnum; i++) {
398 		if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0)
399 			continue;
400 
401 		sva = phdr[i].p_vaddr + imgp->et_dyn_addr;
402 		eva = sva + phdr[i].p_memsz;
403 		pmap_bti_set(vmspace_pmap(imgp->proc->p_vmspace), sva, eva);
404 	}
405 }
406