daily update
[binutils.git] / bfd / elfxx-sparc.c
blob6c1b3e3718e21541204ebde4e03b9da90d4ecf5f
1 /* SPARC-specific support for ELF
2 Copyright 2005, 2006, 2007 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* This file handles functionality common to the different SPARC ABI's. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "libiberty.h"
27 #include "elf-bfd.h"
28 #include "elf/sparc.h"
29 #include "opcode/sparc.h"
30 #include "elfxx-sparc.h"
31 #include "elf-vxworks.h"
33 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
34 #define MINUS_ONE (~ (bfd_vma) 0)
36 #define ABI_64_P(abfd) \
37 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
39 /* The relocation "howto" table. */
41 /* Utility for performing the standard initial work of an instruction
42 relocation.
43 *PRELOCATION will contain the relocated item.
44 *PINSN will contain the instruction from the input stream.
45 If the result is `bfd_reloc_other' the caller can continue with
46 performing the relocation. Otherwise it must stop and return the
47 value to its caller. */
49 static bfd_reloc_status_type
50 init_insn_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
51 PTR data, asection *input_section, bfd *output_bfd,
52 bfd_vma *prelocation, bfd_vma *pinsn)
54 bfd_vma relocation;
55 reloc_howto_type *howto = reloc_entry->howto;
57 if (output_bfd != (bfd *) NULL
58 && (symbol->flags & BSF_SECTION_SYM) == 0
59 && (! howto->partial_inplace
60 || reloc_entry->addend == 0))
62 reloc_entry->address += input_section->output_offset;
63 return bfd_reloc_ok;
66 /* This works because partial_inplace is FALSE. */
67 if (output_bfd != NULL)
68 return bfd_reloc_continue;
70 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
71 return bfd_reloc_outofrange;
73 relocation = (symbol->value
74 + symbol->section->output_section->vma
75 + symbol->section->output_offset);
76 relocation += reloc_entry->addend;
77 if (howto->pc_relative)
79 relocation -= (input_section->output_section->vma
80 + input_section->output_offset);
81 relocation -= reloc_entry->address;
84 *prelocation = relocation;
85 *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
86 return bfd_reloc_other;
89 /* For unsupported relocs. */
91 static bfd_reloc_status_type
92 sparc_elf_notsup_reloc (bfd *abfd ATTRIBUTE_UNUSED,
93 arelent *reloc_entry ATTRIBUTE_UNUSED,
94 asymbol *symbol ATTRIBUTE_UNUSED,
95 PTR data ATTRIBUTE_UNUSED,
96 asection *input_section ATTRIBUTE_UNUSED,
97 bfd *output_bfd ATTRIBUTE_UNUSED,
98 char **error_message ATTRIBUTE_UNUSED)
100 return bfd_reloc_notsupported;
103 /* Handle the WDISP16 reloc. */
105 static bfd_reloc_status_type
106 sparc_elf_wdisp16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
107 PTR data, asection *input_section, bfd *output_bfd,
108 char **error_message ATTRIBUTE_UNUSED)
110 bfd_vma relocation;
111 bfd_vma insn;
112 bfd_reloc_status_type status;
114 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
115 input_section, output_bfd, &relocation, &insn);
116 if (status != bfd_reloc_other)
117 return status;
119 insn &= ~ (bfd_vma) 0x303fff;
120 insn |= (((relocation >> 2) & 0xc000) << 6) | ((relocation >> 2) & 0x3fff);
121 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
123 if ((bfd_signed_vma) relocation < - 0x40000
124 || (bfd_signed_vma) relocation > 0x3ffff)
125 return bfd_reloc_overflow;
126 else
127 return bfd_reloc_ok;
130 /* Handle the HIX22 reloc. */
132 static bfd_reloc_status_type
133 sparc_elf_hix22_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
134 PTR data, asection *input_section, bfd *output_bfd,
135 char **error_message ATTRIBUTE_UNUSED)
137 bfd_vma relocation;
138 bfd_vma insn;
139 bfd_reloc_status_type status;
141 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
142 input_section, output_bfd, &relocation, &insn);
143 if (status != bfd_reloc_other)
144 return status;
146 relocation ^= MINUS_ONE;
147 insn = (insn &~ (bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
148 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
150 if ((relocation & ~ (bfd_vma) 0xffffffff) != 0)
151 return bfd_reloc_overflow;
152 else
153 return bfd_reloc_ok;
156 /* Handle the LOX10 reloc. */
158 static bfd_reloc_status_type
159 sparc_elf_lox10_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
160 PTR data, asection *input_section, bfd *output_bfd,
161 char **error_message ATTRIBUTE_UNUSED)
163 bfd_vma relocation;
164 bfd_vma insn;
165 bfd_reloc_status_type status;
167 status = init_insn_reloc (abfd, reloc_entry, symbol, data,
168 input_section, output_bfd, &relocation, &insn);
169 if (status != bfd_reloc_other)
170 return status;
172 insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff);
173 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
175 return bfd_reloc_ok;
178 static reloc_howto_type _bfd_sparc_elf_howto_table[] =
180 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
181 HOWTO(R_SPARC_8, 0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", FALSE,0,0x000000ff,TRUE),
182 HOWTO(R_SPARC_16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", FALSE,0,0x0000ffff,TRUE),
183 HOWTO(R_SPARC_32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", FALSE,0,0xffffffff,TRUE),
184 HOWTO(R_SPARC_DISP8, 0,0, 8,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", FALSE,0,0x000000ff,TRUE),
185 HOWTO(R_SPARC_DISP16, 0,1,16,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", FALSE,0,0x0000ffff,TRUE),
186 HOWTO(R_SPARC_DISP32, 0,2,32,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", FALSE,0,0xffffffff,TRUE),
187 HOWTO(R_SPARC_WDISP30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
188 HOWTO(R_SPARC_WDISP22, 2,2,22,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
189 HOWTO(R_SPARC_HI22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", FALSE,0,0x003fffff,TRUE),
190 HOWTO(R_SPARC_22, 0,2,22,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", FALSE,0,0x003fffff,TRUE),
191 HOWTO(R_SPARC_13, 0,2,13,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", FALSE,0,0x00001fff,TRUE),
192 HOWTO(R_SPARC_LO10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", FALSE,0,0x000003ff,TRUE),
193 HOWTO(R_SPARC_GOT10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", FALSE,0,0x000003ff,TRUE),
194 HOWTO(R_SPARC_GOT13, 0,2,13,FALSE,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", FALSE,0,0x00001fff,TRUE),
195 HOWTO(R_SPARC_GOT22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", FALSE,0,0x003fffff,TRUE),
196 HOWTO(R_SPARC_PC10, 0,2,10,TRUE, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", FALSE,0,0x000003ff,TRUE),
197 HOWTO(R_SPARC_PC22, 10,2,22,TRUE, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", FALSE,0,0x003fffff,TRUE),
198 HOWTO(R_SPARC_WPLT30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", FALSE,0,0x3fffffff,TRUE),
199 HOWTO(R_SPARC_COPY, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", FALSE,0,0x00000000,TRUE),
200 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),
201 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),
202 HOWTO(R_SPARC_RELATIVE, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
203 HOWTO(R_SPARC_UA32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA32", FALSE,0,0xffffffff,TRUE),
204 HOWTO(R_SPARC_PLT32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT32", FALSE,0,0xffffffff,TRUE),
205 HOWTO(R_SPARC_HIPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_HIPLT22", FALSE,0,0x00000000,TRUE),
206 HOWTO(R_SPARC_LOPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_LOPLT10", FALSE,0,0x00000000,TRUE),
207 HOWTO(R_SPARC_PCPLT32, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT32", FALSE,0,0x00000000,TRUE),
208 HOWTO(R_SPARC_PCPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT22", FALSE,0,0x00000000,TRUE),
209 HOWTO(R_SPARC_PCPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsup_reloc, "R_SPARC_PCPLT10", FALSE,0,0x00000000,TRUE),
210 HOWTO(R_SPARC_10, 0,2,10,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", FALSE,0,0x000003ff,TRUE),
211 HOWTO(R_SPARC_11, 0,2,11,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", FALSE,0,0x000007ff,TRUE),
212 HOWTO(R_SPARC_64, 0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_64", FALSE,0,MINUS_ONE, TRUE),
213 HOWTO(R_SPARC_OLO10, 0,2,13,FALSE,0,complain_overflow_signed, sparc_elf_notsup_reloc, "R_SPARC_OLO10", FALSE,0,0x00001fff,TRUE),
214 HOWTO(R_SPARC_HH22, 42,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_HH22", FALSE,0,0x003fffff,TRUE),
215 HOWTO(R_SPARC_HM10, 32,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HM10", FALSE,0,0x000003ff,TRUE),
216 HOWTO(R_SPARC_LM22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LM22", FALSE,0,0x003fffff,TRUE),
217 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),
218 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),
219 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),
220 HOWTO(R_SPARC_WDISP16, 2,2,16,TRUE, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", FALSE,0,0x00000000,TRUE),
221 HOWTO(R_SPARC_WDISP19, 2,2,19,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", FALSE,0,0x0007ffff,TRUE),
222 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),
223 HOWTO(R_SPARC_7, 0,2, 7,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", FALSE,0,0x0000007f,TRUE),
224 HOWTO(R_SPARC_5, 0,2, 5,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", FALSE,0,0x0000001f,TRUE),
225 HOWTO(R_SPARC_6, 0,2, 6,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", FALSE,0,0x0000003f,TRUE),
226 HOWTO(R_SPARC_DISP64, 0,4,64,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP64", FALSE,0,MINUS_ONE, TRUE),
227 HOWTO(R_SPARC_PLT64, 0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT64", FALSE,0,MINUS_ONE, TRUE),
228 HOWTO(R_SPARC_HIX22, 0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_HIX22", FALSE,0,MINUS_ONE, FALSE),
229 HOWTO(R_SPARC_LOX10, 0,4, 0,FALSE,0,complain_overflow_dont, sparc_elf_lox10_reloc, "R_SPARC_LOX10", FALSE,0,MINUS_ONE, FALSE),
230 HOWTO(R_SPARC_H44, 22,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc, "R_SPARC_H44", FALSE,0,0x003fffff,FALSE),
231 HOWTO(R_SPARC_M44, 12,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_M44", FALSE,0,0x000003ff,FALSE),
232 HOWTO(R_SPARC_L44, 0,2,13,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_L44", FALSE,0,0x00000fff,FALSE),
233 HOWTO(R_SPARC_REGISTER, 0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_REGISTER",FALSE,0,MINUS_ONE, FALSE),
234 HOWTO(R_SPARC_UA64, 0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA64", FALSE,0,MINUS_ONE, TRUE),
235 HOWTO(R_SPARC_UA16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", FALSE,0,0x0000ffff,TRUE),
236 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),
237 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),
238 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),
239 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),
240 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),
241 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),
242 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),
243 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),
244 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),
245 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),
246 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),
247 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),
248 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),
249 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),
250 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),
251 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),
252 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),
253 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),
254 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),
255 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),
256 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),
257 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),
258 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),
259 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)
261 static reloc_howto_type sparc_vtinherit_howto =
262 HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", FALSE,0, 0, FALSE);
263 static reloc_howto_type sparc_vtentry_howto =
264 HOWTO (R_SPARC_GNU_VTENTRY, 0,2,0,FALSE,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_SPARC_GNU_VTENTRY", FALSE,0,0, FALSE);
265 static reloc_howto_type sparc_rev32_howto =
266 HOWTO(R_SPARC_REV32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", FALSE,0,0xffffffff,TRUE);
268 struct elf_reloc_map {
269 bfd_reloc_code_real_type bfd_reloc_val;
270 unsigned char elf_reloc_val;
273 static const struct elf_reloc_map sparc_reloc_map[] =
275 { BFD_RELOC_NONE, R_SPARC_NONE, },
276 { BFD_RELOC_16, R_SPARC_16, },
277 { BFD_RELOC_16_PCREL, R_SPARC_DISP16 },
278 { BFD_RELOC_8, R_SPARC_8 },
279 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
280 { BFD_RELOC_CTOR, R_SPARC_64 },
281 { BFD_RELOC_32, R_SPARC_32 },
282 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
283 { BFD_RELOC_HI22, R_SPARC_HI22 },
284 { BFD_RELOC_LO10, R_SPARC_LO10, },
285 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
286 { BFD_RELOC_64_PCREL, R_SPARC_DISP64 },
287 { BFD_RELOC_SPARC22, R_SPARC_22 },
288 { BFD_RELOC_SPARC13, R_SPARC_13 },
289 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
290 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
291 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
292 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
293 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
294 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
295 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
296 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
297 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
298 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
299 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
300 { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
301 { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
302 { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
303 { BFD_RELOC_SPARC_10, R_SPARC_10 },
304 { BFD_RELOC_SPARC_11, R_SPARC_11 },
305 { BFD_RELOC_SPARC_64, R_SPARC_64 },
306 { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
307 { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
308 { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
309 { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
310 { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
311 { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
312 { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
313 { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
314 { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
315 { BFD_RELOC_SPARC_7, R_SPARC_7 },
316 { BFD_RELOC_SPARC_5, R_SPARC_5 },
317 { BFD_RELOC_SPARC_6, R_SPARC_6 },
318 { BFD_RELOC_SPARC_DISP64, R_SPARC_DISP64 },
319 { BFD_RELOC_SPARC_TLS_GD_HI22, R_SPARC_TLS_GD_HI22 },
320 { BFD_RELOC_SPARC_TLS_GD_LO10, R_SPARC_TLS_GD_LO10 },
321 { BFD_RELOC_SPARC_TLS_GD_ADD, R_SPARC_TLS_GD_ADD },
322 { BFD_RELOC_SPARC_TLS_GD_CALL, R_SPARC_TLS_GD_CALL },
323 { BFD_RELOC_SPARC_TLS_LDM_HI22, R_SPARC_TLS_LDM_HI22 },
324 { BFD_RELOC_SPARC_TLS_LDM_LO10, R_SPARC_TLS_LDM_LO10 },
325 { BFD_RELOC_SPARC_TLS_LDM_ADD, R_SPARC_TLS_LDM_ADD },
326 { BFD_RELOC_SPARC_TLS_LDM_CALL, R_SPARC_TLS_LDM_CALL },
327 { BFD_RELOC_SPARC_TLS_LDO_HIX22, R_SPARC_TLS_LDO_HIX22 },
328 { BFD_RELOC_SPARC_TLS_LDO_LOX10, R_SPARC_TLS_LDO_LOX10 },
329 { BFD_RELOC_SPARC_TLS_LDO_ADD, R_SPARC_TLS_LDO_ADD },
330 { BFD_RELOC_SPARC_TLS_IE_HI22, R_SPARC_TLS_IE_HI22 },
331 { BFD_RELOC_SPARC_TLS_IE_LO10, R_SPARC_TLS_IE_LO10 },
332 { BFD_RELOC_SPARC_TLS_IE_LD, R_SPARC_TLS_IE_LD },
333 { BFD_RELOC_SPARC_TLS_IE_LDX, R_SPARC_TLS_IE_LDX },
334 { BFD_RELOC_SPARC_TLS_IE_ADD, R_SPARC_TLS_IE_ADD },
335 { BFD_RELOC_SPARC_TLS_LE_HIX22, R_SPARC_TLS_LE_HIX22 },
336 { BFD_RELOC_SPARC_TLS_LE_LOX10, R_SPARC_TLS_LE_LOX10 },
337 { BFD_RELOC_SPARC_TLS_DTPMOD32, R_SPARC_TLS_DTPMOD32 },
338 { BFD_RELOC_SPARC_TLS_DTPMOD64, R_SPARC_TLS_DTPMOD64 },
339 { BFD_RELOC_SPARC_TLS_DTPOFF32, R_SPARC_TLS_DTPOFF32 },
340 { BFD_RELOC_SPARC_TLS_DTPOFF64, R_SPARC_TLS_DTPOFF64 },
341 { BFD_RELOC_SPARC_TLS_TPOFF32, R_SPARC_TLS_TPOFF32 },
342 { BFD_RELOC_SPARC_TLS_TPOFF64, R_SPARC_TLS_TPOFF64 },
343 { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 },
344 { BFD_RELOC_SPARC_PLT64, R_SPARC_PLT64 },
345 { BFD_RELOC_SPARC_HIX22, R_SPARC_HIX22 },
346 { BFD_RELOC_SPARC_LOX10, R_SPARC_LOX10 },
347 { BFD_RELOC_SPARC_H44, R_SPARC_H44 },
348 { BFD_RELOC_SPARC_M44, R_SPARC_M44 },
349 { BFD_RELOC_SPARC_L44, R_SPARC_L44 },
350 { BFD_RELOC_SPARC_REGISTER, R_SPARC_REGISTER },
351 { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
352 { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
353 { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
356 reloc_howto_type *
357 _bfd_sparc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
358 bfd_reloc_code_real_type code)
360 unsigned int i;
362 switch (code)
364 case BFD_RELOC_VTABLE_INHERIT:
365 return &sparc_vtinherit_howto;
367 case BFD_RELOC_VTABLE_ENTRY:
368 return &sparc_vtentry_howto;
370 case BFD_RELOC_SPARC_REV32:
371 return &sparc_rev32_howto;
373 default:
374 for (i = 0;
375 i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map);
376 i++)
378 if (sparc_reloc_map[i].bfd_reloc_val == code)
379 return (_bfd_sparc_elf_howto_table
380 + (int) sparc_reloc_map[i].elf_reloc_val);
383 bfd_set_error (bfd_error_bad_value);
384 return NULL;
387 reloc_howto_type *
388 _bfd_sparc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
389 const char *r_name)
391 unsigned int i;
393 for (i = 0;
394 i < (sizeof (_bfd_sparc_elf_howto_table)
395 / sizeof (_bfd_sparc_elf_howto_table[0]));
396 i++)
397 if (_bfd_sparc_elf_howto_table[i].name != NULL
398 && strcasecmp (_bfd_sparc_elf_howto_table[i].name, r_name) == 0)
399 return &_bfd_sparc_elf_howto_table[i];
401 if (strcasecmp (sparc_vtinherit_howto.name, r_name) == 0)
402 return &sparc_vtinherit_howto;
403 if (strcasecmp (sparc_vtentry_howto.name, r_name) == 0)
404 return &sparc_vtentry_howto;
405 if (strcasecmp (sparc_rev32_howto.name, r_name) == 0)
406 return &sparc_rev32_howto;
408 return NULL;
411 reloc_howto_type *
412 _bfd_sparc_elf_info_to_howto_ptr (unsigned int r_type)
414 switch (r_type)
416 case R_SPARC_GNU_VTINHERIT:
417 return &sparc_vtinherit_howto;
419 case R_SPARC_GNU_VTENTRY:
420 return &sparc_vtentry_howto;
422 case R_SPARC_REV32:
423 return &sparc_rev32_howto;
425 default:
426 if (r_type >= (unsigned int) R_SPARC_max_std)
428 (*_bfd_error_handler) (_("invalid relocation type %d"),
429 (int) r_type);
430 r_type = R_SPARC_NONE;
432 return &_bfd_sparc_elf_howto_table[r_type];
436 /* Both 32-bit and 64-bit sparc encode this in an identical manner,
437 so just take advantage of that. */
438 #define SPARC_ELF_R_TYPE(r_info) \
439 ((r_info) & 0xff)
441 void
442 _bfd_sparc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
443 Elf_Internal_Rela *dst)
445 unsigned int r_type = SPARC_ELF_R_TYPE (dst->r_info);
447 cache_ptr->howto = _bfd_sparc_elf_info_to_howto_ptr (r_type);
451 /* The nop opcode we use. */
452 #define SPARC_NOP 0x01000000
454 #define SPARC_INSN_BYTES 4
456 /* The SPARC linker needs to keep track of the number of relocs that it
457 decides to copy as dynamic relocs in check_relocs for each symbol.
458 This is so that it can later discard them if they are found to be
459 unnecessary. We store the information in a field extending the
460 regular ELF linker hash table. */
462 struct _bfd_sparc_elf_dyn_relocs
464 struct _bfd_sparc_elf_dyn_relocs *next;
466 /* The input section of the reloc. */
467 asection *sec;
469 /* Total number of relocs copied for the input section. */
470 bfd_size_type count;
472 /* Number of pc-relative relocs copied for the input section. */
473 bfd_size_type pc_count;
476 /* SPARC ELF linker hash entry. */
478 struct _bfd_sparc_elf_link_hash_entry
480 struct elf_link_hash_entry elf;
482 /* Track dynamic relocs copied for this symbol. */
483 struct _bfd_sparc_elf_dyn_relocs *dyn_relocs;
485 #define GOT_UNKNOWN 0
486 #define GOT_NORMAL 1
487 #define GOT_TLS_GD 2
488 #define GOT_TLS_IE 3
489 unsigned char tls_type;
492 #define _bfd_sparc_elf_hash_entry(ent) ((struct _bfd_sparc_elf_link_hash_entry *)(ent))
494 struct _bfd_sparc_elf_obj_tdata
496 struct elf_obj_tdata root;
498 /* tls_type for each local got entry. */
499 char *local_got_tls_type;
501 /* TRUE if TLS GD relocs has been seen for this object. */
502 bfd_boolean has_tlsgd;
505 #define _bfd_sparc_elf_tdata(abfd) \
506 ((struct _bfd_sparc_elf_obj_tdata *) (abfd)->tdata.any)
508 #define _bfd_sparc_elf_local_got_tls_type(abfd) \
509 (_bfd_sparc_elf_tdata (abfd)->local_got_tls_type)
511 bfd_boolean
512 _bfd_sparc_elf_mkobject (bfd *abfd)
514 if (abfd->tdata.any == NULL)
516 bfd_size_type amt = sizeof (struct _bfd_sparc_elf_obj_tdata);
517 abfd->tdata.any = bfd_zalloc (abfd, amt);
518 if (abfd->tdata.any == NULL)
519 return FALSE;
521 return bfd_elf_mkobject (abfd);
524 static void
525 sparc_put_word_32 (bfd *bfd, bfd_vma val, void *ptr)
527 bfd_put_32 (bfd, val, ptr);
530 static void
531 sparc_put_word_64 (bfd *bfd, bfd_vma val, void *ptr)
533 bfd_put_64 (bfd, val, ptr);
536 static void
537 sparc_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
539 const struct elf_backend_data *bed;
540 bfd_byte *loc;
542 bed = get_elf_backend_data (abfd);
543 loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
544 bed->s->swap_reloca_out (abfd, rel, loc);
547 static bfd_vma
548 sparc_elf_r_info_64 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
549 bfd_vma index ATTRIBUTE_UNUSED,
550 bfd_vma type ATTRIBUTE_UNUSED)
552 return ELF64_R_INFO (index,
553 (in_rel ?
554 ELF64_R_TYPE_INFO (ELF64_R_TYPE_DATA (in_rel->r_info),
555 type) : type));
558 static bfd_vma
559 sparc_elf_r_info_32 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
560 bfd_vma index, bfd_vma type)
562 return ELF32_R_INFO (index, type);
565 static bfd_vma
566 sparc_elf_r_symndx_64 (bfd_vma r_info)
568 bfd_vma r_symndx = ELF32_R_SYM (r_info);
569 return (r_symndx >> 24);
572 static bfd_vma
573 sparc_elf_r_symndx_32 (bfd_vma r_info)
575 return ELF32_R_SYM (r_info);
578 /* PLT/GOT stuff */
580 #define PLT32_ENTRY_SIZE 12
581 #define PLT32_HEADER_SIZE (4 * PLT32_ENTRY_SIZE)
583 /* The first four entries in a 32-bit procedure linkage table are reserved,
584 and the initial contents are unimportant (we zero them out).
585 Subsequent entries look like this. See the SVR4 ABI SPARC
586 supplement to see how this works. */
588 /* sethi %hi(.-.plt0),%g1. We fill in the address later. */
589 #define PLT32_ENTRY_WORD0 0x03000000
590 /* b,a .plt0. We fill in the offset later. */
591 #define PLT32_ENTRY_WORD1 0x30800000
592 /* nop. */
593 #define PLT32_ENTRY_WORD2 SPARC_NOP
595 static int
596 sparc32_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
597 bfd_vma max ATTRIBUTE_UNUSED,
598 bfd_vma *r_offset)
600 bfd_put_32 (output_bfd,
601 PLT32_ENTRY_WORD0 + offset,
602 splt->contents + offset);
603 bfd_put_32 (output_bfd,
604 (PLT32_ENTRY_WORD1
605 + (((- (offset + 4)) >> 2) & 0x3fffff)),
606 splt->contents + offset + 4);
607 bfd_put_32 (output_bfd, (bfd_vma) PLT32_ENTRY_WORD2,
608 splt->contents + offset + 8);
610 *r_offset = offset;
612 return offset / PLT32_ENTRY_SIZE - 4;
615 /* Both the headers and the entries are icache aligned. */
616 #define PLT64_ENTRY_SIZE 32
617 #define PLT64_HEADER_SIZE (4 * PLT64_ENTRY_SIZE)
618 #define PLT64_LARGE_THRESHOLD 32768
620 static int
621 sparc64_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
622 bfd_vma max, bfd_vma *r_offset)
624 unsigned char *entry = splt->contents + offset;
625 const unsigned int nop = SPARC_NOP;
626 int index;
628 if (offset < (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
630 unsigned int sethi, ba;
632 *r_offset = offset;
634 index = (offset / PLT64_ENTRY_SIZE);
636 sethi = 0x03000000 | (index * PLT64_ENTRY_SIZE);
637 ba = 0x30680000
638 | (((splt->contents + PLT64_ENTRY_SIZE) - (entry + 4)) / 4 & 0x7ffff);
640 bfd_put_32 (output_bfd, (bfd_vma) sethi, entry);
641 bfd_put_32 (output_bfd, (bfd_vma) ba, entry + 4);
642 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 8);
643 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 12);
644 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 16);
645 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 20);
646 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 24);
647 bfd_put_32 (output_bfd, (bfd_vma) nop, entry + 28);
649 else
651 unsigned char *ptr;
652 unsigned int ldx;
653 int block, last_block, ofs, last_ofs, chunks_this_block;
654 const int insn_chunk_size = (6 * 4);
655 const int ptr_chunk_size = (1 * 8);
656 const int entries_per_block = 160;
657 const int block_size = entries_per_block * (insn_chunk_size
658 + ptr_chunk_size);
660 /* Entries 32768 and higher are grouped into blocks of 160.
661 The blocks are further subdivided into 160 sequences of
662 6 instructions and 160 pointers. If a block does not require
663 the full 160 entries, let's say it requires N, then there
664 will be N sequences of 6 instructions and N pointers. */
666 offset -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
667 max -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
669 block = offset / block_size;
670 last_block = max / block_size;
671 if (block != last_block)
673 chunks_this_block = 160;
675 else
677 last_ofs = max % block_size;
678 chunks_this_block = last_ofs / (insn_chunk_size + ptr_chunk_size);
681 ofs = offset % block_size;
683 index = (PLT64_LARGE_THRESHOLD +
684 (block * 160) +
685 (ofs / insn_chunk_size));
687 ptr = splt->contents
688 + (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
689 + (block * block_size)
690 + (chunks_this_block * insn_chunk_size)
691 + (ofs / insn_chunk_size) * ptr_chunk_size;
693 *r_offset = (bfd_vma) (ptr - splt->contents);
695 ldx = 0xc25be000 | ((ptr - (entry+4)) & 0x1fff);
697 /* mov %o7,%g5
698 call .+8
700 ldx [%o7+P],%g1
701 jmpl %o7+%g1,%g1
702 mov %g5,%o7 */
703 bfd_put_32 (output_bfd, (bfd_vma) 0x8a10000f, entry);
704 bfd_put_32 (output_bfd, (bfd_vma) 0x40000002, entry + 4);
705 bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP, entry + 8);
706 bfd_put_32 (output_bfd, (bfd_vma) ldx, entry + 12);
707 bfd_put_32 (output_bfd, (bfd_vma) 0x83c3c001, entry + 16);
708 bfd_put_32 (output_bfd, (bfd_vma) 0x9e100005, entry + 20);
710 bfd_put_64 (output_bfd, (bfd_vma) (splt->contents - (entry + 4)), ptr);
713 return index - 4;
716 /* The format of the first PLT entry in a VxWorks executable. */
717 static const bfd_vma sparc_vxworks_exec_plt0_entry[] =
719 0x05000000, /* sethi %hi(_GLOBAL_OFFSET_TABLE_+8), %g2 */
720 0x8410a000, /* or %g2, %lo(_GLOBAL_OFFSET_TABLE_+8), %g2 */
721 0xc4008000, /* ld [ %g2 ], %g2 */
722 0x81c08000, /* jmp %g2 */
723 0x01000000 /* nop */
726 /* The format of subsequent PLT entries. */
727 static const bfd_vma sparc_vxworks_exec_plt_entry[] =
729 0x03000000, /* sethi %hi(_GLOBAL_OFFSET_TABLE_+f@got), %g1 */
730 0x82106000, /* or %g1, %lo(_GLOBAL_OFFSET_TABLE_+f@got), %g1 */
731 0xc2004000, /* ld [ %g1 ], %g1 */
732 0x81c04000, /* jmp %g1 */
733 0x01000000, /* nop */
734 0x03000000, /* sethi %hi(f@pltindex), %g1 */
735 0x10800000, /* b _PLT_resolve */
736 0x82106000 /* or %g1, %lo(f@pltindex), %g1 */
739 /* The format of the first PLT entry in a VxWorks shared object. */
740 static const bfd_vma sparc_vxworks_shared_plt0_entry[] =
742 0xc405e008, /* ld [ %l7 + 8 ], %g2 */
743 0x81c08000, /* jmp %g2 */
744 0x01000000 /* nop */
747 /* The format of subsequent PLT entries. */
748 static const bfd_vma sparc_vxworks_shared_plt_entry[] =
750 0x03000000, /* sethi %hi(f@got), %g1 */
751 0x82106000, /* or %g1, %lo(f@got), %g1 */
752 0xc205c001, /* ld [ %l7 + %g1 ], %g1 */
753 0x81c04000, /* jmp %g1 */
754 0x01000000, /* nop */
755 0x03000000, /* sethi %hi(f@pltindex), %g1 */
756 0x10800000, /* b _PLT_resolve */
757 0x82106000 /* or %g1, %lo(f@pltindex), %g1 */
760 #define SPARC_ELF_PUT_WORD(htab, bfd, val, ptr) \
761 htab->put_word(bfd, val, ptr)
763 #define SPARC_ELF_R_INFO(htab, in_rel, index, type) \
764 htab->r_info(in_rel, index, type)
766 #define SPARC_ELF_R_SYMNDX(htab, r_info) \
767 htab->r_symndx(r_info)
769 #define SPARC_ELF_WORD_BYTES(htab) \
770 htab->bytes_per_word
772 #define SPARC_ELF_RELA_BYTES(htab) \
773 htab->bytes_per_rela
775 #define SPARC_ELF_DTPOFF_RELOC(htab) \
776 htab->dtpoff_reloc
778 #define SPARC_ELF_DTPMOD_RELOC(htab) \
779 htab->dtpmod_reloc
781 #define SPARC_ELF_TPOFF_RELOC(htab) \
782 htab->tpoff_reloc
784 #define SPARC_ELF_BUILD_PLT_ENTRY(htab, obfd, splt, off, max, r_off) \
785 htab->build_plt_entry (obfd, splt, off, max, r_off)
787 /* Create an entry in an SPARC ELF linker hash table. */
789 static struct bfd_hash_entry *
790 link_hash_newfunc (struct bfd_hash_entry *entry,
791 struct bfd_hash_table *table, const char *string)
793 /* Allocate the structure if it has not already been allocated by a
794 subclass. */
795 if (entry == NULL)
797 entry = bfd_hash_allocate (table,
798 sizeof (struct _bfd_sparc_elf_link_hash_entry));
799 if (entry == NULL)
800 return entry;
803 /* Call the allocation method of the superclass. */
804 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
805 if (entry != NULL)
807 struct _bfd_sparc_elf_link_hash_entry *eh;
809 eh = (struct _bfd_sparc_elf_link_hash_entry *) entry;
810 eh->dyn_relocs = NULL;
811 eh->tls_type = GOT_UNKNOWN;
814 return entry;
817 /* The name of the dynamic interpreter. This is put in the .interp
818 section. */
820 #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
821 #define ELF64_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1"
823 /* Create a SPARC ELF linker hash table. */
825 struct bfd_link_hash_table *
826 _bfd_sparc_elf_link_hash_table_create (bfd *abfd)
828 struct _bfd_sparc_elf_link_hash_table *ret;
829 bfd_size_type amt = sizeof (struct _bfd_sparc_elf_link_hash_table);
831 ret = (struct _bfd_sparc_elf_link_hash_table *) bfd_zmalloc (amt);
832 if (ret == NULL)
833 return NULL;
835 if (ABI_64_P (abfd))
837 ret->put_word = sparc_put_word_64;
838 ret->r_info = sparc_elf_r_info_64;
839 ret->r_symndx = sparc_elf_r_symndx_64;
840 ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF64;
841 ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD64;
842 ret->tpoff_reloc = R_SPARC_TLS_TPOFF64;
843 ret->word_align_power = 3;
844 ret->align_power_max = 4;
845 ret->bytes_per_word = 8;
846 ret->bytes_per_rela = sizeof (Elf64_External_Rela);
847 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
848 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
850 else
852 ret->put_word = sparc_put_word_32;
853 ret->r_info = sparc_elf_r_info_32;
854 ret->r_symndx = sparc_elf_r_symndx_32;
855 ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF32;
856 ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD32;
857 ret->tpoff_reloc = R_SPARC_TLS_TPOFF32;
858 ret->word_align_power = 2;
859 ret->align_power_max = 3;
860 ret->bytes_per_word = 4;
861 ret->bytes_per_rela = sizeof (Elf32_External_Rela);
862 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
863 ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
866 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
867 sizeof (struct _bfd_sparc_elf_link_hash_entry)))
869 free (ret);
870 return NULL;
873 return &ret->elf.root;
876 /* Create .got and .rela.got sections in DYNOBJ, and set up
877 shortcuts to them in our hash table. */
879 static bfd_boolean
880 create_got_section (bfd *dynobj, struct bfd_link_info *info)
882 struct _bfd_sparc_elf_link_hash_table *htab;
884 if (! _bfd_elf_create_got_section (dynobj, info))
885 return FALSE;
887 htab = _bfd_sparc_elf_hash_table (info);
888 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
889 BFD_ASSERT (htab->sgot != NULL);
891 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
892 SEC_ALLOC
893 | SEC_LOAD
894 | SEC_HAS_CONTENTS
895 | SEC_IN_MEMORY
896 | SEC_LINKER_CREATED
897 | SEC_READONLY);
898 if (htab->srelgot == NULL
899 || ! bfd_set_section_alignment (dynobj, htab->srelgot,
900 htab->word_align_power))
901 return FALSE;
903 if (htab->is_vxworks)
905 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
906 if (!htab->sgotplt)
907 return FALSE;
910 return TRUE;
913 /* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and
914 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
915 hash table. */
917 bfd_boolean
918 _bfd_sparc_elf_create_dynamic_sections (bfd *dynobj,
919 struct bfd_link_info *info)
921 struct _bfd_sparc_elf_link_hash_table *htab;
923 htab = _bfd_sparc_elf_hash_table (info);
924 if (!htab->sgot && !create_got_section (dynobj, info))
925 return FALSE;
927 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
928 return FALSE;
930 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
931 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
932 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
933 if (!info->shared)
934 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
936 if (htab->is_vxworks)
938 if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
939 return FALSE;
940 if (info->shared)
942 htab->plt_header_size
943 = 4 * ARRAY_SIZE (sparc_vxworks_shared_plt0_entry);
944 htab->plt_entry_size
945 = 4 * ARRAY_SIZE (sparc_vxworks_shared_plt_entry);
947 else
949 htab->plt_header_size
950 = 4 * ARRAY_SIZE (sparc_vxworks_exec_plt0_entry);
951 htab->plt_entry_size
952 = 4 * ARRAY_SIZE (sparc_vxworks_exec_plt_entry);
955 else
957 if (ABI_64_P (dynobj))
959 htab->build_plt_entry = sparc64_plt_entry_build;
960 htab->plt_header_size = PLT64_HEADER_SIZE;
961 htab->plt_entry_size = PLT64_ENTRY_SIZE;
963 else
965 htab->build_plt_entry = sparc32_plt_entry_build;
966 htab->plt_header_size = PLT32_HEADER_SIZE;
967 htab->plt_entry_size = PLT32_ENTRY_SIZE;
971 if (!htab->splt || !htab->srelplt || !htab->sdynbss
972 || (!info->shared && !htab->srelbss))
973 abort ();
975 return TRUE;
978 /* Copy the extra info we tack onto an elf_link_hash_entry. */
980 void
981 _bfd_sparc_elf_copy_indirect_symbol (struct bfd_link_info *info,
982 struct elf_link_hash_entry *dir,
983 struct elf_link_hash_entry *ind)
985 struct _bfd_sparc_elf_link_hash_entry *edir, *eind;
987 edir = (struct _bfd_sparc_elf_link_hash_entry *) dir;
988 eind = (struct _bfd_sparc_elf_link_hash_entry *) ind;
990 if (eind->dyn_relocs != NULL)
992 if (edir->dyn_relocs != NULL)
994 struct _bfd_sparc_elf_dyn_relocs **pp;
995 struct _bfd_sparc_elf_dyn_relocs *p;
997 /* Add reloc counts against the indirect sym to the direct sym
998 list. Merge any entries against the same section. */
999 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1001 struct _bfd_sparc_elf_dyn_relocs *q;
1003 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1004 if (q->sec == p->sec)
1006 q->pc_count += p->pc_count;
1007 q->count += p->count;
1008 *pp = p->next;
1009 break;
1011 if (q == NULL)
1012 pp = &p->next;
1014 *pp = edir->dyn_relocs;
1017 edir->dyn_relocs = eind->dyn_relocs;
1018 eind->dyn_relocs = NULL;
1021 if (ind->root.type == bfd_link_hash_indirect
1022 && dir->got.refcount <= 0)
1024 edir->tls_type = eind->tls_type;
1025 eind->tls_type = GOT_UNKNOWN;
1027 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1030 static int
1031 sparc_elf_tls_transition (struct bfd_link_info *info, bfd *abfd,
1032 int r_type, int is_local)
1034 if (! ABI_64_P (abfd)
1035 && r_type == R_SPARC_TLS_GD_HI22
1036 && ! _bfd_sparc_elf_tdata (abfd)->has_tlsgd)
1037 r_type = R_SPARC_REV32;
1039 if (info->shared)
1040 return r_type;
1042 switch (r_type)
1044 case R_SPARC_TLS_GD_HI22:
1045 if (is_local)
1046 return R_SPARC_TLS_LE_HIX22;
1047 return R_SPARC_TLS_IE_HI22;
1048 case R_SPARC_TLS_GD_LO10:
1049 if (is_local)
1050 return R_SPARC_TLS_LE_LOX10;
1051 return R_SPARC_TLS_IE_LO10;
1052 case R_SPARC_TLS_IE_HI22:
1053 if (is_local)
1054 return R_SPARC_TLS_LE_HIX22;
1055 return r_type;
1056 case R_SPARC_TLS_IE_LO10:
1057 if (is_local)
1058 return R_SPARC_TLS_LE_LOX10;
1059 return r_type;
1060 case R_SPARC_TLS_LDM_HI22:
1061 return R_SPARC_TLS_LE_HIX22;
1062 case R_SPARC_TLS_LDM_LO10:
1063 return R_SPARC_TLS_LE_LOX10;
1066 return r_type;
1069 /* Look through the relocs for a section during the first phase, and
1070 allocate space in the global offset table or procedure linkage
1071 table. */
1073 bfd_boolean
1074 _bfd_sparc_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
1075 asection *sec, const Elf_Internal_Rela *relocs)
1077 struct _bfd_sparc_elf_link_hash_table *htab;
1078 Elf_Internal_Shdr *symtab_hdr;
1079 struct elf_link_hash_entry **sym_hashes;
1080 bfd_vma *local_got_offsets;
1081 const Elf_Internal_Rela *rel;
1082 const Elf_Internal_Rela *rel_end;
1083 asection *sreloc;
1084 int num_relocs;
1085 bfd_boolean checked_tlsgd = FALSE;
1087 if (info->relocatable)
1088 return TRUE;
1090 htab = _bfd_sparc_elf_hash_table (info);
1091 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1092 sym_hashes = elf_sym_hashes (abfd);
1093 local_got_offsets = elf_local_got_offsets (abfd);
1095 sreloc = NULL;
1097 if (ABI_64_P (abfd))
1098 num_relocs = NUM_SHDR_ENTRIES (& elf_section_data (sec)->rel_hdr);
1099 else
1100 num_relocs = sec->reloc_count;
1101 rel_end = relocs + num_relocs;
1102 for (rel = relocs; rel < rel_end; rel++)
1104 unsigned int r_type;
1105 unsigned long r_symndx;
1106 struct elf_link_hash_entry *h;
1108 r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
1109 r_type = SPARC_ELF_R_TYPE (rel->r_info);
1111 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1113 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1114 abfd, r_symndx);
1115 return FALSE;
1118 if (r_symndx < symtab_hdr->sh_info)
1119 h = NULL;
1120 else
1122 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1123 while (h->root.type == bfd_link_hash_indirect
1124 || h->root.type == bfd_link_hash_warning)
1125 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1128 /* Compatibility with old R_SPARC_REV32 reloc conflicting
1129 with R_SPARC_TLS_GD_HI22. */
1130 if (! ABI_64_P (abfd) && ! checked_tlsgd)
1131 switch (r_type)
1133 case R_SPARC_TLS_GD_HI22:
1135 const Elf_Internal_Rela *relt;
1137 for (relt = rel + 1; relt < rel_end; relt++)
1138 if (ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_LO10
1139 || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_ADD
1140 || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_CALL)
1141 break;
1142 checked_tlsgd = TRUE;
1143 _bfd_sparc_elf_tdata (abfd)->has_tlsgd = relt < rel_end;
1145 break;
1146 case R_SPARC_TLS_GD_LO10:
1147 case R_SPARC_TLS_GD_ADD:
1148 case R_SPARC_TLS_GD_CALL:
1149 checked_tlsgd = TRUE;
1150 _bfd_sparc_elf_tdata (abfd)->has_tlsgd = TRUE;
1151 break;
1154 r_type = sparc_elf_tls_transition (info, abfd, r_type, h == NULL);
1155 switch (r_type)
1157 case R_SPARC_TLS_LDM_HI22:
1158 case R_SPARC_TLS_LDM_LO10:
1159 htab->tls_ldm_got.refcount += 1;
1160 break;
1162 case R_SPARC_TLS_LE_HIX22:
1163 case R_SPARC_TLS_LE_LOX10:
1164 if (info->shared)
1165 goto r_sparc_plt32;
1166 break;
1168 case R_SPARC_TLS_IE_HI22:
1169 case R_SPARC_TLS_IE_LO10:
1170 if (info->shared)
1171 info->flags |= DF_STATIC_TLS;
1172 /* Fall through */
1174 case R_SPARC_GOT10:
1175 case R_SPARC_GOT13:
1176 case R_SPARC_GOT22:
1177 case R_SPARC_TLS_GD_HI22:
1178 case R_SPARC_TLS_GD_LO10:
1179 /* This symbol requires a global offset table entry. */
1181 int tls_type, old_tls_type;
1183 switch (r_type)
1185 default:
1186 case R_SPARC_GOT10:
1187 case R_SPARC_GOT13:
1188 case R_SPARC_GOT22:
1189 tls_type = GOT_NORMAL;
1190 break;
1191 case R_SPARC_TLS_GD_HI22:
1192 case R_SPARC_TLS_GD_LO10:
1193 tls_type = GOT_TLS_GD;
1194 break;
1195 case R_SPARC_TLS_IE_HI22:
1196 case R_SPARC_TLS_IE_LO10:
1197 tls_type = GOT_TLS_IE;
1198 break;
1201 if (h != NULL)
1203 h->got.refcount += 1;
1204 old_tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
1206 else
1208 bfd_signed_vma *local_got_refcounts;
1210 /* This is a global offset table entry for a local symbol. */
1211 local_got_refcounts = elf_local_got_refcounts (abfd);
1212 if (local_got_refcounts == NULL)
1214 bfd_size_type size;
1216 size = symtab_hdr->sh_info;
1217 size *= (sizeof (bfd_signed_vma) + sizeof(char));
1218 local_got_refcounts = ((bfd_signed_vma *)
1219 bfd_zalloc (abfd, size));
1220 if (local_got_refcounts == NULL)
1221 return FALSE;
1222 elf_local_got_refcounts (abfd) = local_got_refcounts;
1223 _bfd_sparc_elf_local_got_tls_type (abfd)
1224 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1226 local_got_refcounts[r_symndx] += 1;
1227 old_tls_type = _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx];
1230 /* If a TLS symbol is accessed using IE at least once,
1231 there is no point to use dynamic model for it. */
1232 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1233 && (old_tls_type != GOT_TLS_GD
1234 || tls_type != GOT_TLS_IE))
1236 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
1237 tls_type = old_tls_type;
1238 else
1240 (*_bfd_error_handler)
1241 (_("%B: `%s' accessed both as normal and thread local symbol"),
1242 abfd, h ? h->root.root.string : "<local>");
1243 return FALSE;
1247 if (old_tls_type != tls_type)
1249 if (h != NULL)
1250 _bfd_sparc_elf_hash_entry (h)->tls_type = tls_type;
1251 else
1252 _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
1256 if (htab->sgot == NULL)
1258 if (htab->elf.dynobj == NULL)
1259 htab->elf.dynobj = abfd;
1260 if (!create_got_section (htab->elf.dynobj, info))
1261 return FALSE;
1263 break;
1265 case R_SPARC_TLS_GD_CALL:
1266 case R_SPARC_TLS_LDM_CALL:
1267 if (info->shared)
1269 /* These are basically R_SPARC_TLS_WPLT30 relocs against
1270 __tls_get_addr. */
1271 struct bfd_link_hash_entry *bh = NULL;
1272 if (! _bfd_generic_link_add_one_symbol (info, abfd,
1273 "__tls_get_addr", 0,
1274 bfd_und_section_ptr, 0,
1275 NULL, FALSE, FALSE,
1276 &bh))
1277 return FALSE;
1278 h = (struct elf_link_hash_entry *) bh;
1280 else
1281 break;
1282 /* Fall through */
1284 case R_SPARC_PLT32:
1285 case R_SPARC_WPLT30:
1286 case R_SPARC_HIPLT22:
1287 case R_SPARC_LOPLT10:
1288 case R_SPARC_PCPLT32:
1289 case R_SPARC_PCPLT22:
1290 case R_SPARC_PCPLT10:
1291 case R_SPARC_PLT64:
1292 /* This symbol requires a procedure linkage table entry. We
1293 actually build the entry in adjust_dynamic_symbol,
1294 because this might be a case of linking PIC code without
1295 linking in any dynamic objects, in which case we don't
1296 need to generate a procedure linkage table after all. */
1298 if (h == NULL)
1300 if (! ABI_64_P (abfd))
1302 /* The Solaris native assembler will generate a WPLT30
1303 reloc for a local symbol if you assemble a call from
1304 one section to another when using -K pic. We treat
1305 it as WDISP30. */
1306 if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32)
1307 goto r_sparc_plt32;
1308 break;
1311 /* It does not make sense to have a procedure linkage
1312 table entry for a local symbol. */
1313 bfd_set_error (bfd_error_bad_value);
1314 return FALSE;
1317 h->needs_plt = 1;
1320 int this_r_type;
1322 this_r_type = SPARC_ELF_R_TYPE (rel->r_info);
1323 if (this_r_type == R_SPARC_PLT32
1324 || this_r_type == R_SPARC_PLT64)
1325 goto r_sparc_plt32;
1327 h->plt.refcount += 1;
1328 break;
1330 case R_SPARC_PC10:
1331 case R_SPARC_PC22:
1332 case R_SPARC_PC_HH22:
1333 case R_SPARC_PC_HM10:
1334 case R_SPARC_PC_LM22:
1335 if (h != NULL)
1336 h->non_got_ref = 1;
1338 if (h != NULL
1339 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1340 break;
1341 /* Fall through. */
1343 case R_SPARC_DISP8:
1344 case R_SPARC_DISP16:
1345 case R_SPARC_DISP32:
1346 case R_SPARC_DISP64:
1347 case R_SPARC_WDISP30:
1348 case R_SPARC_WDISP22:
1349 case R_SPARC_WDISP19:
1350 case R_SPARC_WDISP16:
1351 case R_SPARC_8:
1352 case R_SPARC_16:
1353 case R_SPARC_32:
1354 case R_SPARC_HI22:
1355 case R_SPARC_22:
1356 case R_SPARC_13:
1357 case R_SPARC_LO10:
1358 case R_SPARC_UA16:
1359 case R_SPARC_UA32:
1360 case R_SPARC_10:
1361 case R_SPARC_11:
1362 case R_SPARC_64:
1363 case R_SPARC_OLO10:
1364 case R_SPARC_HH22:
1365 case R_SPARC_HM10:
1366 case R_SPARC_LM22:
1367 case R_SPARC_7:
1368 case R_SPARC_5:
1369 case R_SPARC_6:
1370 case R_SPARC_HIX22:
1371 case R_SPARC_LOX10:
1372 case R_SPARC_H44:
1373 case R_SPARC_M44:
1374 case R_SPARC_L44:
1375 case R_SPARC_UA64:
1376 if (h != NULL)
1377 h->non_got_ref = 1;
1379 r_sparc_plt32:
1380 if (h != NULL && !info->shared)
1382 /* We may need a .plt entry if the function this reloc
1383 refers to is in a shared lib. */
1384 h->plt.refcount += 1;
1387 /* If we are creating a shared library, and this is a reloc
1388 against a global symbol, or a non PC relative reloc
1389 against a local symbol, then we need to copy the reloc
1390 into the shared library. However, if we are linking with
1391 -Bsymbolic, we do not need to copy a reloc against a
1392 global symbol which is defined in an object we are
1393 including in the link (i.e., DEF_REGULAR is set). At
1394 this point we have not seen all the input files, so it is
1395 possible that DEF_REGULAR is not set now but will be set
1396 later (it is never cleared). In case of a weak definition,
1397 DEF_REGULAR may be cleared later by a strong definition in
1398 a shared library. We account for that possibility below by
1399 storing information in the relocs_copied field of the hash
1400 table entry. A similar situation occurs when creating
1401 shared libraries and symbol visibility changes render the
1402 symbol local.
1404 If on the other hand, we are creating an executable, we
1405 may need to keep relocations for symbols satisfied by a
1406 dynamic library if we manage to avoid copy relocs for the
1407 symbol. */
1408 if ((info->shared
1409 && (sec->flags & SEC_ALLOC) != 0
1410 && (! _bfd_sparc_elf_howto_table[r_type].pc_relative
1411 || (h != NULL
1412 && (! info->symbolic
1413 || h->root.type == bfd_link_hash_defweak
1414 || !h->def_regular))))
1415 || (!info->shared
1416 && (sec->flags & SEC_ALLOC) != 0
1417 && h != NULL
1418 && (h->root.type == bfd_link_hash_defweak
1419 || !h->def_regular)))
1421 struct _bfd_sparc_elf_dyn_relocs *p;
1422 struct _bfd_sparc_elf_dyn_relocs **head;
1424 /* When creating a shared object, we must copy these
1425 relocs into the output file. We create a reloc
1426 section in dynobj and make room for the reloc. */
1427 if (sreloc == NULL)
1429 const char *name;
1430 bfd *dynobj;
1432 name = (bfd_elf_string_from_elf_section
1433 (abfd,
1434 elf_elfheader (abfd)->e_shstrndx,
1435 elf_section_data (sec)->rel_hdr.sh_name));
1436 if (name == NULL)
1437 return FALSE;
1439 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
1440 && strcmp (bfd_get_section_name (abfd, sec),
1441 name + 5) == 0);
1443 if (htab->elf.dynobj == NULL)
1444 htab->elf.dynobj = abfd;
1445 dynobj = htab->elf.dynobj;
1447 sreloc = bfd_get_section_by_name (dynobj, name);
1448 if (sreloc == NULL)
1450 flagword flags;
1452 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1453 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1454 if ((sec->flags & SEC_ALLOC) != 0)
1455 flags |= SEC_ALLOC | SEC_LOAD;
1456 sreloc = bfd_make_section_with_flags (dynobj,
1457 name,
1458 flags);
1459 if (sreloc == NULL
1460 || ! bfd_set_section_alignment (dynobj, sreloc,
1461 htab->word_align_power))
1462 return FALSE;
1464 elf_section_data (sec)->sreloc = sreloc;
1467 /* If this is a global symbol, we count the number of
1468 relocations we need for this symbol. */
1469 if (h != NULL)
1470 head = &((struct _bfd_sparc_elf_link_hash_entry *) h)->dyn_relocs;
1471 else
1473 /* Track dynamic relocs needed for local syms too.
1474 We really need local syms available to do this
1475 easily. Oh well. */
1477 asection *s;
1478 void *vpp;
1480 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1481 sec, r_symndx);
1482 if (s == NULL)
1483 return FALSE;
1485 vpp = &elf_section_data (s)->local_dynrel;
1486 head = (struct _bfd_sparc_elf_dyn_relocs **) vpp;
1489 p = *head;
1490 if (p == NULL || p->sec != sec)
1492 bfd_size_type amt = sizeof *p;
1493 p = ((struct _bfd_sparc_elf_dyn_relocs *)
1494 bfd_alloc (htab->elf.dynobj, amt));
1495 if (p == NULL)
1496 return FALSE;
1497 p->next = *head;
1498 *head = p;
1499 p->sec = sec;
1500 p->count = 0;
1501 p->pc_count = 0;
1504 p->count += 1;
1505 if (_bfd_sparc_elf_howto_table[r_type].pc_relative)
1506 p->pc_count += 1;
1509 break;
1511 case R_SPARC_GNU_VTINHERIT:
1512 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1513 return FALSE;
1514 break;
1516 case R_SPARC_GNU_VTENTRY:
1517 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1518 return FALSE;
1519 break;
1521 case R_SPARC_REGISTER:
1522 /* Nothing to do. */
1523 break;
1525 default:
1526 break;
1530 return TRUE;
1533 asection *
1534 _bfd_sparc_elf_gc_mark_hook (asection *sec,
1535 struct bfd_link_info *info,
1536 Elf_Internal_Rela *rel,
1537 struct elf_link_hash_entry *h,
1538 Elf_Internal_Sym *sym)
1540 if (h != NULL)
1541 switch (SPARC_ELF_R_TYPE (rel->r_info))
1543 case R_SPARC_GNU_VTINHERIT:
1544 case R_SPARC_GNU_VTENTRY:
1545 return NULL;
1548 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1551 /* Update the got entry reference counts for the section being removed. */
1552 bfd_boolean
1553 _bfd_sparc_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1554 asection *sec, const Elf_Internal_Rela *relocs)
1556 struct _bfd_sparc_elf_link_hash_table *htab;
1557 Elf_Internal_Shdr *symtab_hdr;
1558 struct elf_link_hash_entry **sym_hashes;
1559 bfd_signed_vma *local_got_refcounts;
1560 const Elf_Internal_Rela *rel, *relend;
1562 elf_section_data (sec)->local_dynrel = NULL;
1564 htab = _bfd_sparc_elf_hash_table (info);
1565 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1566 sym_hashes = elf_sym_hashes (abfd);
1567 local_got_refcounts = elf_local_got_refcounts (abfd);
1569 relend = relocs + sec->reloc_count;
1570 for (rel = relocs; rel < relend; rel++)
1572 unsigned long r_symndx;
1573 unsigned int r_type;
1574 struct elf_link_hash_entry *h = NULL;
1576 r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
1577 if (r_symndx >= symtab_hdr->sh_info)
1579 struct _bfd_sparc_elf_link_hash_entry *eh;
1580 struct _bfd_sparc_elf_dyn_relocs **pp;
1581 struct _bfd_sparc_elf_dyn_relocs *p;
1583 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1584 while (h->root.type == bfd_link_hash_indirect
1585 || h->root.type == bfd_link_hash_warning)
1586 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1587 eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
1588 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1589 if (p->sec == sec)
1591 /* Everything must go for SEC. */
1592 *pp = p->next;
1593 break;
1597 r_type = SPARC_ELF_R_TYPE (rel->r_info);
1598 r_type = sparc_elf_tls_transition (info, abfd, r_type, h != NULL);
1599 switch (r_type)
1601 case R_SPARC_TLS_LDM_HI22:
1602 case R_SPARC_TLS_LDM_LO10:
1603 if (_bfd_sparc_elf_hash_table (info)->tls_ldm_got.refcount > 0)
1604 _bfd_sparc_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
1605 break;
1607 case R_SPARC_TLS_GD_HI22:
1608 case R_SPARC_TLS_GD_LO10:
1609 case R_SPARC_TLS_IE_HI22:
1610 case R_SPARC_TLS_IE_LO10:
1611 case R_SPARC_GOT10:
1612 case R_SPARC_GOT13:
1613 case R_SPARC_GOT22:
1614 if (h != NULL)
1616 if (h->got.refcount > 0)
1617 h->got.refcount--;
1619 else
1621 if (local_got_refcounts[r_symndx] > 0)
1622 local_got_refcounts[r_symndx]--;
1624 break;
1626 case R_SPARC_PC10:
1627 case R_SPARC_PC22:
1628 case R_SPARC_PC_HH22:
1629 case R_SPARC_PC_HM10:
1630 case R_SPARC_PC_LM22:
1631 if (h != NULL
1632 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1633 break;
1634 /* Fall through. */
1636 case R_SPARC_DISP8:
1637 case R_SPARC_DISP16:
1638 case R_SPARC_DISP32:
1639 case R_SPARC_DISP64:
1640 case R_SPARC_WDISP30:
1641 case R_SPARC_WDISP22:
1642 case R_SPARC_WDISP19:
1643 case R_SPARC_WDISP16:
1644 case R_SPARC_8:
1645 case R_SPARC_16:
1646 case R_SPARC_32:
1647 case R_SPARC_HI22:
1648 case R_SPARC_22:
1649 case R_SPARC_13:
1650 case R_SPARC_LO10:
1651 case R_SPARC_UA16:
1652 case R_SPARC_UA32:
1653 case R_SPARC_PLT32:
1654 case R_SPARC_10:
1655 case R_SPARC_11:
1656 case R_SPARC_64:
1657 case R_SPARC_OLO10:
1658 case R_SPARC_HH22:
1659 case R_SPARC_HM10:
1660 case R_SPARC_LM22:
1661 case R_SPARC_7:
1662 case R_SPARC_5:
1663 case R_SPARC_6:
1664 case R_SPARC_HIX22:
1665 case R_SPARC_LOX10:
1666 case R_SPARC_H44:
1667 case R_SPARC_M44:
1668 case R_SPARC_L44:
1669 case R_SPARC_UA64:
1670 if (info->shared)
1671 break;
1672 /* Fall through. */
1674 case R_SPARC_WPLT30:
1675 if (h != NULL)
1677 if (h->plt.refcount > 0)
1678 h->plt.refcount--;
1680 break;
1682 default:
1683 break;
1687 return TRUE;
1690 /* Adjust a symbol defined by a dynamic object and referenced by a
1691 regular object. The current definition is in some section of the
1692 dynamic object, but we're not including those sections. We have to
1693 change the definition to something the rest of the link can
1694 understand. */
1696 bfd_boolean
1697 _bfd_sparc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1698 struct elf_link_hash_entry *h)
1700 struct _bfd_sparc_elf_link_hash_table *htab;
1701 struct _bfd_sparc_elf_link_hash_entry * eh;
1702 struct _bfd_sparc_elf_dyn_relocs *p;
1703 asection *s;
1704 unsigned int power_of_two;
1706 htab = _bfd_sparc_elf_hash_table (info);
1708 /* Make sure we know what is going on here. */
1709 BFD_ASSERT (htab->elf.dynobj != NULL
1710 && (h->needs_plt
1711 || h->u.weakdef != NULL
1712 || (h->def_dynamic
1713 && h->ref_regular
1714 && !h->def_regular)));
1716 /* If this is a function, put it in the procedure linkage table. We
1717 will fill in the contents of the procedure linkage table later
1718 (although we could actually do it here). The STT_NOTYPE
1719 condition is a hack specifically for the Oracle libraries
1720 delivered for Solaris; for some inexplicable reason, they define
1721 some of their functions as STT_NOTYPE when they really should be
1722 STT_FUNC. */
1723 if (h->type == STT_FUNC
1724 || h->needs_plt
1725 || (h->type == STT_NOTYPE
1726 && (h->root.type == bfd_link_hash_defined
1727 || h->root.type == bfd_link_hash_defweak)
1728 && (h->root.u.def.section->flags & SEC_CODE) != 0))
1730 if (h->plt.refcount <= 0
1731 || (! info->shared
1732 && !h->def_dynamic
1733 && !h->ref_dynamic
1734 && h->root.type != bfd_link_hash_undefweak
1735 && h->root.type != bfd_link_hash_undefined))
1737 /* This case can occur if we saw a WPLT30 reloc in an input
1738 file, but the symbol was never referred to by a dynamic
1739 object, or if all references were garbage collected. In
1740 such a case, we don't actually need to build a procedure
1741 linkage table, and we can just do a WDISP30 reloc instead. */
1742 h->plt.offset = (bfd_vma) -1;
1743 h->needs_plt = 0;
1746 return TRUE;
1748 else
1749 h->plt.offset = (bfd_vma) -1;
1751 /* If this is a weak symbol, and there is a real definition, the
1752 processor independent code will have arranged for us to see the
1753 real definition first, and we can just use the same value. */
1754 if (h->u.weakdef != NULL)
1756 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1757 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1758 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1759 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1760 return TRUE;
1763 /* This is a reference to a symbol defined by a dynamic object which
1764 is not a function. */
1766 /* If we are creating a shared library, we must presume that the
1767 only references to the symbol are via the global offset table.
1768 For such cases we need not do anything here; the relocations will
1769 be handled correctly by relocate_section. */
1770 if (info->shared)
1771 return TRUE;
1773 /* If there are no references to this symbol that do not use the
1774 GOT, we don't need to generate a copy reloc. */
1775 if (!h->non_got_ref)
1776 return TRUE;
1778 eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
1779 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1781 s = p->sec->output_section;
1782 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1783 break;
1786 /* If we didn't find any dynamic relocs in read-only sections, then
1787 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1788 if (p == NULL)
1790 h->non_got_ref = 0;
1791 return TRUE;
1794 if (h->size == 0)
1796 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1797 h->root.root.string);
1798 return TRUE;
1801 /* We must allocate the symbol in our .dynbss section, which will
1802 become part of the .bss section of the executable. There will be
1803 an entry for this symbol in the .dynsym section. The dynamic
1804 object will contain position independent code, so all references
1805 from the dynamic object to this symbol will go through the global
1806 offset table. The dynamic linker will use the .dynsym entry to
1807 determine the address it must put in the global offset table, so
1808 both the dynamic object and the regular object will refer to the
1809 same memory location for the variable. */
1811 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
1812 to copy the initial value out of the dynamic object and into the
1813 runtime process image. We need to remember the offset into the
1814 .rel.bss section we are going to use. */
1815 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1817 htab->srelbss->size += SPARC_ELF_RELA_BYTES (htab);
1818 h->needs_copy = 1;
1821 /* We need to figure out the alignment required for this symbol. I
1822 have no idea how ELF linkers handle this. */
1823 power_of_two = bfd_log2 (h->size);
1824 if (power_of_two > htab->align_power_max)
1825 power_of_two = htab->align_power_max;
1827 /* Apply the required alignment. */
1828 s = htab->sdynbss;
1829 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1830 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1832 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1833 return FALSE;
1836 /* Define the symbol as being at this point in the section. */
1837 h->root.u.def.section = s;
1838 h->root.u.def.value = s->size;
1840 /* Increment the section size to make room for the symbol. */
1841 s->size += h->size;
1843 return TRUE;
1846 /* Allocate space in .plt, .got and associated reloc sections for
1847 dynamic relocs. */
1849 static bfd_boolean
1850 allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
1852 struct bfd_link_info *info;
1853 struct _bfd_sparc_elf_link_hash_table *htab;
1854 struct _bfd_sparc_elf_link_hash_entry *eh;
1855 struct _bfd_sparc_elf_dyn_relocs *p;
1857 if (h->root.type == bfd_link_hash_indirect)
1858 return TRUE;
1860 if (h->root.type == bfd_link_hash_warning)
1861 /* When warning symbols are created, they **replace** the "real"
1862 entry in the hash table, thus we never get to see the real
1863 symbol in a hash traversal. So look at it now. */
1864 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1866 info = (struct bfd_link_info *) inf;
1867 htab = _bfd_sparc_elf_hash_table (info);
1869 if (htab->elf.dynamic_sections_created
1870 && h->plt.refcount > 0)
1872 /* Make sure this symbol is output as a dynamic symbol.
1873 Undefined weak syms won't yet be marked as dynamic. */
1874 if (h->dynindx == -1
1875 && !h->forced_local)
1877 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1878 return FALSE;
1881 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
1883 asection *s = htab->splt;
1885 /* Allocate room for the header. */
1886 if (s->size == 0)
1888 s->size = htab->plt_header_size;
1890 /* Allocate space for the .rela.plt.unloaded relocations. */
1891 if (htab->is_vxworks && !info->shared)
1892 htab->srelplt2->size = sizeof (Elf32_External_Rela) * 2;
1895 /* The procedure linkage table size is bounded by the magnitude
1896 of the offset we can describe in the entry. */
1897 if (s->size >= (SPARC_ELF_WORD_BYTES(htab) == 8 ?
1898 (((bfd_vma)1 << 31) << 1) : 0x400000))
1900 bfd_set_error (bfd_error_bad_value);
1901 return FALSE;
1904 if (SPARC_ELF_WORD_BYTES(htab) == 8
1905 && s->size >= PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
1907 bfd_vma off = s->size - PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE;
1910 off = (off % (160 * PLT64_ENTRY_SIZE)) / PLT64_ENTRY_SIZE;
1912 h->plt.offset = (s->size - (off * 8));
1914 else
1915 h->plt.offset = s->size;
1917 /* If this symbol is not defined in a regular file, and we are
1918 not generating a shared library, then set the symbol to this
1919 location in the .plt. This is required to make function
1920 pointers compare as equal between the normal executable and
1921 the shared library. */
1922 if (! info->shared
1923 && !h->def_regular)
1925 h->root.u.def.section = s;
1926 h->root.u.def.value = h->plt.offset;
1929 /* Make room for this entry. */
1930 s->size += htab->plt_entry_size;
1932 /* We also need to make an entry in the .rela.plt section. */
1933 htab->srelplt->size += SPARC_ELF_RELA_BYTES (htab);
1935 if (htab->is_vxworks)
1937 /* Allocate space for the .got.plt entry. */
1938 htab->sgotplt->size += 4;
1940 /* ...and for the .rela.plt.unloaded relocations. */
1941 if (!info->shared)
1942 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 3;
1945 else
1947 h->plt.offset = (bfd_vma) -1;
1948 h->needs_plt = 0;
1951 else
1953 h->plt.offset = (bfd_vma) -1;
1954 h->needs_plt = 0;
1957 /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary,
1958 make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry. */
1959 if (h->got.refcount > 0
1960 && !info->shared
1961 && h->dynindx == -1
1962 && _bfd_sparc_elf_hash_entry(h)->tls_type == GOT_TLS_IE)
1963 h->got.offset = (bfd_vma) -1;
1964 else if (h->got.refcount > 0)
1966 asection *s;
1967 bfd_boolean dyn;
1968 int tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
1970 /* Make sure this symbol is output as a dynamic symbol.
1971 Undefined weak syms won't yet be marked as dynamic. */
1972 if (h->dynindx == -1
1973 && !h->forced_local)
1975 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1976 return FALSE;
1979 s = htab->sgot;
1980 h->got.offset = s->size;
1981 s->size += SPARC_ELF_WORD_BYTES (htab);
1982 /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots. */
1983 if (tls_type == GOT_TLS_GD)
1984 s->size += SPARC_ELF_WORD_BYTES (htab);
1985 dyn = htab->elf.dynamic_sections_created;
1986 /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation,
1987 R_SPARC_TLS_GD_{HI22,LO10} needs one if local symbol and two if
1988 global. */
1989 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1990 || tls_type == GOT_TLS_IE)
1991 htab->srelgot->size += SPARC_ELF_RELA_BYTES (htab);
1992 else if (tls_type == GOT_TLS_GD)
1993 htab->srelgot->size += 2 * SPARC_ELF_RELA_BYTES (htab);
1994 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
1995 htab->srelgot->size += SPARC_ELF_RELA_BYTES (htab);
1997 else
1998 h->got.offset = (bfd_vma) -1;
2000 eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
2001 if (eh->dyn_relocs == NULL)
2002 return TRUE;
2004 /* In the shared -Bsymbolic case, discard space allocated for
2005 dynamic pc-relative relocs against symbols which turn out to be
2006 defined in regular objects. For the normal shared case, discard
2007 space for pc-relative relocs that have become local due to symbol
2008 visibility changes. */
2010 if (info->shared)
2012 if (h->def_regular
2013 && (h->forced_local
2014 || info->symbolic))
2016 struct _bfd_sparc_elf_dyn_relocs **pp;
2018 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2020 p->count -= p->pc_count;
2021 p->pc_count = 0;
2022 if (p->count == 0)
2023 *pp = p->next;
2024 else
2025 pp = &p->next;
2029 /* Also discard relocs on undefined weak syms with non-default
2030 visibility. */
2031 if (eh->dyn_relocs != NULL
2032 && h->root.type == bfd_link_hash_undefweak)
2034 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2035 eh->dyn_relocs = NULL;
2037 /* Make sure undefined weak symbols are output as a dynamic
2038 symbol in PIEs. */
2039 else if (h->dynindx == -1
2040 && !h->forced_local)
2042 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2043 return FALSE;
2047 else
2049 /* For the non-shared case, discard space for relocs against
2050 symbols which turn out to need copy relocs or are not
2051 dynamic. */
2053 if (!h->non_got_ref
2054 && ((h->def_dynamic
2055 && !h->def_regular)
2056 || (htab->elf.dynamic_sections_created
2057 && (h->root.type == bfd_link_hash_undefweak
2058 || h->root.type == bfd_link_hash_undefined))))
2060 /* Make sure this symbol is output as a dynamic symbol.
2061 Undefined weak syms won't yet be marked as dynamic. */
2062 if (h->dynindx == -1
2063 && !h->forced_local)
2065 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2066 return FALSE;
2069 /* If that succeeded, we know we'll be keeping all the
2070 relocs. */
2071 if (h->dynindx != -1)
2072 goto keep;
2075 eh->dyn_relocs = NULL;
2077 keep: ;
2080 /* Finally, allocate space. */
2081 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2083 asection *sreloc = elf_section_data (p->sec)->sreloc;
2084 sreloc->size += p->count * SPARC_ELF_RELA_BYTES (htab);
2087 return TRUE;
2090 /* Find any dynamic relocs that apply to read-only sections. */
2092 static bfd_boolean
2093 readonly_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
2095 struct _bfd_sparc_elf_link_hash_entry *eh;
2096 struct _bfd_sparc_elf_dyn_relocs *p;
2098 if (h->root.type == bfd_link_hash_warning)
2099 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2101 eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
2102 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2104 asection *s = p->sec->output_section;
2106 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2108 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2110 info->flags |= DF_TEXTREL;
2112 /* Not an error, just cut short the traversal. */
2113 return FALSE;
2116 return TRUE;
2119 /* Return true if the dynamic symbol for a given section should be
2120 omitted when creating a shared library. */
2122 bfd_boolean
2123 _bfd_sparc_elf_omit_section_dynsym (bfd *output_bfd,
2124 struct bfd_link_info *info,
2125 asection *p)
2127 /* We keep the .got section symbol so that explicit relocations
2128 against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode
2129 can be turned into relocations against the .got symbol. */
2130 if (strcmp (p->name, ".got") == 0)
2131 return FALSE;
2133 return _bfd_elf_link_omit_section_dynsym (output_bfd, info, p);
2136 /* Set the sizes of the dynamic sections. */
2138 bfd_boolean
2139 _bfd_sparc_elf_size_dynamic_sections (bfd *output_bfd,
2140 struct bfd_link_info *info)
2142 struct _bfd_sparc_elf_link_hash_table *htab;
2143 bfd *dynobj;
2144 asection *s;
2145 bfd *ibfd;
2147 htab = _bfd_sparc_elf_hash_table (info);
2148 dynobj = htab->elf.dynobj;
2149 BFD_ASSERT (dynobj != NULL);
2151 if (elf_hash_table (info)->dynamic_sections_created)
2153 /* Set the contents of the .interp section to the interpreter. */
2154 if (info->executable)
2156 s = bfd_get_section_by_name (dynobj, ".interp");
2157 BFD_ASSERT (s != NULL);
2158 s->size = htab->dynamic_interpreter_size;
2159 s->contents = (unsigned char *) htab->dynamic_interpreter;
2163 /* Set up .got offsets for local syms, and space for local dynamic
2164 relocs. */
2165 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2167 bfd_signed_vma *local_got;
2168 bfd_signed_vma *end_local_got;
2169 char *local_tls_type;
2170 bfd_size_type locsymcount;
2171 Elf_Internal_Shdr *symtab_hdr;
2172 asection *srel;
2174 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2175 continue;
2177 for (s = ibfd->sections; s != NULL; s = s->next)
2179 struct _bfd_sparc_elf_dyn_relocs *p;
2181 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
2183 if (!bfd_is_abs_section (p->sec)
2184 && bfd_is_abs_section (p->sec->output_section))
2186 /* Input section has been discarded, either because
2187 it is a copy of a linkonce section or due to
2188 linker script /DISCARD/, so we'll be discarding
2189 the relocs too. */
2191 else if (p->count != 0)
2193 srel = elf_section_data (p->sec)->sreloc;
2194 srel->size += p->count * SPARC_ELF_RELA_BYTES (htab);
2195 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2196 info->flags |= DF_TEXTREL;
2201 local_got = elf_local_got_refcounts (ibfd);
2202 if (!local_got)
2203 continue;
2205 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2206 locsymcount = symtab_hdr->sh_info;
2207 end_local_got = local_got + locsymcount;
2208 local_tls_type = _bfd_sparc_elf_local_got_tls_type (ibfd);
2209 s = htab->sgot;
2210 srel = htab->srelgot;
2211 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
2213 if (*local_got > 0)
2215 *local_got = s->size;
2216 s->size += SPARC_ELF_WORD_BYTES (htab);
2217 if (*local_tls_type == GOT_TLS_GD)
2218 s->size += SPARC_ELF_WORD_BYTES (htab);
2219 if (info->shared
2220 || *local_tls_type == GOT_TLS_GD
2221 || *local_tls_type == GOT_TLS_IE)
2222 srel->size += SPARC_ELF_RELA_BYTES (htab);
2224 else
2225 *local_got = (bfd_vma) -1;
2229 if (htab->tls_ldm_got.refcount > 0)
2231 /* Allocate 2 got entries and 1 dynamic reloc for
2232 R_SPARC_TLS_LDM_{HI22,LO10} relocs. */
2233 htab->tls_ldm_got.offset = htab->sgot->size;
2234 htab->sgot->size += (2 * SPARC_ELF_WORD_BYTES (htab));
2235 htab->srelgot->size += SPARC_ELF_RELA_BYTES (htab);
2237 else
2238 htab->tls_ldm_got.offset = -1;
2240 /* Allocate global sym .plt and .got entries, and space for global
2241 sym dynamic relocs. */
2242 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
2244 if (! ABI_64_P (output_bfd)
2245 && !htab->is_vxworks
2246 && elf_hash_table (info)->dynamic_sections_created)
2248 /* Make space for the trailing nop in .plt. */
2249 if (htab->splt->size > 0)
2250 htab->splt->size += 1 * SPARC_INSN_BYTES;
2252 /* If the .got section is more than 0x1000 bytes, we add
2253 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
2254 bit relocations have a greater chance of working.
2256 FIXME: Make this optimization work for 64-bit too. */
2257 if (htab->sgot->size >= 0x1000
2258 && elf_hash_table (info)->hgot->root.u.def.value == 0)
2259 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
2262 /* The check_relocs and adjust_dynamic_symbol entry points have
2263 determined the sizes of the various dynamic sections. Allocate
2264 memory for them. */
2265 for (s = dynobj->sections; s != NULL; s = s->next)
2267 if ((s->flags & SEC_LINKER_CREATED) == 0)
2268 continue;
2270 if (s == htab->splt
2271 || s == htab->sgot
2272 || s == htab->sdynbss
2273 || s == htab->sgotplt)
2275 /* Strip this section if we don't need it; see the
2276 comment below. */
2278 else if (CONST_STRNEQ (s->name, ".rela"))
2280 if (s->size != 0)
2282 /* We use the reloc_count field as a counter if we need
2283 to copy relocs into the output file. */
2284 s->reloc_count = 0;
2287 else
2289 /* It's not one of our sections. */
2290 continue;
2293 if (s->size == 0)
2295 /* If we don't need this section, strip it from the
2296 output file. This is mostly to handle .rela.bss and
2297 .rela.plt. We must create both sections in
2298 create_dynamic_sections, because they must be created
2299 before the linker maps input sections to output
2300 sections. The linker does that before
2301 adjust_dynamic_symbol is called, and it is that
2302 function which decides whether anything needs to go
2303 into these sections. */
2304 s->flags |= SEC_EXCLUDE;
2305 continue;
2308 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2309 continue;
2311 /* Allocate memory for the section contents. Zero the memory
2312 for the benefit of .rela.plt, which has 4 unused entries
2313 at the beginning, and we don't want garbage. */
2314 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2315 if (s->contents == NULL)
2316 return FALSE;
2319 if (elf_hash_table (info)->dynamic_sections_created)
2321 /* Add some entries to the .dynamic section. We fill in the
2322 values later, in _bfd_sparc_elf_finish_dynamic_sections, but we
2323 must add the entries now so that we get the correct size for
2324 the .dynamic section. The DT_DEBUG entry is filled in by the
2325 dynamic linker and used by the debugger. */
2326 #define add_dynamic_entry(TAG, VAL) \
2327 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2329 if (info->executable)
2331 if (!add_dynamic_entry (DT_DEBUG, 0))
2332 return FALSE;
2335 if (htab->srelplt->size != 0)
2337 if (!add_dynamic_entry (DT_PLTGOT, 0)
2338 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2339 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2340 || !add_dynamic_entry (DT_JMPREL, 0))
2341 return FALSE;
2344 if (!add_dynamic_entry (DT_RELA, 0)
2345 || !add_dynamic_entry (DT_RELASZ, 0)
2346 || !add_dynamic_entry (DT_RELAENT,
2347 SPARC_ELF_RELA_BYTES (htab)))
2348 return FALSE;
2350 /* If any dynamic relocs apply to a read-only section,
2351 then we need a DT_TEXTREL entry. */
2352 if ((info->flags & DF_TEXTREL) == 0)
2353 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2354 (PTR) info);
2356 if (info->flags & DF_TEXTREL)
2358 if (!add_dynamic_entry (DT_TEXTREL, 0))
2359 return FALSE;
2362 if (ABI_64_P (output_bfd))
2364 int reg;
2365 struct _bfd_sparc_elf_app_reg * app_regs;
2366 struct elf_strtab_hash *dynstr;
2367 struct elf_link_hash_table *eht = elf_hash_table (info);
2369 /* Add dynamic STT_REGISTER symbols and corresponding DT_SPARC_REGISTER
2370 entries if needed. */
2371 app_regs = _bfd_sparc_elf_hash_table (info)->app_regs;
2372 dynstr = eht->dynstr;
2374 for (reg = 0; reg < 4; reg++)
2375 if (app_regs [reg].name != NULL)
2377 struct elf_link_local_dynamic_entry *entry, *e;
2379 if (!add_dynamic_entry (DT_SPARC_REGISTER, 0))
2380 return FALSE;
2382 entry = (struct elf_link_local_dynamic_entry *)
2383 bfd_hash_allocate (&info->hash->table, sizeof (*entry));
2384 if (entry == NULL)
2385 return FALSE;
2387 /* We cheat here a little bit: the symbol will not be local, so we
2388 put it at the end of the dynlocal linked list. We will fix it
2389 later on, as we have to fix other fields anyway. */
2390 entry->isym.st_value = reg < 2 ? reg + 2 : reg + 4;
2391 entry->isym.st_size = 0;
2392 if (*app_regs [reg].name != '\0')
2393 entry->isym.st_name
2394 = _bfd_elf_strtab_add (dynstr, app_regs[reg].name, FALSE);
2395 else
2396 entry->isym.st_name = 0;
2397 entry->isym.st_other = 0;
2398 entry->isym.st_info = ELF_ST_INFO (app_regs [reg].bind,
2399 STT_REGISTER);
2400 entry->isym.st_shndx = app_regs [reg].shndx;
2401 entry->next = NULL;
2402 entry->input_bfd = output_bfd;
2403 entry->input_indx = -1;
2405 if (eht->dynlocal == NULL)
2406 eht->dynlocal = entry;
2407 else
2409 for (e = eht->dynlocal; e->next; e = e->next)
2411 e->next = entry;
2413 eht->dynsymcount++;
2417 #undef add_dynamic_entry
2419 return TRUE;
2422 bfd_boolean
2423 _bfd_sparc_elf_new_section_hook (bfd *abfd, asection *sec)
2425 if (!sec->used_by_bfd)
2427 struct _bfd_sparc_elf_section_data *sdata;
2428 bfd_size_type amt = sizeof (*sdata);
2430 sdata = bfd_zalloc (abfd, amt);
2431 if (sdata == NULL)
2432 return FALSE;
2433 sec->used_by_bfd = sdata;
2436 return _bfd_elf_new_section_hook (abfd, sec);
2439 bfd_boolean
2440 _bfd_sparc_elf_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
2441 struct bfd_section *section,
2442 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2443 bfd_boolean *again)
2445 *again = FALSE;
2446 sec_do_relax (section) = 1;
2447 return TRUE;
2450 /* Return the base VMA address which should be subtracted from real addresses
2451 when resolving @dtpoff relocation.
2452 This is PT_TLS segment p_vaddr. */
2454 static bfd_vma
2455 dtpoff_base (struct bfd_link_info *info)
2457 /* If tls_sec is NULL, we should have signalled an error already. */
2458 if (elf_hash_table (info)->tls_sec == NULL)
2459 return 0;
2460 return elf_hash_table (info)->tls_sec->vma;
2463 /* Return the relocation value for @tpoff relocation
2464 if STT_TLS virtual address is ADDRESS. */
2466 static bfd_vma
2467 tpoff (struct bfd_link_info *info, bfd_vma address)
2469 struct elf_link_hash_table *htab = elf_hash_table (info);
2471 /* If tls_sec is NULL, we should have signalled an error already. */
2472 if (htab->tls_sec == NULL)
2473 return 0;
2474 return address - htab->tls_size - htab->tls_sec->vma;
2477 /* Relocate a SPARC ELF section. */
2479 bfd_boolean
2480 _bfd_sparc_elf_relocate_section (bfd *output_bfd,
2481 struct bfd_link_info *info,
2482 bfd *input_bfd,
2483 asection *input_section,
2484 bfd_byte *contents,
2485 Elf_Internal_Rela *relocs,
2486 Elf_Internal_Sym *local_syms,
2487 asection **local_sections)
2489 struct _bfd_sparc_elf_link_hash_table *htab;
2490 Elf_Internal_Shdr *symtab_hdr;
2491 struct elf_link_hash_entry **sym_hashes;
2492 bfd_vma *local_got_offsets;
2493 bfd_vma got_base;
2494 asection *sreloc;
2495 Elf_Internal_Rela *rel;
2496 Elf_Internal_Rela *relend;
2497 int num_relocs;
2499 htab = _bfd_sparc_elf_hash_table (info);
2500 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2501 sym_hashes = elf_sym_hashes (input_bfd);
2502 local_got_offsets = elf_local_got_offsets (input_bfd);
2504 if (elf_hash_table (info)->hgot == NULL)
2505 got_base = 0;
2506 else
2507 got_base = elf_hash_table (info)->hgot->root.u.def.value;
2509 sreloc = elf_section_data (input_section)->sreloc;
2511 rel = relocs;
2512 if (ABI_64_P (output_bfd))
2513 num_relocs = NUM_SHDR_ENTRIES (& elf_section_data (input_section)->rel_hdr);
2514 else
2515 num_relocs = input_section->reloc_count;
2516 relend = relocs + num_relocs;
2517 for (; rel < relend; rel++)
2519 int r_type, tls_type;
2520 reloc_howto_type *howto;
2521 unsigned long r_symndx;
2522 struct elf_link_hash_entry *h;
2523 Elf_Internal_Sym *sym;
2524 asection *sec;
2525 bfd_vma relocation, off;
2526 bfd_reloc_status_type r;
2527 bfd_boolean is_plt = FALSE;
2528 bfd_boolean unresolved_reloc;
2530 r_type = SPARC_ELF_R_TYPE (rel->r_info);
2531 if (r_type == R_SPARC_GNU_VTINHERIT
2532 || r_type == R_SPARC_GNU_VTENTRY)
2533 continue;
2535 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
2537 bfd_set_error (bfd_error_bad_value);
2538 return FALSE;
2540 howto = _bfd_sparc_elf_howto_table + r_type;
2542 r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
2543 h = NULL;
2544 sym = NULL;
2545 sec = NULL;
2546 unresolved_reloc = FALSE;
2547 if (r_symndx < symtab_hdr->sh_info)
2549 sym = local_syms + r_symndx;
2550 sec = local_sections[r_symndx];
2551 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2553 else
2555 bfd_boolean warned;
2557 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2558 r_symndx, symtab_hdr, sym_hashes,
2559 h, sec, relocation,
2560 unresolved_reloc, warned);
2561 if (warned)
2563 /* To avoid generating warning messages about truncated
2564 relocations, set the relocation's address to be the same as
2565 the start of this section. */
2566 if (input_section->output_section != NULL)
2567 relocation = input_section->output_section->vma;
2568 else
2569 relocation = 0;
2573 if (sec != NULL && elf_discarded_section (sec))
2575 /* For relocs against symbols from removed linkonce
2576 sections, or sections discarded by a linker script, we
2577 just want the section contents zeroed. Avoid any
2578 special processing. */
2579 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2580 rel->r_info = 0;
2581 rel->r_addend = 0;
2582 continue;
2585 if (info->relocatable)
2586 continue;
2588 switch (r_type)
2590 case R_SPARC_GOT10:
2591 case R_SPARC_GOT13:
2592 case R_SPARC_GOT22:
2593 /* Relocation is to the entry for this symbol in the global
2594 offset table. */
2595 if (htab->sgot == NULL)
2596 abort ();
2598 if (h != NULL)
2600 bfd_boolean dyn;
2602 off = h->got.offset;
2603 BFD_ASSERT (off != (bfd_vma) -1);
2604 dyn = elf_hash_table (info)->dynamic_sections_created;
2606 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2607 || (info->shared
2608 && (info->symbolic
2609 || h->dynindx == -1
2610 || h->forced_local)
2611 && h->def_regular))
2613 /* This is actually a static link, or it is a
2614 -Bsymbolic link and the symbol is defined
2615 locally, or the symbol was forced to be local
2616 because of a version file. We must initialize
2617 this entry in the global offset table. Since the
2618 offset must always be a multiple of 8 for 64-bit
2619 and 4 for 32-bit, we use the least significant bit
2620 to record whether we have initialized it already.
2622 When doing a dynamic link, we create a .rela.got
2623 relocation entry to initialize the value. This
2624 is done in the finish_dynamic_symbol routine. */
2625 if ((off & 1) != 0)
2626 off &= ~1;
2627 else
2629 SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
2630 htab->sgot->contents + off);
2631 h->got.offset |= 1;
2634 else
2635 unresolved_reloc = FALSE;
2637 else
2639 BFD_ASSERT (local_got_offsets != NULL
2640 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2642 off = local_got_offsets[r_symndx];
2644 /* The offset must always be a multiple of 8 on 64-bit and
2645 4 on 32-bit. We use the least significant bit to record
2646 whether we have already processed this entry. */
2647 if ((off & 1) != 0)
2648 off &= ~1;
2649 else
2652 if (info->shared)
2654 asection *s;
2655 Elf_Internal_Rela outrel;
2657 /* We need to generate a R_SPARC_RELATIVE reloc
2658 for the dynamic linker. */
2659 s = htab->srelgot;
2660 BFD_ASSERT (s != NULL);
2662 outrel.r_offset = (htab->sgot->output_section->vma
2663 + htab->sgot->output_offset
2664 + off);
2665 outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
2666 0, R_SPARC_RELATIVE);
2667 outrel.r_addend = relocation;
2668 relocation = 0;
2669 sparc_elf_append_rela (output_bfd, s, &outrel);
2672 SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
2673 htab->sgot->contents + off);
2674 local_got_offsets[r_symndx] |= 1;
2677 relocation = htab->sgot->output_offset + off - got_base;
2678 break;
2680 case R_SPARC_PLT32:
2681 case R_SPARC_PLT64:
2682 if (h == NULL || h->plt.offset == (bfd_vma) -1)
2684 r_type = (r_type == R_SPARC_PLT32) ? R_SPARC_32 : R_SPARC_64;
2685 goto r_sparc_plt32;
2687 /* Fall through. */
2689 case R_SPARC_WPLT30:
2690 case R_SPARC_HIPLT22:
2691 case R_SPARC_LOPLT10:
2692 case R_SPARC_PCPLT32:
2693 case R_SPARC_PCPLT22:
2694 case R_SPARC_PCPLT10:
2695 r_sparc_wplt30:
2696 /* Relocation is to the entry for this symbol in the
2697 procedure linkage table. */
2699 if (! ABI_64_P (output_bfd))
2701 /* The Solaris native assembler will generate a WPLT30 reloc
2702 for a local symbol if you assemble a call from one
2703 section to another when using -K pic. We treat it as
2704 WDISP30. */
2705 if (h == NULL)
2706 break;
2708 else
2710 BFD_ASSERT (h != NULL);
2713 if (h->plt.offset == (bfd_vma) -1 || htab->splt == NULL)
2715 /* We didn't make a PLT entry for this symbol. This
2716 happens when statically linking PIC code, or when
2717 using -Bsymbolic. */
2718 break;
2721 relocation = (htab->splt->output_section->vma
2722 + htab->splt->output_offset
2723 + h->plt.offset);
2724 unresolved_reloc = FALSE;
2725 if (r_type == R_SPARC_PLT32 || r_type == R_SPARC_PLT64)
2727 r_type = r_type == R_SPARC_PLT32 ? R_SPARC_32 : R_SPARC_64;
2728 is_plt = TRUE;
2729 goto r_sparc_plt32;
2731 break;
2733 case R_SPARC_PC10:
2734 case R_SPARC_PC22:
2735 case R_SPARC_PC_HH22:
2736 case R_SPARC_PC_HM10:
2737 case R_SPARC_PC_LM22:
2738 if (h != NULL
2739 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2740 break;
2741 /* Fall through. */
2742 case R_SPARC_DISP8:
2743 case R_SPARC_DISP16:
2744 case R_SPARC_DISP32:
2745 case R_SPARC_DISP64:
2746 case R_SPARC_WDISP30:
2747 case R_SPARC_WDISP22:
2748 case R_SPARC_WDISP19:
2749 case R_SPARC_WDISP16:
2750 case R_SPARC_8:
2751 case R_SPARC_16:
2752 case R_SPARC_32:
2753 case R_SPARC_HI22:
2754 case R_SPARC_22:
2755 case R_SPARC_13:
2756 case R_SPARC_LO10:
2757 case R_SPARC_UA16:
2758 case R_SPARC_UA32:
2759 case R_SPARC_10:
2760 case R_SPARC_11:
2761 case R_SPARC_64:
2762 case R_SPARC_OLO10:
2763 case R_SPARC_HH22:
2764 case R_SPARC_HM10:
2765 case R_SPARC_LM22:
2766 case R_SPARC_7:
2767 case R_SPARC_5:
2768 case R_SPARC_6:
2769 case R_SPARC_HIX22:
2770 case R_SPARC_LOX10:
2771 case R_SPARC_H44:
2772 case R_SPARC_M44:
2773 case R_SPARC_L44:
2774 case R_SPARC_UA64:
2775 r_sparc_plt32:
2776 if ((input_section->flags & SEC_ALLOC) == 0)
2777 break;
2779 if ((info->shared
2780 && (h == NULL
2781 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2782 || h->root.type != bfd_link_hash_undefweak)
2783 && (! howto->pc_relative
2784 || (h != NULL
2785 && h->dynindx != -1
2786 && (! info->symbolic
2787 || !h->def_regular))))
2788 || (!info->shared
2789 && h != NULL
2790 && h->dynindx != -1
2791 && !h->non_got_ref
2792 && ((h->def_dynamic
2793 && !h->def_regular)
2794 || h->root.type == bfd_link_hash_undefweak
2795 || h->root.type == bfd_link_hash_undefined)))
2797 Elf_Internal_Rela outrel;
2798 bfd_boolean skip, relocate = FALSE;
2800 /* When generating a shared object, these relocations
2801 are copied into the output file to be resolved at run
2802 time. */
2804 BFD_ASSERT (sreloc != NULL);
2806 skip = FALSE;
2808 outrel.r_offset =
2809 _bfd_elf_section_offset (output_bfd, info, input_section,
2810 rel->r_offset);
2811 if (outrel.r_offset == (bfd_vma) -1)
2812 skip = TRUE;
2813 else if (outrel.r_offset == (bfd_vma) -2)
2814 skip = TRUE, relocate = TRUE;
2815 outrel.r_offset += (input_section->output_section->vma
2816 + input_section->output_offset);
2818 /* Optimize unaligned reloc usage now that we know where
2819 it finally resides. */
2820 switch (r_type)
2822 case R_SPARC_16:
2823 if (outrel.r_offset & 1)
2824 r_type = R_SPARC_UA16;
2825 break;
2826 case R_SPARC_UA16:
2827 if (!(outrel.r_offset & 1))
2828 r_type = R_SPARC_16;
2829 break;
2830 case R_SPARC_32:
2831 if (outrel.r_offset & 3)
2832 r_type = R_SPARC_UA32;
2833 break;
2834 case R_SPARC_UA32:
2835 if (!(outrel.r_offset & 3))
2836 r_type = R_SPARC_32;
2837 break;
2838 case R_SPARC_64:
2839 if (outrel.r_offset & 7)
2840 r_type = R_SPARC_UA64;
2841 break;
2842 case R_SPARC_UA64:
2843 if (!(outrel.r_offset & 7))
2844 r_type = R_SPARC_64;
2845 break;
2846 case R_SPARC_DISP8:
2847 case R_SPARC_DISP16:
2848 case R_SPARC_DISP32:
2849 case R_SPARC_DISP64:
2850 /* If the symbol is not dynamic, we should not keep
2851 a dynamic relocation. But an .rela.* slot has been
2852 allocated for it, output R_SPARC_NONE.
2853 FIXME: Add code tracking needed dynamic relocs as
2854 e.g. i386 has. */
2855 if (h->dynindx == -1)
2856 skip = TRUE, relocate = TRUE;
2857 break;
2860 if (skip)
2861 memset (&outrel, 0, sizeof outrel);
2862 /* h->dynindx may be -1 if the symbol was marked to
2863 become local. */
2864 else if (h != NULL && ! is_plt
2865 && ((! info->symbolic && h->dynindx != -1)
2866 || !h->def_regular))
2868 BFD_ASSERT (h->dynindx != -1);
2869 outrel.r_info = SPARC_ELF_R_INFO (htab, rel, h->dynindx, r_type);
2870 outrel.r_addend = rel->r_addend;
2872 else
2874 if (r_type == R_SPARC_32 || r_type == R_SPARC_64)
2876 outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
2877 0, R_SPARC_RELATIVE);
2878 outrel.r_addend = relocation + rel->r_addend;
2880 else
2882 long indx;
2884 outrel.r_addend = relocation + rel->r_addend;
2886 if (is_plt)
2887 sec = htab->splt;
2889 if (bfd_is_abs_section (sec))
2890 indx = 0;
2891 else if (sec == NULL || sec->owner == NULL)
2893 bfd_set_error (bfd_error_bad_value);
2894 return FALSE;
2896 else
2898 asection *osec;
2900 /* We are turning this relocation into one
2901 against a section symbol. It would be
2902 proper to subtract the symbol's value,
2903 osec->vma, from the emitted reloc addend,
2904 but ld.so expects buggy relocs. */
2905 osec = sec->output_section;
2906 indx = elf_section_data (osec)->dynindx;
2908 if (indx == 0)
2910 osec = htab->elf.text_index_section;
2911 indx = elf_section_data (osec)->dynindx;
2914 /* FIXME: we really should be able to link non-pic
2915 shared libraries. */
2916 if (indx == 0)
2918 BFD_FAIL ();
2919 (*_bfd_error_handler)
2920 (_("%B: probably compiled without -fPIC?"),
2921 input_bfd);
2922 bfd_set_error (bfd_error_bad_value);
2923 return FALSE;
2927 outrel.r_info = SPARC_ELF_R_INFO (htab, rel, indx,
2928 r_type);
2932 sparc_elf_append_rela (output_bfd, sreloc, &outrel);
2934 /* This reloc will be computed at runtime, so there's no
2935 need to do anything now. */
2936 if (! relocate)
2937 continue;
2939 break;
2941 case R_SPARC_TLS_GD_HI22:
2942 if (! ABI_64_P (input_bfd)
2943 && ! _bfd_sparc_elf_tdata (input_bfd)->has_tlsgd)
2945 /* R_SPARC_REV32 used the same reloc number as
2946 R_SPARC_TLS_GD_HI22. */
2947 r_type = R_SPARC_REV32;
2948 break;
2950 /* Fall through */
2952 case R_SPARC_TLS_GD_LO10:
2953 case R_SPARC_TLS_IE_HI22:
2954 case R_SPARC_TLS_IE_LO10:
2955 r_type = sparc_elf_tls_transition (info, input_bfd, r_type, h == NULL);
2956 tls_type = GOT_UNKNOWN;
2957 if (h == NULL && local_got_offsets)
2958 tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
2959 else if (h != NULL)
2961 tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
2962 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2963 switch (SPARC_ELF_R_TYPE (rel->r_info))
2965 case R_SPARC_TLS_GD_HI22:
2966 case R_SPARC_TLS_IE_HI22:
2967 r_type = R_SPARC_TLS_LE_HIX22;
2968 break;
2969 default:
2970 r_type = R_SPARC_TLS_LE_LOX10;
2971 break;
2974 if (tls_type == GOT_TLS_IE)
2975 switch (r_type)
2977 case R_SPARC_TLS_GD_HI22:
2978 r_type = R_SPARC_TLS_IE_HI22;
2979 break;
2980 case R_SPARC_TLS_GD_LO10:
2981 r_type = R_SPARC_TLS_IE_LO10;
2982 break;
2985 if (r_type == R_SPARC_TLS_LE_HIX22)
2987 relocation = tpoff (info, relocation);
2988 break;
2990 if (r_type == R_SPARC_TLS_LE_LOX10)
2992 /* Change add into xor. */
2993 relocation = tpoff (info, relocation);
2994 bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd,
2995 contents + rel->r_offset)
2996 | 0x80182000), contents + rel->r_offset);
2997 break;
3000 if (h != NULL)
3002 off = h->got.offset;
3003 h->got.offset |= 1;
3005 else
3007 BFD_ASSERT (local_got_offsets != NULL);
3008 off = local_got_offsets[r_symndx];
3009 local_got_offsets[r_symndx] |= 1;
3012 r_sparc_tlsldm:
3013 if (htab->sgot == NULL)
3014 abort ();
3016 if ((off & 1) != 0)
3017 off &= ~1;
3018 else
3020 Elf_Internal_Rela outrel;
3021 int dr_type, indx;
3023 if (htab->srelgot == NULL)
3024 abort ();
3026 SPARC_ELF_PUT_WORD (htab, output_bfd, 0, htab->sgot->contents + off);
3027 outrel.r_offset = (htab->sgot->output_section->vma
3028 + htab->sgot->output_offset + off);
3029 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3030 if (r_type == R_SPARC_TLS_IE_HI22
3031 || r_type == R_SPARC_TLS_IE_LO10)
3032 dr_type = SPARC_ELF_TPOFF_RELOC (htab);
3033 else
3034 dr_type = SPARC_ELF_DTPMOD_RELOC (htab);
3035 if (dr_type == SPARC_ELF_TPOFF_RELOC (htab) && indx == 0)
3036 outrel.r_addend = relocation - dtpoff_base (info);
3037 else
3038 outrel.r_addend = 0;
3039 outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx, dr_type);
3040 sparc_elf_append_rela (output_bfd, htab->srelgot, &outrel);
3042 if (r_type == R_SPARC_TLS_GD_HI22
3043 || r_type == R_SPARC_TLS_GD_LO10)
3045 if (indx == 0)
3047 BFD_ASSERT (! unresolved_reloc);
3048 SPARC_ELF_PUT_WORD (htab, output_bfd,
3049 relocation - dtpoff_base (info),
3050 (htab->sgot->contents + off
3051 + SPARC_ELF_WORD_BYTES (htab)));
3053 else
3055 SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
3056 (htab->sgot->contents + off
3057 + SPARC_ELF_WORD_BYTES (htab)));
3058 outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx,
3059 SPARC_ELF_DTPOFF_RELOC (htab));
3060 outrel.r_offset += SPARC_ELF_WORD_BYTES (htab);
3061 sparc_elf_append_rela (output_bfd, htab->srelgot,
3062 &outrel);
3065 else if (dr_type == SPARC_ELF_DTPMOD_RELOC (htab))
3067 SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
3068 (htab->sgot->contents + off
3069 + SPARC_ELF_WORD_BYTES (htab)));
3073 if (off >= (bfd_vma) -2)
3074 abort ();
3076 relocation = htab->sgot->output_offset + off - got_base;
3077 unresolved_reloc = FALSE;
3078 howto = _bfd_sparc_elf_howto_table + r_type;
3079 break;
3081 case R_SPARC_TLS_LDM_HI22:
3082 case R_SPARC_TLS_LDM_LO10:
3083 if (! info->shared)
3085 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3086 continue;
3088 off = htab->tls_ldm_got.offset;
3089 htab->tls_ldm_got.offset |= 1;
3090 goto r_sparc_tlsldm;
3092 case R_SPARC_TLS_LDO_HIX22:
3093 case R_SPARC_TLS_LDO_LOX10:
3094 if (info->shared)
3096 relocation -= dtpoff_base (info);
3097 break;
3100 r_type = (r_type == R_SPARC_TLS_LDO_HIX22
3101 ? R_SPARC_TLS_LE_HIX22 : R_SPARC_TLS_LE_LOX10);
3102 /* Fall through. */
3104 case R_SPARC_TLS_LE_HIX22:
3105 case R_SPARC_TLS_LE_LOX10:
3106 if (info->shared)
3108 Elf_Internal_Rela outrel;
3109 bfd_boolean skip, relocate = FALSE;
3111 BFD_ASSERT (sreloc != NULL);
3112 skip = FALSE;
3113 outrel.r_offset =
3114 _bfd_elf_section_offset (output_bfd, info, input_section,
3115 rel->r_offset);
3116 if (outrel.r_offset == (bfd_vma) -1)
3117 skip = TRUE;
3118 else if (outrel.r_offset == (bfd_vma) -2)
3119 skip = TRUE, relocate = TRUE;
3120 outrel.r_offset += (input_section->output_section->vma
3121 + input_section->output_offset);
3122 if (skip)
3123 memset (&outrel, 0, sizeof outrel);
3124 else
3126 outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, r_type);
3127 outrel.r_addend = relocation - dtpoff_base (info)
3128 + rel->r_addend;
3131 sparc_elf_append_rela (output_bfd, sreloc, &outrel);
3132 continue;
3134 relocation = tpoff (info, relocation);
3135 break;
3137 case R_SPARC_TLS_LDM_CALL:
3138 if (! info->shared)
3140 /* mov %g0, %o0 */
3141 bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset);
3142 continue;
3144 /* Fall through */
3146 case R_SPARC_TLS_GD_CALL:
3147 tls_type = GOT_UNKNOWN;
3148 if (h == NULL && local_got_offsets)
3149 tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
3150 else if (h != NULL)
3151 tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
3152 if (! info->shared
3153 || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE))
3155 bfd_vma insn;
3157 if (!info->shared && (h == NULL || h->dynindx == -1))
3159 /* GD -> LE */
3160 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3161 continue;
3164 /* GD -> IE */
3165 if (rel + 1 < relend
3166 && SPARC_ELF_R_TYPE (rel[1].r_info) == R_SPARC_TLS_GD_ADD
3167 && rel[1].r_offset == rel->r_offset + 4
3168 && SPARC_ELF_R_SYMNDX (htab, rel[1].r_info) == r_symndx
3169 && (((insn = bfd_get_32 (input_bfd,
3170 contents + rel[1].r_offset))
3171 >> 25) & 0x1f) == 8)
3173 /* We have
3174 call __tls_get_addr, %tgd_call(foo)
3175 add %reg1, %reg2, %o0, %tgd_add(foo)
3176 and change it into IE:
3177 {ld,ldx} [%reg1 + %reg2], %o0, %tie_ldx(foo)
3178 add %g7, %o0, %o0, %tie_add(foo).
3179 add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2,
3180 ld is 0xc0000000 | (rd << 25) | (rs1 << 14) | rs2,
3181 ldx is 0xc0580000 | (rd << 25) | (rs1 << 14) | rs2. */
3182 bfd_put_32 (output_bfd, insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000),
3183 contents + rel->r_offset);
3184 bfd_put_32 (output_bfd, 0x9001c008,
3185 contents + rel->r_offset + 4);
3186 rel++;
3187 continue;
3190 bfd_put_32 (output_bfd, 0x9001c008, contents + rel->r_offset);
3191 continue;
3194 h = (struct elf_link_hash_entry *)
3195 bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE,
3196 FALSE, TRUE);
3197 BFD_ASSERT (h != NULL);
3198 r_type = R_SPARC_WPLT30;
3199 howto = _bfd_sparc_elf_howto_table + r_type;
3200 goto r_sparc_wplt30;
3202 case R_SPARC_TLS_GD_ADD:
3203 tls_type = GOT_UNKNOWN;
3204 if (h == NULL && local_got_offsets)
3205 tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
3206 else if (h != NULL)
3207 tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
3208 if (! info->shared || tls_type == GOT_TLS_IE)
3210 /* add %reg1, %reg2, %reg3, %tgd_add(foo)
3211 changed into IE:
3212 {ld,ldx} [%reg1 + %reg2], %reg3, %tie_ldx(foo)
3213 or LE:
3214 add %g7, %reg2, %reg3. */
3215 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3216 if ((h != NULL && h->dynindx != -1) || info->shared)
3217 relocation = insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000);
3218 else
3219 relocation = (insn & ~0x7c000) | 0x1c000;
3220 bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
3222 continue;
3224 case R_SPARC_TLS_LDM_ADD:
3225 if (! info->shared)
3226 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
3227 continue;
3229 case R_SPARC_TLS_LDO_ADD:
3230 if (! info->shared)
3232 /* Change rs1 into %g7. */
3233 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3234 insn = (insn & ~0x7c000) | 0x1c000;
3235 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
3237 continue;
3239 case R_SPARC_TLS_IE_LD:
3240 case R_SPARC_TLS_IE_LDX:
3241 if (! info->shared && (h == NULL || h->dynindx == -1))
3243 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3244 int rs2 = insn & 0x1f;
3245 int rd = (insn >> 25) & 0x1f;
3247 if (rs2 == rd)
3248 relocation = SPARC_NOP;
3249 else
3250 relocation = 0x80100000 | (insn & 0x3e00001f);
3251 bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
3253 continue;
3255 case R_SPARC_TLS_IE_ADD:
3256 /* Totally useless relocation. */
3257 continue;
3259 case R_SPARC_TLS_DTPOFF32:
3260 case R_SPARC_TLS_DTPOFF64:
3261 relocation -= dtpoff_base (info);
3262 break;
3264 default:
3265 break;
3268 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3269 because such sections are not SEC_ALLOC and thus ld.so will
3270 not process them. */
3271 if (unresolved_reloc
3272 && !((input_section->flags & SEC_DEBUGGING) != 0
3273 && h->def_dynamic))
3274 (*_bfd_error_handler)
3275 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3276 input_bfd,
3277 input_section,
3278 (long) rel->r_offset,
3279 howto->name,
3280 h->root.root.string);
3282 r = bfd_reloc_continue;
3283 if (r_type == R_SPARC_OLO10)
3285 bfd_vma x;
3287 if (! ABI_64_P (output_bfd))
3288 abort ();
3290 relocation += rel->r_addend;
3291 relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
3293 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3294 x = (x & ~(bfd_vma) 0x1fff) | (relocation & 0x1fff);
3295 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3297 r = bfd_check_overflow (howto->complain_on_overflow,
3298 howto->bitsize, howto->rightshift,
3299 bfd_arch_bits_per_address (input_bfd),
3300 relocation);
3302 else if (r_type == R_SPARC_WDISP16)
3304 bfd_vma x;
3306 relocation += rel->r_addend;
3307 relocation -= (input_section->output_section->vma
3308 + input_section->output_offset);
3309 relocation -= rel->r_offset;
3311 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3312 x |= ((((relocation >> 2) & 0xc000) << 6)
3313 | ((relocation >> 2) & 0x3fff));
3314 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3316 r = bfd_check_overflow (howto->complain_on_overflow,
3317 howto->bitsize, howto->rightshift,
3318 bfd_arch_bits_per_address (input_bfd),
3319 relocation);
3321 else if (r_type == R_SPARC_REV32)
3323 bfd_vma x;
3325 relocation = relocation + rel->r_addend;
3327 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3328 x = x + relocation;
3329 bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
3330 r = bfd_reloc_ok;
3332 else if (r_type == R_SPARC_TLS_LDO_HIX22
3333 || r_type == R_SPARC_TLS_LE_HIX22)
3335 bfd_vma x;
3337 relocation += rel->r_addend;
3338 if (r_type == R_SPARC_TLS_LE_HIX22)
3339 relocation ^= MINUS_ONE;
3341 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3342 x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
3343 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3344 r = bfd_reloc_ok;
3346 else if (r_type == R_SPARC_TLS_LDO_LOX10
3347 || r_type == R_SPARC_TLS_LE_LOX10)
3349 bfd_vma x;
3351 relocation += rel->r_addend;
3352 relocation &= 0x3ff;
3353 if (r_type == R_SPARC_TLS_LE_LOX10)
3354 relocation |= 0x1c00;
3356 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3357 x = (x & ~(bfd_vma) 0x1fff) | relocation;
3358 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3360 r = bfd_reloc_ok;
3362 else if (r_type == R_SPARC_HIX22)
3364 bfd_vma x;
3366 relocation += rel->r_addend;
3367 relocation = relocation ^ MINUS_ONE;
3369 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3370 x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
3371 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3373 r = bfd_check_overflow (howto->complain_on_overflow,
3374 howto->bitsize, howto->rightshift,
3375 bfd_arch_bits_per_address (input_bfd),
3376 relocation);
3378 else if (r_type == R_SPARC_LOX10)
3380 bfd_vma x;
3382 relocation += rel->r_addend;
3383 relocation = (relocation & 0x3ff) | 0x1c00;
3385 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3386 x = (x & ~(bfd_vma) 0x1fff) | relocation;
3387 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3389 r = bfd_reloc_ok;
3391 else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
3392 && sec_do_relax (input_section)
3393 && rel->r_offset + 4 < input_section->size)
3395 #define G0 0
3396 #define O7 15
3397 #define XCC (2 << 20)
3398 #define COND(x) (((x)&0xf)<<25)
3399 #define CONDA COND(0x8)
3400 #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
3401 #define INSN_BA (F2(0,2) | CONDA)
3402 #define INSN_OR F3(2, 0x2, 0)
3403 #define INSN_NOP F2(0,4)
3405 bfd_vma x, y;
3407 /* If the instruction is a call with either:
3408 restore
3409 arithmetic instruction with rd == %o7
3410 where rs1 != %o7 and rs2 if it is register != %o7
3411 then we can optimize if the call destination is near
3412 by changing the call into a branch always. */
3413 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
3414 y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
3415 if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
3417 if (((y & OP3(~0)) == OP3(0x3d) /* restore */
3418 || ((y & OP3(0x28)) == 0 /* arithmetic */
3419 && (y & RD(~0)) == RD(O7)))
3420 && (y & RS1(~0)) != RS1(O7)
3421 && ((y & F3I(~0))
3422 || (y & RS2(~0)) != RS2(O7)))
3424 bfd_vma reloc;
3426 reloc = relocation + rel->r_addend - rel->r_offset;
3427 reloc -= (input_section->output_section->vma
3428 + input_section->output_offset);
3430 /* Ensure the branch fits into simm22. */
3431 if ((reloc & 3) == 0
3432 && ((reloc & ~(bfd_vma)0x7fffff) == 0
3433 || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
3435 reloc >>= 2;
3437 /* Check whether it fits into simm19. */
3438 if (((reloc & 0x3c0000) == 0
3439 || (reloc & 0x3c0000) == 0x3c0000)
3440 && (ABI_64_P (output_bfd)
3441 || elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
3442 x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
3443 else
3444 x = INSN_BA | (reloc & 0x3fffff); /* ba */
3445 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
3446 r = bfd_reloc_ok;
3447 if (rel->r_offset >= 4
3448 && (y & (0xffffffff ^ RS1(~0)))
3449 == (INSN_OR | RD(O7) | RS2(G0)))
3451 bfd_vma z;
3452 unsigned int reg;
3454 z = bfd_get_32 (input_bfd,
3455 contents + rel->r_offset - 4);
3456 if ((z & (0xffffffff ^ RD(~0)))
3457 != (INSN_OR | RS1(O7) | RS2(G0)))
3458 break;
3460 /* The sequence was
3461 or %o7, %g0, %rN
3462 call foo
3463 or %rN, %g0, %o7
3465 If call foo was replaced with ba, replace
3466 or %rN, %g0, %o7 with nop. */
3468 reg = (y & RS1(~0)) >> 14;
3469 if (reg != ((z & RD(~0)) >> 25)
3470 || reg == G0 || reg == O7)
3471 break;
3473 bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
3474 contents + rel->r_offset + 4);
3482 if (r == bfd_reloc_continue)
3483 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3484 contents, rel->r_offset,
3485 relocation, rel->r_addend);
3487 if (r != bfd_reloc_ok)
3489 switch (r)
3491 default:
3492 case bfd_reloc_outofrange:
3493 abort ();
3494 case bfd_reloc_overflow:
3496 const char *name;
3498 /* The Solaris native linker silently disregards overflows.
3499 We don't, but this breaks stabs debugging info, whose
3500 relocations are only 32-bits wide. Ignore overflows in
3501 this case and also for discarded entries. */
3502 if ((r_type == R_SPARC_32 || r_type == R_SPARC_DISP32)
3503 && (((input_section->flags & SEC_DEBUGGING) != 0
3504 && strcmp (bfd_section_name (input_bfd,
3505 input_section),
3506 ".stab") == 0)
3507 || _bfd_elf_section_offset (output_bfd, info,
3508 input_section,
3509 rel->r_offset)
3510 == (bfd_vma)-1))
3511 break;
3513 if (h != NULL)
3515 /* Assume this is a call protected by other code that
3516 detect the symbol is undefined. If this is the case,
3517 we can safely ignore the overflow. If not, the
3518 program is hosed anyway, and a little warning isn't
3519 going to help. */
3520 if (h->root.type == bfd_link_hash_undefweak
3521 && howto->pc_relative)
3522 break;
3524 name = NULL;
3526 else
3528 name = bfd_elf_string_from_elf_section (input_bfd,
3529 symtab_hdr->sh_link,
3530 sym->st_name);
3531 if (name == NULL)
3532 return FALSE;
3533 if (*name == '\0')
3534 name = bfd_section_name (input_bfd, sec);
3536 if (! ((*info->callbacks->reloc_overflow)
3537 (info, (h ? &h->root : NULL), name, howto->name,
3538 (bfd_vma) 0, input_bfd, input_section,
3539 rel->r_offset)))
3540 return FALSE;
3542 break;
3547 return TRUE;
3550 /* Build a VxWorks PLT entry. PLT_INDEX is the index of the PLT entry
3551 and PLT_OFFSET is the byte offset from the start of .plt. GOT_OFFSET
3552 is the offset of the associated .got.plt entry from
3553 _GLOBAL_OFFSET_TABLE_. */
3555 static void
3556 sparc_vxworks_build_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
3557 bfd_vma plt_offset, bfd_vma plt_index,
3558 bfd_vma got_offset)
3560 bfd_vma got_base;
3561 const bfd_vma *plt_entry;
3562 struct _bfd_sparc_elf_link_hash_table *htab;
3563 bfd_byte *loc;
3564 Elf_Internal_Rela rela;
3566 htab = _bfd_sparc_elf_hash_table (info);
3567 if (info->shared)
3569 plt_entry = sparc_vxworks_shared_plt_entry;
3570 got_base = 0;
3572 else
3574 plt_entry = sparc_vxworks_exec_plt_entry;
3575 got_base = (htab->elf.hgot->root.u.def.value
3576 + htab->elf.hgot->root.u.def.section->output_offset
3577 + htab->elf.hgot->root.u.def.section->output_section->vma);
3580 /* Fill in the entry in the procedure linkage table. */
3581 bfd_put_32 (output_bfd, plt_entry[0] + ((got_base + got_offset) >> 10),
3582 htab->splt->contents + plt_offset);
3583 bfd_put_32 (output_bfd, plt_entry[1] + ((got_base + got_offset) & 0x3ff),
3584 htab->splt->contents + plt_offset + 4);
3585 bfd_put_32 (output_bfd, plt_entry[2],
3586 htab->splt->contents + plt_offset + 8);
3587 bfd_put_32 (output_bfd, plt_entry[3],
3588 htab->splt->contents + plt_offset + 12);
3589 bfd_put_32 (output_bfd, plt_entry[4],
3590 htab->splt->contents + plt_offset + 16);
3591 bfd_put_32 (output_bfd, plt_entry[5] + (plt_index >> 10),
3592 htab->splt->contents + plt_offset + 20);
3593 /* PC-relative displacement for a branch to the start of
3594 the PLT section. */
3595 bfd_put_32 (output_bfd, plt_entry[6] + (((-plt_offset - 24) >> 2)
3596 & 0x003fffff),
3597 htab->splt->contents + plt_offset + 24);
3598 bfd_put_32 (output_bfd, plt_entry[7] + (plt_index & 0x3ff),
3599 htab->splt->contents + plt_offset + 28);
3601 /* Fill in the .got.plt entry, pointing initially at the
3602 second half of the PLT entry. */
3603 BFD_ASSERT (htab->sgotplt != NULL);
3604 bfd_put_32 (output_bfd,
3605 htab->splt->output_section->vma
3606 + htab->splt->output_offset
3607 + plt_offset + 20,
3608 htab->sgotplt->contents + got_offset);
3610 /* Add relocations to .rela.plt.unloaded. */
3611 if (!info->shared)
3613 loc = (htab->srelplt2->contents
3614 + (2 + 3 * plt_index) * sizeof (Elf32_External_Rela));
3616 /* Relocate the initial sethi. */
3617 rela.r_offset = (htab->splt->output_section->vma
3618 + htab->splt->output_offset
3619 + plt_offset);
3620 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
3621 rela.r_addend = got_offset;
3622 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3623 loc += sizeof (Elf32_External_Rela);
3625 /* Likewise the following or. */
3626 rela.r_offset += 4;
3627 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
3628 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3629 loc += sizeof (Elf32_External_Rela);
3631 /* Relocate the .got.plt entry. */
3632 rela.r_offset = (htab->sgotplt->output_section->vma
3633 + htab->sgotplt->output_offset
3634 + got_offset);
3635 rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_SPARC_32);
3636 rela.r_addend = plt_offset + 20;
3637 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3641 /* Finish up dynamic symbol handling. We set the contents of various
3642 dynamic sections here. */
3644 bfd_boolean
3645 _bfd_sparc_elf_finish_dynamic_symbol (bfd *output_bfd,
3646 struct bfd_link_info *info,
3647 struct elf_link_hash_entry *h,
3648 Elf_Internal_Sym *sym)
3650 bfd *dynobj;
3651 struct _bfd_sparc_elf_link_hash_table *htab;
3652 const struct elf_backend_data *bed;
3654 htab = _bfd_sparc_elf_hash_table (info);
3655 dynobj = htab->elf.dynobj;
3656 bed = get_elf_backend_data (output_bfd);
3658 if (h->plt.offset != (bfd_vma) -1)
3660 asection *splt;
3661 asection *srela;
3662 Elf_Internal_Rela rela;
3663 bfd_byte *loc;
3664 bfd_vma r_offset, got_offset;
3665 int rela_index;
3667 /* This symbol has an entry in the PLT. Set it up. */
3669 BFD_ASSERT (h->dynindx != -1);
3671 splt = htab->splt;
3672 srela = htab->srelplt;
3673 BFD_ASSERT (splt != NULL && srela != NULL);
3675 /* Fill in the entry in the .rela.plt section. */
3676 if (htab->is_vxworks)
3678 /* Work out the index of this PLT entry. */
3679 rela_index = ((h->plt.offset - htab->plt_header_size)
3680 / htab->plt_entry_size);
3682 /* Calculate the offset of the associated .got.plt entry.
3683 The first three entries are reserved. */
3684 got_offset = (rela_index + 3) * 4;
3686 sparc_vxworks_build_plt_entry (output_bfd, info, h->plt.offset,
3687 rela_index, got_offset);
3690 /* On VxWorks, the relocation points to the .got.plt entry,
3691 not the .plt entry. */
3692 rela.r_offset = (htab->sgotplt->output_section->vma
3693 + htab->sgotplt->output_offset
3694 + got_offset);
3695 rela.r_addend = 0;
3697 else
3699 /* Fill in the entry in the procedure linkage table. */
3700 rela_index = SPARC_ELF_BUILD_PLT_ENTRY (htab, output_bfd, splt,
3701 h->plt.offset, splt->size,
3702 &r_offset);
3704 rela.r_offset = r_offset
3705 + (splt->output_section->vma + splt->output_offset);
3706 if (! ABI_64_P (output_bfd)
3707 || h->plt.offset < (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
3709 rela.r_addend = 0;
3711 else
3713 rela.r_addend = (-(h->plt.offset + 4)
3714 - splt->output_section->vma
3715 - splt->output_offset);
3718 rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_JMP_SLOT);
3720 /* Adjust for the first 4 reserved elements in the .plt section
3721 when setting the offset in the .rela.plt section.
3722 Sun forgot to read their own ABI and copied elf32-sparc behaviour,
3723 thus .plt[4] has corresponding .rela.plt[0] and so on. */
3725 loc = srela->contents;
3726 loc += rela_index * bed->s->sizeof_rela;
3727 bed->s->swap_reloca_out (output_bfd, &rela, loc);
3729 if (!h->def_regular)
3731 /* Mark the symbol as undefined, rather than as defined in
3732 the .plt section. Leave the value alone. */
3733 sym->st_shndx = SHN_UNDEF;
3734 /* If the symbol is weak, we do need to clear the value.
3735 Otherwise, the PLT entry would provide a definition for
3736 the symbol even if the symbol wasn't defined anywhere,
3737 and so the symbol would never be NULL. */
3738 if (!h->ref_regular_nonweak)
3739 sym->st_value = 0;
3743 if (h->got.offset != (bfd_vma) -1
3744 && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_GD
3745 && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_IE)
3747 asection *sgot;
3748 asection *srela;
3749 Elf_Internal_Rela rela;
3751 /* This symbol has an entry in the GOT. Set it up. */
3753 sgot = htab->sgot;
3754 srela = htab->srelgot;
3755 BFD_ASSERT (sgot != NULL && srela != NULL);
3757 rela.r_offset = (sgot->output_section->vma
3758 + sgot->output_offset
3759 + (h->got.offset &~ (bfd_vma) 1));
3761 /* If this is a -Bsymbolic link, and the symbol is defined
3762 locally, we just want to emit a RELATIVE reloc. Likewise if
3763 the symbol was forced to be local because of a version file.
3764 The entry in the global offset table will already have been
3765 initialized in the relocate_section function. */
3766 if (info->shared
3767 && (info->symbolic || h->dynindx == -1)
3768 && h->def_regular)
3770 asection *sec = h->root.u.def.section;
3771 rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, R_SPARC_RELATIVE);
3772 rela.r_addend = (h->root.u.def.value
3773 + sec->output_section->vma
3774 + sec->output_offset);
3776 else
3778 rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_GLOB_DAT);
3779 rela.r_addend = 0;
3782 SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
3783 sgot->contents + (h->got.offset & ~(bfd_vma) 1));
3784 sparc_elf_append_rela (output_bfd, srela, &rela);
3787 if (h->needs_copy)
3789 asection *s;
3790 Elf_Internal_Rela rela;
3792 /* This symbols needs a copy reloc. Set it up. */
3793 BFD_ASSERT (h->dynindx != -1);
3795 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3796 ".rela.bss");
3797 BFD_ASSERT (s != NULL);
3799 rela.r_offset = (h->root.u.def.value
3800 + h->root.u.def.section->output_section->vma
3801 + h->root.u.def.section->output_offset);
3802 rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_COPY);
3803 rela.r_addend = 0;
3804 sparc_elf_append_rela (output_bfd, s, &rela);
3807 /* Mark some specially defined symbols as absolute. On VxWorks,
3808 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
3809 ".got" section. Likewise _PROCEDURE_LINKAGE_TABLE_ and ".plt". */
3810 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3811 || (!htab->is_vxworks
3812 && (h == htab->elf.hgot || h == htab->elf.hplt)))
3813 sym->st_shndx = SHN_ABS;
3815 return TRUE;
3818 /* Finish up the dynamic sections. */
3820 static bfd_boolean
3821 sparc_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
3822 bfd *dynobj, asection *sdyn,
3823 asection *splt ATTRIBUTE_UNUSED)
3825 struct _bfd_sparc_elf_link_hash_table *htab;
3826 const struct elf_backend_data *bed;
3827 bfd_byte *dyncon, *dynconend;
3828 size_t dynsize;
3829 int stt_regidx = -1;
3830 bfd_boolean abi_64_p;
3832 htab = _bfd_sparc_elf_hash_table (info);
3833 bed = get_elf_backend_data (output_bfd);
3834 dynsize = bed->s->sizeof_dyn;
3835 dynconend = sdyn->contents + sdyn->size;
3836 abi_64_p = ABI_64_P (output_bfd);
3837 for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
3839 Elf_Internal_Dyn dyn;
3840 const char *name;
3841 bfd_boolean size;
3843 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
3845 if (htab->is_vxworks && dyn.d_tag == DT_RELASZ)
3847 /* On VxWorks, DT_RELASZ should not include the relocations
3848 in .rela.plt. */
3849 if (htab->srelplt)
3851 dyn.d_un.d_val -= htab->srelplt->size;
3852 bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
3855 else if (htab->is_vxworks && dyn.d_tag == DT_PLTGOT)
3857 /* On VxWorks, DT_PLTGOT should point to the start of the GOT,
3858 not to the start of the PLT. */
3859 if (htab->sgotplt)
3861 dyn.d_un.d_val = (htab->sgotplt->output_section->vma
3862 + htab->sgotplt->output_offset);
3863 bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
3866 else if (abi_64_p && dyn.d_tag == DT_SPARC_REGISTER)
3868 if (stt_regidx == -1)
3870 stt_regidx =
3871 _bfd_elf_link_lookup_local_dynindx (info, output_bfd, -1);
3872 if (stt_regidx == -1)
3873 return FALSE;
3875 dyn.d_un.d_val = stt_regidx++;
3876 bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
3878 else
3880 switch (dyn.d_tag)
3882 case DT_PLTGOT: name = ".plt"; size = FALSE; break;
3883 case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
3884 case DT_JMPREL: name = ".rela.plt"; size = FALSE; break;
3885 default: name = NULL; size = FALSE; break;
3888 if (name != NULL)
3890 asection *s;
3892 s = bfd_get_section_by_name (output_bfd, name);
3893 if (s == NULL)
3894 dyn.d_un.d_val = 0;
3895 else
3897 if (! size)
3898 dyn.d_un.d_ptr = s->vma;
3899 else
3900 dyn.d_un.d_val = s->size;
3902 bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
3906 return TRUE;
3909 /* Install the first PLT entry in a VxWorks executable and make sure that
3910 .rela.plt.unloaded relocations have the correct symbol indexes. */
3912 static void
3913 sparc_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
3915 struct _bfd_sparc_elf_link_hash_table *htab;
3916 Elf_Internal_Rela rela;
3917 bfd_vma got_base;
3918 bfd_byte *loc;
3920 htab = _bfd_sparc_elf_hash_table (info);
3922 /* Calculate the absolute value of _GLOBAL_OFFSET_TABLE_. */
3923 got_base = (htab->elf.hgot->root.u.def.section->output_section->vma
3924 + htab->elf.hgot->root.u.def.section->output_offset
3925 + htab->elf.hgot->root.u.def.value);
3927 /* Install the initial PLT entry. */
3928 bfd_put_32 (output_bfd,
3929 sparc_vxworks_exec_plt0_entry[0] + ((got_base + 8) >> 10),
3930 htab->splt->contents);
3931 bfd_put_32 (output_bfd,
3932 sparc_vxworks_exec_plt0_entry[1] + ((got_base + 8) & 0x3ff),
3933 htab->splt->contents + 4);
3934 bfd_put_32 (output_bfd,
3935 sparc_vxworks_exec_plt0_entry[2],
3936 htab->splt->contents + 8);
3937 bfd_put_32 (output_bfd,
3938 sparc_vxworks_exec_plt0_entry[3],
3939 htab->splt->contents + 12);
3940 bfd_put_32 (output_bfd,
3941 sparc_vxworks_exec_plt0_entry[4],
3942 htab->splt->contents + 16);
3944 loc = htab->srelplt2->contents;
3946 /* Add an unloaded relocation for the initial entry's "sethi". */
3947 rela.r_offset = (htab->splt->output_section->vma
3948 + htab->splt->output_offset);
3949 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
3950 rela.r_addend = 8;
3951 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3952 loc += sizeof (Elf32_External_Rela);
3954 /* Likewise the following "or". */
3955 rela.r_offset += 4;
3956 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
3957 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3958 loc += sizeof (Elf32_External_Rela);
3960 /* Fix up the remaining .rela.plt.unloaded relocations. They may have
3961 the wrong symbol index for _G_O_T_ or _P_L_T_ depending on the order
3962 in which symbols were output. */
3963 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
3965 Elf_Internal_Rela rel;
3967 /* The entry's initial "sethi" (against _G_O_T_). */
3968 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
3969 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
3970 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3971 loc += sizeof (Elf32_External_Rela);
3973 /* The following "or" (also against _G_O_T_). */
3974 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
3975 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
3976 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3977 loc += sizeof (Elf32_External_Rela);
3979 /* The .got.plt entry (against _P_L_T_). */
3980 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
3981 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_SPARC_32);
3982 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3983 loc += sizeof (Elf32_External_Rela);
3987 /* Install the first PLT entry in a VxWorks shared object. */
3989 static void
3990 sparc_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
3992 struct _bfd_sparc_elf_link_hash_table *htab;
3993 unsigned int i;
3995 htab = _bfd_sparc_elf_hash_table (info);
3996 for (i = 0; i < ARRAY_SIZE (sparc_vxworks_shared_plt0_entry); i++)
3997 bfd_put_32 (output_bfd, sparc_vxworks_shared_plt0_entry[i],
3998 htab->splt->contents + i * 4);
4001 bfd_boolean
4002 _bfd_sparc_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
4004 bfd *dynobj;
4005 asection *sdyn;
4006 struct _bfd_sparc_elf_link_hash_table *htab;
4008 htab = _bfd_sparc_elf_hash_table (info);
4009 dynobj = htab->elf.dynobj;
4011 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4013 if (elf_hash_table (info)->dynamic_sections_created)
4015 asection *splt;
4017 splt = bfd_get_section_by_name (dynobj, ".plt");
4018 BFD_ASSERT (splt != NULL && sdyn != NULL);
4020 if (!sparc_finish_dyn (output_bfd, info, dynobj, sdyn, splt))
4021 return FALSE;
4023 /* Initialize the contents of the .plt section. */
4024 if (splt->size > 0)
4026 if (htab->is_vxworks)
4028 if (info->shared)
4029 sparc_vxworks_finish_shared_plt (output_bfd, info);
4030 else
4031 sparc_vxworks_finish_exec_plt (output_bfd, info);
4033 else
4035 memset (splt->contents, 0, htab->plt_header_size);
4036 if (!ABI_64_P (output_bfd))
4037 bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
4038 splt->contents + splt->size - 4);
4042 elf_section_data (splt->output_section)->this_hdr.sh_entsize
4043 = (htab->is_vxworks || !ABI_64_P (output_bfd))
4044 ? 0 : htab->plt_entry_size;
4047 /* Set the first entry in the global offset table to the address of
4048 the dynamic section. */
4049 if (htab->sgot && htab->sgot->size > 0)
4051 bfd_vma val = (sdyn ?
4052 sdyn->output_section->vma + sdyn->output_offset :
4055 SPARC_ELF_PUT_WORD (htab, output_bfd, val, htab->sgot->contents);
4058 if (htab->sgot)
4059 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize =
4060 SPARC_ELF_WORD_BYTES (htab);
4062 return TRUE;
4066 /* Set the right machine number for a SPARC ELF file. */
4068 bfd_boolean
4069 _bfd_sparc_elf_object_p (bfd *abfd)
4071 if (ABI_64_P (abfd))
4073 unsigned long mach = bfd_mach_sparc_v9;
4075 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
4076 mach = bfd_mach_sparc_v9b;
4077 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
4078 mach = bfd_mach_sparc_v9a;
4079 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach);
4081 else
4083 if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
4085 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
4086 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4087 bfd_mach_sparc_v8plusb);
4088 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
4089 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4090 bfd_mach_sparc_v8plusa);
4091 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
4092 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4093 bfd_mach_sparc_v8plus);
4094 else
4095 return FALSE;
4097 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
4098 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
4099 bfd_mach_sparc_sparclite_le);
4100 else
4101 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
4105 /* Return address for Ith PLT stub in section PLT, for relocation REL
4106 or (bfd_vma) -1 if it should not be included. */
4108 bfd_vma
4109 _bfd_sparc_elf_plt_sym_val (bfd_vma i, const asection *plt, const arelent *rel)
4111 if (ABI_64_P (plt->owner))
4113 bfd_vma j;
4115 i += PLT64_HEADER_SIZE / PLT64_ENTRY_SIZE;
4116 if (i < PLT64_LARGE_THRESHOLD)
4117 return plt->vma + i * PLT64_ENTRY_SIZE;
4119 j = (i - PLT64_LARGE_THRESHOLD) % 160;
4120 i -= j;
4121 return plt->vma + i * PLT64_ENTRY_SIZE + j * 4 * 6;
4123 else
4124 return rel->address;