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