]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - libexec/rtld-elf/powerpc64/reloc.c
MFC r270798:
[FreeBSD/stable/10.git] / libexec / rtld-elf / powerpc64 / reloc.c
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$
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 #include <machine/md_var.h>
42
43 #include "debug.h"
44 #include "rtld.h"
45
46 struct funcdesc {
47         Elf_Addr addr;
48         Elf_Addr toc;
49         Elf_Addr env;
50 };
51
52 /*
53  * Process the R_PPC_COPY relocations
54  */
55 int
56 do_copy_relocations(Obj_Entry *dstobj)
57 {
58         const Elf_Rela *relalim;
59         const Elf_Rela *rela;
60
61         /*
62          * COPY relocs are invalid outside of the main program
63          */
64         assert(dstobj->mainprog);
65
66         relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
67             dstobj->relasize);
68         for (rela = dstobj->rela;  rela < relalim;  rela++) {
69                 void *dstaddr;
70                 const Elf_Sym *dstsym;
71                 const char *name;
72                 size_t size;
73                 const void *srcaddr;
74                 const Elf_Sym *srcsym = NULL;
75                 const Obj_Entry *srcobj, *defobj;
76                 SymLook req;
77                 int res;
78
79                 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) {
80                         continue;
81                 }
82
83                 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
84                 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
85                 name = dstobj->strtab + dstsym->st_name;
86                 size = dstsym->st_size;
87                 symlook_init(&req, name);
88                 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
89                 req.flags = SYMLOOK_EARLY;
90
91                 for (srcobj = dstobj->next;  srcobj != NULL;
92                      srcobj = srcobj->next) {
93                         res = symlook_obj(&req, srcobj);
94                         if (res == 0) {
95                                 srcsym = req.sym_out;
96                                 defobj = req.defobj_out;
97                                 break;
98                         }
99                 }
100
101                 if (srcobj == NULL) {
102                         _rtld_error("Undefined symbol \"%s\" "
103                                     " referenced from COPY"
104                                     " relocation in %s", name, dstobj->path);
105                         return (-1);
106                 }
107
108                 srcaddr = (const void *) (defobj->relocbase+srcsym->st_value);
109                 memcpy(dstaddr, srcaddr, size);
110                 dbg("copy_reloc: src=%p,dst=%p,size=%zd\n",srcaddr,dstaddr,size);
111         }
112
113         return (0);
114 }
115
116
117 /*
118  * Perform early relocation of the run-time linker image
119  */
120 void
121 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
122 {
123         const Elf_Rela *rela = 0, *relalim;
124         Elf_Addr relasz = 0;
125         Elf_Addr *where;
126
127         /*
128          * Extract the rela/relasz values from the dynamic section
129          */
130         for (; dynp->d_tag != DT_NULL; dynp++) {
131                 switch (dynp->d_tag) {
132                 case DT_RELA:
133                         rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
134                         break;
135                 case DT_RELASZ:
136                         relasz = dynp->d_un.d_val;
137                         break;
138                 }
139         }
140
141         /*
142          * Relocate these values
143          */
144         relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
145         for (; rela < relalim; rela++) {
146                 where = (Elf_Addr *)(relocbase + rela->r_offset);
147                 *where = (Elf_Addr)(relocbase + rela->r_addend);
148         }
149 }
150
151
152 /*
153  * Relocate a non-PLT object with addend.
154  */
155 static int
156 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
157     SymCache *cache, int flags, RtldLockState *lockstate)
158 {
159         Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
160         const Elf_Sym   *def;
161         const Obj_Entry *defobj;
162         Elf_Addr         tmp;
163
164         switch (ELF_R_TYPE(rela->r_info)) {
165
166         case R_PPC_NONE:
167                 break;
168
169         case R_PPC64_UADDR64:    /* doubleword64 S + A */
170         case R_PPC64_ADDR64:
171         case R_PPC_GLOB_DAT:
172                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
173                     flags, cache, lockstate);
174                 if (def == NULL) {
175                         return (-1);
176                 }
177
178                 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
179                     rela->r_addend);
180
181                 /* Don't issue write if unnecessary; avoid COW page fault */
182                 if (*where != tmp) {
183                         *where = tmp;
184                 }
185                 break;
186
187         case R_PPC_RELATIVE:  /* doubleword64 B + A */
188                 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
189
190                 /* As above, don't issue write unnecessarily */
191                 if (*where != tmp) {
192                         *where = tmp;
193                 }
194                 break;
195
196         case R_PPC_COPY:
197                 /*
198                  * These are deferred until all other relocations
199                  * have been done.  All we do here is make sure
200                  * that the COPY relocation is not in a shared
201                  * library.  They are allowed only in executable
202                  * files.
203                  */
204                 if (!obj->mainprog) {
205                         _rtld_error("%s: Unexpected R_COPY "
206                                     " relocation in shared library",
207                                     obj->path);
208                         return (-1);
209                 }
210                 break;
211
212         case R_PPC_JMP_SLOT:
213                 /*
214                  * These will be handled by the plt/jmpslot routines
215                  */
216                 break;
217
218         case R_PPC64_DTPMOD64:
219                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
220                     flags, cache, lockstate);
221
222                 if (def == NULL)
223                         return (-1);
224
225                 *where = (Elf_Addr) defobj->tlsindex;
226
227                 break;
228
229         case R_PPC64_TPREL64:
230                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
231                     flags, cache, lockstate);
232
233                 if (def == NULL)
234                         return (-1);
235
236                 /*
237                  * We lazily allocate offsets for static TLS as we
238                  * see the first relocation that references the
239                  * TLS block. This allows us to support (small
240                  * amounts of) static TLS in dynamically loaded
241                  * modules. If we run out of space, we generate an
242                  * error.
243                  */
244                 if (!defobj->tls_done) {
245                         if (!allocate_tls_offset((Obj_Entry*) defobj)) {
246                                 _rtld_error("%s: No space available for static "
247                                     "Thread Local Storage", obj->path);
248                                 return (-1);
249                         }
250                 }
251
252                 *(Elf_Addr **)where = *where * sizeof(Elf_Addr)
253                     + (Elf_Addr *)(def->st_value + rela->r_addend 
254                     + defobj->tlsoffset - TLS_TP_OFFSET);
255                 
256                 break;
257                 
258         case R_PPC64_DTPREL64:
259                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
260                     flags, cache, lockstate);
261
262                 if (def == NULL)
263                         return (-1);
264
265                 *where += (Elf_Addr)(def->st_value + rela->r_addend 
266                     - TLS_DTV_OFFSET);
267
268                 break;
269                 
270         default:
271                 _rtld_error("%s: Unsupported relocation type %ld"
272                             " in non-PLT relocations\n", obj->path,
273                             ELF_R_TYPE(rela->r_info));
274                 return (-1);
275         }
276         return (0);
277 }
278
279
280 /*
281  * Process non-PLT relocations
282  */
283 int
284 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
285     RtldLockState *lockstate)
286 {
287         const Elf_Rela *relalim;
288         const Elf_Rela *rela;
289         SymCache *cache;
290         int bytes = obj->dynsymcount * sizeof(SymCache);
291         int r = -1;
292
293         if ((flags & SYMLOOK_IFUNC) != 0)
294                 /* XXX not implemented */
295                 return (0);
296
297         /*
298          * The dynamic loader may be called from a thread, we have
299          * limited amounts of stack available so we cannot use alloca().
300          */
301         if (obj != obj_rtld) {
302                 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON,
303                     -1, 0);
304                 if (cache == MAP_FAILED)
305                         cache = NULL;
306         } else
307                 cache = NULL;
308
309         /*
310          * From the SVR4 PPC ABI:
311          * "The PowerPC family uses only the Elf32_Rela relocation
312          *  entries with explicit addends."
313          */
314         relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
315         for (rela = obj->rela; rela < relalim; rela++) {
316                 if (reloc_nonplt_object(obj_rtld, obj, rela, cache, flags,
317                     lockstate) < 0)
318                         goto done;
319         }
320         r = 0;
321 done:
322         if (cache)
323                 munmap(cache, bytes);
324
325         /* Synchronize icache for text seg in case we made any changes */
326         __syncicache(obj->mapbase, obj->textsize);
327
328         return (r);
329 }
330
331
332 /*
333  * Initialise a PLT slot to the resolving trampoline
334  */
335 static int
336 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela)
337 {
338         Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
339         Elf_Addr *glink;
340         long reloff;
341
342         reloff = rela - obj->pltrela;
343
344         if (obj->priv == NULL)
345                 obj->priv = xmalloc(obj->pltrelasize);
346         glink = obj->priv + reloff*sizeof(Elf_Addr)*2;
347
348         dbg(" reloc_plt_object: where=%p,reloff=%lx,glink=%p", (void *)where, reloff, glink);
349
350         memcpy(where, _rtld_bind_start, sizeof(struct funcdesc));
351         ((struct funcdesc *)(where))->env = (Elf_Addr)glink;
352         *(glink++) = (Elf_Addr)obj;
353         *(glink++) = reloff*sizeof(Elf_Rela);
354
355         return (0);
356 }
357
358
359 /*
360  * Process the PLT relocations.
361  */
362 int
363 reloc_plt(Obj_Entry *obj)
364 {
365         const Elf_Rela *relalim;
366         const Elf_Rela *rela;
367
368         if (obj->pltrelasize != 0) {
369                 relalim = (const Elf_Rela *)((char *)obj->pltrela +
370                     obj->pltrelasize);
371                 for (rela = obj->pltrela;  rela < relalim;  rela++) {
372                         assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
373
374                         if (reloc_plt_object(obj, rela) < 0) {
375                                 return (-1);
376                         }
377                 }
378         }
379
380         return (0);
381 }
382
383
384 /*
385  * LD_BIND_NOW was set - force relocation for all jump slots
386  */
387 int
388 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
389 {
390         const Obj_Entry *defobj;
391         const Elf_Rela *relalim;
392         const Elf_Rela *rela;
393         const Elf_Sym *def;
394         Elf_Addr *where;
395         Elf_Addr target;
396
397         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
398         for (rela = obj->pltrela; rela < relalim; rela++) {
399                 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT);
400                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
401                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
402                     SYMLOOK_IN_PLT | flags, NULL, lockstate);
403                 if (def == NULL) {
404                         dbg("reloc_jmpslots: sym not found");
405                         return (-1);
406                 }
407
408                 target = (Elf_Addr)(defobj->relocbase + def->st_value);
409
410 #if 0
411                 /* PG XXX */
412                 dbg("\"%s\" in \"%s\" --> %p in \"%s\"",
413                     defobj->strtab + def->st_name, basename(obj->path),
414                     (void *)target, basename(defobj->path));
415 #endif
416
417                 if (def == &sym_zero) {
418                         /* Zero undefined weak symbols */
419                         bzero(where, sizeof(struct funcdesc));
420                 } else {
421                         reloc_jmpslot(where, target, defobj, obj,
422                             (const Elf_Rel *) rela);
423                 }
424         }
425
426         obj->jmpslots_done = true;
427
428         return (0);
429 }
430
431
432 /*
433  * Update the value of a PLT jump slot.
434  */
435 Elf_Addr
436 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj,
437               const Obj_Entry *obj, const Elf_Rel *rel)
438 {
439         dbg(" reloc_jmpslot: where=%p, target=%p (%#lx + %#lx)",
440             (void *)wherep, (void *)target, *(Elf_Addr *)target,
441             (Elf_Addr)defobj->relocbase);
442
443         /*
444          * At the PLT entry pointed at by `wherep', construct
445          * a direct transfer to the now fully resolved function
446          * address.
447          */
448
449         memcpy(wherep, (void *)target, sizeof(struct funcdesc));
450         if (((struct funcdesc *)(wherep))->addr < (Elf_Addr)defobj->relocbase) {
451                 /*
452                  * XXX: It is possible (e.g. LD_BIND_NOW) that the function
453                  * descriptor we are copying has not yet been relocated.
454                  * If this happens, fix it.
455                  */
456
457                 ((struct funcdesc *)(wherep))->addr +=
458                     (Elf_Addr)defobj->relocbase;
459                 ((struct funcdesc *)(wherep))->toc +=
460                     (Elf_Addr)defobj->relocbase;
461         }
462
463         __asm __volatile("dcbst 0,%0; sync" :: "r"(wherep) : "memory");
464
465         return (target);
466 }
467
468 int
469 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
470 {
471
472         /* XXX not implemented */
473         return (0);
474 }
475
476 int
477 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
478     struct Struct_RtldLockState *lockstate)
479 {
480
481         /* XXX not implemented */
482         return (0);
483 }
484
485 void
486 init_pltgot(Obj_Entry *obj)
487 {
488 }
489
490 void
491 allocate_initial_tls(Obj_Entry *list)
492 {
493         Elf_Addr **tp;
494
495         /*
496         * Fix the size of the static TLS block by using the maximum
497         * offset allocated so far and adding a bit for dynamic modules to
498         * use.
499         */
500
501         tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
502
503         tp = (Elf_Addr **) ((char *)allocate_tls(list, NULL, TLS_TCB_SIZE, 16) 
504             + TLS_TP_OFFSET + TLS_TCB_SIZE);
505
506         __asm __volatile("mr 13,%0" :: "r"(tp));
507 }
508
509 void*
510 __tls_get_addr(tls_index* ti)
511 {
512         Elf_Addr **tp;
513         char *p;
514
515         __asm __volatile("mr %0,13" : "=r"(tp));
516         p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET 
517             - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset);
518
519         return (p + TLS_DTV_OFFSET);
520 }