Update to ISO-C90 and fix formatting
[external/binutils.git] / bfd / elfxx-sparc.c
1 /* SPARC-specific support for ELF
2    Copyright 2005 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* This file handles functionality common to the different SPARC ABI's.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/sparc.h"
28 #include "opcode/sparc.h"
29 #include "elfxx-sparc.h"
30
31 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
32 #define MINUS_ONE (~ (bfd_vma) 0)
33
34 #define ABI_64_P(abfd) \
35   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
36
37 /* The relocation "howto" table.  */
38
39 /* Utility for performing the standard initial work of an instruction
40    relocation.
41    *PRELOCATION will contain the relocated item.
42    *PINSN will contain the instruction from the input stream.
43    If the result is `bfd_reloc_other' the caller can continue with
44    performing the relocation.  Otherwise it must stop and return the
45    value to its caller.  */
46
47 static bfd_reloc_status_type
48 init_insn_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
49                  PTR data, asection *input_section, bfd *output_bfd,
50                  bfd_vma *prelocation, bfd_vma *pinsn)
51 {
52   bfd_vma relocation;
53   reloc_howto_type *howto = reloc_entry->howto;
54
55   if (output_bfd != (bfd *) NULL
56       && (symbol->flags & BSF_SECTION_SYM) == 0
57       && (! howto->partial_inplace
58           || reloc_entry->addend == 0))
59     {
60       reloc_entry->address += input_section->output_offset;
61       return bfd_reloc_ok;
62     }
63
64   /* This works because partial_inplace is FALSE.  */
65   if (output_bfd != NULL)
66     return bfd_reloc_continue;
67
68   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
69     return bfd_reloc_outofrange;
70
71   relocation = (symbol->value
72                 + symbol->section->output_section->vma
73                 + symbol->section->output_offset);
74   relocation += reloc_entry->addend;
75   if (howto->pc_relative)
76     {
77       relocation -= (input_section->output_section->vma
78                      + input_section->output_offset);
79       relocation -= reloc_entry->address;
80     }
81
82   *prelocation = relocation;
83   *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
84   return bfd_reloc_other;
85 }
86
87 /* For unsupported relocs.  */
88
89 static bfd_reloc_status_type
90 sparc_elf_notsup_reloc (bfd *abfd ATTRIBUTE_UNUSED,
91                         arelent *reloc_entry ATTRIBUTE_UNUSED,
92                         asymbol *symbol ATTRIBUTE_UNUSED,
93                         PTR data ATTRIBUTE_UNUSED,
94                         asection *input_section ATTRIBUTE_UNUSED,
95                         bfd *output_bfd ATTRIBUTE_UNUSED,
96                         char **error_message ATTRIBUTE_UNUSED)
97 {
98   return bfd_reloc_notsupported;
99 }
100
101 /* Handle the WDISP16 reloc.  */
102
103 static bfd_reloc_status_type
104 sparc_elf_wdisp16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
105                          PTR data, asection *input_section, bfd *output_bfd,
106                          char **error_message ATTRIBUTE_UNUSED)
107 {
108   bfd_vma relocation;
109   bfd_vma insn;
110   bfd_reloc_status_type status;
111
112   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
113                             input_section, output_bfd, &relocation, &insn);
114   if (status != bfd_reloc_other)
115     return status;
116
117   insn &= ~ (bfd_vma) 0x303fff;
118   insn |= (((relocation >> 2) & 0xc000) << 6) | ((relocation >> 2) & 0x3fff);
119   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
120
121   if ((bfd_signed_vma) relocation < - 0x40000
122       || (bfd_signed_vma) relocation > 0x3ffff)
123     return bfd_reloc_overflow;
124   else
125     return bfd_reloc_ok;
126 }
127
128 /* Handle the HIX22 reloc.  */
129
130 static bfd_reloc_status_type
131 sparc_elf_hix22_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
132                        PTR data, asection *input_section, bfd *output_bfd,
133                        char **error_message ATTRIBUTE_UNUSED)
134 {
135   bfd_vma relocation;
136   bfd_vma insn;
137   bfd_reloc_status_type status;
138
139   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
140                             input_section, output_bfd, &relocation, &insn);
141   if (status != bfd_reloc_other)
142     return status;
143
144   relocation ^= MINUS_ONE;
145   insn = (insn &~ (bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
146   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
147
148   if ((relocation & ~ (bfd_vma) 0xffffffff) != 0)
149     return bfd_reloc_overflow;
150   else
151     return bfd_reloc_ok;
152 }
153
154 /* Handle the LOX10 reloc.  */
155
156 static bfd_reloc_status_type
157 sparc_elf_lox10_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
158                        PTR data, asection *input_section, bfd *output_bfd,
159                        char **error_message ATTRIBUTE_UNUSED)
160 {
161   bfd_vma relocation;
162   bfd_vma insn;
163   bfd_reloc_status_type status;
164
165   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
166                             input_section, output_bfd, &relocation, &insn);
167   if (status != bfd_reloc_other)
168     return status;
169
170   insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff);
171   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
172
173   return bfd_reloc_ok;
174 }
175
176 static reloc_howto_type _bfd_sparc_elf_howto_table[] =
177 {
178   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
179   HOWTO(R_SPARC_8,         0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_8",       FALSE,0,0x000000ff,TRUE),
180   HOWTO(R_SPARC_16,        0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_16",      FALSE,0,0x0000ffff,TRUE),
181   HOWTO(R_SPARC_32,        0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_32",      FALSE,0,0xffffffff,TRUE),
182   HOWTO(R_SPARC_DISP8,     0,0, 8,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP8",   FALSE,0,0x000000ff,TRUE),
183   HOWTO(R_SPARC_DISP16,    0,1,16,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP16",  FALSE,0,0x0000ffff,TRUE),
184   HOWTO(R_SPARC_DISP32,    0,2,32,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP32",  FALSE,0,0xffffffff,TRUE),
185   HOWTO(R_SPARC_WDISP30,   2,2,30,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
186   HOWTO(R_SPARC_WDISP22,   2,2,22,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
187   HOWTO(R_SPARC_HI22,     10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HI22",    FALSE,0,0x003fffff,TRUE),
188   HOWTO(R_SPARC_22,        0,2,22,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_22",      FALSE,0,0x003fffff,TRUE),
189   HOWTO(R_SPARC_13,        0,2,13,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_13",      FALSE,0,0x00001fff,TRUE),
190   HOWTO(R_SPARC_LO10,      0,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LO10",    FALSE,0,0x000003ff,TRUE),
191   HOWTO(R_SPARC_GOT10,     0,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT10",   FALSE,0,0x000003ff,TRUE),
192   HOWTO(R_SPARC_GOT13,     0,2,13,FALSE,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_GOT13",   FALSE,0,0x00001fff,TRUE),
193   HOWTO(R_SPARC_GOT22,    10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT22",   FALSE,0,0x003fffff,TRUE),
194   HOWTO(R_SPARC_PC10,      0,2,10,TRUE, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC10",    FALSE,0,0x000003ff,TRUE),
195   HOWTO(R_SPARC_PC22,     10,2,22,TRUE, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PC22",    FALSE,0,0x003fffff,TRUE),
196   HOWTO(R_SPARC_WPLT30,    2,2,30,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WPLT30",  FALSE,0,0x3fffffff,TRUE),
197   HOWTO(R_SPARC_COPY,      0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_COPY",    FALSE,0,0x00000000,TRUE),
198   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),
199   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),
200   HOWTO(R_SPARC_RELATIVE,  0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
201   HOWTO(R_SPARC_UA32,      0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA32",    FALSE,0,0xffffffff,TRUE),
202   HOWTO(R_SPARC_PLT32,     0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PLT32",   FALSE,0,0xffffffff,TRUE),
203   HOWTO(R_SPARC_HIPLT22,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_HIPLT22",  FALSE,0,0x00000000,TRUE),
204   HOWTO(R_SPARC_LOPLT10,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_LOPLT10",  FALSE,0,0x00000000,TRUE),
205   HOWTO(R_SPARC_PCPLT32,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT32",  FALSE,0,0x00000000,TRUE),
206   HOWTO(R_SPARC_PCPLT22,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT22",  FALSE,0,0x00000000,TRUE),
207   HOWTO(R_SPARC_PCPLT10,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT10",  FALSE,0,0x00000000,TRUE),
208   HOWTO(R_SPARC_10,        0,2,10,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_10",      FALSE,0,0x000003ff,TRUE),
209   HOWTO(R_SPARC_11,        0,2,11,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_11",      FALSE,0,0x000007ff,TRUE),
210   HOWTO(R_SPARC_64,        0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_64",      FALSE,0,MINUS_ONE, TRUE),
211   HOWTO(R_SPARC_OLO10,     0,2,13,FALSE,0,complain_overflow_signed,  sparc_elf_notsup_reloc, "R_SPARC_OLO10",   FALSE,0,0x00001fff,TRUE),
212   HOWTO(R_SPARC_HH22,     42,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_HH22",    FALSE,0,0x003fffff,TRUE),
213   HOWTO(R_SPARC_HM10,     32,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HM10",    FALSE,0,0x000003ff,TRUE),
214   HOWTO(R_SPARC_LM22,     10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LM22",    FALSE,0,0x003fffff,TRUE),
215   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),
216   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),
217   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),
218   HOWTO(R_SPARC_WDISP16,   2,2,16,TRUE, 0,complain_overflow_signed,  sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", FALSE,0,0x00000000,TRUE),
219   HOWTO(R_SPARC_WDISP19,   2,2,19,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP19", FALSE,0,0x0007ffff,TRUE),
220   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),
221   HOWTO(R_SPARC_7,         0,2, 7,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_7",       FALSE,0,0x0000007f,TRUE),
222   HOWTO(R_SPARC_5,         0,2, 5,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_5",       FALSE,0,0x0000001f,TRUE),
223   HOWTO(R_SPARC_6,         0,2, 6,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_6",       FALSE,0,0x0000003f,TRUE),
224   HOWTO(R_SPARC_DISP64,    0,4,64,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP64",  FALSE,0,MINUS_ONE, TRUE),
225   HOWTO(R_SPARC_PLT64,     0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PLT64",   FALSE,0,MINUS_ONE, TRUE),
226   HOWTO(R_SPARC_HIX22,     0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,  "R_SPARC_HIX22",   FALSE,0,MINUS_ONE, FALSE),
227   HOWTO(R_SPARC_LOX10,     0,4, 0,FALSE,0,complain_overflow_dont,    sparc_elf_lox10_reloc,  "R_SPARC_LOX10",   FALSE,0,MINUS_ONE, FALSE),
228   HOWTO(R_SPARC_H44,      22,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_H44",     FALSE,0,0x003fffff,FALSE),
229   HOWTO(R_SPARC_M44,      12,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_M44",     FALSE,0,0x000003ff,FALSE),
230   HOWTO(R_SPARC_L44,       0,2,13,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_L44",     FALSE,0,0x00000fff,FALSE),
231   HOWTO(R_SPARC_REGISTER,  0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_REGISTER",FALSE,0,MINUS_ONE, FALSE),
232   HOWTO(R_SPARC_UA64,        0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA64",      FALSE,0,MINUS_ONE, TRUE),
233   HOWTO(R_SPARC_UA16,        0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA16",      FALSE,0,0x0000ffff,TRUE),
234   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),
235   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),
236   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),
237   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),
238   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),
239   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),
240   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),
241   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),
242   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),
243   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),
244   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),
245   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),
246   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),
247   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),
248   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),
249   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),
250   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),
251   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),
252   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),
253   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),
254   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),
255   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),
256   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),
257   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)
258 };
259 static reloc_howto_type sparc_vtinherit_howto =
260   HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", FALSE,0, 0, FALSE);
261 static reloc_howto_type sparc_vtentry_howto =
262   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);
263 static reloc_howto_type sparc_rev32_howto =
264   HOWTO(R_SPARC_REV32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", FALSE,0,0xffffffff,TRUE);
265
266 struct elf_reloc_map {
267   bfd_reloc_code_real_type bfd_reloc_val;
268   unsigned char elf_reloc_val;
269 };
270
271 static const struct elf_reloc_map sparc_reloc_map[] =
272 {
273   { BFD_RELOC_NONE, R_SPARC_NONE, },
274   { BFD_RELOC_16, R_SPARC_16, },
275   { BFD_RELOC_16_PCREL, R_SPARC_DISP16 },
276   { BFD_RELOC_8, R_SPARC_8 },
277   { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
278   { BFD_RELOC_CTOR, R_SPARC_64 },
279   { BFD_RELOC_32, R_SPARC_32 },
280   { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
281   { BFD_RELOC_HI22, R_SPARC_HI22 },
282   { BFD_RELOC_LO10, R_SPARC_LO10, },
283   { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
284   { BFD_RELOC_64_PCREL, R_SPARC_DISP64 },
285   { BFD_RELOC_SPARC22, R_SPARC_22 },
286   { BFD_RELOC_SPARC13, R_SPARC_13 },
287   { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
288   { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
289   { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
290   { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
291   { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
292   { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
293   { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
294   { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
295   { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
296   { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
297   { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
298   { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
299   { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
300   { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
301   { BFD_RELOC_SPARC_10, R_SPARC_10 },
302   { BFD_RELOC_SPARC_11, R_SPARC_11 },
303   { BFD_RELOC_SPARC_64, R_SPARC_64 },
304   { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
305   { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
306   { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
307   { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
308   { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
309   { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
310   { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
311   { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
312   { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
313   { BFD_RELOC_SPARC_7, R_SPARC_7 },
314   { BFD_RELOC_SPARC_5, R_SPARC_5 },
315   { BFD_RELOC_SPARC_6, R_SPARC_6 },
316   { BFD_RELOC_SPARC_DISP64, R_SPARC_DISP64 },
317   { BFD_RELOC_SPARC_TLS_GD_HI22, R_SPARC_TLS_GD_HI22 },
318   { BFD_RELOC_SPARC_TLS_GD_LO10, R_SPARC_TLS_GD_LO10 },
319   { BFD_RELOC_SPARC_TLS_GD_ADD, R_SPARC_TLS_GD_ADD },
320   { BFD_RELOC_SPARC_TLS_GD_CALL, R_SPARC_TLS_GD_CALL },
321   { BFD_RELOC_SPARC_TLS_LDM_HI22, R_SPARC_TLS_LDM_HI22 },
322   { BFD_RELOC_SPARC_TLS_LDM_LO10, R_SPARC_TLS_LDM_LO10 },
323   { BFD_RELOC_SPARC_TLS_LDM_ADD, R_SPARC_TLS_LDM_ADD },
324   { BFD_RELOC_SPARC_TLS_LDM_CALL, R_SPARC_TLS_LDM_CALL },
325   { BFD_RELOC_SPARC_TLS_LDO_HIX22, R_SPARC_TLS_LDO_HIX22 },
326   { BFD_RELOC_SPARC_TLS_LDO_LOX10, R_SPARC_TLS_LDO_LOX10 },
327   { BFD_RELOC_SPARC_TLS_LDO_ADD, R_SPARC_TLS_LDO_ADD },
328   { BFD_RELOC_SPARC_TLS_IE_HI22, R_SPARC_TLS_IE_HI22 },
329   { BFD_RELOC_SPARC_TLS_IE_LO10, R_SPARC_TLS_IE_LO10 },
330   { BFD_RELOC_SPARC_TLS_IE_LD, R_SPARC_TLS_IE_LD },
331   { BFD_RELOC_SPARC_TLS_IE_LDX, R_SPARC_TLS_IE_LDX },
332   { BFD_RELOC_SPARC_TLS_IE_ADD, R_SPARC_TLS_IE_ADD },
333   { BFD_RELOC_SPARC_TLS_LE_HIX22, R_SPARC_TLS_LE_HIX22 },
334   { BFD_RELOC_SPARC_TLS_LE_LOX10, R_SPARC_TLS_LE_LOX10 },
335   { BFD_RELOC_SPARC_TLS_DTPMOD32, R_SPARC_TLS_DTPMOD32 },
336   { BFD_RELOC_SPARC_TLS_DTPMOD64, R_SPARC_TLS_DTPMOD64 },
337   { BFD_RELOC_SPARC_TLS_DTPOFF32, R_SPARC_TLS_DTPOFF32 },
338   { BFD_RELOC_SPARC_TLS_DTPOFF64, R_SPARC_TLS_DTPOFF64 },
339   { BFD_RELOC_SPARC_TLS_TPOFF32, R_SPARC_TLS_TPOFF32 },
340   { BFD_RELOC_SPARC_TLS_TPOFF64, R_SPARC_TLS_TPOFF64 },
341   { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 },
342   { BFD_RELOC_SPARC_PLT64, R_SPARC_PLT64 },
343   { BFD_RELOC_SPARC_HIX22, R_SPARC_HIX22 },
344   { BFD_RELOC_SPARC_LOX10, R_SPARC_LOX10 },
345   { BFD_RELOC_SPARC_H44, R_SPARC_H44 },
346   { BFD_RELOC_SPARC_M44, R_SPARC_M44 },
347   { BFD_RELOC_SPARC_L44, R_SPARC_L44 },
348   { BFD_RELOC_SPARC_REGISTER, R_SPARC_REGISTER },
349   { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
350   { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
351   { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
352 };
353
354 reloc_howto_type *
355 _bfd_sparc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
356                                   bfd_reloc_code_real_type code)
357 {
358   unsigned int i;
359
360   switch (code)
361     {
362     case BFD_RELOC_VTABLE_INHERIT:
363       return &sparc_vtinherit_howto;
364
365     case BFD_RELOC_VTABLE_ENTRY:
366       return &sparc_vtentry_howto;
367
368     case BFD_RELOC_SPARC_REV32:
369       return &sparc_rev32_howto;
370
371     default:
372       for (i = 0;
373            i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map);
374            i++)
375         {
376           if (sparc_reloc_map[i].bfd_reloc_val == code)
377             return (_bfd_sparc_elf_howto_table
378                     + (int) sparc_reloc_map[i].elf_reloc_val);
379         }
380     }
381     bfd_set_error (bfd_error_bad_value);
382     return NULL;
383 }
384
385 reloc_howto_type *
386 _bfd_sparc_elf_info_to_howto_ptr (unsigned int r_type)
387 {
388   switch (r_type)
389     {
390     case R_SPARC_GNU_VTINHERIT:
391       return &sparc_vtinherit_howto;
392
393     case R_SPARC_GNU_VTENTRY:
394       return &sparc_vtentry_howto;
395
396     case R_SPARC_REV32:
397       return &sparc_rev32_howto;
398
399     default:
400       BFD_ASSERT (r_type < (unsigned int) R_SPARC_max_std);
401       return &_bfd_sparc_elf_howto_table[r_type];
402     }
403 }
404
405 /* Both 32-bit and 64-bit sparc encode this in an identical manner,
406    so just take advantage of that.  */
407 #define SPARC_ELF_R_TYPE(r_info)        \
408         ((r_info) & 0xff)
409
410 void
411 _bfd_sparc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
412                               Elf_Internal_Rela *dst)
413 {
414   unsigned int r_type = SPARC_ELF_R_TYPE (dst->r_info);
415
416   cache_ptr->howto = _bfd_sparc_elf_info_to_howto_ptr (r_type);
417 }
418 \f
419
420 /* The nop opcode we use.  */
421 #define SPARC_NOP 0x01000000
422
423 #define SPARC_INSN_BYTES        4
424
425 /* The SPARC linker needs to keep track of the number of relocs that it
426    decides to copy as dynamic relocs in check_relocs for each symbol.
427    This is so that it can later discard them if they are found to be
428    unnecessary.  We store the information in a field extending the
429    regular ELF linker hash table.  */
430
431 struct _bfd_sparc_elf_dyn_relocs
432 {
433   struct _bfd_sparc_elf_dyn_relocs *next;
434
435   /* The input section of the reloc.  */
436   asection *sec;
437
438   /* Total number of relocs copied for the input section.  */
439   bfd_size_type count;
440
441   /* Number of pc-relative relocs copied for the input section.  */
442   bfd_size_type pc_count;
443 };
444
445 /* SPARC ELF linker hash entry.  */
446
447 struct _bfd_sparc_elf_link_hash_entry
448 {
449   struct elf_link_hash_entry elf;
450
451   /* Track dynamic relocs copied for this symbol.  */
452   struct _bfd_sparc_elf_dyn_relocs *dyn_relocs;
453
454 #define GOT_UNKNOWN     0
455 #define GOT_NORMAL      1
456 #define GOT_TLS_GD      2
457 #define GOT_TLS_IE      3
458   unsigned char tls_type;
459 };
460
461 #define _bfd_sparc_elf_hash_entry(ent) ((struct _bfd_sparc_elf_link_hash_entry *)(ent))
462
463 struct _bfd_sparc_elf_obj_tdata
464 {
465   struct elf_obj_tdata root;
466
467   /* tls_type for each local got entry.  */
468   char *local_got_tls_type;
469
470   /* TRUE if TLS GD relocs has been seen for this object.  */
471   bfd_boolean has_tlsgd;
472 };
473
474 #define _bfd_sparc_elf_tdata(abfd) \
475   ((struct _bfd_sparc_elf_obj_tdata *) (abfd)->tdata.any)
476
477 #define _bfd_sparc_elf_local_got_tls_type(abfd) \
478   (_bfd_sparc_elf_tdata (abfd)->local_got_tls_type)
479
480 bfd_boolean
481 _bfd_sparc_elf_mkobject (bfd *abfd)
482 {
483   bfd_size_type amt = sizeof (struct _bfd_sparc_elf_obj_tdata);
484   abfd->tdata.any = bfd_zalloc (abfd, amt);
485   if (abfd->tdata.any == NULL)
486     return FALSE;
487   return TRUE;
488 }
489
490 static void
491 sparc_put_word_32 (bfd *bfd, bfd_vma val, void *ptr)
492 {
493   bfd_put_32 (bfd, val, ptr);
494 }
495
496 static void
497 sparc_put_word_64 (bfd *bfd, bfd_vma val, void *ptr)
498 {
499   bfd_put_64 (bfd, val, ptr);
500 }
501
502 static void
503 sparc_elf_append_rela_64 (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
504 {
505   Elf64_External_Rela *loc64;
506
507   loc64 = (Elf64_External_Rela *) s->contents;
508   loc64 += s->reloc_count++;
509   bfd_elf64_swap_reloca_out (abfd, rel, (bfd_byte *) loc64);
510 }
511
512 static void
513 sparc_elf_append_rela_32 (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
514 {
515   Elf32_External_Rela *loc32;
516
517   loc32 = (Elf32_External_Rela *) s->contents;
518   loc32 += s->reloc_count++;
519   bfd_elf32_swap_reloca_out (abfd, rel, (bfd_byte *) loc32);
520 }
521
522 static bfd_vma
523 sparc_elf_r_info_64 (Elf_Internal_Rela *in_rel, bfd_vma index, bfd_vma type)
524 {
525   return ELF64_R_INFO (index,
526                        (in_rel ?
527                         ELF64_R_TYPE_INFO (ELF64_R_TYPE_DATA (in_rel->r_info),
528                                            type) : type));
529 }
530
531 static bfd_vma
532 sparc_elf_r_info_32 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
533                      bfd_vma index, bfd_vma type)
534 {
535   return ELF32_R_INFO (index, type);
536 }
537
538 static bfd_vma
539 sparc_elf_r_symndx_64 (bfd_vma r_info)
540 {
541   bfd_vma r_symndx = ELF32_R_SYM (r_info);
542   return (r_symndx >> 24);
543 }
544
545 static bfd_vma
546 sparc_elf_r_symndx_32 (bfd_vma r_info)
547 {
548   return ELF32_R_SYM (r_info);
549 }
550
551 /* PLT/GOT stuff */
552
553 #define PLT32_ENTRY_SIZE 12
554 #define PLT32_HEADER_SIZE       (4 * PLT32_ENTRY_SIZE)
555
556 /* The first four entries in a 32-bit procedure linkage table are reserved,
557    and the initial contents are unimportant (we zero them out).
558    Subsequent entries look like this.  See the SVR4 ABI SPARC
559    supplement to see how this works.  */
560
561 /* sethi %hi(.-.plt0),%g1.  We fill in the address later.  */
562 #define PLT32_ENTRY_WORD0 0x03000000
563 /* b,a .plt0.  We fill in the offset later.  */
564 #define PLT32_ENTRY_WORD1 0x30800000
565 /* nop.  */
566 #define PLT32_ENTRY_WORD2 SPARC_NOP
567
568 static int
569 sparc32_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
570                          bfd_vma max ATTRIBUTE_UNUSED,
571                          bfd_vma *r_offset)
572 {
573       bfd_put_32 (output_bfd,
574                   PLT32_ENTRY_WORD0 + offset,
575                   splt->contents + offset);
576       bfd_put_32 (output_bfd,
577                   (PLT32_ENTRY_WORD1
578                    + (((- (offset + 4)) >> 2) & 0x3fffff)),
579                   splt->contents + offset + 4);
580       bfd_put_32 (output_bfd, (bfd_vma) PLT32_ENTRY_WORD2,
581                   splt->contents + offset + 8);
582
583       *r_offset = offset;
584
585       return offset / PLT32_ENTRY_SIZE - 4;
586 }
587
588 /* Both the headers and the entries are icache aligned.  */
589 #define PLT64_ENTRY_SIZE        32
590 #define PLT64_HEADER_SIZE       (4 * PLT64_ENTRY_SIZE)
591 #define PLT64_LARGE_THRESHOLD   32768
592
593 static int
594 sparc64_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
595                          bfd_vma max, bfd_vma *r_offset)
596 {
597   unsigned char *entry = splt->contents + offset;
598   const unsigned int nop = SPARC_NOP;
599   int index;
600
601   if (offset < (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
602     {
603       unsigned int sethi, ba;
604
605       *r_offset = offset;
606
607       index = (offset / PLT64_ENTRY_SIZE);
608
609       sethi = 0x03000000 | (index * PLT64_ENTRY_SIZE);
610       ba = 0x30680000
611         | (((splt->contents + PLT64_ENTRY_SIZE) - (entry + 4)) / 4 & 0x7ffff);
612
613       bfd_put_32 (output_bfd, (bfd_vma) sethi, entry);
614       bfd_put_32 (output_bfd, (bfd_vma) ba,    entry + 4);
615       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 8);
616       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 12);
617       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 16);
618       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 20);
619       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 24);
620       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 28);
621     }
622   else
623     {
624       unsigned char *ptr;
625       unsigned int ldx;
626       int block, last_block, ofs, last_ofs, chunks_this_block;
627       const int insn_chunk_size = (6 * 4);
628       const int ptr_chunk_size = (1 * 8);
629       const int entries_per_block = 160;
630       const int block_size = entries_per_block * (insn_chunk_size
631                                                   + ptr_chunk_size);
632
633       /* Entries 32768 and higher are grouped into blocks of 160.
634          The blocks are further subdivided into 160 sequences of
635          6 instructions and 160 pointers.  If a block does not require
636          the full 160 entries, let's say it requires N, then there
637          will be N sequences of 6 instructions and N pointers.  */
638
639       offset -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
640       max -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
641
642       block = offset / block_size;
643       last_block = max / block_size;
644       if (block != last_block)
645         {
646           chunks_this_block = 160;
647         }
648       else
649         {
650           last_ofs = max % block_size;
651           chunks_this_block = last_ofs / (insn_chunk_size + ptr_chunk_size);
652         }
653
654       ofs = offset % block_size;
655
656       index = (PLT64_LARGE_THRESHOLD +
657                (block * 160) +
658                (ofs / insn_chunk_size));
659
660       ptr = splt->contents
661         + (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
662         + (block * block_size)
663         + (chunks_this_block * insn_chunk_size)
664         + (ofs / insn_chunk_size) * ptr_chunk_size;
665
666       *r_offset = (bfd_vma) (ptr - splt->contents);
667
668       ldx = 0xc25be000 | ((ptr - (entry+4)) & 0x1fff);
669
670       /* mov %o7,%g5
671          call .+8
672          nop
673          ldx [%o7+P],%g1
674          jmpl %o7+%g1,%g1
675          mov %g5,%o7  */
676       bfd_put_32 (output_bfd, (bfd_vma) 0x8a10000f, entry);
677       bfd_put_32 (output_bfd, (bfd_vma) 0x40000002, entry + 4);
678       bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,  entry + 8);
679       bfd_put_32 (output_bfd, (bfd_vma) ldx,        entry + 12);
680       bfd_put_32 (output_bfd, (bfd_vma) 0x83c3c001, entry + 16);
681       bfd_put_32 (output_bfd, (bfd_vma) 0x9e100005, entry + 20);
682
683       bfd_put_64 (output_bfd, (bfd_vma) (splt->contents - (entry + 4)), ptr);
684     }
685
686   return index - 4;
687 }
688
689 #define SPARC_ELF_PUT_WORD(htab, bfd, val, ptr) \
690         htab->put_word(bfd, val, ptr)
691
692 #define SPARC_ELF_APPEND_RELA(htab, bfd, sec, rela)     \
693         htab->append_rela(bfd, sec, rela)
694
695 #define SPARC_ELF_R_INFO(htab, in_rel, index, type)     \
696         htab->r_info(in_rel, index, type)
697
698 #define SPARC_ELF_R_SYMNDX(htab, r_info)        \
699         htab->r_symndx(r_info)
700
701 #define SPARC_ELF_WORD_BYTES(htab)      \
702         htab->bytes_per_word
703
704 #define SPARC_ELF_RELA_BYTES(htab)      \
705         htab->bytes_per_rela
706
707 #define SPARC_ELF_DTPOFF_RELOC(htab)    \
708         htab->dtpoff_reloc
709
710 #define SPARC_ELF_DTPMOD_RELOC(htab)    \
711         htab->dtpmod_reloc
712
713 #define SPARC_ELF_TPOFF_RELOC(htab)     \
714         htab->tpoff_reloc
715
716 #define SPARC_ELF_BUILD_PLT_ENTRY(htab, obfd, splt, off, max, r_off) \
717         htab->build_plt_entry (obfd, splt, off, max, r_off)
718
719 /* Create an entry in an SPARC ELF linker hash table.  */
720
721 static struct bfd_hash_entry *
722 link_hash_newfunc (struct bfd_hash_entry *entry,
723                    struct bfd_hash_table *table, const char *string)
724 {
725   /* Allocate the structure if it has not already been allocated by a
726      subclass.  */
727   if (entry == NULL)
728     {
729       entry = bfd_hash_allocate (table,
730                                  sizeof (struct _bfd_sparc_elf_link_hash_entry));
731       if (entry == NULL)
732         return entry;
733     }
734
735   /* Call the allocation method of the superclass.  */
736   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
737   if (entry != NULL)
738     {
739       struct _bfd_sparc_elf_link_hash_entry *eh;
740
741       eh = (struct _bfd_sparc_elf_link_hash_entry *) entry;
742       eh->dyn_relocs = NULL;
743       eh->tls_type = GOT_UNKNOWN;
744     }
745
746   return entry;
747 }
748
749 /* The name of the dynamic interpreter.  This is put in the .interp
750    section.  */
751
752 #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
753 #define ELF64_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1"
754
755 /* Create a SPARC ELF linker hash table.  */
756
757 struct bfd_link_hash_table *
758 _bfd_sparc_elf_link_hash_table_create (bfd *abfd)
759 {
760   struct _bfd_sparc_elf_link_hash_table *ret;
761   bfd_size_type amt = sizeof (struct _bfd_sparc_elf_link_hash_table);
762
763   ret = (struct _bfd_sparc_elf_link_hash_table *) bfd_zmalloc (amt);
764   if (ret == NULL)
765     return NULL;
766
767   if (ABI_64_P (abfd))
768     {
769       ret->put_word = sparc_put_word_64;
770       ret->append_rela = sparc_elf_append_rela_64;
771       ret->r_info = sparc_elf_r_info_64;
772       ret->r_symndx = sparc_elf_r_symndx_64;
773       ret->build_plt_entry = sparc64_plt_entry_build;
774       ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF64;
775       ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD64;
776       ret->tpoff_reloc = R_SPARC_TLS_TPOFF64;
777       ret->word_align_power = 3;
778       ret->align_power_max = 4;
779       ret->bytes_per_word = 8;
780       ret->bytes_per_rela = sizeof (Elf64_External_Rela);
781       ret->dynamic_interpreter =
782         (const unsigned char *) ELF64_DYNAMIC_INTERPRETER;
783       ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
784     }
785   else
786     {
787       ret->put_word = sparc_put_word_32;
788       ret->append_rela = sparc_elf_append_rela_32;
789       ret->r_info = sparc_elf_r_info_32;
790       ret->r_symndx = sparc_elf_r_symndx_32;
791       ret->build_plt_entry = sparc32_plt_entry_build;
792       ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF32;
793       ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD32;
794       ret->tpoff_reloc = R_SPARC_TLS_TPOFF32;
795       ret->word_align_power = 2;
796       ret->align_power_max = 3;
797       ret->bytes_per_word = 4;
798       ret->bytes_per_rela = sizeof (Elf32_External_Rela);
799       ret->dynamic_interpreter =
800         (const unsigned char *) ELF32_DYNAMIC_INTERPRETER;
801       ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
802     }
803
804   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
805     {
806       free (ret);
807       return NULL;
808     }
809
810   return &ret->elf.root;
811 }
812
813 /* Create .got and .rela.got sections in DYNOBJ, and set up
814    shortcuts to them in our hash table.  */
815
816 static bfd_boolean
817 create_got_section (bfd *dynobj, struct bfd_link_info *info)
818 {
819   struct _bfd_sparc_elf_link_hash_table *htab;
820
821   if (! _bfd_elf_create_got_section (dynobj, info))
822     return FALSE;
823
824   htab = _bfd_sparc_elf_hash_table (info);
825   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
826   BFD_ASSERT (htab->sgot != NULL);
827
828   htab->srelgot = bfd_make_section (dynobj, ".rela.got");
829   if (htab->srelgot == NULL
830       || ! bfd_set_section_flags (dynobj, htab->srelgot, SEC_ALLOC
831                                                          | SEC_LOAD
832                                                          | SEC_HAS_CONTENTS
833                                                          | SEC_IN_MEMORY
834                                                          | SEC_LINKER_CREATED
835                                                          | SEC_READONLY)
836       || ! bfd_set_section_alignment (dynobj, htab->srelgot,
837                                       htab->word_align_power))
838     return FALSE;
839   return TRUE;
840 }
841
842 /* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and
843    .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
844    hash table.  */
845
846 bfd_boolean
847 _bfd_sparc_elf_create_dynamic_sections (bfd *dynobj,
848                                         struct bfd_link_info *info)
849 {
850   struct _bfd_sparc_elf_link_hash_table *htab;
851
852   htab = _bfd_sparc_elf_hash_table (info);
853   if (!htab->sgot && !create_got_section (dynobj, info))
854     return FALSE;
855
856   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
857     return FALSE;
858
859   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
860   htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
861   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
862   if (!info->shared)
863     htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
864
865   if (!htab->splt || !htab->srelplt || !htab->sdynbss
866       || (!info->shared && !htab->srelbss))
867     abort ();
868
869   return TRUE;
870 }
871
872 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
873
874 void
875 _bfd_sparc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
876                                      struct elf_link_hash_entry *dir,
877                                      struct elf_link_hash_entry *ind)
878 {
879   struct _bfd_sparc_elf_link_hash_entry *edir, *eind;
880
881   edir = (struct _bfd_sparc_elf_link_hash_entry *) dir;
882   eind = (struct _bfd_sparc_elf_link_hash_entry *) ind;
883
884   if (eind->dyn_relocs != NULL)
885     {
886       if (edir->dyn_relocs != NULL)
887         {
888           struct _bfd_sparc_elf_dyn_relocs **pp;
889           struct _bfd_sparc_elf_dyn_relocs *p;
890
891           if (ind->root.type == bfd_link_hash_indirect)
892             abort ();
893
894           /* Add reloc counts against the weak sym to the strong sym
895              list.  Merge any entries against the same section.  */
896           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
897             {
898               struct _bfd_sparc_elf_dyn_relocs *q;
899
900               for (q = edir->dyn_relocs; q != NULL; q = q->next)
901                 if (q->sec == p->sec)
902                   {
903                     q->pc_count += p->pc_count;
904                     q->count += p->count;
905                     *pp = p->next;
906                     break;
907                   }
908               if (q == NULL)
909                 pp = &p->next;
910             }
911           *pp = edir->dyn_relocs;
912         }
913
914       edir->dyn_relocs = eind->dyn_relocs;
915       eind->dyn_relocs = NULL;
916     }
917
918   if (ind->root.type == bfd_link_hash_indirect
919       && dir->got.refcount <= 0)
920     {
921       edir->tls_type = eind->tls_type;
922       eind->tls_type = GOT_UNKNOWN;
923     }
924   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
925 }
926
927 static int
928 sparc_elf_tls_transition (struct bfd_link_info *info, bfd *abfd,
929                           int r_type, int is_local)
930 {
931   if (! ABI_64_P (abfd)
932       && r_type == R_SPARC_TLS_GD_HI22
933       && ! _bfd_sparc_elf_tdata (abfd)->has_tlsgd)
934     r_type = R_SPARC_REV32;
935
936   if (info->shared)
937     return r_type;
938
939   switch (r_type)
940     {
941     case R_SPARC_TLS_GD_HI22:
942       if (is_local)
943         return R_SPARC_TLS_LE_HIX22;
944       return R_SPARC_TLS_IE_HI22;
945     case R_SPARC_TLS_GD_LO10:
946       if (is_local)
947         return R_SPARC_TLS_LE_LOX10;
948       return R_SPARC_TLS_IE_LO10;
949     case R_SPARC_TLS_IE_HI22:
950       if (is_local)
951         return R_SPARC_TLS_LE_HIX22;
952       return r_type;
953     case R_SPARC_TLS_IE_LO10:
954       if (is_local)
955         return R_SPARC_TLS_LE_LOX10;
956       return r_type;
957     case R_SPARC_TLS_LDM_HI22:
958       return R_SPARC_TLS_LE_HIX22;
959     case R_SPARC_TLS_LDM_LO10:
960       return R_SPARC_TLS_LE_LOX10;
961     }
962
963   return r_type;
964 }
965 \f
966 /* Look through the relocs for a section during the first phase, and
967    allocate space in the global offset table or procedure linkage
968    table.  */
969
970 bfd_boolean
971 _bfd_sparc_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
972                              asection *sec, const Elf_Internal_Rela *relocs)
973 {
974   struct _bfd_sparc_elf_link_hash_table *htab;
975   Elf_Internal_Shdr *symtab_hdr;
976   struct elf_link_hash_entry **sym_hashes;
977   bfd_vma *local_got_offsets;
978   const Elf_Internal_Rela *rel;
979   const Elf_Internal_Rela *rel_end;
980   asection *sreloc;
981   int num_relocs;
982   bfd_boolean checked_tlsgd = FALSE;
983
984   if (info->relocatable)
985     return TRUE;
986
987   htab = _bfd_sparc_elf_hash_table (info);
988   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
989   sym_hashes = elf_sym_hashes (abfd);
990   local_got_offsets = elf_local_got_offsets (abfd);
991
992   sreloc = NULL;
993
994   if (ABI_64_P (abfd))
995     num_relocs = NUM_SHDR_ENTRIES (& elf_section_data (sec)->rel_hdr);
996   else
997     num_relocs = sec->reloc_count;
998   rel_end = relocs + num_relocs;
999   for (rel = relocs; rel < rel_end; rel++)
1000     {
1001       unsigned int r_type;
1002       unsigned long r_symndx;
1003       struct elf_link_hash_entry *h;
1004
1005       r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
1006       r_type = SPARC_ELF_R_TYPE (rel->r_info);
1007
1008       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1009         {
1010           (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1011                                  abfd, r_symndx);
1012           return FALSE;
1013         }
1014
1015       if (r_symndx < symtab_hdr->sh_info)
1016         h = NULL;
1017       else
1018         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1019
1020       /* Compatibility with old R_SPARC_REV32 reloc conflicting
1021          with R_SPARC_TLS_GD_HI22.  */
1022       if (! ABI_64_P (abfd) && ! checked_tlsgd)
1023         switch (r_type)
1024           {
1025           case R_SPARC_TLS_GD_HI22:
1026             {
1027               const Elf_Internal_Rela *relt;
1028
1029               for (relt = rel + 1; relt < rel_end; relt++)
1030                 if (ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_LO10
1031                     || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_ADD
1032                     || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_CALL)
1033                   break;
1034               checked_tlsgd = TRUE;
1035               _bfd_sparc_elf_tdata (abfd)->has_tlsgd = relt < rel_end;
1036             }
1037             break;
1038           case R_SPARC_TLS_GD_LO10:
1039           case R_SPARC_TLS_GD_ADD:
1040           case R_SPARC_TLS_GD_CALL:
1041             checked_tlsgd = TRUE;
1042             _bfd_sparc_elf_tdata (abfd)->has_tlsgd = TRUE;
1043             break;
1044           }
1045
1046       r_type = sparc_elf_tls_transition (info, abfd, r_type, h == NULL);
1047       switch (r_type)
1048         {
1049         case R_SPARC_TLS_LDM_HI22:
1050         case R_SPARC_TLS_LDM_LO10:
1051           htab->tls_ldm_got.refcount += 1;
1052           break;
1053
1054         case R_SPARC_TLS_LE_HIX22:
1055         case R_SPARC_TLS_LE_LOX10:
1056           if (info->shared)
1057             goto r_sparc_plt32;
1058           break;
1059
1060         case R_SPARC_TLS_IE_HI22:
1061         case R_SPARC_TLS_IE_LO10:
1062           if (info->shared)
1063             info->flags |= DF_STATIC_TLS;
1064           /* Fall through */
1065
1066         case R_SPARC_GOT10:
1067         case R_SPARC_GOT13:
1068         case R_SPARC_GOT22:
1069         case R_SPARC_TLS_GD_HI22:
1070         case R_SPARC_TLS_GD_LO10:
1071           /* This symbol requires a global offset table entry.  */
1072           {
1073             int tls_type, old_tls_type;
1074
1075             switch (r_type)
1076               {
1077               default:
1078               case R_SPARC_GOT10:
1079               case R_SPARC_GOT13:
1080               case R_SPARC_GOT22:
1081                 tls_type = GOT_NORMAL;
1082                 break;
1083               case R_SPARC_TLS_GD_HI22:
1084               case R_SPARC_TLS_GD_LO10:
1085                 tls_type = GOT_TLS_GD;
1086                 break;
1087               case R_SPARC_TLS_IE_HI22:
1088               case R_SPARC_TLS_IE_LO10:
1089                 tls_type = GOT_TLS_IE;
1090                 break;
1091               }
1092
1093             if (h != NULL)
1094               {
1095                 h->got.refcount += 1;
1096                 old_tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
1097               }
1098             else
1099               {
1100                 bfd_signed_vma *local_got_refcounts;
1101
1102                 /* This is a global offset table entry for a local symbol.  */
1103                 local_got_refcounts = elf_local_got_refcounts (abfd);
1104                 if (local_got_refcounts == NULL)
1105                   {
1106                     bfd_size_type size;
1107
1108                     size = symtab_hdr->sh_info;
1109                     size *= (sizeof (bfd_signed_vma) + sizeof(char));
1110                     local_got_refcounts = ((bfd_signed_vma *)
1111                                            bfd_zalloc (abfd, size));
1112                     if (local_got_refcounts == NULL)
1113                       return FALSE;
1114                     elf_local_got_refcounts (abfd) = local_got_refcounts;
1115                     _bfd_sparc_elf_local_got_tls_type (abfd)
1116                       = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1117                   }
1118                 local_got_refcounts[r_symndx] += 1;
1119                 old_tls_type = _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx];
1120               }
1121
1122             /* If a TLS symbol is accessed using IE at least once,
1123                there is no point to use dynamic model for it.  */
1124             if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1125                 && (old_tls_type != GOT_TLS_GD
1126                     || tls_type != GOT_TLS_IE))
1127               {
1128                 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
1129                   tls_type = old_tls_type;
1130                 else
1131                   {
1132                     (*_bfd_error_handler)
1133                       (_("%B: `%s' accessed both as normal and thread local symbol"),
1134                        abfd, h ? h->root.root.string : "<local>");
1135                     return FALSE;
1136                   }
1137               }
1138
1139             if (old_tls_type != tls_type)
1140               {
1141                 if (h != NULL)
1142                   _bfd_sparc_elf_hash_entry (h)->tls_type = tls_type;
1143                 else
1144                   _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
1145               }
1146           }
1147
1148           if (htab->sgot == NULL)
1149             {
1150               if (htab->elf.dynobj == NULL)
1151                 htab->elf.dynobj = abfd;
1152               if (!create_got_section (htab->elf.dynobj, info))
1153                 return FALSE;
1154             }
1155           break;
1156
1157         case R_SPARC_TLS_GD_CALL:
1158         case R_SPARC_TLS_LDM_CALL:
1159           if (info->shared)
1160             {
1161               /* These are basically R_SPARC_TLS_WPLT30 relocs against
1162                  __tls_get_addr.  */
1163               struct bfd_link_hash_entry *bh = NULL;
1164               if (! _bfd_generic_link_add_one_symbol (info, abfd,
1165                                                       "__tls_get_addr", 0,
1166                                                       bfd_und_section_ptr, 0,
1167                                                       NULL, FALSE, FALSE,
1168                                                       &bh))
1169                 return FALSE;
1170               h = (struct elf_link_hash_entry *) bh;
1171             }
1172           else
1173             break;
1174           /* Fall through */
1175
1176         case R_SPARC_PLT32:
1177         case R_SPARC_WPLT30:
1178         case R_SPARC_HIPLT22:
1179         case R_SPARC_LOPLT10:
1180         case R_SPARC_PCPLT32:
1181         case R_SPARC_PCPLT22:
1182         case R_SPARC_PCPLT10:
1183         case R_SPARC_PLT64:
1184           /* This symbol requires a procedure linkage table entry.  We
1185              actually build the entry in adjust_dynamic_symbol,
1186              because this might be a case of linking PIC code without
1187              linking in any dynamic objects, in which case we don't
1188              need to generate a procedure linkage table after all.  */
1189
1190           if (h == NULL)
1191             {
1192               if (! ABI_64_P (abfd))
1193                 {
1194                   /* The Solaris native assembler will generate a WPLT30
1195                      reloc for a local symbol if you assemble a call from
1196                      one section to another when using -K pic.  We treat
1197                      it as WDISP30.  */
1198                   if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32)
1199                     goto r_sparc_plt32;
1200                   break;
1201                 }
1202
1203               /* It does not make sense to have a procedure linkage
1204                  table entry for a local symbol.  */
1205               bfd_set_error (bfd_error_bad_value);
1206               return FALSE;
1207             }
1208
1209           h->needs_plt = 1;
1210
1211           {
1212             int this_r_type;
1213
1214             this_r_type = SPARC_ELF_R_TYPE (rel->r_info);
1215             if (this_r_type == R_SPARC_PLT32
1216                 || this_r_type == R_SPARC_PLT64)
1217               goto r_sparc_plt32;
1218           }
1219           h->plt.refcount += 1;
1220           break;
1221
1222         case R_SPARC_PC10:
1223         case R_SPARC_PC22:
1224         case R_SPARC_PC_HH22:
1225         case R_SPARC_PC_HM10:
1226         case R_SPARC_PC_LM22:
1227           if (h != NULL)
1228             h->non_got_ref = 1;
1229
1230           if (h != NULL
1231               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1232             break;
1233           /* Fall through.  */
1234
1235         case R_SPARC_DISP8:
1236         case R_SPARC_DISP16:
1237         case R_SPARC_DISP32:
1238         case R_SPARC_DISP64:
1239         case R_SPARC_WDISP30:
1240         case R_SPARC_WDISP22:
1241         case R_SPARC_WDISP19:
1242         case R_SPARC_WDISP16:
1243         case R_SPARC_8:
1244         case R_SPARC_16:
1245         case R_SPARC_32:
1246         case R_SPARC_HI22:
1247         case R_SPARC_22:
1248         case R_SPARC_13:
1249         case R_SPARC_LO10:
1250         case R_SPARC_UA16:
1251         case R_SPARC_UA32:
1252         case R_SPARC_10:
1253         case R_SPARC_11:
1254         case R_SPARC_64:
1255         case R_SPARC_OLO10:
1256         case R_SPARC_HH22:
1257         case R_SPARC_HM10:
1258         case R_SPARC_LM22:
1259         case R_SPARC_7:
1260         case R_SPARC_5:
1261         case R_SPARC_6:
1262         case R_SPARC_HIX22:
1263         case R_SPARC_LOX10:
1264         case R_SPARC_H44:
1265         case R_SPARC_M44:
1266         case R_SPARC_L44:
1267         case R_SPARC_UA64:
1268           if (h != NULL)
1269             h->non_got_ref = 1;
1270
1271         r_sparc_plt32:
1272           if (h != NULL && !info->shared)
1273             {
1274               /* We may need a .plt entry if the function this reloc
1275                  refers to is in a shared lib.  */
1276               h->plt.refcount += 1;
1277             }
1278
1279           /* If we are creating a shared library, and this is a reloc
1280              against a global symbol, or a non PC relative reloc
1281              against a local symbol, then we need to copy the reloc
1282              into the shared library.  However, if we are linking with
1283              -Bsymbolic, we do not need to copy a reloc against a
1284              global symbol which is defined in an object we are
1285              including in the link (i.e., DEF_REGULAR is set).  At
1286              this point we have not seen all the input files, so it is
1287              possible that DEF_REGULAR is not set now but will be set
1288              later (it is never cleared).  In case of a weak definition,
1289              DEF_REGULAR may be cleared later by a strong definition in
1290              a shared library.  We account for that possibility below by
1291              storing information in the relocs_copied field of the hash
1292              table entry.  A similar situation occurs when creating
1293              shared libraries and symbol visibility changes render the
1294              symbol local.
1295
1296              If on the other hand, we are creating an executable, we
1297              may need to keep relocations for symbols satisfied by a
1298              dynamic library if we manage to avoid copy relocs for the
1299              symbol.  */
1300           if ((info->shared
1301                && (sec->flags & SEC_ALLOC) != 0
1302                && (! _bfd_sparc_elf_howto_table[r_type].pc_relative
1303                    || (h != NULL
1304                        && (! info->symbolic
1305                            || h->root.type == bfd_link_hash_defweak
1306                            || !h->def_regular))))
1307               || (!info->shared
1308                   && (sec->flags & SEC_ALLOC) != 0
1309                   && h != NULL
1310                   && (h->root.type == bfd_link_hash_defweak
1311                       || !h->def_regular)))
1312             {
1313               struct _bfd_sparc_elf_dyn_relocs *p;
1314               struct _bfd_sparc_elf_dyn_relocs **head;
1315
1316               /* When creating a shared object, we must copy these
1317                  relocs into the output file.  We create a reloc
1318                  section in dynobj and make room for the reloc.  */
1319               if (sreloc == NULL)
1320                 {
1321                   const char *name;
1322                   bfd *dynobj;
1323
1324                   name = (bfd_elf_string_from_elf_section
1325                           (abfd,
1326                            elf_elfheader (abfd)->e_shstrndx,
1327                            elf_section_data (sec)->rel_hdr.sh_name));
1328                   if (name == NULL)
1329                     return FALSE;
1330
1331                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1332                               && strcmp (bfd_get_section_name (abfd, sec),
1333                                          name + 5) == 0);
1334
1335                   if (htab->elf.dynobj == NULL)
1336                     htab->elf.dynobj = abfd;
1337                   dynobj = htab->elf.dynobj;
1338
1339                   sreloc = bfd_get_section_by_name (dynobj, name);
1340                   if (sreloc == NULL)
1341                     {
1342                       flagword flags;
1343
1344                       sreloc = bfd_make_section (dynobj, name);
1345                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
1346                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1347                       if ((sec->flags & SEC_ALLOC) != 0)
1348                         flags |= SEC_ALLOC | SEC_LOAD;
1349                       if (sreloc == NULL
1350                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
1351                           || ! bfd_set_section_alignment (dynobj, sreloc,
1352                                                           htab->word_align_power))
1353                         return FALSE;
1354                     }
1355                   elf_section_data (sec)->sreloc = sreloc;
1356                 }
1357
1358               /* If this is a global symbol, we count the number of
1359                  relocations we need for this symbol.  */
1360               if (h != NULL)
1361                 head = &((struct _bfd_sparc_elf_link_hash_entry *) h)->dyn_relocs;
1362               else
1363                 {
1364                   /* Track dynamic relocs needed for local syms too.
1365                      We really need local syms available to do this
1366                      easily.  Oh well.  */
1367
1368                   asection *s;
1369                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1370                                                  sec, r_symndx);
1371                   if (s == NULL)
1372                     return FALSE;
1373
1374                   head = ((struct _bfd_sparc_elf_dyn_relocs **)
1375                           &elf_section_data (s)->local_dynrel);
1376                 }
1377
1378               p = *head;
1379               if (p == NULL || p->sec != sec)
1380                 {
1381                   bfd_size_type amt = sizeof *p;
1382                   p = ((struct _bfd_sparc_elf_dyn_relocs *)
1383                        bfd_alloc (htab->elf.dynobj, amt));
1384                   if (p == NULL)
1385                     return FALSE;
1386                   p->next = *head;
1387                   *head = p;
1388                   p->sec = sec;
1389                   p->count = 0;
1390                   p->pc_count = 0;
1391                 }
1392
1393               p->count += 1;
1394               if (_bfd_sparc_elf_howto_table[r_type].pc_relative)
1395                 p->pc_count += 1;
1396             }
1397
1398           break;
1399
1400         case R_SPARC_GNU_VTINHERIT:
1401           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1402             return FALSE;
1403           break;
1404
1405         case R_SPARC_GNU_VTENTRY:
1406           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1407             return FALSE;
1408           break;
1409
1410         case R_SPARC_REGISTER:
1411           /* Nothing to do.  */
1412           break;
1413
1414         default:
1415           break;
1416         }
1417     }
1418
1419   return TRUE;
1420 }
1421 \f
1422 asection *
1423 _bfd_sparc_elf_gc_mark_hook (asection *sec,
1424                              struct bfd_link_info *info,
1425                              Elf_Internal_Rela *rel,
1426                              struct elf_link_hash_entry *h,
1427                              Elf_Internal_Sym *sym)
1428 {
1429   if (h != NULL)
1430     {
1431       struct _bfd_sparc_elf_link_hash_table *htab;
1432
1433       htab = _bfd_sparc_elf_hash_table (info);
1434       switch (SPARC_ELF_R_TYPE (rel->r_info))
1435       {
1436       case R_SPARC_GNU_VTINHERIT:
1437       case R_SPARC_GNU_VTENTRY:
1438         break;
1439
1440       default:
1441         switch (h->root.type)
1442           {
1443           case bfd_link_hash_defined:
1444           case bfd_link_hash_defweak:
1445             return h->root.u.def.section;
1446
1447           case bfd_link_hash_common:
1448             return h->root.u.c.p->section;
1449
1450           default:
1451             break;
1452           }
1453       }
1454     }
1455   else
1456     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1457
1458   return NULL;
1459 }
1460
1461 /* Update the got entry reference counts for the section being removed.  */
1462 bfd_boolean
1463 _bfd_sparc_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1464                               asection *sec, const Elf_Internal_Rela *relocs)
1465 {
1466   struct _bfd_sparc_elf_link_hash_table *htab;
1467   Elf_Internal_Shdr *symtab_hdr;
1468   struct elf_link_hash_entry **sym_hashes;
1469   bfd_signed_vma *local_got_refcounts;
1470   const Elf_Internal_Rela *rel, *relend;
1471
1472   elf_section_data (sec)->local_dynrel = NULL;
1473
1474   htab = _bfd_sparc_elf_hash_table (info);
1475   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1476   sym_hashes = elf_sym_hashes (abfd);
1477   local_got_refcounts = elf_local_got_refcounts (abfd);
1478
1479   relend = relocs + sec->reloc_count;
1480   for (rel = relocs; rel < relend; rel++)
1481     {
1482       unsigned long r_symndx;
1483       unsigned int r_type;
1484       struct elf_link_hash_entry *h = NULL;
1485
1486       r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
1487       if (r_symndx >= symtab_hdr->sh_info)
1488         {
1489           struct _bfd_sparc_elf_link_hash_entry *eh;
1490           struct _bfd_sparc_elf_dyn_relocs **pp;
1491           struct _bfd_sparc_elf_dyn_relocs *p;
1492
1493           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1494           while (h->root.type == bfd_link_hash_indirect
1495                  || h->root.type == bfd_link_hash_warning)
1496             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1497           eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
1498           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1499             if (p->sec == sec)
1500               {
1501                 /* Everything must go for SEC.  */
1502                 *pp = p->next;
1503                 break;
1504               }
1505         }
1506
1507       r_type = SPARC_ELF_R_TYPE (rel->r_info);
1508       r_type = sparc_elf_tls_transition (info, abfd, r_type, h != NULL);
1509       switch (r_type)
1510         {
1511         case R_SPARC_TLS_LDM_HI22:
1512         case R_SPARC_TLS_LDM_LO10:
1513           if (_bfd_sparc_elf_hash_table (info)->tls_ldm_got.refcount > 0)
1514             _bfd_sparc_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
1515           break;
1516
1517         case R_SPARC_TLS_GD_HI22:
1518         case R_SPARC_TLS_GD_LO10:
1519         case R_SPARC_TLS_IE_HI22:
1520         case R_SPARC_TLS_IE_LO10:
1521         case R_SPARC_GOT10:
1522         case R_SPARC_GOT13:
1523         case R_SPARC_GOT22:
1524           if (h != NULL)
1525             {
1526               if (h->got.refcount > 0)
1527                 h->got.refcount--;
1528             }
1529           else
1530             {
1531               if (local_got_refcounts[r_symndx] > 0)
1532                 local_got_refcounts[r_symndx]--;
1533             }
1534           break;
1535
1536         case R_SPARC_PC10:
1537         case R_SPARC_PC22:
1538         case R_SPARC_PC_HH22:
1539         case R_SPARC_PC_HM10:
1540         case R_SPARC_PC_LM22:
1541           if (h != NULL
1542               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1543             break;
1544           /* Fall through.  */
1545
1546         case R_SPARC_DISP8:
1547         case R_SPARC_DISP16:
1548         case R_SPARC_DISP32:
1549         case R_SPARC_DISP64:
1550         case R_SPARC_WDISP30:
1551         case R_SPARC_WDISP22:
1552         case R_SPARC_WDISP19:
1553         case R_SPARC_WDISP16:
1554         case R_SPARC_8:
1555         case R_SPARC_16:
1556         case R_SPARC_32:
1557         case R_SPARC_HI22:
1558         case R_SPARC_22:
1559         case R_SPARC_13:
1560         case R_SPARC_LO10:
1561         case R_SPARC_UA16:
1562         case R_SPARC_UA32:
1563         case R_SPARC_PLT32:
1564         case R_SPARC_10:
1565         case R_SPARC_11:
1566         case R_SPARC_64:
1567         case R_SPARC_OLO10:
1568         case R_SPARC_HH22:
1569         case R_SPARC_HM10:
1570         case R_SPARC_LM22:
1571         case R_SPARC_7:
1572         case R_SPARC_5:
1573         case R_SPARC_6:
1574         case R_SPARC_HIX22:
1575         case R_SPARC_LOX10:
1576         case R_SPARC_H44:
1577         case R_SPARC_M44:
1578         case R_SPARC_L44:
1579         case R_SPARC_UA64:
1580           if (info->shared)
1581             break;
1582           /* Fall through.  */
1583
1584         case R_SPARC_WPLT30:
1585           if (h != NULL)
1586             {
1587               if (h->plt.refcount > 0)
1588                 h->plt.refcount--;
1589             }
1590           break;
1591
1592         default:
1593           break;
1594         }
1595     }
1596
1597   return TRUE;
1598 }
1599
1600 /* Adjust a symbol defined by a dynamic object and referenced by a
1601    regular object.  The current definition is in some section of the
1602    dynamic object, but we're not including those sections.  We have to
1603    change the definition to something the rest of the link can
1604    understand.  */
1605
1606 bfd_boolean
1607 _bfd_sparc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1608                                      struct elf_link_hash_entry *h)
1609 {
1610   struct _bfd_sparc_elf_link_hash_table *htab;
1611   struct _bfd_sparc_elf_link_hash_entry * eh;
1612   struct _bfd_sparc_elf_dyn_relocs *p;
1613   asection *s;
1614   unsigned int power_of_two;
1615
1616   htab = _bfd_sparc_elf_hash_table (info);
1617
1618   /* Make sure we know what is going on here.  */
1619   BFD_ASSERT (htab->elf.dynobj != NULL
1620               && (h->needs_plt
1621                   || h->u.weakdef != NULL
1622                   || (h->def_dynamic
1623                       && h->ref_regular
1624                       && !h->def_regular)));
1625
1626   /* If this is a function, put it in the procedure linkage table.  We
1627      will fill in the contents of the procedure linkage table later
1628      (although we could actually do it here).  The STT_NOTYPE
1629      condition is a hack specifically for the Oracle libraries
1630      delivered for Solaris; for some inexplicable reason, they define
1631      some of their functions as STT_NOTYPE when they really should be
1632      STT_FUNC.  */
1633   if (h->type == STT_FUNC
1634       || h->needs_plt
1635       || (h->type == STT_NOTYPE
1636           && (h->root.type == bfd_link_hash_defined
1637               || h->root.type == bfd_link_hash_defweak)
1638           && (h->root.u.def.section->flags & SEC_CODE) != 0))
1639     {
1640       if (h->plt.refcount <= 0
1641           || (! info->shared
1642               && !h->def_dynamic
1643               && !h->ref_dynamic
1644               && h->root.type != bfd_link_hash_undefweak
1645               && h->root.type != bfd_link_hash_undefined))
1646         {
1647           /* This case can occur if we saw a WPLT30 reloc in an input
1648              file, but the symbol was never referred to by a dynamic
1649              object, or if all references were garbage collected.  In
1650              such a case, we don't actually need to build a procedure
1651              linkage table, and we can just do a WDISP30 reloc instead.  */
1652           h->plt.offset = (bfd_vma) -1;
1653           h->needs_plt = 0;
1654         }
1655
1656       return TRUE;
1657     }
1658   else
1659     h->plt.offset = (bfd_vma) -1;
1660
1661   /* If this is a weak symbol, and there is a real definition, the
1662      processor independent code will have arranged for us to see the
1663      real definition first, and we can just use the same value.  */
1664   if (h->u.weakdef != NULL)
1665     {
1666       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1667                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1668       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1669       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1670       return TRUE;
1671     }
1672
1673   /* This is a reference to a symbol defined by a dynamic object which
1674      is not a function.  */
1675
1676   /* If we are creating a shared library, we must presume that the
1677      only references to the symbol are via the global offset table.
1678      For such cases we need not do anything here; the relocations will
1679      be handled correctly by relocate_section.  */
1680   if (info->shared)
1681     return TRUE;
1682
1683   /* If there are no references to this symbol that do not use the
1684      GOT, we don't need to generate a copy reloc.  */
1685   if (!h->non_got_ref)
1686     return TRUE;
1687
1688   eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
1689   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1690     {
1691       s = p->sec->output_section;
1692       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1693         break;
1694     }
1695
1696   /* If we didn't find any dynamic relocs in read-only sections, then
1697      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1698   if (p == NULL)
1699     {
1700       h->non_got_ref = 0;
1701       return TRUE;
1702     }
1703
1704   /* We must allocate the symbol in our .dynbss section, which will
1705      become part of the .bss section of the executable.  There will be
1706      an entry for this symbol in the .dynsym section.  The dynamic
1707      object will contain position independent code, so all references
1708      from the dynamic object to this symbol will go through the global
1709      offset table.  The dynamic linker will use the .dynsym entry to
1710      determine the address it must put in the global offset table, so
1711      both the dynamic object and the regular object will refer to the
1712      same memory location for the variable.  */
1713
1714   /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
1715      to copy the initial value out of the dynamic object and into the
1716      runtime process image.  We need to remember the offset into the
1717      .rel.bss section we are going to use.  */
1718   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1719     {
1720       htab->srelbss->size += SPARC_ELF_RELA_BYTES (htab);
1721       h->needs_copy = 1;
1722     }
1723
1724   /* We need to figure out the alignment required for this symbol.  I
1725      have no idea how ELF linkers handle this.  */
1726   power_of_two = bfd_log2 (h->size);
1727   if (power_of_two > htab->align_power_max)
1728     power_of_two = htab->align_power_max;
1729
1730   /* Apply the required alignment.  */
1731   s = htab->sdynbss;
1732   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1733   if (power_of_two > bfd_get_section_alignment (dynobj, s))
1734     {
1735       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1736         return FALSE;
1737     }
1738
1739   /* Define the symbol as being at this point in the section.  */
1740   h->root.u.def.section = s;
1741   h->root.u.def.value = s->size;
1742
1743   /* Increment the section size to make room for the symbol.  */
1744   s->size += h->size;
1745
1746   return TRUE;
1747 }
1748
1749 /* Allocate space in .plt, .got and associated reloc sections for
1750    dynamic relocs.  */
1751
1752 static bfd_boolean
1753 allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
1754 {
1755   struct bfd_link_info *info;
1756   struct _bfd_sparc_elf_link_hash_table *htab;
1757   struct _bfd_sparc_elf_link_hash_entry *eh;
1758   struct _bfd_sparc_elf_dyn_relocs *p;
1759
1760   if (h->root.type == bfd_link_hash_indirect)
1761     return TRUE;
1762
1763   if (h->root.type == bfd_link_hash_warning)
1764     /* When warning symbols are created, they **replace** the "real"
1765        entry in the hash table, thus we never get to see the real
1766        symbol in a hash traversal.  So look at it now.  */
1767     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1768
1769   info = (struct bfd_link_info *) inf;
1770   htab = _bfd_sparc_elf_hash_table (info);
1771
1772   if (htab->elf.dynamic_sections_created
1773       && h->plt.refcount > 0)
1774     {
1775       /* Make sure this symbol is output as a dynamic symbol.
1776          Undefined weak syms won't yet be marked as dynamic.  */
1777       if (h->dynindx == -1
1778           && !h->forced_local)
1779         {
1780           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1781             return FALSE;
1782         }
1783
1784       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
1785         {
1786           asection *s = htab->splt;
1787
1788           /* The first four entries in .plt is reserved.  */
1789           if (s->size == 0)
1790             s->size = (SPARC_ELF_WORD_BYTES(htab) == 8 ?
1791                        PLT64_HEADER_SIZE : PLT32_HEADER_SIZE);
1792
1793           /* The procedure linkage table size is bounded by the magnitude
1794              of the offset we can describe in the entry.  */
1795           if (s->size >= (SPARC_ELF_WORD_BYTES(htab) == 8 ?
1796                           (((bfd_vma)1 << 31) << 1) : 0x400000))
1797             {
1798               bfd_set_error (bfd_error_bad_value);
1799               return FALSE;
1800             }
1801
1802           if (SPARC_ELF_WORD_BYTES(htab) == 8
1803               && s->size >= PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
1804             {
1805               bfd_vma off = s->size - PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE;
1806
1807
1808               off = (off % (160 * PLT64_ENTRY_SIZE)) / PLT64_ENTRY_SIZE;
1809
1810               h->plt.offset = (s->size - (off * 8));
1811             }
1812           else
1813             h->plt.offset = s->size;
1814
1815           /* If this symbol is not defined in a regular file, and we are
1816              not generating a shared library, then set the symbol to this
1817              location in the .plt.  This is required to make function
1818              pointers compare as equal between the normal executable and
1819              the shared library.  */
1820           if (! info->shared
1821               && !h->def_regular)
1822             {
1823               h->root.u.def.section = s;
1824               h->root.u.def.value = h->plt.offset;
1825             }
1826
1827           /* Make room for this entry.  */
1828           s->size += (SPARC_ELF_WORD_BYTES(htab) == 8 ?
1829                       PLT64_ENTRY_SIZE : PLT32_ENTRY_SIZE);
1830
1831           /* We also need to make an entry in the .rela.plt section.  */
1832           htab->srelplt->size += SPARC_ELF_RELA_BYTES (htab);
1833         }
1834       else
1835         {
1836           h->plt.offset = (bfd_vma) -1;
1837           h->needs_plt = 0;
1838         }
1839     }
1840   else
1841     {
1842       h->plt.offset = (bfd_vma) -1;
1843       h->needs_plt = 0;
1844     }
1845
1846   /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary,
1847      make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry.  */
1848   if (h->got.refcount > 0
1849       && !info->shared
1850       && h->dynindx == -1
1851       && _bfd_sparc_elf_hash_entry(h)->tls_type == GOT_TLS_IE)
1852     h->got.offset = (bfd_vma) -1;
1853   else if (h->got.refcount > 0)
1854     {
1855       asection *s;
1856       bfd_boolean dyn;
1857       int tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
1858
1859       /* Make sure this symbol is output as a dynamic symbol.
1860          Undefined weak syms won't yet be marked as dynamic.  */
1861       if (h->dynindx == -1
1862           && !h->forced_local)
1863         {
1864           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1865             return FALSE;
1866         }
1867
1868       s = htab->sgot;
1869       h->got.offset = s->size;
1870       s->size += SPARC_ELF_WORD_BYTES (htab);
1871       /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots.  */
1872       if (tls_type == GOT_TLS_GD)
1873         s->size += SPARC_ELF_WORD_BYTES (htab);
1874       dyn = htab->elf.dynamic_sections_created;
1875       /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation,
1876          R_SPARC_TLS_GD_{HI22,LO10} needs one if local symbol and two if
1877          global.  */
1878       if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1879           || tls_type == GOT_TLS_IE)
1880         htab->srelgot->size += SPARC_ELF_RELA_BYTES (htab);
1881       else if (tls_type == GOT_TLS_GD)
1882         htab->srelgot->size += 2 * SPARC_ELF_RELA_BYTES (htab);
1883       else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
1884         htab->srelgot->size += SPARC_ELF_RELA_BYTES (htab);
1885     }
1886   else
1887     h->got.offset = (bfd_vma) -1;
1888
1889   eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
1890   if (eh->dyn_relocs == NULL)
1891     return TRUE;
1892
1893   /* In the shared -Bsymbolic case, discard space allocated for
1894      dynamic pc-relative relocs against symbols which turn out to be
1895      defined in regular objects.  For the normal shared case, discard
1896      space for pc-relative relocs that have become local due to symbol
1897      visibility changes.  */
1898
1899   if (info->shared)
1900     {
1901       if (h->def_regular
1902           && (h->forced_local
1903               || info->symbolic))
1904         {
1905           struct _bfd_sparc_elf_dyn_relocs **pp;
1906
1907           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1908             {
1909               p->count -= p->pc_count;
1910               p->pc_count = 0;
1911               if (p->count == 0)
1912                 *pp = p->next;
1913               else
1914                 pp = &p->next;
1915             }
1916         }
1917     }
1918   else
1919     {
1920       /* For the non-shared case, discard space for relocs against
1921          symbols which turn out to need copy relocs or are not
1922          dynamic.  */
1923
1924       if (!h->non_got_ref
1925           && ((h->def_dynamic
1926                && !h->def_regular)
1927               || (htab->elf.dynamic_sections_created
1928                   && (h->root.type == bfd_link_hash_undefweak
1929                       || h->root.type == bfd_link_hash_undefined))))
1930         {
1931           /* Make sure this symbol is output as a dynamic symbol.
1932              Undefined weak syms won't yet be marked as dynamic.  */
1933           if (h->dynindx == -1
1934               && !h->forced_local)
1935             {
1936               if (! bfd_elf_link_record_dynamic_symbol (info, h))
1937                 return FALSE;
1938             }
1939
1940           /* If that succeeded, we know we'll be keeping all the
1941              relocs.  */
1942           if (h->dynindx != -1)
1943             goto keep;
1944         }
1945
1946       eh->dyn_relocs = NULL;
1947
1948     keep: ;
1949     }
1950
1951   /* Finally, allocate space.  */
1952   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1953     {
1954       asection *sreloc = elf_section_data (p->sec)->sreloc;
1955       sreloc->size += p->count * SPARC_ELF_RELA_BYTES (htab);
1956     }
1957
1958   return TRUE;
1959 }
1960
1961 /* Find any dynamic relocs that apply to read-only sections.  */
1962
1963 static bfd_boolean
1964 readonly_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
1965 {
1966   struct _bfd_sparc_elf_link_hash_entry *eh;
1967   struct _bfd_sparc_elf_dyn_relocs *p;
1968
1969   if (h->root.type == bfd_link_hash_warning)
1970     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1971
1972   eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
1973   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1974     {
1975       asection *s = p->sec->output_section;
1976
1977       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1978         {
1979           struct bfd_link_info *info = (struct bfd_link_info *) inf;
1980
1981           info->flags |= DF_TEXTREL;
1982
1983           /* Not an error, just cut short the traversal.  */
1984           return FALSE;
1985         }
1986     }
1987   return TRUE;
1988 }
1989
1990 /* Return true if the dynamic symbol for a given section should be
1991    omitted when creating a shared library.  */
1992
1993 bfd_boolean
1994 _bfd_sparc_elf_omit_section_dynsym (bfd *output_bfd,
1995                                     struct bfd_link_info *info,
1996                                     asection *p)
1997 {
1998   /* We keep the .got section symbol so that explicit relocations
1999      against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode
2000      can be turned into relocations against the .got symbol.  */
2001   if (strcmp (p->name, ".got") == 0)
2002     return FALSE;
2003
2004   return _bfd_elf_link_omit_section_dynsym (output_bfd, info, p);
2005 }
2006
2007 /* Set the sizes of the dynamic sections.  */
2008
2009 bfd_boolean
2010 _bfd_sparc_elf_size_dynamic_sections (bfd *output_bfd,
2011                                       struct bfd_link_info *info)
2012 {
2013   struct _bfd_sparc_elf_link_hash_table *htab;
2014   bfd *dynobj;
2015   asection *s;
2016   bfd *ibfd;
2017
2018   htab = _bfd_sparc_elf_hash_table (info);
2019   dynobj = htab->elf.dynobj;
2020   BFD_ASSERT (dynobj != NULL);
2021
2022   if (elf_hash_table (info)->dynamic_sections_created)
2023     {
2024       /* Set the contents of the .interp section to the interpreter.  */
2025       if (info->executable)
2026         {
2027           s = bfd_get_section_by_name (dynobj, ".interp");
2028           BFD_ASSERT (s != NULL);
2029           s->size = htab->dynamic_interpreter_size;
2030           s->contents = (unsigned char *) htab->dynamic_interpreter;
2031         }
2032     }
2033
2034   /* Set up .got offsets for local syms, and space for local dynamic
2035      relocs.  */
2036   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2037     {
2038       bfd_signed_vma *local_got;
2039       bfd_signed_vma *end_local_got;
2040       char *local_tls_type;
2041       bfd_size_type locsymcount;
2042       Elf_Internal_Shdr *symtab_hdr;
2043       asection *srel;
2044
2045       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2046         continue;
2047
2048       for (s = ibfd->sections; s != NULL; s = s->next)
2049         {
2050           struct _bfd_sparc_elf_dyn_relocs *p;
2051
2052           for (p = *((struct _bfd_sparc_elf_dyn_relocs **)
2053                      &elf_section_data (s)->local_dynrel);
2054                p != NULL;
2055                p = p->next)
2056             {
2057               if (!bfd_is_abs_section (p->sec)
2058                   && bfd_is_abs_section (p->sec->output_section))
2059                 {
2060                   /* Input section has been discarded, either because
2061                      it is a copy of a linkonce section or due to
2062                      linker script /DISCARD/, so we'll be discarding
2063                      the relocs too.  */
2064                 }
2065               else if (p->count != 0)
2066                 {
2067                   srel = elf_section_data (p->sec)->sreloc;
2068                   srel->size += p->count * SPARC_ELF_RELA_BYTES (htab);
2069                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2070                     info->flags |= DF_TEXTREL;
2071                 }
2072             }
2073         }
2074
2075       local_got = elf_local_got_refcounts (ibfd);
2076       if (!local_got)
2077         continue;
2078
2079       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2080       locsymcount = symtab_hdr->sh_info;
2081       end_local_got = local_got + locsymcount;
2082       local_tls_type = _bfd_sparc_elf_local_got_tls_type (ibfd);
2083       s = htab->sgot;
2084       srel = htab->srelgot;
2085       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
2086         {
2087           if (*local_got > 0)
2088             {
2089               *local_got = s->size;
2090               s->size += SPARC_ELF_WORD_BYTES (htab);
2091               if (*local_tls_type == GOT_TLS_GD)
2092                 s->size += SPARC_ELF_WORD_BYTES (htab);
2093               if (info->shared
2094                   || *local_tls_type == GOT_TLS_GD
2095                   || *local_tls_type == GOT_TLS_IE)
2096                 srel->size += SPARC_ELF_RELA_BYTES (htab);
2097             }
2098           else
2099             *local_got = (bfd_vma) -1;
2100         }
2101     }
2102
2103   if (htab->tls_ldm_got.refcount > 0)
2104     {
2105       /* Allocate 2 got entries and 1 dynamic reloc for
2106          R_SPARC_TLS_LDM_{HI22,LO10} relocs.  */
2107       htab->tls_ldm_got.offset = htab->sgot->size;
2108       htab->sgot->size += (2 * SPARC_ELF_WORD_BYTES (htab));
2109       htab->srelgot->size += SPARC_ELF_RELA_BYTES (htab);
2110     }
2111   else
2112     htab->tls_ldm_got.offset = -1;
2113
2114   /* Allocate global sym .plt and .got entries, and space for global
2115      sym dynamic relocs.  */
2116   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
2117
2118   if (! ABI_64_P (output_bfd)
2119       && elf_hash_table (info)->dynamic_sections_created)
2120     {
2121       /* Make space for the trailing nop in .plt.  */
2122       if (htab->splt->size > 0)
2123         htab->splt->size += 1 * SPARC_INSN_BYTES;
2124
2125       /* If the .got section is more than 0x1000 bytes, we add
2126          0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
2127          bit relocations have a greater chance of working.
2128
2129          FIXME: Make this optimization work for 64-bit too.  */
2130       if (htab->sgot->size >= 0x1000
2131           && elf_hash_table (info)->hgot->root.u.def.value == 0)
2132         elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
2133     }
2134
2135   /* The check_relocs and adjust_dynamic_symbol entry points have
2136      determined the sizes of the various dynamic sections.  Allocate
2137      memory for them.  */
2138   for (s = dynobj->sections; s != NULL; s = s->next)
2139     {
2140       const char *name;
2141       bfd_boolean strip = FALSE;
2142
2143       if ((s->flags & SEC_LINKER_CREATED) == 0)
2144         continue;
2145
2146       /* It's OK to base decisions on the section name, because none
2147          of the dynobj section names depend upon the input files.  */
2148       name = bfd_get_section_name (dynobj, s);
2149
2150       if (strncmp (name, ".rela", 5) == 0)
2151         {
2152           if (s->size == 0)
2153             {
2154               /* If we don't need this section, strip it from the
2155                  output file.  This is to handle .rela.bss and
2156                  .rel.plt.  We must create it in
2157                  create_dynamic_sections, because it must be created
2158                  before the linker maps input sections to output
2159                  sections.  The linker does that before
2160                  adjust_dynamic_symbol is called, and it is that
2161                  function which decides whether anything needs to go
2162                  into these sections.  */
2163               strip = TRUE;
2164             }
2165           else
2166             {
2167               /* We use the reloc_count field as a counter if we need
2168                  to copy relocs into the output file.  */
2169               s->reloc_count = 0;
2170             }
2171         }
2172       else if (s != htab->splt && s != htab->sgot)
2173         {
2174           /* It's not one of our sections, so don't allocate space.  */
2175           continue;
2176         }
2177
2178       if (strip)
2179         {
2180           _bfd_strip_section_from_output (info, s);
2181           continue;
2182         }
2183
2184       /* Allocate memory for the section contents.  Zero the memory
2185          for the benefit of .rela.plt, which has 4 unused entries
2186          at the beginning, and we don't want garbage.  */
2187       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2188       if (s->contents == NULL && s->size != 0)
2189         return FALSE;
2190     }
2191
2192   if (elf_hash_table (info)->dynamic_sections_created)
2193     {
2194       /* Add some entries to the .dynamic section.  We fill in the
2195          values later, in _bfd_sparc_elf_finish_dynamic_sections, but we
2196          must add the entries now so that we get the correct size for
2197          the .dynamic section.  The DT_DEBUG entry is filled in by the
2198          dynamic linker and used by the debugger.  */
2199 #define add_dynamic_entry(TAG, VAL) \
2200   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2201
2202       if (info->executable)
2203         {
2204           if (!add_dynamic_entry (DT_DEBUG, 0))
2205             return FALSE;
2206         }
2207
2208       if (htab->srelplt->size != 0)
2209         {
2210           if (!add_dynamic_entry (DT_PLTGOT, 0)
2211               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2212               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2213               || !add_dynamic_entry (DT_JMPREL, 0))
2214             return FALSE;
2215         }
2216
2217       if (!add_dynamic_entry (DT_RELA, 0)
2218           || !add_dynamic_entry (DT_RELASZ, 0)
2219           || !add_dynamic_entry (DT_RELAENT,
2220                                  SPARC_ELF_RELA_BYTES (htab)))
2221         return FALSE;
2222
2223       /* If any dynamic relocs apply to a read-only section,
2224          then we need a DT_TEXTREL entry.  */
2225       if ((info->flags & DF_TEXTREL) == 0)
2226         elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2227                                 (PTR) info);
2228
2229       if (info->flags & DF_TEXTREL)
2230         {
2231           if (!add_dynamic_entry (DT_TEXTREL, 0))
2232             return FALSE;
2233         }
2234
2235       if (ABI_64_P (output_bfd))
2236         {
2237           int reg;
2238           struct _bfd_sparc_elf_app_reg * app_regs;
2239           struct elf_strtab_hash *dynstr;
2240           struct elf_link_hash_table *eht = elf_hash_table (info);
2241
2242           /* Add dynamic STT_REGISTER symbols and corresponding DT_SPARC_REGISTER
2243              entries if needed.  */
2244           app_regs = _bfd_sparc_elf_hash_table (info)->app_regs;
2245           dynstr = eht->dynstr;
2246
2247           for (reg = 0; reg < 4; reg++)
2248             if (app_regs [reg].name != NULL)
2249               {
2250                 struct elf_link_local_dynamic_entry *entry, *e;
2251
2252                 if (!add_dynamic_entry (DT_SPARC_REGISTER, 0))
2253                   return FALSE;
2254
2255                 entry = (struct elf_link_local_dynamic_entry *)
2256                   bfd_hash_allocate (&info->hash->table, sizeof (*entry));
2257                 if (entry == NULL)
2258                   return FALSE;
2259
2260                 /* We cheat here a little bit: the symbol will not be local, so we
2261                    put it at the end of the dynlocal linked list.  We will fix it
2262                    later on, as we have to fix other fields anyway.  */
2263                 entry->isym.st_value = reg < 2 ? reg + 2 : reg + 4;
2264                 entry->isym.st_size = 0;
2265                 if (*app_regs [reg].name != '\0')
2266                   entry->isym.st_name
2267                     = _bfd_elf_strtab_add (dynstr, app_regs[reg].name, FALSE);
2268                 else
2269                   entry->isym.st_name = 0;
2270                 entry->isym.st_other = 0;
2271                 entry->isym.st_info = ELF_ST_INFO (app_regs [reg].bind,
2272                                                    STT_REGISTER);
2273                 entry->isym.st_shndx = app_regs [reg].shndx;
2274                 entry->next = NULL;
2275                 entry->input_bfd = output_bfd;
2276                 entry->input_indx = -1;
2277
2278                 if (eht->dynlocal == NULL)
2279                   eht->dynlocal = entry;
2280                 else
2281                   {
2282                     for (e = eht->dynlocal; e->next; e = e->next)
2283                       ;
2284                     e->next = entry;
2285                   }
2286                 eht->dynsymcount++;
2287               }
2288         }
2289     }
2290 #undef add_dynamic_entry
2291
2292   return TRUE;
2293 }
2294 \f
2295 bfd_boolean
2296 _bfd_sparc_elf_new_section_hook (bfd *abfd, asection *sec)
2297 {
2298   struct _bfd_sparc_elf_section_data *sdata;
2299   bfd_size_type amt = sizeof (*sdata);
2300
2301   sdata = (struct _bfd_sparc_elf_section_data *) bfd_zalloc (abfd, amt);
2302   if (sdata == NULL)
2303     return FALSE;
2304   sec->used_by_bfd = (PTR) sdata;
2305
2306   return _bfd_elf_new_section_hook (abfd, sec);
2307 }
2308
2309 bfd_boolean
2310 _bfd_sparc_elf_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
2311                               struct bfd_section *section,
2312                               struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2313                               bfd_boolean *again)
2314 {
2315   *again = FALSE;
2316   sec_do_relax (section) = 1;
2317   return TRUE;
2318 }
2319 \f
2320 /* Return the base VMA address which should be subtracted from real addresses
2321    when resolving @dtpoff relocation.
2322    This is PT_TLS segment p_vaddr.  */
2323
2324 static bfd_vma
2325 dtpoff_base (struct bfd_link_info *info)
2326 {
2327   /* If tls_sec is NULL, we should have signalled an error already.  */
2328   if (elf_hash_table (info)->tls_sec == NULL)
2329     return 0;
2330   return elf_hash_table (info)->tls_sec->vma;
2331 }
2332
2333 /* Return the relocation value for @tpoff relocation
2334    if STT_TLS virtual address is ADDRESS.  */
2335
2336 static bfd_vma
2337 tpoff (struct bfd_link_info *info, bfd_vma address)
2338 {
2339   struct elf_link_hash_table *htab = elf_hash_table (info);
2340
2341   /* If tls_sec is NULL, we should have signalled an error already.  */
2342   if (htab->tls_sec == NULL)
2343     return 0;
2344   return address - htab->tls_size - htab->tls_sec->vma;
2345 }
2346
2347 /* Relocate a SPARC ELF section.  */
2348
2349 bfd_boolean
2350 _bfd_sparc_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2351                                  bfd *input_bfd, asection *input_section,
2352                                  bfd_byte *contents, Elf_Internal_Rela *relocs,
2353                                  Elf_Internal_Sym *local_syms, asection **local_sections)
2354 {
2355   struct _bfd_sparc_elf_link_hash_table *htab;
2356   Elf_Internal_Shdr *symtab_hdr;
2357   struct elf_link_hash_entry **sym_hashes;
2358   bfd_vma *local_got_offsets;
2359   bfd_vma got_base;
2360   asection *sreloc;
2361   Elf_Internal_Rela *rel;
2362   Elf_Internal_Rela *relend;
2363   int num_relocs;
2364
2365   if (info->relocatable)
2366     return TRUE;
2367
2368   htab = _bfd_sparc_elf_hash_table (info);
2369   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2370   sym_hashes = elf_sym_hashes (input_bfd);
2371   local_got_offsets = elf_local_got_offsets (input_bfd);
2372
2373   if (elf_hash_table (info)->hgot == NULL)
2374     got_base = 0;
2375   else
2376     got_base = elf_hash_table (info)->hgot->root.u.def.value;
2377
2378   sreloc = elf_section_data (input_section)->sreloc;
2379
2380   rel = relocs;
2381   if (ABI_64_P (output_bfd))
2382     num_relocs = NUM_SHDR_ENTRIES (& elf_section_data (input_section)->rel_hdr);
2383   else
2384     num_relocs = input_section->reloc_count;
2385   relend = relocs + num_relocs;
2386   for (; rel < relend; rel++)
2387     {
2388       int r_type, tls_type;
2389       reloc_howto_type *howto;
2390       unsigned long r_symndx;
2391       struct elf_link_hash_entry *h;
2392       Elf_Internal_Sym *sym;
2393       asection *sec;
2394       bfd_vma relocation, off;
2395       bfd_reloc_status_type r;
2396       bfd_boolean is_plt = FALSE;
2397       bfd_boolean unresolved_reloc;
2398
2399       r_type = SPARC_ELF_R_TYPE (rel->r_info);
2400       if (r_type == R_SPARC_GNU_VTINHERIT
2401           || r_type == R_SPARC_GNU_VTENTRY)
2402         continue;
2403
2404       if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
2405         {
2406           bfd_set_error (bfd_error_bad_value);
2407           return FALSE;
2408         }
2409       howto = _bfd_sparc_elf_howto_table + r_type;
2410
2411       /* This is a final link.  */
2412       r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
2413       h = NULL;
2414       sym = NULL;
2415       sec = NULL;
2416       unresolved_reloc = FALSE;
2417       if (r_symndx < symtab_hdr->sh_info)
2418         {
2419           sym = local_syms + r_symndx;
2420           sec = local_sections[r_symndx];
2421           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2422         }
2423       else
2424         {
2425           bfd_boolean warned;
2426
2427           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2428                                    r_symndx, symtab_hdr, sym_hashes,
2429                                    h, sec, relocation,
2430                                    unresolved_reloc, warned);
2431           if (warned)
2432             {
2433               /* To avoid generating warning messages about truncated
2434                  relocations, set the relocation's address to be the same as
2435                  the start of this section.  */
2436               if (input_section->output_section != NULL)
2437                 relocation = input_section->output_section->vma;
2438               else
2439                 relocation = 0;
2440             }
2441         }
2442
2443       switch (r_type)
2444         {
2445         case R_SPARC_GOT10:
2446         case R_SPARC_GOT13:
2447         case R_SPARC_GOT22:
2448           /* Relocation is to the entry for this symbol in the global
2449              offset table.  */
2450           if (htab->sgot == NULL)
2451             abort ();
2452
2453           if (h != NULL)
2454             {
2455               bfd_boolean dyn;
2456
2457               off = h->got.offset;
2458               BFD_ASSERT (off != (bfd_vma) -1);
2459               dyn = elf_hash_table (info)->dynamic_sections_created;
2460
2461               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2462                   || (info->shared
2463                       && (info->symbolic
2464                           || h->dynindx == -1
2465                           || h->forced_local)
2466                       && h->def_regular))
2467                 {
2468                   /* This is actually a static link, or it is a
2469                      -Bsymbolic link and the symbol is defined
2470                      locally, or the symbol was forced to be local
2471                      because of a version file.  We must initialize
2472                      this entry in the global offset table.  Since the
2473                      offset must always be a multiple of 8 for 64-bit
2474                      and 4 for 32-bit, we use the least significant bit
2475                      to record whether we have initialized it already.
2476
2477                      When doing a dynamic link, we create a .rela.got
2478                      relocation entry to initialize the value.  This
2479                      is done in the finish_dynamic_symbol routine.  */
2480                   if ((off & 1) != 0)
2481                     off &= ~1;
2482                   else
2483                     {
2484                       SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
2485                                           htab->sgot->contents + off);
2486                       h->got.offset |= 1;
2487                     }
2488                 }
2489               else
2490                 unresolved_reloc = FALSE;
2491             }
2492           else
2493             {
2494               BFD_ASSERT (local_got_offsets != NULL
2495                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
2496
2497               off = local_got_offsets[r_symndx];
2498
2499               /* The offset must always be a multiple of 8 on 64-bit and
2500                  4 on 32-bit.  We use the least significant bit to record
2501                  whether we have already processed this entry.  */
2502               if ((off & 1) != 0)
2503                 off &= ~1;
2504               else
2505                 {
2506
2507                   if (info->shared)
2508                     {
2509                       asection *s;
2510                       Elf_Internal_Rela outrel;
2511
2512                       /* We need to generate a R_SPARC_RELATIVE reloc
2513                          for the dynamic linker.  */
2514                       s = htab->srelgot;
2515                       BFD_ASSERT (s != NULL);
2516
2517                       outrel.r_offset = (htab->sgot->output_section->vma
2518                                          + htab->sgot->output_offset
2519                                          + off);
2520                       outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
2521                                                         0, R_SPARC_RELATIVE);
2522                       outrel.r_addend = relocation;
2523                       relocation = 0;
2524                       SPARC_ELF_APPEND_RELA (htab, output_bfd, s, &outrel);
2525                     }
2526
2527                   SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
2528                                       htab->sgot->contents + off);
2529                   local_got_offsets[r_symndx] |= 1;
2530                 }
2531             }
2532           relocation = htab->sgot->output_offset + off - got_base;
2533           break;
2534
2535         case R_SPARC_PLT32:
2536         case R_SPARC_PLT64:
2537           if (h == NULL || h->plt.offset == (bfd_vma) -1)
2538             {
2539               r_type = (r_type == R_SPARC_PLT32) ? R_SPARC_32 : R_SPARC_64;
2540               goto r_sparc_plt32;
2541             }
2542           /* Fall through.  */
2543
2544         case R_SPARC_WPLT30:
2545         case R_SPARC_HIPLT22:
2546         case R_SPARC_LOPLT10:
2547         case R_SPARC_PCPLT32:
2548         case R_SPARC_PCPLT22:
2549         case R_SPARC_PCPLT10:
2550         r_sparc_wplt30:
2551           /* Relocation is to the entry for this symbol in the
2552              procedure linkage table.  */
2553
2554           if (! ABI_64_P (output_bfd))
2555             {
2556               /* The Solaris native assembler will generate a WPLT30 reloc
2557                  for a local symbol if you assemble a call from one
2558                  section to another when using -K pic.  We treat it as
2559                  WDISP30.  */
2560               if (h == NULL)
2561                 break;
2562             }
2563           else
2564             {
2565               BFD_ASSERT (h != NULL);
2566             }
2567
2568           if (h->plt.offset == (bfd_vma) -1 || htab->splt == NULL)
2569             {
2570               /* We didn't make a PLT entry for this symbol.  This
2571                  happens when statically linking PIC code, or when
2572                  using -Bsymbolic.  */
2573               break;
2574             }
2575
2576           relocation = (htab->splt->output_section->vma
2577                         + htab->splt->output_offset
2578                         + h->plt.offset);
2579           unresolved_reloc = FALSE;
2580           if (r_type == R_SPARC_PLT32 || r_type == R_SPARC_PLT64)
2581             {
2582               r_type = r_type == R_SPARC_PLT32 ? R_SPARC_32 : R_SPARC_64;
2583               is_plt = TRUE;
2584               goto r_sparc_plt32;
2585             }
2586           break;
2587
2588         case R_SPARC_PC10:
2589         case R_SPARC_PC22:
2590         case R_SPARC_PC_HH22:
2591         case R_SPARC_PC_HM10:
2592         case R_SPARC_PC_LM22:
2593           if (h != NULL
2594               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2595             break;
2596           /* Fall through.  */
2597         case R_SPARC_DISP8:
2598         case R_SPARC_DISP16:
2599         case R_SPARC_DISP32:
2600         case R_SPARC_DISP64:
2601         case R_SPARC_WDISP30:
2602         case R_SPARC_WDISP22:
2603         case R_SPARC_WDISP19:
2604         case R_SPARC_WDISP16:
2605         case R_SPARC_8:
2606         case R_SPARC_16:
2607         case R_SPARC_32:
2608         case R_SPARC_HI22:
2609         case R_SPARC_22:
2610         case R_SPARC_13:
2611         case R_SPARC_LO10:
2612         case R_SPARC_UA16:
2613         case R_SPARC_UA32:
2614         case R_SPARC_10:
2615         case R_SPARC_11:
2616         case R_SPARC_64:
2617         case R_SPARC_OLO10:
2618         case R_SPARC_HH22:
2619         case R_SPARC_HM10:
2620         case R_SPARC_LM22:
2621         case R_SPARC_7:
2622         case R_SPARC_5:
2623         case R_SPARC_6:
2624         case R_SPARC_HIX22:
2625         case R_SPARC_LOX10:
2626         case R_SPARC_H44:
2627         case R_SPARC_M44:
2628         case R_SPARC_L44:
2629         case R_SPARC_UA64:
2630         r_sparc_plt32:
2631           /* r_symndx will be zero only for relocs against symbols
2632              from removed linkonce sections, or sections discarded by
2633              a linker script.  */
2634           if (r_symndx == 0
2635               || (input_section->flags & SEC_ALLOC) == 0)
2636             break;
2637
2638           if ((info->shared
2639                && (h == NULL
2640                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2641                    || h->root.type != bfd_link_hash_undefweak)
2642                && (! howto->pc_relative
2643                    || (h != NULL
2644                        && h->dynindx != -1
2645                        && (! info->symbolic
2646                            || !h->def_regular))))
2647               || (!info->shared
2648                   && h != NULL
2649                   && h->dynindx != -1
2650                   && !h->non_got_ref
2651                   && ((h->def_dynamic
2652                        && !h->def_regular)
2653                       || h->root.type == bfd_link_hash_undefweak
2654                       || h->root.type == bfd_link_hash_undefined)))
2655             {
2656               Elf_Internal_Rela outrel;
2657               bfd_boolean skip, relocate = FALSE;
2658
2659               /* When generating a shared object, these relocations
2660                  are copied into the output file to be resolved at run
2661                  time.  */
2662
2663               BFD_ASSERT (sreloc != NULL);
2664
2665               skip = FALSE;
2666
2667               outrel.r_offset =
2668                 _bfd_elf_section_offset (output_bfd, info, input_section,
2669                                          rel->r_offset);
2670               if (outrel.r_offset == (bfd_vma) -1)
2671                 skip = TRUE;
2672               else if (outrel.r_offset == (bfd_vma) -2)
2673                 skip = TRUE, relocate = TRUE;
2674               outrel.r_offset += (input_section->output_section->vma
2675                                   + input_section->output_offset);
2676
2677               /* Optimize unaligned reloc usage now that we know where
2678                  it finally resides.  */
2679               switch (r_type)
2680                 {
2681                 case R_SPARC_16:
2682                   if (outrel.r_offset & 1)
2683                     r_type = R_SPARC_UA16;
2684                   break;
2685                 case R_SPARC_UA16:
2686                   if (!(outrel.r_offset & 1))
2687                     r_type = R_SPARC_16;
2688                   break;
2689                 case R_SPARC_32:
2690                   if (outrel.r_offset & 3)
2691                     r_type = R_SPARC_UA32;
2692                   break;
2693                 case R_SPARC_UA32:
2694                   if (!(outrel.r_offset & 3))
2695                     r_type = R_SPARC_32;
2696                   break;
2697                 case R_SPARC_64:
2698                   if (outrel.r_offset & 7)
2699                     r_type = R_SPARC_UA64;
2700                   break;
2701                 case R_SPARC_UA64:
2702                   if (!(outrel.r_offset & 7))
2703                     r_type = R_SPARC_64;
2704                   break;
2705                 case R_SPARC_DISP8:
2706                 case R_SPARC_DISP16:
2707                 case R_SPARC_DISP32:
2708                 case R_SPARC_DISP64:
2709                   /* If the symbol is not dynamic, we should not keep
2710                      a dynamic relocation.  But an .rela.* slot has been
2711                      allocated for it, output R_SPARC_NONE.
2712                      FIXME: Add code tracking needed dynamic relocs as
2713                      e.g. i386 has.  */
2714                   if (h->dynindx == -1)
2715                     skip = TRUE, relocate = TRUE;
2716                   break;
2717                 }
2718
2719               if (skip)
2720                 memset (&outrel, 0, sizeof outrel);
2721               /* h->dynindx may be -1 if the symbol was marked to
2722                  become local.  */
2723               else if (h != NULL && ! is_plt
2724                        && ((! info->symbolic && h->dynindx != -1)
2725                            || !h->def_regular))
2726                 {
2727                   BFD_ASSERT (h->dynindx != -1);
2728                   outrel.r_info = SPARC_ELF_R_INFO (htab, rel, h->dynindx, r_type);
2729                   outrel.r_addend = rel->r_addend;
2730                 }
2731               else
2732                 {
2733                   if (r_type == R_SPARC_32 || r_type == R_SPARC_64)
2734                     {
2735                       outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
2736                                                         0, R_SPARC_RELATIVE);
2737                       outrel.r_addend = relocation + rel->r_addend;
2738                     }
2739                   else
2740                     {
2741                       long indx;
2742
2743                       if (is_plt)
2744                         sec = htab->splt;
2745
2746                       if (bfd_is_abs_section (sec))
2747                         indx = 0;
2748                       else if (sec == NULL || sec->owner == NULL)
2749                         {
2750                           bfd_set_error (bfd_error_bad_value);
2751                           return FALSE;
2752                         }
2753                       else
2754                         {
2755                           asection *osec;
2756
2757                           osec = sec->output_section;
2758                           indx = elf_section_data (osec)->dynindx;
2759
2760                           /* FIXME: we really should be able to link non-pic
2761                              shared libraries.  */
2762                           if (indx == 0)
2763                             {
2764                               BFD_FAIL ();
2765                               (*_bfd_error_handler)
2766                                 (_("%B: probably compiled without -fPIC?"),
2767                                  input_bfd);
2768                               bfd_set_error (bfd_error_bad_value);
2769                               return FALSE;
2770                             }
2771                         }
2772
2773                       outrel.r_info = SPARC_ELF_R_INFO (htab, rel, indx, r_type);
2774                       outrel.r_addend = relocation + rel->r_addend;
2775                     }
2776                 }
2777
2778               SPARC_ELF_APPEND_RELA (htab, output_bfd, sreloc, &outrel);
2779
2780               /* This reloc will be computed at runtime, so there's no
2781                  need to do anything now.  */
2782               if (! relocate)
2783                 continue;
2784             }
2785           break;
2786
2787         case R_SPARC_TLS_GD_HI22:
2788           if (! ABI_64_P (input_bfd)
2789               && ! _bfd_sparc_elf_tdata (input_bfd)->has_tlsgd)
2790             {
2791               /* R_SPARC_REV32 used the same reloc number as
2792                  R_SPARC_TLS_GD_HI22.  */
2793               r_type = R_SPARC_REV32;
2794               break;
2795             }
2796           /* Fall through */
2797
2798         case R_SPARC_TLS_GD_LO10:
2799         case R_SPARC_TLS_IE_HI22:
2800         case R_SPARC_TLS_IE_LO10:
2801           r_type = sparc_elf_tls_transition (info, input_bfd, r_type, h == NULL);
2802           tls_type = GOT_UNKNOWN;
2803           if (h == NULL && local_got_offsets)
2804             tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
2805           else if (h != NULL)
2806             {
2807               tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
2808               if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2809                 switch (SPARC_ELF_R_TYPE (rel->r_info))
2810                   {
2811                   case R_SPARC_TLS_GD_HI22:
2812                   case R_SPARC_TLS_IE_HI22:
2813                     r_type = R_SPARC_TLS_LE_HIX22;
2814                     break;
2815                   default:
2816                     r_type = R_SPARC_TLS_LE_LOX10;
2817                     break;
2818                   }
2819             }
2820           if (tls_type == GOT_TLS_IE)
2821             switch (r_type)
2822               {
2823               case R_SPARC_TLS_GD_HI22:
2824                 r_type = R_SPARC_TLS_IE_HI22;
2825                 break;
2826               case R_SPARC_TLS_GD_LO10:
2827                 r_type = R_SPARC_TLS_IE_LO10;
2828                 break;
2829               }
2830
2831           if (r_type == R_SPARC_TLS_LE_HIX22)
2832             {
2833               relocation = tpoff (info, relocation);
2834               break;
2835             }
2836           if (r_type == R_SPARC_TLS_LE_LOX10)
2837             {
2838               /* Change add into xor.  */
2839               relocation = tpoff (info, relocation);
2840               bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd,
2841                                                    contents + rel->r_offset)
2842                                        | 0x80182000), contents + rel->r_offset);
2843               break;
2844             }
2845
2846           if (h != NULL)
2847             {
2848               off = h->got.offset;
2849               h->got.offset |= 1;
2850             }
2851           else
2852             {
2853               BFD_ASSERT (local_got_offsets != NULL);
2854               off = local_got_offsets[r_symndx];
2855               local_got_offsets[r_symndx] |= 1;
2856             }
2857
2858         r_sparc_tlsldm:
2859           if (htab->sgot == NULL)
2860             abort ();
2861
2862           if ((off & 1) != 0)
2863             off &= ~1;
2864           else
2865             {
2866               Elf_Internal_Rela outrel;
2867               int dr_type, indx;
2868
2869               if (htab->srelgot == NULL)
2870                 abort ();
2871
2872               SPARC_ELF_PUT_WORD (htab, output_bfd, 0, htab->sgot->contents + off);
2873               outrel.r_offset = (htab->sgot->output_section->vma
2874                                  + htab->sgot->output_offset + off);
2875               indx = h && h->dynindx != -1 ? h->dynindx : 0;
2876               if (r_type == R_SPARC_TLS_IE_HI22
2877                   || r_type == R_SPARC_TLS_IE_LO10)
2878                 dr_type = SPARC_ELF_TPOFF_RELOC (htab);
2879               else
2880                 dr_type = SPARC_ELF_DTPMOD_RELOC (htab);
2881               if (dr_type == SPARC_ELF_TPOFF_RELOC (htab) && indx == 0)
2882                 outrel.r_addend = relocation - dtpoff_base (info);
2883               else
2884                 outrel.r_addend = 0;
2885               outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx, dr_type);
2886               SPARC_ELF_APPEND_RELA (htab, output_bfd, htab->srelgot, &outrel);
2887
2888               if (r_type == R_SPARC_TLS_GD_HI22
2889                   || r_type == R_SPARC_TLS_GD_LO10)
2890                 {
2891                   if (indx == 0)
2892                     {
2893                       BFD_ASSERT (! unresolved_reloc);
2894                       SPARC_ELF_PUT_WORD (htab, output_bfd,
2895                                           relocation - dtpoff_base (info),
2896                                           (htab->sgot->contents + off
2897                                            + SPARC_ELF_WORD_BYTES (htab)));
2898                     }
2899                   else
2900                     {
2901                       SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
2902                                           (htab->sgot->contents + off
2903                                            + SPARC_ELF_WORD_BYTES (htab)));
2904                       outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx,
2905                                                         SPARC_ELF_DTPOFF_RELOC (htab));
2906                       outrel.r_offset += SPARC_ELF_WORD_BYTES (htab);
2907                       SPARC_ELF_APPEND_RELA (htab, output_bfd, htab->srelgot, &outrel);
2908                     }
2909                 }
2910               else if (dr_type == SPARC_ELF_DTPMOD_RELOC (htab))
2911                 {
2912                   SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
2913                                       (htab->sgot->contents + off
2914                                        + SPARC_ELF_WORD_BYTES (htab)));
2915                 }
2916             }
2917
2918           if (off >= (bfd_vma) -2)
2919             abort ();
2920
2921           relocation = htab->sgot->output_offset + off - got_base;
2922           unresolved_reloc = FALSE;
2923           howto = _bfd_sparc_elf_howto_table + r_type;
2924           break;
2925
2926         case R_SPARC_TLS_LDM_HI22:
2927         case R_SPARC_TLS_LDM_LO10:
2928           if (! info->shared)
2929             {
2930               bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2931               continue;
2932             }
2933           off = htab->tls_ldm_got.offset;
2934           htab->tls_ldm_got.offset |= 1;
2935           goto r_sparc_tlsldm;
2936
2937         case R_SPARC_TLS_LDO_HIX22:
2938         case R_SPARC_TLS_LDO_LOX10:
2939           if (info->shared)
2940             {
2941               relocation -= dtpoff_base (info);
2942               break;
2943             }
2944
2945           r_type = (r_type == R_SPARC_TLS_LDO_HIX22
2946                     ? R_SPARC_TLS_LE_HIX22 : R_SPARC_TLS_LE_LOX10);
2947           /* Fall through.  */
2948
2949         case R_SPARC_TLS_LE_HIX22:
2950         case R_SPARC_TLS_LE_LOX10:
2951           if (info->shared)
2952             {
2953               Elf_Internal_Rela outrel;
2954               bfd_boolean skip, relocate = FALSE;
2955
2956               BFD_ASSERT (sreloc != NULL);
2957               skip = FALSE;
2958               outrel.r_offset =
2959                 _bfd_elf_section_offset (output_bfd, info, input_section,
2960                                          rel->r_offset);
2961               if (outrel.r_offset == (bfd_vma) -1)
2962                 skip = TRUE;
2963               else if (outrel.r_offset == (bfd_vma) -2)
2964                 skip = TRUE, relocate = TRUE;
2965               outrel.r_offset += (input_section->output_section->vma
2966                                   + input_section->output_offset);
2967               if (skip)
2968                 memset (&outrel, 0, sizeof outrel);
2969               else
2970                 {
2971                   outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, r_type);
2972                   outrel.r_addend = relocation - dtpoff_base (info)
2973                                     + rel->r_addend;
2974                 }
2975
2976               SPARC_ELF_APPEND_RELA (htab, output_bfd, sreloc, &outrel);
2977               continue;
2978             }
2979           relocation = tpoff (info, relocation);
2980           break;
2981
2982         case R_SPARC_TLS_LDM_CALL:
2983           if (! info->shared)
2984             {
2985               /* mov %g0, %o0 */
2986               bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset);
2987               continue;
2988             }
2989           /* Fall through */
2990
2991         case R_SPARC_TLS_GD_CALL:
2992           tls_type = GOT_UNKNOWN;
2993           if (h == NULL && local_got_offsets)
2994             tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
2995           else if (h != NULL)
2996             tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
2997           if (! info->shared
2998               || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE))
2999             {
3000               bfd_vma insn;
3001
3002               if (!info->shared && (h == NULL || h->dynindx == -1))
3003                 {
3004                   /* GD -> LE */
3005                   bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3006                   continue;
3007                 }
3008
3009               /* GD -> IE */
3010               if (rel + 1 < relend
3011                   && SPARC_ELF_R_TYPE (rel[1].r_info) == R_SPARC_TLS_GD_ADD
3012                   && rel[1].r_offset == rel->r_offset + 4
3013                   && SPARC_ELF_R_SYMNDX (htab, rel[1].r_info) == r_symndx
3014                   && (((insn = bfd_get_32 (input_bfd,
3015                                            contents + rel[1].r_offset))
3016                        >> 25) & 0x1f) == 8)
3017                 {
3018                   /* We have
3019                      call __tls_get_addr, %tgd_call(foo)
3020                       add %reg1, %reg2, %o0, %tgd_add(foo)
3021                      and change it into IE:
3022                      {ld,ldx} [%reg1 + %reg2], %o0, %tie_ldx(foo)
3023                      add %g7, %o0, %o0, %tie_add(foo).
3024                      add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2,
3025                      ld is 0xc0000000 | (rd << 25) | (rs1 << 14) | rs2,
3026                      ldx is 0xc0580000 | (rd << 25) | (rs1 << 14) | rs2.  */
3027                   bfd_put_32 (output_bfd, insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000),
3028                               contents + rel->r_offset);
3029                   bfd_put_32 (output_bfd, 0x9001c008,
3030                               contents + rel->r_offset + 4);
3031                   rel++;
3032                   continue;
3033                 }
3034
3035               bfd_put_32 (output_bfd, 0x9001c008, contents + rel->r_offset);
3036               continue;
3037             }
3038
3039           h = (struct elf_link_hash_entry *)
3040               bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE,
3041                                     FALSE, TRUE);
3042           BFD_ASSERT (h != NULL);
3043           r_type = R_SPARC_WPLT30;
3044           howto = _bfd_sparc_elf_howto_table + r_type;
3045           goto r_sparc_wplt30;
3046
3047         case R_SPARC_TLS_GD_ADD:
3048           tls_type = GOT_UNKNOWN;
3049           if (h == NULL && local_got_offsets)
3050             tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
3051           else if (h != NULL)
3052             tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
3053           if (! info->shared || tls_type == GOT_TLS_IE)
3054             {
3055               /* add %reg1, %reg2, %reg3, %tgd_add(foo)
3056                  changed into IE:
3057                  {ld,ldx} [%reg1 + %reg2], %reg3, %tie_ldx(foo)
3058                  or LE:
3059                  add %g7, %reg2, %reg3.  */
3060               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3061               if ((h != NULL && h->dynindx != -1) || info->shared)
3062                 relocation = insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000);
3063               else
3064                 relocation = (insn & ~0x7c000) | 0x1c000;
3065               bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
3066             }
3067           continue;
3068
3069         case R_SPARC_TLS_LDM_ADD:
3070           if (! info->shared)
3071             bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3072           continue;
3073
3074         case R_SPARC_TLS_LDO_ADD:
3075           if (! info->shared)
3076             {
3077               /* Change rs1 into %g7.  */
3078               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3079               insn = (insn & ~0x7c000) | 0x1c000;
3080               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
3081             }
3082           continue;
3083
3084         case R_SPARC_TLS_IE_LD:
3085         case R_SPARC_TLS_IE_LDX:
3086           if (! info->shared && (h == NULL || h->dynindx == -1))
3087             {
3088               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3089               int rs2 = insn & 0x1f;
3090               int rd = (insn >> 25) & 0x1f;
3091
3092               if (rs2 == rd)
3093                 relocation = SPARC_NOP;
3094               else
3095                 relocation = 0x80100000 | (insn & 0x3e00001f);
3096               bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
3097             }
3098           continue;
3099
3100         case R_SPARC_TLS_IE_ADD:
3101           /* Totally useless relocation.  */
3102           continue;
3103
3104         case R_SPARC_TLS_DTPOFF32:
3105         case R_SPARC_TLS_DTPOFF64:
3106           relocation -= dtpoff_base (info);
3107           break;
3108
3109         default:
3110           break;
3111         }
3112
3113       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3114          because such sections are not SEC_ALLOC and thus ld.so will
3115          not process them.  */
3116       if (unresolved_reloc
3117           && !((input_section->flags & SEC_DEBUGGING) != 0
3118                && h->def_dynamic))
3119         (*_bfd_error_handler)
3120           (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
3121            input_bfd,
3122            input_section,
3123            (long) rel->r_offset,
3124            h->root.root.string);
3125
3126       r = bfd_reloc_continue;
3127       if (r_type == R_SPARC_OLO10)
3128         {
3129             bfd_vma x;
3130
3131             if (! ABI_64_P (output_bfd))
3132               abort ();
3133
3134             relocation += rel->r_addend;
3135             relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
3136
3137             x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3138             x = (x & ~(bfd_vma) 0x1fff) | (relocation & 0x1fff);
3139             bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3140
3141             r = bfd_check_overflow (howto->complain_on_overflow,
3142                                     howto->bitsize, howto->rightshift,
3143                                     bfd_arch_bits_per_address (input_bfd),
3144                                     relocation);
3145         }
3146       else if (r_type == R_SPARC_WDISP16)
3147         {
3148           bfd_vma x;
3149
3150           relocation += rel->r_addend;
3151           relocation -= (input_section->output_section->vma
3152                          + input_section->output_offset);
3153           relocation -= rel->r_offset;
3154
3155           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3156           x |= ((((relocation >> 2) & 0xc000) << 6)
3157                 | ((relocation >> 2) & 0x3fff));
3158           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3159
3160           r = bfd_check_overflow (howto->complain_on_overflow,
3161                                   howto->bitsize, howto->rightshift,
3162                                   bfd_arch_bits_per_address (input_bfd),
3163                                   relocation);
3164         }
3165       else if (r_type == R_SPARC_REV32)
3166         {
3167           bfd_vma x;
3168
3169           relocation = relocation + rel->r_addend;
3170
3171           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3172           x = x + relocation;
3173           bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
3174           r = bfd_reloc_ok;
3175         }
3176       else if (r_type == R_SPARC_TLS_LDO_HIX22
3177                || r_type == R_SPARC_TLS_LE_HIX22)
3178         {
3179           bfd_vma x;
3180
3181           relocation += rel->r_addend;
3182           if (r_type == R_SPARC_TLS_LE_HIX22)
3183             relocation ^= MINUS_ONE;
3184
3185           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3186           x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
3187           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3188           r = bfd_reloc_ok;
3189         }
3190       else if (r_type == R_SPARC_TLS_LDO_LOX10
3191                || r_type == R_SPARC_TLS_LE_LOX10)
3192         {
3193           bfd_vma x;
3194
3195           relocation += rel->r_addend;
3196           relocation &= 0x3ff;
3197           if (r_type == R_SPARC_TLS_LE_LOX10)
3198             relocation |= 0x1c00;
3199
3200           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3201           x = (x & ~(bfd_vma) 0x1fff) | relocation;
3202           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3203
3204           r = bfd_reloc_ok;
3205         }
3206       else if (r_type == R_SPARC_HIX22)
3207         {
3208           bfd_vma x;
3209
3210           relocation += rel->r_addend;
3211           relocation = relocation ^ MINUS_ONE;
3212
3213           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3214           x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
3215           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3216
3217           r = bfd_check_overflow (howto->complain_on_overflow,
3218                                   howto->bitsize, howto->rightshift,
3219                                   bfd_arch_bits_per_address (input_bfd),
3220                                   relocation);
3221         }
3222       else if (r_type == R_SPARC_LOX10)
3223         {
3224           bfd_vma x;
3225
3226           relocation += rel->r_addend;
3227           relocation = (relocation & 0x3ff) | 0x1c00;
3228
3229           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3230           x = (x & ~(bfd_vma) 0x1fff) | relocation;
3231           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3232
3233           r = bfd_reloc_ok;
3234         }
3235       else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
3236                && sec_do_relax (input_section)
3237                && rel->r_offset + 4 < input_section->size)
3238         {
3239 #define G0              0
3240 #define O7              15
3241 #define XCC             (2 << 20)
3242 #define COND(x)         (((x)&0xf)<<25)
3243 #define CONDA           COND(0x8)
3244 #define INSN_BPA        (F2(0,1) | CONDA | BPRED | XCC)
3245 #define INSN_BA         (F2(0,2) | CONDA)
3246 #define INSN_OR         F3(2, 0x2, 0)
3247 #define INSN_NOP        F2(0,4)
3248
3249           bfd_vma x, y;
3250
3251           /* If the instruction is a call with either:
3252              restore
3253              arithmetic instruction with rd == %o7
3254              where rs1 != %o7 and rs2 if it is register != %o7
3255              then we can optimize if the call destination is near
3256              by changing the call into a branch always.  */
3257           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3258           y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
3259           if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
3260             {
3261               if (((y & OP3(~0)) == OP3(0x3d) /* restore */
3262                    || ((y & OP3(0x28)) == 0 /* arithmetic */
3263                        && (y & RD(~0)) == RD(O7)))
3264                   && (y & RS1(~0)) != RS1(O7)
3265                   && ((y & F3I(~0))
3266                       || (y & RS2(~0)) != RS2(O7)))
3267                 {
3268                   bfd_vma reloc;
3269
3270                   reloc = relocation + rel->r_addend - rel->r_offset;
3271                   reloc -= (input_section->output_section->vma
3272                             + input_section->output_offset);
3273
3274                   /* Ensure the branch fits into simm22.  */
3275                   if ((reloc & 3) == 0
3276                       && ((reloc & ~(bfd_vma)0x7fffff) == 0
3277                           || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
3278                     {
3279                       reloc >>= 2;
3280
3281                       /* Check whether it fits into simm19.  */
3282                       if (((reloc & 0x3c0000) == 0
3283                            || (reloc & 0x3c0000) == 0x3c0000)
3284                           && (ABI_64_P (output_bfd)
3285                               || elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
3286                         x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
3287                       else
3288                         x = INSN_BA | (reloc & 0x3fffff); /* ba */
3289                       bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3290                       r = bfd_reloc_ok;
3291                       if (rel->r_offset >= 4
3292                           && (y & (0xffffffff ^ RS1(~0)))
3293                              == (INSN_OR | RD(O7) | RS2(G0)))
3294                         {
3295                           bfd_vma z;
3296                           unsigned int reg;
3297
3298                           z = bfd_get_32 (input_bfd,
3299                                           contents + rel->r_offset - 4);
3300                           if ((z & (0xffffffff ^ RD(~0)))
3301                               != (INSN_OR | RS1(O7) | RS2(G0)))
3302                             break;
3303
3304                           /* The sequence was
3305                              or %o7, %g0, %rN
3306                              call foo
3307                              or %rN, %g0, %o7
3308
3309                              If call foo was replaced with ba, replace
3310                              or %rN, %g0, %o7 with nop.  */
3311
3312                           reg = (y & RS1(~0)) >> 14;
3313                           if (reg != ((z & RD(~0)) >> 25)
3314                               || reg == G0 || reg == O7)
3315                             break;
3316
3317                           bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
3318                                       contents + rel->r_offset + 4);
3319                         }
3320
3321                     }
3322                 }
3323             }
3324         }
3325
3326       if (r == bfd_reloc_continue)
3327         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3328                                       contents, rel->r_offset,
3329                                       relocation, rel->r_addend);
3330
3331       if (r != bfd_reloc_ok)
3332         {
3333           switch (r)
3334             {
3335             default:
3336             case bfd_reloc_outofrange:
3337               abort ();
3338             case bfd_reloc_overflow:
3339               {
3340                 const char *name;
3341
3342                 if (h != NULL)
3343                   name = NULL;
3344                 else
3345                   {
3346                     name = bfd_elf_string_from_elf_section (input_bfd,
3347                                                             symtab_hdr->sh_link,
3348                                                             sym->st_name);
3349                     if (name == NULL)
3350                       return FALSE;
3351                     if (*name == '\0')
3352                       name = bfd_section_name (input_bfd, sec);
3353                   }
3354                 if (! ((*info->callbacks->reloc_overflow)
3355                        (info, (h ? &h->root : NULL), name, howto->name,
3356                         (bfd_vma) 0, input_bfd, input_section,
3357                         rel->r_offset)))
3358                   return FALSE;
3359               }
3360               break;
3361             }
3362         }
3363     }
3364
3365   return TRUE;
3366 }
3367
3368 /* Finish up dynamic symbol handling.  We set the contents of various
3369    dynamic sections here.  */
3370
3371 bfd_boolean
3372 _bfd_sparc_elf_finish_dynamic_symbol (bfd *output_bfd,
3373                                       struct bfd_link_info *info,
3374                                       struct elf_link_hash_entry *h,
3375                                       Elf_Internal_Sym *sym)
3376 {
3377   bfd *dynobj;
3378   struct _bfd_sparc_elf_link_hash_table *htab;
3379
3380   htab = _bfd_sparc_elf_hash_table (info);
3381   dynobj = htab->elf.dynobj;
3382
3383   if (h->plt.offset != (bfd_vma) -1)
3384     {
3385       asection *splt;
3386       asection *srela;
3387       Elf_Internal_Rela rela;
3388       bfd_byte *loc;
3389       bfd_vma r_offset;
3390       int rela_index;
3391
3392       /* This symbol has an entry in the PLT.  Set it up.  */
3393
3394       BFD_ASSERT (h->dynindx != -1);
3395
3396       splt = htab->splt;
3397       srela = htab->srelplt;
3398       BFD_ASSERT (splt != NULL && srela != NULL);
3399
3400       /* Fill in the entry in the procedure linkage table.  */
3401       rela_index = SPARC_ELF_BUILD_PLT_ENTRY (htab, output_bfd, splt,
3402                                               h->plt.offset, splt->size,
3403                                               &r_offset);
3404
3405       /* Fill in the entry in the .rela.plt section.  */
3406       rela.r_offset = r_offset
3407         + (splt->output_section->vma + splt->output_offset);
3408       if (! ABI_64_P (output_bfd)
3409           || h->plt.offset < (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
3410         {
3411           rela.r_addend = 0;
3412         }
3413       else
3414         {
3415           rela.r_addend = -(h->plt.offset + 4)
3416                           -(splt->output_section->vma + splt->output_offset);
3417         }
3418       rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_JMP_SLOT);
3419
3420       /* Adjust for the first 4 reserved elements in the .plt section
3421          when setting the offset in the .rela.plt section.
3422          Sun forgot to read their own ABI and copied elf32-sparc behaviour,
3423          thus .plt[4] has corresponding .rela.plt[0] and so on.  */
3424
3425       loc = srela->contents;
3426       if (ABI_64_P (output_bfd))
3427         {
3428           loc += rela_index * sizeof (Elf64_External_Rela);
3429           bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3430         }
3431       else
3432         {
3433           loc += rela_index * sizeof (Elf32_External_Rela);
3434           bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3435         }
3436
3437       if (!h->def_regular)
3438         {
3439           /* Mark the symbol as undefined, rather than as defined in
3440              the .plt section.  Leave the value alone.  */
3441           sym->st_shndx = SHN_UNDEF;
3442           /* If the symbol is weak, we do need to clear the value.
3443              Otherwise, the PLT entry would provide a definition for
3444              the symbol even if the symbol wasn't defined anywhere,
3445              and so the symbol would never be NULL.  */
3446           if (!h->ref_regular_nonweak)
3447             sym->st_value = 0;
3448         }
3449     }
3450
3451   if (h->got.offset != (bfd_vma) -1
3452       && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_GD
3453       && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_IE)
3454     {
3455       asection *sgot;
3456       asection *srela;
3457       Elf_Internal_Rela rela;
3458
3459       /* This symbol has an entry in the GOT.  Set it up.  */
3460
3461       sgot = htab->sgot;
3462       srela = htab->srelgot;
3463       BFD_ASSERT (sgot != NULL && srela != NULL);
3464
3465       rela.r_offset = (sgot->output_section->vma
3466                        + sgot->output_offset
3467                        + (h->got.offset &~ (bfd_vma) 1));
3468
3469       /* If this is a -Bsymbolic link, and the symbol is defined
3470          locally, we just want to emit a RELATIVE reloc.  Likewise if
3471          the symbol was forced to be local because of a version file.
3472          The entry in the global offset table will already have been
3473          initialized in the relocate_section function.  */
3474       if (info->shared
3475           && (info->symbolic || h->dynindx == -1)
3476           && h->def_regular)
3477         {
3478           asection *sec = h->root.u.def.section;
3479           rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, R_SPARC_RELATIVE);
3480           rela.r_addend = (h->root.u.def.value
3481                            + sec->output_section->vma
3482                            + sec->output_offset);
3483         }
3484       else
3485         {
3486           rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_GLOB_DAT);
3487           rela.r_addend = 0;
3488         }
3489
3490       SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
3491                           sgot->contents + (h->got.offset & ~(bfd_vma) 1));
3492       SPARC_ELF_APPEND_RELA (htab, output_bfd, srela, &rela);
3493     }
3494
3495   if (h->needs_copy)
3496     {
3497       asection *s;
3498       Elf_Internal_Rela rela;
3499
3500       /* This symbols needs a copy reloc.  Set it up.  */
3501       BFD_ASSERT (h->dynindx != -1);
3502
3503       s = bfd_get_section_by_name (h->root.u.def.section->owner,
3504                                    ".rela.bss");
3505       BFD_ASSERT (s != NULL);
3506
3507       rela.r_offset = (h->root.u.def.value
3508                        + h->root.u.def.section->output_section->vma
3509                        + h->root.u.def.section->output_offset);
3510       rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_COPY);
3511       rela.r_addend = 0;
3512       SPARC_ELF_APPEND_RELA (htab, output_bfd, s, &rela);
3513     }
3514
3515   /* Mark some specially defined symbols as absolute.  */
3516   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3517       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3518       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3519     sym->st_shndx = SHN_ABS;
3520
3521   return TRUE;
3522 }
3523
3524 /* Finish up the dynamic sections.  */
3525
3526 static bfd_boolean
3527 sparc64_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
3528                     bfd *dynobj, asection *sdyn,
3529                     asection *splt ATTRIBUTE_UNUSED)
3530 {
3531   Elf64_External_Dyn *dyncon, *dynconend;
3532   int stt_regidx = -1;
3533
3534   dyncon = (Elf64_External_Dyn *) sdyn->contents;
3535   dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3536   for (; dyncon < dynconend; dyncon++)
3537     {
3538       Elf_Internal_Dyn dyn;
3539       const char *name;
3540       bfd_boolean size;
3541
3542       bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3543
3544       switch (dyn.d_tag)
3545         {
3546         case DT_PLTGOT:   name = ".plt"; size = FALSE; break;
3547         case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
3548         case DT_JMPREL:   name = ".rela.plt"; size = FALSE; break;
3549         case DT_SPARC_REGISTER:
3550           if (stt_regidx == -1)
3551             {
3552               stt_regidx =
3553                 _bfd_elf_link_lookup_local_dynindx (info, output_bfd, -1);
3554               if (stt_regidx == -1)
3555                 return FALSE;
3556             }
3557           dyn.d_un.d_val = stt_regidx++;
3558           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3559           /* fallthrough */
3560         default:          name = NULL; size = FALSE; break;
3561         }
3562
3563       if (name != NULL)
3564         {
3565           asection *s;
3566
3567           s = bfd_get_section_by_name (output_bfd, name);
3568           if (s == NULL)
3569             dyn.d_un.d_val = 0;
3570           else
3571             {
3572               if (! size)
3573                 dyn.d_un.d_ptr = s->vma;
3574               else
3575                 dyn.d_un.d_val = s->size;
3576             }
3577           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3578         }
3579     }
3580   return TRUE;
3581 }
3582
3583 static bfd_boolean
3584 sparc32_finish_dyn (bfd *output_bfd,
3585                     struct bfd_link_info *info ATTRIBUTE_UNUSED,
3586                     bfd *dynobj, asection *sdyn,
3587                     asection *splt ATTRIBUTE_UNUSED)
3588 {
3589   Elf32_External_Dyn *dyncon, *dynconend;
3590
3591   dyncon = (Elf32_External_Dyn *) sdyn->contents;
3592   dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3593   for (; dyncon < dynconend; dyncon++)
3594     {
3595       Elf_Internal_Dyn dyn;
3596       const char *name;
3597       bfd_boolean size;
3598
3599       bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3600
3601       switch (dyn.d_tag)
3602         {
3603         case DT_PLTGOT:   name = ".plt"; size = FALSE; break;
3604         case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
3605         case DT_JMPREL:   name = ".rela.plt"; size = FALSE; break;
3606         default:          name = NULL; size = FALSE; break;
3607         }
3608
3609       if (name != NULL)
3610         {
3611           asection *s;
3612
3613           s = bfd_get_section_by_name (output_bfd, name);
3614           if (s == NULL)
3615             dyn.d_un.d_val = 0;
3616           else
3617             {
3618               if (! size)
3619                 dyn.d_un.d_ptr = s->vma;
3620               else
3621                 dyn.d_un.d_val = s->size;
3622             }
3623           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3624         }
3625     }
3626   return TRUE;
3627 }
3628
3629 bfd_boolean
3630 _bfd_sparc_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3631 {
3632   bfd *dynobj;
3633   asection *sdyn;
3634   struct _bfd_sparc_elf_link_hash_table *htab;
3635
3636   htab = _bfd_sparc_elf_hash_table (info);
3637   dynobj = htab->elf.dynobj;
3638
3639   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3640
3641   if (elf_hash_table (info)->dynamic_sections_created)
3642     {
3643       asection *splt;
3644       bfd_boolean ret;
3645
3646       splt = bfd_get_section_by_name (dynobj, ".plt");
3647       BFD_ASSERT (splt != NULL && sdyn != NULL);
3648
3649       if (ABI_64_P (output_bfd))
3650         ret = sparc64_finish_dyn (output_bfd, info, dynobj, sdyn, splt);
3651       else
3652         ret = sparc32_finish_dyn (output_bfd, info, dynobj, sdyn, splt);
3653
3654       if (ret != TRUE)
3655         return ret;
3656
3657       /* Initialize the contents of the .plt section.  */
3658       if (splt->size > 0)
3659         {
3660           if (ABI_64_P (output_bfd))
3661             memset (splt->contents, 0, 4 * PLT64_ENTRY_SIZE);
3662           else
3663             {
3664               memset (splt->contents, 0, 4 * PLT32_ENTRY_SIZE);
3665               bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
3666                           splt->contents + splt->size - 4);
3667             }
3668         }
3669
3670       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3671         (ABI_64_P (output_bfd) ? PLT64_ENTRY_SIZE : PLT32_ENTRY_SIZE);
3672     }
3673
3674   /* Set the first entry in the global offset table to the address of
3675      the dynamic section.  */
3676   if (htab->sgot && htab->sgot->size > 0)
3677     {
3678       bfd_vma val = (sdyn ?
3679                      sdyn->output_section->vma + sdyn->output_offset :
3680                      0);
3681
3682       SPARC_ELF_PUT_WORD (htab, output_bfd, val, htab->sgot->contents);
3683     }
3684
3685   if (htab->sgot)
3686     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize =
3687       SPARC_ELF_WORD_BYTES (htab);
3688
3689   return TRUE;
3690 }
3691
3692 \f
3693 /* Set the right machine number for a SPARC ELF file.  */
3694
3695 bfd_boolean
3696 _bfd_sparc_elf_object_p (bfd *abfd)
3697 {
3698   if (ABI_64_P (abfd))
3699     {
3700       unsigned long mach = bfd_mach_sparc_v9;
3701
3702       if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
3703         mach = bfd_mach_sparc_v9b;
3704       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
3705         mach = bfd_mach_sparc_v9a;
3706       return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach);
3707     }
3708   else
3709     {
3710       if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
3711         {
3712           if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
3713             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3714                                               bfd_mach_sparc_v8plusb);
3715           else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
3716             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3717                                               bfd_mach_sparc_v8plusa);
3718           else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
3719             return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3720                                               bfd_mach_sparc_v8plus);
3721           else
3722             return FALSE;
3723         }
3724       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
3725         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3726                                           bfd_mach_sparc_sparclite_le);
3727       else
3728         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
3729     }
3730 }
3731
3732 /* Return address for Ith PLT stub in section PLT, for relocation REL
3733    or (bfd_vma) -1 if it should not be included.  */
3734
3735 bfd_vma
3736 _bfd_sparc_elf_plt_sym_val (bfd_vma i, const asection *plt, const arelent *rel)
3737 {
3738   if (ABI_64_P (plt->owner))
3739     {
3740       bfd_vma j;
3741
3742       i += PLT64_HEADER_SIZE / PLT64_ENTRY_SIZE;
3743       if (i < PLT64_LARGE_THRESHOLD)
3744         return plt->vma + i * PLT64_ENTRY_SIZE;
3745
3746       j = (i - PLT64_LARGE_THRESHOLD) % 160;
3747       i -= j;
3748       return plt->vma + i * PLT64_ENTRY_SIZE + j * 4 * 6;
3749     }
3750   else
3751     return rel->address;
3752 }