2006-09-16 Paul Brook <paul@codesourcery.com>
[deliverable/binutils-gdb.git] / bfd / elf32-sh.c
CommitLineData
ef230218 1/* Renesas / SuperH SH specific support for 32-bit ELF
22d606e9
AM
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by Ian Lance Taylor, Cygnus Support.
5
571fe01f 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
571fe01f
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
252b5132 12
571fe01f
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
571fe01f
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
3e110533 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132
RH
21
22#include "bfd.h"
23#include "sysdep.h"
24#include "bfdlink.h"
25#include "libbfd.h"
26#include "elf-bfd.h"
55e6e397 27#include "elf-vxworks.h"
252b5132 28#include "elf/sh.h"
f6f9408f 29#include "libiberty.h"
871ec896 30#include "../opcodes/sh-opc.h"
252b5132
RH
31
32static bfd_reloc_status_type sh_elf_reloc
09fd220b 33 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
252b5132 34static bfd_reloc_status_type sh_elf_ignore_reloc
09fd220b 35 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
b34976b6 36static bfd_boolean sh_elf_relax_delete_bytes
09fd220b 37 (bfd *, asection *, bfd_vma, int);
b34976b6 38static bfd_boolean sh_elf_align_loads
09fd220b 39 (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
85fbca6a 40#ifndef SH64_ELF
b34976b6 41static bfd_boolean sh_elf_swap_insns
09fd220b 42 (bfd *, asection *, void *, bfd_byte *, bfd_vma);
85fbca6a 43#endif
3376eaf5 44static int sh_elf_optimized_tls_reloc
09fd220b 45 (struct bfd_link_info *, int, int);
3376eaf5 46static bfd_vma dtpoff_base
09fd220b 47 (struct bfd_link_info *);
267fb3c1 48static bfd_vma tpoff
09fd220b 49 (struct bfd_link_info *, bfd_vma);
37c644f2
AO
50
51/* The name of the dynamic interpreter. This is put in the .interp
52 section. */
53
54#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
252b5132 55
55e6e397
RS
56#define MINUS_ONE ((bfd_vma) 0 - 1)
57\f
58#define SH_PARTIAL32 TRUE
59#define SH_SRC_MASK32 0xffffffff
60#define SH_ELF_RELOC sh_elf_reloc
38b1a46c
NC
61static reloc_howto_type sh_elf_howto_table[] =
62{
55e6e397
RS
63#include "elf32-sh-relocs.h"
64};
fbca6ad9 65
55e6e397
RS
66#define SH_PARTIAL32 FALSE
67#define SH_SRC_MASK32 0
68#define SH_ELF_RELOC bfd_elf_generic_reloc
69static reloc_howto_type sh_vxworks_howto_table[] =
70{
71#include "elf32-sh-relocs.h"
72};
73\f
74/* Return true if OUTPUT_BFD is a VxWorks object. */
fbca6ad9 75
55e6e397 76static bfd_boolean
527a23b8 77vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
55e6e397 78{
527a23b8 79#if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
55e6e397
RS
80 extern const bfd_target bfd_elf32_shlvxworks_vec;
81 extern const bfd_target bfd_elf32_shvxworks_vec;
fbca6ad9 82
55e6e397
RS
83 return (abfd->xvec == &bfd_elf32_shlvxworks_vec
84 || abfd->xvec == &bfd_elf32_shvxworks_vec);
527a23b8
NC
85#else
86 return FALSE;
87#endif
55e6e397 88}
fbca6ad9 89
55e6e397 90/* Return the howto table for ABFD. */
fbca6ad9 91
55e6e397
RS
92static reloc_howto_type *
93get_howto_table (bfd *abfd)
94{
95 if (vxworks_object_p (abfd))
96 return sh_vxworks_howto_table;
97 return sh_elf_howto_table;
98}
252b5132 99
015551fc 100static bfd_reloc_status_type
09fd220b
KK
101sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
102 asection *input_section, bfd_byte *contents,
103 bfd_vma addr, asection *symbol_section,
104 bfd_vma start, bfd_vma end)
015551fc
JR
105{
106 static bfd_vma last_addr;
00fdaf47 107 static asection *last_symbol_section;
015551fc
JR
108 bfd_byte *start_ptr, *ptr, *last_ptr;
109 int diff, cum_diff;
110 bfd_signed_vma x;
111 int insn;
112
113 /* Sanity check the address. */
07515404 114 if (addr > bfd_get_section_limit (input_bfd, input_section))
015551fc
JR
115 return bfd_reloc_outofrange;
116
117 /* We require the start and end relocations to be processed consecutively -
118 although we allow then to be processed forwards or backwards. */
119 if (! last_addr)
120 {
121 last_addr = addr;
122 last_symbol_section = symbol_section;
123 return bfd_reloc_ok;
124 }
125 if (last_addr != addr)
126 abort ();
127 last_addr = 0;
128
129 if (! symbol_section || last_symbol_section != symbol_section || end < start)
130 return bfd_reloc_outofrange;
131
132 /* Get the symbol_section contents. */
133 if (symbol_section != input_section)
134 {
135 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
136 contents = elf_section_data (symbol_section)->this_hdr.contents;
137 else
138 {
eea6121a
AM
139 if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
140 &contents))
015551fc 141 {
eea6121a
AM
142 if (contents != NULL)
143 free (contents);
015551fc
JR
144 return bfd_reloc_outofrange;
145 }
146 }
147 }
148#define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
149 start_ptr = contents + start;
150 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
151 {
152 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
153 ptr -= 2;
154 ptr += 2;
61ff1804 155 diff = (last_ptr - ptr) >> 1;
015551fc
JR
156 cum_diff += diff & 1;
157 cum_diff += diff;
158 }
159 /* Calculate the start / end values to load into rs / re minus four -
160 so that will cancel out the four we would otherwise have to add to
161 addr to get the value to subtract in order to get relative addressing. */
162 if (cum_diff >= 0)
163 {
164 start -= 4;
165 end = (ptr + cum_diff * 2) - contents;
166 }
167 else
168 {
169 bfd_vma start0 = start - 4;
170
a0fc8ba1 171 while (start0 && IS_PPI (contents + start0))
015551fc
JR
172 start0 -= 2;
173 start0 = start - 2 - ((start - start0) & 2);
174 start = start0 - cum_diff - 2;
175 end = start0;
176 }
177
6cdc0ccc
AM
178 if (contents != NULL
179 && elf_section_data (symbol_section)->this_hdr.contents != contents)
180 free (contents);
015551fc
JR
181
182 insn = bfd_get_16 (input_bfd, contents + addr);
183
184 x = (insn & 0x200 ? end : start) - addr;
185 if (input_section != symbol_section)
186 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
187 - (input_section->output_section->vma
188 + input_section->output_offset));
189 x >>= 1;
190 if (x < -128 || x > 127)
191 return bfd_reloc_overflow;
192
61ff1804 193 x = (insn & ~0xff) | (x & 0xff);
dc810e39 194 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
015551fc
JR
195
196 return bfd_reloc_ok;
197}
198
199/* This function is used for normal relocs. This used to be like the COFF
252b5132
RH
200 function, and is almost certainly incorrect for other ELF targets. */
201
202static bfd_reloc_status_type
09fd220b
KK
203sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
204 void *data, asection *input_section, bfd *output_bfd,
205 char **error_message ATTRIBUTE_UNUSED)
252b5132
RH
206{
207 unsigned long insn;
208 bfd_vma sym_value;
209 enum elf_sh_reloc_type r_type;
210 bfd_vma addr = reloc_entry->address;
211 bfd_byte *hit_data = addr + (bfd_byte *) data;
212
213 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
214
215 if (output_bfd != NULL)
216 {
217 /* Partial linking--do nothing. */
218 reloc_entry->address += input_section->output_offset;
219 return bfd_reloc_ok;
220 }
221
222 /* Almost all relocs have to do with relaxing. If any work must be
223 done for them, it has been done in sh_relax_section. */
015551fc 224 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
252b5132
RH
225 return bfd_reloc_ok;
226
227 if (symbol_in != NULL
228 && bfd_is_und_section (symbol_in->section))
229 return bfd_reloc_undefined;
230
231 if (bfd_is_com_section (symbol_in->section))
435b1e90
KH
232 sym_value = 0;
233 else
252b5132
RH
234 sym_value = (symbol_in->value +
235 symbol_in->section->output_section->vma +
236 symbol_in->section->output_offset);
237
238 switch (r_type)
239 {
240 case R_SH_DIR32:
241 insn = bfd_get_32 (abfd, hit_data);
242 insn += sym_value + reloc_entry->addend;
dc810e39 243 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
252b5132
RH
244 break;
245 case R_SH_IND12W:
246 insn = bfd_get_16 (abfd, hit_data);
247 sym_value += reloc_entry->addend;
248 sym_value -= (input_section->output_section->vma
249 + input_section->output_offset
250 + addr
251 + 4);
252 sym_value += (insn & 0xfff) << 1;
253 if (insn & 0x800)
254 sym_value -= 0x1000;
255 insn = (insn & 0xf000) | (sym_value & 0xfff);
dc810e39 256 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
252b5132
RH
257 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
258 return bfd_reloc_overflow;
259 break;
260 default:
261 abort ();
262 break;
263 }
264
265 return bfd_reloc_ok;
266}
267
268/* This function is used for relocs which are only used for relaxing,
269 which the linker should otherwise ignore. */
270
271static bfd_reloc_status_type
09fd220b
KK
272sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
273 asymbol *symbol ATTRIBUTE_UNUSED,
274 void *data ATTRIBUTE_UNUSED, asection *input_section,
275 bfd *output_bfd,
276 char **error_message ATTRIBUTE_UNUSED)
252b5132
RH
277{
278 if (output_bfd != NULL)
279 reloc_entry->address += input_section->output_offset;
280 return bfd_reloc_ok;
281}
282
283/* This structure is used to map BFD reloc codes to SH ELF relocs. */
284
38b1a46c
NC
285struct elf_reloc_map
286{
252b5132
RH
287 bfd_reloc_code_real_type bfd_reloc_val;
288 unsigned char elf_reloc_val;
289};
290
291/* An array mapping BFD reloc codes to SH ELF relocs. */
292
38b1a46c
NC
293static const struct elf_reloc_map sh_reloc_map[] =
294{
252b5132
RH
295 { BFD_RELOC_NONE, R_SH_NONE },
296 { BFD_RELOC_32, R_SH_DIR32 },
d38eb334
DD
297 { BFD_RELOC_16, R_SH_DIR16 },
298 { BFD_RELOC_8, R_SH_DIR8 },
252b5132
RH
299 { BFD_RELOC_CTOR, R_SH_DIR32 },
300 { BFD_RELOC_32_PCREL, R_SH_REL32 },
301 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
302 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
303 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
304 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
305 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
306 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
307 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
308 { BFD_RELOC_SH_USES, R_SH_USES },
309 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
310 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
311 { BFD_RELOC_SH_CODE, R_SH_CODE },
312 { BFD_RELOC_SH_DATA, R_SH_DATA },
313 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
314 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
315 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
015551fc
JR
316 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
317 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
3376eaf5
KK
318 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
319 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
320 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
321 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
322 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
323 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
324 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
325 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
37c644f2
AO
326 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
327 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
328 { BFD_RELOC_SH_COPY, R_SH_COPY },
329 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
330 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
331 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
332 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
333 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
fbca6ad9
AO
334 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
335#ifdef INCLUDE_SHMEDIA
336 { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
337 { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
338 { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
339 { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
340 { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
341 { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
342 { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
343 { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
344 { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
345 { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
346 { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
347 { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
348 { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
349 { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
350 { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
351 { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
352 { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
353 { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
354 { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
355 { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
356 { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
357 { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
358 { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
359 { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
360 { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
361 { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
362 { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
363 { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
364 { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
365 { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
366 { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
367 { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
368 { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
369 { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
370 { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
371 { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
372 { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
373 { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
374 { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
375 { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
376 { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
377 { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
378 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
379 { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
380 { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
381 { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
382 { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
383 { BFD_RELOC_64, R_SH_64 },
384 { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
385#endif /* not INCLUDE_SHMEDIA */
252b5132
RH
386};
387
388/* Given a BFD reloc code, return the howto structure for the
55e6e397 389 corresponding SH ELF reloc. */
252b5132
RH
390
391static reloc_howto_type *
55e6e397 392sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
252b5132
RH
393{
394 unsigned int i;
395
396 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
397 {
398 if (sh_reloc_map[i].bfd_reloc_val == code)
55e6e397 399 return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
252b5132
RH
400 }
401
402 return NULL;
403}
404
405/* Given an ELF reloc, fill in the howto field of a relent. */
406
407static void
55e6e397 408sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
252b5132
RH
409{
410 unsigned int r;
411
412 r = ELF32_R_TYPE (dst->r_info);
413
414 BFD_ASSERT (r < (unsigned int) R_SH_max);
415 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
06bb75c1 416 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
fbca6ad9
AO
417 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_3 || r > R_SH_LAST_INVALID_RELOC_3);
418 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_4 || r > R_SH_LAST_INVALID_RELOC_4);
3376eaf5 419 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_5 || r > R_SH_LAST_INVALID_RELOC_5);
252b5132 420
55e6e397 421 cache_ptr->howto = get_howto_table (abfd) + r;
252b5132
RH
422}
423\f
424/* This function handles relaxing for SH ELF. See the corresponding
425 function in coff-sh.c for a description of what this does. FIXME:
426 There is a lot of duplication here between this code and the COFF
427 specific code. The format of relocs and symbols is wound deeply
428 into this code, but it would still be better if the duplication
429 could be eliminated somehow. Note in particular that although both
430 functions use symbols like R_SH_CODE, those symbols have different
431 values; in coff-sh.c they come from include/coff/sh.h, whereas here
432 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
433
b34976b6 434static bfd_boolean
09fd220b
KK
435sh_elf_relax_section (bfd *abfd, asection *sec,
436 struct bfd_link_info *link_info, bfd_boolean *again)
252b5132
RH
437{
438 Elf_Internal_Shdr *symtab_hdr;
439 Elf_Internal_Rela *internal_relocs;
b34976b6 440 bfd_boolean have_code;
252b5132
RH
441 Elf_Internal_Rela *irel, *irelend;
442 bfd_byte *contents = NULL;
6cdc0ccc 443 Elf_Internal_Sym *isymbuf = NULL;
252b5132 444
b34976b6 445 *again = FALSE;
252b5132 446
1049f94e 447 if (link_info->relocatable
252b5132
RH
448 || (sec->flags & SEC_RELOC) == 0
449 || sec->reloc_count == 0)
b34976b6 450 return TRUE;
252b5132 451
fbca6ad9
AO
452#ifdef INCLUDE_SHMEDIA
453 if (elf_section_data (sec)->this_hdr.sh_flags
454 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
455 {
b34976b6 456 return TRUE;
fbca6ad9
AO
457 }
458#endif
459
252b5132
RH
460 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
461
45d6a902 462 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 463 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
252b5132
RH
464 link_info->keep_memory));
465 if (internal_relocs == NULL)
466 goto error_return;
252b5132 467
b34976b6 468 have_code = FALSE;
252b5132
RH
469
470 irelend = internal_relocs + sec->reloc_count;
471 for (irel = internal_relocs; irel < irelend; irel++)
472 {
473 bfd_vma laddr, paddr, symval;
474 unsigned short insn;
475 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
476 bfd_signed_vma foff;
477
478 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
b34976b6 479 have_code = TRUE;
252b5132
RH
480
481 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
482 continue;
483
484 /* Get the section contents. */
485 if (contents == NULL)
486 {
487 if (elf_section_data (sec)->this_hdr.contents != NULL)
488 contents = elf_section_data (sec)->this_hdr.contents;
489 else
490 {
eea6121a 491 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
252b5132
RH
492 goto error_return;
493 }
494 }
495
496 /* The r_addend field of the R_SH_USES reloc will point us to
067653c5
AM
497 the register load. The 4 is because the r_addend field is
498 computed as though it were a jump offset, which are based
499 from 4 bytes after the jump instruction. */
252b5132 500 laddr = irel->r_offset + 4 + irel->r_addend;
eea6121a 501 if (laddr >= sec->size)
252b5132 502 {
d003868e
AM
503 (*_bfd_error_handler) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
504 abfd,
252b5132
RH
505 (unsigned long) irel->r_offset);
506 continue;
507 }
508 insn = bfd_get_16 (abfd, contents + laddr);
509
510 /* If the instruction is not mov.l NN,rN, we don't know what to
067653c5 511 do. */
252b5132
RH
512 if ((insn & 0xf000) != 0xd000)
513 {
514 ((*_bfd_error_handler)
d003868e
AM
515 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
516 abfd, (unsigned long) irel->r_offset, insn));
252b5132
RH
517 continue;
518 }
519
520 /* Get the address from which the register is being loaded. The
99e4ae17
AJ
521 displacement in the mov.l instruction is quadrupled. It is a
522 displacement from four bytes after the movl instruction, but,
523 before adding in the PC address, two least significant bits
524 of the PC are cleared. We assume that the section is aligned
525 on a four byte boundary. */
252b5132
RH
526 paddr = insn & 0xff;
527 paddr *= 4;
dc810e39 528 paddr += (laddr + 4) &~ (bfd_vma) 3;
eea6121a 529 if (paddr >= sec->size)
252b5132
RH
530 {
531 ((*_bfd_error_handler)
d003868e
AM
532 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
533 abfd, (unsigned long) irel->r_offset));
252b5132
RH
534 continue;
535 }
536
537 /* Get the reloc for the address from which the register is
067653c5
AM
538 being loaded. This reloc will tell us which function is
539 actually being called. */
252b5132
RH
540 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
541 if (irelfn->r_offset == paddr
542 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
543 break;
544 if (irelfn >= irelend)
545 {
546 ((*_bfd_error_handler)
d003868e
AM
547 (_("%B: 0x%lx: warning: could not find expected reloc"),
548 abfd, (unsigned long) paddr));
252b5132
RH
549 continue;
550 }
551
552 /* Read this BFD's symbols if we haven't done so already. */
6cdc0ccc 553 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
252b5132 554 {
6cdc0ccc
AM
555 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
556 if (isymbuf == NULL)
557 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
558 symtab_hdr->sh_info, 0,
559 NULL, NULL, NULL);
560 if (isymbuf == NULL)
561 goto error_return;
252b5132
RH
562 }
563
564 /* Get the value of the symbol referred to by the reloc. */
565 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
566 {
9ad5cbcf 567 /* A local symbol. */
6cdc0ccc 568 Elf_Internal_Sym *isym;
252b5132 569
6cdc0ccc
AM
570 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
571 if (isym->st_shndx
d426c6b0 572 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
252b5132
RH
573 {
574 ((*_bfd_error_handler)
d003868e
AM
575 (_("%B: 0x%lx: warning: symbol in unexpected section"),
576 abfd, (unsigned long) paddr));
252b5132
RH
577 continue;
578 }
579
6cdc0ccc 580 symval = (isym->st_value
252b5132
RH
581 + sec->output_section->vma
582 + sec->output_offset);
583 }
584 else
585 {
586 unsigned long indx;
587 struct elf_link_hash_entry *h;
588
589 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
590 h = elf_sym_hashes (abfd)[indx];
591 BFD_ASSERT (h != NULL);
592 if (h->root.type != bfd_link_hash_defined
593 && h->root.type != bfd_link_hash_defweak)
594 {
595 /* This appears to be a reference to an undefined
067653c5
AM
596 symbol. Just ignore it--it will be caught by the
597 regular reloc processing. */
252b5132
RH
598 continue;
599 }
600
601 symval = (h->root.u.def.value
602 + h->root.u.def.section->output_section->vma
603 + h->root.u.def.section->output_offset);
604 }
605
55e6e397
RS
606 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
607 symval += bfd_get_32 (abfd, contents + paddr);
608 else
609 symval += irelfn->r_addend;
252b5132
RH
610
611 /* See if this function call can be shortened. */
612 foff = (symval
613 - (irel->r_offset
614 + sec->output_section->vma
615 + sec->output_offset
616 + 4));
f725025b
DD
617 /* A branch to an address beyond ours might be increased by an
618 .align that doesn't move when bytes behind us are deleted.
619 So, we add some slop in this calculation to allow for
620 that. */
621 if (foff < -0x1000 || foff >= 0x1000 - 8)
252b5132
RH
622 {
623 /* After all that work, we can't shorten this function call. */
624 continue;
625 }
626
627 /* Shorten the function call. */
628
629 /* For simplicity of coding, we are going to modify the section
630 contents, the section relocs, and the BFD symbol table. We
631 must tell the rest of the code not to free up this
632 information. It would be possible to instead create a table
633 of changes which have to be made, as is done in coff-mips.c;
634 that would be more work, but would require less memory when
635 the linker is run. */
636
637 elf_section_data (sec)->relocs = internal_relocs;
252b5132 638 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 639 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132
RH
640
641 /* Replace the jsr with a bsr. */
642
643 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
067653c5 644 replace the jsr with a bsr. */
252b5132 645 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
bdfaef52
JR
646 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
647 here, but that only checks if the symbol is an external symbol,
648 not if the symbol is in a different section. Besides, we need
649 a consistent meaning for the relocation, so we just assume here that
650 the value of the symbol is not available. */
0e71e495
BE
651
652 /* We can't fully resolve this yet, because the external
653 symbol value may be changed by future relaxing. We let
654 the final link phase handle it. */
655 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
656
bdfaef52 657 irel->r_addend = -4;
252b5132 658
f725025b
DD
659 /* When we calculated the symbol "value" we had an offset in the
660 DIR32's word in memory (we read and add it above). However,
661 the jsr we create does NOT have this offset encoded, so we
662 have to add it to the addend to preserve it. */
663 irel->r_addend += bfd_get_32 (abfd, contents + paddr);
664
252b5132 665 /* See if there is another R_SH_USES reloc referring to the same
067653c5 666 register load. */
252b5132
RH
667 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
668 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
669 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
670 break;
671 if (irelscan < irelend)
672 {
673 /* Some other function call depends upon this register load,
674 and we have not yet converted that function call.
675 Indeed, we may never be able to convert it. There is
676 nothing else we can do at this point. */
677 continue;
678 }
679
680 /* Look for a R_SH_COUNT reloc on the location where the
067653c5
AM
681 function address is stored. Do this before deleting any
682 bytes, to avoid confusion about the address. */
252b5132
RH
683 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
684 if (irelcount->r_offset == paddr
685 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
686 break;
687
688 /* Delete the register load. */
689 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
690 goto error_return;
691
692 /* That will change things, so, just in case it permits some
067653c5
AM
693 other function call to come within range, we should relax
694 again. Note that this is not required, and it may be slow. */
b34976b6 695 *again = TRUE;
252b5132
RH
696
697 /* Now check whether we got a COUNT reloc. */
698 if (irelcount >= irelend)
699 {
700 ((*_bfd_error_handler)
d003868e
AM
701 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
702 abfd, (unsigned long) paddr));
252b5132
RH
703 continue;
704 }
705
706 /* The number of uses is stored in the r_addend field. We've
067653c5 707 just deleted one. */
252b5132
RH
708 if (irelcount->r_addend == 0)
709 {
d003868e
AM
710 ((*_bfd_error_handler) (_("%B: 0x%lx: warning: bad count"),
711 abfd,
252b5132
RH
712 (unsigned long) paddr));
713 continue;
714 }
715
716 --irelcount->r_addend;
717
718 /* If there are no more uses, we can delete the address. Reload
067653c5
AM
719 the address from irelfn, in case it was changed by the
720 previous call to sh_elf_relax_delete_bytes. */
252b5132
RH
721 if (irelcount->r_addend == 0)
722 {
723 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
724 goto error_return;
725 }
726
727 /* We've done all we can with that function call. */
728 }
729
730 /* Look for load and store instructions that we can align on four
731 byte boundaries. */
bdfaef52
JR
732 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
733 && have_code)
252b5132 734 {
b34976b6 735 bfd_boolean swapped;
252b5132
RH
736
737 /* Get the section contents. */
738 if (contents == NULL)
739 {
740 if (elf_section_data (sec)->this_hdr.contents != NULL)
741 contents = elf_section_data (sec)->this_hdr.contents;
742 else
743 {
eea6121a 744 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
252b5132
RH
745 goto error_return;
746 }
747 }
748
749 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
750 &swapped))
751 goto error_return;
752
753 if (swapped)
754 {
755 elf_section_data (sec)->relocs = internal_relocs;
252b5132 756 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 757 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132
RH
758 }
759 }
760
6cdc0ccc
AM
761 if (isymbuf != NULL
762 && symtab_hdr->contents != (unsigned char *) isymbuf)
252b5132
RH
763 {
764 if (! link_info->keep_memory)
6cdc0ccc 765 free (isymbuf);
252b5132
RH
766 else
767 {
6cdc0ccc
AM
768 /* Cache the symbols for elf_link_input_bfd. */
769 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132 770 }
9ad5cbcf
AM
771 }
772
6cdc0ccc
AM
773 if (contents != NULL
774 && elf_section_data (sec)->this_hdr.contents != contents)
252b5132
RH
775 {
776 if (! link_info->keep_memory)
6cdc0ccc
AM
777 free (contents);
778 else
252b5132 779 {
6cdc0ccc
AM
780 /* Cache the section contents for elf_link_input_bfd. */
781 elf_section_data (sec)->this_hdr.contents = contents;
252b5132 782 }
252b5132
RH
783 }
784
6cdc0ccc
AM
785 if (internal_relocs != NULL
786 && elf_section_data (sec)->relocs != internal_relocs)
787 free (internal_relocs);
788
b34976b6 789 return TRUE;
252b5132
RH
790
791 error_return:
6cdc0ccc
AM
792 if (isymbuf != NULL
793 && symtab_hdr->contents != (unsigned char *) isymbuf)
794 free (isymbuf);
795 if (contents != NULL
796 && elf_section_data (sec)->this_hdr.contents != contents)
797 free (contents);
798 if (internal_relocs != NULL
799 && elf_section_data (sec)->relocs != internal_relocs)
800 free (internal_relocs);
9ad5cbcf 801
b34976b6 802 return FALSE;
252b5132
RH
803}
804
805/* Delete some bytes from a section while relaxing. FIXME: There is a
806 lot of duplication between this function and sh_relax_delete_bytes
807 in coff-sh.c. */
808
b34976b6 809static bfd_boolean
09fd220b
KK
810sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
811 int count)
252b5132
RH
812{
813 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 814 unsigned int sec_shndx;
252b5132
RH
815 bfd_byte *contents;
816 Elf_Internal_Rela *irel, *irelend;
817 Elf_Internal_Rela *irelalign;
818 bfd_vma toaddr;
6cdc0ccc 819 Elf_Internal_Sym *isymbuf, *isym, *isymend;
9ad5cbcf
AM
820 struct elf_link_hash_entry **sym_hashes;
821 struct elf_link_hash_entry **end_hashes;
822 unsigned int symcount;
252b5132
RH
823 asection *o;
824
825 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc 826 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9ad5cbcf
AM
827
828 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132
RH
829
830 contents = elf_section_data (sec)->this_hdr.contents;
831
832 /* The deletion must stop at the next ALIGN reloc for an aligment
833 power larger than the number of bytes we are deleting. */
834
835 irelalign = NULL;
eea6121a 836 toaddr = sec->size;
252b5132
RH
837
838 irel = elf_section_data (sec)->relocs;
839 irelend = irel + sec->reloc_count;
840 for (; irel < irelend; irel++)
841 {
842 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
843 && irel->r_offset > addr
844 && count < (1 << irel->r_addend))
845 {
846 irelalign = irel;
847 toaddr = irel->r_offset;
848 break;
849 }
850 }
851
852 /* Actually delete the bytes. */
dc810e39
AM
853 memmove (contents + addr, contents + addr + count,
854 (size_t) (toaddr - addr - count));
252b5132 855 if (irelalign == NULL)
eea6121a 856 sec->size -= count;
252b5132
RH
857 else
858 {
859 int i;
860
861#define NOP_OPCODE (0x0009)
862
863 BFD_ASSERT ((count & 1) == 0);
864 for (i = 0; i < count; i += 2)
dc810e39 865 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
252b5132
RH
866 }
867
868 /* Adjust all the relocs. */
869 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
870 {
871 bfd_vma nraddr, stop;
872 bfd_vma start = 0;
873 int insn = 0;
252b5132
RH
874 int off, adjust, oinsn;
875 bfd_signed_vma voff = 0;
b34976b6 876 bfd_boolean overflow;
252b5132
RH
877
878 /* Get the new reloc address. */
879 nraddr = irel->r_offset;
880 if ((irel->r_offset > addr
881 && irel->r_offset < toaddr)
882 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
883 && irel->r_offset == toaddr))
884 nraddr -= count;
885
886 /* See if this reloc was for the bytes we have deleted, in which
887 case we no longer care about it. Don't delete relocs which
888 represent addresses, though. */
889 if (irel->r_offset >= addr
890 && irel->r_offset < addr + count
891 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
892 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
893 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
894 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
895 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
896 (int) R_SH_NONE);
897
898 /* If this is a PC relative reloc, see if the range it covers
067653c5 899 includes the bytes we have deleted. */
252b5132
RH
900 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
901 {
902 default:
903 break;
904
905 case R_SH_DIR8WPN:
906 case R_SH_IND12W:
907 case R_SH_DIR8WPZ:
908 case R_SH_DIR8WPL:
909 start = irel->r_offset;
910 insn = bfd_get_16 (abfd, contents + nraddr);
911 break;
912 }
913
914 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
915 {
916 default:
917 start = stop = addr;
918 break;
919
920 case R_SH_DIR32:
921 /* If this reloc is against a symbol defined in this
067653c5
AM
922 section, and the symbol will not be adjusted below, we
923 must check the addend to see it will put the value in
924 range to be adjusted, and hence must be changed. */
252b5132
RH
925 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
926 {
6cdc0ccc
AM
927 isym = isymbuf + ELF32_R_SYM (irel->r_info);
928 if (isym->st_shndx == sec_shndx
929 && (isym->st_value <= addr
930 || isym->st_value >= toaddr))
252b5132
RH
931 {
932 bfd_vma val;
933
55e6e397
RS
934 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
935 {
936 val = bfd_get_32 (abfd, contents + nraddr);
937 val += isym->st_value;
938 if (val > addr && val < toaddr)
939 bfd_put_32 (abfd, val - count, contents + nraddr);
940 }
941 else
942 {
943 val = isym->st_value + irel->r_addend;
944 if (val > addr && val < toaddr)
945 irel->r_addend -= count;
946 }
252b5132
RH
947 }
948 }
949 start = stop = addr;
950 break;
951
952 case R_SH_DIR8WPN:
953 off = insn & 0xff;
954 if (off & 0x80)
955 off -= 0x100;
956 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
957 break;
958
959 case R_SH_IND12W:
bdfaef52
JR
960 off = insn & 0xfff;
961 if (! off)
962 {
963 /* This has been made by previous relaxation. Since the
964 relocation will be against an external symbol, the
965 final relocation will just do the right thing. */
966 start = stop = addr;
967 }
252b5132
RH
968 else
969 {
252b5132
RH
970 if (off & 0x800)
971 off -= 0x1000;
972 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
bdfaef52
JR
973
974 /* The addend will be against the section symbol, thus
975 for adjusting the addend, the relevant start is the
976 start of the section.
4cc11e76 977 N.B. If we want to abandon in-place changes here and
bdfaef52
JR
978 test directly using symbol + addend, we have to take into
979 account that the addend has already been adjusted by -4. */
980 if (stop > addr && stop < toaddr)
981 irel->r_addend -= count;
252b5132
RH
982 }
983 break;
984
985 case R_SH_DIR8WPZ:
986 off = insn & 0xff;
987 stop = start + 4 + off * 2;
988 break;
989
990 case R_SH_DIR8WPL:
991 off = insn & 0xff;
435b1e90 992 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
252b5132
RH
993 break;
994
995 case R_SH_SWITCH8:
996 case R_SH_SWITCH16:
997 case R_SH_SWITCH32:
998 /* These relocs types represent
999 .word L2-L1
06e1ba78 1000 The r_addend field holds the difference between the reloc
252b5132
RH
1001 address and L1. That is the start of the reloc, and
1002 adding in the contents gives us the top. We must adjust
06e1ba78
JR
1003 both the r_offset field and the section contents.
1004 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1005 and the elf bfd r_offset is called r_vaddr. */
252b5132 1006
06e1ba78
JR
1007 stop = irel->r_offset;
1008 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
252b5132
RH
1009
1010 if (start > addr
1011 && start < toaddr
1012 && (stop <= addr || stop >= toaddr))
1013 irel->r_addend += count;
1014 else if (stop > addr
1015 && stop < toaddr
1016 && (start <= addr || start >= toaddr))
1017 irel->r_addend -= count;
1018
252b5132
RH
1019 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1020 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1021 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1022 voff = bfd_get_8 (abfd, contents + nraddr);
1023 else
1024 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1025 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1026
1027 break;
1028
1029 case R_SH_USES:
1030 start = irel->r_offset;
1031 stop = (bfd_vma) ((bfd_signed_vma) start
1032 + (long) irel->r_addend
1033 + 4);
1034 break;
1035 }
1036
1037 if (start > addr
1038 && start < toaddr
1039 && (stop <= addr || stop >= toaddr))
1040 adjust = count;
1041 else if (stop > addr
1042 && stop < toaddr
1043 && (start <= addr || start >= toaddr))
1044 adjust = - count;
1045 else
1046 adjust = 0;
1047
1048 if (adjust != 0)
1049 {
1050 oinsn = insn;
b34976b6 1051 overflow = FALSE;
252b5132
RH
1052 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1053 {
1054 default:
1055 abort ();
1056 break;
1057
1058 case R_SH_DIR8WPN:
1059 case R_SH_DIR8WPZ:
1060 insn += adjust / 2;
1061 if ((oinsn & 0xff00) != (insn & 0xff00))
b34976b6 1062 overflow = TRUE;
dc810e39 1063 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
252b5132
RH
1064 break;
1065
1066 case R_SH_IND12W:
1067 insn += adjust / 2;
1068 if ((oinsn & 0xf000) != (insn & 0xf000))
b34976b6 1069 overflow = TRUE;
dc810e39 1070 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
252b5132
RH
1071 break;
1072
1073 case R_SH_DIR8WPL:
1074 BFD_ASSERT (adjust == count || count >= 4);
1075 if (count >= 4)
1076 insn += adjust / 4;
1077 else
1078 {
1079 if ((irel->r_offset & 3) == 0)
1080 ++insn;
1081 }
1082 if ((oinsn & 0xff00) != (insn & 0xff00))
b34976b6 1083 overflow = TRUE;
dc810e39 1084 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
252b5132
RH
1085 break;
1086
851cde10
JR
1087 case R_SH_SWITCH8:
1088 voff += adjust;
1089 if (voff < 0 || voff >= 0xff)
b34976b6 1090 overflow = TRUE;
851cde10
JR
1091 bfd_put_8 (abfd, voff, contents + nraddr);
1092 break;
1093
252b5132
RH
1094 case R_SH_SWITCH16:
1095 voff += adjust;
1096 if (voff < - 0x8000 || voff >= 0x8000)
b34976b6 1097 overflow = TRUE;
dc810e39 1098 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
252b5132
RH
1099 break;
1100
1101 case R_SH_SWITCH32:
1102 voff += adjust;
dc810e39 1103 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
252b5132
RH
1104 break;
1105
1106 case R_SH_USES:
1107 irel->r_addend += adjust;
1108 break;
1109 }
1110
1111 if (overflow)
1112 {
1113 ((*_bfd_error_handler)
d003868e
AM
1114 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1115 abfd, (unsigned long) irel->r_offset));
252b5132 1116 bfd_set_error (bfd_error_bad_value);
b34976b6 1117 return FALSE;
252b5132
RH
1118 }
1119 }
1120
1121 irel->r_offset = nraddr;
1122 }
1123
1124 /* Look through all the other sections. If there contain any IMM32
1125 relocs against internal symbols which we are not going to adjust
1126 below, we may need to adjust the addends. */
1127 for (o = abfd->sections; o != NULL; o = o->next)
1128 {
1129 Elf_Internal_Rela *internal_relocs;
1130 Elf_Internal_Rela *irelscan, *irelscanend;
1131 bfd_byte *ocontents;
1132
1133 if (o == sec
1134 || (o->flags & SEC_RELOC) == 0
1135 || o->reloc_count == 0)
1136 continue;
1137
1138 /* We always cache the relocs. Perhaps, if info->keep_memory is
b34976b6 1139 FALSE, we should free them, if we are permitted to, when we
067653c5 1140 leave sh_coff_relax_section. */
45d6a902 1141 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 1142 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
252b5132 1143 if (internal_relocs == NULL)
b34976b6 1144 return FALSE;
252b5132
RH
1145
1146 ocontents = NULL;
1147 irelscanend = internal_relocs + o->reloc_count;
1148 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1149 {
084aa3aa
JR
1150 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1151 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1152 {
1153 bfd_vma start, stop;
1154 bfd_signed_vma voff;
1155
1156 if (ocontents == NULL)
1157 {
1158 if (elf_section_data (o)->this_hdr.contents != NULL)
1159 ocontents = elf_section_data (o)->this_hdr.contents;
1160 else
1161 {
1162 /* We always cache the section contents.
b34976b6 1163 Perhaps, if info->keep_memory is FALSE, we
067653c5
AM
1164 should free them, if we are permitted to,
1165 when we leave sh_coff_relax_section. */
eea6121a
AM
1166 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1167 {
1168 if (ocontents != NULL)
1169 free (ocontents);
1170 return FALSE;
1171 }
1172
084aa3aa
JR
1173 elf_section_data (o)->this_hdr.contents = ocontents;
1174 }
1175 }
1176
1177 stop = irelscan->r_offset;
1178 start
1179 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1180
1181 /* STOP is in a different section, so it won't change. */
1182 if (start > addr && start < toaddr)
1183 irelscan->r_addend += count;
1184
1185 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1186 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1187
1188 if (start > addr
1189 && start < toaddr
1190 && (stop <= addr || stop >= toaddr))
dc810e39 1191 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
084aa3aa
JR
1192 ocontents + irelscan->r_offset);
1193 else if (stop > addr
1194 && stop < toaddr
1195 && (start <= addr || start >= toaddr))
dc810e39 1196 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
084aa3aa
JR
1197 ocontents + irelscan->r_offset);
1198 }
1199
252b5132
RH
1200 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1201 continue;
1202
1203 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1204 continue;
1205
252b5132 1206
6cdc0ccc
AM
1207 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1208 if (isym->st_shndx == sec_shndx
1209 && (isym->st_value <= addr
1210 || isym->st_value >= toaddr))
252b5132
RH
1211 {
1212 bfd_vma val;
1213
1214 if (ocontents == NULL)
1215 {
1216 if (elf_section_data (o)->this_hdr.contents != NULL)
1217 ocontents = elf_section_data (o)->this_hdr.contents;
1218 else
1219 {
1220 /* We always cache the section contents.
b34976b6 1221 Perhaps, if info->keep_memory is FALSE, we
067653c5
AM
1222 should free them, if we are permitted to,
1223 when we leave sh_coff_relax_section. */
eea6121a
AM
1224 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1225 {
1226 if (ocontents != NULL)
1227 free (ocontents);
1228 return FALSE;
1229 }
1230
252b5132
RH
1231 elf_section_data (o)->this_hdr.contents = ocontents;
1232 }
1233 }
1234
1235 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
6cdc0ccc 1236 val += isym->st_value;
252b5132
RH
1237 if (val > addr && val < toaddr)
1238 bfd_put_32 (abfd, val - count,
1239 ocontents + irelscan->r_offset);
1240 }
1241 }
1242 }
1243
1244 /* Adjust the local symbols defined in this section. */
6cdc0ccc
AM
1245 isymend = isymbuf + symtab_hdr->sh_info;
1246 for (isym = isymbuf; isym < isymend; isym++)
252b5132 1247 {
6cdc0ccc
AM
1248 if (isym->st_shndx == sec_shndx
1249 && isym->st_value > addr
1250 && isym->st_value < toaddr)
1251 isym->st_value -= count;
252b5132
RH
1252 }
1253
1254 /* Now adjust the global symbols defined in this section. */
9ad5cbcf
AM
1255 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1256 - symtab_hdr->sh_info);
1257 sym_hashes = elf_sym_hashes (abfd);
1258 end_hashes = sym_hashes + symcount;
1259 for (; sym_hashes < end_hashes; sym_hashes++)
252b5132 1260 {
9ad5cbcf
AM
1261 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1262 if ((sym_hash->root.type == bfd_link_hash_defined
1263 || sym_hash->root.type == bfd_link_hash_defweak)
1264 && sym_hash->root.u.def.section == sec
1265 && sym_hash->root.u.def.value > addr
1266 && sym_hash->root.u.def.value < toaddr)
252b5132 1267 {
9ad5cbcf 1268 sym_hash->root.u.def.value -= count;
252b5132
RH
1269 }
1270 }
1271
1272 /* See if we can move the ALIGN reloc forward. We have adjusted
1273 r_offset for it already. */
1274 if (irelalign != NULL)
1275 {
1276 bfd_vma alignto, alignaddr;
1277
1278 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1279 alignaddr = BFD_ALIGN (irelalign->r_offset,
1280 1 << irelalign->r_addend);
1281 if (alignto != alignaddr)
1282 {
1283 /* Tail recursion. */
1284 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
dc810e39 1285 (int) (alignto - alignaddr));
252b5132
RH
1286 }
1287 }
1288
b34976b6 1289 return TRUE;
252b5132
RH
1290}
1291
1292/* Look for loads and stores which we can align to four byte
1293 boundaries. This is like sh_align_loads in coff-sh.c. */
1294
b34976b6 1295static bfd_boolean
09fd220b
KK
1296sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1297 Elf_Internal_Rela *internal_relocs,
1298 bfd_byte *contents ATTRIBUTE_UNUSED,
1299 bfd_boolean *pswapped)
252b5132
RH
1300{
1301 Elf_Internal_Rela *irel, *irelend;
1302 bfd_vma *labels = NULL;
1303 bfd_vma *label, *label_end;
dc810e39 1304 bfd_size_type amt;
252b5132 1305
b34976b6 1306 *pswapped = FALSE;
252b5132
RH
1307
1308 irelend = internal_relocs + sec->reloc_count;
1309
1310 /* Get all the addresses with labels on them. */
dc810e39
AM
1311 amt = sec->reloc_count;
1312 amt *= sizeof (bfd_vma);
1313 labels = (bfd_vma *) bfd_malloc (amt);
252b5132
RH
1314 if (labels == NULL)
1315 goto error_return;
1316 label_end = labels;
1317 for (irel = internal_relocs; irel < irelend; irel++)
1318 {
1319 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1320 {
1321 *label_end = irel->r_offset;
1322 ++label_end;
1323 }
1324 }
1325
1326 /* Note that the assembler currently always outputs relocs in
1327 address order. If that ever changes, this code will need to sort
1328 the label values and the relocs. */
1329
1330 label = labels;
1331
1332 for (irel = internal_relocs; irel < irelend; irel++)
1333 {
1334 bfd_vma start, stop;
1335
1336 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1337 continue;
1338
1339 start = irel->r_offset;
1340
1341 for (irel++; irel < irelend; irel++)
1342 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1343 break;
1344 if (irel < irelend)
1345 stop = irel->r_offset;
1346 else
eea6121a 1347 stop = sec->size;
252b5132
RH
1348
1349 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
09fd220b 1350 internal_relocs, &label,
252b5132
RH
1351 label_end, start, stop, pswapped))
1352 goto error_return;
1353 }
1354
1355 free (labels);
1356
b34976b6 1357 return TRUE;
252b5132
RH
1358
1359 error_return:
1360 if (labels != NULL)
1361 free (labels);
b34976b6 1362 return FALSE;
252b5132
RH
1363}
1364
85fbca6a 1365#ifndef SH64_ELF
252b5132
RH
1366/* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1367
b34976b6 1368static bfd_boolean
09fd220b
KK
1369sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1370 bfd_byte *contents, bfd_vma addr)
252b5132
RH
1371{
1372 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1373 unsigned short i1, i2;
1374 Elf_Internal_Rela *irel, *irelend;
1375
1376 /* Swap the instructions themselves. */
1377 i1 = bfd_get_16 (abfd, contents + addr);
1378 i2 = bfd_get_16 (abfd, contents + addr + 2);
dc810e39
AM
1379 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1380 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
252b5132
RH
1381
1382 /* Adjust all reloc addresses. */
1383 irelend = internal_relocs + sec->reloc_count;
1384 for (irel = internal_relocs; irel < irelend; irel++)
1385 {
1386 enum elf_sh_reloc_type type;
1387 int add;
1388
1389 /* There are a few special types of relocs that we don't want to
067653c5
AM
1390 adjust. These relocs do not apply to the instruction itself,
1391 but are only associated with the address. */
252b5132
RH
1392 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1393 if (type == R_SH_ALIGN
1394 || type == R_SH_CODE
1395 || type == R_SH_DATA
1396 || type == R_SH_LABEL)
1397 continue;
1398
1399 /* If an R_SH_USES reloc points to one of the addresses being
067653c5
AM
1400 swapped, we must adjust it. It would be incorrect to do this
1401 for a jump, though, since we want to execute both
1402 instructions after the jump. (We have avoided swapping
1403 around a label, so the jump will not wind up executing an
1404 instruction it shouldn't). */
252b5132
RH
1405 if (type == R_SH_USES)
1406 {
1407 bfd_vma off;
1408
1409 off = irel->r_offset + 4 + irel->r_addend;
1410 if (off == addr)
1411 irel->r_offset += 2;
1412 else if (off == addr + 2)
1413 irel->r_offset -= 2;
1414 }
1415
1416 if (irel->r_offset == addr)
1417 {
1418 irel->r_offset += 2;
1419 add = -2;
1420 }
1421 else if (irel->r_offset == addr + 2)
1422 {
1423 irel->r_offset -= 2;
1424 add = 2;
1425 }
1426 else
1427 add = 0;
1428
1429 if (add != 0)
1430 {
1431 bfd_byte *loc;
1432 unsigned short insn, oinsn;
b34976b6 1433 bfd_boolean overflow;
252b5132
RH
1434
1435 loc = contents + irel->r_offset;
b34976b6 1436 overflow = FALSE;
252b5132
RH
1437 switch (type)
1438 {
1439 default:
1440 break;
1441
1442 case R_SH_DIR8WPN:
1443 case R_SH_DIR8WPZ:
1444 insn = bfd_get_16 (abfd, loc);
1445 oinsn = insn;
1446 insn += add / 2;
1447 if ((oinsn & 0xff00) != (insn & 0xff00))
b34976b6 1448 overflow = TRUE;
dc810e39 1449 bfd_put_16 (abfd, (bfd_vma) insn, loc);
252b5132
RH
1450 break;
1451
1452 case R_SH_IND12W:
1453 insn = bfd_get_16 (abfd, loc);
1454 oinsn = insn;
1455 insn += add / 2;
1456 if ((oinsn & 0xf000) != (insn & 0xf000))
b34976b6 1457 overflow = TRUE;
dc810e39 1458 bfd_put_16 (abfd, (bfd_vma) insn, loc);
252b5132
RH
1459 break;
1460
1461 case R_SH_DIR8WPL:
1462 /* This reloc ignores the least significant 3 bits of
067653c5
AM
1463 the program counter before adding in the offset.
1464 This means that if ADDR is at an even address, the
1465 swap will not affect the offset. If ADDR is an at an
1466 odd address, then the instruction will be crossing a
1467 four byte boundary, and must be adjusted. */
252b5132
RH
1468 if ((addr & 3) != 0)
1469 {
1470 insn = bfd_get_16 (abfd, loc);
1471 oinsn = insn;
1472 insn += add / 2;
1473 if ((oinsn & 0xff00) != (insn & 0xff00))
b34976b6 1474 overflow = TRUE;
dc810e39 1475 bfd_put_16 (abfd, (bfd_vma) insn, loc);
252b5132
RH
1476 }
1477
1478 break;
1479 }
1480
1481 if (overflow)
1482 {
1483 ((*_bfd_error_handler)
d003868e
AM
1484 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1485 abfd, (unsigned long) irel->r_offset));
252b5132 1486 bfd_set_error (bfd_error_bad_value);
b34976b6 1487 return FALSE;
252b5132
RH
1488 }
1489 }
1490 }
1491
b34976b6 1492 return TRUE;
252b5132 1493}
85fbca6a 1494#endif /* defined SH64_ELF */
252b5132 1495\f
55e6e397
RS
1496/* Describes one of the various PLT styles. */
1497
1498struct elf_sh_plt_info
1499{
1500 /* The template for the first PLT entry, or NULL if there is no special
1501 first entry. */
1502 const bfd_byte *plt0_entry;
1503
1504 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1505 bfd_vma plt0_entry_size;
1506
1507 /* Index I is the offset into PLT0_ENTRY of a pointer to
1508 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1509 if there is no such pointer. */
1510 bfd_vma plt0_got_fields[3];
1511
1512 /* The template for a symbol's PLT entry. */
1513 const bfd_byte *symbol_entry;
1514
1515 /* The size of SYMBOL_ENTRY in bytes. */
1516 bfd_vma symbol_entry_size;
1517
1518 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1519 on all targets. The comments by each member indicate the value
1520 that the field must hold. */
1521 struct {
1522 bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1523 bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1524 bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1525 } symbol_fields;
1526
1527 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1528 bfd_vma symbol_resolve_offset;
1529};
1530
fbca6ad9
AO
1531#ifdef INCLUDE_SHMEDIA
1532
1533/* The size in bytes of an entry in the procedure linkage table. */
1534
55e6e397 1535#define ELF_PLT_ENTRY_SIZE 64
fbca6ad9
AO
1536
1537/* First entry in an absolute procedure linkage table look like this. */
1538
55e6e397 1539static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1540{
1541 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1542 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1543 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
c8614e8e 1544 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
fbca6ad9
AO
1545 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1546 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1547 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1548 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1549 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1550 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1551 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1552 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1553 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1554 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1555 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1556 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1557};
1558
55e6e397 1559static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1560{
1561 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1562 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1563 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
c8614e8e 1564 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
fbca6ad9
AO
1565 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1566 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1567 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1568 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1569 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1570 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1571 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1572 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1573 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1574 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1575 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1576 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1577};
1578
1579/* Sebsequent entries in an absolute procedure linkage table look like
1580 this. */
1581
55e6e397 1582static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1583{
1584 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1585 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1586 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1587 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1588 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1589 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1590 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1591 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1592 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1593 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1594 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1595 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1596 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1597 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1598 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1599 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1600};
1601
55e6e397 1602static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1603{
1604 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1605 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1606 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1607 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1608 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1609 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1610 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1611 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1612 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1613 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1614 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1615 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1616 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1617 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1618 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1619 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1620};
1621
1622/* Entries in a PIC procedure linkage table look like this. */
1623
55e6e397 1624static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1625{
1626 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1627 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1628 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1629 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1630 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1631 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1632 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1633 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1634 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
0a4ef3f4 1635 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
fbca6ad9
AO
1636 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1637 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1638 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1639 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1640 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1641 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1642};
1643
55e6e397 1644static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1645{
1646 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1647 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1648 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1649 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1650 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1651 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1652 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1653 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1654 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
0a4ef3f4 1655 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
fbca6ad9
AO
1656 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1657 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1658 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1659 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1660 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1661 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1662};
1663
55e6e397
RS
1664static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1665 {
1666 {
1667 /* Big-endian non-PIC. */
1668 elf_sh_plt0_entry_be,
1669 ELF_PLT_ENTRY_SIZE,
1670 { 0, MINUS_ONE, MINUS_ONE },
1671 elf_sh_plt_entry_be,
1672 ELF_PLT_ENTRY_SIZE,
1673 { 0, 32, 48 },
1674 33 /* includes ISA encoding */
1675 },
1676 {
1677 /* Little-endian non-PIC. */
1678 elf_sh_plt0_entry_le,
1679 ELF_PLT_ENTRY_SIZE,
1680 { 0, MINUS_ONE, MINUS_ONE },
1681 elf_sh_plt_entry_le,
1682 ELF_PLT_ENTRY_SIZE,
1683 { 0, 32, 48 },
1684 33 /* includes ISA encoding */
1685 },
1686 },
1687 {
1688 {
1689 /* Big-endian PIC. */
1690 elf_sh_plt0_entry_be,
1691 ELF_PLT_ENTRY_SIZE,
1692 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1693 elf_sh_pic_plt_entry_be,
1694 ELF_PLT_ENTRY_SIZE,
1695 { 0, MINUS_ONE, 52 },
1696 33 /* includes ISA encoding */
1697 },
1698 {
1699 /* Little-endian PIC. */
1700 elf_sh_plt0_entry_le,
1701 ELF_PLT_ENTRY_SIZE,
1702 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1703 elf_sh_pic_plt_entry_le,
1704 ELF_PLT_ENTRY_SIZE,
1705 { 0, MINUS_ONE, 52 },
1706 33 /* includes ISA encoding */
1707 },
1708 }
1709};
fbca6ad9
AO
1710
1711/* Return offset of the linker in PLT0 entry. */
1712#define elf_sh_plt0_gotplt_offset(info) 0
1713
55e6e397
RS
1714/* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1715 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1716 not data.
fbca6ad9 1717
55e6e397 1718 On SH64, each 32-bit field is loaded by a movi/shori pair. */
fbca6ad9
AO
1719
1720inline static void
55e6e397
RS
1721install_plt_field (bfd *output_bfd, bfd_boolean code_p,
1722 unsigned long value, bfd_byte *addr)
fbca6ad9 1723{
55e6e397 1724 value |= code_p;
fbca6ad9
AO
1725 bfd_put_32 (output_bfd,
1726 bfd_get_32 (output_bfd, addr)
1727 | ((value >> 6) & 0x3fffc00),
1728 addr);
1729 bfd_put_32 (output_bfd,
1730 bfd_get_32 (output_bfd, addr + 4)
1731 | ((value << 10) & 0x3fffc00),
1732 addr + 4);
1733}
1734
55e6e397
RS
1735/* Return the type of PLT associated with ABFD. PIC_P is true if
1736 the object is position-independent. */
1737
1738static const struct elf_sh_plt_info *
1739get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
1740{
1741 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
1742}
fbca6ad9 1743#else
37c644f2 1744/* The size in bytes of an entry in the procedure linkage table. */
252b5132 1745
55e6e397 1746#define ELF_PLT_ENTRY_SIZE 28
37c644f2
AO
1747
1748/* First entry in an absolute procedure linkage table look like this. */
1749
6c426cf3 1750/* Note - this code has been "optimised" not to use r2. r2 is used by
4cc11e76 1751 GCC to return the address of large structures, so it should not be
6c426cf3
NC
1752 corrupted here. This does mean however, that this PLT does not conform
1753 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1754 and r2 contains the GOT id. This version stores the GOT id in r0 and
1755 ignores the type. Loaders can easily detect this difference however,
1756 since the type will always be 0 or 8, and the GOT ids will always be
1757 greater than or equal to 12. */
55e6e397 1758static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1759{
1760 0xd0, 0x05, /* mov.l 2f,r0 */
1761 0x60, 0x02, /* mov.l @r0,r0 */
1762 0x2f, 0x06, /* mov.l r0,@-r15 */
1763 0xd0, 0x03, /* mov.l 1f,r0 */
1764 0x60, 0x02, /* mov.l @r0,r0 */
1765 0x40, 0x2b, /* jmp @r0 */
1766 0x60, 0xf6, /* mov.l @r15+,r0 */
1767 0x00, 0x09, /* nop */
1768 0x00, 0x09, /* nop */
1769 0x00, 0x09, /* nop */
1770 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1771 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1772};
1773
55e6e397 1774static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1775{
1776 0x05, 0xd0, /* mov.l 2f,r0 */
1777 0x02, 0x60, /* mov.l @r0,r0 */
1778 0x06, 0x2f, /* mov.l r0,@-r15 */
1779 0x03, 0xd0, /* mov.l 1f,r0 */
1780 0x02, 0x60, /* mov.l @r0,r0 */
1781 0x2b, 0x40, /* jmp @r0 */
1782 0xf6, 0x60, /* mov.l @r15+,r0 */
1783 0x09, 0x00, /* nop */
1784 0x09, 0x00, /* nop */
1785 0x09, 0x00, /* nop */
1786 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1787 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1788};
1789
1790/* Sebsequent entries in an absolute procedure linkage table look like
1791 this. */
1792
55e6e397 1793static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1794{
1795 0xd0, 0x04, /* mov.l 1f,r0 */
55e6e397 1796 0x60, 0x02, /* mov.l @(r0,r12),r0 */
6c426cf3
NC
1797 0xd1, 0x02, /* mov.l 0f,r1 */
1798 0x40, 0x2b, /* jmp @r0 */
1799 0x60, 0x13, /* mov r1,r0 */
1800 0xd1, 0x03, /* mov.l 2f,r1 */
1801 0x40, 0x2b, /* jmp @r0 */
1802 0x00, 0x09, /* nop */
1803 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1804 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1805 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1806};
1807
55e6e397 1808static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1809{
1810 0x04, 0xd0, /* mov.l 1f,r0 */
1811 0x02, 0x60, /* mov.l @r0,r0 */
1812 0x02, 0xd1, /* mov.l 0f,r1 */
1813 0x2b, 0x40, /* jmp @r0 */
1814 0x13, 0x60, /* mov r1,r0 */
1815 0x03, 0xd1, /* mov.l 2f,r1 */
1816 0x2b, 0x40, /* jmp @r0 */
1817 0x09, 0x00, /* nop */
1818 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1819 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1820 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1821};
1822
1823/* Entries in a PIC procedure linkage table look like this. */
1824
55e6e397 1825static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1826{
1827 0xd0, 0x04, /* mov.l 1f,r0 */
1828 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1829 0x40, 0x2b, /* jmp @r0 */
1830 0x00, 0x09, /* nop */
1831 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1832 0xd1, 0x03, /* mov.l 2f,r1 */
1833 0x40, 0x2b, /* jmp @r0 */
1834 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1835 0x00, 0x09, /* nop */
1836 0x00, 0x09, /* nop */
1837 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1838 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1839};
1840
55e6e397 1841static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1842{
1843 0x04, 0xd0, /* mov.l 1f,r0 */
1844 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1845 0x2b, 0x40, /* jmp @r0 */
1846 0x09, 0x00, /* nop */
1847 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1848 0x03, 0xd1, /* mov.l 2f,r1 */
1849 0x2b, 0x40, /* jmp @r0 */
1850 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1851 0x09, 0x00, /* nop */
1852 0x09, 0x00, /* nop */
1853 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1854 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1855};
1856
55e6e397
RS
1857static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1858 {
1859 {
1860 /* Big-endian non-PIC. */
1861 elf_sh_plt0_entry_be,
1862 ELF_PLT_ENTRY_SIZE,
1863 { MINUS_ONE, 24, 20 },
1864 elf_sh_plt_entry_be,
1865 ELF_PLT_ENTRY_SIZE,
1866 { 20, 16, 24 },
1867 8
1868 },
1869 {
1870 /* Little-endian non-PIC. */
1871 elf_sh_plt0_entry_le,
1872 ELF_PLT_ENTRY_SIZE,
1873 { MINUS_ONE, 24, 20 },
1874 elf_sh_plt_entry_le,
1875 ELF_PLT_ENTRY_SIZE,
1876 { 20, 16, 24 },
1877 8
1878 },
1879 },
1880 {
1881 {
1882 /* Big-endian PIC. */
1883 elf_sh_plt0_entry_be,
1884 ELF_PLT_ENTRY_SIZE,
1885 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1886 elf_sh_pic_plt_entry_be,
1887 ELF_PLT_ENTRY_SIZE,
1888 { 20, MINUS_ONE, 24 },
1889 8
1890 },
1891 {
1892 /* Little-endian PIC. */
1893 elf_sh_plt0_entry_le,
1894 ELF_PLT_ENTRY_SIZE,
1895 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1896 elf_sh_pic_plt_entry_le,
1897 ELF_PLT_ENTRY_SIZE,
1898 { 20, MINUS_ONE, 24 },
1899 8
1900 },
1901 }
1902};
252b5132 1903
55e6e397
RS
1904#define VXWORKS_PLT_HEADER_SIZE 12
1905#define VXWORKS_PLT_ENTRY_SIZE 24
252b5132 1906
55e6e397
RS
1907static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
1908{
1909 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1910 0x61, 0x12, /* mov.l @r1,r1 */
1911 0x41, 0x2b, /* jmp @r1 */
1912 0x00, 0x09, /* nop */
1913 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1914};
252b5132 1915
55e6e397
RS
1916static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
1917{
1918 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1919 0x12, 0x61, /* mov.l @r1,r1 */
1920 0x2b, 0x41, /* jmp @r1 */
1921 0x09, 0x00, /* nop */
1922 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1923};
1924
1925static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1926{
1927 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1928 0x60, 0x02, /* mov.l @r0,r0 */
1929 0x40, 0x2b, /* jmp @r0 */
1930 0x00, 0x09, /* nop */
1931 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1932 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1933 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1934 0x00, 0x09, /* nop */
1935 0x00, 0x09, /* nop */
1936 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1937};
1938
1939static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1940{
1941 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1942 0x02, 0x60, /* mov.l @r0,r0 */
1943 0x2b, 0x40, /* jmp @r0 */
1944 0x09, 0x00, /* nop */
1945 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1946 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1947 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1948 0x09, 0x00, /* nop */
1949 0x09, 0x00, /* nop */
1950 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1951};
1952
1953static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1954{
1955 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1956 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1957 0x40, 0x2b, /* jmp @r0 */
1958 0x00, 0x09, /* nop */
1959 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1960 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1961 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1962 0x41, 0x2b, /* jmp @r1 */
1963 0x00, 0x09, /* nop */
1964 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1965};
1966
1967static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1968{
1969 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1970 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1971 0x2b, 0x40, /* jmp @r0 */
1972 0x09, 0x00, /* nop */
1973 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1974 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1975 0xc2, 0x51, /* mov.l @(8,r12),r1 */
1976 0x2b, 0x41, /* jmp @r1 */
1977 0x09, 0x00, /* nop */
1978 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1979};
1980
1981static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
1982 {
1983 {
1984 /* Big-endian non-PIC. */
1985 vxworks_sh_plt0_entry_be,
1986 VXWORKS_PLT_HEADER_SIZE,
1987 { MINUS_ONE, MINUS_ONE, 8 },
1988 vxworks_sh_plt_entry_be,
1989 VXWORKS_PLT_ENTRY_SIZE,
1990 { 8, 14, 20 },
1991 12
1992 },
1993 {
1994 /* Little-endian non-PIC. */
1995 vxworks_sh_plt0_entry_le,
1996 VXWORKS_PLT_HEADER_SIZE,
1997 { MINUS_ONE, MINUS_ONE, 8 },
1998 vxworks_sh_plt_entry_le,
1999 VXWORKS_PLT_ENTRY_SIZE,
2000 { 8, 14, 20 },
2001 12
2002 },
2003 },
2004 {
2005 {
2006 /* Big-endian PIC. */
2007 NULL,
2008 0,
2009 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2010 vxworks_sh_pic_plt_entry_be,
2011 VXWORKS_PLT_ENTRY_SIZE,
2012 { 8, MINUS_ONE, 20 },
2013 12
2014 },
2015 {
2016 /* Little-endian PIC. */
2017 NULL,
2018 0,
2019 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2020 vxworks_sh_pic_plt_entry_le,
2021 VXWORKS_PLT_ENTRY_SIZE,
2022 { 8, MINUS_ONE, 20 },
2023 12
2024 },
2025 }
2026};
9bdafcce 2027
55e6e397
RS
2028/* Return the type of PLT associated with ABFD. PIC_P is true if
2029 the object is position-independent. */
9bdafcce 2030
55e6e397
RS
2031static const struct elf_sh_plt_info *
2032get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
2033{
2034 if (vxworks_object_p (abfd))
2035 return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2036 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2037}
9bdafcce 2038
55e6e397
RS
2039/* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2040 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2041 not data. */
9bdafcce 2042
55e6e397
RS
2043inline static void
2044install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
2045 unsigned long value, bfd_byte *addr)
2046{
2047 bfd_put_32 (output_bfd, value, addr);
2048}
fbca6ad9 2049#endif
015551fc 2050
55e6e397
RS
2051/* Return the index of the PLT entry at byte offset OFFSET. */
2052
2053static bfd_vma
2054get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2055{
2056 return (offset - info->plt0_entry_size) / info->symbol_entry_size;
2057}
2058
2059/* Do the inverse operation. */
2060
2061static bfd_vma
2062get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma index)
2063{
2064 return info->plt0_entry_size + (index * info->symbol_entry_size);
2065}
2066
37c644f2 2067/* The sh linker needs to keep track of the number of relocs that it
067653c5
AM
2068 decides to copy as dynamic relocs in check_relocs for each symbol.
2069 This is so that it can later discard them if they are found to be
2070 unnecessary. We store the information in a field extending the
2071 regular ELF linker hash table. */
252b5132 2072
067653c5 2073struct elf_sh_dyn_relocs
38b1a46c 2074{
067653c5
AM
2075 struct elf_sh_dyn_relocs *next;
2076
2077 /* The input section of the reloc. */
2078 asection *sec;
2079
2080 /* Total number of relocs copied for the input section. */
37c644f2 2081 bfd_size_type count;
067653c5
AM
2082
2083 /* Number of pc-relative relocs copied for the input section. */
2084 bfd_size_type pc_count;
37c644f2 2085};
252b5132 2086
37c644f2
AO
2087/* sh ELF linker hash entry. */
2088
38b1a46c
NC
2089struct elf_sh_link_hash_entry
2090{
37c644f2
AO
2091 struct elf_link_hash_entry root;
2092
fbca6ad9 2093#ifdef INCLUDE_SHMEDIA
396a6083
SC
2094 union
2095 {
2096 bfd_signed_vma refcount;
2097 bfd_vma offset;
2098 } datalabel_got;
fbca6ad9
AO
2099#endif
2100
067653c5
AM
2101 /* Track dynamic relocs copied for this symbol. */
2102 struct elf_sh_dyn_relocs *dyn_relocs;
4989d792
SC
2103
2104 bfd_signed_vma gotplt_refcount;
3376eaf5
KK
2105
2106 enum {
2107 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE
2108 } tls_type;
3376eaf5
KK
2109};
2110
2111#define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2112
2113struct sh_elf_obj_tdata
2114{
2115 struct elf_obj_tdata root;
2116
2117 /* tls_type for each local got entry. */
2118 char *local_got_tls_type;
37c644f2
AO
2119};
2120
3376eaf5
KK
2121#define sh_elf_tdata(abfd) \
2122 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2123
2124#define sh_elf_local_got_tls_type(abfd) \
2125 (sh_elf_tdata (abfd)->local_got_tls_type)
2126
2127/* Override the generic function because we need to store sh_elf_obj_tdata
2128 as the specific tdata. */
2129
b34976b6 2130static bfd_boolean
09fd220b 2131sh_elf_mkobject (bfd *abfd)
3376eaf5 2132{
3376eaf5 2133 if (abfd->tdata.any == NULL)
62d7a5f6
AM
2134 {
2135 bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
2136 abfd->tdata.any = bfd_zalloc (abfd, amt);
2137 if (abfd->tdata.any == NULL)
2138 return FALSE;
2139 }
2140 return bfd_elf_mkobject (abfd);
3376eaf5 2141}
b34976b6 2142
37c644f2
AO
2143/* sh ELF linker hash table. */
2144
38b1a46c
NC
2145struct elf_sh_link_hash_table
2146{
37c644f2 2147 struct elf_link_hash_table root;
37c644f2 2148
067653c5
AM
2149 /* Short-cuts to get to dynamic linker sections. */
2150 asection *sgot;
2151 asection *sgotplt;
2152 asection *srelgot;
2153 asection *splt;
2154 asection *srelplt;
2155 asection *sdynbss;
2156 asection *srelbss;
37c644f2 2157
55e6e397
RS
2158 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2159 asection *srelplt2;
2160
067653c5
AM
2161 /* Small local sym to section mapping cache. */
2162 struct sym_sec_cache sym_sec;
3376eaf5
KK
2163
2164 /* A counter or offset to track a TLS got entry. */
2165 union
2166 {
2167 bfd_signed_vma refcount;
2168 bfd_vma offset;
2169 } tls_ldm_got;
55e6e397
RS
2170
2171 /* The type of PLT to use. */
2172 const struct elf_sh_plt_info *plt_info;
2173
2174 /* True if the target system is VxWorks. */
2175 bfd_boolean vxworks_p;
067653c5 2176};
37c644f2
AO
2177
2178/* Traverse an sh ELF linker hash table. */
2179
2180#define sh_elf_link_hash_traverse(table, func, info) \
2181 (elf_link_hash_traverse \
2182 (&(table)->root, \
09fd220b 2183 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
37c644f2
AO
2184 (info)))
2185
2186/* Get the sh ELF linker hash table from a link_info structure. */
2187
2188#define sh_elf_hash_table(p) \
2189 ((struct elf_sh_link_hash_table *) ((p)->hash))
2190
2191/* Create an entry in an sh ELF linker hash table. */
2192
2193static struct bfd_hash_entry *
09fd220b
KK
2194sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2195 struct bfd_hash_table *table,
2196 const char *string)
37c644f2
AO
2197{
2198 struct elf_sh_link_hash_entry *ret =
2199 (struct elf_sh_link_hash_entry *) entry;
2200
2201 /* Allocate the structure if it has not already been allocated by a
2202 subclass. */
2203 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2204 ret = ((struct elf_sh_link_hash_entry *)
2205 bfd_hash_allocate (table,
2206 sizeof (struct elf_sh_link_hash_entry)));
2207 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2208 return (struct bfd_hash_entry *) ret;
2209
2210 /* Call the allocation method of the superclass. */
2211 ret = ((struct elf_sh_link_hash_entry *)
2212 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2213 table, string));
2214 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2215 {
396a6083
SC
2216 ret->dyn_relocs = NULL;
2217 ret->gotplt_refcount = 0;
fbca6ad9 2218#ifdef INCLUDE_SHMEDIA
396a6083 2219 ret->datalabel_got.refcount = ret->root.got.refcount;
fbca6ad9 2220#endif
3376eaf5 2221 ret->tls_type = GOT_UNKNOWN;
37c644f2
AO
2222 }
2223
2224 return (struct bfd_hash_entry *) ret;
2225}
2226
2227/* Create an sh ELF linker hash table. */
2228
2229static struct bfd_link_hash_table *
09fd220b 2230sh_elf_link_hash_table_create (bfd *abfd)
37c644f2
AO
2231{
2232 struct elf_sh_link_hash_table *ret;
dc810e39 2233 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
37c644f2 2234
e2d34d7d 2235 ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
37c644f2
AO
2236 if (ret == (struct elf_sh_link_hash_table *) NULL)
2237 return NULL;
2238
66eb6687
AM
2239 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2240 sh_elf_link_hash_newfunc,
2241 sizeof (struct elf_sh_link_hash_entry)))
37c644f2 2242 {
e2d34d7d 2243 free (ret);
37c644f2
AO
2244 return NULL;
2245 }
2246
067653c5
AM
2247 ret->sgot = NULL;
2248 ret->sgotplt = NULL;
2249 ret->srelgot = NULL;
2250 ret->splt = NULL;
2251 ret->srelplt = NULL;
2252 ret->sdynbss = NULL;
2253 ret->srelbss = NULL;
55e6e397 2254 ret->srelplt2 = NULL;
067653c5 2255 ret->sym_sec.abfd = NULL;
3376eaf5 2256 ret->tls_ldm_got.refcount = 0;
55e6e397
RS
2257 ret->plt_info = NULL;
2258 ret->vxworks_p = vxworks_object_p (abfd);
067653c5 2259
37c644f2
AO
2260 return &ret->root.root;
2261}
2262
067653c5
AM
2263/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2264 shortcuts to them in our hash table. */
2265
b34976b6 2266static bfd_boolean
09fd220b 2267create_got_section (bfd *dynobj, struct bfd_link_info *info)
067653c5
AM
2268{
2269 struct elf_sh_link_hash_table *htab;
2270
2271 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 2272 return FALSE;
067653c5
AM
2273
2274 htab = sh_elf_hash_table (info);
2275 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2276 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
2277 if (! htab->sgot || ! htab->sgotplt)
2278 abort ();
2279
3496cb2a
L
2280 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
2281 (SEC_ALLOC | SEC_LOAD
2282 | SEC_HAS_CONTENTS
2283 | SEC_IN_MEMORY
2284 | SEC_LINKER_CREATED
2285 | SEC_READONLY));
067653c5 2286 if (htab->srelgot == NULL
067653c5 2287 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
b34976b6
AM
2288 return FALSE;
2289 return TRUE;
067653c5
AM
2290}
2291
37c644f2
AO
2292/* Create dynamic sections when linking against a dynamic object. */
2293
b34976b6 2294static bfd_boolean
09fd220b 2295sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
37c644f2 2296{
067653c5 2297 struct elf_sh_link_hash_table *htab;
37c644f2
AO
2298 flagword flags, pltflags;
2299 register asection *s;
9c5bfbb7 2300 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
37c644f2
AO
2301 int ptralign = 0;
2302
2303 switch (bed->s->arch_size)
2304 {
2305 case 32:
2306 ptralign = 2;
2307 break;
2308
2309 case 64:
2310 ptralign = 3;
2311 break;
2312
2313 default:
2314 bfd_set_error (bfd_error_bad_value);
b34976b6 2315 return FALSE;
37c644f2
AO
2316 }
2317
067653c5 2318 htab = sh_elf_hash_table (info);
35cad4c4
KK
2319 if (htab->root.dynamic_sections_created)
2320 return TRUE;
067653c5 2321
37c644f2
AO
2322 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2323 .rel[a].bss sections. */
2324
2325 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2326 | SEC_LINKER_CREATED);
2327
2328 pltflags = flags;
2329 pltflags |= SEC_CODE;
2330 if (bed->plt_not_loaded)
2331 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2332 if (bed->plt_readonly)
2333 pltflags |= SEC_READONLY;
2334
3496cb2a 2335 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
35cad4c4
KK
2336 htab->splt = s;
2337 if (s == NULL
35cad4c4
KK
2338 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2339 return FALSE;
37c644f2
AO
2340
2341 if (bed->want_plt_sym)
2342 {
2343 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2344 .plt section. */
14a793b2
AM
2345 struct elf_link_hash_entry *h;
2346 struct bfd_link_hash_entry *bh = NULL;
2347
37c644f2
AO
2348 if (! (_bfd_generic_link_add_one_symbol
2349 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
b34976b6 2350 (bfd_vma) 0, (const char *) NULL, FALSE,
14a793b2 2351 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 2352 return FALSE;
14a793b2
AM
2353
2354 h = (struct elf_link_hash_entry *) bh;
f5385ebf 2355 h->def_regular = 1;
37c644f2 2356 h->type = STT_OBJECT;
7325306f 2357 htab->root.hplt = h;
37c644f2
AO
2358
2359 if (info->shared
c152c796 2360 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2361 return FALSE;
37c644f2
AO
2362 }
2363
3496cb2a
L
2364 s = bfd_make_section_with_flags (abfd,
2365 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt",
2366 flags | SEC_READONLY);
35cad4c4
KK
2367 htab->srelplt = s;
2368 if (s == NULL
35cad4c4
KK
2369 || ! bfd_set_section_alignment (abfd, s, ptralign))
2370 return FALSE;
37c644f2 2371
2293c499
AM
2372 if (htab->sgot == NULL
2373 && !create_got_section (abfd, info))
b34976b6 2374 return FALSE;
37c644f2
AO
2375
2376 {
2377 const char *secname;
2378 char *relname;
2379 flagword secflags;
2380 asection *sec;
2381
2382 for (sec = abfd->sections; sec; sec = sec->next)
2383 {
2384 secflags = bfd_get_section_flags (abfd, sec);
2385 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
2386 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
2387 continue;
2388 secname = bfd_get_section_name (abfd, sec);
dc810e39 2389 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
37c644f2
AO
2390 strcpy (relname, ".rela");
2391 strcat (relname, secname);
03151a5a
KK
2392 if (bfd_get_section_by_name (abfd, secname))
2393 continue;
3496cb2a
L
2394 s = bfd_make_section_with_flags (abfd, relname,
2395 flags | SEC_READONLY);
37c644f2 2396 if (s == NULL
37c644f2 2397 || ! bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 2398 return FALSE;
37c644f2
AO
2399 }
2400 }
2401
2402 if (bed->want_dynbss)
2403 {
2404 /* The .dynbss section is a place to put symbols which are defined
2405 by dynamic objects, are referenced by regular objects, and are
2406 not functions. We must allocate space for them in the process
2407 image and use a R_*_COPY reloc to tell the dynamic linker to
2408 initialize them at run time. The linker script puts the .dynbss
2409 section into the .bss section of the final image. */
3496cb2a
L
2410 s = bfd_make_section_with_flags (abfd, ".dynbss",
2411 SEC_ALLOC | SEC_LINKER_CREATED);
35cad4c4 2412 htab->sdynbss = s;
3496cb2a 2413 if (s == NULL)
35cad4c4 2414 return FALSE;
37c644f2
AO
2415
2416 /* The .rel[a].bss section holds copy relocs. This section is not
2417 normally needed. We need to create it here, though, so that the
2418 linker will map it to an output section. We can't just create it
2419 only if we need it, because we will not know whether we need it
2420 until we have seen all the input files, and the first time the
2421 main linker code calls BFD after examining all the input files
2422 (size_dynamic_sections) the input sections have already been
2423 mapped to the output sections. If the section turns out not to
2424 be needed, we can discard it later. We will never need this
2425 section when generating a shared object, since they do not use
2426 copy relocs. */
35cad4c4 2427 if (! info->shared)
37c644f2 2428 {
3496cb2a
L
2429 s = bfd_make_section_with_flags (abfd,
2430 (bed->default_use_rela_p
2431 ? ".rela.bss" : ".rel.bss"),
2432 flags | SEC_READONLY);
067653c5 2433 htab->srelbss = s;
37c644f2 2434 if (s == NULL
37c644f2 2435 || ! bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 2436 return FALSE;
37c644f2
AO
2437 }
2438 }
2439
55e6e397
RS
2440 if (htab->vxworks_p)
2441 {
2442 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2443 return FALSE;
2444 }
2445
b34976b6 2446 return TRUE;
37c644f2
AO
2447}
2448\f
37c644f2
AO
2449/* Adjust a symbol defined by a dynamic object and referenced by a
2450 regular object. The current definition is in some section of the
2451 dynamic object, but we're not including those sections. We have to
2452 change the definition to something the rest of the link can
2453 understand. */
2454
b34976b6 2455static bfd_boolean
09fd220b
KK
2456sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2457 struct elf_link_hash_entry *h)
37c644f2 2458{
067653c5
AM
2459 struct elf_sh_link_hash_table *htab;
2460 struct elf_sh_link_hash_entry *eh;
2461 struct elf_sh_dyn_relocs *p;
37c644f2
AO
2462 asection *s;
2463 unsigned int power_of_two;
2464
2293c499 2465 htab = sh_elf_hash_table (info);
37c644f2
AO
2466
2467 /* Make sure we know what is going on here. */
2293c499 2468 BFD_ASSERT (htab->root.dynobj != NULL
f5385ebf 2469 && (h->needs_plt
f6e332e6 2470 || h->u.weakdef != NULL
f5385ebf
AM
2471 || (h->def_dynamic
2472 && h->ref_regular
2473 && !h->def_regular)));
37c644f2
AO
2474
2475 /* If this is a function, put it in the procedure linkage table. We
2476 will fill in the contents of the procedure linkage table later,
2477 when we know the address of the .got section. */
2478 if (h->type == STT_FUNC
f5385ebf 2479 || h->needs_plt)
37c644f2 2480 {
067653c5 2481 if (h->plt.refcount <= 0
3d85aebe
KK
2482 || SYMBOL_CALLS_LOCAL (info, h)
2483 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2484 && h->root.type == bfd_link_hash_undefweak))
37c644f2
AO
2485 {
2486 /* This case can occur if we saw a PLT reloc in an input
2487 file, but the symbol was never referred to by a dynamic
2488 object. In such a case, we don't actually need to build
2489 a procedure linkage table, and we can just do a REL32
2490 reloc instead. */
067653c5 2491 h->plt.offset = (bfd_vma) -1;
f5385ebf 2492 h->needs_plt = 0;
37c644f2
AO
2493 }
2494
b34976b6 2495 return TRUE;
37c644f2 2496 }
067653c5
AM
2497 else
2498 h->plt.offset = (bfd_vma) -1;
37c644f2
AO
2499
2500 /* If this is a weak symbol, and there is a real definition, the
2501 processor independent code will have arranged for us to see the
2502 real definition first, and we can just use the same value. */
f6e332e6 2503 if (h->u.weakdef != NULL)
37c644f2 2504 {
f6e332e6
AM
2505 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2506 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2507 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2508 h->root.u.def.value = h->u.weakdef->root.u.def.value;
04e534c3 2509 if (info->nocopyreloc)
f6e332e6 2510 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 2511 return TRUE;
37c644f2
AO
2512 }
2513
2514 /* This is a reference to a symbol defined by a dynamic object which
2515 is not a function. */
2516
2517 /* If we are creating a shared library, we must presume that the
2518 only references to the symbol are via the global offset table.
2519 For such cases we need not do anything here; the relocations will
2520 be handled correctly by relocate_section. */
2521 if (info->shared)
b34976b6 2522 return TRUE;
37c644f2
AO
2523
2524 /* If there are no references to this symbol that do not use the
2525 GOT, we don't need to generate a copy reloc. */
f5385ebf 2526 if (!h->non_got_ref)
b34976b6 2527 return TRUE;
37c644f2 2528
067653c5
AM
2529 /* If -z nocopyreloc was given, we won't generate them either. */
2530 if (info->nocopyreloc)
2531 {
f5385ebf 2532 h->non_got_ref = 0;
b34976b6 2533 return TRUE;
067653c5
AM
2534 }
2535
2536 eh = (struct elf_sh_link_hash_entry *) h;
2537 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2538 {
2539 s = p->sec->output_section;
2540 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2541 break;
2542 }
2543
2544 /* If we didn't find any dynamic relocs in sections which needs the
2545 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2546 the copy reloc. */
2547 if (p == NULL)
2548 {
f5385ebf 2549 h->non_got_ref = 0;
b34976b6 2550 return TRUE;
067653c5
AM
2551 }
2552
909272ee
AM
2553 if (h->size == 0)
2554 {
2555 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2556 h->root.root.string);
2557 return TRUE;
2558 }
2559
37c644f2
AO
2560 /* We must allocate the symbol in our .dynbss section, which will
2561 become part of the .bss section of the executable. There will be
2562 an entry for this symbol in the .dynsym section. The dynamic
2563 object will contain position independent code, so all references
2564 from the dynamic object to this symbol will go through the global
2565 offset table. The dynamic linker will use the .dynsym entry to
2566 determine the address it must put in the global offset table, so
2567 both the dynamic object and the regular object will refer to the
2568 same memory location for the variable. */
2569
067653c5 2570 s = htab->sdynbss;
37c644f2
AO
2571 BFD_ASSERT (s != NULL);
2572
2573 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2574 copy the initial value out of the dynamic object and into the
2575 runtime process image. We need to remember the offset into the
2576 .rela.bss section we are going to use. */
2577 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2578 {
2579 asection *srel;
2580
067653c5 2581 srel = htab->srelbss;
37c644f2 2582 BFD_ASSERT (srel != NULL);
eea6121a 2583 srel->size += sizeof (Elf32_External_Rela);
f5385ebf 2584 h->needs_copy = 1;
37c644f2
AO
2585 }
2586
2587 /* We need to figure out the alignment required for this symbol. I
2588 have no idea how ELF linkers handle this. */
2589 power_of_two = bfd_log2 (h->size);
2590 if (power_of_two > 3)
2591 power_of_two = 3;
2592
2593 /* Apply the required alignment. */
eea6121a 2594 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2293c499 2595 if (power_of_two > bfd_get_section_alignment (htab->root.dynobj, s))
37c644f2 2596 {
2293c499 2597 if (! bfd_set_section_alignment (htab->root.dynobj, s, power_of_two))
b34976b6 2598 return FALSE;
37c644f2
AO
2599 }
2600
2601 /* Define the symbol as being at this point in the section. */
2602 h->root.u.def.section = s;
eea6121a 2603 h->root.u.def.value = s->size;
37c644f2
AO
2604
2605 /* Increment the section size to make room for the symbol. */
eea6121a 2606 s->size += h->size;
37c644f2 2607
b34976b6 2608 return TRUE;
37c644f2
AO
2609}
2610
067653c5
AM
2611/* Allocate space in .plt, .got and associated reloc sections for
2612 dynamic relocs. */
2613
b34976b6 2614static bfd_boolean
09fd220b 2615allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
067653c5
AM
2616{
2617 struct bfd_link_info *info;
2618 struct elf_sh_link_hash_table *htab;
2619 struct elf_sh_link_hash_entry *eh;
2620 struct elf_sh_dyn_relocs *p;
2621
2622 if (h->root.type == bfd_link_hash_indirect)
b34976b6 2623 return TRUE;
067653c5
AM
2624
2625 if (h->root.type == bfd_link_hash_warning)
2626 /* When warning symbols are created, they **replace** the "real"
2627 entry in the hash table, thus we never get to see the real
2628 symbol in a hash traversal. So look at it now. */
2629 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2630
2631 info = (struct bfd_link_info *) inf;
2632 htab = sh_elf_hash_table (info);
2633
4989d792
SC
2634 eh = (struct elf_sh_link_hash_entry *) h;
2635 if ((h->got.refcount > 0
f5385ebf 2636 || h->forced_local)
4989d792
SC
2637 && eh->gotplt_refcount > 0)
2638 {
2639 /* The symbol has been forced local, or we have some direct got refs,
26e41594 2640 so treat all the gotplt refs as got refs. */
4989d792
SC
2641 h->got.refcount += eh->gotplt_refcount;
2642 if (h->plt.refcount >= eh->gotplt_refcount)
2643 h->plt.refcount -= eh->gotplt_refcount;
2644 }
2645
067653c5 2646 if (htab->root.dynamic_sections_created
04e534c3
KK
2647 && h->plt.refcount > 0
2648 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2649 || h->root.type != bfd_link_hash_undefweak))
067653c5
AM
2650 {
2651 /* Make sure this symbol is output as a dynamic symbol.
2652 Undefined weak syms won't yet be marked as dynamic. */
2653 if (h->dynindx == -1
f5385ebf 2654 && !h->forced_local)
067653c5 2655 {
c152c796 2656 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2657 return FALSE;
067653c5
AM
2658 }
2659
267fb3c1
KK
2660 if (info->shared
2661 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
067653c5
AM
2662 {
2663 asection *s = htab->splt;
2664
2665 /* If this is the first .plt entry, make room for the special
2666 first entry. */
eea6121a 2667 if (s->size == 0)
55e6e397 2668 s->size += htab->plt_info->plt0_entry_size;
067653c5 2669
eea6121a 2670 h->plt.offset = s->size;
067653c5
AM
2671
2672 /* If this symbol is not defined in a regular file, and we are
2673 not generating a shared library, then set the symbol to this
2674 location in the .plt. This is required to make function
2675 pointers compare as equal between the normal executable and
2676 the shared library. */
2677 if (! info->shared
f5385ebf 2678 && !h->def_regular)
067653c5
AM
2679 {
2680 h->root.u.def.section = s;
2681 h->root.u.def.value = h->plt.offset;
2682 }
2683
2684 /* Make room for this entry. */
55e6e397 2685 s->size += htab->plt_info->symbol_entry_size;
067653c5
AM
2686
2687 /* We also need to make an entry in the .got.plt section, which
2688 will be placed in the .got section by the linker script. */
eea6121a 2689 htab->sgotplt->size += 4;
067653c5
AM
2690
2691 /* We also need to make an entry in the .rel.plt section. */
eea6121a 2692 htab->srelplt->size += sizeof (Elf32_External_Rela);
55e6e397
RS
2693
2694 if (htab->vxworks_p && !info->shared)
2695 {
2696 /* VxWorks executables have a second set of relocations
2697 for each PLT entry. They go in a separate relocation
2698 section, which is processed by the kernel loader. */
2699
2700 /* There is a relocation for the initial PLT entry:
2701 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2702 if (h->plt.offset == htab->plt_info->plt0_entry_size)
2703 htab->srelplt2->size += sizeof (Elf32_External_Rela);
2704
2705 /* There are two extra relocations for each subsequent
2706 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2707 and an R_SH_DIR32 relocation for the PLT entry. */
2708 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
2709 }
067653c5
AM
2710 }
2711 else
2712 {
2713 h->plt.offset = (bfd_vma) -1;
f5385ebf 2714 h->needs_plt = 0;
067653c5
AM
2715 }
2716 }
2717 else
2718 {
2719 h->plt.offset = (bfd_vma) -1;
f5385ebf 2720 h->needs_plt = 0;
067653c5
AM
2721 }
2722
2723 if (h->got.refcount > 0)
2724 {
2725 asection *s;
b34976b6 2726 bfd_boolean dyn;
3376eaf5 2727 int tls_type = sh_elf_hash_entry (h)->tls_type;
067653c5
AM
2728
2729 /* Make sure this symbol is output as a dynamic symbol.
2730 Undefined weak syms won't yet be marked as dynamic. */
2731 if (h->dynindx == -1
f5385ebf 2732 && !h->forced_local)
067653c5 2733 {
c152c796 2734 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2735 return FALSE;
067653c5
AM
2736 }
2737
2738 s = htab->sgot;
eea6121a
AM
2739 h->got.offset = s->size;
2740 s->size += 4;
3376eaf5
KK
2741 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2742 if (tls_type == GOT_TLS_GD)
eea6121a 2743 s->size += 4;
067653c5 2744 dyn = htab->root.dynamic_sections_created;
267fb3c1 2745 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3376eaf5
KK
2746 R_SH_TLS_GD needs one if local symbol and two if global. */
2747 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
267fb3c1 2748 || (tls_type == GOT_TLS_IE && dyn))
eea6121a 2749 htab->srelgot->size += sizeof (Elf32_External_Rela);
3376eaf5 2750 else if (tls_type == GOT_TLS_GD)
eea6121a 2751 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
04e534c3
KK
2752 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2753 || h->root.type != bfd_link_hash_undefweak)
2754 && (info->shared
2755 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
eea6121a 2756 htab->srelgot->size += sizeof (Elf32_External_Rela);
067653c5
AM
2757 }
2758 else
2759 h->got.offset = (bfd_vma) -1;
2760
396a6083
SC
2761#ifdef INCLUDE_SHMEDIA
2762 if (eh->datalabel_got.refcount > 0)
2763 {
2764 asection *s;
b34976b6 2765 bfd_boolean dyn;
396a6083
SC
2766
2767 /* Make sure this symbol is output as a dynamic symbol.
2768 Undefined weak syms won't yet be marked as dynamic. */
2769 if (h->dynindx == -1
f5385ebf 2770 && !h->forced_local)
396a6083 2771 {
c152c796 2772 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2773 return FALSE;
396a6083
SC
2774 }
2775
2776 s = htab->sgot;
eea6121a
AM
2777 eh->datalabel_got.offset = s->size;
2778 s->size += 4;
396a6083 2779 dyn = htab->root.dynamic_sections_created;
267fb3c1 2780 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
eea6121a 2781 htab->srelgot->size += sizeof (Elf32_External_Rela);
396a6083
SC
2782 }
2783 else
2784 eh->datalabel_got.offset = (bfd_vma) -1;
2785#endif
2786
067653c5 2787 if (eh->dyn_relocs == NULL)
b34976b6 2788 return TRUE;
067653c5
AM
2789
2790 /* In the shared -Bsymbolic case, discard space allocated for
2791 dynamic pc-relative relocs against symbols which turn out to be
2792 defined in regular objects. For the normal shared case, discard
2793 space for pc-relative relocs that have become local due to symbol
2794 visibility changes. */
2795
2796 if (info->shared)
2797 {
3d85aebe 2798 if (SYMBOL_CALLS_LOCAL (info, h))
067653c5
AM
2799 {
2800 struct elf_sh_dyn_relocs **pp;
2801
2802 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2803 {
2804 p->count -= p->pc_count;
2805 p->pc_count = 0;
2806 if (p->count == 0)
2807 *pp = p->next;
2808 else
2809 pp = &p->next;
2810 }
2811 }
04e534c3
KK
2812
2813 /* Also discard relocs on undefined weak syms with non-default
2814 visibility. */
22d606e9 2815 if (eh->dyn_relocs != NULL
04e534c3 2816 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
2817 {
2818 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2819 eh->dyn_relocs = NULL;
2820
2821 /* Make sure undefined weak symbols are output as a dynamic
2822 symbol in PIEs. */
2823 else if (h->dynindx == -1
2824 && !h->forced_local)
2825 {
2826 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2827 return FALSE;
2828 }
2829 }
067653c5
AM
2830 }
2831 else
2832 {
2833 /* For the non-shared case, discard space for relocs against
2834 symbols which turn out to need copy relocs or are not
2835 dynamic. */
2836
f5385ebf
AM
2837 if (!h->non_got_ref
2838 && ((h->def_dynamic
2839 && !h->def_regular)
067653c5
AM
2840 || (htab->root.dynamic_sections_created
2841 && (h->root.type == bfd_link_hash_undefweak
2842 || h->root.type == bfd_link_hash_undefined))))
2843 {
2844 /* Make sure this symbol is output as a dynamic symbol.
2845 Undefined weak syms won't yet be marked as dynamic. */
2846 if (h->dynindx == -1
f5385ebf 2847 && !h->forced_local)
067653c5 2848 {
c152c796 2849 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2850 return FALSE;
067653c5
AM
2851 }
2852
2853 /* If that succeeded, we know we'll be keeping all the
2854 relocs. */
2855 if (h->dynindx != -1)
2856 goto keep;
2857 }
2858
2859 eh->dyn_relocs = NULL;
2860
2861 keep: ;
2862 }
2863
2864 /* Finally, allocate space. */
2865 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2866 {
2867 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 2868 sreloc->size += p->count * sizeof (Elf32_External_Rela);
067653c5
AM
2869 }
2870
b34976b6 2871 return TRUE;
067653c5
AM
2872}
2873
2874/* Find any dynamic relocs that apply to read-only sections. */
2875
b34976b6 2876static bfd_boolean
09fd220b 2877readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
067653c5
AM
2878{
2879 struct elf_sh_link_hash_entry *eh;
2880 struct elf_sh_dyn_relocs *p;
2881
2882 if (h->root.type == bfd_link_hash_warning)
2883 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2884
2885 eh = (struct elf_sh_link_hash_entry *) h;
2886 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2887 {
2888 asection *s = p->sec->output_section;
2889
2890 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2891 {
2892 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2893
2894 info->flags |= DF_TEXTREL;
2895
2896 /* Not an error, just cut short the traversal. */
b34976b6 2897 return FALSE;
067653c5
AM
2898 }
2899 }
b34976b6 2900 return TRUE;
067653c5
AM
2901}
2902
55e6e397
RS
2903/* This function is called after all the input files have been read,
2904 and the input sections have been assigned to output sections.
2905 It's a convenient place to determine the PLT style. */
2906
2907static bfd_boolean
2908sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
2909{
2910 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd, info->shared);
2911 return TRUE;
2912}
2913
37c644f2
AO
2914/* Set the sizes of the dynamic sections. */
2915
b34976b6 2916static bfd_boolean
09fd220b
KK
2917sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2918 struct bfd_link_info *info)
37c644f2 2919{
067653c5 2920 struct elf_sh_link_hash_table *htab;
37c644f2
AO
2921 bfd *dynobj;
2922 asection *s;
b34976b6 2923 bfd_boolean relocs;
067653c5 2924 bfd *ibfd;
37c644f2 2925
067653c5
AM
2926 htab = sh_elf_hash_table (info);
2927 dynobj = htab->root.dynobj;
37c644f2
AO
2928 BFD_ASSERT (dynobj != NULL);
2929
067653c5 2930 if (htab->root.dynamic_sections_created)
37c644f2
AO
2931 {
2932 /* Set the contents of the .interp section to the interpreter. */
2558b9a9 2933 if (info->executable)
37c644f2
AO
2934 {
2935 s = bfd_get_section_by_name (dynobj, ".interp");
2936 BFD_ASSERT (s != NULL);
eea6121a 2937 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
37c644f2
AO
2938 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2939 }
2940 }
37c644f2 2941
067653c5
AM
2942 /* Set up .got offsets for local syms, and space for local dynamic
2943 relocs. */
2944 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
37c644f2 2945 {
067653c5
AM
2946 bfd_signed_vma *local_got;
2947 bfd_signed_vma *end_local_got;
3376eaf5 2948 char *local_tls_type;
067653c5
AM
2949 bfd_size_type locsymcount;
2950 Elf_Internal_Shdr *symtab_hdr;
2951 asection *srel;
37c644f2 2952
067653c5 2953 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
37c644f2
AO
2954 continue;
2955
067653c5 2956 for (s = ibfd->sections; s != NULL; s = s->next)
37c644f2 2957 {
067653c5
AM
2958 struct elf_sh_dyn_relocs *p;
2959
2960 for (p = ((struct elf_sh_dyn_relocs *)
2961 elf_section_data (s)->local_dynrel);
2962 p != NULL;
2963 p = p->next)
37c644f2 2964 {
067653c5
AM
2965 if (! bfd_is_abs_section (p->sec)
2966 && bfd_is_abs_section (p->sec->output_section))
2967 {
2968 /* Input section has been discarded, either because
2969 it is a copy of a linkonce section or due to
2970 linker script /DISCARD/, so we'll be discarding
2971 the relocs too. */
2972 }
2973 else if (p->count != 0)
2974 {
2975 srel = elf_section_data (p->sec)->sreloc;
eea6121a 2976 srel->size += p->count * sizeof (Elf32_External_Rela);
067653c5
AM
2977 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2978 info->flags |= DF_TEXTREL;
2979 }
37c644f2
AO
2980 }
2981 }
067653c5
AM
2982
2983 local_got = elf_local_got_refcounts (ibfd);
2984 if (!local_got)
2985 continue;
2986
2987 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2988 locsymcount = symtab_hdr->sh_info;
2989#ifdef INCLUDE_SHMEDIA
2990 /* Count datalabel local GOT. */
2991 locsymcount *= 2;
2992#endif
2993 end_local_got = local_got + locsymcount;
3376eaf5 2994 local_tls_type = sh_elf_local_got_tls_type (ibfd);
067653c5
AM
2995 s = htab->sgot;
2996 srel = htab->srelgot;
2997 for (; local_got < end_local_got; ++local_got)
37c644f2 2998 {
067653c5 2999 if (*local_got > 0)
37c644f2 3000 {
eea6121a
AM
3001 *local_got = s->size;
3002 s->size += 4;
3376eaf5 3003 if (*local_tls_type == GOT_TLS_GD)
eea6121a 3004 s->size += 4;
067653c5 3005 if (info->shared)
eea6121a 3006 srel->size += sizeof (Elf32_External_Rela);
37c644f2
AO
3007 }
3008 else
067653c5 3009 *local_got = (bfd_vma) -1;
3376eaf5 3010 ++local_tls_type;
37c644f2 3011 }
067653c5
AM
3012 }
3013
3376eaf5
KK
3014 if (htab->tls_ldm_got.refcount > 0)
3015 {
3016 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3017 relocs. */
eea6121a
AM
3018 htab->tls_ldm_got.offset = htab->sgot->size;
3019 htab->sgot->size += 8;
3020 htab->srelgot->size += sizeof (Elf32_External_Rela);
3376eaf5
KK
3021 }
3022 else
3023 htab->tls_ldm_got.offset = -1;
3024
067653c5
AM
3025 /* Allocate global sym .plt and .got entries, and space for global
3026 sym dynamic relocs. */
09fd220b 3027 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
067653c5
AM
3028
3029 /* We now have determined the sizes of the various dynamic sections.
3030 Allocate memory for them. */
b34976b6 3031 relocs = FALSE;
067653c5
AM
3032 for (s = dynobj->sections; s != NULL; s = s->next)
3033 {
3034 if ((s->flags & SEC_LINKER_CREATED) == 0)
3035 continue;
3036
3037 if (s == htab->splt
3038 || s == htab->sgot
c456f082
AM
3039 || s == htab->sgotplt
3040 || s == htab->sdynbss)
067653c5
AM
3041 {
3042 /* Strip this section if we don't need it; see the
3043 comment below. */
3044 }
3045 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3046 {
55e6e397 3047 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
b34976b6 3048 relocs = TRUE;
067653c5
AM
3049
3050 /* We use the reloc_count field as a counter if we need
3051 to copy relocs into the output file. */
3052 s->reloc_count = 0;
3053 }
3054 else
37c644f2
AO
3055 {
3056 /* It's not one of our sections, so don't allocate space. */
3057 continue;
3058 }
3059
eea6121a 3060 if (s->size == 0)
37c644f2 3061 {
067653c5
AM
3062 /* If we don't need this section, strip it from the
3063 output file. This is mostly to handle .rela.bss and
3064 .rela.plt. We must create both sections in
3065 create_dynamic_sections, because they must be created
3066 before the linker maps input sections to output
3067 sections. The linker does that before
3068 adjust_dynamic_symbol is called, and it is that
3069 function which decides whether anything needs to go
3070 into these sections. */
3071
8423293d 3072 s->flags |= SEC_EXCLUDE;
37c644f2
AO
3073 continue;
3074 }
3075
c456f082
AM
3076 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3077 continue;
3078
067653c5
AM
3079 /* Allocate memory for the section contents. We use bfd_zalloc
3080 here in case unused entries are not reclaimed before the
3081 section's contents are written out. This should not happen,
3082 but this way if it does, we get a R_SH_NONE reloc instead
3083 of garbage. */
eea6121a 3084 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
067653c5 3085 if (s->contents == NULL)
b34976b6 3086 return FALSE;
37c644f2
AO
3087 }
3088
067653c5 3089 if (htab->root.dynamic_sections_created)
37c644f2
AO
3090 {
3091 /* Add some entries to the .dynamic section. We fill in the
3092 values later, in sh_elf_finish_dynamic_sections, but we
3093 must add the entries now so that we get the correct size for
3094 the .dynamic section. The DT_DEBUG entry is filled in by the
3095 dynamic linker and used by the debugger. */
dc810e39 3096#define add_dynamic_entry(TAG, VAL) \
5a580b3a 3097 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 3098
2558b9a9 3099 if (info->executable)
37c644f2 3100 {
067653c5 3101 if (! add_dynamic_entry (DT_DEBUG, 0))
b34976b6 3102 return FALSE;
37c644f2
AO
3103 }
3104
eea6121a 3105 if (htab->splt->size != 0)
37c644f2 3106 {
067653c5
AM
3107 if (! add_dynamic_entry (DT_PLTGOT, 0)
3108 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
3109 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
3110 || ! add_dynamic_entry (DT_JMPREL, 0))
b34976b6 3111 return FALSE;
37c644f2
AO
3112 }
3113
3114 if (relocs)
3115 {
067653c5
AM
3116 if (! add_dynamic_entry (DT_RELA, 0)
3117 || ! add_dynamic_entry (DT_RELASZ, 0)
3118 || ! add_dynamic_entry (DT_RELAENT,
3119 sizeof (Elf32_External_Rela)))
b34976b6 3120 return FALSE;
37c644f2 3121
067653c5
AM
3122 /* If any dynamic relocs apply to a read-only section,
3123 then we need a DT_TEXTREL entry. */
3124 if ((info->flags & DF_TEXTREL) == 0)
09fd220b 3125 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
067653c5
AM
3126
3127 if ((info->flags & DF_TEXTREL) != 0)
3128 {
3129 if (! add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 3130 return FALSE;
067653c5 3131 }
37c644f2
AO
3132 }
3133 }
dc810e39 3134#undef add_dynamic_entry
37c644f2 3135
b34976b6 3136 return TRUE;
37c644f2 3137}
37c644f2
AO
3138\f
3139/* Relocate an SH ELF section. */
3140
b34976b6 3141static bfd_boolean
09fd220b
KK
3142sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3143 bfd *input_bfd, asection *input_section,
3144 bfd_byte *contents, Elf_Internal_Rela *relocs,
3145 Elf_Internal_Sym *local_syms,
3146 asection **local_sections)
37c644f2 3147{
067653c5 3148 struct elf_sh_link_hash_table *htab;
37c644f2
AO
3149 Elf_Internal_Shdr *symtab_hdr;
3150 struct elf_link_hash_entry **sym_hashes;
3151 Elf_Internal_Rela *rel, *relend;
3152 bfd *dynobj;
3153 bfd_vma *local_got_offsets;
3154 asection *sgot;
fbca6ad9 3155 asection *sgotplt;
37c644f2
AO
3156 asection *splt;
3157 asection *sreloc;
3376eaf5 3158 asection *srelgot;
37c644f2 3159
067653c5 3160 htab = sh_elf_hash_table (info);
37c644f2
AO
3161 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3162 sym_hashes = elf_sym_hashes (input_bfd);
067653c5 3163 dynobj = htab->root.dynobj;
37c644f2
AO
3164 local_got_offsets = elf_local_got_offsets (input_bfd);
3165
067653c5
AM
3166 sgot = htab->sgot;
3167 sgotplt = htab->sgotplt;
3168 splt = htab->splt;
37c644f2 3169 sreloc = NULL;
3376eaf5 3170 srelgot = NULL;
37c644f2
AO
3171
3172 rel = relocs;
3173 relend = relocs + input_section->reloc_count;
3174 for (; rel < relend; rel++)
3175 {
3176 int r_type;
3177 reloc_howto_type *howto;
3178 unsigned long r_symndx;
3179 Elf_Internal_Sym *sym;
3180 asection *sec;
3181 struct elf_link_hash_entry *h;
3182 bfd_vma relocation;
435b1e90 3183 bfd_vma addend = (bfd_vma) 0;
37c644f2 3184 bfd_reloc_status_type r;
fbca6ad9 3185 int seen_stt_datalabel = 0;
3376eaf5
KK
3186 bfd_vma off;
3187 int tls_type;
37c644f2
AO
3188
3189 r_symndx = ELF32_R_SYM (rel->r_info);
3190
37c644f2
AO
3191 r_type = ELF32_R_TYPE (rel->r_info);
3192
3193 /* Many of the relocs are only used for relaxing, and are
067653c5 3194 handled entirely by the relaxation code. */
d38eb334
DD
3195 if (r_type >= (int) R_SH_GNU_VTINHERIT
3196 && r_type <= (int) R_SH_LABEL)
37c644f2 3197 continue;
c5aeb40f
AO
3198 if (r_type == (int) R_SH_NONE)
3199 continue;
37c644f2
AO
3200
3201 if (r_type < 0
3202 || r_type >= R_SH_max
3203 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
06bb75c1 3204 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
fbca6ad9
AO
3205 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3206 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3207 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3208 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3376eaf5
KK
3209 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3210 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
06bb75c1
AO
3211 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3212 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
37c644f2
AO
3213 {
3214 bfd_set_error (bfd_error_bad_value);
b34976b6 3215 return FALSE;
37c644f2
AO
3216 }
3217
55e6e397 3218 howto = get_howto_table (output_bfd) + r_type;
37c644f2 3219
146be91a 3220 /* For relocs that aren't partial_inplace, we get the addend from
067653c5 3221 the relocation. */
146be91a
HPN
3222 if (! howto->partial_inplace)
3223 addend = rel->r_addend;
3224
37c644f2
AO
3225 h = NULL;
3226 sym = NULL;
3227 sec = NULL;
3228 if (r_symndx < symtab_hdr->sh_info)
3229 {
3230 sym = local_syms + r_symndx;
3231 sec = local_sections[r_symndx];
3232 relocation = (sec->output_section->vma
3233 + sec->output_offset
3234 + sym->st_value);
fbca6ad9
AO
3235 /* A local symbol never has STO_SH5_ISA32, so we don't need
3236 datalabel processing here. Make sure this does not change
3237 without notice. */
3238 if ((sym->st_other & STO_SH5_ISA32) != 0)
3239 ((*info->callbacks->reloc_dangerous)
3240 (info,
3241 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3242 input_bfd, input_section, rel->r_offset));
1049f94e 3243 if (info->relocatable)
8a3975e6 3244 {
1049f94e 3245 /* This is a relocatable link. We don't have to change
8a3975e6
AO
3246 anything, unless the reloc is against a section symbol,
3247 in which case we have to adjust according to where the
3248 section symbol winds up in the output section. */
3249 sym = local_syms + r_symndx;
3250 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
146be91a
HPN
3251 {
3252 if (! howto->partial_inplace)
3253 {
3254 /* For relocations with the addend in the
3255 relocation, we need just to update the addend.
3256 All real relocs are of type partial_inplace; this
3257 code is mostly for completeness. */
3258 rel->r_addend += sec->output_offset + sym->st_value;
3259
3260 continue;
3261 }
3262
3263 /* Relocs of type partial_inplace need to pick up the
3264 contents in the contents and add the offset resulting
3265 from the changed location of the section symbol.
3266 Using _bfd_final_link_relocate (e.g. goto
3267 final_link_relocate) here would be wrong, because
3268 relocations marked pc_relative would get the current
3269 location subtracted, and we must only do that at the
3270 final link. */
3271 r = _bfd_relocate_contents (howto, input_bfd,
3272 sec->output_offset
3273 + sym->st_value,
3274 contents + rel->r_offset);
3275 goto relocation_done;
3276 }
8a3975e6
AO
3277
3278 continue;
3279 }
f8df10f4
JJ
3280 else if (! howto->partial_inplace)
3281 {
8517fae7 3282 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
f8df10f4
JJ
3283 addend = rel->r_addend;
3284 }
3285 else if ((sec->flags & SEC_MERGE)
3286 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3287 {
3288 asection *msec;
3289
3290 if (howto->rightshift || howto->src_mask != 0xffffffff)
3291 {
3292 (*_bfd_error_handler)
d003868e
AM
3293 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
3294 input_bfd, input_section,
f8df10f4 3295 (long) rel->r_offset, howto->name);
b34976b6 3296 return FALSE;
f8df10f4
JJ
3297 }
3298
067653c5
AM
3299 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
3300 msec = sec;
3301 addend =
c629eae0 3302 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
f8df10f4
JJ
3303 - relocation;
3304 addend += msec->output_section->vma + msec->output_offset;
3305 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
3306 addend = 0;
3307 }
37c644f2
AO
3308 }
3309 else
3310 {
560e09e9
NC
3311 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3312
8a3975e6
AO
3313 /* Section symbol are never (?) placed in the hash table, so
3314 we can just ignore hash relocations when creating a
1049f94e
AM
3315 relocatable object file. */
3316 if (info->relocatable)
8a3975e6
AO
3317 continue;
3318
37c644f2
AO
3319 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3320 while (h->root.type == bfd_link_hash_indirect
3321 || h->root.type == bfd_link_hash_warning)
fbca6ad9
AO
3322 {
3323#ifdef INCLUDE_SHMEDIA
3324 /* If the reference passes a symbol marked with
3325 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
3326 doesn't count. */
3327 seen_stt_datalabel |= h->type == STT_DATALABEL;
3328#endif
3329 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3330 }
37c644f2
AO
3331 if (h->root.type == bfd_link_hash_defined
3332 || h->root.type == bfd_link_hash_defweak)
3333 {
b34976b6 3334 bfd_boolean dyn;
067653c5
AM
3335
3336 dyn = htab->root.dynamic_sections_created;
37c644f2
AO
3337 sec = h->root.u.def.section;
3338 /* In these cases, we don't need the relocation value.
3339 We check specially because in some obscure cases
435b1e90 3340 sec->output_section will be NULL. */
37c644f2 3341 if (r_type == R_SH_GOTPC
fbca6ad9
AO
3342 || r_type == R_SH_GOTPC_LOW16
3343 || r_type == R_SH_GOTPC_MEDLOW16
3344 || r_type == R_SH_GOTPC_MEDHI16
3345 || r_type == R_SH_GOTPC_HI16
3346 || ((r_type == R_SH_PLT32
3347 || r_type == R_SH_PLT_LOW16
3348 || r_type == R_SH_PLT_MEDLOW16
3349 || r_type == R_SH_PLT_MEDHI16
3350 || r_type == R_SH_PLT_HI16)
37c644f2 3351 && h->plt.offset != (bfd_vma) -1)
fbca6ad9
AO
3352 || ((r_type == R_SH_GOT32
3353 || r_type == R_SH_GOT_LOW16
3354 || r_type == R_SH_GOT_MEDLOW16
3355 || r_type == R_SH_GOT_MEDHI16
3356 || r_type == R_SH_GOT_HI16)
267fb3c1 3357 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
37c644f2
AO
3358 && (! info->shared
3359 || (! info->symbolic && h->dynindx != -1)
f5385ebf 3360 || !h->def_regular))
37c644f2
AO
3361 /* The cases above are those in which relocation is
3362 overwritten in the switch block below. The cases
3363 below are those in which we must defer relocation
3364 to run-time, because we can't resolve absolute
3365 addresses when creating a shared library. */
3366 || (info->shared
3367 && ((! info->symbolic && h->dynindx != -1)
f5385ebf 3368 || !h->def_regular)
37c644f2 3369 && ((r_type == R_SH_DIR32
f5385ebf 3370 && !h->forced_local)
f50ad433
KK
3371 || (r_type == R_SH_REL32
3372 && !SYMBOL_CALLS_LOCAL (info, h)))
37c644f2
AO
3373 && ((input_section->flags & SEC_ALLOC) != 0
3374 /* DWARF will emit R_SH_DIR32 relocations in its
3375 sections against symbols defined externally
3376 in shared libraries. We can't do anything
3377 with them here. */
067653c5 3378 || ((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3379 && h->def_dynamic)))
1448fa32
KK
3380 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3381 sections because such sections are not SEC_ALLOC and
3382 thus ld.so will not process them. */
3383 || (sec->output_section == NULL
3384 && ((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3385 && h->def_dynamic))
3376eaf5
KK
3386 || (sec->output_section == NULL
3387 && (sh_elf_hash_entry (h)->tls_type == GOT_TLS_IE
3388 || sh_elf_hash_entry (h)->tls_type == GOT_TLS_GD)))
37c644f2
AO
3389 relocation = 0;
3390 else if (sec->output_section == NULL)
3391 {
3392 (*_bfd_error_handler)
843fe662
L
3393 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3394 input_bfd,
3395 input_section,
3396 (long) rel->r_offset,
3397 howto->name,
3398 h->root.root.string);
b34976b6 3399 return FALSE;
37c644f2
AO
3400 }
3401 else
fbca6ad9 3402 relocation = ((h->root.u.def.value
37c644f2 3403 + sec->output_section->vma
fbca6ad9
AO
3404 + sec->output_offset)
3405 /* A STO_SH5_ISA32 causes a "bitor 1" to the
3406 symbol value, unless we've seen
3407 STT_DATALABEL on the way to it. */
3408 | ((h->other & STO_SH5_ISA32) != 0
3409 && ! seen_stt_datalabel));
37c644f2
AO
3410 }
3411 else if (h->root.type == bfd_link_hash_undefweak)
3412 relocation = 0;
59c2e50f 3413 else if (info->unresolved_syms_in_objects == RM_IGNORE
067653c5 3414 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
37c644f2
AO
3415 relocation = 0;
3416 else
3417 {
560e09e9
NC
3418 if (! info->callbacks->undefined_symbol
3419 (info, h->root.root.string, input_bfd,
3420 input_section, rel->r_offset,
59c2e50f 3421 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
560e09e9 3422 || ELF_ST_VISIBILITY (h->other))))
b34976b6 3423 return FALSE;
37c644f2
AO
3424 relocation = 0;
3425 }
3426 }
3427
435b1e90 3428 switch ((int) r_type)
37c644f2
AO
3429 {
3430 final_link_relocate:
3431 /* COFF relocs don't use the addend. The addend is used for
435b1e90 3432 R_SH_DIR32 to be compatible with other compilers. */
37c644f2
AO
3433 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3434 contents, rel->r_offset,
3435 relocation, addend);
3436 break;
3437
3438 case R_SH_IND12W:
cd6ec716
DD
3439 goto final_link_relocate;
3440
37c644f2
AO
3441 case R_SH_DIR8WPN:
3442 case R_SH_DIR8WPZ:
3443 case R_SH_DIR8WPL:
cd6ec716
DD
3444 /* If the reloc is against the start of this section, then
3445 the assembler has already taken care of it and the reloc
3446 is here only to assist in relaxing. If the reloc is not
3447 against the start of this section, then it's against an
3448 external symbol and we must deal with it ourselves. */
3449 if (input_section->output_section->vma + input_section->output_offset
3450 != relocation)
3451 {
3452 int disp = (relocation
3453 - input_section->output_section->vma
3454 - input_section->output_offset
3455 - rel->r_offset);
3456 int mask = 0;
3457 switch (r_type)
3458 {
3459 case R_SH_DIR8WPN:
3460 case R_SH_DIR8WPZ: mask = 1; break;
3461 case R_SH_DIR8WPL: mask = 3; break;
3462 default: mask = 0; break;
3463 }
3464 if (disp & mask)
3465 {
3466 ((*_bfd_error_handler)
d003868e
AM
3467 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
3468 input_section->owner,
cd6ec716
DD
3469 (unsigned long) rel->r_offset));
3470 bfd_set_error (bfd_error_bad_value);
b34976b6 3471 return FALSE;
cd6ec716
DD
3472 }
3473 relocation -= 4;
3474 goto final_link_relocate;
3475 }
3476 r = bfd_reloc_ok;
3477 break;
37c644f2
AO
3478
3479 default:
fbca6ad9
AO
3480#ifdef INCLUDE_SHMEDIA
3481 if (shmedia_prepare_reloc (info, input_bfd, input_section,
3482 contents, rel, &relocation))
3483 goto final_link_relocate;
3484#endif
37c644f2 3485 bfd_set_error (bfd_error_bad_value);
b34976b6 3486 return FALSE;
37c644f2 3487
d38eb334
DD
3488 case R_SH_DIR16:
3489 case R_SH_DIR8:
3490 case R_SH_DIR8U:
3491 case R_SH_DIR8S:
3492 case R_SH_DIR4U:
3493 goto final_link_relocate;
3494
3495 case R_SH_DIR8UL:
3496 case R_SH_DIR4UL:
3497 if (relocation & 3)
3498 {
3499 ((*_bfd_error_handler)
d003868e
AM
3500 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3501 input_section->owner,
d38eb334 3502 (unsigned long) rel->r_offset, howto->name,
d003868e 3503 (unsigned long) relocation));
d38eb334
DD
3504 bfd_set_error (bfd_error_bad_value);
3505 return FALSE;
3506 }
3507 goto final_link_relocate;
3508
3509 case R_SH_DIR8UW:
3510 case R_SH_DIR8SW:
3511 case R_SH_DIR4UW:
3512 if (relocation & 1)
3513 {
3514 ((*_bfd_error_handler)
d003868e
AM
3515 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3516 input_section->owner,
d38eb334 3517 (unsigned long) rel->r_offset, howto->name,
d003868e 3518 (unsigned long) relocation));
d38eb334
DD
3519 bfd_set_error (bfd_error_bad_value);
3520 return FALSE;
3521 }
3522 goto final_link_relocate;
3523
3524 case R_SH_PSHA:
3525 if ((signed int)relocation < -32
3526 || (signed int)relocation > 32)
3527 {
3528 ((*_bfd_error_handler)
d003868e
AM
3529 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
3530 input_section->owner,
d38eb334 3531 (unsigned long) rel->r_offset,
d003868e 3532 (unsigned long) relocation));
d38eb334
DD
3533 bfd_set_error (bfd_error_bad_value);
3534 return FALSE;
3535 }
3536 goto final_link_relocate;
3537
3538 case R_SH_PSHL:
3539 if ((signed int)relocation < -16
3540 || (signed int)relocation > 16)
3541 {
3542 ((*_bfd_error_handler)
d003868e
AM
3543 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
3544 input_section->owner,
d38eb334 3545 (unsigned long) rel->r_offset,
d003868e 3546 (unsigned long) relocation));
d38eb334
DD
3547 bfd_set_error (bfd_error_bad_value);
3548 return FALSE;
3549 }
3550 goto final_link_relocate;
3551
37c644f2
AO
3552 case R_SH_DIR32:
3553 case R_SH_REL32:
46e993b9
KK
3554#ifdef INCLUDE_SHMEDIA
3555 case R_SH_IMM_LOW16_PCREL:
3556 case R_SH_IMM_MEDLOW16_PCREL:
3557 case R_SH_IMM_MEDHI16_PCREL:
3558 case R_SH_IMM_HI16_PCREL:
3559#endif
37c644f2 3560 if (info->shared
04e534c3
KK
3561 && (h == NULL
3562 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3563 || h->root.type != bfd_link_hash_undefweak)
ec338859 3564 && r_symndx != 0
37c644f2 3565 && (input_section->flags & SEC_ALLOC) != 0
46e993b9 3566 && (r_type == R_SH_DIR32
3d85aebe 3567 || !SYMBOL_CALLS_LOCAL (info, h)))
37c644f2
AO
3568 {
3569 Elf_Internal_Rela outrel;
947216bf 3570 bfd_byte *loc;
b34976b6 3571 bfd_boolean skip, relocate;
37c644f2
AO
3572
3573 /* When generating a shared object, these relocations
3574 are copied into the output file to be resolved at run
3575 time. */
3576
3577 if (sreloc == NULL)
3578 {
3579 const char *name;
3580
3581 name = (bfd_elf_string_from_elf_section
3582 (input_bfd,
3583 elf_elfheader (input_bfd)->e_shstrndx,
3584 elf_section_data (input_section)->rel_hdr.sh_name));
3585 if (name == NULL)
b34976b6 3586 return FALSE;
37c644f2
AO
3587
3588 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3589 && strcmp (bfd_get_section_name (input_bfd,
3590 input_section),
3591 name + 5) == 0);
3592
3593 sreloc = bfd_get_section_by_name (dynobj, name);
3594 BFD_ASSERT (sreloc != NULL);
3595 }
3596
b34976b6
AM
3597 skip = FALSE;
3598 relocate = FALSE;
37c644f2 3599
c629eae0
JJ
3600 outrel.r_offset =
3601 _bfd_elf_section_offset (output_bfd, info, input_section,
3602 rel->r_offset);
3603 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 3604 skip = TRUE;
0bb2d96a 3605 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 3606 skip = TRUE, relocate = TRUE;
37c644f2
AO
3607 outrel.r_offset += (input_section->output_section->vma
3608 + input_section->output_offset);
3609
3610 if (skip)
0bb2d96a 3611 memset (&outrel, 0, sizeof outrel);
37c644f2
AO
3612 else if (r_type == R_SH_REL32)
3613 {
3614 BFD_ASSERT (h != NULL && h->dynindx != -1);
37c644f2 3615 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
146be91a 3616 outrel.r_addend
55e6e397
RS
3617 = (howto->partial_inplace
3618 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3619 : addend);
37c644f2 3620 }
46e993b9
KK
3621#ifdef INCLUDE_SHMEDIA
3622 else if (r_type == R_SH_IMM_LOW16_PCREL
3623 || r_type == R_SH_IMM_MEDLOW16_PCREL
3624 || r_type == R_SH_IMM_MEDHI16_PCREL
3625 || r_type == R_SH_IMM_HI16_PCREL)
3626 {
3627 BFD_ASSERT (h != NULL && h->dynindx != -1);
3628 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3629 outrel.r_addend = addend;
3630 }
3631#endif
37c644f2
AO
3632 else
3633 {
3634 /* h->dynindx may be -1 if this symbol was marked to
3635 become local. */
3636 if (h == NULL
3637 || ((info->symbolic || h->dynindx == -1)
f5385ebf 3638 && h->def_regular))
37c644f2 3639 {
55e6e397 3640 relocate = howto->partial_inplace;
37c644f2 3641 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
37c644f2
AO
3642 }
3643 else
3644 {
3645 BFD_ASSERT (h->dynindx != -1);
37c644f2 3646 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
37c644f2 3647 }
55e6e397
RS
3648 outrel.r_addend = relocation;
3649 outrel.r_addend
3650 += (howto->partial_inplace
3651 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3652 : addend);
37c644f2
AO
3653 }
3654
947216bf
AM
3655 loc = sreloc->contents;
3656 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3657 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
37c644f2
AO
3658
3659 /* If this reloc is against an external symbol, we do
3660 not want to fiddle with the addend. Otherwise, we
3661 need to include the symbol value so that it becomes
3662 an addend for the dynamic reloc. */
3663 if (! relocate)
3664 continue;
3665 }
37c644f2
AO
3666 goto final_link_relocate;
3667
fbca6ad9
AO
3668 case R_SH_GOTPLT32:
3669#ifdef INCLUDE_SHMEDIA
3670 case R_SH_GOTPLT_LOW16:
3671 case R_SH_GOTPLT_MEDLOW16:
3672 case R_SH_GOTPLT_MEDHI16:
3673 case R_SH_GOTPLT_HI16:
3674 case R_SH_GOTPLT10BY4:
3675 case R_SH_GOTPLT10BY8:
3676#endif
3677 /* Relocation is to the entry for this symbol in the
3678 procedure linkage table. */
3679
3680 if (h == NULL
f5385ebf 3681 || h->forced_local
fbca6ad9
AO
3682 || ! info->shared
3683 || info->symbolic
3684 || h->dynindx == -1
3685 || h->plt.offset == (bfd_vma) -1
3686 || h->got.offset != (bfd_vma) -1)
3687 goto force_got;
3688
3689 /* Relocation is to the entry for this symbol in the global
3690 offset table extension for the procedure linkage table. */
fbca6ad9 3691
067653c5 3692 BFD_ASSERT (sgotplt != NULL);
fbca6ad9 3693 relocation = (sgotplt->output_offset
55e6e397
RS
3694 + (get_plt_index (htab->plt_info, h->plt.offset)
3695 + 3) * 4);
fbca6ad9
AO
3696
3697#ifdef GOT_BIAS
3698 relocation -= GOT_BIAS;
3699#endif
3700
3701 goto final_link_relocate;
3702
3703 force_got:
37c644f2 3704 case R_SH_GOT32:
fbca6ad9
AO
3705#ifdef INCLUDE_SHMEDIA
3706 case R_SH_GOT_LOW16:
3707 case R_SH_GOT_MEDLOW16:
3708 case R_SH_GOT_MEDHI16:
3709 case R_SH_GOT_HI16:
3710 case R_SH_GOT10BY4:
3711 case R_SH_GOT10BY8:
3712#endif
37c644f2
AO
3713 /* Relocation is to the entry for this symbol in the global
3714 offset table. */
067653c5
AM
3715
3716 BFD_ASSERT (sgot != NULL);
37c644f2
AO
3717
3718 if (h != NULL)
3719 {
b34976b6 3720 bfd_boolean dyn;
37c644f2
AO
3721
3722 off = h->got.offset;
fbca6ad9
AO
3723#ifdef INCLUDE_SHMEDIA
3724 if (seen_stt_datalabel)
3725 {
3726 struct elf_sh_link_hash_entry *hsh;
3727
3728 hsh = (struct elf_sh_link_hash_entry *)h;
396a6083 3729 off = hsh->datalabel_got.offset;
fbca6ad9
AO
3730 }
3731#endif
37c644f2
AO
3732 BFD_ASSERT (off != (bfd_vma) -1);
3733
067653c5 3734 dyn = htab->root.dynamic_sections_created;
267fb3c1 3735 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
37c644f2 3736 || (info->shared
3d85aebe 3737 && SYMBOL_REFERENCES_LOCAL (info, h))
04e534c3
KK
3738 || (ELF_ST_VISIBILITY (h->other)
3739 && h->root.type == bfd_link_hash_undefweak))
37c644f2
AO
3740 {
3741 /* This is actually a static link, or it is a
3742 -Bsymbolic link and the symbol is defined
3743 locally, or the symbol was forced to be local
3744 because of a version file. We must initialize
3745 this entry in the global offset table. Since the
3746 offset must always be a multiple of 4, we use the
3747 least significant bit to record whether we have
3748 initialized it already.
3749
3750 When doing a dynamic link, we create a .rela.got
3751 relocation entry to initialize the value. This
3752 is done in the finish_dynamic_symbol routine. */
3753 if ((off & 1) != 0)
3754 off &= ~1;
3755 else
3756 {
3757 bfd_put_32 (output_bfd, relocation,
3758 sgot->contents + off);
fbca6ad9
AO
3759#ifdef INCLUDE_SHMEDIA
3760 if (seen_stt_datalabel)
3761 {
3762 struct elf_sh_link_hash_entry *hsh;
3763
3764 hsh = (struct elf_sh_link_hash_entry *)h;
396a6083 3765 hsh->datalabel_got.offset |= 1;
fbca6ad9
AO
3766 }
3767 else
3768#endif
3769 h->got.offset |= 1;
37c644f2
AO
3770 }
3771 }
3772
3773 relocation = sgot->output_offset + off;
3774 }
3775 else
3776 {
fbca6ad9
AO
3777#ifdef INCLUDE_SHMEDIA
3778 if (rel->r_addend)
3779 {
3780 BFD_ASSERT (local_got_offsets != NULL
3781 && (local_got_offsets[symtab_hdr->sh_info
3782 + r_symndx]
3783 != (bfd_vma) -1));
3784
3785 off = local_got_offsets[symtab_hdr->sh_info
3786 + r_symndx];
3787 }
3788 else
3789 {
3790#endif
37c644f2
AO
3791 BFD_ASSERT (local_got_offsets != NULL
3792 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3793
3794 off = local_got_offsets[r_symndx];
fbca6ad9
AO
3795#ifdef INCLUDE_SHMEDIA
3796 }
3797#endif
37c644f2
AO
3798
3799 /* The offset must always be a multiple of 4. We use
3800 the least significant bit to record whether we have
3801 already generated the necessary reloc. */
3802 if ((off & 1) != 0)
3803 off &= ~1;
3804 else
3805 {
3806 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3807
3808 if (info->shared)
3809 {
37c644f2 3810 Elf_Internal_Rela outrel;
947216bf 3811 bfd_byte *loc;
37c644f2 3812
3376eaf5
KK
3813 if (srelgot == NULL)
3814 {
3815 srelgot = bfd_get_section_by_name (dynobj,
3816 ".rela.got");
3817 BFD_ASSERT (srelgot != NULL);
3818 }
37c644f2
AO
3819
3820 outrel.r_offset = (sgot->output_section->vma
3821 + sgot->output_offset
3822 + off);
3823 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
3824 outrel.r_addend = relocation;
947216bf
AM
3825 loc = srelgot->contents;
3826 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3827 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
37c644f2
AO
3828 }
3829
fbca6ad9
AO
3830#ifdef INCLUDE_SHMEDIA
3831 if (rel->r_addend)
3832 local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
3833 else
3834#endif
3835 local_got_offsets[r_symndx] |= 1;
37c644f2
AO
3836 }
3837
3838 relocation = sgot->output_offset + off;
3839 }
3840
fbca6ad9 3841#ifdef GOT_BIAS
067653c5 3842 relocation -= GOT_BIAS;
fbca6ad9
AO
3843#endif
3844
37c644f2
AO
3845 goto final_link_relocate;
3846
3847 case R_SH_GOTOFF:
fbca6ad9
AO
3848#ifdef INCLUDE_SHMEDIA
3849 case R_SH_GOTOFF_LOW16:
3850 case R_SH_GOTOFF_MEDLOW16:
3851 case R_SH_GOTOFF_MEDHI16:
3852 case R_SH_GOTOFF_HI16:
3853#endif
37c644f2
AO
3854 /* Relocation is relative to the start of the global offset
3855 table. */
3856
067653c5 3857 BFD_ASSERT (sgot != NULL);
37c644f2
AO
3858
3859 /* Note that sgot->output_offset is not involved in this
3860 calculation. We always want the start of .got. If we
3861 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3862 permitted by the ABI, we might have to change this
3863 calculation. */
3864 relocation -= sgot->output_section->vma;
3865
fbca6ad9
AO
3866#ifdef GOT_BIAS
3867 relocation -= GOT_BIAS;
3868#endif
3869
3870 addend = rel->r_addend;
3871
37c644f2
AO
3872 goto final_link_relocate;
3873
3874 case R_SH_GOTPC:
fbca6ad9
AO
3875#ifdef INCLUDE_SHMEDIA
3876 case R_SH_GOTPC_LOW16:
3877 case R_SH_GOTPC_MEDLOW16:
3878 case R_SH_GOTPC_MEDHI16:
3879 case R_SH_GOTPC_HI16:
3880#endif
37c644f2
AO
3881 /* Use global offset table as symbol value. */
3882
067653c5 3883 BFD_ASSERT (sgot != NULL);
37c644f2
AO
3884 relocation = sgot->output_section->vma;
3885
fbca6ad9
AO
3886#ifdef GOT_BIAS
3887 relocation += GOT_BIAS;
3888#endif
3889
3890 addend = rel->r_addend;
3891
37c644f2
AO
3892 goto final_link_relocate;
3893
3894 case R_SH_PLT32:
fbca6ad9
AO
3895#ifdef INCLUDE_SHMEDIA
3896 case R_SH_PLT_LOW16:
3897 case R_SH_PLT_MEDLOW16:
3898 case R_SH_PLT_MEDHI16:
3899 case R_SH_PLT_HI16:
3900#endif
37c644f2
AO
3901 /* Relocation is to the entry for this symbol in the
3902 procedure linkage table. */
3903
3904 /* Resolve a PLT reloc against a local symbol directly,
3905 without using the procedure linkage table. */
3906 if (h == NULL)
3907 goto final_link_relocate;
3908
f5385ebf 3909 if (h->forced_local)
37c644f2
AO
3910 goto final_link_relocate;
3911
3912 if (h->plt.offset == (bfd_vma) -1)
3913 {
3914 /* We didn't make a PLT entry for this symbol. This
3915 happens when statically linking PIC code, or when
3916 using -Bsymbolic. */
3917 goto final_link_relocate;
3918 }
3919
067653c5 3920 BFD_ASSERT (splt != NULL);
37c644f2
AO
3921 relocation = (splt->output_section->vma
3922 + splt->output_offset
3923 + h->plt.offset);
3924
fbca6ad9
AO
3925#ifdef INCLUDE_SHMEDIA
3926 relocation++;
3927#endif
3928
3929 addend = rel->r_addend;
3930
37c644f2
AO
3931 goto final_link_relocate;
3932
3933 case R_SH_LOOP_START:
3934 {
3935 static bfd_vma start, end;
3936
3937 start = (relocation + rel->r_addend
3938 - (sec->output_section->vma + sec->output_offset));
3939 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3940 rel->r_offset, sec, start, end);
3941 break;
3942
3943 case R_SH_LOOP_END:
3944 end = (relocation + rel->r_addend
3945 - (sec->output_section->vma + sec->output_offset));
3946 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3947 rel->r_offset, sec, start, end);
3948 break;
3949 }
3376eaf5
KK
3950
3951 case R_SH_TLS_GD_32:
3952 case R_SH_TLS_IE_32:
3953 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
3954 tls_type = GOT_UNKNOWN;
3955 if (h == NULL && local_got_offsets)
3956 tls_type = sh_elf_local_got_tls_type (input_bfd) [r_symndx];
3957 else if (h != NULL)
3958 {
3959 tls_type = sh_elf_hash_entry (h)->tls_type;
3960 if (! info->shared
3961 && (h->dynindx == -1
f5385ebf 3962 || h->def_regular))
267fb3c1 3963 r_type = R_SH_TLS_LE_32;
3376eaf5
KK
3964 }
3965
3966 if (r_type == R_SH_TLS_GD_32 && tls_type == GOT_TLS_IE)
3967 r_type = R_SH_TLS_IE_32;
3968
3969 if (r_type == R_SH_TLS_LE_32)
3970 {
3971 bfd_vma offset;
3972 unsigned short insn;
3376eaf5
KK
3973
3974 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
3975 {
3976 /* GD->LE transition:
3977 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
b34976b6 3978 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
3376eaf5
KK
3979 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
3980 We change it into:
3981 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
3982 nop; nop; ...
3983 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
3984
3985 offset = rel->r_offset;
3986 BFD_ASSERT (offset >= 16);
3987 /* Size of GD instructions is 16 or 18. */
3988 offset -= 16;
3989 insn = bfd_get_16 (input_bfd, contents + offset + 0);
3990 if ((insn & 0xff00) == 0xc700)
3991 {
3992 BFD_ASSERT (offset >= 2);
3993 offset -= 2;
3994 insn = bfd_get_16 (input_bfd, contents + offset + 0);
3995 }
3996
3997 BFD_ASSERT ((insn & 0xff00) == 0xd400);
3998 insn = bfd_get_16 (input_bfd, contents + offset + 2);
3999 BFD_ASSERT ((insn & 0xff00) == 0xc700);
4000 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4001 BFD_ASSERT ((insn & 0xff00) == 0xd100);
4002 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4003 BFD_ASSERT (insn == 0x310c);
4004 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4005 BFD_ASSERT (insn == 0x410b);
4006 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4007 BFD_ASSERT (insn == 0x34cc);
4008
4009 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
4010 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
4011 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
4012 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4013 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4014 }
4015 else
4016 {
4017 int index;
4018
4019 /* IE->LE transition:
4020 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
4021 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
4022 We change it into:
4023 mov.l .Ln,rM; stc gbr,rN; nop; ...;
4024 1: x@TPOFF; 2:. */
4025
4026 offset = rel->r_offset;
4027 BFD_ASSERT (offset >= 16);
4028 /* Size of IE instructions is 10 or 12. */
4029 offset -= 10;
4030 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4031 if ((insn & 0xf0ff) == 0x0012)
4032 {
4033 BFD_ASSERT (offset >= 2);
4034 offset -= 2;
4035 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4036 }
4037
4038 BFD_ASSERT ((insn & 0xff00) == 0xd000);
4039 index = insn & 0x00ff;
4040 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4041 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
4042 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4043 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
4044 insn = 0xd000 | (insn & 0x0f00) | index;
4045 bfd_put_16 (output_bfd, insn, contents + offset + 0);
4046 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
4047 }
4048
267fb3c1
KK
4049 bfd_put_32 (output_bfd, tpoff (info, relocation),
4050 contents + rel->r_offset);
3376eaf5
KK
4051 continue;
4052 }
4053
4054 sgot = htab->sgot;
4055 if (sgot == NULL)
4056 abort ();
4057
4058 if (h != NULL)
4059 off = h->got.offset;
4060 else
4061 {
4062 if (local_got_offsets == NULL)
4063 abort ();
4064
4065 off = local_got_offsets[r_symndx];
4066 }
4067
267fb3c1
KK
4068 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4069 if (r_type == R_SH_TLS_IE_32
4070 && ! htab->root.dynamic_sections_created)
4071 {
4072 off &= ~1;
4073 bfd_put_32 (output_bfd, tpoff (info, relocation),
4074 sgot->contents + off);
4075 bfd_put_32 (output_bfd, sgot->output_offset + off,
4076 contents + rel->r_offset);
4077 continue;
4078 }
4079
3376eaf5
KK
4080 if ((off & 1) != 0)
4081 off &= ~1;
26e41594 4082 else
3376eaf5
KK
4083 {
4084 Elf_Internal_Rela outrel;
947216bf 4085 bfd_byte *loc;
3376eaf5
KK
4086 int dr_type, indx;
4087
4088 if (srelgot == NULL)
4089 {
4090 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4091 BFD_ASSERT (srelgot != NULL);
4092 }
4093
4094 outrel.r_offset = (sgot->output_section->vma
4095 + sgot->output_offset + off);
4096
267fb3c1 4097 if (h == NULL || h->dynindx == -1)
c95b8a7a
KK
4098 indx = 0;
4099 else
4100 indx = h->dynindx;
267fb3c1 4101
3376eaf5
KK
4102 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
4103 R_SH_TLS_TPOFF32);
4104 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
4105 outrel.r_addend = relocation - dtpoff_base (info);
4106 else
4107 outrel.r_addend = 0;
4108 outrel.r_info = ELF32_R_INFO (indx, dr_type);
947216bf
AM
4109 loc = srelgot->contents;
4110 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3376eaf5
KK
4111 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4112
4113 if (r_type == R_SH_TLS_GD_32)
4114 {
4115 if (indx == 0)
4116 {
4117 bfd_put_32 (output_bfd,
4118 relocation - dtpoff_base (info),
4119 sgot->contents + off + 4);
4120 }
4121 else
4122 {
4123 outrel.r_info = ELF32_R_INFO (indx,
4124 R_SH_TLS_DTPOFF32);
4125 outrel.r_offset += 4;
4126 outrel.r_addend = 0;
4127 srelgot->reloc_count++;
947216bf
AM
4128 loc += sizeof (Elf32_External_Rela);
4129 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3376eaf5
KK
4130 }
4131 }
4132
4133 if (h != NULL)
4134 h->got.offset |= 1;
4135 else
4136 local_got_offsets[r_symndx] |= 1;
4137 }
4138
4139 if (off >= (bfd_vma) -2)
4140 abort ();
4141
4142 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
4143 relocation = sgot->output_offset + off;
4144 else
4145 {
4146 bfd_vma offset;
4147 unsigned short insn;
4148
4149 /* GD->IE transition:
4150 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4151 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4152 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4153 We change it into:
4154 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4155 nop; nop; bra 3f; nop; .align 2;
4156 1: .long x@TPOFF; 2:...; 3:. */
4157
4158 offset = rel->r_offset;
4159 BFD_ASSERT (offset >= 16);
4160 /* Size of GD instructions is 16 or 18. */
4161 offset -= 16;
4162 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4163 if ((insn & 0xff00) == 0xc700)
4164 {
4165 BFD_ASSERT (offset >= 2);
4166 offset -= 2;
4167 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4168 }
4169
4170 BFD_ASSERT ((insn & 0xff00) == 0xd400);
4171
4172 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4173 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
4174
4175 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4176 BFD_ASSERT ((insn & 0xff00) == 0xc700);
4177 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4178 BFD_ASSERT ((insn & 0xff00) == 0xd100);
4179 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4180 BFD_ASSERT (insn == 0x310c);
4181 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4182 BFD_ASSERT (insn == 0x410b);
4183 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4184 BFD_ASSERT (insn == 0x34cc);
4185
4186 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
4187 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
4188 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
4189 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4190 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4191
4192 bfd_put_32 (output_bfd, sgot->output_offset + off,
4193 contents + rel->r_offset);
4194
4195 continue;
4196 }
4197
4198 addend = rel->r_addend;
4199
4200 goto final_link_relocate;
4201
4202 case R_SH_TLS_LD_32:
4203 if (! info->shared)
4204 {
4205 bfd_vma offset;
4206 unsigned short insn;
4207
4208 /* LD->LE transition:
4209 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4210 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4211 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
4212 We change it into:
4213 stc gbr,r0; nop; nop; nop;
4214 nop; nop; bra 3f; ...; 3:. */
4215
4216 offset = rel->r_offset;
4217 BFD_ASSERT (offset >= 16);
4218 /* Size of LD instructions is 16 or 18. */
4219 offset -= 16;
4220 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4221 if ((insn & 0xff00) == 0xc700)
4222 {
4223 BFD_ASSERT (offset >= 2);
4224 offset -= 2;
4225 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4226 }
4227
4228 BFD_ASSERT ((insn & 0xff00) == 0xd400);
4229 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4230 BFD_ASSERT ((insn & 0xff00) == 0xc700);
4231 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4232 BFD_ASSERT ((insn & 0xff00) == 0xd100);
4233 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4234 BFD_ASSERT (insn == 0x310c);
4235 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4236 BFD_ASSERT (insn == 0x410b);
4237 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4238 BFD_ASSERT (insn == 0x34cc);
4239
4240 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
4241 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
4242 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
4243 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
4244 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4245 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4246
4247 continue;
4248 }
4249
4250 sgot = htab->sgot;
4251 if (sgot == NULL)
4252 abort ();
4253
4254 off = htab->tls_ldm_got.offset;
4255 if (off & 1)
4256 off &= ~1;
4257 else
4258 {
4259 Elf_Internal_Rela outrel;
947216bf 4260 bfd_byte *loc;
3376eaf5
KK
4261
4262 srelgot = htab->srelgot;
4263 if (srelgot == NULL)
4264 abort ();
4265
4266 outrel.r_offset = (sgot->output_section->vma
4267 + sgot->output_offset + off);
4268 outrel.r_addend = 0;
4269 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
947216bf
AM
4270 loc = srelgot->contents;
4271 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3376eaf5
KK
4272 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4273 htab->tls_ldm_got.offset |= 1;
4274 }
4275
4276 relocation = sgot->output_offset + off;
4277 addend = rel->r_addend;
4278
4279 goto final_link_relocate;
4280
4281 case R_SH_TLS_LDO_32:
4282 if (! info->shared)
267fb3c1 4283 relocation = tpoff (info, relocation);
3376eaf5
KK
4284 else
4285 relocation -= dtpoff_base (info);
4286
4287 addend = rel->r_addend;
4288 goto final_link_relocate;
4289
4290 case R_SH_TLS_LE_32:
4291 {
4292 int indx;
4293 Elf_Internal_Rela outrel;
947216bf 4294 bfd_byte *loc;
3376eaf5 4295
267fb3c1
KK
4296 if (! info->shared)
4297 {
4298 relocation = tpoff (info, relocation);
4299 addend = rel->r_addend;
4300 goto final_link_relocate;
4301 }
4302
3376eaf5
KK
4303 if (sreloc == NULL)
4304 {
4305 const char *name;
4306
4307 name = (bfd_elf_string_from_elf_section
4308 (input_bfd,
4309 elf_elfheader (input_bfd)->e_shstrndx,
4310 elf_section_data (input_section)->rel_hdr.sh_name));
4311 if (name == NULL)
b34976b6 4312 return FALSE;
3376eaf5
KK
4313
4314 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4315 && strcmp (bfd_get_section_name (input_bfd,
4316 input_section),
4317 name + 5) == 0);
4318
4319 sreloc = bfd_get_section_by_name (dynobj, name);
4320 BFD_ASSERT (sreloc != NULL);
4321 }
4322
267fb3c1
KK
4323 if (h == NULL || h->dynindx == -1)
4324 indx = 0;
4325 else
4326 indx = h->dynindx;
4327
3376eaf5
KK
4328 outrel.r_offset = (input_section->output_section->vma
4329 + input_section->output_offset
4330 + rel->r_offset);
4331 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
4332 if (indx == 0)
4333 outrel.r_addend = relocation - dtpoff_base (info);
4334 else
4335 outrel.r_addend = 0;
3376eaf5 4336
947216bf
AM
4337 loc = sreloc->contents;
4338 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4339 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3376eaf5
KK
4340 continue;
4341 }
37c644f2
AO
4342 }
4343
146be91a 4344 relocation_done:
37c644f2
AO
4345 if (r != bfd_reloc_ok)
4346 {
4347 switch (r)
4348 {
4349 default:
4350 case bfd_reloc_outofrange:
4351 abort ();
4352 case bfd_reloc_overflow:
4353 {
4354 const char *name;
4355
4356 if (h != NULL)
dfeffb9f 4357 name = NULL;
37c644f2
AO
4358 else
4359 {
252b5132
RH
4360 name = (bfd_elf_string_from_elf_section
4361 (input_bfd, symtab_hdr->sh_link, sym->st_name));
4362 if (name == NULL)
b34976b6 4363 return FALSE;
252b5132
RH
4364 if (*name == '\0')
4365 name = bfd_section_name (input_bfd, sec);
4366 }
4367 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
4368 (info, (h ? &h->root : NULL), name, howto->name,
4369 (bfd_vma) 0, input_bfd, input_section,
4370 rel->r_offset)))
b34976b6 4371 return FALSE;
252b5132
RH
4372 }
4373 break;
4374 }
4375 }
4376 }
4377
b34976b6 4378 return TRUE;
252b5132
RH
4379}
4380
4381/* This is a version of bfd_generic_get_relocated_section_contents
4382 which uses sh_elf_relocate_section. */
4383
4384static bfd_byte *
09fd220b
KK
4385sh_elf_get_relocated_section_contents (bfd *output_bfd,
4386 struct bfd_link_info *link_info,
4387 struct bfd_link_order *link_order,
4388 bfd_byte *data,
4389 bfd_boolean relocatable,
4390 asymbol **symbols)
252b5132
RH
4391{
4392 Elf_Internal_Shdr *symtab_hdr;
4393 asection *input_section = link_order->u.indirect.section;
4394 bfd *input_bfd = input_section->owner;
4395 asection **sections = NULL;
4396 Elf_Internal_Rela *internal_relocs = NULL;
6cdc0ccc 4397 Elf_Internal_Sym *isymbuf = NULL;
252b5132
RH
4398
4399 /* We only need to handle the case of relaxing, or of having a
4400 particular set of section contents, specially. */
1049f94e 4401 if (relocatable
252b5132
RH
4402 || elf_section_data (input_section)->this_hdr.contents == NULL)
4403 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4404 link_order, data,
1049f94e 4405 relocatable,
252b5132
RH
4406 symbols);
4407
4408 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4409
4410 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
eea6121a 4411 (size_t) input_section->size);
252b5132
RH
4412
4413 if ((input_section->flags & SEC_RELOC) != 0
4414 && input_section->reloc_count > 0)
4415 {
252b5132 4416 asection **secpp;
6cdc0ccc 4417 Elf_Internal_Sym *isym, *isymend;
9ad5cbcf 4418 bfd_size_type amt;
252b5132 4419
45d6a902 4420 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 4421 (input_bfd, input_section, NULL,
b34976b6 4422 (Elf_Internal_Rela *) NULL, FALSE));
252b5132
RH
4423 if (internal_relocs == NULL)
4424 goto error_return;
4425
6cdc0ccc
AM
4426 if (symtab_hdr->sh_info != 0)
4427 {
4428 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4429 if (isymbuf == NULL)
4430 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4431 symtab_hdr->sh_info, 0,
4432 NULL, NULL, NULL);
4433 if (isymbuf == NULL)
4434 goto error_return;
4435 }
252b5132 4436
9ad5cbcf
AM
4437 amt = symtab_hdr->sh_info;
4438 amt *= sizeof (asection *);
4439 sections = (asection **) bfd_malloc (amt);
4440 if (sections == NULL && amt != 0)
252b5132
RH
4441 goto error_return;
4442
6cdc0ccc
AM
4443 isymend = isymbuf + symtab_hdr->sh_info;
4444 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
252b5132
RH
4445 {
4446 asection *isec;
4447
6cdc0ccc 4448 if (isym->st_shndx == SHN_UNDEF)
252b5132 4449 isec = bfd_und_section_ptr;
6cdc0ccc 4450 else if (isym->st_shndx == SHN_ABS)
252b5132 4451 isec = bfd_abs_section_ptr;
6cdc0ccc 4452 else if (isym->st_shndx == SHN_COMMON)
252b5132
RH
4453 isec = bfd_com_section_ptr;
4454 else
6cdc0ccc 4455 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
252b5132
RH
4456
4457 *secpp = isec;
4458 }
4459
4460 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
4461 input_section, data, internal_relocs,
6cdc0ccc 4462 isymbuf, sections))
252b5132
RH
4463 goto error_return;
4464
4465 if (sections != NULL)
4466 free (sections);
6cdc0ccc
AM
4467 if (isymbuf != NULL
4468 && symtab_hdr->contents != (unsigned char *) isymbuf)
4469 free (isymbuf);
4470 if (elf_section_data (input_section)->relocs != internal_relocs)
252b5132 4471 free (internal_relocs);
252b5132
RH
4472 }
4473
4474 return data;
4475
4476 error_return:
252b5132
RH
4477 if (sections != NULL)
4478 free (sections);
6cdc0ccc
AM
4479 if (isymbuf != NULL
4480 && symtab_hdr->contents != (unsigned char *) isymbuf)
4481 free (isymbuf);
4482 if (internal_relocs != NULL
4483 && elf_section_data (input_section)->relocs != internal_relocs)
4484 free (internal_relocs);
252b5132
RH
4485 return NULL;
4486}
917583ad 4487
3376eaf5
KK
4488/* Return the base VMA address which should be subtracted from real addresses
4489 when resolving @dtpoff relocation.
4490 This is PT_TLS segment p_vaddr. */
4491
4492static bfd_vma
09fd220b 4493dtpoff_base (struct bfd_link_info *info)
3376eaf5 4494{
e1918d23
AM
4495 /* If tls_sec is NULL, we should have signalled an error already. */
4496 if (elf_hash_table (info)->tls_sec == NULL)
267fb3c1 4497 return 0;
e1918d23 4498 return elf_hash_table (info)->tls_sec->vma;
3376eaf5
KK
4499}
4500
267fb3c1
KK
4501/* Return the relocation value for R_SH_TLS_TPOFF32.. */
4502
4503static bfd_vma
09fd220b 4504tpoff (struct bfd_link_info *info, bfd_vma address)
267fb3c1 4505{
e1918d23
AM
4506 /* If tls_sec is NULL, we should have signalled an error already. */
4507 if (elf_hash_table (info)->tls_sec == NULL)
267fb3c1
KK
4508 return 0;
4509 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
4510 structure which has 2 pointer fields. */
044ad7e3
KK
4511 return (address - elf_hash_table (info)->tls_sec->vma
4512 + align_power ((bfd_vma) 8,
4513 elf_hash_table (info)->tls_sec->alignment_power));
267fb3c1
KK
4514}
4515
252b5132 4516static asection *
09fd220b
KK
4517sh_elf_gc_mark_hook (asection *sec,
4518 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4519 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
4520 Elf_Internal_Sym *sym)
252b5132
RH
4521{
4522 if (h != NULL)
4523 {
4524 switch (ELF32_R_TYPE (rel->r_info))
435b1e90
KH
4525 {
4526 case R_SH_GNU_VTINHERIT:
4527 case R_SH_GNU_VTENTRY:
4528 break;
252b5132 4529
435b1e90 4530 default:
4972a8e9
SC
4531#ifdef INCLUDE_SHMEDIA
4532 while (h->root.type == bfd_link_hash_indirect
4533 && h->root.u.i.link)
4534 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4535#endif
435b1e90
KH
4536 switch (h->root.type)
4537 {
4538 case bfd_link_hash_defined:
4539 case bfd_link_hash_defweak:
4540 return h->root.u.def.section;
252b5132 4541
435b1e90
KH
4542 case bfd_link_hash_common:
4543 return h->root.u.c.p->section;
e049a0de 4544
435b1e90
KH
4545 default:
4546 break;
4547 }
4548 }
4549 }
4550 else
1e2f5b6e 4551 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
9ad5cbcf 4552
252b5132
RH
4553 return NULL;
4554}
4555
37c644f2
AO
4556/* Update the got entry reference counts for the section being removed. */
4557
b34976b6 4558static bfd_boolean
09fd220b
KK
4559sh_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
4560 asection *sec, const Elf_Internal_Rela *relocs)
252b5132 4561{
067653c5
AM
4562 Elf_Internal_Shdr *symtab_hdr;
4563 struct elf_link_hash_entry **sym_hashes;
4564 bfd_signed_vma *local_got_refcounts;
4565 const Elf_Internal_Rela *rel, *relend;
067653c5
AM
4566
4567 elf_section_data (sec)->local_dynrel = NULL;
4568
4569 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4570 sym_hashes = elf_sym_hashes (abfd);
4571 local_got_refcounts = elf_local_got_refcounts (abfd);
4572
4573 relend = relocs + sec->reloc_count;
4574 for (rel = relocs; rel < relend; rel++)
396a6083 4575 {
26e41594
AM
4576 unsigned long r_symndx;
4577 unsigned int r_type;
4578 struct elf_link_hash_entry *h = NULL;
396a6083
SC
4579#ifdef INCLUDE_SHMEDIA
4580 int seen_stt_datalabel = 0;
4581#endif
3376eaf5 4582
396a6083 4583 r_symndx = ELF32_R_SYM (rel->r_info);
26e41594 4584 if (r_symndx >= symtab_hdr->sh_info)
396a6083 4585 {
26e41594
AM
4586 struct elf_sh_link_hash_entry *eh;
4587 struct elf_sh_dyn_relocs **pp;
4588 struct elf_sh_dyn_relocs *p;
4589
396a6083 4590 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
396a6083
SC
4591 while (h->root.type == bfd_link_hash_indirect
4592 || h->root.type == bfd_link_hash_warning)
4593 {
3eb128b2 4594#ifdef INCLUDE_SHMEDIA
396a6083 4595 seen_stt_datalabel |= h->type == STT_DATALABEL;
3eb128b2 4596#endif
396a6083
SC
4597 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4598 }
26e41594
AM
4599 eh = (struct elf_sh_link_hash_entry *) h;
4600 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4601 if (p->sec == sec)
4602 {
4603 /* Everything must go for SEC. */
4604 *pp = p->next;
4605 break;
4606 }
396a6083 4607 }
067653c5 4608
26e41594
AM
4609 r_type = ELF32_R_TYPE (rel->r_info);
4610 switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL))
396a6083
SC
4611 {
4612 case R_SH_TLS_LD_32:
4613 if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
4614 sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
4615 break;
4616
4617 case R_SH_GOT32:
4618 case R_SH_GOTOFF:
4619 case R_SH_GOTPC:
4620#ifdef INCLUDE_SHMEDIA
4621 case R_SH_GOT_LOW16:
4622 case R_SH_GOT_MEDLOW16:
4623 case R_SH_GOT_MEDHI16:
4624 case R_SH_GOT_HI16:
4625 case R_SH_GOT10BY4:
4626 case R_SH_GOT10BY8:
4627 case R_SH_GOTOFF_LOW16:
4628 case R_SH_GOTOFF_MEDLOW16:
4629 case R_SH_GOTOFF_MEDHI16:
4630 case R_SH_GOTOFF_HI16:
4631 case R_SH_GOTPC_LOW16:
4632 case R_SH_GOTPC_MEDLOW16:
4633 case R_SH_GOTPC_MEDHI16:
4634 case R_SH_GOTPC_HI16:
4635#endif
4636 case R_SH_TLS_GD_32:
4637 case R_SH_TLS_IE_32:
4638 if (h != NULL)
4639 {
4640#ifdef INCLUDE_SHMEDIA
4641 if (seen_stt_datalabel)
4642 {
26e41594
AM
4643 struct elf_sh_link_hash_entry *eh;
4644 eh = (struct elf_sh_link_hash_entry *) h;
396a6083
SC
4645 if (eh->datalabel_got.refcount > 0)
4646 eh->datalabel_got.refcount -= 1;
4647 }
4648 else
4649#endif
4650 if (h->got.refcount > 0)
4651 h->got.refcount -= 1;
4652 }
4653 else if (local_got_refcounts != NULL)
4654 {
4655#ifdef INCLUDE_SHMEDIA
4656 if (rel->r_addend & 1)
4657 {
4658 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
4659 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
4660 }
4661 else
4662#endif
4663 if (local_got_refcounts[r_symndx] > 0)
4664 local_got_refcounts[r_symndx] -= 1;
4665 }
4666 break;
4667
4668 case R_SH_DIR32:
4669 case R_SH_REL32:
26e41594
AM
4670 if (info->shared)
4671 break;
4672 /* Fall thru */
396a6083
SC
4673
4674 case R_SH_PLT32:
4675#ifdef INCLUDE_SHMEDIA
4676 case R_SH_PLT_LOW16:
4677 case R_SH_PLT_MEDLOW16:
4678 case R_SH_PLT_MEDHI16:
4679 case R_SH_PLT_HI16:
4680#endif
4681 if (h != NULL)
4682 {
4683 if (h->plt.refcount > 0)
4684 h->plt.refcount -= 1;
4685 }
4686 break;
067653c5 4687
396a6083
SC
4688 case R_SH_GOTPLT32:
4689#ifdef INCLUDE_SHMEDIA
4690 case R_SH_GOTPLT_LOW16:
4691 case R_SH_GOTPLT_MEDLOW16:
4692 case R_SH_GOTPLT_MEDHI16:
4693 case R_SH_GOTPLT_HI16:
4694 case R_SH_GOTPLT10BY4:
4695 case R_SH_GOTPLT10BY8:
4696#endif
4697 if (h != NULL)
4698 {
26e41594
AM
4699 struct elf_sh_link_hash_entry *eh;
4700 eh = (struct elf_sh_link_hash_entry *) h;
396a6083 4701 if (eh->gotplt_refcount > 0)
067653c5 4702 {
396a6083
SC
4703 eh->gotplt_refcount -= 1;
4704 if (h->plt.refcount > 0)
4705 h->plt.refcount -= 1;
067653c5 4706 }
067653c5 4707#ifdef INCLUDE_SHMEDIA
396a6083
SC
4708 else if (seen_stt_datalabel)
4709 {
4710 if (eh->datalabel_got.refcount > 0)
4711 eh->datalabel_got.refcount -= 1;
4712 }
067653c5 4713#endif
396a6083
SC
4714 else if (h->got.refcount > 0)
4715 h->got.refcount -= 1;
4716 }
4717 else if (local_got_refcounts != NULL)
4718 {
067653c5 4719#ifdef INCLUDE_SHMEDIA
396a6083
SC
4720 if (rel->r_addend & 1)
4721 {
4722 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
4723 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
4724 }
4725 else
067653c5 4726#endif
396a6083
SC
4727 if (local_got_refcounts[r_symndx] > 0)
4728 local_got_refcounts[r_symndx] -= 1;
4729 }
4730 break;
067653c5 4731
396a6083
SC
4732 default:
4733 break;
4734 }
4735 }
067653c5 4736
b34976b6 4737 return TRUE;
252b5132
RH
4738}
4739
067653c5
AM
4740/* Copy the extra info we tack onto an elf_link_hash_entry. */
4741
4742static void
fcfa13d2 4743sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
09fd220b
KK
4744 struct elf_link_hash_entry *dir,
4745 struct elf_link_hash_entry *ind)
067653c5
AM
4746{
4747 struct elf_sh_link_hash_entry *edir, *eind;
4748
4749 edir = (struct elf_sh_link_hash_entry *) dir;
4750 eind = (struct elf_sh_link_hash_entry *) ind;
4751
4752 if (eind->dyn_relocs != NULL)
4753 {
4754 if (edir->dyn_relocs != NULL)
4755 {
4756 struct elf_sh_dyn_relocs **pp;
4757 struct elf_sh_dyn_relocs *p;
4758
fcfa13d2 4759 /* Add reloc counts against the indirect sym to the direct sym
067653c5
AM
4760 list. Merge any entries against the same section. */
4761 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4762 {
4763 struct elf_sh_dyn_relocs *q;
4764
4765 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4766 if (q->sec == p->sec)
4767 {
4768 q->pc_count += p->pc_count;
4769 q->count += p->count;
4770 *pp = p->next;
4771 break;
4772 }
4773 if (q == NULL)
4774 pp = &p->next;
4775 }
4776 *pp = edir->dyn_relocs;
4777 }
4778
4779 edir->dyn_relocs = eind->dyn_relocs;
4780 eind->dyn_relocs = NULL;
4781 }
4989d792
SC
4782 edir->gotplt_refcount = eind->gotplt_refcount;
4783 eind->gotplt_refcount = 0;
396a6083 4784#ifdef INCLUDE_SHMEDIA
fcfa13d2
AM
4785 edir->datalabel_got.refcount += eind->datalabel_got.refcount;
4786 eind->datalabel_got.refcount = 0;
396a6083 4787#endif
067653c5 4788
3376eaf5
KK
4789 if (ind->root.type == bfd_link_hash_indirect
4790 && dir->got.refcount <= 0)
4791 {
4792 edir->tls_type = eind->tls_type;
4793 eind->tls_type = GOT_UNKNOWN;
4794 }
4795
04e534c3 4796 if (ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
4797 && dir->dynamic_adjusted)
4798 {
4799 /* If called to transfer flags for a weakdef during processing
4800 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
4801 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4802 dir->ref_dynamic |= ind->ref_dynamic;
4803 dir->ref_regular |= ind->ref_regular;
4804 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
4805 dir->needs_plt |= ind->needs_plt;
4806 }
04e534c3 4807 else
fcfa13d2 4808 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
067653c5
AM
4809}
4810
3376eaf5 4811static int
09fd220b
KK
4812sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
4813 int is_local)
3376eaf5
KK
4814{
4815 if (info->shared)
4816 return r_type;
4817
4818 switch (r_type)
4819 {
4820 case R_SH_TLS_GD_32:
4821 case R_SH_TLS_IE_32:
4822 if (is_local)
4823 return R_SH_TLS_LE_32;
4824 return R_SH_TLS_IE_32;
4825 case R_SH_TLS_LD_32:
4826 return R_SH_TLS_LE_32;
4827 }
4828
4829 return r_type;
4830}
4831
252b5132
RH
4832/* Look through the relocs for a section during the first phase.
4833 Since we don't do .gots or .plts, we just need to consider the
4834 virtual table relocs for gc. */
435b1e90 4835
b34976b6 4836static bfd_boolean
09fd220b
KK
4837sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
4838 const Elf_Internal_Rela *relocs)
252b5132
RH
4839{
4840 Elf_Internal_Shdr *symtab_hdr;
4841 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
067653c5 4842 struct elf_sh_link_hash_table *htab;
252b5132
RH
4843 const Elf_Internal_Rela *rel;
4844 const Elf_Internal_Rela *rel_end;
37c644f2
AO
4845 bfd_vma *local_got_offsets;
4846 asection *sgot;
4847 asection *srelgot;
4848 asection *sreloc;
3376eaf5
KK
4849 unsigned int r_type;
4850 int tls_type, old_tls_type;
435b1e90 4851
37c644f2
AO
4852 sgot = NULL;
4853 srelgot = NULL;
4854 sreloc = NULL;
4855
1049f94e 4856 if (info->relocatable)
b34976b6 4857 return TRUE;
435b1e90 4858
252b5132
RH
4859 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4860 sym_hashes = elf_sym_hashes (abfd);
435b1e90 4861 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
252b5132
RH
4862 if (!elf_bad_symtab (abfd))
4863 sym_hashes_end -= symtab_hdr->sh_info;
435b1e90 4864
067653c5 4865 htab = sh_elf_hash_table (info);
37c644f2
AO
4866 local_got_offsets = elf_local_got_offsets (abfd);
4867
252b5132
RH
4868 rel_end = relocs + sec->reloc_count;
4869 for (rel = relocs; rel < rel_end; rel++)
4870 {
4871 struct elf_link_hash_entry *h;
4872 unsigned long r_symndx;
396a6083
SC
4873#ifdef INCLUDE_SHMEDIA
4874 int seen_stt_datalabel = 0;
4875#endif
435b1e90 4876
252b5132 4877 r_symndx = ELF32_R_SYM (rel->r_info);
3376eaf5
KK
4878 r_type = ELF32_R_TYPE (rel->r_info);
4879
252b5132 4880 if (r_symndx < symtab_hdr->sh_info)
435b1e90 4881 h = NULL;
252b5132 4882 else
396a6083
SC
4883 {
4884 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
396a6083
SC
4885 while (h->root.type == bfd_link_hash_indirect
4886 || h->root.type == bfd_link_hash_warning)
4887 {
973a3492 4888#ifdef INCLUDE_SHMEDIA
396a6083 4889 seen_stt_datalabel |= h->type == STT_DATALABEL;
973a3492 4890#endif
396a6083
SC
4891 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4892 }
396a6083 4893 }
435b1e90 4894
3376eaf5
KK
4895 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
4896 if (! info->shared
4897 && r_type == R_SH_TLS_IE_32
4898 && h != NULL
4899 && h->root.type != bfd_link_hash_undefined
4900 && h->root.type != bfd_link_hash_undefweak
4901 && (h->dynindx == -1
f5385ebf 4902 || h->def_regular))
3376eaf5
KK
4903 r_type = R_SH_TLS_LE_32;
4904
37c644f2 4905 /* Some relocs require a global offset table. */
067653c5 4906 if (htab->sgot == NULL)
37c644f2 4907 {
3376eaf5 4908 switch (r_type)
37c644f2 4909 {
fbca6ad9 4910 case R_SH_GOTPLT32:
37c644f2
AO
4911 case R_SH_GOT32:
4912 case R_SH_GOTOFF:
4913 case R_SH_GOTPC:
fbca6ad9
AO
4914#ifdef INCLUDE_SHMEDIA
4915 case R_SH_GOTPLT_LOW16:
4916 case R_SH_GOTPLT_MEDLOW16:
4917 case R_SH_GOTPLT_MEDHI16:
4918 case R_SH_GOTPLT_HI16:
4919 case R_SH_GOTPLT10BY4:
4920 case R_SH_GOTPLT10BY8:
4921 case R_SH_GOT_LOW16:
4922 case R_SH_GOT_MEDLOW16:
4923 case R_SH_GOT_MEDHI16:
4924 case R_SH_GOT_HI16:
4925 case R_SH_GOT10BY4:
4926 case R_SH_GOT10BY8:
4927 case R_SH_GOTOFF_LOW16:
4928 case R_SH_GOTOFF_MEDLOW16:
4929 case R_SH_GOTOFF_MEDHI16:
4930 case R_SH_GOTOFF_HI16:
4931 case R_SH_GOTPC_LOW16:
4932 case R_SH_GOTPC_MEDLOW16:
4933 case R_SH_GOTPC_MEDHI16:
4934 case R_SH_GOTPC_HI16:
4935#endif
3376eaf5
KK
4936 case R_SH_TLS_GD_32:
4937 case R_SH_TLS_LD_32:
4938 case R_SH_TLS_IE_32:
2293c499
AM
4939 if (htab->sgot == NULL)
4940 {
4941 if (htab->root.dynobj == NULL)
4942 htab->root.dynobj = abfd;
4943 if (!create_got_section (htab->root.dynobj, info))
4944 return FALSE;
4945 }
37c644f2
AO
4946 break;
4947
4948 default:
4949 break;
4950 }
4951 }
4952
3376eaf5 4953 switch (r_type)
067653c5
AM
4954 {
4955 /* This relocation describes the C++ object vtable hierarchy.
4956 Reconstruct it for later use during GC. */
435b1e90 4957 case R_SH_GNU_VTINHERIT:
c152c796 4958 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4959 return FALSE;
435b1e90
KH
4960 break;
4961
067653c5
AM
4962 /* This relocation describes which C++ vtable entries are actually
4963 used. Record for later use during GC. */
435b1e90 4964 case R_SH_GNU_VTENTRY:
c152c796 4965 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4966 return FALSE;
435b1e90 4967 break;
37c644f2 4968
3376eaf5
KK
4969 case R_SH_TLS_IE_32:
4970 if (info->shared)
4971 info->flags |= DF_STATIC_TLS;
4972
4973 /* FALLTHROUGH */
267fb3c1 4974 force_got:
3376eaf5 4975 case R_SH_TLS_GD_32:
37c644f2 4976 case R_SH_GOT32:
fbca6ad9
AO
4977#ifdef INCLUDE_SHMEDIA
4978 case R_SH_GOT_LOW16:
4979 case R_SH_GOT_MEDLOW16:
4980 case R_SH_GOT_MEDHI16:
4981 case R_SH_GOT_HI16:
4982 case R_SH_GOT10BY4:
4983 case R_SH_GOT10BY8:
4984#endif
3376eaf5
KK
4985 switch (r_type)
4986 {
4987 default:
4988 tls_type = GOT_NORMAL;
4989 break;
4990 case R_SH_TLS_GD_32:
4991 tls_type = GOT_TLS_GD;
4992 break;
4993 case R_SH_TLS_IE_32:
4994 tls_type = GOT_TLS_IE;
4995 break;
4996 }
4997
37c644f2 4998 if (h != NULL)
3376eaf5 4999 {
396a6083
SC
5000#ifdef INCLUDE_SHMEDIA
5001 if (seen_stt_datalabel)
5002 {
b34976b6
AM
5003 struct elf_sh_link_hash_entry *eh
5004 = (struct elf_sh_link_hash_entry *) h;
396a6083
SC
5005
5006 eh->datalabel_got.refcount += 1;
5007 }
5008 else
5009#endif
5010 h->got.refcount += 1;
3376eaf5
KK
5011 old_tls_type = sh_elf_hash_entry (h)->tls_type;
5012 }
37c644f2
AO
5013 else
5014 {
067653c5
AM
5015 bfd_signed_vma *local_got_refcounts;
5016
435b1e90 5017 /* This is a global offset table entry for a local
067653c5
AM
5018 symbol. */
5019 local_got_refcounts = elf_local_got_refcounts (abfd);
5020 if (local_got_refcounts == NULL)
37c644f2 5021 {
dc810e39 5022 bfd_size_type size;
37c644f2 5023
dc810e39 5024 size = symtab_hdr->sh_info;
067653c5 5025 size *= sizeof (bfd_signed_vma);
fbca6ad9
AO
5026#ifdef INCLUDE_SHMEDIA
5027 /* Reserve space for both the datalabel and
5028 codelabel local GOT offsets. */
5029 size *= 2;
5030#endif
3376eaf5 5031 size += symtab_hdr->sh_info;
067653c5
AM
5032 local_got_refcounts = ((bfd_signed_vma *)
5033 bfd_zalloc (abfd, size));
5034 if (local_got_refcounts == NULL)
b34976b6 5035 return FALSE;
067653c5 5036 elf_local_got_refcounts (abfd) = local_got_refcounts;
3376eaf5
KK
5037#ifdef INCLUDE_SHMEDIA
5038 /* Take care of both the datalabel and codelabel local
5039 GOT offsets. */
5040 sh_elf_local_got_tls_type (abfd)
5041 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
5042#else
5043 sh_elf_local_got_tls_type (abfd)
5044 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
5045#endif
37c644f2 5046 }
396a6083
SC
5047#ifdef INCLUDE_SHMEDIA
5048 if (rel->r_addend & 1)
5049 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
5050 else
5051#endif
5052 local_got_refcounts[r_symndx] += 1;
3376eaf5
KK
5053 old_tls_type = sh_elf_local_got_tls_type (abfd) [r_symndx];
5054 }
5055
5056 /* If a TLS symbol is accessed using IE at least once,
5057 there is no point to use dynamic model for it. */
5058 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
5059 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
5060 {
5061 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
5062 tls_type = GOT_TLS_IE;
5063 else
5064 {
5065 (*_bfd_error_handler)
d003868e
AM
5066 (_("%B: `%s' accessed both as normal and thread local symbol"),
5067 abfd, h->root.root.string);
b34976b6 5068 return FALSE;
3376eaf5
KK
5069 }
5070 }
5071
5072 if (old_tls_type != tls_type)
5073 {
5074 if (h != NULL)
5075 sh_elf_hash_entry (h)->tls_type = tls_type;
5076 else
5077 sh_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
37c644f2 5078 }
3376eaf5
KK
5079
5080 break;
5081
5082 case R_SH_TLS_LD_32:
5083 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
37c644f2
AO
5084 break;
5085
fbca6ad9
AO
5086 case R_SH_GOTPLT32:
5087#ifdef INCLUDE_SHMEDIA
5088 case R_SH_GOTPLT_LOW16:
5089 case R_SH_GOTPLT_MEDLOW16:
5090 case R_SH_GOTPLT_MEDHI16:
5091 case R_SH_GOTPLT_HI16:
5092 case R_SH_GOTPLT10BY4:
5093 case R_SH_GOTPLT10BY8:
5094#endif
5095 /* If this is a local symbol, we resolve it directly without
5096 creating a procedure linkage table entry. */
5097
5098 if (h == NULL
f5385ebf 5099 || h->forced_local
fbca6ad9
AO
5100 || ! info->shared
5101 || info->symbolic
067653c5 5102 || h->dynindx == -1)
fbca6ad9
AO
5103 goto force_got;
5104
f5385ebf 5105 h->needs_plt = 1;
067653c5 5106 h->plt.refcount += 1;
4989d792 5107 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
fbca6ad9
AO
5108
5109 break;
5110
37c644f2 5111 case R_SH_PLT32:
fbca6ad9
AO
5112#ifdef INCLUDE_SHMEDIA
5113 case R_SH_PLT_LOW16:
5114 case R_SH_PLT_MEDLOW16:
5115 case R_SH_PLT_MEDHI16:
5116 case R_SH_PLT_HI16:
5117#endif
37c644f2
AO
5118 /* This symbol requires a procedure linkage table entry. We
5119 actually build the entry in adjust_dynamic_symbol,
5120 because this might be a case of linking PIC code which is
5121 never referenced by a dynamic object, in which case we
5122 don't need to generate a procedure linkage table entry
5123 after all. */
5124
5125 /* If this is a local symbol, we resolve it directly without
5126 creating a procedure linkage table entry. */
5127 if (h == NULL)
5128 continue;
5129
f5385ebf 5130 if (h->forced_local)
37c644f2
AO
5131 break;
5132
f5385ebf 5133 h->needs_plt = 1;
067653c5 5134 h->plt.refcount += 1;
37c644f2
AO
5135 break;
5136
5137 case R_SH_DIR32:
5138 case R_SH_REL32:
46e993b9
KK
5139#ifdef INCLUDE_SHMEDIA
5140 case R_SH_IMM_LOW16_PCREL:
5141 case R_SH_IMM_MEDLOW16_PCREL:
5142 case R_SH_IMM_MEDHI16_PCREL:
5143 case R_SH_IMM_HI16_PCREL:
5144#endif
067653c5
AM
5145 if (h != NULL && ! info->shared)
5146 {
f5385ebf 5147 h->non_got_ref = 1;
067653c5
AM
5148 h->plt.refcount += 1;
5149 }
37c644f2
AO
5150
5151 /* If we are creating a shared library, and this is a reloc
5152 against a global symbol, or a non PC relative reloc
5153 against a local symbol, then we need to copy the reloc
5154 into the shared library. However, if we are linking with
5155 -Bsymbolic, we do not need to copy a reloc against a
5156 global symbol which is defined in an object we are
5157 including in the link (i.e., DEF_REGULAR is set). At
5158 this point we have not seen all the input files, so it is
5159 possible that DEF_REGULAR is not set now but will be set
5160 later (it is never cleared). We account for that
5161 possibility below by storing information in the
067653c5
AM
5162 dyn_relocs field of the hash table entry. A similar
5163 situation occurs when creating shared libraries and symbol
5164 visibility changes render the symbol local.
5165
5166 If on the other hand, we are creating an executable, we
5167 may need to keep relocations for symbols satisfied by a
5168 dynamic library if we manage to avoid copy relocs for the
5169 symbol. */
5170 if ((info->shared
5171 && (sec->flags & SEC_ALLOC) != 0
3376eaf5 5172 && (r_type != R_SH_REL32
067653c5
AM
5173 || (h != NULL
5174 && (! info->symbolic
5175 || h->root.type == bfd_link_hash_defweak
f5385ebf 5176 || !h->def_regular))))
067653c5
AM
5177 || (! info->shared
5178 && (sec->flags & SEC_ALLOC) != 0
5179 && h != NULL
5180 && (h->root.type == bfd_link_hash_defweak
f5385ebf 5181 || !h->def_regular)))
37c644f2 5182 {
067653c5
AM
5183 struct elf_sh_dyn_relocs *p;
5184 struct elf_sh_dyn_relocs **head;
5185
2293c499
AM
5186 if (htab->root.dynobj == NULL)
5187 htab->root.dynobj = abfd;
067653c5 5188
37c644f2
AO
5189 /* When creating a shared object, we must copy these
5190 reloc types into the output file. We create a reloc
5191 section in dynobj and make room for this reloc. */
5192 if (sreloc == NULL)
5193 {
5194 const char *name;
5195
5196 name = (bfd_elf_string_from_elf_section
5197 (abfd,
5198 elf_elfheader (abfd)->e_shstrndx,
5199 elf_section_data (sec)->rel_hdr.sh_name));
5200 if (name == NULL)
b34976b6 5201 return FALSE;
37c644f2
AO
5202
5203 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5204 && strcmp (bfd_get_section_name (abfd, sec),
5205 name + 5) == 0);
5206
2293c499 5207 sreloc = bfd_get_section_by_name (htab->root.dynobj, name);
37c644f2
AO
5208 if (sreloc == NULL)
5209 {
5210 flagword flags;
5211
37c644f2
AO
5212 flags = (SEC_HAS_CONTENTS | SEC_READONLY
5213 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
5214 if ((sec->flags & SEC_ALLOC) != 0)
5215 flags |= SEC_ALLOC | SEC_LOAD;
3496cb2a
L
5216 sreloc = bfd_make_section_with_flags (htab->root.dynobj,
5217 name,
5218 flags);
37c644f2 5219 if (sreloc == NULL
2293c499
AM
5220 || ! bfd_set_section_alignment (htab->root.dynobj,
5221 sreloc, 2))
b34976b6 5222 return FALSE;
37c644f2 5223 }
067653c5 5224 elf_section_data (sec)->sreloc = sreloc;
37c644f2
AO
5225 }
5226
067653c5
AM
5227 /* If this is a global symbol, we count the number of
5228 relocations we need for this symbol. */
5229 if (h != NULL)
5230 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
5231 else
37c644f2 5232 {
067653c5 5233 asection *s;
6edfbbad 5234 void *vpp;
37c644f2 5235
067653c5
AM
5236 /* Track dynamic relocs needed for local syms too. */
5237 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
5238 sec, r_symndx);
5239 if (s == NULL)
b34976b6 5240 return FALSE;
37c644f2 5241
6edfbbad
DJ
5242 vpp = &elf_section_data (s)->local_dynrel;
5243 head = (struct elf_sh_dyn_relocs **) vpp;
067653c5 5244 }
37c644f2 5245
067653c5
AM
5246 p = *head;
5247 if (p == NULL || p->sec != sec)
5248 {
5249 bfd_size_type amt = sizeof (*p);
2293c499 5250 p = bfd_alloc (htab->root.dynobj, amt);
37c644f2 5251 if (p == NULL)
b34976b6 5252 return FALSE;
067653c5
AM
5253 p->next = *head;
5254 *head = p;
5255 p->sec = sec;
5256 p->count = 0;
5257 p->pc_count = 0;
37c644f2 5258 }
067653c5
AM
5259
5260 p->count += 1;
46e993b9
KK
5261 if (r_type == R_SH_REL32
5262#ifdef INCLUDE_SHMEDIA
5263 || r_type == R_SH_IMM_LOW16_PCREL
5264 || r_type == R_SH_IMM_MEDLOW16_PCREL
5265 || r_type == R_SH_IMM_MEDHI16_PCREL
5266 || r_type == R_SH_IMM_HI16_PCREL
5267#endif
5268 )
067653c5 5269 p->pc_count += 1;
37c644f2
AO
5270 }
5271
5272 break;
3376eaf5
KK
5273
5274 case R_SH_TLS_LE_32:
5275 if (info->shared)
5276 {
d003868e
AM
5277 (*_bfd_error_handler)
5278 (_("%B: TLS local exec code cannot be linked into shared objects"),
5279 abfd);
b34976b6 5280 return FALSE;
3376eaf5
KK
5281 }
5282
267fb3c1 5283 break;
3376eaf5 5284
3376eaf5 5285 case R_SH_TLS_LDO_32:
267fb3c1 5286 /* Nothing to do. */
3376eaf5
KK
5287 break;
5288
5289 default:
5290 break;
435b1e90 5291 }
252b5132 5292 }
435b1e90 5293
b34976b6 5294 return TRUE;
252b5132
RH
5295}
5296
fbca6ad9 5297#ifndef sh_elf_set_mach_from_flags
f6f9408f
JR
5298static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
5299
b34976b6 5300static bfd_boolean
09fd220b 5301sh_elf_set_mach_from_flags (bfd *abfd)
d4845d57 5302{
f6f9408f
JR
5303 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
5304
5305 if (flags >= sizeof(sh_ef_bfd_table))
5306 return FALSE;
5307
5308 if (sh_ef_bfd_table[flags] == 0)
5309 return FALSE;
5310
5311 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
d4845d57 5312
b34976b6 5313 return TRUE;
d4845d57 5314}
f6f9408f
JR
5315
5316
5317/* Reverse table lookup for sh_ef_bfd_table[].
5318 Given a bfd MACH value from archures.c
5319 return the equivalent ELF flags from the table.
5320 Return -1 if no match is found. */
5321
5322int
5323sh_elf_get_flags_from_mach (unsigned long mach)
5324{
4195f552 5325 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
f6f9408f
JR
5326
5327 for (; i>0; i--)
5328 if (sh_ef_bfd_table[i] == mach)
5329 return i;
5330
5331 /* shouldn't get here */
5332 BFD_FAIL();
5333
5334 return -1;
5335}
fbca6ad9 5336#endif /* not sh_elf_set_mach_from_flags */
d4845d57 5337
fbca6ad9 5338#ifndef sh_elf_set_private_flags
435b1e90
KH
5339/* Function to keep SH specific file flags. */
5340
b34976b6 5341static bfd_boolean
09fd220b 5342sh_elf_set_private_flags (bfd *abfd, flagword flags)
d4845d57
JR
5343{
5344 BFD_ASSERT (! elf_flags_init (abfd)
5345 || elf_elfheader (abfd)->e_flags == flags);
5346
5347 elf_elfheader (abfd)->e_flags = flags;
b34976b6 5348 elf_flags_init (abfd) = TRUE;
d4845d57
JR
5349 return sh_elf_set_mach_from_flags (abfd);
5350}
fbca6ad9 5351#endif /* not sh_elf_set_private_flags */
d4845d57 5352
fbca6ad9 5353#ifndef sh_elf_copy_private_data
d4845d57 5354/* Copy backend specific data from one object module to another */
435b1e90 5355
b34976b6 5356static bfd_boolean
09fd220b 5357sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
d4845d57 5358{
38b1a46c 5359 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
d4845d57 5360 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 5361 return TRUE;
d4845d57
JR
5362
5363 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
5364}
fbca6ad9 5365#endif /* not sh_elf_copy_private_data */
d4845d57 5366
fbca6ad9 5367#ifndef sh_elf_merge_private_data
d4845d57 5368
f6f9408f
JR
5369/* This function returns the ELF architecture number that
5370 corresponds to the given arch_sh* flags. */
85fbca6a 5371
f6f9408f
JR
5372int
5373sh_find_elf_flags (unsigned int arch_set)
d4845d57 5374{
85fbca6a 5375 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
f6f9408f 5376 unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
d4845d57 5377
f6f9408f
JR
5378 return sh_elf_get_flags_from_mach (bfd_mach);
5379}
f6f9408f
JR
5380
5381/* This routine initialises the elf flags when required and
5382 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
d4845d57 5383
f6f9408f
JR
5384static bfd_boolean
5385sh_elf_merge_private_data (bfd *ibfd, bfd *obfd)
5386{
85fbca6a
NC
5387 extern bfd_boolean sh_merge_bfd_arch (bfd *, bfd *);
5388
d4845d57
JR
5389 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5390 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 5391 return TRUE;
d4845d57
JR
5392
5393 if (! elf_flags_init (obfd))
5394 {
a39b79b9 5395 /* This happens when ld starts out with a 'blank' output file. */
b34976b6 5396 elf_flags_init (obfd) = TRUE;
a39b79b9 5397 elf_elfheader (obfd)->e_flags = EF_SH1;
f6f9408f 5398 sh_elf_set_mach_from_flags (obfd);
d4845d57 5399 }
d4845d57 5400
85fbca6a 5401 if (! sh_merge_bfd_arch (ibfd, obfd))
1d70c7fb 5402 {
d003868e 5403 _bfd_error_handler ("%B: uses instructions which are incompatible "
1d70c7fb 5404 "with instructions used in previous modules",
d003868e 5405 ibfd);
1d70c7fb
AO
5406 bfd_set_error (bfd_error_bad_value);
5407 return FALSE;
5408 }
f6f9408f
JR
5409
5410 elf_elfheader (obfd)->e_flags =
5411 sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
5412
5413 return TRUE;
d4845d57 5414}
fbca6ad9 5415#endif /* not sh_elf_merge_private_data */
d4845d57 5416
3376eaf5
KK
5417/* Override the generic function because we need to store sh_elf_obj_tdata
5418 as the specific tdata. We set also the machine architecture from flags
5419 here. */
5420
b34976b6 5421static bfd_boolean
09fd220b 5422sh_elf_object_p (bfd *abfd)
3376eaf5 5423{
66becf32 5424 return sh_elf_set_mach_from_flags (abfd);
3376eaf5
KK
5425}
5426
37c644f2
AO
5427/* Finish up dynamic symbol handling. We set the contents of various
5428 dynamic sections here. */
5429
b34976b6 5430static bfd_boolean
09fd220b
KK
5431sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
5432 struct elf_link_hash_entry *h,
5433 Elf_Internal_Sym *sym)
37c644f2 5434{
067653c5 5435 struct elf_sh_link_hash_table *htab;
37c644f2 5436
067653c5 5437 htab = sh_elf_hash_table (info);
37c644f2
AO
5438
5439 if (h->plt.offset != (bfd_vma) -1)
5440 {
5441 asection *splt;
5442 asection *sgot;
5443 asection *srel;
5444
5445 bfd_vma plt_index;
5446 bfd_vma got_offset;
5447 Elf_Internal_Rela rel;
947216bf 5448 bfd_byte *loc;
37c644f2
AO
5449
5450 /* This symbol has an entry in the procedure linkage table. Set
5451 it up. */
5452
5453 BFD_ASSERT (h->dynindx != -1);
5454
067653c5
AM
5455 splt = htab->splt;
5456 sgot = htab->sgotplt;
5457 srel = htab->srelplt;
37c644f2
AO
5458 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
5459
5460 /* Get the index in the procedure linkage table which
5461 corresponds to this symbol. This is the index of this symbol
5462 in all the symbols for which we are making plt entries. The
5463 first entry in the procedure linkage table is reserved. */
55e6e397 5464 plt_index = get_plt_index (htab->plt_info, h->plt.offset);
37c644f2
AO
5465
5466 /* Get the offset into the .got table of the entry that
5467 corresponds to this function. Each .got entry is 4 bytes.
5468 The first three are reserved. */
5469 got_offset = (plt_index + 3) * 4;
5470
fbca6ad9 5471#ifdef GOT_BIAS
c8614e8e
AM
5472 if (info->shared)
5473 got_offset -= GOT_BIAS;
fbca6ad9
AO
5474#endif
5475
37c644f2 5476 /* Fill in the entry in the procedure linkage table. */
55e6e397
RS
5477 memcpy (splt->contents + h->plt.offset,
5478 htab->plt_info->symbol_entry,
5479 htab->plt_info->symbol_entry_size);
5480
5481 if (info->shared)
5482 install_plt_field (output_bfd, FALSE, got_offset,
5483 (splt->contents
5484 + h->plt.offset
5485 + htab->plt_info->symbol_fields.got_entry));
5486 else
37c644f2 5487 {
55e6e397 5488 install_plt_field (output_bfd, FALSE,
fbca6ad9
AO
5489 (sgot->output_section->vma
5490 + sgot->output_offset
5491 + got_offset),
55e6e397
RS
5492 (splt->contents
5493 + h->plt.offset
5494 + htab->plt_info->symbol_fields.got_entry));
5495 if (htab->vxworks_p)
37c644f2 5496 {
55e6e397
RS
5497 unsigned int reachable_plts, plts_per_4k;
5498 int distance;
5499
5500 /* Divide the PLT into groups. The first group contains
5501 REACHABLE_PLTS entries and the other groups contain
5502 PLTS_PER_4K entries. Entries in the first group can
5503 branch directly to .plt; those in later groups branch
5504 to the last element of the previous group. */
5505 /* ??? It would be better to create multiple copies of
5506 the common resolver stub. */
5507 reachable_plts = ((4096
5508 - htab->plt_info->plt0_entry_size
5509 - (htab->plt_info->symbol_fields.plt + 4))
5510 / htab->plt_info->symbol_entry_size) + 1;
5511 plts_per_4k = (4096 / htab->plt_info->symbol_entry_size);
5512 if (plt_index < reachable_plts)
5513 distance = -(h->plt.offset
5514 + htab->plt_info->symbol_fields.plt);
5515 else
5516 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
5517 * htab->plt_info->symbol_entry_size);
5518
5519 /* Install the 'bra' with this offset. */
5520 bfd_put_16 (output_bfd,
5521 0xa000 | (0x0fff & ((distance - 4) / 2)),
5522 (splt->contents
5523 + h->plt.offset
5524 + htab->plt_info->symbol_fields.plt));
37c644f2 5525 }
55e6e397
RS
5526 else
5527 install_plt_field (output_bfd, TRUE,
5528 splt->output_section->vma + splt->output_offset,
5529 (splt->contents
5530 + h->plt.offset
5531 + htab->plt_info->symbol_fields.plt));
37c644f2
AO
5532 }
5533
fbca6ad9 5534#ifdef GOT_BIAS
c8614e8e
AM
5535 if (info->shared)
5536 got_offset += GOT_BIAS;
fbca6ad9
AO
5537#endif
5538
55e6e397 5539 install_plt_field (output_bfd, FALSE,
fbca6ad9 5540 plt_index * sizeof (Elf32_External_Rela),
55e6e397
RS
5541 (splt->contents
5542 + h->plt.offset
5543 + htab->plt_info->symbol_fields.reloc_offset));
37c644f2
AO
5544
5545 /* Fill in the entry in the global offset table. */
5546 bfd_put_32 (output_bfd,
5547 (splt->output_section->vma
5548 + splt->output_offset
5549 + h->plt.offset
55e6e397 5550 + htab->plt_info->symbol_resolve_offset),
37c644f2
AO
5551 sgot->contents + got_offset);
5552
5553 /* Fill in the entry in the .rela.plt section. */
5554 rel.r_offset = (sgot->output_section->vma
5555 + sgot->output_offset
5556 + got_offset);
5557 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
5558 rel.r_addend = 0;
fbca6ad9
AO
5559#ifdef GOT_BIAS
5560 rel.r_addend = GOT_BIAS;
5561#endif
947216bf
AM
5562 loc = srel->contents + plt_index * sizeof (Elf32_External_Rela);
5563 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2 5564
55e6e397
RS
5565 if (htab->vxworks_p && !info->shared)
5566 {
5567 /* Create the .rela.plt.unloaded relocations for this PLT entry.
5568 Begin by pointing LOC to the first such relocation. */
5569 loc = (htab->srelplt2->contents
5570 + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
5571
5572 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
5573 for the PLT entry's pointer to the .got.plt entry. */
5574 rel.r_offset = (htab->splt->output_section->vma
5575 + htab->splt->output_offset
5576 + h->plt.offset
5577 + htab->plt_info->symbol_fields.got_entry);
5578 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
5579 rel.r_addend = got_offset;
5580 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
5581 loc += sizeof (Elf32_External_Rela);
5582
5583 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
5584 the .got.plt entry, which initially points to .plt. */
5585 rel.r_offset = (htab->sgotplt->output_section->vma
5586 + htab->sgotplt->output_offset
5587 + got_offset);
5588 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
5589 rel.r_addend = 0;
5590 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5591 }
5592
f5385ebf 5593 if (!h->def_regular)
37c644f2
AO
5594 {
5595 /* Mark the symbol as undefined, rather than as defined in
5596 the .plt section. Leave the value alone. */
5597 sym->st_shndx = SHN_UNDEF;
5598 }
5599 }
5600
3376eaf5
KK
5601 if (h->got.offset != (bfd_vma) -1
5602 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_GD
5603 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_IE)
37c644f2
AO
5604 {
5605 asection *sgot;
5606 asection *srel;
5607 Elf_Internal_Rela rel;
947216bf 5608 bfd_byte *loc;
37c644f2
AO
5609
5610 /* This symbol has an entry in the global offset table. Set it
5611 up. */
5612
067653c5
AM
5613 sgot = htab->sgot;
5614 srel = htab->srelgot;
37c644f2
AO
5615 BFD_ASSERT (sgot != NULL && srel != NULL);
5616
5617 rel.r_offset = (sgot->output_section->vma
5618 + sgot->output_offset
dc810e39 5619 + (h->got.offset &~ (bfd_vma) 1));
37c644f2 5620
067653c5
AM
5621 /* If this is a static link, or it is a -Bsymbolic link and the
5622 symbol is defined locally or was forced to be local because
5623 of a version file, we just want to emit a RELATIVE reloc.
37c644f2
AO
5624 The entry in the global offset table will already have been
5625 initialized in the relocate_section function. */
5626 if (info->shared
3d85aebe 5627 && SYMBOL_REFERENCES_LOCAL (info, h))
37c644f2
AO
5628 {
5629 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
5630 rel.r_addend = (h->root.u.def.value
5631 + h->root.u.def.section->output_section->vma
5632 + h->root.u.def.section->output_offset);
5633 }
5634 else
5635 {
5636 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5637 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
5638 rel.r_addend = 0;
5639 }
5640
947216bf
AM
5641 loc = srel->contents;
5642 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
5643 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2
AO
5644 }
5645
396a6083
SC
5646#ifdef INCLUDE_SHMEDIA
5647 {
5648 struct elf_sh_link_hash_entry *eh;
5649
5650 eh = (struct elf_sh_link_hash_entry *) h;
5651 if (eh->datalabel_got.offset != (bfd_vma) -1)
5652 {
5653 asection *sgot;
5654 asection *srel;
5655 Elf_Internal_Rela rel;
947216bf 5656 bfd_byte *loc;
396a6083
SC
5657
5658 /* This symbol has a datalabel entry in the global offset table.
5659 Set it up. */
5660
5661 sgot = htab->sgot;
5662 srel = htab->srelgot;
5663 BFD_ASSERT (sgot != NULL && srel != NULL);
5664
5665 rel.r_offset = (sgot->output_section->vma
5666 + sgot->output_offset
5667 + (eh->datalabel_got.offset &~ (bfd_vma) 1));
5668
5669 /* If this is a static link, or it is a -Bsymbolic link and the
5670 symbol is defined locally or was forced to be local because
5671 of a version file, we just want to emit a RELATIVE reloc.
5672 The entry in the global offset table will already have been
5673 initialized in the relocate_section function. */
5674 if (info->shared
3d85aebe 5675 && SYMBOL_REFERENCES_LOCAL (info, h))
396a6083
SC
5676 {
5677 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
5678 rel.r_addend = (h->root.u.def.value
5679 + h->root.u.def.section->output_section->vma
5680 + h->root.u.def.section->output_offset);
5681 }
5682 else
5683 {
5684 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
5685 + eh->datalabel_got.offset);
5686 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
5687 rel.r_addend = 0;
5688 }
5689
947216bf
AM
5690 loc = srel->contents;
5691 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
5692 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
396a6083
SC
5693 }
5694 }
5695#endif
5696
f5385ebf 5697 if (h->needs_copy)
37c644f2
AO
5698 {
5699 asection *s;
5700 Elf_Internal_Rela rel;
947216bf 5701 bfd_byte *loc;
37c644f2
AO
5702
5703 /* This symbol needs a copy reloc. Set it up. */
5704
5705 BFD_ASSERT (h->dynindx != -1
5706 && (h->root.type == bfd_link_hash_defined
5707 || h->root.type == bfd_link_hash_defweak));
5708
5709 s = bfd_get_section_by_name (h->root.u.def.section->owner,
5710 ".rela.bss");
5711 BFD_ASSERT (s != NULL);
5712
5713 rel.r_offset = (h->root.u.def.value
5714 + h->root.u.def.section->output_section->vma
5715 + h->root.u.def.section->output_offset);
5716 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
5717 rel.r_addend = 0;
947216bf
AM
5718 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5719 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2
AO
5720 }
5721
55e6e397
RS
5722 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
5723 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
5724 ".got" section. */
37c644f2 5725 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
55e6e397 5726 || (!htab->vxworks_p && h == htab->root.hgot))
37c644f2
AO
5727 sym->st_shndx = SHN_ABS;
5728
b34976b6 5729 return TRUE;
37c644f2
AO
5730}
5731
5732/* Finish up the dynamic sections. */
5733
b34976b6 5734static bfd_boolean
09fd220b 5735sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
37c644f2 5736{
067653c5 5737 struct elf_sh_link_hash_table *htab;
37c644f2
AO
5738 asection *sgot;
5739 asection *sdyn;
5740
067653c5 5741 htab = sh_elf_hash_table (info);
067653c5 5742 sgot = htab->sgotplt;
2293c499 5743 sdyn = bfd_get_section_by_name (htab->root.dynobj, ".dynamic");
37c644f2 5744
067653c5 5745 if (htab->root.dynamic_sections_created)
37c644f2
AO
5746 {
5747 asection *splt;
5748 Elf32_External_Dyn *dyncon, *dynconend;
5749
067653c5 5750 BFD_ASSERT (sgot != NULL && sdyn != NULL);
37c644f2
AO
5751
5752 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 5753 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
37c644f2
AO
5754 for (; dyncon < dynconend; dyncon++)
5755 {
5756 Elf_Internal_Dyn dyn;
37c644f2 5757 asection *s;
067653c5
AM
5758#ifdef INCLUDE_SHMEDIA
5759 const char *name;
5760#endif
37c644f2 5761
2293c499 5762 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
37c644f2
AO
5763
5764 switch (dyn.d_tag)
5765 {
5766 default:
5767 break;
5768
3b587c71
AM
5769#ifdef INCLUDE_SHMEDIA
5770 case DT_INIT:
5771 name = info->init_function;
5772 goto get_sym;
5773
5774 case DT_FINI:
5775 name = info->fini_function;
5776 get_sym:
5777 if (dyn.d_un.d_val != 0)
5778 {
067653c5
AM
5779 struct elf_link_hash_entry *h;
5780
5781 h = elf_link_hash_lookup (&htab->root, name,
b34976b6 5782 FALSE, FALSE, TRUE);
3b587c71
AM
5783 if (h != NULL && (h->other & STO_SH5_ISA32))
5784 {
5785 dyn.d_un.d_val |= 1;
5786 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5787 }
5788 }
5789 break;
5790#endif
5791
37c644f2 5792 case DT_PLTGOT:
067653c5 5793 s = htab->sgot->output_section;
37c644f2
AO
5794 goto get_vma;
5795
5796 case DT_JMPREL:
067653c5 5797 s = htab->srelplt->output_section;
37c644f2 5798 get_vma:
37c644f2
AO
5799 BFD_ASSERT (s != NULL);
5800 dyn.d_un.d_ptr = s->vma;
5801 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5802 break;
5803
5804 case DT_PLTRELSZ:
067653c5 5805 s = htab->srelplt->output_section;
37c644f2 5806 BFD_ASSERT (s != NULL);
eea6121a 5807 dyn.d_un.d_val = s->size;
37c644f2
AO
5808 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5809 break;
5810
5811 case DT_RELASZ:
5812 /* My reading of the SVR4 ABI indicates that the
5813 procedure linkage table relocs (DT_JMPREL) should be
5814 included in the overall relocs (DT_RELA). This is
5815 what Solaris does. However, UnixWare can not handle
5816 that case. Therefore, we override the DT_RELASZ entry
5817 here to make it not include the JMPREL relocs. Since
5818 the linker script arranges for .rela.plt to follow all
5819 other relocation sections, we don't have to worry
5820 about changing the DT_RELA entry. */
067653c5 5821 if (htab->srelplt != NULL)
37c644f2 5822 {
067653c5 5823 s = htab->srelplt->output_section;
eea6121a 5824 dyn.d_un.d_val -= s->size;
37c644f2
AO
5825 }
5826 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5827 break;
5828 }
5829 }
5830
5831 /* Fill in the first entry in the procedure linkage table. */
067653c5 5832 splt = htab->splt;
55e6e397 5833 if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
37c644f2 5834 {
55e6e397
RS
5835 unsigned int i;
5836
5837 memcpy (splt->contents,
5838 htab->plt_info->plt0_entry,
5839 htab->plt_info->plt0_entry_size);
5840 for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
5841 if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
5842 install_plt_field (output_bfd, FALSE,
5843 (sgot->output_section->vma
5844 + sgot->output_offset
5845 + (i * 4)),
5846 (splt->contents
5847 + htab->plt_info->plt0_got_fields[i]));
5848
5849 if (htab->vxworks_p)
37c644f2 5850 {
55e6e397
RS
5851 /* Finalize the .rela.plt.unloaded contents. */
5852 Elf_Internal_Rela rel;
5853 bfd_byte *loc;
5854
5855 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
5856 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
5857 loc = htab->srelplt2->contents;
5858 rel.r_offset = (splt->output_section->vma
5859 + splt->output_offset
5860 + htab->plt_info->plt0_got_fields[2]);
5861 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
5862 rel.r_addend = 8;
5863 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
5864 loc += sizeof (Elf32_External_Rela);
5865
5866 /* Fix up the remaining .rela.plt.unloaded relocations.
5867 They may have the wrong symbol index for _G_O_T_ or
5868 _P_L_T_ depending on the order in which symbols were
5869 output. */
5870 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
37c644f2 5871 {
55e6e397
RS
5872 /* The PLT entry's pointer to the .got.plt slot. */
5873 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
5874 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
5875 R_SH_DIR32);
5876 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5877 loc += sizeof (Elf32_External_Rela);
5878
5879 /* The .got.plt slot's pointer to .plt. */
5880 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
5881 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
5882 R_SH_DIR32);
5883 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5884 loc += sizeof (Elf32_External_Rela);
37c644f2 5885 }
37c644f2
AO
5886 }
5887
5888 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5889 really seem like the right value. */
5890 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
5891 }
5892 }
5893
5894 /* Fill in the first three entries in the global offset table. */
eea6121a 5895 if (sgot && sgot->size > 0)
37c644f2
AO
5896 {
5897 if (sdyn == NULL)
5898 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5899 else
5900 bfd_put_32 (output_bfd,
5901 sdyn->output_section->vma + sdyn->output_offset,
5902 sgot->contents);
5903 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5904 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
37c644f2 5905
067653c5
AM
5906 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5907 }
37c644f2 5908
b34976b6 5909 return TRUE;
37c644f2
AO
5910}
5911
99e4ae17 5912static enum elf_reloc_type_class
09fd220b 5913sh_elf_reloc_type_class (const Elf_Internal_Rela *rela)
99e4ae17 5914{
f51e552e 5915 switch ((int) ELF32_R_TYPE (rela->r_info))
99e4ae17
AJ
5916 {
5917 case R_SH_RELATIVE:
5918 return reloc_class_relative;
5919 case R_SH_JMP_SLOT:
5920 return reloc_class_plt;
5921 case R_SH_COPY:
5922 return reloc_class_copy;
5923 default:
5924 return reloc_class_normal;
5925 }
5926}
5927
85fbca6a 5928#if !defined SH_TARGET_ALREADY_DEFINED
571fe01f 5929/* Support for Linux core dump NOTE sections. */
85fbca6a 5930
b34976b6 5931static bfd_boolean
09fd220b 5932elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2bc3c89a
AM
5933{
5934 int offset;
eea6121a 5935 unsigned int size;
2bc3c89a
AM
5936
5937 switch (note->descsz)
5938 {
5939 default:
b34976b6 5940 return FALSE;
2bc3c89a
AM
5941
5942 case 168: /* Linux/SH */
5943 /* pr_cursig */
5944 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
5945
5946 /* pr_pid */
5947 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
5948
5949 /* pr_reg */
5950 offset = 72;
eea6121a 5951 size = 92;
2bc3c89a
AM
5952
5953 break;
5954 }
5955
5956 /* Make a ".reg/999" section. */
5957 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 5958 size, note->descpos + offset);
2bc3c89a
AM
5959}
5960
b34976b6 5961static bfd_boolean
09fd220b 5962elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2bc3c89a
AM
5963{
5964 switch (note->descsz)
5965 {
5966 default:
b34976b6 5967 return FALSE;
2bc3c89a
AM
5968
5969 case 124: /* Linux/SH elf_prpsinfo */
5970 elf_tdata (abfd)->core_program
5971 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
5972 elf_tdata (abfd)->core_command
5973 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
5974 }
5975
5976 /* Note that for some reason, a spurious space is tacked
5977 onto the end of the args in some (at least one anyway)
5978 implementations, so strip it off if it exists. */
5979
5980 {
5981 char *command = elf_tdata (abfd)->core_command;
5982 int n = strlen (command);
5983
5984 if (0 < n && command[n - 1] == ' ')
5985 command[n - 1] = '\0';
5986 }
5987
b34976b6 5988 return TRUE;
2bc3c89a 5989}
85fbca6a 5990#endif /* not SH_TARGET_ALREADY_DEFINED */
2bc3c89a 5991
1f1799d5
KK
5992
5993/* Return address for Ith PLT stub in section PLT, for relocation REL
5994 or (bfd_vma) -1 if it should not be included. */
5995
5996static bfd_vma
5997sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
5998 const arelent *rel ATTRIBUTE_UNUSED)
5999{
55e6e397
RS
6000 const struct elf_sh_plt_info *plt_info;
6001
6002 plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
6003 return plt->vma + get_plt_offset (plt_info, i);
1f1799d5
KK
6004}
6005
85fbca6a 6006#if !defined SH_TARGET_ALREADY_DEFINED
252b5132
RH
6007#define TARGET_BIG_SYM bfd_elf32_sh_vec
6008#define TARGET_BIG_NAME "elf32-sh"
6009#define TARGET_LITTLE_SYM bfd_elf32_shl_vec
6010#define TARGET_LITTLE_NAME "elf32-shl"
85fbca6a
NC
6011#endif
6012
252b5132
RH
6013#define ELF_ARCH bfd_arch_sh
6014#define ELF_MACHINE_CODE EM_SH
d0facd1b
NC
6015#ifdef __QNXTARGET__
6016#define ELF_MAXPAGESIZE 0x1000
6017#else
6018#define ELF_MAXPAGESIZE 0x80
6019#endif
252b5132
RH
6020
6021#define elf_symbol_leading_char '_'
6022
6023#define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6024#define elf_info_to_howto sh_elf_info_to_howto
6025#define bfd_elf32_bfd_relax_section sh_elf_relax_section
6026#define elf_backend_relocate_section sh_elf_relocate_section
6027#define bfd_elf32_bfd_get_relocated_section_contents \
6028 sh_elf_get_relocated_section_contents
3376eaf5
KK
6029#define bfd_elf32_mkobject sh_elf_mkobject
6030#define elf_backend_object_p sh_elf_object_p
d4845d57
JR
6031#define bfd_elf32_bfd_set_private_bfd_flags \
6032 sh_elf_set_private_flags
6033#define bfd_elf32_bfd_copy_private_bfd_data \
6034 sh_elf_copy_private_data
875f7f69 6035#define bfd_elf32_bfd_merge_private_bfd_data \
d4845d57 6036 sh_elf_merge_private_data
252b5132 6037
067653c5
AM
6038#define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
6039#define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
6040#define elf_backend_check_relocs sh_elf_check_relocs
6041#define elf_backend_copy_indirect_symbol \
6042 sh_elf_copy_indirect_symbol
37c644f2
AO
6043#define elf_backend_create_dynamic_sections \
6044 sh_elf_create_dynamic_sections
6045#define bfd_elf32_bfd_link_hash_table_create \
6046 sh_elf_link_hash_table_create
6047#define elf_backend_adjust_dynamic_symbol \
6048 sh_elf_adjust_dynamic_symbol
55e6e397
RS
6049#define elf_backend_always_size_sections \
6050 sh_elf_always_size_sections
37c644f2
AO
6051#define elf_backend_size_dynamic_sections \
6052 sh_elf_size_dynamic_sections
6053#define elf_backend_finish_dynamic_symbol \
6054 sh_elf_finish_dynamic_symbol
6055#define elf_backend_finish_dynamic_sections \
6056 sh_elf_finish_dynamic_sections
99e4ae17 6057#define elf_backend_reloc_type_class sh_elf_reloc_type_class
1f1799d5 6058#define elf_backend_plt_sym_val sh_elf_plt_sym_val
37c644f2 6059
067653c5
AM
6060#define elf_backend_can_gc_sections 1
6061#define elf_backend_can_refcount 1
37c644f2
AO
6062#define elf_backend_want_got_plt 1
6063#define elf_backend_plt_readonly 1
6064#define elf_backend_want_plt_sym 0
6065#define elf_backend_got_header_size 12
ed71e111 6066
85fbca6a 6067#if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
2bc3c89a 6068
252b5132 6069#include "elf32-target.h"
ed71e111 6070
2bc3c89a
AM
6071/* NetBSD support. */
6072#undef TARGET_BIG_SYM
6073#define TARGET_BIG_SYM bfd_elf32_shnbsd_vec
6074#undef TARGET_BIG_NAME
6075#define TARGET_BIG_NAME "elf32-sh-nbsd"
6076#undef TARGET_LITTLE_SYM
6077#define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec
6078#undef TARGET_LITTLE_NAME
6079#define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6080#undef ELF_MAXPAGESIZE
6081#define ELF_MAXPAGESIZE 0x10000
24718e3b 6082#undef ELF_COMMONPAGESIZE
2bc3c89a
AM
6083#undef elf_symbol_leading_char
6084#define elf_symbol_leading_char 0
571fe01f 6085#undef elf32_bed
2bc3c89a
AM
6086#define elf32_bed elf32_sh_nbsd_bed
6087
6088#include "elf32-target.h"
6089
2bc3c89a
AM
6090
6091/* Linux support. */
6092#undef TARGET_BIG_SYM
6093#define TARGET_BIG_SYM bfd_elf32_shblin_vec
6094#undef TARGET_BIG_NAME
6095#define TARGET_BIG_NAME "elf32-shbig-linux"
6096#undef TARGET_LITTLE_SYM
6097#define TARGET_LITTLE_SYM bfd_elf32_shlin_vec
6098#undef TARGET_LITTLE_NAME
6099#define TARGET_LITTLE_NAME "elf32-sh-linux"
24718e3b
L
6100#undef ELF_COMMONPAGESIZE
6101#define ELF_COMMONPAGESIZE 0x1000
2bc3c89a
AM
6102
6103#undef elf_backend_grok_prstatus
6104#define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6105#undef elf_backend_grok_psinfo
6106#define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
571fe01f 6107#undef elf32_bed
2bc3c89a
AM
6108#define elf32_bed elf32_sh_lin_bed
6109
6110#include "elf32-target.h"
6111
55e6e397
RS
6112#undef TARGET_BIG_SYM
6113#define TARGET_BIG_SYM bfd_elf32_shvxworks_vec
6114#undef TARGET_BIG_NAME
6115#define TARGET_BIG_NAME "elf32-sh-vxworks"
6116#undef TARGET_LITTLE_SYM
6117#define TARGET_LITTLE_SYM bfd_elf32_shlvxworks_vec
6118#undef TARGET_LITTLE_NAME
6119#define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6120#undef elf32_bed
6121#define elf32_bed elf32_sh_vxworks_bed
6122
6123#undef elf_backend_want_plt_sym
6124#define elf_backend_want_plt_sym 1
6125#undef elf_symbol_leading_char
6126#define elf_symbol_leading_char '_'
6127#define elf_backend_want_got_underscore 1
6128#undef elf_backend_grok_prstatus
6129#undef elf_backend_grok_psinfo
6130#undef elf_backend_add_symbol_hook
6131#define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6132#undef elf_backend_link_output_symbol_hook
6133#define elf_backend_link_output_symbol_hook \
6134 elf_vxworks_link_output_symbol_hook
6135#undef elf_backend_emit_relocs
6136#define elf_backend_emit_relocs elf_vxworks_emit_relocs
6137#undef elf_backend_final_write_processing
6138#define elf_backend_final_write_processing \
6139 elf_vxworks_final_write_processing
6140#undef ELF_MAXPAGESIZE
6141#define ELF_MAXPAGESIZE 0x1000
6142#undef ELF_COMMONPAGESIZE
6143
6144#include "elf32-target.h"
6145
85fbca6a 6146#endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */
This page took 0.785515 seconds and 4 git commands to generate.