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 u_long __read_frequently elf_hwcap; 59 u_long __read_frequently elf_hwcap2; 60 61 static struct sysentvec elf64_freebsd_sysvec = { 62 .sv_size = SYS_MAXSYSCALL, 63 .sv_table = sysent, 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_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_imgact_try = NULL, 75 .sv_minsigstksz = MINSIGSTKSZ, 76 .sv_minuser = VM_MIN_ADDRESS, 77 .sv_maxuser = VM_MAXUSER_ADDRESS, 78 .sv_usrstack = USRSTACK, 79 .sv_psstrings = 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_stackgap = elf64_stackgap, 97 .sv_hwcap = &elf_hwcap, 98 .sv_hwcap2 = &elf_hwcap2, 99 .sv_onexec_old = exec_onexec_old, 100 .sv_onexit = exit_onexit, 101 }; 102 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec); 103 104 static Elf64_Brandinfo freebsd_brand_info = { 105 .brand = ELFOSABI_FREEBSD, 106 .machine = EM_AARCH64, 107 .compat_3_brand = "FreeBSD", 108 .emul_path = NULL, 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 void 120 elf64_dump_thread(struct thread *td __unused, void *dst __unused, 121 size_t *off __unused) 122 { 123 124 } 125 126 bool 127 elf_is_ifunc_reloc(Elf_Size r_info __unused) 128 { 129 130 return (ELF_R_TYPE(r_info) == R_AARCH64_IRELATIVE); 131 } 132 133 static int 134 reloc_instr_imm(Elf32_Addr *where, Elf_Addr val, u_int msb, u_int lsb) 135 { 136 137 /* Check bounds: upper bits must be all ones or all zeros. */ 138 if ((uint64_t)((int64_t)val >> (msb + 1)) + 1 > 1) 139 return (-1); 140 val >>= lsb; 141 val &= (1 << (msb - lsb + 1)) - 1; 142 *where |= (Elf32_Addr)val; 143 return (0); 144 } 145 146 /* 147 * Process a relocation. Support for some static relocations is required 148 * in order for the -zifunc-noplt optimization to work. 149 */ 150 static int 151 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data, 152 int type, int flags, elf_lookup_fn lookup) 153 { 154 #define ARM64_ELF_RELOC_LOCAL (1 << 0) 155 #define ARM64_ELF_RELOC_LATE_IFUNC (1 << 1) 156 Elf_Addr *where, addr, addend, val; 157 Elf_Word rtype, symidx; 158 const Elf_Rel *rel; 159 const Elf_Rela *rela; 160 int error; 161 162 switch (type) { 163 case ELF_RELOC_REL: 164 rel = (const Elf_Rel *)data; 165 where = (Elf_Addr *) (relocbase + rel->r_offset); 166 addend = *where; 167 rtype = ELF_R_TYPE(rel->r_info); 168 symidx = ELF_R_SYM(rel->r_info); 169 break; 170 case ELF_RELOC_RELA: 171 rela = (const Elf_Rela *)data; 172 where = (Elf_Addr *) (relocbase + rela->r_offset); 173 addend = rela->r_addend; 174 rtype = ELF_R_TYPE(rela->r_info); 175 symidx = ELF_R_SYM(rela->r_info); 176 break; 177 default: 178 panic("unknown reloc type %d\n", type); 179 } 180 181 if ((flags & ARM64_ELF_RELOC_LATE_IFUNC) != 0) { 182 KASSERT(type == ELF_RELOC_RELA, 183 ("Only RELA ifunc relocations are supported")); 184 if (rtype != R_AARCH64_IRELATIVE) 185 return (0); 186 } 187 188 if ((flags & ARM64_ELF_RELOC_LOCAL) != 0) { 189 if (rtype == R_AARCH64_RELATIVE) 190 *where = elf_relocaddr(lf, relocbase + addend); 191 return (0); 192 } 193 194 error = 0; 195 switch (rtype) { 196 case R_AARCH64_NONE: 197 case R_AARCH64_RELATIVE: 198 break; 199 case R_AARCH64_TSTBR14: 200 error = lookup(lf, symidx, 1, &addr); 201 if (error != 0) 202 return (-1); 203 error = reloc_instr_imm((Elf32_Addr *)where, 204 addr + addend - (Elf_Addr)where, 15, 2); 205 break; 206 case R_AARCH64_CONDBR19: 207 error = lookup(lf, symidx, 1, &addr); 208 if (error != 0) 209 return (-1); 210 error = reloc_instr_imm((Elf32_Addr *)where, 211 addr + addend - (Elf_Addr)where, 20, 2); 212 break; 213 case R_AARCH64_JUMP26: 214 case R_AARCH64_CALL26: 215 error = lookup(lf, symidx, 1, &addr); 216 if (error != 0) 217 return (-1); 218 error = reloc_instr_imm((Elf32_Addr *)where, 219 addr + addend - (Elf_Addr)where, 27, 2); 220 break; 221 case R_AARCH64_ABS64: 222 case R_AARCH64_GLOB_DAT: 223 case R_AARCH64_JUMP_SLOT: 224 error = lookup(lf, symidx, 1, &addr); 225 if (error != 0) 226 return (-1); 227 *where = addr + addend; 228 break; 229 case R_AARCH64_IRELATIVE: 230 addr = relocbase + addend; 231 val = ((Elf64_Addr (*)(void))addr)(); 232 if (*where != val) 233 *where = val; 234 break; 235 default: 236 printf("kldload: unexpected relocation type %d, " 237 "symbol index %d\n", rtype, symidx); 238 return (-1); 239 } 240 return (error); 241 } 242 243 int 244 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data, 245 int type, elf_lookup_fn lookup) 246 { 247 248 return (elf_reloc_internal(lf, relocbase, data, type, 249 ARM64_ELF_RELOC_LOCAL, lookup)); 250 } 251 252 /* Process one elf relocation with addend. */ 253 int 254 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 255 elf_lookup_fn lookup) 256 { 257 258 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup)); 259 } 260 261 int 262 elf_reloc_late(linker_file_t lf, Elf_Addr relocbase, const void *data, 263 int type, elf_lookup_fn lookup) 264 { 265 266 return (elf_reloc_internal(lf, relocbase, data, type, 267 ARM64_ELF_RELOC_LATE_IFUNC, lookup)); 268 } 269 270 int 271 elf_cpu_load_file(linker_file_t lf) 272 { 273 274 if (lf->id != 1) 275 cpu_icache_sync_range((vm_offset_t)lf->address, lf->size); 276 return (0); 277 } 278 279 int 280 elf_cpu_unload_file(linker_file_t lf __unused) 281 { 282 283 return (0); 284 } 285 286 int 287 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused) 288 { 289 290 return (0); 291 } 292