474aa0e6e609b53da9856abad80f6e78c276ac4c
[platform/upstream/glibc.git] / sysdeps / sparc / sparc64 / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions.  Sparc64 version.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3         Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #define ELF_MACHINE_NAME "sparc64"
22
23 #include <string.h>
24 #include <sys/param.h>
25 #include <ldsodefs.h>
26 #include <sysdep.h>
27
28 #ifndef VALIDX
29 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
30                       + DT_EXTRANUM + DT_VALTAGIDX (tag))
31 #endif
32
33 #define ELF64_R_TYPE_ID(info)   ((info) & 0xff)
34 #define ELF64_R_TYPE_DATA(info) ((info) >> 8)
35
36 /* Return nonzero iff ELF header is compatible with the running host.  */
37 static inline int
38 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
39 {
40   return ehdr->e_machine == EM_SPARCV9;
41 }
42
43 /* We have to do this because elf_machine_{dynamic,load_address} can be
44    invoked from functions that have no GOT references, and thus the compiler
45    has no obligation to load the PIC register.  */
46 #define LOAD_PIC_REG(PIC_REG)   \
47 do {    Elf64_Addr tmp;         \
48         __asm("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t" \
49               "rd %%pc, %0\n\t" \
50               "add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n\t" \
51               "add %0, %1, %0" \
52               : "=r" (PIC_REG), "=r" (tmp)); \
53 } while (0)
54
55 /* Return the link-time address of _DYNAMIC.  Conveniently, this is the
56    first element of the GOT.  This must be inlined in a function which
57    uses global data.  */
58 static inline Elf64_Addr
59 elf_machine_dynamic (void)
60 {
61   register Elf64_Addr *elf_pic_register __asm__("%l7");
62
63   LOAD_PIC_REG (elf_pic_register);
64
65   return *elf_pic_register;
66 }
67
68 /* Return the run-time load address of the shared object.  */
69 static inline Elf64_Addr
70 elf_machine_load_address (void)
71 {
72   register Elf32_Addr *pc __asm ("%o7");
73   register Elf64_Addr *got __asm ("%l7");
74
75   __asm ("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t"
76          "call 1f\n\t"
77          " add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n\t"
78          "call _DYNAMIC\n\t"
79          "call _GLOBAL_OFFSET_TABLE_\n"
80          "1:\tadd %1, %0, %1\n\t" : "=r" (pc), "=r" (got));
81
82   /* got is now l_addr + _GLOBAL_OFFSET_TABLE_
83      *got is _DYNAMIC
84      pc[2]*4 is l_addr + _DYNAMIC - (long)pc - 8
85      pc[3]*4 is l_addr + _GLOBAL_OFFSET_TABLE_ - (long)pc - 12  */
86   return (Elf64_Addr) got - *got + (Elf32_Sword) ((pc[2] - pc[3]) * 4) - 4;
87 }
88
89 /* We have 4 cases to handle.  And we code different code sequences
90    for each one.  I love V9 code models...  */
91 static inline void
92 sparc64_fixup_plt (struct link_map *map, const Elf64_Rela *reloc,
93                    Elf64_Addr *reloc_addr, Elf64_Addr value,
94                    Elf64_Addr high, int t)
95 {
96   unsigned int *insns = (unsigned int *) reloc_addr;
97   Elf64_Addr plt_vaddr = (Elf64_Addr) reloc_addr;
98   Elf64_Sxword disp = value - plt_vaddr;
99
100   /* Now move plt_vaddr up to the call instruction.  */
101   plt_vaddr += ((t + 1) * 4);
102
103   /* PLT entries .PLT32768 and above look always the same.  */
104   if (__builtin_expect (high, 0) != 0)
105     {
106       *reloc_addr = value - map->l_addr;
107     }
108   /* Near destination.  */
109   else if (disp >= -0x800000 && disp < 0x800000)
110     {
111       /* As this is just one instruction, it is thread safe and so
112          we can avoid the unnecessary sethi FOO, %g1.
113          b,a target  */
114       insns[0] = 0x30800000 | ((disp >> 2) & 0x3fffff);
115       __asm __volatile ("flush %0" : : "r" (insns));
116     }
117   /* 32-bit Sparc style, the target is in the lower 32-bits of
118      address space.  */
119   else if (insns += t, (value >> 32) == 0)
120     {
121       /* sethi  %hi(target), %g1
122          jmpl   %g1 + %lo(target), %g0  */
123
124       insns[1] = 0x81c06000 | (value & 0x3ff);
125       __asm __volatile ("flush %0 + 4" : : "r" (insns));
126
127       insns[0] = 0x03000000 | ((unsigned int)(value >> 10));
128       __asm __volatile ("flush %0" : : "r" (insns));
129     }
130   /* We can also get somewhat simple sequences if the distance between
131      the target and the PLT entry is within +/- 2GB.  */
132   else if ((plt_vaddr > value
133             && ((plt_vaddr - value) >> 31) == 0)
134            || (value > plt_vaddr
135                && ((value - plt_vaddr) >> 31) == 0))
136     {
137       unsigned int displacement;
138
139       if (plt_vaddr > value)
140         displacement = (0 - (plt_vaddr - value));
141       else
142         displacement = value - plt_vaddr;
143
144       /* mov    %o7, %g1
145          call   displacement
146           mov   %g1, %o7  */
147
148       insns[2] = 0x9e100001;
149       __asm __volatile ("flush %0 + 8" : : "r" (insns));
150
151       insns[1] = 0x40000000 | (displacement >> 2);
152       __asm __volatile ("flush %0 + 4" : : "r" (insns));
153
154       insns[0] = 0x8210000f;
155       __asm __volatile ("flush %0" : : "r" (insns));
156     }
157   /* Worst case, ho hum...  */
158   else
159     {
160       unsigned int high32 = (value >> 32);
161       unsigned int low32 = (unsigned int) value;
162
163       /* ??? Some tricks can be stolen from the sparc64 egcs backend
164              constant formation code I wrote.  -DaveM  */
165
166       if (__builtin_expect (high32 & 0x3ff, 0))
167         {
168           /* sethi      %hh(value), %g1
169              sethi      %lm(value), %g5
170              or         %g1, %hm(value), %g1
171              or         %g5, %lo(value), %g5
172              sllx       %g1, 32, %g1
173              jmpl       %g1 + %g5, %g0
174               nop  */
175
176           insns[5] = 0x81c04005;
177           __asm __volatile ("flush %0 + 20" : : "r" (insns));
178
179           insns[4] = 0x83287020;
180           __asm __volatile ("flush %0 + 16" : : "r" (insns));
181
182           insns[3] = 0x8a116000 | (low32 & 0x3ff);
183           __asm __volatile ("flush %0 + 12" : : "r" (insns));
184
185           insns[2] = 0x82106000 | (high32 & 0x3ff);
186         }
187       else
188         {
189           /* sethi      %hh(value), %g1
190              sethi      %lm(value), %g5
191              sllx       %g1, 32, %g1
192              or         %g5, %lo(value), %g5
193              jmpl       %g1 + %g5, %g0
194               nop  */
195
196           insns[4] = 0x81c04005;
197           __asm __volatile ("flush %0 + 16" : : "r" (insns));
198
199           insns[3] = 0x8a116000 | (low32 & 0x3ff);
200           __asm __volatile ("flush %0 + 12" : : "r" (insns));
201
202           insns[2] = 0x83287020;
203         }
204
205       __asm __volatile ("flush %0 + 8" : : "r" (insns));
206
207       insns[1] = 0x0b000000 | (low32 >> 10);
208       __asm __volatile ("flush %0 + 4" : : "r" (insns));
209
210       insns[0] = 0x03000000 | (high32 >> 10);
211       __asm __volatile ("flush %0" : : "r" (insns));
212     }
213 }
214
215 static inline Elf64_Addr
216 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
217                        const Elf64_Rela *reloc,
218                        Elf64_Addr *reloc_addr, Elf64_Addr value)
219 {
220   sparc64_fixup_plt (map, reloc, reloc_addr, value + reloc->r_addend,
221                      reloc->r_addend, 1);
222   return value;
223 }
224
225 /* Return the final value of a plt relocation.  */
226 static inline Elf64_Addr
227 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
228                        Elf64_Addr value)
229 {
230   /* Don't add addend here, but in elf_machine_fixup_plt instead.
231      value + reloc->r_addend is the value which should actually be
232      stored into .plt data slot.  */
233   return value;
234 }
235
236 #ifdef RESOLVE
237
238 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
239    MAP is the object containing the reloc.  */
240
241 static inline void
242 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
243                   const Elf64_Sym *sym, const struct r_found_version *version,
244                   void *const reloc_addr_arg)
245 {
246   Elf64_Addr *const reloc_addr = reloc_addr_arg;
247   const unsigned long int r_type = ELF64_R_TYPE_ID (reloc->r_info);
248
249 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
250   if (__builtin_expect (r_type == R_SPARC_RELATIVE, 0))
251     *reloc_addr = map->l_addr + reloc->r_addend;
252 # ifndef RTLD_BOOTSTRAP
253   else if (r_type == R_SPARC_NONE) /* Who is Wilbur? */
254     return;
255 # endif
256   else
257 #endif
258     {
259 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
260       const Elf64_Sym *const refsym = sym;
261 #endif
262       Elf64_Addr value;
263 #ifndef RESOLVE_CONFLICT_FIND_MAP
264       if (sym->st_shndx != SHN_UNDEF &&
265           ELF64_ST_BIND (sym->st_info) == STB_LOCAL)
266         value = map->l_addr;
267       else
268         {
269           value = RESOLVE (&sym, version, r_type);
270           if (sym)
271             value += sym->st_value;
272         }
273 #else
274       value = 0;
275 #endif
276       value += reloc->r_addend; /* Assume copy relocs have zero addend.  */
277
278       switch (r_type)
279         {
280 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
281         case R_SPARC_COPY:
282           if (sym == NULL)
283             /* This can happen in trace mode if an object could not be
284                found.  */
285             break;
286           if (sym->st_size > refsym->st_size
287               || (GLRO(dl_verbose) && sym->st_size < refsym->st_size))
288             {
289               const char *strtab;
290
291               strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
292               _dl_error_printf ("\
293 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
294                                 rtld_progname ?: "<program name unknown>",
295                                 strtab + refsym->st_name);
296             }
297           memcpy (reloc_addr_arg, (void *) value,
298                   MIN (sym->st_size, refsym->st_size));
299           break;
300 #endif
301         case R_SPARC_64:
302         case R_SPARC_GLOB_DAT:
303           *reloc_addr = value;
304           break;
305 #ifndef RTLD_BOOTSTRAP
306         case R_SPARC_8:
307           *(char *) reloc_addr = value;
308           break;
309         case R_SPARC_16:
310           *(short *) reloc_addr = value;
311           break;
312         case R_SPARC_32:
313           *(unsigned int *) reloc_addr = value;
314           break;
315         case R_SPARC_DISP8:
316           *(char *) reloc_addr = (value - (Elf64_Addr) reloc_addr);
317           break;
318         case R_SPARC_DISP16:
319           *(short *) reloc_addr = (value - (Elf64_Addr) reloc_addr);
320           break;
321         case R_SPARC_DISP32:
322           *(unsigned int *) reloc_addr = (value - (Elf64_Addr) reloc_addr);
323           break;
324         case R_SPARC_WDISP30:
325           *(unsigned int *) reloc_addr =
326             ((*(unsigned int *)reloc_addr & 0xc0000000) |
327              ((value - (Elf64_Addr) reloc_addr) >> 2));
328           break;
329
330         /* MEDLOW code model relocs */
331         case R_SPARC_LO10:
332           *(unsigned int *) reloc_addr =
333             ((*(unsigned int *)reloc_addr & ~0x3ff) |
334              (value & 0x3ff));
335           break;
336         case R_SPARC_HI22:
337           *(unsigned int *) reloc_addr =
338             ((*(unsigned int *)reloc_addr & 0xffc00000) |
339              (value >> 10));
340           break;
341         case R_SPARC_OLO10:
342           *(unsigned int *) reloc_addr =
343             ((*(unsigned int *)reloc_addr & ~0x1fff) |
344              (((value & 0x3ff) + ELF64_R_TYPE_DATA (reloc->r_info)) & 0x1fff));
345           break;
346
347         /* MEDMID code model relocs */
348         case R_SPARC_H44:
349           *(unsigned int *) reloc_addr =
350             ((*(unsigned int *)reloc_addr & 0xffc00000) |
351              (value >> 22));
352           break;
353         case R_SPARC_M44:
354           *(unsigned int *) reloc_addr =
355             ((*(unsigned int *)reloc_addr & ~0x3ff) |
356              ((value >> 12) & 0x3ff));
357           break;
358         case R_SPARC_L44:
359           *(unsigned int *) reloc_addr =
360             ((*(unsigned int *)reloc_addr & ~0xfff) |
361              (value & 0xfff));
362           break;
363
364         /* MEDANY code model relocs */
365         case R_SPARC_HH22:
366           *(unsigned int *) reloc_addr =
367             ((*(unsigned int *)reloc_addr & 0xffc00000) |
368              (value >> 42));
369           break;
370         case R_SPARC_HM10:
371           *(unsigned int *) reloc_addr =
372             ((*(unsigned int *)reloc_addr & ~0x3ff) |
373              ((value >> 32) & 0x3ff));
374           break;
375         case R_SPARC_LM22:
376           *(unsigned int *) reloc_addr =
377             ((*(unsigned int *)reloc_addr & 0xffc00000) |
378              ((value >> 10) & 0x003fffff));
379           break;
380 #endif
381         case R_SPARC_JMP_SLOT:
382 #ifdef RESOLVE_CONFLICT_FIND_MAP
383           /* R_SPARC_JMP_SLOT conflicts against .plt[32768+]
384              relocs should be turned into R_SPARC_64 relocs
385              in .gnu.conflict section.
386              r_addend non-zero does not mean it is a .plt[32768+]
387              reloc, instead it is the actual address of the function
388              to call.  */
389           sparc64_fixup_plt (NULL, reloc, reloc_addr, value, 0, 0);
390 #else
391           sparc64_fixup_plt (map, reloc, reloc_addr, value,
392                              reloc->r_addend, 0);
393 #endif
394           break;
395 #ifndef RTLD_BOOTSTRAP
396         case R_SPARC_UA16:
397           ((unsigned char *) reloc_addr_arg) [0] = value >> 8;
398           ((unsigned char *) reloc_addr_arg) [1] = value;
399           break;
400         case R_SPARC_UA32:
401           ((unsigned char *) reloc_addr_arg) [0] = value >> 24;
402           ((unsigned char *) reloc_addr_arg) [1] = value >> 16;
403           ((unsigned char *) reloc_addr_arg) [2] = value >> 8;
404           ((unsigned char *) reloc_addr_arg) [3] = value;
405           break;
406         case R_SPARC_UA64:
407           if (! ((long) reloc_addr_arg & 3))
408             {
409               /* Common in .eh_frame */
410               ((unsigned int *) reloc_addr_arg) [0] = value >> 32;
411               ((unsigned int *) reloc_addr_arg) [1] = value;
412               break;
413             }
414           ((unsigned char *) reloc_addr_arg) [0] = value >> 56;
415           ((unsigned char *) reloc_addr_arg) [1] = value >> 48;
416           ((unsigned char *) reloc_addr_arg) [2] = value >> 40;
417           ((unsigned char *) reloc_addr_arg) [3] = value >> 32;
418           ((unsigned char *) reloc_addr_arg) [4] = value >> 24;
419           ((unsigned char *) reloc_addr_arg) [5] = value >> 16;
420           ((unsigned char *) reloc_addr_arg) [6] = value >> 8;
421           ((unsigned char *) reloc_addr_arg) [7] = value;
422           break;
423 #endif
424 #if !defined RTLD_BOOTSTRAP || defined _NDEBUG
425         default:
426           _dl_reloc_bad_type (map, r_type, 0);
427           break;
428 #endif
429         }
430     }
431 }
432
433 static inline void
434 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
435                            void *const reloc_addr_arg)
436 {
437   Elf64_Addr *const reloc_addr = reloc_addr_arg;
438   *reloc_addr = l_addr + reloc->r_addend;
439 }
440
441 static inline void
442 elf_machine_lazy_rel (struct link_map *map,
443                       Elf64_Addr l_addr, const Elf64_Rela *reloc)
444 {
445   switch (ELF64_R_TYPE (reloc->r_info))
446     {
447     case R_SPARC_NONE:
448       break;
449     case R_SPARC_JMP_SLOT:
450       break;
451     default:
452       _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 1);
453       break;
454     }
455 }
456
457 #endif  /* RESOLVE */
458
459 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
460    PLT entries should not be allowed to define the value.
461    ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
462    of the main executable's symbols, as for a COPY reloc.  */
463 #define elf_machine_type_class(type) \
464   ((((type) == R_SPARC_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
465    | (((type) == R_SPARC_COPY) * ELF_RTYPE_CLASS_COPY))
466
467 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.  */
468 #define ELF_MACHINE_JMP_SLOT    R_SPARC_JMP_SLOT
469
470 /* The SPARC never uses Elf64_Rel relocations.  */
471 #define ELF_MACHINE_NO_REL 1
472
473 /* The SPARC overlaps DT_RELA and DT_PLTREL.  */
474 #define ELF_MACHINE_PLTREL_OVERLAP 1
475
476 /* Set up the loaded object described by L so its unrelocated PLT
477    entries will jump to the on-demand fixup code in dl-runtime.c.  */
478
479 static inline int
480 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
481 {
482   if (l->l_info[DT_JMPREL] && lazy)
483     {
484       extern void _dl_runtime_resolve_0 (void);
485       extern void _dl_runtime_resolve_1 (void);
486       extern void _dl_runtime_profile_0 (void);
487       extern void _dl_runtime_profile_1 (void);
488       Elf64_Addr res0_addr, res1_addr;
489       unsigned int *plt = (void *) D_PTR (l, l_info[DT_PLTGOT]);
490       int i = 0;
491
492       if (! profile)
493         {
494           res0_addr = (Elf64_Addr) &_dl_runtime_resolve_0;
495           res1_addr = (Elf64_Addr) &_dl_runtime_resolve_1;
496         }
497       else
498         {
499           res0_addr = (Elf64_Addr) &_dl_runtime_profile_0;
500           res1_addr = (Elf64_Addr) &_dl_runtime_profile_1;
501           if (_dl_name_match_p (GLRO(dl_profile), l))
502             GL(dl_profile_map) = l;
503         }
504
505       /* PLT0 looks like:
506
507          save   %sp, -192, %sp
508          sethi  %hh(_dl_runtime_{resolve,profile}_0), %l0
509          sethi  %lm(_dl_runtime_{resolve,profile}_0), %l1
510          or     %l0, %hm(_dl_runtime_{resolve,profile}_0), %l0
511          or     %l1, %lo(_dl_runtime_{resolve,profile}_0), %l1
512          sllx   %l0, 32, %l0
513          jmpl   %l0 + %l1, %l6
514           sethi %hi(0xffc00), %l2
515        */
516
517       plt[0] = 0x9de3bf40;
518       plt[1] = 0x21000000 | (res0_addr >> (64 - 22));
519       plt[2] = 0x23000000 | ((res0_addr >> 10) & 0x003fffff);
520       plt[3] = 0xa0142000 | ((res0_addr >> 32) & 0x3ff);
521       plt[4] = 0xa2146000 | (res0_addr & 0x3ff);
522       plt[5] = 0xa12c3020;
523       plt[6] = 0xadc40011;
524       plt[7] = 0x250003ff;
525
526       /* PLT1 looks like:
527
528          save   %sp, -192, %sp
529          sethi  %hh(_dl_runtime_{resolve,profile}_1), %l0
530          sethi  %lm(_dl_runtime_{resolve,profile}_1), %l1
531          or     %l0, %hm(_dl_runtime_{resolve,profile}_1), %l0
532          or     %l1, %lo(_dl_runtime_{resolve,profile}_1), %l1
533          sllx   %l0, 32, %l0
534          jmpl   %l0 + %l1, %l6
535           srlx  %g1, 12, %o1
536        */
537
538       plt[8 + 0] = 0x9de3bf40;
539       if (__builtin_expect (((res1_addr + 4) >> 32) & 0x3ff, 0))
540         i = 1;
541       else
542         res1_addr += 4;
543       plt[8 + 1] = 0x21000000 | (res1_addr >> (64 - 22));
544       plt[8 + 2] = 0x23000000 | ((res1_addr >> 10) & 0x003fffff);
545       if (__builtin_expect (i, 0))
546         plt[8 + 3] = 0xa0142000 | ((res1_addr >> 32) & 0x3ff);
547       else
548         plt[8 + 3] = 0xa12c3020;
549       plt[8 + 4] = 0xa2146000 | (res1_addr & 0x3ff);
550       if (__builtin_expect (i, 0))
551         plt[8 + 5] = 0xa12c3020;
552       plt[8 + 5 + i] = 0xadc40011;
553       plt[8 + 6 + i] = 0x9330700c;
554
555       /* Now put the magic cookie at the beginning of .PLT2
556          Entry .PLT3 is unused by this implementation.  */
557       *((struct link_map **)(&plt[16 + 0])) = l;
558
559       if (__builtin_expect (l->l_info[VALIDX(DT_GNU_PRELINKED)] != NULL, 0)
560           || __builtin_expect (l->l_info [VALIDX (DT_GNU_LIBLISTSZ)] != NULL, 0))
561         {
562           /* Need to reinitialize .plt to undo prelinking.  */
563           Elf64_Rela *rela = (Elf64_Rela *) D_PTR (l, l_info[DT_JMPREL]);
564           Elf64_Rela *relaend
565             = (Elf64_Rela *) ((char *) rela
566                               + l->l_info[DT_PLTRELSZ]->d_un.d_val);
567
568           /* prelink must ensure there are no R_SPARC_NONE relocs left
569              in .rela.plt.  */
570           while (rela < relaend)
571             {
572               if (__builtin_expect (rela->r_addend, 0) != 0)
573                 {
574                   Elf64_Addr slot = ((rela->r_offset + 0x400
575                                       - (Elf64_Addr) plt)
576                                      / 0x1400) * 0x1400
577                                     + (Elf64_Addr) plt - 0x400;
578                   /* ldx [%o7 + X], %g1  */
579                   unsigned int first_ldx = *(unsigned int *)(slot + 12);
580                   Elf64_Addr ptr = slot + (first_ldx & 0xfff) + 4;
581
582                   *(Elf64_Addr *) rela->r_offset
583                     = (Elf64_Addr) plt
584                       - (slot + ((rela->r_offset - ptr) / 8) * 24 + 4);
585                   ++rela;
586                   continue;
587                 }
588
589               *(unsigned int *) rela->r_offset
590                 = 0x03000000 | (rela->r_offset - (Elf64_Addr) plt);
591               *(unsigned int *) (rela->r_offset + 4)
592                 = 0x30680000 | ((((Elf64_Addr) plt + 32
593                                   - rela->r_offset - 4) >> 2) & 0x7ffff);
594               __asm __volatile ("flush %0" : : "r" (rela->r_offset));
595               __asm __volatile ("flush %0+4" : : "r" (rela->r_offset));
596               ++rela;
597             }
598         }
599     }
600
601   return lazy;
602 }
603
604 /* This code is used in dl-runtime.c to call the `fixup' function
605    and then redirect to the address it returns.  */
606 #define TRAMPOLINE_TEMPLATE(tramp_name, fixup_name)     \
607   asm ("\n"                                             \
608 "       .text\n"                                        \
609 "       .globl  " #tramp_name "_0\n"                    \
610 "       .type   " #tramp_name "_0, @function\n"         \
611 "       .align  32\n"                                   \
612 "\t" #tramp_name "_0:\n"                                \
613 "       ! sethi   %hi(1047552), %l2 - Done in .PLT0\n"  \
614 "       ldx     [%l6 + 32 + 8], %o0\n"                  \
615 "       sub     %g1, %l6, %l0\n"                        \
616 "       xor     %l2, -1016, %l2\n"                      \
617 "       sethi   %hi(5120), %l3  ! 160 * 32\n"           \
618 "       add     %l0, %l2, %l0\n"                        \
619 "       sethi   %hi(32768), %l4\n"                      \
620 "       udivx   %l0, %l3, %l3\n"                        \
621 "       sllx    %l3, 2, %l1\n"                          \
622 "       add     %l1, %l3, %l1\n"                        \
623 "       sllx    %l1, 10, %l2\n"                         \
624 "       sub     %l4, 4, %l4     ! No thanks to Sun for not obeying their own ABI\n" \
625 "       sllx    %l1, 5, %l1\n"                          \
626 "       sub     %l0, %l2, %l0\n"                        \
627 "       udivx   %l0, 24, %l0\n"                         \
628 "       add     %l0, %l4, %l0\n"                        \
629 "       add     %l1, %l0, %l1\n"                        \
630 "       add     %l1, %l1, %l0\n"                        \
631 "       add     %l0, %l1, %l0\n"                        \
632 "       mov     %i7, %o2\n"                             \
633 "       call    " #fixup_name "\n"                      \
634 "        sllx    %l0, 3, %o1\n"                         \
635 "       jmp     %o0\n"                                  \
636 "        restore\n"                                     \
637 "       .size   " #tramp_name "_0, . - " #tramp_name "_0\n" \
638 "\n"                                                    \
639 "       .globl  " #tramp_name "_1\n"                    \
640 "       .type   " #tramp_name "_1, @function\n"         \
641 "       ! tramp_name_1 + 4 needs to be .align 32\n"     \
642 "\t" #tramp_name "_1:\n"                                \
643 "       sub     %l6, 4, %l6\n"                          \
644 "       ! srlx  %g1, 12, %o1 - Done in .PLT1\n"         \
645 "       ldx     [%l6 + 12], %o0\n"                      \
646 "       add     %o1, %o1, %o3\n"                        \
647 "       sub     %o1, 96, %o1    ! No thanks to Sun for not obeying their own ABI\n" \
648 "       mov     %i7, %o2\n"                             \
649 "       call    " #fixup_name "\n"                      \
650 "        add    %o1, %o3, %o1\n"                        \
651 "       jmp     %o0\n"                                  \
652 "        restore\n"                                     \
653 "       .size   " #tramp_name "_1, . - " #tramp_name "_1\n" \
654 "       .previous\n");
655
656 #ifndef PROF
657 #define ELF_MACHINE_RUNTIME_TRAMPOLINE                  \
658   TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup);     \
659   TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup);
660 #else
661 #define ELF_MACHINE_RUNTIME_TRAMPOLINE                  \
662   TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup);     \
663   TRAMPOLINE_TEMPLATE (_dl_runtime_profile, fixup);
664 #endif
665
666 /* The PLT uses Elf64_Rela relocs.  */
667 #define elf_machine_relplt elf_machine_rela
668
669 /* Undo the sub %sp, 6*8, %sp; add %sp, STACK_BIAS + 22*8, %o0 below
670    to get at the value we want in __libc_stack_end.  */
671 #define DL_STACK_END(cookie) \
672   ((void *) (((long) (cookie)) - (22 - 6) * 8 - STACK_BIAS))
673
674 /* Initial entry point code for the dynamic linker.
675    The C function `_dl_start' is the real entry point;
676    its return value is the user program's entry point.  */
677
678 #define __S1(x) #x
679 #define __S(x)  __S1(x)
680
681 #define RTLD_START __asm__ ( "\n"                                       \
682 "       .text\n"                                                        \
683 "       .global _start\n"                                               \
684 "       .type   _start, @function\n"                                    \
685 "       .align  32\n"                                                   \
686 "_start:\n"                                                             \
687 "   /* Make room for functions to drop their arguments on the stack.  */\n" \
688 "       sub     %sp, 6*8, %sp\n"                                        \
689 "   /* Pass pointer to argument block to _dl_start.  */\n"              \
690 "       call    _dl_start\n"                                            \
691 "        add     %sp," __S(STACK_BIAS) "+22*8,%o0\n"                    \
692 "       /* FALLTHRU */\n"                                               \
693 "       .size _start, .-_start\n"                                       \
694 "\n"                                                                    \
695 "       .global _dl_start_user\n"                                       \
696 "       .type   _dl_start_user, @function\n"                            \
697 "_dl_start_user:\n"                                                     \
698 "   /* Load the GOT register.  */\n"                                    \
699 "1:     call    11f\n"                                                  \
700 "        sethi  %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n"               \
701 "11:    or      %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n"          \
702 "       sethi   %hi(_dl_skip_args), %g5\n"                              \
703 "       add     %l7, %o7, %l7\n"                                        \
704 "       or      %g5, %lo(_dl_skip_args), %g5\n"                         \
705 "   /* Save the user entry point address in %l0.  */\n"                 \
706 "       mov     %o0, %l0\n"                                             \
707 "   /* See if we were run as a command with the executable file name as an\n" \
708 "      extra leading argument.  If so, we must shift things around since we\n" \
709 "      must keep the stack doubleword aligned.  */\n"                   \
710 "       ldx     [%l7 + %g5], %i0\n"                                     \
711 "       ld      [%i0], %i0\n"                                           \
712 "       brz,pt  %i0, 2f\n"                                              \
713 "        ldx    [%sp + " __S(STACK_BIAS) " + 22*8], %i5\n"              \
714 "       /* Find out how far to shift.  */\n"                            \
715 "       sethi   %hi(_dl_argv), %l4\n"                                   \
716 "       sub     %i5, %i0, %i5\n"                                        \
717 "       or      %l4, %lo(_dl_argv), %l4\n"                              \
718 "       sllx    %i0, 3, %l6\n"                                          \
719 "       ldx     [%l7 + %l4], %l4\n"                                     \
720 "       stx     %i5, [%sp + " __S(STACK_BIAS) " + 22*8]\n"              \
721 "       add     %sp, " __S(STACK_BIAS) " + 23*8, %i1\n"                 \
722 "       add     %i1, %l6, %i2\n"                                        \
723 "       ldx     [%l4], %l5\n"                                           \
724 "       /* Copy down argv.  */\n"                                       \
725 "12:    ldx     [%i2], %i3\n"                                           \
726 "       add     %i2, 8, %i2\n"                                          \
727 "       stx     %i3, [%i1]\n"                                           \
728 "       brnz,pt %i3, 12b\n"                                             \
729 "        add    %i1, 8, %i1\n"                                          \
730 "       sub     %l5, %l6, %l5\n"                                        \
731 "       /* Copy down envp.  */\n"                                       \
732 "13:    ldx     [%i2], %i3\n"                                           \
733 "       add     %i2, 8, %i2\n"                                          \
734 "       stx     %i3, [%i1]\n"                                           \
735 "       brnz,pt %i3, 13b\n"                                             \
736 "        add    %i1, 8, %i1\n"                                          \
737 "       /* Copy down auxiliary table.  */\n"                            \
738 "14:    ldx     [%i2], %i3\n"                                           \
739 "       ldx     [%i2 + 8], %i4\n"                                       \
740 "       add     %i2, 16, %i2\n"                                         \
741 "       stx     %i3, [%i1]\n"                                           \
742 "       stx     %i4, [%i1 + 8]\n"                                       \
743 "       brnz,pt %i3, 14b\n"                                             \
744 "        add    %i1, 16, %i1\n"                                         \
745 "       stx     %l5, [%l4]\n"                                           \
746 "  /* %o0 = _dl_loaded, %o1 = argc, %o2 = argv, %o3 = envp.  */\n"      \
747 "2:     sethi   %hi(_rtld_local), %o0\n"                                \
748 "       add     %sp, " __S(STACK_BIAS) " + 23*8, %o2\n"                 \
749 "       orcc    %o0, %lo(_rtld_local), %o0\n"                           \
750 "       sllx    %i5, 3, %o3\n"                                          \
751 "       ldx     [%l7 + %o0], %o0\n"                                     \
752 "       add     %o3, 8, %o3\n"                                          \
753 "       mov     %i5, %o1\n"                                             \
754 "       add     %o2, %o3, %o3\n"                                        \
755 "       call    _dl_init_internal\n"                                    \
756 "        ldx    [%o0], %o0\n"                                           \
757 "   /* Pass our finalizer function to the user in %g1.  */\n"           \
758 "       sethi   %hi(_dl_fini), %g1\n"                                   \
759 "       or      %g1, %lo(_dl_fini), %g1\n"                              \
760 "       ldx     [%l7 + %g1], %g1\n"                                     \
761 "  /* Jump to the user's entry point and deallocate the extra stack we got.  */\n" \
762 "       jmp     %l0\n"                                                  \
763 "        add    %sp, 6*8, %sp\n"                                        \
764 "       .size   _dl_start_user, . - _dl_start_user\n"                   \
765 "       .previous\n");