1 /*-
2 * Copyright (c) 2001 Jake Burkholder.
3 * Copyright (c) 2000 Eduardo Horvath.
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 *
38 * from: NetBSD: mdreloc.c,v 1.5 2001/04/25 12:24:51 kleink Exp
39 * $FreeBSD: releng/5.2/sys/sparc64/sparc64/elf_machdep.c 120422 2003-09-25 01:10:26Z peter $
40 */
41
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/exec.h>
46 #include <sys/imgact.h>
47 #include <sys/linker.h>
48 #include <sys/sysent.h>
49 #include <sys/imgact_elf.h>
50 #include <sys/syscall.h>
51 #include <sys/signalvar.h>
52 #include <sys/vnode.h>
53
54 #include <vm/vm.h>
55 #include <vm/vm_param.h>
56
57 #include <machine/elf.h>
58
59 #include "linker_if.h"
60
61 struct sysentvec elf64_freebsd_sysvec = {
62 SYS_MAXSYSCALL,
63 sysent,
64 0,
65 0,
66 NULL,
67 0,
68 NULL,
69 NULL,
70 __elfN(freebsd_fixup),
71 sendsig,
72 NULL,
73 NULL,
74 NULL,
75 "FreeBSD ELF64",
76 __elfN(coredump),
77 NULL,
78 MINSIGSTKSZ,
79 PAGE_SIZE,
80 VM_MIN_ADDRESS,
81 VM_MAXUSER_ADDRESS,
82 USRSTACK,
83 PS_STRINGS,
84 VM_PROT_READ | VM_PROT_WRITE,
85 exec_copyout_strings,
86 exec_setregs,
87 NULL
88 };
89
90 static Elf64_Brandinfo freebsd_brand_info = {
91 ELFOSABI_FREEBSD,
92 EM_SPARCV9,
93 "FreeBSD",
94 "",
95 "/libexec/ld-elf.so.1",
96 &elf64_freebsd_sysvec
97 };
98
99 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
100 (sysinit_cfunc_t) elf64_insert_brand_entry,
101 &freebsd_brand_info);
102
103 /*
104 * The following table holds for each relocation type:
105 * - the width in bits of the memory location the relocation
106 * applies to (not currently used)
107 * - the number of bits the relocation value must be shifted to the
108 * right (i.e. discard least significant bits) to fit into
109 * the appropriate field in the instruction word.
110 * - flags indicating whether
111 * * the relocation involves a symbol
112 * * the relocation is relative to the current position
113 * * the relocation is for a GOT entry
114 * * the relocation is relative to the load address
115 *
116 */
117 #define _RF_S 0x80000000 /* Resolve symbol */
118 #define _RF_A 0x40000000 /* Use addend */
119 #define _RF_P 0x20000000 /* Location relative */
120 #define _RF_G 0x10000000 /* GOT offset */
121 #define _RF_B 0x08000000 /* Load address relative */
122 #define _RF_U 0x04000000 /* Unaligned */
123 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */
124 #define _RF_RS(s) ( (s) & 0xff) /* right shift */
125 static int reloc_target_flags[] = {
126 0, /* NONE */
127 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */
128 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */
129 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */
130 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */
131 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */
132 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */
133 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */
134 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */
135 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */
136 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */
137 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */
138 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */
139 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */
140 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */
141 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */
142 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */
143 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */
144 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */
145 _RF_SZ(32) | _RF_RS(0), /* COPY */
146 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */
147 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
148 _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */
149 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
150
151 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */
152 _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */
153 _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
154 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */
155 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */
156 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */
157 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */
158 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */
159 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */
160 _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */
161 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */
162 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */
163 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */
164 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */
165 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */
166 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */
167 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */
168 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */
169 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */
170 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */
171 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */
172 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */
173 _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */
174 _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */
175 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */
176 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */
177 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */
178 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */
179 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */
180 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */
181 _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */
182 _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */
183 };
184
185 #if 0
186 static const char *reloc_names[] = {
187 "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
188 "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
189 "22", "13", "LO10", "GOT10", "GOT13",
190 "GOT22", "PC10", "PC22", "WPLT30", "COPY",
191 "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
192 "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
193 "10", "11", "64", "OLO10", "HH22",
194 "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
195 "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
196 "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
197 "L44", "REGISTER", "UA64", "UA16"
198 };
199 #endif
200
201 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
202 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
203 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
204 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0)
205 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0)
206 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
207 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
208
209 static long reloc_target_bitmask[] = {
210 #define _BM(x) (~(-(1ULL << (x))))
211 0, /* NONE */
212 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
213 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
214 _BM(30), _BM(22), /* WDISP30, WDISP22 */
215 _BM(22), _BM(22), /* HI22, _22 */
216 _BM(13), _BM(10), /* RELOC_13, _LO10 */
217 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
218 _BM(10), _BM(22), /* _PC10, _PC22 */
219 _BM(30), 0, /* _WPLT30, _COPY */
220 _BM(32), _BM(32), _BM(32), /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
221 _BM(32), _BM(32), /* _UA32, PLT32 */
222 _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */
223 _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */
224 _BM(10), _BM(11), -1, /* _10, _11, _64 */
225 _BM(10), _BM(22), /* _OLO10, _HH22 */
226 _BM(10), _BM(22), /* _HM10, _LM22 */
227 _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */
228 _BM(16), _BM(19), /* _WDISP16, _WDISP19 */
229 -1, /* GLOB_JMP */
230 _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */
231 -1, -1, /* DISP64, PLT64 */
232 _BM(22), _BM(13), /* HIX22, LOX10 */
233 _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */
234 -1, -1, _BM(16), /* REGISTER, UA64, UA16 */
235 #undef _BM
236 };
237 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
238
239 int
240 elf_reloc_local(linker_file_t lf, const void *data, int type)
241 {
242 const Elf_Rela *rela;
243 Elf_Addr value;
244 Elf_Addr *where;
245
246 if (type != ELF_RELOC_RELA)
247 return (-1);
248
249 rela = (const Elf_Rela *)data;
250 if (ELF_R_TYPE(rela->r_info) != R_SPARC_RELATIVE)
251 return (-1);
252
253 value = rela->r_addend + (Elf_Addr)lf->address;
254 where = (Elf_Addr *)((Elf_Addr)lf->address + rela->r_offset);
255
256 *where = value;
257
258 return (0);
259 }
260
261 /* Process one elf relocation with addend. */
262 int
263 elf_reloc(linker_file_t lf, const void *data, int type)
264 {
265 const Elf_Rela *rela;
266 Elf_Addr relocbase;
267 Elf_Half *where32;
268 Elf_Addr *where;
269 Elf_Word rtype, symidx;
270 Elf_Addr value;
271 Elf_Addr mask;
272 Elf_Addr addr;
273
274 if (type != ELF_RELOC_RELA)
275 return (-1);
276
277 relocbase = (Elf_Addr)lf->address;
278 rela = (const Elf_Rela *)data;
279 where = (Elf_Addr *)(relocbase + rela->r_offset);
280 where32 = (Elf_Half *)where;
281 rtype = ELF_R_TYPE(rela->r_info);
282 symidx = ELF_R_SYM(rela->r_info);
283
284 if (rtype == R_SPARC_NONE || rtype == R_SPARC_RELATIVE)
285 return (0);
286
287 if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY ||
288 rtype > R_SPARC_UA16)
289 return (-1);
290
291 if (RELOC_UNALIGNED(rtype))
292 return (-1);
293
294 value = rela->r_addend;
295
296 if (RELOC_RESOLVE_SYMBOL(rtype)) {
297 addr = elf_lookup(lf, symidx, 1);
298 if (addr == 0)
299 return (-1);
300 value += addr;
301 }
302
303 if (RELOC_PC_RELATIVE(rtype))
304 value -= (Elf_Addr)where;
305
306 if (RELOC_BASE_RELATIVE(rtype))
307 value += relocbase;
308
309 mask = RELOC_VALUE_BITMASK(rtype);
310 value >>= RELOC_VALUE_RIGHTSHIFT(rtype);
311 value &= mask;
312
313 if (RELOC_TARGET_SIZE(rtype) > 32) {
314 *where &= ~mask;
315 *where |= value;
316 } else {
317 *where32 &= ~mask;
318 *where32 |= value;
319 }
320
321 return (0);
322 }
323
324 int
325 elf_cpu_load_file(linker_file_t lf __unused)
326 {
327
328 return (0);
329 }
330
331 int
332 elf_cpu_unload_file(linker_file_t lf __unused)
333 {
334
335 return (0);
336 }
Cache object: 7b841c31792122f734b76a26e977c611
|