fs/mfs: Remove a few assert.h includes
[minix3.git] / libexec / ld.elf_so / arch / powerpc / ppc_reloc.c
blob2c286c4df5c95a3d074d9dea6968cc7effa36c81
1 /* $NetBSD: ppc_reloc.c,v 1.53 2014/08/25 20:40:52 joerg Exp $ */
3 /*-
4 * Copyright (C) 1998 Tsubai Masanari
5 * Portions copyright 2002 Charles M. Hannum <root@ihack.net>
6 * All rights reserved.
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.
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.
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: ppc_reloc.c,v 1.53 2014/08/25 20:40:52 joerg Exp $");
34 #endif /* not lint */
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>
43 #include "debug.h"
44 #include "rtld.h"
46 void _rtld_powerpc_pltcall(Elf_Word);
47 void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
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)
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 *);
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).
88 void
89 _rtld_setup_pltgot(const Obj_Entry *obj)
91 #ifdef _LP64
93 * For powerpc64, just copy the function descriptor to pltgot[0].
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;
100 #else
102 * Secure-PLT is much more sane.
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 {
112 * Setup the plt glue routines (for bss-plt).
114 #define BSSPLTCALL_SIZE 20
115 #define BSSPLTRESOLVE_SIZE 24
117 Elf_Word *pltcall, *pltresolve;
118 Elf_Word *jmptab;
119 int N = obj->pltrelalim - obj->pltrela;
121 /* Entries beyond 8192 take twice as much space. */
122 if (N > 8192)
123 N += N-8192;
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));
129 pltcall = obj->pltgot;
130 jmptab = pltcall + 18 + N * 2;
132 memcpy(pltcall, _rtld_powerpc_pltcall, BSSPLTCALL_SIZE);
133 pltcall[1] |= ha(jmptab);
134 pltcall[2] |= lo(jmptab);
136 pltresolve = obj->pltgot + 8;
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);
145 * Invalidate the icache for only the code part of the PLT
146 * (and not the jump table at the end).
148 __syncicache(pltcall, (char *)jmptab - (char *)pltcall);
150 #endif
153 void
154 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
156 const Elf_Rela *rela = 0, *relalim;
157 Elf_Addr relasz = 0;
158 Elf_Addr *where;
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;
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);
178 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
180 const Elf_Rela *rela;
182 for (rela = obj->rela; rela < obj->relalim; rela++) {
183 Elf_Addr *where;
184 const Elf_Sym *def;
185 const Obj_Entry *defobj;
186 Elf_Addr tmp;
187 unsigned long symnum;
189 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
190 symnum = ELF_R_SYM(rela->r_info);
192 switch (ELF_R_TYPE(rela->r_info)) {
193 #if 1 /* XXX Should not be necessary. */
194 case R_TYPE(JMP_SLOT):
195 #endif
196 case R_TYPE(NONE):
197 break;
199 #ifdef _LP64
200 case R_TYPE(ADDR64): /* <address> S + A */
201 #else
202 case R_TYPE(ADDR32): /* <address> S + A */
203 #endif
204 case R_TYPE(GLOB_DAT): /* <address> S + A */
205 def = _rtld_find_symdef(symnum, obj, &defobj, false);
206 if (def == NULL)
207 return -1;
209 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
210 rela->r_addend);
211 if (*where != tmp)
212 *where = tmp;
213 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
214 obj->strtab + obj->symtab[symnum].st_name,
215 obj->path, (void *)*where, defobj->path));
216 break;
218 case R_TYPE(RELATIVE): /* <address> B + A */
219 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
220 rdbg(("RELATIVE in %s --> %p", obj->path,
221 (void *)*where));
222 break;
224 case R_TYPE(COPY):
226 * These are deferred until all other relocations have
227 * been done. All we do here is make sure that the
228 * COPY relocation is not in a shared library. They
229 * are allowed only in executable files.
231 if (obj->isdynamic) {
232 _rtld_error(
233 "%s: Unexpected R_COPY relocation in shared library",
234 obj->path);
235 return -1;
237 rdbg(("COPY (avoid in main)"));
238 break;
240 case R_TYPE(DTPMOD):
241 def = _rtld_find_symdef(symnum, obj, &defobj, false);
242 if (def == NULL)
243 return -1;
245 *where = (Elf_Addr)defobj->tlsindex;
246 rdbg(("DTPMOD32 %s in %s --> %p in %s",
247 obj->strtab + obj->symtab[symnum].st_name,
248 obj->path, (void *)*where, defobj->path));
249 break;
251 case R_TYPE(DTPREL):
252 def = _rtld_find_symdef(symnum, obj, &defobj, false);
253 if (def == NULL)
254 return -1;
256 if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
257 return -1;
259 *where = (Elf_Addr)(def->st_value + rela->r_addend
260 - TLS_DTV_OFFSET);
261 rdbg(("DTPREL32 %s in %s --> %p in %s",
262 obj->strtab + obj->symtab[symnum].st_name,
263 obj->path, (void *)*where, defobj->path));
264 break;
266 case R_TYPE(TPREL):
267 def = _rtld_find_symdef(symnum, obj, &defobj, false);
268 if (def == NULL)
269 return -1;
271 if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
272 return -1;
274 *where = (Elf_Addr)(def->st_value + rela->r_addend
275 + defobj->tlsoffset - TLS_TP_OFFSET);
276 rdbg(("TPREL32 %s in %s --> %p in %s",
277 obj->strtab + obj->symtab[symnum].st_name,
278 obj->path, (void *)*where, defobj->path));
279 break;
281 default:
282 rdbg(("sym = %lu, type = %lu, offset = %p, "
283 "addend = %p, contents = %p, symbol = %s",
284 symnum, (u_long)ELF_R_TYPE(rela->r_info),
285 (void *)rela->r_offset, (void *)rela->r_addend,
286 (void *)*where,
287 obj->strtab + obj->symtab[symnum].st_name));
288 _rtld_error("%s: Unsupported relocation type %ld "
289 "in non-PLT relocations",
290 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
291 return -1;
294 return 0;
298 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
300 #ifdef _LP64
302 * For PowerPC64, the plt stubs handle an empty function descriptor
303 * so there's nothing to do.
305 #else
306 Elf_Addr * const pltresolve = obj->pltgot + 8;
307 const Elf_Rela *rela;
308 int reloff;
310 for (rela = obj->pltrela, reloff = 0;
311 rela < obj->pltrelalim;
312 rela++, reloff++) {
313 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
315 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
317 if (obj->gotptr != NULL) {
319 * For now, simply treat then as relative.
321 *where += (Elf_Addr)obj->relocbase;
322 } else {
323 int distance;
325 if (reloff < 32768) {
326 /* li r11,reloff */
327 *where++ = 0x39600000 | reloff;
328 } else {
329 /* lis r11,ha(reloff) */
330 /* addi r11,lo(reloff) */
331 *where++ = 0x3d600000 | ha(reloff);
332 *where++ = 0x396b0000 | lo(reloff);
334 /* b pltresolve */
335 distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
336 *where++ = 0x48000000 | (distance & 0x03fffffc);
339 * Icache invalidation is not done for each entry here
340 * because we sync the entire code part of the PLT once
341 * in _rtld_setup_pltgot() after all the entries have been
342 * initialized.
344 /* __syncicache(where - 3, 12); */
347 #endif /* !_LP64 */
349 return 0;
352 static int
353 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
355 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
356 Elf_Addr value;
357 const Elf_Sym *def;
358 const Obj_Entry *defobj;
359 unsigned long info = rela->r_info;
361 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
363 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
364 if (__predict_false(def == NULL))
365 return -1;
366 if (__predict_false(def == &_rtld_sym_zero))
367 return 0;
369 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
370 if (tp == NULL)
371 return 0;
372 value = _rtld_resolve_ifunc(defobj, def);
373 } else {
374 value = (Elf_Addr)(defobj->relocbase + def->st_value);
376 rdbg(("bind now/fixup in %s --> new=%p",
377 defobj->strtab + def->st_name, (void *)value));
379 #ifdef _LP64
381 * For PowerPC64 we simply replace the function descriptor in the
382 * PLTGOT with the one from source object.
384 assert(where >= (Elf_Word *)obj->pltgot);
385 assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
386 const Elf_Addr * const fdesc = (Elf_Addr *) value;
387 where[0] = fdesc[0];
388 where[1] = fdesc[1];
389 where[2] = fdesc[2];
390 #else
391 ptrdiff_t distance = value - (Elf_Addr)where;
392 if (obj->gotptr != NULL) {
394 * For Secure-PLT we simply replace the entry in GOT with the
395 * address of the routine.
397 assert(where >= (Elf_Word *)obj->pltgot);
398 assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
399 *where = value;
400 } else if (labs(distance) < 32*1024*1024) { /* inside 32MB? */
401 /* b value # branch directly */
402 *where = 0x48000000 | (distance & 0x03fffffc);
403 __syncicache(where, 4);
404 } else {
405 Elf_Addr *pltcall, *jmptab;
406 int N = obj->pltrelalim - obj->pltrela;
408 /* Entries beyond 8192 take twice as much space. */
409 if (N > 8192)
410 N += N-8192;
412 pltcall = obj->pltgot;
413 jmptab = pltcall + 18 + N * 2;
415 jmptab[reloff] = value;
417 if (reloff < 32768) {
418 /* li r11,reloff */
419 *where++ = 0x39600000 | reloff;
420 } else {
421 #ifdef notyet
422 /* lis r11,ha(value) */
423 /* addi r11,lo(value) */
424 /* mtctr r11 */
425 /* bctr */
426 *where++ = 0x3d600000 | ha(value);
427 *where++ = 0x396b0000 | lo(value);
428 *where++ = 0x7d6903a6;
429 *where++ = 0x4e800420;
430 #else
431 /* lis r11,ha(reloff) */
432 /* addi r11,lo(reloff) */
433 *where++ = 0x3d600000 | ha(reloff);
434 *where++ = 0x396b0000 | lo(reloff);
435 #endif
437 /* b pltcall */
438 distance = (Elf_Addr)pltcall - (Elf_Addr)where;
439 *where++ = 0x48000000 | (distance & 0x03fffffc);
440 __syncicache(where - 3, 12);
442 #endif /* _LP64 */
444 if (tp)
445 *tp = value;
446 return 0;
449 Elf_Addr
450 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
452 const Elf_Rela *rela = obj->pltrela + reloff;
453 Elf_Addr new_value;
454 int err;
456 new_value = 0; /* XXX gcc */
458 _rtld_shared_enter();
459 err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
460 if (err)
461 _rtld_die();
462 _rtld_shared_exit();
464 #ifdef _LP64
465 return obj->glink;
466 #else
467 return new_value;
468 #endif
472 _rtld_relocate_plt_objects(const Obj_Entry *obj)
474 const Elf_Rela *rela;
475 int reloff;
477 for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
478 if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
479 return -1;
481 return 0;