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