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