]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/sparc64/reloc.c
This commit was generated by cvs2svn to compensate for changes in r169962,
[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         cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
264         if (cache == MAP_FAILED)
265                 cache = NULL;
266
267         relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
268         for (rela = obj->rela; rela < relalim; rela++) {
269                 if (reloc_nonplt_object(obj, rela, cache) < 0)
270                         goto done;
271         }
272         r = 0;
273 done:
274         if (cache)
275                 munmap(cache, bytes);
276         return (r);
277 }
278
279 static int
280 reloc_nonplt_object(Obj_Entry *obj, const Elf_Rela *rela, SymCache *cache)
281 {
282         const Obj_Entry *defobj;
283         const Elf_Sym *def;
284         Elf_Addr *where;
285         Elf_Word *where32;
286         Elf_Word type;
287         Elf_Addr value;
288         Elf_Addr mask;
289
290         where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
291         where32 = (Elf_Word *)where;
292         defobj = NULL;
293         def = NULL;
294
295         type = ELF_R_TYPE(rela->r_info);
296         if (type == R_SPARC_NONE)
297                 return (0);
298
299         /* We do JMP_SLOTs below */
300         if (type == R_SPARC_JMP_SLOT)
301                 return (0);
302
303         /* COPY relocs are also handled elsewhere */
304         if (type == R_SPARC_COPY)
305                 return (0);
306
307         /*
308          * Note: R_SPARC_UA16 must be numerically largest relocation type.
309          */
310         if (type > R_SPARC_UA16)
311                 return (-1);
312
313         value = rela->r_addend;
314
315         /*
316          * Handle relative relocs here, because we might not
317          * be able to access globals yet.
318          */
319         if (type == R_SPARC_RELATIVE) {
320                 /* XXXX -- apparently we ignore the preexisting value */
321                 *where = (Elf_Addr)(obj->relocbase + value);
322                 return (0);
323         }
324
325         /*
326          * If we get here while relocating rtld itself, we will crash because
327          * a non-local variable is accessed.
328          */
329         if (RELOC_RESOLVE_SYMBOL(type)) {
330
331                 /* Find the symbol */
332                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
333                     false, cache);
334                 if (def == NULL)
335                         return (-1);
336
337                 /* Add in the symbol's absolute address */
338                 value += (Elf_Addr)(defobj->relocbase + def->st_value);
339         }
340
341         if (RELOC_PC_RELATIVE(type))
342                 value -= (Elf_Addr)where;
343
344         if (RELOC_BASE_RELATIVE(type)) {
345                 /*
346                  * Note that even though sparcs use `Elf_rela' exclusively
347                  * we still need the implicit memory addend in relocations
348                  * referring to GOT entries. Undoubtedly, someone f*cked
349                  * this up in the distant past, and now we're stuck with
350                  * it in the name of compatibility for all eternity..
351                  *
352                  * In any case, the implicit and explicit should be mutually
353                  * exclusive. We provide a check for that here.
354                  */
355                 /* XXXX -- apparently we ignore the preexisting value */
356                 value += (Elf_Addr)(obj->relocbase);
357         }
358
359         mask = RELOC_VALUE_BITMASK(type);
360         value >>= RELOC_VALUE_RIGHTSHIFT(type);
361         value &= mask;
362
363         if (RELOC_UNALIGNED(type)) {
364                 /* Handle unaligned relocations. */
365                 Elf_Addr tmp;
366                 char *ptr;
367                 int size;
368                 int i;
369
370                 size = RELOC_TARGET_SIZE(type) / 8;
371                 ptr = (char *)where;
372                 tmp = 0;
373
374                 /* Read it in one byte at a time. */
375                 for (i = 0; i < size; i++)
376                         tmp = (tmp << 8) | ptr[i];
377
378                 tmp &= ~mask;
379                 tmp |= value;
380
381                 /* Write it back out. */
382                 for (i = 0; i < size; i++)
383                         ptr[i] = ((tmp >> ((size - i - 1) * 8)) & 0xff);
384         } else if (RELOC_TARGET_SIZE(type) > 32) {
385                 *where &= ~mask;
386                 *where |= value;
387         } else {
388                 *where32 &= ~mask;
389                 *where32 |= value;
390         }
391
392         return (0);
393 }
394
395 int
396 reloc_plt(Obj_Entry *obj)
397 {
398 #if 0
399         const Obj_Entry *defobj;
400         const Elf_Rela *relalim;
401         const Elf_Rela *rela;
402         const Elf_Sym *def;
403         Elf_Addr *where;
404         Elf_Addr value;
405
406         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
407         for (rela = obj->pltrela; rela < relalim; rela++) {
408                 if (rela->r_addend == 0)
409                         continue;
410                 assert(ELF_R_TYPE(rela->r_info) == R_SPARC_JMP_SLOT);
411                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
412                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
413                     true, NULL);
414                 value = (Elf_Addr)(defobj->relocbase + def->st_value);
415                 *where = value;
416         }
417 #endif
418         return (0);
419 }
420
421 /*
422  * Instruction templates:
423  */
424 #define BAA     0x10400000      /*      ba,a    %xcc, 0 */
425 #define SETHI   0x03000000      /*      sethi   %hi(0), %g1 */
426 #define JMP     0x81c06000      /*      jmpl    %g1+%lo(0), %g0 */
427 #define NOP     0x01000000      /*      sethi   %hi(0), %g0 */
428 #define OR      0x82806000      /*      or      %g1, 0, %g1 */
429 #define XOR     0x82c06000      /*      xor     %g1, 0, %g1 */
430 #define MOV71   0x8283a000      /*      or      %o7, 0, %g1 */
431 #define MOV17   0x9c806000      /*      or      %g1, 0, %o7 */
432 #define CALL    0x40000000      /*      call    0 */
433 #define SLLX    0x8b407000      /*      sllx    %g1, 0, %g1 */
434 #define SETHIG5 0x0b000000      /*      sethi   %hi(0), %g5 */
435 #define ORG5    0x82804005      /*      or      %g1, %g5, %g1 */
436
437
438 /* %hi(v) with variable shift */
439 #define HIVAL(v, s)     (((v) >> (s)) &  0x003fffff)
440 #define LOVAL(v)        ((v) & 0x000003ff)
441
442 int
443 reloc_jmpslots(Obj_Entry *obj)
444 {
445         const Obj_Entry *defobj;
446         const Elf_Rela *relalim;
447         const Elf_Rela *rela;
448         const Elf_Sym *def;
449         Elf_Addr *where;
450         Elf_Addr target;
451
452         relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
453         for (rela = obj->pltrela; rela < relalim; rela++) {
454                 assert(ELF_R_TYPE(rela->r_info) == R_SPARC_JMP_SLOT);
455                 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
456                 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
457                     true, NULL);
458                 if (def == NULL)
459                         return -1;
460                 target = (Elf_Addr)(defobj->relocbase + def->st_value);
461                 reloc_jmpslot(where, target, defobj, obj, (Elf_Rel *)rela);
462         }
463         obj->jmpslots_done = true;
464         return (0);
465 }
466
467 Elf_Addr
468 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *obj,
469               const Obj_Entry *refobj, const Elf_Rel *rel)
470 {
471         const Elf_Rela *rela = (const Elf_Rela *)rel;
472         Elf_Addr offset;
473         Elf_Word *where;
474
475         if (rela - refobj->pltrela < 32764) {
476                 /*
477                  * At the PLT entry pointed at by `where', we now construct
478                  * a direct transfer to the now fully resolved function
479                  * address.
480                  *
481                  * A PLT entry is supposed to start by looking like this:
482                  *
483                  *      sethi   (. - .PLT0), %g1
484                  *      ba,a    %xcc, .PLT1
485                  *      nop
486                  *      nop
487                  *      nop
488                  *      nop
489                  *      nop
490                  *      nop
491                  *
492                  * When we replace these entries we start from the second
493                  * entry and do it in reverse order so the last thing we
494                  * do is replace the branch.  That allows us to change this
495                  * atomically.
496                  *
497                  * We now need to find out how far we need to jump.  We
498                  * have a choice of several different relocation techniques
499                  * which are increasingly expensive.
500                  */
501                 where = (Elf_Word *)wherep;
502                 offset = ((Elf_Addr)where) - target;
503                 if (offset <= (1L<<20) && offset >= -(1L<<20)) {
504                         /*
505                          * We're within 1MB -- we can use a direct branch insn.
506                          *
507                          * We can generate this pattern:
508                          *
509                          *      sethi   %hi(. - .PLT0), %g1
510                          *      ba,a    %xcc, addr
511                          *      nop
512                          *      nop
513                          *      nop
514                          *      nop
515                          *      nop
516                          *      nop
517                          *
518                          */
519                         where[1] = BAA | ((offset >> 2) &0x3fffff);
520                         flush(where, 4);
521                 } else if (target >= 0 && target < (1L<<32)) {
522                         /*
523                          * We're withing 32-bits of address zero.
524                          *
525                          * The resulting code in the jump slot is:
526                          *
527                          *      sethi   %hi(. - .PLT0), %g1
528                          *      sethi   %hi(addr), %g1
529                          *      jmp     %g1+%lo(addr)
530                          *      nop
531                          *      nop
532                          *      nop
533                          *      nop
534                          *      nop
535                          *
536                          */
537                         where[2] = JMP   | LOVAL(target);
538                         flush(where, 8);
539                         where[1] = SETHI | HIVAL(target, 10);
540                         flush(where, 4);
541                 } else if (target <= 0 && target > -(1L<<32)) {
542                         /*
543                          * We're withing 32-bits of address -1.
544                          *
545                          * The resulting code in the jump slot is:
546                          *
547                          *      sethi   %hi(. - .PLT0), %g1
548                          *      sethi   %hix(addr), %g1
549                          *      xor     %g1, %lox(addr), %g1
550                          *      jmp     %g1
551                          *      nop
552                          *      nop
553                          *      nop
554                          *      nop
555                          *
556                          */
557                         where[3] = JMP;
558                         flush(where, 12);
559                         where[2] = XOR | ((~target) & 0x00001fff);
560                         flush(where, 8);
561                         where[1] = SETHI | HIVAL(~target, 10);
562                         flush(where, 4);
563                 } else if (offset <= (1L<<32) && offset >= -((1L<<32) - 4)) {
564                         /*
565                          * We're withing 32-bits -- we can use a direct call
566                          * insn
567                          *
568                          * The resulting code in the jump slot is:
569                          *
570                          *      sethi   %hi(. - .PLT0), %g1
571                          *      mov     %o7, %g1
572                          *      call    (.+offset)
573                          *       mov    %g1, %o7
574                          *      nop
575                          *      nop
576                          *      nop
577                          *      nop
578                          *
579                          */
580                         where[3] = MOV17;
581                         flush(where, 12);
582                         where[2] = CALL   | ((offset >> 4) & 0x3fffffff);
583                         flush(where, 8);
584                         where[1] = MOV71;
585                         flush(where, 4);
586                 } else if (offset >= 0 && offset < (1L<<44)) {
587                         /*
588                          * We're withing 44 bits.  We can generate this pattern:
589                          *
590                          * The resulting code in the jump slot is:
591                          *
592                          *      sethi   %hi(. - .PLT0), %g1
593                          *      sethi   %h44(addr), %g1
594                          *      or      %g1, %m44(addr), %g1
595                          *      sllx    %g1, 12, %g1
596                          *      jmp     %g1+%l44(addr)
597                          *      nop
598                          *      nop
599                          *      nop
600                          *
601                          */
602                         where[4] = JMP   | LOVAL(offset);
603                         flush(where, 16);
604                         where[3] = SLLX  | 12;
605                         flush(where, 12);
606                         where[2] = OR    | (((offset) >> 12) & 0x00001fff);
607                         flush(where, 8);
608                         where[1] = SETHI | HIVAL(offset, 22);
609                         flush(where, 4);
610                 } else if (offset < 0 && offset > -(1L<<44)) {
611                         /*
612                          * We're withing 44 bits.  We can generate this pattern:
613                          *
614                          * The resulting code in the jump slot is:
615                          *
616                          *      sethi   %hi(. - .PLT0), %g1
617                          *      sethi   %h44(-addr), %g1
618                          *      xor     %g1, %m44(-addr), %g1
619                          *      sllx    %g1, 12, %g1
620                          *      jmp     %g1+%l44(addr)
621                          *      nop
622                          *      nop
623                          *      nop
624                          *
625                          */
626                         where[4] = JMP   | LOVAL(offset);
627                         flush(where, 16);
628                         where[3] = SLLX  | 12;
629                         flush(where, 12);
630                         where[2] = XOR   | (((~offset) >> 12) & 0x00001fff);
631                         flush(where, 8);
632                         where[1] = SETHI | HIVAL(~offset, 22);
633                         flush(where, 4);
634                 } else {
635                         /*
636                          * We need to load all 64-bits
637                          *
638                          * The resulting code in the jump slot is:
639                          *
640                          *      sethi   %hi(. - .PLT0), %g1
641                          *      sethi   %hh(addr), %g1
642                          *      sethi   %lm(addr), %g5
643                          *      or      %g1, %hm(addr), %g1
644                          *      sllx    %g1, 32, %g1
645                          *      or      %g1, %g5, %g1
646                          *      jmp     %g1+%lo(addr)
647                          *      nop
648                          *
649                          */
650                         where[6] = JMP     | LOVAL(target);
651                         flush(where, 24);
652                         where[5] = ORG5;
653                         flush(where, 20);
654                         where[4] = SLLX    | 32;
655                         flush(where, 16);
656                         where[3] = OR      | LOVAL((target) >> 32);
657                         flush(where, 12);
658                         where[2] = SETHIG5 | HIVAL(target, 10);
659                         flush(where, 8);
660                         where[1] = SETHI   | HIVAL(target, 42);
661                         flush(where, 4);
662                 }
663         } else {
664                 /*
665                  * This is a high PLT slot; the relocation offset specifies a
666                  * pointer that needs to be frobbed; no actual code needs to
667                  * be modified. The pointer to be calculated needs the addend
668                  * added and the reference object relocation base subtraced.
669                  */
670                 *wherep = target + rela->r_addend -
671                     (Elf_Addr)refobj->relocbase;
672         }
673
674         return (target);
675 }
676
677 /*
678  * Install rtld function call into this PLT slot.
679  */
680 #define SAVE            0x9de3bf50
681 #define SETHI_l0        0x21000000
682 #define SETHI_l1        0x23000000
683 #define OR_l0_l0        0xa0142000
684 #define SLLX_l0_32_l0   0xa12c3020
685 #define OR_l0_l1_l0     0xa0140011
686 #define JMPL_l0_o1      0x93c42000
687 #define MOV_g1_o0       0x90100001
688
689 void
690 init_pltgot(Obj_Entry *obj)
691 {
692         Elf_Word *entry;
693
694         if (obj->pltgot != NULL) {
695                 entry = (Elf_Word *)obj->pltgot;
696                 install_plt(&entry[0], (Elf_Addr)_rtld_bind_start_0);
697                 install_plt(&entry[8], (Elf_Addr)_rtld_bind_start_1);
698                 obj->pltgot[8] = (Elf_Addr)obj;
699         }
700 }
701
702 static void
703 install_plt(Elf_Word *pltgot, Elf_Addr proc)
704 {
705         pltgot[0] = SAVE;
706         flush(pltgot, 0);
707         pltgot[1] = SETHI_l0 | HIVAL(proc, 42);
708         flush(pltgot, 4);
709         pltgot[2] = SETHI_l1 | HIVAL(proc, 10);
710         flush(pltgot, 8);
711         pltgot[3] = OR_l0_l0 | LOVAL((proc) >> 32);
712         flush(pltgot, 12);
713         pltgot[4] = SLLX_l0_32_l0;
714         flush(pltgot, 16);
715         pltgot[5] = OR_l0_l1_l0;
716         flush(pltgot, 20);
717         pltgot[6] = JMPL_l0_o1 | LOVAL(proc);
718         flush(pltgot, 24);
719         pltgot[7] = MOV_g1_o0;
720         flush(pltgot, 28);
721 }
722
723 void
724 allocate_initial_tls(Obj_Entry *objs)
725 {
726     Elf_Addr* tpval;
727
728     /*
729      * Fix the size of the static TLS block by using the maximum
730      * offset allocated so far and adding a bit for dynamic modules to
731      * use.
732      */
733     tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
734     tpval = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr));
735     __asm __volatile("mov %0, %%g7" : : "r" (tpval));
736 }
737
738 void *__tls_get_addr(tls_index *ti)
739 {
740     register Elf_Addr** tp __asm__("%g7");
741
742     return tls_get_addr_common(tp, ti->ti_module, ti->ti_offset);
743 }