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