elf_machdep.c revision 123742
180708Sjake/*- 280708Sjake * Copyright (c) 2001 Jake Burkholder. 385586Sjake * Copyright (c) 2000 Eduardo Horvath. 485586Sjake * Copyright (c) 1999 The NetBSD Foundation, Inc. 580708Sjake * All rights reserved. 680708Sjake * 785586Sjake * This code is derived from software contributed to The NetBSD Foundation 885586Sjake * by Paul Kranenburg. 985586Sjake * 1080708Sjake * Redistribution and use in source and binary forms, with or without 1180708Sjake * modification, are permitted provided that the following conditions 1280708Sjake * are met: 1380708Sjake * 1. Redistributions of source code must retain the above copyright 1480708Sjake * notice, this list of conditions and the following disclaimer. 1580708Sjake * 2. Redistributions in binary form must reproduce the above copyright 1680708Sjake * notice, this list of conditions and the following disclaimer in the 1780708Sjake * documentation and/or other materials provided with the distribution. 1885586Sjake * 3. All advertising materials mentioning features or use of this software 1985586Sjake * must display the following acknowledgement: 2085586Sjake * This product includes software developed by the NetBSD 2185586Sjake * Foundation, Inc. and its contributors. 2285586Sjake * 4. Neither the name of The NetBSD Foundation nor the names of its 2385586Sjake * contributors may be used to endorse or promote products derived 2485586Sjake * from this software without specific prior written permission. 2580708Sjake * 2685586Sjake * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2785586Sjake * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2885586Sjake * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2985586Sjake * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 3085586Sjake * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3185586Sjake * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3285586Sjake * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3385586Sjake * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3485586Sjake * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3585586Sjake * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3685586Sjake * POSSIBILITY OF SUCH DAMAGE. 3780708Sjake * 3885586Sjake * from: NetBSD: mdreloc.c,v 1.5 2001/04/25 12:24:51 kleink Exp 3980708Sjake * $FreeBSD: head/sys/sparc64/sparc64/elf_machdep.c 123742 2003-12-23 02:42:39Z peter $ 4080708Sjake */ 4180708Sjake 4280708Sjake#include <sys/param.h> 43100384Speter#include <sys/kernel.h> 4480708Sjake#include <sys/systm.h> 45102808Sjake#include <sys/exec.h> 46102808Sjake#include <sys/imgact.h> 4780708Sjake#include <sys/linker.h> 48100384Speter#include <sys/sysent.h> 49100384Speter#include <sys/imgact_elf.h> 50100384Speter#include <sys/syscall.h> 51100384Speter#include <sys/signalvar.h> 52100384Speter#include <sys/vnode.h> 53102808Sjake 54102808Sjake#include <vm/vm.h> 55102808Sjake#include <vm/vm_param.h> 56102808Sjake 5780708Sjake#include <machine/elf.h> 5880708Sjake 5985586Sjake#include "linker_if.h" 6085586Sjake 61100384Speterstruct sysentvec elf64_freebsd_sysvec = { 62100384Speter SYS_MAXSYSCALL, 63100384Speter sysent, 64100384Speter 0, 65100384Speter 0, 66102808Sjake NULL, 67100384Speter 0, 68102808Sjake NULL, 69102808Sjake NULL, 70102808Sjake __elfN(freebsd_fixup), 71100384Speter sendsig, 72102555Sjake NULL, 73102555Sjake NULL, 74102808Sjake NULL, 75100384Speter "FreeBSD ELF64", 76100384Speter __elfN(coredump), 77100384Speter NULL, 78102808Sjake MINSIGSTKSZ, 79102808Sjake PAGE_SIZE, 80102808Sjake VM_MIN_ADDRESS, 81102808Sjake VM_MAXUSER_ADDRESS, 82102808Sjake USRSTACK, 83102808Sjake PS_STRINGS, 84102808Sjake VM_PROT_READ | VM_PROT_WRITE, 85102808Sjake exec_copyout_strings, 86120422Speter exec_setregs, 87120422Speter NULL 88100384Speter}; 89100384Speter 90100384Speterstatic Elf64_Brandinfo freebsd_brand_info = { 91100384Speter ELFOSABI_FREEBSD, 92100384Speter EM_SPARCV9, 93100384Speter "FreeBSD", 94123742Speter NULL, 95119015Sgordon "/libexec/ld-elf.so.1", 96123742Speter &elf64_freebsd_sysvec, 97123742Speter NULL, 98100384Speter }; 99100384Speter 100100384SpeterSYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY, 101100384Speter (sysinit_cfunc_t) elf64_insert_brand_entry, 102100384Speter &freebsd_brand_info); 103100384Speter 104123742Speterstatic Elf64_Brandinfo freebsd_brand_oinfo = { 105123742Speter ELFOSABI_FREEBSD, 106123742Speter EM_SPARCV9, 107123742Speter "FreeBSD", 108123742Speter NULL, 109123742Speter "/usr/libexec/ld-elf.so.1", 110123742Speter &elf64_freebsd_sysvec, 111123742Speter NULL, 112123742Speter }; 113123742Speter 114123742SpeterSYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY, 115123742Speter (sysinit_cfunc_t) elf64_insert_brand_entry, 116123742Speter &freebsd_brand_oinfo); 117123742Speter 11885586Sjake/* 11985586Sjake * The following table holds for each relocation type: 12085586Sjake * - the width in bits of the memory location the relocation 12185586Sjake * applies to (not currently used) 12285586Sjake * - the number of bits the relocation value must be shifted to the 12385586Sjake * right (i.e. discard least significant bits) to fit into 12485586Sjake * the appropriate field in the instruction word. 12585586Sjake * - flags indicating whether 12685586Sjake * * the relocation involves a symbol 12785586Sjake * * the relocation is relative to the current position 12885586Sjake * * the relocation is for a GOT entry 12985586Sjake * * the relocation is relative to the load address 13085586Sjake * 13185586Sjake */ 13285586Sjake#define _RF_S 0x80000000 /* Resolve symbol */ 13385586Sjake#define _RF_A 0x40000000 /* Use addend */ 13485586Sjake#define _RF_P 0x20000000 /* Location relative */ 13585586Sjake#define _RF_G 0x10000000 /* GOT offset */ 13685586Sjake#define _RF_B 0x08000000 /* Load address relative */ 13785586Sjake#define _RF_U 0x04000000 /* Unaligned */ 13885586Sjake#define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 13985586Sjake#define _RF_RS(s) ( (s) & 0xff) /* right shift */ 14085586Sjakestatic int reloc_target_flags[] = { 14185586Sjake 0, /* NONE */ 14285586Sjake _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */ 14385586Sjake _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */ 14485586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */ 14585586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */ 14685586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */ 14785586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */ 14885586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */ 14985586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */ 150104072Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */ 15185586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */ 15285586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */ 153104072Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */ 15485586Sjake _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */ 15585586Sjake _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */ 15685586Sjake _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */ 15785586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */ 15885586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */ 15985586Sjake _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */ 16085586Sjake _RF_SZ(32) | _RF_RS(0), /* COPY */ 16185586Sjake _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */ 16285586Sjake _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */ 163104072Sjake _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */ 164104072Sjake _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */ 16585586Sjake 16685586Sjake _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */ 16785586Sjake _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */ 16885586Sjake _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */ 16985586Sjake _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */ 17085586Sjake _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */ 17185586Sjake _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */ 17285586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */ 17385586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */ 17485586Sjake _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */ 17585586Sjake _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */ 17685586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */ 17785586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */ 17885586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */ 17985586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */ 18085586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */ 18185586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */ 18285586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */ 18385586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */ 18485586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */ 18585586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */ 18685586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */ 18785586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */ 18885586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */ 18985586Sjake _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */ 19085586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */ 19185586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */ 19285586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */ 19385586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */ 19485586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */ 19585586Sjake _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */ 19685586Sjake _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */ 19785586Sjake _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */ 19885586Sjake}; 19985586Sjake 200104072Sjake#if 0 201104072Sjakestatic const char *reloc_names[] = { 202104072Sjake "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8", 203104072Sjake "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22", 204104072Sjake "22", "13", "LO10", "GOT10", "GOT13", 205104072Sjake "GOT22", "PC10", "PC22", "WPLT30", "COPY", 206104072Sjake "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32", 207104072Sjake "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32", 208104072Sjake "10", "11", "64", "OLO10", "HH22", 209104072Sjake "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22", 210104072Sjake "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6", 211104072Sjake "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44", 212104072Sjake "L44", "REGISTER", "UA64", "UA16" 213104072Sjake}; 214104072Sjake#endif 215104072Sjake 21685586Sjake#define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 21785586Sjake#define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 21885586Sjake#define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 21985586Sjake#define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 22085586Sjake#define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 22185586Sjake#define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 22285586Sjake#define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 22385586Sjake 22485586Sjakestatic long reloc_target_bitmask[] = { 22585586Sjake#define _BM(x) (~(-(1ULL << (x)))) 22685586Sjake 0, /* NONE */ 22785586Sjake _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */ 22885586Sjake _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */ 22985586Sjake _BM(30), _BM(22), /* WDISP30, WDISP22 */ 23085586Sjake _BM(22), _BM(22), /* HI22, _22 */ 23185586Sjake _BM(13), _BM(10), /* RELOC_13, _LO10 */ 23285586Sjake _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */ 23385586Sjake _BM(10), _BM(22), /* _PC10, _PC22 */ 23485586Sjake _BM(30), 0, /* _WPLT30, _COPY */ 23585586Sjake _BM(32), _BM(32), _BM(32), /* _GLOB_DAT, JMP_SLOT, _RELATIVE */ 23685586Sjake _BM(32), _BM(32), /* _UA32, PLT32 */ 23785586Sjake _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */ 23885586Sjake _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */ 23985586Sjake _BM(10), _BM(11), -1, /* _10, _11, _64 */ 24085586Sjake _BM(10), _BM(22), /* _OLO10, _HH22 */ 24185586Sjake _BM(10), _BM(22), /* _HM10, _LM22 */ 24285586Sjake _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */ 24385586Sjake _BM(16), _BM(19), /* _WDISP16, _WDISP19 */ 24485586Sjake -1, /* GLOB_JMP */ 24585586Sjake _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */ 24685586Sjake -1, -1, /* DISP64, PLT64 */ 24785586Sjake _BM(22), _BM(13), /* HIX22, LOX10 */ 24885586Sjake _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */ 24985586Sjake -1, -1, _BM(16), /* REGISTER, UA64, UA16 */ 25085586Sjake#undef _BM 25185586Sjake}; 25285586Sjake#define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 25385586Sjake 254109605Sjakeint 255109605Sjakeelf_reloc_local(linker_file_t lf, const void *data, int type) 256109605Sjake{ 257109605Sjake const Elf_Rela *rela; 258109605Sjake Elf_Addr value; 259109605Sjake Elf_Addr *where; 260109605Sjake 261109605Sjake if (type != ELF_RELOC_RELA) 262109605Sjake return (-1); 263109605Sjake 264109605Sjake rela = (const Elf_Rela *)data; 265109605Sjake if (ELF_R_TYPE(rela->r_info) != R_SPARC_RELATIVE) 266109605Sjake return (-1); 267109605Sjake 268109605Sjake value = rela->r_addend + (Elf_Addr)lf->address; 269109605Sjake where = (Elf_Addr *)((Elf_Addr)lf->address + rela->r_offset); 270109605Sjake 271109605Sjake *where = value; 272109605Sjake 273109605Sjake return (0); 274109605Sjake} 275109605Sjake 27685586Sjake/* Process one elf relocation with addend. */ 27780708Sjakeint 27895410Smarcelelf_reloc(linker_file_t lf, const void *data, int type) 27980708Sjake{ 28085586Sjake const Elf_Rela *rela; 28185586Sjake Elf_Addr relocbase; 28285586Sjake Elf_Half *where32; 28385586Sjake Elf_Addr *where; 28495410Smarcel Elf_Word rtype, symidx; 28585586Sjake Elf_Addr value; 28685586Sjake Elf_Addr mask; 28798635Smux Elf_Addr addr; 28885586Sjake 28985586Sjake if (type != ELF_RELOC_RELA) 29085586Sjake return (-1); 29185586Sjake 29285586Sjake relocbase = (Elf_Addr)lf->address; 29385586Sjake rela = (const Elf_Rela *)data; 29485586Sjake where = (Elf_Addr *)(relocbase + rela->r_offset); 29585586Sjake where32 = (Elf_Half *)where; 29685586Sjake rtype = ELF_R_TYPE(rela->r_info); 29795410Smarcel symidx = ELF_R_SYM(rela->r_info); 29885586Sjake 299109605Sjake if (rtype == R_SPARC_NONE || rtype == R_SPARC_RELATIVE) 30085586Sjake return (0); 30185586Sjake 30285586Sjake if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY || 30385586Sjake rtype > R_SPARC_UA16) 30485586Sjake return (-1); 30585586Sjake 30685586Sjake if (RELOC_UNALIGNED(rtype)) 30785586Sjake return (-1); 30885586Sjake 30985586Sjake value = rela->r_addend; 31085586Sjake 31185586Sjake if (RELOC_RESOLVE_SYMBOL(rtype)) { 312107517Stmm addr = elf_lookup(lf, symidx, 1); 313107517Stmm if (addr == 0) 314107517Stmm return (-1); 315107517Stmm value += addr; 31685586Sjake } 31785586Sjake 31885586Sjake if (RELOC_PC_RELATIVE(rtype)) 31985586Sjake value -= (Elf_Addr)where; 32085586Sjake 32185586Sjake if (RELOC_BASE_RELATIVE(rtype)) 32298635Smux value += relocbase; 32385586Sjake 32485586Sjake mask = RELOC_VALUE_BITMASK(rtype); 32585586Sjake value >>= RELOC_VALUE_RIGHTSHIFT(rtype); 32685586Sjake value &= mask; 32785586Sjake 32885586Sjake if (RELOC_TARGET_SIZE(rtype) > 32) { 32985586Sjake *where &= ~mask; 33085586Sjake *where |= value; 33185586Sjake } else { 33285586Sjake *where32 &= ~mask; 33385586Sjake *where32 |= value; 33485586Sjake } 33585586Sjake 33680708Sjake return (0); 33780708Sjake} 338105469Smarcel 339105469Smarcelint 340105469Smarcelelf_cpu_load_file(linker_file_t lf __unused) 341105469Smarcel{ 342105469Smarcel 343105469Smarcel return (0); 344105469Smarcel} 345105469Smarcel 346105469Smarcelint 347105469Smarcelelf_cpu_unload_file(linker_file_t lf __unused) 348105469Smarcel{ 349105469Smarcel 350105469Smarcel return (0); 351105469Smarcel} 352