]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/elf_machdep.c
Implement in-kernel relocator for the arm64 module linker.
[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/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
58 static struct sysentvec elf64_freebsd_sysvec = {
59         .sv_size        = SYS_MAXSYSCALL,
60         .sv_table       = sysent,
61         .sv_mask        = 0,
62         .sv_sigsize     = 0,
63         .sv_sigtbl      = NULL,
64         .sv_errsize     = 0,
65         .sv_errtbl      = NULL,
66         .sv_transtrap   = NULL,
67         .sv_fixup       = __elfN(freebsd_fixup),
68         .sv_sendsig     = sendsig,
69         .sv_sigcode     = sigcode,
70         .sv_szsigcode   = &szsigcode,
71         .sv_prepsyscall = NULL,
72         .sv_name        = "FreeBSD ELF64",
73         .sv_coredump    = __elfN(coredump),
74         .sv_imgact_try  = NULL,
75         .sv_minsigstksz = MINSIGSTKSZ,
76         .sv_pagesize    = PAGE_SIZE,
77         .sv_minuser     = VM_MIN_ADDRESS,
78         .sv_maxuser     = VM_MAXUSER_ADDRESS,
79         .sv_usrstack    = USRSTACK,
80         .sv_psstrings   = PS_STRINGS,
81         .sv_stackprot   = VM_PROT_READ | VM_PROT_WRITE,
82         .sv_copyout_strings = exec_copyout_strings,
83         .sv_setregs     = exec_setregs,
84         .sv_fixlimit    = NULL,
85         .sv_maxssiz     = NULL,
86         .sv_flags       = SV_ABI_FREEBSD | SV_LP64,
87         .sv_set_syscall_retval = cpu_set_syscall_retval,
88         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
89         .sv_syscallnames = syscallnames,
90         .sv_schedtail   = NULL,
91 };
92
93 static Elf64_Brandinfo freebsd_brand_info = {
94         .brand          = ELFOSABI_FREEBSD,
95         .machine        = EM_AARCH64,
96         .compat_3_brand = "FreeBSD",
97         .emul_path      = NULL,
98         .interp_path    = "/libexec/ld-elf.so.1",
99         .sysvec         = &elf64_freebsd_sysvec,
100         .interp_newpath = NULL,
101         .brand_note     = &elf64_freebsd_brandnote,
102         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
103 };
104
105 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
106     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
107
108 static Elf64_Brandinfo freebsd_brand_oinfo = {
109         .brand          = ELFOSABI_FREEBSD,
110         .machine        = EM_AARCH64,
111         .compat_3_brand = "FreeBSD",
112         .emul_path      = NULL,
113         .interp_path    = "/usr/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(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
121     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_oinfo);
122
123 void
124 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
125     size_t *off __unused)
126 {
127
128 }
129
130 static int
131 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
132     int type, int local, elf_lookup_fn lookup)
133 {
134         Elf_Addr *where, addr, addend;
135         Elf_Word rtype, symidx;
136         const Elf_Rel *rel;
137         const Elf_Rela *rela;
138         int error;
139
140         switch (type) {
141         case ELF_RELOC_REL:
142                 rel = (const Elf_Rel *)data;
143                 where = (Elf_Addr *) (relocbase + rel->r_offset);
144                 addend = *where;
145                 rtype = ELF_R_TYPE(rel->r_info);
146                 symidx = ELF_R_SYM(rel->r_info);
147                 break;
148         case ELF_RELOC_RELA:
149                 rela = (const Elf_Rela *)data;
150                 where = (Elf_Addr *) (relocbase + rela->r_offset);
151                 addend = rela->r_addend;
152                 rtype = ELF_R_TYPE(rela->r_info);
153                 symidx = ELF_R_SYM(rela->r_info);
154                 break;
155         default:
156                 panic("unknown reloc type %d\n", type);
157         }
158
159         if (local) {
160                 if (rtype == R_AARCH64_RELATIVE)
161                         *where = elf_relocaddr(lf, relocbase + addend);
162                 return (0);
163         }
164
165         switch (rtype) {
166         case R_AARCH64_NONE:
167         case R_AARCH64_RELATIVE:
168                 break;
169         case R_AARCH64_ABS64:
170         case R_AARCH64_GLOB_DAT:
171         case R_AARCH64_JUMP_SLOT:
172                 error = lookup(lf, symidx, 1, &addr);
173                 if (error != 0)
174                         return (-1);
175                 *where = addr + addend;
176                 break;
177         default:
178                 printf("kldload: unexpected relocation type %d\n", rtype);
179                 return (-1);
180         }
181         return (0);
182 }
183
184 int
185 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
186     int type, elf_lookup_fn lookup)
187 {
188
189         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
190 }
191
192 /* Process one elf relocation with addend. */
193 int
194 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
195     elf_lookup_fn lookup)
196 {
197
198         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
199 }
200
201 int
202 elf_cpu_load_file(linker_file_t lf)
203 {
204
205         if (lf->id != 1)
206                 cpu_icache_sync_range((vm_offset_t)lf->address, lf->size);
207         return (0);
208 }
209
210 int
211 elf_cpu_unload_file(linker_file_t lf __unused)
212 {
213
214         return (0);
215 }