xref: /freebsd/sys/arm64/arm64/elf_machdep.c (revision c03c5b1c)
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/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/exec.h>
41 #include <sys/imgact.h>
42 #include <sys/linker.h>
43 #include <sys/proc.h>
44 #include <sys/reg.h>
45 #include <sys/sysent.h>
46 #include <sys/imgact_elf.h>
47 #include <sys/syscall.h>
48 #include <sys/signalvar.h>
49 #include <sys/vnode.h>
50 
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 
54 #include <machine/elf.h>
55 #include <machine/md_var.h>
56 
57 #include "linker_if.h"
58 
59 u_long __read_frequently elf_hwcap;
60 u_long __read_frequently elf_hwcap2;
61 
62 struct arm64_addr_mask elf64_addr_mask;
63 
64 static struct sysentvec elf64_freebsd_sysvec = {
65 	.sv_size	= SYS_MAXSYSCALL,
66 	.sv_table	= sysent,
67 	.sv_transtrap	= NULL,
68 	.sv_fixup	= __elfN(freebsd_fixup),
69 	.sv_sendsig	= sendsig,
70 	.sv_sigcode	= sigcode,
71 	.sv_szsigcode	= &szsigcode,
72 	.sv_name	= "FreeBSD ELF64",
73 	.sv_coredump	= __elfN(coredump),
74 	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
75 	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
76 	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
77 	.sv_imgact_try	= NULL,
78 	.sv_minsigstksz	= MINSIGSTKSZ,
79 	.sv_minuser	= VM_MIN_ADDRESS,
80 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
81 	.sv_usrstack	= USRSTACK,
82 	.sv_psstrings	= PS_STRINGS,
83 	.sv_psstringssz	= sizeof(struct ps_strings),
84 	.sv_stackprot	= VM_PROT_READ | VM_PROT_WRITE,
85 	.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
86 	.sv_copyout_strings = exec_copyout_strings,
87 	.sv_setregs	= exec_setregs,
88 	.sv_fixlimit	= NULL,
89 	.sv_maxssiz	= NULL,
90 	.sv_flags	= SV_SHP | SV_TIMEKEEP | SV_ABI_FREEBSD | SV_LP64 |
91 	    SV_ASLR | SV_RNG_SEED_VER,
92 	.sv_set_syscall_retval = cpu_set_syscall_retval,
93 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
94 	.sv_syscallnames = syscallnames,
95 	.sv_shared_page_base = SHAREDPAGE,
96 	.sv_shared_page_len = PAGE_SIZE,
97 	.sv_schedtail	= NULL,
98 	.sv_thread_detach = NULL,
99 	.sv_trap	= NULL,
100 	.sv_hwcap	= &elf_hwcap,
101 	.sv_hwcap2	= &elf_hwcap2,
102 	.sv_onexec_old	= exec_onexec_old,
103 	.sv_onexit	= exit_onexit,
104 	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
105 	.sv_regset_end	= SET_LIMIT(__elfN(regset)),
106 };
107 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
108 
109 static Elf64_Brandinfo freebsd_brand_info = {
110 	.brand		= ELFOSABI_FREEBSD,
111 	.machine	= EM_AARCH64,
112 	.compat_3_brand	= "FreeBSD",
113 	.emul_path	= NULL,
114 	.interp_path	= "/libexec/ld-elf.so.1",
115 	.sysvec		= &elf64_freebsd_sysvec,
116 	.interp_newpath	= NULL,
117 	.brand_note	= &elf64_freebsd_brandnote,
118 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
119 };
120 
121 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
122     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
123 
124 static bool
125 get_arm64_addr_mask(struct regset *rs, struct thread *td, void *buf,
126     size_t *sizep)
127 {
128 	if (buf != NULL) {
129 		KASSERT(*sizep == sizeof(elf64_addr_mask),
130 		    ("%s: invalid size", __func__));
131 		memcpy(buf, &elf64_addr_mask, sizeof(elf64_addr_mask));
132 	}
133 	*sizep = sizeof(elf64_addr_mask);
134 
135 	return (true);
136 }
137 
138 static struct regset regset_arm64_addr_mask = {
139 	.note = NT_ARM_ADDR_MASK,
140 	.size = sizeof(struct arm64_addr_mask),
141 	.get = get_arm64_addr_mask,
142 };
143 ELF_REGSET(regset_arm64_addr_mask);
144 
145 void
146 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
147 {
148 	struct arm64_addr_mask addr_mask;
149 	size_t len, mask_size;
150 
151 	len = 0;
152 	if (dst != NULL) {
153 		mask_size = sizeof(addr_mask);
154 		get_arm64_addr_mask(&regset_arm64_addr_mask, td, &addr_mask,
155 		    &mask_size);
156 
157 		len += elf64_populate_note(NT_ARM_ADDR_MASK, &addr_mask, dst,
158 		    sizeof(addr_mask), NULL);
159 	} else {
160 		len += elf64_populate_note(NT_ARM_ADDR_MASK, NULL, NULL,
161 		    sizeof(addr_mask), NULL);
162 	}
163 
164 	*off += len;
165 }
166 
167 bool
168 elf_is_ifunc_reloc(Elf_Size r_info __unused)
169 {
170 
171 	return (ELF_R_TYPE(r_info) == R_AARCH64_IRELATIVE);
172 }
173 
174 static int
175 reloc_instr_imm(Elf32_Addr *where, Elf_Addr val, u_int msb, u_int lsb)
176 {
177 
178 	/* Check bounds: upper bits must be all ones or all zeros. */
179 	if ((uint64_t)((int64_t)val >> (msb + 1)) + 1 > 1)
180 		return (-1);
181 	val >>= lsb;
182 	val &= (1 << (msb - lsb + 1)) - 1;
183 	*where |= (Elf32_Addr)val;
184 	return (0);
185 }
186 
187 /*
188  * Process a relocation.  Support for some static relocations is required
189  * in order for the -zifunc-noplt optimization to work.
190  */
191 static int
192 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
193     int type, int flags, elf_lookup_fn lookup)
194 {
195 #define	ARM64_ELF_RELOC_LOCAL		(1 << 0)
196 #define	ARM64_ELF_RELOC_LATE_IFUNC	(1 << 1)
197 	Elf_Addr *where, addr, addend, val;
198 	Elf_Word rtype, symidx;
199 	const Elf_Rel *rel;
200 	const Elf_Rela *rela;
201 	int error;
202 
203 	switch (type) {
204 	case ELF_RELOC_REL:
205 		rel = (const Elf_Rel *)data;
206 		where = (Elf_Addr *) (relocbase + rel->r_offset);
207 		addend = *where;
208 		rtype = ELF_R_TYPE(rel->r_info);
209 		symidx = ELF_R_SYM(rel->r_info);
210 		break;
211 	case ELF_RELOC_RELA:
212 		rela = (const Elf_Rela *)data;
213 		where = (Elf_Addr *) (relocbase + rela->r_offset);
214 		addend = rela->r_addend;
215 		rtype = ELF_R_TYPE(rela->r_info);
216 		symidx = ELF_R_SYM(rela->r_info);
217 		break;
218 	default:
219 		panic("unknown reloc type %d\n", type);
220 	}
221 
222 	if ((flags & ARM64_ELF_RELOC_LATE_IFUNC) != 0) {
223 		KASSERT(type == ELF_RELOC_RELA,
224 		    ("Only RELA ifunc relocations are supported"));
225 		if (rtype != R_AARCH64_IRELATIVE)
226 			return (0);
227 	}
228 
229 	if ((flags & ARM64_ELF_RELOC_LOCAL) != 0) {
230 		if (rtype == R_AARCH64_RELATIVE)
231 			*where = elf_relocaddr(lf, relocbase + addend);
232 		return (0);
233 	}
234 
235 	error = 0;
236 	switch (rtype) {
237 	case R_AARCH64_NONE:
238 	case R_AARCH64_RELATIVE:
239 		break;
240 	case R_AARCH64_TSTBR14:
241 		error = lookup(lf, symidx, 1, &addr);
242 		if (error != 0)
243 			return (-1);
244 		error = reloc_instr_imm((Elf32_Addr *)where,
245 		    addr + addend - (Elf_Addr)where, 15, 2);
246 		break;
247 	case R_AARCH64_CONDBR19:
248 		error = lookup(lf, symidx, 1, &addr);
249 		if (error != 0)
250 			return (-1);
251 		error = reloc_instr_imm((Elf32_Addr *)where,
252 		    addr + addend - (Elf_Addr)where, 20, 2);
253 		break;
254 	case R_AARCH64_JUMP26:
255 	case R_AARCH64_CALL26:
256 		error = lookup(lf, symidx, 1, &addr);
257 		if (error != 0)
258 			return (-1);
259 		error = reloc_instr_imm((Elf32_Addr *)where,
260 		    addr + addend - (Elf_Addr)where, 27, 2);
261 		break;
262 	case R_AARCH64_ABS64:
263 	case R_AARCH64_GLOB_DAT:
264 	case R_AARCH64_JUMP_SLOT:
265 		error = lookup(lf, symidx, 1, &addr);
266 		if (error != 0)
267 			return (-1);
268 		*where = addr + addend;
269 		break;
270 	case R_AARCH64_IRELATIVE:
271 		addr = relocbase + addend;
272 		val = ((Elf64_Addr (*)(void))addr)();
273 		if (*where != val)
274 			*where = val;
275 		break;
276 	default:
277 		printf("kldload: unexpected relocation type %d, "
278 		    "symbol index %d\n", rtype, symidx);
279 		return (-1);
280 	}
281 	return (error);
282 }
283 
284 int
285 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
286     int type, elf_lookup_fn lookup)
287 {
288 
289 	return (elf_reloc_internal(lf, relocbase, data, type,
290 	    ARM64_ELF_RELOC_LOCAL, lookup));
291 }
292 
293 /* Process one elf relocation with addend. */
294 int
295 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
296     elf_lookup_fn lookup)
297 {
298 
299 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
300 }
301 
302 int
303 elf_reloc_late(linker_file_t lf, Elf_Addr relocbase, const void *data,
304     int type, elf_lookup_fn lookup)
305 {
306 
307 	return (elf_reloc_internal(lf, relocbase, data, type,
308 	    ARM64_ELF_RELOC_LATE_IFUNC, lookup));
309 }
310 
311 int
312 elf_cpu_load_file(linker_file_t lf)
313 {
314 
315 	if (lf->id != 1)
316 		cpu_icache_sync_range((vm_offset_t)lf->address, lf->size);
317 	return (0);
318 }
319 
320 int
321 elf_cpu_unload_file(linker_file_t lf __unused)
322 {
323 
324 	return (0);
325 }
326 
327 int
328 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
329 {
330 
331 	return (0);
332 }
333