* elf64-ppc.c (adjust_opd_syms): Fix merge error.
[external/binutils.git] / bfd / elf32-sparc.c
1 /* SPARC-specific support for 32-bit ELF
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004 Free Software Foundation, Inc.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/sparc.h"
27 #include "opcode/sparc.h"
28
29 static reloc_howto_type *elf32_sparc_reloc_type_lookup
30   PARAMS ((bfd *, bfd_reloc_code_real_type));
31 static void elf32_sparc_info_to_howto
32   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
33 static bfd_boolean elf32_sparc_check_relocs
34   PARAMS ((bfd *, struct bfd_link_info *, asection *,
35            const Elf_Internal_Rela *));
36 static bfd_boolean elf32_sparc_adjust_dynamic_symbol
37   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
38 static bfd_boolean allocate_dynrelocs
39   PARAMS ((struct elf_link_hash_entry *, PTR));
40 static bfd_boolean readonly_dynrelocs
41   PARAMS ((struct elf_link_hash_entry *, PTR));
42 static bfd_boolean elf32_sparc_size_dynamic_sections
43   PARAMS ((bfd *, struct bfd_link_info *));
44 static bfd_boolean elf32_sparc_new_section_hook
45   PARAMS ((bfd *, asection *));
46 static bfd_boolean elf32_sparc_relax_section
47   PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
48 static bfd_vma dtpoff_base
49   PARAMS ((struct bfd_link_info *));
50 static bfd_vma tpoff
51   PARAMS ((struct bfd_link_info *, bfd_vma));
52 static bfd_boolean elf32_sparc_relocate_section
53   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
54            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
55 static bfd_boolean elf32_sparc_finish_dynamic_symbol
56   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
57            Elf_Internal_Sym *));
58 static bfd_boolean elf32_sparc_finish_dynamic_sections
59   PARAMS ((bfd *, struct bfd_link_info *));
60 static bfd_boolean elf32_sparc_merge_private_bfd_data
61   PARAMS ((bfd *, bfd *));
62 static struct bfd_hash_entry *link_hash_newfunc
63   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
64 static struct bfd_link_hash_table *elf32_sparc_link_hash_table_create
65   PARAMS ((bfd *));
66 static bfd_boolean create_got_section
67   PARAMS ((bfd *, struct bfd_link_info *));
68 static bfd_boolean elf32_sparc_create_dynamic_sections
69   PARAMS ((bfd *, struct bfd_link_info *));
70 static void elf32_sparc_copy_indirect_symbol
71   PARAMS ((const struct elf_backend_data *, struct elf_link_hash_entry *,
72           struct elf_link_hash_entry *));
73 static int elf32_sparc_tls_transition
74   PARAMS ((struct bfd_link_info *, bfd *, int, int));
75
76 static bfd_boolean elf32_sparc_mkobject
77   PARAMS ((bfd *));
78 static bfd_boolean elf32_sparc_object_p
79   PARAMS ((bfd *));
80 static void elf32_sparc_final_write_processing
81   PARAMS ((bfd *, bfd_boolean));
82 static enum elf_reloc_type_class elf32_sparc_reloc_type_class
83   PARAMS ((const Elf_Internal_Rela *));
84 static asection * elf32_sparc_gc_mark_hook
85   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
86            struct elf_link_hash_entry *, Elf_Internal_Sym *));
87 static bfd_boolean elf32_sparc_gc_sweep_hook
88   PARAMS ((bfd *, struct bfd_link_info *, asection *,
89            const Elf_Internal_Rela *));
90 \f
91 /* The relocation "howto" table.  */
92
93 static bfd_reloc_status_type sparc_elf_notsupported_reloc
94   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
95 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
96   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
97 static bfd_reloc_status_type sparc_elf_hix22_reloc
98   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
99 static bfd_reloc_status_type sparc_elf_lox10_reloc
100   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
101
102 reloc_howto_type _bfd_sparc_elf_howto_table[] =
103 {
104   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
105   HOWTO(R_SPARC_8,         0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_8",       FALSE,0,0x000000ff,TRUE),
106   HOWTO(R_SPARC_16,        0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_16",      FALSE,0,0x0000ffff,TRUE),
107   HOWTO(R_SPARC_32,        0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_32",      FALSE,0,0xffffffff,TRUE),
108   HOWTO(R_SPARC_DISP8,     0,0, 8,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP8",   FALSE,0,0x000000ff,TRUE),
109   HOWTO(R_SPARC_DISP16,    0,1,16,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP16",  FALSE,0,0x0000ffff,TRUE),
110   HOWTO(R_SPARC_DISP32,    0,2,32,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP32",  FALSE,0,0xffffffff,TRUE),
111   HOWTO(R_SPARC_WDISP30,   2,2,30,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
112   HOWTO(R_SPARC_WDISP22,   2,2,22,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
113   HOWTO(R_SPARC_HI22,     10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HI22",    FALSE,0,0x003fffff,TRUE),
114   HOWTO(R_SPARC_22,        0,2,22,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_22",      FALSE,0,0x003fffff,TRUE),
115   HOWTO(R_SPARC_13,        0,2,13,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_13",      FALSE,0,0x00001fff,TRUE),
116   HOWTO(R_SPARC_LO10,      0,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LO10",    FALSE,0,0x000003ff,TRUE),
117   HOWTO(R_SPARC_GOT10,     0,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT10",   FALSE,0,0x000003ff,TRUE),
118   HOWTO(R_SPARC_GOT13,     0,2,13,FALSE,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_GOT13",   FALSE,0,0x00001fff,TRUE),
119   HOWTO(R_SPARC_GOT22,    10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT22",   FALSE,0,0x003fffff,TRUE),
120   HOWTO(R_SPARC_PC10,      0,2,10,TRUE, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC10",    FALSE,0,0x000003ff,TRUE),
121   HOWTO(R_SPARC_PC22,     10,2,22,TRUE, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PC22",    FALSE,0,0x003fffff,TRUE),
122   HOWTO(R_SPARC_WPLT30,    2,2,30,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WPLT30",  FALSE,0,0x3fffffff,TRUE),
123   HOWTO(R_SPARC_COPY,      0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_COPY",    FALSE,0,0x00000000,TRUE),
124   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),
125   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),
126   HOWTO(R_SPARC_RELATIVE,  0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
127   HOWTO(R_SPARC_UA32,      0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA32",    FALSE,0,0xffffffff,TRUE),
128   HOWTO(R_SPARC_PLT32,     0,0,00,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PLT32",   FALSE,0,0xffffffff,TRUE),
129   HOWTO(R_SPARC_HIPLT22,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HIPLT22",  FALSE,0,0x00000000,TRUE),
130   HOWTO(R_SPARC_LOPLT10,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_LOPLT10",  FALSE,0,0x00000000,TRUE),
131   HOWTO(R_SPARC_PCPLT32,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT32",  FALSE,0,0x00000000,TRUE),
132   HOWTO(R_SPARC_PCPLT22,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT22",  FALSE,0,0x00000000,TRUE),
133   HOWTO(R_SPARC_PCPLT10,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT10",  FALSE,0,0x00000000,TRUE),
134   HOWTO(R_SPARC_10,        0,2,10,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_10",      FALSE,0,0x000003ff,TRUE),
135   HOWTO(R_SPARC_11,        0,2,11,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_11",      FALSE,0,0x000007ff,TRUE),
136   /* These are for sparc64 in a 64 bit environment.
137      Values need to be here because the table is indexed by reloc number.  */
138   HOWTO(R_SPARC_64,        0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_64",      FALSE,0,0x00000000,TRUE),
139   HOWTO(R_SPARC_OLO10,     0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_OLO10",   FALSE,0,0x00000000,TRUE),
140   HOWTO(R_SPARC_HH22,      0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HH22",    FALSE,0,0x00000000,TRUE),
141   HOWTO(R_SPARC_HM10,      0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HM10",    FALSE,0,0x00000000,TRUE),
142   HOWTO(R_SPARC_LM22,      0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_LM22",    FALSE,0,0x00000000,TRUE),
143   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),
144   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),
145   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),
146   /* End sparc64 in 64 bit environment values.
147      The following are for sparc64 in a 32 bit environment.  */
148   HOWTO(R_SPARC_WDISP16,   2,2,16,TRUE, 0,complain_overflow_signed,  sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", FALSE,0,0x00000000,TRUE),
149   HOWTO(R_SPARC_WDISP19,   2,2,19,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP19", FALSE,0,0x0007ffff,TRUE),
150   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),
151   HOWTO(R_SPARC_7,         0,2, 7,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_7",       FALSE,0,0x0000007f,TRUE),
152   HOWTO(R_SPARC_5,         0,2, 5,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_5",       FALSE,0,0x0000001f,TRUE),
153   HOWTO(R_SPARC_6,         0,2, 6,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_6",       FALSE,0,0x0000003f,TRUE),
154   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
155   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
156   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
157   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
158   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
159   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
160   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
161   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
162   HOWTO(R_SPARC_UA64,      0,0, 0,FALSE,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_UA64",    FALSE,0,0x00000000,TRUE),
163   HOWTO(R_SPARC_UA16,      0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA16",    FALSE,0,0x0000ffff,TRUE),
164   HOWTO(R_SPARC_TLS_GD_HI22,10,2,22,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_HI22",FALSE,0,0x003fffff,TRUE),
165   HOWTO(R_SPARC_TLS_GD_LO10,0,2,10,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_LO10",FALSE,0,0x000003ff,TRUE),
166   HOWTO(R_SPARC_TLS_GD_ADD,0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_ADD",FALSE,0,0x00000000,TRUE),
167   HOWTO(R_SPARC_TLS_GD_CALL,2,2,30,TRUE,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_CALL",FALSE,0,0x3fffffff,TRUE),
168   HOWTO(R_SPARC_TLS_LDM_HI22,10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_HI22",FALSE,0,0x003fffff,TRUE),
169   HOWTO(R_SPARC_TLS_LDM_LO10,0,2,10,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_LO10",FALSE,0,0x000003ff,TRUE),
170   HOWTO(R_SPARC_TLS_LDM_ADD,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_ADD",FALSE,0,0x00000000,TRUE),
171   HOWTO(R_SPARC_TLS_LDM_CALL,2,2,30,TRUE,0,complain_overflow_signed, bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_CALL",FALSE,0,0x3fffffff,TRUE),
172   HOWTO(R_SPARC_TLS_LDO_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_TLS_LDO_HIX22",FALSE,0,0x003fffff, FALSE),
173   HOWTO(R_SPARC_TLS_LDO_LOX10,0,2,0,FALSE,0,complain_overflow_dont,  sparc_elf_lox10_reloc,  "R_SPARC_TLS_LDO_LOX10",FALSE,0,0x000003ff, FALSE),
174   HOWTO(R_SPARC_TLS_LDO_ADD,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_LDO_ADD",FALSE,0,0x00000000,TRUE),
175   HOWTO(R_SPARC_TLS_IE_HI22,10,2,22,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_HI22",FALSE,0,0x003fffff,TRUE),
176   HOWTO(R_SPARC_TLS_IE_LO10,0,2,10,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_LO10",FALSE,0,0x000003ff,TRUE),
177   HOWTO(R_SPARC_TLS_IE_LD,0,0, 0,FALSE,0,complain_overflow_dont,     bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_LD",FALSE,0,0x00000000,TRUE),
178   HOWTO(R_SPARC_TLS_IE_LDX,0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_LDX",FALSE,0,0x00000000,TRUE),
179   HOWTO(R_SPARC_TLS_IE_ADD,0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_ADD",FALSE,0,0x00000000,TRUE),
180   HOWTO(R_SPARC_TLS_LE_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_TLS_LE_HIX22",FALSE,0,0x003fffff, FALSE),
181   HOWTO(R_SPARC_TLS_LE_LOX10,0,2,0,FALSE,0,complain_overflow_dont,   sparc_elf_lox10_reloc,  "R_SPARC_TLS_LE_LOX10",FALSE,0,0x000003ff, FALSE),
182   HOWTO(R_SPARC_TLS_DTPMOD32,0,0, 0,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_DTPMOD32",FALSE,0,0x00000000,TRUE),
183   HOWTO(R_SPARC_TLS_DTPMOD64,0,0, 0,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_DTPMOD64",FALSE,0,0x00000000,TRUE),
184   HOWTO(R_SPARC_TLS_DTPOFF32,0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF32",FALSE,0,0xffffffff,TRUE),
185   HOWTO(R_SPARC_TLS_DTPOFF64,0,0, 0,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_DTPOFF64",FALSE,0,0x00000000,TRUE),
186   HOWTO(R_SPARC_TLS_TPOFF32,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_TPOFF32",FALSE,0,0x00000000,TRUE),
187   HOWTO(R_SPARC_TLS_TPOFF64,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_TPOFF64",FALSE,0,0x00000000,TRUE)
188 };
189 static reloc_howto_type elf32_sparc_vtinherit_howto =
190   HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", FALSE,0, 0, FALSE);
191 static reloc_howto_type elf32_sparc_vtentry_howto =
192   HOWTO (R_SPARC_GNU_VTENTRY, 0,2,0,FALSE,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_SPARC_GNU_VTENTRY", FALSE,0,0, FALSE);
193 static reloc_howto_type elf32_sparc_rev32_howto =
194   HOWTO(R_SPARC_REV32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", FALSE,0,0xffffffff,TRUE);
195
196 struct elf_reloc_map {
197   bfd_reloc_code_real_type bfd_reloc_val;
198   unsigned char elf_reloc_val;
199 };
200
201 static const struct elf_reloc_map sparc_reloc_map[] =
202 {
203   { BFD_RELOC_NONE, R_SPARC_NONE, },
204   { BFD_RELOC_16, R_SPARC_16, },
205   { BFD_RELOC_16_PCREL, R_SPARC_DISP16 },
206   { BFD_RELOC_8, R_SPARC_8 },
207   { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
208   { BFD_RELOC_CTOR, R_SPARC_32 },
209   { BFD_RELOC_32, R_SPARC_32 },
210   { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
211   { BFD_RELOC_HI22, R_SPARC_HI22 },
212   { BFD_RELOC_LO10, R_SPARC_LO10, },
213   { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
214   { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 },
215   { BFD_RELOC_SPARC22, R_SPARC_22 },
216   { BFD_RELOC_SPARC13, R_SPARC_13 },
217   { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
218   { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
219   { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
220   { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
221   { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
222   { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
223   { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
224   { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
225   { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
226   { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
227   { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
228   { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
229   { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
230   { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
231   { BFD_RELOC_SPARC_10, R_SPARC_10 },
232   { BFD_RELOC_SPARC_11, R_SPARC_11 },
233   { BFD_RELOC_SPARC_64, R_SPARC_64 },
234   { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
235   { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
236   { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
237   { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
238   { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
239   { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
240   { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
241   { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
242   { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
243   { BFD_RELOC_SPARC_7, R_SPARC_7 },
244   { BFD_RELOC_SPARC_5, R_SPARC_5 },
245   { BFD_RELOC_SPARC_6, R_SPARC_6 },
246   { BFD_RELOC_SPARC_TLS_GD_HI22, R_SPARC_TLS_GD_HI22 },
247   { BFD_RELOC_SPARC_TLS_GD_LO10, R_SPARC_TLS_GD_LO10 },
248   { BFD_RELOC_SPARC_TLS_GD_ADD, R_SPARC_TLS_GD_ADD },
249   { BFD_RELOC_SPARC_TLS_GD_CALL, R_SPARC_TLS_GD_CALL },
250   { BFD_RELOC_SPARC_TLS_LDM_HI22, R_SPARC_TLS_LDM_HI22 },
251   { BFD_RELOC_SPARC_TLS_LDM_LO10, R_SPARC_TLS_LDM_LO10 },
252   { BFD_RELOC_SPARC_TLS_LDM_ADD, R_SPARC_TLS_LDM_ADD },
253   { BFD_RELOC_SPARC_TLS_LDM_CALL, R_SPARC_TLS_LDM_CALL },
254   { BFD_RELOC_SPARC_TLS_LDO_HIX22, R_SPARC_TLS_LDO_HIX22 },
255   { BFD_RELOC_SPARC_TLS_LDO_LOX10, R_SPARC_TLS_LDO_LOX10 },
256   { BFD_RELOC_SPARC_TLS_LDO_ADD, R_SPARC_TLS_LDO_ADD },
257   { BFD_RELOC_SPARC_TLS_IE_HI22, R_SPARC_TLS_IE_HI22 },
258   { BFD_RELOC_SPARC_TLS_IE_LO10, R_SPARC_TLS_IE_LO10 },
259   { BFD_RELOC_SPARC_TLS_IE_LD, R_SPARC_TLS_IE_LD },
260   { BFD_RELOC_SPARC_TLS_IE_LDX, R_SPARC_TLS_IE_LDX },
261   { BFD_RELOC_SPARC_TLS_IE_ADD, R_SPARC_TLS_IE_ADD },
262   { BFD_RELOC_SPARC_TLS_LE_HIX22, R_SPARC_TLS_LE_HIX22 },
263   { BFD_RELOC_SPARC_TLS_LE_LOX10, R_SPARC_TLS_LE_LOX10 },
264   { BFD_RELOC_SPARC_TLS_DTPMOD32, R_SPARC_TLS_DTPMOD32 },
265   { BFD_RELOC_SPARC_TLS_DTPMOD64, R_SPARC_TLS_DTPMOD64 },
266   { BFD_RELOC_SPARC_TLS_DTPOFF32, R_SPARC_TLS_DTPOFF32 },
267   { BFD_RELOC_SPARC_TLS_DTPOFF64, R_SPARC_TLS_DTPOFF64 },
268   { BFD_RELOC_SPARC_TLS_TPOFF32, R_SPARC_TLS_TPOFF32 },
269   { BFD_RELOC_SPARC_TLS_TPOFF64, R_SPARC_TLS_TPOFF64 },
270   { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
271   { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
272   { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 }
273 };
274
275 static reloc_howto_type *
276 elf32_sparc_reloc_type_lookup (abfd, code)
277      bfd *abfd ATTRIBUTE_UNUSED;
278      bfd_reloc_code_real_type code;
279 {
280   unsigned int i;
281
282   switch (code)
283     {
284     case BFD_RELOC_VTABLE_INHERIT:
285       return &elf32_sparc_vtinherit_howto;
286
287     case BFD_RELOC_VTABLE_ENTRY:
288       return &elf32_sparc_vtentry_howto;
289
290     case BFD_RELOC_SPARC_REV32:
291       return &elf32_sparc_rev32_howto;
292
293     default:
294       for (i = 0;
295            i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map);
296            i++)
297         {
298           if (sparc_reloc_map[i].bfd_reloc_val == code)
299             return (_bfd_sparc_elf_howto_table
300                     + (int) sparc_reloc_map[i].elf_reloc_val);
301         }
302     }
303     bfd_set_error (bfd_error_bad_value);
304     return NULL;
305 }
306
307 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
308    and elf64-sparc.c has its own copy.  */
309
310 static void
311 elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
312      bfd *abfd ATTRIBUTE_UNUSED;
313      arelent *cache_ptr;
314      Elf_Internal_Rela *dst;
315 {
316   switch (ELF32_R_TYPE(dst->r_info))
317     {
318     case R_SPARC_GNU_VTINHERIT:
319       cache_ptr->howto = &elf32_sparc_vtinherit_howto;
320       break;
321
322     case R_SPARC_GNU_VTENTRY:
323       cache_ptr->howto = &elf32_sparc_vtentry_howto;
324       break;
325
326     case R_SPARC_REV32:
327       cache_ptr->howto = &elf32_sparc_rev32_howto;
328       break;
329
330     default:
331       BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
332       cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
333     }
334 }
335 \f
336 /* For unsupported relocs.  */
337
338 static bfd_reloc_status_type
339 sparc_elf_notsupported_reloc (abfd,
340                              reloc_entry,
341                              symbol,
342                              data,
343                              input_section,
344                              output_bfd,
345                              error_message)
346      bfd *abfd ATTRIBUTE_UNUSED;
347      arelent *reloc_entry ATTRIBUTE_UNUSED;
348      asymbol *symbol ATTRIBUTE_UNUSED;
349      PTR data ATTRIBUTE_UNUSED;
350      asection *input_section ATTRIBUTE_UNUSED;
351      bfd *output_bfd ATTRIBUTE_UNUSED;
352      char **error_message ATTRIBUTE_UNUSED;
353 {
354   return bfd_reloc_notsupported;
355 }
356
357 /* Handle the WDISP16 reloc.  */
358
359 static bfd_reloc_status_type
360 sparc_elf_wdisp16_reloc (abfd,
361                          reloc_entry,
362                          symbol,
363                          data,
364                          input_section,
365                          output_bfd,
366                          error_message)
367      bfd *abfd;
368      arelent *reloc_entry;
369      asymbol *symbol;
370      PTR data;
371      asection *input_section;
372      bfd *output_bfd;
373      char **error_message ATTRIBUTE_UNUSED;
374 {
375   bfd_vma relocation;
376   bfd_vma x;
377
378   if (output_bfd != (bfd *) NULL
379       && (symbol->flags & BSF_SECTION_SYM) == 0
380       && (! reloc_entry->howto->partial_inplace
381           || reloc_entry->addend == 0))
382     {
383       reloc_entry->address += input_section->output_offset;
384       return bfd_reloc_ok;
385     }
386
387   if (output_bfd != NULL)
388     return bfd_reloc_continue;
389
390   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
391     return bfd_reloc_outofrange;
392
393   relocation = (symbol->value
394                 + symbol->section->output_section->vma
395                 + symbol->section->output_offset);
396   relocation += reloc_entry->addend;
397   relocation -= (input_section->output_section->vma
398                  + input_section->output_offset);
399   relocation -= reloc_entry->address;
400
401   x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
402   x |= ((((relocation >> 2) & 0xc000) << 6)
403         | ((relocation >> 2) & 0x3fff));
404   bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
405
406   if ((bfd_signed_vma) relocation < - 0x40000
407       || (bfd_signed_vma) relocation > 0x3ffff)
408     return bfd_reloc_overflow;
409   else
410     return bfd_reloc_ok;
411 }
412
413 /* Handle the HIX22 reloc.  */
414
415 static bfd_reloc_status_type
416 sparc_elf_hix22_reloc (abfd,
417                        reloc_entry,
418                        symbol,
419                        data,
420                        input_section,
421                        output_bfd,
422                        error_message)
423      bfd *abfd;
424      arelent *reloc_entry;
425      asymbol *symbol;
426      PTR data;
427      asection *input_section;
428      bfd *output_bfd;
429      char **error_message ATTRIBUTE_UNUSED;
430 {
431   bfd_vma relocation;
432   bfd_vma insn;
433
434   if (output_bfd != (bfd *) NULL
435       && (symbol->flags & BSF_SECTION_SYM) == 0)
436     {
437       reloc_entry->address += input_section->output_offset;
438       return bfd_reloc_ok;
439     }
440
441   if (output_bfd != NULL)
442     return bfd_reloc_continue;
443
444   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
445     return bfd_reloc_outofrange;
446
447   relocation = (symbol->value
448                 + symbol->section->output_section->vma
449                 + symbol->section->output_offset);
450   relocation += reloc_entry->addend;
451   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
452
453   insn = (insn &~ (bfd_vma) 0x3fffff) | (((~relocation) >> 10) & 0x3fffff);
454   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
455
456   return bfd_reloc_ok;
457 }
458
459 /* Handle the LOX10 reloc.  */
460
461 static bfd_reloc_status_type
462 sparc_elf_lox10_reloc (abfd,
463                        reloc_entry,
464                        symbol,
465                        data,
466                        input_section,
467                        output_bfd,
468                        error_message)
469      bfd *abfd;
470      arelent *reloc_entry;
471      asymbol *symbol;
472      PTR data;
473      asection *input_section;
474      bfd *output_bfd;
475      char **error_message ATTRIBUTE_UNUSED;
476 {
477   bfd_vma relocation;
478   bfd_vma insn;
479
480   if (output_bfd != (bfd *) NULL
481       && (symbol->flags & BSF_SECTION_SYM) == 0)
482     {
483       reloc_entry->address += input_section->output_offset;
484       return bfd_reloc_ok;
485     }
486
487   if (output_bfd != NULL)
488     return bfd_reloc_continue;
489
490   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
491     return bfd_reloc_outofrange;
492
493   relocation = (symbol->value
494                 + symbol->section->output_section->vma
495                 + symbol->section->output_offset);
496   relocation += reloc_entry->addend;
497   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
498
499   insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff);
500   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
501
502   return bfd_reloc_ok;
503 }
504 \f
505 /* Support for core dump NOTE sections.  */
506
507 static bfd_boolean
508 elf32_sparc_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
509 {
510   switch (note->descsz)
511     {
512     default:
513       return FALSE;
514
515     case 260:                   /* Solaris prpsinfo_t.  */
516       elf_tdata (abfd)->core_program
517         = _bfd_elfcore_strndup (abfd, note->descdata + 84, 16);
518       elf_tdata (abfd)->core_command
519         = _bfd_elfcore_strndup (abfd, note->descdata + 100, 80);
520       break;
521
522     case 336:                   /* Solaris psinfo_t.  */
523       elf_tdata (abfd)->core_program
524         = _bfd_elfcore_strndup (abfd, note->descdata + 88, 16);
525       elf_tdata (abfd)->core_command
526         = _bfd_elfcore_strndup (abfd, note->descdata + 104, 80);
527       break;
528     }
529
530   return TRUE;
531 }
532
533 \f
534 /* Functions for the SPARC ELF linker.  */
535
536 /* The name of the dynamic interpreter.  This is put in the .interp
537    section.  */
538
539 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
540
541 /* The nop opcode we use.  */
542
543 #define SPARC_NOP 0x01000000
544
545 /* The size in bytes of an entry in the procedure linkage table.  */
546
547 #define PLT_ENTRY_SIZE 12
548
549 /* The first four entries in a procedure linkage table are reserved,
550    and the initial contents are unimportant (we zero them out).
551    Subsequent entries look like this.  See the SVR4 ABI SPARC
552    supplement to see how this works.  */
553
554 /* sethi %hi(.-.plt0),%g1.  We fill in the address later.  */
555 #define PLT_ENTRY_WORD0 0x03000000
556 /* b,a .plt0.  We fill in the offset later.  */
557 #define PLT_ENTRY_WORD1 0x30800000
558 /* nop.  */
559 #define PLT_ENTRY_WORD2 SPARC_NOP
560
561 /* The SPARC linker needs to keep track of the number of relocs that it
562    decides to copy as dynamic relocs in check_relocs for each symbol.
563    This is so that it can later discard them if they are found to be
564    unnecessary.  We store the information in a field extending the
565    regular ELF linker hash table.  */
566
567 struct elf32_sparc_dyn_relocs
568 {
569   struct elf32_sparc_dyn_relocs *next;
570
571   /* The input section of the reloc.  */
572   asection *sec;
573
574   /* Total number of relocs copied for the input section.  */
575   bfd_size_type count;
576
577   /* Number of pc-relative relocs copied for the input section.  */
578   bfd_size_type pc_count;
579 };
580
581 /* SPARC ELF linker hash entry.  */
582
583 struct elf32_sparc_link_hash_entry
584 {
585   struct elf_link_hash_entry elf;
586
587   /* Track dynamic relocs copied for this symbol.  */
588   struct elf32_sparc_dyn_relocs *dyn_relocs;
589
590 #define GOT_UNKNOWN     0
591 #define GOT_NORMAL      1
592 #define GOT_TLS_GD      2
593 #define GOT_TLS_IE      3
594   unsigned char tls_type;
595 };
596
597 #define elf32_sparc_hash_entry(ent) ((struct elf32_sparc_link_hash_entry *)(ent))
598
599 struct elf32_sparc_obj_tdata
600 {
601   struct elf_obj_tdata root;
602
603   /* tls_type for each local got entry.  */
604   char *local_got_tls_type;
605
606   /* TRUE if TLS GD relocs has been seen for this object.  */
607   bfd_boolean has_tlsgd;
608 };
609
610 #define elf32_sparc_tdata(abfd) \
611   ((struct elf32_sparc_obj_tdata *) (abfd)->tdata.any)
612
613 #define elf32_sparc_local_got_tls_type(abfd) \
614   (elf32_sparc_tdata (abfd)->local_got_tls_type)
615
616 static bfd_boolean
617 elf32_sparc_mkobject (abfd)
618      bfd *abfd;
619 {
620   bfd_size_type amt = sizeof (struct elf32_sparc_obj_tdata);
621   abfd->tdata.any = bfd_zalloc (abfd, amt);
622   if (abfd->tdata.any == NULL)
623     return FALSE;
624   return TRUE;
625 }
626
627 /* SPARC ELF linker hash table.  */
628
629 struct elf32_sparc_link_hash_table
630 {
631   struct elf_link_hash_table elf;
632
633   /* Short-cuts to get to dynamic linker sections.  */
634   asection *sgot;
635   asection *srelgot;
636   asection *splt;
637   asection *srelplt;
638   asection *sdynbss;
639   asection *srelbss;
640
641   union {
642     bfd_signed_vma refcount;
643     bfd_vma offset;
644   } tls_ldm_got;
645
646   /* Small local sym to section mapping cache.  */
647   struct sym_sec_cache sym_sec;
648 };
649
650 /* Get the SPARC ELF linker hash table from a link_info structure.  */
651
652 #define elf32_sparc_hash_table(p) \
653   ((struct elf32_sparc_link_hash_table *) ((p)->hash))
654
655 /* Create an entry in an i386 ELF linker hash table.  */
656
657 static struct bfd_hash_entry *
658 link_hash_newfunc (entry, table, string)
659      struct bfd_hash_entry *entry;
660      struct bfd_hash_table *table;
661      const char *string;
662 {
663   /* Allocate the structure if it has not already been allocated by a
664      subclass.  */
665   if (entry == NULL)
666     {
667       entry = bfd_hash_allocate (table,
668                                  sizeof (struct elf32_sparc_link_hash_entry));
669       if (entry == NULL)
670         return entry;
671     }
672
673   /* Call the allocation method of the superclass.  */
674   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
675   if (entry != NULL)
676     {
677       struct elf32_sparc_link_hash_entry *eh;
678
679       eh = (struct elf32_sparc_link_hash_entry *) entry;
680       eh->dyn_relocs = NULL;
681       eh->tls_type = GOT_UNKNOWN;
682     }
683
684   return entry;
685 }
686
687 /* Create a SPARC ELF linker hash table.  */
688
689 static struct bfd_link_hash_table *
690 elf32_sparc_link_hash_table_create (abfd)
691      bfd *abfd;
692 {
693   struct elf32_sparc_link_hash_table *ret;
694   bfd_size_type amt = sizeof (struct elf32_sparc_link_hash_table);
695
696   ret = (struct elf32_sparc_link_hash_table *) bfd_malloc (amt);
697   if (ret == NULL)
698     return NULL;
699
700   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
701     {
702       free (ret);
703       return NULL;
704     }
705
706   ret->sgot = NULL;
707   ret->srelgot = NULL;
708   ret->splt = NULL;
709   ret->srelplt = NULL;
710   ret->sdynbss = NULL;
711   ret->srelbss = NULL;
712   ret->tls_ldm_got.refcount = 0;
713   ret->sym_sec.abfd = NULL;
714
715   return &ret->elf.root;
716 }
717
718 /* Create .got and .rela.got sections in DYNOBJ, and set up
719    shortcuts to them in our hash table.  */
720
721 static bfd_boolean
722 create_got_section (dynobj, info)
723      bfd *dynobj;
724      struct bfd_link_info *info;
725 {
726   struct elf32_sparc_link_hash_table *htab;
727
728   if (! _bfd_elf_create_got_section (dynobj, info))
729     return FALSE;
730
731   htab = elf32_sparc_hash_table (info);
732   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
733   if (!htab->sgot)
734     abort ();
735
736   htab->srelgot = bfd_make_section (dynobj, ".rela.got");
737   if (htab->srelgot == NULL
738       || ! bfd_set_section_flags (dynobj, htab->srelgot,
739                                   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
740                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED
741                                    | SEC_READONLY))
742       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
743     return FALSE;
744   return TRUE;
745 }
746
747 /* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and
748    .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
749    hash table.  */
750
751 static bfd_boolean
752 elf32_sparc_create_dynamic_sections (dynobj, info)
753      bfd *dynobj;
754      struct bfd_link_info *info;
755 {
756   struct elf32_sparc_link_hash_table *htab;
757
758   htab = elf32_sparc_hash_table (info);
759   if (!htab->sgot && !create_got_section (dynobj, info))
760     return FALSE;
761
762   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
763     return FALSE;
764
765   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
766   htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
767   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
768   if (!info->shared)
769     htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
770
771   if (!htab->splt || !htab->srelplt || !htab->sdynbss
772       || (!info->shared && !htab->srelbss))
773     abort ();
774
775   return TRUE;
776 }
777
778 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
779
780 static void
781 elf32_sparc_copy_indirect_symbol (bed, dir, ind)
782      const struct elf_backend_data *bed;
783      struct elf_link_hash_entry *dir, *ind;
784 {
785   struct elf32_sparc_link_hash_entry *edir, *eind;
786
787   edir = (struct elf32_sparc_link_hash_entry *) dir;
788   eind = (struct elf32_sparc_link_hash_entry *) ind;
789
790   if (eind->dyn_relocs != NULL)
791     {
792       if (edir->dyn_relocs != NULL)
793         {
794           struct elf32_sparc_dyn_relocs **pp;
795           struct elf32_sparc_dyn_relocs *p;
796
797           if (ind->root.type == bfd_link_hash_indirect)
798             abort ();
799
800           /* Add reloc counts against the weak sym to the strong sym
801              list.  Merge any entries against the same section.  */
802           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
803             {
804               struct elf32_sparc_dyn_relocs *q;
805
806               for (q = edir->dyn_relocs; q != NULL; q = q->next)
807                 if (q->sec == p->sec)
808                   {
809                     q->pc_count += p->pc_count;
810                     q->count += p->count;
811                     *pp = p->next;
812                     break;
813                   }
814               if (q == NULL)
815                 pp = &p->next;
816             }
817           *pp = edir->dyn_relocs;
818         }
819
820       edir->dyn_relocs = eind->dyn_relocs;
821       eind->dyn_relocs = NULL;
822     }
823
824   if (ind->root.type == bfd_link_hash_indirect
825       && dir->got.refcount <= 0)
826     {
827       edir->tls_type = eind->tls_type;
828       eind->tls_type = GOT_UNKNOWN;
829     }
830   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
831 }
832
833 static int
834 elf32_sparc_tls_transition (info, abfd, r_type, is_local)
835      struct bfd_link_info *info;
836      bfd *abfd;
837      int r_type;
838      int is_local;
839 {
840   if (r_type == R_SPARC_TLS_GD_HI22
841       && ! elf32_sparc_tdata (abfd)->has_tlsgd)
842     r_type = R_SPARC_REV32;
843
844   if (info->shared)
845     return r_type;
846
847   switch (r_type)
848     {
849     case R_SPARC_TLS_GD_HI22:
850       if (is_local)
851         return R_SPARC_TLS_LE_HIX22;
852       return R_SPARC_TLS_IE_HI22;
853     case R_SPARC_TLS_GD_LO10:
854       if (is_local)
855         return R_SPARC_TLS_LE_LOX10;
856       return R_SPARC_TLS_IE_LO10;
857     case R_SPARC_TLS_IE_HI22:
858       if (is_local)
859         return R_SPARC_TLS_LE_HIX22;
860       return r_type;
861     case R_SPARC_TLS_IE_LO10:
862       if (is_local)
863         return R_SPARC_TLS_LE_LOX10;
864       return r_type;
865     case R_SPARC_TLS_LDM_HI22:
866       return R_SPARC_TLS_LE_HIX22;
867     case R_SPARC_TLS_LDM_LO10:
868       return R_SPARC_TLS_LE_LOX10;
869     }
870
871   return r_type;
872 }
873
874 /* Look through the relocs for a section during the first phase, and
875    allocate space in the global offset table or procedure linkage
876    table.  */
877
878 static bfd_boolean
879 elf32_sparc_check_relocs (abfd, info, sec, relocs)
880      bfd *abfd;
881      struct bfd_link_info *info;
882      asection *sec;
883      const Elf_Internal_Rela *relocs;
884 {
885   struct elf32_sparc_link_hash_table *htab;
886   Elf_Internal_Shdr *symtab_hdr;
887   struct elf_link_hash_entry **sym_hashes;
888   bfd_vma *local_got_offsets;
889   const Elf_Internal_Rela *rel;
890   const Elf_Internal_Rela *rel_end;
891   asection *sreloc;
892   bfd_boolean checked_tlsgd = FALSE;
893
894   if (info->relocatable)
895     return TRUE;
896
897   htab = elf32_sparc_hash_table (info);
898   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
899   sym_hashes = elf_sym_hashes (abfd);
900   local_got_offsets = elf_local_got_offsets (abfd);
901
902   sreloc = NULL;
903
904   rel_end = relocs + sec->reloc_count;
905   for (rel = relocs; rel < rel_end; rel++)
906     {
907       unsigned int r_type;
908       unsigned long r_symndx;
909       struct elf_link_hash_entry *h;
910
911       r_symndx = ELF32_R_SYM (rel->r_info);
912       r_type = ELF32_R_TYPE (rel->r_info);
913
914       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
915         {
916           (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
917                                  bfd_archive_filename (abfd),
918                                  r_symndx);
919           return FALSE;
920         }
921
922       if (r_symndx < symtab_hdr->sh_info)
923         h = NULL;
924       else
925         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
926
927       /* Compatibility with old R_SPARC_REV32 reloc conflicting
928          with R_SPARC_TLS_GD_HI22.  */
929       if (! checked_tlsgd)
930         switch (r_type)
931           {
932           case R_SPARC_TLS_GD_HI22:
933             {
934               const Elf_Internal_Rela *relt;
935
936               for (relt = rel + 1; relt < rel_end; relt++)
937                 if (ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_LO10
938                     || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_ADD
939                     || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_CALL)
940                   break;
941               checked_tlsgd = TRUE;
942               elf32_sparc_tdata (abfd)->has_tlsgd = relt < rel_end;
943             }
944             break;
945           case R_SPARC_TLS_GD_LO10:
946           case R_SPARC_TLS_GD_ADD:
947           case R_SPARC_TLS_GD_CALL:
948             checked_tlsgd = TRUE;
949             elf32_sparc_tdata (abfd)->has_tlsgd = TRUE;
950             break;
951           }
952
953       r_type = elf32_sparc_tls_transition (info, abfd, r_type, h == NULL);
954       switch (r_type)
955         {
956         case R_SPARC_TLS_LDM_HI22:
957         case R_SPARC_TLS_LDM_LO10:
958           htab->tls_ldm_got.refcount += 1;
959           break;
960
961         case R_SPARC_TLS_LE_HIX22:
962         case R_SPARC_TLS_LE_LOX10:
963           if (info->shared)
964             goto r_sparc_plt32;
965           break;
966
967         case R_SPARC_TLS_IE_HI22:
968         case R_SPARC_TLS_IE_LO10:
969           if (info->shared)
970             info->flags |= DF_STATIC_TLS;
971           /* Fall through */
972
973         case R_SPARC_GOT10:
974         case R_SPARC_GOT13:
975         case R_SPARC_GOT22:
976         case R_SPARC_TLS_GD_HI22:
977         case R_SPARC_TLS_GD_LO10:
978           /* This symbol requires a global offset table entry.  */
979           {
980             int tls_type, old_tls_type;
981
982             switch (r_type)
983               {
984               default:
985               case R_SPARC_GOT10:
986               case R_SPARC_GOT13:
987               case R_SPARC_GOT22:
988                 tls_type = GOT_NORMAL;
989                 break;
990               case R_SPARC_TLS_GD_HI22:
991               case R_SPARC_TLS_GD_LO10:
992                 tls_type = GOT_TLS_GD;
993                 break;
994               case R_SPARC_TLS_IE_HI22:
995               case R_SPARC_TLS_IE_LO10:
996                 tls_type = GOT_TLS_IE;
997                 break;
998               }
999
1000             if (h != NULL)
1001               {
1002                 h->got.refcount += 1;
1003                 old_tls_type = elf32_sparc_hash_entry(h)->tls_type;
1004               }
1005             else
1006               {
1007                 bfd_signed_vma *local_got_refcounts;
1008
1009                 /* This is a global offset table entry for a local symbol.  */
1010                 local_got_refcounts = elf_local_got_refcounts (abfd);
1011                 if (local_got_refcounts == NULL)
1012                   {
1013                     bfd_size_type size;
1014
1015                     size = symtab_hdr->sh_info;
1016                     size *= (sizeof (bfd_signed_vma) + sizeof(char));
1017                     local_got_refcounts = ((bfd_signed_vma *)
1018                                            bfd_zalloc (abfd, size));
1019                     if (local_got_refcounts == NULL)
1020                       return FALSE;
1021                     elf_local_got_refcounts (abfd) = local_got_refcounts;
1022                     elf32_sparc_local_got_tls_type (abfd)
1023                       = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1024                   }
1025                 local_got_refcounts[r_symndx] += 1;
1026                 old_tls_type = elf32_sparc_local_got_tls_type (abfd) [r_symndx];
1027               }
1028
1029             /* If a TLS symbol is accessed using IE at least once,
1030                there is no point to use dynamic model for it.  */
1031             if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1032                 && (old_tls_type != GOT_TLS_GD
1033                     || tls_type != GOT_TLS_IE))
1034               {
1035                 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
1036                   tls_type = old_tls_type;
1037                 else
1038                   {
1039                     (*_bfd_error_handler)
1040                       (_("%s: `%s' accessed both as normal and thread local symbol"),
1041                        bfd_archive_filename (abfd),
1042                        h ? h->root.root.string : "<local>");
1043                     return FALSE;
1044                   }
1045               }
1046
1047             if (old_tls_type != tls_type)
1048               {
1049                 if (h != NULL)
1050                   elf32_sparc_hash_entry (h)->tls_type = tls_type;
1051                 else
1052                   elf32_sparc_local_got_tls_type (abfd) [r_symndx] = tls_type;
1053               }
1054           }
1055
1056           if (htab->sgot == NULL)
1057             {
1058               if (htab->elf.dynobj == NULL)
1059                 htab->elf.dynobj = abfd;
1060               if (!create_got_section (htab->elf.dynobj, info))
1061                 return FALSE;
1062             }
1063           break;
1064
1065         case R_SPARC_TLS_GD_CALL:
1066         case R_SPARC_TLS_LDM_CALL:
1067           if (info->shared)
1068             {
1069               /* These are basically R_SPARC_TLS_WPLT30 relocs against
1070                  __tls_get_addr.  */
1071               struct bfd_link_hash_entry *bh = NULL;
1072               if (! _bfd_generic_link_add_one_symbol (info, abfd,
1073                                                       "__tls_get_addr", 0,
1074                                                       bfd_und_section_ptr, 0,
1075                                                       NULL, FALSE, FALSE,
1076                                                       &bh))
1077                 return FALSE;
1078               h = (struct elf_link_hash_entry *) bh;
1079             }
1080           else
1081             break;
1082           /* Fall through */
1083
1084         case R_SPARC_PLT32:
1085         case R_SPARC_WPLT30:
1086           /* This symbol requires a procedure linkage table entry.  We
1087              actually build the entry in adjust_dynamic_symbol,
1088              because this might be a case of linking PIC code without
1089              linking in any dynamic objects, in which case we don't
1090              need to generate a procedure linkage table after all.  */
1091
1092           if (h == NULL)
1093             {
1094               /* The Solaris native assembler will generate a WPLT30
1095                  reloc for a local symbol if you assemble a call from
1096                  one section to another when using -K pic.  We treat
1097                  it as WDISP30.  */
1098               if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32)
1099                 goto r_sparc_plt32;
1100               break;
1101             }
1102
1103           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1104
1105           if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32)
1106             goto r_sparc_plt32;
1107           h->plt.refcount += 1;
1108           break;
1109
1110         case R_SPARC_PC10:
1111         case R_SPARC_PC22:
1112           if (h != NULL)
1113             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1114
1115           if (h != NULL
1116               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1117             break;
1118           /* Fall through.  */
1119
1120         case R_SPARC_DISP8:
1121         case R_SPARC_DISP16:
1122         case R_SPARC_DISP32:
1123         case R_SPARC_WDISP30:
1124         case R_SPARC_WDISP22:
1125         case R_SPARC_WDISP19:
1126         case R_SPARC_WDISP16:
1127         case R_SPARC_8:
1128         case R_SPARC_16:
1129         case R_SPARC_32:
1130         case R_SPARC_HI22:
1131         case R_SPARC_22:
1132         case R_SPARC_13:
1133         case R_SPARC_LO10:
1134         case R_SPARC_UA16:
1135         case R_SPARC_UA32:
1136           if (h != NULL)
1137             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1138
1139         r_sparc_plt32:
1140           if (h != NULL && !info->shared)
1141             {
1142               /* We may need a .plt entry if the function this reloc
1143                  refers to is in a shared lib.  */
1144               h->plt.refcount += 1;
1145             }
1146
1147           /* If we are creating a shared library, and this is a reloc
1148              against a global symbol, or a non PC relative reloc
1149              against a local symbol, then we need to copy the reloc
1150              into the shared library.  However, if we are linking with
1151              -Bsymbolic, we do not need to copy a reloc against a
1152              global symbol which is defined in an object we are
1153              including in the link (i.e., DEF_REGULAR is set).  At
1154              this point we have not seen all the input files, so it is
1155              possible that DEF_REGULAR is not set now but will be set
1156              later (it is never cleared).  In case of a weak definition,
1157              DEF_REGULAR may be cleared later by a strong definition in
1158              a shared library.  We account for that possibility below by
1159              storing information in the relocs_copied field of the hash
1160              table entry.  A similar situation occurs when creating
1161              shared libraries and symbol visibility changes render the
1162              symbol local.
1163
1164              If on the other hand, we are creating an executable, we
1165              may need to keep relocations for symbols satisfied by a
1166              dynamic library if we manage to avoid copy relocs for the
1167              symbol.  */
1168           if ((info->shared
1169                && (sec->flags & SEC_ALLOC) != 0
1170                && (! _bfd_sparc_elf_howto_table[r_type].pc_relative
1171                    || (h != NULL
1172                        && (! info->symbolic
1173                            || h->root.type == bfd_link_hash_defweak
1174                            || (h->elf_link_hash_flags
1175                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1176               || (!info->shared
1177                   && (sec->flags & SEC_ALLOC) != 0
1178                   && h != NULL
1179                   && (h->root.type == bfd_link_hash_defweak
1180                       || (h->elf_link_hash_flags
1181                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1182             {
1183               struct elf32_sparc_dyn_relocs *p;
1184               struct elf32_sparc_dyn_relocs **head;
1185
1186               /* When creating a shared object, we must copy these
1187                  relocs into the output file.  We create a reloc
1188                  section in dynobj and make room for the reloc.  */
1189               if (sreloc == NULL)
1190                 {
1191                   const char *name;
1192                   bfd *dynobj;
1193
1194                   name = (bfd_elf_string_from_elf_section
1195                           (abfd,
1196                            elf_elfheader (abfd)->e_shstrndx,
1197                            elf_section_data (sec)->rel_hdr.sh_name));
1198                   if (name == NULL)
1199                     return FALSE;
1200
1201                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1202                               && strcmp (bfd_get_section_name (abfd, sec),
1203                                          name + 5) == 0);
1204
1205                   if (htab->elf.dynobj == NULL)
1206                     htab->elf.dynobj = abfd;
1207                   dynobj = htab->elf.dynobj;
1208
1209                   sreloc = bfd_get_section_by_name (dynobj, name);
1210                   if (sreloc == NULL)
1211                     {
1212                       flagword flags;
1213
1214                       sreloc = bfd_make_section (dynobj, name);
1215                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
1216                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1217                       if ((sec->flags & SEC_ALLOC) != 0)
1218                         flags |= SEC_ALLOC | SEC_LOAD;
1219                       if (sreloc == NULL
1220                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
1221                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1222                         return FALSE;
1223                     }
1224                   elf_section_data (sec)->sreloc = sreloc;
1225                 }
1226
1227               /* If this is a global symbol, we count the number of
1228                  relocations we need for this symbol.  */
1229               if (h != NULL)
1230                 head = &((struct elf32_sparc_link_hash_entry *) h)->dyn_relocs;
1231               else
1232                 {
1233                   /* Track dynamic relocs needed for local syms too.
1234                      We really need local syms available to do this
1235                      easily.  Oh well.  */
1236
1237                   asection *s;
1238                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1239                                                  sec, r_symndx);
1240                   if (s == NULL)
1241                     return FALSE;
1242
1243                   head = ((struct elf32_sparc_dyn_relocs **)
1244                           &elf_section_data (s)->local_dynrel);
1245                 }
1246
1247               p = *head;
1248               if (p == NULL || p->sec != sec)
1249                 {
1250                   bfd_size_type amt = sizeof *p;
1251                   p = ((struct elf32_sparc_dyn_relocs *)
1252                        bfd_alloc (htab->elf.dynobj, amt));
1253                   if (p == NULL)
1254                     return FALSE;
1255                   p->next = *head;
1256                   *head = p;
1257                   p->sec = sec;
1258                   p->count = 0;
1259                   p->pc_count = 0;
1260                 }
1261
1262               p->count += 1;
1263               if (_bfd_sparc_elf_howto_table[r_type].pc_relative)
1264                 p->pc_count += 1;
1265             }
1266
1267           break;
1268
1269         case R_SPARC_GNU_VTINHERIT:
1270           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1271             return FALSE;
1272           break;
1273
1274         case R_SPARC_GNU_VTENTRY:
1275           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1276             return FALSE;
1277           break;
1278
1279         default:
1280           break;
1281         }
1282     }
1283
1284   return TRUE;
1285 }
1286
1287 static asection *
1288 elf32_sparc_gc_mark_hook (sec, info, rel, h, sym)
1289        asection *sec;
1290        struct bfd_link_info *info ATTRIBUTE_UNUSED;
1291        Elf_Internal_Rela *rel;
1292        struct elf_link_hash_entry *h;
1293        Elf_Internal_Sym *sym;
1294 {
1295   if (h != NULL)
1296     {
1297       switch (ELF32_R_TYPE (rel->r_info))
1298       {
1299       case R_SPARC_GNU_VTINHERIT:
1300       case R_SPARC_GNU_VTENTRY:
1301         break;
1302
1303       default:
1304         switch (h->root.type)
1305           {
1306           case bfd_link_hash_defined:
1307           case bfd_link_hash_defweak:
1308             return h->root.u.def.section;
1309
1310           case bfd_link_hash_common:
1311             return h->root.u.c.p->section;
1312
1313           default:
1314             break;
1315           }
1316        }
1317      }
1318    else
1319      return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1320
1321   return NULL;
1322 }
1323
1324 /* Update the got entry reference counts for the section being removed.  */
1325 static bfd_boolean
1326 elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
1327      bfd *abfd;
1328      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1329      asection *sec;
1330      const Elf_Internal_Rela *relocs;
1331 {
1332   Elf_Internal_Shdr *symtab_hdr;
1333   struct elf_link_hash_entry **sym_hashes;
1334   bfd_signed_vma *local_got_refcounts;
1335   const Elf_Internal_Rela *rel, *relend;
1336
1337   elf_section_data (sec)->local_dynrel = NULL;
1338
1339   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1340   sym_hashes = elf_sym_hashes (abfd);
1341   local_got_refcounts = elf_local_got_refcounts (abfd);
1342
1343   relend = relocs + sec->reloc_count;
1344   for (rel = relocs; rel < relend; rel++)
1345     {
1346       unsigned long r_symndx;
1347       unsigned int r_type;
1348       struct elf_link_hash_entry *h = NULL;
1349
1350       r_symndx = ELF32_R_SYM (rel->r_info);
1351       if (r_symndx >= symtab_hdr->sh_info)
1352         {
1353           struct elf32_sparc_link_hash_entry *eh;
1354           struct elf32_sparc_dyn_relocs **pp;
1355           struct elf32_sparc_dyn_relocs *p;
1356
1357           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1358           eh = (struct elf32_sparc_link_hash_entry *) h;
1359           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1360             if (p->sec == sec)
1361               {
1362                 /* Everything must go for SEC.  */
1363                 *pp = p->next;
1364                 break;
1365               }
1366         }
1367
1368       r_type = ELF32_R_TYPE (rel->r_info);
1369       r_type = elf32_sparc_tls_transition (info, abfd, r_type, h != NULL);
1370       switch (r_type)
1371         {
1372         case R_SPARC_TLS_LDM_HI22:
1373         case R_SPARC_TLS_LDM_LO10:
1374           if (elf32_sparc_hash_table (info)->tls_ldm_got.refcount > 0)
1375             elf32_sparc_hash_table (info)->tls_ldm_got.refcount -= 1;
1376           break;
1377
1378         case R_SPARC_TLS_GD_HI22:
1379         case R_SPARC_TLS_GD_LO10:
1380         case R_SPARC_TLS_IE_HI22:
1381         case R_SPARC_TLS_IE_LO10:
1382         case R_SPARC_GOT10:
1383         case R_SPARC_GOT13:
1384         case R_SPARC_GOT22:
1385           if (h != NULL)
1386             {
1387               if (h->got.refcount > 0)
1388                 h->got.refcount--;
1389             }
1390           else
1391             {
1392               if (local_got_refcounts[r_symndx] > 0)
1393                 local_got_refcounts[r_symndx]--;
1394             }
1395           break;
1396
1397         case R_SPARC_PC10:
1398         case R_SPARC_PC22:
1399           if (h != NULL
1400               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1401             break;
1402           /* Fall through.  */
1403
1404         case R_SPARC_DISP8:
1405         case R_SPARC_DISP16:
1406         case R_SPARC_DISP32:
1407         case R_SPARC_WDISP30:
1408         case R_SPARC_WDISP22:
1409         case R_SPARC_WDISP19:
1410         case R_SPARC_WDISP16:
1411         case R_SPARC_8:
1412         case R_SPARC_16:
1413         case R_SPARC_32:
1414         case R_SPARC_HI22:
1415         case R_SPARC_22:
1416         case R_SPARC_13:
1417         case R_SPARC_LO10:
1418         case R_SPARC_UA16:
1419         case R_SPARC_UA32:
1420         case R_SPARC_PLT32:
1421           if (info->shared)
1422             break;
1423           /* Fall through.  */
1424
1425         case R_SPARC_WPLT30:
1426           if (h != NULL)
1427             {
1428               if (h->plt.refcount > 0)
1429                 h->plt.refcount--;
1430             }
1431           break;
1432
1433         default:
1434           break;
1435         }
1436     }
1437
1438   return TRUE;
1439 }
1440
1441 /* Adjust a symbol defined by a dynamic object and referenced by a
1442    regular object.  The current definition is in some section of the
1443    dynamic object, but we're not including those sections.  We have to
1444    change the definition to something the rest of the link can
1445    understand.  */
1446
1447 static bfd_boolean
1448 elf32_sparc_adjust_dynamic_symbol (info, h)
1449      struct bfd_link_info *info;
1450      struct elf_link_hash_entry *h;
1451 {
1452   struct elf32_sparc_link_hash_table *htab;
1453   struct elf32_sparc_link_hash_entry * eh;
1454   struct elf32_sparc_dyn_relocs *p;
1455   asection *s;
1456   unsigned int power_of_two;
1457
1458   htab = elf32_sparc_hash_table (info);
1459
1460   /* Make sure we know what is going on here.  */
1461   BFD_ASSERT (htab->elf.dynobj != NULL
1462               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1463                   || h->weakdef != NULL
1464                   || ((h->elf_link_hash_flags
1465                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1466                       && (h->elf_link_hash_flags
1467                           & ELF_LINK_HASH_REF_REGULAR) != 0
1468                       && (h->elf_link_hash_flags
1469                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1470
1471   /* If this is a function, put it in the procedure linkage table.  We
1472      will fill in the contents of the procedure linkage table later
1473      (although we could actually do it here).  The STT_NOTYPE
1474      condition is a hack specifically for the Oracle libraries
1475      delivered for Solaris; for some inexplicable reason, they define
1476      some of their functions as STT_NOTYPE when they really should be
1477      STT_FUNC.  */
1478   if (h->type == STT_FUNC
1479       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
1480       || (h->type == STT_NOTYPE
1481           && (h->root.type == bfd_link_hash_defined
1482               || h->root.type == bfd_link_hash_defweak)
1483           && (h->root.u.def.section->flags & SEC_CODE) != 0))
1484     {
1485       if (h->plt.refcount <= 0
1486           || (! info->shared
1487               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1488               && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1489               && h->root.type != bfd_link_hash_undefweak
1490               && h->root.type != bfd_link_hash_undefined))
1491         {
1492           /* This case can occur if we saw a WPLT30 reloc in an input
1493              file, but the symbol was never referred to by a dynamic
1494              object, or if all references were garbage collected.  In
1495              such a case, we don't actually need to build a procedure
1496              linkage table, and we can just do a WDISP30 reloc instead.  */
1497           h->plt.offset = (bfd_vma) -1;
1498           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1499         }
1500
1501       return TRUE;
1502     }
1503   else
1504     h->plt.offset = (bfd_vma) -1;
1505
1506   /* If this is a weak symbol, and there is a real definition, the
1507      processor independent code will have arranged for us to see the
1508      real definition first, and we can just use the same value.  */
1509   if (h->weakdef != NULL)
1510     {
1511       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1512                   || h->weakdef->root.type == bfd_link_hash_defweak);
1513       h->root.u.def.section = h->weakdef->root.u.def.section;
1514       h->root.u.def.value = h->weakdef->root.u.def.value;
1515       return TRUE;
1516     }
1517
1518   /* This is a reference to a symbol defined by a dynamic object which
1519      is not a function.  */
1520
1521   /* If we are creating a shared library, we must presume that the
1522      only references to the symbol are via the global offset table.
1523      For such cases we need not do anything here; the relocations will
1524      be handled correctly by relocate_section.  */
1525   if (info->shared)
1526     return TRUE;
1527
1528   /* If there are no references to this symbol that do not use the
1529      GOT, we don't need to generate a copy reloc.  */
1530   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1531     return TRUE;
1532
1533   eh = (struct elf32_sparc_link_hash_entry *) h;
1534   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1535     {
1536       s = p->sec->output_section;
1537       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1538         break;
1539     }
1540
1541   /* If we didn't find any dynamic relocs in read-only sections, then
1542      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1543   if (p == NULL)
1544     {
1545       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1546       return TRUE;
1547     }
1548
1549   /* We must allocate the symbol in our .dynbss section, which will
1550      become part of the .bss section of the executable.  There will be
1551      an entry for this symbol in the .dynsym section.  The dynamic
1552      object will contain position independent code, so all references
1553      from the dynamic object to this symbol will go through the global
1554      offset table.  The dynamic linker will use the .dynsym entry to
1555      determine the address it must put in the global offset table, so
1556      both the dynamic object and the regular object will refer to the
1557      same memory location for the variable.  */
1558
1559   /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
1560      to copy the initial value out of the dynamic object and into the
1561      runtime process image.  We need to remember the offset into the
1562      .rel.bss section we are going to use.  */
1563   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1564     {
1565       htab->srelbss->size += sizeof (Elf32_External_Rela);
1566       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1567     }
1568
1569   /* We need to figure out the alignment required for this symbol.  I
1570      have no idea how ELF linkers handle this.  */
1571   power_of_two = bfd_log2 (h->size);
1572   if (power_of_two > 3)
1573     power_of_two = 3;
1574
1575   /* Apply the required alignment.  */
1576   s = htab->sdynbss;
1577   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1578   if (power_of_two > bfd_get_section_alignment (dynobj, s))
1579     {
1580       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1581         return FALSE;
1582     }
1583
1584   /* Define the symbol as being at this point in the section.  */
1585   h->root.u.def.section = s;
1586   h->root.u.def.value = s->size;
1587
1588   /* Increment the section size to make room for the symbol.  */
1589   s->size += h->size;
1590
1591   return TRUE;
1592 }
1593
1594 /* Allocate space in .plt, .got and associated reloc sections for
1595    dynamic relocs.  */
1596
1597 static bfd_boolean
1598 allocate_dynrelocs (h, inf)
1599      struct elf_link_hash_entry *h;
1600      PTR inf;
1601 {
1602   struct bfd_link_info *info;
1603   struct elf32_sparc_link_hash_table *htab;
1604   struct elf32_sparc_link_hash_entry *eh;
1605   struct elf32_sparc_dyn_relocs *p;
1606
1607   if (h->root.type == bfd_link_hash_indirect)
1608     return TRUE;
1609
1610   if (h->root.type == bfd_link_hash_warning)
1611     /* When warning symbols are created, they **replace** the "real"
1612        entry in the hash table, thus we never get to see the real
1613        symbol in a hash traversal.  So look at it now.  */
1614     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1615
1616   info = (struct bfd_link_info *) inf;
1617   htab = elf32_sparc_hash_table (info);
1618
1619   if (htab->elf.dynamic_sections_created
1620       && h->plt.refcount > 0)
1621     {
1622       /* Make sure this symbol is output as a dynamic symbol.
1623          Undefined weak syms won't yet be marked as dynamic.  */
1624       if (h->dynindx == -1
1625           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1626         {
1627           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1628             return FALSE;
1629         }
1630
1631       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
1632         {
1633           asection *s = htab->splt;
1634
1635           /* The first four entries in .plt are reserved.  */
1636           if (s->size == 0)
1637             s->size = 4 * PLT_ENTRY_SIZE;
1638
1639           /* The procedure linkage table has a maximum size.  */
1640           if (s->size >= 0x400000)
1641             {
1642               bfd_set_error (bfd_error_bad_value);
1643               return FALSE;
1644             }
1645
1646           h->plt.offset = s->size;
1647
1648           /* If this symbol is not defined in a regular file, and we are
1649              not generating a shared library, then set the symbol to this
1650              location in the .plt.  This is required to make function
1651              pointers compare as equal between the normal executable and
1652              the shared library.  */
1653           if (! info->shared
1654               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1655             {
1656               h->root.u.def.section = s;
1657               h->root.u.def.value = h->plt.offset;
1658             }
1659
1660           /* Make room for this entry.  */
1661           s->size += PLT_ENTRY_SIZE;
1662
1663           /* We also need to make an entry in the .rela.plt section.  */
1664           htab->srelplt->size += sizeof (Elf32_External_Rela);
1665         }
1666       else
1667         {
1668           h->plt.offset = (bfd_vma) -1;
1669           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1670         }
1671     }
1672   else
1673     {
1674       h->plt.offset = (bfd_vma) -1;
1675       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1676     }
1677
1678   /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary,
1679      make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry.  */
1680   if (h->got.refcount > 0
1681       && !info->shared
1682       && h->dynindx == -1
1683       && elf32_sparc_hash_entry(h)->tls_type == GOT_TLS_IE)
1684     h->got.offset = (bfd_vma) -1;
1685   else if (h->got.refcount > 0)
1686     {
1687       asection *s;
1688       bfd_boolean dyn;
1689       int tls_type = elf32_sparc_hash_entry(h)->tls_type;
1690
1691       /* Make sure this symbol is output as a dynamic symbol.
1692          Undefined weak syms won't yet be marked as dynamic.  */
1693       if (h->dynindx == -1
1694           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1695         {
1696           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1697             return FALSE;
1698         }
1699
1700       s = htab->sgot;
1701       h->got.offset = s->size;
1702       s->size += 4;
1703       /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots.  */
1704       if (tls_type == GOT_TLS_GD)
1705         s->size += 4;
1706       dyn = htab->elf.dynamic_sections_created;
1707       /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation,
1708          R_SPARC_TLS_GD_{HI22,LO10} needs one if local symbol and two if
1709          global.  */
1710       if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1711           || tls_type == GOT_TLS_IE)
1712         htab->srelgot->size += sizeof (Elf32_External_Rela);
1713       else if (tls_type == GOT_TLS_GD)
1714         htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
1715       else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
1716         htab->srelgot->size += sizeof (Elf32_External_Rela);
1717     }
1718   else
1719     h->got.offset = (bfd_vma) -1;
1720
1721   eh = (struct elf32_sparc_link_hash_entry *) h;
1722   if (eh->dyn_relocs == NULL)
1723     return TRUE;
1724
1725   /* In the shared -Bsymbolic case, discard space allocated for
1726      dynamic pc-relative relocs against symbols which turn out to be
1727      defined in regular objects.  For the normal shared case, discard
1728      space for pc-relative relocs that have become local due to symbol
1729      visibility changes.  */
1730
1731   if (info->shared)
1732     {
1733       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1734           && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1735               || info->symbolic))
1736         {
1737           struct elf32_sparc_dyn_relocs **pp;
1738
1739           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1740             {
1741               p->count -= p->pc_count;
1742               p->pc_count = 0;
1743               if (p->count == 0)
1744                 *pp = p->next;
1745               else
1746                 pp = &p->next;
1747             }
1748         }
1749     }
1750   else
1751     {
1752       /* For the non-shared case, discard space for relocs against
1753          symbols which turn out to need copy relocs or are not
1754          dynamic.  */
1755
1756       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1757           && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1758                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1759               || (htab->elf.dynamic_sections_created
1760                   && (h->root.type == bfd_link_hash_undefweak
1761                       || h->root.type == bfd_link_hash_undefined))))
1762         {
1763           /* Make sure this symbol is output as a dynamic symbol.
1764              Undefined weak syms won't yet be marked as dynamic.  */
1765           if (h->dynindx == -1
1766               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1767             {
1768               if (! bfd_elf_link_record_dynamic_symbol (info, h))
1769                 return FALSE;
1770             }
1771
1772           /* If that succeeded, we know we'll be keeping all the
1773              relocs.  */
1774           if (h->dynindx != -1)
1775             goto keep;
1776         }
1777
1778       eh->dyn_relocs = NULL;
1779
1780     keep: ;
1781     }
1782
1783   /* Finally, allocate space.  */
1784   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1785     {
1786       asection *sreloc = elf_section_data (p->sec)->sreloc;
1787       sreloc->size += p->count * sizeof (Elf32_External_Rela);
1788     }
1789
1790   return TRUE;
1791 }
1792
1793 /* Find any dynamic relocs that apply to read-only sections.  */
1794
1795 static bfd_boolean
1796 readonly_dynrelocs (h, inf)
1797      struct elf_link_hash_entry *h;
1798      PTR inf;
1799 {
1800   struct elf32_sparc_link_hash_entry *eh;
1801   struct elf32_sparc_dyn_relocs *p;
1802
1803   if (h->root.type == bfd_link_hash_warning)
1804     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1805
1806   eh = (struct elf32_sparc_link_hash_entry *) h;
1807   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1808     {
1809       asection *s = p->sec->output_section;
1810
1811       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1812         {
1813           struct bfd_link_info *info = (struct bfd_link_info *) inf;
1814
1815           info->flags |= DF_TEXTREL;
1816
1817           /* Not an error, just cut short the traversal.  */
1818           return FALSE;
1819         }
1820     }
1821   return TRUE;
1822 }
1823
1824 /* Set the sizes of the dynamic sections.  */
1825
1826 static bfd_boolean
1827 elf32_sparc_size_dynamic_sections (output_bfd, info)
1828      bfd *output_bfd ATTRIBUTE_UNUSED;
1829      struct bfd_link_info *info;
1830 {
1831   struct elf32_sparc_link_hash_table *htab;
1832   bfd *dynobj;
1833   asection *s;
1834   bfd *ibfd;
1835
1836   htab = elf32_sparc_hash_table (info);
1837   dynobj = htab->elf.dynobj;
1838   BFD_ASSERT (dynobj != NULL);
1839
1840   if (elf_hash_table (info)->dynamic_sections_created)
1841     {
1842       /* Set the contents of the .interp section to the interpreter.  */
1843       if (info->executable)
1844         {
1845           s = bfd_get_section_by_name (dynobj, ".interp");
1846           BFD_ASSERT (s != NULL);
1847           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1848           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1849         }
1850     }
1851
1852   /* Set up .got offsets for local syms, and space for local dynamic
1853      relocs.  */
1854   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1855     {
1856       bfd_signed_vma *local_got;
1857       bfd_signed_vma *end_local_got;
1858       char *local_tls_type;
1859       bfd_size_type locsymcount;
1860       Elf_Internal_Shdr *symtab_hdr;
1861       asection *srel;
1862
1863       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1864         continue;
1865
1866       for (s = ibfd->sections; s != NULL; s = s->next)
1867         {
1868           struct elf32_sparc_dyn_relocs *p;
1869
1870           for (p = *((struct elf32_sparc_dyn_relocs **)
1871                      &elf_section_data (s)->local_dynrel);
1872                p != NULL;
1873                p = p->next)
1874             {
1875               if (!bfd_is_abs_section (p->sec)
1876                   && bfd_is_abs_section (p->sec->output_section))
1877                 {
1878                   /* Input section has been discarded, either because
1879                      it is a copy of a linkonce section or due to
1880                      linker script /DISCARD/, so we'll be discarding
1881                      the relocs too.  */
1882                 }
1883               else if (p->count != 0)
1884                 {
1885                   srel = elf_section_data (p->sec)->sreloc;
1886                   srel->size += p->count * sizeof (Elf32_External_Rela);
1887                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1888                     info->flags |= DF_TEXTREL;
1889                 }
1890             }
1891         }
1892
1893       local_got = elf_local_got_refcounts (ibfd);
1894       if (!local_got)
1895         continue;
1896
1897       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1898       locsymcount = symtab_hdr->sh_info;
1899       end_local_got = local_got + locsymcount;
1900       local_tls_type = elf32_sparc_local_got_tls_type (ibfd);
1901       s = htab->sgot;
1902       srel = htab->srelgot;
1903       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1904         {
1905           if (*local_got > 0)
1906             {
1907               *local_got = s->size;
1908               s->size += 4;
1909               if (*local_tls_type == GOT_TLS_GD)
1910                 s->size += 4;
1911               if (info->shared
1912                   || *local_tls_type == GOT_TLS_GD
1913                   || *local_tls_type == GOT_TLS_IE)
1914                 srel->size += sizeof (Elf32_External_Rela);
1915             }
1916           else
1917             *local_got = (bfd_vma) -1;
1918         }
1919     }
1920
1921   if (htab->tls_ldm_got.refcount > 0)
1922     {
1923       /* Allocate 2 got entries and 1 dynamic reloc for
1924          R_SPARC_TLS_LDM_{HI22,LO10} relocs.  */
1925       htab->tls_ldm_got.offset = htab->sgot->size;
1926       htab->sgot->size += 8;
1927       htab->srelgot->size += sizeof (Elf32_External_Rela);
1928     }
1929   else
1930     htab->tls_ldm_got.offset = -1;
1931
1932   /* Allocate global sym .plt and .got entries, and space for global
1933      sym dynamic relocs.  */
1934   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1935
1936   if (elf_hash_table (info)->dynamic_sections_created)
1937     {
1938       /* Make space for the trailing nop in .plt.  */
1939       if (htab->splt->size > 0)
1940         htab->splt->size += 4;
1941
1942       /* If the .got section is more than 0x1000 bytes, we add
1943          0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
1944          bit relocations have a greater chance of working.  */
1945       if (htab->sgot->size >= 0x1000
1946           && elf_hash_table (info)->hgot->root.u.def.value == 0)
1947         elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
1948     }
1949
1950   /* The check_relocs and adjust_dynamic_symbol entry points have
1951      determined the sizes of the various dynamic sections.  Allocate
1952      memory for them.  */
1953   for (s = dynobj->sections; s != NULL; s = s->next)
1954     {
1955       const char *name;
1956       bfd_boolean strip = FALSE;
1957
1958       if ((s->flags & SEC_LINKER_CREATED) == 0)
1959         continue;
1960
1961       /* It's OK to base decisions on the section name, because none
1962          of the dynobj section names depend upon the input files.  */
1963       name = bfd_get_section_name (dynobj, s);
1964
1965       if (strncmp (name, ".rela", 5) == 0)
1966         {
1967           if (s->size == 0)
1968             {
1969               /* If we don't need this section, strip it from the
1970                  output file.  This is to handle .rela.bss and
1971                  .rel.plt.  We must create it in
1972                  create_dynamic_sections, because it must be created
1973                  before the linker maps input sections to output
1974                  sections.  The linker does that before
1975                  adjust_dynamic_symbol is called, and it is that
1976                  function which decides whether anything needs to go
1977                  into these sections.  */
1978               strip = TRUE;
1979             }
1980           else
1981             {
1982               /* We use the reloc_count field as a counter if we need
1983                  to copy relocs into the output file.  */
1984               s->reloc_count = 0;
1985             }
1986         }
1987       else if (s != htab->splt && s != htab->sgot)
1988         {
1989           /* It's not one of our sections, so don't allocate space.  */
1990           continue;
1991         }
1992
1993       if (strip)
1994         {
1995           _bfd_strip_section_from_output (info, s);
1996           continue;
1997         }
1998
1999       /* Allocate memory for the section contents.  */
2000       /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
2001          Unused entries should be reclaimed before the section's contents
2002          are written out, but at the moment this does not happen.  Thus in
2003          order to prevent writing out garbage, we initialise the section's
2004          contents to zero.  */
2005       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2006       if (s->contents == NULL && s->size != 0)
2007         return FALSE;
2008     }
2009
2010   if (elf_hash_table (info)->dynamic_sections_created)
2011     {
2012       /* Add some entries to the .dynamic section.  We fill in the
2013          values later, in elf32_sparc_finish_dynamic_sections, but we
2014          must add the entries now so that we get the correct size for
2015          the .dynamic section.  The DT_DEBUG entry is filled in by the
2016          dynamic linker and used by the debugger.  */
2017 #define add_dynamic_entry(TAG, VAL) \
2018   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2019
2020       if (info->executable)
2021         {
2022           if (!add_dynamic_entry (DT_DEBUG, 0))
2023             return FALSE;
2024         }
2025
2026       if (htab->srelplt->size != 0)
2027         {
2028           if (!add_dynamic_entry (DT_PLTGOT, 0)
2029               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2030               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2031               || !add_dynamic_entry (DT_JMPREL, 0))
2032             return FALSE;
2033         }
2034
2035       if (!add_dynamic_entry (DT_RELA, 0)
2036           || !add_dynamic_entry (DT_RELASZ, 0)
2037           || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2038         return FALSE;
2039
2040       /* If any dynamic relocs apply to a read-only section,
2041          then we need a DT_TEXTREL entry.  */
2042       if ((info->flags & DF_TEXTREL) == 0)
2043         elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2044                                 (PTR) info);
2045
2046       if (info->flags & DF_TEXTREL)
2047         {
2048           if (!add_dynamic_entry (DT_TEXTREL, 0))
2049             return FALSE;
2050         }
2051     }
2052 #undef add_dynamic_entry
2053
2054   return TRUE;
2055 }
2056
2057 struct elf32_sparc_section_data
2058 {
2059   struct bfd_elf_section_data elf;
2060   unsigned int do_relax;
2061 };
2062
2063 #define sec_do_relax(sec) \
2064   ((struct elf32_sparc_section_data *) elf_section_data (sec))->do_relax
2065
2066 static bfd_boolean
2067 elf32_sparc_new_section_hook (abfd, sec)
2068      bfd *abfd;
2069      asection *sec;
2070 {
2071   struct elf32_sparc_section_data *sdata;
2072   bfd_size_type amt = sizeof (*sdata);
2073
2074   sdata = (struct elf32_sparc_section_data *) bfd_zalloc (abfd, amt);
2075   if (sdata == NULL)
2076     return FALSE;
2077   sec->used_by_bfd = (PTR) sdata;
2078
2079   return _bfd_elf_new_section_hook (abfd, sec);
2080 }
2081
2082 static bfd_boolean
2083 elf32_sparc_relax_section (abfd, section, link_info, again)
2084      bfd *abfd ATTRIBUTE_UNUSED;
2085      asection *section ATTRIBUTE_UNUSED;
2086      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
2087      bfd_boolean *again;
2088 {
2089   *again = FALSE;
2090   sec_do_relax (section) = 1;
2091   return TRUE;
2092 }
2093
2094 /* Return the base VMA address which should be subtracted from real addresses
2095    when resolving @dtpoff relocation.
2096    This is PT_TLS segment p_vaddr.  */
2097
2098 static bfd_vma
2099 dtpoff_base (info)
2100      struct bfd_link_info *info;
2101 {
2102   /* If tls_sec is NULL, we should have signalled an error already.  */
2103   if (elf_hash_table (info)->tls_sec == NULL)
2104     return 0;
2105   return elf_hash_table (info)->tls_sec->vma;
2106 }
2107
2108 /* Return the relocation value for @tpoff relocation
2109    if STT_TLS virtual address is ADDRESS.  */
2110
2111 static bfd_vma
2112 tpoff (info, address)
2113      struct bfd_link_info *info;
2114      bfd_vma address;
2115 {
2116   struct elf_link_hash_table *htab = elf_hash_table (info);
2117
2118   /* If tls_sec is NULL, we should have signalled an error already.  */
2119   if (htab->tls_sec == NULL)
2120     return 0;
2121   return address - htab->tls_size - htab->tls_sec->vma;
2122 }
2123
2124 /* Relocate a SPARC ELF section.  */
2125
2126 static bfd_boolean
2127 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
2128                               contents, relocs, local_syms, local_sections)
2129      bfd *output_bfd;
2130      struct bfd_link_info *info;
2131      bfd *input_bfd;
2132      asection *input_section;
2133      bfd_byte *contents;
2134      Elf_Internal_Rela *relocs;
2135      Elf_Internal_Sym *local_syms;
2136      asection **local_sections;
2137 {
2138   struct elf32_sparc_link_hash_table *htab;
2139   Elf_Internal_Shdr *symtab_hdr;
2140   struct elf_link_hash_entry **sym_hashes;
2141   bfd_vma *local_got_offsets;
2142   bfd_vma got_base;
2143   asection *sreloc;
2144   Elf_Internal_Rela *rel;
2145   Elf_Internal_Rela *relend;
2146
2147   if (info->relocatable)
2148     return TRUE;
2149
2150   htab = elf32_sparc_hash_table (info);
2151   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2152   sym_hashes = elf_sym_hashes (input_bfd);
2153   local_got_offsets = elf_local_got_offsets (input_bfd);
2154
2155   if (elf_hash_table (info)->hgot == NULL)
2156     got_base = 0;
2157   else
2158     got_base = elf_hash_table (info)->hgot->root.u.def.value;
2159
2160   sreloc = elf_section_data (input_section)->sreloc;
2161
2162   rel = relocs;
2163   relend = relocs + input_section->reloc_count;
2164   for (; rel < relend; rel++)
2165     {
2166       int r_type, tls_type;
2167       reloc_howto_type *howto;
2168       unsigned long r_symndx;
2169       struct elf_link_hash_entry *h;
2170       Elf_Internal_Sym *sym;
2171       asection *sec;
2172       bfd_vma relocation, off;
2173       bfd_reloc_status_type r;
2174       bfd_boolean is_plt = FALSE;
2175       bfd_boolean unresolved_reloc;
2176
2177       r_type = ELF32_R_TYPE (rel->r_info);
2178
2179       if (r_type == R_SPARC_GNU_VTINHERIT
2180           || r_type == R_SPARC_GNU_VTENTRY)
2181         continue;
2182
2183       if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
2184         {
2185           bfd_set_error (bfd_error_bad_value);
2186           return FALSE;
2187         }
2188       howto = _bfd_sparc_elf_howto_table + r_type;
2189
2190       /* This is a final link.  */
2191       r_symndx = ELF32_R_SYM (rel->r_info);
2192       h = NULL;
2193       sym = NULL;
2194       sec = NULL;
2195       unresolved_reloc = FALSE;
2196       if (r_symndx < symtab_hdr->sh_info)
2197         {
2198           sym = local_syms + r_symndx;
2199           sec = local_sections[r_symndx];
2200           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2201         }
2202       else
2203         {
2204           bfd_boolean warned ATTRIBUTE_UNUSED;
2205
2206           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2207                                    r_symndx, symtab_hdr, sym_hashes,
2208                                    h, sec, relocation,
2209                                    unresolved_reloc, warned);
2210         }
2211
2212       switch (r_type)
2213         {
2214         case R_SPARC_GOT10:
2215         case R_SPARC_GOT13:
2216         case R_SPARC_GOT22:
2217           /* Relocation is to the entry for this symbol in the global
2218              offset table.  */
2219           if (htab->sgot == NULL)
2220             abort ();
2221
2222           if (h != NULL)
2223             {
2224               bfd_boolean dyn;
2225
2226               off = h->got.offset;
2227               BFD_ASSERT (off != (bfd_vma) -1);
2228               dyn = elf_hash_table (info)->dynamic_sections_created;
2229
2230               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2231                   || (info->shared
2232                       && (info->symbolic
2233                           || h->dynindx == -1
2234                           || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2235                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2236                 {
2237                   /* This is actually a static link, or it is a
2238                      -Bsymbolic link and the symbol is defined
2239                      locally, or the symbol was forced to be local
2240                      because of a version file.  We must initialize
2241                      this entry in the global offset table.  Since the
2242                      offset must always be a multiple of 4, we use the
2243                      least significant bit to record whether we have
2244                      initialized it already.
2245
2246                      When doing a dynamic link, we create a .rela.got
2247                      relocation entry to initialize the value.  This
2248                      is done in the finish_dynamic_symbol routine.  */
2249                   if ((off & 1) != 0)
2250                     off &= ~1;
2251                   else
2252                     {
2253                       bfd_put_32 (output_bfd, relocation,
2254                                   htab->sgot->contents + off);
2255                       h->got.offset |= 1;
2256                     }
2257                 }
2258               else
2259                 unresolved_reloc = FALSE;
2260             }
2261           else
2262             {
2263               BFD_ASSERT (local_got_offsets != NULL
2264                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
2265
2266               off = local_got_offsets[r_symndx];
2267
2268               /* The offset must always be a multiple of 4.  We use
2269                  the least significant bit to record whether we have
2270                  already processed this entry.  */
2271               if ((off & 1) != 0)
2272                 off &= ~1;
2273               else
2274                 {
2275
2276                   if (info->shared)
2277                     {
2278                       asection *s;
2279                       Elf_Internal_Rela outrel;
2280                       bfd_byte *loc;
2281
2282                       /* We need to generate a R_SPARC_RELATIVE reloc
2283                          for the dynamic linker.  */
2284                       s = htab->srelgot;
2285                       BFD_ASSERT (s != NULL);
2286
2287                       outrel.r_offset = (htab->sgot->output_section->vma
2288                                          + htab->sgot->output_offset
2289                                          + off);
2290                       outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
2291                       outrel.r_addend = relocation;
2292                       relocation = 0;
2293                       loc = s->contents;
2294                       loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
2295                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2296                     }
2297
2298                   bfd_put_32 (output_bfd, relocation,
2299                               htab->sgot->contents + off);
2300                   local_got_offsets[r_symndx] |= 1;
2301                 }
2302             }
2303           relocation = htab->sgot->output_offset + off - got_base;
2304           break;
2305
2306         case R_SPARC_PLT32:
2307           if (h == NULL || h->plt.offset == (bfd_vma) -1)
2308             {
2309               r_type = R_SPARC_32;
2310               goto r_sparc_plt32;
2311             }
2312           /* Fall through.  */
2313         case R_SPARC_WPLT30:
2314         r_sparc_wplt30:
2315           /* Relocation is to the entry for this symbol in the
2316              procedure linkage table.  */
2317
2318           /* The Solaris native assembler will generate a WPLT30 reloc
2319              for a local symbol if you assemble a call from one
2320              section to another when using -K pic.  We treat it as
2321              WDISP30.  */
2322           if (h == NULL)
2323             break;
2324
2325           if (h->plt.offset == (bfd_vma) -1 || htab->splt == NULL)
2326             {
2327               /* We didn't make a PLT entry for this symbol.  This
2328                  happens when statically linking PIC code, or when
2329                  using -Bsymbolic.  */
2330               break;
2331             }
2332
2333           relocation = (htab->splt->output_section->vma
2334                         + htab->splt->output_offset
2335                         + h->plt.offset);
2336           unresolved_reloc = FALSE;
2337           if (r_type == R_SPARC_PLT32)
2338             {
2339               r_type = R_SPARC_32;
2340               is_plt = TRUE;
2341               goto r_sparc_plt32;
2342             }
2343           break;
2344
2345         case R_SPARC_PC10:
2346         case R_SPARC_PC22:
2347           if (h != NULL
2348               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2349             break;
2350           /* Fall through.  */
2351         case R_SPARC_DISP8:
2352         case R_SPARC_DISP16:
2353         case R_SPARC_DISP32:
2354         case R_SPARC_WDISP30:
2355         case R_SPARC_WDISP22:
2356         case R_SPARC_WDISP19:
2357         case R_SPARC_WDISP16:
2358         case R_SPARC_8:
2359         case R_SPARC_16:
2360         case R_SPARC_32:
2361         case R_SPARC_HI22:
2362         case R_SPARC_22:
2363         case R_SPARC_13:
2364         case R_SPARC_LO10:
2365         case R_SPARC_UA16:
2366         case R_SPARC_UA32:
2367         r_sparc_plt32:
2368           /* r_symndx will be zero only for relocs against symbols
2369              from removed linkonce sections, or sections discarded by
2370              a linker script.  */
2371           if (r_symndx == 0
2372               || (input_section->flags & SEC_ALLOC) == 0)
2373             break;
2374
2375           if ((info->shared
2376                && (h == NULL
2377                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2378                    || h->root.type != bfd_link_hash_undefweak)
2379                && (! howto->pc_relative
2380                    || (h != NULL
2381                        && h->dynindx != -1
2382                        && (! info->symbolic
2383                            || (h->elf_link_hash_flags
2384                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2385               || (!info->shared
2386                   && h != NULL
2387                   && h->dynindx != -1
2388                   && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2389                   && (((h->elf_link_hash_flags
2390                         & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2391                        && (h->elf_link_hash_flags
2392                            & ELF_LINK_HASH_DEF_REGULAR) == 0)
2393                       || h->root.type == bfd_link_hash_undefweak
2394                       || h->root.type == bfd_link_hash_undefined)))
2395             {
2396               Elf_Internal_Rela outrel;
2397               bfd_byte *loc;
2398               bfd_boolean skip, relocate = FALSE;
2399
2400               /* When generating a shared object, these relocations
2401                  are copied into the output file to be resolved at run
2402                  time.  */
2403
2404               BFD_ASSERT (sreloc != NULL);
2405
2406               skip = FALSE;
2407
2408               outrel.r_offset =
2409                 _bfd_elf_section_offset (output_bfd, info, input_section,
2410                                          rel->r_offset);
2411               if (outrel.r_offset == (bfd_vma) -1)
2412                 skip = TRUE;
2413               else if (outrel.r_offset == (bfd_vma) -2)
2414                 skip = TRUE, relocate = TRUE;
2415               outrel.r_offset += (input_section->output_section->vma
2416                                   + input_section->output_offset);
2417
2418               /* Optimize unaligned reloc usage now that we know where
2419                  it finally resides.  */
2420               switch (r_type)
2421                 {
2422                 case R_SPARC_16:
2423                   if (outrel.r_offset & 1)
2424                     r_type = R_SPARC_UA16;
2425                   break;
2426                 case R_SPARC_UA16:
2427                   if (!(outrel.r_offset & 1))
2428                     r_type = R_SPARC_16;
2429                   break;
2430                 case R_SPARC_32:
2431                   if (outrel.r_offset & 3)
2432                     r_type = R_SPARC_UA32;
2433                   break;
2434                 case R_SPARC_UA32:
2435                   if (!(outrel.r_offset & 3))
2436                     r_type = R_SPARC_32;
2437                   break;
2438                 case R_SPARC_DISP8:
2439                 case R_SPARC_DISP16:
2440                 case R_SPARC_DISP32:
2441                   /* If the symbol is not dynamic, we should not keep
2442                      a dynamic relocation.  But an .rela.* slot has been
2443                      allocated for it, output R_SPARC_NONE.
2444                      FIXME: Add code tracking needed dynamic relocs as
2445                      e.g. i386 has.  */
2446                   if (h->dynindx == -1)
2447                     skip = TRUE, relocate = TRUE;
2448                   break;
2449                 }
2450
2451               if (skip)
2452                 memset (&outrel, 0, sizeof outrel);
2453               /* h->dynindx may be -1 if the symbol was marked to
2454                  become local.  */
2455               else if (h != NULL && ! is_plt
2456                        && ((! info->symbolic && h->dynindx != -1)
2457                            || (h->elf_link_hash_flags
2458                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
2459                 {
2460                   BFD_ASSERT (h->dynindx != -1);
2461                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2462                   outrel.r_addend = rel->r_addend;
2463                 }
2464               else
2465                 {
2466                   if (r_type == R_SPARC_32)
2467                     {
2468                       outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
2469                       outrel.r_addend = relocation + rel->r_addend;
2470                     }
2471                   else
2472                     {
2473                       long indx;
2474
2475                       if (is_plt)
2476                         sec = htab->splt;
2477
2478                       if (bfd_is_abs_section (sec))
2479                         indx = 0;
2480                       else if (sec == NULL || sec->owner == NULL)
2481                         {
2482                           bfd_set_error (bfd_error_bad_value);
2483                           return FALSE;
2484                         }
2485                       else
2486                         {
2487                           asection *osec;
2488
2489                           osec = sec->output_section;
2490                           indx = elf_section_data (osec)->dynindx;
2491
2492                           /* FIXME: we really should be able to link non-pic
2493                              shared libraries.  */
2494                           if (indx == 0)
2495                             {
2496                               BFD_FAIL ();
2497                               (*_bfd_error_handler)
2498                                 (_("%s: probably compiled without -fPIC?"),
2499                                  bfd_archive_filename (input_bfd));
2500                               bfd_set_error (bfd_error_bad_value);
2501                               return FALSE;
2502                             }
2503                         }
2504
2505                       outrel.r_info = ELF32_R_INFO (indx, r_type);
2506                       outrel.r_addend = relocation + rel->r_addend;
2507                     }
2508                 }
2509
2510               loc = sreloc->contents;
2511               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2512               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2513
2514               /* This reloc will be computed at runtime, so there's no
2515                  need to do anything now.  */
2516               if (! relocate)
2517                 continue;
2518             }
2519           break;
2520
2521         case R_SPARC_TLS_GD_HI22:
2522           if (! elf32_sparc_tdata (input_bfd)->has_tlsgd)
2523             {
2524               /* R_SPARC_REV32 used the same reloc number as
2525                  R_SPARC_TLS_GD_HI22.  */
2526               r_type = R_SPARC_REV32;
2527               break;
2528             }
2529           /* Fall through */
2530
2531         case R_SPARC_TLS_GD_LO10:
2532         case R_SPARC_TLS_IE_HI22:
2533         case R_SPARC_TLS_IE_LO10:
2534           r_type = elf32_sparc_tls_transition (info, input_bfd, r_type,
2535                                                h == NULL);
2536           tls_type = GOT_UNKNOWN;
2537           if (h == NULL && local_got_offsets)
2538             tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx];
2539           else if (h != NULL)
2540             {
2541               tls_type = elf32_sparc_hash_entry(h)->tls_type;
2542               if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2543                 switch (ELF32_R_TYPE (rel->r_info))
2544                   {
2545                   case R_SPARC_TLS_GD_HI22:
2546                   case R_SPARC_TLS_IE_HI22:
2547                     r_type = R_SPARC_TLS_LE_HIX22;
2548                     break;
2549                   default:
2550                     r_type = R_SPARC_TLS_LE_LOX10;
2551                     break;
2552                   }
2553             }
2554           if (tls_type == GOT_TLS_IE)
2555             switch (r_type)
2556               {
2557               case R_SPARC_TLS_GD_HI22:
2558                 r_type = R_SPARC_TLS_IE_HI22;
2559                 break;
2560               case R_SPARC_TLS_GD_LO10:
2561                 r_type = R_SPARC_TLS_IE_LO10;
2562                 break;
2563               }
2564
2565           if (r_type == R_SPARC_TLS_LE_HIX22)
2566             {
2567               relocation = tpoff (info, relocation);
2568               break;
2569             }
2570           if (r_type == R_SPARC_TLS_LE_LOX10)
2571             {
2572               /* Change add into xor.  */
2573               relocation = tpoff (info, relocation);
2574               bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd,
2575                                                    contents + rel->r_offset)
2576                                        | 0x80182000), contents + rel->r_offset);
2577               break;
2578             }
2579
2580           if (h != NULL)
2581             {
2582               off = h->got.offset;
2583               h->got.offset |= 1;
2584             }
2585           else
2586             {
2587               BFD_ASSERT (local_got_offsets != NULL);
2588               off = local_got_offsets[r_symndx];
2589               local_got_offsets[r_symndx] |= 1;
2590             }
2591
2592         r_sparc_tlsldm:
2593           if (htab->sgot == NULL)
2594             abort ();
2595
2596           if ((off & 1) != 0)
2597             off &= ~1;
2598           else
2599             {
2600               Elf_Internal_Rela outrel;
2601               Elf32_External_Rela *loc;
2602               int dr_type, indx;
2603
2604               if (htab->srelgot == NULL)
2605                 abort ();
2606
2607               bfd_put_32 (output_bfd, 0, htab->sgot->contents + off);
2608               outrel.r_offset = (htab->sgot->output_section->vma
2609                                  + htab->sgot->output_offset + off);
2610               indx = h && h->dynindx != -1 ? h->dynindx : 0;
2611               if (r_type == R_SPARC_TLS_IE_HI22
2612                 || r_type == R_SPARC_TLS_IE_LO10)
2613                 dr_type = R_SPARC_TLS_TPOFF32;
2614               else
2615                 dr_type = R_SPARC_TLS_DTPMOD32;
2616               if (dr_type == R_SPARC_TLS_TPOFF32 && indx == 0)
2617                 outrel.r_addend = relocation - dtpoff_base (info);
2618               else
2619                 outrel.r_addend = 0;
2620               outrel.r_info = ELF32_R_INFO (indx, dr_type);
2621               loc = (Elf32_External_Rela *) htab->srelgot->contents;
2622               loc += htab->srelgot->reloc_count++;
2623               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2624                                          (bfd_byte *) loc);
2625
2626               if (r_type == R_SPARC_TLS_GD_HI22
2627                   || r_type == R_SPARC_TLS_GD_LO10)
2628                 {
2629                   if (indx == 0)
2630                     {
2631                       BFD_ASSERT (! unresolved_reloc);
2632                       bfd_put_32 (output_bfd,
2633                                   relocation - dtpoff_base (info),
2634                                   htab->sgot->contents + off + 4);
2635                     }
2636                   else
2637                     {
2638                       bfd_put_32 (output_bfd, 0,
2639                                   htab->sgot->contents + off + 4);
2640                       outrel.r_info = ELF32_R_INFO (indx,
2641                                                     R_SPARC_TLS_DTPOFF32);
2642                       outrel.r_offset += 4;
2643                       htab->srelgot->reloc_count++;
2644                       loc++;
2645                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2646                                                  (bfd_byte *) loc);
2647                     }
2648                 }
2649               else if (dr_type == R_SPARC_TLS_DTPMOD32)
2650                 {
2651                   bfd_put_32 (output_bfd, 0,
2652                               htab->sgot->contents + off + 4);
2653                 }
2654             }
2655
2656           if (off >= (bfd_vma) -2)
2657             abort ();
2658
2659           relocation = htab->sgot->output_offset + off - got_base;
2660           unresolved_reloc = FALSE;
2661           howto = _bfd_sparc_elf_howto_table + r_type;
2662           break;
2663
2664         case R_SPARC_TLS_LDM_HI22:
2665         case R_SPARC_TLS_LDM_LO10:
2666           if (! info->shared)
2667             {
2668               bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2669               continue;
2670             }
2671           off = htab->tls_ldm_got.offset;
2672           htab->tls_ldm_got.offset |= 1;
2673           goto r_sparc_tlsldm;
2674
2675         case R_SPARC_TLS_LDO_HIX22:
2676         case R_SPARC_TLS_LDO_LOX10:
2677           if (info->shared)
2678             relocation -= dtpoff_base (info);
2679           else
2680             relocation = tpoff (info, relocation);
2681           break;
2682
2683         case R_SPARC_TLS_LE_HIX22:
2684         case R_SPARC_TLS_LE_LOX10:
2685           if (info->shared)
2686             {
2687               Elf_Internal_Rela outrel;
2688               bfd_boolean skip, relocate = FALSE;
2689
2690               BFD_ASSERT (sreloc != NULL);
2691               skip = FALSE;
2692               outrel.r_offset =
2693                 _bfd_elf_section_offset (output_bfd, info, input_section,
2694                                          rel->r_offset);
2695               if (outrel.r_offset == (bfd_vma) -1)
2696                 skip = TRUE;
2697               else if (outrel.r_offset == (bfd_vma) -2)
2698                 skip = TRUE, relocate = TRUE;
2699               outrel.r_offset += (input_section->output_section->vma
2700                                   + input_section->output_offset);
2701               if (skip)
2702                 memset (&outrel, 0, sizeof outrel);
2703               else
2704                 {
2705                   outrel.r_info = ELF32_R_INFO (0, r_type);
2706                   outrel.r_addend = relocation - dtpoff_base (info)
2707                                     + rel->r_addend;
2708                 }
2709
2710               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2711                                          (bfd_byte *) (((Elf32_External_Rela *)
2712                                                         sreloc->contents)
2713                                                        + sreloc->reloc_count));
2714               ++sreloc->reloc_count;
2715               continue;
2716             }
2717           relocation = tpoff (info, relocation);
2718           break;
2719
2720         case R_SPARC_TLS_LDM_CALL:
2721           if (! info->shared)
2722             {
2723               /* mov %g0, %o0 */
2724               bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset);
2725               continue;
2726             }
2727           /* Fall through */
2728
2729         case R_SPARC_TLS_GD_CALL:
2730           tls_type = GOT_UNKNOWN;
2731           if (h == NULL && local_got_offsets)
2732             tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx];
2733           else if (h != NULL)
2734             tls_type = elf32_sparc_hash_entry(h)->tls_type;
2735           if (! info->shared
2736               || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE))
2737             {
2738               bfd_vma insn;
2739
2740               if (!info->shared && (h == NULL || h->dynindx == -1))
2741                 {
2742                   /* GD -> LE */
2743                   bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2744                   continue;
2745                 }
2746
2747               /* GD -> IE */
2748               if (rel + 1 < relend
2749                   && ELF32_R_TYPE (rel[1].r_info) == R_SPARC_TLS_GD_ADD
2750                   && rel[1].r_offset == rel->r_offset + 4
2751                   && ELF32_R_SYM (rel[1].r_info) == r_symndx
2752                   && (((insn = bfd_get_32 (input_bfd,
2753                                            contents + rel[1].r_offset))
2754                        >> 25) & 0x1f) == 8)
2755                 {
2756                   /* We have
2757                      call __tls_get_addr, %tgd_call(foo)
2758                       add %reg1, %reg2, %o0, %tgd_add(foo)
2759                      and change it into IE:
2760                      ld [%reg1 + %reg2], %o0, %tie_ld(foo)
2761                      add %g7, %o0, %o0, %tie_add(foo).
2762                      add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2,
2763                      ld is 0xc0000000 | (rd << 25) | (rs1 << 14) | rs2.  */
2764                   bfd_put_32 (output_bfd, insn | 0xc0000000,
2765                               contents + rel->r_offset);
2766                   bfd_put_32 (output_bfd, 0x9001c008,
2767                               contents + rel->r_offset + 4);
2768                   rel++;
2769                   continue;
2770                 }
2771
2772               bfd_put_32 (output_bfd, 0x9001c008, contents + rel->r_offset);
2773               continue;
2774             }
2775
2776           h = (struct elf_link_hash_entry *)
2777               bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE,
2778                                     FALSE, TRUE);
2779           BFD_ASSERT (h != NULL);
2780           r_type = R_SPARC_WPLT30;
2781           howto = _bfd_sparc_elf_howto_table + r_type;
2782           goto r_sparc_wplt30;
2783
2784         case R_SPARC_TLS_GD_ADD:
2785           tls_type = GOT_UNKNOWN;
2786           if (h == NULL && local_got_offsets)
2787             tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx];
2788           else if (h != NULL)
2789             tls_type = elf32_sparc_hash_entry(h)->tls_type;
2790           if (! info->shared || tls_type == GOT_TLS_IE)
2791             {
2792               /* add %reg1, %reg2, %reg3, %tgd_add(foo)
2793                  changed into IE:
2794                  ld [%reg1 + %reg2], %reg3, %tie_ld(foo)
2795                  or LE:
2796                  add %g7, %reg2, %reg3.  */
2797               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2798               if ((h != NULL && h->dynindx != -1) || info->shared)
2799                 relocation = insn | 0xc0000000;
2800               else
2801                 relocation = (insn & ~0x7c000) | 0x1c000;
2802               bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
2803             }
2804           continue;
2805
2806         case R_SPARC_TLS_LDM_ADD:
2807           if (! info->shared)
2808             bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2809           continue;
2810
2811         case R_SPARC_TLS_LDO_ADD:
2812           if (! info->shared)
2813             {
2814               /* Change rs1 into %g7.  */
2815               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2816               insn = (insn & ~0x7c000) | 0x1c000;
2817               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
2818             }
2819           continue;
2820
2821         case R_SPARC_TLS_IE_LD:
2822         case R_SPARC_TLS_IE_LDX:
2823           if (! info->shared && (h == NULL || h->dynindx == -1))
2824             {
2825               bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2826               int rs2 = insn & 0x1f;
2827               int rd = (insn >> 25) & 0x1f;
2828
2829               if (rs2 == rd)
2830                 relocation = SPARC_NOP;
2831               else
2832                 relocation = 0x80100000 | (insn & 0x3e00001f);
2833               bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
2834             }
2835           continue;
2836
2837         case R_SPARC_TLS_IE_ADD:
2838           /* Totally useless relocation.  */
2839           continue;
2840
2841         case R_SPARC_TLS_DTPOFF32:
2842           relocation -= dtpoff_base (info);
2843           break;
2844
2845         default:
2846           break;
2847         }
2848
2849       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2850          because such sections are not SEC_ALLOC and thus ld.so will
2851          not process them.  */
2852       if (unresolved_reloc
2853           && !((input_section->flags & SEC_DEBUGGING) != 0
2854                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2855         (*_bfd_error_handler)
2856           (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2857            bfd_archive_filename (input_bfd),
2858            bfd_get_section_name (input_bfd, input_section),
2859            (long) rel->r_offset,
2860            h->root.root.string);
2861
2862       r = bfd_reloc_continue;
2863       if (r_type == R_SPARC_WDISP16)
2864         {
2865           bfd_vma x;
2866
2867           relocation += rel->r_addend;
2868           relocation -= (input_section->output_section->vma
2869                          + input_section->output_offset);
2870           relocation -= rel->r_offset;
2871
2872           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2873           x |= ((((relocation >> 2) & 0xc000) << 6)
2874                 | ((relocation >> 2) & 0x3fff));
2875           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2876
2877           if ((bfd_signed_vma) relocation < - 0x40000
2878               || (bfd_signed_vma) relocation > 0x3ffff)
2879             r = bfd_reloc_overflow;
2880           else
2881             r = bfd_reloc_ok;
2882         }
2883       else if (r_type == R_SPARC_REV32)
2884         {
2885           bfd_vma x;
2886
2887           relocation = relocation + rel->r_addend;
2888
2889           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2890           x = x + relocation;
2891           bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
2892           r = bfd_reloc_ok;
2893         }
2894       else if (r_type == R_SPARC_TLS_LDO_HIX22
2895                || r_type == R_SPARC_TLS_LE_HIX22)
2896         {
2897           bfd_vma x;
2898
2899           relocation += rel->r_addend;
2900           relocation = relocation ^ 0xffffffff;
2901
2902           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2903           x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
2904           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2905           r = bfd_reloc_ok;
2906         }
2907       else if (r_type == R_SPARC_TLS_LDO_LOX10
2908                || r_type == R_SPARC_TLS_LE_LOX10)
2909         {
2910           bfd_vma x;
2911
2912           relocation += rel->r_addend;
2913           relocation = (relocation & 0x3ff) | 0x1c00;
2914
2915           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2916           x = (x & ~(bfd_vma) 0x1fff) | relocation;
2917           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2918
2919           r = bfd_reloc_ok;
2920         }
2921       else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
2922                && sec_do_relax (input_section)
2923                && rel->r_offset + 4 < input_section->size)
2924         {
2925 #define G0              0
2926 #define O7              15
2927 #define XCC             (2 << 20)
2928 #define COND(x)         (((x)&0xf)<<25)
2929 #define CONDA           COND(0x8)
2930 #define INSN_BPA        (F2(0,1) | CONDA | BPRED | XCC)
2931 #define INSN_BA         (F2(0,2) | CONDA)
2932 #define INSN_OR         F3(2, 0x2, 0)
2933 #define INSN_NOP        F2(0,4)
2934
2935           bfd_vma x, y;
2936
2937           /* If the instruction is a call with either:
2938              restore
2939              arithmetic instruction with rd == %o7
2940              where rs1 != %o7 and rs2 if it is register != %o7
2941              then we can optimize if the call destination is near
2942              by changing the call into a branch always.  */
2943           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2944           y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
2945           if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
2946             {
2947               if (((y & OP3(~0)) == OP3(0x3d) /* restore */
2948                    || ((y & OP3(0x28)) == 0 /* arithmetic */
2949                        && (y & RD(~0)) == RD(O7)))
2950                   && (y & RS1(~0)) != RS1(O7)
2951                   && ((y & F3I(~0))
2952                       || (y & RS2(~0)) != RS2(O7)))
2953                 {
2954                   bfd_vma reloc;
2955
2956                   reloc = relocation + rel->r_addend - rel->r_offset;
2957                   reloc -= (input_section->output_section->vma
2958                            + input_section->output_offset);
2959
2960                   /* Ensure the reloc fits into simm22.  */
2961                   if ((reloc & 3) == 0
2962                       && ((reloc & ~(bfd_vma)0x7fffff) == 0
2963                           || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
2964                     {
2965                       reloc >>= 2;
2966
2967                       /* Check whether it fits into simm19 on v9.  */
2968                       if (((reloc & 0x3c0000) == 0
2969                            || (reloc & 0x3c0000) == 0x3c0000)
2970                           && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
2971                         x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
2972                       else
2973                         x = INSN_BA | (reloc & 0x3fffff); /* ba */
2974                       bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2975                       r = bfd_reloc_ok;
2976                       if (rel->r_offset >= 4
2977                           && (y & (0xffffffff ^ RS1(~0)))
2978                               == (INSN_OR | RD(O7) | RS2(G0)))
2979                         {
2980                           bfd_vma z;
2981                           unsigned int reg;
2982
2983                           z = bfd_get_32 (input_bfd,
2984                                           contents + rel->r_offset - 4);
2985                           if ((z & (0xffffffff ^ RD(~0)))
2986                               != (INSN_OR | RS1(O7) | RS2(G0)))
2987                             break;
2988
2989                           /* The sequence was
2990                              or %o7, %g0, %rN
2991                              call foo
2992                              or %rN, %g0, %o7
2993
2994                              If call foo was replaced with ba, replace
2995                              or %rN, %g0, %o7 with nop.  */
2996
2997                           reg = (y & RS1(~0)) >> 14;
2998                           if (reg != ((z & RD(~0)) >> 25)
2999                               || reg == G0 || reg == O7)
3000                             break;
3001
3002                           bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
3003                                       contents + rel->r_offset + 4);
3004                         }
3005
3006                     }
3007                 }
3008             }
3009         }
3010
3011       if (r == bfd_reloc_continue)
3012         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3013                                       contents, rel->r_offset,
3014                                       relocation, rel->r_addend);
3015
3016       if (r != bfd_reloc_ok)
3017         {
3018           switch (r)
3019             {
3020             default:
3021             case bfd_reloc_outofrange:
3022               abort ();
3023             case bfd_reloc_overflow:
3024               {
3025                 const char *name;
3026
3027                 if (h != NULL)
3028                   name = h->root.root.string;
3029                 else
3030                   {
3031                     name = bfd_elf_string_from_elf_section (input_bfd,
3032                                                             symtab_hdr->sh_link,
3033                                                             sym->st_name);
3034                     if (name == NULL)
3035                       return FALSE;
3036                     if (*name == '\0')
3037                       name = bfd_section_name (input_bfd, sec);
3038                   }
3039                 if (! ((*info->callbacks->reloc_overflow)
3040                        (info, name, howto->name, (bfd_vma) 0,
3041                         input_bfd, input_section, rel->r_offset)))
3042                   return FALSE;
3043               }
3044               break;
3045             }
3046         }
3047     }
3048
3049   return TRUE;
3050 }
3051
3052 /* Finish up dynamic symbol handling.  We set the contents of various
3053    dynamic sections here.  */
3054
3055 static bfd_boolean
3056 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
3057      bfd *output_bfd;
3058      struct bfd_link_info *info;
3059      struct elf_link_hash_entry *h;
3060      Elf_Internal_Sym *sym;
3061 {
3062   bfd *dynobj;
3063   struct elf32_sparc_link_hash_table *htab;
3064
3065   htab = elf32_sparc_hash_table (info);
3066   dynobj = htab->elf.dynobj;
3067
3068   if (h->plt.offset != (bfd_vma) -1)
3069     {
3070       asection *splt;
3071       asection *srela;
3072       Elf_Internal_Rela rela;
3073       bfd_byte *loc;
3074
3075       /* This symbol has an entry in the procedure linkage table.  Set
3076          it up.  */
3077
3078       BFD_ASSERT (h->dynindx != -1);
3079
3080       splt = htab->splt;
3081       srela = htab->srelplt;
3082       BFD_ASSERT (splt != NULL && srela != NULL);
3083
3084       /* Fill in the entry in the procedure linkage table.  */
3085       bfd_put_32 (output_bfd,
3086                   PLT_ENTRY_WORD0 + h->plt.offset,
3087                   splt->contents + h->plt.offset);
3088       bfd_put_32 (output_bfd,
3089                   (PLT_ENTRY_WORD1
3090                    + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
3091                   splt->contents + h->plt.offset + 4);
3092       bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
3093                   splt->contents + h->plt.offset + 8);
3094
3095       /* Fill in the entry in the .rela.plt section.  */
3096       rela.r_offset = (splt->output_section->vma
3097                        + splt->output_offset
3098                        + h->plt.offset);
3099       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
3100       rela.r_addend = 0;
3101       loc = srela->contents;
3102       loc += (h->plt.offset / PLT_ENTRY_SIZE - 4) * sizeof (Elf32_External_Rela);
3103       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3104
3105       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3106         {
3107           /* Mark the symbol as undefined, rather than as defined in
3108              the .plt section.  Leave the value alone.  */
3109           sym->st_shndx = SHN_UNDEF;
3110           /* If the symbol is weak, we do need to clear the value.
3111              Otherwise, the PLT entry would provide a definition for
3112              the symbol even if the symbol wasn't defined anywhere,
3113              and so the symbol would never be NULL.  */
3114           if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
3115               == 0)
3116             sym->st_value = 0;
3117         }
3118     }
3119
3120   if (h->got.offset != (bfd_vma) -1
3121       && elf32_sparc_hash_entry(h)->tls_type != GOT_TLS_GD
3122       && elf32_sparc_hash_entry(h)->tls_type != GOT_TLS_IE)
3123     {
3124       asection *sgot;
3125       asection *srela;
3126       Elf_Internal_Rela rela;
3127       bfd_byte *loc;
3128
3129       /* This symbol has an entry in the global offset table.  Set it
3130          up.  */
3131
3132       sgot = htab->sgot;
3133       srela = htab->srelgot;
3134       BFD_ASSERT (sgot != NULL && srela != NULL);
3135
3136       rela.r_offset = (sgot->output_section->vma
3137                        + sgot->output_offset
3138                        + (h->got.offset &~ (bfd_vma) 1));
3139
3140       /* If this is a -Bsymbolic link, and the symbol is defined
3141          locally, we just want to emit a RELATIVE reloc.  Likewise if
3142          the symbol was forced to be local because of a version file.
3143          The entry in the global offset table will already have been
3144          initialized in the relocate_section function.  */
3145       if (info->shared
3146           && (info->symbolic || h->dynindx == -1)
3147           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3148         {
3149           asection *sec = h->root.u.def.section;
3150           rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
3151           rela.r_addend = (h->root.u.def.value
3152                            + sec->output_section->vma
3153                            + sec->output_offset);
3154         }
3155       else
3156         {
3157           rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
3158           rela.r_addend = 0;
3159         }
3160
3161       bfd_put_32 (output_bfd, (bfd_vma) 0,
3162                   sgot->contents + (h->got.offset &~ (bfd_vma) 1));
3163       loc = srela->contents;
3164       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3165       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3166     }
3167
3168   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3169     {
3170       asection *s;
3171       Elf_Internal_Rela rela;
3172       bfd_byte *loc;
3173
3174       /* This symbols needs a copy reloc.  Set it up.  */
3175
3176       BFD_ASSERT (h->dynindx != -1);
3177
3178       s = bfd_get_section_by_name (h->root.u.def.section->owner,
3179                                    ".rela.bss");
3180       BFD_ASSERT (s != NULL);
3181
3182       rela.r_offset = (h->root.u.def.value
3183                        + h->root.u.def.section->output_section->vma
3184                        + h->root.u.def.section->output_offset);
3185       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
3186       rela.r_addend = 0;
3187       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3188       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3189     }
3190
3191   /* Mark some specially defined symbols as absolute.  */
3192   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3193       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3194       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3195     sym->st_shndx = SHN_ABS;
3196
3197   return TRUE;
3198 }
3199
3200 /* Finish up the dynamic sections.  */
3201
3202 static bfd_boolean
3203 elf32_sparc_finish_dynamic_sections (output_bfd, info)
3204      bfd *output_bfd;
3205      struct bfd_link_info *info;
3206 {
3207   bfd *dynobj;
3208   asection *sdyn;
3209   struct elf32_sparc_link_hash_table *htab;
3210
3211   htab = elf32_sparc_hash_table (info);
3212   dynobj = htab->elf.dynobj;
3213
3214   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3215
3216   if (elf_hash_table (info)->dynamic_sections_created)
3217     {
3218       asection *splt;
3219       Elf32_External_Dyn *dyncon, *dynconend;
3220
3221       splt = bfd_get_section_by_name (dynobj, ".plt");
3222       BFD_ASSERT (splt != NULL && sdyn != NULL);
3223
3224       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3225       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3226       for (; dyncon < dynconend; dyncon++)
3227         {
3228           Elf_Internal_Dyn dyn;
3229           const char *name;
3230           bfd_boolean size;
3231
3232           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3233
3234           switch (dyn.d_tag)
3235             {
3236             case DT_PLTGOT:   name = ".plt"; size = FALSE; break;
3237             case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
3238             case DT_JMPREL:   name = ".rela.plt"; size = FALSE; break;
3239             default:      name = NULL; size = FALSE; break;
3240             }
3241
3242           if (name != NULL)
3243             {
3244               asection *s;
3245
3246               s = bfd_get_section_by_name (output_bfd, name);
3247               if (s == NULL)
3248                 dyn.d_un.d_val = 0;
3249               else
3250                 {
3251                   if (! size)
3252                     dyn.d_un.d_ptr = s->vma;
3253                   else
3254                     dyn.d_un.d_val = s->size;
3255                 }
3256               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3257             }
3258         }
3259
3260       /* Clear the first four entries in the procedure linkage table,
3261          and put a nop in the last four bytes.  */
3262       if (splt->size > 0)
3263         {
3264           memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
3265           bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
3266                       splt->contents + splt->size - 4);
3267         }
3268
3269       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 0;
3270     }
3271
3272   /* Set the first entry in the global offset table to the address of
3273      the dynamic section.  */
3274   if (htab->sgot && htab->sgot->size > 0)
3275     {
3276       if (sdyn == NULL)
3277         bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgot->contents);
3278       else
3279         bfd_put_32 (output_bfd,
3280                     sdyn->output_section->vma + sdyn->output_offset,
3281                     htab->sgot->contents);
3282     }
3283
3284   if (htab->sgot)
3285     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
3286
3287   return TRUE;
3288 }
3289 \f
3290 /* Functions for dealing with the e_flags field.
3291
3292    We don't define set_private_flags or copy_private_bfd_data because
3293    the only currently defined values are based on the bfd mach number,
3294    so we use the latter instead and defer setting e_flags until the
3295    file is written out.  */
3296
3297 /* Merge backend specific data from an object file to the output
3298    object file when linking.  */
3299
3300 static bfd_boolean
3301 elf32_sparc_merge_private_bfd_data (ibfd, obfd)
3302      bfd *ibfd;
3303      bfd *obfd;
3304 {
3305   bfd_boolean error;
3306   unsigned long ibfd_mach;
3307   /* FIXME: This should not be static.  */
3308   static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
3309
3310   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3311       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3312     return TRUE;
3313
3314   error = FALSE;
3315
3316   ibfd_mach = bfd_get_mach (ibfd);
3317   if (bfd_mach_sparc_64bit_p (ibfd_mach))
3318     {
3319       error = TRUE;
3320       (*_bfd_error_handler)
3321         (_("%s: compiled for a 64 bit system and target is 32 bit"),
3322          bfd_archive_filename (ibfd));
3323     }
3324   else if ((ibfd->flags & DYNAMIC) == 0)
3325     {
3326       if (bfd_get_mach (obfd) < ibfd_mach)
3327         bfd_set_arch_mach (obfd, bfd_arch_sparc, ibfd_mach);
3328     }
3329
3330   if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
3331        != previous_ibfd_e_flags)
3332       && previous_ibfd_e_flags != (unsigned long) -1)
3333     {
3334       (*_bfd_error_handler)
3335         (_("%s: linking little endian files with big endian files"),
3336          bfd_archive_filename (ibfd));
3337       error = TRUE;
3338     }
3339   previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
3340
3341   if (error)
3342     {
3343       bfd_set_error (bfd_error_bad_value);
3344       return FALSE;
3345     }
3346
3347   return TRUE;
3348 }
3349 \f
3350 /* Set the right machine number.  */
3351
3352 static bfd_boolean
3353 elf32_sparc_object_p (abfd)
3354      bfd *abfd;
3355 {
3356   if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
3357     {
3358       if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
3359         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3360                                           bfd_mach_sparc_v8plusb);
3361       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
3362         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3363                                           bfd_mach_sparc_v8plusa);
3364       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
3365         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3366                                           bfd_mach_sparc_v8plus);
3367       else
3368         return FALSE;
3369     }
3370   else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
3371     return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3372                                       bfd_mach_sparc_sparclite_le);
3373   else
3374     return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
3375 }
3376
3377 /* The final processing done just before writing out the object file.
3378    We need to set the e_machine field appropriately.  */
3379
3380 static void
3381 elf32_sparc_final_write_processing (abfd, linker)
3382      bfd *abfd;
3383      bfd_boolean linker ATTRIBUTE_UNUSED;
3384 {
3385   switch (bfd_get_mach (abfd))
3386     {
3387     case bfd_mach_sparc :
3388       break; /* nothing to do */
3389     case bfd_mach_sparc_v8plus :
3390       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
3391       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
3392       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
3393       break;
3394     case bfd_mach_sparc_v8plusa :
3395       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
3396       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
3397       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
3398       break;
3399     case bfd_mach_sparc_v8plusb :
3400       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
3401       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
3402       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
3403                                        | EF_SPARC_SUN_US3;
3404       break;
3405     case bfd_mach_sparc_sparclite_le :
3406       elf_elfheader (abfd)->e_machine = EM_SPARC;
3407       elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
3408       break;
3409     default :
3410       abort ();
3411       break;
3412     }
3413 }
3414
3415 static enum elf_reloc_type_class
3416 elf32_sparc_reloc_type_class (rela)
3417      const Elf_Internal_Rela *rela;
3418 {
3419   switch ((int) ELF32_R_TYPE (rela->r_info))
3420     {
3421     case R_SPARC_RELATIVE:
3422       return reloc_class_relative;
3423     case R_SPARC_JMP_SLOT:
3424       return reloc_class_plt;
3425     case R_SPARC_COPY:
3426       return reloc_class_copy;
3427     default:
3428       return reloc_class_normal;
3429     }
3430 }
3431
3432 /* Return address for Ith PLT stub in section PLT, for relocation REL
3433    or (bfd_vma) -1 if it should not be included.  */
3434
3435 static bfd_vma
3436 elf32_sparc_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
3437                          const asection *plt ATTRIBUTE_UNUSED,
3438                          const arelent *rel)
3439 {
3440   return rel->address;
3441 }
3442 \f
3443 #define TARGET_BIG_SYM  bfd_elf32_sparc_vec
3444 #define TARGET_BIG_NAME "elf32-sparc"
3445 #define ELF_ARCH        bfd_arch_sparc
3446 #define ELF_MACHINE_CODE EM_SPARC
3447 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
3448 #define ELF_MAXPAGESIZE 0x10000
3449
3450 #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
3451 #define bfd_elf32_bfd_link_hash_table_create \
3452                                         elf32_sparc_link_hash_table_create
3453 #define bfd_elf32_bfd_relax_section     elf32_sparc_relax_section
3454 #define bfd_elf32_new_section_hook      elf32_sparc_new_section_hook
3455 #define elf_info_to_howto               elf32_sparc_info_to_howto
3456 #define elf_backend_copy_indirect_symbol \
3457                                         elf32_sparc_copy_indirect_symbol
3458 #define elf_backend_create_dynamic_sections \
3459                                         elf32_sparc_create_dynamic_sections
3460 #define elf_backend_check_relocs        elf32_sparc_check_relocs
3461 #define elf_backend_adjust_dynamic_symbol \
3462                                         elf32_sparc_adjust_dynamic_symbol
3463 #define elf_backend_size_dynamic_sections \
3464                                         elf32_sparc_size_dynamic_sections
3465 #define elf_backend_relocate_section    elf32_sparc_relocate_section
3466 #define elf_backend_finish_dynamic_symbol \
3467                                         elf32_sparc_finish_dynamic_symbol
3468 #define elf_backend_finish_dynamic_sections \
3469                                         elf32_sparc_finish_dynamic_sections
3470 #define bfd_elf32_bfd_merge_private_bfd_data \
3471                                         elf32_sparc_merge_private_bfd_data
3472 #define bfd_elf32_mkobject              elf32_sparc_mkobject
3473 #define elf_backend_object_p            elf32_sparc_object_p
3474 #define elf_backend_final_write_processing \
3475                                         elf32_sparc_final_write_processing
3476 #define elf_backend_gc_mark_hook        elf32_sparc_gc_mark_hook
3477 #define elf_backend_gc_sweep_hook       elf32_sparc_gc_sweep_hook
3478 #define elf_backend_grok_psinfo         elf32_sparc_grok_psinfo
3479 #define elf_backend_reloc_type_class    elf32_sparc_reloc_type_class
3480 #define elf_backend_plt_sym_val         elf32_sparc_plt_sym_val
3481
3482 #define elf_backend_can_gc_sections 1
3483 #define elf_backend_can_refcount 1
3484 #define elf_backend_want_got_plt 0
3485 #define elf_backend_plt_readonly 0
3486 #define elf_backend_want_plt_sym 1
3487 #define elf_backend_got_header_size 4
3488 #define elf_backend_rela_normal 1
3489
3490 #include "elf32-target.h"