xref: /openbsd/libexec/ld.so/i386/rtld_machine.c (revision db3296cf)
1 /*	$OpenBSD: rtld_machine.c,v 1.10 2003/07/28 03:11:00 drahn 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 void
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 */
113 static 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 
148 static 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 
178 void _dl_reloc_plt(Elf_Addr *where, Elf_Addr value);
179 
180 int
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(symn, _dl_objects,
241 				    &this, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
242 				    ((type == R_TYPE(JUMP_SLOT))?
243 					SYM_PLT:SYM_NOTPLT),
244 				    sym->st_size, object->load_name);
245 				if (this == NULL) {
246 resolve_failed:
247 					_dl_printf("%s: %s: can't resolve "
248 					    "reference '%s'\n",
249 					    _dl_progname, object->load_name,
250 					    symn);
251 					fails++;
252 					continue;
253 				}
254 				value += (Elf_Addr)(ooff + this->st_value);
255 			}
256 		}
257 
258 		if (type == R_TYPE(JUMP_SLOT)) {
259 			_dl_reloc_plt((Elf_Word *)where, value);
260 			continue;
261 		}
262 
263 		if (type == R_TYPE(COPY)) {
264 			void *dstaddr = where;
265 			const void *srcaddr;
266 			const Elf_Sym *dstsym = sym, *srcsym = NULL;
267 			size_t size = dstsym->st_size;
268 			Elf_Addr soff;
269 
270 			soff = _dl_find_symbol(symn, object->next, &srcsym,
271 			    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
272 			    ((type == R_TYPE(JUMP_SLOT)) ? SYM_PLT:SYM_NOTPLT),
273 			    size, object->load_name);
274 			if (srcsym == NULL)
275 				goto resolve_failed;
276 
277 			srcaddr = (void *)(soff + srcsym->st_value);
278 			_dl_bcopy(srcaddr, dstaddr, size);
279 			continue;
280 		}
281 
282 		if (RELOC_PC_RELATIVE(type))
283 			value -= (Elf_Addr)where;
284 		if (RELOC_BASE_RELATIVE(type))
285 			value += loff;
286 
287 		mask = RELOC_VALUE_BITMASK(type);
288 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
289 		value &= mask;
290 
291 		if (RELOC_UNALIGNED(type)) {
292 			/* Handle unaligned relocations. */
293 			Elf_Addr tmp = 0;
294 			char *ptr = (char *)where;
295 			int i, size = RELOC_TARGET_SIZE(type)/8;
296 
297 			/* Read it in one byte at a time. */
298 			for (i=0; i<size; i++)
299 				tmp = (tmp << 8) | ptr[i];
300 
301 			tmp &= ~mask;
302 			tmp |= value;
303 
304 			/* Write it back out. */
305 			for (i=0; i<size; i++)
306 				ptr[i] = ((tmp >> (8*i)) & 0xff);
307 		} else if (RELOC_TARGET_SIZE(type) > 32) {
308 			*where &= ~mask;
309 			*where |= value;
310 		} else {
311 			Elf32_Addr *where32 = (Elf32_Addr *)where;
312 
313 			*where32 &= ~mask;
314 			*where32 |= value;
315 		}
316 	}
317 
318 	/* reprotect the unprotected segments */
319 	if ((rel == DT_REL || rel == DT_RELA)) {
320 		for (llist = object->load_list; llist != NULL; llist = llist->next) {
321 			if (!(llist->prot & PROT_WRITE))
322 				_dl_mprotect(llist->start, llist->size,
323 				    llist->prot);
324 		}
325 	}
326 
327 	return (fails);
328 }
329 
330 #if 0
331 struct jmpslot {
332 	u_short opcode;
333 	u_short addr[2];
334 	u_short reloc_index;
335 #define JMPSLOT_RELOC_MASK              0xffff
336 };
337 #define JUMP    0xe990          /* NOP + JMP opcode */
338 #endif
339 
340 void
341 _dl_reloc_plt(Elf_Addr *where, Elf_Addr value)
342 {
343 	*where = value;
344 }
345 
346 /*
347  * Resolve a symbol at run-time.
348  */
349 Elf_Addr
350 _dl_bind(elf_object_t *object, int index)
351 {
352 	Elf_Rel *rel;
353 	Elf_Word *addr;
354 	const Elf_Sym *sym, *this;
355 	const char *symn;
356 	Elf_Addr ooff;
357 	sigset_t omask, nmask;
358 
359 	rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
360 
361 	rel += index/sizeof(Elf_Rel);
362 
363 	sym = object->dyn.symtab;
364 	sym += ELF_R_SYM(rel->r_info);
365 	symn = object->dyn.strtab + sym->st_name;
366 
367 	addr = (Elf_Word *)(object->load_offs + rel->r_offset);
368 	this = NULL;
369 	ooff = _dl_find_symbol(symn, _dl_objects, &this,
370 	    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, 0, object->load_name);
371 	if (this == NULL) {
372 		_dl_printf("lazy binding failed!\n");
373 		*((int *)0) = 0;        /* XXX */
374 	}
375 
376 	/* if GOT is protected, allow the write */
377 	if (object->got_size != 0) {
378 		sigfillset(&nmask);
379 		_dl_sigprocmask(SIG_BLOCK, &nmask, &omask);
380 		_dl_mprotect((void*)object->got_start, object->got_size,
381 		    PROT_READ|PROT_WRITE);
382 	}
383 
384 	_dl_reloc_plt(addr, ooff + this->st_value);
385 
386 	/* put the GOT back to RO */
387 	if (object->got_size != 0) {
388 		_dl_mprotect((void*)object->got_start, object->got_size,
389 		    PROT_READ);
390 		_dl_sigprocmask(SIG_SETMASK, &omask, NULL);
391 	}
392 
393 	return((Elf_Addr)ooff + this->st_value);
394 }
395 
396 void
397 _dl_md_reloc_got(elf_object_t *object, int lazy)
398 {
399 	extern void _dl_bind_start(void);       /* XXX */
400 	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
401 	int i, num;
402 	Elf_Rel *rel;
403 	struct load_list *llist;
404 	Elf_Addr ooff;
405 	const Elf_Sym *this;
406 
407 	if (pltgot == NULL)
408 		return; /* it is possible to have no PLT/GOT relocations */
409 
410 	pltgot[1] = (Elf_Addr)object;
411 	pltgot[2] = (Elf_Addr)&_dl_bind_start;
412 
413 	if (object->Dyn.info[DT_PLTREL] != DT_REL)
414 		return;
415 
416 	object->got_addr = NULL;
417 	object->got_size = 0;
418 	this = NULL;
419 	ooff = _dl_find_symbol("__got_start", object, &this,
420 	    SYM_SEARCH_SELF|SYM_NOWARNNOTFOUND|SYM_PLT, 0, NULL);
421 	if (this != NULL)
422 		object->got_addr = ooff + this->st_value;
423 
424 	this = NULL;
425 	ooff = _dl_find_symbol("__got_end", object, &this,
426 	    SYM_SEARCH_SELF|SYM_NOWARNNOTFOUND|SYM_PLT, 0, NULL);
427 	if (this != NULL)
428 		object->got_size = ooff + this->st_value  - object->got_addr;
429 
430 	if (object->got_addr == NULL)
431 		object->got_start = NULL;
432 	else {
433 		object->got_start = ELF_TRUNC(object->got_addr, _dl_pagesz);
434 		object->got_size += object->got_addr - object->got_start;
435 		object->got_size = ELF_ROUND(object->got_size, _dl_pagesz);
436 	}
437 
438 	if (!lazy) {
439 		_dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
440 		return;
441 	}
442 
443 	rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
444 	num = (object->Dyn.info[DT_PLTRELSZ]);
445 	for (llist = object->load_list; llist != NULL; 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; llist = llist->next) {
456 		if (!(llist->prot & PROT_WRITE))
457 			_dl_mprotect(llist->start, llist->size,
458 			    llist->prot);
459 	}
460 
461 	/* PLT is already RO on i386, no point in mprotecting it, just GOT */
462 	if (object->got_size != 0)
463 		_dl_mprotect((void*)object->got_start, object->got_size,
464 		    PROT_READ);
465 }
466