unrecognized/unsupported reloc message
[external/binutils.git] / bfd / elfxx-sparc.c
1 /* SPARC-specific support for ELF
2    Copyright (C) 2005-2018 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21
22 /* This file handles functionality common to the different SPARC ABI's.  */
23
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "libiberty.h"
29 #include "elf-bfd.h"
30 #include "elf/sparc.h"
31 #include "opcode/sparc.h"
32 #include "elfxx-sparc.h"
33 #include "elf-vxworks.h"
34 #include "objalloc.h"
35 #include "hashtab.h"
36
37 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
38 #define MINUS_ONE (~ (bfd_vma) 0)
39
40 #define ABI_64_P(abfd) \
41   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
42
43 /* The relocation "howto" table.  */
44
45 /* Utility for performing the standard initial work of an instruction
46    relocation.
47    *PRELOCATION will contain the relocated item.
48    *PINSN will contain the instruction from the input stream.
49    If the result is `bfd_reloc_other' the caller can continue with
50    performing the relocation.  Otherwise it must stop and return the
51    value to its caller.  */
52
53 static bfd_reloc_status_type
54 init_insn_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
55                  void * data, asection *input_section, bfd *output_bfd,
56                  bfd_vma *prelocation, bfd_vma *pinsn)
57 {
58   bfd_vma relocation;
59   reloc_howto_type *howto = reloc_entry->howto;
60
61   if (output_bfd != (bfd *) NULL
62       && (symbol->flags & BSF_SECTION_SYM) == 0
63       && (! howto->partial_inplace
64           || reloc_entry->addend == 0))
65     {
66       reloc_entry->address += input_section->output_offset;
67       return bfd_reloc_ok;
68     }
69
70   /* This works because partial_inplace is FALSE.  */
71   if (output_bfd != NULL)
72     return bfd_reloc_continue;
73
74   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
75     return bfd_reloc_outofrange;
76
77   relocation = (symbol->value
78                 + symbol->section->output_section->vma
79                 + symbol->section->output_offset);
80   relocation += reloc_entry->addend;
81   if (howto->pc_relative)
82     {
83       relocation -= (input_section->output_section->vma
84                      + input_section->output_offset);
85       relocation -= reloc_entry->address;
86     }
87
88   *prelocation = relocation;
89   *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
90   return bfd_reloc_other;
91 }
92
93 /* For unsupported relocs.  */
94
95 static bfd_reloc_status_type
96 sparc_elf_notsup_reloc (bfd *abfd ATTRIBUTE_UNUSED,
97                         arelent *reloc_entry ATTRIBUTE_UNUSED,
98                         asymbol *symbol ATTRIBUTE_UNUSED,
99                         void * data ATTRIBUTE_UNUSED,
100                         asection *input_section ATTRIBUTE_UNUSED,
101                         bfd *output_bfd ATTRIBUTE_UNUSED,
102                         char **error_message ATTRIBUTE_UNUSED)
103 {
104   return bfd_reloc_notsupported;
105 }
106
107 /* Handle the WDISP16 reloc.  */
108
109 static bfd_reloc_status_type
110 sparc_elf_wdisp16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
111                          void * data, asection *input_section, bfd *output_bfd,
112                          char **error_message ATTRIBUTE_UNUSED)
113 {
114   bfd_vma relocation;
115   bfd_vma insn;
116   bfd_reloc_status_type status;
117
118   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
119                             input_section, output_bfd, &relocation, &insn);
120   if (status != bfd_reloc_other)
121     return status;
122
123   insn &= ~ (bfd_vma) 0x303fff;
124   insn |= (((relocation >> 2) & 0xc000) << 6) | ((relocation >> 2) & 0x3fff);
125   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
126
127   if ((bfd_signed_vma) relocation < - 0x40000
128       || (bfd_signed_vma) relocation > 0x3ffff)
129     return bfd_reloc_overflow;
130   else
131     return bfd_reloc_ok;
132 }
133
134 /* Handle the WDISP10 reloc.  */
135
136 static bfd_reloc_status_type
137 sparc_elf_wdisp10_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
138                          void * data, asection *input_section, bfd *output_bfd,
139                          char **error_message ATTRIBUTE_UNUSED)
140 {
141   bfd_vma relocation;
142   bfd_vma insn;
143   bfd_reloc_status_type status;
144
145   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
146                             input_section, output_bfd, &relocation, &insn);
147   if (status != bfd_reloc_other)
148     return status;
149
150   insn &= ~ (bfd_vma) 0x181fe0;
151   insn |= (((relocation >> 2) & 0x300) << 11)
152           | (((relocation >> 2) & 0xff) << 5);
153   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
154
155   if ((bfd_signed_vma) relocation < - 0x1000
156       || (bfd_signed_vma) relocation > 0xfff)
157     return bfd_reloc_overflow;
158   else
159     return bfd_reloc_ok;
160 }
161
162 /* Handle the HIX22 reloc.  */
163
164 static bfd_reloc_status_type
165 sparc_elf_hix22_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
166                        void * data, asection *input_section, bfd *output_bfd,
167                        char **error_message ATTRIBUTE_UNUSED)
168 {
169   bfd_vma relocation;
170   bfd_vma insn;
171   bfd_reloc_status_type status;
172
173   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
174                             input_section, output_bfd, &relocation, &insn);
175   if (status != bfd_reloc_other)
176     return status;
177
178   relocation ^= MINUS_ONE;
179   insn = (insn &~ (bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
180   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
181
182   if ((relocation & ~ (bfd_vma) 0xffffffff) != 0)
183     return bfd_reloc_overflow;
184   else
185     return bfd_reloc_ok;
186 }
187
188 /* Handle the LOX10 reloc.  */
189
190 static bfd_reloc_status_type
191 sparc_elf_lox10_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
192                        void * data, asection *input_section, bfd *output_bfd,
193                        char **error_message ATTRIBUTE_UNUSED)
194 {
195   bfd_vma relocation;
196   bfd_vma insn;
197   bfd_reloc_status_type status;
198
199   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
200                             input_section, output_bfd, &relocation, &insn);
201   if (status != bfd_reloc_other)
202     return status;
203
204   insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff);
205   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
206
207   return bfd_reloc_ok;
208 }
209
210 static reloc_howto_type _bfd_sparc_elf_howto_table[] =
211 {
212   HOWTO(R_SPARC_NONE,      0,3, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
213   HOWTO(R_SPARC_8,         0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_8",       FALSE,0,0x000000ff,TRUE),
214   HOWTO(R_SPARC_16,        0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_16",      FALSE,0,0x0000ffff,TRUE),
215   HOWTO(R_SPARC_32,        0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_32",      FALSE,0,0xffffffff,TRUE),
216   HOWTO(R_SPARC_DISP8,     0,0, 8,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP8",   FALSE,0,0x000000ff,TRUE),
217   HOWTO(R_SPARC_DISP16,    0,1,16,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP16",  FALSE,0,0x0000ffff,TRUE),
218   HOWTO(R_SPARC_DISP32,    0,2,32,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP32",  FALSE,0,0xffffffff,TRUE),
219   HOWTO(R_SPARC_WDISP30,   2,2,30,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
220   HOWTO(R_SPARC_WDISP22,   2,2,22,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
221   HOWTO(R_SPARC_HI22,     10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HI22",    FALSE,0,0x003fffff,TRUE),
222   HOWTO(R_SPARC_22,        0,2,22,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_22",      FALSE,0,0x003fffff,TRUE),
223   HOWTO(R_SPARC_13,        0,2,13,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_13",      FALSE,0,0x00001fff,TRUE),
224   HOWTO(R_SPARC_LO10,      0,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LO10",    FALSE,0,0x000003ff,TRUE),
225   HOWTO(R_SPARC_GOT10,     0,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT10",   FALSE,0,0x000003ff,TRUE),
226   HOWTO(R_SPARC_GOT13,     0,2,13,FALSE,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_GOT13",   FALSE,0,0x00001fff,TRUE),
227   HOWTO(R_SPARC_GOT22,    10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT22",   FALSE,0,0x003fffff,TRUE),
228   HOWTO(R_SPARC_PC10,      0,2,10,TRUE, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC10",    FALSE,0,0x000003ff,TRUE),
229   HOWTO(R_SPARC_PC22,     10,2,22,TRUE, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PC22",    FALSE,0,0x003fffff,TRUE),
230   HOWTO(R_SPARC_WPLT30,    2,2,30,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WPLT30",  FALSE,0,0x3fffffff,TRUE),
231   HOWTO(R_SPARC_COPY,      0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_COPY",    FALSE,0,0x00000000,TRUE),
232   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),
233   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),
234   HOWTO(R_SPARC_RELATIVE,  0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
235   HOWTO(R_SPARC_UA32,      0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA32",    FALSE,0,0xffffffff,TRUE),
236   HOWTO(R_SPARC_PLT32,     0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PLT32",   FALSE,0,0xffffffff,TRUE),
237   HOWTO(R_SPARC_HIPLT22,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_HIPLT22",  FALSE,0,0x00000000,TRUE),
238   HOWTO(R_SPARC_LOPLT10,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_LOPLT10",  FALSE,0,0x00000000,TRUE),
239   HOWTO(R_SPARC_PCPLT32,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT32",  FALSE,0,0x00000000,TRUE),
240   HOWTO(R_SPARC_PCPLT22,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT22",  FALSE,0,0x00000000,TRUE),
241   HOWTO(R_SPARC_PCPLT10,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT10",  FALSE,0,0x00000000,TRUE),
242   HOWTO(R_SPARC_10,        0,2,10,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_10",      FALSE,0,0x000003ff,TRUE),
243   HOWTO(R_SPARC_11,        0,2,11,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_11",      FALSE,0,0x000007ff,TRUE),
244   HOWTO(R_SPARC_64,        0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_64",      FALSE,0,MINUS_ONE, TRUE),
245   HOWTO(R_SPARC_OLO10,     0,2,13,FALSE,0,complain_overflow_signed,  sparc_elf_notsup_reloc, "R_SPARC_OLO10",   FALSE,0,0x00001fff,TRUE),
246   HOWTO(R_SPARC_HH22,     42,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_HH22",    FALSE,0,0x003fffff,TRUE),
247   HOWTO(R_SPARC_HM10,     32,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HM10",    FALSE,0,0x000003ff,TRUE),
248   HOWTO(R_SPARC_LM22,     10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LM22",    FALSE,0,0x003fffff,TRUE),
249   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),
250   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),
251   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),
252   HOWTO(R_SPARC_WDISP16,   2,2,16,TRUE, 0,complain_overflow_signed,  sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", FALSE,0,0x00000000,TRUE),
253   HOWTO(R_SPARC_WDISP19,   2,2,19,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP19", FALSE,0,0x0007ffff,TRUE),
254   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),
255   HOWTO(R_SPARC_7,         0,2, 7,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_7",       FALSE,0,0x0000007f,TRUE),
256   HOWTO(R_SPARC_5,         0,2, 5,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_5",       FALSE,0,0x0000001f,TRUE),
257   HOWTO(R_SPARC_6,         0,2, 6,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_6",       FALSE,0,0x0000003f,TRUE),
258   HOWTO(R_SPARC_DISP64,    0,4,64,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP64",  FALSE,0,MINUS_ONE, TRUE),
259   HOWTO(R_SPARC_PLT64,     0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PLT64",   FALSE,0,MINUS_ONE, TRUE),
260   HOWTO(R_SPARC_HIX22,     0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,  "R_SPARC_HIX22",   FALSE,0,MINUS_ONE, FALSE),
261   HOWTO(R_SPARC_LOX10,     0,4, 0,FALSE,0,complain_overflow_dont,    sparc_elf_lox10_reloc,  "R_SPARC_LOX10",   FALSE,0,MINUS_ONE, FALSE),
262   HOWTO(R_SPARC_H44,      22,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_H44",     FALSE,0,0x003fffff,FALSE),
263   HOWTO(R_SPARC_M44,      12,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_M44",     FALSE,0,0x000003ff,FALSE),
264   HOWTO(R_SPARC_L44,       0,2,13,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_L44",     FALSE,0,0x00000fff,FALSE),
265   HOWTO(R_SPARC_REGISTER,  0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_REGISTER",FALSE,0,MINUS_ONE, FALSE),
266   HOWTO(R_SPARC_UA64,        0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA64",      FALSE,0,MINUS_ONE, TRUE),
267   HOWTO(R_SPARC_UA16,        0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA16",      FALSE,0,0x0000ffff,TRUE),
268   HOWTO(R_SPARC_TLS_GD_HI22,10,2,22,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_HI22",FALSE,0,0x003fffff,TRUE),
269   HOWTO(R_SPARC_TLS_GD_LO10,0,2,10,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_LO10",FALSE,0,0x000003ff,TRUE),
270   HOWTO(R_SPARC_TLS_GD_ADD,0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_ADD",FALSE,0,0x00000000,TRUE),
271   HOWTO(R_SPARC_TLS_GD_CALL,2,2,30,TRUE,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_CALL",FALSE,0,0x3fffffff,TRUE),
272   HOWTO(R_SPARC_TLS_LDM_HI22,10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_HI22",FALSE,0,0x003fffff,TRUE),
273   HOWTO(R_SPARC_TLS_LDM_LO10,0,2,10,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_LO10",FALSE,0,0x000003ff,TRUE),
274   HOWTO(R_SPARC_TLS_LDM_ADD,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_ADD",FALSE,0,0x00000000,TRUE),
275   HOWTO(R_SPARC_TLS_LDM_CALL,2,2,30,TRUE,0,complain_overflow_signed, bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_CALL",FALSE,0,0x3fffffff,TRUE),
276   HOWTO(R_SPARC_TLS_LDO_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_TLS_LDO_HIX22",FALSE,0,0x003fffff, FALSE),
277   HOWTO(R_SPARC_TLS_LDO_LOX10,0,2,0,FALSE,0,complain_overflow_dont,  sparc_elf_lox10_reloc,  "R_SPARC_TLS_LDO_LOX10",FALSE,0,0x000003ff, FALSE),
278   HOWTO(R_SPARC_TLS_LDO_ADD,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_LDO_ADD",FALSE,0,0x00000000,TRUE),
279   HOWTO(R_SPARC_TLS_IE_HI22,10,2,22,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_HI22",FALSE,0,0x003fffff,TRUE),
280   HOWTO(R_SPARC_TLS_IE_LO10,0,2,10,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_LO10",FALSE,0,0x000003ff,TRUE),
281   HOWTO(R_SPARC_TLS_IE_LD,0,0, 0,FALSE,0,complain_overflow_dont,     bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_LD",FALSE,0,0x00000000,TRUE),
282   HOWTO(R_SPARC_TLS_IE_LDX,0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_LDX",FALSE,0,0x00000000,TRUE),
283   HOWTO(R_SPARC_TLS_IE_ADD,0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_ADD",FALSE,0,0x00000000,TRUE),
284   HOWTO(R_SPARC_TLS_LE_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_TLS_LE_HIX22",FALSE,0,0x003fffff, FALSE),
285   HOWTO(R_SPARC_TLS_LE_LOX10,0,2,0,FALSE,0,complain_overflow_dont,   sparc_elf_lox10_reloc,  "R_SPARC_TLS_LE_LOX10",FALSE,0,0x000003ff, FALSE),
286   HOWTO(R_SPARC_TLS_DTPMOD32,0,0, 0,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_DTPMOD32",FALSE,0,0x00000000,TRUE),
287   HOWTO(R_SPARC_TLS_DTPMOD64,0,0, 0,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_DTPMOD64",FALSE,0,0x00000000,TRUE),
288   HOWTO(R_SPARC_TLS_DTPOFF32,0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF32",FALSE,0,0xffffffff,TRUE),
289   HOWTO(R_SPARC_TLS_DTPOFF64,0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF64",FALSE,0,MINUS_ONE,TRUE),
290   HOWTO(R_SPARC_TLS_TPOFF32,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_TPOFF32",FALSE,0,0x00000000,TRUE),
291   HOWTO(R_SPARC_TLS_TPOFF64,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_TPOFF64",FALSE,0,0x00000000,TRUE),
292   HOWTO(R_SPARC_GOTDATA_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_GOTDATA_HIX22",FALSE,0,0x003fffff, FALSE),
293   HOWTO(R_SPARC_GOTDATA_LOX10,0,2,0,FALSE,0,complain_overflow_dont,  sparc_elf_lox10_reloc,  "R_SPARC_GOTDATA_LOX10",FALSE,0,0x000003ff, FALSE),
294   HOWTO(R_SPARC_GOTDATA_OP_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_GOTDATA_OP_HIX22",FALSE,0,0x003fffff, FALSE),
295   HOWTO(R_SPARC_GOTDATA_OP_LOX10,0,2,0,FALSE,0,complain_overflow_dont,  sparc_elf_lox10_reloc,  "R_SPARC_GOTDATA_OP_LOX10",FALSE,0,0x000003ff, FALSE),
296   HOWTO(R_SPARC_GOTDATA_OP,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_GOTDATA_OP",FALSE,0,0x00000000,TRUE),
297   HOWTO(R_SPARC_H34,12,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc,"R_SPARC_H34",FALSE,0,0x003fffff,FALSE),
298   HOWTO(R_SPARC_SIZE32,0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_SIZE32",FALSE,0,0xffffffff,TRUE),
299   HOWTO(R_SPARC_SIZE64,0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_SIZE64",FALSE,0,MINUS_ONE, TRUE),
300   HOWTO(R_SPARC_WDISP10,2,2,10,TRUE, 0,complain_overflow_signed,sparc_elf_wdisp10_reloc,"R_SPARC_WDISP10",FALSE,0,0x00000000,TRUE),
301 };
302 static reloc_howto_type sparc_jmp_irel_howto =
303   HOWTO(R_SPARC_JMP_IREL,  0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_JMP_IREL",FALSE,0,0x00000000,TRUE);
304 static reloc_howto_type sparc_irelative_howto =
305   HOWTO(R_SPARC_IRELATIVE,  0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_IRELATIVE",FALSE,0,0x00000000,TRUE);
306 static reloc_howto_type sparc_vtinherit_howto =
307   HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", FALSE,0, 0, FALSE);
308 static reloc_howto_type sparc_vtentry_howto =
309   HOWTO (R_SPARC_GNU_VTENTRY, 0,2,0,FALSE,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_SPARC_GNU_VTENTRY", FALSE,0,0, FALSE);
310 static reloc_howto_type sparc_rev32_howto =
311   HOWTO(R_SPARC_REV32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", FALSE,0,0xffffffff,TRUE);
312
313 reloc_howto_type *
314 _bfd_sparc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
315                                   bfd_reloc_code_real_type code)
316 {
317   /* We explicitly handle each relocation type in the switch
318      instead of using a lookup table for efficiency.  */
319   switch (code)
320     {
321     case BFD_RELOC_NONE:
322       return &_bfd_sparc_elf_howto_table[R_SPARC_NONE];
323
324     case BFD_RELOC_8:
325       return &_bfd_sparc_elf_howto_table[R_SPARC_8];
326
327     case BFD_RELOC_16:
328       return &_bfd_sparc_elf_howto_table[R_SPARC_16];
329
330     case BFD_RELOC_32:
331       return &_bfd_sparc_elf_howto_table[R_SPARC_32];
332
333     case BFD_RELOC_8_PCREL:
334       return &_bfd_sparc_elf_howto_table[R_SPARC_DISP8];
335
336     case BFD_RELOC_16_PCREL:
337       return &_bfd_sparc_elf_howto_table[R_SPARC_DISP16];
338
339     case BFD_RELOC_32_PCREL:
340       return &_bfd_sparc_elf_howto_table[R_SPARC_DISP32];
341
342     case BFD_RELOC_32_PCREL_S2:
343       return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP30];
344
345     case BFD_RELOC_SPARC_WDISP22:
346       return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP22];
347
348     case BFD_RELOC_HI22:
349       return &_bfd_sparc_elf_howto_table[R_SPARC_HI22];
350
351     case BFD_RELOC_SPARC22:
352       return &_bfd_sparc_elf_howto_table[R_SPARC_22];
353
354     case BFD_RELOC_SPARC13:
355       return &_bfd_sparc_elf_howto_table[R_SPARC_13];
356
357     case BFD_RELOC_LO10:
358       return &_bfd_sparc_elf_howto_table[R_SPARC_LO10];
359
360     case BFD_RELOC_SPARC_GOT10:
361       return &_bfd_sparc_elf_howto_table[R_SPARC_GOT10];
362
363     case BFD_RELOC_SPARC_GOT13:
364       return &_bfd_sparc_elf_howto_table[R_SPARC_GOT13];
365
366     case BFD_RELOC_SPARC_GOT22:
367       return &_bfd_sparc_elf_howto_table[R_SPARC_GOT22];
368
369     case BFD_RELOC_SPARC_PC10:
370       return &_bfd_sparc_elf_howto_table[R_SPARC_PC10];
371
372     case BFD_RELOC_SPARC_PC22:
373       return &_bfd_sparc_elf_howto_table[R_SPARC_PC22];
374
375     case BFD_RELOC_SPARC_WPLT30:
376       return &_bfd_sparc_elf_howto_table[R_SPARC_WPLT30];
377
378     case BFD_RELOC_SPARC_COPY:
379       return &_bfd_sparc_elf_howto_table[R_SPARC_COPY];
380
381     case BFD_RELOC_SPARC_GLOB_DAT:
382       return &_bfd_sparc_elf_howto_table[R_SPARC_GLOB_DAT];
383
384     case BFD_RELOC_SPARC_JMP_SLOT:
385       return &_bfd_sparc_elf_howto_table[R_SPARC_JMP_SLOT];
386
387     case BFD_RELOC_SPARC_RELATIVE:
388       return &_bfd_sparc_elf_howto_table[R_SPARC_RELATIVE];
389
390     case BFD_RELOC_SPARC_UA32:
391       return &_bfd_sparc_elf_howto_table[R_SPARC_UA32];
392
393     case BFD_RELOC_SPARC_PLT32:
394       return &_bfd_sparc_elf_howto_table[R_SPARC_PLT32];
395
396     case BFD_RELOC_SPARC_10:
397       return &_bfd_sparc_elf_howto_table[R_SPARC_10];
398
399     case BFD_RELOC_SPARC_11:
400       return &_bfd_sparc_elf_howto_table[R_SPARC_11];
401
402     case BFD_RELOC_SPARC_64:
403       return &_bfd_sparc_elf_howto_table[R_SPARC_64];
404
405     case BFD_RELOC_SPARC_OLO10:
406       return &_bfd_sparc_elf_howto_table[R_SPARC_OLO10];
407
408     case BFD_RELOC_SPARC_HH22:
409       return &_bfd_sparc_elf_howto_table[R_SPARC_HH22];
410
411     case BFD_RELOC_SPARC_HM10:
412       return &_bfd_sparc_elf_howto_table[R_SPARC_HM10];
413
414     case BFD_RELOC_SPARC_LM22:
415       return &_bfd_sparc_elf_howto_table[R_SPARC_LM22];
416
417     case BFD_RELOC_SPARC_PC_HH22:
418       return &_bfd_sparc_elf_howto_table[R_SPARC_PC_HH22];
419
420     case BFD_RELOC_SPARC_PC_HM10:
421       return &_bfd_sparc_elf_howto_table[R_SPARC_PC_HM10];
422
423     case BFD_RELOC_SPARC_PC_LM22:
424       return &_bfd_sparc_elf_howto_table[R_SPARC_PC_LM22];
425
426     case BFD_RELOC_SPARC_WDISP16:
427       return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP16];
428
429     case BFD_RELOC_SPARC_WDISP19:
430       return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP19];
431
432     case BFD_RELOC_SPARC_7:
433       return &_bfd_sparc_elf_howto_table[R_SPARC_7];
434
435     case BFD_RELOC_SPARC_5:
436       return &_bfd_sparc_elf_howto_table[R_SPARC_5];
437
438     case BFD_RELOC_SPARC_6:
439       return &_bfd_sparc_elf_howto_table[R_SPARC_6];
440
441     case BFD_RELOC_SPARC_DISP64:
442       return &_bfd_sparc_elf_howto_table[R_SPARC_DISP64];
443
444     case BFD_RELOC_SPARC_PLT64:
445       return &_bfd_sparc_elf_howto_table[R_SPARC_PLT64];
446
447     case BFD_RELOC_SPARC_HIX22:
448       return &_bfd_sparc_elf_howto_table[R_SPARC_HIX22];
449
450     case BFD_RELOC_SPARC_LOX10:
451       return &_bfd_sparc_elf_howto_table[R_SPARC_LOX10];
452
453     case BFD_RELOC_SPARC_H44:
454       return &_bfd_sparc_elf_howto_table[R_SPARC_H44];
455
456     case BFD_RELOC_SPARC_M44:
457       return &_bfd_sparc_elf_howto_table[R_SPARC_M44];
458
459     case BFD_RELOC_SPARC_L44:
460       return &_bfd_sparc_elf_howto_table[R_SPARC_L44];
461
462     case BFD_RELOC_SPARC_REGISTER:
463       return &_bfd_sparc_elf_howto_table[R_SPARC_REGISTER];
464
465     case BFD_RELOC_SPARC_UA64:
466       return &_bfd_sparc_elf_howto_table[R_SPARC_UA64];
467
468     case BFD_RELOC_SPARC_UA16:
469       return &_bfd_sparc_elf_howto_table[R_SPARC_UA16];
470
471     case BFD_RELOC_SPARC_TLS_GD_HI22:
472       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_GD_HI22];
473
474     case BFD_RELOC_SPARC_TLS_GD_LO10:
475       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_GD_LO10];
476
477     case BFD_RELOC_SPARC_TLS_GD_ADD:
478       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_GD_ADD];
479
480     case BFD_RELOC_SPARC_TLS_GD_CALL:
481       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_GD_CALL];
482
483     case BFD_RELOC_SPARC_TLS_LDM_HI22:
484       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDM_HI22];
485
486     case BFD_RELOC_SPARC_TLS_LDM_LO10:
487       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDM_LO10];
488
489     case BFD_RELOC_SPARC_TLS_LDM_ADD:
490       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDM_ADD];
491
492     case BFD_RELOC_SPARC_TLS_LDM_CALL:
493       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDM_CALL];
494
495     case BFD_RELOC_SPARC_TLS_LDO_HIX22:
496       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDO_HIX22];
497
498     case BFD_RELOC_SPARC_TLS_LDO_LOX10:
499       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDO_LOX10];
500
501     case BFD_RELOC_SPARC_TLS_LDO_ADD:
502       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LDO_ADD];
503
504     case BFD_RELOC_SPARC_TLS_IE_HI22:
505       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_HI22];
506
507     case BFD_RELOC_SPARC_TLS_IE_LO10:
508       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_LO10];
509
510     case BFD_RELOC_SPARC_TLS_IE_LD:
511       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_LD];
512
513     case BFD_RELOC_SPARC_TLS_IE_LDX:
514       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_LDX];
515
516     case BFD_RELOC_SPARC_TLS_IE_ADD:
517       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_IE_ADD];
518
519     case BFD_RELOC_SPARC_TLS_LE_HIX22:
520       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LE_HIX22];
521
522     case BFD_RELOC_SPARC_TLS_LE_LOX10:
523       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_LE_LOX10];
524
525     case BFD_RELOC_SPARC_TLS_DTPMOD32:
526       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_DTPMOD32];
527
528     case BFD_RELOC_SPARC_TLS_DTPMOD64:
529       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_DTPMOD64];
530
531     case BFD_RELOC_SPARC_TLS_DTPOFF32:
532       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_DTPOFF32];
533
534     case BFD_RELOC_SPARC_TLS_DTPOFF64:
535       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_DTPOFF64];
536
537     case BFD_RELOC_SPARC_TLS_TPOFF32:
538       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_TPOFF32];
539
540     case BFD_RELOC_SPARC_TLS_TPOFF64:
541       return &_bfd_sparc_elf_howto_table[R_SPARC_TLS_TPOFF64];
542
543     case BFD_RELOC_SPARC_GOTDATA_HIX22:
544       return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_HIX22];
545
546     case BFD_RELOC_SPARC_GOTDATA_LOX10:
547       return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_LOX10];
548
549     case BFD_RELOC_SPARC_GOTDATA_OP_HIX22:
550       return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_OP_HIX22];
551
552     case BFD_RELOC_SPARC_GOTDATA_OP_LOX10:
553       return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_OP_LOX10];
554
555     case BFD_RELOC_SPARC_GOTDATA_OP:
556       return &_bfd_sparc_elf_howto_table[R_SPARC_GOTDATA_OP];
557
558     case BFD_RELOC_SPARC_H34:
559       return &_bfd_sparc_elf_howto_table[R_SPARC_H34];
560
561     case BFD_RELOC_SPARC_SIZE32:
562       return &_bfd_sparc_elf_howto_table[R_SPARC_SIZE32];
563
564     case BFD_RELOC_SPARC_SIZE64:
565       return &_bfd_sparc_elf_howto_table[R_SPARC_SIZE64];
566
567     case BFD_RELOC_SPARC_WDISP10:
568       return &_bfd_sparc_elf_howto_table[R_SPARC_WDISP10];
569
570     case BFD_RELOC_SPARC_JMP_IREL:
571       return &sparc_jmp_irel_howto;
572
573     case BFD_RELOC_SPARC_IRELATIVE:
574       return &sparc_irelative_howto;
575
576     case BFD_RELOC_VTABLE_INHERIT:
577       return &sparc_vtinherit_howto;
578
579     case BFD_RELOC_VTABLE_ENTRY:
580       return &sparc_vtentry_howto;
581
582     case BFD_RELOC_SPARC_REV32:
583       return &sparc_rev32_howto;
584
585     default:
586       break;
587     }
588     bfd_set_error (bfd_error_bad_value);
589     return NULL;
590 }
591
592 reloc_howto_type *
593 _bfd_sparc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
594                                   const char *r_name)
595 {
596   unsigned int i;
597
598   for (i = 0;
599        i < (sizeof (_bfd_sparc_elf_howto_table)
600             / sizeof (_bfd_sparc_elf_howto_table[0]));
601        i++)
602     if (_bfd_sparc_elf_howto_table[i].name != NULL
603         && strcasecmp (_bfd_sparc_elf_howto_table[i].name, r_name) == 0)
604       return &_bfd_sparc_elf_howto_table[i];
605
606   if (strcasecmp (sparc_vtinherit_howto.name, r_name) == 0)
607     return &sparc_vtinherit_howto;
608   if (strcasecmp (sparc_vtentry_howto.name, r_name) == 0)
609     return &sparc_vtentry_howto;
610   if (strcasecmp (sparc_rev32_howto.name, r_name) == 0)
611     return &sparc_rev32_howto;
612
613   return NULL;
614 }
615
616 reloc_howto_type *
617 _bfd_sparc_elf_info_to_howto_ptr (bfd *abfd, unsigned int r_type)
618 {
619   switch (r_type)
620     {
621     case R_SPARC_JMP_IREL:
622       return &sparc_jmp_irel_howto;
623
624     case R_SPARC_IRELATIVE:
625       return &sparc_irelative_howto;
626
627     case R_SPARC_GNU_VTINHERIT:
628       return &sparc_vtinherit_howto;
629
630     case R_SPARC_GNU_VTENTRY:
631       return &sparc_vtentry_howto;
632
633     case R_SPARC_REV32:
634       return &sparc_rev32_howto;
635
636     default:
637       if (r_type >= (unsigned int) R_SPARC_max_std)
638         {
639           _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
640                               abfd, r_type);
641           r_type = R_SPARC_NONE;
642         }
643       return &_bfd_sparc_elf_howto_table[r_type];
644     }
645 }
646
647 /* Both 32-bit and 64-bit sparc encode this in an identical manner,
648    so just take advantage of that.  */
649 #define SPARC_ELF_R_TYPE(r_info)        \
650         ((r_info) & 0xff)
651
652 void
653 _bfd_sparc_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
654                               Elf_Internal_Rela *dst)
655 {
656   unsigned int r_type = SPARC_ELF_R_TYPE (dst->r_info);
657
658   cache_ptr->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, r_type);
659 }
660 \f
661
662 /* The nop opcode we use.  */
663 #define SPARC_NOP 0x01000000
664
665 #define SPARC_INSN_BYTES        4
666
667 /* Is an undefined weak symbol resolved to 0 ?
668    Reference to an undefined weak symbol is resolved to 0 when
669    building an executable if it isn't dynamic and
670    1. Has non-GOT/non-PLT relocations in text section.
671    Or
672    2. Has no GOT/PLT relocation.  */
673 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, EH)               \
674   ((EH)->elf.root.type == bfd_link_hash_undefweak               \
675    && bfd_link_executable (INFO)                                \
676    && (_bfd_sparc_elf_hash_table (INFO)->interp == NULL         \
677        || !(INFO)->dynamic_undefined_weak                       \
678        || (EH)->has_non_got_reloc                               \
679        || !(EH)->has_got_reloc))
680
681 /* SPARC ELF linker hash entry.  */
682
683 struct _bfd_sparc_elf_link_hash_entry
684 {
685   struct elf_link_hash_entry elf;
686
687   /* Track dynamic relocs copied for this symbol.  */
688   struct elf_dyn_relocs *dyn_relocs;
689
690 #define GOT_UNKNOWN     0
691 #define GOT_NORMAL      1
692 #define GOT_TLS_GD      2
693 #define GOT_TLS_IE      3
694   unsigned char tls_type;
695
696     /* Symbol has GOT or PLT relocations.  */
697   unsigned int has_got_reloc : 1;
698
699   /* Symbol has non-GOT/non-PLT relocations in text sections.  */
700   unsigned int has_non_got_reloc : 1;
701
702 };
703
704 #define _bfd_sparc_elf_hash_entry(ent) ((struct _bfd_sparc_elf_link_hash_entry *)(ent))
705
706 struct _bfd_sparc_elf_obj_tdata
707 {
708   struct elf_obj_tdata root;
709
710   /* tls_type for each local got entry.  */
711   char *local_got_tls_type;
712
713   /* TRUE if TLS GD relocs has been seen for this object.  */
714   bfd_boolean has_tlsgd;
715 };
716
717 #define _bfd_sparc_elf_tdata(abfd) \
718   ((struct _bfd_sparc_elf_obj_tdata *) (abfd)->tdata.any)
719
720 #define _bfd_sparc_elf_local_got_tls_type(abfd) \
721   (_bfd_sparc_elf_tdata (abfd)->local_got_tls_type)
722
723 #define is_sparc_elf(bfd)                               \
724   (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
725    && elf_tdata (bfd) != NULL                           \
726    && elf_object_id (bfd) == SPARC_ELF_DATA)
727
728 bfd_boolean
729 _bfd_sparc_elf_mkobject (bfd *abfd)
730 {
731   return bfd_elf_allocate_object (abfd, sizeof (struct _bfd_sparc_elf_obj_tdata),
732                                   SPARC_ELF_DATA);
733 }
734
735 static void
736 sparc_put_word_32 (bfd *abfd, bfd_vma val, void *ptr)
737 {
738   bfd_put_32 (abfd, val, ptr);
739 }
740
741 static void
742 sparc_put_word_64 (bfd *abfd, bfd_vma val, void *ptr)
743 {
744   bfd_put_64 (abfd, val, ptr);
745 }
746
747 static void
748 sparc_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
749 {
750   const struct elf_backend_data *bed;
751   bfd_byte *loc;
752
753   bed = get_elf_backend_data (abfd);
754   BFD_ASSERT (s->reloc_count * bed->s->sizeof_rela < s->size);
755   loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
756   bed->s->swap_reloca_out (abfd, rel, loc);
757 }
758
759 static bfd_vma
760 sparc_elf_r_info_64 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
761                      bfd_vma rel_index ATTRIBUTE_UNUSED,
762                      bfd_vma type ATTRIBUTE_UNUSED)
763 {
764   return ELF64_R_INFO (rel_index,
765                        (in_rel ?
766                         ELF64_R_TYPE_INFO (ELF64_R_TYPE_DATA (in_rel->r_info),
767                                            type) : type));
768 }
769
770 static bfd_vma
771 sparc_elf_r_info_32 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
772                      bfd_vma rel_index, bfd_vma type)
773 {
774   return ELF32_R_INFO (rel_index, type);
775 }
776
777 static bfd_vma
778 sparc_elf_r_symndx_64 (bfd_vma r_info)
779 {
780   bfd_vma r_symndx = ELF32_R_SYM (r_info);
781   return (r_symndx >> 24);
782 }
783
784 static bfd_vma
785 sparc_elf_r_symndx_32 (bfd_vma r_info)
786 {
787   return ELF32_R_SYM (r_info);
788 }
789
790 /* PLT/GOT stuff */
791
792 #define PLT32_ENTRY_SIZE 12
793 #define PLT32_HEADER_SIZE       (4 * PLT32_ENTRY_SIZE)
794
795 /* The first four entries in a 32-bit procedure linkage table are reserved,
796    and the initial contents are unimportant (we zero them out).
797    Subsequent entries look like this.  See the SVR4 ABI SPARC
798    supplement to see how this works.  */
799
800 /* sethi %hi(.-.plt0),%g1.  We fill in the address later.  */
801 #define PLT32_ENTRY_WORD0 0x03000000
802 /* b,a .plt0.  We fill in the offset later.  */
803 #define PLT32_ENTRY_WORD1 0x30800000
804 /* nop.  */
805 #define PLT32_ENTRY_WORD2 SPARC_NOP
806
807 static int
808 sparc32_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
809                          bfd_vma max ATTRIBUTE_UNUSED,
810                          bfd_vma *r_offset)
811 {
812       bfd_put_32 (output_bfd,
813                   PLT32_ENTRY_WORD0 + offset,
814                   splt->contents + offset);
815       bfd_put_32 (output_bfd,
816                   (PLT32_ENTRY_WORD1
817                    + (((- (offset + 4)) >> 2) & 0x3fffff)),
818                   splt->contents + offset + 4);
819       bfd_put_32 (output_bfd, (bfd_vma) PLT32_ENTRY_WORD2,
820                   splt->contents + offset + 8);
821
822       *r_offset = offset;
823
824       return offset / PLT32_ENTRY_SIZE - 4;
825 }
826
827 /* Both the headers and the entries are icache aligned.  */
828 #define PLT64_ENTRY_SIZE        32
829 #define PLT64_HEADER_SIZE       (4 * PLT64_ENTRY_SIZE)
830 #define PLT64_LARGE_THRESHOLD   32768
831
832 static int
833 sparc64_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
834                          bfd_vma max, bfd_vma *r_offset)
835 {
836   unsigned char *entry = splt->contents + offset;
837   const unsigned int nop = SPARC_NOP;
838   int plt_index;
839
840   if (offset < (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
841     {
842       unsigned int sethi, ba;
843
844       *r_offset = offset;
845
846       plt_index = (offset / PLT64_ENTRY_SIZE);
847
848       sethi = 0x03000000 | (plt_index * PLT64_ENTRY_SIZE);
849       ba = 0x30680000
850         | (((splt->contents + PLT64_ENTRY_SIZE) - (entry + 4)) / 4 & 0x7ffff);
851
852       bfd_put_32 (output_bfd, (bfd_vma) sethi, entry);
853       bfd_put_32 (output_bfd, (bfd_vma) ba,    entry + 4);
854       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 8);
855       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 12);
856       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 16);
857       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 20);
858       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 24);
859       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 28);
860     }
861   else
862     {
863       unsigned char *ptr;
864       unsigned int ldx;
865       int block, last_block, ofs, last_ofs, chunks_this_block;
866       const int insn_chunk_size = (6 * 4);
867       const int ptr_chunk_size = (1 * 8);
868       const int entries_per_block = 160;
869       const int block_size = entries_per_block * (insn_chunk_size
870                                                   + ptr_chunk_size);
871
872       /* Entries 32768 and higher are grouped into blocks of 160.
873          The blocks are further subdivided into 160 sequences of
874          6 instructions and 160 pointers.  If a block does not require
875          the full 160 entries, let's say it requires N, then there
876          will be N sequences of 6 instructions and N pointers.  */
877
878       offset -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
879       max -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
880
881       block = offset / block_size;
882       last_block = max / block_size;
883       if (block != last_block)
884         {
885           chunks_this_block = 160;
886         }
887       else
888         {
889           last_ofs = max % block_size;
890           chunks_this_block = last_ofs / (insn_chunk_size + ptr_chunk_size);
891         }
892
893       ofs = offset % block_size;
894
895       plt_index = (PLT64_LARGE_THRESHOLD +
896                (block * 160) +
897                (ofs / insn_chunk_size));
898
899       ptr = splt->contents
900         + (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
901         + (block * block_size)
902         + (chunks_this_block * insn_chunk_size)
903         + (ofs / insn_chunk_size) * ptr_chunk_size;
904
905       *r_offset = (bfd_vma) (ptr - splt->contents);
906
907       ldx = 0xc25be000 | ((ptr - (entry+4)) & 0x1fff);
908
909       /* mov %o7,%g5
910          call .+8
911          nop
912          ldx [%o7+P],%g1
913          jmpl %o7+%g1,%g1
914          mov %g5,%o7  */
915       bfd_put_32 (output_bfd, (bfd_vma) 0x8a10000f, entry);
916       bfd_put_32 (output_bfd, (bfd_vma) 0x40000002, entry + 4);
917       bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,  entry + 8);
918       bfd_put_32 (output_bfd, (bfd_vma) ldx,        entry + 12);
919       bfd_put_32 (output_bfd, (bfd_vma) 0x83c3c001, entry + 16);
920       bfd_put_32 (output_bfd, (bfd_vma) 0x9e100005, entry + 20);
921
922       bfd_put_64 (output_bfd, (bfd_vma) (splt->contents - (entry + 4)), ptr);
923     }
924
925   return plt_index - 4;
926 }
927
928 /* The format of the first PLT entry in a VxWorks executable.  */
929 static const bfd_vma sparc_vxworks_exec_plt0_entry[] =
930   {
931     0x05000000, /* sethi  %hi(_GLOBAL_OFFSET_TABLE_+8), %g2 */
932     0x8410a000, /* or     %g2, %lo(_GLOBAL_OFFSET_TABLE_+8), %g2 */
933     0xc4008000, /* ld     [ %g2 ], %g2 */
934     0x81c08000, /* jmp    %g2 */
935     0x01000000  /* nop */
936   };
937
938 /* The format of subsequent PLT entries.  */
939 static const bfd_vma sparc_vxworks_exec_plt_entry[] =
940   {
941     0x03000000, /* sethi  %hi(_GLOBAL_OFFSET_TABLE_+f@got), %g1 */
942     0x82106000, /* or     %g1, %lo(_GLOBAL_OFFSET_TABLE_+f@got), %g1 */
943     0xc2004000, /* ld     [ %g1 ], %g1 */
944     0x81c04000, /* jmp    %g1 */
945     0x01000000, /* nop */
946     0x03000000, /* sethi  %hi(f@pltindex), %g1 */
947     0x10800000, /* b      _PLT_resolve */
948     0x82106000  /* or     %g1, %lo(f@pltindex), %g1 */
949   };
950
951 /* The format of the first PLT entry in a VxWorks shared object.  */
952 static const bfd_vma sparc_vxworks_shared_plt0_entry[] =
953   {
954     0xc405e008, /* ld     [ %l7 + 8 ], %g2 */
955     0x81c08000, /* jmp    %g2 */
956     0x01000000  /* nop */
957   };
958
959 /* The format of subsequent PLT entries.  */
960 static const bfd_vma sparc_vxworks_shared_plt_entry[] =
961   {
962     0x03000000, /* sethi  %hi(f@got), %g1 */
963     0x82106000, /* or     %g1, %lo(f@got), %g1 */
964     0xc205c001, /* ld     [ %l7 + %g1 ], %g1 */
965     0x81c04000, /* jmp    %g1 */
966     0x01000000, /* nop */
967     0x03000000, /* sethi  %hi(f@pltindex), %g1 */
968     0x10800000, /* b      _PLT_resolve */
969     0x82106000  /* or     %g1, %lo(f@pltindex), %g1 */
970   };
971
972 #define SPARC_ELF_PUT_WORD(htab, bfd, val, ptr) \
973         htab->put_word(bfd, val, ptr)
974
975 #define SPARC_ELF_R_INFO(htab, in_rel, index, type)     \
976         htab->r_info(in_rel, index, type)
977
978 #define SPARC_ELF_R_SYMNDX(htab, r_info)        \
979         htab->r_symndx(r_info)
980
981 #define SPARC_ELF_WORD_BYTES(htab)      \
982         htab->bytes_per_word
983
984 #define SPARC_ELF_RELA_BYTES(htab)      \
985         htab->bytes_per_rela
986
987 #define SPARC_ELF_DTPOFF_RELOC(htab)    \
988         htab->dtpoff_reloc
989
990 #define SPARC_ELF_DTPMOD_RELOC(htab)    \
991         htab->dtpmod_reloc
992
993 #define SPARC_ELF_TPOFF_RELOC(htab)     \
994         htab->tpoff_reloc
995
996 #define SPARC_ELF_BUILD_PLT_ENTRY(htab, obfd, splt, off, max, r_off) \
997         htab->build_plt_entry (obfd, splt, off, max, r_off)
998
999 /* Create an entry in an SPARC ELF linker hash table.  */
1000
1001 static struct bfd_hash_entry *
1002 link_hash_newfunc (struct bfd_hash_entry *entry,
1003                    struct bfd_hash_table *table, const char *string)
1004 {
1005   /* Allocate the structure if it has not already been allocated by a
1006      subclass.  */
1007   if (entry == NULL)
1008     {
1009       entry = bfd_hash_allocate (table,
1010                                  sizeof (struct _bfd_sparc_elf_link_hash_entry));
1011       if (entry == NULL)
1012         return entry;
1013     }
1014
1015   /* Call the allocation method of the superclass.  */
1016   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
1017   if (entry != NULL)
1018     {
1019       struct _bfd_sparc_elf_link_hash_entry *eh;
1020
1021       eh = (struct _bfd_sparc_elf_link_hash_entry *) entry;
1022       eh->dyn_relocs = NULL;
1023       eh->tls_type = GOT_UNKNOWN;
1024       eh->has_got_reloc = 0;
1025       eh->has_non_got_reloc = 0;
1026     }
1027
1028   return entry;
1029 }
1030
1031 /* The name of the dynamic interpreter.  This is put in the .interp
1032    section.  */
1033
1034 #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
1035 #define ELF64_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1"
1036
1037 /* Compute a hash of a local hash entry.  We use elf_link_hash_entry
1038    for local symbol so that we can handle local STT_GNU_IFUNC symbols
1039    as global symbol.  We reuse indx and dynstr_index for local symbol
1040    hash since they aren't used by global symbols in this backend.  */
1041
1042 static hashval_t
1043 elf_sparc_local_htab_hash (const void *ptr)
1044 {
1045   struct elf_link_hash_entry *h
1046     = (struct elf_link_hash_entry *) ptr;
1047   return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
1048 }
1049
1050 /* Compare local hash entries.  */
1051
1052 static int
1053 elf_sparc_local_htab_eq (const void *ptr1, const void *ptr2)
1054 {
1055   struct elf_link_hash_entry *h1
1056      = (struct elf_link_hash_entry *) ptr1;
1057   struct elf_link_hash_entry *h2
1058     = (struct elf_link_hash_entry *) ptr2;
1059
1060   return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
1061 }
1062
1063 /* Find and/or create a hash entry for local symbol.  */
1064
1065 static struct elf_link_hash_entry *
1066 elf_sparc_get_local_sym_hash (struct _bfd_sparc_elf_link_hash_table *htab,
1067                               bfd *abfd, const Elf_Internal_Rela *rel,
1068                               bfd_boolean create)
1069 {
1070   struct _bfd_sparc_elf_link_hash_entry e, *ret;
1071   asection *sec = abfd->sections;
1072   unsigned long r_symndx;
1073   hashval_t h;
1074   void **slot;
1075
1076   r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
1077   h = ELF_LOCAL_SYMBOL_HASH (sec->id, r_symndx);
1078
1079   e.elf.indx = sec->id;
1080   e.elf.dynstr_index = r_symndx;
1081   slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
1082                                    create ? INSERT : NO_INSERT);
1083
1084   if (!slot)
1085     return NULL;
1086
1087   if (*slot)
1088     {
1089       ret = (struct _bfd_sparc_elf_link_hash_entry *) *slot;
1090       return &ret->elf;
1091     }
1092
1093   ret = (struct _bfd_sparc_elf_link_hash_entry *)
1094         objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
1095                         sizeof (struct _bfd_sparc_elf_link_hash_entry));
1096   if (ret)
1097     {
1098       memset (ret, 0, sizeof (*ret));
1099       ret->elf.indx = sec->id;
1100       ret->elf.dynstr_index = r_symndx;
1101       ret->elf.dynindx = -1;
1102       ret->elf.plt.offset = (bfd_vma) -1;
1103       ret->elf.got.offset = (bfd_vma) -1;
1104       *slot = ret;
1105     }
1106   return &ret->elf;
1107 }
1108
1109 /* Destroy a SPARC ELF linker hash table.  */
1110
1111 static void
1112 _bfd_sparc_elf_link_hash_table_free (bfd *obfd)
1113 {
1114   struct _bfd_sparc_elf_link_hash_table *htab
1115     = (struct _bfd_sparc_elf_link_hash_table *) obfd->link.hash;
1116
1117   if (htab->loc_hash_table)
1118     htab_delete (htab->loc_hash_table);
1119   if (htab->loc_hash_memory)
1120     objalloc_free ((struct objalloc *) htab->loc_hash_memory);
1121   _bfd_elf_link_hash_table_free (obfd);
1122 }
1123
1124 /* Create a SPARC ELF linker hash table.  */
1125
1126 struct bfd_link_hash_table *
1127 _bfd_sparc_elf_link_hash_table_create (bfd *abfd)
1128 {
1129   struct _bfd_sparc_elf_link_hash_table *ret;
1130   bfd_size_type amt = sizeof (struct _bfd_sparc_elf_link_hash_table);
1131
1132   ret = (struct _bfd_sparc_elf_link_hash_table *) bfd_zmalloc (amt);
1133   if (ret == NULL)
1134     return NULL;
1135
1136   if (ABI_64_P (abfd))
1137     {
1138       ret->put_word = sparc_put_word_64;
1139       ret->r_info = sparc_elf_r_info_64;
1140       ret->r_symndx = sparc_elf_r_symndx_64;
1141       ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF64;
1142       ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD64;
1143       ret->tpoff_reloc = R_SPARC_TLS_TPOFF64;
1144       ret->word_align_power = 3;
1145       ret->align_power_max = 4;
1146       ret->bytes_per_word = 8;
1147       ret->bytes_per_rela = sizeof (Elf64_External_Rela);
1148       ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
1149       ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
1150
1151       ret->build_plt_entry = sparc64_plt_entry_build;
1152       ret->plt_header_size = PLT64_HEADER_SIZE;
1153       ret->plt_entry_size = PLT64_ENTRY_SIZE;
1154     }
1155   else
1156     {
1157       ret->put_word = sparc_put_word_32;
1158       ret->r_info = sparc_elf_r_info_32;
1159       ret->r_symndx = sparc_elf_r_symndx_32;
1160       ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF32;
1161       ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD32;
1162       ret->tpoff_reloc = R_SPARC_TLS_TPOFF32;
1163       ret->word_align_power = 2;
1164       ret->align_power_max = 3;
1165       ret->bytes_per_word = 4;
1166       ret->bytes_per_rela = sizeof (Elf32_External_Rela);
1167       ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
1168       ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
1169
1170       ret->build_plt_entry = sparc32_plt_entry_build;
1171       ret->plt_header_size = PLT32_HEADER_SIZE;
1172       ret->plt_entry_size = PLT32_ENTRY_SIZE;
1173     }
1174
1175   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
1176                                       sizeof (struct _bfd_sparc_elf_link_hash_entry),
1177                                       SPARC_ELF_DATA))
1178     {
1179       free (ret);
1180       return NULL;
1181     }
1182
1183   ret->loc_hash_table = htab_try_create (1024,
1184                                          elf_sparc_local_htab_hash,
1185                                          elf_sparc_local_htab_eq,
1186                                          NULL);
1187   ret->loc_hash_memory = objalloc_create ();
1188   if (!ret->loc_hash_table || !ret->loc_hash_memory)
1189     {
1190       _bfd_sparc_elf_link_hash_table_free (abfd);
1191       return NULL;
1192     }
1193   ret->elf.root.hash_table_free = _bfd_sparc_elf_link_hash_table_free;
1194
1195   return &ret->elf.root;
1196 }
1197
1198 /* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and
1199    .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
1200    hash table.  */
1201
1202 bfd_boolean
1203 _bfd_sparc_elf_create_dynamic_sections (bfd *dynobj,
1204                                         struct bfd_link_info *info)
1205 {
1206   struct _bfd_sparc_elf_link_hash_table *htab;
1207
1208   htab = _bfd_sparc_elf_hash_table (info);
1209   BFD_ASSERT (htab != NULL);
1210
1211   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1212     return FALSE;
1213
1214   if (htab->is_vxworks)
1215     {
1216       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
1217         return FALSE;
1218       if (bfd_link_pic (info))
1219         {
1220           htab->plt_header_size
1221             = 4 * ARRAY_SIZE (sparc_vxworks_shared_plt0_entry);
1222           htab->plt_entry_size
1223             = 4 * ARRAY_SIZE (sparc_vxworks_shared_plt_entry);
1224         }
1225       else
1226         {
1227           htab->plt_header_size
1228             = 4 * ARRAY_SIZE (sparc_vxworks_exec_plt0_entry);
1229           htab->plt_entry_size
1230             = 4 * ARRAY_SIZE (sparc_vxworks_exec_plt_entry);
1231         }
1232     }
1233
1234   if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
1235       || (!bfd_link_pic (info) && !htab->elf.srelbss))
1236     abort ();
1237
1238   return TRUE;
1239 }
1240
1241 static bfd_boolean
1242 create_ifunc_sections (bfd *abfd, struct bfd_link_info *info)
1243 {
1244   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1245   struct elf_link_hash_table *htab = elf_hash_table (info);
1246   flagword flags, pltflags;
1247   asection *s;
1248
1249   if (htab->irelifunc != NULL || htab->iplt != NULL)
1250     return TRUE;
1251
1252   flags = bed->dynamic_sec_flags;
1253   pltflags = flags | SEC_ALLOC | SEC_CODE | SEC_LOAD;
1254
1255   s = bfd_make_section_with_flags (abfd, ".iplt", pltflags);
1256   if (s == NULL
1257       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
1258     return FALSE;
1259   htab->iplt = s;
1260
1261   s = bfd_make_section_with_flags (abfd, ".rela.iplt",
1262                                    flags | SEC_READONLY);
1263   if (s == NULL
1264       || ! bfd_set_section_alignment (abfd, s,
1265                                       bed->s->log_file_align))
1266     return FALSE;
1267   htab->irelplt = s;
1268
1269   return TRUE;
1270 }
1271
1272 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
1273
1274 void
1275 _bfd_sparc_elf_copy_indirect_symbol (struct bfd_link_info *info,
1276                                      struct elf_link_hash_entry *dir,
1277                                      struct elf_link_hash_entry *ind)
1278 {
1279   struct _bfd_sparc_elf_link_hash_entry *edir, *eind;
1280
1281   edir = (struct _bfd_sparc_elf_link_hash_entry *) dir;
1282   eind = (struct _bfd_sparc_elf_link_hash_entry *) ind;
1283
1284   if (eind->dyn_relocs != NULL)
1285     {
1286       if (edir->dyn_relocs != NULL)
1287         {
1288           struct elf_dyn_relocs **pp;
1289           struct elf_dyn_relocs *p;
1290
1291           /* Add reloc counts against the indirect sym to the direct sym
1292              list.  Merge any entries against the same section.  */
1293           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1294             {
1295               struct elf_dyn_relocs *q;
1296
1297               for (q = edir->dyn_relocs; q != NULL; q = q->next)
1298                 if (q->sec == p->sec)
1299                   {
1300                     q->pc_count += p->pc_count;
1301                     q->count += p->count;
1302                     *pp = p->next;
1303                     break;
1304                   }
1305               if (q == NULL)
1306                 pp = &p->next;
1307             }
1308           *pp = edir->dyn_relocs;
1309         }
1310
1311       edir->dyn_relocs = eind->dyn_relocs;
1312       eind->dyn_relocs = NULL;
1313     }
1314
1315   if (ind->root.type == bfd_link_hash_indirect && dir->got.refcount <= 0)
1316     {
1317       edir->tls_type = eind->tls_type;
1318       eind->tls_type = GOT_UNKNOWN;
1319     }
1320
1321   /* Copy has_got_reloc and has_non_got_reloc.  */
1322   edir->has_got_reloc |= eind->has_got_reloc;
1323   edir->has_non_got_reloc |= eind->has_non_got_reloc;
1324
1325   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1326 }
1327
1328 static int
1329 sparc_elf_tls_transition (struct bfd_link_info *info, bfd *abfd,
1330                           int r_type, int is_local)
1331 {
1332   if (! ABI_64_P (abfd)
1333       && r_type == R_SPARC_TLS_GD_HI22
1334       && ! _bfd_sparc_elf_tdata (abfd)->has_tlsgd)
1335     return R_SPARC_REV32;
1336
1337   if (!bfd_link_executable (info))
1338     return r_type;
1339
1340   switch (r_type)
1341     {
1342     case R_SPARC_TLS_GD_HI22:
1343       return is_local ? R_SPARC_TLS_LE_HIX22 : R_SPARC_TLS_IE_HI22;
1344     case R_SPARC_TLS_GD_LO10:
1345       return is_local ? R_SPARC_TLS_LE_LOX10 : R_SPARC_TLS_IE_LO10;
1346     case R_SPARC_TLS_LDM_HI22:
1347       return R_SPARC_TLS_LE_HIX22;
1348     case R_SPARC_TLS_LDM_LO10:
1349       return R_SPARC_TLS_LE_LOX10;
1350     case R_SPARC_TLS_IE_HI22:
1351       return is_local ? R_SPARC_TLS_LE_HIX22 : r_type;
1352     case R_SPARC_TLS_IE_LO10:
1353       return is_local ? R_SPARC_TLS_LE_LOX10 : r_type;
1354     }
1355
1356   return r_type;
1357 }
1358 \f
1359 /* Look through the relocs for a section during the first phase, and
1360    allocate space in the global offset table or procedure linkage
1361    table.  */
1362
1363 bfd_boolean
1364 _bfd_sparc_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
1365                              asection *sec, const Elf_Internal_Rela *relocs)
1366 {
1367   struct _bfd_sparc_elf_link_hash_table *htab;
1368   Elf_Internal_Shdr *symtab_hdr;
1369   struct elf_link_hash_entry **sym_hashes;
1370   const Elf_Internal_Rela *rel;
1371   const Elf_Internal_Rela *rel_end;
1372   asection *sreloc;
1373   int num_relocs;
1374   bfd_boolean checked_tlsgd = FALSE;
1375
1376   if (bfd_link_relocatable (info))
1377     return TRUE;
1378
1379   htab = _bfd_sparc_elf_hash_table (info);
1380   BFD_ASSERT (htab != NULL);
1381   symtab_hdr = &elf_symtab_hdr (abfd);
1382   sym_hashes = elf_sym_hashes (abfd);
1383
1384   sreloc = NULL;
1385
1386   if (ABI_64_P (abfd))
1387     num_relocs = NUM_SHDR_ENTRIES (_bfd_elf_single_rel_hdr (sec));
1388   else
1389     num_relocs = sec->reloc_count;
1390
1391   BFD_ASSERT (is_sparc_elf (abfd) || num_relocs == 0);
1392
1393   if (htab->elf.dynobj == NULL)
1394     htab->elf.dynobj = abfd;
1395   if (!create_ifunc_sections (htab->elf.dynobj, info))
1396     return FALSE;
1397
1398   rel_end = relocs + num_relocs;
1399   for (rel = relocs; rel < rel_end; rel++)
1400     {
1401       unsigned int r_type;
1402       unsigned int r_symndx;
1403       struct elf_link_hash_entry *h;
1404       struct _bfd_sparc_elf_link_hash_entry *eh;
1405       Elf_Internal_Sym *isym;
1406
1407       r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
1408       r_type = SPARC_ELF_R_TYPE (rel->r_info);
1409
1410       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1411         {
1412           /* xgettext:c-format */
1413           _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd, r_symndx);
1414           return FALSE;
1415         }
1416
1417       isym = NULL;
1418       if (r_symndx < symtab_hdr->sh_info)
1419         {
1420           /* A local symbol.  */
1421           isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
1422           if (isym == NULL)
1423             return FALSE;
1424
1425           /* Check relocation against local STT_GNU_IFUNC symbol.  */
1426           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1427             {
1428               h = elf_sparc_get_local_sym_hash (htab, abfd, rel, TRUE);
1429               if (h == NULL)
1430                 return FALSE;
1431
1432               /* Fake a STT_GNU_IFUNC symbol.  */
1433               h->type = STT_GNU_IFUNC;
1434               h->def_regular = 1;
1435               h->ref_regular = 1;
1436               h->forced_local = 1;
1437               h->root.type = bfd_link_hash_defined;
1438             }
1439           else
1440             h = NULL;
1441         }
1442       else
1443         {
1444           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1445           while (h->root.type == bfd_link_hash_indirect
1446                  || h->root.type == bfd_link_hash_warning)
1447             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1448         }
1449
1450       if (h && h->type == STT_GNU_IFUNC && h->def_regular)
1451         {
1452           h->ref_regular = 1;
1453           h->plt.refcount += 1;
1454         }
1455
1456       /* Compatibility with old R_SPARC_REV32 reloc conflicting
1457          with R_SPARC_TLS_GD_HI22.  */
1458       if (! ABI_64_P (abfd) && ! checked_tlsgd)
1459         switch (r_type)
1460           {
1461           case R_SPARC_TLS_GD_HI22:
1462             {
1463               const Elf_Internal_Rela *relt;
1464
1465               for (relt = rel + 1; relt < rel_end; relt++)
1466                 if (ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_LO10
1467                     || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_ADD
1468                     || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_CALL)
1469                   break;
1470               checked_tlsgd = TRUE;
1471               _bfd_sparc_elf_tdata (abfd)->has_tlsgd = relt < rel_end;
1472             }
1473             break;
1474           case R_SPARC_TLS_GD_LO10:
1475           case R_SPARC_TLS_GD_ADD:
1476           case R_SPARC_TLS_GD_CALL:
1477             checked_tlsgd = TRUE;
1478             _bfd_sparc_elf_tdata (abfd)->has_tlsgd = TRUE;
1479             break;
1480           }
1481
1482       r_type = sparc_elf_tls_transition (info, abfd, r_type, h == NULL);
1483       eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
1484
1485       switch (r_type)
1486         {
1487         case R_SPARC_TLS_LDM_HI22:
1488         case R_SPARC_TLS_LDM_LO10:
1489           htab->tls_ldm_got.refcount += 1;
1490           if (eh != NULL)
1491             eh->has_got_reloc = 1;
1492           break;
1493
1494         case R_SPARC_TLS_LE_HIX22:
1495         case R_SPARC_TLS_LE_LOX10:
1496           if (!bfd_link_executable (info))
1497             goto r_sparc_plt32;
1498           break;
1499
1500         case R_SPARC_TLS_IE_HI22:
1501         case R_SPARC_TLS_IE_LO10:
1502           if (!bfd_link_executable (info))
1503             info->flags |= DF_STATIC_TLS;
1504           /* Fall through */
1505
1506         case R_SPARC_GOT10:
1507         case R_SPARC_GOT13:
1508         case R_SPARC_GOT22:
1509         case R_SPARC_GOTDATA_HIX22:
1510         case R_SPARC_GOTDATA_LOX10:
1511         case R_SPARC_GOTDATA_OP_HIX22:
1512         case R_SPARC_GOTDATA_OP_LOX10:
1513         case R_SPARC_TLS_GD_HI22:
1514         case R_SPARC_TLS_GD_LO10:
1515           /* This symbol requires a global offset table entry.  */
1516           {
1517             int tls_type, old_tls_type;
1518
1519             switch (r_type)
1520               {
1521               case R_SPARC_TLS_GD_HI22:
1522               case R_SPARC_TLS_GD_LO10:
1523                 tls_type = GOT_TLS_GD;
1524                 break;
1525               case R_SPARC_TLS_IE_HI22:
1526               case R_SPARC_TLS_IE_LO10:
1527                 tls_type = GOT_TLS_IE;
1528                 break;
1529               default:
1530                 tls_type = GOT_NORMAL;
1531                 break;
1532               }
1533
1534             if (h != NULL)
1535               {
1536                 h->got.refcount += 1;
1537                 old_tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
1538               }
1539             else
1540               {
1541                 bfd_signed_vma *local_got_refcounts;
1542
1543                 /* This is a global offset table entry for a local symbol.  */
1544                 local_got_refcounts = elf_local_got_refcounts (abfd);
1545                 if (local_got_refcounts == NULL)
1546                   {
1547                     bfd_size_type size;
1548
1549                     size = symtab_hdr->sh_info;
1550                     size *= (sizeof (bfd_signed_vma) + sizeof(char));
1551                     local_got_refcounts = ((bfd_signed_vma *)
1552                                            bfd_zalloc (abfd, size));
1553                     if (local_got_refcounts == NULL)
1554                       return FALSE;
1555                     elf_local_got_refcounts (abfd) = local_got_refcounts;
1556                     _bfd_sparc_elf_local_got_tls_type (abfd)
1557                       = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1558                   }
1559
1560                 if (r_type != R_SPARC_GOTDATA_OP_HIX22
1561                     && r_type != R_SPARC_GOTDATA_OP_LOX10)
1562                   local_got_refcounts[r_symndx] += 1;
1563
1564                 old_tls_type = _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx];
1565               }
1566
1567             /* If a TLS symbol is accessed using IE at least once, there is no point
1568                in using the dynamic model for it.  */
1569             if (old_tls_type != tls_type)
1570               {
1571                 if (old_tls_type == GOT_UNKNOWN)
1572                   ;
1573                 else if (old_tls_type == GOT_TLS_GD && tls_type == GOT_TLS_IE)
1574                   ;
1575                 else if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
1576                   tls_type = old_tls_type;
1577                 else
1578                   {
1579                     _bfd_error_handler
1580                       /* xgettext:c-format */
1581                       (_("%pB: `%s' accessed both as normal and thread local symbol"),
1582                        abfd, h ? h->root.root.string : "<local>");
1583                     return FALSE;
1584                   }
1585
1586                 if (h != NULL)
1587                   _bfd_sparc_elf_hash_entry (h)->tls_type = tls_type;
1588                 else
1589                   _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
1590               }
1591           }
1592
1593           if (!htab->elf.sgot
1594               && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
1595             return FALSE;
1596
1597           if (eh != NULL)
1598             eh->has_got_reloc = 1;
1599           break;
1600
1601         case R_SPARC_TLS_GD_CALL:
1602         case R_SPARC_TLS_LDM_CALL:
1603           if (bfd_link_executable (info))
1604             break;
1605
1606           /* Essentially R_SPARC_WPLT30 relocs against __tls_get_addr.  */
1607           h = (struct elf_link_hash_entry *)
1608                bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE,
1609                                      FALSE, TRUE);
1610           BFD_ASSERT (h != NULL);
1611           /* Fall through */
1612
1613         case R_SPARC_WPLT30:
1614         case R_SPARC_PLT32:
1615         case R_SPARC_PLT64:
1616         case R_SPARC_HIPLT22:
1617         case R_SPARC_LOPLT10:
1618         case R_SPARC_PCPLT32:
1619         case R_SPARC_PCPLT22:
1620         case R_SPARC_PCPLT10:
1621           /* This symbol requires a procedure linkage table entry.
1622              We actually build the entry in adjust_dynamic_symbol,
1623              because this might be a case of linking PIC code without
1624              linking in any dynamic objects, in which case we don't
1625              need to generate a procedure linkage table after all.  */
1626
1627           if (h == NULL)
1628             {
1629               if (! ABI_64_P (abfd))
1630                 {
1631                   /* The Solaris native assembler will generate a WPLT30
1632                      reloc for a local symbol if you assemble a call from
1633                      one section to another when using -K pic.  We treat
1634                      it as WDISP30.  */
1635                   if (r_type == R_SPARC_PLT32)
1636                     goto r_sparc_plt32;
1637                   break;
1638                 }
1639               /* PR 7027: We need similar behaviour for 64-bit binaries.  */
1640               else if (r_type == R_SPARC_WPLT30)
1641                 break;
1642
1643               /* It does not make sense to have a procedure linkage
1644                  table entry for a local symbol.  */
1645               bfd_set_error (bfd_error_bad_value);
1646               return FALSE;
1647             }
1648
1649           h->needs_plt = 1;
1650
1651           if (r_type == R_SPARC_PLT32 || r_type == R_SPARC_PLT64)
1652             goto r_sparc_plt32;
1653
1654           h->plt.refcount += 1;
1655
1656           eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
1657           eh->has_got_reloc = 1;
1658           break;
1659
1660         case R_SPARC_PC10:
1661         case R_SPARC_PC22:
1662         case R_SPARC_PC_HH22:
1663         case R_SPARC_PC_HM10:
1664         case R_SPARC_PC_LM22:
1665           if (h != NULL)
1666             h->non_got_ref = 1;
1667
1668           if (h != NULL
1669               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1670             break;
1671           /* Fall through.  */
1672
1673         case R_SPARC_DISP8:
1674         case R_SPARC_DISP16:
1675         case R_SPARC_DISP32:
1676         case R_SPARC_DISP64:
1677         case R_SPARC_WDISP30:
1678         case R_SPARC_WDISP22:
1679         case R_SPARC_WDISP19:
1680         case R_SPARC_WDISP16:
1681         case R_SPARC_WDISP10:
1682         case R_SPARC_8:
1683         case R_SPARC_16:
1684         case R_SPARC_32:
1685         case R_SPARC_HI22:
1686         case R_SPARC_22:
1687         case R_SPARC_13:
1688         case R_SPARC_LO10:
1689         case R_SPARC_UA16:
1690         case R_SPARC_UA32:
1691         case R_SPARC_10:
1692         case R_SPARC_11:
1693         case R_SPARC_64:
1694         case R_SPARC_OLO10:
1695         case R_SPARC_HH22:
1696         case R_SPARC_HM10:
1697         case R_SPARC_LM22:
1698         case R_SPARC_7:
1699         case R_SPARC_5:
1700         case R_SPARC_6:
1701         case R_SPARC_HIX22:
1702         case R_SPARC_LOX10:
1703         case R_SPARC_H44:
1704         case R_SPARC_M44:
1705         case R_SPARC_L44:
1706         case R_SPARC_H34:
1707         case R_SPARC_UA64:
1708           if (h != NULL)
1709             h->non_got_ref = 1;
1710
1711           if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1712             eh->has_non_got_reloc = 1;
1713
1714         r_sparc_plt32:
1715           if (h != NULL && !bfd_link_pic (info))
1716             {
1717               /* We may need a .plt entry if the function this reloc
1718                  refers to is in a shared lib.  */
1719               h->plt.refcount += 1;
1720             }
1721
1722           /* If we are creating a shared library, and this is a reloc
1723              against a global symbol, or a non PC relative reloc
1724              against a local symbol, then we need to copy the reloc
1725              into the shared library.  However, if we are linking with
1726              -Bsymbolic, we do not need to copy a reloc against a
1727              global symbol which is defined in an object we are
1728              including in the link (i.e., DEF_REGULAR is set).  At
1729              this point we have not seen all the input files, so it is
1730              possible that DEF_REGULAR is not set now but will be set
1731              later (it is never cleared).  In case of a weak definition,
1732              DEF_REGULAR may be cleared later by a strong definition in
1733              a shared library.  We account for that possibility below by
1734              storing information in the relocs_copied field of the hash
1735              table entry.  A similar situation occurs when creating
1736              shared libraries and symbol visibility changes render the
1737              symbol local.
1738
1739              If on the other hand, we are creating an executable, we
1740              may need to keep relocations for symbols satisfied by a
1741              dynamic library if we manage to avoid copy relocs for the
1742              symbol.  */
1743           if ((bfd_link_pic (info)
1744                && (sec->flags & SEC_ALLOC) != 0
1745                && (! _bfd_sparc_elf_howto_table[r_type].pc_relative
1746                    || (h != NULL
1747                        && (! SYMBOLIC_BIND (info, h)
1748                            || h->root.type == bfd_link_hash_defweak
1749                            || !h->def_regular))))
1750               || (!bfd_link_pic (info)
1751                   && (sec->flags & SEC_ALLOC) != 0
1752                   && h != NULL
1753                   && (h->root.type == bfd_link_hash_defweak
1754                       || !h->def_regular))
1755               || (!bfd_link_pic (info)
1756                   && h != NULL
1757                   && h->type == STT_GNU_IFUNC))
1758             {
1759               struct elf_dyn_relocs *p;
1760               struct elf_dyn_relocs **head;
1761
1762               /* When creating a shared object, we must copy these
1763                  relocs into the output file.  We create a reloc
1764                  section in dynobj and make room for the reloc.  */
1765               if (sreloc == NULL)
1766                 {
1767                   sreloc = _bfd_elf_make_dynamic_reloc_section
1768                     (sec, htab->elf.dynobj, htab->word_align_power,
1769                      abfd, /*rela?*/ TRUE);
1770
1771                   if (sreloc == NULL)
1772                     return FALSE;
1773                 }
1774
1775               /* If this is a global symbol, we count the number of
1776                  relocations we need for this symbol.  */
1777               if (h != NULL)
1778                 head = &((struct _bfd_sparc_elf_link_hash_entry *) h)->dyn_relocs;
1779               else
1780                 {
1781                   /* Track dynamic relocs needed for local syms too.
1782                      We really need local syms available to do this
1783                      easily.  Oh well.  */
1784                   asection *s;
1785                   void *vpp;
1786
1787                   BFD_ASSERT (isym != NULL);
1788                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1789                   if (s == NULL)
1790                     s = sec;
1791
1792                   vpp = &elf_section_data (s)->local_dynrel;
1793                   head = (struct elf_dyn_relocs **) vpp;
1794                 }
1795
1796               p = *head;
1797               if (p == NULL || p->sec != sec)
1798                 {
1799                   bfd_size_type amt = sizeof *p;
1800                   p = ((struct elf_dyn_relocs *)
1801                        bfd_alloc (htab->elf.dynobj, amt));
1802                   if (p == NULL)
1803                     return FALSE;
1804                   p->next = *head;
1805                   *head = p;
1806                   p->sec = sec;
1807                   p->count = 0;
1808                   p->pc_count = 0;
1809                 }
1810
1811               p->count += 1;
1812               if (_bfd_sparc_elf_howto_table[r_type].pc_relative)
1813                 p->pc_count += 1;
1814             }
1815
1816           break;
1817
1818         case R_SPARC_GNU_VTINHERIT:
1819           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1820             return FALSE;
1821           break;
1822
1823         case R_SPARC_GNU_VTENTRY:
1824           BFD_ASSERT (h != NULL);
1825           if (h != NULL
1826               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1827             return FALSE;
1828           break;
1829
1830         case R_SPARC_REGISTER:
1831           /* Nothing to do.  */
1832           break;
1833
1834         default:
1835           break;
1836         }
1837     }
1838
1839   return TRUE;
1840 }
1841 \f
1842 asection *
1843 _bfd_sparc_elf_gc_mark_hook (asection *sec,
1844                              struct bfd_link_info *info,
1845                              Elf_Internal_Rela *rel,
1846                              struct elf_link_hash_entry *h,
1847                              Elf_Internal_Sym *sym)
1848 {
1849   if (h != NULL)
1850     switch (SPARC_ELF_R_TYPE (rel->r_info))
1851       {
1852       case R_SPARC_GNU_VTINHERIT:
1853       case R_SPARC_GNU_VTENTRY:
1854         return NULL;
1855       }
1856
1857   if (!bfd_link_executable (info))
1858     {
1859       switch (SPARC_ELF_R_TYPE (rel->r_info))
1860         {
1861         case R_SPARC_TLS_GD_CALL:
1862         case R_SPARC_TLS_LDM_CALL:
1863           /* This reloc implicitly references __tls_get_addr.  We know
1864              another reloc will reference the same symbol as the one
1865              on this reloc, so the real symbol and section will be
1866              gc marked when processing the other reloc.  That lets
1867              us handle __tls_get_addr here.  */
1868           h = elf_link_hash_lookup (elf_hash_table (info), "__tls_get_addr",
1869                                     FALSE, FALSE, TRUE);
1870           BFD_ASSERT (h != NULL);
1871           h->mark = 1;
1872           if (h->is_weakalias)
1873             weakdef (h)->mark = 1;
1874           sym = NULL;
1875         }
1876     }
1877
1878   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1879 }
1880
1881 static Elf_Internal_Rela *
1882 sparc_elf_find_reloc_at_ofs (Elf_Internal_Rela *rel,
1883                              Elf_Internal_Rela *relend,
1884                              bfd_vma offset)
1885 {
1886   while (rel < relend)
1887     {
1888       if (rel->r_offset == offset)
1889         return rel;
1890       rel++;
1891     }
1892   return NULL;
1893 }
1894
1895 /* Remove undefined weak symbol from the dynamic symbol table if it
1896    is resolved to 0.   */
1897
1898 bfd_boolean
1899 _bfd_sparc_elf_fixup_symbol (struct bfd_link_info *info,
1900                              struct elf_link_hash_entry *h)
1901 {
1902   if (h->dynindx != -1
1903       && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
1904                                           _bfd_sparc_elf_hash_entry (h)))
1905     {
1906       h->dynindx = -1;
1907       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1908                               h->dynstr_index);
1909     }
1910   return TRUE;
1911 }
1912
1913 /* Find dynamic relocs for H that apply to read-only sections.  */
1914
1915 static asection *
1916 readonly_dynrelocs (struct elf_link_hash_entry *h)
1917 {
1918   struct elf_dyn_relocs *p;
1919
1920   for (p = _bfd_sparc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
1921     {
1922       asection *s = p->sec->output_section;
1923
1924       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1925         return p->sec;
1926     }
1927   return NULL;
1928 }
1929
1930 /* Adjust a symbol defined by a dynamic object and referenced by a
1931    regular object.  The current definition is in some section of the
1932    dynamic object, but we're not including those sections.  We have to
1933    change the definition to something the rest of the link can
1934    understand.  */
1935
1936 bfd_boolean
1937 _bfd_sparc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1938                                      struct elf_link_hash_entry *h)
1939 {
1940   struct _bfd_sparc_elf_link_hash_table *htab;
1941   asection *s, *srel;
1942
1943   htab = _bfd_sparc_elf_hash_table (info);
1944   BFD_ASSERT (htab != NULL);
1945
1946   /* Make sure we know what is going on here.  */
1947   BFD_ASSERT (htab->elf.dynobj != NULL
1948               && (h->needs_plt
1949                   || h->type == STT_GNU_IFUNC
1950                   || h->is_weakalias
1951                   || (h->def_dynamic
1952                       && h->ref_regular
1953                       && !h->def_regular)));
1954
1955   /* If this is a function, put it in the procedure linkage table.  We
1956      will fill in the contents of the procedure linkage table later
1957      (although we could actually do it here).  The STT_NOTYPE
1958      condition is a hack specifically for the Oracle libraries
1959      delivered for Solaris; for some inexplicable reason, they define
1960      some of their functions as STT_NOTYPE when they really should be
1961      STT_FUNC.  */
1962   if (h->type == STT_FUNC
1963       || h->type == STT_GNU_IFUNC
1964       || h->needs_plt
1965       || (h->type == STT_NOTYPE
1966           && (h->root.type == bfd_link_hash_defined
1967               || h->root.type == bfd_link_hash_defweak)
1968           && (h->root.u.def.section->flags & SEC_CODE) != 0))
1969     {
1970       if (h->plt.refcount <= 0
1971           || (h->type != STT_GNU_IFUNC
1972               && (SYMBOL_CALLS_LOCAL (info, h)
1973                   || (h->root.type == bfd_link_hash_undefweak
1974                       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT))))
1975         {
1976           /* This case can occur if we saw a WPLT30 reloc in an input
1977              file, but the symbol was never referred to by a dynamic
1978              object, or if all references were garbage collected.  In
1979              such a case, we don't actually need to build a procedure
1980              linkage table, and we can just do a WDISP30 reloc instead.  */
1981           h->plt.offset = (bfd_vma) -1;
1982           h->needs_plt = 0;
1983         }
1984
1985       return TRUE;
1986     }
1987   else
1988     h->plt.offset = (bfd_vma) -1;
1989
1990   /* If this is a weak symbol, and there is a real definition, the
1991      processor independent code will have arranged for us to see the
1992      real definition first, and we can just use the same value.  */
1993   if (h->is_weakalias)
1994     {
1995       struct elf_link_hash_entry *def = weakdef (h);
1996       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1997       h->root.u.def.section = def->root.u.def.section;
1998       h->root.u.def.value = def->root.u.def.value;
1999       return TRUE;
2000     }
2001
2002   /* This is a reference to a symbol defined by a dynamic object which
2003      is not a function.  */
2004
2005   /* If we are creating a shared library, we must presume that the
2006      only references to the symbol are via the global offset table.
2007      For such cases we need not do anything here; the relocations will
2008      be handled correctly by relocate_section.  */
2009   if (bfd_link_pic (info))
2010     return TRUE;
2011
2012   /* If there are no references to this symbol that do not use the
2013      GOT, we don't need to generate a copy reloc.  */
2014   if (!h->non_got_ref)
2015     return TRUE;
2016
2017   /* If -z nocopyreloc was given, we won't generate them either.  */
2018   if (info->nocopyreloc)
2019     {
2020       h->non_got_ref = 0;
2021       return TRUE;
2022     }
2023
2024   /* If we don't find any dynamic relocs in read-only sections, then
2025      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
2026   if (!readonly_dynrelocs (h))
2027     {
2028       h->non_got_ref = 0;
2029       return TRUE;
2030     }
2031
2032   /* We must allocate the symbol in our .dynbss section, which will
2033      become part of the .bss section of the executable.  There will be
2034      an entry for this symbol in the .dynsym section.  The dynamic
2035      object will contain position independent code, so all references
2036      from the dynamic object to this symbol will go through the global
2037      offset table.  The dynamic linker will use the .dynsym entry to
2038      determine the address it must put in the global offset table, so
2039      both the dynamic object and the regular object will refer to the
2040      same memory location for the variable.  */
2041
2042   /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
2043      to copy the initial value out of the dynamic object and into the
2044      runtime process image.  We need to remember the offset into the
2045      .rel.bss section we are going to use.  */
2046   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2047     {
2048       s = htab->elf.sdynrelro;
2049       srel = htab->elf.sreldynrelro;
2050     }
2051   else
2052     {
2053       s = htab->elf.sdynbss;
2054       srel = htab->elf.srelbss;
2055     }
2056   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2057     {
2058       srel->size += SPARC_ELF_RELA_BYTES (htab);
2059       h->needs_copy = 1;
2060     }
2061
2062   return _bfd_elf_adjust_dynamic_copy (info, h, s);
2063 }
2064
2065 /* Allocate space in .plt, .got and associated reloc sections for
2066    dynamic relocs.  */
2067
2068 static bfd_boolean
2069 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2070 {
2071   struct bfd_link_info *info;
2072   struct _bfd_sparc_elf_link_hash_table *htab;
2073   struct _bfd_sparc_elf_link_hash_entry *eh;
2074   struct elf_dyn_relocs *p;
2075   bfd_boolean resolved_to_zero;
2076
2077   if (h->root.type == bfd_link_hash_indirect)
2078     return TRUE;
2079
2080   info = (struct bfd_link_info *) inf;
2081   htab = _bfd_sparc_elf_hash_table (info);
2082   BFD_ASSERT (htab != NULL);
2083
2084   eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
2085   resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
2086
2087   if ((htab->elf.dynamic_sections_created
2088        && h->plt.refcount > 0)
2089       || (h->type == STT_GNU_IFUNC
2090           && h->def_regular
2091           && h->ref_regular))
2092     {
2093       /* Undefined weak syms won't yet be marked as dynamic.  */
2094       if (h->root.type == bfd_link_hash_undefweak
2095           && !resolved_to_zero
2096           && h->dynindx == -1
2097           && !h->forced_local)
2098         {
2099           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2100             return FALSE;
2101         }
2102
2103       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h)
2104           || (h->type == STT_GNU_IFUNC
2105               && h->def_regular))
2106         {
2107           asection *s = htab->elf.splt;
2108
2109           if (s == NULL)
2110             s = htab->elf.iplt;
2111
2112           /* Allocate room for the header.  */
2113           if (s->size == 0)
2114             {
2115               s->size = htab->plt_header_size;
2116
2117               /* Allocate space for the .rela.plt.unloaded relocations.  */
2118               if (htab->is_vxworks && !bfd_link_pic (info))
2119                 htab->srelplt2->size = sizeof (Elf32_External_Rela) * 2;
2120             }
2121
2122           /* The procedure linkage table size is bounded by the magnitude
2123              of the offset we can describe in the entry.  */
2124           if (s->size >= (SPARC_ELF_WORD_BYTES(htab) == 8 ?
2125                           (((bfd_vma)1 << 31) << 1) : 0x400000))
2126             {
2127               bfd_set_error (bfd_error_bad_value);
2128               return FALSE;
2129             }
2130
2131           if (SPARC_ELF_WORD_BYTES(htab) == 8
2132               && s->size >= PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
2133             {
2134               bfd_vma off = s->size - PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE;
2135
2136
2137               off = (off % (160 * PLT64_ENTRY_SIZE)) / PLT64_ENTRY_SIZE;
2138
2139               h->plt.offset = (s->size - (off * 8));
2140             }
2141           else
2142             h->plt.offset = s->size;
2143
2144           /* If this symbol is not defined in a regular file, and we are
2145              not generating a shared library, then set the symbol to this
2146              location in the .plt.  This is required to make function
2147              pointers compare as equal between the normal executable and
2148              the shared library.  */
2149           if (! bfd_link_pic (info)
2150               && !h->def_regular)
2151             {
2152               h->root.u.def.section = s;
2153               h->root.u.def.value = h->plt.offset;
2154             }
2155
2156           /* Make room for this entry.  */
2157           s->size += htab->plt_entry_size;
2158
2159           /* There should be no PLT relocations against resolved undefined
2160              weak symbols in the executable.  */
2161           if (!resolved_to_zero)
2162             {
2163               /* We also need to make an entry in the .rela.plt section.  */
2164               if (s == htab->elf.splt)
2165                 htab->elf.srelplt->size += SPARC_ELF_RELA_BYTES (htab);
2166               else
2167                 htab->elf.irelplt->size += SPARC_ELF_RELA_BYTES (htab);
2168             }
2169
2170           if (htab->is_vxworks)
2171             {
2172               /* Allocate space for the .got.plt entry.  */
2173               htab->elf.sgotplt->size += 4;
2174
2175               /* ...and for the .rela.plt.unloaded relocations.  */
2176               if (!bfd_link_pic (info))
2177                 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 3;
2178             }
2179         }
2180       else
2181         {
2182           h->plt.offset = (bfd_vma) -1;
2183           h->needs_plt = 0;
2184         }
2185     }
2186   else
2187     {
2188       h->plt.offset = (bfd_vma) -1;
2189       h->needs_plt = 0;
2190     }
2191
2192   /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary,
2193      make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry.  */
2194   if (h->got.refcount > 0
2195       && bfd_link_executable (info)
2196       && h->dynindx == -1
2197       && _bfd_sparc_elf_hash_entry(h)->tls_type == GOT_TLS_IE)
2198     h->got.offset = (bfd_vma) -1;
2199   else if (h->got.refcount > 0)
2200     {
2201       asection *s;
2202       bfd_boolean dyn;
2203       int tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
2204
2205       /* Undefined weak syms won't yet be marked as dynamic.  */
2206       if (h->root.type == bfd_link_hash_undefweak
2207           && !resolved_to_zero
2208           && h->dynindx == -1
2209           && !h->forced_local)
2210         {
2211           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2212             return FALSE;
2213         }
2214
2215       s = htab->elf.sgot;
2216       h->got.offset = s->size;
2217       s->size += SPARC_ELF_WORD_BYTES (htab);
2218       /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots.  */
2219       if (tls_type == GOT_TLS_GD)
2220         s->size += SPARC_ELF_WORD_BYTES (htab);
2221       dyn = htab->elf.dynamic_sections_created;
2222       /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation,
2223          R_SPARC_TLS_GD_{HI22,LO10} needs one if local and two if global.  */
2224       if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
2225           || tls_type == GOT_TLS_IE
2226           || h->type == STT_GNU_IFUNC)
2227         htab->elf.srelgot->size += SPARC_ELF_RELA_BYTES (htab);
2228       else if (tls_type == GOT_TLS_GD)
2229         htab->elf.srelgot->size += 2 * SPARC_ELF_RELA_BYTES (htab);
2230       else if ((WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
2231                 /* Even if the symbol isn't dynamic, we may generate a
2232                    reloc for the dynamic linker in PIC mode.  */
2233                 || (h->dynindx == -1
2234                     && !h->forced_local
2235                     && h->root.type != bfd_link_hash_undefweak
2236                     && bfd_link_pic (info)))
2237                /* No dynamic relocations are needed against resolved
2238                   undefined weak symbols in an executable.  */
2239                && !(h->root.type == bfd_link_hash_undefweak
2240                     && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2241                         || resolved_to_zero)))
2242         htab->elf.srelgot->size += SPARC_ELF_RELA_BYTES (htab);
2243     }
2244   else
2245     h->got.offset = (bfd_vma) -1;
2246
2247   if (eh->dyn_relocs == NULL)
2248     return TRUE;
2249
2250   /* In the shared -Bsymbolic case, discard space allocated for
2251      dynamic pc-relative relocs against symbols which turn out to be
2252      defined in regular objects.  For the normal shared case, discard
2253      space for pc-relative relocs that have become local due to symbol
2254      visibility changes.  */
2255
2256   if (bfd_link_pic (info))
2257     {
2258       if (SYMBOL_CALLS_LOCAL (info, h))
2259         {
2260           struct elf_dyn_relocs **pp;
2261
2262           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2263             {
2264               p->count -= p->pc_count;
2265               p->pc_count = 0;
2266               if (p->count == 0)
2267                 *pp = p->next;
2268               else
2269                 pp = &p->next;
2270             }
2271         }
2272
2273       if (htab->is_vxworks)
2274         {
2275           struct elf_dyn_relocs **pp;
2276
2277           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2278             {
2279               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2280                 *pp = p->next;
2281               else
2282                 pp = &p->next;
2283             }
2284         }
2285
2286       /* Also discard relocs on undefined weak syms with non-default
2287          visibility or in PIE.  */
2288       if (eh->dyn_relocs != NULL
2289           && h->root.type == bfd_link_hash_undefweak)
2290         {
2291           /* An undefined weak symbol is never
2292              bound locally in a shared library.  */
2293
2294           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2295               || resolved_to_zero)
2296             {
2297               if (h->non_got_ref)
2298                 {
2299                   /* Keep dynamic non-GOT/non-PLT relocation so that we
2300                      can branch to 0 without PLT.  */
2301                   struct elf_dyn_relocs **pp;
2302
2303                   for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
2304                     if (p->pc_count == 0)
2305                       *pp = p->next;
2306                     else
2307                       {
2308                         /* Remove other relocations.  */
2309                         p->count = p->pc_count;
2310                         pp = &p->next;
2311                       }
2312
2313                   if (eh->dyn_relocs != NULL)
2314                     {
2315                       /* Make sure undefined weak symbols are output
2316                          as dynamic symbols in PIEs for dynamic non-GOT
2317                          non-PLT reloations.  */
2318                       if (! bfd_elf_link_record_dynamic_symbol (info, h))
2319                         return FALSE;
2320                     }
2321                 }
2322               else
2323                 eh->dyn_relocs = NULL;
2324             }
2325
2326           /* Make sure undefined weak symbols are output as a dynamic
2327              symbol in PIEs.  */
2328           else if (h->dynindx == -1
2329                    && !h->forced_local)
2330             {
2331               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2332                 return FALSE;
2333             }
2334         }
2335     }
2336   else
2337     {
2338       /* For the non-shared case, discard space for relocs against
2339          symbols which turn out to need copy relocs or are not
2340          dynamic.  */
2341
2342       if ((!h->non_got_ref
2343            || (h->root.type == bfd_link_hash_undefweak
2344                && !resolved_to_zero))
2345           && ((h->def_dynamic
2346                && !h->def_regular)
2347               || (htab->elf.dynamic_sections_created
2348                   && (h->root.type == bfd_link_hash_undefweak
2349                       || h->root.type == bfd_link_hash_undefined))))
2350         {
2351           /* Undefined weak syms won't yet be marked as dynamic.  */
2352           if (h->root.type == bfd_link_hash_undefweak
2353               && !resolved_to_zero
2354               && h->dynindx == -1
2355               && !h->forced_local)
2356             {
2357               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2358                 return FALSE;
2359             }
2360
2361           /* If that succeeded, we know we'll be keeping all the
2362              relocs.  */
2363           if (h->dynindx != -1)
2364             goto keep;
2365         }
2366
2367       eh->dyn_relocs = NULL;
2368
2369     keep: ;
2370     }
2371
2372   /* Finally, allocate space.  */
2373   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2374     {
2375       asection *sreloc = elf_section_data (p->sec)->sreloc;
2376       sreloc->size += p->count * SPARC_ELF_RELA_BYTES (htab);
2377     }
2378
2379   return TRUE;
2380 }
2381
2382 /* Allocate space in .plt, .got and associated reloc sections for
2383    local dynamic relocs.  */
2384
2385 static bfd_boolean
2386 allocate_local_dynrelocs (void **slot, void *inf)
2387 {
2388   struct elf_link_hash_entry *h
2389     = (struct elf_link_hash_entry *) *slot;
2390
2391   if (h->type != STT_GNU_IFUNC
2392       || !h->def_regular
2393       || !h->ref_regular
2394       || !h->forced_local
2395       || h->root.type != bfd_link_hash_defined)
2396     abort ();
2397
2398   return allocate_dynrelocs (h, inf);
2399 }
2400
2401 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2402    read-only sections.  */
2403
2404 static bfd_boolean
2405 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
2406 {
2407   asection *sec;
2408
2409   if (h->root.type == bfd_link_hash_indirect)
2410     return TRUE;
2411
2412   sec = readonly_dynrelocs (h);
2413   if (sec != NULL)
2414     {
2415       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
2416
2417       info->flags |= DF_TEXTREL;
2418       info->callbacks->minfo
2419         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
2420          sec->owner, h->root.root.string, sec);
2421
2422       /* Not an error, just cut short the traversal.  */
2423       return FALSE;
2424     }
2425   return TRUE;
2426 }
2427
2428 /* Return true if the dynamic symbol for a given section should be
2429    omitted when creating a shared library.  */
2430
2431 bfd_boolean
2432 _bfd_sparc_elf_omit_section_dynsym (bfd *output_bfd,
2433                                     struct bfd_link_info *info,
2434                                     asection *p)
2435 {
2436   /* We keep the .got section symbol so that explicit relocations
2437      against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode
2438      can be turned into relocations against the .got symbol.  */
2439   if (strcmp (p->name, ".got") == 0)
2440     return FALSE;
2441
2442   return _bfd_elf_omit_section_dynsym_default (output_bfd, info, p);
2443 }
2444
2445 /* Set the sizes of the dynamic sections.  */
2446
2447 bfd_boolean
2448 _bfd_sparc_elf_size_dynamic_sections (bfd *output_bfd,
2449                                       struct bfd_link_info *info)
2450 {
2451   struct _bfd_sparc_elf_link_hash_table *htab;
2452   bfd *dynobj;
2453   asection *s;
2454   bfd *ibfd;
2455
2456   htab = _bfd_sparc_elf_hash_table (info);
2457   BFD_ASSERT (htab != NULL);
2458   dynobj = htab->elf.dynobj;
2459   BFD_ASSERT (dynobj != NULL);
2460
2461   if (elf_hash_table (info)->dynamic_sections_created)
2462     {
2463       /* Set the contents of the .interp section to the interpreter.  */
2464       if (bfd_link_executable (info) && !info->nointerp)
2465         {
2466           s = bfd_get_linker_section (dynobj, ".interp");
2467           BFD_ASSERT (s != NULL);
2468           s->size = htab->dynamic_interpreter_size;
2469           s->contents = (unsigned char *) htab->dynamic_interpreter;
2470           htab->interp = s;
2471         }
2472     }
2473
2474   /* Set up .got offsets for local syms, and space for local dynamic
2475      relocs.  */
2476   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2477     {
2478       bfd_signed_vma *local_got;
2479       bfd_signed_vma *end_local_got;
2480       char *local_tls_type;
2481       bfd_size_type locsymcount;
2482       Elf_Internal_Shdr *symtab_hdr;
2483       asection *srel;
2484
2485       if (! is_sparc_elf (ibfd))
2486         continue;
2487
2488       for (s = ibfd->sections; s != NULL; s = s->next)
2489         {
2490           struct elf_dyn_relocs *p;
2491
2492           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
2493             {
2494               if (!bfd_is_abs_section (p->sec)
2495                   && bfd_is_abs_section (p->sec->output_section))
2496                 {
2497                   /* Input section has been discarded, either because
2498                      it is a copy of a linkonce section or due to
2499                      linker script /DISCARD/, so we'll be discarding
2500                      the relocs too.  */
2501                 }
2502               else if (htab->is_vxworks
2503                        && strcmp (p->sec->output_section->name,
2504                                   ".tls_vars") == 0)
2505                 {
2506                   /* Relocations in vxworks .tls_vars sections are
2507                      handled specially by the loader.  */
2508                 }
2509               else if (p->count != 0)
2510                 {
2511                   srel = elf_section_data (p->sec)->sreloc;
2512                   if (!htab->elf.dynamic_sections_created)
2513                     srel = htab->elf.irelplt;
2514                   srel->size += p->count * SPARC_ELF_RELA_BYTES (htab);
2515                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2516                     {
2517                       info->flags |= DF_TEXTREL;
2518                       info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
2519                                               p->sec->owner, p->sec);
2520                     }
2521                 }
2522             }
2523         }
2524
2525       local_got = elf_local_got_refcounts (ibfd);
2526       if (!local_got)
2527         continue;
2528
2529       symtab_hdr = &elf_symtab_hdr (ibfd);
2530       locsymcount = symtab_hdr->sh_info;
2531       end_local_got = local_got + locsymcount;
2532       local_tls_type = _bfd_sparc_elf_local_got_tls_type (ibfd);
2533       s = htab->elf.sgot;
2534       srel = htab->elf.srelgot;
2535       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
2536         {
2537           if (*local_got > 0)
2538             {
2539               *local_got = s->size;
2540               s->size += SPARC_ELF_WORD_BYTES (htab);
2541               if (*local_tls_type == GOT_TLS_GD)
2542                 s->size += SPARC_ELF_WORD_BYTES (htab);
2543               if (bfd_link_pic (info)
2544                   || *local_tls_type == GOT_TLS_GD
2545                   || *local_tls_type == GOT_TLS_IE)
2546                 srel->size += SPARC_ELF_RELA_BYTES (htab);
2547             }
2548           else
2549             *local_got = (bfd_vma) -1;
2550         }
2551     }
2552
2553   if (htab->tls_ldm_got.refcount > 0)
2554     {
2555       /* Allocate 2 got entries and 1 dynamic reloc for
2556          R_SPARC_TLS_LDM_{HI22,LO10} relocs.  */
2557       htab->tls_ldm_got.offset = htab->elf.sgot->size;
2558       htab->elf.sgot->size += (2 * SPARC_ELF_WORD_BYTES (htab));
2559       htab->elf.srelgot->size += SPARC_ELF_RELA_BYTES (htab);
2560     }
2561   else
2562     htab->tls_ldm_got.offset = -1;
2563
2564   /* Allocate global sym .plt and .got entries, and space for global
2565      sym dynamic relocs.  */
2566   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2567
2568   /* Allocate .plt and .got entries, and space for local symbols.  */
2569   htab_traverse (htab->loc_hash_table, allocate_local_dynrelocs, info);
2570
2571   if (! ABI_64_P (output_bfd)
2572       && !htab->is_vxworks
2573       && elf_hash_table (info)->dynamic_sections_created)
2574     {
2575       /* Make space for the trailing nop in .plt.  */
2576       if (htab->elf.splt->size > 0)
2577         htab->elf.splt->size += 1 * SPARC_INSN_BYTES;
2578
2579       /* If the .got section is more than 0x1000 bytes, we add
2580          0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
2581          bit relocations have a greater chance of working.
2582
2583          FIXME: Make this optimization work for 64-bit too.  */
2584       if (htab->elf.sgot->size >= 0x1000
2585           && elf_hash_table (info)->hgot->root.u.def.value == 0)
2586         elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
2587     }
2588
2589   /* The check_relocs and adjust_dynamic_symbol entry points have
2590      determined the sizes of the various dynamic sections.  Allocate
2591      memory for them.  */
2592   for (s = dynobj->sections; s != NULL; s = s->next)
2593     {
2594       if ((s->flags & SEC_LINKER_CREATED) == 0)
2595         continue;
2596
2597       if (s == htab->elf.splt
2598           || s == htab->elf.sgot
2599           || s == htab->elf.sdynbss
2600           || s == htab->elf.sdynrelro
2601           || s == htab->elf.iplt
2602           || s == htab->elf.sgotplt)
2603         {
2604           /* Strip this section if we don't need it; see the
2605              comment below.  */
2606         }
2607       else if (CONST_STRNEQ (s->name, ".rela"))
2608         {
2609           if (s->size != 0)
2610             {
2611               /* We use the reloc_count field as a counter if we need
2612                  to copy relocs into the output file.  */
2613               s->reloc_count = 0;
2614             }
2615         }
2616       else
2617         {
2618           /* It's not one of our sections.  */
2619           continue;
2620         }
2621
2622       if (s->size == 0)
2623         {
2624           /* If we don't need this section, strip it from the
2625              output file.  This is mostly to handle .rela.bss and
2626              .rela.plt.  We must create both sections in
2627              create_dynamic_sections, because they must be created
2628              before the linker maps input sections to output
2629              sections.  The linker does that before
2630              adjust_dynamic_symbol is called, and it is that
2631              function which decides whether anything needs to go
2632              into these sections.  */
2633           s->flags |= SEC_EXCLUDE;
2634           continue;
2635         }
2636
2637       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2638         continue;
2639
2640       /* Allocate memory for the section contents.  Zero the memory
2641          for the benefit of .rela.plt, which has 4 unused entries
2642          at the beginning, and we don't want garbage.  */
2643       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2644       if (s->contents == NULL)
2645         return FALSE;
2646     }
2647
2648   if (elf_hash_table (info)->dynamic_sections_created)
2649     {
2650       /* Add some entries to the .dynamic section.  We fill in the
2651          values later, in _bfd_sparc_elf_finish_dynamic_sections, but we
2652          must add the entries now so that we get the correct size for
2653          the .dynamic section.  The DT_DEBUG entry is filled in by the
2654          dynamic linker and used by the debugger.  */
2655 #define add_dynamic_entry(TAG, VAL) \
2656   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2657
2658       if (bfd_link_executable (info))
2659         {
2660           if (!add_dynamic_entry (DT_DEBUG, 0))
2661             return FALSE;
2662         }
2663
2664       if (htab->elf.srelplt->size != 0)
2665         {
2666           if (!add_dynamic_entry (DT_PLTGOT, 0)
2667               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2668               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2669               || !add_dynamic_entry (DT_JMPREL, 0))
2670             return FALSE;
2671         }
2672
2673       if (!add_dynamic_entry (DT_RELA, 0)
2674           || !add_dynamic_entry (DT_RELASZ, 0)
2675           || !add_dynamic_entry (DT_RELAENT,
2676                                  SPARC_ELF_RELA_BYTES (htab)))
2677         return FALSE;
2678
2679       /* If any dynamic relocs apply to a read-only section,
2680          then we need a DT_TEXTREL entry.  */
2681       if ((info->flags & DF_TEXTREL) == 0)
2682         elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
2683
2684       if (info->flags & DF_TEXTREL)
2685         {
2686           if (!add_dynamic_entry (DT_TEXTREL, 0))
2687             return FALSE;
2688         }
2689
2690       if (ABI_64_P (output_bfd))
2691         {
2692           int reg;
2693           struct _bfd_sparc_elf_app_reg * app_regs;
2694           struct elf_strtab_hash *dynstr;
2695           struct elf_link_hash_table *eht = elf_hash_table (info);
2696
2697           /* Add dynamic STT_REGISTER symbols and corresponding DT_SPARC_REGISTER
2698              entries if needed.  */
2699           app_regs = _bfd_sparc_elf_hash_table (info)->app_regs;
2700           dynstr = eht->dynstr;
2701
2702           for (reg = 0; reg < 4; reg++)
2703             if (app_regs [reg].name != NULL)
2704               {
2705                 struct elf_link_local_dynamic_entry *entry, *e;
2706
2707                 if (!add_dynamic_entry (DT_SPARC_REGISTER, 0))
2708                   return FALSE;
2709
2710                 entry = (struct elf_link_local_dynamic_entry *)
2711                   bfd_hash_allocate (&info->hash->table, sizeof (*entry));
2712                 if (entry == NULL)
2713                   return FALSE;
2714
2715                 /* We cheat here a little bit: the symbol will not be local, so we
2716                    put it at the end of the dynlocal linked list.  We will fix it
2717                    later on, as we have to fix other fields anyway.  */
2718                 entry->isym.st_value = reg < 2 ? reg + 2 : reg + 4;
2719                 entry->isym.st_size = 0;
2720                 if (*app_regs [reg].name != '\0')
2721                   entry->isym.st_name
2722                     = _bfd_elf_strtab_add (dynstr, app_regs[reg].name, FALSE);
2723                 else
2724                   entry->isym.st_name = 0;
2725                 entry->isym.st_other = 0;
2726                 entry->isym.st_info = ELF_ST_INFO (app_regs [reg].bind,
2727                                                    STT_REGISTER);
2728                 entry->isym.st_shndx = app_regs [reg].shndx;
2729                 entry->isym.st_target_internal = 0;
2730                 entry->next = NULL;
2731                 entry->input_bfd = output_bfd;
2732                 entry->input_indx = -1;
2733
2734                 if (eht->dynlocal == NULL)
2735                   eht->dynlocal = entry;
2736                 else
2737                   {
2738                     for (e = eht->dynlocal; e->next; e = e->next)
2739                       ;
2740                     e->next = entry;
2741                   }
2742                 eht->dynsymcount++;
2743               }
2744         }
2745       if (htab->is_vxworks
2746           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2747         return FALSE;
2748     }
2749 #undef add_dynamic_entry
2750
2751   return TRUE;
2752 }
2753 \f
2754 bfd_boolean
2755 _bfd_sparc_elf_new_section_hook (bfd *abfd, asection *sec)
2756 {
2757   if (!sec->used_by_bfd)
2758     {
2759       struct _bfd_sparc_elf_section_data *sdata;
2760       bfd_size_type amt = sizeof (*sdata);
2761
2762       sdata = bfd_zalloc (abfd, amt);
2763       if (sdata == NULL)
2764         return FALSE;
2765       sec->used_by_bfd = sdata;
2766     }
2767
2768   return _bfd_elf_new_section_hook (abfd, sec);
2769 }
2770
2771 bfd_boolean
2772 _bfd_sparc_elf_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
2773                               struct bfd_section *section,
2774                               struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2775                               bfd_boolean *again)
2776 {
2777   if (bfd_link_relocatable (link_info))
2778     (*link_info->callbacks->einfo)
2779       (_("%P%F: --relax and -r may not be used together\n"));
2780
2781   *again = FALSE;
2782   sec_do_relax (section) = 1;
2783   return TRUE;
2784 }
2785 \f
2786 /* Return the base VMA address which should be subtracted from real addresses
2787    when resolving @dtpoff relocation.
2788    This is PT_TLS segment p_vaddr.  */
2789
2790 static bfd_vma
2791 dtpoff_base (struct bfd_link_info *info)
2792 {
2793   /* If tls_sec is NULL, we should have signalled an error already.  */
2794   if (elf_hash_table (info)->tls_sec == NULL)
2795     return 0;
2796   return elf_hash_table (info)->tls_sec->vma;
2797 }
2798
2799 /* Return the relocation value for @tpoff relocation
2800    if STT_TLS virtual address is ADDRESS.  */
2801
2802 static bfd_vma
2803 tpoff (struct bfd_link_info *info, bfd_vma address)
2804 {
2805   struct elf_link_hash_table *htab = elf_hash_table (info);
2806   const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2807   bfd_vma static_tls_size;
2808
2809   /* If tls_sec is NULL, we should have signalled an error already.  */
2810   if (htab->tls_sec == NULL)
2811     return 0;
2812
2813   /* Consider special static TLS alignment requirements.  */
2814   static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2815   return address - static_tls_size - htab->tls_sec->vma;
2816 }
2817
2818 /* Return the relocation value for a %gdop relocation.  */
2819
2820 static bfd_vma
2821 gdopoff (struct bfd_link_info *info, bfd_vma address)
2822 {
2823   struct elf_link_hash_table *htab = elf_hash_table (info);
2824   bfd_vma got_base;
2825
2826   got_base = (htab->hgot->root.u.def.value
2827               + htab->hgot->root.u.def.section->output_offset
2828               + htab->hgot->root.u.def.section->output_section->vma);
2829
2830   return address - got_base;
2831 }
2832
2833 /* Return whether H is local and its ADDRESS is within 4G of
2834    _GLOBAL_OFFSET_TABLE_ and thus the offset may be calculated by a
2835    sethi, xor sequence.  */
2836
2837 static bfd_boolean
2838 gdop_relative_offset_ok (struct bfd_link_info *info,
2839                          struct elf_link_hash_entry *h,
2840                          bfd_vma address ATTRIBUTE_UNUSED)
2841 {
2842   if (!SYMBOL_REFERENCES_LOCAL (info, h))
2843     return FALSE;
2844   /* If H is undefined, ADDRESS will be zero.  We can't allow a
2845      relative offset to "zero" when producing PIEs or shared libs.
2846      Note that to get here with an undefined symbol it must also be
2847      hidden or internal visibility.  */
2848   if (bfd_link_pic (info)
2849       && h != NULL
2850       && (h->root.type == bfd_link_hash_undefweak
2851           || h->root.type == bfd_link_hash_undefined))
2852     return FALSE;
2853 #ifdef BFD64
2854   return gdopoff (info, address) + ((bfd_vma) 1 << 32) < (bfd_vma) 2 << 32;
2855 #else
2856   return TRUE;
2857 #endif
2858 }
2859
2860 /* Relocate a SPARC ELF section.  */
2861
2862 bfd_boolean
2863 _bfd_sparc_elf_relocate_section (bfd *output_bfd,
2864                                  struct bfd_link_info *info,
2865                                  bfd *input_bfd,
2866                                  asection *input_section,
2867                                  bfd_byte *contents,
2868                                  Elf_Internal_Rela *relocs,
2869                                  Elf_Internal_Sym *local_syms,
2870                                  asection **local_sections)
2871 {
2872   struct _bfd_sparc_elf_link_hash_table *htab;
2873   Elf_Internal_Shdr *symtab_hdr;
2874   struct elf_link_hash_entry **sym_hashes;
2875   bfd_vma *local_got_offsets;
2876   bfd_vma got_base;
2877   asection *sreloc;
2878   Elf_Internal_Rela *rel;
2879   Elf_Internal_Rela *relend;
2880   int num_relocs;
2881   bfd_boolean is_vxworks_tls;
2882
2883   htab = _bfd_sparc_elf_hash_table (info);
2884   BFD_ASSERT (htab != NULL);
2885   symtab_hdr = &elf_symtab_hdr (input_bfd);
2886   sym_hashes = elf_sym_hashes (input_bfd);
2887   local_got_offsets = elf_local_got_offsets (input_bfd);
2888
2889   if (elf_hash_table (info)->hgot == NULL)
2890     got_base = 0;
2891   else
2892     got_base = elf_hash_table (info)->hgot->root.u.def.value;
2893
2894   sreloc = elf_section_data (input_section)->sreloc;
2895   /* We have to handle relocations in vxworks .tls_vars sections
2896      specially, because the dynamic loader is 'weird'.  */
2897   is_vxworks_tls = (htab->is_vxworks && bfd_link_pic (info)
2898                     && !strcmp (input_section->output_section->name,
2899                                 ".tls_vars"));
2900
2901   rel = relocs;
2902   if (ABI_64_P (output_bfd))
2903     num_relocs = NUM_SHDR_ENTRIES (_bfd_elf_single_rel_hdr (input_section));
2904   else
2905     num_relocs = input_section->reloc_count;
2906   relend = relocs + num_relocs;
2907   for (; rel < relend; rel++)
2908     {
2909       int r_type, tls_type;
2910       reloc_howto_type *howto;
2911       unsigned long r_symndx;
2912       struct elf_link_hash_entry *h;
2913       struct _bfd_sparc_elf_link_hash_entry *eh;
2914       Elf_Internal_Sym *sym;
2915       asection *sec;
2916       bfd_vma relocation, off;
2917       bfd_reloc_status_type r;
2918       bfd_boolean is_plt = FALSE;
2919       bfd_boolean unresolved_reloc;
2920       bfd_boolean resolved_to_zero;
2921       bfd_boolean relative_reloc;
2922
2923       r_type = SPARC_ELF_R_TYPE (rel->r_info);
2924       if (r_type == R_SPARC_GNU_VTINHERIT
2925           || r_type == R_SPARC_GNU_VTENTRY)
2926         continue;
2927
2928       if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
2929         {
2930           bfd_set_error (bfd_error_bad_value);
2931           return FALSE;
2932         }
2933
2934       howto = _bfd_sparc_elf_howto_table + r_type;
2935       r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
2936       h = NULL;
2937       sym = NULL;
2938       sec = NULL;
2939       unresolved_reloc = FALSE;
2940       if (r_symndx < symtab_hdr->sh_info)
2941         {
2942           sym = local_syms + r_symndx;
2943           sec = local_sections[r_symndx];
2944           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2945
2946           if (!bfd_link_relocatable (info)
2947               && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2948             {
2949               /* Relocate against local STT_GNU_IFUNC symbol.  */
2950               h = elf_sparc_get_local_sym_hash (htab, input_bfd,
2951                                                 rel, FALSE);
2952               if (h == NULL)
2953                 abort ();
2954
2955               /* Set STT_GNU_IFUNC symbol value.  */
2956               h->root.u.def.value = sym->st_value;
2957               h->root.u.def.section = sec;
2958             }
2959         }
2960       else
2961         {
2962           bfd_boolean warned, ignored;
2963
2964           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2965                                    r_symndx, symtab_hdr, sym_hashes,
2966                                    h, sec, relocation,
2967                                    unresolved_reloc, warned, ignored);
2968           if (warned)
2969             {
2970               /* To avoid generating warning messages about truncated
2971                  relocations, set the relocation's address to be the same as
2972                  the start of this section.  */
2973               if (input_section->output_section != NULL)
2974                 relocation = input_section->output_section->vma;
2975               else
2976                 relocation = 0;
2977             }
2978         }
2979
2980       if (sec != NULL && discarded_section (sec))
2981         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2982                                          rel, 1, relend, howto, 0, contents);
2983
2984       if (bfd_link_relocatable (info))
2985         continue;
2986
2987       if (h != NULL
2988           && h->type == STT_GNU_IFUNC
2989           && h->def_regular)
2990         {
2991           asection *plt_sec;
2992           const char *name;
2993
2994           if ((input_section->flags & SEC_ALLOC) == 0
2995               || h->plt.offset == (bfd_vma) -1)
2996             abort ();
2997
2998           plt_sec = htab->elf.splt;
2999           if (! plt_sec)
3000             plt_sec =htab->elf.iplt;
3001
3002           switch (r_type)
3003             {
3004             case R_SPARC_GOTDATA_OP:
3005               continue;
3006
3007             case R_SPARC_GOTDATA_OP_HIX22:
3008             case R_SPARC_GOTDATA_OP_LOX10:
3009               r_type = (r_type == R_SPARC_GOTDATA_OP_HIX22
3010                         ? R_SPARC_GOT22
3011                         : R_SPARC_GOT10);
3012               howto = _bfd_sparc_elf_howto_table + r_type;
3013               /* Fall through.  */
3014
3015             case R_SPARC_GOT10:
3016             case R_SPARC_GOT13:
3017             case R_SPARC_GOT22:
3018               if (htab->elf.sgot == NULL)
3019                 abort ();
3020               off = h->got.offset;
3021               if (off == (bfd_vma) -1)
3022                 abort();
3023               relocation = htab->elf.sgot->output_offset + off - got_base;
3024               goto do_relocation;
3025
3026             case R_SPARC_WPLT30:
3027             case R_SPARC_WDISP30:
3028               relocation = (plt_sec->output_section->vma
3029                             + plt_sec->output_offset + h->plt.offset);
3030               goto do_relocation;
3031
3032             case R_SPARC_32:
3033             case R_SPARC_64:
3034               if (bfd_link_pic (info) && h->non_got_ref)
3035                 {
3036                   Elf_Internal_Rela outrel;
3037                   bfd_vma offset;
3038
3039                   offset = _bfd_elf_section_offset (output_bfd, info,
3040                                                     input_section,
3041                                                     rel->r_offset);
3042                   if (offset == (bfd_vma) -1
3043                       || offset == (bfd_vma) -2)
3044                     abort();
3045
3046                   outrel.r_offset = (input_section->output_section->vma
3047                                      + input_section->output_offset
3048                                      + offset);
3049
3050                   if (h->dynindx == -1
3051                       || h->forced_local
3052                       || bfd_link_executable (info))
3053                     {
3054                       outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
3055                                                         0, R_SPARC_IRELATIVE);
3056                       outrel.r_addend = relocation + rel->r_addend;
3057                     }
3058                   else
3059                     {
3060                       if (h->dynindx == -1)
3061                         abort();
3062                       outrel.r_info = SPARC_ELF_R_INFO (htab, rel, h->dynindx, r_type);
3063                       outrel.r_addend = rel->r_addend;
3064                     }
3065
3066                   sparc_elf_append_rela (output_bfd, sreloc, &outrel);
3067                   continue;
3068                 }
3069
3070               relocation = (plt_sec->output_section->vma
3071                             + plt_sec->output_offset + h->plt.offset);
3072               goto do_relocation;
3073
3074             case R_SPARC_HI22:
3075             case R_SPARC_LO10:
3076               /* We should only see such relocs in static links.  */
3077               if (bfd_link_pic (info))
3078                 abort();
3079               relocation = (plt_sec->output_section->vma
3080                             + plt_sec->output_offset + h->plt.offset);
3081               goto do_relocation;
3082
3083             default:
3084               if (h->root.root.string)
3085                 name = h->root.root.string;
3086               else
3087                 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3088                                          NULL);
3089               _bfd_error_handler
3090                 /* xgettext:c-format */
3091                 (_("%pB: relocation %s against STT_GNU_IFUNC "
3092                    "symbol `%s' isn't handled by %s"), input_bfd,
3093                  _bfd_sparc_elf_howto_table[r_type].name,
3094                  name, __FUNCTION__);
3095               bfd_set_error (bfd_error_bad_value);
3096               return FALSE;
3097             }
3098         }
3099
3100       eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
3101       resolved_to_zero = eh && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3102
3103       switch (r_type)
3104         {
3105         case R_SPARC_GOTDATA_OP_HIX22:
3106         case R_SPARC_GOTDATA_OP_LOX10:
3107           if (gdop_relative_offset_ok (info, h, relocation))
3108             {
3109               r_type = (r_type == R_SPARC_GOTDATA_OP_HIX22
3110                         ? R_SPARC_GOTDATA_HIX22
3111                         : R_SPARC_GOTDATA_LOX10);
3112               howto = _bfd_sparc_elf_howto_table + r_type;
3113             }
3114           break;
3115
3116         case R_SPARC_GOTDATA_OP:
3117           if (gdop_relative_offset_ok (info, h, relocation))
3118             {
3119               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3120
3121               /* {ld,ldx} [%rs1 + %rs2], %rd --> add %rs1, %rs2, %rd */
3122               relocation = 0x80000000 | (insn & 0x3e07c01f);
3123               bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
3124
3125               /* If the symbol is global but not dynamic, an .rela.* slot has
3126                  been allocated for it in the GOT so output R_SPARC_NONE here.
3127                  See also the handling of other GOT relocations just below.  */
3128               if (h != NULL
3129                   && h->dynindx == -1
3130                   && !h->forced_local
3131                   && h->root.type != bfd_link_hash_undefweak
3132                   && (h->got.offset & 1) == 0
3133                   && bfd_link_pic (info))
3134                 {
3135                   asection *s = htab->elf.srelgot;
3136                   Elf_Internal_Rela outrel;
3137
3138                   BFD_ASSERT (s != NULL);
3139
3140                   memset (&outrel, 0, sizeof outrel);
3141                   sparc_elf_append_rela (output_bfd, s, &outrel);
3142                   h->got.offset |= 1;
3143                 }
3144             }
3145           continue;
3146         }
3147
3148       switch (r_type)
3149         {
3150         case R_SPARC_GOTDATA_HIX22:
3151         case R_SPARC_GOTDATA_LOX10:
3152           relocation = gdopoff (info, relocation);
3153           break;
3154
3155         case R_SPARC_GOTDATA_OP_HIX22:
3156         case R_SPARC_GOTDATA_OP_LOX10:
3157         case R_SPARC_GOT10:
3158         case R_SPARC_GOT13:
3159         case R_SPARC_GOT22:
3160           /* Relocation is to the entry for this symbol in the global
3161              offset table.  */
3162           if (htab->elf.sgot == NULL)
3163             abort ();
3164
3165           relative_reloc = FALSE;
3166           if (h != NULL)
3167             {
3168               bfd_boolean dyn;
3169
3170               off = h->got.offset;
3171               BFD_ASSERT (off != (bfd_vma) -1);
3172               dyn = elf_hash_table (info)->dynamic_sections_created;
3173
3174               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
3175                                                      bfd_link_pic (info),
3176                                                      h)
3177                   || (bfd_link_pic (info)
3178                       && SYMBOL_REFERENCES_LOCAL (info, h)))
3179                 {
3180                   /* This is actually a static link, or it is a
3181                      -Bsymbolic link and the symbol is defined
3182                      locally, or the symbol was forced to be local
3183                      because of a version file.  We must initialize
3184                      this entry in the global offset table.  Since the
3185                      offset must always be a multiple of 8 for 64-bit
3186                      and 4 for 32-bit, we use the least significant bit
3187                      to record whether we have initialized it already.
3188
3189                      When doing a dynamic link, we create a .rela.got
3190                      relocation entry to initialize the value.  This
3191                      is done in the finish_dynamic_symbol routine.  */
3192                   if ((off & 1) != 0)
3193                     off &= ~1;
3194                   else
3195                     {
3196                       /* If this symbol isn't dynamic in PIC mode, treat it
3197                          like a local symbol in PIC mode below.  */
3198                       if (h->dynindx == -1
3199                           && !h->forced_local
3200                           && h->root.type != bfd_link_hash_undefweak
3201                           && bfd_link_pic (info))
3202                         relative_reloc = TRUE;
3203                       else
3204                         SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
3205                                             htab->elf.sgot->contents + off);
3206                       h->got.offset |= 1;
3207                     }
3208                 }
3209               else
3210                 unresolved_reloc = FALSE;
3211             }
3212           else
3213             {
3214               BFD_ASSERT (local_got_offsets != NULL
3215                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
3216
3217               off = local_got_offsets[r_symndx];
3218
3219               /* The offset must always be a multiple of 8 on 64-bit and
3220                  4 on 32-bit.  We use the least significant bit to record
3221                  whether we have already processed this entry.  */
3222               if ((off & 1) != 0)
3223                 off &= ~1;
3224               else
3225                 {
3226                   /* For a local symbol in PIC mode, we need to generate a
3227                      R_SPARC_RELATIVE reloc for the dynamic linker.  */
3228                   if (bfd_link_pic (info))
3229                     relative_reloc = TRUE;
3230                   else
3231                     SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
3232                                         htab->elf.sgot->contents + off);
3233                   local_got_offsets[r_symndx] |= 1;
3234                 }
3235             }
3236
3237           if (relative_reloc)
3238             {
3239               asection *s = htab->elf.srelgot;
3240               Elf_Internal_Rela outrel;
3241
3242               BFD_ASSERT (s != NULL);
3243
3244               outrel.r_offset = (htab->elf.sgot->output_section->vma
3245                                  + htab->elf.sgot->output_offset
3246                                  + off);
3247               outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
3248                                                 0, R_SPARC_RELATIVE);
3249               outrel.r_addend = relocation;
3250               sparc_elf_append_rela (output_bfd, s, &outrel);
3251               /* Versions of glibc ld.so at least up to 2.26 wrongly
3252                  add the section contents to the value calculated for
3253                  a RELATIVE reloc.  Zero the contents to work around
3254                  this bug.  */
3255               relocation = 0;
3256               SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
3257                                   htab->elf.sgot->contents + off);
3258             }
3259
3260           relocation = htab->elf.sgot->output_offset + off - got_base;
3261           break;
3262
3263         case R_SPARC_PLT32:
3264         case R_SPARC_PLT64:
3265           if (h == NULL || h->plt.offset == (bfd_vma) -1)
3266             {
3267               r_type = (r_type == R_SPARC_PLT32) ? R_SPARC_32 : R_SPARC_64;
3268               goto r_sparc_plt32;
3269             }
3270           /* Fall through.  */
3271
3272         case R_SPARC_WPLT30:
3273         case R_SPARC_HIPLT22:
3274         case R_SPARC_LOPLT10:
3275         case R_SPARC_PCPLT32:
3276         case R_SPARC_PCPLT22:
3277         case R_SPARC_PCPLT10:
3278         r_sparc_wplt30:
3279           /* Relocation is to the entry for this symbol in the
3280              procedure linkage table.  */
3281
3282           if (! ABI_64_P (output_bfd))
3283             {
3284               /* The Solaris native assembler will generate a WPLT30 reloc
3285                  for a local symbol if you assemble a call from one
3286                  section to another when using -K pic.  We treat it as
3287                  WDISP30.  */
3288               if (h == NULL)
3289                 break;
3290             }
3291           /* PR 7027: We need similar behaviour for 64-bit binaries.  */
3292           else if (r_type == R_SPARC_WPLT30 && h == NULL)
3293             break;
3294           else
3295             {
3296               BFD_ASSERT (h != NULL);
3297             }
3298
3299           if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
3300             {
3301               /* We didn't make a PLT entry for this symbol.  This
3302                  happens when statically linking PIC code, or when
3303                  using -Bsymbolic.  */
3304               break;
3305             }
3306
3307           relocation = (htab->elf.splt->output_section->vma
3308                         + htab->elf.splt->output_offset
3309                         + h->plt.offset);
3310           unresolved_reloc = FALSE;
3311           if (r_type == R_SPARC_PLT32 || r_type == R_SPARC_PLT64)
3312             {
3313               r_type = r_type == R_SPARC_PLT32 ? R_SPARC_32 : R_SPARC_64;
3314               is_plt = TRUE;
3315               goto r_sparc_plt32;
3316             }
3317           break;
3318
3319         case R_SPARC_PC10:
3320         case R_SPARC_PC22:
3321         case R_SPARC_PC_HH22:
3322         case R_SPARC_PC_HM10:
3323         case R_SPARC_PC_LM22:
3324           if (h != NULL
3325               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3326             break;
3327           /* Fall through.  */
3328         case R_SPARC_DISP8:
3329         case R_SPARC_DISP16:
3330         case R_SPARC_DISP32:
3331         case R_SPARC_DISP64:
3332         case R_SPARC_WDISP30:
3333         case R_SPARC_WDISP22:
3334         case R_SPARC_WDISP19:
3335         case R_SPARC_WDISP16:
3336         case R_SPARC_WDISP10:
3337         case R_SPARC_8:
3338         case R_SPARC_16:
3339         case R_SPARC_32:
3340         case R_SPARC_HI22:
3341         case R_SPARC_22:
3342         case R_SPARC_13:
3343         case R_SPARC_LO10:
3344         case R_SPARC_UA16:
3345         case R_SPARC_UA32:
3346         case R_SPARC_10:
3347         case R_SPARC_11:
3348         case R_SPARC_64:
3349         case R_SPARC_OLO10:
3350         case R_SPARC_HH22:
3351         case R_SPARC_HM10:
3352         case R_SPARC_LM22:
3353         case R_SPARC_7:
3354         case R_SPARC_5:
3355         case R_SPARC_6:
3356         case R_SPARC_HIX22:
3357         case R_SPARC_LOX10:
3358         case R_SPARC_H44:
3359         case R_SPARC_M44:
3360         case R_SPARC_L44:
3361         case R_SPARC_H34:
3362         case R_SPARC_UA64:
3363         r_sparc_plt32:
3364           if ((input_section->flags & SEC_ALLOC) == 0 || is_vxworks_tls)
3365             break;
3366
3367           /* Copy dynamic function pointer relocations.  Don't generate
3368              dynamic relocations against resolved undefined weak symbols
3369              in PIE.  */
3370           if ((bfd_link_pic (info)
3371                && (h == NULL
3372                    || !(h->root.type == bfd_link_hash_undefweak
3373                         && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3374                             || resolved_to_zero)))
3375                && (! howto->pc_relative
3376                    || !SYMBOL_CALLS_LOCAL (info, h)))
3377               || (!bfd_link_pic (info)
3378                   && h != NULL
3379                   && h->dynindx != -1
3380                   && !h->non_got_ref
3381                   && ((h->def_dynamic
3382                        && !h->def_regular)
3383                       || (h->root.type == bfd_link_hash_undefweak
3384                           && !resolved_to_zero)
3385                       || h->root.type == bfd_link_hash_undefined)))
3386             {
3387               Elf_Internal_Rela outrel;
3388               bfd_boolean skip, relocate = FALSE;
3389
3390               /* When generating a shared object, these relocations
3391                  are copied into the output file to be resolved at run
3392                  time.  */
3393
3394               BFD_ASSERT (sreloc != NULL);
3395
3396               skip = FALSE;
3397
3398               outrel.r_offset =
3399                 _bfd_elf_section_offset (output_bfd, info, input_section,
3400                                          rel->r_offset);
3401               if (outrel.r_offset == (bfd_vma) -1)
3402                 skip = TRUE;
3403               else if (outrel.r_offset == (bfd_vma) -2)
3404                 skip = TRUE, relocate = TRUE;
3405               outrel.r_offset += (input_section->output_section->vma
3406                                   + input_section->output_offset);
3407
3408               /* Optimize unaligned reloc usage now that we know where
3409                  it finally resides.  */
3410               switch (r_type)
3411                 {
3412                 case R_SPARC_16:
3413                   if (outrel.r_offset & 1)
3414                     r_type = R_SPARC_UA16;
3415                   break;
3416                 case R_SPARC_UA16:
3417                   if (!(outrel.r_offset & 1))
3418                     r_type = R_SPARC_16;
3419                   break;
3420                 case R_SPARC_32:
3421                   if (outrel.r_offset & 3)
3422                     r_type = R_SPARC_UA32;
3423                   break;
3424                 case R_SPARC_UA32:
3425                   if (!(outrel.r_offset & 3))
3426                     r_type = R_SPARC_32;
3427                   break;
3428                 case R_SPARC_64:
3429                   if (outrel.r_offset & 7)
3430                     r_type = R_SPARC_UA64;
3431                   break;
3432                 case R_SPARC_UA64:
3433                   if (!(outrel.r_offset & 7))
3434                     r_type = R_SPARC_64;
3435                   break;
3436                 case R_SPARC_DISP8:
3437                 case R_SPARC_DISP16:
3438                 case R_SPARC_DISP32:
3439                 case R_SPARC_DISP64:
3440                   /* If the symbol is not dynamic, we should not keep
3441                      a dynamic relocation.  But an .rela.* slot has been
3442                      allocated for it, output R_SPARC_NONE.
3443                      FIXME: Add code tracking needed dynamic relocs as
3444                      e.g. i386 has.  */
3445                   if (h->dynindx == -1)
3446                     skip = TRUE, relocate = TRUE;
3447                   break;
3448                 }
3449
3450               if (skip)
3451                 memset (&outrel, 0, sizeof outrel);
3452               /* h->dynindx may be -1 if the symbol was marked to
3453                  become local.  */
3454               else if (h != NULL
3455                        && h->dynindx != -1
3456                        && (_bfd_sparc_elf_howto_table[r_type].pc_relative
3457                            || !bfd_link_pic (info)
3458                            || !SYMBOLIC_BIND (info, h)
3459                            || !h->def_regular))
3460                 {
3461                   outrel.r_info = SPARC_ELF_R_INFO (htab, rel, h->dynindx, r_type);
3462                   outrel.r_addend = rel->r_addend;
3463                 }
3464               else
3465                 {
3466                   if (  (!ABI_64_P (output_bfd) && r_type == R_SPARC_32)
3467                       || (ABI_64_P (output_bfd) && r_type == R_SPARC_64))
3468                     {
3469                       outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
3470                                                         0, R_SPARC_RELATIVE);
3471                       outrel.r_addend = relocation + rel->r_addend;
3472                     }
3473                   else
3474                     {
3475                       long indx;
3476
3477                       outrel.r_addend = relocation + rel->r_addend;
3478
3479                       if (is_plt)
3480                         sec = htab->elf.splt;
3481
3482                       if (bfd_is_abs_section (sec))
3483                         indx = 0;
3484                       else if (sec == NULL || sec->owner == NULL)
3485                         {
3486                           bfd_set_error (bfd_error_bad_value);
3487                           return FALSE;
3488                         }
3489                       else
3490                         {
3491                           asection *osec;
3492
3493                           /* We are turning this relocation into one
3494                              against a section symbol.  It would be
3495                              proper to subtract the symbol's value,
3496                              osec->vma, from the emitted reloc addend,
3497                              but ld.so expects buggy relocs.  */
3498                           osec = sec->output_section;
3499                           indx = elf_section_data (osec)->dynindx;
3500
3501                           if (indx == 0)
3502                             {
3503                               osec = htab->elf.text_index_section;
3504                               indx = elf_section_data (osec)->dynindx;
3505                             }
3506
3507                           /* FIXME: we really should be able to link non-pic
3508                              shared libraries.  */
3509                           if (indx == 0)
3510                             {
3511                               BFD_FAIL ();
3512                               _bfd_error_handler
3513                                 (_("%pB: probably compiled without -fPIC?"),
3514                                  input_bfd);
3515                               bfd_set_error (bfd_error_bad_value);
3516                               return FALSE;
3517                             }
3518                         }
3519
3520                       outrel.r_info = SPARC_ELF_R_INFO (htab, rel, indx,
3521                                                         r_type);
3522                     }
3523                 }
3524
3525               sparc_elf_append_rela (output_bfd, sreloc, &outrel);
3526
3527               /* This reloc will be computed at runtime, so there's no
3528                  need to do anything now.  */
3529               if (! relocate)
3530                 continue;
3531             }
3532           break;
3533
3534         case R_SPARC_TLS_GD_HI22:
3535         case R_SPARC_TLS_GD_LO10:
3536         case R_SPARC_TLS_IE_HI22:
3537         case R_SPARC_TLS_IE_LO10:
3538           r_type = sparc_elf_tls_transition (info, input_bfd, r_type,
3539                                              h == NULL || h->dynindx == -1);
3540           if (r_type == R_SPARC_REV32)
3541             break;
3542           if (h != NULL)
3543             tls_type = _bfd_sparc_elf_hash_entry (h)->tls_type;
3544           else if (local_got_offsets)
3545             tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
3546           else
3547             tls_type = GOT_UNKNOWN;
3548           if (tls_type == GOT_TLS_IE)
3549             switch (r_type)
3550               {
3551               case R_SPARC_TLS_GD_HI22:
3552                 r_type = R_SPARC_TLS_IE_HI22;
3553                 break;
3554               case R_SPARC_TLS_GD_LO10:
3555                 r_type = R_SPARC_TLS_IE_LO10;
3556                 break;
3557               }
3558
3559           if (r_type == R_SPARC_TLS_LE_HIX22)
3560             {
3561               relocation = tpoff (info, relocation);
3562               break;
3563             }
3564           if (r_type == R_SPARC_TLS_LE_LOX10)
3565             {
3566               /* Change add into xor.  */
3567               relocation = tpoff (info, relocation);
3568               bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd,
3569                                                    contents + rel->r_offset)
3570                                        | 0x80182000), contents + rel->r_offset);
3571               break;
3572             }
3573
3574           if (h != NULL)
3575             {
3576               off = h->got.offset;
3577               h->got.offset |= 1;
3578             }
3579           else
3580             {
3581               BFD_ASSERT (local_got_offsets != NULL);
3582               off = local_got_offsets[r_symndx];
3583               local_got_offsets[r_symndx] |= 1;
3584             }
3585
3586         r_sparc_tlsldm:
3587           if (htab->elf.sgot == NULL)
3588             abort ();
3589
3590           if ((off & 1) != 0)
3591             off &= ~1;
3592           else
3593             {
3594               Elf_Internal_Rela outrel;
3595               int dr_type, indx;
3596
3597               if (htab->elf.srelgot == NULL)
3598                 abort ();
3599
3600               SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
3601                                   htab->elf.sgot->contents + off);
3602               outrel.r_offset = (htab->elf.sgot->output_section->vma
3603                                  + htab->elf.sgot->output_offset + off);
3604               indx = h && h->dynindx != -1 ? h->dynindx : 0;
3605               if (r_type == R_SPARC_TLS_IE_HI22
3606                   || r_type == R_SPARC_TLS_IE_LO10)
3607                 dr_type = SPARC_ELF_TPOFF_RELOC (htab);
3608               else
3609                 dr_type = SPARC_ELF_DTPMOD_RELOC (htab);
3610               if (dr_type == SPARC_ELF_TPOFF_RELOC (htab) && indx == 0)
3611                 outrel.r_addend = relocation - dtpoff_base (info);
3612               else
3613                 outrel.r_addend = 0;
3614               outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx, dr_type);
3615               sparc_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
3616
3617               if (r_type == R_SPARC_TLS_GD_HI22
3618                   || r_type == R_SPARC_TLS_GD_LO10)
3619                 {
3620                   if (indx == 0)
3621                     {
3622                       BFD_ASSERT (! unresolved_reloc);
3623                       SPARC_ELF_PUT_WORD (htab, output_bfd,
3624                                           relocation - dtpoff_base (info),
3625                                           (htab->elf.sgot->contents + off
3626                                            + SPARC_ELF_WORD_BYTES (htab)));
3627                     }
3628                   else
3629                     {
3630                       SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
3631                                           (htab->elf.sgot->contents + off
3632                                            + SPARC_ELF_WORD_BYTES (htab)));
3633                       outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx,
3634                                                         SPARC_ELF_DTPOFF_RELOC (htab));
3635                       outrel.r_offset += SPARC_ELF_WORD_BYTES (htab);
3636                       sparc_elf_append_rela (output_bfd, htab->elf.srelgot,
3637                                              &outrel);
3638                     }
3639                 }
3640               else if (dr_type == SPARC_ELF_DTPMOD_RELOC (htab))
3641                 {
3642                   SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
3643                                       (htab->elf.sgot->contents + off
3644                                        + SPARC_ELF_WORD_BYTES (htab)));
3645                 }
3646             }
3647
3648           if (off >= (bfd_vma) -2)
3649             abort ();
3650
3651           relocation = htab->elf.sgot->output_offset + off - got_base;
3652           unresolved_reloc = FALSE;
3653           howto = _bfd_sparc_elf_howto_table + r_type;
3654           break;
3655
3656         case R_SPARC_TLS_LDM_HI22:
3657         case R_SPARC_TLS_LDM_LO10:
3658           /* LD -> LE */
3659           if (bfd_link_executable (info))
3660             {
3661               bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3662               continue;
3663             }
3664           off = htab->tls_ldm_got.offset;
3665           htab->tls_ldm_got.offset |= 1;
3666           goto r_sparc_tlsldm;
3667
3668         case R_SPARC_TLS_LDO_HIX22:
3669         case R_SPARC_TLS_LDO_LOX10:
3670           /* LD -> LE */
3671           if (bfd_link_executable (info))
3672             {
3673               if (r_type == R_SPARC_TLS_LDO_HIX22)
3674                 r_type = R_SPARC_TLS_LE_HIX22;
3675               else
3676                 r_type = R_SPARC_TLS_LE_LOX10;
3677             }
3678           else
3679             {
3680               relocation -= dtpoff_base (info);
3681               break;
3682             }
3683           /* Fall through.  */
3684
3685         case R_SPARC_TLS_LE_HIX22:
3686         case R_SPARC_TLS_LE_LOX10:
3687           if (!bfd_link_executable (info))
3688             {
3689               Elf_Internal_Rela outrel;
3690               bfd_vma offset
3691                 = _bfd_elf_section_offset (output_bfd, info, input_section,
3692                                            rel->r_offset);
3693               if (offset == (bfd_vma) -1 || offset == (bfd_vma) -2)
3694                 memset (&outrel, 0, sizeof outrel);
3695               else
3696                 {
3697                   outrel.r_offset = offset
3698                                     + input_section->output_section->vma
3699                                     + input_section->output_offset;
3700                   outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, r_type);
3701                   outrel.r_addend
3702                     = relocation - dtpoff_base (info) + rel->r_addend;
3703                 }
3704
3705               BFD_ASSERT (sreloc != NULL);
3706               sparc_elf_append_rela (output_bfd, sreloc, &outrel);
3707               continue;
3708             }
3709           relocation = tpoff (info, relocation);
3710           break;
3711
3712         case R_SPARC_TLS_LDM_CALL:
3713           /* LD -> LE */
3714           if (bfd_link_executable (info))
3715             {
3716               /* mov %g0, %o0 */
3717               bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset);
3718               continue;
3719             }
3720           /* Fall through */
3721
3722         case R_SPARC_TLS_GD_CALL:
3723           if (h != NULL)
3724             tls_type = _bfd_sparc_elf_hash_entry (h)->tls_type;
3725           else if (local_got_offsets)
3726             tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
3727           else
3728             tls_type = GOT_UNKNOWN;
3729           /* GD -> IE or LE */
3730           if (bfd_link_executable (info)
3731               || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE))
3732             {
3733               Elf_Internal_Rela *rel2;
3734               bfd_vma insn;
3735
3736               /* GD -> LE */
3737               if (bfd_link_executable (info) && (h == NULL || h->dynindx == -1))
3738                 {
3739                   bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3740                   continue;
3741                 }
3742
3743               /* GD -> IE */
3744               if (rel + 1 < relend
3745                   && SPARC_ELF_R_TYPE (rel[1].r_info) == R_SPARC_TLS_GD_ADD
3746                   && rel[1].r_offset == rel->r_offset + 4
3747                   && SPARC_ELF_R_SYMNDX (htab, rel[1].r_info) == r_symndx
3748                   && (((insn = bfd_get_32 (input_bfd,
3749                                            contents + rel[1].r_offset))
3750                        >> 25) & 0x1f) == 8)
3751                 {
3752                   /* We have
3753                      call __tls_get_addr, %tgd_call(foo)
3754                       add %reg1, %reg2, %o0, %tgd_add(foo)
3755                      and change it into IE:
3756                      {ld,ldx} [%reg1 + %reg2], %o0, %tie_ldx(foo)
3757                      add %g7, %o0, %o0, %tie_add(foo).
3758                      add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2,
3759                      ld is 0xc0000000 | (rd << 25) | (rs1 << 14) | rs2,
3760                      ldx is 0xc0580000 | (rd << 25) | (rs1 << 14) | rs2.  */
3761                   bfd_put_32 (output_bfd, insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000),
3762                               contents + rel->r_offset);
3763                   bfd_put_32 (output_bfd, 0x9001c008,
3764                               contents + rel->r_offset + 4);
3765                   rel++;
3766                   continue;
3767                 }
3768
3769               /* We cannot just overwrite the delay slot instruction,
3770                  as it might be what puts the %o0 argument to
3771                  __tls_get_addr into place.  So we have to transpose
3772                  the delay slot with the add we patch in.  */
3773               insn = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
3774               bfd_put_32 (output_bfd, insn,
3775                           contents + rel->r_offset);
3776               bfd_put_32 (output_bfd, 0x9001c008,
3777                           contents + rel->r_offset + 4);
3778
3779               rel2 = rel;
3780               while ((rel2 = sparc_elf_find_reloc_at_ofs (rel2 + 1, relend,
3781                                                           rel->r_offset + 4))
3782                      != NULL)
3783                 {
3784                   /* If the instruction we moved has a relocation attached to
3785                      it, adjust the offset so that it will apply to the correct
3786                      instruction.  */
3787                   rel2->r_offset -= 4;
3788                 }
3789               continue;
3790             }
3791
3792           h = (struct elf_link_hash_entry *)
3793               bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE,
3794                                     FALSE, TRUE);
3795           BFD_ASSERT (h != NULL);
3796           r_type = R_SPARC_WPLT30;
3797           howto = _bfd_sparc_elf_howto_table + r_type;
3798           goto r_sparc_wplt30;
3799
3800         case R_SPARC_TLS_GD_ADD:
3801           if (h != NULL)
3802             tls_type = _bfd_sparc_elf_hash_entry (h)->tls_type;
3803           else if (local_got_offsets)
3804             tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
3805           else
3806             tls_type = GOT_UNKNOWN;
3807           /* GD -> IE or LE */
3808           if (bfd_link_executable (info) || tls_type == GOT_TLS_IE)
3809             {
3810               /* add %reg1, %reg2, %reg3, %tgd_add(foo)
3811                  changed into IE:
3812                  {ld,ldx} [%reg1 + %reg2], %reg3, %tie_ldx(foo)
3813                  or LE:
3814                  add %g7, %reg2, %reg3.  */
3815               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3816               if (bfd_link_executable (info) && (h == NULL || h->dynindx == -1))
3817                 relocation = (insn & ~0x7c000) | 0x1c000;
3818               else
3819                 relocation = insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000);
3820               bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
3821             }
3822           continue;
3823
3824         case R_SPARC_TLS_LDM_ADD:
3825           /* LD -> LE */
3826           if (bfd_link_executable (info))
3827             bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3828           continue;
3829
3830         case R_SPARC_TLS_LDO_ADD:
3831           /* LD -> LE */
3832           if (bfd_link_executable (info))
3833             {
3834               /* Change rs1 into %g7.  */
3835               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3836               insn = (insn & ~0x7c000) | 0x1c000;
3837               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
3838             }
3839           continue;
3840
3841         case R_SPARC_TLS_IE_LD:
3842         case R_SPARC_TLS_IE_LDX:
3843           /* IE -> LE */
3844           if (bfd_link_executable (info) && (h == NULL || h->dynindx == -1))
3845             {
3846               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3847               int rs2 = insn & 0x1f;
3848               int rd = (insn >> 25) & 0x1f;
3849
3850               if (rs2 == rd)
3851                 relocation = SPARC_NOP;
3852               else
3853                 relocation = 0x80100000 | (insn & 0x3e00001f);
3854               bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
3855             }
3856           continue;
3857
3858         case R_SPARC_TLS_IE_ADD:
3859           /* Totally useless relocation.  */
3860           continue;
3861
3862         case R_SPARC_TLS_DTPOFF32:
3863         case R_SPARC_TLS_DTPOFF64:
3864           relocation -= dtpoff_base (info);
3865           break;
3866
3867         default:
3868           break;
3869         }
3870
3871       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3872          because such sections are not SEC_ALLOC and thus ld.so will
3873          not process them.  */
3874       if (unresolved_reloc
3875           && !((input_section->flags & SEC_DEBUGGING) != 0
3876                && h->def_dynamic)
3877           && _bfd_elf_section_offset (output_bfd, info, input_section,
3878                                       rel->r_offset) != (bfd_vma) -1)
3879         _bfd_error_handler
3880           /* xgettext:c-format */
3881           (_("%pB(%pA+%#" PRIx64 "): "
3882              "unresolvable %s relocation against symbol `%s'"),
3883            input_bfd,
3884            input_section,
3885            (uint64_t) rel->r_offset,
3886            howto->name,
3887            h->root.root.string);
3888
3889       r = bfd_reloc_continue;
3890       if (r_type == R_SPARC_OLO10)
3891         {
3892             bfd_vma x;
3893
3894             if (! ABI_64_P (output_bfd))
3895               abort ();
3896
3897             relocation += rel->r_addend;
3898             relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
3899
3900             x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3901             x = (x & ~(bfd_vma) 0x1fff) | (relocation & 0x1fff);
3902             bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3903
3904             r = bfd_check_overflow (howto->complain_on_overflow,
3905                                     howto->bitsize, howto->rightshift,
3906                                     bfd_arch_bits_per_address (input_bfd),
3907                                     relocation);
3908         }
3909       else if (r_type == R_SPARC_WDISP16)
3910         {
3911           bfd_vma x;
3912
3913           relocation += rel->r_addend;
3914           relocation -= (input_section->output_section->vma
3915                          + input_section->output_offset);
3916           relocation -= rel->r_offset;
3917
3918           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3919           x |= ((((relocation >> 2) & 0xc000) << 6)
3920                 | ((relocation >> 2) & 0x3fff));
3921           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3922
3923           r = bfd_check_overflow (howto->complain_on_overflow,
3924                                   howto->bitsize, howto->rightshift,
3925                                   bfd_arch_bits_per_address (input_bfd),
3926                                   relocation);
3927         }
3928       else if (r_type == R_SPARC_WDISP10)
3929         {
3930           bfd_vma x;
3931
3932           relocation += rel->r_addend;
3933           relocation -= (input_section->output_section->vma
3934                          + input_section->output_offset);
3935           relocation -= rel->r_offset;
3936
3937           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3938           x |= ((((relocation >> 2) & 0x300) << 11)
3939                 | (((relocation >> 2) & 0xff) << 5));
3940           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3941
3942           r = bfd_check_overflow (howto->complain_on_overflow,
3943                                   howto->bitsize, howto->rightshift,
3944                                   bfd_arch_bits_per_address (input_bfd),
3945                                   relocation);
3946         }
3947       else if (r_type == R_SPARC_REV32)
3948         {
3949           bfd_vma x;
3950
3951           relocation = relocation + rel->r_addend;
3952
3953           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3954           x = x + relocation;
3955           bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
3956           r = bfd_reloc_ok;
3957         }
3958       else if (r_type == R_SPARC_TLS_LDO_HIX22
3959                || r_type == R_SPARC_TLS_LE_HIX22)
3960         {
3961           bfd_vma x;
3962
3963           relocation += rel->r_addend;
3964           if (r_type == R_SPARC_TLS_LE_HIX22)
3965             relocation ^= MINUS_ONE;
3966
3967           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3968           x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
3969           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3970           r = bfd_reloc_ok;
3971         }
3972       else if (r_type == R_SPARC_TLS_LDO_LOX10
3973                || r_type == R_SPARC_TLS_LE_LOX10)
3974         {
3975           bfd_vma x;
3976
3977           relocation += rel->r_addend;
3978           relocation &= 0x3ff;
3979           if (r_type == R_SPARC_TLS_LE_LOX10)
3980             relocation |= 0x1c00;
3981
3982           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3983           x = (x & ~(bfd_vma) 0x1fff) | relocation;
3984           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3985
3986           r = bfd_reloc_ok;
3987         }
3988       else if (r_type == R_SPARC_HIX22
3989                || r_type == R_SPARC_GOTDATA_HIX22
3990                || r_type == R_SPARC_GOTDATA_OP_HIX22)
3991         {
3992           bfd_vma x;
3993
3994           relocation += rel->r_addend;
3995           if (r_type == R_SPARC_HIX22
3996               || (bfd_signed_vma) relocation < 0)
3997             relocation = relocation ^ MINUS_ONE;
3998
3999           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
4000           x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
4001           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
4002
4003           r = bfd_check_overflow (howto->complain_on_overflow,
4004                                   howto->bitsize, howto->rightshift,
4005                                   bfd_arch_bits_per_address (input_bfd),
4006                                   relocation);
4007         }
4008       else if (r_type == R_SPARC_LOX10
4009                || r_type == R_SPARC_GOTDATA_LOX10
4010                || r_type == R_SPARC_GOTDATA_OP_LOX10)
4011         {
4012           bfd_vma x;
4013
4014           relocation += rel->r_addend;
4015           if (r_type == R_SPARC_LOX10
4016               || (bfd_signed_vma) relocation < 0)
4017             relocation = (relocation & 0x3ff) | 0x1c00;
4018           else
4019             relocation = (relocation & 0x3ff);
4020
4021           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
4022           x = (x & ~(bfd_vma) 0x1fff) | relocation;
4023           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
4024
4025           r = bfd_reloc_ok;
4026         }
4027       else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
4028                && sec_do_relax (input_section)
4029                && rel->r_offset + 4 < input_section->size)
4030         {
4031 #define G0              0
4032 #define O7              15
4033 #define XCC             (2 << 20)
4034 #define COND(x)         (((x)&0xf)<<25)
4035 #define CONDA           COND(0x8)
4036 #define INSN_BPA        (F2(0,1) | CONDA | BPRED | XCC)
4037 #define INSN_BA         (F2(0,2) | CONDA)
4038 #define INSN_OR         F3(2, 0x2, 0)
4039 #define INSN_NOP        F2(0,4)
4040
4041           bfd_vma x, y;
4042
4043           /* If the instruction is a call with either:
4044              restore
4045              arithmetic instruction with rd == %o7
4046              where rs1 != %o7 and rs2 if it is register != %o7
4047              then we can optimize if the call destination is near
4048              by changing the call into a branch always.  */
4049           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
4050           y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
4051           if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
4052             {
4053               if (((y & OP3(~0)) == OP3(0x3d) /* restore */
4054                    || ((y & OP3(0x28)) == 0 /* arithmetic */
4055                        && (y & RD(~0)) == RD(O7)))
4056                   && (y & RS1(~0)) != RS1(O7)
4057                   && ((y & F3I(~0))
4058                       || (y & RS2(~0)) != RS2(O7)))
4059                 {
4060                   bfd_vma reloc;
4061
4062                   reloc = relocation + rel->r_addend - rel->r_offset;
4063                   reloc -= (input_section->output_section->vma
4064                             + input_section->output_offset);
4065
4066                   /* Ensure the branch fits into simm22.  */
4067                   if ((reloc & 3) == 0
4068                       && ((reloc & ~(bfd_vma)0x7fffff) == 0
4069                           || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
4070                     {
4071                       reloc >>= 2;
4072
4073                       /* Check whether it fits into simm19.  */
4074                       if (((reloc & 0x3c0000) == 0
4075                            || (reloc & 0x3c0000) == 0x3c0000)
4076                           && (ABI_64_P (output_bfd)
4077                               || elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
4078                         x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
4079                       else
4080                         x = INSN_BA | (reloc & 0x3fffff); /* ba */
4081                       bfd_put_32 (input_bfd, x, contents + rel->r_offset);
4082                       r = bfd_reloc_ok;
4083                       if (rel->r_offset >= 4
4084                           && (y & (0xffffffff ^ RS1(~0)))
4085                              == (INSN_OR | RD(O7) | RS2(G0)))
4086                         {
4087                           bfd_vma z;
4088                           unsigned int reg;
4089
4090                           z = bfd_get_32 (input_bfd,
4091                                           contents + rel->r_offset - 4);
4092                           if ((z & (0xffffffff ^ RD(~0)))
4093                               != (INSN_OR | RS1(O7) | RS2(G0)))
4094                             continue;
4095
4096                           /* The sequence was
4097                              or %o7, %g0, %rN
4098                              call foo
4099                              or %rN, %g0, %o7
4100
4101                              If call foo was replaced with ba, replace
4102                              or %rN, %g0, %o7 with nop.  */
4103
4104                           reg = (y & RS1(~0)) >> 14;
4105                           if (reg != ((z & RD(~0)) >> 25)
4106                               || reg == G0 || reg == O7)
4107                             continue;
4108
4109                           bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
4110                                       contents + rel->r_offset + 4);
4111                         }
4112
4113                     }
4114                 }
4115             }
4116         }
4117
4118       if (r == bfd_reloc_continue)
4119         {
4120 do_relocation:
4121           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4122                                         contents, rel->r_offset,
4123                                         relocation, rel->r_addend);
4124         }
4125       if (r != bfd_reloc_ok)
4126         {
4127           switch (r)
4128             {
4129             default:
4130             case bfd_reloc_outofrange:
4131               abort ();
4132             case bfd_reloc_overflow:
4133               {
4134                 const char *name;
4135
4136                 /* The Solaris native linker silently disregards overflows.
4137                    We don't, but this breaks stabs debugging info, whose
4138                    relocations are only 32-bits wide.  Ignore overflows in
4139                    this case and also for discarded entries.  */
4140                 if ((r_type == R_SPARC_32
4141                      || r_type == R_SPARC_UA32
4142                      || r_type == R_SPARC_DISP32)
4143                     && (((input_section->flags & SEC_DEBUGGING) != 0
4144                          && strcmp (bfd_section_name (input_bfd,
4145                                                       input_section),
4146                                     ".stab") == 0)
4147                         || _bfd_elf_section_offset (output_bfd, info,
4148                                                     input_section,
4149                                                     rel->r_offset)
4150                              == (bfd_vma)-1))
4151                   break;
4152
4153                 if (h != NULL)
4154                   {
4155                     /* Assume this is a call protected by other code that
4156                        detect the symbol is undefined.  If this is the case,
4157                        we can safely ignore the overflow.  If not, the
4158                        program is hosed anyway, and a little warning isn't
4159                        going to help.  */
4160                     if (h->root.type == bfd_link_hash_undefweak
4161                         && howto->pc_relative)
4162                       break;
4163
4164                     name = NULL;
4165                   }
4166                 else
4167                   {
4168                     name = bfd_elf_string_from_elf_section (input_bfd,
4169                                                             symtab_hdr->sh_link,
4170                                                             sym->st_name);
4171                     if (name == NULL)
4172                       return FALSE;
4173                     if (*name == '\0')
4174                       name = bfd_section_name (input_bfd, sec);
4175                   }
4176                 (*info->callbacks->reloc_overflow)
4177                   (info, (h ? &h->root : NULL), name, howto->name,
4178                    (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4179               }
4180               break;
4181             }
4182         }
4183     }
4184
4185   return TRUE;
4186 }
4187
4188 /* Build a VxWorks PLT entry.  PLT_INDEX is the index of the PLT entry
4189    and PLT_OFFSET is the byte offset from the start of .plt.  GOT_OFFSET
4190    is the offset of the associated .got.plt entry from
4191    _GLOBAL_OFFSET_TABLE_.  */
4192
4193 static void
4194 sparc_vxworks_build_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
4195                                bfd_vma plt_offset, bfd_vma plt_index,
4196                                bfd_vma got_offset)
4197 {
4198   bfd_vma got_base;
4199   const bfd_vma *plt_entry;
4200   struct _bfd_sparc_elf_link_hash_table *htab;
4201   bfd_byte *loc;
4202   Elf_Internal_Rela rela;
4203
4204   htab = _bfd_sparc_elf_hash_table (info);
4205   BFD_ASSERT (htab != NULL);
4206
4207   if (bfd_link_pic (info))
4208     {
4209       plt_entry = sparc_vxworks_shared_plt_entry;
4210       got_base = 0;
4211     }
4212   else
4213     {
4214       plt_entry = sparc_vxworks_exec_plt_entry;
4215       got_base = (htab->elf.hgot->root.u.def.value
4216                   + htab->elf.hgot->root.u.def.section->output_offset
4217                   + htab->elf.hgot->root.u.def.section->output_section->vma);
4218     }
4219
4220   /* Fill in the entry in the procedure linkage table.  */
4221   bfd_put_32 (output_bfd, plt_entry[0] + ((got_base + got_offset) >> 10),
4222               htab->elf.splt->contents + plt_offset);
4223   bfd_put_32 (output_bfd, plt_entry[1] + ((got_base + got_offset) & 0x3ff),
4224               htab->elf.splt->contents + plt_offset + 4);
4225   bfd_put_32 (output_bfd, plt_entry[2],
4226               htab->elf.splt->contents + plt_offset + 8);
4227   bfd_put_32 (output_bfd, plt_entry[3],
4228               htab->elf.splt->contents + plt_offset + 12);
4229   bfd_put_32 (output_bfd, plt_entry[4],
4230               htab->elf.splt->contents + plt_offset + 16);
4231   bfd_put_32 (output_bfd, plt_entry[5] + (plt_index >> 10),
4232               htab->elf.splt->contents + plt_offset + 20);
4233   /* PC-relative displacement for a branch to the start of
4234      the PLT section.  */
4235   bfd_put_32 (output_bfd, plt_entry[6] + (((-plt_offset - 24) >> 2)
4236                                           & 0x003fffff),
4237               htab->elf.splt->contents + plt_offset + 24);
4238   bfd_put_32 (output_bfd, plt_entry[7] + (plt_index & 0x3ff),
4239               htab->elf.splt->contents + plt_offset + 28);
4240
4241   /* Fill in the .got.plt entry, pointing initially at the
4242      second half of the PLT entry.  */
4243   BFD_ASSERT (htab->elf.sgotplt != NULL);
4244   bfd_put_32 (output_bfd,
4245               htab->elf.splt->output_section->vma
4246               + htab->elf.splt->output_offset
4247               + plt_offset + 20,
4248               htab->elf.sgotplt->contents + got_offset);
4249
4250   /* Add relocations to .rela.plt.unloaded.  */
4251   if (!bfd_link_pic (info))
4252     {
4253       loc = (htab->srelplt2->contents
4254              + (2 + 3 * plt_index) * sizeof (Elf32_External_Rela));
4255
4256       /* Relocate the initial sethi.  */
4257       rela.r_offset = (htab->elf.splt->output_section->vma
4258                        + htab->elf.splt->output_offset
4259                        + plt_offset);
4260       rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
4261       rela.r_addend = got_offset;
4262       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4263       loc += sizeof (Elf32_External_Rela);
4264
4265       /* Likewise the following or.  */
4266       rela.r_offset += 4;
4267       rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
4268       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4269       loc += sizeof (Elf32_External_Rela);
4270
4271       /* Relocate the .got.plt entry.  */
4272       rela.r_offset = (htab->elf.sgotplt->output_section->vma
4273                        + htab->elf.sgotplt->output_offset
4274                        + got_offset);
4275       rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_SPARC_32);
4276       rela.r_addend = plt_offset + 20;
4277       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4278     }
4279 }
4280
4281 /* Finish up dynamic symbol handling.  We set the contents of various
4282    dynamic sections here.  */
4283
4284 bfd_boolean
4285 _bfd_sparc_elf_finish_dynamic_symbol (bfd *output_bfd,
4286                                       struct bfd_link_info *info,
4287                                       struct elf_link_hash_entry *h,
4288                                       Elf_Internal_Sym *sym)
4289 {
4290   struct _bfd_sparc_elf_link_hash_table *htab;
4291   const struct elf_backend_data *bed;
4292   struct _bfd_sparc_elf_link_hash_entry  *eh;
4293   bfd_boolean resolved_to_zero;
4294
4295   htab = _bfd_sparc_elf_hash_table (info);
4296   BFD_ASSERT (htab != NULL);
4297   bed = get_elf_backend_data (output_bfd);
4298
4299   eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
4300
4301   /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
4302      resolved undefined weak symbols in executable so that their
4303      references have value 0 at run-time.  */
4304   resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
4305
4306   if (h->plt.offset != (bfd_vma) -1)
4307     {
4308       asection *splt;
4309       asection *srela;
4310       Elf_Internal_Rela rela;
4311       bfd_byte *loc;
4312       bfd_vma r_offset, got_offset;
4313       int rela_index;
4314
4315       /* When building a static executable, use .iplt and
4316          .rela.iplt sections for STT_GNU_IFUNC symbols.  */
4317       if (htab->elf.splt != NULL)
4318         {
4319           splt = htab->elf.splt;
4320           srela = htab->elf.srelplt;
4321         }
4322       else
4323         {
4324           splt = htab->elf.iplt;
4325           srela = htab->elf.irelplt;
4326         }
4327
4328       if (splt == NULL || srela == NULL)
4329         abort ();
4330
4331       /* Fill in the entry in the .rela.plt section.  */
4332       if (htab->is_vxworks)
4333         {
4334           /* Work out the index of this PLT entry.  */
4335           rela_index = ((h->plt.offset - htab->plt_header_size)
4336                         / htab->plt_entry_size);
4337
4338           /* Calculate the offset of the associated .got.plt entry.
4339              The first three entries are reserved.  */
4340           got_offset = (rela_index + 3) * 4;
4341
4342           sparc_vxworks_build_plt_entry (output_bfd, info, h->plt.offset,
4343                                          rela_index, got_offset);
4344
4345
4346           /* On VxWorks, the relocation points to the .got.plt entry,
4347              not the .plt entry.  */
4348           rela.r_offset = (htab->elf.sgotplt->output_section->vma
4349                            + htab->elf.sgotplt->output_offset
4350                            + got_offset);
4351           rela.r_addend = 0;
4352           rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx,
4353                                           R_SPARC_JMP_SLOT);
4354         }
4355       else
4356         {
4357           bfd_boolean ifunc = FALSE;
4358
4359           /* Fill in the entry in the procedure linkage table.  */
4360           rela_index = SPARC_ELF_BUILD_PLT_ENTRY (htab, output_bfd, splt,
4361                                                   h->plt.offset, splt->size,
4362                                                   &r_offset);
4363
4364           if (h == NULL
4365               || h->dynindx == -1
4366               || ((bfd_link_executable (info)
4367                    || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4368                   && h->def_regular
4369                   && h->type == STT_GNU_IFUNC))
4370             {
4371               ifunc = TRUE;
4372               BFD_ASSERT (h == NULL
4373                           || (h->type == STT_GNU_IFUNC
4374                               && h->def_regular
4375                               && (h->root.type == bfd_link_hash_defined
4376                                   || h->root.type == bfd_link_hash_defweak)));
4377             }
4378
4379           rela.r_offset = r_offset
4380             + (splt->output_section->vma + splt->output_offset);
4381           if (ABI_64_P (output_bfd)
4382               && h->plt.offset >= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
4383             {
4384               if (ifunc)
4385                 {
4386                   rela.r_addend = (h->root.u.def.section->output_section->vma
4387                                    + h->root.u.def.section->output_offset
4388                                    + h->root.u.def.value);
4389                   rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0,
4390                                                   R_SPARC_IRELATIVE);
4391                 }
4392               else
4393                 {
4394                   rela.r_addend = (-(h->plt.offset + 4)
4395                                    - splt->output_section->vma
4396                                    - splt->output_offset);
4397                   rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx,
4398                                                   R_SPARC_JMP_SLOT);
4399                 }
4400             }
4401           else
4402             {
4403               if (ifunc)
4404                 {
4405                   rela.r_addend = (h->root.u.def.section->output_section->vma
4406                                    + h->root.u.def.section->output_offset
4407                                    + h->root.u.def.value);
4408                   rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0,
4409                                                   R_SPARC_JMP_IREL);
4410                 }
4411               else
4412                 {
4413                   rela.r_addend = 0;
4414                   rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx,
4415                                                   R_SPARC_JMP_SLOT);
4416                 }
4417             }
4418         }
4419
4420       /* Adjust for the first 4 reserved elements in the .plt section
4421          when setting the offset in the .rela.plt section.
4422          Sun forgot to read their own ABI and copied elf32-sparc behaviour,
4423          thus .plt[4] has corresponding .rela.plt[0] and so on.  */
4424
4425       loc = srela->contents;
4426       loc += rela_index * bed->s->sizeof_rela;
4427       bed->s->swap_reloca_out (output_bfd, &rela, loc);
4428
4429       if (!resolved_to_zero && !h->def_regular)
4430         {
4431           /* Mark the symbol as undefined, rather than as defined in
4432              the .plt section.  Leave the value alone.  */
4433           sym->st_shndx = SHN_UNDEF;
4434           /* If the symbol is weak, we do need to clear the value.
4435              Otherwise, the PLT entry would provide a definition for
4436              the symbol even if the symbol wasn't defined anywhere,
4437              and so the symbol would never be NULL.  */
4438           if (!h->ref_regular_nonweak)
4439             sym->st_value = 0;
4440         }
4441     }
4442
4443   /* Don't generate dynamic GOT relocation against resolved undefined weak
4444      symbols in an executable.  */
4445   if (h->got.offset != (bfd_vma) -1
4446       && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_GD
4447       && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_IE
4448       && !(h->root.type == bfd_link_hash_undefweak
4449            && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4450                || resolved_to_zero)))
4451     {
4452       asection *sgot;
4453       asection *srela;
4454       Elf_Internal_Rela rela;
4455
4456       /* This symbol has an entry in the GOT.  Set it up.  */
4457
4458       sgot = htab->elf.sgot;
4459       srela = htab->elf.srelgot;
4460       BFD_ASSERT (sgot != NULL && srela != NULL);
4461
4462       rela.r_offset = (sgot->output_section->vma
4463                        + sgot->output_offset
4464                        + (h->got.offset &~ (bfd_vma) 1));
4465
4466       /* If this is a -Bsymbolic link, and the symbol is defined
4467          locally, we just want to emit a RELATIVE reloc.  Likewise if
4468          the symbol was forced to be local because of a version file.
4469          The entry in the global offset table will already have been
4470          initialized in the relocate_section function.  */
4471       if (! bfd_link_pic (info)
4472           && h->type == STT_GNU_IFUNC
4473           && h->def_regular)
4474         {
4475           asection *plt;
4476
4477           /* We load the GOT entry with the PLT entry.  */
4478           plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4479           SPARC_ELF_PUT_WORD (htab, output_bfd,
4480                               (plt->output_section->vma
4481                                + plt->output_offset + h->plt.offset),
4482                               htab->elf.sgot->contents
4483                               + (h->got.offset & ~(bfd_vma) 1));
4484           return TRUE;
4485         }
4486
4487       if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
4488         {
4489           asection *sec = h->root.u.def.section;
4490           if (h->type == STT_GNU_IFUNC)
4491             rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, R_SPARC_IRELATIVE);
4492           else
4493             rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, R_SPARC_RELATIVE);
4494           rela.r_addend = (h->root.u.def.value
4495                            + sec->output_section->vma
4496                            + sec->output_offset);
4497         }
4498       else
4499         {
4500           rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_GLOB_DAT);
4501           rela.r_addend = 0;
4502         }
4503
4504       SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
4505                           sgot->contents + (h->got.offset & ~(bfd_vma) 1));
4506       sparc_elf_append_rela (output_bfd, srela, &rela);
4507     }
4508
4509   if (h->needs_copy)
4510     {
4511       asection *s;
4512       Elf_Internal_Rela rela;
4513
4514       /* This symbols needs a copy reloc.  Set it up.  */
4515       BFD_ASSERT (h->dynindx != -1);
4516
4517       rela.r_offset = (h->root.u.def.value
4518                        + h->root.u.def.section->output_section->vma
4519                        + h->root.u.def.section->output_offset);
4520       rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_COPY);
4521       rela.r_addend = 0;
4522       if (h->root.u.def.section == htab->elf.sdynrelro)
4523         s = htab->elf.sreldynrelro;
4524       else
4525         s = htab->elf.srelbss;
4526       sparc_elf_append_rela (output_bfd, s, &rela);
4527     }
4528
4529   /* Mark some specially defined symbols as absolute.  On VxWorks,
4530      _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
4531      ".got" section.  Likewise _PROCEDURE_LINKAGE_TABLE_ and ".plt".  */
4532   if (sym != NULL
4533       && (h == htab->elf.hdynamic
4534           || (!htab->is_vxworks
4535               && (h == htab->elf.hgot || h == htab->elf.hplt))))
4536     sym->st_shndx = SHN_ABS;
4537
4538   return TRUE;
4539 }
4540
4541 /* Finish up the dynamic sections.  */
4542
4543 static bfd_boolean
4544 sparc_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
4545                   bfd *dynobj, asection *sdyn,
4546                   asection *splt ATTRIBUTE_UNUSED)
4547 {
4548   struct _bfd_sparc_elf_link_hash_table *htab;
4549   const struct elf_backend_data *bed;
4550   bfd_byte *dyncon, *dynconend;
4551   size_t dynsize;
4552   int stt_regidx = -1;
4553   bfd_boolean abi_64_p;
4554
4555   htab = _bfd_sparc_elf_hash_table (info);
4556   BFD_ASSERT (htab != NULL);
4557   bed = get_elf_backend_data (output_bfd);
4558   dynsize = bed->s->sizeof_dyn;
4559   dynconend = sdyn->contents + sdyn->size;
4560   abi_64_p = ABI_64_P (output_bfd);
4561   for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
4562     {
4563       Elf_Internal_Dyn dyn;
4564       bfd_boolean size;
4565
4566       bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
4567
4568       if (htab->is_vxworks && dyn.d_tag == DT_PLTGOT)
4569         {
4570           /* On VxWorks, DT_PLTGOT should point to the start of the GOT,
4571              not to the start of the PLT.  */
4572           if (htab->elf.sgotplt)
4573             {
4574               dyn.d_un.d_val = (htab->elf.sgotplt->output_section->vma
4575                                 + htab->elf.sgotplt->output_offset);
4576               bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
4577             }
4578         }
4579       else if (htab->is_vxworks
4580                && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4581         bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
4582       else if (abi_64_p && dyn.d_tag == DT_SPARC_REGISTER)
4583         {
4584           if (stt_regidx == -1)
4585             {
4586               stt_regidx =
4587                 _bfd_elf_link_lookup_local_dynindx (info, output_bfd, -1);
4588               if (stt_regidx == -1)
4589                 return FALSE;
4590             }
4591           dyn.d_un.d_val = stt_regidx++;
4592           bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
4593         }
4594       else
4595         {
4596           asection *s;
4597
4598           switch (dyn.d_tag)
4599             {
4600             case DT_PLTGOT:
4601               s = htab->elf.splt;
4602               size = FALSE;
4603               break;
4604             case DT_PLTRELSZ:
4605               s = htab->elf.srelplt;
4606               size = TRUE;
4607               break;
4608             case DT_JMPREL:
4609               s = htab->elf.srelplt;
4610               size = FALSE;
4611               break;
4612             default:
4613               continue;
4614             }
4615
4616           if (s == NULL)
4617             dyn.d_un.d_val = 0;
4618           else
4619             {
4620               if (!size)
4621                 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4622               else
4623                 dyn.d_un.d_val = s->size;
4624             }
4625           bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
4626         }
4627     }
4628   return TRUE;
4629 }
4630
4631 /* Install the first PLT entry in a VxWorks executable and make sure that
4632    .rela.plt.unloaded relocations have the correct symbol indexes.  */
4633
4634 static void
4635 sparc_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
4636 {
4637   struct _bfd_sparc_elf_link_hash_table *htab;
4638   Elf_Internal_Rela rela;
4639   bfd_vma got_base;
4640   bfd_byte *loc;
4641
4642   htab = _bfd_sparc_elf_hash_table (info);
4643   BFD_ASSERT (htab != NULL);
4644
4645   /* Calculate the absolute value of _GLOBAL_OFFSET_TABLE_.  */
4646   got_base = (htab->elf.hgot->root.u.def.section->output_section->vma
4647               + htab->elf.hgot->root.u.def.section->output_offset
4648               + htab->elf.hgot->root.u.def.value);
4649
4650   /* Install the initial PLT entry.  */
4651   bfd_put_32 (output_bfd,
4652               sparc_vxworks_exec_plt0_entry[0] + ((got_base + 8) >> 10),
4653               htab->elf.splt->contents);
4654   bfd_put_32 (output_bfd,
4655               sparc_vxworks_exec_plt0_entry[1] + ((got_base + 8) & 0x3ff),
4656               htab->elf.splt->contents + 4);
4657   bfd_put_32 (output_bfd,
4658               sparc_vxworks_exec_plt0_entry[2],
4659               htab->elf.splt->contents + 8);
4660   bfd_put_32 (output_bfd,
4661               sparc_vxworks_exec_plt0_entry[3],
4662               htab->elf.splt->contents + 12);
4663   bfd_put_32 (output_bfd,
4664               sparc_vxworks_exec_plt0_entry[4],
4665               htab->elf.splt->contents + 16);
4666
4667   loc = htab->srelplt2->contents;
4668
4669   /* Add an unloaded relocation for the initial entry's "sethi".  */
4670   rela.r_offset = (htab->elf.splt->output_section->vma
4671                    + htab->elf.splt->output_offset);
4672   rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
4673   rela.r_addend = 8;
4674   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4675   loc += sizeof (Elf32_External_Rela);
4676
4677   /* Likewise the following "or".  */
4678   rela.r_offset += 4;
4679   rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
4680   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4681   loc += sizeof (Elf32_External_Rela);
4682
4683   /* Fix up the remaining .rela.plt.unloaded relocations.  They may have
4684      the wrong symbol index for _G_O_T_ or _P_L_T_ depending on the order
4685      in which symbols were output.  */
4686   while (loc < htab->srelplt2->contents + htab->srelplt2->size)
4687     {
4688       Elf_Internal_Rela rel;
4689
4690       /* The entry's initial "sethi" (against _G_O_T_).  */
4691       bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
4692       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
4693       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4694       loc += sizeof (Elf32_External_Rela);
4695
4696       /* The following "or" (also against _G_O_T_).  */
4697       bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
4698       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
4699       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4700       loc += sizeof (Elf32_External_Rela);
4701
4702       /* The .got.plt entry (against _P_L_T_).  */
4703       bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
4704       rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_SPARC_32);
4705       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4706       loc += sizeof (Elf32_External_Rela);
4707     }
4708 }
4709
4710 /* Install the first PLT entry in a VxWorks shared object.  */
4711
4712 static void
4713 sparc_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
4714 {
4715   struct _bfd_sparc_elf_link_hash_table *htab;
4716   unsigned int i;
4717
4718   htab = _bfd_sparc_elf_hash_table (info);
4719   BFD_ASSERT (htab != NULL);
4720
4721   for (i = 0; i < ARRAY_SIZE (sparc_vxworks_shared_plt0_entry); i++)
4722     bfd_put_32 (output_bfd, sparc_vxworks_shared_plt0_entry[i],
4723                 htab->elf.splt->contents + i * 4);
4724 }
4725
4726 /* Finish up local dynamic symbol handling.  We set the contents of
4727    various dynamic sections here.  */
4728
4729 static bfd_boolean
4730 finish_local_dynamic_symbol (void **slot, void *inf)
4731 {
4732   struct elf_link_hash_entry *h
4733     = (struct elf_link_hash_entry *) *slot;
4734   struct bfd_link_info *info
4735     = (struct bfd_link_info *) inf;
4736
4737   return _bfd_sparc_elf_finish_dynamic_symbol (info->output_bfd, info,
4738                                                h, NULL);
4739 }
4740
4741 /* Finish up undefined weak symbol handling in PIE.  Fill its PLT entry
4742    here since undefined weak symbol may not be dynamic and may not be
4743    called for _bfd_sparc_elf_finish_dynamic_symbol.  */
4744
4745 static bfd_boolean
4746 pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4747                              void *inf)
4748 {
4749   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4750   struct bfd_link_info *info = (struct bfd_link_info *) inf;
4751
4752   if (h->root.type != bfd_link_hash_undefweak
4753       || h->dynindx != -1)
4754     return TRUE;
4755
4756   return _bfd_sparc_elf_finish_dynamic_symbol (info->output_bfd, info,
4757                                                h, NULL);
4758 }
4759
4760 bfd_boolean
4761 _bfd_sparc_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
4762 {
4763   bfd *dynobj;
4764   asection *sdyn;
4765   struct _bfd_sparc_elf_link_hash_table *htab;
4766
4767   htab = _bfd_sparc_elf_hash_table (info);
4768   BFD_ASSERT (htab != NULL);
4769   dynobj = htab->elf.dynobj;
4770
4771   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4772
4773   if (elf_hash_table (info)->dynamic_sections_created)
4774     {
4775       asection *splt;
4776
4777       splt = htab->elf.splt;
4778       BFD_ASSERT (splt != NULL && sdyn != NULL);
4779
4780       if (!sparc_finish_dyn (output_bfd, info, dynobj, sdyn, splt))
4781         return FALSE;
4782
4783       /* Initialize the contents of the .plt section.  */
4784       if (splt->size > 0)
4785         {
4786           if (htab->is_vxworks)
4787             {
4788               if (bfd_link_pic (info))
4789                 sparc_vxworks_finish_shared_plt (output_bfd, info);
4790               else
4791                 sparc_vxworks_finish_exec_plt (output_bfd, info);
4792             }
4793           else
4794             {
4795               memset (splt->contents, 0, htab->plt_header_size);
4796               if (!ABI_64_P (output_bfd))
4797                 bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
4798                             splt->contents + splt->size - 4);
4799             }
4800         }
4801
4802       if (elf_section_data (splt->output_section) != NULL)
4803         elf_section_data (splt->output_section)->this_hdr.sh_entsize
4804           = ((htab->is_vxworks || !ABI_64_P (output_bfd))
4805              ? 0 : htab->plt_entry_size);
4806     }
4807
4808   /* Set the first entry in the global offset table to the address of
4809      the dynamic section.  */
4810   if (htab->elf.sgot && htab->elf.sgot->size > 0)
4811     {
4812       bfd_vma val = (sdyn ?
4813                      sdyn->output_section->vma + sdyn->output_offset :
4814                      0);
4815
4816       SPARC_ELF_PUT_WORD (htab, output_bfd, val, htab->elf.sgot->contents);
4817     }
4818
4819   if (htab->elf.sgot)
4820     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize =
4821       SPARC_ELF_WORD_BYTES (htab);
4822
4823   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
4824   htab_traverse (htab->loc_hash_table, finish_local_dynamic_symbol, info);
4825
4826   /* Fill PLT entries for undefined weak symbols in PIE.  */
4827   if (bfd_link_pie (info))
4828     bfd_hash_traverse (&info->hash->table,
4829                        pie_finish_undefweak_symbol,
4830                        info);
4831   return TRUE;
4832 }
4833
4834 \f
4835 /* Set the right machine number for a SPARC ELF file.  */
4836
4837 bfd_boolean
4838 _bfd_sparc_elf_object_p (bfd *abfd)
4839 {
4840   obj_attribute *attrs = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
4841   obj_attribute *hwcaps = &attrs[Tag_GNU_Sparc_HWCAPS];
4842   obj_attribute *hwcaps2 = &attrs[Tag_GNU_Sparc_HWCAPS2];
4843
4844   unsigned int v9c_hwcaps_mask = ELF_SPARC_HWCAP_ASI_BLK_INIT;
4845   unsigned int v9d_hwcaps_mask = (ELF_SPARC_HWCAP_FMAF
4846                                   | ELF_SPARC_HWCAP_VIS3
4847                                   | ELF_SPARC_HWCAP_HPC);
4848   unsigned int v9e_hwcaps_mask = (ELF_SPARC_HWCAP_AES
4849                                   | ELF_SPARC_HWCAP_DES
4850                                   | ELF_SPARC_HWCAP_KASUMI
4851                                   | ELF_SPARC_HWCAP_CAMELLIA
4852                                   | ELF_SPARC_HWCAP_MD5
4853                                   | ELF_SPARC_HWCAP_SHA1
4854                                   | ELF_SPARC_HWCAP_SHA256
4855                                   | ELF_SPARC_HWCAP_SHA512
4856                                   | ELF_SPARC_HWCAP_MPMUL
4857                                   | ELF_SPARC_HWCAP_MONT
4858                                   | ELF_SPARC_HWCAP_CRC32C
4859                                   | ELF_SPARC_HWCAP_CBCOND
4860                                   | ELF_SPARC_HWCAP_PAUSE);
4861   unsigned int v9v_hwcaps_mask = (ELF_SPARC_HWCAP_FJFMAU
4862                                  | ELF_SPARC_HWCAP_IMA);
4863   unsigned int v9m_hwcaps2_mask = (ELF_SPARC_HWCAP2_SPARC5
4864                                    | ELF_SPARC_HWCAP2_MWAIT
4865                                    | ELF_SPARC_HWCAP2_XMPMUL
4866                                    | ELF_SPARC_HWCAP2_XMONT);
4867   unsigned int m8_hwcaps2_mask = (ELF_SPARC_HWCAP2_SPARC6
4868                                   | ELF_SPARC_HWCAP2_ONADDSUB
4869                                   | ELF_SPARC_HWCAP2_ONMUL
4870                                   | ELF_SPARC_HWCAP2_ONDIV
4871                                   | ELF_SPARC_HWCAP2_DICTUNP
4872                                   | ELF_SPARC_HWCAP2_FPCMPSHL
4873                                   | ELF_SPARC_HWCAP2_RLE
4874                                   | ELF_SPARC_HWCAP2_SHA3);
4875
4876   if (ABI_64_P (abfd))
4877     {
4878       unsigned long mach = bfd_mach_sparc_v9;
4879
4880       if (hwcaps2->i & m8_hwcaps2_mask)
4881         mach = bfd_mach_sparc_v9m8;
4882       else if (hwcaps2->i & v9m_hwcaps2_mask)
4883         mach = bfd_mach_sparc_v9m;
4884       else if (hwcaps->i & v9v_hwcaps_mask)
4885         mach = bfd_mach_sparc_v9v;
4886       else if (hwcaps->i & v9e_hwcaps_mask)
4887         mach = bfd_mach_sparc_v9e;
4888       else if (hwcaps->i & v9d_hwcaps_mask)
4889         mach = bfd_mach_sparc_v9d;
4890       else if (hwcaps->i & v9c_hwcaps_mask)
4891         mach = bfd_mach_sparc_v9c;
4892       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
4893         mach = bfd_mach_sparc_v9b;
4894       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
4895         mach = bfd_mach_sparc_v9a;
4896       return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach);
4897     }
4898   else
4899     {
4900       if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
4901         {
4902           if (hwcaps2->i & m8_hwcaps2_mask)
4903             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4904                                               bfd_mach_sparc_v8plusm8);
4905           else if (hwcaps2->i & v9m_hwcaps2_mask)
4906             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4907                                               bfd_mach_sparc_v8plusm);
4908           else if (hwcaps->i & v9v_hwcaps_mask)
4909             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4910                                               bfd_mach_sparc_v8plusv);
4911           else if (hwcaps->i & v9e_hwcaps_mask)
4912             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4913                                               bfd_mach_sparc_v8pluse);
4914           else if (hwcaps->i & v9d_hwcaps_mask)
4915             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4916                                               bfd_mach_sparc_v8plusd);
4917           else if (hwcaps->i & v9c_hwcaps_mask)
4918             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4919                                               bfd_mach_sparc_v8plusc);
4920           else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
4921             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4922                                               bfd_mach_sparc_v8plusb);
4923           else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
4924             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4925                                               bfd_mach_sparc_v8plusa);
4926           else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
4927             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4928                                               bfd_mach_sparc_v8plus);
4929           else
4930             return FALSE;
4931         }
4932       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
4933         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4934                                           bfd_mach_sparc_sparclite_le);
4935       else
4936         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
4937     }
4938 }
4939
4940 /* Return address for Ith PLT stub in section PLT, for relocation REL
4941    or (bfd_vma) -1 if it should not be included.  */
4942
4943 bfd_vma
4944 _bfd_sparc_elf_plt_sym_val (bfd_vma i, const asection *plt, const arelent *rel)
4945 {
4946   if (ABI_64_P (plt->owner))
4947     {
4948       bfd_vma j;
4949
4950       i += PLT64_HEADER_SIZE / PLT64_ENTRY_SIZE;
4951       if (i < PLT64_LARGE_THRESHOLD)
4952         return plt->vma + i * PLT64_ENTRY_SIZE;
4953
4954       j = (i - PLT64_LARGE_THRESHOLD) % 160;
4955       i -= j;
4956       return plt->vma + i * PLT64_ENTRY_SIZE + j * 4 * 6;
4957     }
4958   else
4959     return rel->address;
4960 }
4961
4962 /* Merge backend specific data from an object file to the output
4963    object file when linking.  */
4964
4965 bfd_boolean
4966 _bfd_sparc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4967 {
4968   bfd *obfd = info->output_bfd;
4969   obj_attribute *in_attr, *in_attrs;
4970   obj_attribute *out_attr, *out_attrs;
4971
4972   if (!elf_known_obj_attributes_proc (obfd)[0].i)
4973     {
4974       /* This is the first object.  Copy the attributes.  */
4975       _bfd_elf_copy_obj_attributes (ibfd, obfd);
4976
4977       /* Use the Tag_null value to indicate the attributes have been
4978          initialized.  */
4979       elf_known_obj_attributes_proc (obfd)[0].i = 1;
4980
4981       return TRUE;
4982     }
4983
4984   in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
4985   out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
4986
4987   in_attr = &in_attrs[Tag_GNU_Sparc_HWCAPS];
4988   out_attr = &out_attrs[Tag_GNU_Sparc_HWCAPS];
4989
4990   out_attr->i |= in_attr->i;
4991   out_attr->type = 1;
4992
4993   in_attr = &in_attrs[Tag_GNU_Sparc_HWCAPS2];
4994   out_attr = &out_attrs[Tag_GNU_Sparc_HWCAPS2];
4995
4996   out_attr->i |= in_attr->i;
4997   out_attr->type = 1;
4998
4999   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5000   _bfd_elf_merge_object_attributes (ibfd, info);
5001
5002   return TRUE;
5003 }