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