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