xref: /freebsd/libexec/rtld-elf/powerpc/reloc.c (revision f05cddf9)
1 /*      $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 mycroft Exp $   */
2 
3 /*-
4  * Copyright (C) 1998   Tsubai Masanari
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
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 /*
59  * Process the R_PPC_COPY relocations
60  */
61 int
62 do_copy_relocations(Obj_Entry *dstobj)
63 {
64 	const Elf_Rela *relalim;
65 	const Elf_Rela *rela;
66 
67 	/*
68 	 * COPY relocs are invalid outside of the main program
69 	 */
70 	assert(dstobj->mainprog);
71 
72 	relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
73 	    dstobj->relasize);
74 	for (rela = dstobj->rela;  rela < relalim;  rela++) {
75 		void *dstaddr;
76 		const Elf_Sym *dstsym;
77 		const char *name;
78 		size_t size;
79 		const void *srcaddr;
80 		const Elf_Sym *srcsym = NULL;
81 		const Obj_Entry *srcobj, *defobj;
82 		SymLook req;
83 		int res;
84 
85 		if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
86 			continue;
87 		}
88 
89 		dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
90 		dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
91 		name = dstobj->strtab + dstsym->st_name;
92 		size = dstsym->st_size;
93 		symlook_init(&req, name);
94 		req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
95 		req.flags = SYMLOOK_EARLY;
96 
97 		for (srcobj = dstobj->next;  srcobj != NULL;
98 		     srcobj = srcobj->next) {
99 			res = symlook_obj(&req, srcobj);
100 			if (res == 0) {
101 				srcsym = req.sym_out;
102 				defobj = req.defobj_out;
103 				break;
104 			}
105 		}
106 
107 		if (srcobj == NULL) {
108 			_rtld_error("Undefined symbol \"%s\" "
109 				    " referenced from COPY"
110 				    " relocation in %s", name, dstobj->path);
111 			return (-1);
112 		}
113 
114 		srcaddr = (const void *) (defobj->relocbase+srcsym->st_value);
115 		memcpy(dstaddr, srcaddr, size);
116 		dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
117 	}
118 
119 	return (0);
120 }
121 
122 
123 /*
124  * Perform early relocation of the run-time linker image
125  */
126 void
127 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
128 {
129 	const Elf_Rela *rela = 0, *relalim;
130 	Elf_Addr relasz = 0;
131 	Elf_Addr *where;
132 
133 	/*
134 	 * Extract the rela/relasz values from the dynamic section
135 	 */
136 	for (; dynp->d_tag != DT_NULL; dynp++) {
137 		switch (dynp->d_tag) {
138 		case DT_RELA:
139 			rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
140 			break;
141 		case DT_RELASZ:
142 			relasz = dynp->d_un.d_val;
143 			break;
144 		}
145 	}
146 
147 	/*
148 	 * Relocate these values
149 	 */
150 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
151 	for (; rela < relalim; rela++) {
152 		where = (Elf_Addr *)(relocbase + rela->r_offset);
153 		*where = (Elf_Addr)(relocbase + rela->r_addend);
154 	}
155 }
156 
157 
158 /*
159  * Relocate a non-PLT object with addend.
160  */
161 static int
162 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
163     SymCache *cache, int flags, RtldLockState *lockstate)
164 {
165 	Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
166 	const Elf_Sym   *def;
167 	const Obj_Entry *defobj;
168 	Elf_Addr         tmp;
169 
170 	switch (ELF_R_TYPE(rela->r_info)) {
171 
172 	case R_PPC_NONE:
173 		break;
174 
175         case R_PPC_ADDR32:    /* word32 S + A */
176         case R_PPC_GLOB_DAT:  /* word32 S + A */
177 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
178 		    flags, cache, lockstate);
179 		if (def == NULL) {
180 			return (-1);
181 		}
182 
183                 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
184                     rela->r_addend);
185 
186 		/* Don't issue write if unnecessary; avoid COW page fault */
187                 if (*where != tmp) {
188                         *where = tmp;
189 		}
190                 break;
191 
192         case R_PPC_RELATIVE:  /* word32 B + A */
193 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
194 
195 		/* As above, don't issue write unnecessarily */
196 		if (*where != tmp) {
197 			*where = tmp;
198 		}
199 		break;
200 
201 	case R_PPC_COPY:
202 		/*
203 		 * These are deferred until all other relocations
204 		 * have been done.  All we do here is make sure
205 		 * that the COPY relocation is not in a shared
206 		 * library.  They are allowed only in executable
207 		 * files.
208 		 */
209 		if (!obj->mainprog) {
210 			_rtld_error("%s: Unexpected R_COPY "
211 				    " relocation in shared library",
212 				    obj->path);
213 			return (-1);
214 		}
215 		break;
216 
217 	case R_PPC_JMP_SLOT:
218 		/*
219 		 * These will be handled by the plt/jmpslot routines
220 		 */
221 		break;
222 
223 	case R_PPC_DTPMOD32:
224 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
225 		    flags, cache, lockstate);
226 
227 		if (def == NULL)
228 			return (-1);
229 
230 		*where = (Elf_Addr) defobj->tlsindex;
231 
232 		break;
233 
234 	case R_PPC_TPREL32:
235 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
236 		    flags, cache, lockstate);
237 
238 		if (def == NULL)
239 			return (-1);
240 
241 		/*
242 		 * We lazily allocate offsets for static TLS as we
243 		 * see the first relocation that references the
244 		 * TLS block. This allows us to support (small
245 		 * amounts of) static TLS in dynamically loaded
246 		 * modules. If we run out of space, we generate an
247 		 * error.
248 		 */
249 		if (!defobj->tls_done) {
250 			if (!allocate_tls_offset((Obj_Entry*) defobj)) {
251 				_rtld_error("%s: No space available for static "
252 				    "Thread Local Storage", obj->path);
253 				return (-1);
254 			}
255 		}
256 
257 		*(Elf_Addr **)where = *where * sizeof(Elf_Addr)
258 		    + (Elf_Addr *)(def->st_value + rela->r_addend
259 		    + defobj->tlsoffset - TLS_TP_OFFSET);
260 
261 		break;
262 
263 	case R_PPC_DTPREL32:
264 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
265 		    flags, cache, lockstate);
266 
267 		if (def == NULL)
268 			return (-1);
269 
270 		*where += (Elf_Addr)(def->st_value + rela->r_addend
271 		    - TLS_DTV_OFFSET);
272 
273 		break;
274 
275 	default:
276 		_rtld_error("%s: Unsupported relocation type %d"
277 			    " in non-PLT relocations\n", obj->path,
278 			    ELF_R_TYPE(rela->r_info));
279 		return (-1);
280         }
281 	return (0);
282 }
283 
284 
285 /*
286  * Process non-PLT relocations
287  */
288 int
289 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
290     RtldLockState *lockstate)
291 {
292 	const Elf_Rela *relalim;
293 	const Elf_Rela *rela;
294 	SymCache *cache;
295 	int r = -1;
296 
297 	/*
298 	 * The dynamic loader may be called from a thread, we have
299 	 * limited amounts of stack available so we cannot use alloca().
300 	 */
301 	if (obj != obj_rtld) {
302 		cache = calloc(obj->dynsymcount, sizeof(SymCache));
303 		/* No need to check for NULL here */
304 	} else
305 		cache = NULL;
306 
307 	/*
308 	 * From the SVR4 PPC ABI:
309 	 * "The PowerPC family uses only the Elf32_Rela relocation
310 	 *  entries with explicit addends."
311 	 */
312 	relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
313 	for (rela = obj->rela; rela < relalim; rela++) {
314 		if (reloc_nonplt_object(obj_rtld, obj, rela, cache, flags,
315 		    lockstate) < 0)
316 			goto done;
317 	}
318 	r = 0;
319 done:
320 	if (cache != NULL)
321 		free(cache);
322 
323 	/* Synchronize icache for text seg in case we made any changes */
324 	__syncicache(obj->mapbase, obj->textsize);
325 
326 	return (r);
327 }
328 
329 /*
330  * Initialise a PLT slot to the resolving trampoline
331  */
332 static int
333 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
334 {
335 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
336 	Elf_Addr *pltresolve, *pltlongresolve, *jmptab;
337 	Elf_Addr distance;
338 	int N = obj->pltrelasize / sizeof(Elf_Rela);
339 	int reloff;
340 
341 	reloff = rela - obj->pltrela;
342 
343 	if (reloff < 0)
344 		return (-1);
345 
346 	pltlongresolve = obj->pltgot + 5;
347 	pltresolve = pltlongresolve + 5;
348 
349 	distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
350 
351 	dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
352 	    (void *)where, (void *)pltresolve, reloff, distance);
353 
354 	if (reloff < PLT_EXTENDED_BEGIN) {
355 		/* li   r11,reloff  */
356 		/* b    pltresolve  */
357 		where[0] = 0x39600000 | reloff;
358 		where[1] = 0x48000000 | (distance & 0x03fffffc);
359 	} else {
360 		jmptab = obj->pltgot + JMPTAB_BASE(N);
361 		jmptab[reloff] = (u_int)pltlongresolve;
362 
363 		/* lis	r11,jmptab[reloff]@ha */
364 		/* lwzu	r12,jmptab[reloff]@l(r11) */
365 		/* mtctr r12 */
366 		/* bctr */
367 		where[0] = 0x3d600000 | _ppc_ha(&jmptab[reloff]);
368 		where[1] = 0x858b0000 | _ppc_la(&jmptab[reloff]);
369 		where[2] = 0x7d8903a6;
370 		where[3] = 0x4e800420;
371 	}
372 
373 
374 	/*
375 	 * The icache will be sync'd in reloc_plt, which is called
376 	 * after all the slots have been updated
377 	 */
378 
379 	return (0);
380 }
381 
382 
383 /*
384  * Process the PLT relocations.
385  */
386 int
387 reloc_plt(Obj_Entry *obj)
388 {
389 	const Elf_Rela *relalim;
390 	const Elf_Rela *rela;
391 	int N = obj->pltrelasize / sizeof(Elf_Rela);
392 
393 	if (obj->pltrelasize != 0) {
394 
395 		relalim = (const Elf_Rela *)((char *)obj->pltrela +
396 		    obj->pltrelasize);
397 		for (rela = obj->pltrela;  rela < relalim;  rela++) {
398 			assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
399 
400 			if (reloc_plt_object(obj, rela) < 0) {
401 				return (-1);
402 			}
403 		}
404 	}
405 
406 	/*
407 	 * Sync the icache for the byte range represented by the
408 	 * trampoline routines and call slots.
409 	 */
410 	if (obj->pltgot != NULL)
411 		__syncicache(obj->pltgot, JMPTAB_BASE(N)*4);
412 
413 	return (0);
414 }
415 
416 
417 /*
418  * LD_BIND_NOW was set - force relocation for all jump slots
419  */
420 int
421 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
422 {
423 	const Obj_Entry *defobj;
424 	const Elf_Rela *relalim;
425 	const Elf_Rela *rela;
426 	const Elf_Sym *def;
427 	Elf_Addr *where;
428 	Elf_Addr target;
429 
430 	relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
431 	for (rela = obj->pltrela; rela < relalim; rela++) {
432 		assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
433 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
434 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
435 		    SYMLOOK_IN_PLT | flags, NULL, lockstate);
436 		if (def == NULL) {
437 			dbg("reloc_jmpslots: sym not found");
438 			return (-1);
439 		}
440 
441 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
442 
443 #if 0
444 		/* PG XXX */
445 		dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
446 		    defobj->strtab + def->st_name, basename(obj->path),
447 		    (void *)target, basename(defobj->path));
448 #endif
449 
450 		reloc_jmpslot(where, target, defobj, obj,
451 		    (const Elf_Rel *) rela);
452 	}
453 
454 	obj->jmpslots_done = true;
455 
456 	return (0);
457 }
458 
459 
460 /*
461  * Update the value of a PLT jump slot. Branch directly to the target if
462  * it is within +/- 32Mb, otherwise go indirectly via the pltcall
463  * trampoline call and jump table.
464  */
465 Elf_Addr
466 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
467 	      const Obj_Entry *obj, const Elf_Rel *rel)
468 {
469 	Elf_Addr offset;
470 	const Elf_Rela *rela = (const Elf_Rela *) rel;
471 
472 	dbg(" reloc_jmpslot: where=%p, target=%p",
473 	    (void *)wherep, (void *)target);
474 
475 	/*
476 	 * At the PLT entry pointed at by `wherep', construct
477 	 * a direct transfer to the now fully resolved function
478 	 * address.
479 	 */
480 	offset = target - (Elf_Addr)wherep;
481 
482 	if (abs(offset) < 32*1024*1024) {     /* inside 32MB? */
483 		/* b    value   # branch directly */
484 		*wherep = 0x48000000 | (offset & 0x03fffffc);
485 		__syncicache(wherep, 4);
486 	} else {
487 		Elf_Addr *pltcall, *jmptab;
488 		int distance;
489 		int N = obj->pltrelasize / sizeof(Elf_Rela);
490 		int reloff = rela - obj->pltrela;
491 
492 		if (reloff < 0)
493 			return (-1);
494 
495 		pltcall = obj->pltgot;
496 
497 		dbg(" reloc_jmpslot: indir, reloff=%x, N=%x\n",
498 		    reloff, N);
499 
500 		jmptab = obj->pltgot + JMPTAB_BASE(N);
501 		jmptab[reloff] = target;
502 		mb(); /* Order jmptab update before next changes */
503 
504 		if (reloff < PLT_EXTENDED_BEGIN) {
505 			/* for extended PLT entries, we keep the old code */
506 
507 			distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
508 
509 			/* li   r11,reloff */
510 			/* b    pltcall  # use indirect pltcall routine */
511 
512 			/* first instruction same as before */
513 			wherep[1] = 0x48000000 | (distance & 0x03fffffc);
514 			__syncicache(wherep, 8);
515 		}
516 	}
517 
518 	return (target);
519 }
520 
521 int
522 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
523 {
524 
525 	/* XXX not implemented */
526 	return (0);
527 }
528 
529 int
530 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
531     struct Struct_RtldLockState *lockstate)
532 {
533 
534 	/* XXX not implemented */
535 	return (0);
536 }
537 
538 /*
539  * Setup the plt glue routines.
540  */
541 #define PLTCALL_SIZE	   	20
542 #define PLTLONGRESOLVE_SIZE	20
543 #define PLTRESOLVE_SIZE		24
544 
545 void
546 init_pltgot(Obj_Entry *obj)
547 {
548 	Elf_Word *pltcall, *pltresolve, *pltlongresolve;
549 	Elf_Word *jmptab;
550 	int N = obj->pltrelasize / sizeof(Elf_Rela);
551 
552 	pltcall = obj->pltgot;
553 
554 	if (pltcall == NULL) {
555 		return;
556 	}
557 
558 	/*
559 	 * From the SVR4 PPC ABI:
560 	 *
561 	 * 'The first 18 words (72 bytes) of the PLT are reserved for
562 	 * use by the dynamic linker.
563 	 *   ...
564 	 * 'If the executable or shared object requires N procedure
565 	 *  linkage table entries, the link editor shall reserve 3*N
566 	 *  words (12*N bytes) following the 18 reserved words. The
567 	 *  first 2*N of these words are the procedure linkage table
568 	 *  entries themselves. The static linker directs calls to bytes
569 	 *  (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
570 	 *  N words (4*N bytes) are reserved for use by the dynamic linker.'
571 	 */
572 
573 	/*
574 	 * Copy the absolute-call assembler stub into the first part of
575 	 * the reserved PLT area.
576 	 */
577 	memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
578 
579 	/*
580 	 * Determine the address of the jumptable, which is the dyn-linker
581 	 * reserved area after the call cells. Write the absolute address
582 	 * of the jumptable into the absolute-call assembler code so it
583 	 * can determine this address.
584 	 */
585 	jmptab = obj->pltgot + JMPTAB_BASE(N);
586 	pltcall[1] |= _ppc_ha(jmptab);	   /* addis 11,11,jmptab@ha */
587 	pltcall[2] |= _ppc_la(jmptab);     /* lwz   11,jmptab@l(11) */
588 
589 	/*
590 	 * Skip down 20 bytes into the initial reserved area and copy
591 	 * in the standard resolving assembler call. Into this assembler,
592 	 * insert the absolute address of the _rtld_bind_start routine
593 	 * and the address of the relocation object.
594 	 *
595 	 * We place pltlongresolve first, so it can fix up its arguments
596 	 * and then fall through to the regular PLT resolver.
597 	 */
598 	pltlongresolve = obj->pltgot + 5;
599 
600 	memcpy(pltlongresolve, _rtld_powerpc_pltlongresolve,
601 	    PLTLONGRESOLVE_SIZE);
602 	pltlongresolve[0] |= _ppc_ha(jmptab);	/* lis	12,jmptab@ha	*/
603 	pltlongresolve[1] |= _ppc_la(jmptab);	/* addi	12,12,jmptab@l	*/
604 
605 	pltresolve = pltlongresolve + PLTLONGRESOLVE_SIZE/sizeof(uint32_t);
606 	memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
607 	pltresolve[0] |= _ppc_ha(_rtld_bind_start);
608 	pltresolve[1] |= _ppc_la(_rtld_bind_start);
609 	pltresolve[3] |= _ppc_ha(obj);
610 	pltresolve[4] |= _ppc_la(obj);
611 
612 	/*
613 	 * The icache will be sync'd in reloc_plt, which is called
614 	 * after all the slots have been updated
615 	 */
616 }
617 
618 void
619 allocate_initial_tls(Obj_Entry *list)
620 {
621 	register Elf_Addr **tp __asm__("r2");
622 	Elf_Addr **_tp;
623 
624 	/*
625 	* Fix the size of the static TLS block by using the maximum
626 	* offset allocated so far and adding a bit for dynamic modules to
627 	* use.
628 	*/
629 
630 	tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
631 
632 	_tp = (Elf_Addr **) ((char *) allocate_tls(list, NULL, TLS_TCB_SIZE, 8)
633 	    + TLS_TP_OFFSET + TLS_TCB_SIZE);
634 
635 	/*
636 	 * XXX gcc seems to ignore 'tp = _tp;'
637 	 */
638 
639 	__asm __volatile("mr %0,%1" : "=r"(tp) : "r"(_tp));
640 }
641 
642 void*
643 __tls_get_addr(tls_index* ti)
644 {
645 	register Elf_Addr **tp __asm__("r2");
646 	char *p;
647 
648 	p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
649 	    - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
650 
651 	return (p + TLS_DTV_OFFSET);
652 }
653