]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/libexec/rtld-elf/alpha/reloc.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / libexec / rtld-elf / alpha / reloc.c
1 /*-
2  * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27
28 /*
29  * Dynamic linker for ELF.
30  *
31  * John Polstra <jdp@polstra.com>.
32  */
33
34 #include <sys/param.h>
35 #include <sys/mman.h>
36
37 #include <dlfcn.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #include "debug.h"
48 #include "rtld.h"
49
50 extern Elf_Dyn _GOT_END_;
51
52 /*
53  * Macros for loading/storing unaligned 64-bit values.  These are
54  * needed because relocations can point to unaligned data.  This
55  * occurs in the DWARF2 exception frame tables generated by the
56  * compiler, for instance.
57  *
58  * We don't use these when relocating jump slots and GOT entries,
59  * since they are guaranteed to be aligned.
60  */
61
62 struct ualong {
63         Elf_Addr x __attribute__((packed));
64 };
65
66 #define load64(p)       (((struct ualong *)(p))->x)
67 #define store64(p,v)    (((struct ualong *)(p))->x = (v))
68
69 /* Relocate a non-PLT object with addend. */
70 static int
71 reloc_non_plt_obj(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela,
72         SymCache *cache)
73 {
74         Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
75
76         switch (ELF_R_TYPE(rela->r_info)) {
77
78                 case R_ALPHA_NONE:
79                         break;
80
81                 case R_ALPHA_REFQUAD: {
82                         const Elf_Sym *def;
83                         const Obj_Entry *defobj;
84
85                         def = find_symdef(ELF_R_SYM(rela->r_info), obj,
86                             &defobj, false, cache);
87                         if (def == NULL)
88                                 return -1;
89                         store64(where,
90                             (Elf_Addr) (defobj->relocbase + def->st_value) +
91                             load64(where) + rela->r_addend);
92                 }
93                 break;
94
95                 case R_ALPHA_GLOB_DAT: {
96                         const Elf_Sym *def;
97                         const Obj_Entry *defobj;
98                         Elf_Addr val;
99
100                         def = find_symdef(ELF_R_SYM(rela->r_info), obj,
101                             &defobj, false, cache);
102                         if (def == NULL)
103                                 return -1;
104                         val = (Elf_Addr) (defobj->relocbase + def->st_value +
105                             rela->r_addend);
106                         if (load64(where) != val)
107                                 store64(where, val);
108                 }
109                 break;
110
111                 case R_ALPHA_RELATIVE: {
112                         if (obj != obj_rtld ||
113                             (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
114                             (caddr_t)where >= (caddr_t)&_GOT_END_)
115                                 store64(where,
116                                     load64(where) + (Elf_Addr) obj->relocbase);
117                 }
118                 break;
119
120                 case R_ALPHA_COPY: {
121                         /*
122                          * These are deferred until all other relocations
123                          * have been done.  All we do here is make sure
124                          * that the COPY relocation is not in a shared
125                          * library.  They are allowed only in executable
126                          * files.
127                         */
128                         if (!obj->mainprog) {
129                                 _rtld_error("%s: Unexpected R_COPY "
130                                     " relocation in shared library",
131                                     obj->path);
132                                 return -1;
133                         }
134                 }
135                 break;
136
137                 default:
138                         _rtld_error("%s: Unsupported relocation type %u"
139                             " in non-PLT relocations\n", obj->path,
140                             (unsigned int)ELF_R_TYPE(rela->r_info));
141                         return -1;
142         }
143         return(0);
144 }
145
146 /* Process the non-PLT relocations. */
147 int
148 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
149 {
150         const Elf_Rel *rellim;
151         const Elf_Rel *rel;
152         const Elf_Rela *relalim;
153         const Elf_Rela *rela;
154         SymCache *cache;
155         int bytes = obj->nchains * sizeof(SymCache);
156         int r = -1;
157
158         /*
159          * The dynamic loader may be called from a thread, we have
160          * limited amounts of stack available so we cannot use alloca().
161          */
162         cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
163         if (cache == MAP_FAILED)
164             cache = NULL;
165
166         /* Perform relocations without addend if there are any: */
167         rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
168         for (rel = obj->rel;  obj->rel != NULL && rel < rellim;  rel++) {
169                 Elf_Rela locrela;
170
171                 locrela.r_info = rel->r_info;
172                 locrela.r_offset = rel->r_offset;
173                 locrela.r_addend = 0;
174                 if (reloc_non_plt_obj(obj_rtld, obj, &locrela, cache))
175                         goto done;
176         }
177
178         /* Perform relocations with addend if there are any: */
179         relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
180         for (rela = obj->rela;  obj->rela != NULL && rela < relalim;  rela++) {
181                 if (reloc_non_plt_obj(obj_rtld, obj, rela, cache))
182                         goto done;
183         }
184         r = 0;
185 done:
186         if (cache)
187             munmap(cache, bytes);
188         return(r);
189 }
190
191 /* Process the PLT relocations. */
192 int
193 reloc_plt(Obj_Entry *obj)
194 {
195     /* All PLT relocations are the same kind: either Elf_Rel or Elf_Rela. */
196     if (obj->pltrelsize != 0) {
197         const Elf_Rel *rellim;
198         const Elf_Rel *rel;
199
200         rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
201         for (rel = obj->pltrel;  rel < rellim;  rel++) {
202             Elf_Addr *where;
203
204             assert(ELF_R_TYPE(rel->r_info) == R_ALPHA_JMP_SLOT);
205
206             /* Relocate the GOT slot pointing into the PLT. */
207             where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
208             *where += (Elf_Addr)obj->relocbase;
209         }
210     } else {
211         const Elf_Rela *relalim;
212         const Elf_Rela *rela;
213
214         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
215         for (rela = obj->pltrela;  rela < relalim;  rela++) {
216             Elf_Addr *where;
217
218             assert(ELF_R_TYPE(rela->r_info) == R_ALPHA_JMP_SLOT);
219
220             /* Relocate the GOT slot pointing into the PLT. */
221             where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
222             *where += (Elf_Addr)obj->relocbase;
223         }
224     }
225     return 0;
226 }
227
228 /* Relocate the jump slots in an object. */
229 int
230 reloc_jmpslots(Obj_Entry *obj)
231 {
232     if (obj->jmpslots_done)
233         return 0;
234     /* All PLT relocations are the same kind: either Elf_Rel or Elf_Rela. */
235     if (obj->pltrelsize != 0) {
236         const Elf_Rel *rellim;
237         const Elf_Rel *rel;
238
239         rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
240         for (rel = obj->pltrel;  rel < rellim;  rel++) {
241             Elf_Addr *where;
242             const Elf_Sym *def;
243             const Obj_Entry *defobj;
244
245             assert(ELF_R_TYPE(rel->r_info) == R_ALPHA_JMP_SLOT);
246             where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
247             def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true,
248                 NULL);
249             if (def == NULL)
250                 return -1;
251             reloc_jmpslot(where,
252               (Elf_Addr)(defobj->relocbase + def->st_value),
253               defobj, obj, rel);
254         }
255     } else {
256         const Elf_Rela *relalim;
257         const Elf_Rela *rela;
258
259         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
260         for (rela = obj->pltrela;  rela < relalim;  rela++) {
261             Elf_Addr *where;
262             const Elf_Sym *def;
263             const Obj_Entry *defobj;
264
265             assert(ELF_R_TYPE(rela->r_info) == R_ALPHA_JMP_SLOT);
266             where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
267             def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true,
268                 NULL);
269             if (def == NULL)
270                 return -1;
271             reloc_jmpslot(where,
272               (Elf_Addr)(defobj->relocbase + def->st_value),
273               defobj, obj, (Elf_Rel *)rela);
274         }
275     }
276     obj->jmpslots_done = true;
277     return 0;
278 }
279
280 /* Fixup the jump slot at "where" to transfer control to "target". */
281 Elf_Addr
282 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *obj,
283               const Obj_Entry *refobj, const Elf_Rel *rel)
284 {
285     Elf_Addr stubaddr;
286
287     dbg(" reloc_jmpslot: where=%p, target=%p", (void *)where, (void *)target);
288     stubaddr = *where;
289     if (stubaddr != target) {
290         int64_t delta;
291         u_int32_t inst[3];
292         int instct;
293         Elf_Addr pc;
294         int64_t idisp;
295         u_int32_t *stubptr;
296
297         /* Point this GOT entry directly at the target. */
298         *where = target;
299
300         /*
301          * There may be multiple GOT tables, each with an entry
302          * pointing to the stub in the PLT.  But we can only find and
303          * fix up the first GOT entry.  So we must rewrite the stub as
304          * well, to perform a call to the target if it is executed.
305          *
306          * When the stub gets control, register pv ($27) contains its
307          * address.  We adjust its value so that it points to the
308          * target, and then jump indirect through it.
309          *
310          * Each PLT entry has room for 3 instructions.  If the
311          * adjustment amount fits in a signed 32-bit integer, we can
312          * simply add it to register pv.  Otherwise we must load the
313          * GOT entry itself into the pv register.
314          */
315         delta = target - stubaddr;
316         dbg("  stubaddr=%p, where-stubaddr=%ld, delta=%ld", (void *)stubaddr,
317           (long)where - (long)stubaddr, (long)delta);
318         instct = 0;
319         if ((int32_t)delta == delta) {
320             /*
321              * We can adjust pv with a LDA, LDAH sequence.
322              *
323              * First build an LDA instruction to adjust the low 16 bits.
324              */
325             inst[instct++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
326               (delta & 0xffff);
327             dbg("  LDA  $27,%d($27)", (int16_t)delta);
328             /*
329              * Adjust the delta to account for the effects of the LDA,
330              * including sign-extension.
331              */
332             delta -= (int16_t)delta;
333             if (delta != 0) {
334                 /* Build an LDAH instruction to adjust the high 16 bits. */
335                 inst[instct++] = 0x09 << 26 | 27 << 21 | 27 << 16 |
336                   (delta >> 16 & 0xffff);
337                 dbg("  LDAH $27,%d($27)", (int16_t)(delta >> 16));
338             }
339         } else {
340             int64_t dhigh;
341
342             /* We must load the GOT entry from memory. */
343             delta = (Elf_Addr)where - stubaddr;
344             /*
345              * If the GOT entry is too far away from the PLT entry,
346              * then punt. This PLT entry will have to be looked up
347              * manually for all GOT entries except the first one.
348              * The program will still run, albeit very slowly.  It's
349              * extremely unlikely that this case could ever arise in
350              * practice, but we might as well handle it correctly if
351              * it does.
352              */
353             if ((int32_t)delta != delta) {
354                 dbg("  PLT stub too far from GOT to relocate");
355                 return target;
356             }
357             dhigh = delta - (int16_t)delta;
358             if (dhigh != 0) {
359                 /* Build an LDAH instruction to adjust the high 16 bits. */
360                 inst[instct++] = 0x09 << 26 | 27 << 21 | 27 << 16 |
361                   (dhigh >> 16 & 0xffff);
362                 dbg("  LDAH $27,%d($27)", (int16_t)(dhigh >> 16));
363             }
364             /* Build an LDQ to load the GOT entry. */
365             inst[instct++] = 0x29 << 26 | 27 << 21 | 27 << 16 |
366               (delta & 0xffff);
367             dbg("  LDQ  $27,%d($27)", (int16_t)delta);
368         }
369
370         /*
371          * Build a JMP or BR instruction to jump to the target.  If
372          * the instruction displacement fits in a sign-extended 21-bit
373          * field, we can use the more efficient BR instruction.
374          * Otherwise we have to jump indirect through the pv register.
375          */
376         pc = stubaddr + 4 * (instct + 1);
377         idisp = (int64_t)(target - pc) >> 2;
378         if (-0x100000 <= idisp && idisp < 0x100000) {
379             inst[instct++] = 0x30 << 26 | 31 << 21 | (idisp & 0x1fffff);
380             dbg("  BR   $31,%p", (void *)target);
381         } else {
382             inst[instct++] = 0x1a << 26 | 31 << 21 | 27 << 16 |
383               (idisp & 0x3fff);
384             dbg("  JMP  $31,($27),%d", (int)(idisp & 0x3fff));
385         }
386
387         /*
388          * Fill in the tail of the PLT entry first for reentrancy.
389          * Until we have overwritten the first instruction (an
390          * unconditional branch), the remaining instructions have no
391          * effect.
392          */
393         stubptr = (u_int32_t *)stubaddr;
394         while (instct > 1) {
395             instct--;
396             stubptr[instct] = inst[instct];
397         }
398         /*
399          * Commit the tail of the instruction sequence to memory
400          * before overwriting the first instruction.
401          */
402         __asm__ __volatile__("wmb" : : : "memory");
403         stubptr[0] = inst[0];
404     }
405
406     return target;
407 }
408
409 /* Process an R_ALPHA_COPY relocation. */
410 static int
411 do_copy_relocation(Obj_Entry *dstobj, const Elf_Rela *rela)
412 {
413         void *dstaddr;
414         const Elf_Sym *dstsym;
415         const char *name;
416         unsigned long hash;
417         size_t size;
418         const void *srcaddr;
419         const Elf_Sym *srcsym;
420         Obj_Entry *srcobj;
421
422         dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
423         dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
424         name = dstobj->strtab + dstsym->st_name;
425         hash = elf_hash(name);
426         size = dstsym->st_size;
427
428         for (srcobj = dstobj->next;  srcobj != NULL;  srcobj = srcobj->next)
429                 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
430                         break;
431
432         if (srcobj == NULL) {
433                 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
434                     " relocation in %s", name, dstobj->path);
435                 return -1;
436         }
437
438         srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
439         memcpy(dstaddr, srcaddr, size);
440         return 0;
441 }
442
443 /*
444  * Process the special R_ALPHA_COPY relocations in the main program.  These
445  * copy data from a shared object into a region in the main program's BSS
446  * segment.
447  *
448  * Returns 0 on success, -1 on failure.
449  */
450 int
451 do_copy_relocations(Obj_Entry *dstobj)
452 {
453         const Elf_Rel *rellim;
454         const Elf_Rel *rel;
455         const Elf_Rela *relalim;
456         const Elf_Rela *rela;
457
458         assert(dstobj->mainprog);       /* COPY relocations are invalid elsewhere */
459
460         rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
461         for (rel = dstobj->rel; dstobj->rel != NULL && rel < rellim;  rel++) {
462                 if (ELF_R_TYPE(rel->r_info) == R_ALPHA_COPY) {
463                         Elf_Rela locrela;
464
465                         locrela.r_info = rel->r_info;
466                         locrela.r_offset = rel->r_offset;
467                         locrela.r_addend = 0;
468                         if (do_copy_relocation(dstobj, &locrela))
469                                 return -1;
470                 }
471         }
472
473         relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela +
474             dstobj->relasize);
475         for (rela = dstobj->rela; dstobj->rela != NULL && rela < relalim;
476             rela++) {
477                 if (ELF_R_TYPE(rela->r_info) == R_ALPHA_COPY) {
478                         if (do_copy_relocation(dstobj, rela))
479                                 return -1;
480                 }
481         }
482
483         return 0;
484 }
485
486 /* Initialize the special PLT entries. */
487 void
488 init_pltgot(Obj_Entry *obj)
489 {
490         u_int32_t *pltgot;
491
492         if (obj->pltgot != NULL &&
493             (obj->pltrelsize != 0 || obj->pltrelasize != 0)) {
494                 /*
495                  * This function will be called to perform the relocation.
496                  * Look for the ldah instruction from the old PLT format since
497                  * that will tell us what format we are trying to relocate.
498                  */
499                 pltgot = (u_int32_t *) obj->pltgot;
500                 if ((pltgot[8] & 0xffff0000) == 0x279f0000)
501                         obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
502                 else
503                         obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
504                 /* Identify this shared object */
505                 obj->pltgot[3] = (Elf_Addr) obj;
506         }
507 }
508
509 void
510 allocate_initial_tls(Obj_Entry *list)
511 {
512     void *tls;
513
514     /*
515      * Fix the size of the static TLS block by using the maximum
516      * offset allocated so far and adding a bit for dynamic modules to
517      * use.
518      */
519     tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
520     tls = allocate_tls(list, 0, 16, 16);
521     alpha_pal_wrunique((u_int64_t) tls);
522 }
523
524 void *__tls_get_addr(tls_index* ti)
525 {
526     Elf_Addr** tp = (Elf_Addr**) alpha_pal_rdunique();
527
528     return tls_get_addr_common(tp, ti->ti_module, ti->ti_offset);
529 }