1 1.1 joerg /* $NetBSD: elf_support.h,v 1.1 2018/03/29 13:23:40 joerg Exp $ */ 2 1.1 joerg 3 1.1 joerg /*- 4 1.1 joerg * Copyright (c) 2000 Eduardo Horvath. 5 1.1 joerg * Copyright (c) 2018 The NetBSD Foundation, Inc. 6 1.1 joerg * All rights reserved. 7 1.1 joerg * 8 1.1 joerg * Redistribution and use in source and binary forms, with or without 9 1.1 joerg * modification, are permitted provided that the following conditions 10 1.1 joerg * are met: 11 1.1 joerg * 1. Redistributions of source code must retain the above copyright 12 1.1 joerg * notice, this list of conditions and the following disclaimer. 13 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 joerg * notice, this list of conditions and the following disclaimer in the 15 1.1 joerg * documentation and/or other materials provided with the distribution. 16 1.1 joerg * 17 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 1.1 joerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 1.1 joerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 1.1 joerg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 1.1 joerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 1.1 joerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 1.1 joerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 1.1 joerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 1.1 joerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 1.1 joerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 1.1 joerg * POSSIBILITY OF SUCH DAMAGE. 28 1.1 joerg */ 29 1.1 joerg #ifndef _SPARC64_ELF_SUPPORT_H 30 1.1 joerg #define _SPARC64_ELF_SUPPORT_H 31 1.1 joerg 32 1.1 joerg #ifdef __arch64__ 33 1.1 joerg /* 34 1.1 joerg * Create a jump to the location `target` starting at `where`. 35 1.1 joerg * This requires up to 6 instructions. 36 1.1 joerg * The first instruction is written last as it replaces a branch 37 1.1 joerg * in the PLT during lazy binding. 38 1.1 joerg * The resulting code can trash %g1 and %g5. 39 1.1 joerg */ 40 1.1 joerg static inline void 41 1.1 joerg sparc_write_branch(void *where_, void *target) 42 1.1 joerg { 43 1.1 joerg const unsigned int BAA = 0x30800000U; /* ba,a (offset / 4) */ 44 1.1 joerg const unsigned int SETHI = 0x03000000U; /* sethi %hi(0), %g1 */ 45 1.1 joerg const unsigned int JMP = 0x81c06000U; /* jmpl %g1+%lo(0), %g0 */ 46 1.1 joerg const unsigned int OR = 0x82106000U; /* or %g1, 0, %g1 */ 47 1.1 joerg const unsigned int XOR = 0x82186000U; /* xor %g1, 0, %g1 */ 48 1.1 joerg const unsigned int MOV71 = 0x8213e000U; /* or %o7, 0, %g1 */ 49 1.1 joerg const unsigned int MOV17 = 0x9e106000U; /* or %g1, 0, %o7 */ 50 1.1 joerg const unsigned int CALL = 0x40000000U; /* call 0 */ 51 1.1 joerg const unsigned int SLLX = 0x83287000U; /* sllx %g1, 0, %g1 */ 52 1.1 joerg const unsigned int NEG = 0x82200001U; /* neg %g1 */ 53 1.1 joerg const unsigned int SETHIG5 = 0x0b000000U; /* sethi %hi(0), %g5 */ 54 1.1 joerg const unsigned int ORG5 = 0x82104005U; /* or %g1, %g5, %g1 */ 55 1.1 joerg 56 1.1 joerg unsigned int *where = (unsigned int *)where_; 57 1.1 joerg unsigned long value = (unsigned long)target; 58 1.1 joerg unsigned long offset = value - (unsigned long)where; 59 1.1 joerg 60 1.1 joerg #define HIVAL(v, s) (((v) >> (s)) & 0x003fffffU) 61 1.1 joerg #define LOVAL(v, s) (((v) >> (s)) & 0x000003ffU) 62 1.1 joerg if (offset + 0x800000 <= 0x7ffffc) { 63 1.1 joerg /* Displacement is within 8MB, use a direct branch. */ 64 1.1 joerg where[0] = BAA | ((offset >> 2) & 0x3fffff); 65 1.1 joerg __asm volatile("iflush %0+0" : : "r" (where)); 66 1.1 joerg return; 67 1.1 joerg } 68 1.1 joerg 69 1.1 joerg if (value <= 0xffffffffUL) { 70 1.1 joerg /* 71 1.1 joerg * The absolute address is a 32bit value. 72 1.1 joerg * This can be encoded as: 73 1.1 joerg * sethi %hi(value), %g1 74 1.1 joerg * jmp %g1+%lo(value) 75 1.1 joerg */ 76 1.1 joerg where[1] = JMP | LOVAL(value, 0); 77 1.1 joerg __asm volatile("iflush %0+4" : : "r" (where)); 78 1.1 joerg where[0] = SETHI | HIVAL(value, 10); 79 1.1 joerg __asm volatile("iflush %0+0" : : "r" (where)); 80 1.1 joerg return; 81 1.1 joerg } 82 1.1 joerg 83 1.1 joerg if (value >= 0xffffffff00000000UL) { 84 1.1 joerg /* 85 1.1 joerg * The top 32bit address range can be encoded as: 86 1.1 joerg * sethi %hix(addr), %g1 87 1.1 joerg * xor %g1, %lox(addr), %g1 88 1.1 joerg * jmp %g1 89 1.1 joerg */ 90 1.1 joerg where[2] = JMP; 91 1.1 joerg where[1] = XOR | (value & 0x00003ff) | 0x1c00; 92 1.1 joerg __asm volatile("iflush %0+4" : : "r" (where)); 93 1.1 joerg __asm volatile("iflush %0+8" : : "r" (where)); 94 1.1 joerg where[0] = SETHI | HIVAL(~value, 10); 95 1.1 joerg __asm volatile("iflush %0+0" : : "r" (where)); 96 1.1 joerg return; 97 1.1 joerg } 98 1.1 joerg 99 1.1 joerg if ((offset + 4) + 0x80000000UL <= 0x100000000UL) { 100 1.1 joerg /* 101 1.1 joerg * Displacement of the second instruction is within 102 1.1 joerg * +-2GB. This can use a direct call instruction: 103 1.1 joerg * mov %o7, %g1 104 1.1 joerg * call (value - .) 105 1.1 joerg * mov %g1, %o7 106 1.1 joerg */ 107 1.1 joerg where[1] = CALL | ((-(offset + 4)>> 2) & 0x3fffffffU); 108 1.1 joerg where[2] = MOV17; 109 1.1 joerg __asm volatile("iflush %0+4" : : "r" (where)); 110 1.1 joerg __asm volatile("iflush %0+8" : : "r" (where)); 111 1.1 joerg where[0] = MOV71; 112 1.1 joerg __asm volatile("iflush %0+0" : : "r" (where)); 113 1.1 joerg return; 114 1.1 joerg } 115 1.1 joerg 116 1.1 joerg if (value < 0x100000000000UL) { 117 1.1 joerg /* 118 1.1 joerg * The absolute address is a 44bit value. 119 1.1 joerg * This can be encoded as: 120 1.1 joerg * sethi %h44(addr), %g1 121 1.1 joerg * or %g1, %m44(addr), %g1 122 1.1 joerg * sllx %g1, 12, %g1 123 1.1 joerg * jmp %g1+%l44(addr) 124 1.1 joerg */ 125 1.1 joerg where[1] = OR | (((value) >> 12) & 0x00001fff); 126 1.1 joerg where[2] = SLLX | 12; 127 1.1 joerg where[3] = JMP | LOVAL(value, 0); 128 1.1 joerg __asm volatile("iflush %0+4" : : "r" (where)); 129 1.1 joerg __asm volatile("iflush %0+8" : : "r" (where)); 130 1.1 joerg __asm volatile("iflush %0+12" : : "r" (where)); 131 1.1 joerg where[0] = SETHI | HIVAL(value, 22); 132 1.1 joerg __asm volatile("iflush %0+0" : : "r" (where)); 133 1.1 joerg return; 134 1.1 joerg } 135 1.1 joerg 136 1.1 joerg if (value > 0xfffff00000000000UL) { 137 1.1 joerg /* 138 1.1 joerg * The top 44bit address range can be encoded as: 139 1.1 joerg * sethi %hi((-addr)>>12), %g1 140 1.1 joerg * or %g1, %lo((-addr)>>12), %g1 141 1.1 joerg * neg %g1 142 1.1 joerg * sllx %g1, 12, %g1 143 1.1 joerg * jmp %g1+(addr&0x0fff) 144 1.1 joerg */ 145 1.1 joerg unsigned long neg = (-value)>>12; 146 1.1 joerg where[1] = OR | (LOVAL(neg, 0)+1); 147 1.1 joerg where[2] = NEG; 148 1.1 joerg where[3] = SLLX | 12; 149 1.1 joerg where[4] = JMP | (value & 0x0fff); 150 1.1 joerg __asm volatile("iflush %0+4" : : "r" (where)); 151 1.1 joerg __asm volatile("iflush %0+8" : : "r" (where)); 152 1.1 joerg __asm volatile("iflush %0+12" : : "r" (where)); 153 1.1 joerg __asm volatile("iflush %0+16" : : "r" (where)); 154 1.1 joerg where[0] = SETHI | HIVAL(neg, 10); 155 1.1 joerg __asm volatile("iflush %0+0" : : "r" (where)); 156 1.1 joerg return; 157 1.1 joerg } 158 1.1 joerg 159 1.1 joerg /* 160 1.1 joerg * The general case of a 64bit address is encoded as: 161 1.1 joerg * sethi %hh(addr), %g1 162 1.1 joerg * sethi %lm(addr), %g5 163 1.1 joerg * or %g1, %hm(addr), %g1 164 1.1 joerg * sllx %g1, 32, %g1 165 1.1 joerg * or %g1, %g5, %g1 166 1.1 joerg * jmp %g1+%lo(addr) 167 1.1 joerg */ 168 1.1 joerg where[1] = SETHIG5 | HIVAL(value, 10); 169 1.1 joerg where[2] = OR | LOVAL(value, 32); 170 1.1 joerg where[3] = SLLX | 32; 171 1.1 joerg where[4] = ORG5; 172 1.1 joerg where[5] = JMP | LOVAL(value, 0); 173 1.1 joerg __asm volatile("iflush %0+4" : : "r" (where)); 174 1.1 joerg __asm volatile("iflush %0+8" : : "r" (where)); 175 1.1 joerg __asm volatile("iflush %0+12" : : "r" (where)); 176 1.1 joerg __asm volatile("iflush %0+16" : : "r" (where)); 177 1.1 joerg __asm volatile("iflush %0+20" : : "r" (where)); 178 1.1 joerg where[0] = SETHI | HIVAL(value, 42); 179 1.1 joerg __asm volatile("iflush %0+0" : : "r" (where)); 180 1.1 joerg #undef HIVAL 181 1.1 joerg #undef LOVAL 182 1.1 joerg } 183 1.1 joerg #else 184 1.1 joerg #include <sparc/elf_support.h> 185 1.1 joerg #endif 186 1.1 joerg #endif 187