xref: /freebsd/libexec/rtld-elf/powerpc/reloc.c (revision dafba19e42e78cd3d7c9264ece49ddd3d7d70da5)
1 /*      $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 mycroft Exp $   */
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (C) 1998   Tsubai Masanari
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/mman.h>
34 
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <machine/cpu.h>
41 #include <machine/atomic.h>
42 #include <machine/md_var.h>
43 
44 #include "debug.h"
45 #include "rtld.h"
46 
47 #define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \
48                         ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
49 #define _ppc_la(x) ((u_int32_t)(x) & 0xffff)
50 
51 #define min(a,b) (((a) < (b)) ? (a) : (b))
52 #define max(a,b) (((a) > (b)) ? (a) : (b))
53 
54 #define PLT_EXTENDED_BEGIN	(1 << 13)
55 #define JMPTAB_BASE(N)		(18 + N*2 + ((N > PLT_EXTENDED_BEGIN) ? \
56 				    (N - PLT_EXTENDED_BEGIN)*2 : 0))
57 
58 void _rtld_bind_secureplt_start(void);
59 
60 bool
61 arch_digest_dynamic(struct Struct_Obj_Entry *obj, const Elf_Dyn *dynp)
62 {
63 	if (dynp->d_tag == DT_PPC_GOT) {
64 		obj->gotptr = (Elf_Addr *)(obj->relocbase + dynp->d_un.d_ptr);
65 		return (true);
66 	}
67 
68 	return (false);
69 }
70 
71 /*
72  * Process the R_PPC_COPY relocations
73  */
74 int
75 do_copy_relocations(Obj_Entry *dstobj)
76 {
77 	const Elf_Rela *relalim;
78 	const Elf_Rela *rela;
79 
80 	/*
81 	 * COPY relocs are invalid outside of the main program
82 	 */
83 	assert(dstobj->mainprog);
84 
85 	relalim = (const Elf_Rela *)((const char *) dstobj->rela +
86 	    dstobj->relasize);
87 	for (rela = dstobj->rela;  rela < relalim;  rela++) {
88 		void *dstaddr;
89 		const Elf_Sym *dstsym;
90 		const char *name;
91 		size_t size;
92 		const void *srcaddr;
93 		const Elf_Sym *srcsym = NULL;
94 		const Obj_Entry *srcobj, *defobj;
95 		SymLook req;
96 		int res;
97 
98 		if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
99 			continue;
100 		}
101 
102 		dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
103 		dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
104 		name = dstobj->strtab + dstsym->st_name;
105 		size = dstsym->st_size;
106 		symlook_init(&req, name);
107 		req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
108 		req.flags = SYMLOOK_EARLY;
109 
110 		for (srcobj = globallist_next(dstobj); srcobj != NULL;
111 		     srcobj = globallist_next(srcobj)) {
112 			res = symlook_obj(&req, srcobj);
113 			if (res == 0) {
114 				srcsym = req.sym_out;
115 				defobj = req.defobj_out;
116 				break;
117 			}
118 		}
119 
120 		if (srcobj == NULL) {
121 			_rtld_error("Undefined symbol \"%s\" "
122 				    " referenced from COPY"
123 				    " relocation in %s", name, dstobj->path);
124 			return (-1);
125 		}
126 
127 		srcaddr = (const void *)(defobj->relocbase+srcsym->st_value);
128 		memcpy(dstaddr, srcaddr, size);
129 		dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
130 	}
131 
132 	return (0);
133 }
134 
135 
136 /*
137  * Perform early relocation of the run-time linker image
138  */
139 void
140 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
141 {
142 	const Elf_Rela *rela = NULL, *relalim;
143 	Elf_Addr relasz = 0;
144 	Elf_Addr *where;
145 
146 	/*
147 	 * Extract the rela/relasz values from the dynamic section
148 	 */
149 	for (; dynp->d_tag != DT_NULL; dynp++) {
150 		switch (dynp->d_tag) {
151 		case DT_RELA:
152 			rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
153 			break;
154 		case DT_RELASZ:
155 			relasz = dynp->d_un.d_val;
156 			break;
157 		}
158 	}
159 
160 	/*
161 	 * Relocate these values
162 	 */
163 	relalim = (const Elf_Rela *)((const char *)rela + relasz);
164 	for (; rela < relalim; rela++) {
165 		where = (Elf_Addr *)(relocbase + rela->r_offset);
166 		*where = (Elf_Addr)(relocbase + rela->r_addend);
167 	}
168 }
169 
170 
171 /*
172  * Relocate a non-PLT object with addend.
173  */
174 static int
175 reloc_nonplt_object(Obj_Entry *obj_rtld __unused, Obj_Entry *obj,
176     const Elf_Rela *rela, SymCache *cache, int flags, RtldLockState *lockstate)
177 {
178 	const Elf_Sym	*def = NULL;
179 	const Obj_Entry	*defobj;
180 	Elf_Addr	*where, symval = 0;
181 
182 	/*
183 	 * First, resolve symbol for relocations which
184 	 * reference symbols.
185 	 */
186 	switch (ELF_R_TYPE(rela->r_info)) {
187 
188 	case R_PPC_UADDR32:    /* word32 S + A */
189 	case R_PPC_ADDR32:
190 	case R_PPC_GLOB_DAT:  /* word32 S + A */
191 	case R_PPC_DTPMOD32:
192 	case R_PPC_TPREL32:
193 	case R_PPC_DTPREL32:
194 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
195 		    flags, cache, lockstate);
196 		if (def == NULL) {
197 			return (-1);
198 		}
199 
200 		/*
201 		 * If symbol is IFUNC, only perform relocation
202 		 * when caller allowed it by passing
203 		 * SYMLOOK_IFUNC flag.  Skip the relocations
204 		 * otherwise.
205 		 *
206 		 * Also error out in case IFUNC relocations
207 		 * are specified for TLS, which cannot be
208 		 * usefully interpreted.
209 		 */
210 		if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
211 			switch (ELF_R_TYPE(rela->r_info)) {
212 			case R_PPC_UADDR32:
213 			case R_PPC_ADDR32:
214 			case R_PPC_GLOB_DAT:
215 				if ((flags & SYMLOOK_IFUNC) == 0) {
216 					dbg("Non-PLT reference to IFUNC found!");
217 					obj->non_plt_gnu_ifunc = true;
218 					return (0);
219 				}
220 				symval = (Elf_Addr)rtld_resolve_ifunc(
221 					defobj, def);
222 				break;
223 			default:
224 				_rtld_error("%s: IFUNC for TLS reloc",
225 					obj->path);
226 				return (-1);
227 			}
228 		} else {
229 			if ((flags & SYMLOOK_IFUNC) != 0)
230 				return (0);
231 			symval = (Elf_Addr)defobj->relocbase +
232 				def->st_value;
233 		}
234 		break;
235 	default:
236 		if ((flags & SYMLOOK_IFUNC) != 0)
237 			return (0);
238 	}
239 	where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
240 
241 	switch (ELF_R_TYPE(rela->r_info)) {
242 	case R_PPC_NONE:
243 		break;
244 	case R_PPC_UADDR32:
245 	case R_PPC_ADDR32:
246 	case R_PPC_GLOB_DAT:
247 		/* Don't issue write if unnecessary; avoid COW page fault */
248 		if (*where != symval + rela->r_addend) {
249 			*where = symval + rela->r_addend;
250 		}
251 		break;
252 	case R_PPC_DTPMOD32:
253 		*where = (Elf_Addr) defobj->tlsindex;
254 		break;
255 	case R_PPC_TPREL32:
256 		/*
257 		 * We lazily allocate offsets for static TLS as we
258 		 * see the first relocation that references the
259 		 * TLS block. This allows us to support (small
260 		 * amounts of) static TLS in dynamically loaded
261 		 * modules. If we run out of space, we generate an
262 		 * error.
263 		 */
264 		if (!defobj->tls_static) {
265 			if (!allocate_tls_offset(
266 				    __DECONST(Obj_Entry *, defobj))) {
267 				_rtld_error("%s: No space available for static "
268 				    "Thread Local Storage", obj->path);
269 				return (-1);
270 			}
271 		}
272 
273 		*(Elf_Addr **)where = *where * sizeof(Elf_Addr)
274 		    + (Elf_Addr *)(def->st_value + rela->r_addend
275 		    + defobj->tlsoffset - TLS_TP_OFFSET - TLS_TCB_SIZE);
276 		break;
277 	case R_PPC_DTPREL32:
278 		*where += (Elf_Addr)(def->st_value + rela->r_addend
279 		    - TLS_DTV_OFFSET);
280 		break;
281 	case R_PPC_RELATIVE: /* word32 B + A */
282 		symval = (Elf_Addr)(obj->relocbase + rela->r_addend);
283 
284 		/* As above, don't issue write unnecessarily */
285 		if (*where != symval) {
286 			*where = symval;
287 		}
288 		break;
289 	case R_PPC_COPY:
290 		/*
291 		 * These are deferred until all other relocations
292 		 * have been done.  All we do here is make sure
293 		 * that the COPY relocation is not in a shared
294 		 * library.  They are allowed only in executable
295 		 * files.
296 		 */
297 		if (!obj->mainprog) {
298 			_rtld_error("%s: Unexpected R_COPY "
299 				    " relocation in shared library",
300 				    obj->path);
301 			return (-1);
302 		}
303 		break;
304 	case R_PPC_IRELATIVE:
305 		/*
306 		 * These will be handled by reloc_iresolve().
307 		 */
308 		obj->irelative = true;
309 		break;
310 	case R_PPC_JMP_SLOT:
311 		/*
312 		 * These will be handled by the plt/jmpslot routines
313 		 */
314 		break;
315 
316 	default:
317 		_rtld_error("%s: Unsupported relocation type %d"
318 			    " in non-PLT relocations\n", obj->path,
319 			    ELF_R_TYPE(rela->r_info));
320 		return (-1);
321 	}
322 	return (0);
323 }
324 
325 
326 /*
327  * Process non-PLT relocations
328  */
329 int
330 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
331     RtldLockState *lockstate)
332 {
333 	const Elf_Rela *relalim;
334 	const Elf_Rela *rela;
335 	const Elf_Phdr *phdr;
336 	SymCache *cache;
337 	int r = -1;
338 
339 	/*
340 	 * The dynamic loader may be called from a thread, we have
341 	 * limited amounts of stack available so we cannot use alloca().
342 	 */
343 	if (obj != obj_rtld) {
344 		cache = calloc(obj->dynsymcount, sizeof(SymCache));
345 		/* No need to check for NULL here */
346 	} else
347 		cache = NULL;
348 
349 	/*
350 	 * From the SVR4 PPC ABI:
351 	 * "The PowerPC family uses only the Elf32_Rela relocation
352 	 *  entries with explicit addends."
353 	 */
354 	relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
355 	for (rela = obj->rela; rela < relalim; rela++) {
356 		if (reloc_nonplt_object(obj_rtld, obj, rela, cache, flags,
357 		    lockstate) < 0)
358 			goto done;
359 	}
360 	r = 0;
361 done:
362 	if (cache != NULL)
363 		free(cache);
364 
365 	/*
366 	 * Synchronize icache for executable segments in case we made
367 	 * any changes.
368 	 */
369 	for (phdr = obj->phdr; phdr < obj->phdr + obj->phnum; phdr++) {
370 		if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X) != 0) {
371 			__syncicache(obj->relocbase + phdr->p_vaddr,
372 			    phdr->p_memsz);
373 		}
374 	}
375 
376 	return (r);
377 }
378 
379 /*
380  * Initialise a PLT slot to the resolving trampoline
381  */
382 static int
383 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
384 {
385 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
386 	Elf_Addr *pltresolve, *pltlongresolve, *jmptab;
387 	Elf_Addr distance;
388 	int N = obj->pltrelasize / sizeof(Elf_Rela);
389 	int reloff;
390 
391 	reloff = rela - obj->pltrela;
392 
393 	if (reloff < 0)
394 		return (-1);
395 
396 	if (obj->gotptr != NULL) {
397 		*where += (Elf_Addr)obj->relocbase;
398 		return (0);
399 	}
400 
401 	pltlongresolve = obj->pltgot + 5;
402 	pltresolve = pltlongresolve + 5;
403 
404 	distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
405 
406 	dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
407 	    (void *)where, (void *)pltresolve, reloff, distance);
408 
409 	if (reloff < PLT_EXTENDED_BEGIN) {
410 		/* li   r11,reloff  */
411 		/* b    pltresolve  */
412 		where[0] = 0x39600000 | reloff;
413 		where[1] = 0x48000000 | (distance & 0x03fffffc);
414 	} else {
415 		jmptab = obj->pltgot + JMPTAB_BASE(N);
416 		jmptab[reloff] = (u_int)pltlongresolve;
417 
418 		/* lis	r11,jmptab[reloff]@ha */
419 		/* lwzu	r12,jmptab[reloff]@l(r11) */
420 		/* mtctr r12 */
421 		/* bctr */
422 		where[0] = 0x3d600000 | _ppc_ha(&jmptab[reloff]);
423 		where[1] = 0x858b0000 | _ppc_la(&jmptab[reloff]);
424 		where[2] = 0x7d8903a6;
425 		where[3] = 0x4e800420;
426 	}
427 
428 
429 	/*
430 	 * The icache will be sync'd in reloc_plt, which is called
431 	 * after all the slots have been updated
432 	 */
433 
434 	return (0);
435 }
436 
437 /*
438  * Process the PLT relocations.
439  */
440 int
441 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
442 {
443 	const Elf_Rela *relalim;
444 	const Elf_Rela *rela;
445 	int N = obj->pltrelasize / sizeof(Elf_Rela);
446 
447 	if (obj->pltrelasize != 0) {
448 
449 		relalim = (const Elf_Rela *)((const char *)obj->pltrela +
450 		    obj->pltrelasize);
451 		for (rela = obj->pltrela;  rela < relalim;  rela++) {
452 			if (ELF_R_TYPE(rela->r_info) == R_PPC_IRELATIVE) {
453 				dbg("ABI violation - found IRELATIVE in the PLT.");
454 				obj->irelative = true;
455 				continue;
456 			}
457 
458 			/*
459 			 * PowerPC(64) .rela.plt is composed of an array of
460 			 * R_PPC_JMP_SLOT relocations. Unlike other platforms,
461 			 * this is the ONLY relocation type that is valid here.
462 			 */
463 			assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
464 
465 			if (reloc_plt_object(obj, rela) < 0) {
466 				return (-1);
467 			}
468 		}
469 	}
470 
471 	/*
472 	 * Sync the icache for the byte range represented by the
473 	 * trampoline routines and call slots.
474 	 */
475 	if (obj->pltgot != NULL && obj->gotptr == NULL)
476 		__syncicache(obj->pltgot, JMPTAB_BASE(N)*4);
477 
478 	return (0);
479 }
480 
481 /*
482  * LD_BIND_NOW was set - force relocation for all jump slots
483  */
484 int
485 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
486 {
487 	const Obj_Entry *defobj;
488 	const Elf_Rela *relalim;
489 	const Elf_Rela *rela;
490 	const Elf_Sym *def;
491 	Elf_Addr *where;
492 	Elf_Addr target;
493 
494 	relalim = (const Elf_Rela *)((const char *)obj->pltrela +
495 	    obj->pltrelasize);
496 	for (rela = obj->pltrela; rela < relalim; rela++) {
497 		/* This isn't actually a jump slot, ignore it. */
498 		if (ELF_R_TYPE(rela->r_info) == R_PPC_IRELATIVE)
499 			continue;
500 		assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
501 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
502 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
503 		    SYMLOOK_IN_PLT | flags, NULL, lockstate);
504 		if (def == NULL) {
505 			dbg("reloc_jmpslots: sym not found");
506 			return (-1);
507 		}
508 
509 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
510 
511 		if (def == &sym_zero) {
512 			/* Zero undefined weak symbols */
513 			*where = 0;
514 		} else {
515 			if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
516 				/* LD_BIND_NOW, ifunc in shared lib.*/
517 				obj->gnu_ifunc = true;
518 				continue;
519 			}
520 			reloc_jmpslot(where, target, defobj, obj,
521 			    (const Elf_Rel *) rela);
522 		}
523 	}
524 
525 	obj->jmpslots_done = true;
526 
527 	return (0);
528 }
529 
530 
531 /*
532  * Update the value of a PLT jump slot.
533  */
534 Elf_Addr
535 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target,
536     const Obj_Entry *defobj __unused, const Obj_Entry *obj, const Elf_Rel *rel)
537 {
538 	Elf_Addr offset;
539 	const Elf_Rela *rela = (const Elf_Rela *) rel;
540 
541 	dbg(" reloc_jmpslot: where=%p, target=%p",
542 	    (void *)wherep, (void *)target);
543 
544 	if (ld_bind_not)
545 		goto out;
546 
547 
548 	/*
549 	 * Process Secure-PLT.
550 	 */
551 	if (obj->gotptr != NULL) {
552 		assert(wherep >= (Elf_Word *)obj->pltgot);
553 		assert(wherep <
554 		    (Elf_Word *)obj->pltgot + obj->pltrelasize);
555 		if (*wherep != target)
556 			*wherep = target;
557 		goto out;
558 	}
559 
560 	/*
561 	 * BSS-PLT optimization:
562 	 * Branch directly to the target if it is within +/- 32Mb,
563 	 * otherwise go indirectly via the pltcall trampoline call and
564 	 * jump table.
565 	 */
566 	offset = target - (Elf_Addr)wherep;
567 	if (abs((int)offset) < 32*1024*1024) {     /* inside 32MB? */
568 		/*
569 		 * At the PLT entry pointed at by `wherep', construct
570 		 * a direct transfer to the now fully resolved function
571 		 * address.
572 		 */
573 		/* b    value   # branch directly */
574 		*wherep = 0x48000000 | (offset & 0x03fffffc);
575 		__syncicache(wherep, 4);
576 	} else {
577 		Elf_Addr *pltcall, *jmptab;
578 		int distance;
579 		int N = obj->pltrelasize / sizeof(Elf_Rela);
580 		int reloff = rela - obj->pltrela;
581 
582 		if (reloff < 0)
583 			return (-1);
584 
585 		pltcall = obj->pltgot;
586 
587 		dbg(" reloc_jmpslot: indir, reloff=%x, N=%x\n",
588 		    reloff, N);
589 
590 		jmptab = obj->pltgot + JMPTAB_BASE(N);
591 		jmptab[reloff] = target;
592 		mb(); /* Order jmptab update before next changes */
593 
594 		if (reloff < PLT_EXTENDED_BEGIN) {
595 			/* for extended PLT entries, we keep the old code */
596 
597 			distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
598 
599 			/* li   r11,reloff */
600 			/* b    pltcall  # use indirect pltcall routine */
601 
602 			/* first instruction same as before */
603 			wherep[1] = 0x48000000 | (distance & 0x03fffffc);
604 			__syncicache(wherep, 8);
605 		}
606 	}
607 
608 out:
609 	return (target);
610 }
611 
612 int
613 reloc_iresolve(Obj_Entry *obj,
614     struct Struct_RtldLockState *lockstate)
615 {
616 	/*
617 	 * Since PLT slots on PowerPC are always R_PPC_JMP_SLOT,
618 	 * R_PPC_IRELATIVE is in RELA.
619 	 */
620 	const Elf_Rela *relalim;
621 	const Elf_Rela *rela;
622 	Elf_Addr *where, target, *ptr;
623 
624 	if (!obj->irelative)
625 		return (0);
626 
627 	relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
628 	for (rela = obj->rela;  rela < relalim;  rela++) {
629 		if (ELF_R_TYPE(rela->r_info) == R_PPC_IRELATIVE) {
630 			ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
631 			where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
632 
633 			lock_release(rtld_bind_lock, lockstate);
634 			target = call_ifunc_resolver(ptr);
635 			wlock_acquire(rtld_bind_lock, lockstate);
636 
637 			*where = target;
638 		}
639 	}
640 	/*
641 	 * XXX Remove me when lld is fixed!
642 	 * LLD currently makes illegal relocations in the PLT.
643 	 */
644 	relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
645 	for (rela = obj->pltrela;  rela < relalim;  rela++) {
646 		if (ELF_R_TYPE(rela->r_info) == R_PPC_IRELATIVE) {
647 			ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
648 			where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
649 
650 			lock_release(rtld_bind_lock, lockstate);
651 			target = call_ifunc_resolver(ptr);
652 			wlock_acquire(rtld_bind_lock, lockstate);
653 
654 			*where = target;
655 		}
656 	}
657 
658 	obj->irelative = false;
659 	return (0);
660 }
661 
662 int
663 reloc_iresolve_nonplt(Obj_Entry *obj __unused,
664     struct Struct_RtldLockState *lockstate __unused)
665 {
666 	return (0);
667 }
668 
669 int
670 reloc_gnu_ifunc(Obj_Entry *obj __unused, int flags __unused,
671     struct Struct_RtldLockState *lockstate __unused)
672 {
673 	const Elf_Rela *relalim;
674 	const Elf_Rela *rela;
675 	Elf_Addr *where, target;
676 	const Elf_Sym *def;
677 	const Obj_Entry *defobj;
678 
679 	if (!obj->gnu_ifunc)
680 		return (0);
681 	relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
682 	for (rela = obj->pltrela;  rela < relalim;  rela++) {
683 		if (ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT) {
684 			where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
685 			def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
686 			    SYMLOOK_IN_PLT | flags, NULL, lockstate);
687 			if (def == NULL)
688 				return (-1);
689 			if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
690 				continue;
691 			lock_release(rtld_bind_lock, lockstate);
692 			target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
693 			wlock_acquire(rtld_bind_lock, lockstate);
694 			reloc_jmpslot(where, target, defobj, obj,
695 			    (const Elf_Rel *)rela);
696 		}
697 	}
698 	obj->gnu_ifunc = false;
699 	return (0);
700 }
701 
702 /*
703  * Setup the plt glue routines.
704  */
705 #define PLTCALL_SIZE	   	20
706 #define PLTLONGRESOLVE_SIZE	20
707 #define PLTRESOLVE_SIZE		24
708 
709 void
710 init_pltgot(Obj_Entry *obj)
711 {
712 	Elf_Word *pltcall, *pltresolve, *pltlongresolve;
713 	Elf_Word *jmptab;
714 	int N = obj->pltrelasize / sizeof(Elf_Rela);
715 
716 	pltcall = obj->pltgot;
717 
718 	if (pltcall == NULL) {
719 		return;
720 	}
721 
722 	/* Handle Secure-PLT first, if applicable. */
723 	if (obj->gotptr != NULL) {
724 		obj->gotptr[1] = (Elf_Addr)_rtld_bind_secureplt_start;
725 		obj->gotptr[2] = (Elf_Addr)obj;
726 		dbg("obj %s secure-plt gotptr=%p start=%p obj=%p",
727 		    obj->path, obj->gotptr,
728 		    (void *)obj->gotptr[1], (void *)obj->gotptr[2]);
729 		return;
730 	}
731 
732 	/*
733 	 * From the SVR4 PPC ABI:
734 	 *
735 	 * 'The first 18 words (72 bytes) of the PLT are reserved for
736 	 * use by the dynamic linker.
737 	 *   ...
738 	 * 'If the executable or shared object requires N procedure
739 	 *  linkage table entries, the link editor shall reserve 3*N
740 	 *  words (12*N bytes) following the 18 reserved words. The
741 	 *  first 2*N of these words are the procedure linkage table
742 	 *  entries themselves. The static linker directs calls to bytes
743 	 *  (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
744 	 *  N words (4*N bytes) are reserved for use by the dynamic linker.'
745 	 */
746 
747 	/*
748 	 * Copy the absolute-call assembler stub into the first part of
749 	 * the reserved PLT area.
750 	 */
751 	memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
752 
753 	/*
754 	 * Determine the address of the jumptable, which is the dyn-linker
755 	 * reserved area after the call cells. Write the absolute address
756 	 * of the jumptable into the absolute-call assembler code so it
757 	 * can determine this address.
758 	 */
759 	jmptab = obj->pltgot + JMPTAB_BASE(N);
760 	pltcall[1] |= _ppc_ha(jmptab);	   /* addis 11,11,jmptab@ha */
761 	pltcall[2] |= _ppc_la(jmptab);     /* lwz   11,jmptab@l(11) */
762 
763 	/*
764 	 * Skip down 20 bytes into the initial reserved area and copy
765 	 * in the standard resolving assembler call. Into this assembler,
766 	 * insert the absolute address of the _rtld_bind_start routine
767 	 * and the address of the relocation object.
768 	 *
769 	 * We place pltlongresolve first, so it can fix up its arguments
770 	 * and then fall through to the regular PLT resolver.
771 	 */
772 	pltlongresolve = obj->pltgot + 5;
773 
774 	memcpy(pltlongresolve, _rtld_powerpc_pltlongresolve,
775 	    PLTLONGRESOLVE_SIZE);
776 	pltlongresolve[0] |= _ppc_ha(jmptab);	/* lis	12,jmptab@ha	*/
777 	pltlongresolve[1] |= _ppc_la(jmptab);	/* addi	12,12,jmptab@l	*/
778 
779 	pltresolve = pltlongresolve + PLTLONGRESOLVE_SIZE/sizeof(uint32_t);
780 	memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
781 	pltresolve[0] |= _ppc_ha(_rtld_bind_start);
782 	pltresolve[1] |= _ppc_la(_rtld_bind_start);
783 	pltresolve[3] |= _ppc_ha(obj);
784 	pltresolve[4] |= _ppc_la(obj);
785 
786 	/*
787 	 * The icache will be sync'd in reloc_plt, which is called
788 	 * after all the slots have been updated
789 	 */
790 }
791 
792 /*
793  * 32 bit cpu feature flag fields.
794  */
795 u_long cpu_features;
796 u_long cpu_features2;
797 
798 void
799 powerpc_abi_variant_hook(Elf_Auxinfo** aux_info)
800 {
801 	/*
802 	 * Since aux_info[] is easier to work with than aux, go ahead and
803 	 * initialize cpu_features / cpu_features2.
804 	 */
805 	cpu_features = -1UL;
806 	cpu_features2 = -1UL;
807 	if (aux_info[AT_HWCAP] != NULL)
808 		cpu_features = aux_info[AT_HWCAP]->a_un.a_val;
809 	if (aux_info[AT_HWCAP2] != NULL)
810 		cpu_features2 = aux_info[AT_HWCAP2]->a_un.a_val;
811 }
812 
813 void
814 ifunc_init(Elf_Auxinfo *aux_info[__min_size(AT_COUNT)] __unused)
815 {
816 
817 }
818 
819 void
820 allocate_initial_tls(Obj_Entry *list)
821 {
822 
823 	/*
824 	* Fix the size of the static TLS block by using the maximum
825 	* offset allocated so far and adding a bit for dynamic modules to
826 	* use.
827 	*/
828 
829 	tls_static_space = tls_last_offset + tls_last_size +
830 	    ld_static_tls_extra;
831 
832 	_tcb_set(allocate_tls(list, NULL, TLS_TCB_SIZE, TLS_TCB_ALIGN));
833 }
834 
835 void*
836 __tls_get_addr(tls_index* ti)
837 {
838 	return (tls_get_addr_common(_tcb_get(), ti->ti_module, ti->ti_offset +
839 	    TLS_DTV_OFFSET));
840 }
841 
842 void
843 arch_fix_auxv(Elf_Auxinfo *aux, Elf_Auxinfo *aux_info[])
844 {
845 	Elf_Auxinfo *auxp;
846 
847 	for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
848 		if (auxp->a_type == 23) /* AT_STACKPROT */
849 			return;
850 	}
851 
852 	/* Remap from old-style auxv numbers. */
853 	aux_info[23] = aux_info[21]; /* AT_STACKPROT */
854 	aux_info[21] = aux_info[19]; /* AT_PAGESIZESLEN */
855 	aux_info[19] = aux_info[17]; /* AT_NCPUS */
856 	aux_info[17] = aux_info[15]; /* AT_CANARYLEN */
857 	aux_info[15] = aux_info[13]; /* AT_EXECPATH */
858 	aux_info[13] = NULL;	     /* AT_GID */
859 
860 	aux_info[20] = aux_info[18]; /* AT_PAGESIZES */
861 	aux_info[18] = aux_info[16]; /* AT_OSRELDATE */
862 	aux_info[16] = aux_info[14]; /* AT_CANARY */
863 	aux_info[14] = NULL;	     /* AT_EGID */
864 }
865