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