xref: /openbsd-src/libexec/ld.so/arm/rtld_machine.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: rtld_machine.c,v 1.19 2013/06/13 04:13:47 brad Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Dale Rahn
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #define _DYN_LOADER
30 
31 #include <sys/types.h>
32 #include <sys/mman.h>
33 
34 #include <nlist.h>
35 #include <link.h>
36 #include <signal.h>
37 
38 #include "syscall.h"
39 #include "archdep.h"
40 #include "resolve.h"
41 
42 void _dl_bind_start(void); /* XXX */
43 Elf_Addr _dl_bind(elf_object_t *object, int reloff);
44 #define _RF_S		0x80000000		/* Resolve symbol */
45 #define _RF_A		0x40000000		/* Use addend */
46 #define _RF_P		0x20000000		/* Location relative */
47 #define _RF_G		0x10000000		/* GOT offset */
48 #define _RF_B		0x08000000		/* Load address relative */
49 #define _RF_U		0x04000000		/* Unaligned */
50 #define _RF_E		0x02000000		/* ERROR */
51 #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
52 #define _RF_RS(s)	((s) & 0xff)		/* right shift */
53 static int reloc_target_flags[] = {
54 	0,						/*  0 NONE */
55 	_RF_S|_RF_P|_RF_A|	_RF_SZ(32) | _RF_RS(0),	/*  1 PC24 */
56 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),	/*  2 ABS32 */
57 	_RF_S|_RF_P|_RF_A|	_RF_SZ(32) | _RF_RS(0),	/*  3 REL32 */
58 	_RF_S|_RF_P|_RF_A|	_RF_E,			/*  4 REL13 */
59 	_RF_S|_RF_A|		_RF_E,			/*  5 ABS16 */
60 	_RF_S|_RF_A|		_RF_E,			/*  6 ABS12 */
61 	_RF_S|_RF_A|		_RF_E,			/*  7 T_ABS5 */
62 	_RF_S|_RF_A|		_RF_E,			/*  8 ABS8 */
63 	_RF_S|_RF_B|_RF_A|	_RF_E,			/*  9 SBREL32 */
64 	_RF_S|_RF_P|_RF_A|	_RF_E,			/* 10 T_PC22 */
65 	_RF_S|_RF_P|_RF_A|	_RF_E,			/* 11 T_PC8 */
66 	_RF_E,						/* 12 Reserved */
67 	_RF_S|_RF_A|		_RF_E,			/* 13 SWI24 */
68 	_RF_S|_RF_A|		_RF_E,			/* 14 T_SWI8 */
69 	_RF_E,						/* 15 OBSL */
70 	_RF_E,						/* 16 OBSL */
71 	_RF_E,						/* 17 UNUSED */
72 	_RF_E,						/* 18 UNUSED */
73 	_RF_E,						/* 19 UNUSED */
74 	_RF_S|			_RF_SZ(32) | _RF_RS(0),	/* 20 COPY */
75 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),	/* 21 GLOB_DAT */
76 	_RF_S|			_RF_SZ(32) | _RF_RS(0),	/* 22 JUMP_SLOT */
77 	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),	/* 23 RELATIVE */
78 	_RF_E,						/* 24 GOTOFF */
79 	_RF_E,						/* 25 GOTPC */
80 	_RF_E,						/* 26 GOT32 */
81 	_RF_E,						/* 27 PLT32 */
82 	_RF_E,						/* 28 UNUSED */
83 	_RF_E,						/* 29 UNUSED */
84 	_RF_E,						/* 30 UNUSED */
85 	_RF_E,						/* 31 UNUSED */
86 	_RF_E,						/* 32 A_PCR 0 */
87 	_RF_E,						/* 33 A_PCR 8 */
88 	_RF_E,						/* 34 A_PCR 16 */
89 	_RF_E,						/* 35 B_PCR 0 */
90 	_RF_E,						/* 36 B_PCR 12 */
91 	_RF_E,						/* 37 B_PCR 20 */
92 	_RF_E,						/* 38 RELAB32 */
93 	_RF_E,						/* 39 ROSGREL32 */
94 	_RF_E,						/* 40 V4BX */
95 	_RF_E,						/* 41 STKCHK */
96 	_RF_E						/* 42 TSTKCHK */
97 };
98 
99 #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
100 #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
101 #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
102 #define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
103 #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
104 #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
105 #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
106 static int reloc_target_bitmask[] = {
107 #define _BM(x)  (x == 32? ~0 : ~(-(1UL << (x))))
108 	_BM(0),		/*  0 NONE */
109 	_BM(24),	/*  1 PC24 */
110 	_BM(32),	/*  2 ABS32 */
111 	_BM(32),	/*  3 REL32 */
112 	_BM(0),		/*  4 REL13 */
113 	_BM(0),		/*  5 ABS16 */
114 	_BM(0),		/*  6 ABS12 */
115 	_BM(0),		/*  7 T_ABS5 */
116 	_BM(0),		/*  8 ABS8 */
117 	_BM(32),	/*  9 SBREL32 */
118 	_BM(0),		/* 10 T_PC22 */
119 	_BM(0),		/* 11 T_PC8 */
120 	_BM(0),		/* 12 Reserved */
121 	_BM(0),		/* 13 SWI24 */
122 	_BM(0),		/* 14 T_SWI8 */
123 	_BM(0),		/* 15 OBSL */
124 	_BM(0),		/* 16 OBSL */
125 	_BM(0),		/* 17 UNUSED */
126 	_BM(0),		/* 18 UNUSED */
127 	_BM(0),		/* 19 UNUSED */
128 	_BM(32),	/* 20 COPY */
129 	_BM(32),	/* 21 GLOB_DAT */
130 	_BM(32),	/* 22 JUMP_SLOT */
131 	_BM(32),	/* 23 RELATIVE */
132 	_BM(0),		/* 24 GOTOFF */
133 	_BM(0),		/* 25 GOTPC */
134 	_BM(0),		/* 26 GOT32 */
135 	_BM(0),		/* 27 PLT32 */
136 	_BM(0),		/* 28 UNUSED */
137 	_BM(0),		/* 29 UNUSED */
138 	_BM(0),		/* 30 UNUSED */
139 	_BM(0),		/* 31 UNUSED */
140 	_BM(0),		/* 32 A_PCR 0 */
141 	_BM(0),		/* 33 A_PCR 8 */
142 	_BM(0),		/* 34 A_PCR 16 */
143 	_BM(0),		/* 35 B_PCR 0 */
144 	_BM(0),		/* 36 B_PCR 12 */
145 	_BM(0),		/* 37 B_PCR 20 */
146 	_BM(0),		/* 38 RELAB32 */
147 	_BM(0),		/* 39 ROSGREL32 */
148 	_BM(0),		/* 40 V4BX */
149 	_BM(0),		/* 41 STKCHK */
150 	_BM(0)		/* 42 TSTKCHK */
151 #undef _BM
152 };
153 #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
154 
155 #define R_TYPE(x) R_ARM_ ## x
156 
157 void _dl_reloc_plt(Elf_Word *where, Elf_Addr value, Elf_Rel *rel);
158 
159 int
160 _dl_md_reloc(elf_object_t *object, int rel, int relsz)
161 {
162 	long	i;
163 	long	numrel;
164 	long	relrel;
165 	int	fails = 0;
166 	Elf_Addr loff;
167 	Elf_Addr prev_value = 0;
168 	const Elf_Sym *prev_sym = NULL;
169 	Elf_Rel *rels;
170 	struct load_list *llist;
171 
172 	loff = object->obj_base;
173 	numrel = object->Dyn.info[relsz] / sizeof(Elf_Rel);
174 	relrel = rel == DT_REL ? object->relcount : 0;
175 	rels = (Elf_Rel *)(object->Dyn.info[rel]);
176 
177 	if (rels == NULL)
178 		return(0);
179 
180 	if (relrel > numrel) {
181 		_dl_printf("relcount > numrel: %ld > %ld\n", relrel, numrel);
182 		_dl_exit(20);
183 	}
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;
190 		    llist != NULL;
191 		    llist = llist->next) {
192 			if (!(llist->prot & PROT_WRITE))
193 				_dl_mprotect(llist->start, llist->size,
194 				    llist->prot|PROT_WRITE);
195 		}
196 	}
197 
198 	/* tight loop for leading RELATIVE relocs */
199 	for (i = 0; i < relrel; i++, rels++) {
200 		Elf_Addr *where;
201 
202 #ifdef DEBUG
203 		if (ELF_R_TYPE(rels->r_info) != R_TYPE(RELATIVE)) {
204 			_dl_printf("RELCOUNT wrong\n");
205 			_dl_exit(20);
206 		}
207 #endif
208 		where = (Elf_Addr *)(rels->r_offset + loff);
209 		*where += loff;
210 	}
211 	for (; i < numrel; i++, rels++) {
212 		Elf_Addr *where, value, ooff, mask;
213 		Elf_Word type;
214 		const Elf_Sym *sym, *this;
215 		const char *symn;
216 
217 		type = ELF_R_TYPE(rels->r_info);
218 
219 		if (reloc_target_flags[type] & _RF_E) {
220 			_dl_printf(" bad relocation %d %d\n", i, type);
221 			_dl_exit(1);
222 		}
223 		if (type == R_TYPE(NONE))
224 			continue;
225 
226 		if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL)
227 			continue;
228 
229 		where = (Elf_Addr *)(rels->r_offset + loff);
230 
231 		if (RELOC_USE_ADDEND(type))
232 #ifdef LDSO_ARCH_IS_RELA_
233 			value = rels->r_addend;
234 #else
235 			value = *where & RELOC_VALUE_BITMASK(type);
236 #endif
237 		else
238 			value = 0;
239 
240 		sym = NULL;
241 		symn = NULL;
242 		if (RELOC_RESOLVE_SYMBOL(type)) {
243 			sym = object->dyn.symtab;
244 			sym += ELF_R_SYM(rels->r_info);
245 			symn = object->dyn.strtab + sym->st_name;
246 
247 			if (sym->st_shndx != SHN_UNDEF &&
248 			    ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
249 				value += loff;
250 			} else if (sym == prev_sym) {
251 				value += prev_value;
252 			} else {
253 				this = NULL;
254 				ooff = _dl_find_symbol_bysym(object,
255 				    ELF_R_SYM(rels->r_info), &this,
256 				    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
257 				    ((type == R_TYPE(JUMP_SLOT)) ?
258 					SYM_PLT : SYM_NOTPLT),
259 				    sym, NULL);
260 				if (this == NULL) {
261 resolve_failed:
262 					if (ELF_ST_BIND(sym->st_info) !=
263 					    STB_WEAK)
264 						fails++;
265 					continue;
266 				}
267 				prev_sym = sym;
268 				prev_value = (Elf_Addr)(ooff + this->st_value);
269 				value += prev_value;
270 			}
271 		}
272 
273 		if (type == R_TYPE(JUMP_SLOT)) {
274 			/*
275 			_dl_reloc_plt((Elf_Word *)where, value, rels);
276 			*/
277 			*where = value;
278 			continue;
279 		}
280 
281 		if (type == R_TYPE(COPY)) {
282 			void *dstaddr = where;
283 			const void *srcaddr;
284 			const Elf_Sym *dstsym = sym, *srcsym = NULL;
285 			Elf_Addr soff;
286 
287 			soff = _dl_find_symbol(symn, &srcsym,
288 			    SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT,
289 			    dstsym, object, NULL);
290 			if (srcsym == NULL)
291 				goto resolve_failed;
292 
293 			srcaddr = (void *)(soff + srcsym->st_value);
294 			_dl_bcopy(srcaddr, dstaddr, dstsym->st_size);
295 			continue;
296 		}
297 
298 		if (RELOC_PC_RELATIVE(type))
299 			value -= (Elf_Addr)where;
300 		if (RELOC_BASE_RELATIVE(type))
301 			value += loff;
302 
303 		mask = RELOC_VALUE_BITMASK(type);
304 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
305 		value &= mask;
306 
307 		if (RELOC_UNALIGNED(type)) {
308 			/* Handle unaligned relocations. */
309 			Elf_Addr tmp = 0;
310 			char *ptr = (char *)where;
311 			int i, size = RELOC_TARGET_SIZE(type)/8;
312 
313 			/* Read it in one byte at a time. */
314 			for (i=0; i<size; i++)
315 				tmp = (tmp << 8) | ptr[i];
316 
317 			tmp &= ~mask;
318 			tmp |= value;
319 
320 			/* Write it back out. */
321 			for (i=0; i<size; i++)
322 				ptr[i] = ((tmp >> (8*i)) & 0xff);
323 		} else {
324 			*where &= ~mask;
325 			*where |= value;
326 		}
327 	}
328 
329 	/* reprotect the unprotected segments */
330 	if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) {
331 		for (llist = object->load_list;
332 		    llist != NULL;
333 		    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 /*
344  *	Relocate the Global Offset Table (GOT).
345  *	This is done by calling _dl_md_reloc on DT_JUMPREL for DL_BIND_NOW,
346  *	otherwise the lazy binding plt initialization is performed.
347  */
348 int
349 _dl_md_reloc_got(elf_object_t *object, int lazy)
350 {
351 	int	fails = 0;
352 	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
353 	Elf_Addr ooff;
354 	const Elf_Sym *this;
355 	int i, num;
356 	Elf_Rel *rel;
357 
358 	if (object->Dyn.info[DT_PLTREL] != DT_REL)
359 		return (0);
360 
361 	object->got_addr = 0;
362 	object->got_size = 0;
363 	this = NULL;
364 	ooff = _dl_find_symbol("__got_start", &this,
365 	    SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, object, NULL);
366 	if (this != NULL)
367 		object->got_addr = ooff + this->st_value;
368 
369 	this = NULL;
370 	ooff = _dl_find_symbol("__got_end", &this,
371 	    SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, object, NULL);
372 	if (this != NULL)
373 		object->got_size = ooff + this->st_value  - object->got_addr;
374 
375 	object->plt_size = 0;	/* Text PLT on ARM */
376 
377 	if (object->got_addr == 0)
378 		object->got_start = 0;
379 	else {
380 		object->got_start = ELF_TRUNC(object->got_addr, _dl_pagesz);
381 		object->got_size += object->got_addr - object->got_start;
382 		object->got_size = ELF_ROUND(object->got_size, _dl_pagesz);
383 	}
384 	object->plt_start = 0;
385 
386 	if (object->traced)
387 		lazy = 1;
388 
389 	if (!lazy) {
390 		fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
391 	} else {
392 		rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
393 		num = (object->Dyn.info[DT_PLTRELSZ]);
394 
395 		for (i = 0; i < num/sizeof(Elf_Rel); i++, rel++) {
396 			Elf_Addr *where;
397 			where = (Elf_Addr *)(rel->r_offset + object->obj_base);
398 			*where += object->obj_base;
399 		}
400 
401 		pltgot[1] = (Elf_Addr)object;
402 		pltgot[2] = (Elf_Addr)_dl_bind_start;
403 	}
404 	if (object->got_size != 0)
405 		_dl_mprotect((void*)object->got_start, object->got_size,
406 		    PROT_READ);
407 	if (object->plt_size != 0)
408 		_dl_mprotect((void*)object->plt_start, object->plt_size,
409 		    PROT_READ|PROT_EXEC);
410 
411 	return (fails);
412 }
413 
414 Elf_Addr
415 _dl_bind(elf_object_t *object, int relidx)
416 {
417 	Elf_Rel *rel;
418 	Elf_Word *addr;
419 	const Elf_Sym *sym, *this;
420 	const char *symn;
421 	const elf_object_t *sobj;
422 	Elf_Addr ooff, newval;
423 	sigset_t savedmask;
424 
425 	rel = ((Elf_Rel *)object->Dyn.info[DT_JMPREL]) + (relidx);
426 
427 	sym = object->dyn.symtab;
428 	sym += ELF_R_SYM(rel->r_info);
429 	symn = object->dyn.strtab + sym->st_name;
430 
431 	this = NULL;
432 	ooff = _dl_find_symbol(symn,  &this,
433 	    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, sym, object, &sobj);
434 	if (this == NULL) {
435 		_dl_printf("lazy binding failed!\n");
436 		*(volatile int *)0 = 0;		/* XXX */
437 	}
438 
439 	addr = (Elf_Addr *)(object->obj_base + rel->r_offset);
440 	newval = ooff + this->st_value;
441 
442 	if (sobj->traced && _dl_trace_plt(sobj, symn))
443 		return newval;
444 
445 	/* if GOT is protected, allow the write */
446 	if (object->got_size != 0) {
447 		_dl_thread_bind_lock(0, &savedmask);
448 		_dl_mprotect((void*)object->got_start, object->got_size,
449 		    PROT_READ|PROT_WRITE);
450 	}
451 
452 	if (*addr != newval)
453 		*addr = newval;
454 
455 	/* put the GOT back to RO */
456 	if (object->got_size != 0) {
457 		_dl_mprotect((void*)object->got_start, object->got_size,
458 		    PROT_READ);
459 		_dl_thread_bind_lock(1, &savedmask);
460 	}
461 	return newval;
462 }
463