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 */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/exec.h>
48 #include <sys/imgact.h>
49 #include <sys/linker.h>
50 #include <sys/sysent.h>
51 #include <sys/imgact_elf.h>
52 #include <sys/syscall.h>
53 #include <sys/signalvar.h>
54 #include <sys/vnode.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_param.h>
58
59 #include <machine/elf.h>
60
61 #include "linker_if.h"
62
63 static struct sysentvec elf64_freebsd_sysvec = {
64 SYS_MAXSYSCALL,
65 sysent,
66 0,
67 0,
68 NULL,
69 0,
70 NULL,
71 NULL,
72 __elfN(freebsd_fixup),
73 sendsig,
74 NULL,
75 NULL,
76 NULL,
77 "FreeBSD ELF64",
78 __elfN(coredump),
79 NULL,
80 MINSIGSTKSZ,
81 PAGE_SIZE,
82 VM_MIN_ADDRESS,
83 VM_MAXUSER_ADDRESS,
84 USRSTACK,
85 PS_STRINGS,
86 VM_PROT_READ | VM_PROT_WRITE,
87 exec_copyout_strings,
88 exec_setregs,
89 NULL
90 };
91
92 static Elf64_Brandinfo freebsd_brand_info = {
93 ELFOSABI_FREEBSD,
94 EM_SPARCV9,
95 "FreeBSD",
96 NULL,
97 "/libexec/ld-elf.so.1",
98 &elf64_freebsd_sysvec,
99 NULL,
100 BI_CAN_EXEC_DYN,
101 };
102
103 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
104 (sysinit_cfunc_t) elf64_insert_brand_entry,
105 &freebsd_brand_info);
106
107 static Elf64_Brandinfo freebsd_brand_oinfo = {
108 ELFOSABI_FREEBSD,
109 EM_SPARCV9,
110 "FreeBSD",
111 NULL,
112 "/usr/libexec/ld-elf.so.1",
113 &elf64_freebsd_sysvec,
114 NULL,
115 BI_CAN_EXEC_DYN,
116 };
117
118 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
119 (sysinit_cfunc_t) elf64_insert_brand_entry,
120 &freebsd_brand_oinfo);
121
122
123 void
124 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
125 size_t *off __unused)
126 {
127 }
128
129
130 /*
131 * The following table holds for each relocation type:
132 * - the width in bits of the memory location the relocation
133 * applies to (not currently used)
134 * - the number of bits the relocation value must be shifted to the
135 * right (i.e. discard least significant bits) to fit into
136 * the appropriate field in the instruction word.
137 * - flags indicating whether
138 * * the relocation involves a symbol
139 * * the relocation is relative to the current position
140 * * the relocation is for a GOT entry
141 * * the relocation is relative to the load address
142 *
143 */
144 #define _RF_S 0x80000000 /* Resolve symbol */
145 #define _RF_A 0x40000000 /* Use addend */
146 #define _RF_P 0x20000000 /* Location relative */
147 #define _RF_G 0x10000000 /* GOT offset */
148 #define _RF_B 0x08000000 /* Load address relative */
149 #define _RF_U 0x04000000 /* Unaligned */
150 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */
151 #define _RF_RS(s) ( (s) & 0xff) /* right shift */
152 static const int reloc_target_flags[] = {
153 0, /* NONE */
154 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */
155 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */
156 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */
157 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */
158 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */
159 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */
160 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */
161 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */
162 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */
163 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */
164 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */
165 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */
166 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */
167 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */
168 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */
169 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */
170 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */
171 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */
172 _RF_SZ(32) | _RF_RS(0), /* COPY */
173 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */
174 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
175 _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */
176 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
177
178 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */
179 _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */
180 _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
181 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */
182 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */
183 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */
184 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */
185 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */
186 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */
187 _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */
188 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */
189 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */
190 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */
191 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */
192 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */
193 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */
194 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */
195 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */
196 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */
197 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */
198 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */
199 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */
200 _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */
201 _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */
202 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */
203 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */
204 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */
205 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */
206 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */
207 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */
208 _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */
209 _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */
210 };
211
212 #if 0
213 static const char *reloc_names[] = {
214 "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
215 "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
216 "22", "13", "LO10", "GOT10", "GOT13",
217 "GOT22", "PC10", "PC22", "WPLT30", "COPY",
218 "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
219 "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
220 "10", "11", "64", "OLO10", "HH22",
221 "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
222 "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
223 "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
224 "L44", "REGISTER", "UA64", "UA16"
225 };
226 #endif
227
228 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
229 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
230 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
231 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0)
232 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0)
233 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
234 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
235
236 static const long reloc_target_bitmask[] = {
237 #define _BM(x) (~(-(1ULL << (x))))
238 0, /* NONE */
239 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
240 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
241 _BM(30), _BM(22), /* WDISP30, WDISP22 */
242 _BM(22), _BM(22), /* HI22, _22 */
243 _BM(13), _BM(10), /* RELOC_13, _LO10 */
244 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
245 _BM(10), _BM(22), /* _PC10, _PC22 */
246 _BM(30), 0, /* _WPLT30, _COPY */
247 _BM(32), _BM(32), _BM(32), /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
248 _BM(32), _BM(32), /* _UA32, PLT32 */
249 _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */
250 _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */
251 _BM(10), _BM(11), -1, /* _10, _11, _64 */
252 _BM(13), _BM(22), /* _OLO10, _HH22 */
253 _BM(10), _BM(22), /* _HM10, _LM22 */
254 _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */
255 _BM(16), _BM(19), /* _WDISP16, _WDISP19 */
256 -1, /* GLOB_JMP */
257 _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */
258 -1, -1, /* DISP64, PLT64 */
259 _BM(22), _BM(13), /* HIX22, LOX10 */
260 _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */
261 -1, -1, _BM(16), /* REGISTER, UA64, UA16 */
262 #undef _BM
263 };
264 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
265
266 int
267 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
268 int type, elf_lookup_fn lookup)
269 {
270 const Elf_Rela *rela;
271 Elf_Addr value;
272 Elf_Addr *where;
273
274 if (type != ELF_RELOC_RELA)
275 return (-1);
276
277 rela = (const Elf_Rela *)data;
278 if (ELF64_R_TYPE_ID(rela->r_info) != R_SPARC_RELATIVE)
279 return (-1);
280
281 value = rela->r_addend + (Elf_Addr)lf->address;
282 where = (Elf_Addr *)((Elf_Addr)lf->address + rela->r_offset);
283
284 *where = value;
285
286 return (0);
287 }
288
289 /* Process one elf relocation with addend. */
290 int
291 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
292 elf_lookup_fn lookup)
293 {
294 const Elf_Rela *rela;
295 Elf_Word *where32;
296 Elf_Addr *where;
297 Elf_Size rtype, symidx;
298 Elf_Addr value;
299 Elf_Addr mask;
300 Elf_Addr addr;
301
302 if (type != ELF_RELOC_RELA)
303 return (-1);
304
305 rela = (const Elf_Rela *)data;
306 where = (Elf_Addr *)(relocbase + rela->r_offset);
307 where32 = (Elf_Word *)where;
308 rtype = ELF64_R_TYPE_ID(rela->r_info);
309 symidx = ELF_R_SYM(rela->r_info);
310
311 if (rtype == R_SPARC_NONE || rtype == R_SPARC_RELATIVE)
312 return (0);
313
314 if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY ||
315 rtype >= sizeof(reloc_target_bitmask) /
316 sizeof(*reloc_target_bitmask))
317 return (-1);
318
319 if (RELOC_UNALIGNED(rtype))
320 return (-1);
321
322 value = rela->r_addend;
323
324 if (RELOC_RESOLVE_SYMBOL(rtype)) {
325 addr = lookup(lf, symidx, 1);
326 if (addr == 0)
327 return (-1);
328 value += addr;
329 }
330
331 if (rtype == R_SPARC_OLO10)
332 value = (value & 0x3ff) + ELF64_R_TYPE_DATA(rela->r_info);
333
334 if (RELOC_PC_RELATIVE(rtype))
335 value -= (Elf_Addr)where;
336
337 if (RELOC_BASE_RELATIVE(rtype))
338 value += relocbase;
339
340 mask = RELOC_VALUE_BITMASK(rtype);
341 value >>= RELOC_VALUE_RIGHTSHIFT(rtype);
342 value &= mask;
343
344 if (RELOC_TARGET_SIZE(rtype) > 32) {
345 *where &= ~mask;
346 *where |= value;
347 } else {
348 *where32 &= ~mask;
349 *where32 |= value;
350 }
351
352 return (0);
353 }
354
355 int
356 elf_cpu_load_file(linker_file_t lf __unused)
357 {
358
359 return (0);
360 }
361
362 int
363 elf_cpu_unload_file(linker_file_t lf __unused)
364 {
365
366 return (0);
367 }
Cache object: 2cf8bd39ea52d3c73fc321090e36e3f4
|