elf32_machdep.c revision 276634
1/*- 2 * Copyright 1996-1998 John D. Polstra. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * $FreeBSD: head/sys/powerpc/powerpc/elf32_machdep.c 276634 2015-01-03 21:06:06Z jhibbits $ 26 */ 27 28#include <sys/param.h> 29#include <sys/kernel.h> 30#include <sys/systm.h> 31 32#define __ELF_WORD_SIZE 32 33 34#include <sys/exec.h> 35#include <sys/imgact.h> 36#include <sys/malloc.h> 37#include <sys/proc.h> 38#include <sys/namei.h> 39#include <sys/fcntl.h> 40#include <sys/sysent.h> 41#include <sys/imgact_elf.h> 42#include <sys/syscall.h> 43#include <sys/signalvar.h> 44#include <sys/vnode.h> 45#include <sys/linker.h> 46 47#include <vm/vm.h> 48#include <vm/vm_param.h> 49 50#include <machine/altivec.h> 51#include <machine/cpu.h> 52#include <machine/elf.h> 53#include <machine/reg.h> 54#include <machine/md_var.h> 55 56#ifdef __powerpc64__ 57#include <compat/freebsd32/freebsd32_proto.h> 58#include <compat/freebsd32/freebsd32_util.h> 59 60extern const char *freebsd32_syscallnames[]; 61#endif 62 63struct sysentvec elf32_freebsd_sysvec = { 64 .sv_size = SYS_MAXSYSCALL, 65#ifdef __powerpc64__ 66 .sv_table = freebsd32_sysent, 67#else 68 .sv_table = sysent, 69#endif 70 .sv_mask = 0, 71 .sv_sigsize = 0, 72 .sv_sigtbl = NULL, 73 .sv_errsize = 0, 74 .sv_errtbl = NULL, 75 .sv_transtrap = NULL, 76 .sv_fixup = __elfN(freebsd_fixup), 77 .sv_sendsig = sendsig, 78 .sv_sigcode = sigcode32, 79 .sv_szsigcode = &szsigcode32, 80 .sv_prepsyscall = NULL, 81 .sv_name = "FreeBSD ELF32", 82 .sv_coredump = __elfN(coredump), 83 .sv_imgact_try = NULL, 84 .sv_minsigstksz = MINSIGSTKSZ, 85 .sv_pagesize = PAGE_SIZE, 86 .sv_minuser = VM_MIN_ADDRESS, 87 .sv_stackprot = VM_PROT_ALL, 88#ifdef __powerpc64__ 89 .sv_maxuser = VM_MAXUSER_ADDRESS, 90 .sv_usrstack = FREEBSD32_USRSTACK, 91 .sv_psstrings = FREEBSD32_PS_STRINGS, 92 .sv_copyout_strings = freebsd32_copyout_strings, 93 .sv_setregs = ppc32_setregs, 94 .sv_syscallnames = freebsd32_syscallnames, 95#else 96 .sv_maxuser = VM_MAXUSER_ADDRESS, 97 .sv_usrstack = USRSTACK, 98 .sv_psstrings = PS_STRINGS, 99 .sv_copyout_strings = exec_copyout_strings, 100 .sv_setregs = exec_setregs, 101 .sv_syscallnames = syscallnames, 102#endif 103 .sv_fixlimit = NULL, 104 .sv_maxssiz = NULL, 105 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP, 106 .sv_set_syscall_retval = cpu_set_syscall_retval, 107 .sv_fetch_syscall_args = cpu_fetch_syscall_args, 108 .sv_shared_page_base = FREEBSD32_SHAREDPAGE, 109 .sv_shared_page_len = PAGE_SIZE, 110 .sv_schedtail = NULL, 111}; 112INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec); 113 114static Elf32_Brandinfo freebsd_brand_info = { 115 .brand = ELFOSABI_FREEBSD, 116 .machine = EM_PPC, 117 .compat_3_brand = "FreeBSD", 118 .emul_path = NULL, 119 .interp_path = "/libexec/ld-elf.so.1", 120 .sysvec = &elf32_freebsd_sysvec, 121#ifdef __powerpc64__ 122 .interp_newpath = "/libexec/ld-elf32.so.1", 123#else 124 .interp_newpath = NULL, 125#endif 126 .brand_note = &elf32_freebsd_brandnote, 127 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 128}; 129 130SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST, 131 (sysinit_cfunc_t) elf32_insert_brand_entry, 132 &freebsd_brand_info); 133 134static Elf32_Brandinfo freebsd_brand_oinfo = { 135 .brand = ELFOSABI_FREEBSD, 136 .machine = EM_PPC, 137 .compat_3_brand = "FreeBSD", 138 .emul_path = NULL, 139 .interp_path = "/usr/libexec/ld-elf.so.1", 140 .sysvec = &elf32_freebsd_sysvec, 141 .interp_newpath = NULL, 142 .brand_note = &elf32_freebsd_brandnote, 143 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 144}; 145 146SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY, 147 (sysinit_cfunc_t) elf32_insert_brand_entry, 148 &freebsd_brand_oinfo); 149 150void 151elf32_dump_thread(struct thread *td, void *dst, size_t *off) 152{ 153 size_t len; 154 struct pcb *pcb; 155 156 len = 0; 157 pcb = td->td_pcb; 158 if (pcb->pcb_flags & PCB_VEC) { 159 save_vec_nodrop(td); 160 if (dst != NULL) { 161 len += elf32_populate_note(NT_PPC_VMX, 162 &pcb->pcb_vec, dst, 163 sizeof(pcb->pcb_vec), NULL); 164 } else 165 len += elf32_populate_note(NT_PPC_VMX, NULL, NULL, 166 sizeof(pcb->pcb_vec), NULL); 167 } 168 *off = len; 169} 170 171#ifndef __powerpc64__ 172/* Process one elf relocation with addend. */ 173static int 174elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data, 175 int type, int local, elf_lookup_fn lookup) 176{ 177 Elf_Addr *where; 178 Elf_Half *hwhere; 179 Elf_Addr addr; 180 Elf_Addr addend; 181 Elf_Word rtype, symidx; 182 const Elf_Rela *rela; 183 184 switch (type) { 185 case ELF_RELOC_REL: 186 panic("PPC only supports RELA relocations"); 187 break; 188 case ELF_RELOC_RELA: 189 rela = (const Elf_Rela *)data; 190 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset); 191 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset); 192 addend = rela->r_addend; 193 rtype = ELF_R_TYPE(rela->r_info); 194 symidx = ELF_R_SYM(rela->r_info); 195 break; 196 default: 197 panic("elf_reloc: unknown relocation mode %d\n", type); 198 } 199 200 switch (rtype) { 201 202 case R_PPC_NONE: 203 break; 204 205 case R_PPC_ADDR32: /* word32 S + A */ 206 addr = lookup(lf, symidx, 1); 207 if (addr == 0) 208 return -1; 209 *where = elf_relocaddr(lf, addr + addend); 210 break; 211 212 case R_PPC_ADDR16_LO: /* #lo(S) */ 213 addr = lookup(lf, symidx, 1); 214 if (addr == 0) 215 return -1; 216 /* 217 * addend values are sometimes relative to sections 218 * (i.e. .rodata) in rela, where in reality they 219 * are relative to relocbase. Detect this condition. 220 */ 221 if (addr > relocbase && addr <= (relocbase + addend)) 222 addr = relocbase; 223 addr = elf_relocaddr(lf, addr + addend); 224 *hwhere = addr & 0xffff; 225 break; 226 227 case R_PPC_ADDR16_HA: /* #ha(S) */ 228 addr = lookup(lf, symidx, 1); 229 if (addr == 0) 230 return -1; 231 /* 232 * addend values are sometimes relative to sections 233 * (i.e. .rodata) in rela, where in reality they 234 * are relative to relocbase. Detect this condition. 235 */ 236 if (addr > relocbase && addr <= (relocbase + addend)) 237 addr = relocbase; 238 addr = elf_relocaddr(lf, addr + addend); 239 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0)) 240 & 0xffff; 241 break; 242 243 case R_PPC_RELATIVE: /* word32 B + A */ 244 *where = elf_relocaddr(lf, relocbase + addend); 245 break; 246 247 default: 248 printf("kldload: unexpected relocation type %d\n", 249 (int) rtype); 250 return -1; 251 } 252 return(0); 253} 254 255int 256elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 257 elf_lookup_fn lookup) 258{ 259 260 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup)); 261} 262 263int 264elf_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, 1, lookup)); 269} 270 271int 272elf_cpu_load_file(linker_file_t lf) 273{ 274 /* Only sync the cache for non-kernel modules */ 275 if (lf->id != 1) 276 __syncicache(lf->address, lf->size); 277 return (0); 278} 279 280int 281elf_cpu_unload_file(linker_file_t lf __unused) 282{ 283 284 return (0); 285} 286#endif 287