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