1/*	$NetBSD: kobj_machdep.c,v 1.6 2023/04/28 07:33:56 skrll Exp $	*/
2
3/*-
4 * Copyright (c) 1999, 2002, 2008 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 and by Charles M. Hannum.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#define	ELFSIZE		ARCH_ELFSIZE
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kobj.h>
37#include <sys/exec.h>
38#include <sys/exec_elf.h>
39
40/*
41 * The following table holds for each relocation type:
42 *	- the width in bits of the memory location the relocation
43 *	  applies to (not currently used)
44 *	- the number of bits the relocation value must be shifted to the
45 *	  right (i.e. discard least significant bits) to fit into
46 *	  the appropriate field in the instruction word.
47 *	- flags indicating whether
48 *		* the relocation involves a symbol
49 *		* the relocation is relative to the current position
50 *		* the relocation is for a GOT entry
51 *		* the relocation is relative to the load address
52 *
53 */
54#define _RF_S		0x80000000		/* Resolve symbol */
55#define _RF_A		0x40000000		/* Use addend */
56#define _RF_P		0x20000000		/* Location relative */
57#define _RF_G		0x10000000		/* GOT offset */
58#define _RF_B		0x08000000		/* Load address relative */
59#define _RF_U		0x04000000		/* Unaligned */
60#define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
61#define _RF_RS(s)	( (s) & 0xff)		/* right shift */
62static const int reloc_target_flags[] = {
63	0,							/* NONE */
64	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
65	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
66	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
67	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
68	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
69	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
70	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
71	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
72	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
73	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
74	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
75	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
76	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
77	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
78	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
79	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
80	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
81	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
82				_RF_SZ(32) | _RF_RS(0),		/* COPY */
83	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_DAT */
84				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
85	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),		/* RELATIVE */
86	_RF_S|_RF_A|	_RF_U|	_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
87};
88
89#ifdef RTLD_DEBUG_RELOC
90static const char *reloc_names[] = {
91	"NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
92	"DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
93	"22", "13", "LO10", "GOT10", "GOT13",
94	"GOT22", "PC10", "PC22", "WPLT30", "COPY",
95	"GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32"
96};
97#endif
98
99#define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
100#define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
101#define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
102#define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
103#define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
104#define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
105#define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
106
107static const int reloc_target_bitmask[] = {
108#define _BM(x)	(~(-(1ULL << (x))))
109	0,				/* NONE */
110	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
111	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
112	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
113	_BM(22), _BM(22),		/* HI22, _22 */
114	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
115	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
116	_BM(10), _BM(22),		/* _PC10, _PC22 */
117	_BM(30), 0,			/* _WPLT30, _COPY */
118	-1, -1, -1,			/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
119	_BM(32)				/* _UA32 */
120#undef _BM
121};
122#define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
123
124int
125kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data,
126	   bool isrela, bool local)
127{
128	const Elf_Rela *rela;
129	Elf_Addr *where, addr;
130	Elf_Word value, mask;
131	uintptr_t tmp;
132	u_int symidx, type;
133	int error;
134
135	rela = data;
136	where = (Elf_Addr *) (relocbase + rela->r_offset);
137	symidx = ELF_R_SYM(rela->r_info);
138	type = ELF_R_TYPE(rela->r_info);
139	value = rela->r_addend;
140
141	const Elf_Sym *sym = kobj_symbol(ko, symidx);
142
143	if (!local && ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
144		return 0;
145	}
146
147	if (type == R_TYPE(NONE))
148		return 0;
149
150	if (!isrela) {
151		printf("kobj_reloc: only support RELA relocations\n");
152		return -1;
153	}
154
155	if (type == R_TYPE(JMP_SLOT) || type == R_TYPE(COPY) ||
156	    type > R_TYPE(6)) {
157		printf("kobj_reloc: unexpected reloc type %d\n", type);
158		return -1;
159	}
160
161	if (type == R_TYPE(RELATIVE)) {
162		*where += (Elf_Addr)(relocbase + value);
163		return 0;
164	}
165
166	if (RELOC_RESOLVE_SYMBOL(type)) {
167		error = kobj_sym_lookup(ko, symidx, &addr);
168		if (error)
169			return -1;
170		value += addr;
171	}
172
173	if (RELOC_PC_RELATIVE(type)) {
174		value -= (Elf_Word)where;
175	}
176
177	if (RELOC_BASE_RELATIVE(type)) {
178		value += (Elf_Word)(relocbase + *where);
179	}
180
181	mask = RELOC_VALUE_BITMASK(type);
182	value >>= RELOC_VALUE_RIGHTSHIFT(type);
183	value &= mask;
184
185	if (RELOC_UNALIGNED(type)) {
186		/* Handle unaligned relocations. */
187		char *ptr = (char *)where;
188		int i, size = RELOC_TARGET_SIZE(type)/8;
189
190		/* Read it in one byte at a time. */
191		for (i = 0, tmp = 0; i < size; i++)
192			tmp = (tmp << 8) | ptr[i];
193
194		tmp &= ~mask;
195		tmp |= value;
196
197		/* Write it back out. */
198		for (i=0; i<size; i++)
199			ptr[i] = ((tmp >> (8*i)) & 0xff);
200
201	} else {
202		*where &= ~mask;
203		*where |= value;
204	}
205
206	return 0;
207}
208
209int
210kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
211{
212
213	return 0;
214}
215