source: trunk/src/binutils/bfd/elf64-sparc.c@ 466

Last change on this file since 466 was 10, checked in by bird, 22 years ago

Initial revision

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