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