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