]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/powerpc/reloc.c
sys/{x86,amd64}: remove one of doubled ;s
[FreeBSD/FreeBSD.git] / libexec / rtld-elf / powerpc / reloc.c
1 /*      $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 mycroft Exp $   */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (C) 1998   Tsubai Masanari
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33
34 #include <sys/param.h>
35 #include <sys/mman.h>
36
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <machine/cpu.h>
43 #include <machine/atomic.h>
44 #include <machine/md_var.h>
45
46 #include "debug.h"
47 #include "rtld.h"
48
49 #define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \
50                         ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16)
51 #define _ppc_la(x) ((u_int32_t)(x) & 0xffff)
52
53 #define min(a,b) (((a) < (b)) ? (a) : (b))
54 #define max(a,b) (((a) > (b)) ? (a) : (b))
55
56 #define PLT_EXTENDED_BEGIN      (1 << 13)
57 #define JMPTAB_BASE(N)          (18 + N*2 + ((N > PLT_EXTENDED_BEGIN) ? \
58                                     (N - PLT_EXTENDED_BEGIN)*2 : 0))
59
60 void _rtld_bind_secureplt_start(void);
61
62 /*
63  * Process the R_PPC_COPY relocations
64  */
65 int
66 do_copy_relocations(Obj_Entry *dstobj)
67 {
68         const Elf_Rela *relalim;
69         const Elf_Rela *rela;
70
71         /*
72          * COPY relocs are invalid outside of the main program
73          */
74         assert(dstobj->mainprog);
75
76         relalim = (const Elf_Rela *)((const char *) dstobj->rela +
77             dstobj->relasize);
78         for (rela = dstobj->rela;  rela < relalim;  rela++) {
79                 void *dstaddr;
80                 const Elf_Sym *dstsym;
81                 const char *name;
82                 size_t size;
83                 const void *srcaddr;
84                 const Elf_Sym *srcsym = NULL;
85                 const Obj_Entry *srcobj, *defobj;
86                 SymLook req;
87                 int res;
88
89                 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
90                         continue;
91                 }
92
93                 dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
94                 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
95                 name = dstobj->strtab + dstsym->st_name;
96                 size = dstsym->st_size;
97                 symlook_init(&req, name);
98                 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
99                 req.flags = SYMLOOK_EARLY;
100
101                 for (srcobj = globallist_next(dstobj); srcobj != NULL;
102                      srcobj = globallist_next(srcobj)) {
103                         res = symlook_obj(&req, srcobj);
104                         if (res == 0) {
105                                 srcsym = req.sym_out;
106                                 defobj = req.defobj_out;
107                                 break;
108                         }
109                 }
110
111                 if (srcobj == NULL) {
112                         _rtld_error("Undefined symbol \"%s\" "
113                                     " referenced from COPY"
114                                     " relocation in %s", name, dstobj->path);
115                         return (-1);
116                 }
117
118                 srcaddr = (const void *)(defobj->relocbase+srcsym->st_value);
119                 memcpy(dstaddr, srcaddr, size);
120                 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size);
121         }
122
123         return (0);
124 }
125
126
127 /*
128  * Perform early relocation of the run-time linker image
129  */
130 void
131 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
132 {
133         const Elf_Rela *rela = NULL, *relalim;
134         Elf_Addr relasz = 0;
135         Elf_Addr *where;
136
137         /*
138          * Extract the rela/relasz values from the dynamic section
139          */
140         for (; dynp->d_tag != DT_NULL; dynp++) {
141                 switch (dynp->d_tag) {
142                 case DT_RELA:
143                         rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
144                         break;
145                 case DT_RELASZ:
146                         relasz = dynp->d_un.d_val;
147                         break;
148                 }
149         }
150
151         /*
152          * Relocate these values
153          */
154         relalim = (const Elf_Rela *)((const char *)rela + relasz);
155         for (; rela < relalim; rela++) {
156                 where = (Elf_Addr *)(relocbase + rela->r_offset);
157                 *where = (Elf_Addr)(relocbase + rela->r_addend);
158         }
159 }
160
161
162 /*
163  * Relocate a non-PLT object with addend.
164  */
165 static int
166 reloc_nonplt_object(Obj_Entry *obj_rtld __unused, Obj_Entry *obj,
167     const Elf_Rela *rela, SymCache *cache, int flags, RtldLockState *lockstate)
168 {
169         Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
170         const Elf_Sym   *def;
171         const Obj_Entry *defobj;
172         Elf_Addr         tmp;
173
174         switch (ELF_R_TYPE(rela->r_info)) {
175
176         case R_PPC_NONE:
177                 break;
178
179         case R_PPC_ADDR32:    /* word32 S + A */
180         case R_PPC_GLOB_DAT:  /* word32 S + A */
181                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
182                     flags, cache, lockstate);
183                 if (def == NULL) {
184                         return (-1);
185                 }
186
187                 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
188                     rela->r_addend);
189
190                 /* Don't issue write if unnecessary; avoid COW page fault */
191                 if (*where != tmp) {
192                         *where = tmp;
193                 }
194                 break;
195
196         case R_PPC_RELATIVE:  /* word32 B + A */
197                 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
198
199                 /* As above, don't issue write unnecessarily */
200                 if (*where != tmp) {
201                         *where = tmp;
202                 }
203                 break;
204
205         case R_PPC_COPY:
206                 /*
207                  * These are deferred until all other relocations
208                  * have been done.  All we do here is make sure
209                  * that the COPY relocation is not in a shared
210                  * library.  They are allowed only in executable
211                  * files.
212                  */
213                 if (!obj->mainprog) {
214                         _rtld_error("%s: Unexpected R_COPY "
215                                     " relocation in shared library",
216                                     obj->path);
217                         return (-1);
218                 }
219                 break;
220
221         case R_PPC_JMP_SLOT:
222                 /*
223                  * These will be handled by the plt/jmpslot routines
224                  */
225                 break;
226
227         case R_PPC_DTPMOD32:
228                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
229                     flags, cache, lockstate);
230
231                 if (def == NULL)
232                         return (-1);
233
234                 *where = (Elf_Addr) defobj->tlsindex;
235
236                 break;
237
238         case R_PPC_TPREL32:
239                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
240                     flags, cache, lockstate);
241
242                 if (def == NULL)
243                         return (-1);
244
245                 /*
246                  * We lazily allocate offsets for static TLS as we
247                  * see the first relocation that references the
248                  * TLS block. This allows us to support (small
249                  * amounts of) static TLS in dynamically loaded
250                  * modules. If we run out of space, we generate an
251                  * error.
252                  */
253                 if (!defobj->tls_done) {
254                         if (!allocate_tls_offset(
255                                     __DECONST(Obj_Entry *, defobj))) {
256                                 _rtld_error("%s: No space available for static "
257                                     "Thread Local Storage", obj->path);
258                                 return (-1);
259                         }
260                 }
261
262                 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
263                     + (Elf_Addr *)(def->st_value + rela->r_addend 
264                     + defobj->tlsoffset - TLS_TP_OFFSET - TLS_TCB_SIZE);
265                 
266                 break;
267                 
268         case R_PPC_DTPREL32:
269                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
270                     flags, cache, lockstate);
271
272                 if (def == NULL)
273                         return (-1);
274
275                 *where += (Elf_Addr)(def->st_value + rela->r_addend 
276                     - TLS_DTV_OFFSET);
277
278                 break;
279                 
280         default:
281                 _rtld_error("%s: Unsupported relocation type %d"
282                             " in non-PLT relocations\n", obj->path,
283                             ELF_R_TYPE(rela->r_info));
284                 return (-1);
285         }
286         return (0);
287 }
288
289
290 /*
291  * Process non-PLT relocations
292  */
293 int
294 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
295     RtldLockState *lockstate)
296 {
297         const Elf_Rela *relalim;
298         const Elf_Rela *rela;
299         const Elf_Phdr *phdr;
300         SymCache *cache;
301         int r = -1;
302
303         if ((flags & SYMLOOK_IFUNC) != 0)
304                 /* XXX not implemented */
305                 return (0);
306
307         /*
308          * The dynamic loader may be called from a thread, we have
309          * limited amounts of stack available so we cannot use alloca().
310          */
311         if (obj != obj_rtld) {
312                 cache = calloc(obj->dynsymcount, sizeof(SymCache));
313                 /* No need to check for NULL here */
314         } else
315                 cache = NULL;
316
317         /*
318          * From the SVR4 PPC ABI:
319          * "The PowerPC family uses only the Elf32_Rela relocation
320          *  entries with explicit addends."
321          */
322         relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
323         for (rela = obj->rela; rela < relalim; rela++) {
324                 if (reloc_nonplt_object(obj_rtld, obj, rela, cache, flags,
325                     lockstate) < 0)
326                         goto done;
327         }
328         r = 0;
329 done:
330         if (cache != NULL)
331                 free(cache);
332
333         /*
334          * Synchronize icache for executable segments in case we made
335          * any changes.
336          */
337         for (phdr = obj->phdr;
338             (const char *)phdr < (const char *)obj->phdr + obj->phsize;
339             phdr++) {
340                 if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X) != 0) {
341                         __syncicache(obj->relocbase + phdr->p_vaddr,
342                             phdr->p_memsz);
343                 }
344         }
345
346         return (r);
347 }
348
349 /*
350  * Initialise a PLT slot to the resolving trampoline
351  */
352 static int
353 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
354 {
355         Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
356         Elf_Addr *pltresolve, *pltlongresolve, *jmptab;
357         Elf_Addr distance;
358         int N = obj->pltrelasize / sizeof(Elf_Rela);
359         int reloff;
360
361         reloff = rela - obj->pltrela;
362
363         if (reloff < 0)
364                 return (-1);
365
366         if (obj->gotptr != NULL) {
367                 *where += (Elf_Addr)obj->relocbase;
368                 return (0);
369         }
370
371         pltlongresolve = obj->pltgot + 5;
372         pltresolve = pltlongresolve + 5;
373
374         distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1);
375
376         dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x",
377             (void *)where, (void *)pltresolve, reloff, distance);
378
379         if (reloff < PLT_EXTENDED_BEGIN) {
380                 /* li   r11,reloff  */
381                 /* b    pltresolve  */
382                 where[0] = 0x39600000 | reloff;
383                 where[1] = 0x48000000 | (distance & 0x03fffffc);
384         } else {
385                 jmptab = obj->pltgot + JMPTAB_BASE(N);
386                 jmptab[reloff] = (u_int)pltlongresolve;
387
388                 /* lis  r11,jmptab[reloff]@ha */
389                 /* lwzu r12,jmptab[reloff]@l(r11) */
390                 /* mtctr r12 */
391                 /* bctr */
392                 where[0] = 0x3d600000 | _ppc_ha(&jmptab[reloff]);
393                 where[1] = 0x858b0000 | _ppc_la(&jmptab[reloff]);
394                 where[2] = 0x7d8903a6;
395                 where[3] = 0x4e800420;
396         }
397                 
398
399         /*
400          * The icache will be sync'd in reloc_plt, which is called
401          * after all the slots have been updated
402          */
403
404         return (0);
405 }
406
407
408 /*
409  * Process the PLT relocations.
410  */
411 int
412 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
413 {
414         const Elf_Rela *relalim;
415         const Elf_Rela *rela;
416         int N = obj->pltrelasize / sizeof(Elf_Rela);
417
418         if (obj->pltrelasize != 0) {
419
420                 relalim = (const Elf_Rela *)((const char *)obj->pltrela +
421                     obj->pltrelasize);
422                 for (rela = obj->pltrela;  rela < relalim;  rela++) {
423                         assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
424
425                         if (reloc_plt_object(obj, rela) < 0) {
426                                 return (-1);
427                         }
428                 }
429         }
430
431         /*
432          * Sync the icache for the byte range represented by the
433          * trampoline routines and call slots.
434          */
435         if (obj->pltgot != NULL && obj->gotptr == NULL)
436                 __syncicache(obj->pltgot, JMPTAB_BASE(N)*4);
437
438         return (0);
439 }
440
441
442 /*
443  * LD_BIND_NOW was set - force relocation for all jump slots
444  */
445 int
446 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
447 {
448         const Obj_Entry *defobj;
449         const Elf_Rela *relalim;
450         const Elf_Rela *rela;
451         const Elf_Sym *def;
452         Elf_Addr *where;
453         Elf_Addr target;
454
455         relalim = (const Elf_Rela *)((const char *)obj->pltrela +
456             obj->pltrelasize);
457         for (rela = obj->pltrela; rela < relalim; rela++) {
458                 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
459                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
460                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
461                     SYMLOOK_IN_PLT | flags, NULL, lockstate);
462                 if (def == NULL) {
463                         dbg("reloc_jmpslots: sym not found");
464                         return (-1);
465                 }
466
467                 target = (Elf_Addr)(defobj->relocbase + def->st_value);
468
469 #if 0
470                 /* PG XXX */
471                 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
472                     defobj->strtab + def->st_name, basename(obj->path),
473                     (void *)target, basename(defobj->path));
474 #endif
475
476                 reloc_jmpslot(where, target, defobj, obj,
477                     (const Elf_Rel *) rela);
478         }
479
480         obj->jmpslots_done = true;
481
482         return (0);
483 }
484
485
486 /*
487  * Update the value of a PLT jump slot. Branch directly to the target if
488  * it is within +/- 32Mb, otherwise go indirectly via the pltcall
489  * trampoline call and jump table.
490  */
491 Elf_Addr
492 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target,
493     const Obj_Entry *defobj __unused, const Obj_Entry *obj, const Elf_Rel *rel)
494 {
495         Elf_Addr offset;
496         const Elf_Rela *rela = (const Elf_Rela *) rel;
497
498         dbg(" reloc_jmpslot: where=%p, target=%p",
499             (void *)wherep, (void *)target);
500
501         if (ld_bind_not)
502                 goto out;
503
504         /*
505          * At the PLT entry pointed at by `wherep', construct
506          * a direct transfer to the now fully resolved function
507          * address.
508          */
509         offset = target - (Elf_Addr)wherep;
510
511         if (obj->gotptr != NULL) {
512                 assert(wherep >= (Elf_Word *)obj->pltgot);
513                 assert(wherep <
514                     (Elf_Word *)obj->pltgot + obj->pltrelasize);
515                 *wherep = target;
516                 goto out;
517         }
518
519         if (abs((int)offset) < 32*1024*1024) {     /* inside 32MB? */
520                 /* b    value   # branch directly */
521                 *wherep = 0x48000000 | (offset & 0x03fffffc);
522                 __syncicache(wherep, 4);
523         } else {
524                 Elf_Addr *pltcall, *jmptab;
525                 int distance;
526                 int N = obj->pltrelasize / sizeof(Elf_Rela);
527                 int reloff = rela - obj->pltrela;
528
529                 if (reloff < 0)
530                         return (-1);
531
532                 pltcall = obj->pltgot;
533
534                 dbg(" reloc_jmpslot: indir, reloff=%x, N=%x\n",
535                     reloff, N);
536
537                 jmptab = obj->pltgot + JMPTAB_BASE(N);
538                 jmptab[reloff] = target;
539                 mb(); /* Order jmptab update before next changes */
540
541                 if (reloff < PLT_EXTENDED_BEGIN) {
542                         /* for extended PLT entries, we keep the old code */
543
544                         distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1);
545
546                         /* li   r11,reloff */
547                         /* b    pltcall  # use indirect pltcall routine */
548
549                         /* first instruction same as before */
550                         wherep[1] = 0x48000000 | (distance & 0x03fffffc);
551                         __syncicache(wherep, 8);
552                 }
553         }
554
555 out:
556         return (target);
557 }
558
559 int
560 reloc_iresolve(Obj_Entry *obj __unused,
561     struct Struct_RtldLockState *lockstate __unused)
562 {
563
564         /* XXX not implemented */
565         return (0);
566 }
567
568 int
569 reloc_gnu_ifunc(Obj_Entry *obj __unused, int flags __unused,
570     struct Struct_RtldLockState *lockstate __unused)
571 {
572
573         /* XXX not implemented */
574         return (0);
575 }
576
577 /*
578  * Setup the plt glue routines.
579  */
580 #define PLTCALL_SIZE            20
581 #define PLTLONGRESOLVE_SIZE     20
582 #define PLTRESOLVE_SIZE         24
583
584 void
585 init_pltgot(Obj_Entry *obj)
586 {
587         Elf_Word *pltcall, *pltresolve, *pltlongresolve;
588         Elf_Word *jmptab;
589         int N = obj->pltrelasize / sizeof(Elf_Rela);
590
591         pltcall = obj->pltgot;
592
593         if (pltcall == NULL) {
594                 return;
595         }
596
597         /* Handle Secure-PLT first, if applicable. */
598         if (obj->gotptr != NULL) {
599                 obj->gotptr[1] = (Elf_Addr)_rtld_bind_secureplt_start;
600                 obj->gotptr[2] = (Elf_Addr)obj;
601                 dbg("obj %s secure-plt gotptr=%p start=%p obj=%p",
602                     obj->path, obj->gotptr,
603                     (void *)obj->gotptr[1], (void *)obj->gotptr[2]);
604                 return;
605         }
606
607         /*
608          * From the SVR4 PPC ABI:
609          *
610          * 'The first 18 words (72 bytes) of the PLT are reserved for
611          * use by the dynamic linker.
612          *   ...
613          * 'If the executable or shared object requires N procedure
614          *  linkage table entries, the link editor shall reserve 3*N
615          *  words (12*N bytes) following the 18 reserved words. The
616          *  first 2*N of these words are the procedure linkage table
617          *  entries themselves. The static linker directs calls to bytes
618          *  (72 + (i-1)*8), for i between 1 and N inclusive. The remaining
619          *  N words (4*N bytes) are reserved for use by the dynamic linker.'
620          */
621
622         /*
623          * Copy the absolute-call assembler stub into the first part of
624          * the reserved PLT area.
625          */
626         memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE);
627
628         /*
629          * Determine the address of the jumptable, which is the dyn-linker
630          * reserved area after the call cells. Write the absolute address
631          * of the jumptable into the absolute-call assembler code so it
632          * can determine this address.
633          */
634         jmptab = obj->pltgot + JMPTAB_BASE(N);
635         pltcall[1] |= _ppc_ha(jmptab);     /* addis 11,11,jmptab@ha */
636         pltcall[2] |= _ppc_la(jmptab);     /* lwz   11,jmptab@l(11) */
637
638         /*
639          * Skip down 20 bytes into the initial reserved area and copy
640          * in the standard resolving assembler call. Into this assembler,
641          * insert the absolute address of the _rtld_bind_start routine
642          * and the address of the relocation object.
643          *
644          * We place pltlongresolve first, so it can fix up its arguments
645          * and then fall through to the regular PLT resolver.
646          */
647         pltlongresolve = obj->pltgot + 5;
648
649         memcpy(pltlongresolve, _rtld_powerpc_pltlongresolve,
650             PLTLONGRESOLVE_SIZE);
651         pltlongresolve[0] |= _ppc_ha(jmptab);   /* lis  12,jmptab@ha    */
652         pltlongresolve[1] |= _ppc_la(jmptab);   /* addi 12,12,jmptab@l  */
653
654         pltresolve = pltlongresolve + PLTLONGRESOLVE_SIZE/sizeof(uint32_t);
655         memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE);
656         pltresolve[0] |= _ppc_ha(_rtld_bind_start);
657         pltresolve[1] |= _ppc_la(_rtld_bind_start);
658         pltresolve[3] |= _ppc_ha(obj);
659         pltresolve[4] |= _ppc_la(obj);
660
661         /*
662          * The icache will be sync'd in reloc_plt, which is called
663          * after all the slots have been updated
664          */
665 }
666
667 void
668 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
669 {
670
671 }
672
673 void
674 pre_init(void)
675 {
676
677 }
678
679 void
680 allocate_initial_tls(Obj_Entry *list)
681 {
682         Elf_Addr **tp;
683
684         /*
685         * Fix the size of the static TLS block by using the maximum
686         * offset allocated so far and adding a bit for dynamic modules to
687         * use.
688         */
689
690         tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
691
692         tp = (Elf_Addr **)((char *) allocate_tls(list, NULL, TLS_TCB_SIZE, 8)
693             + TLS_TP_OFFSET + TLS_TCB_SIZE);
694
695         /*
696          * XXX gcc seems to ignore 'tp = _tp;' 
697          */
698          
699         __asm __volatile("mr 2,%0" :: "r"(tp));
700 }
701
702 void*
703 __tls_get_addr(tls_index* ti)
704 {
705         register Elf_Addr **tp;
706         char *p;
707
708         __asm __volatile("mr %0,2" : "=r"(tp));
709         p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET 
710             - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
711
712         return (p + TLS_DTV_OFFSET);
713 }