ppc_reloc.c revision 1.56 1 1.56 joerg /* $NetBSD: ppc_reloc.c,v 1.56 2018/03/09 20:19:11 joerg 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.56 joerg __RCSID("$NetBSD: ppc_reloc.c,v 1.56 2018/03/09 20:19:11 joerg 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.1 tsubai #include <machine/cpu.h>
42 1.1 tsubai
43 1.1 tsubai #include "debug.h"
44 1.1 tsubai #include "rtld.h"
45 1.1 tsubai
46 1.35 skrll void _rtld_powerpc_pltcall(Elf_Word);
47 1.35 skrll void _rtld_powerpc_pltresolve(Elf_Word, Elf_Word);
48 1.1 tsubai
49 1.50 matt #define __u64(x) ((uint64_t)(x))
50 1.50 matt #define __u32(x) ((uint32_t)(x))
51 1.50 matt #define __ha48 __u64(0xffffffff8000)
52 1.50 matt #define __ha32 __u64(0xffff8000)
53 1.50 matt #define __ha16 __u32(0x8000)
54 1.52 matt #define __ha(x,n) ((((x) >> (n)) + (((x) & __ha##n) == __ha##n)) & 0xffff)
55 1.50 matt #define __hi(x,n) (((x) >> (n)) & 0xffff)
56 1.50 matt #ifdef __LP64
57 1.50 matt #define highesta(x) __ha(__u64(x), 48)
58 1.50 matt #define highest(x) __hi(__u64(x), 48)
59 1.50 matt #define higher(x) __ha(__u64(x), 32)
60 1.50 matt #define higher(x) __hi(__u64(x), 32)
61 1.50 matt #endif
62 1.50 matt #define ha(x) __ha(__u32(x), 16)
63 1.50 matt #define hi(x) __hi(__u32(x), 16)
64 1.50 matt #define lo(x) (__u32(x) & 0xffff)
65 1.50 matt
66 1.50 matt #ifdef _LP64
67 1.50 matt /* function descriptor for _rtld_bind_start */
68 1.50 matt extern const uint64_t _rtld_bind_start[3];
69 1.50 matt #else
70 1.46 matt void _rtld_bind_bssplt_start(void);
71 1.46 matt void _rtld_bind_secureplt_start(void);
72 1.50 matt #endif
73 1.51 matt Elf_Addr _rtld_bind(const Obj_Entry *, Elf_Word);
74 1.22 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
75 1.46 matt static int _rtld_relocate_plt_object(const Obj_Entry *,
76 1.36 skrll const Elf_Rela *, int, Elf_Addr *);
77 1.1 tsubai
78 1.1 tsubai /*
79 1.50 matt * The PPC32 PLT format consists of three sections:
80 1.39 chs * (1) The "pltcall" and "pltresolve" glue code. This is always 18 words.
81 1.39 chs * (2) The code part of the PLT entries. There are 2 words per entry for
82 1.39 chs * up to 8192 entries, then 4 words per entry for any additional entries.
83 1.39 chs * (3) The data part of the PLT entries, comprising a jump table.
84 1.39 chs * This section is half the size of the second section (ie. 1 or 2 words
85 1.39 chs * per entry).
86 1.39 chs */
87 1.39 chs
88 1.1 tsubai void
89 1.35 skrll _rtld_setup_pltgot(const Obj_Entry *obj)
90 1.1 tsubai {
91 1.50 matt #ifdef _LP64
92 1.50 matt /*
93 1.50 matt * For powerpc64, just copy the function descriptor to pltgot[0].
94 1.50 matt */
95 1.50 matt if (obj->pltgot != NULL) {
96 1.50 matt obj->pltgot[0] = (Elf_Addr) _rtld_bind_start[0];
97 1.50 matt obj->pltgot[1] = (Elf_Addr) _rtld_bind_start[1];
98 1.50 matt obj->pltgot[2] = (Elf_Addr) obj;
99 1.50 matt }
100 1.50 matt #else
101 1.39 chs /*
102 1.46 matt * Secure-PLT is much more sane.
103 1.39 chs */
104 1.46 matt if (obj->gotptr != NULL) {
105 1.46 matt obj->gotptr[1] = (Elf_Addr) _rtld_bind_secureplt_start;
106 1.46 matt obj->gotptr[2] = (Elf_Addr) obj;
107 1.47 matt dbg(("obj %s secure-plt gotptr=%p start=%p obj=%p",
108 1.47 matt obj->path, obj->gotptr,
109 1.47 matt (void *) obj->gotptr[1], (void *) obj->gotptr[2]));
110 1.46 matt } else {
111 1.50 matt /*
112 1.50 matt * Setup the plt glue routines (for bss-plt).
113 1.50 matt */
114 1.50 matt #define BSSPLTCALL_SIZE 20
115 1.50 matt #define BSSPLTRESOLVE_SIZE 24
116 1.50 matt
117 1.46 matt Elf_Word *pltcall, *pltresolve;
118 1.46 matt Elf_Word *jmptab;
119 1.46 matt int N = obj->pltrelalim - obj->pltrela;
120 1.46 matt
121 1.46 matt /* Entries beyond 8192 take twice as much space. */
122 1.46 matt if (N > 8192)
123 1.46 matt N += N-8192;
124 1.46 matt
125 1.47 matt dbg(("obj %s bss-plt pltgot=%p jmptab=%u start=%p obj=%p",
126 1.47 matt obj->path, obj->pltgot, 18 + N * 2,
127 1.47 matt _rtld_bind_bssplt_start, obj));
128 1.47 matt
129 1.46 matt pltcall = obj->pltgot;
130 1.46 matt jmptab = pltcall + 18 + N * 2;
131 1.46 matt
132 1.50 matt memcpy(pltcall, _rtld_powerpc_pltcall, BSSPLTCALL_SIZE);
133 1.46 matt pltcall[1] |= ha(jmptab);
134 1.50 matt pltcall[2] |= lo(jmptab);
135 1.46 matt
136 1.46 matt pltresolve = obj->pltgot + 8;
137 1.46 matt
138 1.50 matt memcpy(pltresolve, _rtld_powerpc_pltresolve, BSSPLTRESOLVE_SIZE);
139 1.46 matt pltresolve[0] |= ha(_rtld_bind_bssplt_start);
140 1.50 matt pltresolve[1] |= lo(_rtld_bind_bssplt_start);
141 1.46 matt pltresolve[3] |= ha(obj);
142 1.50 matt pltresolve[4] |= lo(obj);
143 1.46 matt
144 1.46 matt /*
145 1.46 matt * Invalidate the icache for only the code part of the PLT
146 1.46 matt * (and not the jump table at the end).
147 1.46 matt */
148 1.46 matt __syncicache(pltcall, (char *)jmptab - (char *)pltcall);
149 1.46 matt }
150 1.50 matt #endif
151 1.13 mycroft }
152 1.13 mycroft
153 1.22 mycroft void
154 1.35 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
155 1.22 mycroft {
156 1.22 mycroft const Elf_Rela *rela = 0, *relalim;
157 1.22 mycroft Elf_Addr relasz = 0;
158 1.22 mycroft Elf_Addr *where;
159 1.22 mycroft
160 1.22 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
161 1.22 mycroft switch (dynp->d_tag) {
162 1.22 mycroft case DT_RELA:
163 1.22 mycroft rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
164 1.22 mycroft break;
165 1.22 mycroft case DT_RELASZ:
166 1.22 mycroft relasz = dynp->d_un.d_val;
167 1.22 mycroft break;
168 1.22 mycroft }
169 1.22 mycroft }
170 1.42 he relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
171 1.22 mycroft for (; rela < relalim; rela++) {
172 1.22 mycroft where = (Elf_Addr *)(relocbase + rela->r_offset);
173 1.22 mycroft *where = (Elf_Addr)(relocbase + rela->r_addend);
174 1.22 mycroft }
175 1.22 mycroft }
176 1.22 mycroft
177 1.13 mycroft int
178 1.45 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
179 1.13 mycroft {
180 1.14 mycroft const Elf_Rela *rela;
181 1.54 joerg const Elf_Sym *def = NULL;
182 1.54 joerg const Obj_Entry *defobj = NULL;
183 1.54 joerg unsigned long last_symnum = ULONG_MAX;
184 1.22 mycroft
185 1.14 mycroft for (rela = obj->rela; rela < obj->relalim; rela++) {
186 1.14 mycroft Elf_Addr *where;
187 1.14 mycroft Elf_Addr tmp;
188 1.15 mycroft unsigned long symnum;
189 1.14 mycroft
190 1.14 mycroft where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
191 1.54 joerg
192 1.54 joerg switch (ELF_R_TYPE(rela->r_info)) {
193 1.54 joerg #ifdef _LP64
194 1.54 joerg case R_TYPE(ADDR64): /* <address> S + A */
195 1.54 joerg #else
196 1.54 joerg case R_TYPE(ADDR32): /* <address> S + A */
197 1.54 joerg #endif
198 1.54 joerg case R_TYPE(GLOB_DAT): /* <address> S + A */
199 1.54 joerg case R_TYPE(DTPMOD):
200 1.54 joerg case R_TYPE(DTPREL):
201 1.54 joerg case R_TYPE(TPREL):
202 1.54 joerg symnum = ELF_R_SYM(rela->r_info);
203 1.54 joerg if (last_symnum != symnum) {
204 1.54 joerg last_symnum = symnum;
205 1.54 joerg def = _rtld_find_symdef(symnum, obj, &defobj,
206 1.54 joerg false);
207 1.54 joerg if (def == NULL)
208 1.54 joerg return -1;
209 1.54 joerg }
210 1.54 joerg break;
211 1.54 joerg default:
212 1.54 joerg break;
213 1.54 joerg }
214 1.13 mycroft
215 1.14 mycroft switch (ELF_R_TYPE(rela->r_info)) {
216 1.26 mycroft #if 1 /* XXX Should not be necessary. */
217 1.26 mycroft case R_TYPE(JMP_SLOT):
218 1.26 mycroft #endif
219 1.14 mycroft case R_TYPE(NONE):
220 1.14 mycroft break;
221 1.14 mycroft
222 1.50 matt #ifdef _LP64
223 1.50 matt case R_TYPE(ADDR64): /* <address> S + A */
224 1.50 matt #else
225 1.50 matt case R_TYPE(ADDR32): /* <address> S + A */
226 1.50 matt #endif
227 1.50 matt case R_TYPE(GLOB_DAT): /* <address> S + A */
228 1.14 mycroft tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
229 1.14 mycroft rela->r_addend);
230 1.14 mycroft if (*where != tmp)
231 1.14 mycroft *where = tmp;
232 1.24 mycroft rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
233 1.16 mycroft obj->strtab + obj->symtab[symnum].st_name,
234 1.16 mycroft obj->path, (void *)*where, defobj->path));
235 1.14 mycroft break;
236 1.14 mycroft
237 1.50 matt case R_TYPE(RELATIVE): /* <address> B + A */
238 1.22 mycroft *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
239 1.24 mycroft rdbg(("RELATIVE in %s --> %p", obj->path,
240 1.14 mycroft (void *)*where));
241 1.14 mycroft break;
242 1.14 mycroft
243 1.14 mycroft case R_TYPE(COPY):
244 1.14 mycroft /*
245 1.14 mycroft * These are deferred until all other relocations have
246 1.14 mycroft * been done. All we do here is make sure that the
247 1.14 mycroft * COPY relocation is not in a shared library. They
248 1.14 mycroft * are allowed only in executable files.
249 1.14 mycroft */
250 1.20 mycroft if (obj->isdynamic) {
251 1.14 mycroft _rtld_error(
252 1.13 mycroft "%s: Unexpected R_COPY relocation in shared library",
253 1.14 mycroft obj->path);
254 1.14 mycroft return -1;
255 1.14 mycroft }
256 1.24 mycroft rdbg(("COPY (avoid in main)"));
257 1.14 mycroft break;
258 1.14 mycroft
259 1.50 matt case R_TYPE(DTPMOD):
260 1.48 matt *where = (Elf_Addr)defobj->tlsindex;
261 1.48 matt rdbg(("DTPMOD32 %s in %s --> %p in %s",
262 1.48 matt obj->strtab + obj->symtab[symnum].st_name,
263 1.48 matt obj->path, (void *)*where, defobj->path));
264 1.48 matt break;
265 1.48 matt
266 1.50 matt case R_TYPE(DTPREL):
267 1.48 matt if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
268 1.48 matt return -1;
269 1.48 matt
270 1.48 matt *where = (Elf_Addr)(def->st_value + rela->r_addend
271 1.48 matt - TLS_DTV_OFFSET);
272 1.48 matt rdbg(("DTPREL32 %s in %s --> %p in %s",
273 1.48 matt obj->strtab + obj->symtab[symnum].st_name,
274 1.48 matt obj->path, (void *)*where, defobj->path));
275 1.48 matt break;
276 1.48 matt
277 1.50 matt case R_TYPE(TPREL):
278 1.48 matt if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
279 1.48 matt return -1;
280 1.48 matt
281 1.48 matt *where = (Elf_Addr)(def->st_value + rela->r_addend
282 1.48 matt + defobj->tlsoffset - TLS_TP_OFFSET);
283 1.48 matt rdbg(("TPREL32 %s in %s --> %p in %s",
284 1.48 matt obj->strtab + obj->symtab[symnum].st_name,
285 1.48 matt obj->path, (void *)*where, defobj->path));
286 1.48 matt break;
287 1.48 matt
288 1.56 joerg case R_TYPE(IRELATIVE):
289 1.56 joerg /* IFUNC relocations are handled in _rtld_call_ifunc */
290 1.56 joerg if (obj->ifunc_remaining_nonplt == 0) {
291 1.56 joerg obj->ifunc_remaining_nonplt =
292 1.56 joerg rela - obj->rela + 1;
293 1.56 joerg }
294 1.56 joerg break;
295 1.56 joerg
296 1.14 mycroft default:
297 1.24 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
298 1.14 mycroft "addend = %p, contents = %p, symbol = %s",
299 1.54 joerg (u_long)ELF_R_SYM(rela->r_info),
300 1.54 joerg (u_long)ELF_R_TYPE(rela->r_info),
301 1.14 mycroft (void *)rela->r_offset, (void *)rela->r_addend,
302 1.14 mycroft (void *)*where,
303 1.15 mycroft obj->strtab + obj->symtab[symnum].st_name));
304 1.14 mycroft _rtld_error("%s: Unsupported relocation type %ld "
305 1.43 jmmv "in non-PLT relocations",
306 1.14 mycroft obj->path, (u_long) ELF_R_TYPE(rela->r_info));
307 1.13 mycroft return -1;
308 1.13 mycroft }
309 1.13 mycroft }
310 1.17 mycroft return 0;
311 1.17 mycroft }
312 1.17 mycroft
313 1.17 mycroft int
314 1.55 joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
315 1.17 mycroft {
316 1.51 matt #ifdef _LP64
317 1.51 matt /*
318 1.51 matt * For PowerPC64, the plt stubs handle an empty function descriptor
319 1.51 matt * so there's nothing to do.
320 1.51 matt */
321 1.55 joerg /* XXX ifunc support */
322 1.51 matt #else
323 1.46 matt Elf_Addr * const pltresolve = obj->pltgot + 8;
324 1.17 mycroft const Elf_Rela *rela;
325 1.17 mycroft
326 1.56 joerg for (rela = obj->pltrelalim; rela-- > obj->pltrela;) {
327 1.56 joerg size_t reloff = rela - obj->pltrela;
328 1.17 mycroft Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
329 1.17 mycroft
330 1.55 joerg assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT) ||
331 1.55 joerg ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE));
332 1.55 joerg
333 1.55 joerg if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE)) {
334 1.55 joerg /* No ifunc support for old-style insecure PLT. */
335 1.55 joerg assert(obj->gotptr != NULL);
336 1.55 joerg obj->ifunc_remaining = obj->pltrelalim - rela;
337 1.55 joerg }
338 1.17 mycroft
339 1.46 matt if (obj->gotptr != NULL) {
340 1.46 matt /*
341 1.46 matt * For now, simply treat then as relative.
342 1.46 matt */
343 1.46 matt *where += (Elf_Addr)obj->relocbase;
344 1.46 matt } else {
345 1.46 matt int distance;
346 1.46 matt
347 1.46 matt if (reloff < 32768) {
348 1.46 matt /* li r11,reloff */
349 1.46 matt *where++ = 0x39600000 | reloff;
350 1.46 matt } else {
351 1.46 matt /* lis r11,ha(reloff) */
352 1.50 matt /* addi r11,lo(reloff) */
353 1.46 matt *where++ = 0x3d600000 | ha(reloff);
354 1.50 matt *where++ = 0x396b0000 | lo(reloff);
355 1.46 matt }
356 1.46 matt /* b pltresolve */
357 1.46 matt distance = (Elf_Addr)pltresolve - (Elf_Addr)where;
358 1.46 matt *where++ = 0x48000000 | (distance & 0x03fffffc);
359 1.17 mycroft
360 1.46 matt /*
361 1.46 matt * Icache invalidation is not done for each entry here
362 1.46 matt * because we sync the entire code part of the PLT once
363 1.46 matt * in _rtld_setup_pltgot() after all the entries have been
364 1.46 matt * initialized.
365 1.46 matt */
366 1.46 matt /* __syncicache(where - 3, 12); */
367 1.28 mycroft }
368 1.17 mycroft }
369 1.51 matt #endif /* !_LP64 */
370 1.17 mycroft
371 1.13 mycroft return 0;
372 1.27 mycroft }
373 1.27 mycroft
374 1.55 joerg void
375 1.55 joerg _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
376 1.55 joerg {
377 1.55 joerg const Elf_Rela *rela;
378 1.55 joerg Elf_Addr *where, target;
379 1.55 joerg
380 1.55 joerg while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
381 1.55 joerg rela = obj->pltrelalim - obj->ifunc_remaining;
382 1.55 joerg --obj->ifunc_remaining;
383 1.55 joerg if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE)) {
384 1.55 joerg where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
385 1.55 joerg target = (Elf_Addr)(obj->relocbase + rela->r_addend);
386 1.55 joerg _rtld_exclusive_exit(mask);
387 1.55 joerg target = _rtld_resolve_ifunc2(obj, target);
388 1.55 joerg _rtld_exclusive_enter(mask);
389 1.55 joerg if (*where != target)
390 1.55 joerg *where = target;
391 1.55 joerg }
392 1.55 joerg }
393 1.56 joerg
394 1.56 joerg while (obj->ifunc_remaining_nonplt > 0 && _rtld_objgen == cur_objgen) {
395 1.56 joerg rela = obj->relalim - --obj->ifunc_remaining_nonplt;
396 1.56 joerg if (ELF_R_TYPE(rela->r_info) != R_TYPE(IRELATIVE))
397 1.56 joerg continue;
398 1.56 joerg where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
399 1.56 joerg target = (Elf_Addr)(obj->relocbase + rela->r_addend);
400 1.56 joerg _rtld_exclusive_exit(mask);
401 1.56 joerg target = _rtld_resolve_ifunc2(obj, target);
402 1.56 joerg _rtld_exclusive_enter(mask);
403 1.56 joerg if (*where != target)
404 1.56 joerg *where = target;
405 1.56 joerg }
406 1.55 joerg }
407 1.55 joerg
408 1.46 matt static int
409 1.36 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
410 1.27 mycroft {
411 1.27 mycroft Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
412 1.27 mycroft Elf_Addr value;
413 1.27 mycroft const Elf_Sym *def;
414 1.27 mycroft const Obj_Entry *defobj;
415 1.44 christos unsigned long info = rela->r_info;
416 1.27 mycroft
417 1.44 christos assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
418 1.27 mycroft
419 1.44 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
420 1.44 christos if (__predict_false(def == NULL))
421 1.36 skrll return -1;
422 1.44 christos if (__predict_false(def == &_rtld_sym_zero))
423 1.44 christos return 0;
424 1.27 mycroft
425 1.53 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
426 1.53 joerg if (tp == NULL)
427 1.53 joerg return 0;
428 1.53 joerg value = _rtld_resolve_ifunc(defobj, def);
429 1.53 joerg } else {
430 1.53 joerg value = (Elf_Addr)(defobj->relocbase + def->st_value);
431 1.53 joerg }
432 1.29 mycroft rdbg(("bind now/fixup in %s --> new=%p",
433 1.29 mycroft defobj->strtab + def->st_name, (void *)value));
434 1.27 mycroft
435 1.50 matt #ifdef _LP64
436 1.50 matt /*
437 1.51 matt * For PowerPC64 we simply replace the function descriptor in the
438 1.51 matt * PLTGOT with the one from source object.
439 1.50 matt */
440 1.50 matt assert(where >= (Elf_Word *)obj->pltgot);
441 1.50 matt assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
442 1.51 matt const Elf_Addr * const fdesc = (Elf_Addr *) value;
443 1.51 matt where[0] = fdesc[0];
444 1.51 matt where[1] = fdesc[1];
445 1.51 matt where[2] = fdesc[2];
446 1.50 matt #else
447 1.51 matt ptrdiff_t distance = value - (Elf_Addr)where;
448 1.46 matt if (obj->gotptr != NULL) {
449 1.46 matt /*
450 1.50 matt * For Secure-PLT we simply replace the entry in GOT with the
451 1.50 matt * address of the routine.
452 1.46 matt */
453 1.46 matt assert(where >= (Elf_Word *)obj->pltgot);
454 1.46 matt assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
455 1.46 matt *where = value;
456 1.51 matt } else if (labs(distance) < 32*1024*1024) { /* inside 32MB? */
457 1.27 mycroft /* b value # branch directly */
458 1.27 mycroft *where = 0x48000000 | (distance & 0x03fffffc);
459 1.27 mycroft __syncicache(where, 4);
460 1.27 mycroft } else {
461 1.27 mycroft Elf_Addr *pltcall, *jmptab;
462 1.27 mycroft int N = obj->pltrelalim - obj->pltrela;
463 1.27 mycroft
464 1.28 mycroft /* Entries beyond 8192 take twice as much space. */
465 1.28 mycroft if (N > 8192)
466 1.28 mycroft N += N-8192;
467 1.28 mycroft
468 1.27 mycroft pltcall = obj->pltgot;
469 1.28 mycroft jmptab = pltcall + 18 + N * 2;
470 1.27 mycroft
471 1.27 mycroft jmptab[reloff] = value;
472 1.27 mycroft
473 1.28 mycroft if (reloff < 32768) {
474 1.28 mycroft /* li r11,reloff */
475 1.28 mycroft *where++ = 0x39600000 | reloff;
476 1.28 mycroft } else {
477 1.46 matt #ifdef notyet
478 1.46 matt /* lis r11,ha(value) */
479 1.50 matt /* addi r11,lo(value) */
480 1.46 matt /* mtctr r11 */
481 1.46 matt /* bctr */
482 1.46 matt *where++ = 0x3d600000 | ha(value);
483 1.50 matt *where++ = 0x396b0000 | lo(value);
484 1.46 matt *where++ = 0x7d6903a6;
485 1.46 matt *where++ = 0x4e800420;
486 1.46 matt #else
487 1.28 mycroft /* lis r11,ha(reloff) */
488 1.50 matt /* addi r11,lo(reloff) */
489 1.28 mycroft *where++ = 0x3d600000 | ha(reloff);
490 1.50 matt *where++ = 0x396b0000 | lo(reloff);
491 1.46 matt #endif
492 1.28 mycroft }
493 1.28 mycroft /* b pltcall */
494 1.28 mycroft distance = (Elf_Addr)pltcall - (Elf_Addr)where;
495 1.28 mycroft *where++ = 0x48000000 | (distance & 0x03fffffc);
496 1.38 chs __syncicache(where - 3, 12);
497 1.27 mycroft }
498 1.50 matt #endif /* _LP64 */
499 1.27 mycroft
500 1.36 skrll if (tp)
501 1.36 skrll *tp = value;
502 1.36 skrll return 0;
503 1.36 skrll }
504 1.36 skrll
505 1.51 matt Elf_Addr
506 1.36 skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
507 1.36 skrll {
508 1.47 matt const Elf_Rela *rela = obj->pltrela + reloff;
509 1.36 skrll Elf_Addr new_value;
510 1.36 skrll int err;
511 1.36 skrll
512 1.40 mrg new_value = 0; /* XXX gcc */
513 1.40 mrg
514 1.49 joerg _rtld_shared_enter();
515 1.36 skrll err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
516 1.44 christos if (err)
517 1.36 skrll _rtld_die();
518 1.49 joerg _rtld_shared_exit();
519 1.36 skrll
520 1.51 matt #ifdef _LP64
521 1.51 matt return obj->glink;
522 1.51 matt #else
523 1.51 matt return new_value;
524 1.51 matt #endif
525 1.36 skrll }
526 1.36 skrll
527 1.36 skrll int
528 1.36 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
529 1.36 skrll {
530 1.36 skrll const Elf_Rela *rela;
531 1.36 skrll int reloff;
532 1.36 skrll
533 1.36 skrll for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
534 1.36 skrll if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
535 1.36 skrll return -1;
536 1.36 skrll }
537 1.36 skrll return 0;
538 1.1 tsubai }
539