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