1/*	$OpenBSD: rtld_machine.c,v 1.42 2023/01/29 20:30:21 gnezdo Exp $ */
2
3/*
4 * Copyright (c) 2002,2004 Dale Rahn
5 * Copyright (c) 2001 Niklas Hallqvist
6 * Copyright (c) 2001 Artur Grabowski
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29/*-
30 * Copyright (c) 2000 Eduardo Horvath.
31 * Copyright (c) 1999 The NetBSD Foundation, Inc.
32 * All rights reserved.
33 *
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Paul Kranenburg.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 *    must display the following acknowledgement:
47 *	This product includes software developed by the NetBSD
48 *	Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 *    contributors may be used to endorse or promote products derived
51 *    from this software without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
64 */
65
66#define _DYN_LOADER
67
68#include <sys/types.h>
69#include <sys/exec_elf.h>
70#include <sys/syscall.h>
71#include <sys/unistd.h>
72
73#include <machine/reloc.h>
74
75#include "util.h"
76#include "resolve.h"
77
78int64_t pcookie __attribute__((section(".openbsd.randomdata"))) __dso_hidden;
79
80/*
81 * The following table holds for each relocation type:
82 *	- the width in bits of the memory location the relocation
83 *	  applies to
84 *	- the number of bits the relocation value must be shifted to the
85 *	  right (i.e. discard least significant bits) to fit into
86 *	  the appropriate field in the instruction word.
87 *	- flags indicating whether
88 *		* the relocation involves a symbol
89 *		* the relocation is relative to the current position
90 *		* the relocation is for a GOT entry
91 *		* the relocation is relative to the load address
92 *
93 */
94#define _RF_S		0x80000000		/* Resolve symbol */
95#define _RF_A		0x40000000		/* Use addend */
96#define _RF_P		0x20000000		/* Location relative */
97#define _RF_G		0x10000000		/* GOT offset */
98#define _RF_B		0x08000000		/* Load address relative */
99#define _RF_E		0x02000000		/* ERROR */
100#define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
101#define _RF_RS(s)	((s) & 0xff)		/* right shift */
102static const int reloc_target_flags[] = {
103	0,							/*  0 NONE */
104	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/*  1 _64*/
105	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/*  2 PC32 */
106	_RF_G|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/*  3 GOT32 */
107	_RF_E|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/*  4 PLT32 */
108	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/*  5 COPY */
109	_RF_S|			_RF_SZ(64) | _RF_RS(0),		/*  6 GLOB_DAT*/
110	_RF_S|			_RF_SZ(64) | _RF_RS(0),		/* 7 JUMP_SLOT*/
111	      _RF_A|	_RF_B|	_RF_SZ(64) | _RF_RS(0),		/*  8 RELATIVE*/
112	_RF_E,							/*  9 GOTPCREL*/
113	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 10 32 */
114	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 11 32S */
115	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* 12 16 */
116	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* 13 PC16 */
117	_RF_S|_RF_A|		_RF_SZ(8) | _RF_RS(0),		/* 14 8 */
118	_RF_S|_RF_A|_RF_P|	_RF_SZ(8) | _RF_RS(0),		/* 15 PC8 */
119	_RF_E,							/* 16 DTPMOD64*/
120	_RF_E,							/* 17 DTPOFF64*/
121	_RF_E,							/* 18 TPOFF64 */
122	_RF_E,							/* 19 TLSGD */
123	_RF_E,							/* 20 TLSLD */
124	_RF_E,							/* 21 DTPOFF32*/
125	_RF_E,							/* 22 GOTTPOFF*/
126	_RF_E							/* 23 TPOFF32*/
127};
128
129#define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
130#define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
131#define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
132#define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
133#define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
134#define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
135#define RELOC_ERROR(t) \
136	((t) >= nitems(reloc_target_flags) || (reloc_target_flags[t] & _RF_E))
137
138static const Elf_Addr reloc_target_bitmask[] = {
139#define _BM(x)  (~(Elf_Addr)0 >> ((8*sizeof(reloc_target_bitmask[0])) - (x)))
140	0,			/*  0 NONE */
141	_BM(64),		/*  1 _64*/
142	_BM(32),		/*  2 PC32 */
143	_BM(32),		/*  3 GOT32 */
144	_BM(32),		/*  4 PLT32 */
145	0,			/*  5 COPY */
146	_BM(64),		/*  6 GLOB_DAT*/
147	_BM(64),		/*  7 JUMP_SLOT*/
148	_BM(64),		/*  8 RELATIVE*/
149	_BM(32),		/*  9 GOTPCREL*/
150	_BM(32),		/* 10 32 */
151	_BM(32),		/* 11 32S */
152	_BM(16),		/* 12 16 */
153	_BM(16),		/* 13 PC16 */
154	_BM(8),			/* 14 8 */
155	_BM(8),			/* 15 PC8 */
156	0,			/* 16 DTPMOD64*/
157	0,			/* 17 DTPOFF64*/
158	0,			/* 18 TPOFF64 */
159	0,			/* 19 TLSGD */
160	0,			/* 20 TLSLD */
161	0,			/* 21 DTPOFF32*/
162	0,			/* 22 GOTTPOFF*/
163	0			/* 23 TPOFF32*/
164#undef _BM
165};
166#define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
167
168void _dl_reloc_plt(Elf_Addr *where, Elf_Addr value);
169
170int
171_dl_md_reloc(elf_object_t *object, int rel, int relsz)
172{
173	long	i;
174	long	numrel;
175	long	relrel;
176	int	fails = 0;
177	Elf_Addr loff;
178	Elf_Addr prev_value = 0;
179	const Elf_Sym *prev_sym = NULL;
180	Elf_RelA *rels;
181
182	loff = object->obj_base;
183	numrel = object->Dyn.info[relsz] / sizeof(Elf_RelA);
184	relrel = rel == DT_RELA ? object->relacount : 0;
185	rels = (Elf_RelA *)(object->Dyn.info[rel]);
186	if (rels == NULL)
187		return 0;
188
189	if (relrel > numrel)
190		_dl_die("relacount > numrel: %ld > %ld", relrel, numrel);
191
192	/* tight loop for leading RELATIVE relocs */
193	for (i = 0; i < relrel; i++, rels++) {
194		Elf_Addr *where;
195
196		where = (Elf_Addr *)(rels->r_offset + loff);
197		*where = rels->r_addend + loff;
198	}
199	for (; i < numrel; i++, rels++) {
200		Elf_Addr *where, value, mask;
201		Elf_Word type;
202		const Elf_Sym *sym;
203		const char *symn;
204
205		type = ELF_R_TYPE(rels->r_info);
206
207		if (RELOC_ERROR(type))
208			_dl_die("relocation error %d idx %ld", type, i);
209
210		if (type == R_TYPE(NONE))
211			continue;
212
213		if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL)
214			continue;
215
216		where = (Elf_Addr *)(rels->r_offset + loff);
217
218		if (RELOC_USE_ADDEND(type))
219			value = rels->r_addend;
220		else
221			value = 0;
222
223		sym = NULL;
224		symn = NULL;
225		if (RELOC_RESOLVE_SYMBOL(type)) {
226			sym = object->dyn.symtab;
227			sym += ELF_R_SYM(rels->r_info);
228			symn = object->dyn.strtab + sym->st_name;
229
230			if (sym->st_shndx != SHN_UNDEF &&
231			    ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
232				value += loff;
233			} else if (sym == prev_sym) {
234				value += prev_value;
235			} else {
236				struct sym_res sr;
237
238				sr = _dl_find_symbol(symn,
239				    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
240				    ((type == R_TYPE(JUMP_SLOT))?
241					SYM_PLT:SYM_NOTPLT), sym, object);
242				if (sr.sym == NULL) {
243resolve_failed:
244					if (ELF_ST_BIND(sym->st_info) !=
245					    STB_WEAK)
246						fails++;
247					continue;
248				}
249				prev_sym = sym;
250				prev_value = (Elf_Addr)(sr.obj->obj_base +
251				    sr.sym->st_value);
252				value += prev_value;
253			}
254		}
255
256		if (type == R_TYPE(JUMP_SLOT)) {
257			_dl_reloc_plt(where, value);
258			continue;
259		}
260
261		if (type == R_TYPE(COPY)) {
262			void *dstaddr = where;
263			const void *srcaddr;
264			const Elf_Sym *dstsym = sym;
265			struct sym_res sr;
266
267			sr = _dl_find_symbol(symn,
268			    SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT,
269			    dstsym, object);
270			if (sr.sym == NULL)
271				goto resolve_failed;
272
273			srcaddr = (void *)(sr.obj->obj_base + sr.sym->st_value);
274			_dl_bcopy(srcaddr, dstaddr, dstsym->st_size);
275			continue;
276		}
277
278		if (RELOC_PC_RELATIVE(type))
279			value -= (Elf_Addr)where;
280		if (RELOC_BASE_RELATIVE(type))
281			value += loff;
282
283		mask = RELOC_VALUE_BITMASK(type);
284		value >>= RELOC_VALUE_RIGHTSHIFT(type);
285		value &= mask;
286
287		if (RELOC_TARGET_SIZE(type) > 32) {
288			*where &= ~mask;
289			*where |= value;
290		} else {
291			Elf32_Addr *where32 = (Elf32_Addr *)where;
292
293			*where32 &= ~mask;
294			*where32 |= value;
295		}
296	}
297
298	return fails;
299}
300
301void
302_dl_reloc_plt(Elf_Addr *where, Elf_Addr value)
303{
304	*where = value;
305}
306
307/*
308 * Resolve a symbol at run-time.
309 */
310Elf_Addr
311_dl_bind(elf_object_t *object, int index)
312{
313	Elf_RelA *rel;
314	const Elf_Sym *sym;
315	const char *symn;
316	struct sym_res sr;
317	int64_t cookie = pcookie;
318	struct {
319		struct __kbind param;
320		Elf_Addr newval;
321	} buf;
322
323	rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]) + index;
324
325	sym = object->dyn.symtab;
326	sym += ELF_R_SYM(rel->r_info);
327	symn = object->dyn.strtab + sym->st_name;
328
329	sr = _dl_find_symbol(symn, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT,
330	    sym, object);
331	if (sr.sym == NULL)
332		_dl_die("lazy binding failed!");
333
334	buf.newval = sr.obj->obj_base + sr.sym->st_value;
335
336	if (__predict_false(sr.obj->traced) && _dl_trace_plt(sr.obj, symn))
337		return buf.newval;
338
339	buf.param.kb_addr = (Elf_Word *)(object->obj_base + rel->r_offset);
340	buf.param.kb_size = sizeof(Elf_Addr);
341
342	/* directly code the syscall, so that it's actually inline here */
343	{
344		register long syscall_num __asm("rax") = SYS_kbind;
345		register void *arg1 __asm("rdi") = &buf;
346		register long  arg2 __asm("rsi") = sizeof(buf);
347		register long  arg3 __asm("rdx") = cookie;
348
349		__asm volatile("syscall" : "+r" (syscall_num), "+r" (arg3) :
350		    "r" (arg1), "r" (arg2) : "cc", "rcx", "r11", "memory");
351	}
352	return buf.newval;
353}
354
355int
356_dl_md_reloc_got(elf_object_t *object, int lazy)
357{
358	extern void _dl_bind_start(void);	/* XXX */
359	int	fails = 0;
360	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
361	int i, num;
362	Elf_RelA *rel;
363
364	if (pltgot == NULL)
365		return 0; /* it is possible to have no PLT/GOT relocations */
366
367	if (object->Dyn.info[DT_PLTREL] != DT_RELA)
368		return 0;
369
370	if (__predict_false(!lazy)) {
371		fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
372	} else {
373		pltgot[1] = (Elf_Addr)object;
374		pltgot[2] = (Elf_Addr)&_dl_bind_start;
375
376		rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]);
377		num = (object->Dyn.info[DT_PLTRELSZ]);
378		for (i = 0; i < num/sizeof(Elf_RelA); i++, rel++) {
379			Elf_Addr *where;
380			where = (Elf_Addr *)(rel->r_offset + object->obj_base);
381			*where += object->obj_base;
382		}
383	}
384
385	return fails;
386}
387