ppc_reloc.c revision 1.27 1 /* $NetBSD: ppc_reloc.c,v 1.27 2002/09/25 07:27:53 mycroft Exp $ */
2
3 /*-
4 * Copyright (C) 1998 Tsubai Masanari
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <machine/cpu.h>
37
38 #include "debug.h"
39 #include "rtld.h"
40
41 void _rtld_powerpc_pltcall __P((Elf_Word));
42 void _rtld_powerpc_pltresolve __P((Elf_Word, Elf_Word));
43
44 #define ha(x) ((((u_int32_t)(x) & 0x8000) ? \
45 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
46 #define l(x) ((u_int32_t)(x) & 0xffff)
47
48 void _rtld_bind_start(void);
49 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
50 caddr_t _rtld_bind __P((const Obj_Entry *, Elf_Word));
51
52 /*
53 * Setup the plt glue routines.
54 */
55 #define PLTCALL_SIZE 20
56 #define PLTRESOLVE_SIZE 24
57
58 void
59 _rtld_setup_pltgot(obj)
60 const Obj_Entry *obj;
61 {
62 Elf_Word *pltcall, *pltresolve;
63 Elf_Word *jmptab;
64 int N = obj->pltrelalim - obj->pltrela;
65
66 pltcall = obj->pltgot;
67
68 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
69 jmptab = pltcall + 18 + N * 2;
70 pltcall[1] |= ha(jmptab);
71 pltcall[2] |= l(jmptab);
72
73 pltresolve = obj->pltgot + 8;
74
75 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
76 pltresolve[0] |= ha(_rtld_bind_start);
77 pltresolve[1] |= l(_rtld_bind_start);
78 pltresolve[3] |= ha(obj);
79 pltresolve[4] |= l(obj);
80
81 __syncicache(pltcall, 72 + N * 8);
82 }
83
84 void
85 _rtld_relocate_nonplt_self(dynp, relocbase)
86 Elf_Dyn *dynp;
87 Elf_Addr relocbase;
88 {
89 const Elf_Rela *rela = 0, *relalim;
90 Elf_Addr relasz = 0;
91 Elf_Addr *where;
92
93 for (; dynp->d_tag != DT_NULL; dynp++) {
94 switch (dynp->d_tag) {
95 case DT_RELA:
96 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
97 break;
98 case DT_RELASZ:
99 relasz = dynp->d_un.d_val;
100 break;
101 }
102 }
103 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
104 for (; rela < relalim; rela++) {
105 where = (Elf_Addr *)(relocbase + rela->r_offset);
106 *where = (Elf_Addr)(relocbase + rela->r_addend);
107 }
108 }
109
110 int
111 _rtld_relocate_nonplt_objects(obj, self)
112 const Obj_Entry *obj;
113 bool self;
114 {
115 const Elf_Rela *rela;
116
117 if (self)
118 return 0;
119
120 for (rela = obj->rela; rela < obj->relalim; rela++) {
121 Elf_Addr *where;
122 const Elf_Sym *def;
123 const Obj_Entry *defobj;
124 Elf_Addr tmp;
125 unsigned long symnum;
126
127 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
128 symnum = ELF_R_SYM(rela->r_info);
129
130 switch (ELF_R_TYPE(rela->r_info)) {
131 #if 1 /* XXX Should not be necessary. */
132 case R_TYPE(JMP_SLOT):
133 #endif
134 case R_TYPE(NONE):
135 break;
136
137 case R_TYPE(32): /* word32 S + A */
138 case R_TYPE(GLOB_DAT): /* word32 S + A */
139 def = _rtld_find_symdef(symnum, obj, &defobj, false);
140 if (def == NULL)
141 return -1;
142
143 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
144 rela->r_addend);
145 if (*where != tmp)
146 *where = tmp;
147 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
148 obj->strtab + obj->symtab[symnum].st_name,
149 obj->path, (void *)*where, defobj->path));
150 break;
151
152 case R_TYPE(RELATIVE): /* word32 B + A */
153 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
154 rdbg(("RELATIVE in %s --> %p", obj->path,
155 (void *)*where));
156 break;
157
158 case R_TYPE(COPY):
159 /*
160 * These are deferred until all other relocations have
161 * been done. All we do here is make sure that the
162 * COPY relocation is not in a shared library. They
163 * are allowed only in executable files.
164 */
165 if (obj->isdynamic) {
166 _rtld_error(
167 "%s: Unexpected R_COPY relocation in shared library",
168 obj->path);
169 return -1;
170 }
171 rdbg(("COPY (avoid in main)"));
172 break;
173
174 default:
175 rdbg(("sym = %lu, type = %lu, offset = %p, "
176 "addend = %p, contents = %p, symbol = %s",
177 symnum, (u_long)ELF_R_TYPE(rela->r_info),
178 (void *)rela->r_offset, (void *)rela->r_addend,
179 (void *)*where,
180 obj->strtab + obj->symtab[symnum].st_name));
181 _rtld_error("%s: Unsupported relocation type %ld "
182 "in non-PLT relocations\n",
183 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
184 return -1;
185 }
186 }
187 return 0;
188 }
189
190 int
191 _rtld_relocate_plt_lazy(obj)
192 const Obj_Entry *obj;
193 {
194 const Elf_Rela *rela;
195
196 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
197 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
198 int distance;
199 Elf_Addr *pltresolve;
200 int reloff = rela - obj->pltrela;
201
202 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
203
204 if (reloff < 0 || reloff >= 0x8000)
205 return (-1);
206
207 pltresolve = obj->pltgot + 8;
208
209 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
210
211 /* li r11,reloff */
212 /* b pltresolve */
213 where[0] = 0x39600000 | reloff;
214 where[1] = 0x48000000 | (distance & 0x03fffffc);
215 /* __syncicache(where, 8); */
216 }
217
218 return 0;
219 }
220
221 caddr_t
222 _rtld_bind(obj, reloff)
223 const Obj_Entry *obj;
224 Elf_Word reloff;
225 {
226 const Elf_Rela *rela = obj->pltrela + reloff;
227 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
228 Elf_Addr value;
229 const Elf_Sym *def;
230 const Obj_Entry *defobj;
231 int distance;
232
233 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
234
235 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
236 if (def == NULL)
237 _rtld_die();
238
239 value = (Elf_Addr)(defobj->relocbase + def->st_value);
240 distance = value - (Elf_Addr)where;
241
242 if (abs(distance) < 32*1024*1024) { /* inside 32MB? */
243 /* b value # branch directly */
244 *where = 0x48000000 | (distance & 0x03fffffc);
245 __syncicache(where, 4);
246 } else {
247 Elf_Addr *pltcall, *jmptab;
248 int N = obj->pltrelalim - obj->pltrela;
249
250 pltcall = obj->pltgot;
251
252 jmptab = pltcall + 18 + N * 2;
253 jmptab[reloff] = value;
254
255 distance = (Elf_Addr)pltcall - (Elf_Addr)(where + 1);
256
257 /* li r11,reloff */
258 /* b pltcall # use pltcall routine */
259 where[0] = 0x39600000 | reloff;
260 where[1] = 0x48000000 | (distance & 0x03fffffc);
261 __syncicache(where, 8);
262 }
263
264 return (caddr_t)value;
265 }
266