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