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