rtld_machine.c revision 1.16
1/*	$OpenBSD: rtld_machine.c,v 1.16 2004/05/25 21:42:48 mickey Exp $ */
2
3/*
4 * Copyright (c) 2002 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/cdefs.h>
70#include <sys/mman.h>
71
72#include <nlist.h>
73#include <link.h>
74#include <signal.h>
75
76#include "syscall.h"
77#include "archdep.h"
78#include "resolve.h"
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 (not currently used)
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_U		0x04000000		/* Unaligned */
100#define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
101#define _RF_RS(s)	((s) & 0xff)		/* right shift */
102static int reloc_target_flags[] = {
103	0,							/* NONE */
104	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32*/
105	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC32 */
106	_RF_G|			_RF_SZ(32) | _RF_RS(00),	/* GOT32 */
107	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
108	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/* COPY */
109	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_DAT */
110	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/* JUMP_SLOT */
111	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),		/* RELATIVE */
112	0,							/* GOTOFF XXX */
113	0,							/* GOTPC XXX */
114	0,							/* DUMMY 11 */
115	0,							/* DUMMY 12 */
116	0,							/* DUMMY 13 */
117	0,							/* DUMMY 14 */
118	0,							/* DUMMY 15 */
119	0,							/* DUMMY 16 */
120	0,							/* DUMMY 17 */
121	0,							/* DUMMY 18 */
122	0,							/* DUMMY 19 */
123	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
124	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* PC_16 */
125	_RF_S|_RF_A|		_RF_SZ(8) | _RF_RS(0),		/* RELOC_8 */
126	_RF_S|_RF_A|_RF_P|	_RF_SZ(8) | _RF_RS(0),		/* RELOC_PC8 */
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_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
133#define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
134#define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
135#define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
136
137static long reloc_target_bitmask[] = {
138#define _BM(x)	(~(-(1ULL << (x))))
139	0,		/* NONE */
140	_BM(32),	/* RELOC_32*/
141	_BM(32),	/* PC32 */
142	_BM(32),	/* GOT32 */
143	_BM(32),	/* PLT32 */
144	0,		/* COPY */
145	_BM(32),	/* GLOB_DAT */
146	_BM(32),	/* JUMP_SLOT */
147	_BM(32),	/* RELATIVE */
148	0,		/* GOTOFF XXX */
149	0,		/* GOTPC XXX */
150	0,		/* DUMMY 11 */
151	0,		/* DUMMY 12 */
152	0,		/* DUMMY 13 */
153	0,		/* DUMMY 14 */
154	0,		/* DUMMY 15 */
155	0,		/* DUMMY 16 */
156	0,		/* DUMMY 17 */
157	0,		/* DUMMY 18 */
158	0,		/* DUMMY 19 */
159	_BM(16),	/* RELOC_16 */
160	_BM(8),		/* PC_16 */
161	_BM(8),		/* RELOC_8 */
162	_BM(8),		/* RELOC_PC8 */
163#undef _BM
164};
165#define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
166
167void _dl_reloc_plt(Elf_Addr *where, Elf_Addr value);
168
169int
170_dl_md_reloc(elf_object_t *object, int rel, int relsz)
171{
172	long	i;
173	long	numrel;
174	long	fails = 0;
175	Elf_Addr loff;
176	Elf_Rel *rels;
177	struct load_list *llist;
178
179	loff = object->load_offs;
180	numrel = object->Dyn.info[relsz] / sizeof(Elf32_Rel);
181	rels = (Elf32_Rel *)(object->Dyn.info[rel]);
182	if (rels == NULL)
183		return(0);
184
185	/*
186	 * unprotect some segments if we need it.
187	 */
188	if ((rel == DT_REL || rel == DT_RELA)) {
189		for (llist = object->load_list; llist != NULL; llist = llist->next) {
190			if (!(llist->prot & PROT_WRITE))
191				_dl_mprotect(llist->start, llist->size,
192				    llist->prot|PROT_WRITE);
193		}
194	}
195
196	for (i = 0; i < numrel; i++, rels++) {
197		Elf_Addr *where, value, ooff, mask;
198		Elf_Word type;
199		const Elf_Sym *sym, *this;
200		const char *symn;
201
202		type = ELF_R_TYPE(rels->r_info);
203
204		if (type == R_TYPE(NONE))
205			continue;
206
207		if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL)
208			continue;
209
210		where = (Elf_Addr *)(rels->r_offset + loff);
211
212		if (RELOC_USE_ADDEND(type))
213			value = *where & RELOC_VALUE_BITMASK(type);
214		else
215			value = 0;
216
217		sym = NULL;
218		symn = NULL;
219		if (RELOC_RESOLVE_SYMBOL(type)) {
220			sym = object->dyn.symtab;
221			sym += ELF_R_SYM(rels->r_info);
222			symn = object->dyn.strtab + sym->st_name;
223
224			if (sym->st_shndx != SHN_UNDEF &&
225			    ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
226				value += loff;
227			} else {
228				this = NULL;
229				ooff = _dl_find_symbol_bysym(object,
230				    ELF_R_SYM(rels->r_info), _dl_objects,
231				    &this,NULL,SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
232				    ((type == R_TYPE(JUMP_SLOT))?
233					SYM_PLT:SYM_NOTPLT),
234				    sym->st_size);
235				if (this == NULL) {
236resolve_failed:
237					_dl_printf("%s: %s: can't resolve "
238					    "reference '%s'\n",
239					    _dl_progname, object->load_name,
240					    symn);
241					fails++;
242					continue;
243				}
244				value += (Elf_Addr)(ooff + this->st_value);
245			}
246		}
247
248		if (type == R_TYPE(JUMP_SLOT)) {
249			_dl_reloc_plt((Elf_Word *)where, value);
250			continue;
251		}
252
253		if (type == R_TYPE(COPY)) {
254			void *dstaddr = where;
255			const void *srcaddr;
256			const Elf_Sym *dstsym = sym, *srcsym = NULL;
257			size_t size = dstsym->st_size;
258			Elf_Addr soff;
259
260			soff = _dl_find_symbol(symn, object->next, &srcsym,
261			    NULL, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
262			    ((type == R_TYPE(JUMP_SLOT)) ? SYM_PLT:SYM_NOTPLT),
263			    size, object);
264			if (srcsym == NULL)
265				goto resolve_failed;
266
267			srcaddr = (void *)(soff + srcsym->st_value);
268			_dl_bcopy(srcaddr, dstaddr, size);
269			continue;
270		}
271
272		if (RELOC_PC_RELATIVE(type))
273			value -= (Elf_Addr)where;
274		if (RELOC_BASE_RELATIVE(type))
275			value += loff;
276
277		mask = RELOC_VALUE_BITMASK(type);
278		value >>= RELOC_VALUE_RIGHTSHIFT(type);
279		value &= mask;
280
281		if (RELOC_UNALIGNED(type)) {
282			/* Handle unaligned relocations. */
283			Elf_Addr tmp = 0;
284			char *ptr = (char *)where;
285			int i, size = RELOC_TARGET_SIZE(type)/8;
286
287			/* Read it in one byte at a time. */
288			for (i=0; i<size; i++)
289				tmp = (tmp << 8) | ptr[i];
290
291			tmp &= ~mask;
292			tmp |= value;
293
294			/* Write it back out. */
295			for (i=0; i<size; i++)
296				ptr[i] = ((tmp >> (8*i)) & 0xff);
297		} else if (RELOC_TARGET_SIZE(type) > 32) {
298			*where &= ~mask;
299			*where |= value;
300		} else {
301			Elf32_Addr *where32 = (Elf32_Addr *)where;
302
303			*where32 &= ~mask;
304			*where32 |= value;
305		}
306	}
307
308	/* reprotect the unprotected segments */
309	if ((rel == DT_REL || rel == DT_RELA)) {
310		for (llist = object->load_list; llist != NULL; llist = llist->next) {
311			if (!(llist->prot & PROT_WRITE))
312				_dl_mprotect(llist->start, llist->size,
313				    llist->prot);
314		}
315	}
316
317	return (fails);
318}
319
320#if 0
321struct jmpslot {
322	u_short opcode;
323	u_short addr[2];
324	u_short reloc_index;
325#define JMPSLOT_RELOC_MASK	0xffff
326};
327#define JUMP			0xe990	/* NOP + JMP opcode */
328#endif
329
330void
331_dl_reloc_plt(Elf_Addr *where, Elf_Addr value)
332{
333	*where = value;
334}
335
336/*
337 * Resolve a symbol at run-time.
338 */
339Elf_Addr
340_dl_bind(elf_object_t *object, int index)
341{
342	Elf_Rel *rel;
343	Elf_Word *addr;
344	const Elf_Sym *sym, *this;
345	const char *symn;
346	Elf_Addr ooff;
347	sigset_t omask, nmask;
348
349	rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
350
351	rel += index/sizeof(Elf_Rel);
352
353	sym = object->dyn.symtab;
354	sym += ELF_R_SYM(rel->r_info);
355	symn = object->dyn.strtab + sym->st_name;
356
357	addr = (Elf_Word *)(object->load_offs + rel->r_offset);
358	this = NULL;
359	ooff = _dl_find_symbol(symn, _dl_objects, &this, NULL,
360	    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, sym->st_size, object);
361	if (this == NULL) {
362		_dl_printf("lazy binding failed!\n");
363		*((int *)0) = 0;	/* XXX */
364	}
365
366	/* if GOT is protected, allow the write */
367	if (object->got_size != 0) {
368		sigfillset(&nmask);
369		_dl_sigprocmask(SIG_BLOCK, &nmask, &omask);
370		_dl_mprotect((void*)object->got_start, object->got_size,
371		    PROT_READ|PROT_WRITE);
372	}
373
374	_dl_reloc_plt(addr, ooff + this->st_value);
375
376	/* put the GOT back to RO */
377	if (object->got_size != 0) {
378		_dl_mprotect((void*)object->got_start, object->got_size,
379		    PROT_READ);
380		_dl_sigprocmask(SIG_SETMASK, &omask, NULL);
381	}
382
383	return((Elf_Addr)ooff + this->st_value);
384}
385
386void
387_dl_md_reloc_got(elf_object_t *object, int lazy)
388{
389	extern void _dl_bind_start(void);	/* XXX */
390	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
391	int i, num;
392	Elf_Rel *rel;
393	struct load_list *llist;
394	Elf_Addr ooff;
395	const Elf_Sym *this;
396
397	if (pltgot == NULL)
398		return; /* it is possible to have no PLT/GOT relocations */
399
400	pltgot[1] = (Elf_Addr)object;
401	pltgot[2] = (Elf_Addr)&_dl_bind_start;
402
403	if (object->Dyn.info[DT_PLTREL] != DT_REL)
404		return;
405
406	object->got_addr = NULL;
407	object->got_size = 0;
408	this = NULL;
409	ooff = _dl_find_symbol("__got_start", object, &this, NULL,
410	    SYM_SEARCH_SELF|SYM_NOWARNNOTFOUND|SYM_PLT, 0, object);
411	if (this != NULL)
412		object->got_addr = ooff + this->st_value;
413
414	this = NULL;
415	ooff = _dl_find_symbol("__got_end", object, &this, NULL,
416	    SYM_SEARCH_SELF|SYM_NOWARNNOTFOUND|SYM_PLT, 0, object);
417	if (this != NULL)
418		object->got_size = ooff + this->st_value  - object->got_addr;
419
420	if (object->got_addr == NULL)
421		object->got_start = NULL;
422	else {
423		object->got_start = ELF_TRUNC(object->got_addr, _dl_pagesz);
424		object->got_size += object->got_addr - object->got_start;
425		object->got_size = ELF_ROUND(object->got_size, _dl_pagesz);
426	}
427
428	if (!lazy) {
429		_dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
430	} else {
431		rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
432		num = (object->Dyn.info[DT_PLTRELSZ]);
433		for (llist = object->load_list; llist != NULL;
434		    llist = llist->next) {
435			if (!(llist->prot & PROT_WRITE))
436				_dl_mprotect(llist->start, llist->size,
437				    llist->prot|PROT_WRITE);
438		}
439		for (i = 0; i < num/sizeof(Elf_Rel); i++, rel++) {
440			Elf_Addr *where;
441			where = (Elf_Addr *)(rel->r_offset + object->load_offs);
442			*where += object->load_offs;
443		}
444		for (llist = object->load_list; llist != NULL;
445		    llist = llist->next) {
446			if (!(llist->prot & PROT_WRITE))
447				_dl_mprotect(llist->start, llist->size,
448				    llist->prot);
449		}
450
451	}
452	/* PLT is already RO on i386, no point in mprotecting it, just GOT */
453	if (object->got_size != 0)
454		_dl_mprotect((void*)object->got_start, object->got_size,
455		    PROT_READ);
456}
457