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