]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-sparc.c
Correctly handle Vtentry relocs.
[thirdparty/binutils-gdb.git] / bfd / elf64-sparc.c
CommitLineData
252b5132
RH
1/* SPARC-specific support for 64-bit ELF
2 Copyright (C) 1993, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20#include "bfd.h"
21#include "sysdep.h"
22#include "libbfd.h"
23#include "elf-bfd.h"
24
25/* This is defined if one wants to build upward compatible binaries
26 with the original sparc64-elf toolchain. The support is kept in for
27 now but is turned off by default. dje 970930 */
28/*#define SPARC64_OLD_RELOCS*/
29
30#include "elf/sparc.h"
31
32/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
33#define MINUS_ONE (~ (bfd_vma) 0)
34
35static reloc_howto_type *sparc64_elf_reloc_type_lookup
36 PARAMS ((bfd *, bfd_reloc_code_real_type));
37static void sparc64_elf_info_to_howto
38 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
39
40static void sparc64_elf_build_plt
41 PARAMS((bfd *, unsigned char *, int));
42static bfd_vma sparc64_elf_plt_entry_offset
43 PARAMS((int));
44static bfd_vma sparc64_elf_plt_ptr_offset
45 PARAMS((int, int));
46
47static boolean sparc64_elf_check_relocs
48 PARAMS((bfd *, struct bfd_link_info *, asection *sec,
49 const Elf_Internal_Rela *));
50static boolean sparc64_elf_adjust_dynamic_symbol
51 PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
52static boolean sparc64_elf_size_dynamic_sections
53 PARAMS((bfd *, struct bfd_link_info *));
252b5132
RH
54
55static boolean sparc64_elf_merge_private_bfd_data
56 PARAMS ((bfd *, bfd *));
57
58static boolean sparc64_elf_relocate_section
59 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
60 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
61static boolean sparc64_elf_object_p PARAMS ((bfd *));
f65054f7
RH
62static long sparc64_elf_get_reloc_upper_bound PARAMS ((bfd *, asection *));
63static long sparc64_elf_get_dynamic_reloc_upper_bound PARAMS ((bfd *));
64static boolean sparc64_elf_slurp_one_reloc_table
65 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, asymbol **, boolean));
66static boolean sparc64_elf_slurp_reloc_table
67 PARAMS ((bfd *, asection *, asymbol **, boolean));
68static long sparc64_elf_canonicalize_dynamic_reloc
69 PARAMS ((bfd *, arelent **, asymbol **));
70static void sparc64_elf_write_relocs PARAMS ((bfd *, asection *, PTR));
252b5132
RH
71\f
72/* The relocation "howto" table. */
73
74static bfd_reloc_status_type sparc_elf_notsup_reloc
75 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
76static bfd_reloc_status_type sparc_elf_wdisp16_reloc
77 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
78static bfd_reloc_status_type sparc_elf_hix22_reloc
79 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
80static bfd_reloc_status_type sparc_elf_lox10_reloc
81 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
82
83static reloc_howto_type sparc64_elf_howto_table[] =
84{
85 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", false,0,0x00000000,true),
86 HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", false,0,0x000000ff,true),
87 HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", false,0,0x0000ffff,true),
88 HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", false,0,0xffffffff,true),
89 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", false,0,0x000000ff,true),
90 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", false,0,0x0000ffff,true),
91 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", false,0,0x00ffffff,true),
92 HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", false,0,0x3fffffff,true),
93 HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", false,0,0x003fffff,true),
94 HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", false,0,0x003fffff,true),
95 HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", false,0,0x003fffff,true),
96 HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", false,0,0x00001fff,true),
97 HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", false,0,0x000003ff,true),
98 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", false,0,0x000003ff,true),
99 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", false,0,0x00001fff,true),
100 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", false,0,0x003fffff,true),
101 HOWTO(R_SPARC_PC10, 0,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", false,0,0x000003ff,true),
102 HOWTO(R_SPARC_PC22, 10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", false,0,0x003fffff,true),
103 HOWTO(R_SPARC_WPLT30, 2,2,30,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", false,0,0x3fffffff,true),
104 HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", false,0,0x00000000,true),
105 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),
106 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),
107 HOWTO(R_SPARC_RELATIVE, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",false,0,0x00000000,true),
108 HOWTO(R_SPARC_UA32, 0,0,00,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_UA32", false,0,0x00000000,true),
109#ifndef SPARC64_OLD_RELOCS
110 /* These aren't implemented yet. */
111 HOWTO(R_SPARC_PLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PLT32", false,0,0x00000000,true),
112 HOWTO(R_SPARC_HIPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_HIPLT22", false,0,0x00000000,true),
113 HOWTO(R_SPARC_LOPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_LOPLT10", false,0,0x00000000,true),
114 HOWTO(R_SPARC_PCPLT32, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT32", false,0,0x00000000,true),
115 HOWTO(R_SPARC_PCPLT22, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT22", false,0,0x00000000,true),
116 HOWTO(R_SPARC_PCPLT10, 0,0,00,false,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT10", false,0,0x00000000,true),
117#endif
118 HOWTO(R_SPARC_10, 0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", false,0,0x000003ff,true),
119 HOWTO(R_SPARC_11, 0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", false,0,0x000007ff,true),
120 HOWTO(R_SPARC_64, 0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_64", false,0,MINUS_ONE, true),
121 HOWTO(R_SPARC_OLO10, 0,2,13,false,0,complain_overflow_signed, sparc_elf_notsup_reloc, "R_SPARC_OLO10", false,0,0x00001fff,true),
122 HOWTO(R_SPARC_HH22, 42,2,22,false,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_HH22", false,0,0x003fffff,true),
123 HOWTO(R_SPARC_HM10, 32,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HM10", false,0,0x000003ff,true),
124 HOWTO(R_SPARC_LM22, 10,2,22,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LM22", false,0,0x003fffff,true),
125 HOWTO(R_SPARC_PC_HH22, 42,2,22,true, 0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_PC_HH22", false,0,0x003fffff,true),
126 HOWTO(R_SPARC_PC_HM10, 32,2,10,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC_HM10", false,0,0x000003ff,true),
127 HOWTO(R_SPARC_PC_LM22, 10,2,22,true, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC_LM22", false,0,0x003fffff,true),
128 HOWTO(R_SPARC_WDISP16, 2,2,16,true, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
129 HOWTO(R_SPARC_WDISP19, 2,2,19,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", false,0,0x0007ffff,true),
130 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),
131 HOWTO(R_SPARC_7, 0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", false,0,0x0000007f,true),
132 HOWTO(R_SPARC_5, 0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", false,0,0x0000001f,true),
133 HOWTO(R_SPARC_6, 0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", false,0,0x0000003f,true),
134 HOWTO(R_SPARC_DISP64, 0,4,64,true, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP64", false,0,MINUS_ONE, true),
135 HOWTO(R_SPARC_PLT64, 0,4,64,false,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_PLT64", false,0,MINUS_ONE, false),
136 HOWTO(R_SPARC_HIX22, 0,4, 0,false,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_HIX22", false,0,MINUS_ONE, false),
137 HOWTO(R_SPARC_LOX10, 0,4, 0,false,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_LOX10", false,0,MINUS_ONE, false),
138 HOWTO(R_SPARC_H44, 22,2,22,false,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_H44", false,0,0x003fffff,false),
139 HOWTO(R_SPARC_M44, 12,2,10,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_M44", false,0,0x000003ff,false),
140 HOWTO(R_SPARC_L44, 0,2,13,false,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_L44", false,0,0x00000fff,false),
141 HOWTO(R_SPARC_REGISTER, 0,4, 0,false,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_REGISTER",false,0,MINUS_ONE, false),
142 HOWTO(R_SPARC_UA64, 0,4,64,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA64", false,0,MINUS_ONE, true),
143 HOWTO(R_SPARC_UA16, 0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", false,0,0x0000ffff,true)
144};
145
146struct elf_reloc_map {
147 bfd_reloc_code_real_type bfd_reloc_val;
148 unsigned char elf_reloc_val;
149};
150
151static CONST struct elf_reloc_map sparc_reloc_map[] =
152{
153 { BFD_RELOC_NONE, R_SPARC_NONE, },
154 { BFD_RELOC_16, R_SPARC_16, },
155 { BFD_RELOC_8, R_SPARC_8 },
156 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
157 { BFD_RELOC_CTOR, R_SPARC_64 },
158 { BFD_RELOC_32, R_SPARC_32 },
159 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
160 { BFD_RELOC_HI22, R_SPARC_HI22 },
161 { BFD_RELOC_LO10, R_SPARC_LO10, },
162 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
163 { BFD_RELOC_SPARC22, R_SPARC_22 },
164 { BFD_RELOC_SPARC13, R_SPARC_13 },
165 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
166 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
167 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
168 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
169 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
170 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
171 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
172 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
173 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
174 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
175 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
176 /* ??? Doesn't dwarf use this? */
177/*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
178 {BFD_RELOC_SPARC_10, R_SPARC_10},
179 {BFD_RELOC_SPARC_11, R_SPARC_11},
180 {BFD_RELOC_SPARC_64, R_SPARC_64},
181 {BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10},
182 {BFD_RELOC_SPARC_HH22, R_SPARC_HH22},
183 {BFD_RELOC_SPARC_HM10, R_SPARC_HM10},
184 {BFD_RELOC_SPARC_LM22, R_SPARC_LM22},
185 {BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22},
186 {BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10},
187 {BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22},
188 {BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16},
189 {BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19},
190 {BFD_RELOC_SPARC_7, R_SPARC_7},
191 {BFD_RELOC_SPARC_5, R_SPARC_5},
192 {BFD_RELOC_SPARC_6, R_SPARC_6},
193 {BFD_RELOC_SPARC_DISP64, R_SPARC_DISP64},
194 {BFD_RELOC_SPARC_PLT64, R_SPARC_PLT64},
195 {BFD_RELOC_SPARC_HIX22, R_SPARC_HIX22},
196 {BFD_RELOC_SPARC_LOX10, R_SPARC_LOX10},
197 {BFD_RELOC_SPARC_H44, R_SPARC_H44},
198 {BFD_RELOC_SPARC_M44, R_SPARC_M44},
199 {BFD_RELOC_SPARC_L44, R_SPARC_L44},
200 {BFD_RELOC_SPARC_REGISTER, R_SPARC_REGISTER}
201};
202
203static reloc_howto_type *
204sparc64_elf_reloc_type_lookup (abfd, code)
205 bfd *abfd;
206 bfd_reloc_code_real_type code;
207{
208 unsigned int i;
209 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
210 {
211 if (sparc_reloc_map[i].bfd_reloc_val == code)
212 return &sparc64_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
213 }
214 return 0;
215}
216
217static void
218sparc64_elf_info_to_howto (abfd, cache_ptr, dst)
219 bfd *abfd;
220 arelent *cache_ptr;
221 Elf64_Internal_Rela *dst;
222{
f65054f7
RH
223 BFD_ASSERT (ELF64_R_TYPE_ID (dst->r_info) < (unsigned int) R_SPARC_max_std);
224 cache_ptr->howto = &sparc64_elf_howto_table[ELF64_R_TYPE_ID (dst->r_info)];
225}
226\f
227/* Due to the way how we handle R_SPARC_OLO10, each entry in a SHT_RELA
228 section can represent up to two relocs, we must tell the user to allocate
229 more space. */
230
231static long
232sparc64_elf_get_reloc_upper_bound (abfd, sec)
233 bfd *abfd;
234 asection *sec;
235{
236 return (sec->reloc_count * 2 + 1) * sizeof (arelent *);
237}
238
239static long
240sparc64_elf_get_dynamic_reloc_upper_bound (abfd)
241 bfd *abfd;
242{
243 return _bfd_elf_get_dynamic_reloc_upper_bound (abfd) * 2;
244}
245
246/* Read relocations for ASECT from REL_HDR. There are RELOC_COUNT of
247 them. We cannot use generic elf routines for this, because R_SPARC_OLO10
248 has secondary addend in ELF64_R_TYPE_DATA. We handle it as two relocations
249 for the same location, R_SPARC_LO10 and R_SPARC_13. */
250
251static boolean
252sparc64_elf_slurp_one_reloc_table (abfd, asect, rel_hdr, symbols, dynamic)
253 bfd *abfd;
254 asection *asect;
255 Elf_Internal_Shdr *rel_hdr;
256 asymbol **symbols;
257 boolean dynamic;
258{
259 struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
260 PTR allocated = NULL;
261 bfd_byte *native_relocs;
262 arelent *relent;
263 unsigned int i;
264 int entsize;
265 bfd_size_type count;
266 arelent *relents;
267
268 allocated = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size);
269 if (allocated == NULL)
270 goto error_return;
271
272 if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
273 || (bfd_read (allocated, 1, rel_hdr->sh_size, abfd)
274 != rel_hdr->sh_size))
275 goto error_return;
276
277 native_relocs = (bfd_byte *) allocated;
278
279 relents = asect->relocation + asect->reloc_count;
280
281 entsize = rel_hdr->sh_entsize;
282 BFD_ASSERT (entsize == sizeof (Elf64_External_Rela));
283
284 count = rel_hdr->sh_size / entsize;
285
286 for (i = 0, relent = relents; i < count;
287 i++, relent++, native_relocs += entsize)
288 {
289 Elf_Internal_Rela rela;
290
291 bfd_elf64_swap_reloca_in (abfd, (Elf64_External_Rela *) native_relocs, &rela);
292
293 /* The address of an ELF reloc is section relative for an object
294 file, and absolute for an executable file or shared library.
295 The address of a normal BFD reloc is always section relative,
296 and the address of a dynamic reloc is absolute.. */
297 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
298 relent->address = rela.r_offset;
299 else
300 relent->address = rela.r_offset - asect->vma;
301
302 if (ELF64_R_SYM (rela.r_info) == 0)
303 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
304 else
305 {
306 asymbol **ps, *s;
307
308 ps = symbols + ELF64_R_SYM (rela.r_info) - 1;
309 s = *ps;
310
311 /* Canonicalize ELF section symbols. FIXME: Why? */
312 if ((s->flags & BSF_SECTION_SYM) == 0)
313 relent->sym_ptr_ptr = ps;
314 else
315 relent->sym_ptr_ptr = s->section->symbol_ptr_ptr;
316 }
317
318 relent->addend = rela.r_addend;
319
320 BFD_ASSERT (ELF64_R_TYPE_ID (rela.r_info) < (unsigned int) R_SPARC_max_std);
321 if (ELF64_R_TYPE_ID (rela.r_info) == R_SPARC_OLO10)
322 {
323 relent->howto = &sparc64_elf_howto_table[R_SPARC_LO10];
324 relent[1].address = relent->address;
325 relent++;
326 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
327 relent->addend = ELF64_R_TYPE_DATA (rela.r_info);
328 relent->howto = &sparc64_elf_howto_table[R_SPARC_13];
329 }
330 else
331 relent->howto = &sparc64_elf_howto_table[ELF64_R_TYPE_ID (rela.r_info)];
332 }
333
334 asect->reloc_count += relent - relents;
335
336 if (allocated != NULL)
337 free (allocated);
338
339 return true;
340
341 error_return:
342 if (allocated != NULL)
343 free (allocated);
344 return false;
345}
346
347/* Read in and swap the external relocs. */
348
349static boolean
350sparc64_elf_slurp_reloc_table (abfd, asect, symbols, dynamic)
351 bfd *abfd;
352 asection *asect;
353 asymbol **symbols;
354 boolean dynamic;
355{
356 struct bfd_elf_section_data * const d = elf_section_data (asect);
357 Elf_Internal_Shdr *rel_hdr;
358 Elf_Internal_Shdr *rel_hdr2;
359
360 if (asect->relocation != NULL)
361 return true;
362
363 if (! dynamic)
364 {
365 if ((asect->flags & SEC_RELOC) == 0
366 || asect->reloc_count == 0)
367 return true;
368
369 rel_hdr = &d->rel_hdr;
370 rel_hdr2 = d->rel_hdr2;
371
372 BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset
373 || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
374 }
375 else
376 {
377 /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
378 case because relocations against this section may use the
379 dynamic symbol table, and in that case bfd_section_from_shdr
380 in elf.c does not update the RELOC_COUNT. */
381 if (asect->_raw_size == 0)
382 return true;
383
384 rel_hdr = &d->this_hdr;
385 asect->reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize;
386 rel_hdr2 = NULL;
387 }
388
389 asect->relocation = ((arelent *)
390 bfd_alloc (abfd,
391 asect->reloc_count * 2 * sizeof (arelent)));
392 if (asect->relocation == NULL)
393 return false;
394
395 /* The sparc64_elf_slurp_one_reloc_table routine increments reloc_count. */
396 asect->reloc_count = 0;
397
398 if (!sparc64_elf_slurp_one_reloc_table (abfd, asect, rel_hdr, symbols,
399 dynamic))
400 return false;
401
402 if (rel_hdr2
403 && !sparc64_elf_slurp_one_reloc_table (abfd, asect, rel_hdr2, symbols,
404 dynamic))
405 return false;
406
407 return true;
408}
409
410/* Canonicalize the dynamic relocation entries. Note that we return
411 the dynamic relocations as a single block, although they are
412 actually associated with particular sections; the interface, which
413 was designed for SunOS style shared libraries, expects that there
414 is only one set of dynamic relocs. Any section that was actually
415 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
416 the dynamic symbol table, is considered to be a dynamic reloc
417 section. */
418
419static long
420sparc64_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
421 bfd *abfd;
422 arelent **storage;
423 asymbol **syms;
424{
425 asection *s;
426 long ret;
427
428 if (elf_dynsymtab (abfd) == 0)
429 {
430 bfd_set_error (bfd_error_invalid_operation);
431 return -1;
432 }
433
434 ret = 0;
435 for (s = abfd->sections; s != NULL; s = s->next)
436 {
437 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
438 && (elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
439 {
440 arelent *p;
441 long count, i;
442
443 if (! sparc64_elf_slurp_reloc_table (abfd, s, syms, true))
444 return -1;
445 count = s->reloc_count;
446 p = s->relocation;
447 for (i = 0; i < count; i++)
448 *storage++ = p++;
449 ret += count;
450 }
451 }
452
453 *storage = NULL;
454
455 return ret;
456}
457
458/* Write out the relocs. */
459
460static void
461sparc64_elf_write_relocs (abfd, sec, data)
462 bfd *abfd;
463 asection *sec;
464 PTR data;
465{
466 boolean *failedp = (boolean *) data;
467 Elf_Internal_Shdr *rela_hdr;
468 Elf64_External_Rela *outbound_relocas;
469 unsigned int idx, count;
470 asymbol *last_sym = 0;
471 int last_sym_idx = 0;
472
473 /* If we have already failed, don't do anything. */
474 if (*failedp)
475 return;
476
477 if ((sec->flags & SEC_RELOC) == 0)
478 return;
479
480 /* The linker backend writes the relocs out itself, and sets the
481 reloc_count field to zero to inhibit writing them here. Also,
482 sometimes the SEC_RELOC flag gets set even when there aren't any
483 relocs. */
484 if (sec->reloc_count == 0)
485 return;
486
487 /* We can combine two relocs that refer to the same address
488 into R_SPARC_OLO10 if first one is R_SPARC_LO10 and the
489 latter is R_SPARC_13 with no associated symbol. */
490 count = 0;
491 for (idx = 0; idx < sec->reloc_count; idx++)
492 {
493 bfd_vma addr;
494 unsigned int i;
495
496 ++count;
497
498 addr = sec->orelocation[idx]->address;
499 if (sec->orelocation[idx]->howto->type == R_SPARC_LO10
500 && idx < sec->reloc_count - 1)
501 {
502 arelent *r = sec->orelocation[idx + 1];
503
504 if (r->howto->type == R_SPARC_13
505 && r->address == addr
506 && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
507 && (*r->sym_ptr_ptr)->value == 0)
508 ++idx;
509 }
510 }
511
512 rela_hdr = &elf_section_data (sec)->rel_hdr;
513
514 rela_hdr->sh_size = rela_hdr->sh_entsize * count;
515 rela_hdr->contents = (PTR) bfd_alloc (abfd, rela_hdr->sh_size);
516 if (rela_hdr->contents == NULL)
517 {
518 *failedp = true;
519 return;
520 }
521
522 /* Figure out whether the relocations are RELA or REL relocations. */
523 if (rela_hdr->sh_type != SHT_RELA)
524 abort ();
525
526 /* orelocation has the data, reloc_count has the count... */
527 outbound_relocas = (Elf64_External_Rela *) rela_hdr->contents;
528
529 for (idx = 0; idx < sec->reloc_count; idx++)
530 {
531 Elf_Internal_Rela dst_rela;
532 Elf64_External_Rela *src_rela;
533 arelent *ptr;
534 asymbol *sym;
535 int n;
536
537 ptr = sec->orelocation[idx];
538 src_rela = outbound_relocas + idx;
539
540 /* The address of an ELF reloc is section relative for an object
541 file, and absolute for an executable file or shared library.
542 The address of a BFD reloc is always section relative. */
543 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
544 dst_rela.r_offset = ptr->address;
545 else
546 dst_rela.r_offset = ptr->address + sec->vma;
547
548 sym = *ptr->sym_ptr_ptr;
549 if (sym == last_sym)
550 n = last_sym_idx;
551 else if (bfd_is_abs_section (sym->section) && sym->value == 0)
552 n = STN_UNDEF;
553 else
554 {
555 last_sym = sym;
556 n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
557 if (n < 0)
558 {
559 *failedp = true;
560 return;
561 }
562 last_sym_idx = n;
563 }
564
565 if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
566 && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
567 && ! _bfd_elf_validate_reloc (abfd, ptr))
568 {
569 *failedp = true;
570 return;
571 }
572
573 if (ptr->howto->type == R_SPARC_LO10
574 && idx < sec->reloc_count - 1)
575 {
576 arelent *r = sec->orelocation[idx + 1];
577
578 if (r->howto->type == R_SPARC_13
579 && r->address == ptr->address
580 && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
581 && (*r->sym_ptr_ptr)->value == 0)
582 {
583 idx++;
584 dst_rela.r_info
585 = ELF64_R_INFO (n, ELF64_R_TYPE_INFO (r->addend,
586 R_SPARC_OLO10));
587 }
588 else
589 dst_rela.r_info = ELF64_R_INFO (n, R_SPARC_LO10);
590 }
591 else
592 dst_rela.r_info = ELF64_R_INFO (n, ptr->howto->type);
593
594 dst_rela.r_addend = ptr->addend;
595 bfd_elf64_swap_reloca_out (abfd, &dst_rela, src_rela);
596 }
252b5132
RH
597}
598\f
599/* Utility for performing the standard initial work of an instruction
600 relocation.
601 *PRELOCATION will contain the relocated item.
602 *PINSN will contain the instruction from the input stream.
603 If the result is `bfd_reloc_other' the caller can continue with
604 performing the relocation. Otherwise it must stop and return the
605 value to its caller. */
606
607static bfd_reloc_status_type
608init_insn_reloc (abfd,
609 reloc_entry,
610 symbol,
611 data,
612 input_section,
613 output_bfd,
614 prelocation,
615 pinsn)
616 bfd *abfd;
617 arelent *reloc_entry;
618 asymbol *symbol;
619 PTR data;
620 asection *input_section;
621 bfd *output_bfd;
622 bfd_vma *prelocation;
623 bfd_vma *pinsn;
624{
625 bfd_vma relocation;
626 reloc_howto_type *howto = reloc_entry->howto;
627
628 if (output_bfd != (bfd *) NULL
629 && (symbol->flags & BSF_SECTION_SYM) == 0
630 && (! howto->partial_inplace
631 || reloc_entry->addend == 0))
632 {
633 reloc_entry->address += input_section->output_offset;
634 return bfd_reloc_ok;
635 }
636
637 /* This works because partial_inplace == false. */
638 if (output_bfd != NULL)
639 return bfd_reloc_continue;
640
641 if (reloc_entry->address > input_section->_cooked_size)
642 return bfd_reloc_outofrange;
643
644 relocation = (symbol->value
645 + symbol->section->output_section->vma
646 + symbol->section->output_offset);
647 relocation += reloc_entry->addend;
648 if (howto->pc_relative)
649 {
650 relocation -= (input_section->output_section->vma
651 + input_section->output_offset);
652 relocation -= reloc_entry->address;
653 }
654
655 *prelocation = relocation;
656 *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
657 return bfd_reloc_other;
658}
659
660/* For unsupported relocs. */
661
662static bfd_reloc_status_type
663sparc_elf_notsup_reloc (abfd,
664 reloc_entry,
665 symbol,
666 data,
667 input_section,
668 output_bfd,
669 error_message)
670 bfd *abfd;
671 arelent *reloc_entry;
672 asymbol *symbol;
673 PTR data;
674 asection *input_section;
675 bfd *output_bfd;
676 char **error_message;
677{
678 return bfd_reloc_notsupported;
679}
680
681/* Handle the WDISP16 reloc. */
682
683static bfd_reloc_status_type
684sparc_elf_wdisp16_reloc (abfd, reloc_entry, symbol, data, input_section,
685 output_bfd, error_message)
686 bfd *abfd;
687 arelent *reloc_entry;
688 asymbol *symbol;
689 PTR data;
690 asection *input_section;
691 bfd *output_bfd;
692 char **error_message;
693{
694 bfd_vma relocation;
695 bfd_vma insn;
696 bfd_reloc_status_type status;
697
698 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
699 input_section, output_bfd, &relocation, &insn);
700 if (status != bfd_reloc_other)
701 return status;
702
703 insn = (insn & ~0x303fff) | ((((relocation >> 2) & 0xc000) << 6)
704 | ((relocation >> 2) & 0x3fff));
705 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
706
707 if ((bfd_signed_vma) relocation < - 0x40000
708 || (bfd_signed_vma) relocation > 0x3ffff)
709 return bfd_reloc_overflow;
710 else
711 return bfd_reloc_ok;
712}
713
714/* Handle the HIX22 reloc. */
715
716static bfd_reloc_status_type
717sparc_elf_hix22_reloc (abfd,
718 reloc_entry,
719 symbol,
720 data,
721 input_section,
722 output_bfd,
723 error_message)
724 bfd *abfd;
725 arelent *reloc_entry;
726 asymbol *symbol;
727 PTR data;
728 asection *input_section;
729 bfd *output_bfd;
730 char **error_message;
731{
732 bfd_vma relocation;
733 bfd_vma insn;
734 bfd_reloc_status_type status;
735
736 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
737 input_section, output_bfd, &relocation, &insn);
738 if (status != bfd_reloc_other)
739 return status;
740
741 relocation ^= MINUS_ONE;
742 insn = (insn & ~0x3fffff) | ((relocation >> 10) & 0x3fffff);
743 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
744
745 if ((relocation & ~ (bfd_vma) 0xffffffff) != 0)
746 return bfd_reloc_overflow;
747 else
748 return bfd_reloc_ok;
749}
750
751/* Handle the LOX10 reloc. */
752
753static bfd_reloc_status_type
754sparc_elf_lox10_reloc (abfd,
755 reloc_entry,
756 symbol,
757 data,
758 input_section,
759 output_bfd,
760 error_message)
761 bfd *abfd;
762 arelent *reloc_entry;
763 asymbol *symbol;
764 PTR data;
765 asection *input_section;
766 bfd *output_bfd;
767 char **error_message;
768{
769 bfd_vma relocation;
770 bfd_vma insn;
771 bfd_reloc_status_type status;
772
773 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
774 input_section, output_bfd, &relocation, &insn);
775 if (status != bfd_reloc_other)
776 return status;
777
778 insn = (insn & ~0x1fff) | 0x1c00 | (relocation & 0x3ff);
779 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
780
781 return bfd_reloc_ok;
782}
783\f
784/* PLT/GOT stuff */
785
786/* Both the headers and the entries are icache aligned. */
787#define PLT_ENTRY_SIZE 32
788#define PLT_HEADER_SIZE (4 * PLT_ENTRY_SIZE)
789#define LARGE_PLT_THRESHOLD 32768
790#define GOT_RESERVED_ENTRIES 1
791
792#define ELF_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1"
793
794
795/* Fill in the .plt section. */
796
797static void
798sparc64_elf_build_plt (output_bfd, contents, nentries)
799 bfd *output_bfd;
800 unsigned char *contents;
801 int nentries;
802{
803 const unsigned int nop = 0x01000000;
804 int i, j;
805
806 /* The first four entries are reserved, and are initially undefined.
807 We fill them with `illtrap 0' to force ld.so to do something. */
808
809 for (i = 0; i < PLT_HEADER_SIZE/4; ++i)
810 bfd_put_32 (output_bfd, 0, contents+i*4);
811
812 /* The first 32768 entries are close enough to plt1 to get there via
813 a straight branch. */
814
815 for (i = 4; i < LARGE_PLT_THRESHOLD && i < nentries; ++i)
816 {
817 unsigned char *entry = contents + i * PLT_ENTRY_SIZE;
818 unsigned int sethi, ba;
819
820 /* sethi (. - plt0), %g1 */
821 sethi = 0x03000000 | (i * PLT_ENTRY_SIZE);
822
823 /* ba,a,pt %icc, plt1 */
824 ba = 0x30480000 | (((contents+PLT_ENTRY_SIZE) - (entry+4)) / 4 & 0x7ffff);
825
826 bfd_put_32 (output_bfd, sethi, entry);
827 bfd_put_32 (output_bfd, ba, entry+4);
828 bfd_put_32 (output_bfd, nop, entry+8);
829 bfd_put_32 (output_bfd, nop, entry+12);
830 bfd_put_32 (output_bfd, nop, entry+16);
831 bfd_put_32 (output_bfd, nop, entry+20);
832 bfd_put_32 (output_bfd, nop, entry+24);
833 bfd_put_32 (output_bfd, nop, entry+28);
834 }
835
836 /* Now the tricky bit. Entries 32768 and higher are grouped in blocks of
837 160: 160 entries and 160 pointers. This is to separate code from data,
838 which is much friendlier on the cache. */
839
840 for (; i < nentries; i += 160)
841 {
842 int block = (i + 160 <= nentries ? 160 : nentries - i);
843 for (j = 0; j < block; ++j)
844 {
845 unsigned char *entry, *ptr;
846 unsigned int ldx;
847
848 entry = contents + i*PLT_ENTRY_SIZE + j*4*6;
849 ptr = contents + i*PLT_ENTRY_SIZE + block*4*6 + j*8;
850
851 /* ldx [%o7 + ptr - entry+4], %g1 */
852 ldx = 0xc25be000 | ((ptr - entry+4) & 0x1fff);
853
854 bfd_put_32 (output_bfd, 0x8a10000f, entry); /* mov %o7,%g5 */
855 bfd_put_32 (output_bfd, 0x40000002, entry+4); /* call .+8 */
856 bfd_put_32 (output_bfd, nop, entry+8); /* nop */
857 bfd_put_32 (output_bfd, ldx, entry+12); /* ldx [%o7+P],%g1 */
858 bfd_put_32 (output_bfd, 0x83c3c001, entry+16); /* jmpl %o7+%g1,%g1 */
859 bfd_put_32 (output_bfd, 0x9e100005, entry+20); /* mov %g5,%o7 */
860
861 bfd_put_64 (output_bfd, contents - entry+4, ptr);
862 }
863 }
864}
865
866/* Return the offset of a particular plt entry within the .plt section. */
867
868static bfd_vma
869sparc64_elf_plt_entry_offset (index)
870 int index;
871{
872 int block, ofs;
873
874 if (index < LARGE_PLT_THRESHOLD)
875 return index * PLT_ENTRY_SIZE;
876
877 /* See above for details. */
878
879 block = (index - LARGE_PLT_THRESHOLD) / 160;
880 ofs = (index - LARGE_PLT_THRESHOLD) % 160;
881
882 return ((bfd_vma)(LARGE_PLT_THRESHOLD + block*160) * PLT_ENTRY_SIZE
883 + ofs * 6*4);
884}
885
886static bfd_vma
887sparc64_elf_plt_ptr_offset (index, max)
888 int index, max;
889{
890 int block, ofs, last;
891
892 BFD_ASSERT(index >= LARGE_PLT_THRESHOLD);
893
894 /* See above for details. */
895
896 block = (index - LARGE_PLT_THRESHOLD) / 160;
897 ofs = (index - LARGE_PLT_THRESHOLD) % 160;
898 last = (max - LARGE_PLT_THRESHOLD) % 160;
899
900 return ((LARGE_PLT_THRESHOLD + block*160) * PLT_ENTRY_SIZE
901 + last * 6*4
902 + ofs * 8);
903}
904
905
906\f
907/* Look through the relocs for a section during the first phase, and
908 allocate space in the global offset table or procedure linkage
909 table. */
910
911static boolean
912sparc64_elf_check_relocs (abfd, info, sec, relocs)
913 bfd *abfd;
914 struct bfd_link_info *info;
915 asection *sec;
916 const Elf_Internal_Rela *relocs;
917{
918 bfd *dynobj;
919 Elf_Internal_Shdr *symtab_hdr;
920 struct elf_link_hash_entry **sym_hashes;
921 bfd_vma *local_got_offsets;
922 const Elf_Internal_Rela *rel;
923 const Elf_Internal_Rela *rel_end;
924 asection *sgot;
925 asection *srelgot;
926 asection *sreloc;
927
928 if (info->relocateable || !(sec->flags & SEC_ALLOC))
929 return true;
930
931 dynobj = elf_hash_table (info)->dynobj;
932 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
933 sym_hashes = elf_sym_hashes (abfd);
934 local_got_offsets = elf_local_got_offsets (abfd);
935
936 sgot = NULL;
937 srelgot = NULL;
938 sreloc = NULL;
939
940 rel_end = relocs + sec->reloc_count;
941 for (rel = relocs; rel < rel_end; rel++)
942 {
943 unsigned long r_symndx;
944 struct elf_link_hash_entry *h;
945
946 r_symndx = ELF64_R_SYM (rel->r_info);
947 if (r_symndx < symtab_hdr->sh_info)
948 h = NULL;
949 else
950 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
951
f65054f7 952 switch (ELF64_R_TYPE_ID (rel->r_info))
252b5132
RH
953 {
954 case R_SPARC_GOT10:
955 case R_SPARC_GOT13:
956 case R_SPARC_GOT22:
957 /* This symbol requires a global offset table entry. */
958
959 if (dynobj == NULL)
960 {
961 /* Create the .got section. */
962 elf_hash_table (info)->dynobj = dynobj = abfd;
963 if (! _bfd_elf_create_got_section (dynobj, info))
964 return false;
965 }
966
967 if (sgot == NULL)
968 {
969 sgot = bfd_get_section_by_name (dynobj, ".got");
970 BFD_ASSERT (sgot != NULL);
971 }
972
973 if (srelgot == NULL && (h != NULL || info->shared))
974 {
975 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
976 if (srelgot == NULL)
977 {
978 srelgot = bfd_make_section (dynobj, ".rela.got");
979 if (srelgot == NULL
980 || ! bfd_set_section_flags (dynobj, srelgot,
981 (SEC_ALLOC
982 | SEC_LOAD
983 | SEC_HAS_CONTENTS
984 | SEC_IN_MEMORY
985 | SEC_LINKER_CREATED
986 | SEC_READONLY))
987 || ! bfd_set_section_alignment (dynobj, srelgot, 3))
988 return false;
989 }
990 }
991
992 if (h != NULL)
993 {
994 if (h->got.offset != (bfd_vma) -1)
995 {
996 /* We have already allocated space in the .got. */
997 break;
998 }
999 h->got.offset = sgot->_raw_size;
1000
1001 /* Make sure this symbol is output as a dynamic symbol. */
1002 if (h->dynindx == -1)
1003 {
1004 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1005 return false;
1006 }
1007
1008 srelgot->_raw_size += sizeof (Elf64_External_Rela);
1009 }
1010 else
1011 {
1012 /* This is a global offset table entry for a local
1013 symbol. */
1014 if (local_got_offsets == NULL)
1015 {
1016 size_t size;
1017 register unsigned int i;
1018
1019 size = symtab_hdr->sh_info * sizeof (bfd_vma);
1020 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
1021 if (local_got_offsets == NULL)
1022 return false;
1023 elf_local_got_offsets (abfd) = local_got_offsets;
1024 for (i = 0; i < symtab_hdr->sh_info; i++)
1025 local_got_offsets[i] = (bfd_vma) -1;
1026 }
1027 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
1028 {
1029 /* We have already allocated space in the .got. */
1030 break;
1031 }
1032 local_got_offsets[r_symndx] = sgot->_raw_size;
1033
1034 if (info->shared)
1035 {
1036 /* If we are generating a shared object, we need to
1037 output a R_SPARC_RELATIVE reloc so that the
1038 dynamic linker can adjust this GOT entry. */
1039 srelgot->_raw_size += sizeof (Elf64_External_Rela);
1040 }
1041 }
1042
1043 sgot->_raw_size += 8;
1044
1045#if 0
1046 /* Doesn't work for 64-bit -fPIC, since sethi/or builds
1047 unsigned numbers. If we permit ourselves to modify
1048 code so we get sethi/xor, this could work.
1049 Question: do we consider conditionally re-enabling
1050 this for -fpic, once we know about object code models? */
1051 /* If the .got section is more than 0x1000 bytes, we add
1052 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
1053 bit relocations have a greater chance of working. */
1054 if (sgot->_raw_size >= 0x1000
1055 && elf_hash_table (info)->hgot->root.u.def.value == 0)
1056 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
1057#endif
1058
1059 break;
1060
1061 case R_SPARC_WPLT30:
1062 case R_SPARC_PLT32:
1063 case R_SPARC_HIPLT22:
1064 case R_SPARC_LOPLT10:
1065 case R_SPARC_PCPLT32:
1066 case R_SPARC_PCPLT22:
1067 case R_SPARC_PCPLT10:
1068 case R_SPARC_PLT64:
1069 /* This symbol requires a procedure linkage table entry. We
1070 actually build the entry in adjust_dynamic_symbol,
1071 because this might be a case of linking PIC code without
1072 linking in any dynamic objects, in which case we don't
1073 need to generate a procedure linkage table after all. */
1074
1075 if (h == NULL)
1076 {
1077 /* It does not make sense to have a procedure linkage
1078 table entry for a local symbol. */
1079 bfd_set_error (bfd_error_bad_value);
1080 return false;
1081 }
1082
1083 /* Make sure this symbol is output as a dynamic symbol. */
1084 if (h->dynindx == -1)
1085 {
1086 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1087 return false;
1088 }
1089
1090 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1091 break;
1092
1093 case R_SPARC_PC10:
1094 case R_SPARC_PC22:
1095 case R_SPARC_PC_HH22:
1096 case R_SPARC_PC_HM10:
1097 case R_SPARC_PC_LM22:
1098 if (h != NULL
1099 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1100 break;
1101 /* Fall through. */
1102 case R_SPARC_DISP8:
1103 case R_SPARC_DISP16:
1104 case R_SPARC_DISP32:
1105 case R_SPARC_DISP64:
1106 case R_SPARC_WDISP30:
1107 case R_SPARC_WDISP22:
1108 case R_SPARC_WDISP19:
1109 case R_SPARC_WDISP16:
1110 if (h == NULL)
1111 break;
1112 /* Fall through. */
1113 case R_SPARC_8:
1114 case R_SPARC_16:
1115 case R_SPARC_32:
1116 case R_SPARC_HI22:
1117 case R_SPARC_22:
1118 case R_SPARC_13:
1119 case R_SPARC_LO10:
1120 case R_SPARC_UA32:
1121 case R_SPARC_10:
1122 case R_SPARC_11:
1123 case R_SPARC_64:
1124 case R_SPARC_OLO10:
1125 case R_SPARC_HH22:
1126 case R_SPARC_HM10:
1127 case R_SPARC_LM22:
1128 case R_SPARC_7:
1129 case R_SPARC_5:
1130 case R_SPARC_6:
1131 case R_SPARC_HIX22:
1132 case R_SPARC_LOX10:
1133 case R_SPARC_H44:
1134 case R_SPARC_M44:
1135 case R_SPARC_L44:
1136 case R_SPARC_UA64:
1137 case R_SPARC_UA16:
1138 /* When creating a shared object, we must copy these relocs
1139 into the output file. We create a reloc section in
1140 dynobj and make room for the reloc.
1141
1142 But don't do this for debugging sections -- this shows up
1143 with DWARF2 -- first because they are not loaded, and
1144 second because DWARF sez the debug info is not to be
1145 biased by the load address. */
1146 if (info->shared && (sec->flags & SEC_ALLOC))
1147 {
1148 if (sreloc == NULL)
1149 {
1150 const char *name;
1151
1152 name = (bfd_elf_string_from_elf_section
1153 (abfd,
1154 elf_elfheader (abfd)->e_shstrndx,
1155 elf_section_data (sec)->rel_hdr.sh_name));
1156 if (name == NULL)
1157 return false;
1158
1159 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1160 && strcmp (bfd_get_section_name (abfd, sec),
1161 name + 5) == 0);
1162
1163 sreloc = bfd_get_section_by_name (dynobj, name);
1164 if (sreloc == NULL)
1165 {
1166 flagword flags;
1167
1168 sreloc = bfd_make_section (dynobj, name);
1169 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1170 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1171 if ((sec->flags & SEC_ALLOC) != 0)
1172 flags |= SEC_ALLOC | SEC_LOAD;
1173 if (sreloc == NULL
1174 || ! bfd_set_section_flags (dynobj, sreloc, flags)
1175 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
1176 return false;
1177 }
1178 }
1179
1180 sreloc->_raw_size += sizeof (Elf64_External_Rela);
1181 }
1182 break;
1183
1184 case R_SPARC_REGISTER:
1185 /* Nothing to do. */
1186 break;
1187
1188 default:
1189 (*_bfd_error_handler)(_("%s: check_relocs: unhandled reloc type %d"),
1190 bfd_get_filename(abfd),
f65054f7 1191 ELF64_R_TYPE_ID (rel->r_info));
252b5132
RH
1192 return false;
1193 }
1194 }
1195
1196 return true;
1197}
1198
1199/* Adjust a symbol defined by a dynamic object and referenced by a
1200 regular object. The current definition is in some section of the
1201 dynamic object, but we're not including those sections. We have to
1202 change the definition to something the rest of the link can
1203 understand. */
1204
1205static boolean
1206sparc64_elf_adjust_dynamic_symbol (info, h)
1207 struct bfd_link_info *info;
1208 struct elf_link_hash_entry *h;
1209{
1210 bfd *dynobj;
1211 asection *s;
1212 unsigned int power_of_two;
1213
1214 dynobj = elf_hash_table (info)->dynobj;
1215
1216 /* Make sure we know what is going on here. */
1217 BFD_ASSERT (dynobj != NULL
1218 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1219 || h->weakdef != NULL
1220 || ((h->elf_link_hash_flags
1221 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1222 && (h->elf_link_hash_flags
1223 & ELF_LINK_HASH_REF_REGULAR) != 0
1224 && (h->elf_link_hash_flags
1225 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1226
1227 /* If this is a function, put it in the procedure linkage table. We
1228 will fill in the contents of the procedure linkage table later
1229 (although we could actually do it here). The STT_NOTYPE
1230 condition is a hack specifically for the Oracle libraries
1231 delivered for Solaris; for some inexplicable reason, they define
1232 some of their functions as STT_NOTYPE when they really should be
1233 STT_FUNC. */
1234 if (h->type == STT_FUNC
1235 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
1236 || (h->type == STT_NOTYPE
1237 && (h->root.type == bfd_link_hash_defined
1238 || h->root.type == bfd_link_hash_defweak)
1239 && (h->root.u.def.section->flags & SEC_CODE) != 0))
1240 {
1241 if (! elf_hash_table (info)->dynamic_sections_created)
1242 {
1243 /* This case can occur if we saw a WPLT30 reloc in an input
1244 file, but none of the input files were dynamic objects.
1245 In such a case, we don't actually need to build a
1246 procedure linkage table, and we can just do a WDISP30
1247 reloc instead. */
1248 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
1249 return true;
1250 }
1251
1252 s = bfd_get_section_by_name (dynobj, ".plt");
1253 BFD_ASSERT (s != NULL);
1254
1255 /* The first four bit in .plt is reserved. */
1256 if (s->_raw_size == 0)
1257 s->_raw_size = PLT_HEADER_SIZE;
1258
1259 /* If this symbol is not defined in a regular file, and we are
1260 not generating a shared library, then set the symbol to this
1261 location in the .plt. This is required to make function
1262 pointers compare as equal between the normal executable and
1263 the shared library. */
1264 if (! info->shared
1265 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1266 {
1267 h->root.u.def.section = s;
1268 h->root.u.def.value = s->_raw_size;
1269 }
1270
1271 /* To simplify matters later, just store the plt index here. */
1272 h->plt.offset = s->_raw_size / PLT_ENTRY_SIZE;
1273
1274 /* Make room for this entry. */
1275 s->_raw_size += PLT_ENTRY_SIZE;
1276
1277 /* We also need to make an entry in the .rela.plt section. */
1278
1279 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1280 BFD_ASSERT (s != NULL);
1281
1282 /* The first plt entries are reserved, and the relocations must
1283 pair up exactly. */
1284 if (s->_raw_size == 0)
1285 s->_raw_size += (PLT_HEADER_SIZE/PLT_ENTRY_SIZE
1286 * sizeof (Elf64_External_Rela));
1287
1288 s->_raw_size += sizeof (Elf64_External_Rela);
1289
1290 /* The procedure linkage table size is bounded by the magnitude
1291 of the offset we can describe in the entry. */
1292 if (s->_raw_size >= (bfd_vma)1 << 32)
1293 {
1294 bfd_set_error (bfd_error_bad_value);
1295 return false;
1296 }
1297
1298 return true;
1299 }
1300
1301 /* If this is a weak symbol, and there is a real definition, the
1302 processor independent code will have arranged for us to see the
1303 real definition first, and we can just use the same value. */
1304 if (h->weakdef != NULL)
1305 {
1306 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1307 || h->weakdef->root.type == bfd_link_hash_defweak);
1308 h->root.u.def.section = h->weakdef->root.u.def.section;
1309 h->root.u.def.value = h->weakdef->root.u.def.value;
1310 return true;
1311 }
1312
1313 /* This is a reference to a symbol defined by a dynamic object which
1314 is not a function. */
1315
1316 /* If we are creating a shared library, we must presume that the
1317 only references to the symbol are via the global offset table.
1318 For such cases we need not do anything here; the relocations will
1319 be handled correctly by relocate_section. */
1320 if (info->shared)
1321 return true;
1322
1323 /* We must allocate the symbol in our .dynbss section, which will
1324 become part of the .bss section of the executable. There will be
1325 an entry for this symbol in the .dynsym section. The dynamic
1326 object will contain position independent code, so all references
1327 from the dynamic object to this symbol will go through the global
1328 offset table. The dynamic linker will use the .dynsym entry to
1329 determine the address it must put in the global offset table, so
1330 both the dynamic object and the regular object will refer to the
1331 same memory location for the variable. */
1332
1333 s = bfd_get_section_by_name (dynobj, ".dynbss");
1334 BFD_ASSERT (s != NULL);
1335
1336 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
1337 to copy the initial value out of the dynamic object and into the
1338 runtime process image. We need to remember the offset into the
1339 .rel.bss section we are going to use. */
1340 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1341 {
1342 asection *srel;
1343
1344 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1345 BFD_ASSERT (srel != NULL);
1346 srel->_raw_size += sizeof (Elf64_External_Rela);
1347 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1348 }
1349
1350 /* We need to figure out the alignment required for this symbol. I
1351 have no idea how ELF linkers handle this. 16-bytes is the size
1352 of the largest type that requires hard alignment -- long double. */
1353 power_of_two = bfd_log2 (h->size);
1354 if (power_of_two > 4)
1355 power_of_two = 4;
1356
1357 /* Apply the required alignment. */
1358 s->_raw_size = BFD_ALIGN (s->_raw_size,
1359 (bfd_size_type) (1 << power_of_two));
1360 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1361 {
1362 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1363 return false;
1364 }
1365
1366 /* Define the symbol as being at this point in the section. */
1367 h->root.u.def.section = s;
1368 h->root.u.def.value = s->_raw_size;
1369
1370 /* Increment the section size to make room for the symbol. */
1371 s->_raw_size += h->size;
1372
1373 return true;
1374}
1375
1376/* Set the sizes of the dynamic sections. */
1377
1378static boolean
1379sparc64_elf_size_dynamic_sections (output_bfd, info)
1380 bfd *output_bfd;
1381 struct bfd_link_info *info;
1382{
1383 bfd *dynobj;
1384 asection *s;
1385 boolean reltext;
1386 boolean relplt;
1387
1388 dynobj = elf_hash_table (info)->dynobj;
1389 BFD_ASSERT (dynobj != NULL);
1390
1391 if (elf_hash_table (info)->dynamic_sections_created)
1392 {
1393 /* Set the contents of the .interp section to the interpreter. */
1394 if (! info->shared)
1395 {
1396 s = bfd_get_section_by_name (dynobj, ".interp");
1397 BFD_ASSERT (s != NULL);
1398 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1399 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1400 }
1401 }
1402 else
1403 {
1404 /* We may have created entries in the .rela.got section.
1405 However, if we are not creating the dynamic sections, we will
1406 not actually use these entries. Reset the size of .rela.got,
1407 which will cause it to get stripped from the output file
1408 below. */
1409 s = bfd_get_section_by_name (dynobj, ".rela.got");
1410 if (s != NULL)
1411 s->_raw_size = 0;
1412 }
1413
1414 /* The check_relocs and adjust_dynamic_symbol entry points have
1415 determined the sizes of the various dynamic sections. Allocate
1416 memory for them. */
1417 reltext = false;
1418 relplt = false;
1419 for (s = dynobj->sections; s != NULL; s = s->next)
1420 {
1421 const char *name;
1422 boolean strip;
1423
1424 if ((s->flags & SEC_LINKER_CREATED) == 0)
1425 continue;
1426
1427 /* It's OK to base decisions on the section name, because none
1428 of the dynobj section names depend upon the input files. */
1429 name = bfd_get_section_name (dynobj, s);
1430
1431 strip = false;
1432
1433 if (strncmp (name, ".rela", 5) == 0)
1434 {
1435 if (s->_raw_size == 0)
1436 {
1437 /* If we don't need this section, strip it from the
1438 output file. This is to handle .rela.bss and
1439 .rel.plt. We must create it in
1440 create_dynamic_sections, because it must be created
1441 before the linker maps input sections to output
1442 sections. The linker does that before
1443 adjust_dynamic_symbol is called, and it is that
1444 function which decides whether anything needs to go
1445 into these sections. */
1446 strip = true;
1447 }
1448 else
1449 {
1450 const char *outname;
1451 asection *target;
1452
1453 /* If this relocation section applies to a read only
1454 section, then we probably need a DT_TEXTREL entry. */
1455 outname = bfd_get_section_name (output_bfd,
1456 s->output_section);
1457 target = bfd_get_section_by_name (output_bfd, outname + 5);
1458 if (target != NULL
1459 && (target->flags & SEC_READONLY) != 0)
1460 reltext = true;
1461
1462 if (strcmp (name, ".rela.plt") == 0)
1463 relplt = true;
1464
1465 /* We use the reloc_count field as a counter if we need
1466 to copy relocs into the output file. */
1467 s->reloc_count = 0;
1468 }
1469 }
1470 else if (strcmp (name, ".plt") != 0
1471 && strncmp (name, ".got", 4) != 0)
1472 {
1473 /* It's not one of our sections, so don't allocate space. */
1474 continue;
1475 }
1476
1477 if (strip)
1478 {
1479 _bfd_strip_section_from_output (s);
1480 continue;
1481 }
1482
1483 /* Allocate memory for the section contents. Zero the memory
1484 for the benefit of .rela.plt, which has 4 unused entries
1485 at the beginning, and we don't want garbage. */
1486 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1487 if (s->contents == NULL && s->_raw_size != 0)
1488 return false;
1489 }
1490
1491 if (elf_hash_table (info)->dynamic_sections_created)
1492 {
1493 /* Add some entries to the .dynamic section. We fill in the
1494 values later, in sparc64_elf_finish_dynamic_sections, but we
1495 must add the entries now so that we get the correct size for
1496 the .dynamic section. The DT_DEBUG entry is filled in by the
1497 dynamic linker and used by the debugger. */
1498 if (! info->shared)
1499 {
1500 if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
1501 return false;
1502 }
1503
1504 if (relplt)
1505 {
1506 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0)
1507 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1508 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
d6bcbdc1 1509 || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
252b5132
RH
1510 return false;
1511 }
1512
1513 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
1514 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
1515 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
1516 sizeof (Elf64_External_Rela)))
1517 return false;
1518
1519 if (reltext)
1520 {
1521 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
1522 return false;
1523 }
1524 }
1525
252b5132
RH
1526 return true;
1527}
252b5132
RH
1528\f
1529/* Relocate a SPARC64 ELF section. */
1530
1531static boolean
1532sparc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1533 contents, relocs, local_syms, local_sections)
1534 bfd *output_bfd;
1535 struct bfd_link_info *info;
1536 bfd *input_bfd;
1537 asection *input_section;
1538 bfd_byte *contents;
1539 Elf_Internal_Rela *relocs;
1540 Elf_Internal_Sym *local_syms;
1541 asection **local_sections;
1542{
1543 bfd *dynobj;
1544 Elf_Internal_Shdr *symtab_hdr;
1545 struct elf_link_hash_entry **sym_hashes;
1546 bfd_vma *local_got_offsets;
1547 bfd_vma got_base;
1548 asection *sgot;
1549 asection *splt;
1550 asection *sreloc;
1551 Elf_Internal_Rela *rel;
1552 Elf_Internal_Rela *relend;
1553
1554 dynobj = elf_hash_table (info)->dynobj;
1555 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1556 sym_hashes = elf_sym_hashes (input_bfd);
1557 local_got_offsets = elf_local_got_offsets (input_bfd);
1558
1559 if (elf_hash_table(info)->hgot == NULL)
1560 got_base = 0;
1561 else
1562 got_base = elf_hash_table (info)->hgot->root.u.def.value;
1563
1564 sgot = splt = sreloc = NULL;
1565
1566 rel = relocs;
1567 relend = relocs + input_section->reloc_count;
1568 for (; rel < relend; rel++)
1569 {
1570 int r_type;
1571 reloc_howto_type *howto;
1572 long r_symndx;
1573 struct elf_link_hash_entry *h;
1574 Elf_Internal_Sym *sym;
1575 asection *sec;
1576 bfd_vma relocation;
1577 bfd_reloc_status_type r;
1578
f65054f7 1579 r_type = ELF64_R_TYPE_ID (rel->r_info);
60dac299 1580 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
252b5132
RH
1581 {
1582 bfd_set_error (bfd_error_bad_value);
1583 return false;
1584 }
1585 howto = sparc64_elf_howto_table + r_type;
1586
1587 r_symndx = ELF64_R_SYM (rel->r_info);
1588
1589 if (info->relocateable)
1590 {
1591 /* This is a relocateable link. We don't have to change
1592 anything, unless the reloc is against a section symbol,
1593 in which case we have to adjust according to where the
1594 section symbol winds up in the output section. */
1595 if (r_symndx < symtab_hdr->sh_info)
1596 {
1597 sym = local_syms + r_symndx;
1598 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1599 {
1600 sec = local_sections[r_symndx];
1601 rel->r_addend += sec->output_offset + sym->st_value;
1602 }
1603 }
1604
1605 continue;
1606 }
1607
1608 /* This is a final link. */
1609 h = NULL;
1610 sym = NULL;
1611 sec = NULL;
1612 if (r_symndx < symtab_hdr->sh_info)
1613 {
1614 sym = local_syms + r_symndx;
1615 sec = local_sections[r_symndx];
1616 relocation = (sec->output_section->vma
1617 + sec->output_offset
1618 + sym->st_value);
1619 }
1620 else
1621 {
1622 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1623 while (h->root.type == bfd_link_hash_indirect
1624 || h->root.type == bfd_link_hash_warning)
1625 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1626 if (h->root.type == bfd_link_hash_defined
1627 || h->root.type == bfd_link_hash_defweak)
1628 {
1629 boolean skip_it = false;
1630 sec = h->root.u.def.section;
1631
1632 switch (r_type)
1633 {
1634 case R_SPARC_WPLT30:
1635 case R_SPARC_PLT32:
1636 case R_SPARC_HIPLT22:
1637 case R_SPARC_LOPLT10:
1638 case R_SPARC_PCPLT32:
1639 case R_SPARC_PCPLT22:
1640 case R_SPARC_PCPLT10:
1641 case R_SPARC_PLT64:
1642 if (h->plt.offset != (bfd_vma) -1)
1643 skip_it = true;
1644 break;
1645
1646 case R_SPARC_GOT10:
1647 case R_SPARC_GOT13:
1648 case R_SPARC_GOT22:
1649 if (elf_hash_table(info)->dynamic_sections_created
1650 && (!info->shared
1651 || (!info->symbolic && h->dynindx != -1)
1652 || !(h->elf_link_hash_flags
1653 & ELF_LINK_HASH_DEF_REGULAR)))
1654 skip_it = true;
1655 break;
1656
1657 case R_SPARC_PC10:
1658 case R_SPARC_PC22:
1659 case R_SPARC_PC_HH22:
1660 case R_SPARC_PC_HM10:
1661 case R_SPARC_PC_LM22:
1662 if (!strcmp(h->root.root.string, "_GLOBAL_OFFSET_TABLE_"))
1663 break;
1664 /* FALLTHRU */
1665
1666 case R_SPARC_8:
1667 case R_SPARC_16:
1668 case R_SPARC_32:
1669 case R_SPARC_DISP8:
1670 case R_SPARC_DISP16:
1671 case R_SPARC_DISP32:
1672 case R_SPARC_WDISP30:
1673 case R_SPARC_WDISP22:
1674 case R_SPARC_HI22:
1675 case R_SPARC_22:
1676 case R_SPARC_13:
1677 case R_SPARC_LO10:
1678 case R_SPARC_UA32:
1679 case R_SPARC_10:
1680 case R_SPARC_11:
1681 case R_SPARC_64:
1682 case R_SPARC_OLO10:
1683 case R_SPARC_HH22:
1684 case R_SPARC_HM10:
1685 case R_SPARC_LM22:
1686 case R_SPARC_WDISP19:
1687 case R_SPARC_WDISP16:
1688 case R_SPARC_7:
1689 case R_SPARC_5:
1690 case R_SPARC_6:
1691 case R_SPARC_DISP64:
1692 case R_SPARC_HIX22:
1693 case R_SPARC_LOX10:
1694 case R_SPARC_H44:
1695 case R_SPARC_M44:
1696 case R_SPARC_L44:
1697 case R_SPARC_UA64:
1698 case R_SPARC_UA16:
1699 if (info->shared
1700 && ((!info->symbolic && h->dynindx != -1)
1701 || !(h->elf_link_hash_flags
1702 & ELF_LINK_HASH_DEF_REGULAR)))
1703 skip_it = true;
1704 break;
1705 }
1706
1707 if (skip_it)
1708 {
1709 /* In these cases, we don't need the relocation
1710 value. We check specially because in some
1711 obscure cases sec->output_section will be NULL. */
1712 relocation = 0;
1713 }
1714 else
1715 {
1716 relocation = (h->root.u.def.value
1717 + sec->output_section->vma
1718 + sec->output_offset);
1719 }
1720 }
1721 else if (h->root.type == bfd_link_hash_undefweak)
1722 relocation = 0;
1723 else if (info->shared && !info->symbolic && !info->no_undefined)
1724 relocation = 0;
1725 else
1726 {
1727 if (! ((*info->callbacks->undefined_symbol)
1728 (info, h->root.root.string, input_bfd,
1729 input_section, rel->r_offset)))
1730 return false;
1731 relocation = 0;
1732 }
1733 }
1734
1735 /* When generating a shared object, these relocations are copied
1736 into the output file to be resolved at run time. */
1737 if (info->shared && (input_section->flags & SEC_ALLOC))
1738 {
1739 switch (r_type)
1740 {
1741 case R_SPARC_PC10:
1742 case R_SPARC_PC22:
1743 case R_SPARC_PC_HH22:
1744 case R_SPARC_PC_HM10:
1745 case R_SPARC_PC_LM22:
1746 if (h != NULL
1747 && !strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_"))
1748 break;
1749 /* Fall through. */
1750 case R_SPARC_DISP8:
1751 case R_SPARC_DISP16:
1752 case R_SPARC_DISP32:
1753 case R_SPARC_WDISP30:
1754 case R_SPARC_WDISP22:
1755 case R_SPARC_WDISP19:
1756 case R_SPARC_WDISP16:
1757 case R_SPARC_DISP64:
1758 if (h == NULL)
1759 break;
1760 /* Fall through. */
1761 case R_SPARC_8:
1762 case R_SPARC_16:
1763 case R_SPARC_32:
1764 case R_SPARC_HI22:
1765 case R_SPARC_22:
1766 case R_SPARC_13:
1767 case R_SPARC_LO10:
1768 case R_SPARC_UA32:
1769 case R_SPARC_10:
1770 case R_SPARC_11:
1771 case R_SPARC_64:
1772 case R_SPARC_OLO10:
1773 case R_SPARC_HH22:
1774 case R_SPARC_HM10:
1775 case R_SPARC_LM22:
1776 case R_SPARC_7:
1777 case R_SPARC_5:
1778 case R_SPARC_6:
1779 case R_SPARC_HIX22:
1780 case R_SPARC_LOX10:
1781 case R_SPARC_H44:
1782 case R_SPARC_M44:
1783 case R_SPARC_L44:
1784 case R_SPARC_UA64:
1785 case R_SPARC_UA16:
1786 {
1787 Elf_Internal_Rela outrel;
1788 boolean skip;
1789
1790 if (sreloc == NULL)
1791 {
1792 const char *name =
1793 (bfd_elf_string_from_elf_section
1794 (input_bfd,
1795 elf_elfheader (input_bfd)->e_shstrndx,
1796 elf_section_data (input_section)->rel_hdr.sh_name));
1797
1798 if (name == NULL)
1799 return false;
1800
1801 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1802 && strcmp (bfd_get_section_name(input_bfd,
1803 input_section),
1804 name + 5) == 0);
1805
1806 sreloc = bfd_get_section_by_name (dynobj, name);
1807 BFD_ASSERT (sreloc != NULL);
1808 }
1809
1810 skip = false;
1811
1812 if (elf_section_data (input_section)->stab_info == NULL)
1813 outrel.r_offset = rel->r_offset;
1814 else
1815 {
1816 bfd_vma off;
1817
1818 off = (_bfd_stab_section_offset
1819 (output_bfd, &elf_hash_table (info)->stab_info,
1820 input_section,
1821 &elf_section_data (input_section)->stab_info,
1822 rel->r_offset));
1823 if (off == MINUS_ONE)
1824 skip = true;
1825 outrel.r_offset = off;
1826 }
1827
1828 outrel.r_offset += (input_section->output_section->vma
1829 + input_section->output_offset);
1830
1831 /* Optimize unaligned reloc usage now that we know where
1832 it finally resides. */
1833 switch (r_type)
1834 {
1835 case R_SPARC_16:
1836 if (outrel.r_offset & 1) r_type = R_SPARC_UA16;
1837 break;
1838 case R_SPARC_UA16:
1839 if (!(outrel.r_offset & 1)) r_type = R_SPARC_16;
1840 break;
1841 case R_SPARC_32:
1842 if (outrel.r_offset & 3) r_type = R_SPARC_UA32;
1843 break;
1844 case R_SPARC_UA32:
1845 if (!(outrel.r_offset & 3)) r_type = R_SPARC_32;
1846 break;
1847 case R_SPARC_64:
1848 if (outrel.r_offset & 7) r_type = R_SPARC_UA64;
1849 break;
1850 case R_SPARC_UA64:
1851 if (!(outrel.r_offset & 7)) r_type = R_SPARC_64;
1852 break;
1853 }
1854
1855 if (skip)
1856 memset (&outrel, 0, sizeof outrel);
1857 /* h->dynindx may be -1 if the symbol was marked to
1858 become local. */
1859 else if (h != NULL
1860 && ((! info->symbolic && h->dynindx != -1)
1861 || (h->elf_link_hash_flags
1862 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1863 {
1864 BFD_ASSERT (h->dynindx != -1);
f65054f7
RH
1865 outrel.r_info
1866 = ELF64_R_INFO (h->dynindx,
1867 ELF64_R_TYPE_INFO (
1868 ELF64_R_TYPE_DATA (rel->r_info),
1869 r_type));
252b5132
RH
1870 outrel.r_addend = rel->r_addend;
1871 }
1872 else
1873 {
1874 if (r_type == R_SPARC_64)
1875 {
1876 outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
1877 outrel.r_addend = relocation + rel->r_addend;
1878 }
1879 else
1880 {
1881 long indx;
1882
1883 if (h == NULL)
1884 sec = local_sections[r_symndx];
1885 else
1886 {
1887 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1888 || (h->root.type
1889 == bfd_link_hash_defweak));
1890 sec = h->root.u.def.section;
1891 }
1892 if (sec != NULL && bfd_is_abs_section (sec))
1893 indx = 0;
1894 else if (sec == NULL || sec->owner == NULL)
1895 {
1896 bfd_set_error (bfd_error_bad_value);
1897 return false;
1898 }
1899 else
1900 {
1901 asection *osec;
1902
1903 osec = sec->output_section;
1904 indx = elf_section_data (osec)->dynindx;
1905
1906 /* FIXME: we really should be able to link non-pic
1907 shared libraries. */
1908 if (indx == 0)
1909 {
1910 BFD_FAIL ();
1911 (*_bfd_error_handler)
1912 (_("%s: probably compiled without -fPIC?"),
1913 bfd_get_filename (input_bfd));
1914 bfd_set_error (bfd_error_bad_value);
1915 return false;
1916 }
1917 }
1918
f65054f7
RH
1919 outrel.r_info
1920 = ELF64_R_INFO (indx,
1921 ELF64_R_TYPE_INFO (
1922 ELF64_R_TYPE_DATA (rel->r_info),
1923 r_type));
252b5132
RH
1924
1925 /* For non-RELATIVE dynamic relocations, we keep the
1926 same symbol, and so generally the same addend. But
1927 we do need to adjust those relocations referencing
1928 sections. */
1929 outrel.r_addend = rel->r_addend;
1930 if (r_symndx < symtab_hdr->sh_info
1931 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1932 outrel.r_addend += sec->output_offset+sym->st_value;
1933 }
1934 }
1935
1936 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1937 (((Elf64_External_Rela *)
1938 sreloc->contents)
1939 + sreloc->reloc_count));
1940 ++sreloc->reloc_count;
1941
1942 /* This reloc will be computed at runtime, so there's no
1943 need to do anything now, unless this is a RELATIVE
1944 reloc in an unallocated section. */
1945 if (skip
1946 || (input_section->flags & SEC_ALLOC) != 0
f65054f7 1947 || ELF64_R_TYPE_ID (outrel.r_info) != R_SPARC_RELATIVE)
252b5132
RH
1948 continue;
1949 }
1950 break;
1951 }
1952 }
1953
1954 switch (r_type)
1955 {
1956 case R_SPARC_GOT10:
1957 case R_SPARC_GOT13:
1958 case R_SPARC_GOT22:
1959 /* Relocation is to the entry for this symbol in the global
1960 offset table. */
1961 if (sgot == NULL)
1962 {
1963 sgot = bfd_get_section_by_name (dynobj, ".got");
1964 BFD_ASSERT (sgot != NULL);
1965 }
1966
1967 if (h != NULL)
1968 {
1969 bfd_vma off = h->got.offset;
1970 BFD_ASSERT (off != (bfd_vma) -1);
1971
1972 if (! elf_hash_table (info)->dynamic_sections_created
1973 || (info->shared
1974 && (info->symbolic || h->dynindx == -1)
1975 && (h->elf_link_hash_flags
1976 & ELF_LINK_HASH_DEF_REGULAR)))
1977 {
1978 /* This is actually a static link, or it is a -Bsymbolic
1979 link and the symbol is defined locally, or the symbol
1980 was forced to be local because of a version file. We
1981 must initialize this entry in the global offset table.
1982 Since the offset must always be a multiple of 8, we
1983 use the least significant bit to record whether we
1984 have initialized it already.
1985
1986 When doing a dynamic link, we create a .rela.got
1987 relocation entry to initialize the value. This is
1988 done in the finish_dynamic_symbol routine. */
1989
1990 if ((off & 1) != 0)
1991 off &= ~1;
1992 else
1993 {
1994 bfd_put_64 (output_bfd, relocation,
1995 sgot->contents + off);
1996 h->got.offset |= 1;
1997 }
1998 }
1999 relocation = sgot->output_offset + off - got_base;
2000 }
2001 else
2002 {
2003 bfd_vma off;
2004
2005 BFD_ASSERT (local_got_offsets != NULL);
2006 off = local_got_offsets[r_symndx];
2007 BFD_ASSERT (off != (bfd_vma) -1);
2008
2009 /* The offset must always be a multiple of 8. We use
2010 the least significant bit to record whether we have
2011 already processed this entry. */
2012 if ((off & 1) != 0)
2013 off &= ~1;
2014 else
2015 {
2016 bfd_put_64 (output_bfd, relocation, sgot->contents + off);
2017 local_got_offsets[r_symndx] |= 1;
2018
2019 if (info->shared)
2020 {
2021 asection *srelgot;
2022 Elf_Internal_Rela outrel;
2023
2024 /* We need to generate a R_SPARC_RELATIVE reloc
2025 for the dynamic linker. */
2026 srelgot = bfd_get_section_by_name(dynobj, ".rela.got");
2027 BFD_ASSERT (srelgot != NULL);
2028
2029 outrel.r_offset = (sgot->output_section->vma
2030 + sgot->output_offset
2031 + off);
2032 outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
2033 outrel.r_addend = relocation;
2034 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2035 (((Elf64_External_Rela *)
2036 srelgot->contents)
2037 + srelgot->reloc_count));
2038 ++srelgot->reloc_count;
2039 }
2040 }
2041 relocation = sgot->output_offset + off - got_base;
2042 }
2043 goto do_default;
2044
2045 case R_SPARC_WPLT30:
2046 case R_SPARC_PLT32:
2047 case R_SPARC_HIPLT22:
2048 case R_SPARC_LOPLT10:
2049 case R_SPARC_PCPLT32:
2050 case R_SPARC_PCPLT22:
2051 case R_SPARC_PCPLT10:
2052 case R_SPARC_PLT64:
2053 /* Relocation is to the entry for this symbol in the
2054 procedure linkage table. */
2055 BFD_ASSERT (h != NULL);
2056
2057 if (h->plt.offset == (bfd_vma) -1)
2058 {
2059 /* We didn't make a PLT entry for this symbol. This
2060 happens when statically linking PIC code, or when
2061 using -Bsymbolic. */
2062 goto do_default;
2063 }
2064
2065 if (splt == NULL)
2066 {
2067 splt = bfd_get_section_by_name (dynobj, ".plt");
2068 BFD_ASSERT (splt != NULL);
2069 }
2070
2071 relocation = (splt->output_section->vma
2072 + splt->output_offset
2073 + sparc64_elf_plt_entry_offset (h->plt.offset));
2074 goto do_default;
2075
2076 case R_SPARC_OLO10:
2077 {
2078 bfd_vma x;
2079
2080 relocation += rel->r_addend;
2081 relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
2082
2083 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2084 x = (x & ~0x1fff) | (relocation & 0x1fff);
2085 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2086
2087 r = bfd_check_overflow (howto->complain_on_overflow,
2088 howto->bitsize, howto->rightshift,
2089 bfd_arch_bits_per_address (input_bfd),
2090 relocation);
2091 }
2092 break;
2093
2094 case R_SPARC_WDISP16:
2095 {
2096 bfd_vma x;
2097
2098 relocation += rel->r_addend;
2099 /* Adjust for pc-relative-ness. */
2100 relocation -= (input_section->output_section->vma
2101 + input_section->output_offset);
2102 relocation -= rel->r_offset;
2103
2104 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2105 x = (x & ~0x303fff) | ((((relocation >> 2) & 0xc000) << 6)
2106 | ((relocation >> 2) & 0x3fff));
2107 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2108
2109 r = bfd_check_overflow (howto->complain_on_overflow,
2110 howto->bitsize, howto->rightshift,
2111 bfd_arch_bits_per_address (input_bfd),
2112 relocation);
2113 }
2114 break;
2115
2116 case R_SPARC_HIX22:
2117 {
2118 bfd_vma x;
2119
2120 relocation += rel->r_addend;
2121 relocation = relocation ^ MINUS_ONE;
2122
2123 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2124 x = (x & ~0x3fffff) | ((relocation >> 10) & 0x3fffff);
2125 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2126
2127 r = bfd_check_overflow (howto->complain_on_overflow,
2128 howto->bitsize, howto->rightshift,
2129 bfd_arch_bits_per_address (input_bfd),
2130 relocation);
2131 }
2132 break;
2133
2134 case R_SPARC_LOX10:
2135 {
2136 bfd_vma x;
2137
2138 relocation += rel->r_addend;
2139 relocation = (relocation & 0x3ff) | 0x1c00;
2140
2141 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2142 x = (x & ~0x1fff) | relocation;
2143 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2144
2145 r = bfd_reloc_ok;
2146 }
2147 break;
2148
2149 default:
2150 do_default:
2151 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2152 contents, rel->r_offset,
2153 relocation, rel->r_addend);
2154 break;
2155 }
2156
2157 switch (r)
2158 {
2159 case bfd_reloc_ok:
2160 break;
2161
2162 default:
2163 case bfd_reloc_outofrange:
2164 abort ();
2165
2166 case bfd_reloc_overflow:
2167 {
2168 const char *name;
2169
2170 if (h != NULL)
2171 {
2172 if (h->root.type == bfd_link_hash_undefweak
2173 && howto->pc_relative)
2174 {
2175 /* Assume this is a call protected by other code that
2176 detect the symbol is undefined. If this is the case,
2177 we can safely ignore the overflow. If not, the
2178 program is hosed anyway, and a little warning isn't
2179 going to help. */
2180 break;
2181 }
2182
2183 name = h->root.root.string;
2184 }
2185 else
2186 {
2187 name = (bfd_elf_string_from_elf_section
2188 (input_bfd,
2189 symtab_hdr->sh_link,
2190 sym->st_name));
2191 if (name == NULL)
2192 return false;
2193 if (*name == '\0')
2194 name = bfd_section_name (input_bfd, sec);
2195 }
2196 if (! ((*info->callbacks->reloc_overflow)
2197 (info, name, howto->name, (bfd_vma) 0,
2198 input_bfd, input_section, rel->r_offset)))
2199 return false;
2200 }
2201 break;
2202 }
2203 }
2204
2205 return true;
2206}
2207
2208/* Finish up dynamic symbol handling. We set the contents of various
2209 dynamic sections here. */
2210
2211static boolean
2212sparc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
2213 bfd *output_bfd;
2214 struct bfd_link_info *info;
2215 struct elf_link_hash_entry *h;
2216 Elf_Internal_Sym *sym;
2217{
2218 bfd *dynobj;
2219
2220 dynobj = elf_hash_table (info)->dynobj;
2221
2222 if (h->plt.offset != (bfd_vma) -1)
2223 {
2224 asection *splt;
2225 asection *srela;
2226 Elf_Internal_Rela rela;
2227
2228 /* This symbol has an entry in the PLT. Set it up. */
2229
2230 BFD_ASSERT (h->dynindx != -1);
2231
2232 splt = bfd_get_section_by_name (dynobj, ".plt");
2233 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2234 BFD_ASSERT (splt != NULL && srela != NULL);
2235
2236 /* Fill in the entry in the .rela.plt section. */
2237
2238 if (h->plt.offset < LARGE_PLT_THRESHOLD)
2239 {
2240 rela.r_offset = sparc64_elf_plt_entry_offset (h->plt.offset);
2241 rela.r_addend = 0;
2242 }
2243 else
2244 {
2245 int max = splt->_raw_size / PLT_ENTRY_SIZE;
2246 rela.r_offset = sparc64_elf_plt_ptr_offset (h->plt.offset, max);
2247 rela.r_addend = -(sparc64_elf_plt_entry_offset (h->plt.offset) + 4);
2248 }
2249 rela.r_offset += (splt->output_section->vma + splt->output_offset);
2250 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
2251
2252 bfd_elf64_swap_reloca_out (output_bfd, &rela,
2253 ((Elf64_External_Rela *) srela->contents
2254 + h->plt.offset));
2255
2256 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2257 {
2258 /* Mark the symbol as undefined, rather than as defined in
2259 the .plt section. Leave the value alone. */
2260 sym->st_shndx = SHN_UNDEF;
2261 }
2262 }
2263
2264 if (h->got.offset != (bfd_vma) -1)
2265 {
2266 asection *sgot;
2267 asection *srela;
2268 Elf_Internal_Rela rela;
2269
2270 /* This symbol has an entry in the GOT. Set it up. */
2271
2272 sgot = bfd_get_section_by_name (dynobj, ".got");
2273 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2274 BFD_ASSERT (sgot != NULL && srela != NULL);
2275
2276 rela.r_offset = (sgot->output_section->vma
2277 + sgot->output_offset
2278 + (h->got.offset &~ 1));
2279
2280 /* If this is a -Bsymbolic link, and the symbol is defined
2281 locally, we just want to emit a RELATIVE reloc. Likewise if
2282 the symbol was forced to be local because of a version file.
2283 The entry in the global offset table will already have been
2284 initialized in the relocate_section function. */
2285 if (info->shared
2286 && (info->symbolic || h->dynindx == -1)
2287 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2288 {
2289 asection *sec = h->root.u.def.section;
2290 rela.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
2291 rela.r_addend = (h->root.u.def.value
2292 + sec->output_section->vma
2293 + sec->output_offset);
2294 }
2295 else
2296 {
2297 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
2298 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
2299 rela.r_addend = 0;
2300 }
2301
2302 bfd_elf64_swap_reloca_out (output_bfd, &rela,
2303 ((Elf64_External_Rela *) srela->contents
2304 + srela->reloc_count));
2305 ++srela->reloc_count;
2306 }
2307
2308 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2309 {
2310 asection *s;
2311 Elf_Internal_Rela rela;
2312
2313 /* This symbols needs a copy reloc. Set it up. */
2314
2315 BFD_ASSERT (h->dynindx != -1);
2316
2317 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2318 ".rela.bss");
2319 BFD_ASSERT (s != NULL);
2320
2321 rela.r_offset = (h->root.u.def.value
2322 + h->root.u.def.section->output_section->vma
2323 + h->root.u.def.section->output_offset);
2324 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_COPY);
2325 rela.r_addend = 0;
2326 bfd_elf64_swap_reloca_out (output_bfd, &rela,
2327 ((Elf64_External_Rela *) s->contents
2328 + s->reloc_count));
2329 ++s->reloc_count;
2330 }
2331
2332 /* Mark some specially defined symbols as absolute. */
2333 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2334 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2335 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2336 sym->st_shndx = SHN_ABS;
2337
2338 return true;
2339}
2340
2341/* Finish up the dynamic sections. */
2342
2343static boolean
2344sparc64_elf_finish_dynamic_sections (output_bfd, info)
2345 bfd *output_bfd;
2346 struct bfd_link_info *info;
2347{
2348 bfd *dynobj;
2349 asection *sdyn;
2350 asection *sgot;
2351
2352 dynobj = elf_hash_table (info)->dynobj;
2353
2354 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2355
2356 if (elf_hash_table (info)->dynamic_sections_created)
2357 {
2358 asection *splt;
2359 Elf64_External_Dyn *dyncon, *dynconend;
2360
2361 splt = bfd_get_section_by_name (dynobj, ".plt");
2362 BFD_ASSERT (splt != NULL && sdyn != NULL);
2363
2364 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2365 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2366 for (; dyncon < dynconend; dyncon++)
2367 {
2368 Elf_Internal_Dyn dyn;
2369 const char *name;
2370 boolean size;
2371
2372 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2373
2374 switch (dyn.d_tag)
2375 {
2376 case DT_PLTGOT: name = ".plt"; size = false; break;
2377 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
2378 case DT_JMPREL: name = ".rela.plt"; size = false; break;
2379 default: name = NULL; size = false; break;
2380 }
2381
2382 if (name != NULL)
2383 {
2384 asection *s;
2385
2386 s = bfd_get_section_by_name (output_bfd, name);
2387 if (s == NULL)
2388 dyn.d_un.d_val = 0;
2389 else
2390 {
2391 if (! size)
2392 dyn.d_un.d_ptr = s->vma;
2393 else
2394 {
2395 if (s->_cooked_size != 0)
2396 dyn.d_un.d_val = s->_cooked_size;
2397 else
2398 dyn.d_un.d_val = s->_raw_size;
2399 }
2400 }
2401 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2402 }
2403 }
2404
2405 /* Initialize the contents of the .plt section. */
2406 if (splt->_raw_size > 0)
2407 {
2408 sparc64_elf_build_plt(output_bfd, splt->contents,
2409 splt->_raw_size / PLT_ENTRY_SIZE);
2410 }
2411
2412 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
2413 PLT_ENTRY_SIZE;
2414 }
2415
2416 /* Set the first entry in the global offset table to the address of
2417 the dynamic section. */
2418 sgot = bfd_get_section_by_name (dynobj, ".got");
2419 BFD_ASSERT (sgot != NULL);
2420 if (sgot->_raw_size > 0)
2421 {
2422 if (sdyn == NULL)
2423 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
2424 else
2425 bfd_put_64 (output_bfd,
2426 sdyn->output_section->vma + sdyn->output_offset,
2427 sgot->contents);
2428 }
2429
2430 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 8;
2431
252b5132
RH
2432 return true;
2433}
2434\f
2435/* Functions for dealing with the e_flags field. */
2436
2437/* Merge backend specific data from an object file to the output
2438 object file when linking. */
2439
2440static boolean
2441sparc64_elf_merge_private_bfd_data (ibfd, obfd)
2442 bfd *ibfd;
2443 bfd *obfd;
2444{
2445 boolean error;
2446 flagword new_flags, old_flags;
2447 int new_mm, old_mm;
2448
2449 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2450 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2451 return true;
2452
2453 new_flags = elf_elfheader (ibfd)->e_flags;
2454 old_flags = elf_elfheader (obfd)->e_flags;
2455
2456 if (!elf_flags_init (obfd)) /* First call, no flags set */
2457 {
2458 elf_flags_init (obfd) = true;
2459 elf_elfheader (obfd)->e_flags = new_flags;
2460 }
2461
2462 else if (new_flags == old_flags) /* Compatible flags are ok */
2463 ;
2464
2465 else /* Incompatible flags */
2466 {
2467 error = false;
2468
2469 old_flags |= (new_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1));
2470 new_flags |= (old_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1));
2471 if ((old_flags & (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1)) ==
2472 (EF_SPARC_SUN_US1|EF_SPARC_HAL_R1))
2473 {
2474 error = true;
2475 (*_bfd_error_handler)
2476 (_("%s: linking UltraSPARC specific with HAL specific code"),
2477 bfd_get_filename (ibfd));
2478 }
2479
2480 /* Choose the most restrictive memory ordering */
2481 old_mm = (old_flags & EF_SPARCV9_MM);
2482 new_mm = (new_flags & EF_SPARCV9_MM);
2483 old_flags &= ~EF_SPARCV9_MM;
2484 new_flags &= ~EF_SPARCV9_MM;
2485 if (new_mm < old_mm) old_mm = new_mm;
2486 old_flags |= old_mm;
2487 new_flags |= old_mm;
2488
2489 /* Warn about any other mismatches */
2490 if (new_flags != old_flags)
2491 {
2492 error = true;
2493 (*_bfd_error_handler)
2494 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2495 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
2496 }
2497
2498 elf_elfheader (obfd)->e_flags = old_flags;
2499
2500 if (error)
2501 {
2502 bfd_set_error (bfd_error_bad_value);
2503 return false;
2504 }
2505 }
2506 return true;
2507}
2508
2509\f
2510/* Set the right machine number for a SPARC64 ELF file. */
2511
2512static boolean
2513sparc64_elf_object_p (abfd)
2514 bfd *abfd;
2515{
2516 unsigned long mach = bfd_mach_sparc_v9;
2517
2518 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
2519 mach = bfd_mach_sparc_v9a;
2520 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach);
2521}
2522
f65054f7
RH
2523/* Relocations in the 64 bit SPARC ELF ABI are more complex than in
2524 standard ELF, because R_SPARC_OLO10 has secondary addend in
2525 ELF64_R_TYPE_DATA field. This structure is used to redirect the
2526 relocation handling routines. */
2527
2528const struct elf_size_info sparc64_elf_size_info =
2529{
2530 sizeof (Elf64_External_Ehdr),
2531 sizeof (Elf64_External_Phdr),
2532 sizeof (Elf64_External_Shdr),
2533 sizeof (Elf64_External_Rel),
2534 sizeof (Elf64_External_Rela),
2535 sizeof (Elf64_External_Sym),
2536 sizeof (Elf64_External_Dyn),
2537 sizeof (Elf_External_Note),
2538 8, /* hash-table entry size */
2539 /* internal relocations per external relocations.
2540 For link purposes we use just 1 internal per
2541 1 external, for assembly and slurp symbol table
2542 we use 2. */
2543 1,
2544 64, /* arch_size */
2545 8, /* file_align */
2546 ELFCLASS64,
2547 EV_CURRENT,
2548 bfd_elf64_write_out_phdrs,
2549 bfd_elf64_write_shdrs_and_ehdr,
2550 sparc64_elf_write_relocs,
2551 bfd_elf64_swap_symbol_out,
2552 sparc64_elf_slurp_reloc_table,
2553 bfd_elf64_slurp_symbol_table,
2554 bfd_elf64_swap_dyn_in,
2555 bfd_elf64_swap_dyn_out,
2556 NULL,
2557 NULL,
2558 NULL,
2559 NULL
2560};
2561
252b5132
RH
2562#define TARGET_BIG_SYM bfd_elf64_sparc_vec
2563#define TARGET_BIG_NAME "elf64-sparc"
2564#define ELF_ARCH bfd_arch_sparc
2565#define ELF_MAXPAGESIZE 0x100000
2566
2567/* This is the official ABI value. */
2568#define ELF_MACHINE_CODE EM_SPARCV9
2569
2570/* This is the value that we used before the ABI was released. */
2571#define ELF_MACHINE_ALT1 EM_OLD_SPARCV9
2572
2573#define elf_info_to_howto \
2574 sparc64_elf_info_to_howto
f65054f7
RH
2575#define bfd_elf64_get_reloc_upper_bound \
2576 sparc64_elf_get_reloc_upper_bound
2577#define bfd_elf64_get_dynamic_reloc_upper_bound \
2578 sparc64_elf_get_dynamic_reloc_upper_bound
2579#define bfd_elf64_canonicalize_dynamic_reloc \
2580 sparc64_elf_canonicalize_dynamic_reloc
252b5132
RH
2581#define bfd_elf64_bfd_reloc_type_lookup \
2582 sparc64_elf_reloc_type_lookup
2583
2584#define elf_backend_create_dynamic_sections \
2585 _bfd_elf_create_dynamic_sections
2586#define elf_backend_check_relocs \
2587 sparc64_elf_check_relocs
2588#define elf_backend_adjust_dynamic_symbol \
2589 sparc64_elf_adjust_dynamic_symbol
2590#define elf_backend_size_dynamic_sections \
2591 sparc64_elf_size_dynamic_sections
2592#define elf_backend_relocate_section \
2593 sparc64_elf_relocate_section
2594#define elf_backend_finish_dynamic_symbol \
2595 sparc64_elf_finish_dynamic_symbol
2596#define elf_backend_finish_dynamic_sections \
2597 sparc64_elf_finish_dynamic_sections
2598
2599#define bfd_elf64_bfd_merge_private_bfd_data \
2600 sparc64_elf_merge_private_bfd_data
2601
f65054f7
RH
2602#define elf_backend_size_info \
2603 sparc64_elf_size_info
252b5132
RH
2604#define elf_backend_object_p \
2605 sparc64_elf_object_p
2606
2607#define elf_backend_want_got_plt 0
2608#define elf_backend_plt_readonly 0
2609#define elf_backend_want_plt_sym 1
2610
2611/* Section 5.2.4 of the ABI specifies a 256-byte boundary for the table. */
2612#define elf_backend_plt_alignment 8
2613
2614#define elf_backend_got_header_size 8
2615#define elf_backend_plt_header_size PLT_HEADER_SIZE
2616
2617#include "elf64-target.h"