OSDN Git Service

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