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