]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf64-sparc.c
2005-04-14 David S. Miller <davem@davemloft.net>
[thirdparty/binutils-gdb.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 2003, 2004, 2005 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
26 /* This is defined if one wants to build upward compatible binaries
27 with the original sparc64-elf toolchain. The support is kept in for
28 now but is turned off by default. dje 970930 */
29 /*#define SPARC64_OLD_RELOCS*/
30
31 #include "elf/sparc.h"
32 #include "opcode/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 reloc_howto_type *sparc64_elf_reloc_type_lookup
38 PARAMS ((bfd *, bfd_reloc_code_real_type));
39 static void sparc64_elf_info_to_howto
40 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
41 static bfd_boolean sparc64_elf_check_relocs
42 PARAMS ((bfd *, struct bfd_link_info *, asection *,
43 const Elf_Internal_Rela *));
44 static bfd_boolean sparc64_elf_adjust_dynamic_symbol
45 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
46 static bfd_boolean allocate_dynrelocs
47 PARAMS ((struct elf_link_hash_entry *, PTR));
48 static bfd_boolean readonly_dynrelocs
49 PARAMS ((struct elf_link_hash_entry *, PTR));
50 static bfd_boolean sparc64_elf_omit_section_dynsym
51 PARAMS ((bfd *, struct bfd_link_info *, asection *));
52 static bfd_boolean sparc64_elf_size_dynamic_sections
53 PARAMS ((bfd *, struct bfd_link_info *));
54 static bfd_boolean sparc64_elf_new_section_hook
55 PARAMS ((bfd *, asection *));
56 static bfd_boolean sparc64_elf_relax_section
57 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
58 static bfd_vma dtpoff_base
59 PARAMS ((struct bfd_link_info *));
60 static bfd_vma tpoff
61 PARAMS ((struct bfd_link_info *, bfd_vma));
62 static bfd_boolean sparc64_elf_relocate_section
63 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
64 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
65 static bfd_boolean sparc64_elf_finish_dynamic_symbol
66 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
67 Elf_Internal_Sym *));
68 static bfd_boolean sparc64_elf_finish_dynamic_sections
69 PARAMS ((bfd *, struct bfd_link_info *));
70 static bfd_boolean sparc64_elf_merge_private_bfd_data
71 PARAMS ((bfd *, bfd *));
72 static struct bfd_hash_entry *link_hash_newfunc
73 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
74 static struct bfd_link_hash_table *sparc64_elf_link_hash_table_create
75 PARAMS ((bfd *));
76 static bfd_boolean create_got_section
77 PARAMS ((bfd *, struct bfd_link_info *));
78 static bfd_boolean sparc64_elf_create_dynamic_sections
79 PARAMS ((bfd *, struct bfd_link_info *));
80 static void sparc64_elf_copy_indirect_symbol
81 PARAMS ((const struct elf_backend_data *, struct elf_link_hash_entry *,
82 struct elf_link_hash_entry *));
83 static int sparc64_elf_tls_transition
84 PARAMS ((struct bfd_link_info *, int, int));
85
86 static bfd_boolean sparc64_elf_mkobject
87 PARAMS ((bfd *));
88 static bfd_boolean sparc64_elf_object_p
89 PARAMS ((bfd *));
90 static enum elf_reloc_type_class sparc64_elf_reloc_type_class
91 PARAMS ((const Elf_Internal_Rela *));
92 static asection *sparc64_elf_gc_mark_hook
93 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
94 struct elf_link_hash_entry *, Elf_Internal_Sym *));
95 static bfd_boolean sparc64_elf_gc_sweep_hook
96 PARAMS ((bfd *, struct bfd_link_info *, asection *,
97 const Elf_Internal_Rela *));
98
99 static bfd_reloc_status_type init_insn_reloc
100 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *,
101 bfd *, bfd_vma *, bfd_vma *));
102 static int sparc64_plt_entry_build
103 PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, bfd_vma *));
104 static int sparc64_elf_get_symbol_type
105 PARAMS (( Elf_Internal_Sym *, int));
106 static bfd_boolean sparc64_elf_add_symbol_hook
107 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
108 const char **, flagword *, asection **, bfd_vma *));
109 static bfd_boolean sparc64_elf_output_arch_syms
110 PARAMS ((bfd *, struct bfd_link_info *, PTR,
111 bfd_boolean (*) (PTR, const char *, Elf_Internal_Sym *,
112 asection *, struct elf_link_hash_entry *)));
113 static void sparc64_elf_symbol_processing
114 PARAMS ((bfd *, asymbol *));
115 static bfd_boolean sparc64_elf_fake_sections
116 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
117 static const char *sparc64_elf_print_symbol_all
118 PARAMS ((bfd *, PTR, asymbol *));
119 static long sparc64_elf_get_reloc_upper_bound
120 PARAMS ((bfd *, asection *));
121 static long sparc64_elf_get_dynamic_reloc_upper_bound
122 PARAMS ((bfd *));
123 static bfd_boolean sparc64_elf_slurp_one_reloc_table
124 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, asymbol **, bfd_boolean));
125 static bfd_boolean sparc64_elf_slurp_reloc_table
126 PARAMS ((bfd *, asection *, asymbol **, bfd_boolean));
127 static long sparc64_elf_canonicalize_reloc
128 PARAMS ((bfd *, asection *, arelent **, asymbol **));
129 static long sparc64_elf_canonicalize_dynamic_reloc
130 PARAMS ((bfd *, arelent **, asymbol **));
131 static void sparc64_elf_write_relocs
132 PARAMS ((bfd *, asection *, PTR));
133 \f
134 /* The relocation "howto" table. */
135
136 static bfd_reloc_status_type sparc_elf_notsup_reloc
137 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
138 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
139 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
140 static bfd_reloc_status_type sparc_elf_hix22_reloc
141 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
142 static bfd_reloc_status_type sparc_elf_lox10_reloc
143 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
144
145 static reloc_howto_type sparc64_elf_howto_table[] =
146 {
147 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
148 HOWTO(R_SPARC_8, 0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", FALSE,0,0x000000ff,TRUE),
149 HOWTO(R_SPARC_16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", FALSE,0,0x0000ffff,TRUE),
150 HOWTO(R_SPARC_32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", FALSE,0,0xffffffff,TRUE),
151 HOWTO(R_SPARC_DISP8, 0,0, 8,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", FALSE,0,0x000000ff,TRUE),
152 HOWTO(R_SPARC_DISP16, 0,1,16,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", FALSE,0,0x0000ffff,TRUE),
153 HOWTO(R_SPARC_DISP32, 0,2,32,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", FALSE,0,0xffffffff,TRUE),
154 HOWTO(R_SPARC_WDISP30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
155 HOWTO(R_SPARC_WDISP22, 2,2,22,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
156 HOWTO(R_SPARC_HI22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", FALSE,0,0x003fffff,TRUE),
157 HOWTO(R_SPARC_22, 0,2,22,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", FALSE,0,0x003fffff,TRUE),
158 HOWTO(R_SPARC_13, 0,2,13,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", FALSE,0,0x00001fff,TRUE),
159 HOWTO(R_SPARC_LO10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", FALSE,0,0x000003ff,TRUE),
160 HOWTO(R_SPARC_GOT10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", FALSE,0,0x000003ff,TRUE),
161 HOWTO(R_SPARC_GOT13, 0,2,13,FALSE,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", FALSE,0,0x00001fff,TRUE),
162 HOWTO(R_SPARC_GOT22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", FALSE,0,0x003fffff,TRUE),
163 HOWTO(R_SPARC_PC10, 0,2,10,TRUE, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", FALSE,0,0x000003ff,TRUE),
164 HOWTO(R_SPARC_PC22, 10,2,22,TRUE, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", FALSE,0,0x003fffff,TRUE),
165 HOWTO(R_SPARC_WPLT30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", FALSE,0,0x3fffffff,TRUE),
166 HOWTO(R_SPARC_COPY, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", FALSE,0,0x00000000,TRUE),
167 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),
168 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),
169 HOWTO(R_SPARC_RELATIVE, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
170 HOWTO(R_SPARC_UA32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA32", FALSE,0,0xffffffff,TRUE),
171 #ifndef SPARC64_OLD_RELOCS
172 HOWTO(R_SPARC_PLT32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT32", FALSE,0,0xffffffff,TRUE),
173 /* These aren't implemented yet. */
174 HOWTO(R_SPARC_HIPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_HIPLT22", FALSE,0,0x00000000,TRUE),
175 HOWTO(R_SPARC_LOPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_LOPLT10", FALSE,0,0x00000000,TRUE),
176 HOWTO(R_SPARC_PCPLT32, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT32", FALSE,0,0x00000000,TRUE),
177 HOWTO(R_SPARC_PCPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT22", FALSE,0,0x00000000,TRUE),
178 HOWTO(R_SPARC_PCPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT10", FALSE,0,0x00000000,TRUE),
179 #endif
180 HOWTO(R_SPARC_10, 0,2,10,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", FALSE,0,0x000003ff,TRUE),
181 HOWTO(R_SPARC_11, 0,2,11,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", FALSE,0,0x000007ff,TRUE),
182 HOWTO(R_SPARC_64, 0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_64", FALSE,0,MINUS_ONE, TRUE),
183 HOWTO(R_SPARC_OLO10, 0,2,13,FALSE,0,complain_overflow_signed, sparc_elf_notsup_reloc, "R_SPARC_OLO10", FALSE,0,0x00001fff,TRUE),
184 HOWTO(R_SPARC_HH22, 42,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_HH22", FALSE,0,0x003fffff,TRUE),
185 HOWTO(R_SPARC_HM10, 32,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HM10", FALSE,0,0x000003ff,TRUE),
186 HOWTO(R_SPARC_LM22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LM22", FALSE,0,0x003fffff,TRUE),
187 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),
188 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),
189 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),
190 HOWTO(R_SPARC_WDISP16, 2,2,16,TRUE, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", FALSE,0,0x00000000,TRUE),
191 HOWTO(R_SPARC_WDISP19, 2,2,19,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", FALSE,0,0x0007ffff,TRUE),
192 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),
193 HOWTO(R_SPARC_7, 0,2, 7,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", FALSE,0,0x0000007f,TRUE),
194 HOWTO(R_SPARC_5, 0,2, 5,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", FALSE,0,0x0000001f,TRUE),
195 HOWTO(R_SPARC_6, 0,2, 6,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", FALSE,0,0x0000003f,TRUE),
196 HOWTO(R_SPARC_DISP64, 0,4,64,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP64", FALSE,0,MINUS_ONE, TRUE),
197 HOWTO(R_SPARC_PLT64, 0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT64", FALSE,0,MINUS_ONE, TRUE),
198 HOWTO(R_SPARC_HIX22, 0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_HIX22", FALSE,0,MINUS_ONE, FALSE),
199 HOWTO(R_SPARC_LOX10, 0,4, 0,FALSE,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_LOX10", FALSE,0,MINUS_ONE, FALSE),
200 HOWTO(R_SPARC_H44, 22,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_H44", FALSE,0,0x003fffff,FALSE),
201 HOWTO(R_SPARC_M44, 12,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_M44", FALSE,0,0x000003ff,FALSE),
202 HOWTO(R_SPARC_L44, 0,2,13,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_L44", FALSE,0,0x00000fff,FALSE),
203 HOWTO(R_SPARC_REGISTER, 0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_REGISTER",FALSE,0,MINUS_ONE, FALSE),
204 HOWTO(R_SPARC_UA64, 0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA64", FALSE,0,MINUS_ONE, TRUE),
205 HOWTO(R_SPARC_UA16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", FALSE,0,0x0000ffff,TRUE),
206 HOWTO(R_SPARC_TLS_GD_HI22,10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_HI22",FALSE,0,0x003fffff,TRUE),
207 HOWTO(R_SPARC_TLS_GD_LO10,0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_LO10",FALSE,0,0x000003ff,TRUE),
208 HOWTO(R_SPARC_TLS_GD_ADD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_ADD",FALSE,0,0x00000000,TRUE),
209 HOWTO(R_SPARC_TLS_GD_CALL,2,2,30,TRUE,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_TLS_GD_CALL",FALSE,0,0x3fffffff,TRUE),
210 HOWTO(R_SPARC_TLS_LDM_HI22,10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_HI22",FALSE,0,0x003fffff,TRUE),
211 HOWTO(R_SPARC_TLS_LDM_LO10,0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_LO10",FALSE,0,0x000003ff,TRUE),
212 HOWTO(R_SPARC_TLS_LDM_ADD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_ADD",FALSE,0,0x00000000,TRUE),
213 HOWTO(R_SPARC_TLS_LDM_CALL,2,2,30,TRUE,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_TLS_LDM_CALL",FALSE,0,0x3fffffff,TRUE),
214 HOWTO(R_SPARC_TLS_LDO_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_TLS_LDO_HIX22",FALSE,0,0x003fffff, FALSE),
215 HOWTO(R_SPARC_TLS_LDO_LOX10,0,2,0,FALSE,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_TLS_LDO_LOX10",FALSE,0,0x000003ff, FALSE),
216 HOWTO(R_SPARC_TLS_LDO_ADD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_LDO_ADD",FALSE,0,0x00000000,TRUE),
217 HOWTO(R_SPARC_TLS_IE_HI22,10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_HI22",FALSE,0,0x003fffff,TRUE),
218 HOWTO(R_SPARC_TLS_IE_LO10,0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_LO10",FALSE,0,0x000003ff,TRUE),
219 HOWTO(R_SPARC_TLS_IE_LD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_LD",FALSE,0,0x00000000,TRUE),
220 HOWTO(R_SPARC_TLS_IE_LDX,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_LDX",FALSE,0,0x00000000,TRUE),
221 HOWTO(R_SPARC_TLS_IE_ADD,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_IE_ADD",FALSE,0,0x00000000,TRUE),
222 HOWTO(R_SPARC_TLS_LE_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_TLS_LE_HIX22",FALSE,0,0x003fffff, FALSE),
223 HOWTO(R_SPARC_TLS_LE_LOX10,0,2,0,FALSE,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_TLS_LE_LOX10",FALSE,0,0x000003ff, FALSE),
224 HOWTO(R_SPARC_TLS_DTPMOD32,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_DTPMOD32",FALSE,0,0x00000000,TRUE),
225 HOWTO(R_SPARC_TLS_DTPMOD64,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_DTPMOD64",FALSE,0,0x00000000,TRUE),
226 HOWTO(R_SPARC_TLS_DTPOFF32,0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF32",FALSE,0,0xffffffff,TRUE),
227 HOWTO(R_SPARC_TLS_DTPOFF64,0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF64",FALSE,0,MINUS_ONE,TRUE),
228 HOWTO(R_SPARC_TLS_TPOFF32,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_TPOFF32",FALSE,0,0x00000000,TRUE),
229 HOWTO(R_SPARC_TLS_TPOFF64,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_TPOFF64",FALSE,0,0x00000000,TRUE)
230 };
231
232 struct elf_reloc_map {
233 bfd_reloc_code_real_type bfd_reloc_val;
234 unsigned char elf_reloc_val;
235 };
236
237 static const struct elf_reloc_map sparc_reloc_map[] =
238 {
239 { BFD_RELOC_NONE, R_SPARC_NONE, },
240 { BFD_RELOC_16, R_SPARC_16, },
241 { BFD_RELOC_16_PCREL, R_SPARC_DISP16 },
242 { BFD_RELOC_8, R_SPARC_8 },
243 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
244 { BFD_RELOC_CTOR, R_SPARC_64 },
245 { BFD_RELOC_32, R_SPARC_32 },
246 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
247 { BFD_RELOC_HI22, R_SPARC_HI22 },
248 { BFD_RELOC_LO10, R_SPARC_LO10, },
249 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
250 { BFD_RELOC_64_PCREL, R_SPARC_DISP64 },
251 { BFD_RELOC_SPARC22, R_SPARC_22 },
252 { BFD_RELOC_SPARC13, R_SPARC_13 },
253 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
254 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
255 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
256 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
257 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
258 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
259 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
260 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
261 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
262 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
263 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
264 { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
265 { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
266 { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
267 { BFD_RELOC_SPARC_10, R_SPARC_10 },
268 { BFD_RELOC_SPARC_11, R_SPARC_11 },
269 { BFD_RELOC_SPARC_64, R_SPARC_64 },
270 { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
271 { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
272 { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
273 { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
274 { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
275 { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
276 { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
277 { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
278 { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
279 { BFD_RELOC_SPARC_7, R_SPARC_7 },
280 { BFD_RELOC_SPARC_5, R_SPARC_5 },
281 { BFD_RELOC_SPARC_6, R_SPARC_6 },
282 { BFD_RELOC_SPARC_DISP64, R_SPARC_DISP64 },
283 { BFD_RELOC_SPARC_TLS_GD_HI22, R_SPARC_TLS_GD_HI22 },
284 { BFD_RELOC_SPARC_TLS_GD_LO10, R_SPARC_TLS_GD_LO10 },
285 { BFD_RELOC_SPARC_TLS_GD_ADD, R_SPARC_TLS_GD_ADD },
286 { BFD_RELOC_SPARC_TLS_GD_CALL, R_SPARC_TLS_GD_CALL },
287 { BFD_RELOC_SPARC_TLS_LDM_HI22, R_SPARC_TLS_LDM_HI22 },
288 { BFD_RELOC_SPARC_TLS_LDM_LO10, R_SPARC_TLS_LDM_LO10 },
289 { BFD_RELOC_SPARC_TLS_LDM_ADD, R_SPARC_TLS_LDM_ADD },
290 { BFD_RELOC_SPARC_TLS_LDM_CALL, R_SPARC_TLS_LDM_CALL },
291 { BFD_RELOC_SPARC_TLS_LDO_HIX22, R_SPARC_TLS_LDO_HIX22 },
292 { BFD_RELOC_SPARC_TLS_LDO_LOX10, R_SPARC_TLS_LDO_LOX10 },
293 { BFD_RELOC_SPARC_TLS_LDO_ADD, R_SPARC_TLS_LDO_ADD },
294 { BFD_RELOC_SPARC_TLS_IE_HI22, R_SPARC_TLS_IE_HI22 },
295 { BFD_RELOC_SPARC_TLS_IE_LO10, R_SPARC_TLS_IE_LO10 },
296 { BFD_RELOC_SPARC_TLS_IE_LD, R_SPARC_TLS_IE_LD },
297 { BFD_RELOC_SPARC_TLS_IE_LDX, R_SPARC_TLS_IE_LDX },
298 { BFD_RELOC_SPARC_TLS_IE_ADD, R_SPARC_TLS_IE_ADD },
299 { BFD_RELOC_SPARC_TLS_LE_HIX22, R_SPARC_TLS_LE_HIX22 },
300 { BFD_RELOC_SPARC_TLS_LE_LOX10, R_SPARC_TLS_LE_LOX10 },
301 { BFD_RELOC_SPARC_TLS_DTPMOD32, R_SPARC_TLS_DTPMOD32 },
302 { BFD_RELOC_SPARC_TLS_DTPMOD64, R_SPARC_TLS_DTPMOD64 },
303 { BFD_RELOC_SPARC_TLS_DTPOFF32, R_SPARC_TLS_DTPOFF32 },
304 { BFD_RELOC_SPARC_TLS_DTPOFF64, R_SPARC_TLS_DTPOFF64 },
305 { BFD_RELOC_SPARC_TLS_TPOFF32, R_SPARC_TLS_TPOFF32 },
306 { BFD_RELOC_SPARC_TLS_TPOFF64, R_SPARC_TLS_TPOFF64 },
307 #ifndef SPARC64_OLD_RELOCS
308 { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 },
309 #endif
310 { BFD_RELOC_SPARC_PLT64, R_SPARC_PLT64 },
311 { BFD_RELOC_SPARC_HIX22, R_SPARC_HIX22 },
312 { BFD_RELOC_SPARC_LOX10, R_SPARC_LOX10 },
313 { BFD_RELOC_SPARC_H44, R_SPARC_H44 },
314 { BFD_RELOC_SPARC_M44, R_SPARC_M44 },
315 { BFD_RELOC_SPARC_L44, R_SPARC_L44 },
316 { BFD_RELOC_SPARC_REGISTER, R_SPARC_REGISTER }
317 };
318
319 static reloc_howto_type *
320 sparc64_elf_reloc_type_lookup (abfd, code)
321 bfd *abfd ATTRIBUTE_UNUSED;
322 bfd_reloc_code_real_type code;
323 {
324 unsigned int i;
325 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
326 {
327 if (sparc_reloc_map[i].bfd_reloc_val == code)
328 return &sparc64_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
329 }
330 return 0;
331 }
332
333 static void
334 sparc64_elf_info_to_howto (abfd, cache_ptr, dst)
335 bfd *abfd ATTRIBUTE_UNUSED;
336 arelent *cache_ptr;
337 Elf_Internal_Rela *dst;
338 {
339 BFD_ASSERT (ELF64_R_TYPE_ID (dst->r_info) < (unsigned int) R_SPARC_max_std);
340 cache_ptr->howto = &sparc64_elf_howto_table[ELF64_R_TYPE_ID (dst->r_info)];
341 }
342 \f
343 struct sparc64_elf_section_data
344 {
345 struct bfd_elf_section_data elf;
346 unsigned int do_relax, reloc_count;
347 };
348
349 #define sec_do_relax(sec) \
350 ((struct sparc64_elf_section_data *) elf_section_data (sec))->do_relax
351 #define canon_reloc_count(sec) \
352 ((struct sparc64_elf_section_data *) elf_section_data (sec))->reloc_count
353
354 /* Due to the way how we handle R_SPARC_OLO10, each entry in a SHT_RELA
355 section can represent up to two relocs, we must tell the user to allocate
356 more space. */
357
358 static long
359 sparc64_elf_get_reloc_upper_bound (abfd, sec)
360 bfd *abfd ATTRIBUTE_UNUSED;
361 asection *sec;
362 {
363 return (sec->reloc_count * 2 + 1) * sizeof (arelent *);
364 }
365
366 static long
367 sparc64_elf_get_dynamic_reloc_upper_bound (abfd)
368 bfd *abfd;
369 {
370 return _bfd_elf_get_dynamic_reloc_upper_bound (abfd) * 2;
371 }
372
373 /* Read relocations for ASECT from REL_HDR. There are RELOC_COUNT of
374 them. We cannot use generic elf routines for this, because R_SPARC_OLO10
375 has secondary addend in ELF64_R_TYPE_DATA. We handle it as two relocations
376 for the same location, R_SPARC_LO10 and R_SPARC_13. */
377
378 static bfd_boolean
379 sparc64_elf_slurp_one_reloc_table (abfd, asect, rel_hdr, symbols, dynamic)
380 bfd *abfd;
381 asection *asect;
382 Elf_Internal_Shdr *rel_hdr;
383 asymbol **symbols;
384 bfd_boolean dynamic;
385 {
386 PTR allocated = NULL;
387 bfd_byte *native_relocs;
388 arelent *relent;
389 unsigned int i;
390 int entsize;
391 bfd_size_type count;
392 arelent *relents;
393
394 allocated = (PTR) bfd_malloc (rel_hdr->sh_size);
395 if (allocated == NULL)
396 goto error_return;
397
398 if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
399 || bfd_bread (allocated, rel_hdr->sh_size, abfd) != rel_hdr->sh_size)
400 goto error_return;
401
402 native_relocs = (bfd_byte *) allocated;
403
404 relents = asect->relocation + canon_reloc_count (asect);
405
406 entsize = rel_hdr->sh_entsize;
407 BFD_ASSERT (entsize == sizeof (Elf64_External_Rela));
408
409 count = rel_hdr->sh_size / entsize;
410
411 for (i = 0, relent = relents; i < count;
412 i++, relent++, native_relocs += entsize)
413 {
414 Elf_Internal_Rela rela;
415
416 bfd_elf64_swap_reloca_in (abfd, native_relocs, &rela);
417
418 /* The address of an ELF reloc is section relative for an object
419 file, and absolute for an executable file or shared library.
420 The address of a normal BFD reloc is always section relative,
421 and the address of a dynamic reloc is absolute.. */
422 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
423 relent->address = rela.r_offset;
424 else
425 relent->address = rela.r_offset - asect->vma;
426
427 if (ELF64_R_SYM (rela.r_info) == 0)
428 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
429 else
430 {
431 asymbol **ps, *s;
432
433 ps = symbols + ELF64_R_SYM (rela.r_info) - 1;
434 s = *ps;
435
436 /* Canonicalize ELF section symbols. FIXME: Why? */
437 if ((s->flags & BSF_SECTION_SYM) == 0)
438 relent->sym_ptr_ptr = ps;
439 else
440 relent->sym_ptr_ptr = s->section->symbol_ptr_ptr;
441 }
442
443 relent->addend = rela.r_addend;
444
445 BFD_ASSERT (ELF64_R_TYPE_ID (rela.r_info) < (unsigned int) R_SPARC_max_std);
446 if (ELF64_R_TYPE_ID (rela.r_info) == R_SPARC_OLO10)
447 {
448 relent->howto = &sparc64_elf_howto_table[R_SPARC_LO10];
449 relent[1].address = relent->address;
450 relent++;
451 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
452 relent->addend = ELF64_R_TYPE_DATA (rela.r_info);
453 relent->howto = &sparc64_elf_howto_table[R_SPARC_13];
454 }
455 else
456 relent->howto = &sparc64_elf_howto_table[ELF64_R_TYPE_ID (rela.r_info)];
457 }
458
459 canon_reloc_count (asect) += relent - relents;
460
461 if (allocated != NULL)
462 free (allocated);
463
464 return TRUE;
465
466 error_return:
467 if (allocated != NULL)
468 free (allocated);
469 return FALSE;
470 }
471
472 /* Read in and swap the external relocs. */
473
474 static bfd_boolean
475 sparc64_elf_slurp_reloc_table (abfd, asect, symbols, dynamic)
476 bfd *abfd;
477 asection *asect;
478 asymbol **symbols;
479 bfd_boolean dynamic;
480 {
481 struct bfd_elf_section_data * const d = elf_section_data (asect);
482 Elf_Internal_Shdr *rel_hdr;
483 Elf_Internal_Shdr *rel_hdr2;
484 bfd_size_type amt;
485
486 if (asect->relocation != NULL)
487 return TRUE;
488
489 if (! dynamic)
490 {
491 if ((asect->flags & SEC_RELOC) == 0
492 || asect->reloc_count == 0)
493 return TRUE;
494
495 rel_hdr = &d->rel_hdr;
496 rel_hdr2 = d->rel_hdr2;
497
498 BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset
499 || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
500 }
501 else
502 {
503 /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
504 case because relocations against this section may use the
505 dynamic symbol table, and in that case bfd_section_from_shdr
506 in elf.c does not update the RELOC_COUNT. */
507 if (asect->size == 0)
508 return TRUE;
509
510 rel_hdr = &d->this_hdr;
511 asect->reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
512 rel_hdr2 = NULL;
513 }
514
515 amt = asect->reloc_count;
516 amt *= 2 * sizeof (arelent);
517 asect->relocation = (arelent *) bfd_alloc (abfd, amt);
518 if (asect->relocation == NULL)
519 return FALSE;
520
521 /* The sparc64_elf_slurp_one_reloc_table routine increments
522 canon_reloc_count. */
523 canon_reloc_count (asect) = 0;
524
525 if (!sparc64_elf_slurp_one_reloc_table (abfd, asect, rel_hdr, symbols,
526 dynamic))
527 return FALSE;
528
529 if (rel_hdr2
530 && !sparc64_elf_slurp_one_reloc_table (abfd, asect, rel_hdr2, symbols,
531 dynamic))
532 return FALSE;
533
534 return TRUE;
535 }
536
537 /* Canonicalize the relocs. */
538
539 static long
540 sparc64_elf_canonicalize_reloc (abfd, section, relptr, symbols)
541 bfd *abfd;
542 sec_ptr section;
543 arelent **relptr;
544 asymbol **symbols;
545 {
546 arelent *tblptr;
547 unsigned int i;
548 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
549
550 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
551 return -1;
552
553 tblptr = section->relocation;
554 for (i = 0; i < canon_reloc_count (section); i++)
555 *relptr++ = tblptr++;
556
557 *relptr = NULL;
558
559 return canon_reloc_count (section);
560 }
561
562
563 /* Canonicalize the dynamic relocation entries. Note that we return
564 the dynamic relocations as a single block, although they are
565 actually associated with particular sections; the interface, which
566 was designed for SunOS style shared libraries, expects that there
567 is only one set of dynamic relocs. Any section that was actually
568 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
569 the dynamic symbol table, is considered to be a dynamic reloc
570 section. */
571
572 static long
573 sparc64_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
574 bfd *abfd;
575 arelent **storage;
576 asymbol **syms;
577 {
578 asection *s;
579 long ret;
580
581 if (elf_dynsymtab (abfd) == 0)
582 {
583 bfd_set_error (bfd_error_invalid_operation);
584 return -1;
585 }
586
587 ret = 0;
588 for (s = abfd->sections; s != NULL; s = s->next)
589 {
590 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
591 && (elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
592 {
593 arelent *p;
594 long count, i;
595
596 if (! sparc64_elf_slurp_reloc_table (abfd, s, syms, TRUE))
597 return -1;
598 count = canon_reloc_count (s);
599 p = s->relocation;
600 for (i = 0; i < count; i++)
601 *storage++ = p++;
602 ret += count;
603 }
604 }
605
606 *storage = NULL;
607
608 return ret;
609 }
610
611 /* Write out the relocs. */
612
613 static void
614 sparc64_elf_write_relocs (abfd, sec, data)
615 bfd *abfd;
616 asection *sec;
617 PTR data;
618 {
619 bfd_boolean *failedp = (bfd_boolean *) data;
620 Elf_Internal_Shdr *rela_hdr;
621 Elf64_External_Rela *outbound_relocas, *src_rela;
622 unsigned int idx, count;
623 asymbol *last_sym = 0;
624 int last_sym_idx = 0;
625
626 /* If we have already failed, don't do anything. */
627 if (*failedp)
628 return;
629
630 if ((sec->flags & SEC_RELOC) == 0)
631 return;
632
633 /* The linker backend writes the relocs out itself, and sets the
634 reloc_count field to zero to inhibit writing them here. Also,
635 sometimes the SEC_RELOC flag gets set even when there aren't any
636 relocs. */
637 if (sec->reloc_count == 0)
638 return;
639
640 /* We can combine two relocs that refer to the same address
641 into R_SPARC_OLO10 if first one is R_SPARC_LO10 and the
642 latter is R_SPARC_13 with no associated symbol. */
643 count = 0;
644 for (idx = 0; idx < sec->reloc_count; idx++)
645 {
646 bfd_vma addr;
647
648 ++count;
649
650 addr = sec->orelocation[idx]->address;
651 if (sec->orelocation[idx]->howto->type == R_SPARC_LO10
652 && idx < sec->reloc_count - 1)
653 {
654 arelent *r = sec->orelocation[idx + 1];
655
656 if (r->howto->type == R_SPARC_13
657 && r->address == addr
658 && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
659 && (*r->sym_ptr_ptr)->value == 0)
660 ++idx;
661 }
662 }
663
664 rela_hdr = &elf_section_data (sec)->rel_hdr;
665
666 rela_hdr->sh_size = rela_hdr->sh_entsize * count;
667 rela_hdr->contents = (PTR) bfd_alloc (abfd, rela_hdr->sh_size);
668 if (rela_hdr->contents == NULL)
669 {
670 *failedp = TRUE;
671 return;
672 }
673
674 /* Figure out whether the relocations are RELA or REL relocations. */
675 if (rela_hdr->sh_type != SHT_RELA)
676 abort ();
677
678 /* orelocation has the data, reloc_count has the count... */
679 outbound_relocas = (Elf64_External_Rela *) rela_hdr->contents;
680 src_rela = outbound_relocas;
681
682 for (idx = 0; idx < sec->reloc_count; idx++)
683 {
684 Elf_Internal_Rela dst_rela;
685 arelent *ptr;
686 asymbol *sym;
687 int n;
688
689 ptr = sec->orelocation[idx];
690
691 /* The address of an ELF reloc is section relative for an object
692 file, and absolute for an executable file or shared library.
693 The address of a BFD reloc is always section relative. */
694 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
695 dst_rela.r_offset = ptr->address;
696 else
697 dst_rela.r_offset = ptr->address + sec->vma;
698
699 sym = *ptr->sym_ptr_ptr;
700 if (sym == last_sym)
701 n = last_sym_idx;
702 else if (bfd_is_abs_section (sym->section) && sym->value == 0)
703 n = STN_UNDEF;
704 else
705 {
706 last_sym = sym;
707 n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
708 if (n < 0)
709 {
710 *failedp = TRUE;
711 return;
712 }
713 last_sym_idx = n;
714 }
715
716 if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
717 && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
718 && ! _bfd_elf_validate_reloc (abfd, ptr))
719 {
720 *failedp = TRUE;
721 return;
722 }
723
724 if (ptr->howto->type == R_SPARC_LO10
725 && idx < sec->reloc_count - 1)
726 {
727 arelent *r = sec->orelocation[idx + 1];
728
729 if (r->howto->type == R_SPARC_13
730 && r->address == ptr->address
731 && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
732 && (*r->sym_ptr_ptr)->value == 0)
733 {
734 idx++;
735 dst_rela.r_info
736 = ELF64_R_INFO (n, ELF64_R_TYPE_INFO (r->addend,
737 R_SPARC_OLO10));
738 }
739 else
740 dst_rela.r_info = ELF64_R_INFO (n, R_SPARC_LO10);
741 }
742 else
743 dst_rela.r_info = ELF64_R_INFO (n, ptr->howto->type);
744
745 dst_rela.r_addend = ptr->addend;
746 bfd_elf64_swap_reloca_out (abfd, &dst_rela, (bfd_byte *) src_rela);
747 ++src_rela;
748 }
749 }
750 \f
751 /* The SPARC linker needs to keep track of the number of relocs that it
752 decides to copy as dynamic relocs in check_relocs for each symbol.
753 This is so that it can later discard them if they are found to be
754 unnecessary. We store the information in a field extending the
755 regular ELF linker hash table. */
756
757 struct sparc64_elf_dyn_relocs
758 {
759 struct sparc64_elf_dyn_relocs *next;
760
761 /* The input section of the reloc. */
762 asection *sec;
763
764 /* Total number of relocs copied for the input section. */
765 bfd_size_type count;
766
767 /* Number of pc-relative relocs copied for the input section. */
768 bfd_size_type pc_count;
769 };
770
771 /* SPARC ELF linker hash entry. */
772
773 struct sparc64_elf_link_hash_entry
774 {
775 struct elf_link_hash_entry elf;
776
777 /* Track dynamic relocs copied for this symbol. */
778 struct sparc64_elf_dyn_relocs *dyn_relocs;
779
780 #define GOT_UNKNOWN 0
781 #define GOT_NORMAL 1
782 #define GOT_TLS_GD 2
783 #define GOT_TLS_IE 3
784 unsigned char tls_type;
785 };
786
787 #define sparc64_elf_hash_entry(ent) ((struct sparc64_elf_link_hash_entry *)(ent))
788
789 struct sparc64_elf_obj_tdata
790 {
791 struct elf_obj_tdata root;
792
793 /* tls_type for each local got entry. */
794 char *local_got_tls_type;
795
796 /* TRUE if TLS GD relocs has been seen for this object. */
797 bfd_boolean has_tlsgd;
798 };
799
800 #define sparc64_elf_tdata(abfd) \
801 ((struct sparc64_elf_obj_tdata *) (abfd)->tdata.any)
802
803 #define sparc64_elf_local_got_tls_type(abfd) \
804 (sparc64_elf_tdata (abfd)->local_got_tls_type)
805
806 static bfd_boolean
807 sparc64_elf_mkobject (abfd)
808 bfd *abfd;
809 {
810 bfd_size_type amt = sizeof (struct sparc64_elf_obj_tdata);
811 abfd->tdata.any = bfd_zalloc (abfd, amt);
812 if (abfd->tdata.any == NULL)
813 return FALSE;
814 return TRUE;
815 }
816
817 /* Sparc64 ELF linker hash table. */
818
819 struct sparc64_elf_app_reg
820 {
821 unsigned char bind;
822 unsigned short shndx;
823 bfd *abfd;
824 char *name;
825 };
826
827 struct sparc64_elf_link_hash_table
828 {
829 struct elf_link_hash_table elf;
830
831 /* Short-cuts to get to dynamic linker sections. */
832 asection *sgot;
833 asection *srelgot;
834 asection *splt;
835 asection *srelplt;
836 asection *sdynbss;
837 asection *srelbss;
838
839 union {
840 bfd_signed_vma refcount;
841 bfd_vma offset;
842 } tls_ldm_got;
843
844 /* Small local sym to section mapping cache. */
845 struct sym_sec_cache sym_sec;
846
847 struct sparc64_elf_app_reg app_regs [4];
848 };
849
850 /* Get the Sparc64 ELF linker hash table from a link_info structure. */
851
852 #define sparc64_elf_hash_table(p) \
853 ((struct sparc64_elf_link_hash_table *) ((p)->hash))
854
855 /* Create an entry in an SPARC ELF linker hash table. */
856
857 static struct bfd_hash_entry *
858 link_hash_newfunc (entry, table, string)
859 struct bfd_hash_entry *entry;
860 struct bfd_hash_table *table;
861 const char *string;
862 {
863 /* Allocate the structure if it has not already been allocated by a
864 subclass. */
865 if (entry == NULL)
866 {
867 entry = bfd_hash_allocate (table,
868 sizeof (struct sparc64_elf_link_hash_entry));
869 if (entry == NULL)
870 return entry;
871 }
872
873 /* Call the allocation method of the superclass. */
874 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
875 if (entry != NULL)
876 {
877 struct sparc64_elf_link_hash_entry *eh;
878
879 eh = (struct sparc64_elf_link_hash_entry *) entry;
880 eh->dyn_relocs = NULL;
881 eh->tls_type = GOT_UNKNOWN;
882 }
883
884 return entry;
885 }
886
887 /* Create a SPARC64 ELF linker hash table. */
888
889 static struct bfd_link_hash_table *
890 sparc64_elf_link_hash_table_create (abfd)
891 bfd *abfd;
892 {
893 struct sparc64_elf_link_hash_table *ret;
894 bfd_size_type amt = sizeof (struct sparc64_elf_link_hash_table);
895
896 ret = (struct sparc64_elf_link_hash_table *) bfd_zmalloc (amt);
897 if (ret == NULL)
898 return NULL;
899
900 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
901 {
902 free (ret);
903 return NULL;
904 }
905
906 return &ret->elf.root;
907 }
908
909 /* Create .got and .rela.got sections in DYNOBJ, and set up
910 shortcuts to them in our hash table. */
911
912 static bfd_boolean
913 create_got_section (dynobj, info)
914 bfd *dynobj;
915 struct bfd_link_info *info;
916 {
917 struct sparc64_elf_link_hash_table *htab;
918
919 if (! _bfd_elf_create_got_section (dynobj, info))
920 return FALSE;
921
922 htab = sparc64_elf_hash_table (info);
923 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
924 BFD_ASSERT (htab->sgot != NULL);
925
926 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
927 if (htab->srelgot == NULL
928 || ! bfd_set_section_flags (dynobj, htab->srelgot, SEC_ALLOC
929 | SEC_LOAD
930 | SEC_HAS_CONTENTS
931 | SEC_IN_MEMORY
932 | SEC_LINKER_CREATED
933 | SEC_READONLY)
934 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
935 return FALSE;
936 return TRUE;
937 }
938
939 /* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and
940 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
941 hash table. */
942
943 static bfd_boolean
944 sparc64_elf_create_dynamic_sections (dynobj, info)
945 bfd *dynobj;
946 struct bfd_link_info *info;
947 {
948 struct sparc64_elf_link_hash_table *htab;
949
950 htab = sparc64_elf_hash_table (info);
951 if (!htab->sgot && !create_got_section (dynobj, info))
952 return FALSE;
953
954 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
955 return FALSE;
956
957 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
958 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
959 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
960 if (!info->shared)
961 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
962
963 if (!htab->splt || !htab->srelplt || !htab->sdynbss
964 || (!info->shared && !htab->srelbss))
965 abort ();
966
967 return TRUE;
968 }
969
970 /* Copy the extra info we tack onto an elf_link_hash_entry. */
971
972 static void
973 sparc64_elf_copy_indirect_symbol (bed, dir, ind)
974 const struct elf_backend_data *bed;
975 struct elf_link_hash_entry *dir, *ind;
976 {
977 struct sparc64_elf_link_hash_entry *edir, *eind;
978
979 edir = (struct sparc64_elf_link_hash_entry *) dir;
980 eind = (struct sparc64_elf_link_hash_entry *) ind;
981
982 if (eind->dyn_relocs != NULL)
983 {
984 if (edir->dyn_relocs != NULL)
985 {
986 struct sparc64_elf_dyn_relocs **pp;
987 struct sparc64_elf_dyn_relocs *p;
988
989 if (ind->root.type == bfd_link_hash_indirect)
990 abort ();
991
992 /* Add reloc counts against the weak sym to the strong sym
993 list. Merge any entries against the same section. */
994 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
995 {
996 struct sparc64_elf_dyn_relocs *q;
997
998 for (q = edir->dyn_relocs; q != NULL; q = q->next)
999 if (q->sec == p->sec)
1000 {
1001 q->pc_count += p->pc_count;
1002 q->count += p->count;
1003 *pp = p->next;
1004 break;
1005 }
1006 if (q == NULL)
1007 pp = &p->next;
1008 }
1009 *pp = edir->dyn_relocs;
1010 }
1011
1012 edir->dyn_relocs = eind->dyn_relocs;
1013 eind->dyn_relocs = NULL;
1014 }
1015
1016 if (ind->root.type == bfd_link_hash_indirect
1017 && dir->got.refcount <= 0)
1018 {
1019 edir->tls_type = eind->tls_type;
1020 eind->tls_type = GOT_UNKNOWN;
1021 }
1022 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1023 }
1024
1025 static int
1026 sparc64_elf_tls_transition (info, r_type, is_local)
1027 struct bfd_link_info *info;
1028 int r_type;
1029 int is_local;
1030 {
1031 if (info->shared)
1032 return r_type;
1033
1034 switch (r_type)
1035 {
1036 case R_SPARC_TLS_GD_HI22:
1037 if (is_local)
1038 return R_SPARC_TLS_LE_HIX22;
1039 return R_SPARC_TLS_IE_HI22;
1040 case R_SPARC_TLS_GD_LO10:
1041 if (is_local)
1042 return R_SPARC_TLS_LE_LOX10;
1043 return R_SPARC_TLS_IE_LO10;
1044 case R_SPARC_TLS_IE_HI22:
1045 if (is_local)
1046 return R_SPARC_TLS_LE_HIX22;
1047 return r_type;
1048 case R_SPARC_TLS_IE_LO10:
1049 if (is_local)
1050 return R_SPARC_TLS_LE_LOX10;
1051 return r_type;
1052 case R_SPARC_TLS_LDM_HI22:
1053 return R_SPARC_TLS_LE_HIX22;
1054 case R_SPARC_TLS_LDM_LO10:
1055 return R_SPARC_TLS_LE_LOX10;
1056 }
1057
1058 return r_type;
1059 }
1060
1061 \f
1062 /* Utility for performing the standard initial work of an instruction
1063 relocation.
1064 *PRELOCATION will contain the relocated item.
1065 *PINSN will contain the instruction from the input stream.
1066 If the result is `bfd_reloc_other' the caller can continue with
1067 performing the relocation. Otherwise it must stop and return the
1068 value to its caller. */
1069
1070 static bfd_reloc_status_type
1071 init_insn_reloc (abfd,
1072 reloc_entry,
1073 symbol,
1074 data,
1075 input_section,
1076 output_bfd,
1077 prelocation,
1078 pinsn)
1079 bfd *abfd;
1080 arelent *reloc_entry;
1081 asymbol *symbol;
1082 PTR data;
1083 asection *input_section;
1084 bfd *output_bfd;
1085 bfd_vma *prelocation;
1086 bfd_vma *pinsn;
1087 {
1088 bfd_vma relocation;
1089 reloc_howto_type *howto = reloc_entry->howto;
1090
1091 if (output_bfd != (bfd *) NULL
1092 && (symbol->flags & BSF_SECTION_SYM) == 0
1093 && (! howto->partial_inplace
1094 || reloc_entry->addend == 0))
1095 {
1096 reloc_entry->address += input_section->output_offset;
1097 return bfd_reloc_ok;
1098 }
1099
1100 /* This works because partial_inplace is FALSE. */
1101 if (output_bfd != NULL)
1102 return bfd_reloc_continue;
1103
1104 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1105 return bfd_reloc_outofrange;
1106
1107 relocation = (symbol->value
1108 + symbol->section->output_section->vma
1109 + symbol->section->output_offset);
1110 relocation += reloc_entry->addend;
1111 if (howto->pc_relative)
1112 {
1113 relocation -= (input_section->output_section->vma
1114 + input_section->output_offset);
1115 relocation -= reloc_entry->address;
1116 }
1117
1118 *prelocation = relocation;
1119 *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1120 return bfd_reloc_other;
1121 }
1122
1123 /* For unsupported relocs. */
1124
1125 static bfd_reloc_status_type
1126 sparc_elf_notsup_reloc (abfd,
1127 reloc_entry,
1128 symbol,
1129 data,
1130 input_section,
1131 output_bfd,
1132 error_message)
1133 bfd *abfd ATTRIBUTE_UNUSED;
1134 arelent *reloc_entry ATTRIBUTE_UNUSED;
1135 asymbol *symbol ATTRIBUTE_UNUSED;
1136 PTR data ATTRIBUTE_UNUSED;
1137 asection *input_section ATTRIBUTE_UNUSED;
1138 bfd *output_bfd ATTRIBUTE_UNUSED;
1139 char **error_message ATTRIBUTE_UNUSED;
1140 {
1141 return bfd_reloc_notsupported;
1142 }
1143
1144 /* Handle the WDISP16 reloc. */
1145
1146 static bfd_reloc_status_type
1147 sparc_elf_wdisp16_reloc (abfd, reloc_entry, symbol, data, input_section,
1148 output_bfd, error_message)
1149 bfd *abfd;
1150 arelent *reloc_entry;
1151 asymbol *symbol;
1152 PTR data;
1153 asection *input_section;
1154 bfd *output_bfd;
1155 char **error_message ATTRIBUTE_UNUSED;
1156 {
1157 bfd_vma relocation;
1158 bfd_vma insn;
1159 bfd_reloc_status_type status;
1160
1161 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
1162 input_section, output_bfd, &relocation, &insn);
1163 if (status != bfd_reloc_other)
1164 return status;
1165
1166 insn &= ~ (bfd_vma) 0x303fff;
1167 insn |= (((relocation >> 2) & 0xc000) << 6) | ((relocation >> 2) & 0x3fff);
1168 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1169
1170 if ((bfd_signed_vma) relocation < - 0x40000
1171 || (bfd_signed_vma) relocation > 0x3ffff)
1172 return bfd_reloc_overflow;
1173 else
1174 return bfd_reloc_ok;
1175 }
1176
1177 /* Handle the HIX22 reloc. */
1178
1179 static bfd_reloc_status_type
1180 sparc_elf_hix22_reloc (abfd,
1181 reloc_entry,
1182 symbol,
1183 data,
1184 input_section,
1185 output_bfd,
1186 error_message)
1187 bfd *abfd;
1188 arelent *reloc_entry;
1189 asymbol *symbol;
1190 PTR data;
1191 asection *input_section;
1192 bfd *output_bfd;
1193 char **error_message ATTRIBUTE_UNUSED;
1194 {
1195 bfd_vma relocation;
1196 bfd_vma insn;
1197 bfd_reloc_status_type status;
1198
1199 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
1200 input_section, output_bfd, &relocation, &insn);
1201 if (status != bfd_reloc_other)
1202 return status;
1203
1204 relocation ^= MINUS_ONE;
1205 insn = (insn &~ (bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
1206 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1207
1208 if ((relocation & ~ (bfd_vma) 0xffffffff) != 0)
1209 return bfd_reloc_overflow;
1210 else
1211 return bfd_reloc_ok;
1212 }
1213
1214 /* Handle the LOX10 reloc. */
1215
1216 static bfd_reloc_status_type
1217 sparc_elf_lox10_reloc (abfd,
1218 reloc_entry,
1219 symbol,
1220 data,
1221 input_section,
1222 output_bfd,
1223 error_message)
1224 bfd *abfd;
1225 arelent *reloc_entry;
1226 asymbol *symbol;
1227 PTR data;
1228 asection *input_section;
1229 bfd *output_bfd;
1230 char **error_message ATTRIBUTE_UNUSED;
1231 {
1232 bfd_vma relocation;
1233 bfd_vma insn;
1234 bfd_reloc_status_type status;
1235
1236 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
1237 input_section, output_bfd, &relocation, &insn);
1238 if (status != bfd_reloc_other)
1239 return status;
1240
1241 insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff);
1242 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1243
1244 return bfd_reloc_ok;
1245 }
1246 \f
1247 /* PLT/GOT stuff */
1248
1249 /* Both the headers and the entries are icache aligned. */
1250 #define PLT_ENTRY_SIZE 32
1251 #define PLT_HEADER_SIZE (4 * PLT_ENTRY_SIZE)
1252 #define LARGE_PLT_THRESHOLD 32768
1253 #define GOT_RESERVED_ENTRIES 1
1254
1255 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1"
1256
1257 #define SPARC_NOP 0x01000000
1258
1259 static int
1260 sparc64_plt_entry_build (output_bfd, splt, offset, max, r_offset)
1261 bfd *output_bfd;
1262 asection *splt;
1263 bfd_vma offset;
1264 bfd_vma max;
1265 bfd_vma *r_offset;
1266 {
1267 unsigned char *entry = splt->contents + offset;
1268 const unsigned int nop = SPARC_NOP;
1269 int index;
1270
1271 if (offset < (LARGE_PLT_THRESHOLD * PLT_ENTRY_SIZE))
1272 {
1273 unsigned int sethi, ba;
1274
1275 *r_offset = offset;
1276
1277 index = (offset / PLT_ENTRY_SIZE);
1278
1279 sethi = 0x03000000 | (index * PLT_ENTRY_SIZE);
1280 ba = 0x30680000
1281 | (((splt->contents + PLT_ENTRY_SIZE) - (entry + 4)) / 4 & 0x7ffff);
1282
1283 bfd_put_32 (output_bfd, (bfd_vma) sethi, entry);
1284 bfd_put_32 (output_bfd, (bfd_vma) ba, entry + 4);
1285 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 8);
1286 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 12);
1287 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 16);
1288 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 20);
1289 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 24);
1290 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 28);
1291 }
1292 else
1293 {
1294 unsigned char *ptr;
1295 unsigned int ldx;
1296 int block, last_block, ofs, last_ofs, chunks_this_block;
1297 const int insn_chunk_size = (6 * 4);
1298 const int ptr_chunk_size = (1 * 8);
1299 const int entries_per_block = 160;
1300 const int block_size = entries_per_block * (insn_chunk_size
1301 + ptr_chunk_size);
1302
1303 /* Entries 32768 and higher are grouped into blocks of 160.
1304 The blocks are further subdivided into 160 sequences of
1305 6 instructions and 160 pointers. If a block does not require
1306 the full 160 entries, let's say it requires N, then there
1307 will be N sequences of 6 instructions and N pointers. */
1308
1309 offset -= (LARGE_PLT_THRESHOLD * PLT_ENTRY_SIZE);
1310 max -= (LARGE_PLT_THRESHOLD * PLT_ENTRY_SIZE);
1311
1312 block = offset / block_size;
1313 last_block = max / block_size;
1314 if (block != last_block)
1315 {
1316 chunks_this_block = 160;
1317 }
1318 else
1319 {
1320 last_ofs = max % block_size;
1321 chunks_this_block = last_ofs / (insn_chunk_size + ptr_chunk_size);
1322 }
1323
1324 ofs = offset % block_size;
1325
1326 index = (LARGE_PLT_THRESHOLD +
1327 (block * 160) +
1328 (ofs / insn_chunk_size));
1329
1330 ptr = splt->contents
1331 + (LARGE_PLT_THRESHOLD * PLT_ENTRY_SIZE)
1332 + (block * block_size)
1333 + (chunks_this_block * insn_chunk_size)
1334 + (ofs / insn_chunk_size) * ptr_chunk_size;
1335
1336 *r_offset = (bfd_vma) (ptr - splt->contents);
1337
1338 ldx = 0xc25be000 | ((ptr - (entry+4)) & 0x1fff);
1339
1340 /* mov %o7,%g5
1341 call .+8
1342 nop
1343 ldx [%o7+P],%g1
1344 jmpl %o7+%g1,%g1
1345 mov %g5,%o7 */
1346 bfd_put_32 (output_bfd, (bfd_vma) 0x8a10000f, entry);
1347 bfd_put_32 (output_bfd, (bfd_vma) 0x40000002, entry + 4);
1348 bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP, entry + 8);
1349 bfd_put_32 (output_bfd, (bfd_vma) ldx, entry + 12);
1350 bfd_put_32 (output_bfd, (bfd_vma) 0x83c3c001, entry + 16);
1351 bfd_put_32 (output_bfd, (bfd_vma) 0x9e100005, entry + 20);
1352
1353 bfd_put_64 (output_bfd, (bfd_vma) (splt->contents - (entry + 4)), ptr);
1354 }
1355
1356 return index - 4;
1357 }
1358 \f
1359 /* Look through the relocs for a section during the first phase, and
1360 allocate space in the global offset table or procedure linkage
1361 table. */
1362
1363 static bfd_boolean
1364 sparc64_elf_check_relocs (abfd, info, sec, relocs)
1365 bfd *abfd;
1366 struct bfd_link_info *info;
1367 asection *sec;
1368 const Elf_Internal_Rela *relocs;
1369 {
1370 struct sparc64_elf_link_hash_table *htab;
1371 Elf_Internal_Shdr *symtab_hdr;
1372 struct elf_link_hash_entry **sym_hashes;
1373 bfd_vma *local_got_offsets;
1374 const Elf_Internal_Rela *rel;
1375 const Elf_Internal_Rela *rel_end;
1376 asection *sreloc;
1377
1378 if (info->relocatable || !(sec->flags & SEC_ALLOC))
1379 return TRUE;
1380
1381 htab = sparc64_elf_hash_table (info);
1382 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1383 sym_hashes = elf_sym_hashes (abfd);
1384 local_got_offsets = elf_local_got_offsets (abfd);
1385
1386 sreloc = NULL;
1387
1388 rel_end = relocs + NUM_SHDR_ENTRIES (& elf_section_data (sec)->rel_hdr);
1389 for (rel = relocs; rel < rel_end; rel++)
1390 {
1391 unsigned int r_type;
1392 unsigned long r_symndx;
1393 struct elf_link_hash_entry *h;
1394
1395 r_symndx = ELF64_R_SYM (rel->r_info);
1396 r_type = ELF64_R_TYPE_ID (rel->r_info);
1397
1398 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1399 {
1400 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1401 abfd, r_symndx);
1402 return FALSE;
1403 }
1404
1405 if (r_symndx < symtab_hdr->sh_info)
1406 h = NULL;
1407 else
1408 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1409
1410 r_type = sparc64_elf_tls_transition (info, r_type, h == NULL);
1411 switch (r_type)
1412 {
1413 case R_SPARC_TLS_LDM_HI22:
1414 case R_SPARC_TLS_LDM_LO10:
1415 htab->tls_ldm_got.refcount += 1;
1416 break;
1417
1418 case R_SPARC_TLS_LE_HIX22:
1419 case R_SPARC_TLS_LE_LOX10:
1420 if (info->shared)
1421 goto r_sparc_plt32;
1422 break;
1423
1424 case R_SPARC_TLS_IE_HI22:
1425 case R_SPARC_TLS_IE_LO10:
1426 if (info->shared)
1427 info->flags |= DF_STATIC_TLS;
1428 /* Fall through */
1429
1430 case R_SPARC_GOT10:
1431 case R_SPARC_GOT13:
1432 case R_SPARC_GOT22:
1433 case R_SPARC_TLS_GD_HI22:
1434 case R_SPARC_TLS_GD_LO10:
1435 /* This symbol requires a global offset table entry. */
1436 {
1437 int tls_type, old_tls_type;
1438
1439 switch (r_type)
1440 {
1441 default:
1442 case R_SPARC_GOT10:
1443 case R_SPARC_GOT13:
1444 case R_SPARC_GOT22:
1445 tls_type = GOT_NORMAL;
1446 break;
1447 case R_SPARC_TLS_GD_HI22:
1448 case R_SPARC_TLS_GD_LO10:
1449 tls_type = GOT_TLS_GD;
1450 break;
1451 case R_SPARC_TLS_IE_HI22:
1452 case R_SPARC_TLS_IE_LO10:
1453 tls_type = GOT_TLS_IE;
1454 break;
1455 }
1456
1457 if (h != NULL)
1458 {
1459 h->got.refcount += 1;
1460 old_tls_type = sparc64_elf_hash_entry(h)->tls_type;
1461 }
1462 else
1463 {
1464 bfd_signed_vma *local_got_refcounts;
1465
1466 /* This is a global offset table entry for a local symbol. */
1467 local_got_refcounts = elf_local_got_refcounts (abfd);
1468 if (local_got_refcounts == NULL)
1469 {
1470 bfd_size_type size;
1471
1472 size = symtab_hdr->sh_info;
1473 size *= (sizeof (bfd_signed_vma) + sizeof(char));
1474 local_got_refcounts = ((bfd_signed_vma *)
1475 bfd_zalloc (abfd, size));
1476 if (local_got_refcounts == NULL)
1477 return FALSE;
1478 elf_local_got_refcounts (abfd) = local_got_refcounts;
1479 sparc64_elf_local_got_tls_type (abfd)
1480 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1481 }
1482 local_got_refcounts[r_symndx] += 1;
1483 old_tls_type = sparc64_elf_local_got_tls_type (abfd) [r_symndx];
1484 }
1485
1486 /* If a TLS symbol is accessed using IE at least once,
1487 there is no point to use dynamic model for it. */
1488 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1489 && (old_tls_type != GOT_TLS_GD
1490 || tls_type != GOT_TLS_IE))
1491 {
1492 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
1493 tls_type = old_tls_type;
1494 else
1495 {
1496 (*_bfd_error_handler)
1497 (_("%B: `%s' accessed both as normal and thread local symbol"),
1498 abfd, h ? h->root.root.string : "<local>");
1499 return FALSE;
1500 }
1501 }
1502
1503 if (old_tls_type != tls_type)
1504 {
1505 if (h != NULL)
1506 sparc64_elf_hash_entry (h)->tls_type = tls_type;
1507 else
1508 sparc64_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
1509 }
1510 }
1511
1512 if (htab->sgot == NULL)
1513 {
1514 if (htab->elf.dynobj == NULL)
1515 htab->elf.dynobj = abfd;
1516 if (!create_got_section (htab->elf.dynobj, info))
1517 return FALSE;
1518 }
1519 break;
1520
1521 case R_SPARC_TLS_GD_CALL:
1522 case R_SPARC_TLS_LDM_CALL:
1523 if (info->shared)
1524 {
1525 /* These are basically R_SPARC_TLS_WPLT30 relocs against
1526 __tls_get_addr. */
1527 struct bfd_link_hash_entry *bh = NULL;
1528 if (! _bfd_generic_link_add_one_symbol (info, abfd,
1529 "__tls_get_addr", 0,
1530 bfd_und_section_ptr, 0,
1531 NULL, FALSE, FALSE,
1532 &bh))
1533 return FALSE;
1534 h = (struct elf_link_hash_entry *) bh;
1535 }
1536 else
1537 break;
1538 /* Fall through */
1539
1540 case R_SPARC_PLT32:
1541 case R_SPARC_WPLT30:
1542 case R_SPARC_HIPLT22:
1543 case R_SPARC_LOPLT10:
1544 case R_SPARC_PCPLT32:
1545 case R_SPARC_PCPLT22:
1546 case R_SPARC_PCPLT10:
1547 case R_SPARC_PLT64:
1548 /* This symbol requires a procedure linkage table entry. We
1549 actually build the entry in adjust_dynamic_symbol,
1550 because this might be a case of linking PIC code without
1551 linking in any dynamic objects, in which case we don't
1552 need to generate a procedure linkage table after all. */
1553
1554 if (h == NULL)
1555 {
1556 /* It does not make sense to have a procedure linkage
1557 table entry for a local symbol. */
1558 bfd_set_error (bfd_error_bad_value);
1559 return FALSE;
1560 }
1561
1562 h->needs_plt = 1;
1563
1564 if (ELF64_R_TYPE_ID (rel->r_info) == R_SPARC_PLT32
1565 || ELF64_R_TYPE_ID (rel->r_info) == R_SPARC_PLT64)
1566 goto r_sparc_plt32;
1567 h->plt.refcount += 1;
1568 break;
1569
1570 case R_SPARC_PC10:
1571 case R_SPARC_PC22:
1572 case R_SPARC_PC_HH22:
1573 case R_SPARC_PC_HM10:
1574 case R_SPARC_PC_LM22:
1575 if (h != NULL)
1576 h->non_got_ref = 1;
1577
1578 if (h != NULL
1579 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1580 break;
1581 /* Fall through. */
1582
1583 case R_SPARC_DISP8:
1584 case R_SPARC_DISP16:
1585 case R_SPARC_DISP32:
1586 case R_SPARC_DISP64:
1587 case R_SPARC_WDISP30:
1588 case R_SPARC_WDISP22:
1589 case R_SPARC_WDISP19:
1590 case R_SPARC_WDISP16:
1591 case R_SPARC_8:
1592 case R_SPARC_16:
1593 case R_SPARC_32:
1594 case R_SPARC_HI22:
1595 case R_SPARC_22:
1596 case R_SPARC_13:
1597 case R_SPARC_LO10:
1598 case R_SPARC_UA16:
1599 case R_SPARC_UA32:
1600 case R_SPARC_10:
1601 case R_SPARC_11:
1602 case R_SPARC_64:
1603 case R_SPARC_OLO10:
1604 case R_SPARC_HH22:
1605 case R_SPARC_HM10:
1606 case R_SPARC_LM22:
1607 case R_SPARC_7:
1608 case R_SPARC_5:
1609 case R_SPARC_6:
1610 case R_SPARC_HIX22:
1611 case R_SPARC_LOX10:
1612 case R_SPARC_H44:
1613 case R_SPARC_M44:
1614 case R_SPARC_L44:
1615 case R_SPARC_UA64:
1616 if (h != NULL)
1617 h->non_got_ref = 1;
1618
1619 r_sparc_plt32:
1620 if (h != NULL && !info->shared)
1621 {
1622 /* We may need a .plt entry if the function this reloc
1623 refers to is in a shared lib. */
1624 h->plt.refcount += 1;
1625 }
1626
1627 /* If we are creating a shared library, and this is a reloc
1628 against a global symbol, or a non PC relative reloc
1629 against a local symbol, then we need to copy the reloc
1630 into the shared library. However, if we are linking with
1631 -Bsymbolic, we do not need to copy a reloc against a
1632 global symbol which is defined in an object we are
1633 including in the link (i.e., DEF_REGULAR is set). At
1634 this point we have not seen all the input files, so it is
1635 possible that DEF_REGULAR is not set now but will be set
1636 later (it is never cleared). In case of a weak definition,
1637 DEF_REGULAR may be cleared later by a strong definition in
1638 a shared library. We account for that possibility below by
1639 storing information in the relocs_copied field of the hash
1640 table entry. A similar situation occurs when creating
1641 shared libraries and symbol visibility changes render the
1642 symbol local.
1643
1644 If on the other hand, we are creating an executable, we
1645 may need to keep relocations for symbols satisfied by a
1646 dynamic library if we manage to avoid copy relocs for the
1647 symbol. */
1648 if ((info->shared
1649 && (sec->flags & SEC_ALLOC) != 0
1650 && (! sparc64_elf_howto_table[r_type].pc_relative
1651 || (h != NULL
1652 && (! info->symbolic
1653 || h->root.type == bfd_link_hash_defweak
1654 || !h->def_regular))))
1655 || (!info->shared
1656 && (sec->flags & SEC_ALLOC) != 0
1657 && h != NULL
1658 && (h->root.type == bfd_link_hash_defweak
1659 || !h->def_regular)))
1660 {
1661 struct sparc64_elf_dyn_relocs *p;
1662 struct sparc64_elf_dyn_relocs **head;
1663
1664 /* When creating a shared object, we must copy these
1665 relocs into the output file. We create a reloc
1666 section in dynobj and make room for the reloc. */
1667 if (sreloc == NULL)
1668 {
1669 const char *name;
1670 bfd *dynobj;
1671
1672 name = (bfd_elf_string_from_elf_section
1673 (abfd,
1674 elf_elfheader (abfd)->e_shstrndx,
1675 elf_section_data (sec)->rel_hdr.sh_name));
1676 if (name == NULL)
1677 return FALSE;
1678
1679 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1680 && strcmp (bfd_get_section_name (abfd, sec),
1681 name + 5) == 0);
1682
1683 if (htab->elf.dynobj == NULL)
1684 htab->elf.dynobj = abfd;
1685 dynobj = htab->elf.dynobj;
1686
1687 sreloc = bfd_get_section_by_name (dynobj, name);
1688 if (sreloc == NULL)
1689 {
1690 flagword flags;
1691
1692 sreloc = bfd_make_section (dynobj, name);
1693 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1694 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1695 if ((sec->flags & SEC_ALLOC) != 0)
1696 flags |= SEC_ALLOC | SEC_LOAD;
1697 if (sreloc == NULL
1698 || ! bfd_set_section_flags (dynobj, sreloc, flags)
1699 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
1700 return FALSE;
1701 }
1702 elf_section_data (sec)->sreloc = sreloc;
1703 }
1704
1705 /* If this is a global symbol, we count the number of
1706 relocations we need for this symbol. */
1707 if (h != NULL)
1708 head = &((struct sparc64_elf_link_hash_entry *) h)->dyn_relocs;
1709 else
1710 {
1711 /* Track dynamic relocs needed for local syms too.
1712 We really need local syms available to do this
1713 easily. Oh well. */
1714
1715 asection *s;
1716 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1717 sec, r_symndx);
1718 if (s == NULL)
1719 return FALSE;
1720
1721 head = ((struct sparc64_elf_dyn_relocs **)
1722 &elf_section_data (s)->local_dynrel);
1723 }
1724
1725 p = *head;
1726 if (p == NULL || p->sec != sec)
1727 {
1728 bfd_size_type amt = sizeof *p;
1729 p = ((struct sparc64_elf_dyn_relocs *)
1730 bfd_alloc (htab->elf.dynobj, amt));
1731 if (p == NULL)
1732 return FALSE;
1733 p->next = *head;
1734 *head = p;
1735 p->sec = sec;
1736 p->count = 0;
1737 p->pc_count = 0;
1738 }
1739
1740 p->count += 1;
1741 if (sparc64_elf_howto_table[r_type].pc_relative)
1742 p->pc_count += 1;
1743 }
1744
1745 break;
1746
1747 case R_SPARC_REGISTER:
1748 /* Nothing to do. */
1749 break;
1750
1751 default:
1752 break;
1753 }
1754 }
1755
1756 return TRUE;
1757 }
1758
1759 /* Hook called by the linker routine which adds symbols from an object
1760 file. We use it for STT_REGISTER symbols. */
1761
1762 static bfd_boolean
1763 sparc64_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1764 bfd *abfd;
1765 struct bfd_link_info *info;
1766 Elf_Internal_Sym *sym;
1767 const char **namep;
1768 flagword *flagsp ATTRIBUTE_UNUSED;
1769 asection **secp ATTRIBUTE_UNUSED;
1770 bfd_vma *valp ATTRIBUTE_UNUSED;
1771 {
1772 static const char *const stt_types[] = { "NOTYPE", "OBJECT", "FUNCTION" };
1773
1774 if (ELF_ST_TYPE (sym->st_info) == STT_REGISTER)
1775 {
1776 int reg;
1777 struct sparc64_elf_app_reg *p;
1778
1779 reg = (int)sym->st_value;
1780 switch (reg & ~1)
1781 {
1782 case 2: reg -= 2; break;
1783 case 6: reg -= 4; break;
1784 default:
1785 (*_bfd_error_handler)
1786 (_("%B: Only registers %%g[2367] can be declared using STT_REGISTER"),
1787 abfd);
1788 return FALSE;
1789 }
1790
1791 if (info->hash->creator != abfd->xvec
1792 || (abfd->flags & DYNAMIC) != 0)
1793 {
1794 /* STT_REGISTER only works when linking an elf64_sparc object.
1795 If STT_REGISTER comes from a dynamic object, don't put it into
1796 the output bfd. The dynamic linker will recheck it. */
1797 *namep = NULL;
1798 return TRUE;
1799 }
1800
1801 p = sparc64_elf_hash_table(info)->app_regs + reg;
1802
1803 if (p->name != NULL && strcmp (p->name, *namep))
1804 {
1805 (*_bfd_error_handler)
1806 (_("Register %%g%d used incompatibly: %s in %B, previously %s in %B"),
1807 abfd, p->abfd, (int) sym->st_value,
1808 **namep ? *namep : "#scratch",
1809 *p->name ? p->name : "#scratch");
1810 return FALSE;
1811 }
1812
1813 if (p->name == NULL)
1814 {
1815 if (**namep)
1816 {
1817 struct elf_link_hash_entry *h;
1818
1819 h = (struct elf_link_hash_entry *)
1820 bfd_link_hash_lookup (info->hash, *namep, FALSE, FALSE, FALSE);
1821
1822 if (h != NULL)
1823 {
1824 unsigned char type = h->type;
1825
1826 if (type > STT_FUNC)
1827 type = 0;
1828 (*_bfd_error_handler)
1829 (_("Symbol `%s' has differing types: REGISTER in %B, previously %s in %B"),
1830 abfd, p->abfd, *namep, stt_types[type]);
1831 return FALSE;
1832 }
1833
1834 p->name = bfd_hash_allocate (&info->hash->table,
1835 strlen (*namep) + 1);
1836 if (!p->name)
1837 return FALSE;
1838
1839 strcpy (p->name, *namep);
1840 }
1841 else
1842 p->name = "";
1843 p->bind = ELF_ST_BIND (sym->st_info);
1844 p->abfd = abfd;
1845 p->shndx = sym->st_shndx;
1846 }
1847 else
1848 {
1849 if (p->bind == STB_WEAK
1850 && ELF_ST_BIND (sym->st_info) == STB_GLOBAL)
1851 {
1852 p->bind = STB_GLOBAL;
1853 p->abfd = abfd;
1854 }
1855 }
1856 *namep = NULL;
1857 return TRUE;
1858 }
1859 else if (*namep && **namep
1860 && info->hash->creator == abfd->xvec)
1861 {
1862 int i;
1863 struct sparc64_elf_app_reg *p;
1864
1865 p = sparc64_elf_hash_table(info)->app_regs;
1866 for (i = 0; i < 4; i++, p++)
1867 if (p->name != NULL && ! strcmp (p->name, *namep))
1868 {
1869 unsigned char type = ELF_ST_TYPE (sym->st_info);
1870
1871 if (type > STT_FUNC)
1872 type = 0;
1873 (*_bfd_error_handler)
1874 (_("Symbol `%s' has differing types: %s in %B, previously REGISTER in %B"),
1875 abfd, p->abfd, *namep, stt_types[type]);
1876 return FALSE;
1877 }
1878 }
1879 return TRUE;
1880 }
1881
1882 /* This function takes care of emitting STT_REGISTER symbols
1883 which we cannot easily keep in the symbol hash table. */
1884
1885 static bfd_boolean
1886 sparc64_elf_output_arch_syms (output_bfd, info, finfo, func)
1887 bfd *output_bfd ATTRIBUTE_UNUSED;
1888 struct bfd_link_info *info;
1889 PTR finfo;
1890 bfd_boolean (*func)
1891 PARAMS ((PTR, const char *, Elf_Internal_Sym *, asection *,
1892 struct elf_link_hash_entry *));
1893 {
1894 int reg;
1895 struct sparc64_elf_app_reg *app_regs =
1896 sparc64_elf_hash_table(info)->app_regs;
1897 Elf_Internal_Sym sym;
1898
1899 /* We arranged in size_dynamic_sections to put the STT_REGISTER entries
1900 at the end of the dynlocal list, so they came at the end of the local
1901 symbols in the symtab. Except that they aren't STB_LOCAL, so we need
1902 to back up symtab->sh_info. */
1903 if (elf_hash_table (info)->dynlocal)
1904 {
1905 bfd * dynobj = elf_hash_table (info)->dynobj;
1906 asection *dynsymsec = bfd_get_section_by_name (dynobj, ".dynsym");
1907 struct elf_link_local_dynamic_entry *e;
1908
1909 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
1910 if (e->input_indx == -1)
1911 break;
1912 if (e)
1913 {
1914 elf_section_data (dynsymsec->output_section)->this_hdr.sh_info
1915 = e->dynindx;
1916 }
1917 }
1918
1919 if (info->strip == strip_all)
1920 return TRUE;
1921
1922 for (reg = 0; reg < 4; reg++)
1923 if (app_regs [reg].name != NULL)
1924 {
1925 if (info->strip == strip_some
1926 && bfd_hash_lookup (info->keep_hash,
1927 app_regs [reg].name,
1928 FALSE, FALSE) == NULL)
1929 continue;
1930
1931 sym.st_value = reg < 2 ? reg + 2 : reg + 4;
1932 sym.st_size = 0;
1933 sym.st_other = 0;
1934 sym.st_info = ELF_ST_INFO (app_regs [reg].bind, STT_REGISTER);
1935 sym.st_shndx = app_regs [reg].shndx;
1936 if (! (*func) (finfo, app_regs [reg].name, &sym,
1937 sym.st_shndx == SHN_ABS
1938 ? bfd_abs_section_ptr : bfd_und_section_ptr,
1939 NULL))
1940 return FALSE;
1941 }
1942
1943 return TRUE;
1944 }
1945
1946 static int
1947 sparc64_elf_get_symbol_type (elf_sym, type)
1948 Elf_Internal_Sym * elf_sym;
1949 int type;
1950 {
1951 if (ELF_ST_TYPE (elf_sym->st_info) == STT_REGISTER)
1952 return STT_REGISTER;
1953 else
1954 return type;
1955 }
1956
1957 /* A STB_GLOBAL,STT_REGISTER symbol should be BSF_GLOBAL
1958 even in SHN_UNDEF section. */
1959
1960 static void
1961 sparc64_elf_symbol_processing (abfd, asym)
1962 bfd *abfd ATTRIBUTE_UNUSED;
1963 asymbol *asym;
1964 {
1965 elf_symbol_type *elfsym;
1966
1967 elfsym = (elf_symbol_type *) asym;
1968 if (elfsym->internal_elf_sym.st_info
1969 == ELF_ST_INFO (STB_GLOBAL, STT_REGISTER))
1970 {
1971 asym->flags |= BSF_GLOBAL;
1972 }
1973 }
1974
1975 static asection *
1976 sparc64_elf_gc_mark_hook (sec, info, rel, h, sym)
1977 asection *sec;
1978 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1979 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED;
1980 struct elf_link_hash_entry *h;
1981 Elf_Internal_Sym *sym;
1982 {
1983 if (h != NULL)
1984 {
1985 switch (h->root.type)
1986 {
1987 case bfd_link_hash_defined:
1988 case bfd_link_hash_defweak:
1989 return h->root.u.def.section;
1990
1991 case bfd_link_hash_common:
1992 return h->root.u.c.p->section;
1993
1994 default:
1995 break;
1996 }
1997 }
1998 else
1999 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2000
2001 return NULL;
2002 }
2003
2004 /* Update the got entry reference counts for the section being removed. */
2005 static bfd_boolean
2006 sparc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
2007 bfd *abfd;
2008 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2009 asection *sec;
2010 const Elf_Internal_Rela *relocs;
2011 {
2012 Elf_Internal_Shdr *symtab_hdr;
2013 struct elf_link_hash_entry **sym_hashes;
2014 bfd_signed_vma *local_got_refcounts;
2015 const Elf_Internal_Rela *rel, *relend;
2016
2017 elf_section_data (sec)->local_dynrel = NULL;
2018
2019 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2020 sym_hashes = elf_sym_hashes (abfd);
2021 local_got_refcounts = elf_local_got_refcounts (abfd);
2022
2023 relend = relocs + sec->reloc_count;
2024 for (rel = relocs; rel < relend; rel++)
2025 {
2026 unsigned long r_symndx;
2027 unsigned int r_type;
2028 struct elf_link_hash_entry *h = NULL;
2029
2030 r_symndx = ELF64_R_SYM (rel->r_info);
2031 if (r_symndx >= symtab_hdr->sh_info)
2032 {
2033 struct sparc64_elf_link_hash_entry *eh;
2034 struct sparc64_elf_dyn_relocs **pp;
2035 struct sparc64_elf_dyn_relocs *p;
2036
2037 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2038 while (h->root.type == bfd_link_hash_indirect
2039 || h->root.type == bfd_link_hash_warning)
2040 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2041 eh = (struct sparc64_elf_link_hash_entry *) h;
2042 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
2043 if (p->sec == sec)
2044 {
2045 /* Everything must go for SEC. */
2046 *pp = p->next;
2047 break;
2048 }
2049 }
2050
2051 r_type = ELF64_R_TYPE_ID (rel->r_info);
2052 r_type = sparc64_elf_tls_transition (info, r_type, h != NULL);
2053 switch (r_type)
2054 {
2055 case R_SPARC_TLS_LDM_HI22:
2056 case R_SPARC_TLS_LDM_LO10:
2057 if (sparc64_elf_hash_table (info)->tls_ldm_got.refcount > 0)
2058 sparc64_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
2059 break;
2060
2061 case R_SPARC_TLS_GD_HI22:
2062 case R_SPARC_TLS_GD_LO10:
2063 case R_SPARC_TLS_IE_HI22:
2064 case R_SPARC_TLS_IE_LO10:
2065 case R_SPARC_GOT10:
2066 case R_SPARC_GOT13:
2067 case R_SPARC_GOT22:
2068 if (h != NULL)
2069 {
2070 if (h->got.refcount > 0)
2071 h->got.refcount--;
2072 }
2073 else
2074 {
2075 if (local_got_refcounts[r_symndx] > 0)
2076 local_got_refcounts[r_symndx]--;
2077 }
2078 break;
2079
2080 case R_SPARC_PC10:
2081 case R_SPARC_PC22:
2082 case R_SPARC_PC_HH22:
2083 case R_SPARC_PC_HM10:
2084 case R_SPARC_PC_LM22:
2085 if (h != NULL
2086 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2087 break;
2088 /* Fall through. */
2089
2090 case R_SPARC_DISP8:
2091 case R_SPARC_DISP16:
2092 case R_SPARC_DISP32:
2093 case R_SPARC_DISP64:
2094 case R_SPARC_WDISP30:
2095 case R_SPARC_WDISP22:
2096 case R_SPARC_WDISP19:
2097 case R_SPARC_WDISP16:
2098 case R_SPARC_8:
2099 case R_SPARC_16:
2100 case R_SPARC_32:
2101 case R_SPARC_HI22:
2102 case R_SPARC_22:
2103 case R_SPARC_13:
2104 case R_SPARC_LO10:
2105 case R_SPARC_UA16:
2106 case R_SPARC_UA32:
2107 case R_SPARC_PLT32:
2108 case R_SPARC_10:
2109 case R_SPARC_11:
2110 case R_SPARC_64:
2111 case R_SPARC_OLO10:
2112 case R_SPARC_HH22:
2113 case R_SPARC_HM10:
2114 case R_SPARC_LM22:
2115 case R_SPARC_7:
2116 case R_SPARC_5:
2117 case R_SPARC_6:
2118 case R_SPARC_HIX22:
2119 case R_SPARC_LOX10:
2120 case R_SPARC_H44:
2121 case R_SPARC_M44:
2122 case R_SPARC_L44:
2123 case R_SPARC_UA64:
2124 if (info->shared)
2125 break;
2126 /* Fall through. */
2127
2128 case R_SPARC_WPLT30:
2129 if (h != NULL)
2130 {
2131 if (h->plt.refcount > 0)
2132 h->plt.refcount--;
2133 }
2134 break;
2135
2136 default:
2137 break;
2138 }
2139 }
2140
2141 return TRUE;
2142 }
2143
2144 /* Adjust a symbol defined by a dynamic object and referenced by a
2145 regular object. The current definition is in some section of the
2146 dynamic object, but we're not including those sections. We have to
2147 change the definition to something the rest of the link can
2148 understand. */
2149
2150 static bfd_boolean
2151 sparc64_elf_adjust_dynamic_symbol (info, h)
2152 struct bfd_link_info *info;
2153 struct elf_link_hash_entry *h;
2154 {
2155 struct sparc64_elf_link_hash_table *htab;
2156 struct sparc64_elf_link_hash_entry * eh;
2157 struct sparc64_elf_dyn_relocs *p;
2158 asection *s;
2159 unsigned int power_of_two;
2160
2161 htab = sparc64_elf_hash_table (info);
2162
2163 /* Make sure we know what is going on here. */
2164 BFD_ASSERT (htab->elf.dynobj != NULL
2165 && (h->needs_plt
2166 || h->u.weakdef != NULL
2167 || (h->def_dynamic
2168 && h->ref_regular
2169 && !h->def_regular)));
2170
2171 /* If this is a function, put it in the procedure linkage table. We
2172 will fill in the contents of the procedure linkage table later
2173 (although we could actually do it here). The STT_NOTYPE
2174 condition is a hack specifically for the Oracle libraries
2175 delivered for Solaris; for some inexplicable reason, they define
2176 some of their functions as STT_NOTYPE when they really should be
2177 STT_FUNC. */
2178 if (h->type == STT_FUNC
2179 || h->needs_plt
2180 || (h->type == STT_NOTYPE
2181 && (h->root.type == bfd_link_hash_defined
2182 || h->root.type == bfd_link_hash_defweak)
2183 && (h->root.u.def.section->flags & SEC_CODE) != 0))
2184 {
2185 if (h->plt.refcount <= 0
2186 || (! info->shared
2187 && !h->def_dynamic
2188 && !h->ref_dynamic
2189 && h->root.type != bfd_link_hash_undefweak
2190 && h->root.type != bfd_link_hash_undefined))
2191 {
2192 /* This case can occur if we saw a WPLT30 reloc in an input
2193 file, but the symbol was never referred to by a dynamic
2194 object, or if all references were garbage collected. In
2195 such a case, we don't actually need to build a procedure
2196 linkage table, and we can just do a WDISP30 reloc instead. */
2197 h->plt.offset = (bfd_vma) -1;
2198 h->needs_plt = 0;
2199 }
2200
2201 return TRUE;
2202 }
2203 else
2204 h->plt.offset = (bfd_vma) -1;
2205
2206 /* If this is a weak symbol, and there is a real definition, the
2207 processor independent code will have arranged for us to see the
2208 real definition first, and we can just use the same value. */
2209 if (h->u.weakdef != NULL)
2210 {
2211 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2212 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2213 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2214 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2215 return TRUE;
2216 }
2217
2218 /* This is a reference to a symbol defined by a dynamic object which
2219 is not a function. */
2220
2221 /* If we are creating a shared library, we must presume that the
2222 only references to the symbol are via the global offset table.
2223 For such cases we need not do anything here; the relocations will
2224 be handled correctly by relocate_section. */
2225 if (info->shared)
2226 return TRUE;
2227
2228 /* If there are no references to this symbol that do not use the
2229 GOT, we don't need to generate a copy reloc. */
2230 if (!h->non_got_ref)
2231 return TRUE;
2232
2233 eh = (struct sparc64_elf_link_hash_entry *) h;
2234 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2235 {
2236 s = p->sec->output_section;
2237 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2238 break;
2239 }
2240
2241 /* If we didn't find any dynamic relocs in read-only sections, then
2242 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2243 if (p == NULL)
2244 {
2245 h->non_got_ref = 0;
2246 return TRUE;
2247 }
2248
2249 /* We must allocate the symbol in our .dynbss section, which will
2250 become part of the .bss section of the executable. There will be
2251 an entry for this symbol in the .dynsym section. The dynamic
2252 object will contain position independent code, so all references
2253 from the dynamic object to this symbol will go through the global
2254 offset table. The dynamic linker will use the .dynsym entry to
2255 determine the address it must put in the global offset table, so
2256 both the dynamic object and the regular object will refer to the
2257 same memory location for the variable. */
2258
2259 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
2260 to copy the initial value out of the dynamic object and into the
2261 runtime process image. We need to remember the offset into the
2262 .rel.bss section we are going to use. */
2263 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2264 {
2265 htab->srelbss->size += sizeof (Elf64_External_Rela);
2266 h->needs_copy = 1;
2267 }
2268
2269 /* We need to figure out the alignment required for this symbol. I
2270 have no idea how ELF linkers handle this. 16-bytes is the size
2271 of the largest type that requires hard alignment -- long double. */
2272 power_of_two = bfd_log2 (h->size);
2273 if (power_of_two > 4)
2274 power_of_two = 4;
2275
2276 /* Apply the required alignment. */
2277 s = htab->sdynbss;
2278 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2279 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2280 {
2281 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2282 return FALSE;
2283 }
2284
2285 /* Define the symbol as being at this point in the section. */
2286 h->root.u.def.section = s;
2287 h->root.u.def.value = s->size;
2288
2289 /* Increment the section size to make room for the symbol. */
2290 s->size += h->size;
2291
2292 return TRUE;
2293 }
2294
2295 /* Allocate space in .plt, .got and associated reloc sections for
2296 dynamic relocs. */
2297
2298 static bfd_boolean
2299 allocate_dynrelocs (h, inf)
2300 struct elf_link_hash_entry *h;
2301 PTR inf;
2302 {
2303 struct bfd_link_info *info;
2304 struct sparc64_elf_link_hash_table *htab;
2305 struct sparc64_elf_link_hash_entry *eh;
2306 struct sparc64_elf_dyn_relocs *p;
2307
2308 if (h->root.type == bfd_link_hash_indirect)
2309 return TRUE;
2310
2311 if (h->root.type == bfd_link_hash_warning)
2312 /* When warning symbols are created, they **replace** the "real"
2313 entry in the hash table, thus we never get to see the real
2314 symbol in a hash traversal. So look at it now. */
2315 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2316
2317 info = (struct bfd_link_info *) inf;
2318 htab = sparc64_elf_hash_table (info);
2319
2320 if (htab->elf.dynamic_sections_created
2321 && h->plt.refcount > 0)
2322 {
2323 /* Make sure this symbol is output as a dynamic symbol.
2324 Undefined weak syms won't yet be marked as dynamic. */
2325 if (h->dynindx == -1
2326 && !h->forced_local)
2327 {
2328 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2329 return FALSE;
2330 }
2331
2332 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2333 {
2334 asection *s = htab->splt;
2335
2336 /* The first four bit in .plt is reserved. */
2337 if (s->size == 0)
2338 s->size = PLT_HEADER_SIZE;
2339
2340 /* The procedure linkage table size is bounded by the magnitude
2341 of the offset we can describe in the entry. */
2342 if (s->size >= (bfd_vma)1 << 32)
2343 {
2344 bfd_set_error (bfd_error_bad_value);
2345 return FALSE;
2346 }
2347
2348 if (s->size >= LARGE_PLT_THRESHOLD * PLT_ENTRY_SIZE)
2349 {
2350 bfd_vma off = s->size - LARGE_PLT_THRESHOLD * PLT_ENTRY_SIZE;
2351
2352
2353 off = (off % (160 * PLT_ENTRY_SIZE)) / PLT_ENTRY_SIZE;
2354
2355 h->plt.offset = (s->size - (off * 8));
2356 }
2357 else
2358 h->plt.offset = s->size;
2359
2360 /* If this symbol is not defined in a regular file, and we are
2361 not generating a shared library, then set the symbol to this
2362 location in the .plt. This is required to make function
2363 pointers compare as equal between the normal executable and
2364 the shared library. */
2365 if (! info->shared
2366 && !h->def_regular)
2367 {
2368 h->root.u.def.section = s;
2369 h->root.u.def.value = h->plt.offset;
2370 }
2371
2372 /* Make room for this entry. */
2373 s->size += PLT_ENTRY_SIZE;
2374
2375 /* We also need to make an entry in the .rela.plt section. */
2376 htab->srelplt->size += sizeof (Elf64_External_Rela);
2377 }
2378 else
2379 {
2380 h->plt.offset = (bfd_vma) -1;
2381 h->needs_plt = 0;
2382 }
2383 }
2384 else
2385 {
2386 h->plt.offset = (bfd_vma) -1;
2387 h->needs_plt = 0;
2388 }
2389
2390 /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary,
2391 make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry. */
2392 if (h->got.refcount > 0
2393 && !info->shared
2394 && h->dynindx == -1
2395 && sparc64_elf_hash_entry(h)->tls_type == GOT_TLS_IE)
2396 h->got.offset = (bfd_vma) -1;
2397 else if (h->got.refcount > 0)
2398 {
2399 asection *s;
2400 bfd_boolean dyn;
2401 int tls_type = sparc64_elf_hash_entry(h)->tls_type;
2402
2403 /* Make sure this symbol is output as a dynamic symbol.
2404 Undefined weak syms won't yet be marked as dynamic. */
2405 if (h->dynindx == -1
2406 && !h->forced_local)
2407 {
2408 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2409 return FALSE;
2410 }
2411
2412 s = htab->sgot;
2413 h->got.offset = s->size;
2414 s->size += 8;
2415 /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots. */
2416 if (tls_type == GOT_TLS_GD)
2417 s->size += 8;
2418 dyn = htab->elf.dynamic_sections_created;
2419 /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation,
2420 R_SPARC_TLS_GD_{HI22,LO10} needs one if local symbol and two if
2421 global. */
2422 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
2423 || tls_type == GOT_TLS_IE)
2424 htab->srelgot->size += sizeof (Elf64_External_Rela);
2425 else if (tls_type == GOT_TLS_GD)
2426 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
2427 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2428 htab->srelgot->size += sizeof (Elf64_External_Rela);
2429 }
2430 else
2431 h->got.offset = (bfd_vma) -1;
2432
2433 eh = (struct sparc64_elf_link_hash_entry *) h;
2434 if (eh->dyn_relocs == NULL)
2435 return TRUE;
2436
2437 /* In the shared -Bsymbolic case, discard space allocated for
2438 dynamic pc-relative relocs against symbols which turn out to be
2439 defined in regular objects. For the normal shared case, discard
2440 space for pc-relative relocs that have become local due to symbol
2441 visibility changes. */
2442
2443 if (info->shared)
2444 {
2445 if (h->def_regular
2446 && (h->forced_local
2447 || info->symbolic))
2448 {
2449 struct sparc64_elf_dyn_relocs **pp;
2450
2451 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2452 {
2453 p->count -= p->pc_count;
2454 p->pc_count = 0;
2455 if (p->count == 0)
2456 *pp = p->next;
2457 else
2458 pp = &p->next;
2459 }
2460 }
2461 }
2462 else
2463 {
2464 /* For the non-shared case, discard space for relocs against
2465 symbols which turn out to need copy relocs or are not
2466 dynamic. */
2467
2468 if (!h->non_got_ref
2469 && ((h->def_dynamic
2470 && !h->def_regular)
2471 || (htab->elf.dynamic_sections_created
2472 && (h->root.type == bfd_link_hash_undefweak
2473 || h->root.type == bfd_link_hash_undefined))))
2474 {
2475 /* Make sure this symbol is output as a dynamic symbol.
2476 Undefined weak syms won't yet be marked as dynamic. */
2477 if (h->dynindx == -1
2478 && !h->forced_local)
2479 {
2480 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2481 return FALSE;
2482 }
2483
2484 /* If that succeeded, we know we'll be keeping all the
2485 relocs. */
2486 if (h->dynindx != -1)
2487 goto keep;
2488 }
2489
2490 eh->dyn_relocs = NULL;
2491
2492 keep: ;
2493 }
2494
2495 /* Finally, allocate space. */
2496 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2497 {
2498 asection *sreloc = elf_section_data (p->sec)->sreloc;
2499 sreloc->size += p->count * sizeof (Elf64_External_Rela);
2500 }
2501
2502 return TRUE;
2503 }
2504
2505 /* Find any dynamic relocs that apply to read-only sections. */
2506
2507 static bfd_boolean
2508 readonly_dynrelocs (h, inf)
2509 struct elf_link_hash_entry *h;
2510 PTR inf;
2511 {
2512 struct sparc64_elf_link_hash_entry *eh;
2513 struct sparc64_elf_dyn_relocs *p;
2514
2515 if (h->root.type == bfd_link_hash_warning)
2516 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2517
2518 eh = (struct sparc64_elf_link_hash_entry *) h;
2519 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2520 {
2521 asection *s = p->sec->output_section;
2522
2523 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2524 {
2525 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2526
2527 info->flags |= DF_TEXTREL;
2528
2529 /* Not an error, just cut short the traversal. */
2530 return FALSE;
2531 }
2532 }
2533 return TRUE;
2534 }
2535
2536 /* Return true if the dynamic symbol for a given section should be
2537 omitted when creating a shared library. */
2538
2539 static bfd_boolean
2540 sparc64_elf_omit_section_dynsym (bfd *output_bfd,
2541 struct bfd_link_info *info,
2542 asection *p)
2543 {
2544 /* We keep the .got section symbol so that explicit relocations
2545 against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode
2546 can be turned into relocations against the .got symbol. */
2547 if (strcmp (p->name, ".got") == 0)
2548 return FALSE;
2549
2550 return _bfd_elf_link_omit_section_dynsym (output_bfd, info, p);
2551 }
2552
2553 /* Set the sizes of the dynamic sections. */
2554
2555 static bfd_boolean
2556 sparc64_elf_size_dynamic_sections (output_bfd, info)
2557 bfd *output_bfd;
2558 struct bfd_link_info *info;
2559 {
2560 struct sparc64_elf_link_hash_table *htab;
2561 bfd *dynobj;
2562 asection *s;
2563 bfd *ibfd;
2564
2565 htab = sparc64_elf_hash_table (info);
2566 dynobj = htab->elf.dynobj;
2567 BFD_ASSERT (dynobj != NULL);
2568
2569 if (elf_hash_table (info)->dynamic_sections_created)
2570 {
2571 /* Set the contents of the .interp section to the interpreter. */
2572 if (info->executable)
2573 {
2574 s = bfd_get_section_by_name (dynobj, ".interp");
2575 BFD_ASSERT (s != NULL);
2576 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2577 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2578 }
2579 }
2580
2581 /* Set up .got offsets for local syms, and space for local dynamic
2582 relocs. */
2583 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2584 {
2585 bfd_signed_vma *local_got;
2586 bfd_signed_vma *end_local_got;
2587 char *local_tls_type;
2588 bfd_size_type locsymcount;
2589 Elf_Internal_Shdr *symtab_hdr;
2590 asection *srel;
2591
2592 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2593 continue;
2594
2595 for (s = ibfd->sections; s != NULL; s = s->next)
2596 {
2597 struct sparc64_elf_dyn_relocs *p;
2598
2599 for (p = *((struct sparc64_elf_dyn_relocs **)
2600 &elf_section_data (s)->local_dynrel);
2601 p != NULL;
2602 p = p->next)
2603 {
2604 if (!bfd_is_abs_section (p->sec)
2605 && bfd_is_abs_section (p->sec->output_section))
2606 {
2607 /* Input section has been discarded, either because
2608 it is a copy of a linkonce section or due to
2609 linker script /DISCARD/, so we'll be discarding
2610 the relocs too. */
2611 }
2612 else if (p->count != 0)
2613 {
2614 srel = elf_section_data (p->sec)->sreloc;
2615 srel->size += p->count * sizeof (Elf64_External_Rela);
2616 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2617 info->flags |= DF_TEXTREL;
2618 }
2619 }
2620 }
2621
2622 local_got = elf_local_got_refcounts (ibfd);
2623 if (!local_got)
2624 continue;
2625
2626 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2627 locsymcount = symtab_hdr->sh_info;
2628 end_local_got = local_got + locsymcount;
2629 local_tls_type = sparc64_elf_local_got_tls_type (ibfd);
2630 s = htab->sgot;
2631 srel = htab->srelgot;
2632 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
2633 {
2634 if (*local_got > 0)
2635 {
2636 *local_got = s->size;
2637 s->size += 8;
2638 if (*local_tls_type == GOT_TLS_GD)
2639 s->size += 8;
2640 if (info->shared
2641 || *local_tls_type == GOT_TLS_GD
2642 || *local_tls_type == GOT_TLS_IE)
2643 srel->size += sizeof (Elf64_External_Rela);
2644 }
2645 else
2646 *local_got = (bfd_vma) -1;
2647 }
2648 }
2649
2650 if (htab->tls_ldm_got.refcount > 0)
2651 {
2652 /* Allocate 2 got entries and 1 dynamic reloc for
2653 R_SPARC_TLS_LDM_{HI22,LO10} relocs. */
2654 htab->tls_ldm_got.offset = htab->sgot->size;
2655 htab->sgot->size += (2 * 8);
2656 htab->srelgot->size += sizeof (Elf64_External_Rela);
2657 }
2658 else
2659 htab->tls_ldm_got.offset = -1;
2660
2661 /* Allocate global sym .plt and .got entries, and space for global
2662 sym dynamic relocs. */
2663 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
2664
2665 /* The 32-bit sparc BFD backend has a hack that, iff the .got
2666 section is larger than 0x1000 bytes, it increments the
2667 .got base by 0x1000 so that 13 bit relocations are more
2668 likely to work.
2669
2670 On 64-bit we have to be more careful, since sethi+or will
2671 not create a proper 64-bit sign extended negative value
2672 for relocations to the first 0x1000 bytes of the .got area.
2673 We could do this using sethi+xor tricks just like the
2674 sparc backend of GCC does, in order to make this work at
2675 some point. Just... not today. */
2676
2677 /* The check_relocs and adjust_dynamic_symbol entry points have
2678 determined the sizes of the various dynamic sections. Allocate
2679 memory for them. */
2680 for (s = dynobj->sections; s != NULL; s = s->next)
2681 {
2682 const char *name;
2683 bfd_boolean strip = FALSE;
2684
2685 if ((s->flags & SEC_LINKER_CREATED) == 0)
2686 continue;
2687
2688 /* It's OK to base decisions on the section name, because none
2689 of the dynobj section names depend upon the input files. */
2690 name = bfd_get_section_name (dynobj, s);
2691
2692 if (strncmp (name, ".rela", 5) == 0)
2693 {
2694 if (s->size == 0)
2695 {
2696 /* If we don't need this section, strip it from the
2697 output file. This is to handle .rela.bss and
2698 .rel.plt. We must create it in
2699 create_dynamic_sections, because it must be created
2700 before the linker maps input sections to output
2701 sections. The linker does that before
2702 adjust_dynamic_symbol is called, and it is that
2703 function which decides whether anything needs to go
2704 into these sections. */
2705 strip = TRUE;
2706 }
2707 else
2708 {
2709 /* We use the reloc_count field as a counter if we need
2710 to copy relocs into the output file. */
2711 s->reloc_count = 0;
2712 }
2713 }
2714 else if (s != htab->splt && s != htab->sgot)
2715 {
2716 /* It's not one of our sections, so don't allocate space. */
2717 continue;
2718 }
2719
2720 if (strip)
2721 {
2722 _bfd_strip_section_from_output (info, s);
2723 continue;
2724 }
2725
2726 /* Allocate memory for the section contents. Zero the memory
2727 for the benefit of .rela.plt, which has 4 unused entries
2728 at the beginning, and we don't want garbage. */
2729 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2730 if (s->contents == NULL && s->size != 0)
2731 return FALSE;
2732 }
2733
2734 if (elf_hash_table (info)->dynamic_sections_created)
2735 {
2736 /* Add some entries to the .dynamic section. We fill in the
2737 values later, in sparc64_elf_finish_dynamic_sections, but we
2738 must add the entries now so that we get the correct size for
2739 the .dynamic section. The DT_DEBUG entry is filled in by the
2740 dynamic linker and used by the debugger. */
2741 #define add_dynamic_entry(TAG, VAL) \
2742 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2743
2744 int reg;
2745 struct sparc64_elf_app_reg * app_regs;
2746 struct elf_strtab_hash *dynstr;
2747 struct elf_link_hash_table *eht = elf_hash_table (info);
2748
2749 if (info->executable)
2750 {
2751 if (!add_dynamic_entry (DT_DEBUG, 0))
2752 return FALSE;
2753 }
2754
2755 if (htab->srelplt->size != 0)
2756 {
2757 if (!add_dynamic_entry (DT_PLTGOT, 0)
2758 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2759 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2760 || !add_dynamic_entry (DT_JMPREL, 0))
2761 return FALSE;
2762 }
2763
2764 if (!add_dynamic_entry (DT_RELA, 0)
2765 || !add_dynamic_entry (DT_RELASZ, 0)
2766 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2767 return FALSE;
2768
2769 /* If any dynamic relocs apply to a read-only section,
2770 then we need a DT_TEXTREL entry. */
2771 if ((info->flags & DF_TEXTREL) == 0)
2772 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2773 (PTR) info);
2774
2775 if (info->flags & DF_TEXTREL)
2776 {
2777 if (!add_dynamic_entry (DT_TEXTREL, 0))
2778 return FALSE;
2779 }
2780
2781 /* Add dynamic STT_REGISTER symbols and corresponding DT_SPARC_REGISTER
2782 entries if needed. */
2783 app_regs = sparc64_elf_hash_table (info)->app_regs;
2784 dynstr = eht->dynstr;
2785
2786 for (reg = 0; reg < 4; reg++)
2787 if (app_regs [reg].name != NULL)
2788 {
2789 struct elf_link_local_dynamic_entry *entry, *e;
2790
2791 if (!add_dynamic_entry (DT_SPARC_REGISTER, 0))
2792 return FALSE;
2793
2794 entry = (struct elf_link_local_dynamic_entry *)
2795 bfd_hash_allocate (&info->hash->table, sizeof (*entry));
2796 if (entry == NULL)
2797 return FALSE;
2798
2799 /* We cheat here a little bit: the symbol will not be local, so we
2800 put it at the end of the dynlocal linked list. We will fix it
2801 later on, as we have to fix other fields anyway. */
2802 entry->isym.st_value = reg < 2 ? reg + 2 : reg + 4;
2803 entry->isym.st_size = 0;
2804 if (*app_regs [reg].name != '\0')
2805 entry->isym.st_name
2806 = _bfd_elf_strtab_add (dynstr, app_regs[reg].name, FALSE);
2807 else
2808 entry->isym.st_name = 0;
2809 entry->isym.st_other = 0;
2810 entry->isym.st_info = ELF_ST_INFO (app_regs [reg].bind,
2811 STT_REGISTER);
2812 entry->isym.st_shndx = app_regs [reg].shndx;
2813 entry->next = NULL;
2814 entry->input_bfd = output_bfd;
2815 entry->input_indx = -1;
2816
2817 if (eht->dynlocal == NULL)
2818 eht->dynlocal = entry;
2819 else
2820 {
2821 for (e = eht->dynlocal; e->next; e = e->next)
2822 ;
2823 e->next = entry;
2824 }
2825 eht->dynsymcount++;
2826 }
2827 }
2828 #undef add_dynamic_entry
2829
2830 return TRUE;
2831 }
2832 \f
2833 static bfd_boolean
2834 sparc64_elf_new_section_hook (abfd, sec)
2835 bfd *abfd;
2836 asection *sec;
2837 {
2838 struct sparc64_elf_section_data *sdata;
2839 bfd_size_type amt = sizeof (*sdata);
2840
2841 sdata = (struct sparc64_elf_section_data *) bfd_zalloc (abfd, amt);
2842 if (sdata == NULL)
2843 return FALSE;
2844 sec->used_by_bfd = (PTR) sdata;
2845
2846 return _bfd_elf_new_section_hook (abfd, sec);
2847 }
2848
2849 static bfd_boolean
2850 sparc64_elf_relax_section (abfd, section, link_info, again)
2851 bfd *abfd ATTRIBUTE_UNUSED;
2852 asection *section ATTRIBUTE_UNUSED;
2853 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
2854 bfd_boolean *again;
2855 {
2856 *again = FALSE;
2857 sec_do_relax (section) = 1;
2858 return TRUE;
2859 }
2860 \f
2861 /* Return the base VMA address which should be subtracted from real addresses
2862 when resolving @dtpoff relocation.
2863 This is PT_TLS segment p_vaddr. */
2864
2865 static bfd_vma
2866 dtpoff_base (info)
2867 struct bfd_link_info *info;
2868 {
2869 /* If tls_sec is NULL, we should have signalled an error already. */
2870 if (elf_hash_table (info)->tls_sec == NULL)
2871 return 0;
2872 return elf_hash_table (info)->tls_sec->vma;
2873 }
2874
2875 /* Return the relocation value for @tpoff relocation
2876 if STT_TLS virtual address is ADDRESS. */
2877
2878 static bfd_vma
2879 tpoff (info, address)
2880 struct bfd_link_info *info;
2881 bfd_vma address;
2882 {
2883 struct elf_link_hash_table *htab = elf_hash_table (info);
2884
2885 /* If tls_sec is NULL, we should have signalled an error already. */
2886 if (htab->tls_sec == NULL)
2887 return 0;
2888 return address - htab->tls_size - htab->tls_sec->vma;
2889 }
2890
2891 /* Relocate a SPARC64 ELF section. */
2892
2893 static bfd_boolean
2894 sparc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2895 contents, relocs, local_syms, local_sections)
2896 bfd *output_bfd;
2897 struct bfd_link_info *info;
2898 bfd *input_bfd;
2899 asection *input_section;
2900 bfd_byte *contents;
2901 Elf_Internal_Rela *relocs;
2902 Elf_Internal_Sym *local_syms;
2903 asection **local_sections;
2904 {
2905 struct sparc64_elf_link_hash_table *htab;
2906 Elf_Internal_Shdr *symtab_hdr;
2907 struct elf_link_hash_entry **sym_hashes;
2908 bfd_vma *local_got_offsets;
2909 bfd_vma got_base;
2910 asection *sreloc;
2911 Elf_Internal_Rela *rel;
2912 Elf_Internal_Rela *relend;
2913
2914 if (info->relocatable)
2915 return TRUE;
2916
2917 htab = sparc64_elf_hash_table (info);
2918 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2919 sym_hashes = elf_sym_hashes (input_bfd);
2920 local_got_offsets = elf_local_got_offsets (input_bfd);
2921
2922 if (elf_hash_table (info)->hgot == NULL)
2923 got_base = 0;
2924 else
2925 got_base = elf_hash_table (info)->hgot->root.u.def.value;
2926
2927 sreloc = elf_section_data (input_section)->sreloc;
2928
2929 rel = relocs;
2930 relend = relocs + NUM_SHDR_ENTRIES (& elf_section_data (input_section)->rel_hdr);
2931 for (; rel < relend; rel++)
2932 {
2933 int r_type, tls_type;
2934 reloc_howto_type *howto;
2935 unsigned long r_symndx;
2936 struct elf_link_hash_entry *h;
2937 Elf_Internal_Sym *sym;
2938 asection *sec;
2939 bfd_vma relocation, off;
2940 bfd_reloc_status_type r;
2941 bfd_boolean is_plt = FALSE;
2942 bfd_boolean unresolved_reloc;
2943
2944 r_type = ELF64_R_TYPE_ID (rel->r_info);
2945 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
2946 {
2947 bfd_set_error (bfd_error_bad_value);
2948 return FALSE;
2949 }
2950 howto = sparc64_elf_howto_table + r_type;
2951
2952 /* This is a final link. */
2953 r_symndx = ELF64_R_SYM (rel->r_info);
2954 h = NULL;
2955 sym = NULL;
2956 sec = NULL;
2957 unresolved_reloc = FALSE;
2958 if (r_symndx < symtab_hdr->sh_info)
2959 {
2960 sym = local_syms + r_symndx;
2961 sec = local_sections[r_symndx];
2962 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2963 }
2964 else
2965 {
2966 bfd_boolean warned;
2967
2968 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2969 r_symndx, symtab_hdr, sym_hashes,
2970 h, sec, relocation,
2971 unresolved_reloc, warned);
2972 if (warned)
2973 {
2974 /* To avoid generating warning messages about truncated
2975 relocations, set the relocation's address to be the same as
2976 the start of this section. */
2977 if (input_section->output_section != NULL)
2978 relocation = input_section->output_section->vma;
2979 else
2980 relocation = 0;
2981 }
2982 }
2983
2984 switch (r_type)
2985 {
2986 case R_SPARC_GOT10:
2987 case R_SPARC_GOT13:
2988 case R_SPARC_GOT22:
2989 /* Relocation is to the entry for this symbol in the global
2990 offset table. */
2991 if (htab->sgot == NULL)
2992 abort ();
2993
2994 if (h != NULL)
2995 {
2996 bfd_boolean dyn;
2997
2998 off = h->got.offset;
2999 BFD_ASSERT (off != (bfd_vma) -1);
3000 dyn = elf_hash_table (info)->dynamic_sections_created;
3001
3002 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3003 || (info->shared
3004 && (info->symbolic
3005 || h->dynindx == -1
3006 || h->forced_local)
3007 && h->def_regular))
3008 {
3009 /* This is actually a static link, or it is a
3010 -Bsymbolic link and the symbol is defined
3011 locally, or the symbol was forced to be local
3012 because of a version file. We must initialize
3013 this entry in the global offset table. Since the
3014 offset must always be a multiple of 8, we use the
3015 least significant bit to record whether we have
3016 initialized it already.
3017
3018 When doing a dynamic link, we create a .rela.got
3019 relocation entry to initialize the value. This
3020 is done in the finish_dynamic_symbol routine. */
3021 if ((off & 1) != 0)
3022 off &= ~1;
3023 else
3024 {
3025 bfd_put_64 (output_bfd, relocation,
3026 htab->sgot->contents + off);
3027 h->got.offset |= 1;
3028 }
3029 }
3030 else
3031 unresolved_reloc = FALSE;
3032 }
3033 else
3034 {
3035 BFD_ASSERT (local_got_offsets != NULL
3036 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3037
3038 off = local_got_offsets[r_symndx];
3039
3040 /* The offset must always be a multiple of 8. We use
3041 the least significant bit to record whether we have
3042 already processed this entry. */
3043 if ((off & 1) != 0)
3044 off &= ~1;
3045 else
3046 {
3047
3048 if (info->shared)
3049 {
3050 asection *s;
3051 Elf_Internal_Rela outrel;
3052 bfd_byte *loc;
3053
3054 /* We need to generate a R_SPARC_RELATIVE reloc
3055 for the dynamic linker. */
3056 s = htab->srelgot;
3057 BFD_ASSERT (s != NULL);
3058
3059 outrel.r_offset = (htab->sgot->output_section->vma
3060 + htab->sgot->output_offset
3061 + off);
3062 outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
3063 outrel.r_addend = relocation;
3064 relocation = 0;
3065 loc = s->contents;
3066 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
3067 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3068 }
3069
3070 bfd_put_64 (output_bfd, relocation,
3071 htab->sgot->contents + off);
3072 local_got_offsets[r_symndx] |= 1;
3073 }
3074 }
3075 relocation = htab->sgot->output_offset + off - got_base;
3076 break;
3077
3078 case R_SPARC_PLT32:
3079 case R_SPARC_PLT64:
3080 if (h == NULL || h->plt.offset == (bfd_vma) -1)
3081 {
3082 r_type = (r_type == R_SPARC_PLT32) ? R_SPARC_32 : R_SPARC_64;
3083 goto r_sparc_plt32;
3084 }
3085 /* Fall through. */
3086
3087 case R_SPARC_WPLT30:
3088 case R_SPARC_HIPLT22:
3089 case R_SPARC_LOPLT10:
3090 case R_SPARC_PCPLT32:
3091 case R_SPARC_PCPLT22:
3092 case R_SPARC_PCPLT10:
3093 r_sparc_wplt30:
3094 /* Relocation is to the entry for this symbol in the
3095 procedure linkage table. */
3096
3097 /* Relocation is to the entry for this symbol in the
3098 procedure linkage table. */
3099 BFD_ASSERT (h != NULL);
3100
3101 if (h->plt.offset == (bfd_vma) -1 || htab->splt == NULL)
3102 {
3103 /* We didn't make a PLT entry for this symbol. This
3104 happens when statically linking PIC code, or when
3105 using -Bsymbolic. */
3106 break;
3107 }
3108
3109 relocation = (htab->splt->output_section->vma
3110 + htab->splt->output_offset
3111 + h->plt.offset);
3112 unresolved_reloc = FALSE;
3113 if (r_type == R_SPARC_PLT32 || r_type == R_SPARC_PLT64)
3114 {
3115 r_type = r_type == R_SPARC_PLT32 ? R_SPARC_32 : R_SPARC_64;
3116 is_plt = TRUE;
3117 goto r_sparc_plt32;
3118 }
3119 break;
3120
3121 case R_SPARC_PC10:
3122 case R_SPARC_PC22:
3123 case R_SPARC_PC_HH22:
3124 case R_SPARC_PC_HM10:
3125 case R_SPARC_PC_LM22:
3126 if (h != NULL
3127 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3128 break;
3129 /* Fall through. */
3130 case R_SPARC_DISP8:
3131 case R_SPARC_DISP16:
3132 case R_SPARC_DISP32:
3133 case R_SPARC_DISP64:
3134 case R_SPARC_WDISP30:
3135 case R_SPARC_WDISP22:
3136 case R_SPARC_WDISP19:
3137 case R_SPARC_WDISP16:
3138 case R_SPARC_8:
3139 case R_SPARC_16:
3140 case R_SPARC_32:
3141 case R_SPARC_HI22:
3142 case R_SPARC_22:
3143 case R_SPARC_13:
3144 case R_SPARC_LO10:
3145 case R_SPARC_UA16:
3146 case R_SPARC_UA32:
3147 case R_SPARC_10:
3148 case R_SPARC_11:
3149 case R_SPARC_64:
3150 case R_SPARC_OLO10:
3151 case R_SPARC_HH22:
3152 case R_SPARC_HM10:
3153 case R_SPARC_LM22:
3154 case R_SPARC_7:
3155 case R_SPARC_5:
3156 case R_SPARC_6:
3157 case R_SPARC_HIX22:
3158 case R_SPARC_LOX10:
3159 case R_SPARC_H44:
3160 case R_SPARC_M44:
3161 case R_SPARC_L44:
3162 case R_SPARC_UA64:
3163 r_sparc_plt32:
3164 /* r_symndx will be zero only for relocs against symbols
3165 from removed linkonce sections, or sections discarded by
3166 a linker script. */
3167 if (r_symndx == 0
3168 || (input_section->flags & SEC_ALLOC) == 0)
3169 break;
3170
3171 if ((info->shared
3172 && (h == NULL
3173 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3174 || h->root.type != bfd_link_hash_undefweak)
3175 && (! howto->pc_relative
3176 || (h != NULL
3177 && h->dynindx != -1
3178 && (! info->symbolic
3179 || !h->def_regular))))
3180 || (!info->shared
3181 && h != NULL
3182 && h->dynindx != -1
3183 && !h->non_got_ref
3184 && ((h->def_dynamic
3185 && !h->def_regular)
3186 || h->root.type == bfd_link_hash_undefweak
3187 || h->root.type == bfd_link_hash_undefined)))
3188 {
3189 Elf_Internal_Rela outrel;
3190 bfd_byte *loc;
3191 bfd_boolean skip, relocate = FALSE;
3192
3193 /* When generating a shared object, these relocations
3194 are copied into the output file to be resolved at run
3195 time. */
3196
3197 BFD_ASSERT (sreloc != NULL);
3198
3199 skip = FALSE;
3200
3201 outrel.r_offset =
3202 _bfd_elf_section_offset (output_bfd, info, input_section,
3203 rel->r_offset);
3204 if (outrel.r_offset == (bfd_vma) -1)
3205 skip = TRUE;
3206 else if (outrel.r_offset == (bfd_vma) -2)
3207 skip = TRUE, relocate = TRUE;
3208 outrel.r_offset += (input_section->output_section->vma
3209 + input_section->output_offset);
3210
3211 /* Optimize unaligned reloc usage now that we know where
3212 it finally resides. */
3213 switch (r_type)
3214 {
3215 case R_SPARC_16:
3216 if (outrel.r_offset & 1)
3217 r_type = R_SPARC_UA16;
3218 break;
3219 case R_SPARC_UA16:
3220 if (!(outrel.r_offset & 1))
3221 r_type = R_SPARC_16;
3222 break;
3223 case R_SPARC_32:
3224 if (outrel.r_offset & 3)
3225 r_type = R_SPARC_UA32;
3226 break;
3227 case R_SPARC_UA32:
3228 if (!(outrel.r_offset & 3))
3229 r_type = R_SPARC_32;
3230 break;
3231 case R_SPARC_64:
3232 if (outrel.r_offset & 7)
3233 r_type = R_SPARC_UA64;
3234 break;
3235 case R_SPARC_UA64:
3236 if (!(outrel.r_offset & 7))
3237 r_type = R_SPARC_64;
3238 break;
3239 case R_SPARC_DISP8:
3240 case R_SPARC_DISP16:
3241 case R_SPARC_DISP32:
3242 case R_SPARC_DISP64:
3243 /* If the symbol is not dynamic, we should not keep
3244 a dynamic relocation. But an .rela.* slot has been
3245 allocated for it, output R_SPARC_NONE.
3246 FIXME: Add code tracking needed dynamic relocs as
3247 e.g. i386 has. */
3248 if (h->dynindx == -1)
3249 skip = TRUE, relocate = TRUE;
3250 break;
3251 }
3252
3253 if (skip)
3254 memset (&outrel, 0, sizeof outrel);
3255 /* h->dynindx may be -1 if the symbol was marked to
3256 become local. */
3257 else if (h != NULL && ! is_plt
3258 && ((! info->symbolic && h->dynindx != -1)
3259 || !h->def_regular))
3260 {
3261 BFD_ASSERT (h->dynindx != -1);
3262 outrel.r_info
3263 = ELF64_R_INFO (h->dynindx,
3264 ELF64_R_TYPE_INFO (
3265 ELF64_R_TYPE_DATA (rel->r_info),
3266 r_type));
3267 outrel.r_addend = rel->r_addend;
3268 }
3269 else
3270 {
3271 if (r_type == R_SPARC_64)
3272 {
3273 outrel.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
3274 outrel.r_addend = relocation + rel->r_addend;
3275 }
3276 else
3277 {
3278 long indx;
3279
3280 if (is_plt)
3281 sec = htab->splt;
3282
3283 if (bfd_is_abs_section (sec))
3284 indx = 0;
3285 else if (sec == NULL || sec->owner == NULL)
3286 {
3287 bfd_set_error (bfd_error_bad_value);
3288 return FALSE;
3289 }
3290 else
3291 {
3292 asection *osec;
3293
3294 osec = sec->output_section;
3295 indx = elf_section_data (osec)->dynindx;
3296
3297 /* FIXME: we really should be able to link non-pic
3298 shared libraries. */
3299 if (indx == 0)
3300 {
3301 BFD_FAIL ();
3302 (*_bfd_error_handler)
3303 (_("%B: probably compiled without -fPIC?"),
3304 input_bfd);
3305 bfd_set_error (bfd_error_bad_value);
3306 return FALSE;
3307 }
3308 }
3309
3310 outrel.r_info
3311 = ELF64_R_INFO (indx,
3312 ELF64_R_TYPE_INFO (
3313 ELF64_R_TYPE_DATA (rel->r_info),
3314 r_type));
3315 outrel.r_addend = relocation + rel->r_addend;
3316 }
3317 }
3318
3319 loc = sreloc->contents;
3320 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3321 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3322
3323 /* This reloc will be computed at runtime, so there's no
3324 need to do anything now. */
3325 if (! relocate)
3326 continue;
3327 }
3328 break;
3329
3330 case R_SPARC_TLS_GD_HI22:
3331 case R_SPARC_TLS_GD_LO10:
3332 case R_SPARC_TLS_IE_HI22:
3333 case R_SPARC_TLS_IE_LO10:
3334 r_type = sparc64_elf_tls_transition (info, r_type, h == NULL);
3335 tls_type = GOT_UNKNOWN;
3336 if (h == NULL && local_got_offsets)
3337 tls_type = sparc64_elf_local_got_tls_type (input_bfd) [r_symndx];
3338 else if (h != NULL)
3339 {
3340 tls_type = sparc64_elf_hash_entry(h)->tls_type;
3341 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
3342 switch (ELF64_R_TYPE_ID (rel->r_info))
3343 {
3344 case R_SPARC_TLS_GD_HI22:
3345 case R_SPARC_TLS_IE_HI22:
3346 r_type = R_SPARC_TLS_LE_HIX22;
3347 break;
3348 default:
3349 r_type = R_SPARC_TLS_LE_LOX10;
3350 break;
3351 }
3352 }
3353 if (tls_type == GOT_TLS_IE)
3354 switch (r_type)
3355 {
3356 case R_SPARC_TLS_GD_HI22:
3357 r_type = R_SPARC_TLS_IE_HI22;
3358 break;
3359 case R_SPARC_TLS_GD_LO10:
3360 r_type = R_SPARC_TLS_IE_LO10;
3361 break;
3362 }
3363
3364 if (r_type == R_SPARC_TLS_LE_HIX22)
3365 {
3366 relocation = tpoff (info, relocation);
3367 break;
3368 }
3369 if (r_type == R_SPARC_TLS_LE_LOX10)
3370 {
3371 /* Change add into xor. */
3372 relocation = tpoff (info, relocation);
3373 bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd,
3374 contents + rel->r_offset)
3375 | 0x80182000), contents + rel->r_offset);
3376 break;
3377 }
3378
3379 if (h != NULL)
3380 {
3381 off = h->got.offset;
3382 h->got.offset |= 1;
3383 }
3384 else
3385 {
3386 BFD_ASSERT (local_got_offsets != NULL);
3387 off = local_got_offsets[r_symndx];
3388 local_got_offsets[r_symndx] |= 1;
3389 }
3390
3391 r_sparc_tlsldm:
3392 if (htab->sgot == NULL)
3393 abort ();
3394
3395 if ((off & 1) != 0)
3396 off &= ~1;
3397 else
3398 {
3399 Elf_Internal_Rela outrel;
3400 Elf64_External_Rela *loc;
3401 int dr_type, indx;
3402
3403 if (htab->srelgot == NULL)
3404 abort ();
3405
3406 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
3407 outrel.r_offset = (htab->sgot->output_section->vma
3408 + htab->sgot->output_offset + off);
3409 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3410 if (r_type == R_SPARC_TLS_IE_HI22
3411 || r_type == R_SPARC_TLS_IE_LO10)
3412 dr_type = R_SPARC_TLS_TPOFF64;
3413 else
3414 dr_type = R_SPARC_TLS_DTPMOD64;
3415 if (dr_type == R_SPARC_TLS_TPOFF64 && indx == 0)
3416 outrel.r_addend = relocation - dtpoff_base (info);
3417 else
3418 outrel.r_addend = 0;
3419 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3420 loc = (Elf64_External_Rela *) htab->srelgot->contents;
3421 loc += htab->srelgot->reloc_count++;
3422 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3423 (bfd_byte *) loc);
3424
3425 if (r_type == R_SPARC_TLS_GD_HI22
3426 || r_type == R_SPARC_TLS_GD_LO10)
3427 {
3428 if (indx == 0)
3429 {
3430 BFD_ASSERT (! unresolved_reloc);
3431 bfd_put_64 (output_bfd,
3432 relocation - dtpoff_base (info),
3433 htab->sgot->contents + off + 8);
3434 }
3435 else
3436 {
3437 bfd_put_64 (output_bfd, 0,
3438 htab->sgot->contents + off + 8);
3439 outrel.r_info = ELF64_R_INFO (indx,
3440 R_SPARC_TLS_DTPOFF64);
3441 outrel.r_offset += 8;
3442 htab->srelgot->reloc_count++;
3443 loc++;
3444 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3445 (bfd_byte *) loc);
3446 }
3447 }
3448 else if (dr_type == R_SPARC_TLS_DTPMOD64)
3449 {
3450 bfd_put_64 (output_bfd, 0,
3451 htab->sgot->contents + off + 8);
3452 }
3453 }
3454
3455 if (off >= (bfd_vma) -2)
3456 abort ();
3457
3458 relocation = htab->sgot->output_offset + off - got_base;
3459 unresolved_reloc = FALSE;
3460 howto = sparc64_elf_howto_table + r_type;
3461 break;
3462
3463 case R_SPARC_TLS_LDM_HI22:
3464 case R_SPARC_TLS_LDM_LO10:
3465 if (! info->shared)
3466 {
3467 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3468 continue;
3469 }
3470 off = htab->tls_ldm_got.offset;
3471 htab->tls_ldm_got.offset |= 1;
3472 goto r_sparc_tlsldm;
3473
3474 case R_SPARC_TLS_LDO_HIX22:
3475 case R_SPARC_TLS_LDO_LOX10:
3476 if (info->shared)
3477 {
3478 relocation -= dtpoff_base (info);
3479 break;
3480 }
3481
3482 r_type = (r_type == R_SPARC_TLS_LDO_HIX22
3483 ? R_SPARC_TLS_LE_HIX22 : R_SPARC_TLS_LE_LOX10);
3484 /* Fall through. */
3485
3486 case R_SPARC_TLS_LE_HIX22:
3487 case R_SPARC_TLS_LE_LOX10:
3488 if (info->shared)
3489 {
3490 Elf_Internal_Rela outrel;
3491 bfd_boolean skip, relocate = FALSE;
3492
3493 BFD_ASSERT (sreloc != NULL);
3494 skip = FALSE;
3495 outrel.r_offset =
3496 _bfd_elf_section_offset (output_bfd, info, input_section,
3497 rel->r_offset);
3498 if (outrel.r_offset == (bfd_vma) -1)
3499 skip = TRUE;
3500 else if (outrel.r_offset == (bfd_vma) -2)
3501 skip = TRUE, relocate = TRUE;
3502 outrel.r_offset += (input_section->output_section->vma
3503 + input_section->output_offset);
3504 if (skip)
3505 memset (&outrel, 0, sizeof outrel);
3506 else
3507 {
3508 outrel.r_info = ELF64_R_INFO (0, r_type);
3509 outrel.r_addend = relocation - dtpoff_base (info)
3510 + rel->r_addend;
3511 }
3512
3513 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3514 (bfd_byte *) (((Elf64_External_Rela *)
3515 sreloc->contents)
3516 + sreloc->reloc_count));
3517 ++sreloc->reloc_count;
3518 continue;
3519 }
3520 relocation = tpoff (info, relocation);
3521 break;
3522
3523 case R_SPARC_TLS_LDM_CALL:
3524 if (! info->shared)
3525 {
3526 /* mov %g0, %o0 */
3527 bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset);
3528 continue;
3529 }
3530 /* Fall through */
3531
3532 case R_SPARC_TLS_GD_CALL:
3533 tls_type = GOT_UNKNOWN;
3534 if (h == NULL && local_got_offsets)
3535 tls_type = sparc64_elf_local_got_tls_type (input_bfd) [r_symndx];
3536 else if (h != NULL)
3537 tls_type = sparc64_elf_hash_entry(h)->tls_type;
3538 if (! info->shared
3539 || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE))
3540 {
3541 bfd_vma insn;
3542
3543 if (!info->shared && (h == NULL || h->dynindx == -1))
3544 {
3545 /* GD -> LE */
3546 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3547 continue;
3548 }
3549
3550 /* GD -> IE */
3551 if (rel + 1 < relend
3552 && ELF64_R_TYPE_ID (rel[1].r_info) == R_SPARC_TLS_GD_ADD
3553 && rel[1].r_offset == rel->r_offset + 4
3554 && ELF64_R_SYM (rel[1].r_info) == r_symndx
3555 && (((insn = bfd_get_32 (input_bfd,
3556 contents + rel[1].r_offset))
3557 >> 25) & 0x1f) == 8)
3558 {
3559 /* We have
3560 call __tls_get_addr, %tgd_call(foo)
3561 add %reg1, %reg2, %o0, %tgd_add(foo)
3562 and change it into IE:
3563 ldx [%reg1 + %reg2], %o0, %tie_ldx(foo)
3564 add %g7, %o0, %o0, %tie_add(foo).
3565 add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2,
3566 ldx is 0xc0580000 | (rd << 25) | (rs1 << 14) | rs2. */
3567 bfd_put_32 (output_bfd, insn | 0xc0580000,
3568 contents + rel->r_offset);
3569 bfd_put_32 (output_bfd, 0x9001c008,
3570 contents + rel->r_offset + 4);
3571 rel++;
3572 continue;
3573 }
3574
3575 bfd_put_32 (output_bfd, 0x9001c008, contents + rel->r_offset);
3576 continue;
3577 }
3578
3579 h = (struct elf_link_hash_entry *)
3580 bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE,
3581 FALSE, TRUE);
3582 BFD_ASSERT (h != NULL);
3583 r_type = R_SPARC_WPLT30;
3584 howto = sparc64_elf_howto_table + r_type;
3585 goto r_sparc_wplt30;
3586
3587 case R_SPARC_TLS_GD_ADD:
3588 tls_type = GOT_UNKNOWN;
3589 if (h == NULL && local_got_offsets)
3590 tls_type = sparc64_elf_local_got_tls_type (input_bfd) [r_symndx];
3591 else if (h != NULL)
3592 tls_type = sparc64_elf_hash_entry(h)->tls_type;
3593 if (! info->shared || tls_type == GOT_TLS_IE)
3594 {
3595 /* add %reg1, %reg2, %reg3, %tgd_add(foo)
3596 changed into IE:
3597 ldx [%reg1 + %reg2], %reg3, %tie_ldx(foo)
3598 or LE:
3599 add %g7, %reg2, %reg3. */
3600 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3601 if ((h != NULL && h->dynindx != -1) || info->shared)
3602 relocation = insn | 0xc0580000;
3603 else
3604 relocation = (insn & ~0x7c000) | 0x1c000;
3605 bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
3606 }
3607 continue;
3608
3609 case R_SPARC_TLS_LDM_ADD:
3610 if (! info->shared)
3611 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3612 continue;
3613
3614 case R_SPARC_TLS_LDO_ADD:
3615 if (! info->shared)
3616 {
3617 /* Change rs1 into %g7. */
3618 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3619 insn = (insn & ~0x7c000) | 0x1c000;
3620 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
3621 }
3622 continue;
3623
3624 case R_SPARC_TLS_IE_LD:
3625 case R_SPARC_TLS_IE_LDX:
3626 if (! info->shared && (h == NULL || h->dynindx == -1))
3627 {
3628 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3629 int rs2 = insn & 0x1f;
3630 int rd = (insn >> 25) & 0x1f;
3631
3632 if (rs2 == rd)
3633 relocation = SPARC_NOP;
3634 else
3635 relocation = 0x80100000 | (insn & 0x3e00001f);
3636 bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
3637 }
3638 continue;
3639
3640 case R_SPARC_TLS_IE_ADD:
3641 /* Totally useless relocation. */
3642 continue;
3643
3644 case R_SPARC_TLS_DTPOFF64:
3645 relocation -= dtpoff_base (info);
3646 break;
3647
3648 default:
3649 break;
3650 }
3651
3652 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3653 because such sections are not SEC_ALLOC and thus ld.so will
3654 not process them. */
3655 if (unresolved_reloc
3656 && !((input_section->flags & SEC_DEBUGGING) != 0
3657 && h->def_dynamic))
3658 (*_bfd_error_handler)
3659 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
3660 input_bfd,
3661 input_section,
3662 (long) rel->r_offset,
3663 h->root.root.string);
3664
3665 r = bfd_reloc_continue;
3666 if (r_type == R_SPARC_OLO10)
3667 {
3668 bfd_vma x;
3669
3670 relocation += rel->r_addend;
3671 relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
3672
3673 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3674 x = (x & ~(bfd_vma) 0x1fff) | (relocation & 0x1fff);
3675 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3676
3677 r = bfd_check_overflow (howto->complain_on_overflow,
3678 howto->bitsize, howto->rightshift,
3679 bfd_arch_bits_per_address (input_bfd),
3680 relocation);
3681 }
3682 else if (r_type == R_SPARC_WDISP16)
3683 {
3684 bfd_vma x;
3685
3686 relocation += rel->r_addend;
3687 relocation -= (input_section->output_section->vma
3688 + input_section->output_offset);
3689 relocation -= rel->r_offset;
3690
3691 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3692 x |= ((((relocation >> 2) & 0xc000) << 6)
3693 | ((relocation >> 2) & 0x3fff));
3694 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3695
3696 r = bfd_check_overflow (howto->complain_on_overflow,
3697 howto->bitsize, howto->rightshift,
3698 bfd_arch_bits_per_address (input_bfd),
3699 relocation);
3700 }
3701 else if (r_type == R_SPARC_TLS_LDO_HIX22
3702 || r_type == R_SPARC_TLS_LE_HIX22)
3703 {
3704 bfd_vma x;
3705
3706 relocation += rel->r_addend;
3707 if (r_type == R_SPARC_TLS_LE_HIX22)
3708 relocation ^= MINUS_ONE;
3709
3710 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3711 x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
3712 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3713 r = bfd_reloc_ok;
3714 }
3715 if (r_type == R_SPARC_TLS_LDO_LOX10
3716 || r_type == R_SPARC_TLS_LE_LOX10)
3717 {
3718 bfd_vma x;
3719
3720 relocation += rel->r_addend;
3721 relocation &= 0x3ff;
3722 if (r_type == R_SPARC_TLS_LE_LOX10)
3723 relocation |= 0x1c00;
3724
3725 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3726 x = (x & ~(bfd_vma) 0x1fff) | relocation;
3727 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3728
3729 r = bfd_reloc_ok;
3730 }
3731 else if (r_type == R_SPARC_HIX22)
3732 {
3733 bfd_vma x;
3734
3735 relocation += rel->r_addend;
3736 relocation = relocation ^ MINUS_ONE;
3737
3738 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3739 x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
3740 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3741
3742 r = bfd_check_overflow (howto->complain_on_overflow,
3743 howto->bitsize, howto->rightshift,
3744 bfd_arch_bits_per_address (input_bfd),
3745 relocation);
3746 }
3747 else if (r_type == R_SPARC_LOX10)
3748 {
3749 bfd_vma x;
3750
3751 relocation += rel->r_addend;
3752 relocation = (relocation & 0x3ff) | 0x1c00;
3753
3754 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3755 x = (x & ~(bfd_vma) 0x1fff) | relocation;
3756 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3757
3758 r = bfd_reloc_ok;
3759 }
3760 else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
3761 && sec_do_relax (input_section)
3762 && rel->r_offset + 4 < input_section->size)
3763 {
3764 #define G0 0
3765 #define O7 15
3766 #define XCC (2 << 20)
3767 #define COND(x) (((x)&0xf)<<25)
3768 #define CONDA COND(0x8)
3769 #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
3770 #define INSN_BA (F2(0,2) | CONDA)
3771 #define INSN_OR F3(2, 0x2, 0)
3772 #define INSN_NOP F2(0,4)
3773
3774 bfd_vma x, y;
3775
3776 /* If the instruction is a call with either:
3777 restore
3778 arithmetic instruction with rd == %o7
3779 where rs1 != %o7 and rs2 if it is register != %o7
3780 then we can optimize if the call destination is near
3781 by changing the call into a branch always. */
3782 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3783 y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
3784 if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
3785 {
3786 if (((y & OP3(~0)) == OP3(0x3d) /* restore */
3787 || ((y & OP3(0x28)) == 0 /* arithmetic */
3788 && (y & RD(~0)) == RD(O7)))
3789 && (y & RS1(~0)) != RS1(O7)
3790 && ((y & F3I(~0))
3791 || (y & RS2(~0)) != RS2(O7)))
3792 {
3793 bfd_vma reloc;
3794
3795 reloc = relocation + rel->r_addend - rel->r_offset;
3796 reloc -= (input_section->output_section->vma
3797 + input_section->output_offset);
3798
3799 /* Ensure the branch fits into simm22. */
3800 if ((reloc & 3) == 0
3801 && ((reloc & ~(bfd_vma)0x7fffff) == 0
3802 || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
3803 {
3804 reloc >>= 2;
3805
3806 /* Check whether it fits into simm19. */
3807 if ((reloc & 0x3c0000) == 0
3808 || (reloc & 0x3c0000) == 0x3c0000)
3809 x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
3810 else
3811 x = INSN_BA | (reloc & 0x3fffff); /* ba */
3812 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3813 r = bfd_reloc_ok;
3814 if (rel->r_offset >= 4
3815 && (y & (0xffffffff ^ RS1(~0)))
3816 == (INSN_OR | RD(O7) | RS2(G0)))
3817 {
3818 bfd_vma z;
3819 unsigned int reg;
3820
3821 z = bfd_get_32 (input_bfd,
3822 contents + rel->r_offset - 4);
3823 if ((z & (0xffffffff ^ RD(~0)))
3824 != (INSN_OR | RS1(O7) | RS2(G0)))
3825 break;
3826
3827 /* The sequence was
3828 or %o7, %g0, %rN
3829 call foo
3830 or %rN, %g0, %o7
3831
3832 If call foo was replaced with ba, replace
3833 or %rN, %g0, %o7 with nop. */
3834
3835 reg = (y & RS1(~0)) >> 14;
3836 if (reg != ((z & RD(~0)) >> 25)
3837 || reg == G0 || reg == O7)
3838 break;
3839
3840 bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
3841 contents + rel->r_offset + 4);
3842 }
3843
3844 }
3845 }
3846 }
3847 }
3848
3849 if (r == bfd_reloc_continue)
3850 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3851 contents, rel->r_offset,
3852 relocation, rel->r_addend);
3853
3854 if (r != bfd_reloc_ok)
3855 {
3856 switch (r)
3857 {
3858 default:
3859 case bfd_reloc_outofrange:
3860 abort ();
3861 case bfd_reloc_overflow:
3862 {
3863 const char *name;
3864
3865 if (h != NULL)
3866 name = NULL;
3867 else
3868 {
3869 name = bfd_elf_string_from_elf_section (input_bfd,
3870 symtab_hdr->sh_link,
3871 sym->st_name);
3872 if (name == NULL)
3873 return FALSE;
3874 if (*name == '\0')
3875 name = bfd_section_name (input_bfd, sec);
3876 }
3877 if (! ((*info->callbacks->reloc_overflow)
3878 (info, (h ? &h->root : NULL), name, howto->name,
3879 (bfd_vma) 0, input_bfd, input_section,
3880 rel->r_offset)))
3881 return FALSE;
3882 }
3883 break;
3884 }
3885 }
3886 }
3887
3888 return TRUE;
3889 }
3890
3891 /* Finish up dynamic symbol handling. We set the contents of various
3892 dynamic sections here. */
3893
3894 static bfd_boolean
3895 sparc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3896 bfd *output_bfd;
3897 struct bfd_link_info *info;
3898 struct elf_link_hash_entry *h;
3899 Elf_Internal_Sym *sym;
3900 {
3901 bfd *dynobj;
3902 struct sparc64_elf_link_hash_table *htab;
3903
3904 htab = sparc64_elf_hash_table (info);
3905 dynobj = htab->elf.dynobj;
3906
3907 if (h->plt.offset != (bfd_vma) -1)
3908 {
3909 asection *splt;
3910 asection *srela;
3911 Elf_Internal_Rela rela;
3912 bfd_byte *loc;
3913 bfd_vma r_offset;
3914 int rela_index;
3915
3916 /* This symbol has an entry in the PLT. Set it up. */
3917
3918 BFD_ASSERT (h->dynindx != -1);
3919
3920 splt = htab->splt;
3921 srela = htab->srelplt;
3922 BFD_ASSERT (splt != NULL && srela != NULL);
3923
3924 /* Fill in the entry in the procedure linkage table. */
3925 rela_index = sparc64_plt_entry_build (output_bfd, splt, h->plt.offset,
3926 splt->size, &r_offset);
3927
3928 /* Fill in the entry in the .rela.plt section. */
3929 rela.r_offset = r_offset
3930 + (splt->output_section->vma + splt->output_offset);
3931 if (h->plt.offset < (LARGE_PLT_THRESHOLD * PLT_ENTRY_SIZE))
3932 {
3933 rela.r_addend = 0;
3934 }
3935 else
3936 {
3937 rela.r_addend = -(h->plt.offset + 4)
3938 -(splt->output_section->vma + splt->output_offset);
3939 }
3940 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
3941
3942 /* Adjust for the first 4 reserved elements in the .plt section
3943 when setting the offset in the .rela.plt section.
3944 Sun forgot to read their own ABI and copied elf32-sparc behaviour,
3945 thus .plt[4] has corresponding .rela.plt[0] and so on. */
3946
3947 loc = srela->contents;
3948 loc += rela_index * sizeof (Elf64_External_Rela);
3949 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3950
3951 if (!h->def_regular)
3952 {
3953 /* Mark the symbol as undefined, rather than as defined in
3954 the .plt section. Leave the value alone. */
3955 sym->st_shndx = SHN_UNDEF;
3956 /* If the symbol is weak, we do need to clear the value.
3957 Otherwise, the PLT entry would provide a definition for
3958 the symbol even if the symbol wasn't defined anywhere,
3959 and so the symbol would never be NULL. */
3960 if (!h->ref_regular_nonweak)
3961 sym->st_value = 0;
3962 }
3963 }
3964
3965 if (h->got.offset != (bfd_vma) -1
3966 && sparc64_elf_hash_entry(h)->tls_type != GOT_TLS_GD
3967 && sparc64_elf_hash_entry(h)->tls_type != GOT_TLS_IE)
3968 {
3969 asection *sgot;
3970 asection *srela;
3971 Elf_Internal_Rela rela;
3972 bfd_byte *loc;
3973
3974 /* This symbol has an entry in the GOT. Set it up. */
3975
3976 sgot = htab->sgot;
3977 srela = htab->srelgot;
3978 BFD_ASSERT (sgot != NULL && srela != NULL);
3979
3980 rela.r_offset = (sgot->output_section->vma
3981 + sgot->output_offset
3982 + (h->got.offset &~ (bfd_vma) 1));
3983
3984 /* If this is a -Bsymbolic link, and the symbol is defined
3985 locally, we just want to emit a RELATIVE reloc. Likewise if
3986 the symbol was forced to be local because of a version file.
3987 The entry in the global offset table will already have been
3988 initialized in the relocate_section function. */
3989 if (info->shared
3990 && (info->symbolic || h->dynindx == -1)
3991 && h->def_regular)
3992 {
3993 asection *sec = h->root.u.def.section;
3994 rela.r_info = ELF64_R_INFO (0, R_SPARC_RELATIVE);
3995 rela.r_addend = (h->root.u.def.value
3996 + sec->output_section->vma
3997 + sec->output_offset);
3998 }
3999 else
4000 {
4001 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
4002 rela.r_addend = 0;
4003 }
4004
4005 bfd_put_64 (output_bfd, (bfd_vma) 0,
4006 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
4007 loc = srela->contents;
4008 loc += srela->reloc_count++ * sizeof (Elf64_External_Rela);
4009 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
4010 }
4011
4012 if (h->needs_copy)
4013 {
4014 asection *s;
4015 Elf_Internal_Rela rela;
4016 bfd_byte *loc;
4017
4018 /* This symbols needs a copy reloc. Set it up. */
4019 BFD_ASSERT (h->dynindx != -1);
4020
4021 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4022 ".rela.bss");
4023 BFD_ASSERT (s != NULL);
4024
4025 rela.r_offset = (h->root.u.def.value
4026 + h->root.u.def.section->output_section->vma
4027 + h->root.u.def.section->output_offset);
4028 rela.r_info = ELF64_R_INFO (h->dynindx, R_SPARC_COPY);
4029 rela.r_addend = 0;
4030 loc = s->contents + s->reloc_count++ * sizeof (Elf64_External_Rela);
4031 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
4032 }
4033
4034 /* Mark some specially defined symbols as absolute. */
4035 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4036 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4037 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4038 sym->st_shndx = SHN_ABS;
4039
4040 return TRUE;
4041 }
4042
4043 /* Finish up the dynamic sections. */
4044
4045 static bfd_boolean
4046 sparc64_elf_finish_dynamic_sections (output_bfd, info)
4047 bfd *output_bfd;
4048 struct bfd_link_info *info;
4049 {
4050 bfd *dynobj;
4051 int stt_regidx = -1;
4052 asection *sdyn;
4053 struct sparc64_elf_link_hash_table *htab;
4054
4055 htab = sparc64_elf_hash_table (info);
4056 dynobj = htab->elf.dynobj;
4057
4058 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4059
4060 if (elf_hash_table (info)->dynamic_sections_created)
4061 {
4062 asection *splt;
4063 Elf64_External_Dyn *dyncon, *dynconend;
4064
4065 splt = bfd_get_section_by_name (dynobj, ".plt");
4066 BFD_ASSERT (splt != NULL && sdyn != NULL);
4067
4068 dyncon = (Elf64_External_Dyn *) sdyn->contents;
4069 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4070 for (; dyncon < dynconend; dyncon++)
4071 {
4072 Elf_Internal_Dyn dyn;
4073 const char *name;
4074 bfd_boolean size;
4075
4076 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4077
4078 switch (dyn.d_tag)
4079 {
4080 case DT_PLTGOT: name = ".plt"; size = FALSE; break;
4081 case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
4082 case DT_JMPREL: name = ".rela.plt"; size = FALSE; break;
4083 case DT_SPARC_REGISTER:
4084 if (stt_regidx == -1)
4085 {
4086 stt_regidx =
4087 _bfd_elf_link_lookup_local_dynindx (info, output_bfd, -1);
4088 if (stt_regidx == -1)
4089 return FALSE;
4090 }
4091 dyn.d_un.d_val = stt_regidx++;
4092 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4093 /* fallthrough */
4094 default: name = NULL; size = FALSE; break;
4095 }
4096
4097 if (name != NULL)
4098 {
4099 asection *s;
4100
4101 s = bfd_get_section_by_name (output_bfd, name);
4102 if (s == NULL)
4103 dyn.d_un.d_val = 0;
4104 else
4105 {
4106 if (! size)
4107 dyn.d_un.d_ptr = s->vma;
4108 else
4109 dyn.d_un.d_val = s->size;
4110 }
4111 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4112 }
4113 }
4114
4115 /* Initialize the contents of the .plt section. */
4116 if (splt->size > 0)
4117 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
4118
4119 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
4120 PLT_ENTRY_SIZE;
4121 }
4122
4123 /* Set the first entry in the global offset table to the address of
4124 the dynamic section. */
4125 if (htab->sgot && htab->sgot->size > 0)
4126 {
4127 if (sdyn == NULL)
4128 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgot->contents);
4129 else
4130 bfd_put_64 (output_bfd,
4131 sdyn->output_section->vma + sdyn->output_offset,
4132 htab->sgot->contents);
4133 }
4134
4135 if (htab->sgot)
4136 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
4137
4138 return TRUE;
4139 }
4140
4141 \f
4142 /* Functions for dealing with the e_flags field. */
4143
4144 /* Merge backend specific data from an object file to the output
4145 object file when linking. */
4146
4147 static bfd_boolean
4148 sparc64_elf_merge_private_bfd_data (ibfd, obfd)
4149 bfd *ibfd;
4150 bfd *obfd;
4151 {
4152 bfd_boolean error;
4153 flagword new_flags, old_flags;
4154 int new_mm, old_mm;
4155
4156 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4157 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4158 return TRUE;
4159
4160 new_flags = elf_elfheader (ibfd)->e_flags;
4161 old_flags = elf_elfheader (obfd)->e_flags;
4162
4163 if (!elf_flags_init (obfd)) /* First call, no flags set */
4164 {
4165 elf_flags_init (obfd) = TRUE;
4166 elf_elfheader (obfd)->e_flags = new_flags;
4167 }
4168
4169 else if (new_flags == old_flags) /* Compatible flags are ok */
4170 ;
4171
4172 else /* Incompatible flags */
4173 {
4174 error = FALSE;
4175
4176 #define EF_SPARC_ISA_EXTENSIONS \
4177 (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3 | EF_SPARC_HAL_R1)
4178
4179 if ((ibfd->flags & DYNAMIC) != 0)
4180 {
4181 /* We don't want dynamic objects memory ordering and
4182 architecture to have any role. That's what dynamic linker
4183 should do. */
4184 new_flags &= ~(EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS);
4185 new_flags |= (old_flags
4186 & (EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS));
4187 }
4188 else
4189 {
4190 /* Choose the highest architecture requirements. */
4191 old_flags |= (new_flags & EF_SPARC_ISA_EXTENSIONS);
4192 new_flags |= (old_flags & EF_SPARC_ISA_EXTENSIONS);
4193 if ((old_flags & (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3))
4194 && (old_flags & EF_SPARC_HAL_R1))
4195 {
4196 error = TRUE;
4197 (*_bfd_error_handler)
4198 (_("%B: linking UltraSPARC specific with HAL specific code"),
4199 ibfd);
4200 }
4201 /* Choose the most restrictive memory ordering. */
4202 old_mm = (old_flags & EF_SPARCV9_MM);
4203 new_mm = (new_flags & EF_SPARCV9_MM);
4204 old_flags &= ~EF_SPARCV9_MM;
4205 new_flags &= ~EF_SPARCV9_MM;
4206 if (new_mm < old_mm)
4207 old_mm = new_mm;
4208 old_flags |= old_mm;
4209 new_flags |= old_mm;
4210 }
4211
4212 /* Warn about any other mismatches */
4213 if (new_flags != old_flags)
4214 {
4215 error = TRUE;
4216 (*_bfd_error_handler)
4217 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
4218 ibfd, (long) new_flags, (long) old_flags);
4219 }
4220
4221 elf_elfheader (obfd)->e_flags = old_flags;
4222
4223 if (error)
4224 {
4225 bfd_set_error (bfd_error_bad_value);
4226 return FALSE;
4227 }
4228 }
4229 return TRUE;
4230 }
4231
4232 /* MARCO: Set the correct entry size for the .stab section. */
4233
4234 static bfd_boolean
4235 sparc64_elf_fake_sections (abfd, hdr, sec)
4236 bfd *abfd ATTRIBUTE_UNUSED;
4237 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4238 asection *sec;
4239 {
4240 const char *name;
4241
4242 name = bfd_get_section_name (abfd, sec);
4243
4244 if (strcmp (name, ".stab") == 0)
4245 {
4246 /* Even in the 64bit case the stab entries are only 12 bytes long. */
4247 elf_section_data (sec)->this_hdr.sh_entsize = 12;
4248 }
4249
4250 return TRUE;
4251 }
4252 \f
4253 /* Print a STT_REGISTER symbol to file FILE. */
4254
4255 static const char *
4256 sparc64_elf_print_symbol_all (abfd, filep, symbol)
4257 bfd *abfd ATTRIBUTE_UNUSED;
4258 PTR filep;
4259 asymbol *symbol;
4260 {
4261 FILE *file = (FILE *) filep;
4262 int reg, type;
4263
4264 if (ELF_ST_TYPE (((elf_symbol_type *) symbol)->internal_elf_sym.st_info)
4265 != STT_REGISTER)
4266 return NULL;
4267
4268 reg = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
4269 type = symbol->flags;
4270 fprintf (file, "REG_%c%c%11s%c%c R", "GOLI" [reg / 8], '0' + (reg & 7), "",
4271 ((type & BSF_LOCAL)
4272 ? (type & BSF_GLOBAL) ? '!' : 'l'
4273 : (type & BSF_GLOBAL) ? 'g' : ' '),
4274 (type & BSF_WEAK) ? 'w' : ' ');
4275 if (symbol->name == NULL || symbol->name [0] == '\0')
4276 return "#scratch";
4277 else
4278 return symbol->name;
4279 }
4280 \f
4281 /* Set the right machine number for a SPARC64 ELF file. */
4282
4283 static bfd_boolean
4284 sparc64_elf_object_p (abfd)
4285 bfd *abfd;
4286 {
4287 unsigned long mach = bfd_mach_sparc_v9;
4288
4289 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
4290 mach = bfd_mach_sparc_v9b;
4291 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
4292 mach = bfd_mach_sparc_v9a;
4293 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach);
4294 }
4295
4296 static enum elf_reloc_type_class
4297 sparc64_elf_reloc_type_class (rela)
4298 const Elf_Internal_Rela *rela;
4299 {
4300 switch ((int) ELF64_R_TYPE (rela->r_info))
4301 {
4302 case R_SPARC_RELATIVE:
4303 return reloc_class_relative;
4304 case R_SPARC_JMP_SLOT:
4305 return reloc_class_plt;
4306 case R_SPARC_COPY:
4307 return reloc_class_copy;
4308 default:
4309 return reloc_class_normal;
4310 }
4311 }
4312
4313 /* Return address for Ith PLT stub in section PLT, for relocation REL
4314 or (bfd_vma) -1 if it should not be included. */
4315
4316 static bfd_vma
4317 sparc64_elf_plt_sym_val (bfd_vma i, const asection *plt,
4318 const arelent *rel ATTRIBUTE_UNUSED)
4319 {
4320 bfd_vma j;
4321
4322 i += PLT_HEADER_SIZE / PLT_ENTRY_SIZE;
4323 if (i < LARGE_PLT_THRESHOLD)
4324 return plt->vma + i * PLT_ENTRY_SIZE;
4325
4326 j = (i - LARGE_PLT_THRESHOLD) % 160;
4327 i -= j;
4328 return plt->vma + i * PLT_ENTRY_SIZE + j * 4 * 6;
4329 }
4330
4331 /* Relocations in the 64 bit SPARC ELF ABI are more complex than in
4332 standard ELF, because R_SPARC_OLO10 has secondary addend in
4333 ELF64_R_TYPE_DATA field. This structure is used to redirect the
4334 relocation handling routines. */
4335
4336 const struct elf_size_info sparc64_elf_size_info =
4337 {
4338 sizeof (Elf64_External_Ehdr),
4339 sizeof (Elf64_External_Phdr),
4340 sizeof (Elf64_External_Shdr),
4341 sizeof (Elf64_External_Rel),
4342 sizeof (Elf64_External_Rela),
4343 sizeof (Elf64_External_Sym),
4344 sizeof (Elf64_External_Dyn),
4345 sizeof (Elf_External_Note),
4346 4, /* hash-table entry size. */
4347 /* Internal relocations per external relocations.
4348 For link purposes we use just 1 internal per
4349 1 external, for assembly and slurp symbol table
4350 we use 2. */
4351 1,
4352 64, /* arch_size. */
4353 3, /* log_file_align. */
4354 ELFCLASS64,
4355 EV_CURRENT,
4356 bfd_elf64_write_out_phdrs,
4357 bfd_elf64_write_shdrs_and_ehdr,
4358 sparc64_elf_write_relocs,
4359 bfd_elf64_swap_symbol_in,
4360 bfd_elf64_swap_symbol_out,
4361 sparc64_elf_slurp_reloc_table,
4362 bfd_elf64_slurp_symbol_table,
4363 bfd_elf64_swap_dyn_in,
4364 bfd_elf64_swap_dyn_out,
4365 bfd_elf64_swap_reloc_in,
4366 bfd_elf64_swap_reloc_out,
4367 bfd_elf64_swap_reloca_in,
4368 bfd_elf64_swap_reloca_out
4369 };
4370
4371 #define TARGET_BIG_SYM bfd_elf64_sparc_vec
4372 #define TARGET_BIG_NAME "elf64-sparc"
4373 #define ELF_ARCH bfd_arch_sparc
4374 #define ELF_MAXPAGESIZE 0x100000
4375
4376 /* This is the official ABI value. */
4377 #define ELF_MACHINE_CODE EM_SPARCV9
4378
4379 /* This is the value that we used before the ABI was released. */
4380 #define ELF_MACHINE_ALT1 EM_OLD_SPARCV9
4381
4382 #define bfd_elf64_bfd_link_hash_table_create \
4383 sparc64_elf_link_hash_table_create
4384
4385 #define elf_info_to_howto \
4386 sparc64_elf_info_to_howto
4387 #define elf_backend_copy_indirect_symbol \
4388 sparc64_elf_copy_indirect_symbol
4389 #define bfd_elf64_get_reloc_upper_bound \
4390 sparc64_elf_get_reloc_upper_bound
4391 #define bfd_elf64_get_dynamic_reloc_upper_bound \
4392 sparc64_elf_get_dynamic_reloc_upper_bound
4393 #define bfd_elf64_canonicalize_reloc \
4394 sparc64_elf_canonicalize_reloc
4395 #define bfd_elf64_canonicalize_dynamic_reloc \
4396 sparc64_elf_canonicalize_dynamic_reloc
4397 #define bfd_elf64_bfd_reloc_type_lookup \
4398 sparc64_elf_reloc_type_lookup
4399 #define bfd_elf64_bfd_relax_section \
4400 sparc64_elf_relax_section
4401 #define bfd_elf64_new_section_hook \
4402 sparc64_elf_new_section_hook
4403
4404 #define elf_backend_create_dynamic_sections \
4405 sparc64_elf_create_dynamic_sections
4406 #define elf_backend_add_symbol_hook \
4407 sparc64_elf_add_symbol_hook
4408 #define elf_backend_get_symbol_type \
4409 sparc64_elf_get_symbol_type
4410 #define elf_backend_symbol_processing \
4411 sparc64_elf_symbol_processing
4412 #define elf_backend_check_relocs \
4413 sparc64_elf_check_relocs
4414 #define elf_backend_adjust_dynamic_symbol \
4415 sparc64_elf_adjust_dynamic_symbol
4416 #define elf_backend_omit_section_dynsym \
4417 sparc64_elf_omit_section_dynsym
4418 #define elf_backend_size_dynamic_sections \
4419 sparc64_elf_size_dynamic_sections
4420 #define elf_backend_relocate_section \
4421 sparc64_elf_relocate_section
4422 #define elf_backend_finish_dynamic_symbol \
4423 sparc64_elf_finish_dynamic_symbol
4424 #define elf_backend_finish_dynamic_sections \
4425 sparc64_elf_finish_dynamic_sections
4426 #define elf_backend_print_symbol_all \
4427 sparc64_elf_print_symbol_all
4428 #define elf_backend_output_arch_syms \
4429 sparc64_elf_output_arch_syms
4430 #define bfd_elf64_bfd_merge_private_bfd_data \
4431 sparc64_elf_merge_private_bfd_data
4432 #define elf_backend_fake_sections \
4433 sparc64_elf_fake_sections
4434 #define elf_backend_plt_sym_val \
4435 sparc64_elf_plt_sym_val
4436
4437 #define elf_backend_size_info \
4438 sparc64_elf_size_info
4439 #define bfd_elf64_mkobject \
4440 sparc64_elf_mkobject
4441 #define elf_backend_object_p \
4442 sparc64_elf_object_p
4443 #define elf_backend_gc_mark_hook \
4444 sparc64_elf_gc_mark_hook
4445 #define elf_backend_gc_sweep_hook \
4446 sparc64_elf_gc_sweep_hook
4447 #define elf_backend_reloc_type_class \
4448 sparc64_elf_reloc_type_class
4449
4450 #define elf_backend_can_gc_sections 1
4451 #define elf_backend_can_refcount 1
4452 #define elf_backend_want_got_plt 0
4453 #define elf_backend_plt_readonly 0
4454 #define elf_backend_want_plt_sym 1
4455 #define elf_backend_got_header_size 8
4456 #define elf_backend_rela_normal 1
4457
4458 /* Section 5.2.4 of the ABI specifies a 256-byte boundary for the table. */
4459 #define elf_backend_plt_alignment 8
4460
4461 #include "elf64-target.h"