ppc_reloc.c revision 1.44 1 1.44 christos /* $NetBSD: ppc_reloc.c,v 1.44 2010/01/13 20:17:22 christos 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.44 christos __RCSID("$NetBSD: ppc_reloc.c,v 1.44 2010/01/13 20:17:22 christos 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.7 mycroft #include <sys/stat.h>
42 1.1 tsubai #include <machine/cpu.h>
43 1.1 tsubai
44 1.1 tsubai #include "debug.h"
45 1.1 tsubai #include "rtld.h"
46 1.1 tsubai
47 1.35 skrll void _rtld_powerpc_pltcall(Elf_Word);
48 1.35 skrll void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
49 1.1 tsubai
50 1.1 tsubai #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
51 1.1 tsubai ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
52 1.1 tsubai #define l(x) ((u_int32_t)(x) & 0xffff)
53 1.1 tsubai
54 1.23 mycroft void _rtld_bind_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.36 skrll static inline 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.1 tsubai * Setup the plt glue routines.
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.1 tsubai Elf_Word *pltcall, *pltresolve;
80 1.1 tsubai Elf_Word *jmptab;
81 1.1 tsubai int N = obj->pltrelalim - obj->pltrela;
82 1.1 tsubai
83 1.28 mycroft /* Entries beyond 8192 take twice as much space. */
84 1.28 mycroft if (N > 8192)
85 1.28 mycroft N += N-8192;
86 1.28 mycroft
87 1.1 tsubai pltcall = obj->pltgot;
88 1.28 mycroft jmptab = pltcall + 18 + N * 2;
89 1.1 tsubai
90 1.1 tsubai memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
91 1.1 tsubai pltcall[1] |= ha(jmptab);
92 1.1 tsubai pltcall[2] |= l(jmptab);
93 1.1 tsubai
94 1.10 mycroft pltresolve = obj->pltgot + 8;
95 1.1 tsubai
96 1.1 tsubai memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
97 1.1 tsubai pltresolve[0] |= ha(_rtld_bind_start);
98 1.1 tsubai pltresolve[1] |= l(_rtld_bind_start);
99 1.1 tsubai pltresolve[3] |= ha(obj);
100 1.1 tsubai pltresolve[4] |= l(obj);
101 1.1 tsubai
102 1.39 chs /*
103 1.39 chs * Invalidate the icache for only the code part of the PLT
104 1.39 chs * (and not the jump table at the end).
105 1.39 chs */
106 1.39 chs __syncicache(pltcall, (char *)jmptab - (char *)pltcall);
107 1.13 mycroft }
108 1.13 mycroft
109 1.22 mycroft void
110 1.35 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
111 1.22 mycroft {
112 1.22 mycroft const Elf_Rela *rela = 0, *relalim;
113 1.22 mycroft Elf_Addr relasz = 0;
114 1.22 mycroft Elf_Addr *where;
115 1.22 mycroft
116 1.22 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
117 1.22 mycroft switch (dynp->d_tag) {
118 1.22 mycroft case DT_RELA:
119 1.22 mycroft rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
120 1.22 mycroft break;
121 1.22 mycroft case DT_RELASZ:
122 1.22 mycroft relasz = dynp->d_un.d_val;
123 1.22 mycroft break;
124 1.22 mycroft }
125 1.22 mycroft }
126 1.42 he relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
127 1.22 mycroft for (; rela < relalim; rela++) {
128 1.22 mycroft where = (Elf_Addr *)(relocbase + rela->r_offset);
129 1.22 mycroft *where = (Elf_Addr)(relocbase + rela->r_addend);
130 1.22 mycroft }
131 1.22 mycroft }
132 1.22 mycroft
133 1.13 mycroft int
134 1.35 skrll _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
135 1.13 mycroft {
136 1.14 mycroft const Elf_Rela *rela;
137 1.22 mycroft
138 1.14 mycroft for (rela = obj->rela; rela < obj->relalim; rela++) {
139 1.14 mycroft Elf_Addr *where;
140 1.14 mycroft const Elf_Sym *def;
141 1.14 mycroft const Obj_Entry *defobj;
142 1.14 mycroft Elf_Addr tmp;
143 1.15 mycroft unsigned long symnum;
144 1.14 mycroft
145 1.14 mycroft where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
146 1.15 mycroft symnum = ELF_R_SYM(rela->r_info);
147 1.13 mycroft
148 1.14 mycroft switch (ELF_R_TYPE(rela->r_info)) {
149 1.26 mycroft #if 1 /* XXX Should not be necessary. */
150 1.26 mycroft case R_TYPE(JMP_SLOT):
151 1.26 mycroft #endif
152 1.14 mycroft case R_TYPE(NONE):
153 1.14 mycroft break;
154 1.14 mycroft
155 1.14 mycroft case R_TYPE(32): /* word32 S + A */
156 1.14 mycroft case R_TYPE(GLOB_DAT): /* word32 S + A */
157 1.15 mycroft def = _rtld_find_symdef(symnum, obj, &defobj, false);
158 1.14 mycroft if (def == NULL)
159 1.14 mycroft return -1;
160 1.14 mycroft
161 1.14 mycroft tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
162 1.14 mycroft rela->r_addend);
163 1.14 mycroft if (*where != tmp)
164 1.14 mycroft *where = tmp;
165 1.24 mycroft rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
166 1.16 mycroft obj->strtab + obj->symtab[symnum].st_name,
167 1.16 mycroft obj->path, (void *)*where, defobj->path));
168 1.14 mycroft break;
169 1.14 mycroft
170 1.14 mycroft case R_TYPE(RELATIVE): /* word32 B + A */
171 1.22 mycroft *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
172 1.24 mycroft rdbg(("RELATIVE in %s --> %p", obj->path,
173 1.14 mycroft (void *)*where));
174 1.14 mycroft break;
175 1.14 mycroft
176 1.14 mycroft case R_TYPE(COPY):
177 1.14 mycroft /*
178 1.14 mycroft * These are deferred until all other relocations have
179 1.14 mycroft * been done. All we do here is make sure that the
180 1.14 mycroft * COPY relocation is not in a shared library. They
181 1.14 mycroft * are allowed only in executable files.
182 1.14 mycroft */
183 1.20 mycroft if (obj->isdynamic) {
184 1.14 mycroft _rtld_error(
185 1.13 mycroft "%s: Unexpected R_COPY relocation in shared library",
186 1.14 mycroft obj->path);
187 1.14 mycroft return -1;
188 1.14 mycroft }
189 1.24 mycroft rdbg(("COPY (avoid in main)"));
190 1.14 mycroft break;
191 1.14 mycroft
192 1.14 mycroft default:
193 1.24 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
194 1.14 mycroft "addend = %p, contents = %p, symbol = %s",
195 1.15 mycroft symnum, (u_long)ELF_R_TYPE(rela->r_info),
196 1.14 mycroft (void *)rela->r_offset, (void *)rela->r_addend,
197 1.14 mycroft (void *)*where,
198 1.15 mycroft obj->strtab + obj->symtab[symnum].st_name));
199 1.14 mycroft _rtld_error("%s: Unsupported relocation type %ld "
200 1.43 jmmv "in non-PLT relocations",
201 1.14 mycroft obj->path, (u_long) ELF_R_TYPE(rela->r_info));
202 1.13 mycroft return -1;
203 1.13 mycroft }
204 1.13 mycroft }
205 1.17 mycroft return 0;
206 1.17 mycroft }
207 1.17 mycroft
208 1.17 mycroft int
209 1.35 skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
210 1.17 mycroft {
211 1.17 mycroft const Elf_Rela *rela;
212 1.28 mycroft int reloff;
213 1.17 mycroft
214 1.28 mycroft for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
215 1.17 mycroft Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
216 1.17 mycroft int distance;
217 1.17 mycroft Elf_Addr *pltresolve;
218 1.17 mycroft
219 1.17 mycroft assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
220 1.17 mycroft
221 1.17 mycroft pltresolve = obj->pltgot + 8;
222 1.17 mycroft
223 1.28 mycroft if (reloff < 32768) {
224 1.28 mycroft /* li r11,reloff */
225 1.28 mycroft *where++ = 0x39600000 | reloff;
226 1.28 mycroft } else {
227 1.28 mycroft /* lis r11,ha(reloff) */
228 1.28 mycroft /* addi r11,l(reloff) */
229 1.28 mycroft *where++ = 0x3d600000 | ha(reloff);
230 1.28 mycroft *where++ = 0x396b0000 | l(reloff);
231 1.28 mycroft }
232 1.28 mycroft /* b pltresolve */
233 1.28 mycroft distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
234 1.28 mycroft *where++ = 0x48000000 | (distance & 0x03fffffc);
235 1.39 chs
236 1.39 chs /*
237 1.39 chs * Icache invalidation is not done for each entry here
238 1.39 chs * because we sync the entire code part of the PLT once
239 1.39 chs * in _rtld_setup_pltgot() after all the entries have been
240 1.39 chs * initialized.
241 1.39 chs */
242 1.38 chs /* __syncicache(where - 3, 12); */
243 1.17 mycroft }
244 1.17 mycroft
245 1.13 mycroft return 0;
246 1.27 mycroft }
247 1.27 mycroft
248 1.36 skrll static inline int
249 1.36 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
250 1.27 mycroft {
251 1.27 mycroft Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
252 1.27 mycroft Elf_Addr value;
253 1.27 mycroft const Elf_Sym *def;
254 1.27 mycroft const Obj_Entry *defobj;
255 1.27 mycroft int distance;
256 1.44 christos unsigned long info = rela->r_info;
257 1.27 mycroft
258 1.44 christos assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
259 1.27 mycroft
260 1.44 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
261 1.44 christos if (__predict_false(def == NULL))
262 1.36 skrll return -1;
263 1.44 christos if (__predict_false(def == &_rtld_sym_zero))
264 1.44 christos return 0;
265 1.27 mycroft
266 1.27 mycroft value = (Elf_Addr)(defobj->relocbase + def->st_value);
267 1.27 mycroft distance = value - (Elf_Addr)where;
268 1.29 mycroft rdbg(("bind now/fixup in %s --> new=%p",
269 1.29 mycroft defobj->strtab + def->st_name, (void *)value));
270 1.27 mycroft
271 1.27 mycroft if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
272 1.27 mycroft /* b value # branch directly */
273 1.27 mycroft *where = 0x48000000 | (distance & 0x03fffffc);
274 1.27 mycroft __syncicache(where, 4);
275 1.27 mycroft } else {
276 1.27 mycroft Elf_Addr *pltcall, *jmptab;
277 1.27 mycroft int N = obj->pltrelalim - obj->pltrela;
278 1.27 mycroft
279 1.28 mycroft /* Entries beyond 8192 take twice as much space. */
280 1.28 mycroft if (N > 8192)
281 1.28 mycroft N += N-8192;
282 1.28 mycroft
283 1.27 mycroft pltcall = obj->pltgot;
284 1.28 mycroft jmptab = pltcall + 18 + N * 2;
285 1.27 mycroft
286 1.27 mycroft jmptab[reloff] = value;
287 1.27 mycroft
288 1.28 mycroft if (reloff < 32768) {
289 1.28 mycroft /* li r11,reloff */
290 1.28 mycroft *where++ = 0x39600000 | reloff;
291 1.28 mycroft } else {
292 1.28 mycroft /* lis r11,ha(reloff) */
293 1.28 mycroft /* addi r11,l(reloff) */
294 1.28 mycroft *where++ = 0x3d600000 | ha(reloff);
295 1.28 mycroft *where++ = 0x396b0000 | l(reloff);
296 1.28 mycroft }
297 1.28 mycroft /* b pltcall */
298 1.28 mycroft distance = (Elf_Addr)pltcall - (Elf_Addr)where;
299 1.28 mycroft *where++ = 0x48000000 | (distance & 0x03fffffc);
300 1.38 chs __syncicache(where - 3, 12);
301 1.27 mycroft }
302 1.27 mycroft
303 1.36 skrll if (tp)
304 1.36 skrll *tp = value;
305 1.36 skrll return 0;
306 1.36 skrll }
307 1.36 skrll
308 1.36 skrll caddr_t
309 1.36 skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
310 1.36 skrll {
311 1.36 skrll const Elf_Rela *rela = obj->pltrela + reloff;
312 1.36 skrll Elf_Addr new_value;
313 1.36 skrll int err;
314 1.36 skrll
315 1.40 mrg new_value = 0; /* XXX gcc */
316 1.40 mrg
317 1.36 skrll err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
318 1.44 christos if (err)
319 1.36 skrll _rtld_die();
320 1.36 skrll
321 1.36 skrll return (caddr_t)new_value;
322 1.36 skrll }
323 1.36 skrll
324 1.36 skrll int
325 1.36 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
326 1.36 skrll {
327 1.36 skrll const Elf_Rela *rela;
328 1.36 skrll int reloff;
329 1.36 skrll
330 1.36 skrll for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
331 1.36 skrll if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
332 1.36 skrll return -1;
333 1.36 skrll }
334 1.36 skrll return 0;
335 1.1 tsubai }
336