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