xref: /netbsd-src/libexec/ld.elf_so/arch/powerpc/ppc_reloc.c (revision 7863ba460b0a05b553c754e5dbc29247dddec322)
1 /*	$NetBSD: ppc_reloc.c,v 1.57 2018/04/03 21:10:27 joerg Exp $	*/
2 
3 /*-
4  * Copyright (C) 1998	Tsubai Masanari
5  * Portions copyright 2002 Charles M. Hannum <root@ihack.net>
6  * All rights reserved.
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  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: ppc_reloc.c,v 1.57 2018/04/03 21:10:27 joerg Exp $");
34 #endif /* not lint */
35 
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <machine/cpu.h>
42 
43 #include "debug.h"
44 #include "rtld.h"
45 
46 void _rtld_powerpc_pltcall(Elf_Word);
47 void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
48 
49 #define __u64(x)	((uint64_t)(x))
50 #define __u32(x)	((uint32_t)(x))
51 #define __ha48		__u64(0xffffffff8000)
52 #define __ha32		__u64(0xffff8000)
53 #define __ha16		__u32(0x8000)
54 #define __ha(x,n) ((((x) >> (n)) + (((x) & __ha##n) == __ha##n)) & 0xffff)
55 #define __hi(x,n) (((x) >> (n)) & 0xffff)
56 #ifdef __LP64
57 #define highesta(x)	__ha(__u64(x), 48)
58 #define highest(x)	__hi(__u64(x), 48)
59 #define higher(x)	__ha(__u64(x), 32)
60 #define higher(x)	__hi(__u64(x), 32)
61 #endif
62 #define ha(x)		__ha(__u32(x), 16)
63 #define hi(x)		__hi(__u32(x), 16)
64 #define lo(x)		(__u32(x) & 0xffff)
65 
66 #ifdef _LP64
67 /* function descriptor for _rtld_bind_start */
68 extern const uint64_t _rtld_bind_start[3];
69 #else
70 void _rtld_bind_bssplt_start(void);
71 void _rtld_bind_secureplt_start(void);
72 #endif
73 Elf_Addr _rtld_bind(const Obj_Entry *, Elf_Word);
74 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
75 static int _rtld_relocate_plt_object(const Obj_Entry *,
76     const Elf_Rela *, int, Elf_Addr *);
77 
78 /*
79  * The PPC32 PLT format consists of three sections:
80  * (1) The "pltcall" and "pltresolve" glue code.  This is always 18 words.
81  * (2) The code part of the PLT entries.  There are 2 words per entry for
82  *     up to 8192 entries, then 4 words per entry for any additional entries.
83  * (3) The data part of the PLT entries, comprising a jump table.
84  *     This section is half the size of the second section (ie. 1 or 2 words
85  *     per entry).
86  */
87 
88 void
89 _rtld_setup_pltgot(const Obj_Entry *obj)
90 {
91 #ifdef _LP64
92 	/*
93 	 * For powerpc64, just copy the function descriptor to pltgot[0].
94 	 */
95 	if (obj->pltgot != NULL) {
96 		obj->pltgot[0] = (Elf_Addr) _rtld_bind_start[0];
97 		obj->pltgot[1] = (Elf_Addr) _rtld_bind_start[1];
98 		obj->pltgot[2] = (Elf_Addr) obj;
99 	}
100 #else
101 	/*
102 	 * Secure-PLT is much more sane.
103 	 */
104 	if (obj->gotptr != NULL) {
105 		obj->gotptr[1] = (Elf_Addr) _rtld_bind_secureplt_start;
106 		obj->gotptr[2] = (Elf_Addr) obj;
107 		dbg(("obj %s secure-plt gotptr=%p start=%p obj=%p",
108 		    obj->path, obj->gotptr,
109 		    (void *) obj->gotptr[1], (void *) obj->gotptr[2]));
110 	} else {
111 /*
112  * Setup the plt glue routines (for bss-plt).
113  */
114 #define BSSPLTCALL_SIZE		20
115 #define BSSPLTRESOLVE_SIZE	24
116 
117 		Elf_Word *pltcall, *pltresolve;
118 		Elf_Word *jmptab;
119 		int N = obj->pltrelalim - obj->pltrela;
120 
121 		/* Entries beyond 8192 take twice as much space. */
122 		if (N > 8192)
123 			N += N-8192;
124 
125 		dbg(("obj %s bss-plt pltgot=%p jmptab=%u start=%p obj=%p",
126 		    obj->path, obj->pltgot, 18 + N * 2,
127 		    _rtld_bind_bssplt_start, obj));
128 
129 		pltcall = obj->pltgot;
130 		jmptab = pltcall + 18 + N * 2;
131 
132 		memcpy(pltcall, _rtld_powerpc_pltcall, BSSPLTCALL_SIZE);
133 		pltcall[1] |= ha(jmptab);
134 		pltcall[2] |= lo(jmptab);
135 
136 		pltresolve = obj->pltgot + 8;
137 
138 		memcpy(pltresolve, _rtld_powerpc_pltresolve, BSSPLTRESOLVE_SIZE);
139 		pltresolve[0] |= ha(_rtld_bind_bssplt_start);
140 		pltresolve[1] |= lo(_rtld_bind_bssplt_start);
141 		pltresolve[3] |= ha(obj);
142 		pltresolve[4] |= lo(obj);
143 
144 		/*
145 		 * Invalidate the icache for only the code part of the PLT
146 		 * (and not the jump table at the end).
147 		 */
148 		__syncicache(pltcall, (char *)jmptab - (char *)pltcall);
149 	}
150 #endif
151 }
152 
153 void
154 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
155 {
156 	const Elf_Rela *rela = 0, *relalim;
157 	Elf_Addr relasz = 0;
158 	Elf_Addr *where;
159 
160 	for (; dynp->d_tag != DT_NULL; dynp++) {
161 		switch (dynp->d_tag) {
162 		case DT_RELA:
163 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
164 			break;
165 		case DT_RELASZ:
166 			relasz = dynp->d_un.d_val;
167 			break;
168 		}
169 	}
170 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
171 	for (; rela < relalim; rela++) {
172 		where = (Elf_Addr *)(relocbase + rela->r_offset);
173 		*where = (Elf_Addr)(relocbase + rela->r_addend);
174 	}
175 }
176 
177 int
178 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
179 {
180 	const Elf_Rela *rela;
181 	const Elf_Sym *def = NULL;
182 	const Obj_Entry *defobj = NULL;
183 	unsigned long last_symnum = ULONG_MAX;
184 
185 	for (rela = obj->rela; rela < obj->relalim; rela++) {
186 		Elf_Addr        *where;
187 		Elf_Addr         tmp;
188 		unsigned long	 symnum;
189 
190 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
191 
192 		switch (ELF_R_TYPE(rela->r_info)) {
193 #ifdef _LP64
194 		case R_TYPE(ADDR64):	/* <address> S + A */
195 #else
196 		case R_TYPE(ADDR32):	/* <address> S + A */
197 #endif
198 		case R_TYPE(GLOB_DAT):	/* <address> S + A */
199 		case R_TYPE(DTPMOD):
200 		case R_TYPE(DTPREL):
201 		case R_TYPE(TPREL):
202 			symnum = ELF_R_SYM(rela->r_info);
203 			if (last_symnum != symnum) {
204 				last_symnum = symnum;
205 				def = _rtld_find_symdef(symnum, obj, &defobj,
206 				    false);
207 				if (def == NULL)
208 					return -1;
209 			}
210 			break;
211 		default:
212 			break;
213 		}
214 
215 		switch (ELF_R_TYPE(rela->r_info)) {
216 #if 1 /* XXX Should not be necessary. */
217 		case R_TYPE(JMP_SLOT):
218 #endif
219 		case R_TYPE(NONE):
220 			break;
221 
222 #ifdef _LP64
223 		case R_TYPE(ADDR64):	/* <address> S + A */
224 #else
225 		case R_TYPE(ADDR32):	/* <address> S + A */
226 #endif
227 		case R_TYPE(GLOB_DAT):	/* <address> S + A */
228 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
229 			    rela->r_addend);
230 			if (*where != tmp)
231 				*where = tmp;
232 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
233 			    obj->strtab + obj->symtab[symnum].st_name,
234 			    obj->path, (void *)*where, defobj->path));
235 			break;
236 
237 		case R_TYPE(RELATIVE):	/* <address> B + A */
238 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
239 			rdbg(("RELATIVE in %s --> %p", obj->path,
240 			    (void *)*where));
241 			break;
242 
243 		case R_TYPE(COPY):
244 			/*
245 			 * These are deferred until all other relocations have
246 			 * been done.  All we do here is make sure that the
247 			 * COPY relocation is not in a shared library.  They
248 			 * are allowed only in executable files.
249 			 */
250 			if (obj->isdynamic) {
251 				_rtld_error(
252 			"%s: Unexpected R_COPY relocation in shared library",
253 				    obj->path);
254 				return -1;
255 			}
256 			rdbg(("COPY (avoid in main)"));
257 			break;
258 
259 		case R_TYPE(DTPMOD):
260 			*where = (Elf_Addr)defobj->tlsindex;
261 			rdbg(("DTPMOD32 %s in %s --> %p in %s",
262 			    obj->strtab + obj->symtab[symnum].st_name,
263 			    obj->path, (void *)*where, defobj->path));
264 			break;
265 
266 		case R_TYPE(DTPREL):
267 			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
268 				return -1;
269 
270 			*where = (Elf_Addr)(def->st_value + rela->r_addend
271 			    - TLS_DTV_OFFSET);
272 			rdbg(("DTPREL32 %s in %s --> %p in %s",
273 			    obj->strtab + obj->symtab[symnum].st_name,
274 			    obj->path, (void *)*where, defobj->path));
275 			break;
276 
277 		case R_TYPE(TPREL):
278 			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
279 				return -1;
280 
281 			*where = (Elf_Addr)(def->st_value + rela->r_addend
282 			    + defobj->tlsoffset - TLS_TP_OFFSET);
283 			rdbg(("TPREL32 %s in %s --> %p in %s",
284 			    obj->strtab + obj->symtab[symnum].st_name,
285 			    obj->path, (void *)*where, defobj->path));
286 			break;
287 
288 		case R_TYPE(IRELATIVE):
289 			/* IFUNC relocations are handled in _rtld_call_ifunc */
290 			if (obj->ifunc_remaining_nonplt == 0) {
291 				obj->ifunc_remaining_nonplt =
292 				    obj->relalim - rela;
293 			}
294 			break;
295 
296 		default:
297 			rdbg(("sym = %lu, type = %lu, offset = %p, "
298 			    "addend = %p, contents = %p, symbol = %s",
299 			    (u_long)ELF_R_SYM(rela->r_info),
300 			    (u_long)ELF_R_TYPE(rela->r_info),
301 			    (void *)rela->r_offset, (void *)rela->r_addend,
302 			    (void *)*where,
303 			    obj->strtab + obj->symtab[symnum].st_name));
304 			_rtld_error("%s: Unsupported relocation type %ld "
305 			    "in non-PLT relocations",
306 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
307 			return -1;
308 		}
309 	}
310 	return 0;
311 }
312 
313 int
314 _rtld_relocate_plt_lazy(Obj_Entry *obj)
315 {
316 #ifdef _LP64
317 	/*
318 	 * For PowerPC64, the plt stubs handle an empty function descriptor
319 	 * so there's nothing to do.
320 	 */
321 	/* XXX ifunc support */
322 #else
323 	Elf_Addr * const pltresolve = obj->pltgot + 8;
324 	const Elf_Rela *rela;
325 
326 	for (rela = obj->pltrelalim; rela-- > obj->pltrela;) {
327 		size_t reloff = rela - obj->pltrela;
328 		Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
329 
330 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT) ||
331 		       ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE));
332 
333 		if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE)) {
334 			/* No ifunc support for old-style insecure PLT. */
335 			assert(obj->gotptr != NULL);
336 			obj->ifunc_remaining = obj->pltrelalim - rela;
337 		}
338 
339 		if (obj->gotptr != NULL) {
340 			/*
341 			 * For now, simply treat then as relative.
342 			 */
343 			*where += (Elf_Addr)obj->relocbase;
344 		} else {
345 			int distance;
346 
347 			if (reloff < 32768) {
348 				/* li	r11,reloff */
349 				*where++ = 0x39600000 | reloff;
350 			} else {
351 				/* lis  r11,ha(reloff) */
352 				/* addi	r11,lo(reloff) */
353 				*where++ = 0x3d600000 | ha(reloff);
354 				*where++ = 0x396b0000 | lo(reloff);
355 			}
356 			/* b	pltresolve */
357 			distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
358 			*where++ = 0x48000000 | (distance & 0x03fffffc);
359 
360 			/*
361 			 * Icache invalidation is not done for each entry here
362 			 * because we sync the entire code part of the PLT once
363 			 * in _rtld_setup_pltgot() after all the entries have been
364 			 * initialized.
365 			 */
366 			/* __syncicache(where - 3, 12); */
367 		}
368 	}
369 #endif /* !_LP64 */
370 
371 	return 0;
372 }
373 
374 static int
375 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
376 {
377 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
378 	Elf_Addr value;
379 	const Elf_Sym *def;
380 	const Obj_Entry *defobj;
381 	unsigned long info = rela->r_info;
382 
383 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
384 
385 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
386 	if (__predict_false(def == NULL))
387 		return -1;
388 	if (__predict_false(def == &_rtld_sym_zero))
389 		return 0;
390 
391 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
392 		if (tp == NULL)
393 			return 0;
394 		value = _rtld_resolve_ifunc(defobj, def);
395 	} else {
396 		value = (Elf_Addr)(defobj->relocbase + def->st_value);
397 	}
398 	rdbg(("bind now/fixup in %s --> new=%p",
399 	    defobj->strtab + def->st_name, (void *)value));
400 
401 #ifdef _LP64
402 	/*
403 	 * For PowerPC64 we simply replace the function descriptor in the
404 	 * PLTGOT with the one from source object.
405 	 */
406 	assert(where >= (Elf_Word *)obj->pltgot);
407 	assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
408 	const Elf_Addr * const fdesc = (Elf_Addr *) value;
409 	where[0] = fdesc[0];
410 	where[1] = fdesc[1];
411 	where[2] = fdesc[2];
412 #else
413 	ptrdiff_t distance = value - (Elf_Addr)where;
414 	if (obj->gotptr != NULL) {
415 		/*
416 		 * For Secure-PLT we simply replace the entry in GOT with the
417 		 * address of the routine.
418 		 */
419 		assert(where >= (Elf_Word *)obj->pltgot);
420 		assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
421 		*where = value;
422 	} else if (labs(distance) < 32*1024*1024) {	/* inside 32MB? */
423 		/* b	value	# branch directly */
424 		*where = 0x48000000 | (distance & 0x03fffffc);
425 		__syncicache(where, 4);
426 	} else {
427 		Elf_Addr *pltcall, *jmptab;
428 		int N = obj->pltrelalim - obj->pltrela;
429 
430 		/* Entries beyond 8192 take twice as much space. */
431 		if (N > 8192)
432 			N += N-8192;
433 
434 		pltcall = obj->pltgot;
435 		jmptab = pltcall + 18 + N * 2;
436 
437 		jmptab[reloff] = value;
438 
439 		if (reloff < 32768) {
440 			/* li	r11,reloff */
441 			*where++ = 0x39600000 | reloff;
442 		} else {
443 #ifdef notyet
444 			/* lis  r11,ha(value) */
445 			/* addi	r11,lo(value) */
446 			/* mtctr r11 */
447 			/* bctr */
448 			*where++ = 0x3d600000 | ha(value);
449 			*where++ = 0x396b0000 | lo(value);
450 			*where++ = 0x7d6903a6;
451 			*where++ = 0x4e800420;
452 #else
453 			/* lis  r11,ha(reloff) */
454 			/* addi	r11,lo(reloff) */
455 			*where++ = 0x3d600000 | ha(reloff);
456 			*where++ = 0x396b0000 | lo(reloff);
457 #endif
458 		}
459 		/* b	pltcall	*/
460 		distance = (Elf_Addr)pltcall - (Elf_Addr)where;
461 		*where++ = 0x48000000 | (distance & 0x03fffffc);
462 		__syncicache(where - 3, 12);
463 	}
464 #endif /* _LP64 */
465 
466 	if (tp)
467 		*tp = value;
468 	return 0;
469 }
470 
471 Elf_Addr
472 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
473 {
474 	const Elf_Rela *rela = obj->pltrela + reloff;
475 	Elf_Addr new_value;
476 	int err;
477 
478 	new_value = 0;	/* XXX gcc */
479 
480 	_rtld_shared_enter();
481 	err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
482 	if (err)
483 		_rtld_die();
484 	_rtld_shared_exit();
485 
486 #ifdef _LP64
487 	return obj->glink;
488 #else
489 	return new_value;
490 #endif
491 }
492 
493 int
494 _rtld_relocate_plt_objects(const Obj_Entry *obj)
495 {
496 	const Elf_Rela *rela;
497 	int reloff;
498 
499 	for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
500 		if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
501 			return -1;
502 	}
503 	return 0;
504 }
505