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