This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / bfd / elf64-sparc.c
1 /* SPARC-specific support for 64-bit ELF
2    Copyright (C) 1993, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24
25 /* This is defined if one wants to build upward compatible binaries
26    with the original sparc64-elf toolchain.  The support is kept in for
27    now but is turned off by default.  dje 970930  */
28 /*#define SPARC64_OLD_RELOCS*/
29
30 #include "elf/sparc.h"
31
32 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
33 #define MINUS_ONE (~ (bfd_vma) 0)
34
35 static reloc_howto_type *sparc64_elf_reloc_type_lookup
36   PARAMS ((bfd *, bfd_reloc_code_real_type));
37 static void sparc64_elf_info_to_howto
38   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
39
40 static void sparc64_elf_build_plt
41   PARAMS((bfd *, unsigned char *, int));
42 static bfd_vma sparc64_elf_plt_entry_offset
43   PARAMS((int));
44 static bfd_vma sparc64_elf_plt_ptr_offset
45   PARAMS((int, int));
46
47 static boolean sparc64_elf_check_relocs
48   PARAMS((bfd *, struct bfd_link_info *, asection *sec,
49           const Elf_Internal_Rela *));
50 static boolean sparc64_elf_adjust_dynamic_symbol
51   PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
52 static boolean sparc64_elf_size_dynamic_sections
53   PARAMS((bfd *, struct bfd_link_info *));
54 static boolean sparc64_elf_adjust_dynindx
55   PARAMS((struct elf_link_hash_entry *, PTR));
56
57 static boolean sparc64_elf_merge_private_bfd_data
58   PARAMS ((bfd *, bfd *));
59
60 static boolean sparc64_elf_relocate_section
61   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
62            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
63 static boolean sparc64_elf_object_p PARAMS ((bfd *));
64 \f
65 /* The relocation "howto" table.  */
66
67 static bfd_reloc_status_type sparc_elf_notsup_reloc
68   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
69 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
70   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
71 static bfd_reloc_status_type sparc_elf_hix22_reloc
72   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
73 static bfd_reloc_status_type sparc_elf_lox10_reloc
74   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
75
76 static reloc_howto_type sparc64_elf_howto_table[] =
77 {
78   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
79   HOWTO(R_SPARC_8,         0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_8",       false,0,0x000000ff,true),
80   HOWTO(R_SPARC_16,        0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_16",      false,0,0x0000ffff,true),
81   HOWTO(R_SPARC_32,        0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_32",      false,0,0xffffffff,true),
82   HOWTO(R_SPARC_DISP8,     0,0, 8,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP8",   false,0,0x000000ff,true),
83   HOWTO(R_SPARC_DISP16,    0,1,16,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP16",  false,0,0x0000ffff,true),
84   HOWTO(R_SPARC_DISP32,    0,2,32,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP32",  false,0,0x00ffffff,true),
85   HOWTO(R_SPARC_WDISP30,   2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP30", false,0,0x3fffffff,true),
86   HOWTO(R_SPARC_WDISP22,   2,2,22,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP22", false,0,0x003fffff,true),
87   HOWTO(R_SPARC_HI22,     10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HI22",    false,0,0x003fffff,true),
88   HOWTO(R_SPARC_22,        0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_22",      false,0,0x003fffff,true),
89   HOWTO(R_SPARC_13,        0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_13",      false,0,0x00001fff,true),
90   HOWTO(R_SPARC_LO10,      0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LO10",    false,0,0x000003ff,true),
91   HOWTO(R_SPARC_GOT10,     0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT10",   false,0,0x000003ff,true),
92   HOWTO(R_SPARC_GOT13,     0,2,13,false,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_GOT13",   false,0,0x00001fff,true),
93   HOWTO(R_SPARC_GOT22,    10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT22",   false,0,0x003fffff,true),
94   HOWTO(R_SPARC_PC10,      0,2,10,true, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC10",    false,0,0x000003ff,true),
95   HOWTO(R_SPARC_PC22,     10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PC22",    false,0,0x003fffff,true),
96   HOWTO(R_SPARC_WPLT30,    2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WPLT30",  false,0,0x3fffffff,true),
97   HOWTO(R_SPARC_COPY,      0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_COPY",    false,0,0x00000000,true),
98   HOWTO(R_SPARC_GLOB_DAT,  0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GLOB_DAT",false,0,0x00000000,true),
99   HOWTO(R_SPARC_JMP_SLOT,  0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_JMP_SLOT",false,0,0x00000000,true),
100   HOWTO(R_SPARC_RELATIVE,  0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_RELATIVE",false,0,0x00000000,true),
101   HOWTO(R_SPARC_UA32,      0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_UA32",    false,0,0x00000000,true),
102 #ifndef SPARC64_OLD_RELOCS
103   /* These aren't implemented yet.  */
104   HOWTO(R_SPARC_PLT32,     0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PLT32",    false,0,0x00000000,true),
105   HOWTO(R_SPARC_HIPLT22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_HIPLT22",  false,0,0x00000000,true),
106   HOWTO(R_SPARC_LOPLT10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_LOPLT10",  false,0,0x00000000,true),
107   HOWTO(R_SPARC_PCPLT32,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT32",  false,0,0x00000000,true),
108   HOWTO(R_SPARC_PCPLT22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT22",  false,0,0x00000000,true),
109   HOWTO(R_SPARC_PCPLT10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT10",  false,0,0x00000000,true),
110 #endif
111   HOWTO(R_SPARC_10,        0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_10",      false,0,0x000003ff,true),
112   HOWTO(R_SPARC_11,        0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_11",      false,0,0x000007ff,true),
113   HOWTO(R_SPARC_64,        0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_64",      false,0,MINUS_ONE, true),
114   HOWTO(R_SPARC_OLO10,     0,2,13,false,0,complain_overflow_signed,  sparc_elf_notsup_reloc, "R_SPARC_OLO10",   false,0,0x00001fff,true),
115   HOWTO(R_SPARC_HH22,     42,2,22,false,0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_HH22",    false,0,0x003fffff,true),
116   HOWTO(R_SPARC_HM10,     32,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HM10",    false,0,0x000003ff,true),
117   HOWTO(R_SPARC_LM22,     10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LM22",    false,0,0x003fffff,true),
118   HOWTO(R_SPARC_PC_HH22,  42,2,22,true, 0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_PC_HH22",    false,0,0x003fffff,true),
119   HOWTO(R_SPARC_PC_HM10,  32,2,10,true, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC_HM10",    false,0,0x000003ff,true),
120   HOWTO(R_SPARC_PC_LM22,  10,2,22,true, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC_LM22",    false,0,0x003fffff,true),
121   HOWTO(R_SPARC_WDISP16,   2,2,16,true, 0,complain_overflow_signed,  sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
122   HOWTO(R_SPARC_WDISP19,   2,2,19,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP19", false,0,0x0007ffff,true),
123   HOWTO(R_SPARC_UNUSED_42, 0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_UNUSED_42",false,0,0x00000000,true),
124   HOWTO(R_SPARC_7,         0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_7",       false,0,0x0000007f,true),
125   HOWTO(R_SPARC_5,         0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_5",       false,0,0x0000001f,true),
126   HOWTO(R_SPARC_6,         0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_6",       false,0,0x0000003f,true),
127   HOWTO(R_SPARC_DISP64,    0,4,64,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP64",  false,0,MINUS_ONE, true),
128   HOWTO(R_SPARC_PLT64,     0,4,64,false,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_PLT64",   false,0,MINUS_ONE, false),
129   HOWTO(R_SPARC_HIX22,     0,4, 0,false,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,  "R_SPARC_HIX22",   false,0,MINUS_ONE, false),
130   HOWTO(R_SPARC_LOX10,     0,4, 0,false,0,complain_overflow_dont,    sparc_elf_lox10_reloc,  "R_SPARC_LOX10",   false,0,MINUS_ONE, false),
131   HOWTO(R_SPARC_H44,      22,2,22,false,0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_H44",     false,0,0x003fffff,false),
132   HOWTO(R_SPARC_M44,      12,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_M44",     false,0,0x000003ff,false),
133   HOWTO(R_SPARC_L44,       0,2,13,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_L44",     false,0,0x00000fff,false),
134   HOWTO(R_SPARC_REGISTER,  0,4, 0,false,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_REGISTER",false,0,MINUS_ONE, false),
135   HOWTO(R_SPARC_UA64,        0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA64",      false,0,MINUS_ONE, true),
136   HOWTO(R_SPARC_UA16,        0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA16",      false,0,0x0000ffff,true)
137 };
138
139 struct elf_reloc_map {
140   bfd_reloc_code_real_type bfd_reloc_val;
141   unsigned char elf_reloc_val;
142 };
143
144 static CONST struct elf_reloc_map sparc_reloc_map[] =
145 {
146   { BFD_RELOC_NONE, R_SPARC_NONE, },
147   { BFD_RELOC_16, R_SPARC_16, },
148   { BFD_RELOC_8, R_SPARC_8 },
149   { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
150   { BFD_RELOC_CTOR, R_SPARC_64 },
151   { BFD_RELOC_32, R_SPARC_32 },
152   { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
153   { BFD_RELOC_HI22, R_SPARC_HI22 },
154   { BFD_RELOC_LO10, R_SPARC_LO10, },
155   { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
156   { BFD_RELOC_SPARC22, R_SPARC_22 },
157   { BFD_RELOC_SPARC13, R_SPARC_13 },
158   { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
159   { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
160   { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
161   { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
162   { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
163   { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
164   { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
165   { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
166   { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
167   { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
168   { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
169   /* ??? Doesn't dwarf use this?  */
170 /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
171   {BFD_RELOC_SPARC_10, R_SPARC_10},
172   {BFD_RELOC_SPARC_11, R_SPARC_11},
173   {BFD_RELOC_SPARC_64, R_SPARC_64},
174   {BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10},
175   {BFD_RELOC_SPARC_HH22, R_SPARC_HH22},
176   {BFD_RELOC_SPARC_HM10, R_SPARC_HM10},
177   {BFD_RELOC_SPARC_LM22, R_SPARC_LM22},
178   {BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22},
179   {BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10},
180   {BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22},
181   {BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16},
182   {BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19},
183   {BFD_RELOC_SPARC_7, R_SPARC_7},
184   {BFD_RELOC_SPARC_5, R_SPARC_5},
185   {BFD_RELOC_SPARC_6, R_SPARC_6},
186   {BFD_RELOC_SPARC_DISP64, R_SPARC_DISP64},
187   {BFD_RELOC_SPARC_PLT64, R_SPARC_PLT64},
188   {BFD_RELOC_SPARC_HIX22, R_SPARC_HIX22},
189   {BFD_RELOC_SPARC_LOX10, R_SPARC_LOX10},
190   {BFD_RELOC_SPARC_H44, R_SPARC_H44},
191   {BFD_RELOC_SPARC_M44, R_SPARC_M44},
192   {BFD_RELOC_SPARC_L44, R_SPARC_L44},
193   {BFD_RELOC_SPARC_REGISTER, R_SPARC_REGISTER}
194 };
195
196 static reloc_howto_type *
197 sparc64_elf_reloc_type_lookup (abfd, code)
198      bfd *abfd;
199      bfd_reloc_code_real_type code;
200 {
201   unsigned int i;
202   for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
203     {
204       if (sparc_reloc_map[i].bfd_reloc_val == code)
205         return &sparc64_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
206     }
207   return 0;
208 }
209
210 static void
211 sparc64_elf_info_to_howto (abfd, cache_ptr, dst)
212      bfd *abfd;
213      arelent *cache_ptr;
214      Elf64_Internal_Rela *dst;
215 {
216   BFD_ASSERT (ELF64_R_TYPE (dst->r_info) < (unsigned int) R_SPARC_max);
217   cache_ptr->howto = &sparc64_elf_howto_table[ELF64_R_TYPE (dst->r_info)];
218 }
219 \f
220 /* Utility for performing the standard initial work of an instruction
221    relocation.
222    *PRELOCATION will contain the relocated item.
223    *PINSN will contain the instruction from the input stream.
224    If the result is `bfd_reloc_other' the caller can continue with
225    performing the relocation.  Otherwise it must stop and return the
226    value to its caller.  */
227
228 static bfd_reloc_status_type
229 init_insn_reloc (abfd,
230                  reloc_entry,
231                  symbol,
232                  data,
233                  input_section,
234                  output_bfd,
235                  prelocation,
236                  pinsn)
237      bfd *abfd;
238      arelent *reloc_entry;
239      asymbol *symbol;
240      PTR data;
241      asection *input_section;
242      bfd *output_bfd;
243      bfd_vma *prelocation;
244      bfd_vma *pinsn;
245 {
246   bfd_vma relocation;
247   reloc_howto_type *howto = reloc_entry->howto;
248
249   if (output_bfd != (bfd *) NULL
250       && (symbol->flags & BSF_SECTION_SYM) == 0
251       && (! howto->partial_inplace
252           || reloc_entry->addend == 0))
253     {
254       reloc_entry->address += input_section->output_offset;
255       return bfd_reloc_ok;
256     }
257
258   /* This works because partial_inplace == false.  */
259   if (output_bfd != NULL)
260     return bfd_reloc_continue;
261
262   if (reloc_entry->address > input_section->_cooked_size)
263     return bfd_reloc_outofrange;
264
265   relocation = (symbol->value
266                 + symbol->section->output_section->vma
267                 + symbol->section->output_offset);
268   relocation += reloc_entry->addend;
269   if (howto->pc_relative)
270     {
271       relocation -= (input_section->output_section->vma
272                      + input_section->output_offset);
273       relocation -= reloc_entry->address;
274     }
275
276   *prelocation = relocation;
277   *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
278   return bfd_reloc_other;
279 }
280
281 /* For unsupported relocs.  */
282
283 static bfd_reloc_status_type
284 sparc_elf_notsup_reloc (abfd,
285                         reloc_entry,
286                         symbol,
287                         data,
288                         input_section,
289                         output_bfd,
290                         error_message)
291      bfd *abfd;
292      arelent *reloc_entry;
293      asymbol *symbol;
294      PTR data;
295      asection *input_section;
296      bfd *output_bfd;
297      char **error_message;
298 {
299   return bfd_reloc_notsupported;
300 }
301
302 /* Handle the WDISP16 reloc.  */
303
304 static bfd_reloc_status_type
305 sparc_elf_wdisp16_reloc (abfd, reloc_entry, symbol, data, input_section,
306                          output_bfd, error_message)
307      bfd *abfd;
308      arelent *reloc_entry;
309      asymbol *symbol;
310      PTR data;
311      asection *input_section;
312      bfd *output_bfd;
313      char **error_message;
314 {
315   bfd_vma relocation;
316   bfd_vma insn;
317   bfd_reloc_status_type status;
318
319   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
320                             input_section, output_bfd, &relocation, &insn);
321   if (status != bfd_reloc_other)
322     return status;
323
324   insn = (insn & ~0x303fff) | ((((relocation >> 2) & 0xc000) << 6)
325                                | ((relocation >> 2) & 0x3fff));
326   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
327
328   if ((bfd_signed_vma) relocation < - 0x40000
329       || (bfd_signed_vma) relocation > 0x3ffff)
330     return bfd_reloc_overflow;
331   else
332     return bfd_reloc_ok;
333 }
334
335 /* Handle the HIX22 reloc.  */
336
337 static bfd_reloc_status_type
338 sparc_elf_hix22_reloc (abfd,
339                        reloc_entry,
340                        symbol,
341                        data,
342                        input_section,
343                        output_bfd,
344                        error_message)
345      bfd *abfd;
346      arelent *reloc_entry;
347      asymbol *symbol;
348      PTR data;
349      asection *input_section;
350      bfd *output_bfd;
351      char **error_message;
352 {
353   bfd_vma relocation;
354   bfd_vma insn;
355   bfd_reloc_status_type status;
356
357   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
358                             input_section, output_bfd, &relocation, &insn);
359   if (status != bfd_reloc_other)
360     return status;
361
362   relocation ^= MINUS_ONE;
363   insn = (insn & ~0x3fffff) | ((relocation >> 10) & 0x3fffff);
364   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
365
366   if ((relocation & ~ (bfd_vma) 0xffffffff) != 0)
367     return bfd_reloc_overflow;
368   else
369     return bfd_reloc_ok;
370 }
371
372 /* Handle the LOX10 reloc.  */
373
374 static bfd_reloc_status_type
375 sparc_elf_lox10_reloc (abfd,
376                        reloc_entry,
377                        symbol,
378                        data,
379                        input_section,
380                        output_bfd,
381                        error_message)
382      bfd *abfd;
383      arelent *reloc_entry;
384      asymbol *symbol;
385      PTR data;
386      asection *input_section;
387      bfd *output_bfd;
388      char **error_message;
389 {
390   bfd_vma relocation;
391   bfd_vma insn;
392   bfd_reloc_status_type status;
393
394   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
395                             input_section, output_bfd, &relocation, &insn);
396   if (status != bfd_reloc_other)
397     return status;
398
399   insn = (insn & ~0x1fff) | 0x1c00 | (relocation & 0x3ff);
400   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
401
402   return bfd_reloc_ok;
403 }
404 \f
405 /* PLT/GOT stuff */
406
407 /* Both the headers and the entries are icache aligned.  */
408 #define PLT_ENTRY_SIZE          32
409 #define PLT_HEADER_SIZE         (4 * PLT_ENTRY_SIZE)
410 #define LARGE_PLT_THRESHOLD     32768
411 #define GOT_RESERVED_ENTRIES    1
412
413 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1"
414
415
416 /* Fill in the .plt section.  */
417
418 static void
419 sparc64_elf_build_plt (output_bfd, contents, nentries)
420      bfd *output_bfd;
421      unsigned char *contents;
422      int nentries;
423 {
424   const unsigned int nop = 0x01000000;
425   int i, j;
426   
427   /* The first four entries are reserved, and are initially undefined.
428      We fill them with `illtrap 0' to force ld.so to do something.  */
429
430   for (i = 0; i < PLT_HEADER_SIZE/4; ++i)
431     bfd_put_32 (output_bfd, 0, contents+i*4);
432
433   /* The first 32768 entries are close enough to plt1 to get there via
434      a straight branch.  */
435
436   for (i = 4; i < LARGE_PLT_THRESHOLD && i < nentries; ++i)
437     {
438       unsigned char *entry = contents + i * PLT_ENTRY_SIZE;
439       unsigned int sethi, ba;
440
441       /* sethi (. - plt0), %g1 */
442       sethi = 0x03000000 | (i * PLT_ENTRY_SIZE);
443
444       /* ba,a,pt %icc, plt1 */
445       ba = 0x30480000 | (((contents+PLT_ENTRY_SIZE) - (entry+4)) / 4 & 0x7ffff);
446
447       bfd_put_32 (output_bfd, sethi, entry);
448       bfd_put_32 (output_bfd, ba, entry+4);
449       bfd_put_32 (output_bfd, nop, entry+8);
450       bfd_put_32 (output_bfd, nop, entry+12);
451       bfd_put_32 (output_bfd, nop, entry+16);
452       bfd_put_32 (output_bfd, nop, entry+20);
453       bfd_put_32 (output_bfd, nop, entry+24);
454       bfd_put_32 (output_bfd, nop, entry+28);
455     }
456
457   /* Now the tricky bit.  Entries 32768 and higher are grouped in blocks of
458      160: 160 entries and 160 pointers.  This is to separate code from data,
459      which is much friendlier on the cache.  */
460   
461   for (; i < nentries; i += 160)
462     {
463       int block = (i + 160 <= nentries ? 160 : nentries - i);
464       for (j = 0; j < block; ++j)
465         {
466           unsigned char *entry, *ptr;
467           unsigned int ldx;
468
469           entry = contents + i*PLT_ENTRY_SIZE + j*4*6;
470           ptr = contents + i*PLT_ENTRY_SIZE + block*4*6 + j*8;
471
472           /* ldx [%o7 + ptr - entry+4], %g1 */
473           ldx = 0xc25be000 | ((ptr - entry+4) & 0x1fff);
474
475           bfd_put_32 (output_bfd, 0x8a10000f, entry);    /* mov %o7,%g5 */
476           bfd_put_32 (output_bfd, 0x40000002, entry+4);  /* call .+8 */
477           bfd_put_32 (output_bfd, nop, entry+8);         /* nop */
478           bfd_put_32 (output_bfd, ldx, entry+12);        /* ldx [%o7+P],%g1 */
479           bfd_put_32 (output_bfd, 0x83c3c001, entry+16); /* jmpl %o7+%g1,%g1 */
480           bfd_put_32 (output_bfd, 0x9e100005, entry+20); /* mov %g5,%o7 */
481
482           bfd_put_64 (output_bfd, contents - entry+4, ptr);
483         }
484     }
485 }
486
487 /* Return the offset of a particular plt entry within the .plt section.  */
488
489 static bfd_vma
490 sparc64_elf_plt_entry_offset (index)
491      int index;
492 {
493   int block, ofs;
494
495   if (index < LARGE_PLT_THRESHOLD)
496     return index * PLT_ENTRY_SIZE;
497
498   /* See above for details.  */
499
500   block = (index - LARGE_PLT_THRESHOLD) / 160;
501   ofs = (index - LARGE_PLT_THRESHOLD) % 160;
502
503   return ((bfd_vma)(LARGE_PLT_THRESHOLD + block*160) * PLT_ENTRY_SIZE
504           + ofs * 6*4);
505 }
506
507 static bfd_vma
508 sparc64_elf_plt_ptr_offset (index, max)
509      int index, max;
510 {
511   int block, ofs, last;
512
513   BFD_ASSERT(index >= LARGE_PLT_THRESHOLD);
514
515   /* See above for details.  */
516
517   block = (index - LARGE_PLT_THRESHOLD) / 160;
518   ofs = (index - LARGE_PLT_THRESHOLD) % 160;
519   last = (max - LARGE_PLT_THRESHOLD) % 160;
520
521   return ((LARGE_PLT_THRESHOLD + block*160) * PLT_ENTRY_SIZE
522           + last * 6*4
523           + ofs * 8);
524 }
525
526
527 \f
528 /* Look through the relocs for a section during the first phase, and
529    allocate space in the global offset table or procedure linkage
530    table.  */
531
532 static boolean
533 sparc64_elf_check_relocs (abfd, info, sec, relocs)
534      bfd *abfd;
535      struct bfd_link_info *info;
536      asection *sec;
537      const Elf_Internal_Rela *relocs;
538 {
539   bfd *dynobj;
540   Elf_Internal_Shdr *symtab_hdr;
541   struct elf_link_hash_entry **sym_hashes;
542   bfd_vma *local_got_offsets;
543   const Elf_Internal_Rela *rel;
544   const Elf_Internal_Rela *rel_end;
545   asection *sgot;
546   asection *srelgot;
547   asection *sreloc;
548
549   if (info->relocateable || !(sec->flags & SEC_ALLOC))
550     return true;
551
552   dynobj = elf_hash_table (info)->dynobj;
553   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
554   sym_hashes = elf_sym_hashes (abfd);
555   local_got_offsets = elf_local_got_offsets (abfd);
556
557   sgot = NULL;
558   srelgot = NULL;
559   sreloc = NULL;
560
561   rel_end = relocs + sec->reloc_count;
562   for (rel = relocs; rel < rel_end; rel++)
563     {
564       unsigned long r_symndx;
565       struct elf_link_hash_entry *h;
566
567       r_symndx = ELF64_R_SYM (rel->r_info);
568       if (r_symndx < symtab_hdr->sh_info)
569         h = NULL;
570       else
571         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
572
573       switch (ELF64_R_TYPE (rel->r_info))
574         {
575         case R_SPARC_GOT10:
576         case R_SPARC_GOT13:
577         case R_SPARC_GOT22:
578           /* This symbol requires a global offset table entry.  */
579
580           if (dynobj == NULL)
581             {
582               /* Create the .got section.  */
583               elf_hash_table (info)->dynobj = dynobj = abfd;
584               if (! _bfd_elf_create_got_section (dynobj, info))
585                 return false;
586             }
587
588           if (sgot == NULL)
589             {
590               sgot = bfd_get_section_by_name (dynobj, ".got");
591               BFD_ASSERT (sgot != NULL);
592             }
593
594           if (srelgot == NULL && (h != NULL || info->shared))
595             {
596               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
597               if (srelgot == NULL)
598                 {
599                   srelgot = bfd_make_section (dynobj, ".rela.got");
600                   if (srelgot == NULL
601                       || ! bfd_set_section_flags (dynobj, srelgot,
602                                                   (SEC_ALLOC
603                                                    | SEC_LOAD
604                                                    | SEC_HAS_CONTENTS
605                                                    | SEC_IN_MEMORY
606                                                    | SEC_LINKER_CREATED
607                                                    | SEC_READONLY))
608                       || ! bfd_set_section_alignment (dynobj, srelgot, 3))
609                     return false;
610                 }
611             }
612
613           if (h != NULL)
614             {
615               if (h->got.offset != (bfd_vma) -1)
616                 {
617                   /* We have already allocated space in the .got.  */
618                   break;
619                 }
620               h->got.offset = sgot->_raw_size;
621
622               /* Make sure this symbol is output as a dynamic symbol.  */
623               if (h->dynindx == -1)
624                 {
625                   if (! bfd_elf64_link_record_dynamic_symbol (info, h))
626                     return false;
627                 }
628
629               srelgot->_raw_size += sizeof (Elf64_External_Rela);
630             }
631           else
632             {
633               /* This is a global offset table entry for a local
634                  symbol.  */
635               if (local_got_offsets == NULL)
636                 {
637                   size_t size;
638                   register unsigned int i;
639
640                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
641                   local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
642                   if (local_got_offsets == NULL)
643                     return false;
644                   elf_local_got_offsets (abfd) = local_got_offsets;
645                   for (i = 0; i < symtab_hdr->sh_info; i++)
646                     local_got_offsets[i] = (bfd_vma) -1;
647                 }
648               if (local_got_offsets[r_symndx] != (bfd_vma) -1)
649                 {
650                   /* We have already allocated space in the .got.  */
651                   break;
652                 }
653               local_got_offsets[r_symndx] = sgot->_raw_size;
654
655               if (info->shared)
656                 {
657                   /* If we are generating a shared object, we need to
658                      output a R_SPARC_RELATIVE reloc so that the
659                      dynamic linker can adjust this GOT entry.  */
660                   srelgot->_raw_size += sizeof (Elf64_External_Rela);
661                 }
662             }
663
664           sgot->_raw_size += 8;
665
666 #if 0
667           /* Doesn't work for 64-bit -fPIC, since sethi/or builds
668              unsigned numbers.  If we permit ourselves to modify
669              code so we get sethi/xor, this could work.
670              Question: do we consider conditionally re-enabling
671              this for -fpic, once we know about object code models?  */
672           /* If the .got section is more than 0x1000 bytes, we add
673              0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
674              bit relocations have a greater chance of working.  */
675           if (sgot->_raw_size >= 0x1000
676               && elf_hash_table (info)->hgot->root.u.def.value == 0)
677             elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
678 #endif
679
680           break;
681
682         case R_SPARC_WPLT30:
683         case R_SPARC_PLT32:
684         case R_SPARC_HIPLT22:
685         case R_SPARC_LOPLT10:
686         case R_SPARC_PCPLT32:
687         case R_SPARC_PCPLT22:
688         case R_SPARC_PCPLT10:
689         case R_SPARC_PLT64:
690           /* This symbol requires a procedure linkage table entry.  We
691              actually build the entry in adjust_dynamic_symbol,
692              because this might be a case of linking PIC code without
693              linking in any dynamic objects, in which case we don't
694              need to generate a procedure linkage table after all.  */
695
696           if (h == NULL)
697             {
698               /* It does not make sense to have a procedure linkage
699                  table entry for a local symbol.  */
700               bfd_set_error (bfd_error_bad_value);
701               return false;
702             }
703
704           /* Make sure this symbol is output as a dynamic symbol.  */
705           if (h->dynindx == -1)
706             {
707               if (! bfd_elf64_link_record_dynamic_symbol (info, h))
708                 return false;
709             }
710
711           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
712           break;
713
714         case R_SPARC_PC10:
715         case R_SPARC_PC22:
716         case R_SPARC_PC_HH22:
717         case R_SPARC_PC_HM10:
718         case R_SPARC_PC_LM22:
719           if (h != NULL
720               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
721             break;
722           /* Fall through.  */
723         case R_SPARC_DISP8:
724         case R_SPARC_DISP16:
725         case R_SPARC_DISP32:
726         case R_SPARC_DISP64:
727         case R_SPARC_WDISP30:
728         case R_SPARC_WDISP22:
729         case R_SPARC_WDISP19:
730         case R_SPARC_WDISP16:
731           if (h == NULL)
732             break;
733           /* Fall through.  */
734         case R_SPARC_8:
735         case R_SPARC_16:
736         case R_SPARC_32:
737         case R_SPARC_HI22:
738         case R_SPARC_22:
739         case R_SPARC_13:
740         case R_SPARC_LO10:
741         case R_SPARC_UA32:
742         case R_SPARC_10:
743         case R_SPARC_11:
744         case R_SPARC_64:
745         case R_SPARC_OLO10:
746         case R_SPARC_HH22:
747         case R_SPARC_HM10:
748         case R_SPARC_LM22:
749         case R_SPARC_7:
750         case R_SPARC_5:
751         case R_SPARC_6:
752         case R_SPARC_HIX22:
753         case R_SPARC_LOX10:
754         case R_SPARC_H44:
755         case R_SPARC_M44:
756         case R_SPARC_L44:
757         case R_SPARC_UA64:
758         case R_SPARC_UA16:
759           /* When creating a shared object, we must copy these relocs
760              into the output file.  We create a reloc section in
761              dynobj and make room for the reloc. 
762
763              But don't do this for debugging sections -- this shows up
764              with DWARF2 -- first because they are not loaded, and
765              second because DWARF sez the debug info is not to be
766              biased by the load address.  */
767           if (info->shared && (sec->flags & SEC_ALLOC))
768             {
769               if (sreloc == NULL)
770                 {
771                   const char *name;
772
773                   name = (bfd_elf_string_from_elf_section
774                           (abfd,
775                            elf_elfheader (abfd)->e_shstrndx,
776                            elf_section_data (sec)->rel_hdr.sh_name));
777                   if (name == NULL)
778                     return false;
779
780                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
781                               && strcmp (bfd_get_section_name (abfd, sec),
782                                          name + 5) == 0);
783
784                   sreloc = bfd_get_section_by_name (dynobj, name);
785                   if (sreloc == NULL)
786                     {
787                       flagword flags;
788
789                       sreloc = bfd_make_section (dynobj, name);
790                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
791                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
792                       if ((sec->flags & SEC_ALLOC) != 0)
793                         flags |= SEC_ALLOC | SEC_LOAD;
794                       if (sreloc == NULL
795                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
796                           || ! bfd_set_section_alignment (dynobj, sreloc, 3))
797                         return false;
798                     }
799                 }
800
801               sreloc->_raw_size += sizeof (Elf64_External_Rela);
802             }
803           break;
804
805         case R_SPARC_REGISTER:
806           /* Nothing to do.  */
807           break;
808
809         default:
810           (*_bfd_error_handler)(_("%s: check_relocs: unhandled reloc type %d"),
811                                 bfd_get_filename(abfd),
812                                 ELF64_R_TYPE (rel->r_info));
813           return false;
814         }
815     }
816
817   return true;
818 }
819
820 /* Adjust a symbol defined by a dynamic object and referenced by a
821    regular object.  The current definition is in some section of the
822    dynamic object, but we're not including those sections.  We have to
823    change the definition to something the rest of the link can
824    understand.  */
825
826 static boolean
827 sparc64_elf_adjust_dynamic_symbol (info, h)
828      struct bfd_link_info *info;
829      struct elf_link_hash_entry *h;
830 {
831   bfd *dynobj;
832   asection *s;
833   unsigned int power_of_two;
834
835   dynobj = elf_hash_table (info)->dynobj;
836
837   /* Make sure we know what is going on here.  */
838   BFD_ASSERT (dynobj != NULL
839               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
840                   || h->weakdef != NULL
841                   || ((h->elf_link_hash_flags
842                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
843                       && (h->elf_link_hash_flags
844                           & ELF_LINK_HASH_REF_REGULAR) != 0
845                       && (h->elf_link_hash_flags
846                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
847
848   /* If this is a function, put it in the procedure linkage table.  We
849      will fill in the contents of the procedure linkage table later
850      (although we could actually do it here).  The STT_NOTYPE
851      condition is a hack specifically for the Oracle libraries
852      delivered for Solaris; for some inexplicable reason, they define
853      some of their functions as STT_NOTYPE when they really should be
854      STT_FUNC.  */
855   if (h->type == STT_FUNC
856       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
857       || (h->type == STT_NOTYPE
858           && (h->root.type == bfd_link_hash_defined
859               || h->root.type == bfd_link_hash_defweak)
860           && (h->root.u.def.section->flags & SEC_CODE) != 0))
861     {
862       if (! elf_hash_table (info)->dynamic_sections_created)
863         {
864           /* This case can occur if we saw a WPLT30 reloc in an input
865              file, but none of the input files were dynamic objects.
866              In such a case, we don't actually need to build a
867              procedure linkage table, and we can just do a WDISP30
868              reloc instead.  */
869           BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
870           return true;
871         }
872
873       s = bfd_get_section_by_name (dynobj, ".plt");
874       BFD_ASSERT (s != NULL);
875
876       /* The first four bit in .plt is reserved.  */
877       if (s->_raw_size == 0)
878         s->_raw_size = PLT_HEADER_SIZE;
879
880       /* If this symbol is not defined in a regular file, and we are
881          not generating a shared library, then set the symbol to this
882          location in the .plt.  This is required to make function
883          pointers compare as equal between the normal executable and
884          the shared library.  */
885       if (! info->shared
886           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
887         {
888           h->root.u.def.section = s;
889           h->root.u.def.value = s->_raw_size;
890         }
891
892       /* To simplify matters later, just store the plt index here.  */
893       h->plt.offset = s->_raw_size / PLT_ENTRY_SIZE;
894
895       /* Make room for this entry.  */
896       s->_raw_size += PLT_ENTRY_SIZE;
897
898       /* We also need to make an entry in the .rela.plt section.  */
899
900       s = bfd_get_section_by_name (dynobj, ".rela.plt");
901       BFD_ASSERT (s != NULL);
902
903       /* The first plt entries are reserved, and the relocations must
904          pair up exactly.  */
905       if (s->_raw_size == 0)
906         s->_raw_size += (PLT_HEADER_SIZE/PLT_ENTRY_SIZE
907                          * sizeof (Elf64_External_Rela));
908
909       s->_raw_size += sizeof (Elf64_External_Rela);
910
911       /* The procedure linkage table size is bounded by the magnitude
912          of the offset we can describe in the entry.  */
913       if (s->_raw_size >= (bfd_vma)1 << 32)
914         {
915           bfd_set_error (bfd_error_bad_value);
916           return false;
917         }
918
919       return true;
920     }
921
922   /* If this is a weak symbol, and there is a real definition, the
923      processor independent code will have arranged for us to see the
924      real definition first, and we can just use the same value.  */
925   if (h->weakdef != NULL)
926     {
927       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
928                   || h->weakdef->root.type == bfd_link_hash_defweak);
929       h->root.u.def.section = h->weakdef->root.u.def.section;
930       h->root.u.def.value = h->weakdef->root.u.def.value;
931       return true;
932     }
933
934   /* This is a reference to a symbol defined by a dynamic object which
935      is not a function.  */
936
937   /* If we are creating a shared library, we must presume that the
938      only references to the symbol are via the global offset table.
939      For such cases we need not do anything here; the relocations will
940      be handled correctly by relocate_section.  */
941   if (info->shared)
942     return true;
943
944   /* We must allocate the symbol in our .dynbss section, which will
945      become part of the .bss section of the executable.  There will be
946      an entry for this symbol in the .dynsym section.  The dynamic
947      object will contain position independent code, so all references
948      from the dynamic object to this symbol will go through the global
949      offset table.  The dynamic linker will use the .dynsym entry to
950      determine the address it must put in the global offset table, so
951      both the dynamic object and the regular object will refer to the
952      same memory location for the variable.  */
953
954   s = bfd_get_section_by_name (dynobj, ".dynbss");
955   BFD_ASSERT (s != NULL);
956
957   /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
958      to copy the initial value out of the dynamic object and into the
959      runtime process image.  We need to remember the offset into the
960      .rel.bss section we are going to use.  */
961   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
962     {
963       asection *srel;
964
965       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
966       BFD_ASSERT (srel != NULL);
967       srel->_raw_size += sizeof (Elf64_External_Rela);
968       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
969     }
970
971   /* We need to figure out the alignment required for this symbol.  I
972      have no idea how ELF linkers handle this.  16-bytes is the size
973      of the largest type that requires hard alignment -- long double.  */
974   power_of_two = bfd_log2 (h->size);
975   if (power_of_two > 4)
976     power_of_two = 4;
977
978   /* Apply the required alignment.  */
979   s->_raw_size = BFD_ALIGN (s->_raw_size,
980                             (bfd_size_type) (1 << power_of_two));
981   if (power_of_two > bfd_get_section_alignment (dynobj, s))
982     {
983       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
984         return false;
985     }
986
987   /* Define the symbol as being at this point in the section.  */
988   h->root.u.def.section = s;
989   h->root.u.def.value = s->_raw_size;
990
991   /* Increment the section size to make room for the symbol.  */
992   s->_raw_size += h->size;
993
994   return true;
995 }
996
997 /* Set the sizes of the dynamic sections.  */
998
999 static boolean
1000 sparc64_elf_size_dynamic_sections (output_bfd, info)
1001      bfd *output_bfd;
1002      struct bfd_link_info *info;
1003 {
1004   bfd *dynobj;
1005   asection *s;
1006   boolean reltext;
1007   boolean relplt;
1008
1009   dynobj = elf_hash_table (info)->dynobj;
1010   BFD_ASSERT (dynobj != NULL);
1011
1012   if (elf_hash_table (info)->dynamic_sections_created)
1013     {
1014       /* Set the contents of the .interp section to the interpreter.  */
1015       if (! info->shared)
1016         {
1017           s = bfd_get_section_by_name (dynobj, ".interp");
1018           BFD_ASSERT (s != NULL);
1019           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1020           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1021         }
1022     }
1023   else
1024     {
1025       /* We may have created entries in the .rela.got section.
1026          However, if we are not creating the dynamic sections, we will
1027          not actually use these entries.  Reset the size of .rela.got,
1028          which will cause it to get stripped from the output file
1029          below.  */
1030       s = bfd_get_section_by_name (dynobj, ".rela.got");
1031       if (s != NULL)
1032         s->_raw_size = 0;
1033     }
1034
1035   /* The check_relocs and adjust_dynamic_symbol entry points have
1036      determined the sizes of the various dynamic sections.  Allocate
1037      memory for them.  */
1038   reltext = false;
1039   relplt = false;
1040   for (s = dynobj->sections; s != NULL; s = s->next)
1041     {
1042       const char *name;
1043       boolean strip;
1044
1045       if ((s->flags & SEC_LINKER_CREATED) == 0)
1046         continue;
1047
1048       /* It's OK to base decisions on the section name, because none
1049          of the dynobj section names depend upon the input files.  */
1050       name = bfd_get_section_name (dynobj, s);
1051
1052       strip = false;
1053
1054       if (strncmp (name, ".rela", 5) == 0)
1055         {
1056           if (s->_raw_size == 0)
1057             {
1058               /* If we don't need this section, strip it from the
1059                  output file.  This is to handle .rela.bss and
1060                  .rel.plt.  We must create it in
1061                  create_dynamic_sections, because it must be created
1062                  before the linker maps input sections to output
1063                  sections.  The linker does that before
1064                  adjust_dynamic_symbol is called, and it is that
1065                  function which decides whether anything needs to go
1066                  into these sections.  */
1067               strip = true;
1068             }
1069           else
1070             {
1071               const char *outname;
1072               asection *target;
1073
1074               /* If this relocation section applies to a read only
1075                  section, then we probably need a DT_TEXTREL entry.  */
1076               outname = bfd_get_section_name (output_bfd,
1077                                               s->output_section);
1078               target = bfd_get_section_by_name (output_bfd, outname + 5);
1079               if (target != NULL
1080                   && (target->flags & SEC_READONLY) != 0)
1081                 reltext = true;
1082
1083               if (strcmp (name, ".rela.plt") == 0)
1084                 relplt = true;
1085
1086               /* We use the reloc_count field as a counter if we need
1087                  to copy relocs into the output file.  */
1088               s->reloc_count = 0;
1089             }
1090         }
1091       else if (strcmp (name, ".plt") != 0
1092                && strncmp (name, ".got", 4) != 0)
1093         {
1094           /* It's not one of our sections, so don't allocate space.  */
1095           continue;
1096         }
1097
1098       if (strip)
1099         {
1100           _bfd_strip_section_from_output (s);
1101           continue;
1102         }
1103
1104       /* Allocate memory for the section contents.  Zero the memory
1105          for the benefit of .rela.plt, which has 4 unused entries
1106          at the beginning, and we don't want garbage.  */
1107       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1108       if (s->contents == NULL && s->_raw_size != 0)
1109         return false;
1110     }
1111
1112   if (elf_hash_table (info)->dynamic_sections_created)
1113     {
1114       /* Add some entries to the .dynamic section.  We fill in the
1115          values later, in sparc64_elf_finish_dynamic_sections, but we
1116          must add the entries now so that we get the correct size for
1117          the .dynamic section.  The DT_DEBUG entry is filled in by the
1118          dynamic linker and used by the debugger.  */
1119       if (! info->shared)
1120         {
1121           if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
1122             return false;
1123         }
1124
1125       if (relplt)
1126         {
1127           if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0)
1128               || ! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1129               || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1130               || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0)
1131               || ! bfd_elf64_add_dynamic_entry (info, DT_SPARC_PLTFMT,
1132                                                 (info->shared != 0) + 1))
1133             return false;
1134         }
1135
1136       if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
1137           || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
1138           || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
1139                                             sizeof (Elf64_External_Rela)))
1140         return false;
1141
1142       if (reltext)
1143         {
1144           if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
1145             return false;
1146         }
1147     }
1148
1149   /* If we are generating a shared library, we generate a section
1150      symbol for each output section for which we might need to copy
1151      relocs.  These are local symbols, which means that they must come
1152      first in the dynamic symbol table.  That means we must increment
1153      the dynamic symbol index of every other dynamic symbol.  */
1154   if (info->shared)
1155     {
1156       int c;
1157
1158       c = 0;
1159       for (s = output_bfd->sections; s != NULL; s = s->next)
1160         {
1161           if ((s->flags & SEC_LINKER_CREATED) != 0
1162               || (s->flags & SEC_ALLOC) == 0)
1163             continue;
1164
1165           elf_section_data (s)->dynindx = c + 1;
1166
1167           /* These symbols will have no names, so we don't need to
1168              fiddle with dynstr_index.  */
1169
1170           ++c;
1171         }
1172
1173       elf_link_hash_traverse (elf_hash_table (info),
1174                               sparc64_elf_adjust_dynindx,
1175                               (PTR) &c);
1176       elf_hash_table (info)->dynsymcount += c;
1177     }
1178
1179   return true;
1180 }
1181
1182 /* Increment the index of a dynamic symbol by a given amount.  Called
1183    via elf_link_hash_traverse.  */
1184
1185 static boolean
1186 sparc64_elf_adjust_dynindx (h, cparg)
1187      struct elf_link_hash_entry *h;
1188      PTR cparg;
1189 {
1190   int *cp = (int *) cparg;
1191
1192   if (h->dynindx != -1)
1193     h->dynindx += *cp;
1194   return true;
1195 }
1196
1197 \f
1198 /* Relocate a SPARC64 ELF section.  */
1199
1200 static boolean
1201 sparc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1202                               contents, relocs, local_syms, local_sections)
1203      bfd *output_bfd;
1204      struct bfd_link_info *info;
1205      bfd *input_bfd;
1206      asection *input_section;
1207      bfd_byte *contents;
1208      Elf_Internal_Rela *relocs;
1209      Elf_Internal_Sym *local_syms;
1210      asection **local_sections;
1211 {
1212   bfd *dynobj;
1213   Elf_Internal_Shdr *symtab_hdr;
1214   struct elf_link_hash_entry **sym_hashes;
1215   bfd_vma *local_got_offsets;
1216   bfd_vma got_base;
1217   asection *sgot;
1218   asection *splt;
1219   asection *sreloc;
1220   Elf_Internal_Rela *rel;
1221   Elf_Internal_Rela *relend;
1222
1223   dynobj = elf_hash_table (info)->dynobj;
1224   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1225   sym_hashes = elf_sym_hashes (input_bfd);
1226   local_got_offsets = elf_local_got_offsets (input_bfd);
1227
1228   if (elf_hash_table(info)->hgot == NULL)
1229     got_base = 0;
1230   else
1231     got_base = elf_hash_table (info)->hgot->root.u.def.value;
1232
1233   sgot = splt = sreloc = NULL;
1234
1235   rel = relocs;
1236   relend = relocs + input_section->reloc_count;
1237   for (; rel < relend; rel++)
1238     {
1239       int r_type;
1240       reloc_howto_type *howto;
1241       long r_symndx;
1242       struct elf_link_hash_entry *h;
1243       Elf_Internal_Sym *sym;
1244       asection *sec;
1245       bfd_vma relocation;
1246       bfd_reloc_status_type r;
1247
1248       r_type = ELF64_R_TYPE (rel->r_info);
1249       if (r_type < 0 || r_type >= (int) R_SPARC_max)
1250         {
1251           bfd_set_error (bfd_error_bad_value);
1252           return false;
1253         }
1254       howto = sparc64_elf_howto_table + r_type;
1255
1256       r_symndx = ELF64_R_SYM (rel->r_info);
1257
1258       if (info->relocateable)
1259         {
1260           /* This is a relocateable link.  We don't have to change
1261              anything, unless the reloc is against a section symbol,
1262              in which case we have to adjust according to where the
1263              section symbol winds up in the output section.  */
1264           if (r_symndx < symtab_hdr->sh_info)
1265             {
1266               sym = local_syms + r_symndx;
1267               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1268                 {
1269                   sec = local_sections[r_symndx];
1270                   rel->r_addend += sec->output_offset + sym->st_value;
1271                 }
1272             }
1273
1274           continue;
1275         }
1276
1277       /* This is a final link.  */
1278       h = NULL;
1279       sym = NULL;
1280       sec = NULL;
1281       if (r_symndx < symtab_hdr->sh_info)
1282         {
1283           sym = local_syms + r_symndx;
1284           sec = local_sections[r_symndx];
1285           relocation = (sec->output_section->vma
1286                         + sec->output_offset
1287                         + sym->st_value);
1288         }
1289       else
1290         {
1291           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1292           while (h->root.type == bfd_link_hash_indirect
1293                  || h->root.type == bfd_link_hash_warning)
1294             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1295           if (h->root.type == bfd_link_hash_defined
1296               || h->root.type == bfd_link_hash_defweak)
1297             {
1298               boolean skip_it = false;
1299               sec = h->root.u.def.section;
1300
1301               switch (r_type)
1302                 {
1303                 case R_SPARC_WPLT30:
1304                 case R_SPARC_PLT32:
1305                 case R_SPARC_HIPLT22:
1306                 case R_SPARC_LOPLT10:
1307                 case R_SPARC_PCPLT32:
1308                 case R_SPARC_PCPLT22:
1309                 case R_SPARC_PCPLT10:
1310                 case R_SPARC_PLT64:
1311                   if (h->plt.offset != (bfd_vma) -1)
1312                     skip_it = true;
1313                   break;
1314
1315                 case R_SPARC_GOT10:
1316                 case R_SPARC_GOT13:
1317                 case R_SPARC_GOT22:
1318                   if (elf_hash_table(info)->dynamic_sections_created
1319                       && (!info->shared
1320                           || (!info->symbolic && h->dynindx != -1)
1321                           || !(h->elf_link_hash_flags
1322                                & ELF_LINK_HASH_DEF_REGULAR)))
1323                     skip_it = true;
1324                   break;
1325
1326                 case R_SPARC_PC10:
1327                 case R_SPARC_PC22:
1328                 case R_SPARC_PC_HH22:
1329                 case R_SPARC_PC_HM10:
1330                 case R_SPARC_PC_LM22:
1331                   if (!strcmp(h->root.root.string, "_GLOBAL_OFFSET_TABLE_"))
1332                     break;
1333                   /* FALLTHRU */
1334
1335                 case R_SPARC_8:
1336                 case R_SPARC_16:
1337                 case R_SPARC_32:
1338                 case R_SPARC_DISP8:
1339                 case R_SPARC_DISP16:
1340                 case R_SPARC_DISP32:
1341                 case R_SPARC_WDISP30:
1342                 case R_SPARC_WDISP22:
1343                 case R_SPARC_HI22:
1344                 case R_SPARC_22:
1345                 case R_SPARC_13:
1346                 case R_SPARC_LO10:
1347                 case R_SPARC_UA32:
1348                 case R_SPARC_10:
1349                 case R_SPARC_11:
1350                 case R_SPARC_64:
1351                 case R_SPARC_OLO10:
1352                 case R_SPARC_HH22:
1353                 case R_SPARC_HM10:
1354                 case R_SPARC_LM22:
1355                 case R_SPARC_WDISP19:
1356                 case R_SPARC_WDISP16:
1357                 case R_SPARC_7:
1358                 case R_SPARC_5:
1359                 case R_SPARC_6:
1360                 case R_SPARC_DISP64:
1361                 case R_SPARC_HIX22:
1362                 case R_SPARC_LOX10:
1363                 case R_SPARC_H44:
1364                 case R_SPARC_M44:
1365                 case R_SPARC_L44:
1366                 case R_SPARC_UA64:
1367                 case R_SPARC_UA16:
1368                   if (info->shared
1369                       && ((!info->symbolic && h->dynindx != -1)
1370                           || !(h->elf_link_hash_flags
1371                                & ELF_LINK_HASH_DEF_REGULAR)))
1372                     skip_it = true;
1373                   break;
1374                 }
1375
1376               if (skip_it)
1377                 {
1378                   /* In these cases, we don't need the relocation
1379                      value.  We check specially because in some
1380                      obscure cases sec->output_section will be NULL.  */
1381                   relocation = 0;
1382                 }
1383               else
1384                 {
1385                   relocation = (h->root.u.def.value
1386                                 + sec->output_section->vma
1387                                 + sec->output_offset);
1388                 }
1389             }
1390           else if (h->root.type == bfd_link_hash_undefweak)
1391             relocation = 0;
1392           else if (info->shared && !info->symbolic && !info->no_undefined)
1393             relocation = 0;
1394           else
1395             {
1396               if (! ((*info->callbacks->undefined_symbol)
1397                      (info, h->root.root.string, input_bfd,
1398                       input_section, rel->r_offset)))
1399                 return false;
1400               relocation = 0;
1401             }
1402         }
1403
1404       /* When generating a shared object, these relocations are copied
1405          into the output file to be resolved at run time.  */
1406       if (info->shared && (input_section->flags & SEC_ALLOC))
1407         {
1408           switch (r_type)
1409             {
1410             case R_SPARC_PC10:
1411             case R_SPARC_PC22:
1412             case R_SPARC_PC_HH22:
1413             case R_SPARC_PC_HM10:
1414             case R_SPARC_PC_LM22:
1415               if (h != NULL
1416                   && !strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_"))
1417                 break;
1418               /* Fall through.  */
1419             case R_SPARC_DISP8:
1420             case R_SPARC_DISP16:
1421             case R_SPARC_DISP32:
1422             case R_SPARC_WDISP30:
1423             case R_SPARC_WDISP22:
1424             case R_SPARC_WDISP19:
1425             case R_SPARC_WDISP16:
1426             case R_SPARC_DISP64:
1427               if (h == NULL)
1428                 break;
1429               /* Fall through.  */
1430             case R_SPARC_8:
1431             case R_SPARC_16:
1432             case R_SPARC_32:
1433             case R_SPARC_HI22:
1434             case R_SPARC_22:
1435             case R_SPARC_13:
1436             case R_SPARC_LO10:
1437             case R_SPARC_UA32:
1438             case R_SPARC_10:
1439             case R_SPARC_11:
1440             case R_SPARC_64:
1441             case R_SPARC_OLO10:
1442             case R_SPARC_HH22:
1443             case R_SPARC_HM10:
1444             case R_SPARC_LM22:
1445             case R_SPARC_7:
1446             case R_SPARC_5:
1447             case R_SPARC_6:
1448             case R_SPARC_HIX22:
1449             case R_SPARC_LOX10:
1450             case R_SPARC_H44:
1451             case R_SPARC_M44:
1452             case R_SPARC_L44:
1453             case R_SPARC_UA64:
1454             case R_SPARC_UA16:
1455               {
1456                 Elf_Internal_Rela outrel;
1457                 boolean skip;
1458
1459                 if (sreloc == NULL)
1460                   {
1461                     const char *name =
1462                       (bfd_elf_string_from_elf_section
1463                        (input_bfd,
1464                         elf_elfheader (input_bfd)->e_shstrndx,
1465                         elf_section_data (input_section)->rel_hdr.sh_name));
1466
1467                     if (name == NULL)
1468                       return false;
1469
1470                     BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1471                                 && strcmp (bfd_get_section_name(input_bfd,
1472                                                                 input_section),
1473                                            name + 5) == 0);
1474
1475                     sreloc = bfd_get_section_by_name (dynobj, name);
1476                     BFD_ASSERT (sreloc != NULL);
1477                   }
1478
1479                 skip = false;
1480
1481                 if (elf_section_data (input_section)->stab_info == NULL)
1482                   outrel.r_offset = rel->r_offset;
1483                 else
1484                   {
1485                     bfd_vma off;
1486
1487                     off = (_bfd_stab_section_offset
1488                            (output_bfd, &elf_hash_table (info)->stab_info,
1489                             input_section,
1490                             &elf_section_data (input_section)->stab_info,
1491                             rel->r_offset));
1492                     if (off == MINUS_ONE)
1493                       skip = true;
1494                     outrel.r_offset = off;
1495                   }
1496
1497                 outrel.r_offset += (input_section->output_section->vma
1498                                     + input_section->output_offset);
1499
1500                 /* Optimize unaligned reloc usage now that we know where
1501                    it finally resides.  */
1502                 switch (r_type)
1503                   {
1504                   case R_SPARC_16:
1505                     if (outrel.r_offset & 1) r_type = R_SPARC_UA16;
1506                     break;
1507                   case R_SPARC_UA16:
1508                     if (!(outrel.r_offset & 1)) r_type = R_SPARC_16;
1509                     break;
1510                   case R_SPARC_32:
1511                     if (outrel.r_offset & 3) r_type = R_SPARC_UA32;
1512                     break;
1513                   case R_SPARC_UA32:
1514                     if (!(outrel.r_offset & 3)) r_type = R_SPARC_32;
1515                     break;
1516                   case R_SPARC_64:
1517                     if (outrel.r_offset & 7) r_type = R_SPARC_UA64;
1518                     break;
1519                   case R_SPARC_UA64:
1520                     if (!(outrel.r_offset & 7)) r_type = R_SPARC_64;
1521                     break;
1522                   }
1523
1524                 if (skip)
1525                   memset (&outrel, 0, sizeof outrel);
1526                 /* h->dynindx may be -1 if the symbol was marked to
1527                    become local.  */
1528                 else if (h != NULL
1529                          && ((! info->symbolic && h->dynindx != -1)
1530                              || (h->elf_link_hash_flags
1531                                  & ELF_LINK_HASH_DEF_REGULAR) == 0))
1532                   {
1533                     BFD_ASSERT (h->dynindx != -1);
1534                     outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1535                     outrel.r_addend = rel->r_addend;
1536                   }
1537                 else
1538                   {
1539                     if (r_type == R_SPARC_64)
1540                       {
1541                         outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
1542                         outrel.r_addend = relocation + rel->r_addend;
1543                       }
1544                     else
1545                       {
1546                         long indx;
1547
1548                         if (h == NULL)
1549                           sec = local_sections[r_symndx];
1550                         else
1551                           {
1552                             BFD_ASSERT (h->root.type == bfd_link_hash_defined
1553                                         || (h->root.type
1554                                             == bfd_link_hash_defweak));
1555                             sec = h->root.u.def.section;
1556                           }
1557                         if (sec != NULL && bfd_is_abs_section (sec))
1558                           indx = 0;
1559                         else if (sec == NULL || sec->owner == NULL)
1560                           {
1561                             bfd_set_error (bfd_error_bad_value);
1562                             return false;
1563                           }
1564                         else
1565                           {
1566                             asection *osec;
1567
1568                             osec = sec->output_section;
1569                             indx = elf_section_data (osec)->dynindx;
1570
1571                             /* FIXME: we really should be able to link non-pic
1572                                shared libraries.  */
1573                             if (indx == 0)
1574                               {
1575                                 BFD_FAIL ();
1576                                 (*_bfd_error_handler)
1577                                   (_("%s: probably compiled without -fPIC?"),
1578                                    bfd_get_filename (input_bfd));
1579                                 bfd_set_error (bfd_error_bad_value);
1580                                 return false;
1581                               }
1582                           }
1583
1584                         outrel.r_info = ELF64_R_INFO (indx, r_type);
1585
1586                         /* For non-RELATIVE dynamic relocations, we keep the
1587                            same symbol, and so generally the same addend.  But
1588                            we do need to adjust those relocations referencing
1589                            sections.  */
1590                         outrel.r_addend = rel->r_addend;
1591                         if (r_symndx < symtab_hdr->sh_info
1592                             && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1593                           outrel.r_addend += sec->output_offset+sym->st_value;
1594                       }
1595                   }
1596
1597                 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1598                                            (((Elf64_External_Rela *)
1599                                              sreloc->contents)
1600                                             + sreloc->reloc_count));
1601                 ++sreloc->reloc_count;
1602
1603                 /* This reloc will be computed at runtime, so there's no
1604                    need to do anything now, unless this is a RELATIVE
1605                    reloc in an unallocated section.  */
1606                 if (skip
1607                     || (input_section->flags & SEC_ALLOC) != 0
1608                     || ELF64_R_TYPE (outrel.r_info) != R_SPARC_RELATIVE)
1609                   continue;
1610               }
1611             break;
1612             }
1613         }
1614
1615       switch (r_type)
1616         {
1617         case R_SPARC_GOT10:
1618         case R_SPARC_GOT13:
1619         case R_SPARC_GOT22:
1620           /* Relocation is to the entry for this symbol in the global
1621              offset table.  */
1622           if (sgot == NULL)
1623             {
1624               sgot = bfd_get_section_by_name (dynobj, ".got");
1625               BFD_ASSERT (sgot != NULL);
1626             }
1627
1628           if (h != NULL)
1629             {
1630               bfd_vma off = h->got.offset;
1631               BFD_ASSERT (off != (bfd_vma) -1);
1632
1633               if (! elf_hash_table (info)->dynamic_sections_created
1634                   || (info->shared
1635                       && (info->symbolic || h->dynindx == -1)
1636                       && (h->elf_link_hash_flags
1637                           & ELF_LINK_HASH_DEF_REGULAR)))
1638                 {
1639                   /* This is actually a static link, or it is a -Bsymbolic
1640                      link and the symbol is defined locally, or the symbol
1641                      was forced to be local because of a version file.  We
1642                      must initialize this entry in the global offset table.
1643                      Since the offset must always be a multiple of 8, we
1644                      use the least significant bit to record whether we
1645                      have initialized it already.
1646
1647                      When doing a dynamic link, we create a .rela.got
1648                      relocation entry to initialize the value.  This is
1649                      done in the finish_dynamic_symbol routine.  */
1650
1651                   if ((off & 1) != 0)
1652                     off &= ~1;
1653                   else
1654                     {
1655                       bfd_put_64 (output_bfd, relocation,
1656                                   sgot->contents + off);
1657                       h->got.offset |= 1;
1658                     }
1659                 }
1660               relocation = sgot->output_offset + off - got_base;
1661             }
1662           else
1663             {
1664               bfd_vma off;
1665
1666               BFD_ASSERT (local_got_offsets != NULL);
1667               off = local_got_offsets[r_symndx];
1668               BFD_ASSERT (off != (bfd_vma) -1);
1669
1670               /* The offset must always be a multiple of 8.  We use
1671                  the least significant bit to record whether we have
1672                  already processed this entry.  */
1673               if ((off & 1) != 0)
1674                 off &= ~1;
1675               else
1676                 {
1677                   bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1678                   local_got_offsets[r_symndx] |= 1;
1679
1680                   if (info->shared)
1681                     {
1682                       asection *srelgot;
1683                       Elf_Internal_Rela outrel;
1684
1685                       /* We need to generate a R_SPARC_RELATIVE reloc
1686                          for the dynamic linker.  */
1687                       srelgot = bfd_get_section_by_name(dynobj, ".rela.got");
1688                       BFD_ASSERT (srelgot != NULL);
1689
1690                       outrel.r_offset = (sgot->output_section->vma
1691                                          + sgot->output_offset
1692                                          + off);
1693                       outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
1694                       outrel.r_addend = relocation;
1695                       bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1696                                                  (((Elf64_External_Rela *)
1697                                                    srelgot->contents)
1698                                                   + srelgot->reloc_count));
1699                       ++srelgot->reloc_count;
1700                     }
1701                 }
1702               relocation = sgot->output_offset + off - got_base;
1703             }
1704           goto do_default;
1705
1706         case R_SPARC_WPLT30:
1707         case R_SPARC_PLT32:
1708         case R_SPARC_HIPLT22:
1709         case R_SPARC_LOPLT10:
1710         case R_SPARC_PCPLT32:
1711         case R_SPARC_PCPLT22:
1712         case R_SPARC_PCPLT10:
1713         case R_SPARC_PLT64:
1714           /* Relocation is to the entry for this symbol in the
1715              procedure linkage table.  */
1716           BFD_ASSERT (h != NULL);
1717
1718           if (h->plt.offset == (bfd_vma) -1)
1719             {
1720               /* We didn't make a PLT entry for this symbol.  This
1721                  happens when statically linking PIC code, or when
1722                  using -Bsymbolic.  */
1723               goto do_default;
1724             }
1725
1726           if (splt == NULL)
1727             {
1728               splt = bfd_get_section_by_name (dynobj, ".plt");
1729               BFD_ASSERT (splt != NULL);
1730             }
1731
1732           relocation = (splt->output_section->vma
1733                         + splt->output_offset
1734                         + sparc64_elf_plt_entry_offset (h->plt.offset));
1735           goto do_default;
1736
1737         case R_SPARC_OLO10:
1738           {
1739             bfd_vma x;
1740
1741             relocation += rel->r_addend;
1742             relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
1743
1744             x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1745             x = (x & ~0x1fff) | (relocation & 0x1fff);
1746             bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1747
1748             r = bfd_check_overflow (howto->complain_on_overflow,
1749                                     howto->bitsize, howto->rightshift,
1750                                     bfd_arch_bits_per_address (input_bfd),
1751                                     relocation);
1752           }
1753           break;
1754
1755         case R_SPARC_WDISP16:
1756           {
1757             bfd_vma x;
1758
1759             relocation += rel->r_addend;
1760             /* Adjust for pc-relative-ness.  */
1761             relocation -= (input_section->output_section->vma
1762                            + input_section->output_offset);
1763             relocation -= rel->r_offset;
1764
1765             x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1766             x = (x & ~0x303fff) | ((((relocation >> 2) & 0xc000) << 6)
1767                                    | ((relocation >> 2) & 0x3fff));
1768             bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1769
1770             r = bfd_check_overflow (howto->complain_on_overflow,
1771                                     howto->bitsize, howto->rightshift,
1772                                     bfd_arch_bits_per_address (input_bfd),
1773                                     relocation);
1774           }
1775           break;
1776
1777         case R_SPARC_HIX22:
1778           {
1779             bfd_vma x;
1780
1781             relocation += rel->r_addend;
1782             relocation = relocation ^ MINUS_ONE;
1783
1784             x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1785             x = (x & ~0x3fffff) | ((relocation >> 10) & 0x3fffff);
1786             bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1787
1788             r = bfd_check_overflow (howto->complain_on_overflow,
1789                                     howto->bitsize, howto->rightshift,
1790                                     bfd_arch_bits_per_address (input_bfd),
1791                                     relocation);
1792           }
1793           break;
1794
1795         case R_SPARC_LOX10:
1796           {
1797             bfd_vma x;
1798
1799             relocation += rel->r_addend;
1800             relocation = (relocation & 0x3ff) | 0x1c00;
1801
1802             x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1803             x = (x & ~0x1fff) | relocation;
1804             bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1805
1806             r = bfd_reloc_ok;
1807           }
1808           break;
1809
1810         default:
1811         do_default:
1812           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1813                                         contents, rel->r_offset,
1814                                         relocation, rel->r_addend);
1815           break;
1816         }
1817
1818       switch (r)
1819         {
1820         case bfd_reloc_ok:
1821           break;
1822
1823         default:
1824         case bfd_reloc_outofrange:
1825           abort ();
1826
1827         case bfd_reloc_overflow:
1828           {
1829             const char *name;
1830
1831             if (h != NULL)
1832               {
1833                 if (h->root.type == bfd_link_hash_undefweak
1834                     && howto->pc_relative)
1835                   {
1836                     /* Assume this is a call protected by other code that
1837                        detect the symbol is undefined.  If this is the case,
1838                        we can safely ignore the overflow.  If not, the 
1839                        program is hosed anyway, and a little warning isn't
1840                        going to help.  */
1841                     break;
1842                   }
1843                   
1844                 name = h->root.root.string;
1845               }
1846             else
1847               {
1848                 name = (bfd_elf_string_from_elf_section
1849                         (input_bfd,
1850                          symtab_hdr->sh_link,
1851                          sym->st_name));
1852                 if (name == NULL)
1853                   return false;
1854                 if (*name == '\0')
1855                   name = bfd_section_name (input_bfd, sec);
1856               }
1857             if (! ((*info->callbacks->reloc_overflow)
1858                    (info, name, howto->name, (bfd_vma) 0,
1859                     input_bfd, input_section, rel->r_offset)))
1860               return false;
1861           }
1862         break;
1863         }
1864     }
1865
1866   return true;
1867 }
1868
1869 /* Finish up dynamic symbol handling.  We set the contents of various
1870    dynamic sections here.  */
1871
1872 static boolean
1873 sparc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
1874      bfd *output_bfd;
1875      struct bfd_link_info *info;
1876      struct elf_link_hash_entry *h;
1877      Elf_Internal_Sym *sym;
1878 {
1879   bfd *dynobj;
1880
1881   dynobj = elf_hash_table (info)->dynobj;
1882
1883   if (h->plt.offset != (bfd_vma) -1)
1884     {
1885       asection *splt;
1886       asection *srela;
1887       Elf_Internal_Rela rela;
1888
1889       /* This symbol has an entry in the PLT.  Set it up. */
1890
1891       BFD_ASSERT (h->dynindx != -1);
1892
1893       splt = bfd_get_section_by_name (dynobj, ".plt");
1894       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1895       BFD_ASSERT (splt != NULL && srela != NULL);
1896
1897       /* Fill in the entry in the .rela.plt section.  */
1898
1899       if (h->plt.offset < LARGE_PLT_THRESHOLD)
1900         {
1901           rela.r_offset = sparc64_elf_plt_entry_offset (h->plt.offset);
1902           rela.r_addend = 0;
1903         }
1904       else
1905         {
1906           int max = splt->_raw_size / PLT_ENTRY_SIZE;
1907           rela.r_offset = sparc64_elf_plt_ptr_offset (h->plt.offset, max);
1908           rela.r_addend = -(sparc64_elf_plt_entry_offset (h->plt.offset) + 4);
1909         }
1910       rela.r_offset += (splt->output_section->vma + splt->output_offset);
1911       rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1912
1913       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1914                                  ((Elf64_External_Rela *) srela->contents
1915                                   + h->plt.offset));
1916
1917       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1918         {
1919           /* Mark the symbol as undefined, rather than as defined in
1920              the .plt section.  Leave the value alone.  */
1921           sym->st_shndx = SHN_UNDEF;
1922         }
1923     }
1924
1925   if (h->got.offset != (bfd_vma) -1)
1926     {
1927       asection *sgot;
1928       asection *srela;
1929       Elf_Internal_Rela rela;
1930
1931       /* This symbol has an entry in the GOT.  Set it up.  */
1932
1933       sgot = bfd_get_section_by_name (dynobj, ".got");
1934       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1935       BFD_ASSERT (sgot != NULL && srela != NULL);
1936
1937       rela.r_offset = (sgot->output_section->vma
1938                        + sgot->output_offset
1939                        + (h->got.offset &~ 1));
1940
1941       /* If this is a -Bsymbolic link, and the symbol is defined
1942          locally, we just want to emit a RELATIVE reloc.  Likewise if
1943          the symbol was forced to be local because of a version file.
1944          The entry in the global offset table will already have been
1945          initialized in the relocate_section function.  */
1946       if (info->shared
1947           && (info->symbolic || h->dynindx == -1)
1948           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1949         {
1950           asection *sec = h->root.u.def.section;
1951           rela.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
1952           rela.r_addend = (h->root.u.def.value
1953                            + sec->output_section->vma
1954                            + sec->output_offset);
1955         }
1956       else
1957         {
1958           bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1959           rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1960           rela.r_addend = 0;
1961         }
1962
1963       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1964                                  ((Elf64_External_Rela *) srela->contents
1965                                   + srela->reloc_count));
1966       ++srela->reloc_count;
1967     }
1968
1969   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1970     {
1971       asection *s;
1972       Elf_Internal_Rela rela;
1973
1974       /* This symbols needs a copy reloc.  Set it up.  */
1975
1976       BFD_ASSERT (h->dynindx != -1);
1977
1978       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1979                                    ".rela.bss");
1980       BFD_ASSERT (s != NULL);
1981
1982       rela.r_offset = (h->root.u.def.value
1983                        + h->root.u.def.section->output_section->vma
1984                        + h->root.u.def.section->output_offset);
1985       rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_COPY);
1986       rela.r_addend = 0;
1987       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1988                                  ((Elf64_External_Rela *) s->contents
1989                                   + s->reloc_count));
1990       ++s->reloc_count;
1991     }
1992
1993   /* Mark some specially defined symbols as absolute.  */
1994   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1995       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1996       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1997     sym->st_shndx = SHN_ABS;
1998
1999   return true;
2000 }
2001
2002 /* Finish up the dynamic sections.  */
2003
2004 static boolean
2005 sparc64_elf_finish_dynamic_sections (output_bfd, info)
2006      bfd *output_bfd;
2007      struct bfd_link_info *info;
2008 {
2009   bfd *dynobj;
2010   asection *sdyn;
2011   asection *sgot;
2012
2013   dynobj = elf_hash_table (info)->dynobj;
2014
2015   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2016
2017   if (elf_hash_table (info)->dynamic_sections_created)
2018     {
2019       asection *splt;
2020       Elf64_External_Dyn *dyncon, *dynconend;
2021
2022       splt = bfd_get_section_by_name (dynobj, ".plt");
2023       BFD_ASSERT (splt != NULL && sdyn != NULL);
2024
2025       dyncon = (Elf64_External_Dyn *) sdyn->contents;
2026       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2027       for (; dyncon < dynconend; dyncon++)
2028         {
2029           Elf_Internal_Dyn dyn;
2030           const char *name;
2031           boolean size;
2032
2033           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2034
2035           switch (dyn.d_tag)
2036             {
2037             case DT_PLTGOT:   name = ".plt"; size = false; break;
2038             case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
2039             case DT_JMPREL:   name = ".rela.plt"; size = false; break;
2040             default:          name = NULL; size = false; break;
2041             }
2042
2043           if (name != NULL)
2044             {
2045               asection *s;
2046
2047               s = bfd_get_section_by_name (output_bfd, name);
2048               if (s == NULL)
2049                 dyn.d_un.d_val = 0;
2050               else
2051                 {
2052                   if (! size)
2053                     dyn.d_un.d_ptr = s->vma;
2054                   else
2055                     {
2056                       if (s->_cooked_size != 0)
2057                         dyn.d_un.d_val = s->_cooked_size;
2058                       else
2059                         dyn.d_un.d_val = s->_raw_size;
2060                     }
2061                 }
2062               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2063             }
2064         }
2065
2066       /* Initialize the contents of the .plt section.  */
2067       if (splt->_raw_size > 0)
2068         {
2069           sparc64_elf_build_plt(output_bfd, splt->contents,
2070                                 splt->_raw_size / PLT_ENTRY_SIZE);
2071         }
2072
2073       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
2074         PLT_ENTRY_SIZE;
2075     }
2076
2077   /* Set the first entry in the global offset table to the address of
2078      the dynamic section.  */
2079   sgot = bfd_get_section_by_name (dynobj, ".got");
2080   BFD_ASSERT (sgot != NULL);
2081   if (sgot->_raw_size > 0)
2082     {
2083       if (sdyn == NULL)
2084         bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
2085       else
2086         bfd_put_64 (output_bfd,
2087                     sdyn->output_section->vma + sdyn->output_offset,
2088                     sgot->contents);
2089     }
2090
2091   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 8;
2092
2093   if (info->shared)
2094     {
2095       asection *sdynsym;
2096       asection *s;
2097       Elf_Internal_Sym sym;
2098       int c;
2099
2100       /* Set up the section symbols for the output sections.  */
2101
2102       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
2103       BFD_ASSERT (sdynsym != NULL);
2104
2105       sym.st_size = 0;
2106       sym.st_name = 0;
2107       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2108       sym.st_other = 0;
2109
2110       c = 0;
2111       for (s = output_bfd->sections; s != NULL; s = s->next)
2112         {
2113           int indx;
2114
2115           if (elf_section_data (s)->dynindx == 0)
2116             continue;
2117
2118           sym.st_value = s->vma;
2119
2120           indx = elf_section_data (s)->this_idx;
2121           BFD_ASSERT (indx > 0);
2122           sym.st_shndx = indx;
2123
2124           bfd_elf64_swap_symbol_out (output_bfd, &sym,
2125                                      (PTR) (((Elf64_External_Sym *)
2126                                              sdynsym->contents)
2127                                             + elf_section_data (s)->dynindx));
2128
2129           ++c;
2130         }
2131
2132       /* Set the sh_info field of the output .dynsym section to the
2133          index of the first global symbol.  */
2134       elf_section_data (sdynsym->output_section)->this_hdr.sh_info = c + 1;
2135     }
2136
2137   return true;
2138 }
2139 \f
2140 /* Functions for dealing with the e_flags field. */
2141
2142 /* Merge backend specific data from an object file to the output
2143    object file when linking.  */
2144
2145 static boolean
2146 sparc64_elf_merge_private_bfd_data (ibfd, obfd)
2147      bfd *ibfd;
2148      bfd *obfd;
2149 {
2150   boolean error;
2151   flagword new_flags, old_flags;
2152   int new_mm, old_mm;
2153
2154   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2155       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2156     return true;
2157
2158   new_flags = elf_elfheader (ibfd)->e_flags;
2159   old_flags = elf_elfheader (obfd)->e_flags;
2160
2161   if (!elf_flags_init (obfd))   /* First call, no flags set */
2162     {
2163       elf_flags_init (obfd) = true;
2164       elf_elfheader (obfd)->e_flags = new_flags;
2165     }
2166                       
2167   else if (new_flags == old_flags)      /* Compatible flags are ok */
2168     ;
2169                             
2170   else                                  /* Incompatible flags */
2171     {
2172       error = false;
2173   
2174       old_flags |= (new_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1));
2175       new_flags |= (old_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1));
2176       if ((old_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1)) ==
2177            (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1))
2178         {
2179           error = true;
2180           (*_bfd_error_handler)
2181             (_("%s: linking UltraSPARC specific with HAL specific code"),
2182              bfd_get_filename (ibfd));
2183         }
2184         
2185       /* Choose the most restrictive memory ordering */
2186       old_mm = (old_flags & EF_SPARCV9_MM);
2187       new_mm = (new_flags & EF_SPARCV9_MM);
2188       old_flags &= ~EF_SPARCV9_MM;
2189       new_flags &= ~EF_SPARCV9_MM;
2190       if (new_mm < old_mm) old_mm = new_mm;
2191       old_flags |= old_mm;
2192       new_flags |= old_mm;
2193
2194       /* Warn about any other mismatches */
2195       if (new_flags != old_flags)
2196         {
2197           error = true;
2198           (*_bfd_error_handler)
2199             (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2200              bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
2201         }
2202
2203       elf_elfheader (obfd)->e_flags = old_flags;
2204
2205       if (error)
2206         {
2207           bfd_set_error (bfd_error_bad_value);
2208           return false;
2209         }
2210     }
2211   return true;
2212 }
2213
2214 \f
2215 /* Set the right machine number for a SPARC64 ELF file.  */
2216
2217 static boolean
2218 sparc64_elf_object_p (abfd)
2219      bfd *abfd;
2220 {
2221   unsigned long mach = bfd_mach_sparc_v9;
2222   
2223   if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
2224     mach = bfd_mach_sparc_v9a;
2225   return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach);
2226 }
2227
2228 #define TARGET_BIG_SYM  bfd_elf64_sparc_vec
2229 #define TARGET_BIG_NAME "elf64-sparc"
2230 #define ELF_ARCH        bfd_arch_sparc
2231 #define ELF_MAXPAGESIZE 0x100000
2232
2233 /* This is the official ABI value.  */
2234 #define ELF_MACHINE_CODE EM_SPARCV9
2235
2236 /* This is the value that we used before the ABI was released.  */
2237 #define ELF_MACHINE_ALT1 EM_OLD_SPARCV9
2238
2239 #define elf_info_to_howto \
2240   sparc64_elf_info_to_howto
2241 #define bfd_elf64_bfd_reloc_type_lookup \
2242   sparc64_elf_reloc_type_lookup
2243
2244 #define elf_backend_create_dynamic_sections \
2245   _bfd_elf_create_dynamic_sections
2246 #define elf_backend_check_relocs \
2247   sparc64_elf_check_relocs
2248 #define elf_backend_adjust_dynamic_symbol \
2249   sparc64_elf_adjust_dynamic_symbol
2250 #define elf_backend_size_dynamic_sections \
2251   sparc64_elf_size_dynamic_sections
2252 #define elf_backend_relocate_section \
2253   sparc64_elf_relocate_section
2254 #define elf_backend_finish_dynamic_symbol \
2255   sparc64_elf_finish_dynamic_symbol
2256 #define elf_backend_finish_dynamic_sections \
2257   sparc64_elf_finish_dynamic_sections
2258
2259 #define bfd_elf64_bfd_merge_private_bfd_data \
2260   sparc64_elf_merge_private_bfd_data
2261
2262 #define elf_backend_object_p \
2263   sparc64_elf_object_p
2264
2265 #define elf_backend_want_got_plt 0
2266 #define elf_backend_plt_readonly 0
2267 #define elf_backend_want_plt_sym 1
2268
2269 /* Section 5.2.4 of the ABI specifies a 256-byte boundary for the table.  */
2270 #define elf_backend_plt_alignment 8
2271
2272 #define elf_backend_got_header_size 8
2273 #define elf_backend_plt_header_size PLT_HEADER_SIZE
2274
2275 #include "elf64-target.h"