xref: /openbsd-src/libexec/ld.so/arm/rtld_machine.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: rtld_machine.c,v 1.23 2016/06/21 15:25:37 deraadt 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 #include <sys/syscall.h>
34 #include <sys/unistd.h>
35 
36 #include <nlist.h>
37 #include <link.h>
38 
39 #include "syscall.h"
40 #include "archdep.h"
41 #include "resolve.h"
42 
43 int64_t pcookie __attribute__((section(".openbsd.randomdata"))) __dso_hidden;
44 
45 void _dl_bind_start(void); /* XXX */
46 Elf_Addr _dl_bind(elf_object_t *object, int reloff);
47 #define _RF_S		0x80000000		/* Resolve symbol */
48 #define _RF_A		0x40000000		/* Use addend */
49 #define _RF_P		0x20000000		/* Location relative */
50 #define _RF_G		0x10000000		/* GOT offset */
51 #define _RF_B		0x08000000		/* Load address relative */
52 #define _RF_U		0x04000000		/* Unaligned */
53 #define _RF_E		0x02000000		/* ERROR */
54 #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
55 #define _RF_RS(s)	((s) & 0xff)		/* right shift */
56 static int reloc_target_flags[] = {
57 	0,						/*  0 NONE */
58 	_RF_S|_RF_P|_RF_A|	_RF_SZ(32) | _RF_RS(0),	/*  1 PC24 */
59 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),	/*  2 ABS32 */
60 	_RF_S|_RF_P|_RF_A|	_RF_SZ(32) | _RF_RS(0),	/*  3 REL32 */
61 	_RF_S|_RF_P|_RF_A|	_RF_E,			/*  4 REL13 */
62 	_RF_S|_RF_A|		_RF_E,			/*  5 ABS16 */
63 	_RF_S|_RF_A|		_RF_E,			/*  6 ABS12 */
64 	_RF_S|_RF_A|		_RF_E,			/*  7 T_ABS5 */
65 	_RF_S|_RF_A|		_RF_E,			/*  8 ABS8 */
66 	_RF_S|_RF_B|_RF_A|	_RF_E,			/*  9 SBREL32 */
67 	_RF_S|_RF_P|_RF_A|	_RF_E,			/* 10 T_PC22 */
68 	_RF_S|_RF_P|_RF_A|	_RF_E,			/* 11 T_PC8 */
69 	_RF_E,						/* 12 Reserved */
70 	_RF_S|_RF_A|		_RF_E,			/* 13 SWI24 */
71 	_RF_S|_RF_A|		_RF_E,			/* 14 T_SWI8 */
72 	_RF_E,						/* 15 OBSL */
73 	_RF_E,						/* 16 OBSL */
74 	_RF_E,						/* 17 UNUSED */
75 	_RF_E,						/* 18 UNUSED */
76 	_RF_E,						/* 19 UNUSED */
77 	_RF_S|			_RF_SZ(32) | _RF_RS(0),	/* 20 COPY */
78 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),	/* 21 GLOB_DAT */
79 	_RF_S|			_RF_SZ(32) | _RF_RS(0),	/* 22 JUMP_SLOT */
80 	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),	/* 23 RELATIVE */
81 	_RF_E,						/* 24 GOTOFF */
82 	_RF_E,						/* 25 GOTPC */
83 	_RF_E,						/* 26 GOT32 */
84 	_RF_E,						/* 27 PLT32 */
85 	_RF_E,						/* 28 UNUSED */
86 	_RF_E,						/* 29 UNUSED */
87 	_RF_E,						/* 30 UNUSED */
88 	_RF_E,						/* 31 UNUSED */
89 	_RF_E,						/* 32 A_PCR 0 */
90 	_RF_E,						/* 33 A_PCR 8 */
91 	_RF_E,						/* 34 A_PCR 16 */
92 	_RF_E,						/* 35 B_PCR 0 */
93 	_RF_E,						/* 36 B_PCR 12 */
94 	_RF_E,						/* 37 B_PCR 20 */
95 	_RF_E,						/* 38 RELAB32 */
96 	_RF_E,						/* 39 ROSGREL32 */
97 	_RF_E,						/* 40 V4BX */
98 	_RF_E,						/* 41 STKCHK */
99 	_RF_E						/* 42 TSTKCHK */
100 };
101 
102 #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
103 #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
104 #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
105 #define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
106 #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
107 #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
108 #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
109 static int reloc_target_bitmask[] = {
110 #define _BM(x)  (x == 32? ~0 : ~(-(1UL << (x))))
111 	_BM(0),		/*  0 NONE */
112 	_BM(24),	/*  1 PC24 */
113 	_BM(32),	/*  2 ABS32 */
114 	_BM(32),	/*  3 REL32 */
115 	_BM(0),		/*  4 REL13 */
116 	_BM(0),		/*  5 ABS16 */
117 	_BM(0),		/*  6 ABS12 */
118 	_BM(0),		/*  7 T_ABS5 */
119 	_BM(0),		/*  8 ABS8 */
120 	_BM(32),	/*  9 SBREL32 */
121 	_BM(0),		/* 10 T_PC22 */
122 	_BM(0),		/* 11 T_PC8 */
123 	_BM(0),		/* 12 Reserved */
124 	_BM(0),		/* 13 SWI24 */
125 	_BM(0),		/* 14 T_SWI8 */
126 	_BM(0),		/* 15 OBSL */
127 	_BM(0),		/* 16 OBSL */
128 	_BM(0),		/* 17 UNUSED */
129 	_BM(0),		/* 18 UNUSED */
130 	_BM(0),		/* 19 UNUSED */
131 	_BM(32),	/* 20 COPY */
132 	_BM(32),	/* 21 GLOB_DAT */
133 	_BM(32),	/* 22 JUMP_SLOT */
134 	_BM(32),	/* 23 RELATIVE */
135 	_BM(0),		/* 24 GOTOFF */
136 	_BM(0),		/* 25 GOTPC */
137 	_BM(0),		/* 26 GOT32 */
138 	_BM(0),		/* 27 PLT32 */
139 	_BM(0),		/* 28 UNUSED */
140 	_BM(0),		/* 29 UNUSED */
141 	_BM(0),		/* 30 UNUSED */
142 	_BM(0),		/* 31 UNUSED */
143 	_BM(0),		/* 32 A_PCR 0 */
144 	_BM(0),		/* 33 A_PCR 8 */
145 	_BM(0),		/* 34 A_PCR 16 */
146 	_BM(0),		/* 35 B_PCR 0 */
147 	_BM(0),		/* 36 B_PCR 12 */
148 	_BM(0),		/* 37 B_PCR 20 */
149 	_BM(0),		/* 38 RELAB32 */
150 	_BM(0),		/* 39 ROSGREL32 */
151 	_BM(0),		/* 40 V4BX */
152 	_BM(0),		/* 41 STKCHK */
153 	_BM(0)		/* 42 TSTKCHK */
154 #undef _BM
155 };
156 #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
157 
158 #define R_TYPE(x) R_ARM_ ## x
159 
160 void _dl_reloc_plt(Elf_Word *where, Elf_Addr value, Elf_Rel *rel);
161 
162 int
163 _dl_md_reloc(elf_object_t *object, int rel, int relsz)
164 {
165 	long	i;
166 	long	numrel;
167 	long	relrel;
168 	int	fails = 0;
169 	Elf_Addr loff;
170 	Elf_Addr prev_value = 0;
171 	const Elf_Sym *prev_sym = NULL;
172 	Elf_Rel *rels;
173 	struct load_list *llist;
174 
175 	loff = object->obj_base;
176 	numrel = object->Dyn.info[relsz] / sizeof(Elf_Rel);
177 	relrel = rel == DT_REL ? object->relcount : 0;
178 	rels = (Elf_Rel *)(object->Dyn.info[rel]);
179 
180 	if (rels == NULL)
181 		return(0);
182 
183 	if (relrel > numrel) {
184 		_dl_printf("relcount > numrel: %ld > %ld\n", relrel, numrel);
185 		_dl_exit(20);
186 	}
187 
188 	/*
189 	 * unprotect some segments if we need it.
190 	 */
191 	if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) {
192 		for (llist = object->load_list;
193 		    llist != NULL;
194 		    llist = llist->next) {
195 			if (!(llist->prot & PROT_WRITE))
196 				_dl_mprotect(llist->start, llist->size,
197 				    PROT_READ | PROT_WRITE);
198 		}
199 	}
200 
201 	/* tight loop for leading RELATIVE relocs */
202 	for (i = 0; i < relrel; i++, rels++) {
203 		Elf_Addr *where;
204 
205 #ifdef DEBUG
206 		if (ELF_R_TYPE(rels->r_info) != R_TYPE(RELATIVE)) {
207 			_dl_printf("RELCOUNT wrong\n");
208 			_dl_exit(20);
209 		}
210 #endif
211 		where = (Elf_Addr *)(rels->r_offset + loff);
212 		*where += loff;
213 	}
214 	for (; i < numrel; i++, rels++) {
215 		Elf_Addr *where, value, ooff, mask;
216 		Elf_Word type;
217 		const Elf_Sym *sym, *this;
218 		const char *symn;
219 
220 		type = ELF_R_TYPE(rels->r_info);
221 
222 		if (reloc_target_flags[type] & _RF_E) {
223 			_dl_printf(" bad relocation %d %d\n", i, type);
224 			_dl_exit(1);
225 		}
226 		if (type == R_TYPE(NONE))
227 			continue;
228 
229 		if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL)
230 			continue;
231 
232 		where = (Elf_Addr *)(rels->r_offset + loff);
233 
234 		if (RELOC_USE_ADDEND(type))
235 #ifdef LDSO_ARCH_IS_RELA_
236 			value = rels->r_addend;
237 #else
238 			value = *where & RELOC_VALUE_BITMASK(type);
239 #endif
240 		else
241 			value = 0;
242 
243 		sym = NULL;
244 		symn = NULL;
245 		if (RELOC_RESOLVE_SYMBOL(type)) {
246 			sym = object->dyn.symtab;
247 			sym += ELF_R_SYM(rels->r_info);
248 			symn = object->dyn.strtab + sym->st_name;
249 
250 			if (sym->st_shndx != SHN_UNDEF &&
251 			    ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
252 				value += loff;
253 			} else if (sym == prev_sym) {
254 				value += prev_value;
255 			} else {
256 				this = NULL;
257 				ooff = _dl_find_symbol_bysym(object,
258 				    ELF_R_SYM(rels->r_info), &this,
259 				    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
260 				    ((type == R_TYPE(JUMP_SLOT)) ?
261 					SYM_PLT : SYM_NOTPLT),
262 				    sym, NULL);
263 				if (this == NULL) {
264 resolve_failed:
265 					if (ELF_ST_BIND(sym->st_info) !=
266 					    STB_WEAK)
267 						fails++;
268 					continue;
269 				}
270 				prev_sym = sym;
271 				prev_value = (Elf_Addr)(ooff + this->st_value);
272 				value += prev_value;
273 			}
274 		}
275 
276 		if (type == R_TYPE(JUMP_SLOT)) {
277 			/*
278 			_dl_reloc_plt((Elf_Word *)where, value, rels);
279 			*/
280 			*where = value;
281 			continue;
282 		}
283 
284 		if (type == R_TYPE(COPY)) {
285 			void *dstaddr = where;
286 			const void *srcaddr;
287 			const Elf_Sym *dstsym = sym, *srcsym = NULL;
288 			Elf_Addr soff;
289 
290 			soff = _dl_find_symbol(symn, &srcsym,
291 			    SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT,
292 			    dstsym, object, NULL);
293 			if (srcsym == NULL)
294 				goto resolve_failed;
295 
296 			srcaddr = (void *)(soff + srcsym->st_value);
297 			_dl_bcopy(srcaddr, dstaddr, dstsym->st_size);
298 			continue;
299 		}
300 
301 		if (RELOC_PC_RELATIVE(type))
302 			value -= (Elf_Addr)where;
303 		if (RELOC_BASE_RELATIVE(type))
304 			value += loff;
305 
306 		mask = RELOC_VALUE_BITMASK(type);
307 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
308 		value &= mask;
309 
310 		if (RELOC_UNALIGNED(type)) {
311 			/* Handle unaligned relocations. */
312 			Elf_Addr tmp = 0;
313 			char *ptr = (char *)where;
314 			int i, size = RELOC_TARGET_SIZE(type)/8;
315 
316 			/* Read it in one byte at a time. */
317 			for (i=0; i<size; i++)
318 				tmp = (tmp << 8) | ptr[i];
319 
320 			tmp &= ~mask;
321 			tmp |= value;
322 
323 			/* Write it back out. */
324 			for (i=0; i<size; i++)
325 				ptr[i] = ((tmp >> (8*i)) & 0xff);
326 		} else {
327 			*where &= ~mask;
328 			*where |= value;
329 		}
330 	}
331 
332 	/* reprotect the unprotected segments */
333 	if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) {
334 		for (llist = object->load_list;
335 		    llist != NULL;
336 		    llist = llist->next) {
337 			if (!(llist->prot & PROT_WRITE))
338 				_dl_mprotect(llist->start, llist->size,
339 				    llist->prot);
340 		}
341 	}
342 
343 	return (fails);
344 }
345 
346 /*
347  *	Relocate the Global Offset Table (GOT).
348  *	This is done by calling _dl_md_reloc on DT_JMPREL for DL_BIND_NOW,
349  *	otherwise the lazy binding plt initialization is performed.
350  */
351 int
352 _dl_md_reloc_got(elf_object_t *object, int lazy)
353 {
354 	int	fails = 0;
355 	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
356 	int i, num;
357 	Elf_Rel *rel;
358 
359 	if (object->Dyn.info[DT_PLTREL] != DT_REL)
360 		return (0);
361 
362 	if (object->traced)
363 		lazy = 1;
364 
365 	if (!lazy) {
366 		fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
367 	} else {
368 		rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
369 		num = (object->Dyn.info[DT_PLTRELSZ]);
370 
371 		for (i = 0; i < num/sizeof(Elf_Rel); i++, rel++) {
372 			Elf_Addr *where;
373 			where = (Elf_Addr *)(rel->r_offset + object->obj_base);
374 			*where += object->obj_base;
375 		}
376 
377 		pltgot[1] = (Elf_Addr)object;
378 		pltgot[2] = (Elf_Addr)_dl_bind_start;
379 	}
380 
381 	/* mprotect the GOT */
382 	_dl_protect_segment(object, 0, "__got_start", "__got_end", PROT_READ);
383 
384 	return (fails);
385 }
386 
387 Elf_Addr
388 _dl_bind(elf_object_t *object, int relidx)
389 {
390 	Elf_Rel *rel;
391 	const Elf_Sym *sym, *this;
392 	const char *symn;
393 	const elf_object_t *sobj;
394 	Elf_Addr ooff;
395 	int64_t cookie = pcookie;
396 	struct {
397 		struct __kbind param;
398 		Elf_Word newval;
399 	} buf;
400 
401 	rel = ((Elf_Rel *)object->Dyn.info[DT_JMPREL]) + (relidx);
402 
403 	sym = object->dyn.symtab;
404 	sym += ELF_R_SYM(rel->r_info);
405 	symn = object->dyn.strtab + sym->st_name;
406 
407 	this = NULL;
408 	ooff = _dl_find_symbol(symn,  &this,
409 	    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, sym, object, &sobj);
410 	if (this == NULL) {
411 		_dl_printf("lazy binding failed!\n");
412 		*(volatile int *)0 = 0;		/* XXX */
413 	}
414 
415 	buf.newval = ooff + this->st_value;
416 
417 	if (__predict_false(sobj->traced) && _dl_trace_plt(sobj, symn))
418 		return (buf.newval);
419 
420 	buf.param.kb_addr = (Elf_Addr *)(object->obj_base + rel->r_offset);
421 	buf.param.kb_size = sizeof(Elf_Word);
422 
423 	/* directly code the syscall, so that it's actually inline here */
424 	{
425 		register long syscall_num __asm("r12") = SYS_kbind;
426 		register void *arg1 __asm("r0") = &buf;
427 		register long  arg2 __asm("r1") = sizeof(buf);
428 		register long  arg3 __asm("r2") = 0xffffffff &  cookie;
429 		register long  arg4 __asm("r3") = 0xffffffff & (cookie >> 32);
430 
431 		__asm volatile("swi 0" : "+r" (arg1), "+r" (arg2)
432 		    : "r" (syscall_num), "r" (arg3), "r" (arg4)
433 		    : "cc", "memory");
434 	}
435 
436 	return (buf.newval);
437 }
438