Deleted Added
full compact
reloc.c (133133) reloc.c (137122)
1/* $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 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 *
1/* $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 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 * $FreeBSD: head/libexec/rtld-elf/powerpc/reloc.c 133133 2004-08-04 19:12:14Z dfr $
29 * $FreeBSD: head/libexec/rtld-elf/powerpc/reloc.c 137122 2004-11-02 09:47:01Z ssouhlal $
30 */
31
32#include <sys/param.h>
33#include <sys/mman.h>
34
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40#include <machine/cpu.h>
41
42#include "debug.h"
43#include "rtld.h"
44
45#define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \
46 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
47#define _ppc_la(x) ((u_int32_t)(x) & 0xffff)
48
49/*
50 * Process the R_PPC_COPY relocations
51 */
52int
53do_copy_relocations(Obj_Entry *dstobj)
54{
55 const Elf_Rela *relalim;
56 const Elf_Rela *rela;
57
58 /*
59 * COPY relocs are invalid outside of the main program
60 */
61 assert(dstobj->mainprog);
62
63 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
64 dstobj->relasize);
65 for (rela = dstobj->rela; rela < relalim; rela++) {
66 void *dstaddr;
67 const Elf_Sym *dstsym;
68 const char *name;
69 unsigned long hash;
70 size_t size;
71 const void *srcaddr;
72 const Elf_Sym *srcsym = NULL;
73 Obj_Entry *srcobj;
74
75 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
76 continue;
77 }
78
79 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
80 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
81 name = dstobj->strtab + dstsym->st_name;
82 hash = elf_hash(name);
83 size = dstsym->st_size;
84
85 for (srcobj = dstobj->next; srcobj != NULL;
86 srcobj = srcobj->next) {
87 if ((srcsym = symlook_obj(name, hash, srcobj, false))
88 != NULL) {
89 break;
90 }
91 }
92
93 if (srcobj == NULL) {
94 _rtld_error("Undefined symbol \"%s\" "
95 " referenced from COPY"
96 " relocation in %s", name, dstobj->path);
97 return (-1);
98 }
99
100 srcaddr = (const void *) (srcobj->relocbase+srcsym->st_value);
101 memcpy(dstaddr, srcaddr, size);
102 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
103 }
104
105 return (0);
106}
107
108
109/*
110 * Perform early relocation of the run-time linker image
111 */
112void
113reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
114{
115 const Elf_Rela *rela = 0, *relalim;
116 Elf_Addr relasz = 0;
117 Elf_Addr *where;
118
119 /*
120 * Extract the rela/relasz values from the dynamic section
121 */
122 for (; dynp->d_tag != DT_NULL; dynp++) {
123 switch (dynp->d_tag) {
124 case DT_RELA:
125 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
126 break;
127 case DT_RELASZ:
128 relasz = dynp->d_un.d_val;
129 break;
130 }
131 }
132
133 /*
134 * Relocate these values
135 */
136 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
137 for (; rela < relalim; rela++) {
138 where = (Elf_Addr *)(relocbase + rela->r_offset);
139 *where = (Elf_Addr)(relocbase + rela->r_addend);
140 }
141}
142
143
144/*
145 * Relocate a non-PLT object with addend.
146 */
147static int
148reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
149 SymCache *cache)
150{
151 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
152 const Elf_Sym *def;
153 const Obj_Entry *defobj;
154 Elf_Addr tmp;
155
156 switch (ELF_R_TYPE(rela->r_info)) {
157
158 case R_PPC_NONE:
159 break;
160
161 case R_PPC_ADDR32: /* word32 S + A */
162 case R_PPC_GLOB_DAT: /* word32 S + A */
163 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
164 false, cache);
165 if (def == NULL) {
166 return (-1);
167 }
168
169 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
170 rela->r_addend);
171
172 /* Don't issue write if unnecessary; avoid COW page fault */
173 if (*where != tmp) {
174 *where = tmp;
175 }
176 break;
177
178 case R_PPC_RELATIVE: /* word32 B + A */
179 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
180
181 /* As above, don't issue write unnecessarily */
182 if (*where != tmp) {
183 *where = tmp;
184 }
185 break;
186
187 case R_PPC_COPY:
188 /*
189 * These are deferred until all other relocations
190 * have been done. All we do here is make sure
191 * that the COPY relocation is not in a shared
192 * library. They are allowed only in executable
193 * files.
194 */
195 if (!obj->mainprog) {
196 _rtld_error("%s: Unexpected R_COPY "
197 " relocation in shared library",
198 obj->path);
199 return (-1);
200 }
201 break;
202
203 case R_PPC_JMP_SLOT:
204 /*
205 * These will be handled by the plt/jmpslot routines
206 */
207 break;
208
30 */
31
32#include <sys/param.h>
33#include <sys/mman.h>
34
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40#include <machine/cpu.h>
41
42#include "debug.h"
43#include "rtld.h"
44
45#define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \
46 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
47#define _ppc_la(x) ((u_int32_t)(x) & 0xffff)
48
49/*
50 * Process the R_PPC_COPY relocations
51 */
52int
53do_copy_relocations(Obj_Entry *dstobj)
54{
55 const Elf_Rela *relalim;
56 const Elf_Rela *rela;
57
58 /*
59 * COPY relocs are invalid outside of the main program
60 */
61 assert(dstobj->mainprog);
62
63 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
64 dstobj->relasize);
65 for (rela = dstobj->rela; rela < relalim; rela++) {
66 void *dstaddr;
67 const Elf_Sym *dstsym;
68 const char *name;
69 unsigned long hash;
70 size_t size;
71 const void *srcaddr;
72 const Elf_Sym *srcsym = NULL;
73 Obj_Entry *srcobj;
74
75 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
76 continue;
77 }
78
79 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
80 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
81 name = dstobj->strtab + dstsym->st_name;
82 hash = elf_hash(name);
83 size = dstsym->st_size;
84
85 for (srcobj = dstobj->next; srcobj != NULL;
86 srcobj = srcobj->next) {
87 if ((srcsym = symlook_obj(name, hash, srcobj, false))
88 != NULL) {
89 break;
90 }
91 }
92
93 if (srcobj == NULL) {
94 _rtld_error("Undefined symbol \"%s\" "
95 " referenced from COPY"
96 " relocation in %s", name, dstobj->path);
97 return (-1);
98 }
99
100 srcaddr = (const void *) (srcobj->relocbase+srcsym->st_value);
101 memcpy(dstaddr, srcaddr, size);
102 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
103 }
104
105 return (0);
106}
107
108
109/*
110 * Perform early relocation of the run-time linker image
111 */
112void
113reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
114{
115 const Elf_Rela *rela = 0, *relalim;
116 Elf_Addr relasz = 0;
117 Elf_Addr *where;
118
119 /*
120 * Extract the rela/relasz values from the dynamic section
121 */
122 for (; dynp->d_tag != DT_NULL; dynp++) {
123 switch (dynp->d_tag) {
124 case DT_RELA:
125 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
126 break;
127 case DT_RELASZ:
128 relasz = dynp->d_un.d_val;
129 break;
130 }
131 }
132
133 /*
134 * Relocate these values
135 */
136 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
137 for (; rela < relalim; rela++) {
138 where = (Elf_Addr *)(relocbase + rela->r_offset);
139 *where = (Elf_Addr)(relocbase + rela->r_addend);
140 }
141}
142
143
144/*
145 * Relocate a non-PLT object with addend.
146 */
147static int
148reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
149 SymCache *cache)
150{
151 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
152 const Elf_Sym *def;
153 const Obj_Entry *defobj;
154 Elf_Addr tmp;
155
156 switch (ELF_R_TYPE(rela->r_info)) {
157
158 case R_PPC_NONE:
159 break;
160
161 case R_PPC_ADDR32: /* word32 S + A */
162 case R_PPC_GLOB_DAT: /* word32 S + A */
163 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
164 false, cache);
165 if (def == NULL) {
166 return (-1);
167 }
168
169 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
170 rela->r_addend);
171
172 /* Don't issue write if unnecessary; avoid COW page fault */
173 if (*where != tmp) {
174 *where = tmp;
175 }
176 break;
177
178 case R_PPC_RELATIVE: /* word32 B + A */
179 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
180
181 /* As above, don't issue write unnecessarily */
182 if (*where != tmp) {
183 *where = tmp;
184 }
185 break;
186
187 case R_PPC_COPY:
188 /*
189 * These are deferred until all other relocations
190 * have been done. All we do here is make sure
191 * that the COPY relocation is not in a shared
192 * library. They are allowed only in executable
193 * files.
194 */
195 if (!obj->mainprog) {
196 _rtld_error("%s: Unexpected R_COPY "
197 " relocation in shared library",
198 obj->path);
199 return (-1);
200 }
201 break;
202
203 case R_PPC_JMP_SLOT:
204 /*
205 * These will be handled by the plt/jmpslot routines
206 */
207 break;
208
209 case R_PPC_DTPMOD32:
210 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
211 false, cache);
212
213 if (def == NULL)
214 return (-1);
215
216 *where = (Elf_Addr) defobj->tlsindex;
217
218 break;
219
220 case R_PPC_TPREL32:
221 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
222 false, cache);
223
224 if (def == NULL)
225 return (-1);
226
227 /*
228 * We lazily allocate offsets for static TLS as we
229 * see the first relocation that references the
230 * TLS block. This allows us to support (small
231 * amounts of) static TLS in dynamically loaded
232 * modules. If we run out of space, we generate an
233 * error.
234 */
235 if (!defobj->tls_done) {
236 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
237 _rtld_error("%s: No space available for static "
238 "Thread Local Storage", obj->path);
239 return (-1);
240 }
241 }
242
243 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
244 + (Elf_Addr *)(def->st_value + rela->r_addend
245 + defobj->tlsoffset - TLS_TP_OFFSET - TLS_TCB_SIZE);
246
247 break;
248
249 case R_PPC_DTPREL32:
250 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
251 false, cache);
252
253 if (def == NULL)
254 return (-1);
255
256 *where += (Elf_Addr)(def->st_value + rela->r_addend
257 - TLS_DTV_OFFSET);
258
259 break;
260
209 default:
210 _rtld_error("%s: Unsupported relocation type %d"
211 " in non-PLT relocations\n", obj->path,
212 ELF_R_TYPE(rela->r_info));
213 return (-1);
214 }
215 return (0);
216}
217
218
219/*
220 * Process non-PLT relocations
221 */
222int
223reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
224{
225 const Elf_Rela *relalim;
226 const Elf_Rela *rela;
227 SymCache *cache;
228 int bytes = obj->nchains * sizeof(SymCache);
229 int r = -1;
230
231 /*
232 * The dynamic loader may be called from a thread, we have
233 * limited amounts of stack available so we cannot use alloca().
234 */
235 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
236 if (cache == MAP_FAILED)
237 cache = NULL;
238
239 /*
240 * From the SVR4 PPC ABI:
241 * "The PowerPC family uses only the Elf32_Rela relocation
242 * entries with explicit addends."
243 */
244 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
245 for (rela = obj->rela; rela < relalim; rela++) {
246 if (reloc_nonplt_object(obj_rtld, obj, rela, cache) < 0)
247 goto done;
248 }
249 r = 0;
250done:
251 if (cache) {
252 munmap(cache, bytes);
253 }
254 return (r);
255}
256
257
258/*
259 * Initialise a PLT slot to the resolving trampoline
260 */
261static int
262reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
263{
264 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
265 Elf_Addr *pltresolve;
266 Elf_Addr distance;
267 int reloff;
268
269 reloff = rela - obj->pltrela;
270
271 if ((reloff < 0) || (reloff >= 0x8000)) {
272 return (-1);
273 }
274
275 pltresolve = obj->pltgot + 8;
276
277 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
278
279 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
280 (void *)where, (void *)pltresolve, reloff, distance);
281
282 /* li r11,reloff */
283 /* b pltresolve */
284 where[0] = 0x39600000 | reloff;
285 where[1] = 0x48000000 | (distance & 0x03fffffc);
286
287 /*
288 * The icache will be sync'd in init_pltgot, which is called
289 * after all the slots have been updated
290 */
291
292 return (0);
293}
294
295
296/*
297 * Process the PLT relocations.
298 */
299int
300reloc_plt(Obj_Entry *obj)
301{
302 const Elf_Rela *relalim;
303 const Elf_Rela *rela;
304
305 if (obj->pltrelasize != 0) {
306
307 relalim = (const Elf_Rela *)((char *)obj->pltrela +
308 obj->pltrelasize);
309 for (rela = obj->pltrela; rela < relalim; rela++) {
310 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
311
312 if (reloc_plt_object(obj, rela) < 0) {
313 return (-1);
314 }
315 }
316 }
317
318 return (0);
319}
320
321
322/*
323 * LD_BIND_NOW was set - force relocation for all jump slots
324 */
325int
326reloc_jmpslots(Obj_Entry *obj)
327{
328 const Obj_Entry *defobj;
329 const Elf_Rela *relalim;
330 const Elf_Rela *rela;
331 const Elf_Sym *def;
332 Elf_Addr *where;
333 Elf_Addr target;
334
335 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
336 for (rela = obj->pltrela; rela < relalim; rela++) {
337 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
338 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
339 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
340 true, NULL);
341 if (def == NULL) {
342 dbg("reloc_jmpslots: sym not found");
343 return (-1);
344 }
345
346 target = (Elf_Addr)(defobj->relocbase + def->st_value);
347
348#if 0
349 /* PG XXX */
350 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
351 defobj->strtab + def->st_name, basename(obj->path),
352 (void *)target, basename(defobj->path));
353#endif
354
355 reloc_jmpslot(where, target, defobj, obj,
356 (const Elf_Rel *) rela);
357 }
358
359 obj->jmpslots_done = true;
360
361 return (0);
362}
363
364
365/*
366 * Update the value of a PLT jump slot. Branch directly to the target if
367 * it is within +/- 32Mb, otherwise go indirectly via the pltcall
368 * trampoline call and jump table.
369 */
370Elf_Addr
371reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
372 const Obj_Entry *obj, const Elf_Rel *rel)
373{
374 Elf_Addr offset;
375 const Elf_Rela *rela = (const Elf_Rela *) rel;
376
377 dbg(" reloc_jmpslot: where=%p, target=%p",
378 (void *)wherep, (void *)target);
379
380 /*
381 * At the PLT entry pointed at by `wherep', construct
382 * a direct transfer to the now fully resolved function
383 * address.
384 */
385 offset = target - (Elf_Addr)wherep;
386
387 if (abs(offset) < 32*1024*1024) { /* inside 32MB? */
388 /* b value # branch directly */
389 *wherep = 0x48000000 | (offset & 0x03fffffc);
390 __syncicache(wherep, 4);
391 } else {
392 Elf_Addr *pltcall, *jmptab;
393 int distance;
394 int N = obj->pltrelasize / sizeof(Elf_Rela);
395 int reloff = rela - obj->pltrela;
396
397 if ((reloff < 0) || (reloff >= 0x8000)) {
398 return (-1);
399 }
400
401 pltcall = obj->pltgot;
402
403 dbg(" reloc_jmpslot: indir, reloff=%d, N=%d\n",
404 reloff, N);
405
406 jmptab = obj->pltgot + 18 + N * 2;
407 jmptab[reloff] = target;
408
409 distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
410
411 /* li r11,reloff */
412 /* b pltcall # use indirect pltcall routine */
413 wherep[0] = 0x39600000 | reloff;
414 wherep[1] = 0x48000000 | (distance & 0x03fffffc);
415 __syncicache(wherep, 8);
416 }
417
418 return (target);
419}
420
421
422/*
423 * Setup the plt glue routines.
424 */
425#define PLTCALL_SIZE 20
426#define PLTRESOLVE_SIZE 24
427
428void
429init_pltgot(Obj_Entry *obj)
430{
431 Elf_Word *pltcall, *pltresolve;
432 Elf_Word *jmptab;
433 int N = obj->pltrelasize / sizeof(Elf_Rela);
434
435 pltcall = obj->pltgot;
436
437 if (pltcall == NULL) {
438 return;
439 }
440
441 /*
442 * From the SVR4 PPC ABI:
443 *
444 * 'The first 18 words (72 bytes) of the PLT are reserved for
445 * use by the dynamic linker.
446 * ...
447 * 'If the executable or shared object requires N procedure
448 * linkage table entries, the link editor shall reserve 3*N
449 * words (12*N bytes) following the 18 reserved words. The
450 * first 2*N of these words are the procedure linkage table
451 * entries themselves. The static linker directs calls to bytes
452 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
453 * N words (4*N bytes) are reserved for use by the dynamic linker.'
454 */
455
456 /*
457 * Copy the absolute-call assembler stub into the first part of
458 * the reserved PLT area.
459 */
460 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
461
462 /*
463 * Determine the address of the jumptable, which is the dyn-linker
464 * reserved area after the call cells. Write the absolute address
465 * of the jumptable into the absolute-call assembler code so it
466 * can determine this address.
467 */
468 jmptab = pltcall + 18 + N * 2;
469 pltcall[1] |= _ppc_ha(jmptab); /* addis 11,11,jmptab@ha */
470 pltcall[2] |= _ppc_la(jmptab); /* lwz 11,jmptab@l(11) */
471
472 /*
473 * Skip down 32 bytes into the initial reserved area and copy
474 * in the standard resolving assembler call. Into this assembler,
475 * insert the absolute address of the _rtld_bind_start routine
476 * and the address of the relocation object.
477 */
478 pltresolve = obj->pltgot + 8;
479
480 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
481 pltresolve[0] |= _ppc_ha(_rtld_bind_start);
482 pltresolve[1] |= _ppc_la(_rtld_bind_start);
483 pltresolve[3] |= _ppc_ha(obj);
484 pltresolve[4] |= _ppc_la(obj);
485
486 /*
487 * Sync the icache for the byte range represented by the
488 * trampoline routines and call slots.
489 */
490 __syncicache(pltcall, 72 + N * 8);
491}
492
493void
494allocate_initial_tls(Obj_Entry *list)
495{
496 register Elf_Addr **tp __asm__("r2");
261 default:
262 _rtld_error("%s: Unsupported relocation type %d"
263 " in non-PLT relocations\n", obj->path,
264 ELF_R_TYPE(rela->r_info));
265 return (-1);
266 }
267 return (0);
268}
269
270
271/*
272 * Process non-PLT relocations
273 */
274int
275reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
276{
277 const Elf_Rela *relalim;
278 const Elf_Rela *rela;
279 SymCache *cache;
280 int bytes = obj->nchains * sizeof(SymCache);
281 int r = -1;
282
283 /*
284 * The dynamic loader may be called from a thread, we have
285 * limited amounts of stack available so we cannot use alloca().
286 */
287 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
288 if (cache == MAP_FAILED)
289 cache = NULL;
290
291 /*
292 * From the SVR4 PPC ABI:
293 * "The PowerPC family uses only the Elf32_Rela relocation
294 * entries with explicit addends."
295 */
296 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
297 for (rela = obj->rela; rela < relalim; rela++) {
298 if (reloc_nonplt_object(obj_rtld, obj, rela, cache) < 0)
299 goto done;
300 }
301 r = 0;
302done:
303 if (cache) {
304 munmap(cache, bytes);
305 }
306 return (r);
307}
308
309
310/*
311 * Initialise a PLT slot to the resolving trampoline
312 */
313static int
314reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
315{
316 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
317 Elf_Addr *pltresolve;
318 Elf_Addr distance;
319 int reloff;
320
321 reloff = rela - obj->pltrela;
322
323 if ((reloff < 0) || (reloff >= 0x8000)) {
324 return (-1);
325 }
326
327 pltresolve = obj->pltgot + 8;
328
329 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
330
331 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
332 (void *)where, (void *)pltresolve, reloff, distance);
333
334 /* li r11,reloff */
335 /* b pltresolve */
336 where[0] = 0x39600000 | reloff;
337 where[1] = 0x48000000 | (distance & 0x03fffffc);
338
339 /*
340 * The icache will be sync'd in init_pltgot, which is called
341 * after all the slots have been updated
342 */
343
344 return (0);
345}
346
347
348/*
349 * Process the PLT relocations.
350 */
351int
352reloc_plt(Obj_Entry *obj)
353{
354 const Elf_Rela *relalim;
355 const Elf_Rela *rela;
356
357 if (obj->pltrelasize != 0) {
358
359 relalim = (const Elf_Rela *)((char *)obj->pltrela +
360 obj->pltrelasize);
361 for (rela = obj->pltrela; rela < relalim; rela++) {
362 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
363
364 if (reloc_plt_object(obj, rela) < 0) {
365 return (-1);
366 }
367 }
368 }
369
370 return (0);
371}
372
373
374/*
375 * LD_BIND_NOW was set - force relocation for all jump slots
376 */
377int
378reloc_jmpslots(Obj_Entry *obj)
379{
380 const Obj_Entry *defobj;
381 const Elf_Rela *relalim;
382 const Elf_Rela *rela;
383 const Elf_Sym *def;
384 Elf_Addr *where;
385 Elf_Addr target;
386
387 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
388 for (rela = obj->pltrela; rela < relalim; rela++) {
389 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
390 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
391 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
392 true, NULL);
393 if (def == NULL) {
394 dbg("reloc_jmpslots: sym not found");
395 return (-1);
396 }
397
398 target = (Elf_Addr)(defobj->relocbase + def->st_value);
399
400#if 0
401 /* PG XXX */
402 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
403 defobj->strtab + def->st_name, basename(obj->path),
404 (void *)target, basename(defobj->path));
405#endif
406
407 reloc_jmpslot(where, target, defobj, obj,
408 (const Elf_Rel *) rela);
409 }
410
411 obj->jmpslots_done = true;
412
413 return (0);
414}
415
416
417/*
418 * Update the value of a PLT jump slot. Branch directly to the target if
419 * it is within +/- 32Mb, otherwise go indirectly via the pltcall
420 * trampoline call and jump table.
421 */
422Elf_Addr
423reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
424 const Obj_Entry *obj, const Elf_Rel *rel)
425{
426 Elf_Addr offset;
427 const Elf_Rela *rela = (const Elf_Rela *) rel;
428
429 dbg(" reloc_jmpslot: where=%p, target=%p",
430 (void *)wherep, (void *)target);
431
432 /*
433 * At the PLT entry pointed at by `wherep', construct
434 * a direct transfer to the now fully resolved function
435 * address.
436 */
437 offset = target - (Elf_Addr)wherep;
438
439 if (abs(offset) < 32*1024*1024) { /* inside 32MB? */
440 /* b value # branch directly */
441 *wherep = 0x48000000 | (offset & 0x03fffffc);
442 __syncicache(wherep, 4);
443 } else {
444 Elf_Addr *pltcall, *jmptab;
445 int distance;
446 int N = obj->pltrelasize / sizeof(Elf_Rela);
447 int reloff = rela - obj->pltrela;
448
449 if ((reloff < 0) || (reloff >= 0x8000)) {
450 return (-1);
451 }
452
453 pltcall = obj->pltgot;
454
455 dbg(" reloc_jmpslot: indir, reloff=%d, N=%d\n",
456 reloff, N);
457
458 jmptab = obj->pltgot + 18 + N * 2;
459 jmptab[reloff] = target;
460
461 distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
462
463 /* li r11,reloff */
464 /* b pltcall # use indirect pltcall routine */
465 wherep[0] = 0x39600000 | reloff;
466 wherep[1] = 0x48000000 | (distance & 0x03fffffc);
467 __syncicache(wherep, 8);
468 }
469
470 return (target);
471}
472
473
474/*
475 * Setup the plt glue routines.
476 */
477#define PLTCALL_SIZE 20
478#define PLTRESOLVE_SIZE 24
479
480void
481init_pltgot(Obj_Entry *obj)
482{
483 Elf_Word *pltcall, *pltresolve;
484 Elf_Word *jmptab;
485 int N = obj->pltrelasize / sizeof(Elf_Rela);
486
487 pltcall = obj->pltgot;
488
489 if (pltcall == NULL) {
490 return;
491 }
492
493 /*
494 * From the SVR4 PPC ABI:
495 *
496 * 'The first 18 words (72 bytes) of the PLT are reserved for
497 * use by the dynamic linker.
498 * ...
499 * 'If the executable or shared object requires N procedure
500 * linkage table entries, the link editor shall reserve 3*N
501 * words (12*N bytes) following the 18 reserved words. The
502 * first 2*N of these words are the procedure linkage table
503 * entries themselves. The static linker directs calls to bytes
504 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
505 * N words (4*N bytes) are reserved for use by the dynamic linker.'
506 */
507
508 /*
509 * Copy the absolute-call assembler stub into the first part of
510 * the reserved PLT area.
511 */
512 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
513
514 /*
515 * Determine the address of the jumptable, which is the dyn-linker
516 * reserved area after the call cells. Write the absolute address
517 * of the jumptable into the absolute-call assembler code so it
518 * can determine this address.
519 */
520 jmptab = pltcall + 18 + N * 2;
521 pltcall[1] |= _ppc_ha(jmptab); /* addis 11,11,jmptab@ha */
522 pltcall[2] |= _ppc_la(jmptab); /* lwz 11,jmptab@l(11) */
523
524 /*
525 * Skip down 32 bytes into the initial reserved area and copy
526 * in the standard resolving assembler call. Into this assembler,
527 * insert the absolute address of the _rtld_bind_start routine
528 * and the address of the relocation object.
529 */
530 pltresolve = obj->pltgot + 8;
531
532 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
533 pltresolve[0] |= _ppc_ha(_rtld_bind_start);
534 pltresolve[1] |= _ppc_la(_rtld_bind_start);
535 pltresolve[3] |= _ppc_ha(obj);
536 pltresolve[4] |= _ppc_la(obj);
537
538 /*
539 * Sync the icache for the byte range represented by the
540 * trampoline routines and call slots.
541 */
542 __syncicache(pltcall, 72 + N * 8);
543}
544
545void
546allocate_initial_tls(Obj_Entry *list)
547{
548 register Elf_Addr **tp __asm__("r2");
549 Elf_Addr **_tp;
497
498 /*
499 * Fix the size of the static TLS block by using the maximum
500 * offset allocated so far and adding a bit for dynamic modules to
501 * use.
502 */
503
504 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
505
550
551 /*
552 * Fix the size of the static TLS block by using the maximum
553 * offset allocated so far and adding a bit for dynamic modules to
554 * use.
555 */
556
557 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
558
506 tp = (Elf_Addr **) ((char *) allocate_tls(list, 0, 8, 8) + 0x7008);
559 _tp = (Elf_Addr **) ((char *) allocate_tls(list, 0, 8, 8)
560 + TLS_TP_OFFSET + TLS_TCB_SIZE);
561
562 /*
563 * XXX gcc seems to ignore 'tp = _tp;'
564 */
565
566 __asm __volatile("mr %0,%1" : "=r"(tp) : "r"(_tp));
507}
508
509void*
510__tls_get_addr(tls_index* ti)
511{
512 register Elf_Addr **tp __asm__("r2");
513 char *p;
514
567}
568
569void*
570__tls_get_addr(tls_index* ti)
571{
572 register Elf_Addr **tp __asm__("r2");
573 char *p;
574
515 p = tls_get_addr_common(tp, ti->ti_module, ti->ti_offset);
516 return p + 0x8000;
575 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET
576 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
577
578 return (p + TLS_DTV_OFFSET);
517}
579}