]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/sparc64/reloc.c
This commit was generated by cvs2svn to compensate for changes in r171537,
[FreeBSD/FreeBSD.git] / libexec / rtld-elf / sparc64 / reloc.c
1 /*      $NetBSD: mdreloc.c,v 1.5 2001/04/25 12:24:51 kleink Exp $       */
2
3 /*-
4  * Copyright (c) 2000 Eduardo Horvath.
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Paul Kranenburg.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * $FreeBSD$
40  */
41
42 #include <sys/param.h>
43 #include <sys/mman.h>
44
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "debug.h"
52 #include "rtld.h"
53
54 /*
55  * The following table holds for each relocation type:
56  *      - the width in bits of the memory location the relocation
57  *        applies to (not currently used)
58  *      - the number of bits the relocation value must be shifted to the
59  *        right (i.e. discard least significant bits) to fit into
60  *        the appropriate field in the instruction word.
61  *      - flags indicating whether
62  *              * the relocation involves a symbol
63  *              * the relocation is relative to the current position
64  *              * the relocation is for a GOT entry
65  *              * the relocation is relative to the load address
66  *
67  */
68 #define _RF_S           0x80000000              /* Resolve symbol */
69 #define _RF_A           0x40000000              /* Use addend */
70 #define _RF_P           0x20000000              /* Location relative */
71 #define _RF_G           0x10000000              /* GOT offset */
72 #define _RF_B           0x08000000              /* Load address relative */
73 #define _RF_U           0x04000000              /* Unaligned */
74 #define _RF_SZ(s)       (((s) & 0xff) << 8)     /* memory target size */
75 #define _RF_RS(s)       ( (s) & 0xff)           /* right shift */
76 static int reloc_target_flags[] = {
77         0,                                                      /* NONE */
78         _RF_S|_RF_A|            _RF_SZ(8)  | _RF_RS(0),         /* RELOC_8 */
79         _RF_S|_RF_A|            _RF_SZ(16) | _RF_RS(0),         /* RELOC_16 */
80         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* RELOC_32 */
81         _RF_S|_RF_A|_RF_P|      _RF_SZ(8)  | _RF_RS(0),         /* DISP_8 */
82         _RF_S|_RF_A|_RF_P|      _RF_SZ(16) | _RF_RS(0),         /* DISP_16 */
83         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(0),         /* DISP_32 */
84         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(2),         /* WDISP_30 */
85         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(2),         /* WDISP_22 */
86         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(10),        /* HI22 */
87         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* 22 */
88         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* 13 */
89         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* LO10 */
90         _RF_G|                  _RF_SZ(32) | _RF_RS(0),         /* GOT10 */
91         _RF_G|                  _RF_SZ(32) | _RF_RS(0),         /* GOT13 */
92         _RF_G|                  _RF_SZ(32) | _RF_RS(10),        /* GOT22 */
93         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(0),         /* PC10 */
94         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(10),        /* PC22 */
95               _RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(2),         /* WPLT30 */
96                                 _RF_SZ(32) | _RF_RS(0),         /* COPY */
97         _RF_S|_RF_A|            _RF_SZ(64) | _RF_RS(0),         /* GLOB_DAT */
98                                 _RF_SZ(32) | _RF_RS(0),         /* JMP_SLOT */
99               _RF_A|    _RF_B|  _RF_SZ(64) | _RF_RS(0),         /* RELATIVE */
100         _RF_S|_RF_A|    _RF_U|  _RF_SZ(32) | _RF_RS(0),         /* UA_32 */
101
102               _RF_A|            _RF_SZ(32) | _RF_RS(0),         /* PLT32 */
103               _RF_A|            _RF_SZ(32) | _RF_RS(10),        /* HIPLT22 */
104               _RF_A|            _RF_SZ(32) | _RF_RS(0),         /* LOPLT10 */
105               _RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(0),         /* PCPLT32 */
106               _RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(10),        /* PCPLT22 */
107               _RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(0),         /* PCPLT10 */
108         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* 10 */
109         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* 11 */
110         _RF_S|_RF_A|            _RF_SZ(64) | _RF_RS(0),         /* 64 */
111         _RF_S|_RF_A|/*extra*/   _RF_SZ(32) | _RF_RS(0),         /* OLO10 */
112         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(42),        /* HH22 */
113         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(32),        /* HM10 */
114         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(10),        /* LM22 */
115         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(42),        /* PC_HH22 */
116         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(32),        /* PC_HM10 */
117         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(10),        /* PC_LM22 */
118         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(2),         /* WDISP16 */
119         _RF_S|_RF_A|_RF_P|      _RF_SZ(32) | _RF_RS(2),         /* WDISP19 */
120         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* GLOB_JMP */
121         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* 7 */
122         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* 5 */
123         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* 6 */
124         _RF_S|_RF_A|_RF_P|      _RF_SZ(64) | _RF_RS(0),         /* DISP64 */
125               _RF_A|            _RF_SZ(64) | _RF_RS(0),         /* PLT64 */
126         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(10),        /* HIX22 */
127         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* LOX10 */
128         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(22),        /* H44 */
129         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(12),        /* M44 */
130         _RF_S|_RF_A|            _RF_SZ(32) | _RF_RS(0),         /* L44 */
131         _RF_S|_RF_A|            _RF_SZ(64) | _RF_RS(0),         /* REGISTER */
132         _RF_S|_RF_A|    _RF_U|  _RF_SZ(64) | _RF_RS(0),         /* UA64 */
133         _RF_S|_RF_A|    _RF_U|  _RF_SZ(16) | _RF_RS(0),         /* UA16 */
134 };
135
136 #if 0
137 static const char *reloc_names[] = {
138         "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
139         "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
140         "22", "13", "LO10", "GOT10", "GOT13",
141         "GOT22", "PC10", "PC22", "WPLT30", "COPY",
142         "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
143         "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
144         "10", "11", "64", "OLO10", "HH22",
145         "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
146         "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
147         "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
148         "L44", "REGISTER", "UA64", "UA16"
149 };
150 #endif
151
152 #define RELOC_RESOLVE_SYMBOL(t)         ((reloc_target_flags[t] & _RF_S) != 0)
153 #define RELOC_PC_RELATIVE(t)            ((reloc_target_flags[t] & _RF_P) != 0)
154 #define RELOC_BASE_RELATIVE(t)          ((reloc_target_flags[t] & _RF_B) != 0)
155 #define RELOC_UNALIGNED(t)              ((reloc_target_flags[t] & _RF_U) != 0)
156 #define RELOC_USE_ADDEND(t)             ((reloc_target_flags[t] & _RF_A) != 0)
157 #define RELOC_TARGET_SIZE(t)            ((reloc_target_flags[t] >> 8) & 0xff)
158 #define RELOC_VALUE_RIGHTSHIFT(t)       (reloc_target_flags[t] & 0xff)
159
160 static long reloc_target_bitmask[] = {
161 #define _BM(x)  (~(-(1ULL << (x))))
162         0,                              /* NONE */
163         _BM(8), _BM(16), _BM(32),       /* RELOC_8, _16, _32 */
164         _BM(8), _BM(16), _BM(32),       /* DISP8, DISP16, DISP32 */
165         _BM(30), _BM(22),               /* WDISP30, WDISP22 */
166         _BM(22), _BM(22),               /* HI22, _22 */
167         _BM(13), _BM(10),               /* RELOC_13, _LO10 */
168         _BM(10), _BM(13), _BM(22),      /* GOT10, GOT13, GOT22 */
169         _BM(10), _BM(22),               /* _PC10, _PC22 */
170         _BM(30), 0,                     /* _WPLT30, _COPY */
171         _BM(32), _BM(32), _BM(32),      /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
172         _BM(32), _BM(32),               /* _UA32, PLT32 */
173         _BM(22), _BM(10),               /* _HIPLT22, LOPLT10 */
174         _BM(32), _BM(22), _BM(10),      /* _PCPLT32, _PCPLT22, _PCPLT10 */
175         _BM(10), _BM(11), -1,           /* _10, _11, _64 */
176         _BM(10), _BM(22),               /* _OLO10, _HH22 */
177         _BM(10), _BM(22),               /* _HM10, _LM22 */
178         _BM(22), _BM(10), _BM(22),      /* _PC_HH22, _PC_HM10, _PC_LM22 */
179         _BM(16), _BM(19),               /* _WDISP16, _WDISP19 */
180         -1,                             /* GLOB_JMP */
181         _BM(7), _BM(5), _BM(6),         /* _7, _5, _6 */
182         -1, -1,                         /* DISP64, PLT64 */
183         _BM(22), _BM(13),               /* HIX22, LOX10 */
184         _BM(22), _BM(10), _BM(13),      /* H44, M44, L44 */
185         -1, -1, _BM(16),                /* REGISTER, UA64, UA16 */
186 #undef _BM
187 };
188 #define RELOC_VALUE_BITMASK(t)  (reloc_target_bitmask[t])
189
190 #undef flush
191 #define flush(va, offs) \
192         __asm __volatile("flush %0 + %1" : : "r" (va), "I" (offs));
193
194 static int reloc_nonplt_object(Obj_Entry *obj, const Elf_Rela *rela,
195                                SymCache *cache);
196 static void install_plt(Elf_Word *pltgot, Elf_Addr proc);
197
198 extern char _rtld_bind_start_0[];
199 extern char _rtld_bind_start_1[];
200
201 int
202 do_copy_relocations(Obj_Entry *dstobj)
203 {
204         const Elf_Rela *relalim;
205         const Elf_Rela *rela;
206         const Elf_Sym *dstsym;
207         const Elf_Sym *srcsym;
208         const Ver_Entry *ve;
209         void *dstaddr;
210         const void *srcaddr;
211         Obj_Entry *srcobj;
212         unsigned long hash;
213         const char *name;
214         size_t size;
215
216         assert(dstobj->mainprog);   /* COPY relocations are invalid elsewhere */
217
218         relalim = (const Elf_Rela *)((caddr_t)dstobj->rela + dstobj->relasize);
219         for (rela = dstobj->rela; rela < relalim; rela++) {
220                 if (ELF_R_TYPE(rela->r_info) == R_SPARC_COPY) {
221                         dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
222                         dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
223                         name = dstobj->strtab + dstsym->st_name;
224                         hash = elf_hash(name);
225                         size = dstsym->st_size;
226                         ve = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
227
228                         for (srcobj = dstobj->next; srcobj != NULL;
229                             srcobj = srcobj->next)
230                                 if ((srcsym = symlook_obj(name, hash, srcobj,
231                                     ve, 0)) != NULL)
232                                         break;
233
234                         if (srcobj == NULL) {
235                                 _rtld_error("Undefined symbol \"%s\""
236                                             "referenced from COPY relocation"
237                                             "in %s", name, dstobj->path);
238                                 return (-1);
239                         }
240
241                         srcaddr = (const void *)(srcobj->relocbase +
242                             srcsym->st_value);
243                         memcpy(dstaddr, srcaddr, size);
244                 }
245         }
246
247         return (0);
248 }
249
250 int
251 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
252 {
253         const Elf_Rela *relalim;
254         const Elf_Rela *rela;
255         SymCache *cache;
256         int bytes = obj->nchains * sizeof(SymCache);
257         int r = -1;
258
259         /*
260          * The dynamic loader may be called from a thread, we have
261          * limited amounts of stack available so we cannot use alloca().
262          */
263         if (obj != obj_rtld) {
264                 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON,
265                     -1, 0);
266                 if (cache == MAP_FAILED)
267                         cache = NULL;
268         } else
269                 cache = NULL;
270
271         relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
272         for (rela = obj->rela; rela < relalim; rela++) {
273                 if (reloc_nonplt_object(obj, rela, cache) < 0)
274                         goto done;
275         }
276         r = 0;
277 done:
278         if (cache)
279                 munmap(cache, bytes);
280         return (r);
281 }
282
283 static int
284 reloc_nonplt_object(Obj_Entry *obj, const Elf_Rela *rela, SymCache *cache)
285 {
286         const Obj_Entry *defobj;
287         const Elf_Sym *def;
288         Elf_Addr *where;
289         Elf_Word *where32;
290         Elf_Word type;
291         Elf_Addr value;
292         Elf_Addr mask;
293
294         where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
295         where32 = (Elf_Word *)where;
296         defobj = NULL;
297         def = NULL;
298
299         type = ELF_R_TYPE(rela->r_info);
300         if (type == R_SPARC_NONE)
301                 return (0);
302
303         /* We do JMP_SLOTs below */
304         if (type == R_SPARC_JMP_SLOT)
305                 return (0);
306
307         /* COPY relocs are also handled elsewhere */
308         if (type == R_SPARC_COPY)
309                 return (0);
310
311         /*
312          * Note: R_SPARC_UA16 must be numerically largest relocation type.
313          */
314         if (type > R_SPARC_UA16)
315                 return (-1);
316
317         value = rela->r_addend;
318
319         /*
320          * Handle relative relocs here, because we might not
321          * be able to access globals yet.
322          */
323         if (type == R_SPARC_RELATIVE) {
324                 /* XXXX -- apparently we ignore the preexisting value */
325                 *where = (Elf_Addr)(obj->relocbase + value);
326                 return (0);
327         }
328
329         /*
330          * If we get here while relocating rtld itself, we will crash because
331          * a non-local variable is accessed.
332          */
333         if (RELOC_RESOLVE_SYMBOL(type)) {
334
335                 /* Find the symbol */
336                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
337                     false, cache);
338                 if (def == NULL)
339                         return (-1);
340
341                 /* Add in the symbol's absolute address */
342                 value += (Elf_Addr)(defobj->relocbase + def->st_value);
343         }
344
345         if (RELOC_PC_RELATIVE(type))
346                 value -= (Elf_Addr)where;
347
348         if (RELOC_BASE_RELATIVE(type)) {
349                 /*
350                  * Note that even though sparcs use `Elf_rela' exclusively
351                  * we still need the implicit memory addend in relocations
352                  * referring to GOT entries. Undoubtedly, someone f*cked
353                  * this up in the distant past, and now we're stuck with
354                  * it in the name of compatibility for all eternity..
355                  *
356                  * In any case, the implicit and explicit should be mutually
357                  * exclusive. We provide a check for that here.
358                  */
359                 /* XXXX -- apparently we ignore the preexisting value */
360                 value += (Elf_Addr)(obj->relocbase);
361         }
362
363         mask = RELOC_VALUE_BITMASK(type);
364         value >>= RELOC_VALUE_RIGHTSHIFT(type);
365         value &= mask;
366
367         if (RELOC_UNALIGNED(type)) {
368                 /* Handle unaligned relocations. */
369                 Elf_Addr tmp;
370                 char *ptr;
371                 int size;
372                 int i;
373
374                 size = RELOC_TARGET_SIZE(type) / 8;
375                 ptr = (char *)where;
376                 tmp = 0;
377
378                 /* Read it in one byte at a time. */
379                 for (i = 0; i < size; i++)
380                         tmp = (tmp << 8) | ptr[i];
381
382                 tmp &= ~mask;
383                 tmp |= value;
384
385                 /* Write it back out. */
386                 for (i = 0; i < size; i++)
387                         ptr[i] = ((tmp >> ((size - i - 1) * 8)) & 0xff);
388         } else if (RELOC_TARGET_SIZE(type) > 32) {
389                 *where &= ~mask;
390                 *where |= value;
391         } else {
392                 *where32 &= ~mask;
393                 *where32 |= value;
394         }
395
396         return (0);
397 }
398
399 int
400 reloc_plt(Obj_Entry *obj)
401 {
402 #if 0
403         const Obj_Entry *defobj;
404         const Elf_Rela *relalim;
405         const Elf_Rela *rela;
406         const Elf_Sym *def;
407         Elf_Addr *where;
408         Elf_Addr value;
409
410         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
411         for (rela = obj->pltrela; rela < relalim; rela++) {
412                 if (rela->r_addend == 0)
413                         continue;
414                 assert(ELF_R_TYPE(rela->r_info) == R_SPARC_JMP_SLOT);
415                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
416                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
417                     true, NULL);
418                 value = (Elf_Addr)(defobj->relocbase + def->st_value);
419                 *where = value;
420         }
421 #endif
422         return (0);
423 }
424
425 /*
426  * Instruction templates:
427  */
428 #define BAA     0x10400000      /*      ba,a    %xcc, 0 */
429 #define SETHI   0x03000000      /*      sethi   %hi(0), %g1 */
430 #define JMP     0x81c06000      /*      jmpl    %g1+%lo(0), %g0 */
431 #define NOP     0x01000000      /*      sethi   %hi(0), %g0 */
432 #define OR      0x82806000      /*      or      %g1, 0, %g1 */
433 #define XOR     0x82c06000      /*      xor     %g1, 0, %g1 */
434 #define MOV71   0x8283a000      /*      or      %o7, 0, %g1 */
435 #define MOV17   0x9c806000      /*      or      %g1, 0, %o7 */
436 #define CALL    0x40000000      /*      call    0 */
437 #define SLLX    0x8b407000      /*      sllx    %g1, 0, %g1 */
438 #define SETHIG5 0x0b000000      /*      sethi   %hi(0), %g5 */
439 #define ORG5    0x82804005      /*      or      %g1, %g5, %g1 */
440
441
442 /* %hi(v) with variable shift */
443 #define HIVAL(v, s)     (((v) >> (s)) &  0x003fffff)
444 #define LOVAL(v)        ((v) & 0x000003ff)
445
446 int
447 reloc_jmpslots(Obj_Entry *obj)
448 {
449         const Obj_Entry *defobj;
450         const Elf_Rela *relalim;
451         const Elf_Rela *rela;
452         const Elf_Sym *def;
453         Elf_Addr *where;
454         Elf_Addr target;
455
456         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
457         for (rela = obj->pltrela; rela < relalim; rela++) {
458                 assert(ELF_R_TYPE(rela->r_info) == R_SPARC_JMP_SLOT);
459                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
460                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
461                     true, NULL);
462                 if (def == NULL)
463                         return -1;
464                 target = (Elf_Addr)(defobj->relocbase + def->st_value);
465                 reloc_jmpslot(where, target, defobj, obj, (Elf_Rel *)rela);
466         }
467         obj->jmpslots_done = true;
468         return (0);
469 }
470
471 Elf_Addr
472 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *obj,
473               const Obj_Entry *refobj, const Elf_Rel *rel)
474 {
475         const Elf_Rela *rela = (const Elf_Rela *)rel;
476         Elf_Addr offset;
477         Elf_Word *where;
478
479         if (rela - refobj->pltrela < 32764) {
480                 /*
481                  * At the PLT entry pointed at by `where', we now construct
482                  * a direct transfer to the now fully resolved function
483                  * address.
484                  *
485                  * A PLT entry is supposed to start by looking like this:
486                  *
487                  *      sethi   (. - .PLT0), %g1
488                  *      ba,a    %xcc, .PLT1
489                  *      nop
490                  *      nop
491                  *      nop
492                  *      nop
493                  *      nop
494                  *      nop
495                  *
496                  * When we replace these entries we start from the second
497                  * entry and do it in reverse order so the last thing we
498                  * do is replace the branch.  That allows us to change this
499                  * atomically.
500                  *
501                  * We now need to find out how far we need to jump.  We
502                  * have a choice of several different relocation techniques
503                  * which are increasingly expensive.
504                  */
505                 where = (Elf_Word *)wherep;
506                 offset = ((Elf_Addr)where) - target;
507                 if (offset <= (1L<<20) && offset >= -(1L<<20)) {
508                         /*
509                          * We're within 1MB -- we can use a direct branch insn.
510                          *
511                          * We can generate this pattern:
512                          *
513                          *      sethi   %hi(. - .PLT0), %g1
514                          *      ba,a    %xcc, addr
515                          *      nop
516                          *      nop
517                          *      nop
518                          *      nop
519                          *      nop
520                          *      nop
521                          *
522                          */
523                         where[1] = BAA | ((offset >> 2) &0x3fffff);
524                         flush(where, 4);
525                 } else if (target >= 0 && target < (1L<<32)) {
526                         /*
527                          * We're withing 32-bits of address zero.
528                          *
529                          * The resulting code in the jump slot is:
530                          *
531                          *      sethi   %hi(. - .PLT0), %g1
532                          *      sethi   %hi(addr), %g1
533                          *      jmp     %g1+%lo(addr)
534                          *      nop
535                          *      nop
536                          *      nop
537                          *      nop
538                          *      nop
539                          *
540                          */
541                         where[2] = JMP   | LOVAL(target);
542                         flush(where, 8);
543                         where[1] = SETHI | HIVAL(target, 10);
544                         flush(where, 4);
545                 } else if (target <= 0 && target > -(1L<<32)) {
546                         /*
547                          * We're withing 32-bits of address -1.
548                          *
549                          * The resulting code in the jump slot is:
550                          *
551                          *      sethi   %hi(. - .PLT0), %g1
552                          *      sethi   %hix(addr), %g1
553                          *      xor     %g1, %lox(addr), %g1
554                          *      jmp     %g1
555                          *      nop
556                          *      nop
557                          *      nop
558                          *      nop
559                          *
560                          */
561                         where[3] = JMP;
562                         flush(where, 12);
563                         where[2] = XOR | ((~target) & 0x00001fff);
564                         flush(where, 8);
565                         where[1] = SETHI | HIVAL(~target, 10);
566                         flush(where, 4);
567                 } else if (offset <= (1L<<32) && offset >= -((1L<<32) - 4)) {
568                         /*
569                          * We're withing 32-bits -- we can use a direct call
570                          * insn
571                          *
572                          * The resulting code in the jump slot is:
573                          *
574                          *      sethi   %hi(. - .PLT0), %g1
575                          *      mov     %o7, %g1
576                          *      call    (.+offset)
577                          *       mov    %g1, %o7
578                          *      nop
579                          *      nop
580                          *      nop
581                          *      nop
582                          *
583                          */
584                         where[3] = MOV17;
585                         flush(where, 12);
586                         where[2] = CALL   | ((offset >> 4) & 0x3fffffff);
587                         flush(where, 8);
588                         where[1] = MOV71;
589                         flush(where, 4);
590                 } else if (offset >= 0 && offset < (1L<<44)) {
591                         /*
592                          * We're withing 44 bits.  We can generate this pattern:
593                          *
594                          * The resulting code in the jump slot is:
595                          *
596                          *      sethi   %hi(. - .PLT0), %g1
597                          *      sethi   %h44(addr), %g1
598                          *      or      %g1, %m44(addr), %g1
599                          *      sllx    %g1, 12, %g1
600                          *      jmp     %g1+%l44(addr)
601                          *      nop
602                          *      nop
603                          *      nop
604                          *
605                          */
606                         where[4] = JMP   | LOVAL(offset);
607                         flush(where, 16);
608                         where[3] = SLLX  | 12;
609                         flush(where, 12);
610                         where[2] = OR    | (((offset) >> 12) & 0x00001fff);
611                         flush(where, 8);
612                         where[1] = SETHI | HIVAL(offset, 22);
613                         flush(where, 4);
614                 } else if (offset < 0 && offset > -(1L<<44)) {
615                         /*
616                          * We're withing 44 bits.  We can generate this pattern:
617                          *
618                          * The resulting code in the jump slot is:
619                          *
620                          *      sethi   %hi(. - .PLT0), %g1
621                          *      sethi   %h44(-addr), %g1
622                          *      xor     %g1, %m44(-addr), %g1
623                          *      sllx    %g1, 12, %g1
624                          *      jmp     %g1+%l44(addr)
625                          *      nop
626                          *      nop
627                          *      nop
628                          *
629                          */
630                         where[4] = JMP   | LOVAL(offset);
631                         flush(where, 16);
632                         where[3] = SLLX  | 12;
633                         flush(where, 12);
634                         where[2] = XOR   | (((~offset) >> 12) & 0x00001fff);
635                         flush(where, 8);
636                         where[1] = SETHI | HIVAL(~offset, 22);
637                         flush(where, 4);
638                 } else {
639                         /*
640                          * We need to load all 64-bits
641                          *
642                          * The resulting code in the jump slot is:
643                          *
644                          *      sethi   %hi(. - .PLT0), %g1
645                          *      sethi   %hh(addr), %g1
646                          *      sethi   %lm(addr), %g5
647                          *      or      %g1, %hm(addr), %g1
648                          *      sllx    %g1, 32, %g1
649                          *      or      %g1, %g5, %g1
650                          *      jmp     %g1+%lo(addr)
651                          *      nop
652                          *
653                          */
654                         where[6] = JMP     | LOVAL(target);
655                         flush(where, 24);
656                         where[5] = ORG5;
657                         flush(where, 20);
658                         where[4] = SLLX    | 32;
659                         flush(where, 16);
660                         where[3] = OR      | LOVAL((target) >> 32);
661                         flush(where, 12);
662                         where[2] = SETHIG5 | HIVAL(target, 10);
663                         flush(where, 8);
664                         where[1] = SETHI   | HIVAL(target, 42);
665                         flush(where, 4);
666                 }
667         } else {
668                 /*
669                  * This is a high PLT slot; the relocation offset specifies a
670                  * pointer that needs to be frobbed; no actual code needs to
671                  * be modified. The pointer to be calculated needs the addend
672                  * added and the reference object relocation base subtraced.
673                  */
674                 *wherep = target + rela->r_addend -
675                     (Elf_Addr)refobj->relocbase;
676         }
677
678         return (target);
679 }
680
681 /*
682  * Install rtld function call into this PLT slot.
683  */
684 #define SAVE            0x9de3bf50
685 #define SETHI_l0        0x21000000
686 #define SETHI_l1        0x23000000
687 #define OR_l0_l0        0xa0142000
688 #define SLLX_l0_32_l0   0xa12c3020
689 #define OR_l0_l1_l0     0xa0140011
690 #define JMPL_l0_o1      0x93c42000
691 #define MOV_g1_o0       0x90100001
692
693 void
694 init_pltgot(Obj_Entry *obj)
695 {
696         Elf_Word *entry;
697
698         if (obj->pltgot != NULL) {
699                 entry = (Elf_Word *)obj->pltgot;
700                 install_plt(&entry[0], (Elf_Addr)_rtld_bind_start_0);
701                 install_plt(&entry[8], (Elf_Addr)_rtld_bind_start_1);
702                 obj->pltgot[8] = (Elf_Addr)obj;
703         }
704 }
705
706 static void
707 install_plt(Elf_Word *pltgot, Elf_Addr proc)
708 {
709         pltgot[0] = SAVE;
710         flush(pltgot, 0);
711         pltgot[1] = SETHI_l0 | HIVAL(proc, 42);
712         flush(pltgot, 4);
713         pltgot[2] = SETHI_l1 | HIVAL(proc, 10);
714         flush(pltgot, 8);
715         pltgot[3] = OR_l0_l0 | LOVAL((proc) >> 32);
716         flush(pltgot, 12);
717         pltgot[4] = SLLX_l0_32_l0;
718         flush(pltgot, 16);
719         pltgot[5] = OR_l0_l1_l0;
720         flush(pltgot, 20);
721         pltgot[6] = JMPL_l0_o1 | LOVAL(proc);
722         flush(pltgot, 24);
723         pltgot[7] = MOV_g1_o0;
724         flush(pltgot, 28);
725 }
726
727 void
728 allocate_initial_tls(Obj_Entry *objs)
729 {
730     Elf_Addr* tpval;
731
732     /*
733      * Fix the size of the static TLS block by using the maximum
734      * offset allocated so far and adding a bit for dynamic modules to
735      * use.
736      */
737     tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
738     tpval = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr));
739     __asm __volatile("mov %0, %%g7" : : "r" (tpval));
740 }
741
742 void *__tls_get_addr(tls_index *ti)
743 {
744     register Elf_Addr** tp __asm__("%g7");
745
746     return tls_get_addr_common(tp, ti->ti_module, ti->ti_offset);
747 }