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_errsize = 0, 63 .sv_errtbl = NULL, 64 .sv_transtrap = NULL, 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_imgact_try = NULL, 72 .sv_minsigstksz = MINSIGSTKSZ, 73 .sv_pagesize = PAGE_SIZE, 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_strings = exec_copyout_strings, 80 .sv_setregs = exec_setregs, 81 .sv_fixlimit = NULL, 82 .sv_maxssiz = NULL, 83 .sv_flags = SV_SHP | SV_TIMEKEEP | SV_ABI_FREEBSD | SV_LP64, 84 .sv_set_syscall_retval = cpu_set_syscall_retval, 85 .sv_fetch_syscall_args = cpu_fetch_syscall_args, 86 .sv_syscallnames = syscallnames, 87 .sv_shared_page_base = SHAREDPAGE, 88 .sv_shared_page_len = PAGE_SIZE, 89 .sv_schedtail = NULL, 90 .sv_thread_detach = NULL, 91 .sv_trap = NULL, 92 }; 93 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec); 94 95 static Elf64_Brandinfo freebsd_brand_info = { 96 .brand = ELFOSABI_FREEBSD, 97 .machine = EM_AARCH64, 98 .compat_3_brand = "FreeBSD", 99 .emul_path = NULL, 100 .interp_path = "/libexec/ld-elf.so.1", 101 .sysvec = &elf64_freebsd_sysvec, 102 .interp_newpath = NULL, 103 .brand_note = &elf64_freebsd_brandnote, 104 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 105 }; 106 107 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST, 108 (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info); 109 110 static Elf64_Brandinfo freebsd_brand_oinfo = { 111 .brand = ELFOSABI_FREEBSD, 112 .machine = EM_AARCH64, 113 .compat_3_brand = "FreeBSD", 114 .emul_path = NULL, 115 .interp_path = "/usr/libexec/ld-elf.so.1", 116 .sysvec = &elf64_freebsd_sysvec, 117 .interp_newpath = NULL, 118 .brand_note = &elf64_freebsd_brandnote, 119 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 120 }; 121 122 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY, 123 (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_oinfo); 124 125 void 126 elf64_dump_thread(struct thread *td __unused, void *dst __unused, 127 size_t *off __unused) 128 { 129 130 } 131 132 static int 133 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data, 134 int type, int local, elf_lookup_fn lookup) 135 { 136 Elf_Addr *where, addr, addend; 137 Elf_Word rtype, symidx; 138 const Elf_Rel *rel; 139 const Elf_Rela *rela; 140 int error; 141 142 switch (type) { 143 case ELF_RELOC_REL: 144 rel = (const Elf_Rel *)data; 145 where = (Elf_Addr *) (relocbase + rel->r_offset); 146 addend = *where; 147 rtype = ELF_R_TYPE(rel->r_info); 148 symidx = ELF_R_SYM(rel->r_info); 149 break; 150 case ELF_RELOC_RELA: 151 rela = (const Elf_Rela *)data; 152 where = (Elf_Addr *) (relocbase + rela->r_offset); 153 addend = rela->r_addend; 154 rtype = ELF_R_TYPE(rela->r_info); 155 symidx = ELF_R_SYM(rela->r_info); 156 break; 157 default: 158 panic("unknown reloc type %d\n", type); 159 } 160 161 if (local) { 162 if (rtype == R_AARCH64_RELATIVE) 163 *where = elf_relocaddr(lf, relocbase + addend); 164 return (0); 165 } 166 167 switch (rtype) { 168 case R_AARCH64_NONE: 169 case R_AARCH64_RELATIVE: 170 break; 171 case R_AARCH64_ABS64: 172 case R_AARCH64_GLOB_DAT: 173 case R_AARCH64_JUMP_SLOT: 174 error = lookup(lf, symidx, 1, &addr); 175 if (error != 0) 176 return (-1); 177 *where = addr + addend; 178 break; 179 default: 180 printf("kldload: unexpected relocation type %d\n", rtype); 181 return (-1); 182 } 183 return (0); 184 } 185 186 int 187 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data, 188 int type, elf_lookup_fn lookup) 189 { 190 191 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup)); 192 } 193 194 /* Process one elf relocation with addend. */ 195 int 196 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 197 elf_lookup_fn lookup) 198 { 199 200 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup)); 201 } 202 203 int 204 elf_cpu_load_file(linker_file_t lf) 205 { 206 207 if (lf->id != 1) 208 cpu_icache_sync_range((vm_offset_t)lf->address, lf->size); 209 return (0); 210 } 211 212 int 213 elf_cpu_unload_file(linker_file_t lf __unused) 214 { 215 216 return (0); 217 } 218