xref: /freebsd/sys/arm64/arm64/elf_machdep.c (revision 53b70c86)
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/sysent.h>
45 #include <sys/imgact_elf.h>
46 #include <sys/syscall.h>
47 #include <sys/signalvar.h>
48 #include <sys/vnode.h>
49 
50 #include <vm/vm.h>
51 #include <vm/vm_param.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 
61 static struct sysentvec elf64_freebsd_sysvec = {
62 	.sv_size	= SYS_MAXSYSCALL,
63 	.sv_table	= sysent,
64 	.sv_transtrap	= NULL,
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_imgact_try	= NULL,
75 	.sv_minsigstksz	= MINSIGSTKSZ,
76 	.sv_minuser	= VM_MIN_ADDRESS,
77 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
78 	.sv_usrstack	= USRSTACK,
79 	.sv_psstrings	= 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,
84 	.sv_fixlimit	= NULL,
85 	.sv_maxssiz	= NULL,
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,
93 	.sv_schedtail	= NULL,
94 	.sv_thread_detach = NULL,
95 	.sv_trap	= NULL,
96 	.sv_hwcap	= &elf_hwcap,
97 	.sv_hwcap2	= &elf_hwcap2,
98 	.sv_onexec_old	= exec_onexec_old,
99 	.sv_onexit	= exit_onexit,
100 };
101 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
102 
103 static Elf64_Brandinfo freebsd_brand_info = {
104 	.brand		= ELFOSABI_FREEBSD,
105 	.machine	= EM_AARCH64,
106 	.compat_3_brand	= "FreeBSD",
107 	.emul_path	= NULL,
108 	.interp_path	= "/libexec/ld-elf.so.1",
109 	.sysvec		= &elf64_freebsd_sysvec,
110 	.interp_newpath	= NULL,
111 	.brand_note	= &elf64_freebsd_brandnote,
112 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
113 };
114 
115 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
116     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
117 
118 void
119 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
120     size_t *off __unused)
121 {
122 
123 }
124 
125 bool
126 elf_is_ifunc_reloc(Elf_Size r_info __unused)
127 {
128 
129 	return (ELF_R_TYPE(r_info) == R_AARCH64_IRELATIVE);
130 }
131 
132 static int
133 reloc_instr_imm(Elf32_Addr *where, Elf_Addr val, u_int msb, u_int lsb)
134 {
135 
136 	/* Check bounds: upper bits must be all ones or all zeros. */
137 	if ((uint64_t)((int64_t)val >> (msb + 1)) + 1 > 1)
138 		return (-1);
139 	val >>= lsb;
140 	val &= (1 << (msb - lsb + 1)) - 1;
141 	*where |= (Elf32_Addr)val;
142 	return (0);
143 }
144 
145 /*
146  * Process a relocation.  Support for some static relocations is required
147  * in order for the -zifunc-noplt optimization to work.
148  */
149 static int
150 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
151     int type, int flags, elf_lookup_fn lookup)
152 {
153 #define	ARM64_ELF_RELOC_LOCAL		(1 << 0)
154 #define	ARM64_ELF_RELOC_LATE_IFUNC	(1 << 1)
155 	Elf_Addr *where, addr, addend, val;
156 	Elf_Word rtype, symidx;
157 	const Elf_Rel *rel;
158 	const Elf_Rela *rela;
159 	int error;
160 
161 	switch (type) {
162 	case ELF_RELOC_REL:
163 		rel = (const Elf_Rel *)data;
164 		where = (Elf_Addr *) (relocbase + rel->r_offset);
165 		addend = *where;
166 		rtype = ELF_R_TYPE(rel->r_info);
167 		symidx = ELF_R_SYM(rel->r_info);
168 		break;
169 	case ELF_RELOC_RELA:
170 		rela = (const Elf_Rela *)data;
171 		where = (Elf_Addr *) (relocbase + rela->r_offset);
172 		addend = rela->r_addend;
173 		rtype = ELF_R_TYPE(rela->r_info);
174 		symidx = ELF_R_SYM(rela->r_info);
175 		break;
176 	default:
177 		panic("unknown reloc type %d\n", type);
178 	}
179 
180 	if ((flags & ARM64_ELF_RELOC_LATE_IFUNC) != 0) {
181 		KASSERT(type == ELF_RELOC_RELA,
182 		    ("Only RELA ifunc relocations are supported"));
183 		if (rtype != R_AARCH64_IRELATIVE)
184 			return (0);
185 	}
186 
187 	if ((flags & ARM64_ELF_RELOC_LOCAL) != 0) {
188 		if (rtype == R_AARCH64_RELATIVE)
189 			*where = elf_relocaddr(lf, relocbase + addend);
190 		return (0);
191 	}
192 
193 	error = 0;
194 	switch (rtype) {
195 	case R_AARCH64_NONE:
196 	case R_AARCH64_RELATIVE:
197 		break;
198 	case R_AARCH64_TSTBR14:
199 		error = lookup(lf, symidx, 1, &addr);
200 		if (error != 0)
201 			return (-1);
202 		error = reloc_instr_imm((Elf32_Addr *)where,
203 		    addr + addend - (Elf_Addr)where, 15, 2);
204 		break;
205 	case R_AARCH64_CONDBR19:
206 		error = lookup(lf, symidx, 1, &addr);
207 		if (error != 0)
208 			return (-1);
209 		error = reloc_instr_imm((Elf32_Addr *)where,
210 		    addr + addend - (Elf_Addr)where, 20, 2);
211 		break;
212 	case R_AARCH64_JUMP26:
213 	case R_AARCH64_CALL26:
214 		error = lookup(lf, symidx, 1, &addr);
215 		if (error != 0)
216 			return (-1);
217 		error = reloc_instr_imm((Elf32_Addr *)where,
218 		    addr + addend - (Elf_Addr)where, 27, 2);
219 		break;
220 	case R_AARCH64_ABS64:
221 	case R_AARCH64_GLOB_DAT:
222 	case R_AARCH64_JUMP_SLOT:
223 		error = lookup(lf, symidx, 1, &addr);
224 		if (error != 0)
225 			return (-1);
226 		*where = addr + addend;
227 		break;
228 	case R_AARCH64_IRELATIVE:
229 		addr = relocbase + addend;
230 		val = ((Elf64_Addr (*)(void))addr)();
231 		if (*where != val)
232 			*where = val;
233 		break;
234 	default:
235 		printf("kldload: unexpected relocation type %d, "
236 		    "symbol index %d\n", rtype, symidx);
237 		return (-1);
238 	}
239 	return (error);
240 }
241 
242 int
243 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
244     int type, elf_lookup_fn lookup)
245 {
246 
247 	return (elf_reloc_internal(lf, relocbase, data, type,
248 	    ARM64_ELF_RELOC_LOCAL, lookup));
249 }
250 
251 /* Process one elf relocation with addend. */
252 int
253 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
254     elf_lookup_fn lookup)
255 {
256 
257 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
258 }
259 
260 int
261 elf_reloc_late(linker_file_t lf, Elf_Addr relocbase, const void *data,
262     int type, elf_lookup_fn lookup)
263 {
264 
265 	return (elf_reloc_internal(lf, relocbase, data, type,
266 	    ARM64_ELF_RELOC_LATE_IFUNC, lookup));
267 }
268 
269 int
270 elf_cpu_load_file(linker_file_t lf)
271 {
272 
273 	if (lf->id != 1)
274 		cpu_icache_sync_range((vm_offset_t)lf->address, lf->size);
275 	return (0);
276 }
277 
278 int
279 elf_cpu_unload_file(linker_file_t lf __unused)
280 {
281 
282 	return (0);
283 }
284 
285 int
286 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
287 {
288 
289 	return (0);
290 }
291