elf32_machdep.c revision 209975
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 209975 2010-07-13 05:32:19Z nwhitehorn $ 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/cpu.h> 51#include <machine/elf.h> 52#include <machine/reg.h> 53#include <machine/md_var.h> 54 55#ifdef __powerpc64__ 56#include <compat/freebsd32/freebsd32_proto.h> 57#include <compat/freebsd32/freebsd32_util.h> 58 59extern const char *freebsd32_syscallnames[]; 60#endif 61 62struct sysentvec elf32_freebsd_sysvec = { 63 .sv_size = SYS_MAXSYSCALL, 64#ifdef __powerpc64__ 65 .sv_table = freebsd32_sysent, 66#else 67 .sv_table = sysent, 68#endif 69 .sv_mask = 0, 70 .sv_sigsize = 0, 71 .sv_sigtbl = NULL, 72 .sv_errsize = 0, 73 .sv_errtbl = NULL, 74 .sv_transtrap = NULL, 75 .sv_fixup = __elfN(freebsd_fixup), 76 .sv_sendsig = sendsig, 77 .sv_sigcode = sigcode32, 78 .sv_szsigcode = &szsigcode32, 79 .sv_prepsyscall = NULL, 80 .sv_name = "FreeBSD ELF32", 81 .sv_coredump = __elfN(coredump), 82 .sv_imgact_try = NULL, 83 .sv_minsigstksz = MINSIGSTKSZ, 84 .sv_pagesize = PAGE_SIZE, 85 .sv_minuser = VM_MIN_ADDRESS, 86 .sv_stackprot = VM_PROT_ALL, 87#ifdef __powerpc64__ 88 .sv_maxuser = VM_MAXUSER_ADDRESS, 89 .sv_usrstack = FREEBSD32_USRSTACK, 90 .sv_psstrings = FREEBSD32_PS_STRINGS, 91 .sv_copyout_strings = freebsd32_copyout_strings, 92 .sv_setregs = ppc32_setregs, 93 .sv_syscallnames = freebsd32_syscallnames, 94#else 95 .sv_maxuser = VM_MAXUSER_ADDRESS, 96 .sv_usrstack = USRSTACK, 97 .sv_psstrings = PS_STRINGS, 98 .sv_copyout_strings = exec_copyout_strings, 99 .sv_setregs = exec_setregs, 100 .sv_syscallnames = syscallnames, 101#endif 102 .sv_fixlimit = NULL, 103 .sv_maxssiz = NULL, 104 .sv_flags = SV_ABI_FREEBSD | SV_ILP32, 105 .sv_set_syscall_retval = cpu_set_syscall_retval, 106 .sv_fetch_syscall_args = cpu_fetch_syscall_args, 107}; 108 109static Elf32_Brandinfo freebsd_brand_info = { 110 .brand = ELFOSABI_FREEBSD, 111 .machine = EM_PPC, 112 .compat_3_brand = "FreeBSD", 113 .emul_path = NULL, 114 .interp_path = "/libexec/ld-elf.so.1", 115 .sysvec = &elf32_freebsd_sysvec, 116#ifdef __powerpc64__ 117 .interp_newpath = "/libexec/ld-elf32.so.1", 118#else 119 .interp_newpath = NULL, 120#endif 121 .brand_note = &elf32_freebsd_brandnote, 122 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 123}; 124 125SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST, 126 (sysinit_cfunc_t) elf32_insert_brand_entry, 127 &freebsd_brand_info); 128 129static Elf32_Brandinfo freebsd_brand_oinfo = { 130 .brand = ELFOSABI_FREEBSD, 131 .machine = EM_PPC, 132 .compat_3_brand = "FreeBSD", 133 .emul_path = NULL, 134 .interp_path = "/usr/libexec/ld-elf.so.1", 135 .sysvec = &elf32_freebsd_sysvec, 136 .interp_newpath = NULL, 137 .brand_note = &elf32_freebsd_brandnote, 138 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 139}; 140 141SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY, 142 (sysinit_cfunc_t) elf32_insert_brand_entry, 143 &freebsd_brand_oinfo); 144 145void 146elf32_dump_thread(struct thread *td __unused, void *dst __unused, 147 size_t *off __unused) 148{ 149} 150 151#ifndef __powerpc64__ 152/* Process one elf relocation with addend. */ 153static int 154elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data, 155 int type, int local, elf_lookup_fn lookup) 156{ 157 Elf_Addr *where; 158 Elf_Half *hwhere; 159 Elf_Addr addr; 160 Elf_Addr addend; 161 Elf_Word rtype, symidx; 162 const Elf_Rela *rela; 163 164 switch (type) { 165 case ELF_RELOC_REL: 166 panic("PPC only supports RELA relocations"); 167 break; 168 case ELF_RELOC_RELA: 169 rela = (const Elf_Rela *)data; 170 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset); 171 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset); 172 addend = rela->r_addend; 173 rtype = ELF_R_TYPE(rela->r_info); 174 symidx = ELF_R_SYM(rela->r_info); 175 break; 176 default: 177 panic("elf_reloc: unknown relocation mode %d\n", type); 178 } 179 180 switch (rtype) { 181 182 case R_PPC_NONE: 183 break; 184 185 case R_PPC_ADDR32: /* word32 S + A */ 186 addr = lookup(lf, symidx, 1); 187 if (addr == 0) 188 return -1; 189 addr += addend; 190 *where = addr; 191 break; 192 193 case R_PPC_ADDR16_LO: /* #lo(S) */ 194 addr = lookup(lf, symidx, 1); 195 if (addr == 0) 196 return -1; 197 /* 198 * addend values are sometimes relative to sections 199 * (i.e. .rodata) in rela, where in reality they 200 * are relative to relocbase. Detect this condition. 201 */ 202 if (addr > relocbase && addr <= (relocbase + addend)) 203 addr = relocbase + addend; 204 else 205 addr += addend; 206 *hwhere = addr & 0xffff; 207 break; 208 209 case R_PPC_ADDR16_HA: /* #ha(S) */ 210 addr = lookup(lf, symidx, 1); 211 if (addr == 0) 212 return -1; 213 /* 214 * addend values are sometimes relative to sections 215 * (i.e. .rodata) in rela, where in reality they 216 * are relative to relocbase. Detect this condition. 217 */ 218 if (addr > relocbase && addr <= (relocbase + addend)) 219 addr = relocbase + addend; 220 else 221 addr += addend; 222 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0)) 223 & 0xffff; 224 break; 225 226 case R_PPC_RELATIVE: /* word32 B + A */ 227 *where = elf_relocaddr(lf, relocbase + addend); 228 break; 229 230 default: 231 printf("kldload: unexpected relocation type %d\n", 232 (int) rtype); 233 return -1; 234 } 235 return(0); 236} 237 238int 239elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 240 elf_lookup_fn lookup) 241{ 242 243 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup)); 244} 245 246int 247elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data, 248 int type, elf_lookup_fn lookup) 249{ 250 251 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup)); 252} 253 254int 255elf_cpu_load_file(linker_file_t lf) 256{ 257 /* Only sync the cache for non-kernel modules */ 258 if (lf->id != 1) 259 __syncicache(lf->address, lf->size); 260 return (0); 261} 262 263int 264elf_cpu_unload_file(linker_file_t lf __unused) 265{ 266 267 return (0); 268} 269#endif 270