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