ppc_reloc.c revision 1.49 1 1.49 joerg /* $NetBSD: ppc_reloc.c,v 1.49 2011/03/25 18:07:06 joerg Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*-
4 1.1 tsubai * Copyright (C) 1998 Tsubai Masanari
5 1.32 mycroft * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net>
6 1.1 tsubai * All rights reserved.
7 1.1 tsubai *
8 1.1 tsubai * Redistribution and use in source and binary forms, with or without
9 1.1 tsubai * modification, are permitted provided that the following conditions
10 1.1 tsubai * are met:
11 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
12 1.1 tsubai * notice, this list of conditions and the following disclaimer.
13 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
15 1.1 tsubai * documentation and/or other materials provided with the distribution.
16 1.1 tsubai * 3. The name of the author may not be used to endorse or promote products
17 1.1 tsubai * derived from this software without specific prior written permission.
18 1.1 tsubai *
19 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 tsubai */
30 1.1 tsubai
31 1.37 skrll #include <sys/cdefs.h>
32 1.37 skrll #ifndef lint
33 1.49 joerg __RCSID("$NetBSD: ppc_reloc.c,v 1.49 2011/03/25 18:07:06 joerg Exp $");
34 1.37 skrll #endif /* not lint */
35 1.37 skrll
36 1.1 tsubai #include <stdarg.h>
37 1.1 tsubai #include <stdio.h>
38 1.1 tsubai #include <stdlib.h>
39 1.1 tsubai #include <string.h>
40 1.1 tsubai #include <sys/types.h>
41 1.1 tsubai #include <machine/cpu.h>
42 1.1 tsubai
43 1.1 tsubai #include "debug.h"
44 1.1 tsubai #include "rtld.h"
45 1.1 tsubai
46 1.35 skrll void _rtld_powerpc_pltcall(Elf_Word);
47 1.35 skrll void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
48 1.1 tsubai
49 1.1 tsubai #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
50 1.1 tsubai ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
51 1.1 tsubai #define l(x) ((u_int32_t)(x) & 0xffff)
52 1.1 tsubai
53 1.46 matt void _rtld_bind_bssplt_start(void);
54 1.46 matt void _rtld_bind_secureplt_start(void);
55 1.22 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
56 1.35 skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
57 1.46 matt static int _rtld_relocate_plt_object(const Obj_Entry *,
58 1.36 skrll const Elf_Rela *, int, Elf_Addr *);
59 1.1 tsubai
60 1.1 tsubai /*
61 1.39 chs * The PPC PLT format consists of three sections:
62 1.39 chs * (1) The "pltcall" and "pltresolve" glue code. This is always 18 words.
63 1.39 chs * (2) The code part of the PLT entries. There are 2 words per entry for
64 1.39 chs * up to 8192 entries, then 4 words per entry for any additional entries.
65 1.39 chs * (3) The data part of the PLT entries, comprising a jump table.
66 1.39 chs * This section is half the size of the second section (ie. 1 or 2 words
67 1.39 chs * per entry).
68 1.39 chs */
69 1.39 chs
70 1.39 chs /*
71 1.46 matt * Setup the plt glue routines (for bss-plt).
72 1.1 tsubai */
73 1.1 tsubai #define PLTCALL_SIZE 20
74 1.1 tsubai #define PLTRESOLVE_SIZE 24
75 1.1 tsubai
76 1.1 tsubai void
77 1.35 skrll _rtld_setup_pltgot(const Obj_Entry *obj)
78 1.1 tsubai {
79 1.39 chs /*
80 1.46 matt * Secure-PLT is much more sane.
81 1.39 chs */
82 1.46 matt if (obj->gotptr != NULL) {
83 1.46 matt obj->gotptr[1] = (Elf_Addr) _rtld_bind_secureplt_start;
84 1.46 matt obj->gotptr[2] = (Elf_Addr) obj;
85 1.47 matt dbg(("obj %s secure-plt gotptr=%p start=%p obj=%p",
86 1.47 matt obj->path, obj->gotptr,
87 1.47 matt (void *) obj->gotptr[1], (void *) obj->gotptr[2]));
88 1.46 matt } else {
89 1.46 matt Elf_Word *pltcall, *pltresolve;
90 1.46 matt Elf_Word *jmptab;
91 1.46 matt int N = obj->pltrelalim - obj->pltrela;
92 1.46 matt
93 1.46 matt /* Entries beyond 8192 take twice as much space. */
94 1.46 matt if (N > 8192)
95 1.46 matt N += N-8192;
96 1.46 matt
97 1.47 matt dbg(("obj %s bss-plt pltgot=%p jmptab=%u start=%p obj=%p",
98 1.47 matt obj->path, obj->pltgot, 18 + N * 2,
99 1.47 matt _rtld_bind_bssplt_start, obj));
100 1.47 matt
101 1.46 matt pltcall = obj->pltgot;
102 1.46 matt jmptab = pltcall + 18 + N * 2;
103 1.46 matt
104 1.46 matt memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
105 1.46 matt pltcall[1] |= ha(jmptab);
106 1.46 matt pltcall[2] |= l(jmptab);
107 1.46 matt
108 1.46 matt pltresolve = obj->pltgot + 8;
109 1.46 matt
110 1.46 matt memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
111 1.46 matt pltresolve[0] |= ha(_rtld_bind_bssplt_start);
112 1.46 matt pltresolve[1] |= l(_rtld_bind_bssplt_start);
113 1.46 matt pltresolve[3] |= ha(obj);
114 1.46 matt pltresolve[4] |= l(obj);
115 1.46 matt
116 1.46 matt /*
117 1.46 matt * Invalidate the icache for only the code part of the PLT
118 1.46 matt * (and not the jump table at the end).
119 1.46 matt */
120 1.46 matt __syncicache(pltcall, (char *)jmptab - (char *)pltcall);
121 1.46 matt }
122 1.13 mycroft }
123 1.13 mycroft
124 1.22 mycroft void
125 1.35 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
126 1.22 mycroft {
127 1.22 mycroft const Elf_Rela *rela = 0, *relalim;
128 1.22 mycroft Elf_Addr relasz = 0;
129 1.22 mycroft Elf_Addr *where;
130 1.22 mycroft
131 1.22 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
132 1.22 mycroft switch (dynp->d_tag) {
133 1.22 mycroft case DT_RELA:
134 1.22 mycroft rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
135 1.22 mycroft break;
136 1.22 mycroft case DT_RELASZ:
137 1.22 mycroft relasz = dynp->d_un.d_val;
138 1.22 mycroft break;
139 1.22 mycroft }
140 1.22 mycroft }
141 1.42 he relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
142 1.22 mycroft for (; rela < relalim; rela++) {
143 1.22 mycroft where = (Elf_Addr *)(relocbase + rela->r_offset);
144 1.22 mycroft *where = (Elf_Addr)(relocbase + rela->r_addend);
145 1.22 mycroft }
146 1.22 mycroft }
147 1.22 mycroft
148 1.13 mycroft int
149 1.45 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
150 1.13 mycroft {
151 1.14 mycroft const Elf_Rela *rela;
152 1.22 mycroft
153 1.14 mycroft for (rela = obj->rela; rela < obj->relalim; rela++) {
154 1.14 mycroft Elf_Addr *where;
155 1.14 mycroft const Elf_Sym *def;
156 1.14 mycroft const Obj_Entry *defobj;
157 1.14 mycroft Elf_Addr tmp;
158 1.15 mycroft unsigned long symnum;
159 1.14 mycroft
160 1.14 mycroft where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
161 1.15 mycroft symnum = ELF_R_SYM(rela->r_info);
162 1.13 mycroft
163 1.14 mycroft switch (ELF_R_TYPE(rela->r_info)) {
164 1.26 mycroft #if 1 /* XXX Should not be necessary. */
165 1.26 mycroft case R_TYPE(JMP_SLOT):
166 1.26 mycroft #endif
167 1.14 mycroft case R_TYPE(NONE):
168 1.14 mycroft break;
169 1.14 mycroft
170 1.14 mycroft case R_TYPE(32): /* word32 S + A */
171 1.14 mycroft case R_TYPE(GLOB_DAT): /* word32 S + A */
172 1.15 mycroft def = _rtld_find_symdef(symnum, obj, &defobj, false);
173 1.14 mycroft if (def == NULL)
174 1.14 mycroft return -1;
175 1.14 mycroft
176 1.14 mycroft tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
177 1.14 mycroft rela->r_addend);
178 1.14 mycroft if (*where != tmp)
179 1.14 mycroft *where = tmp;
180 1.24 mycroft rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
181 1.16 mycroft obj->strtab + obj->symtab[symnum].st_name,
182 1.16 mycroft obj->path, (void *)*where, defobj->path));
183 1.14 mycroft break;
184 1.14 mycroft
185 1.14 mycroft case R_TYPE(RELATIVE): /* word32 B + A */
186 1.22 mycroft *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
187 1.24 mycroft rdbg(("RELATIVE in %s --> %p", obj->path,
188 1.14 mycroft (void *)*where));
189 1.14 mycroft break;
190 1.14 mycroft
191 1.14 mycroft case R_TYPE(COPY):
192 1.14 mycroft /*
193 1.14 mycroft * These are deferred until all other relocations have
194 1.14 mycroft * been done. All we do here is make sure that the
195 1.14 mycroft * COPY relocation is not in a shared library. They
196 1.14 mycroft * are allowed only in executable files.
197 1.14 mycroft */
198 1.20 mycroft if (obj->isdynamic) {
199 1.14 mycroft _rtld_error(
200 1.13 mycroft "%s: Unexpected R_COPY relocation in shared library",
201 1.14 mycroft obj->path);
202 1.14 mycroft return -1;
203 1.14 mycroft }
204 1.24 mycroft rdbg(("COPY (avoid in main)"));
205 1.14 mycroft break;
206 1.14 mycroft
207 1.48 matt case R_TYPE(DTPMOD32):
208 1.48 matt def = _rtld_find_symdef(symnum, obj, &defobj, false);
209 1.48 matt if (def == NULL)
210 1.48 matt return -1;
211 1.48 matt
212 1.48 matt *where = (Elf_Addr)defobj->tlsindex;
213 1.48 matt rdbg(("DTPMOD32 %s in %s --> %p in %s",
214 1.48 matt obj->strtab + obj->symtab[symnum].st_name,
215 1.48 matt obj->path, (void *)*where, defobj->path));
216 1.48 matt break;
217 1.48 matt
218 1.48 matt case R_TYPE(DTPREL32):
219 1.48 matt def = _rtld_find_symdef(symnum, obj, &defobj, false);
220 1.48 matt if (def == NULL)
221 1.48 matt return -1;
222 1.48 matt
223 1.48 matt if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
224 1.48 matt return -1;
225 1.48 matt
226 1.48 matt *where = (Elf_Addr)(def->st_value + rela->r_addend
227 1.48 matt - TLS_DTV_OFFSET);
228 1.48 matt rdbg(("DTPREL32 %s in %s --> %p in %s",
229 1.48 matt obj->strtab + obj->symtab[symnum].st_name,
230 1.48 matt obj->path, (void *)*where, defobj->path));
231 1.48 matt break;
232 1.48 matt
233 1.48 matt case R_TYPE(TPREL32):
234 1.48 matt def = _rtld_find_symdef(symnum, obj, &defobj, false);
235 1.48 matt if (def == NULL)
236 1.48 matt return -1;
237 1.48 matt
238 1.48 matt if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
239 1.48 matt return -1;
240 1.48 matt
241 1.48 matt *where = (Elf_Addr)(def->st_value + rela->r_addend
242 1.48 matt + defobj->tlsoffset - TLS_TP_OFFSET);
243 1.48 matt rdbg(("TPREL32 %s in %s --> %p in %s",
244 1.48 matt obj->strtab + obj->symtab[symnum].st_name,
245 1.48 matt obj->path, (void *)*where, defobj->path));
246 1.48 matt break;
247 1.48 matt
248 1.14 mycroft default:
249 1.24 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
250 1.14 mycroft "addend = %p, contents = %p, symbol = %s",
251 1.15 mycroft symnum, (u_long)ELF_R_TYPE(rela->r_info),
252 1.14 mycroft (void *)rela->r_offset, (void *)rela->r_addend,
253 1.14 mycroft (void *)*where,
254 1.15 mycroft obj->strtab + obj->symtab[symnum].st_name));
255 1.14 mycroft _rtld_error("%s: Unsupported relocation type %ld "
256 1.43 jmmv "in non-PLT relocations",
257 1.14 mycroft obj->path, (u_long) ELF_R_TYPE(rela->r_info));
258 1.13 mycroft return -1;
259 1.13 mycroft }
260 1.13 mycroft }
261 1.17 mycroft return 0;
262 1.17 mycroft }
263 1.17 mycroft
264 1.17 mycroft int
265 1.35 skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
266 1.17 mycroft {
267 1.46 matt Elf_Addr * const pltresolve = obj->pltgot + 8;
268 1.17 mycroft const Elf_Rela *rela;
269 1.28 mycroft int reloff;
270 1.17 mycroft
271 1.46 matt for (rela = obj->pltrela, reloff = 0;
272 1.46 matt rela < obj->pltrelalim;
273 1.46 matt rela++, reloff++) {
274 1.17 mycroft Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
275 1.17 mycroft
276 1.17 mycroft assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
277 1.17 mycroft
278 1.46 matt if (obj->gotptr != NULL) {
279 1.46 matt /*
280 1.46 matt * For now, simply treat then as relative.
281 1.46 matt */
282 1.46 matt *where += (Elf_Addr)obj->relocbase;
283 1.46 matt } else {
284 1.46 matt int distance;
285 1.46 matt
286 1.46 matt if (reloff < 32768) {
287 1.46 matt /* li r11,reloff */
288 1.46 matt *where++ = 0x39600000 | reloff;
289 1.46 matt } else {
290 1.46 matt /* lis r11,ha(reloff) */
291 1.46 matt /* addi r11,l(reloff) */
292 1.46 matt *where++ = 0x3d600000 | ha(reloff);
293 1.46 matt *where++ = 0x396b0000 | l(reloff);
294 1.46 matt }
295 1.46 matt /* b pltresolve */
296 1.46 matt distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
297 1.46 matt *where++ = 0x48000000 | (distance & 0x03fffffc);
298 1.17 mycroft
299 1.46 matt /*
300 1.46 matt * Icache invalidation is not done for each entry here
301 1.46 matt * because we sync the entire code part of the PLT once
302 1.46 matt * in _rtld_setup_pltgot() after all the entries have been
303 1.46 matt * initialized.
304 1.46 matt */
305 1.46 matt /* __syncicache(where - 3, 12); */
306 1.28 mycroft }
307 1.17 mycroft }
308 1.17 mycroft
309 1.13 mycroft return 0;
310 1.27 mycroft }
311 1.27 mycroft
312 1.46 matt static int
313 1.36 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
314 1.27 mycroft {
315 1.27 mycroft Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
316 1.27 mycroft Elf_Addr value;
317 1.27 mycroft const Elf_Sym *def;
318 1.27 mycroft const Obj_Entry *defobj;
319 1.27 mycroft int distance;
320 1.44 christos unsigned long info = rela->r_info;
321 1.27 mycroft
322 1.44 christos assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
323 1.27 mycroft
324 1.44 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
325 1.44 christos if (__predict_false(def == NULL))
326 1.36 skrll return -1;
327 1.44 christos if (__predict_false(def == &_rtld_sym_zero))
328 1.44 christos return 0;
329 1.27 mycroft
330 1.27 mycroft value = (Elf_Addr)(defobj->relocbase + def->st_value);
331 1.27 mycroft distance = value - (Elf_Addr)where;
332 1.29 mycroft rdbg(("bind now/fixup in %s --> new=%p",
333 1.29 mycroft defobj->strtab + def->st_name, (void *)value));
334 1.27 mycroft
335 1.46 matt if (obj->gotptr != NULL) {
336 1.46 matt /*
337 1.46 matt * For Secure-PLT we simply replace the entry in GOT with the address
338 1.46 matt * of the routine.
339 1.46 matt */
340 1.46 matt assert(where >= (Elf_Word *)obj->pltgot);
341 1.46 matt assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
342 1.46 matt *where = value;
343 1.46 matt } else if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
344 1.27 mycroft /* b value # branch directly */
345 1.27 mycroft *where = 0x48000000 | (distance & 0x03fffffc);
346 1.27 mycroft __syncicache(where, 4);
347 1.27 mycroft } else {
348 1.27 mycroft Elf_Addr *pltcall, *jmptab;
349 1.27 mycroft int N = obj->pltrelalim - obj->pltrela;
350 1.27 mycroft
351 1.28 mycroft /* Entries beyond 8192 take twice as much space. */
352 1.28 mycroft if (N > 8192)
353 1.28 mycroft N += N-8192;
354 1.28 mycroft
355 1.27 mycroft pltcall = obj->pltgot;
356 1.28 mycroft jmptab = pltcall + 18 + N * 2;
357 1.27 mycroft
358 1.27 mycroft jmptab[reloff] = value;
359 1.27 mycroft
360 1.28 mycroft if (reloff < 32768) {
361 1.28 mycroft /* li r11,reloff */
362 1.28 mycroft *where++ = 0x39600000 | reloff;
363 1.28 mycroft } else {
364 1.46 matt #ifdef notyet
365 1.46 matt /* lis r11,ha(value) */
366 1.46 matt /* addi r11,l(value) */
367 1.46 matt /* mtctr r11 */
368 1.46 matt /* bctr */
369 1.46 matt *where++ = 0x3d600000 | ha(value);
370 1.46 matt *where++ = 0x396b0000 | l(value);
371 1.46 matt *where++ = 0x7d6903a6;
372 1.46 matt *where++ = 0x4e800420;
373 1.46 matt #else
374 1.28 mycroft /* lis r11,ha(reloff) */
375 1.28 mycroft /* addi r11,l(reloff) */
376 1.28 mycroft *where++ = 0x3d600000 | ha(reloff);
377 1.28 mycroft *where++ = 0x396b0000 | l(reloff);
378 1.46 matt #endif
379 1.28 mycroft }
380 1.28 mycroft /* b pltcall */
381 1.28 mycroft distance = (Elf_Addr)pltcall - (Elf_Addr)where;
382 1.28 mycroft *where++ = 0x48000000 | (distance & 0x03fffffc);
383 1.38 chs __syncicache(where - 3, 12);
384 1.27 mycroft }
385 1.27 mycroft
386 1.36 skrll if (tp)
387 1.36 skrll *tp = value;
388 1.36 skrll return 0;
389 1.36 skrll }
390 1.36 skrll
391 1.36 skrll caddr_t
392 1.36 skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
393 1.36 skrll {
394 1.47 matt const Elf_Rela *rela = obj->pltrela + reloff;
395 1.36 skrll Elf_Addr new_value;
396 1.36 skrll int err;
397 1.36 skrll
398 1.40 mrg new_value = 0; /* XXX gcc */
399 1.40 mrg
400 1.49 joerg _rtld_shared_enter();
401 1.36 skrll err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
402 1.44 christos if (err)
403 1.36 skrll _rtld_die();
404 1.49 joerg _rtld_shared_exit();
405 1.36 skrll
406 1.36 skrll return (caddr_t)new_value;
407 1.36 skrll }
408 1.36 skrll
409 1.36 skrll int
410 1.36 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
411 1.36 skrll {
412 1.36 skrll const Elf_Rela *rela;
413 1.36 skrll int reloff;
414 1.36 skrll
415 1.36 skrll for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
416 1.36 skrll if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
417 1.36 skrll return -1;
418 1.36 skrll }
419 1.36 skrll return 0;
420 1.1 tsubai }
421