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