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