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