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
58u_long elf_hwcap;
59
60static struct sysentvec elf64_freebsd_sysvec = {
61	.sv_size	= SYS_MAXSYSCALL,
62	.sv_table	= sysent,
63	.sv_errsize	= 0,
64	.sv_errtbl	= NULL,
65	.sv_transtrap	= NULL,
66	.sv_fixup	= __elfN(freebsd_fixup),
67	.sv_sendsig	= sendsig,
68	.sv_sigcode	= sigcode,
69	.sv_szsigcode	= &szsigcode,
70	.sv_name	= "FreeBSD ELF64",
71	.sv_coredump	= __elfN(coredump),
72	.sv_imgact_try	= NULL,
73	.sv_minsigstksz	= MINSIGSTKSZ,
74	.sv_minuser	= VM_MIN_ADDRESS,
75	.sv_maxuser	= VM_MAXUSER_ADDRESS,
76	.sv_usrstack	= USRSTACK,
77	.sv_psstrings	= PS_STRINGS,
78	.sv_stackprot	= VM_PROT_READ | VM_PROT_WRITE,
79	.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
80	.sv_copyout_strings = exec_copyout_strings,
81	.sv_setregs	= exec_setregs,
82	.sv_fixlimit	= NULL,
83	.sv_maxssiz	= NULL,
84	.sv_flags	= SV_SHP | SV_TIMEKEEP | SV_ABI_FREEBSD | SV_LP64 |
85	    SV_ASLR,
86	.sv_set_syscall_retval = cpu_set_syscall_retval,
87	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
88	.sv_syscallnames = syscallnames,
89	.sv_shared_page_base = SHAREDPAGE,
90	.sv_shared_page_len = PAGE_SIZE,
91	.sv_schedtail	= NULL,
92	.sv_thread_detach = NULL,
93	.sv_trap	= NULL,
94	.sv_hwcap	= &elf_hwcap,
95};
96INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
97
98static Elf64_Brandinfo freebsd_brand_info = {
99	.brand		= ELFOSABI_FREEBSD,
100	.machine	= EM_AARCH64,
101	.compat_3_brand	= "FreeBSD",
102	.emul_path	= NULL,
103	.interp_path	= "/libexec/ld-elf.so.1",
104	.sysvec		= &elf64_freebsd_sysvec,
105	.interp_newpath	= NULL,
106	.brand_note	= &elf64_freebsd_brandnote,
107	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
108};
109
110SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
111    (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
112
113void
114elf64_dump_thread(struct thread *td __unused, void *dst __unused,
115    size_t *off __unused)
116{
117
118}
119
120bool
121elf_is_ifunc_reloc(Elf_Size r_info __unused)
122{
123
124	return (ELF_R_TYPE(r_info) == R_AARCH64_IRELATIVE);
125}
126
127static int
128reloc_instr_imm(Elf32_Addr *where, Elf_Addr val, u_int msb, u_int lsb)
129{
130
131	/* Check bounds: upper bits must be all ones or all zeros. */
132	if ((uint64_t)((int64_t)val >> (msb + 1)) + 1 > 1)
133		return (-1);
134	val >>= lsb;
135	val &= (1 << (msb - lsb + 1)) - 1;
136	*where |= (Elf32_Addr)val;
137	return (0);
138}
139
140/*
141 * Process a relocation.  Support for some static relocations is required
142 * in order for the -zifunc-noplt optimization to work.
143 */
144static int
145elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
146    int type, int local, elf_lookup_fn lookup)
147{
148	Elf_Addr *where, addr, addend, val;
149	Elf_Word rtype, symidx;
150	const Elf_Rel *rel;
151	const Elf_Rela *rela;
152	int error;
153
154	switch (type) {
155	case ELF_RELOC_REL:
156		rel = (const Elf_Rel *)data;
157		where = (Elf_Addr *) (relocbase + rel->r_offset);
158		addend = *where;
159		rtype = ELF_R_TYPE(rel->r_info);
160		symidx = ELF_R_SYM(rel->r_info);
161		break;
162	case ELF_RELOC_RELA:
163		rela = (const Elf_Rela *)data;
164		where = (Elf_Addr *) (relocbase + rela->r_offset);
165		addend = rela->r_addend;
166		rtype = ELF_R_TYPE(rela->r_info);
167		symidx = ELF_R_SYM(rela->r_info);
168		break;
169	default:
170		panic("unknown reloc type %d\n", type);
171	}
172
173	if (local) {
174		if (rtype == R_AARCH64_RELATIVE)
175			*where = elf_relocaddr(lf, relocbase + addend);
176		return (0);
177	}
178
179	error = 0;
180	switch (rtype) {
181	case R_AARCH64_NONE:
182	case R_AARCH64_RELATIVE:
183		break;
184	case R_AARCH64_TSTBR14:
185		error = lookup(lf, symidx, 1, &addr);
186		if (error != 0)
187			return (-1);
188		error = reloc_instr_imm((Elf32_Addr *)where,
189		    addr + addend - (Elf_Addr)where, 15, 2);
190		break;
191	case R_AARCH64_CONDBR19:
192		error = lookup(lf, symidx, 1, &addr);
193		if (error != 0)
194			return (-1);
195		error = reloc_instr_imm((Elf32_Addr *)where,
196		    addr + addend - (Elf_Addr)where, 20, 2);
197		break;
198	case R_AARCH64_JUMP26:
199	case R_AARCH64_CALL26:
200		error = lookup(lf, symidx, 1, &addr);
201		if (error != 0)
202			return (-1);
203		error = reloc_instr_imm((Elf32_Addr *)where,
204		    addr + addend - (Elf_Addr)where, 27, 2);
205		break;
206	case R_AARCH64_ABS64:
207	case R_AARCH64_GLOB_DAT:
208	case R_AARCH64_JUMP_SLOT:
209		error = lookup(lf, symidx, 1, &addr);
210		if (error != 0)
211			return (-1);
212		*where = addr + addend;
213		break;
214	case R_AARCH64_IRELATIVE:
215		addr = relocbase + addend;
216		val = ((Elf64_Addr (*)(void))addr)();
217		if (*where != val)
218			*where = val;
219		break;
220	default:
221		printf("kldload: unexpected relocation type %d\n", rtype);
222		return (-1);
223	}
224	return (error);
225}
226
227int
228elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
229    int type, elf_lookup_fn lookup)
230{
231
232	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
233}
234
235/* Process one elf relocation with addend. */
236int
237elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
238    elf_lookup_fn lookup)
239{
240
241	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
242}
243
244int
245elf_cpu_load_file(linker_file_t lf)
246{
247
248	if (lf->id != 1)
249		cpu_icache_sync_range((vm_offset_t)lf->address, lf->size);
250	return (0);
251}
252
253int
254elf_cpu_unload_file(linker_file_t lf __unused)
255{
256
257	return (0);
258}
259
260int
261elf_cpu_parse_dynamic(linker_file_t lf __unused, Elf_Dyn *dynamic __unused)
262{
263
264	return (0);
265}
266