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