elf32_machdep.c revision 132562
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/elf_machdep.c 132562 2004-07-23 00:46:05Z grehan $ 26 */ 27 28#include <sys/param.h> 29#include <sys/kernel.h> 30#include <sys/systm.h> 31#include <sys/exec.h> 32#include <sys/imgact.h> 33#include <sys/malloc.h> 34#include <sys/proc.h> 35#include <sys/namei.h> 36#include <sys/fcntl.h> 37#include <sys/sysent.h> 38#include <sys/imgact_elf.h> 39#include <sys/syscall.h> 40#include <sys/signalvar.h> 41#include <sys/vnode.h> 42#include <sys/linker.h> 43 44#include <vm/vm.h> 45#include <vm/vm_param.h> 46 47#include <machine/cpu.h> 48#include <machine/elf.h> 49#include <machine/md_var.h> 50 51struct sysentvec elf32_freebsd_sysvec = { 52 SYS_MAXSYSCALL, 53 sysent, 54 0, 55 0, 56 NULL, 57 0, 58 NULL, 59 NULL, 60 __elfN(freebsd_fixup), 61 sendsig, 62 sigcode, 63 &szsigcode, 64 NULL, 65 "FreeBSD ELF32", 66 __elfN(coredump), 67 NULL, 68 MINSIGSTKSZ, 69 PAGE_SIZE, 70 VM_MIN_ADDRESS, 71 VM_MAXUSER_ADDRESS, 72 USRSTACK, 73 PS_STRINGS, 74 VM_PROT_ALL, 75 exec_copyout_strings, 76 exec_setregs, 77 NULL 78}; 79 80static Elf32_Brandinfo freebsd_brand_info = { 81 ELFOSABI_FREEBSD, 82 EM_PPC, 83 "FreeBSD", 84 NULL, 85 "/libexec/ld-elf.so.1", 86 &elf32_freebsd_sysvec, 87 NULL, 88 }; 89 90SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY, 91 (sysinit_cfunc_t) elf32_insert_brand_entry, 92 &freebsd_brand_info); 93 94static Elf32_Brandinfo freebsd_brand_oinfo = { 95 ELFOSABI_FREEBSD, 96 EM_PPC, 97 "FreeBSD", 98 NULL, 99 "/usr/libexec/ld-elf.so.1", 100 &elf32_freebsd_sysvec, 101 NULL, 102 }; 103 104SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY, 105 (sysinit_cfunc_t) elf32_insert_brand_entry, 106 &freebsd_brand_oinfo); 107 108/* Process one elf relocation with addend. */ 109static int 110elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data, 111 int type, int local, elf_lookup_fn lookup) 112{ 113 Elf_Addr *where; 114 Elf_Half *hwhere; 115 Elf_Addr addr; 116 Elf_Addr addend; 117 Elf_Word rtype, symidx; 118 const Elf_Rela *rela; 119 120 switch (type) { 121 case ELF_RELOC_REL: 122 panic("PPC only supports RELA relocations"); 123 break; 124 case ELF_RELOC_RELA: 125 rela = (const Elf_Rela *)data; 126 where = (Elf_Addr *) (relocbase + rela->r_offset); 127 hwhere = (Elf_Half *) (relocbase + rela->r_offset); 128 addend = rela->r_addend; 129 rtype = ELF_R_TYPE(rela->r_info); 130 symidx = ELF_R_SYM(rela->r_info); 131 break; 132 default: 133 panic("elf_reloc: unknown relocation mode %d\n", type); 134 } 135 136 switch (rtype) { 137 138 case R_PPC_NONE: 139 break; 140 141 case R_PPC_ADDR32: /* word32 S + A */ 142 addr = lookup(lf, symidx, 1); 143 if (addr == 0) 144 return -1; 145 addr += addend; 146 *where = addr; 147 break; 148 149 case R_PPC_ADDR16_LO: /* #lo(S) */ 150 addr = lookup(lf, symidx, 1); 151 if (addr == 0) 152 return -1; 153 /* 154 * addend values are sometimes relative to sections 155 * (i.e. .rodata) in rela, where in reality they 156 * are relative to relocbase. Detect this condition. 157 */ 158 if (addr > relocbase && addr <= (relocbase + addend)) 159 addr = relocbase + addend; 160 else 161 addr += addend; 162 *hwhere = addr & 0xffff; 163 break; 164 165 case R_PPC_ADDR16_HA: /* #ha(S) */ 166 addr = lookup(lf, symidx, 1); 167 if (addr == 0) 168 return -1; 169 /* 170 * addend values are sometimes relative to sections 171 * (i.e. .rodata) in rela, where in reality they 172 * are relative to relocbase. Detect this condition. 173 */ 174 if (addr > relocbase && addr <= (relocbase + addend)) 175 addr = relocbase + addend; 176 else 177 addr += addend; 178 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0)) 179 & 0xffff; 180 break; 181 182 case R_PPC_RELATIVE: /* word32 B + A */ 183 *where = relocbase + addend; 184 break; 185 186 default: 187 printf("kldload: unexpected relocation type %d\n", 188 (int) rtype); 189 return -1; 190 } 191 return(0); 192} 193 194int 195elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 196 elf_lookup_fn lookup) 197{ 198 199 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup)); 200} 201 202int 203elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data, 204 int type, elf_lookup_fn lookup) 205{ 206 207 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup)); 208} 209 210int 211elf_cpu_load_file(linker_file_t lf) 212{ 213 /* Only sync the cache for non-kernel modules */ 214 if (lf->id != 1) 215 __syncicache(lf->address, lf->size); 216 return (0); 217} 218 219int 220elf_cpu_unload_file(linker_file_t lf __unused) 221{ 222 223 return (0); 224} 225