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