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