]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/elf_machdep.c
Merge llvm-project release/15.x llvmorg-15.0.7-0-g8dfdcc7b7bf6
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / elf_machdep.c
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_fixup       = __elfN(freebsd_fixup),
68         .sv_sendsig     = sendsig,
69         .sv_sigcode     = sigcode,
70         .sv_szsigcode   = &szsigcode,
71         .sv_name        = "FreeBSD ELF64",
72         .sv_coredump    = __elfN(coredump),
73         .sv_elf_core_osabi = ELFOSABI_FREEBSD,
74         .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
75         .sv_elf_core_prepare_notes = __elfN(prepare_notes),
76         .sv_imgact_try  = NULL,
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,
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         .emul_path      = NULL,
113         .interp_path    = "/libexec/ld-elf.so.1",
114         .sysvec         = &elf64_freebsd_sysvec,
115         .interp_newpath = NULL,
116         .brand_note     = &elf64_freebsd_brandnote,
117         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
118 };
119
120 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
121     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
122
123 static bool
124 get_arm64_addr_mask(struct regset *rs, struct thread *td, void *buf,
125     size_t *sizep)
126 {
127         if (buf != NULL) {
128                 KASSERT(*sizep == sizeof(elf64_addr_mask),
129                     ("%s: invalid size", __func__));
130                 memcpy(buf, &elf64_addr_mask, sizeof(elf64_addr_mask));
131         }
132         *sizep = sizeof(elf64_addr_mask);
133
134         return (true);
135 }
136
137 static struct regset regset_arm64_addr_mask = {
138         .note = NT_ARM_ADDR_MASK,
139         .size = sizeof(struct arm64_addr_mask),
140         .get = get_arm64_addr_mask,
141 };
142 ELF_REGSET(regset_arm64_addr_mask);
143
144 void
145 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
146     size_t *off __unused)
147 {
148 }
149
150 bool
151 elf_is_ifunc_reloc(Elf_Size r_info __unused)
152 {
153
154         return (ELF_R_TYPE(r_info) == R_AARCH64_IRELATIVE);
155 }
156
157 static int
158 reloc_instr_imm(Elf32_Addr *where, Elf_Addr val, u_int msb, u_int lsb)
159 {
160
161         /* Check bounds: upper bits must be all ones or all zeros. */
162         if ((uint64_t)((int64_t)val >> (msb + 1)) + 1 > 1)
163                 return (-1);
164         val >>= lsb;
165         val &= (1 << (msb - lsb + 1)) - 1;
166         *where |= (Elf32_Addr)val;
167         return (0);
168 }
169
170 /*
171  * Process a relocation.  Support for some static relocations is required
172  * in order for the -zifunc-noplt optimization to work.
173  */
174 static int
175 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
176     int type, int flags, elf_lookup_fn lookup)
177 {
178 #define ARM64_ELF_RELOC_LOCAL           (1 << 0)
179 #define ARM64_ELF_RELOC_LATE_IFUNC      (1 << 1)
180         Elf_Addr *where, addr, addend, val;
181         Elf_Word rtype, symidx;
182         const Elf_Rel *rel;
183         const Elf_Rela *rela;
184         int error;
185
186         switch (type) {
187         case ELF_RELOC_REL:
188                 rel = (const Elf_Rel *)data;
189                 where = (Elf_Addr *) (relocbase + rel->r_offset);
190                 addend = *where;
191                 rtype = ELF_R_TYPE(rel->r_info);
192                 symidx = ELF_R_SYM(rel->r_info);
193                 break;
194         case ELF_RELOC_RELA:
195                 rela = (const Elf_Rela *)data;
196                 where = (Elf_Addr *) (relocbase + rela->r_offset);
197                 addend = rela->r_addend;
198                 rtype = ELF_R_TYPE(rela->r_info);
199                 symidx = ELF_R_SYM(rela->r_info);
200                 break;
201         default:
202                 panic("unknown reloc type %d\n", type);
203         }
204
205         if ((flags & ARM64_ELF_RELOC_LATE_IFUNC) != 0) {
206                 KASSERT(type == ELF_RELOC_RELA,
207                     ("Only RELA ifunc relocations are supported"));
208                 if (rtype != R_AARCH64_IRELATIVE)
209                         return (0);
210         }
211
212         if ((flags & ARM64_ELF_RELOC_LOCAL) != 0) {
213                 if (rtype == R_AARCH64_RELATIVE)
214                         *where = elf_relocaddr(lf, relocbase + addend);
215                 return (0);
216         }
217
218         error = 0;
219         switch (rtype) {
220         case R_AARCH64_NONE:
221         case R_AARCH64_RELATIVE:
222                 break;
223         case R_AARCH64_TSTBR14:
224                 error = lookup(lf, symidx, 1, &addr);
225                 if (error != 0)
226                         return (-1);
227                 error = reloc_instr_imm((Elf32_Addr *)where,
228                     addr + addend - (Elf_Addr)where, 15, 2);
229                 break;
230         case R_AARCH64_CONDBR19:
231                 error = lookup(lf, symidx, 1, &addr);
232                 if (error != 0)
233                         return (-1);
234                 error = reloc_instr_imm((Elf32_Addr *)where,
235                     addr + addend - (Elf_Addr)where, 20, 2);
236                 break;
237         case R_AARCH64_JUMP26:
238         case R_AARCH64_CALL26:
239                 error = lookup(lf, symidx, 1, &addr);
240                 if (error != 0)
241                         return (-1);
242                 error = reloc_instr_imm((Elf32_Addr *)where,
243                     addr + addend - (Elf_Addr)where, 27, 2);
244                 break;
245         case R_AARCH64_ABS64:
246         case R_AARCH64_GLOB_DAT:
247         case R_AARCH64_JUMP_SLOT:
248                 error = lookup(lf, symidx, 1, &addr);
249                 if (error != 0)
250                         return (-1);
251                 *where = addr + addend;
252                 break;
253         case R_AARCH64_IRELATIVE:
254                 addr = relocbase + addend;
255                 val = ((Elf64_Addr (*)(void))addr)();
256                 if (*where != val)
257                         *where = val;
258                 break;
259         default:
260                 printf("kldload: unexpected relocation type %d, "
261                     "symbol index %d\n", rtype, symidx);
262                 return (-1);
263         }
264         return (error);
265 }
266
267 int
268 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
269     int type, elf_lookup_fn lookup)
270 {
271
272         return (elf_reloc_internal(lf, relocbase, data, type,
273             ARM64_ELF_RELOC_LOCAL, lookup));
274 }
275
276 /* Process one elf relocation with addend. */
277 int
278 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
279     elf_lookup_fn lookup)
280 {
281
282         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
283 }
284
285 int
286 elf_reloc_late(linker_file_t lf, Elf_Addr relocbase, const void *data,
287     int type, elf_lookup_fn lookup)
288 {
289
290         return (elf_reloc_internal(lf, relocbase, data, type,
291             ARM64_ELF_RELOC_LATE_IFUNC, lookup));
292 }
293
294 int
295 elf_cpu_load_file(linker_file_t lf)
296 {
297
298         if (lf->id != 1)
299                 cpu_icache_sync_range((vm_offset_t)lf->address, lf->size);
300         return (0);
301 }
302
303 int
304 elf_cpu_unload_file(linker_file_t lf __unused)
305 {
306
307         return (0);
308 }
309
310 int
311 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
312 {
313
314         return (0);
315 }