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