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