* Many files: Changes to avoid gcc warnings: Add ATTRIBUTE_UNUSED
[external/binutils.git] / bfd / elf32-sparc.c
1 /* SPARC-specific support for 32-bit ELF
2    Copyright (C) 1993, 94, 95, 96, 97, 98, 1999 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 #include "bfd.h"
21 #include "sysdep.h"
22 #include "bfdlink.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/sparc.h"
26
27 static reloc_howto_type *elf32_sparc_reloc_type_lookup
28   PARAMS ((bfd *, bfd_reloc_code_real_type));
29 static void elf32_sparc_info_to_howto
30   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
31 static boolean elf32_sparc_check_relocs
32   PARAMS ((bfd *, struct bfd_link_info *, asection *,
33            const Elf_Internal_Rela *));
34 static boolean elf32_sparc_adjust_dynamic_symbol
35   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
36 static boolean elf32_sparc_adjust_dynindx
37   PARAMS ((struct elf_link_hash_entry *, PTR));
38 static boolean elf32_sparc_size_dynamic_sections
39   PARAMS ((bfd *, struct bfd_link_info *));
40 static boolean elf32_sparc_relocate_section
41   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
42            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
43 static boolean elf32_sparc_finish_dynamic_symbol
44   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
45            Elf_Internal_Sym *));
46 static boolean elf32_sparc_finish_dynamic_sections
47   PARAMS ((bfd *, struct bfd_link_info *));
48 static boolean elf32_sparc_merge_private_bfd_data PARAMS ((bfd *, bfd *));
49 static boolean elf32_sparc_object_p
50   PARAMS ((bfd *));
51 static void elf32_sparc_final_write_processing
52   PARAMS ((bfd *, boolean));
53 \f
54 /* The relocation "howto" table.  */
55
56 static bfd_reloc_status_type sparc_elf_notsupported_reloc
57   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
58 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
59   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
60
61 reloc_howto_type _bfd_sparc_elf_howto_table[] =
62 {
63   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
64   HOWTO(R_SPARC_8,         0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_8",       false,0,0x000000ff,true),
65   HOWTO(R_SPARC_16,        0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_16",      false,0,0x0000ffff,true),
66   HOWTO(R_SPARC_32,        0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_32",      false,0,0xffffffff,true),
67   HOWTO(R_SPARC_DISP8,     0,0, 8,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP8",   false,0,0x000000ff,true),
68   HOWTO(R_SPARC_DISP16,    0,1,16,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP16",  false,0,0x0000ffff,true),
69   HOWTO(R_SPARC_DISP32,    0,2,32,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP32",  false,0,0x00ffffff,true),
70   HOWTO(R_SPARC_WDISP30,   2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP30", false,0,0x3fffffff,true),
71   HOWTO(R_SPARC_WDISP22,   2,2,22,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP22", false,0,0x003fffff,true),
72   HOWTO(R_SPARC_HI22,     10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HI22",    false,0,0x003fffff,true),
73   HOWTO(R_SPARC_22,        0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_22",      false,0,0x003fffff,true),
74   HOWTO(R_SPARC_13,        0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_13",      false,0,0x00001fff,true),
75   HOWTO(R_SPARC_LO10,      0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LO10",    false,0,0x000003ff,true),
76   HOWTO(R_SPARC_GOT10,     0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT10",   false,0,0x000003ff,true),
77   HOWTO(R_SPARC_GOT13,     0,2,13,false,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_GOT13",   false,0,0x00001fff,true),
78   HOWTO(R_SPARC_GOT22,    10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT22",   false,0,0x003fffff,true),
79   HOWTO(R_SPARC_PC10,      0,2,10,true, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC10",    false,0,0x000003ff,true),
80   HOWTO(R_SPARC_PC22,     10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PC22",    false,0,0x003fffff,true),
81   HOWTO(R_SPARC_WPLT30,    2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WPLT30",  false,0,0x3fffffff,true),
82   HOWTO(R_SPARC_COPY,      0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_COPY",    false,0,0x00000000,true),
83   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),
84   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),
85   HOWTO(R_SPARC_RELATIVE,  0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_RELATIVE",false,0,0x00000000,true),
86   HOWTO(R_SPARC_UA32,      0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_UA32",    false,0,0x00000000,true),
87   HOWTO(R_SPARC_PLT32,     0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PLT32",    false,0,0x00000000,true),
88   HOWTO(R_SPARC_HIPLT22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HIPLT22",  false,0,0x00000000,true),
89   HOWTO(R_SPARC_LOPLT10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_LOPLT10",  false,0,0x00000000,true),
90   HOWTO(R_SPARC_PCPLT32,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT32",  false,0,0x00000000,true),
91   HOWTO(R_SPARC_PCPLT22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT22",  false,0,0x00000000,true),
92   HOWTO(R_SPARC_PCPLT10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT10",  false,0,0x00000000,true),
93   HOWTO(R_SPARC_10,        0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_10",      false,0,0x000003ff,true),
94   HOWTO(R_SPARC_11,        0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_11",      false,0,0x000007ff,true),
95   /* These are for sparc64 in a 64 bit environment.
96      Values need to be here because the table is indexed by reloc number.  */
97   HOWTO(R_SPARC_64,        0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_64",      false,0,0x00000000,true),
98   HOWTO(R_SPARC_OLO10,     0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_OLO10",   false,0,0x00000000,true),
99   HOWTO(R_SPARC_HH22,      0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HH22",    false,0,0x00000000,true),
100   HOWTO(R_SPARC_HM10,      0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HM10",    false,0,0x00000000,true),
101   HOWTO(R_SPARC_LM22,      0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_LM22",    false,0,0x00000000,true),
102   HOWTO(R_SPARC_PC_HH22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PC_HH22", false,0,0x00000000,true),
103   HOWTO(R_SPARC_PC_HM10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PC_HM10", false,0,0x00000000,true),
104   HOWTO(R_SPARC_PC_LM22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PC_LM22", false,0,0x00000000,true),
105   /* End sparc64 in 64 bit environment values.
106      The following are for sparc64 in a 32 bit environment.  */
107   HOWTO(R_SPARC_WDISP16,   2,2,16,true, 0,complain_overflow_signed,  sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
108   HOWTO(R_SPARC_WDISP19,   2,2,19,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP19", false,0,0x0007ffff,true),
109   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),
110   HOWTO(R_SPARC_7,         0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_7",       false,0,0x0000007f,true),
111   HOWTO(R_SPARC_5,         0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_5",       false,0,0x0000001f,true),
112   HOWTO(R_SPARC_6,         0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_6",       false,0,0x0000003f,true),
113   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
114   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
115   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
116   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
117   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
118   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
119   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
120   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
121   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
122   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
123   HOWTO(R_SPARC_REV32,      0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_REV32",      false,0,0xffffffff,true),
124 };
125 static reloc_howto_type elf32_sparc_vtinherit_howto =
126   HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", false,0, 0, false);
127 static reloc_howto_type elf32_sparc_vtentry_howto =
128   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);
129
130
131 struct elf_reloc_map {
132   bfd_reloc_code_real_type bfd_reloc_val;
133   unsigned char elf_reloc_val;
134 };
135
136 static CONST struct elf_reloc_map sparc_reloc_map[] =
137 {
138   { BFD_RELOC_NONE, R_SPARC_NONE, },
139   { BFD_RELOC_16, R_SPARC_16, },
140   { BFD_RELOC_8, R_SPARC_8 },
141   { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
142   { BFD_RELOC_CTOR, R_SPARC_32 },
143   { BFD_RELOC_32, R_SPARC_32 },
144   { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
145   { BFD_RELOC_HI22, R_SPARC_HI22 },
146   { BFD_RELOC_LO10, R_SPARC_LO10, },
147   { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
148   { BFD_RELOC_SPARC22, R_SPARC_22 },
149   { BFD_RELOC_SPARC13, R_SPARC_13 },
150   { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
151   { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
152   { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
153   { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
154   { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
155   { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
156   { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
157   { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
158   { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
159   { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
160   { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
161   /* ??? Doesn't dwarf use this?  */
162 /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
163   {BFD_RELOC_SPARC_10, R_SPARC_10},
164   {BFD_RELOC_SPARC_11, R_SPARC_11},
165   {BFD_RELOC_SPARC_64, R_SPARC_64},
166   {BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10},
167   {BFD_RELOC_SPARC_HH22, R_SPARC_HH22},
168   {BFD_RELOC_SPARC_HM10, R_SPARC_HM10},
169   {BFD_RELOC_SPARC_LM22, R_SPARC_LM22},
170   {BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22},
171   {BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10},
172   {BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22},
173   {BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16},
174   {BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19},
175   {BFD_RELOC_SPARC_7, R_SPARC_7},
176   {BFD_RELOC_SPARC_5, R_SPARC_5},
177   {BFD_RELOC_SPARC_6, R_SPARC_6},
178   {BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
179   {BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT},
180   {BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY},
181 };
182
183 static reloc_howto_type *
184 elf32_sparc_reloc_type_lookup (abfd, code)
185      bfd *abfd ATTRIBUTE_UNUSED;
186      bfd_reloc_code_real_type code;
187 {
188   unsigned int i;
189   
190   switch (code)
191     {
192     case BFD_RELOC_VTABLE_INHERIT:
193       return &elf32_sparc_vtinherit_howto;
194
195     case BFD_RELOC_VTABLE_ENTRY:
196       return &elf32_sparc_vtentry_howto;
197
198     default:
199       for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
200         {
201           if (sparc_reloc_map[i].bfd_reloc_val == code)
202             return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
203         }
204     }
205     bfd_set_error (bfd_error_bad_value);
206     return NULL;
207 }
208
209 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
210    and elf64-sparc.c has its own copy.  */
211
212 static void
213 elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
214      bfd *abfd ATTRIBUTE_UNUSED;
215      arelent *cache_ptr;
216      Elf_Internal_Rela *dst;
217 {
218   switch (ELF32_R_TYPE(dst->r_info))
219     {
220     case R_SPARC_GNU_VTINHERIT:
221       cache_ptr->howto = &elf32_sparc_vtinherit_howto;
222       break;
223
224     case R_SPARC_GNU_VTENTRY:
225       cache_ptr->howto = &elf32_sparc_vtentry_howto;
226       break;
227
228     default:
229       BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
230       cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
231     }
232 }
233 \f
234 /* For unsupported relocs.  */
235
236 static bfd_reloc_status_type
237 sparc_elf_notsupported_reloc (abfd,
238                              reloc_entry,
239                              symbol,
240                              data,
241                              input_section,
242                              output_bfd,
243                              error_message)
244      bfd *abfd ATTRIBUTE_UNUSED;
245      arelent *reloc_entry ATTRIBUTE_UNUSED;
246      asymbol *symbol ATTRIBUTE_UNUSED;
247      PTR data ATTRIBUTE_UNUSED;
248      asection *input_section ATTRIBUTE_UNUSED;
249      bfd *output_bfd ATTRIBUTE_UNUSED;
250      char **error_message ATTRIBUTE_UNUSED;
251 {
252   return bfd_reloc_notsupported;
253 }
254
255 /* Handle the WDISP16 reloc.  */
256
257 static bfd_reloc_status_type
258 sparc_elf_wdisp16_reloc (abfd,
259                          reloc_entry,
260                          symbol,
261                          data,
262                          input_section,
263                          output_bfd,
264                          error_message)
265      bfd *abfd;
266      arelent *reloc_entry;
267      asymbol *symbol;
268      PTR data;
269      asection *input_section;
270      bfd *output_bfd;
271      char **error_message ATTRIBUTE_UNUSED;
272 {
273   bfd_vma relocation;
274   bfd_vma x;
275
276   if (output_bfd != (bfd *) NULL
277       && (symbol->flags & BSF_SECTION_SYM) == 0
278       && (! reloc_entry->howto->partial_inplace
279           || reloc_entry->addend == 0))
280     {
281       reloc_entry->address += input_section->output_offset;
282       return bfd_reloc_ok;
283     }
284
285   if (output_bfd != NULL)
286     return bfd_reloc_continue;
287
288   if (reloc_entry->address > input_section->_cooked_size)
289     return bfd_reloc_outofrange;
290
291   relocation = (symbol->value
292                 + symbol->section->output_section->vma
293                 + symbol->section->output_offset);
294   relocation += reloc_entry->addend;
295   relocation -= (input_section->output_section->vma
296                  + input_section->output_offset);
297   relocation -= reloc_entry->address;
298
299   x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
300   x |= ((((relocation >> 2) & 0xc000) << 6)
301         | ((relocation >> 2) & 0x3fff));
302   bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
303
304   if ((bfd_signed_vma) relocation < - 0x40000
305       || (bfd_signed_vma) relocation > 0x3ffff)
306     return bfd_reloc_overflow;
307   else
308     return bfd_reloc_ok;
309 }
310 \f
311 /* Functions for the SPARC ELF linker.  */
312
313 /* The name of the dynamic interpreter.  This is put in the .interp
314    section.  */
315
316 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
317
318 /* The nop opcode we use.  */
319
320 #define SPARC_NOP 0x01000000
321
322 /* The size in bytes of an entry in the procedure linkage table.  */
323
324 #define PLT_ENTRY_SIZE 12
325
326 /* The first four entries in a procedure linkage table are reserved,
327    and the initial contents are unimportant (we zero them out).
328    Subsequent entries look like this.  See the SVR4 ABI SPARC
329    supplement to see how this works.  */
330
331 /* sethi %hi(.-.plt0),%g1.  We fill in the address later.  */
332 #define PLT_ENTRY_WORD0 0x03000000
333 /* b,a .plt0.  We fill in the offset later.  */
334 #define PLT_ENTRY_WORD1 0x30800000
335 /* nop.  */
336 #define PLT_ENTRY_WORD2 SPARC_NOP
337
338 /* Look through the relocs for a section during the first phase, and
339    allocate space in the global offset table or procedure linkage
340    table.  */
341
342 static boolean
343 elf32_sparc_check_relocs (abfd, info, sec, relocs)
344      bfd *abfd;
345      struct bfd_link_info *info;
346      asection *sec;
347      const Elf_Internal_Rela *relocs;
348 {
349   bfd *dynobj;
350   Elf_Internal_Shdr *symtab_hdr;
351   struct elf_link_hash_entry **sym_hashes;
352   bfd_vma *local_got_offsets;
353   const Elf_Internal_Rela *rel;
354   const Elf_Internal_Rela *rel_end;
355   asection *sgot;
356   asection *srelgot;
357   asection *sreloc;
358
359   if (info->relocateable)
360     return true;
361
362   dynobj = elf_hash_table (info)->dynobj;
363   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
364   sym_hashes = elf_sym_hashes (abfd);
365   local_got_offsets = elf_local_got_offsets (abfd);
366
367   sgot = NULL;
368   srelgot = NULL;
369   sreloc = NULL;
370
371   rel_end = relocs + sec->reloc_count;
372   for (rel = relocs; rel < rel_end; rel++)
373     {
374       unsigned long r_symndx;
375       struct elf_link_hash_entry *h;
376
377       r_symndx = ELF32_R_SYM (rel->r_info);
378       if (r_symndx < symtab_hdr->sh_info)
379         h = NULL;
380       else
381         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
382
383       switch (ELF32_R_TYPE (rel->r_info))
384         {
385         case R_SPARC_GOT10:
386         case R_SPARC_GOT13:
387         case R_SPARC_GOT22:
388           /* This symbol requires a global offset table entry.  */
389
390           if (dynobj == NULL)
391             {
392               /* Create the .got section.  */
393               elf_hash_table (info)->dynobj = dynobj = abfd;
394               if (! _bfd_elf_create_got_section (dynobj, info))
395                 return false;
396             }
397
398           if (sgot == NULL)
399             {
400               sgot = bfd_get_section_by_name (dynobj, ".got");
401               BFD_ASSERT (sgot != NULL);
402             }
403
404           if (srelgot == NULL
405               && (h != NULL || info->shared))
406             {
407               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
408               if (srelgot == NULL)
409                 {
410                   srelgot = bfd_make_section (dynobj, ".rela.got");
411                   if (srelgot == NULL
412                       || ! bfd_set_section_flags (dynobj, srelgot,
413                                                   (SEC_ALLOC
414                                                    | SEC_LOAD
415                                                    | SEC_HAS_CONTENTS
416                                                    | SEC_IN_MEMORY
417                                                    | SEC_LINKER_CREATED
418                                                    | SEC_READONLY))
419                       || ! bfd_set_section_alignment (dynobj, srelgot, 2))
420                     return false;
421                 }
422             }
423
424           if (h != NULL)
425             {
426               if (h->got.offset != (bfd_vma) -1)
427                 {
428                   /* We have already allocated space in the .got.  */
429                   break;
430                 }
431               h->got.offset = sgot->_raw_size;
432
433               /* Make sure this symbol is output as a dynamic symbol.  */
434               if (h->dynindx == -1)
435                 {
436                   if (! bfd_elf32_link_record_dynamic_symbol (info, h))
437                     return false;
438                 }
439
440               srelgot->_raw_size += sizeof (Elf32_External_Rela);
441             }
442           else
443             {
444               /* This is a global offset table entry for a local
445                  symbol.  */
446               if (local_got_offsets == NULL)
447                 {
448                   size_t size;
449                   register unsigned int i;
450
451                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
452                   local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
453                   if (local_got_offsets == NULL)
454                     return false;
455                   elf_local_got_offsets (abfd) = local_got_offsets;
456                   for (i = 0; i < symtab_hdr->sh_info; i++)
457                     local_got_offsets[i] = (bfd_vma) -1;
458                 }
459               if (local_got_offsets[r_symndx] != (bfd_vma) -1)
460                 {
461                   /* We have already allocated space in the .got.  */
462                   break;
463                 }
464               local_got_offsets[r_symndx] = sgot->_raw_size;
465
466               if (info->shared)
467                 {
468                   /* If we are generating a shared object, we need to
469                      output a R_SPARC_RELATIVE reloc so that the
470                      dynamic linker can adjust this GOT entry.  */
471                   srelgot->_raw_size += sizeof (Elf32_External_Rela);
472                 }
473             }
474
475           sgot->_raw_size += 4;
476
477           /* If the .got section is more than 0x1000 bytes, we add
478              0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
479              bit relocations have a greater chance of working.  */
480           if (sgot->_raw_size >= 0x1000
481               && elf_hash_table (info)->hgot->root.u.def.value == 0)
482             elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
483
484           break;
485
486         case R_SPARC_WPLT30:
487           /* This symbol requires a procedure linkage table entry.  We
488              actually build the entry in adjust_dynamic_symbol,
489              because this might be a case of linking PIC code without
490              linking in any dynamic objects, in which case we don't
491              need to generate a procedure linkage table after all.  */
492
493           if (h == NULL)
494             {
495               /* The Solaris native assembler will generate a WPLT30
496                  reloc for a local symbol if you assemble a call from
497                  one section to another when using -K pic.  We treat
498                  it as WDISP30.  */
499               break;
500             }
501
502           /* Make sure this symbol is output as a dynamic symbol.  */
503           if (h->dynindx == -1)
504             {
505               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
506                 return false;
507             }
508
509           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
510
511           break;
512
513         case R_SPARC_PC10:
514         case R_SPARC_PC22:
515           if (h != NULL
516               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
517             break;
518           /* Fall through.  */
519         case R_SPARC_DISP8:
520         case R_SPARC_DISP16:
521         case R_SPARC_DISP32:
522         case R_SPARC_WDISP30:
523         case R_SPARC_WDISP22:
524         case R_SPARC_WDISP19:
525         case R_SPARC_WDISP16:
526           /* If we are linking with -Bsymbolic, we do not need to copy
527              a PC relative reloc against a global symbol which is
528              defined in an object we are including in the link (i.e.,
529              DEF_REGULAR is set).  FIXME: At this point we have not
530              seen all the input files, so it is possible that
531              DEF_REGULAR is not set now but will be set later (it is
532              never cleared).  This needs to be handled as in
533              elf32-i386.c.  */
534           if (h == NULL
535               || (info->symbolic
536                   && (h->elf_link_hash_flags
537                       & ELF_LINK_HASH_DEF_REGULAR) != 0))
538             break;
539           /* Fall through.  */
540         case R_SPARC_8:
541         case R_SPARC_16:
542         case R_SPARC_32:
543         case R_SPARC_HI22:
544         case R_SPARC_22:
545         case R_SPARC_13:
546         case R_SPARC_LO10:
547         case R_SPARC_UA32:
548           if (info->shared)
549             {
550               /* When creating a shared object, we must copy these
551                  relocs into the output file.  We create a reloc
552                  section in dynobj and make room for the reloc.  */
553               if (sreloc == NULL)
554                 {
555                   const char *name;
556
557                   name = (bfd_elf_string_from_elf_section
558                           (abfd,
559                            elf_elfheader (abfd)->e_shstrndx,
560                            elf_section_data (sec)->rel_hdr.sh_name));
561                   if (name == NULL)
562                     return false;
563
564                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
565                               && strcmp (bfd_get_section_name (abfd, sec),
566                                          name + 5) == 0);
567
568                   sreloc = bfd_get_section_by_name (dynobj, name);
569                   if (sreloc == NULL)
570                     {
571                       flagword flags;
572
573                       sreloc = bfd_make_section (dynobj, name);
574                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
575                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
576                       if ((sec->flags & SEC_ALLOC) != 0)
577                         flags |= SEC_ALLOC | SEC_LOAD;
578                       if (sreloc == NULL
579                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
580                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
581                         return false;
582                     }
583                 }
584
585               sreloc->_raw_size += sizeof (Elf32_External_Rela);
586             }
587
588           break;
589
590         case R_SPARC_GNU_VTINHERIT:
591           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
592             return false;
593           break;
594
595         case R_SPARC_GNU_VTENTRY:
596           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
597             return false;
598           break;
599
600         default:
601           break;
602         }
603     }
604
605   return true;
606 }
607
608 static asection *
609 elf32_sparc_gc_mark_hook (abfd, info, rel, h, sym)
610        bfd *abfd;
611        struct bfd_link_info *info ATTRIBUTE_UNUSED;
612        Elf_Internal_Rela *rel;
613        struct elf_link_hash_entry *h;
614        Elf_Internal_Sym *sym;
615 {
616
617   if (h != NULL)
618     {
619       switch (ELF32_R_TYPE (rel->r_info))
620       {
621       case R_SPARC_GNU_VTINHERIT:
622       case R_SPARC_GNU_VTENTRY:
623         break;
624
625       default:
626         switch (h->root.type)
627           {
628           case bfd_link_hash_defined:
629           case bfd_link_hash_defweak:
630             return h->root.u.def.section;
631
632           case bfd_link_hash_common:
633             return h->root.u.c.p->section;
634
635           default:
636             break;
637           }
638        }
639      }
640    else
641      {
642        if (!(elf_bad_symtab (abfd)
643            && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
644          && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
645                 && sym->st_shndx != SHN_COMMON))
646           {
647             return bfd_section_from_elf_index (abfd, sym->st_shndx);
648           }
649       }
650
651   return NULL;
652 }
653
654 /* Update the got entry reference counts for the section being removed.  */
655 static boolean
656 elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
657      bfd *abfd;
658      struct bfd_link_info *info ATTRIBUTE_UNUSED;
659      asection *sec;
660      const Elf_Internal_Rela *relocs;
661 {
662
663   Elf_Internal_Shdr *symtab_hdr;
664   struct elf_link_hash_entry **sym_hashes;
665   bfd_signed_vma *local_got_refcounts;
666   const Elf_Internal_Rela *rel, *relend;
667   unsigned long r_symndx;
668   struct elf_link_hash_entry *h;
669
670   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
671   sym_hashes = elf_sym_hashes (abfd);
672   local_got_refcounts = elf_local_got_refcounts (abfd);
673
674   relend = relocs + sec->reloc_count;
675   for (rel = relocs; rel < relend; rel++)
676     switch (ELF32_R_TYPE (rel->r_info))
677       {
678       case R_SPARC_GOT10:
679       case R_SPARC_GOT13:
680       case R_SPARC_GOT22:
681         r_symndx = ELF32_R_SYM (rel->r_info);
682         if (r_symndx >= symtab_hdr->sh_info)
683           {
684             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
685             if (h->got.refcount > 0)
686               h->got.refcount--;
687           }
688         else
689           {
690             if (local_got_refcounts[r_symndx] > 0)
691               local_got_refcounts[r_symndx]--;
692           }
693         break;
694
695       case R_SPARC_PLT32:
696       case R_SPARC_HIPLT22:
697       case R_SPARC_LOPLT10:
698       case R_SPARC_PCPLT32:
699       case R_SPARC_PCPLT10:
700         r_symndx = ELF32_R_SYM (rel->r_info);
701         if (r_symndx >= symtab_hdr->sh_info)
702           {
703             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
704             if (h->plt.refcount > 0)
705               h->plt.refcount--;
706           }
707         break;
708
709       default:
710         break;
711       }
712
713   return true;
714 }
715
716 /* Adjust a symbol defined by a dynamic object and referenced by a
717    regular object.  The current definition is in some section of the
718    dynamic object, but we're not including those sections.  We have to
719    change the definition to something the rest of the link can
720    understand.  */
721
722 static boolean
723 elf32_sparc_adjust_dynamic_symbol (info, h)
724      struct bfd_link_info *info;
725      struct elf_link_hash_entry *h;
726 {
727   bfd *dynobj;
728   asection *s;
729   unsigned int power_of_two;
730
731   dynobj = elf_hash_table (info)->dynobj;
732
733   /* Make sure we know what is going on here.  */
734   BFD_ASSERT (dynobj != NULL
735               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
736                   || h->weakdef != NULL
737                   || ((h->elf_link_hash_flags
738                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
739                       && (h->elf_link_hash_flags
740                           & ELF_LINK_HASH_REF_REGULAR) != 0
741                       && (h->elf_link_hash_flags
742                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
743
744   /* If this is a function, put it in the procedure linkage table.  We
745      will fill in the contents of the procedure linkage table later
746      (although we could actually do it here).  The STT_NOTYPE
747      condition is a hack specifically for the Oracle libraries
748      delivered for Solaris; for some inexplicable reason, they define
749      some of their functions as STT_NOTYPE when they really should be
750      STT_FUNC.  */
751   if (h->type == STT_FUNC
752       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
753       || (h->type == STT_NOTYPE
754           && (h->root.type == bfd_link_hash_defined
755               || h->root.type == bfd_link_hash_defweak)
756           && (h->root.u.def.section->flags & SEC_CODE) != 0))
757     {
758       if (! elf_hash_table (info)->dynamic_sections_created
759           || ((!info->shared || info->symbolic || h->dynindx == -1)
760               && (h->elf_link_hash_flags
761                   & ELF_LINK_HASH_DEF_REGULAR) != 0))
762         {
763           /* This case can occur if we saw a WPLT30 reloc in an input
764              file, but none of the input files were dynamic objects.
765              Or, when linking the main application or a -Bsymbolic
766              shared library against PIC code.  Or when a global symbol
767              has been made private, e.g. via versioning.
768
769              In these cases we know what value the symbol will resolve
770              to, so we don't actually need to build a procedure linkage
771              table, and we can just do a WDISP30 reloc instead.  */
772
773           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
774           return true;
775         }
776
777       s = bfd_get_section_by_name (dynobj, ".plt");
778       BFD_ASSERT (s != NULL);
779
780       /* The first four entries in .plt are reserved.  */
781       if (s->_raw_size == 0)
782         s->_raw_size = 4 * PLT_ENTRY_SIZE;
783
784       /* The procedure linkage table has a maximum size.  */
785       if (s->_raw_size >= 0x400000)
786         {
787           bfd_set_error (bfd_error_bad_value);
788           return false;
789         }
790
791      /* If this symbol is not defined in a regular file, and we are
792        not generating a shared library, then set the symbol to this
793        location in the .plt.  This is required to make function
794        pointers compare as equal between the normal executable and
795        the shared library.  */
796      if (! info->shared
797         && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
798       {
799         h->root.u.def.section = s;
800         h->root.u.def.value = s->_raw_size;
801       }
802
803       h->plt.offset = s->_raw_size;
804
805       /* Make room for this entry.  */
806       s->_raw_size += PLT_ENTRY_SIZE;
807
808       /* We also need to make an entry in the .rela.plt section.  */
809
810       s = bfd_get_section_by_name (dynobj, ".rela.plt");
811       BFD_ASSERT (s != NULL);
812       s->_raw_size += sizeof (Elf32_External_Rela);
813
814       return true;
815     }
816
817   /* If this is a weak symbol, and there is a real definition, the
818      processor independent code will have arranged for us to see the
819      real definition first, and we can just use the same value.  */
820   if (h->weakdef != NULL)
821     {
822       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
823                   || h->weakdef->root.type == bfd_link_hash_defweak);
824       h->root.u.def.section = h->weakdef->root.u.def.section;
825       h->root.u.def.value = h->weakdef->root.u.def.value;
826       return true;
827     }
828
829   /* This is a reference to a symbol defined by a dynamic object which
830      is not a function.  */
831
832   /* If we are creating a shared library, we must presume that the
833      only references to the symbol are via the global offset table.
834      For such cases we need not do anything here; the relocations will
835      be handled correctly by relocate_section.  */
836   if (info->shared)
837     return true;
838
839   /* We must allocate the symbol in our .dynbss section, which will
840      become part of the .bss section of the executable.  There will be
841      an entry for this symbol in the .dynsym section.  The dynamic
842      object will contain position independent code, so all references
843      from the dynamic object to this symbol will go through the global
844      offset table.  The dynamic linker will use the .dynsym entry to
845      determine the address it must put in the global offset table, so
846      both the dynamic object and the regular object will refer to the
847      same memory location for the variable.  */
848
849   s = bfd_get_section_by_name (dynobj, ".dynbss");
850   BFD_ASSERT (s != NULL);
851
852   /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
853      to copy the initial value out of the dynamic object and into the
854      runtime process image.  We need to remember the offset into the
855      .rel.bss section we are going to use.  */
856   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
857     {
858       asection *srel;
859
860       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
861       BFD_ASSERT (srel != NULL);
862       srel->_raw_size += sizeof (Elf32_External_Rela);
863       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
864     }
865
866   /* We need to figure out the alignment required for this symbol.  I
867      have no idea how ELF linkers handle this.  */
868   power_of_two = bfd_log2 (h->size);
869   if (power_of_two > 3)
870     power_of_two = 3;
871
872   /* Apply the required alignment.  */
873   s->_raw_size = BFD_ALIGN (s->_raw_size,
874                             (bfd_size_type) (1 << power_of_two));
875   if (power_of_two > bfd_get_section_alignment (dynobj, s))
876     {
877       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
878         return false;
879     }
880
881   /* Define the symbol as being at this point in the section.  */
882   h->root.u.def.section = s;
883   h->root.u.def.value = s->_raw_size;
884
885   /* Increment the section size to make room for the symbol.  */
886   s->_raw_size += h->size;
887
888   return true;
889 }
890
891 /* Set the sizes of the dynamic sections.  */
892
893 static boolean
894 elf32_sparc_size_dynamic_sections (output_bfd, info)
895      bfd *output_bfd;
896      struct bfd_link_info *info;
897 {
898   bfd *dynobj;
899   asection *s;
900   boolean reltext;
901   boolean relplt;
902
903   dynobj = elf_hash_table (info)->dynobj;
904   BFD_ASSERT (dynobj != NULL);
905
906   if (elf_hash_table (info)->dynamic_sections_created)
907     {
908       /* Set the contents of the .interp section to the interpreter.  */
909       if (! info->shared)
910         {
911           s = bfd_get_section_by_name (dynobj, ".interp");
912           BFD_ASSERT (s != NULL);
913           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
914           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
915         }
916
917       /* Make space for the trailing nop in .plt.  */
918       s = bfd_get_section_by_name (dynobj, ".plt");
919       BFD_ASSERT (s != NULL);
920       if (s->_raw_size > 0)
921         s->_raw_size += 4;
922     }
923   else
924     {
925       /* We may have created entries in the .rela.got section.
926          However, if we are not creating the dynamic sections, we will
927          not actually use these entries.  Reset the size of .rela.got,
928          which will cause it to get stripped from the output file
929          below.  */
930       s = bfd_get_section_by_name (dynobj, ".rela.got");
931       if (s != NULL)
932         s->_raw_size = 0;
933     }
934
935   /* The check_relocs and adjust_dynamic_symbol entry points have
936      determined the sizes of the various dynamic sections.  Allocate
937      memory for them.  */
938   reltext = false;
939   relplt = false;
940   for (s = dynobj->sections; s != NULL; s = s->next)
941     {
942       const char *name;
943       boolean strip;
944
945       if ((s->flags & SEC_LINKER_CREATED) == 0)
946         continue;
947
948       /* It's OK to base decisions on the section name, because none
949          of the dynobj section names depend upon the input files.  */
950       name = bfd_get_section_name (dynobj, s);
951
952       strip = false;
953
954       if (strncmp (name, ".rela", 5) == 0)
955         {
956           if (s->_raw_size == 0)
957             {
958               /* If we don't need this section, strip it from the
959                  output file.  This is to handle .rela.bss and
960                  .rel.plt.  We must create it in
961                  create_dynamic_sections, because it must be created
962                  before the linker maps input sections to output
963                  sections.  The linker does that before
964                  adjust_dynamic_symbol is called, and it is that
965                  function which decides whether anything needs to go
966                  into these sections.  */
967               strip = true;
968             }
969           else
970             {
971               const char *outname;
972               asection *target;
973
974               /* If this relocation section applies to a read only
975                  section, then we probably need a DT_TEXTREL entry.  */
976               outname = bfd_get_section_name (output_bfd,
977                                               s->output_section);
978               target = bfd_get_section_by_name (output_bfd, outname + 5);
979               if (target != NULL
980                   && (target->flags & SEC_READONLY) != 0
981                   && (target->flags & SEC_ALLOC) != 0)
982                 reltext = true;
983
984               if (strcmp (name, ".rela.plt") == 0)
985                 relplt = true;
986
987               /* We use the reloc_count field as a counter if we need
988                  to copy relocs into the output file.  */
989               s->reloc_count = 0;
990             }
991         }
992       else if (strcmp (name, ".plt") != 0
993                && strcmp (name, ".got") != 0)
994         {
995           /* It's not one of our sections, so don't allocate space.  */
996           continue;
997         }
998
999       if (strip)
1000         {
1001           _bfd_strip_section_from_output (s);
1002           continue;
1003         }
1004
1005       /* Allocate memory for the section contents.  */
1006       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1007       if (s->contents == NULL && s->_raw_size != 0)
1008         return false;
1009     }
1010
1011   if (elf_hash_table (info)->dynamic_sections_created)
1012     {
1013       /* Add some entries to the .dynamic section.  We fill in the
1014          values later, in elf32_sparc_finish_dynamic_sections, but we
1015          must add the entries now so that we get the correct size for
1016          the .dynamic section.  The DT_DEBUG entry is filled in by the
1017          dynamic linker and used by the debugger.  */
1018       if (! info->shared)
1019         {
1020           if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1021             return false;
1022         }
1023
1024       if (relplt)
1025         {
1026           if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1027               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1028               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1029               || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1030             return false;
1031         }
1032
1033       if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1034           || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1035           || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1036                                             sizeof (Elf32_External_Rela)))
1037         return false;
1038
1039       if (reltext)
1040         {
1041           if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1042             return false;
1043         }
1044     }
1045
1046   /* If we are generating a shared library, we generate a section
1047      symbol for each output section for which we might need to copy
1048      relocs.  These are local symbols, which means that they must come
1049      first in the dynamic symbol table.  That means we must increment
1050      the dynamic symbol index of every other dynamic symbol.  */
1051   if (info->shared)
1052     {
1053       int c;
1054
1055       c = 0;
1056       for (s = output_bfd->sections; s != NULL; s = s->next)
1057         {
1058           if ((s->flags & SEC_LINKER_CREATED) != 0
1059               || (s->flags & SEC_ALLOC) == 0)
1060             continue;
1061
1062           elf_section_data (s)->dynindx = c + 1;
1063
1064           /* These symbols will have no names, so we don't need to
1065              fiddle with dynstr_index.  */
1066
1067           ++c;
1068         }
1069
1070       elf_link_hash_traverse (elf_hash_table (info),
1071                               elf32_sparc_adjust_dynindx,
1072                               (PTR) &c);
1073       elf_hash_table (info)->dynsymcount += c;
1074     }
1075
1076   return true;
1077 }
1078
1079 /* Increment the index of a dynamic symbol by a given amount.  Called
1080    via elf_link_hash_traverse.  */
1081
1082 static boolean
1083 elf32_sparc_adjust_dynindx (h, cparg)
1084      struct elf_link_hash_entry *h;
1085      PTR cparg;
1086 {
1087   int *cp = (int *) cparg;
1088
1089   if (h->dynindx != -1)
1090     h->dynindx += *cp;
1091   return true;
1092 }
1093
1094 /* Relocate a SPARC ELF section.  */
1095
1096 static boolean
1097 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
1098                               contents, relocs, local_syms, local_sections)
1099      bfd *output_bfd;
1100      struct bfd_link_info *info;
1101      bfd *input_bfd;
1102      asection *input_section;
1103      bfd_byte *contents;
1104      Elf_Internal_Rela *relocs;
1105      Elf_Internal_Sym *local_syms;
1106      asection **local_sections;
1107 {
1108   bfd *dynobj;
1109   Elf_Internal_Shdr *symtab_hdr;
1110   struct elf_link_hash_entry **sym_hashes;
1111   bfd_vma *local_got_offsets;
1112   bfd_vma got_base;
1113   asection *sgot;
1114   asection *splt;
1115   asection *sreloc;
1116   Elf_Internal_Rela *rel;
1117   Elf_Internal_Rela *relend;
1118
1119   dynobj = elf_hash_table (info)->dynobj;
1120   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1121   sym_hashes = elf_sym_hashes (input_bfd);
1122   local_got_offsets = elf_local_got_offsets (input_bfd);
1123
1124   if (elf_hash_table (info)->hgot == NULL)
1125     got_base = 0;
1126   else
1127     got_base = elf_hash_table (info)->hgot->root.u.def.value;
1128
1129   sgot = NULL;
1130   splt = NULL;
1131   sreloc = NULL;
1132
1133   rel = relocs;
1134   relend = relocs + input_section->reloc_count;
1135   for (; rel < relend; rel++)
1136     {
1137       int r_type;
1138       reloc_howto_type *howto;
1139       unsigned long r_symndx;
1140       struct elf_link_hash_entry *h;
1141       Elf_Internal_Sym *sym;
1142       asection *sec;
1143       bfd_vma relocation;
1144       bfd_reloc_status_type r;
1145
1146       r_type = ELF32_R_TYPE (rel->r_info);
1147
1148       if (r_type == R_SPARC_GNU_VTINHERIT 
1149           || r_type == R_SPARC_GNU_VTENTRY)
1150         continue;
1151
1152       if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
1153         {
1154           bfd_set_error (bfd_error_bad_value);
1155           return false;
1156         }
1157       howto = _bfd_sparc_elf_howto_table + r_type;
1158
1159       r_symndx = ELF32_R_SYM (rel->r_info);
1160
1161       if (info->relocateable)
1162         {
1163           /* This is a relocateable link.  We don't have to change
1164              anything, unless the reloc is against a section symbol,
1165              in which case we have to adjust according to where the
1166              section symbol winds up in the output section.  */
1167           if (r_symndx < symtab_hdr->sh_info)
1168             {
1169               sym = local_syms + r_symndx;
1170               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1171                 {
1172                   sec = local_sections[r_symndx];
1173                   rel->r_addend += sec->output_offset + sym->st_value;
1174                 }
1175             }
1176
1177           continue;
1178         }
1179
1180       /* This is a final link.  */
1181       h = NULL;
1182       sym = NULL;
1183       sec = NULL;
1184       if (r_symndx < symtab_hdr->sh_info)
1185         {
1186           sym = local_syms + r_symndx;
1187           sec = local_sections[r_symndx];
1188           relocation = (sec->output_section->vma
1189                         + sec->output_offset
1190                         + sym->st_value);
1191         }
1192       else
1193         {
1194           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1195           while (h->root.type == bfd_link_hash_indirect
1196                  || h->root.type == bfd_link_hash_warning)
1197             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1198           if (h->root.type == bfd_link_hash_defined
1199               || h->root.type == bfd_link_hash_defweak)
1200             {
1201               sec = h->root.u.def.section;
1202               if ((r_type == R_SPARC_WPLT30
1203                    && h->plt.offset != (bfd_vma) -1)
1204                   || ((r_type == R_SPARC_GOT10
1205                        || r_type == R_SPARC_GOT13
1206                        || r_type == R_SPARC_GOT22)
1207                       && elf_hash_table (info)->dynamic_sections_created
1208                       && (! info->shared
1209                           || (! info->symbolic && h->dynindx != -1)
1210                           || (h->elf_link_hash_flags
1211                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
1212                   || (info->shared
1213                       && ((! info->symbolic && h->dynindx != -1)
1214                           || (h->elf_link_hash_flags
1215                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
1216                       && (r_type == R_SPARC_8
1217                           || r_type == R_SPARC_16
1218                           || r_type == R_SPARC_32
1219                           || r_type == R_SPARC_DISP8
1220                           || r_type == R_SPARC_DISP16
1221                           || r_type == R_SPARC_DISP32
1222                           || r_type == R_SPARC_WDISP30
1223                           || r_type == R_SPARC_WDISP22
1224                           || r_type == R_SPARC_WDISP19
1225                           || r_type == R_SPARC_WDISP16
1226                           || r_type == R_SPARC_HI22
1227                           || r_type == R_SPARC_22
1228                           || r_type == R_SPARC_13
1229                           || r_type == R_SPARC_LO10
1230                           || r_type == R_SPARC_UA32
1231                           || ((r_type == R_SPARC_PC10
1232                                || r_type == R_SPARC_PC22)
1233                               && strcmp (h->root.root.string,
1234                                          "_GLOBAL_OFFSET_TABLE_") != 0))))
1235                 {
1236                   /* In these cases, we don't need the relocation
1237                      value.  We check specially because in some
1238                      obscure cases sec->output_section will be NULL.  */
1239                   relocation = 0;
1240                 }
1241               else
1242                 relocation = (h->root.u.def.value
1243                               + sec->output_section->vma
1244                               + sec->output_offset);
1245             }
1246           else if (h->root.type == bfd_link_hash_undefweak)
1247             relocation = 0;
1248           else if (info->shared && !info->symbolic && !info->no_undefined)
1249             relocation = 0;
1250           else
1251             {
1252               if (! ((*info->callbacks->undefined_symbol)
1253                      (info, h->root.root.string, input_bfd,
1254                       input_section, rel->r_offset)))
1255                 return false;
1256               relocation = 0;
1257             }
1258         }
1259
1260       switch (r_type)
1261         {
1262         case R_SPARC_GOT10:
1263         case R_SPARC_GOT13:
1264         case R_SPARC_GOT22:
1265           /* Relocation is to the entry for this symbol in the global
1266              offset table.  */
1267           if (sgot == NULL)
1268             {
1269               sgot = bfd_get_section_by_name (dynobj, ".got");
1270               BFD_ASSERT (sgot != NULL);
1271             }
1272
1273           if (h != NULL)
1274             {
1275               bfd_vma off;
1276
1277               off = h->got.offset;
1278               BFD_ASSERT (off != (bfd_vma) -1);
1279
1280               if (! elf_hash_table (info)->dynamic_sections_created
1281                   || (info->shared
1282                       && (info->symbolic || h->dynindx == -1)
1283                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1284                 {
1285                   /* This is actually a static link, or it is a
1286                      -Bsymbolic link and the symbol is defined
1287                      locally, or the symbol was forced to be local
1288                      because of a version file.  We must initialize
1289                      this entry in the global offset table.  Since the
1290                      offset must always be a multiple of 4, we use the
1291                      least significant bit to record whether we have
1292                      initialized it already.
1293
1294                      When doing a dynamic link, we create a .rela.got
1295                      relocation entry to initialize the value.  This
1296                      is done in the finish_dynamic_symbol routine.  */
1297                   if ((off & 1) != 0)
1298                     off &= ~1;
1299                   else
1300                     {
1301                       bfd_put_32 (output_bfd, relocation,
1302                                   sgot->contents + off);
1303                       h->got.offset |= 1;
1304                     }
1305                 }
1306
1307               relocation = sgot->output_offset + off - got_base;
1308             }
1309           else
1310             {
1311               bfd_vma off;
1312
1313               BFD_ASSERT (local_got_offsets != NULL
1314                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
1315
1316               off = local_got_offsets[r_symndx];
1317
1318               /* The offset must always be a multiple of 4.  We use
1319                  the least significant bit to record whether we have
1320                  already processed this entry.  */
1321               if ((off & 1) != 0)
1322                 off &= ~1;
1323               else
1324                 {
1325                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1326
1327                   if (info->shared)
1328                     {
1329                       asection *srelgot;
1330                       Elf_Internal_Rela outrel;
1331
1332                       /* We need to generate a R_SPARC_RELATIVE reloc
1333                          for the dynamic linker.  */
1334                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1335                       BFD_ASSERT (srelgot != NULL);
1336
1337                       outrel.r_offset = (sgot->output_section->vma
1338                                          + sgot->output_offset
1339                                          + off);
1340                       outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1341                       outrel.r_addend = 0;
1342                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1343                                                  (((Elf32_External_Rela *)
1344                                                    srelgot->contents)
1345                                                   + srelgot->reloc_count));
1346                       ++srelgot->reloc_count;
1347                     }
1348
1349                   local_got_offsets[r_symndx] |= 1;
1350                 }
1351
1352               relocation = sgot->output_offset + off - got_base;
1353             }
1354
1355           break;
1356
1357         case R_SPARC_WPLT30:
1358           /* Relocation is to the entry for this symbol in the
1359              procedure linkage table.  */
1360
1361           /* The Solaris native assembler will generate a WPLT30 reloc
1362              for a local symbol if you assemble a call from one
1363              section to another when using -K pic.  We treat it as
1364              WDISP30.  */
1365           if (h == NULL)
1366             break;
1367
1368           if (h->plt.offset == (bfd_vma) -1)
1369             {
1370               /* We didn't make a PLT entry for this symbol.  This
1371                  happens when statically linking PIC code, or when
1372                  using -Bsymbolic.  */
1373               break;
1374             }
1375
1376           if (splt == NULL)
1377             {
1378               splt = bfd_get_section_by_name (dynobj, ".plt");
1379               BFD_ASSERT (splt != NULL);
1380             }
1381
1382           relocation = (splt->output_section->vma
1383                         + splt->output_offset
1384                         + h->plt.offset);
1385           break;
1386
1387         case R_SPARC_PC10:
1388         case R_SPARC_PC22:
1389           if (h != NULL
1390               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1391             break;
1392           /* Fall through.  */
1393         case R_SPARC_DISP8:
1394         case R_SPARC_DISP16:
1395         case R_SPARC_DISP32:
1396         case R_SPARC_WDISP30:
1397         case R_SPARC_WDISP22:
1398         case R_SPARC_WDISP19:
1399         case R_SPARC_WDISP16:
1400           if (h == NULL
1401               || (info->symbolic
1402                   && (h->elf_link_hash_flags
1403                       & ELF_LINK_HASH_DEF_REGULAR) != 0))
1404             break;
1405           /* Fall through.  */
1406         case R_SPARC_8:
1407         case R_SPARC_16:
1408         case R_SPARC_32:
1409         case R_SPARC_HI22:
1410         case R_SPARC_22:
1411         case R_SPARC_13:
1412         case R_SPARC_LO10:
1413         case R_SPARC_UA32:
1414           if (info->shared)
1415             {
1416               Elf_Internal_Rela outrel;
1417               boolean skip;
1418
1419               /* When generating a shared object, these relocations
1420                  are copied into the output file to be resolved at run
1421                  time.  */
1422
1423               if (sreloc == NULL)
1424                 {
1425                   const char *name;
1426
1427                   name = (bfd_elf_string_from_elf_section
1428                           (input_bfd,
1429                            elf_elfheader (input_bfd)->e_shstrndx,
1430                            elf_section_data (input_section)->rel_hdr.sh_name));
1431                   if (name == NULL)
1432                     return false;
1433
1434                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1435                               && strcmp (bfd_get_section_name (input_bfd,
1436                                                                input_section),
1437                                          name + 5) == 0);
1438
1439                   sreloc = bfd_get_section_by_name (dynobj, name);
1440                   BFD_ASSERT (sreloc != NULL);
1441                 }
1442
1443               skip = false;
1444
1445               if (elf_section_data (input_section)->stab_info == NULL)
1446                 outrel.r_offset = rel->r_offset;
1447               else
1448                 {
1449                   bfd_vma off;
1450
1451                   off = (_bfd_stab_section_offset
1452                          (output_bfd, &elf_hash_table (info)->stab_info,
1453                           input_section,
1454                           &elf_section_data (input_section)->stab_info,
1455                           rel->r_offset));
1456                   if (off == (bfd_vma) -1)
1457                     skip = true;
1458                   outrel.r_offset = off;
1459                 }
1460
1461               outrel.r_offset += (input_section->output_section->vma
1462                                   + input_section->output_offset);
1463
1464               if (skip)
1465                 memset (&outrel, 0, sizeof outrel);
1466               /* h->dynindx may be -1 if the symbol was marked to
1467                  become local.  */
1468               else if (h != NULL
1469                        && ((! info->symbolic && h->dynindx != -1)
1470                            || (h->elf_link_hash_flags
1471                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
1472                 {
1473                   BFD_ASSERT (h->dynindx != -1);
1474                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1475                   outrel.r_addend = rel->r_addend;
1476                 }
1477               else
1478                 {
1479                   if (r_type == R_SPARC_32)
1480                     {
1481                       outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1482                       outrel.r_addend = relocation + rel->r_addend;
1483                     }
1484                   else
1485                     {
1486                       long indx;
1487
1488                       if (h == NULL)
1489                         sec = local_sections[r_symndx];
1490                       else
1491                         {
1492                           BFD_ASSERT (h->root.type == bfd_link_hash_defined
1493                                       || (h->root.type
1494                                           == bfd_link_hash_defweak));
1495                           sec = h->root.u.def.section;
1496                         }
1497                       if (sec != NULL && bfd_is_abs_section (sec))
1498                         indx = 0;
1499                       else if (sec == NULL || sec->owner == NULL)
1500                         {
1501                           bfd_set_error (bfd_error_bad_value);
1502                           return false;
1503                         }
1504                       else
1505                         {
1506                           asection *osec;
1507
1508                           osec = sec->output_section;
1509                           indx = elf_section_data (osec)->dynindx;
1510
1511                           /* FIXME: we really should be able to link non-pic
1512                              shared libraries.  */
1513                           if (indx == 0)
1514                             {
1515                               BFD_FAIL ();
1516                               (*_bfd_error_handler)
1517                                 (_("%s: probably compiled without -fPIC?"),
1518                                  bfd_get_filename (input_bfd));
1519                               bfd_set_error (bfd_error_bad_value);
1520                               return false;
1521                             }
1522                         }
1523
1524                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1525
1526                       /* For non-RELATIVE dynamic relocations, we keep the
1527                          same symbol, and so generally the same addend.  But
1528                          we do need to adjust those relocations referencing
1529                          sections.  */
1530                       outrel.r_addend = rel->r_addend;
1531                       if (r_symndx < symtab_hdr->sh_info
1532                           && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1533                         outrel.r_addend += sec->output_offset+sym->st_value;
1534                     }
1535                 }
1536
1537               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1538                                          (((Elf32_External_Rela *)
1539                                            sreloc->contents)
1540                                           + sreloc->reloc_count));
1541               ++sreloc->reloc_count;
1542
1543               /* This reloc will be computed at runtime, so there's no
1544                  need to do anything now, unless this is a RELATIVE
1545                  reloc in an unallocated section.  */
1546               if (skip
1547                   || (input_section->flags & SEC_ALLOC) != 0
1548                   || ELF32_R_TYPE (outrel.r_info) != R_SPARC_RELATIVE)
1549                 continue;
1550             }
1551           break;
1552
1553         default:
1554           break;
1555         }
1556
1557       if (r_type == R_SPARC_WDISP16)
1558         {
1559           bfd_vma x;
1560
1561           relocation += rel->r_addend;
1562           relocation -= (input_section->output_section->vma
1563                          + input_section->output_offset);
1564           relocation -= rel->r_offset;
1565
1566           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1567           x |= ((((relocation >> 2) & 0xc000) << 6)
1568                 | ((relocation >> 2) & 0x3fff));
1569           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1570
1571           if ((bfd_signed_vma) relocation < - 0x40000
1572               || (bfd_signed_vma) relocation > 0x3ffff)
1573             r = bfd_reloc_overflow;
1574           else
1575             r = bfd_reloc_ok;
1576         }
1577       else if (r_type == R_SPARC_REV32)
1578         {
1579           bfd_vma x;
1580
1581           relocation = relocation + rel->r_addend;
1582
1583           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1584           x = x + relocation;
1585           bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
1586           r = bfd_reloc_ok;
1587         }
1588       else
1589         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1590                                       contents, rel->r_offset,
1591                                       relocation, rel->r_addend);
1592
1593
1594       if (r != bfd_reloc_ok)
1595         {
1596           switch (r)
1597             {
1598             default:
1599             case bfd_reloc_outofrange:
1600               abort ();
1601             case bfd_reloc_overflow:
1602               {
1603                 const char *name;
1604
1605                 if (h != NULL)
1606                   name = h->root.root.string;
1607                 else
1608                   {
1609                     name = bfd_elf_string_from_elf_section (input_bfd,
1610                                                             symtab_hdr->sh_link,
1611                                                             sym->st_name);
1612                     if (name == NULL)
1613                       return false;
1614                     if (*name == '\0')
1615                       name = bfd_section_name (input_bfd, sec);
1616                   }
1617                 if (! ((*info->callbacks->reloc_overflow)
1618                        (info, name, howto->name, (bfd_vma) 0,
1619                         input_bfd, input_section, rel->r_offset)))
1620                   return false;
1621               }
1622               break;
1623             }
1624         }
1625     }
1626
1627   return true;
1628 }
1629
1630 /* Finish up dynamic symbol handling.  We set the contents of various
1631    dynamic sections here.  */
1632
1633 static boolean
1634 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1635      bfd *output_bfd;
1636      struct bfd_link_info *info;
1637      struct elf_link_hash_entry *h;
1638      Elf_Internal_Sym *sym;
1639 {
1640   bfd *dynobj;
1641
1642   dynobj = elf_hash_table (info)->dynobj;
1643
1644   if (h->plt.offset != (bfd_vma) -1)
1645     {
1646       asection *splt;
1647       asection *srela;
1648       Elf_Internal_Rela rela;
1649
1650       /* This symbol has an entry in the procedure linkage table.  Set
1651          it up.  */
1652
1653       BFD_ASSERT (h->dynindx != -1);
1654
1655       splt = bfd_get_section_by_name (dynobj, ".plt");
1656       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1657       BFD_ASSERT (splt != NULL && srela != NULL);
1658
1659       /* Fill in the entry in the procedure linkage table.  */
1660       bfd_put_32 (output_bfd,
1661                   PLT_ENTRY_WORD0 + h->plt.offset,
1662                   splt->contents + h->plt.offset);
1663       bfd_put_32 (output_bfd,
1664                   (PLT_ENTRY_WORD1
1665                    + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
1666                   splt->contents + h->plt.offset + 4);
1667       bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1668                   splt->contents + h->plt.offset + 8);
1669
1670       /* Fill in the entry in the .rela.plt section.  */
1671       rela.r_offset = (splt->output_section->vma
1672                        + splt->output_offset
1673                        + h->plt.offset);
1674       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1675       rela.r_addend = 0;
1676       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1677                                  ((Elf32_External_Rela *) srela->contents
1678                                   + h->plt.offset / PLT_ENTRY_SIZE - 4));
1679
1680       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1681         {
1682           /* Mark the symbol as undefined, rather than as defined in
1683              the .plt section.  Leave the value alone.  */
1684           sym->st_shndx = SHN_UNDEF;
1685         }
1686     }
1687
1688   if (h->got.offset != (bfd_vma) -1)
1689     {
1690       asection *sgot;
1691       asection *srela;
1692       Elf_Internal_Rela rela;
1693
1694       /* This symbol has an entry in the global offset table.  Set it
1695          up.  */
1696
1697       sgot = bfd_get_section_by_name (dynobj, ".got");
1698       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1699       BFD_ASSERT (sgot != NULL && srela != NULL);
1700
1701       rela.r_offset = (sgot->output_section->vma
1702                        + sgot->output_offset
1703                        + (h->got.offset &~ 1));
1704
1705       /* If this is a -Bsymbolic link, and the symbol is defined
1706          locally, we just want to emit a RELATIVE reloc.  Likewise if
1707          the symbol was forced to be local because of a version file.
1708          The entry in the global offset table will already have been
1709          initialized in the relocate_section function.  */
1710       if (info->shared
1711           && (info->symbolic || h->dynindx == -1)
1712           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1713         rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1714       else
1715         {
1716           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1717           rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1718         }
1719
1720       rela.r_addend = 0;
1721       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1722                                  ((Elf32_External_Rela *) srela->contents
1723                                   + srela->reloc_count));
1724       ++srela->reloc_count;
1725     }
1726
1727   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1728     {
1729       asection *s;
1730       Elf_Internal_Rela rela;
1731
1732       /* This symbols needs a copy reloc.  Set it up.  */
1733
1734       BFD_ASSERT (h->dynindx != -1);
1735
1736       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1737                                    ".rela.bss");
1738       BFD_ASSERT (s != NULL);
1739
1740       rela.r_offset = (h->root.u.def.value
1741                        + h->root.u.def.section->output_section->vma
1742                        + h->root.u.def.section->output_offset);
1743       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1744       rela.r_addend = 0;
1745       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1746                                  ((Elf32_External_Rela *) s->contents
1747                                   + s->reloc_count));
1748       ++s->reloc_count;
1749     }
1750
1751   /* Mark some specially defined symbols as absolute.  */
1752   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1753       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1754       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1755     sym->st_shndx = SHN_ABS;
1756
1757   return true;
1758 }
1759
1760 /* Finish up the dynamic sections.  */
1761
1762 static boolean
1763 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1764      bfd *output_bfd;
1765      struct bfd_link_info *info;
1766 {
1767   bfd *dynobj;
1768   asection *sdyn;
1769   asection *sgot;
1770
1771   dynobj = elf_hash_table (info)->dynobj;
1772
1773   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1774
1775   if (elf_hash_table (info)->dynamic_sections_created)
1776     {
1777       asection *splt;
1778       Elf32_External_Dyn *dyncon, *dynconend;
1779
1780       splt = bfd_get_section_by_name (dynobj, ".plt");
1781       BFD_ASSERT (splt != NULL && sdyn != NULL);
1782
1783       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1784       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1785       for (; dyncon < dynconend; dyncon++)
1786         {
1787           Elf_Internal_Dyn dyn;
1788           const char *name;
1789           boolean size;
1790
1791           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1792
1793           switch (dyn.d_tag)
1794             {
1795             case DT_PLTGOT:   name = ".plt"; size = false; break;
1796             case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1797             case DT_JMPREL:   name = ".rela.plt"; size = false; break;
1798             default:      name = NULL; size = false; break;
1799             }
1800
1801           if (name != NULL)
1802             {
1803               asection *s;
1804
1805               s = bfd_get_section_by_name (output_bfd, name);
1806               if (s == NULL)
1807                 dyn.d_un.d_val = 0;
1808               else
1809                 {
1810                   if (! size)
1811                     dyn.d_un.d_ptr = s->vma;
1812                   else
1813                     {
1814                       if (s->_cooked_size != 0)
1815                         dyn.d_un.d_val = s->_cooked_size;
1816                       else
1817                         dyn.d_un.d_val = s->_raw_size;
1818                     }
1819                 }
1820               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1821             }
1822         }
1823
1824       /* Clear the first four entries in the procedure linkage table,
1825          and put a nop in the last four bytes.  */
1826       if (splt->_raw_size > 0)
1827         {
1828           memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1829           bfd_put_32 (output_bfd, SPARC_NOP,
1830                       splt->contents + splt->_raw_size - 4);
1831         }
1832
1833       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1834         PLT_ENTRY_SIZE;
1835     }
1836
1837   /* Set the first entry in the global offset table to the address of
1838      the dynamic section.  */
1839   sgot = bfd_get_section_by_name (dynobj, ".got");
1840   BFD_ASSERT (sgot != NULL);
1841   if (sgot->_raw_size > 0)
1842     {
1843       if (sdyn == NULL)
1844         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1845       else
1846         bfd_put_32 (output_bfd,
1847                     sdyn->output_section->vma + sdyn->output_offset,
1848                     sgot->contents);
1849     }
1850
1851   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1852
1853   if (info->shared)
1854     {
1855       asection *sdynsym;
1856       asection *s;
1857       Elf_Internal_Sym sym;
1858       int c;
1859
1860       /* Set up the section symbols for the output sections.  */
1861
1862       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1863       BFD_ASSERT (sdynsym != NULL);
1864
1865       sym.st_size = 0;
1866       sym.st_name = 0;
1867       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1868       sym.st_other = 0;
1869
1870       c = 0;
1871       for (s = output_bfd->sections; s != NULL; s = s->next)
1872         {
1873           int indx;
1874
1875           if (elf_section_data (s)->dynindx == 0)
1876             continue;
1877
1878           sym.st_value = s->vma;
1879
1880           indx = elf_section_data (s)->this_idx;
1881           BFD_ASSERT (indx > 0);
1882           sym.st_shndx = indx;
1883
1884           bfd_elf32_swap_symbol_out (output_bfd, &sym,
1885                                      (PTR) (((Elf32_External_Sym *)
1886                                              sdynsym->contents)
1887                                             + elf_section_data (s)->dynindx));
1888
1889           ++c;
1890         }
1891
1892       /* Set the sh_info field of the output .dynsym section to the
1893          index of the first global symbol.  */
1894       elf_section_data (sdynsym->output_section)->this_hdr.sh_info = c + 1;
1895     }
1896
1897   return true;
1898 }
1899 \f
1900 /* Functions for dealing with the e_flags field.
1901
1902    We don't define set_private_flags or copy_private_bfd_data because
1903    the only currently defined values are based on the bfd mach number,
1904    so we use the latter instead and defer setting e_flags until the
1905    file is written out.  */
1906
1907 /* Merge backend specific data from an object file to the output
1908    object file when linking.  */
1909
1910 static boolean
1911 elf32_sparc_merge_private_bfd_data (ibfd, obfd)
1912      bfd *ibfd;
1913      bfd *obfd;
1914 {
1915   boolean error;
1916   /* FIXME: This should not be static.  */
1917   static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
1918
1919   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1920       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1921     return true;
1922
1923   error = false;
1924
1925 #if 0
1926   /* ??? The native linker doesn't do this so we can't (otherwise gcc would
1927      have to know which linker is being used).  Instead, the native linker
1928      bumps up the architecture level when it has to.  However, I still think
1929      warnings like these are good, so it would be nice to have them turned on
1930      by some option.  */
1931
1932   /* If the output machine is normal sparc, we can't allow v9 input files.  */
1933   if (bfd_get_mach (obfd) == bfd_mach_sparc
1934       && (bfd_get_mach (ibfd) == bfd_mach_sparc_v8plus
1935           || bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa))
1936     {
1937       error = true;
1938       (*_bfd_error_handler)
1939         (_("%s: compiled for a v8plus system and target is v8"),
1940          bfd_get_filename (ibfd));
1941     }
1942   /* If the output machine is v9, we can't allow v9+vis input files.  */
1943   if (bfd_get_mach (obfd) == bfd_mach_sparc_v8plus
1944       && bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa)
1945     {
1946       error = true;
1947       (*_bfd_error_handler)
1948         (_("%s: compiled for a v8plusa system and target is v8plus"),
1949          bfd_get_filename (ibfd));
1950     }
1951 #else
1952   if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9)
1953     {
1954       error = true;
1955       (*_bfd_error_handler)
1956         (_("%s: compiled for a 64 bit system and target is 32 bit"),
1957          bfd_get_filename (ibfd));
1958     }
1959   else if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1960     bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
1961 #endif
1962
1963   if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
1964        != previous_ibfd_e_flags)
1965       && previous_ibfd_e_flags != (unsigned long) -1)
1966     {
1967       (*_bfd_error_handler)
1968         (_("%s: linking little endian files with big endian files"),
1969          bfd_get_filename (ibfd));
1970       error = true;
1971     }
1972   previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
1973
1974   if (error)
1975     {
1976       bfd_set_error (bfd_error_bad_value);
1977       return false;
1978     }
1979
1980   return true;
1981 }
1982 \f
1983 /* Set the right machine number.  */
1984
1985 static boolean
1986 elf32_sparc_object_p (abfd)
1987      bfd *abfd;
1988 {
1989   if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
1990     {
1991       if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
1992         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
1993                                           bfd_mach_sparc_v8plusa);
1994       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
1995         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
1996                                           bfd_mach_sparc_v8plus);
1997       else
1998         return false;
1999     }
2000   else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
2001     return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2002                                       bfd_mach_sparc_sparclite_le);
2003   else
2004     return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
2005 }
2006
2007 /* The final processing done just before writing out the object file.
2008    We need to set the e_machine field appropriately.  */
2009
2010 static void
2011 elf32_sparc_final_write_processing (abfd, linker)
2012      bfd *abfd;
2013      boolean linker ATTRIBUTE_UNUSED;
2014 {
2015   switch (bfd_get_mach (abfd))
2016     {
2017     case bfd_mach_sparc :
2018       break; /* nothing to do */
2019     case bfd_mach_sparc_v8plus :
2020       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2021       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2022       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
2023       break;
2024     case bfd_mach_sparc_v8plusa :
2025       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2026       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2027       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
2028       break;
2029     case bfd_mach_sparc_sparclite_le :
2030       elf_elfheader (abfd)->e_machine = EM_SPARC;
2031       elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
2032       break;
2033     default :
2034       abort ();
2035       break;
2036     }
2037 }
2038 \f
2039 #define TARGET_BIG_SYM  bfd_elf32_sparc_vec
2040 #define TARGET_BIG_NAME "elf32-sparc"
2041 #define ELF_ARCH        bfd_arch_sparc
2042 #define ELF_MACHINE_CODE EM_SPARC
2043 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
2044 #define ELF_MAXPAGESIZE 0x10000
2045
2046 #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
2047 #define elf_info_to_howto               elf32_sparc_info_to_howto
2048 #define elf_backend_create_dynamic_sections \
2049                                         _bfd_elf_create_dynamic_sections
2050 #define elf_backend_check_relocs        elf32_sparc_check_relocs
2051 #define elf_backend_adjust_dynamic_symbol \
2052                                         elf32_sparc_adjust_dynamic_symbol
2053 #define elf_backend_size_dynamic_sections \
2054                                         elf32_sparc_size_dynamic_sections
2055 #define elf_backend_relocate_section    elf32_sparc_relocate_section
2056 #define elf_backend_finish_dynamic_symbol \
2057                                         elf32_sparc_finish_dynamic_symbol
2058 #define elf_backend_finish_dynamic_sections \
2059                                         elf32_sparc_finish_dynamic_sections
2060 #define bfd_elf32_bfd_merge_private_bfd_data \
2061                                         elf32_sparc_merge_private_bfd_data
2062 #define elf_backend_object_p            elf32_sparc_object_p
2063 #define elf_backend_final_write_processing \
2064                                         elf32_sparc_final_write_processing
2065 #define elf_backend_gc_mark_hook        elf32_sparc_gc_mark_hook
2066 #define elf_backend_gc_sweep_hook       elf32_sparc_gc_sweep_hook
2067
2068 #define elf_backend_can_gc_sections 1
2069 #define elf_backend_want_got_plt 0
2070 #define elf_backend_plt_readonly 0
2071 #define elf_backend_want_plt_sym 1
2072 #define elf_backend_got_header_size 4
2073 #define elf_backend_plt_header_size (4*PLT_ENTRY_SIZE)
2074
2075 #include "elf32-target.h"