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