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