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