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