xref: /freebsd/libexec/rtld-elf/powerpc64/reloc.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
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/md_var.h>
42 
43 #include "debug.h"
44 #include "rtld.h"
45 
46 struct funcdesc {
47 	Elf_Addr addr;
48 	Elf_Addr toc;
49 	Elf_Addr env;
50 };
51 
52 /*
53  * Process the R_PPC_COPY relocations
54  */
55 int
56 do_copy_relocations(Obj_Entry *dstobj)
57 {
58 	const Elf_Rela *relalim;
59 	const Elf_Rela *rela;
60 
61 	/*
62 	 * COPY relocs are invalid outside of the main program
63 	 */
64 	assert(dstobj->mainprog);
65 
66 	relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
67 	    dstobj->relasize);
68 	for (rela = dstobj->rela;  rela < relalim;  rela++) {
69 		void *dstaddr;
70 		const Elf_Sym *dstsym;
71 		const char *name;
72 		size_t size;
73 		const void *srcaddr;
74 		const Elf_Sym *srcsym = NULL;
75 		const Obj_Entry *srcobj, *defobj;
76 		SymLook req;
77 		int res;
78 
79 		if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
80 			continue;
81 		}
82 
83 		dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
84 		dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
85 		name = dstobj->strtab + dstsym->st_name;
86 		size = dstsym->st_size;
87 		symlook_init(&req, name);
88 		req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
89 		req.flags = SYMLOOK_EARLY;
90 
91 		for (srcobj = dstobj->next;  srcobj != NULL;
92 		     srcobj = srcobj->next) {
93 			res = symlook_obj(&req, srcobj);
94 			if (res == 0) {
95 				srcsym = req.sym_out;
96 				defobj = req.defobj_out;
97 				break;
98 			}
99 		}
100 
101 		if (srcobj == NULL) {
102 			_rtld_error("Undefined symbol \"%s\" "
103 				    " referenced from COPY"
104 				    " relocation in %s", name, dstobj->path);
105 			return (-1);
106 		}
107 
108 		srcaddr = (const void *) (defobj->relocbase+srcsym->st_value);
109 		memcpy(dstaddr, srcaddr, size);
110 		dbg("copy_reloc: src=%p,dst=%p,size=%zd\n",srcaddr,dstaddr,size);
111 	}
112 
113 	return (0);
114 }
115 
116 
117 /*
118  * Perform early relocation of the run-time linker image
119  */
120 void
121 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
122 {
123 	const Elf_Rela *rela = 0, *relalim;
124 	Elf_Addr relasz = 0;
125 	Elf_Addr *where;
126 
127 	/*
128 	 * Extract the rela/relasz values from the dynamic section
129 	 */
130 	for (; dynp->d_tag != DT_NULL; dynp++) {
131 		switch (dynp->d_tag) {
132 		case DT_RELA:
133 			rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
134 			break;
135 		case DT_RELASZ:
136 			relasz = dynp->d_un.d_val;
137 			break;
138 		}
139 	}
140 
141 	/*
142 	 * Relocate these values
143 	 */
144 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
145 	for (; rela < relalim; rela++) {
146 		where = (Elf_Addr *)(relocbase + rela->r_offset);
147 		*where = (Elf_Addr)(relocbase + rela->r_addend);
148 	}
149 }
150 
151 
152 /*
153  * Relocate a non-PLT object with addend.
154  */
155 static int
156 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
157     SymCache *cache, int flags, RtldLockState *lockstate)
158 {
159 	Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
160 	const Elf_Sym   *def;
161 	const Obj_Entry *defobj;
162 	Elf_Addr         tmp;
163 
164 	switch (ELF_R_TYPE(rela->r_info)) {
165 
166 	case R_PPC_NONE:
167 		break;
168 
169         case R_PPC64_UADDR64:    /* doubleword64 S + A */
170         case R_PPC64_ADDR64:
171         case R_PPC_GLOB_DAT:
172 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
173 		    flags, cache, lockstate);
174 		if (def == NULL) {
175 			return (-1);
176 		}
177 
178                 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
179                     rela->r_addend);
180 
181 		/* Don't issue write if unnecessary; avoid COW page fault */
182                 if (*where != tmp) {
183                         *where = tmp;
184 		}
185                 break;
186 
187         case R_PPC_RELATIVE:  /* doubleword64 B + A */
188 		tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
189 
190 		/* As above, don't issue write unnecessarily */
191 		if (*where != tmp) {
192 			*where = tmp;
193 		}
194 		break;
195 
196 	case R_PPC_COPY:
197 		/*
198 		 * These are deferred until all other relocations
199 		 * have been done.  All we do here is make sure
200 		 * that the COPY relocation is not in a shared
201 		 * library.  They are allowed only in executable
202 		 * files.
203 		 */
204 		if (!obj->mainprog) {
205 			_rtld_error("%s: Unexpected R_COPY "
206 				    " relocation in shared library",
207 				    obj->path);
208 			return (-1);
209 		}
210 		break;
211 
212 	case R_PPC_JMP_SLOT:
213 		/*
214 		 * These will be handled by the plt/jmpslot routines
215 		 */
216 		break;
217 
218 	case R_PPC64_DTPMOD64:
219 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
220 		    flags, cache, lockstate);
221 
222 		if (def == NULL)
223 			return (-1);
224 
225 		*where = (Elf_Addr) defobj->tlsindex;
226 
227 		break;
228 
229 	case R_PPC64_TPREL64:
230 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
231 		    flags, cache, lockstate);
232 
233 		if (def == NULL)
234 			return (-1);
235 
236 		/*
237 		 * We lazily allocate offsets for static TLS as we
238 		 * see the first relocation that references the
239 		 * TLS block. This allows us to support (small
240 		 * amounts of) static TLS in dynamically loaded
241 		 * modules. If we run out of space, we generate an
242 		 * error.
243 		 */
244 		if (!defobj->tls_done) {
245 			if (!allocate_tls_offset((Obj_Entry*) defobj)) {
246 				_rtld_error("%s: No space available for static "
247 				    "Thread Local Storage", obj->path);
248 				return (-1);
249 			}
250 		}
251 
252 		*(Elf_Addr **)where = *where * sizeof(Elf_Addr)
253 		    + (Elf_Addr *)(def->st_value + rela->r_addend
254 		    + defobj->tlsoffset - TLS_TP_OFFSET);
255 
256 		break;
257 
258 	case R_PPC64_DTPREL64:
259 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
260 		    flags, cache, lockstate);
261 
262 		if (def == NULL)
263 			return (-1);
264 
265 		*where += (Elf_Addr)(def->st_value + rela->r_addend
266 		    - TLS_DTV_OFFSET);
267 
268 		break;
269 
270 	default:
271 		_rtld_error("%s: Unsupported relocation type %ld"
272 			    " in non-PLT relocations\n", obj->path,
273 			    ELF_R_TYPE(rela->r_info));
274 		return (-1);
275         }
276 	return (0);
277 }
278 
279 
280 /*
281  * Process non-PLT relocations
282  */
283 int
284 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
285     RtldLockState *lockstate)
286 {
287 	const Elf_Rela *relalim;
288 	const Elf_Rela *rela;
289 	SymCache *cache;
290 	int bytes = obj->dynsymcount * sizeof(SymCache);
291 	int r = -1;
292 
293 	/*
294 	 * The dynamic loader may be called from a thread, we have
295 	 * limited amounts of stack available so we cannot use alloca().
296 	 */
297 	if (obj != obj_rtld) {
298 		cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON,
299 		    -1, 0);
300 		if (cache == MAP_FAILED)
301 			cache = NULL;
302 	} else
303 		cache = NULL;
304 
305 	/*
306 	 * From the SVR4 PPC ABI:
307 	 * "The PowerPC family uses only the Elf32_Rela relocation
308 	 *  entries with explicit addends."
309 	 */
310 	relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
311 	for (rela = obj->rela; rela < relalim; rela++) {
312 		if (reloc_nonplt_object(obj_rtld, obj, rela, cache, flags,
313 		    lockstate) < 0)
314 			goto done;
315 	}
316 	r = 0;
317 done:
318 	if (cache)
319 		munmap(cache, bytes);
320 
321 	/* Synchronize icache for text seg in case we made any changes */
322 	__syncicache(obj->mapbase, obj->textsize);
323 
324 	return (r);
325 }
326 
327 
328 /*
329  * Initialise a PLT slot to the resolving trampoline
330  */
331 static int
332 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
333 {
334 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
335 	Elf_Addr *glink;
336 	long reloff;
337 
338 	reloff = rela - obj->pltrela;
339 
340 	if (obj->priv == NULL)
341 		obj->priv = xmalloc(obj->pltrelasize);
342 	glink = obj->priv + reloff*sizeof(Elf_Addr)*2;
343 
344 	dbg(" reloc_plt_object: where=%p,reloff=%lx,glink=%p", (void *)where, reloff, glink);
345 
346 	memcpy(where, _rtld_bind_start, sizeof(struct funcdesc));
347 	((struct funcdesc *)(where))->env = (Elf_Addr)glink;
348 	*(glink++) = (Elf_Addr)obj;
349 	*(glink++) = reloff*sizeof(Elf_Rela);
350 
351 	return (0);
352 }
353 
354 
355 /*
356  * Process the PLT relocations.
357  */
358 int
359 reloc_plt(Obj_Entry *obj)
360 {
361 	const Elf_Rela *relalim;
362 	const Elf_Rela *rela;
363 
364 	if (obj->pltrelasize != 0) {
365 		relalim = (const Elf_Rela *)((char *)obj->pltrela +
366 		    obj->pltrelasize);
367 		for (rela = obj->pltrela;  rela < relalim;  rela++) {
368 			assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
369 
370 			if (reloc_plt_object(obj, rela) < 0) {
371 				return (-1);
372 			}
373 		}
374 	}
375 
376 	return (0);
377 }
378 
379 
380 /*
381  * LD_BIND_NOW was set - force relocation for all jump slots
382  */
383 int
384 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
385 {
386 	const Obj_Entry *defobj;
387 	const Elf_Rela *relalim;
388 	const Elf_Rela *rela;
389 	const Elf_Sym *def;
390 	Elf_Addr *where;
391 	Elf_Addr target;
392 
393 	relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
394 	for (rela = obj->pltrela; rela < relalim; rela++) {
395 		assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
396 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
397 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
398 		    SYMLOOK_IN_PLT | flags, NULL, lockstate);
399 		if (def == NULL) {
400 			dbg("reloc_jmpslots: sym not found");
401 			return (-1);
402 		}
403 
404 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
405 
406 #if 0
407 		/* PG XXX */
408 		dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
409 		    defobj->strtab + def->st_name, basename(obj->path),
410 		    (void *)target, basename(defobj->path));
411 #endif
412 
413 		if (def == &sym_zero) {
414 			/* Zero undefined weak symbols */
415 			bzero(where, sizeof(struct funcdesc));
416 		} else {
417 			reloc_jmpslot(where, target, defobj, obj,
418 			    (const Elf_Rel *) rela);
419 		}
420 	}
421 
422 	obj->jmpslots_done = true;
423 
424 	return (0);
425 }
426 
427 
428 /*
429  * Update the value of a PLT jump slot.
430  */
431 Elf_Addr
432 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
433 	      const Obj_Entry *obj, const Elf_Rel *rel)
434 {
435 	dbg(" reloc_jmpslot: where=%p, target=%p (%#lx + %#lx)",
436 	    (void *)wherep, (void *)target, *(Elf_Addr *)target,
437 	    (Elf_Addr)defobj->relocbase);
438 
439 	/*
440 	 * At the PLT entry pointed at by `wherep', construct
441 	 * a direct transfer to the now fully resolved function
442 	 * address.
443 	 */
444 
445 	memcpy(wherep, (void *)target, sizeof(struct funcdesc));
446 	if (((struct funcdesc *)(wherep))->addr < (Elf_Addr)defobj->relocbase) {
447 		/*
448 		 * XXX: It is possible (e.g. LD_BIND_NOW) that the function
449 		 * descriptor we are copying has not yet been relocated.
450 		 * If this happens, fix it.
451 		 */
452 
453 		((struct funcdesc *)(wherep))->addr +=
454 		    (Elf_Addr)defobj->relocbase;
455 		((struct funcdesc *)(wherep))->toc +=
456 		    (Elf_Addr)defobj->relocbase;
457 	}
458 
459 	__asm __volatile("dcbst 0,%0; sync" :: "r"(wherep) : "memory");
460 
461 	return (target);
462 }
463 
464 int
465 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
466 {
467 
468 	/* XXX not implemented */
469 	return (0);
470 }
471 
472 int
473 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
474     struct Struct_RtldLockState *lockstate)
475 {
476 
477 	/* XXX not implemented */
478 	return (0);
479 }
480 
481 void
482 init_pltgot(Obj_Entry *obj)
483 {
484 }
485 
486 void
487 allocate_initial_tls(Obj_Entry *list)
488 {
489 	Elf_Addr **tp;
490 
491 	/*
492 	* Fix the size of the static TLS block by using the maximum
493 	* offset allocated so far and adding a bit for dynamic modules to
494 	* use.
495 	*/
496 
497 	tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
498 
499 	tp = (Elf_Addr **) ((char *)allocate_tls(list, NULL, TLS_TCB_SIZE, 16)
500 	    + TLS_TP_OFFSET + TLS_TCB_SIZE);
501 
502 	__asm __volatile("mr 13,%0" :: "r"(tp));
503 }
504 
505 void*
506 __tls_get_addr(tls_index* ti)
507 {
508 	Elf_Addr **tp;
509 	char *p;
510 
511 	__asm __volatile("mr %0,13" : "=r"(tp));
512 	p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
513 	    - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
514 
515 	return (p + TLS_DTV_OFFSET);
516 }
517