1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright 1996-1998 John D. Polstra. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #include <sys/param.h> 30 #include <sys/kernel.h> 31 #include <sys/systm.h> 32 #include <sys/exec.h> 33 #include <sys/imgact.h> 34 #include <sys/linker.h> 35 #include <sys/reg.h> 36 #include <sys/sysent.h> 37 #include <sys/imgact_elf.h> 38 #include <sys/proc.h> 39 #include <sys/syscall.h> 40 #include <sys/signalvar.h> 41 #include <sys/vnode.h> 42 43 #include <vm/vm.h> 44 #include <vm/pmap.h> 45 #include <vm/vm_param.h> 46 47 #include <machine/elf.h> 48 #include <machine/md_var.h> 49 #include <machine/stack.h> 50 #ifdef VFP 51 #include <machine/vfp.h> 52 #endif 53 54 #include "opt_ddb.h" /* for OPT_DDB */ 55 #include "opt_global.h" /* for OPT_KDTRACE_HOOKS */ 56 #include "opt_stack.h" /* for OPT_STACK */ 57 58 static bool elf32_arm_abi_supported(struct image_params *, int32_t *, 59 uint32_t *); 60 61 u_long elf_hwcap; 62 u_long elf_hwcap2; 63 64 struct sysentvec elf32_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 ELF32", 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_minsigstksz = MINSIGSTKSZ, 77 .sv_minuser = VM_MIN_ADDRESS, 78 .sv_maxuser = VM_MAXUSER_ADDRESS, 79 .sv_usrstack = USRSTACK, 80 .sv_psstrings = PS_STRINGS, 81 .sv_psstringssz = sizeof(struct ps_strings), 82 .sv_stackprot = VM_PROT_ALL, 83 .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs), 84 .sv_copyout_strings = exec_copyout_strings, 85 .sv_setregs = exec_setregs, 86 .sv_fixlimit = NULL, 87 .sv_maxssiz = NULL, 88 .sv_flags = 89 SV_ASLR | SV_SHP | SV_TIMEKEEP | SV_RNG_SEED_VER | 90 SV_ABI_FREEBSD | SV_ILP32, 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(elf32_sysvec, &elf32_freebsd_sysvec); 107 108 static Elf32_Brandinfo freebsd_brand_info = { 109 .brand = ELFOSABI_FREEBSD, 110 .machine = EM_ARM, 111 .compat_3_brand = "FreeBSD", 112 .interp_path = "/libexec/ld-elf.so.1", 113 .sysvec = &elf32_freebsd_sysvec, 114 .interp_newpath = NULL, 115 .brand_note = &elf32_freebsd_brandnote, 116 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE, 117 .header_supported= elf32_arm_abi_supported, 118 }; 119 120 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST, 121 (sysinit_cfunc_t) elf32_insert_brand_entry, 122 &freebsd_brand_info); 123 124 static bool 125 elf32_arm_abi_supported(struct image_params *imgp, int32_t *osrel __unused, 126 uint32_t *fctl0 __unused) 127 { 128 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 129 130 /* 131 * When configured for EABI, FreeBSD supports EABI vesions 4 and 5. 132 */ 133 if (EF_ARM_EABI_VERSION(hdr->e_flags) < EF_ARM_EABI_FREEBSD_MIN) { 134 if (bootverbose) 135 uprintf("Attempting to execute non EABI binary (rev %d) image %s", 136 EF_ARM_EABI_VERSION(hdr->e_flags), imgp->args->fname); 137 return (false); 138 } 139 return (true); 140 } 141 142 void 143 elf32_dump_thread(struct thread *td __unused, void *dst __unused, 144 size_t *off __unused) 145 { 146 } 147 148 bool 149 elf_is_ifunc_reloc(Elf_Size r_info __unused) 150 { 151 152 return (false); 153 } 154 155 /* 156 * It is possible for the compiler to emit relocations for unaligned data. 157 * We handle this situation with these inlines. 158 */ 159 #define RELOC_ALIGNED_P(x) \ 160 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0) 161 162 static __inline Elf_Addr 163 load_ptr(Elf_Addr *where) 164 { 165 Elf_Addr res; 166 167 if (RELOC_ALIGNED_P(where)) 168 return *where; 169 memcpy(&res, where, sizeof(res)); 170 return (res); 171 } 172 173 static __inline void 174 store_ptr(Elf_Addr *where, Elf_Addr val) 175 { 176 if (RELOC_ALIGNED_P(where)) 177 *where = val; 178 else 179 memcpy(where, &val, sizeof(val)); 180 } 181 #undef RELOC_ALIGNED_P 182 183 /* Process one elf relocation with addend. */ 184 static int 185 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data, 186 int type, int local, elf_lookup_fn lookup) 187 { 188 Elf_Addr *where; 189 Elf_Addr addr; 190 Elf_Addr addend; 191 Elf_Word rtype, symidx; 192 const Elf_Rel *rel; 193 const Elf_Rela *rela; 194 int error; 195 196 switch (type) { 197 case ELF_RELOC_REL: 198 rel = (const Elf_Rel *)data; 199 where = (Elf_Addr *) (relocbase + rel->r_offset); 200 addend = load_ptr(where); 201 rtype = ELF_R_TYPE(rel->r_info); 202 symidx = ELF_R_SYM(rel->r_info); 203 break; 204 case ELF_RELOC_RELA: 205 rela = (const Elf_Rela *)data; 206 where = (Elf_Addr *) (relocbase + rela->r_offset); 207 addend = rela->r_addend; 208 rtype = ELF_R_TYPE(rela->r_info); 209 symidx = ELF_R_SYM(rela->r_info); 210 break; 211 default: 212 panic("unknown reloc type %d\n", type); 213 } 214 215 if (local) { 216 if (rtype == R_ARM_RELATIVE) { /* A + B */ 217 addr = elf_relocaddr(lf, relocbase + addend); 218 if (load_ptr(where) != addr) 219 store_ptr(where, addr); 220 } 221 return (0); 222 } 223 224 switch (rtype) { 225 case R_ARM_NONE: /* none */ 226 break; 227 228 case R_ARM_ABS32: 229 error = lookup(lf, symidx, 1, &addr); 230 if (error != 0) 231 return (-1); 232 store_ptr(where, addr + load_ptr(where)); 233 break; 234 235 case R_ARM_COPY: /* none */ 236 /* 237 * There shouldn't be copy relocations in kernel 238 * objects. 239 */ 240 printf("kldload: unexpected R_COPY relocation, " 241 "symbol index %d\n", symidx); 242 return (-1); 243 break; 244 245 case R_ARM_JUMP_SLOT: 246 error = lookup(lf, symidx, 1, &addr); 247 if (error == 0) { 248 store_ptr(where, addr); 249 return (0); 250 } 251 return (-1); 252 case R_ARM_RELATIVE: 253 break; 254 255 default: 256 printf("kldload: unexpected relocation type %d, " 257 "symbol index %d\n", rtype, symidx); 258 return (-1); 259 } 260 return(0); 261 } 262 263 int 264 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 265 elf_lookup_fn lookup) 266 { 267 268 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup)); 269 } 270 271 int 272 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data, 273 int type, elf_lookup_fn lookup) 274 { 275 276 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup)); 277 } 278 279 int 280 elf_cpu_load_file(linker_file_t lf) 281 { 282 283 /* 284 * The pmap code does not do an icache sync upon establishing executable 285 * mappings in the kernel pmap. It's an optimization based on the fact 286 * that kernel memory allocations always have EXECUTABLE protection even 287 * when the memory isn't going to hold executable code. The only time 288 * kernel memory holding instructions does need a sync is after loading 289 * a kernel module, and that's when this function gets called. 290 * 291 * This syncs data and instruction caches after loading a module. We 292 * don't worry about the kernel itself (lf->id is 1) as locore.S did 293 * that on entry. Even if data cache maintenance was done by IO code, 294 * the relocation fixup process creates dirty cache entries that we must 295 * write back before doing icache sync. The instruction cache sync also 296 * invalidates the branch predictor cache on platforms that have one. 297 */ 298 if (lf->id == 1) 299 return (0); 300 dcache_wb_pou((vm_offset_t)lf->address, (vm_size_t)lf->size); 301 icache_inv_all(); 302 303 #if defined(DDB) || defined(KDTRACE_HOOKS) || defined(STACK) 304 /* 305 * Inform the stack(9) code of the new module, so it can acquire its 306 * per-module unwind data. 307 */ 308 unwind_module_loaded(lf); 309 #endif 310 311 return (0); 312 } 313 314 int 315 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused) 316 { 317 318 return (0); 319 } 320 321 int 322 elf_cpu_unload_file(linker_file_t lf) 323 { 324 325 #if defined(DDB) || defined(KDTRACE_HOOKS) || defined(STACK) 326 /* Inform the stack(9) code that this module is gone. */ 327 unwind_module_unloaded(lf); 328 #endif 329 return (0); 330 } 331