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