Back to index

cell-binutils  2.17cvs20070401
elf32-sh.c
Go to the documentation of this file.
00001 /* Renesas / SuperH SH specific support for 32-bit ELF
00002    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00003    2006, 2007 Free Software Foundation, Inc.
00004    Contributed by Ian Lance Taylor, Cygnus Support.
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 #include "bfd.h"
00023 #include "sysdep.h"
00024 #include "bfdlink.h"
00025 #include "libbfd.h"
00026 #include "elf-bfd.h"
00027 #include "elf-vxworks.h"
00028 #include "elf/sh.h"
00029 #include "libiberty.h"
00030 #include "../opcodes/sh-opc.h"
00031 
00032 static bfd_reloc_status_type sh_elf_reloc
00033   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00034 static bfd_reloc_status_type sh_elf_ignore_reloc
00035   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00036 static bfd_boolean sh_elf_relax_delete_bytes
00037   (bfd *, asection *, bfd_vma, int);
00038 static bfd_boolean sh_elf_align_loads
00039   (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
00040 #ifndef SH64_ELF
00041 static bfd_boolean sh_elf_swap_insns
00042   (bfd *, asection *, void *, bfd_byte *, bfd_vma);
00043 #endif
00044 static int sh_elf_optimized_tls_reloc
00045   (struct bfd_link_info *, int, int);
00046 static bfd_vma dtpoff_base
00047   (struct bfd_link_info *);
00048 static bfd_vma tpoff
00049   (struct bfd_link_info *, bfd_vma);
00050 
00051 /* The name of the dynamic interpreter.  This is put in the .interp
00052    section.  */
00053 
00054 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
00055 
00056 #define MINUS_ONE ((bfd_vma) 0 - 1)
00057 
00058 #define SH_PARTIAL32 TRUE
00059 #define SH_SRC_MASK32 0xffffffff
00060 #define SH_ELF_RELOC sh_elf_reloc
00061 static reloc_howto_type sh_elf_howto_table[] =
00062 {
00063 #include "elf32-sh-relocs.h"
00064 };
00065 
00066 #define SH_PARTIAL32 FALSE
00067 #define SH_SRC_MASK32 0
00068 #define SH_ELF_RELOC bfd_elf_generic_reloc
00069 static reloc_howto_type sh_vxworks_howto_table[] =
00070 {
00071 #include "elf32-sh-relocs.h"
00072 };
00073 
00074 /* Return true if OUTPUT_BFD is a VxWorks object.  */
00075 
00076 static bfd_boolean
00077 vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
00078 {
00079 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
00080   extern const bfd_target bfd_elf32_shlvxworks_vec;
00081   extern const bfd_target bfd_elf32_shvxworks_vec;
00082 
00083   return (abfd->xvec == &bfd_elf32_shlvxworks_vec
00084          || abfd->xvec == &bfd_elf32_shvxworks_vec);
00085 #else
00086   return FALSE;
00087 #endif
00088 }
00089 
00090 /* Return the howto table for ABFD.  */
00091 
00092 static reloc_howto_type *
00093 get_howto_table (bfd *abfd)
00094 {
00095   if (vxworks_object_p (abfd))
00096     return sh_vxworks_howto_table;
00097   return sh_elf_howto_table;
00098 }
00099 
00100 static bfd_reloc_status_type
00101 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
00102                  asection *input_section, bfd_byte *contents,
00103                  bfd_vma addr, asection *symbol_section,
00104                  bfd_vma start, bfd_vma end)
00105 {
00106   static bfd_vma last_addr;
00107   static asection *last_symbol_section;
00108   bfd_byte *start_ptr, *ptr, *last_ptr;
00109   int diff, cum_diff;
00110   bfd_signed_vma x;
00111   int insn;
00112 
00113   /* Sanity check the address.  */
00114   if (addr > bfd_get_section_limit (input_bfd, input_section))
00115     return bfd_reloc_outofrange;
00116 
00117   /* We require the start and end relocations to be processed consecutively -
00118      although we allow then to be processed forwards or backwards.  */
00119   if (! last_addr)
00120     {
00121       last_addr = addr;
00122       last_symbol_section = symbol_section;
00123       return bfd_reloc_ok;
00124     }
00125   if (last_addr != addr)
00126     abort ();
00127   last_addr = 0;
00128 
00129   if (! symbol_section || last_symbol_section != symbol_section || end < start)
00130     return bfd_reloc_outofrange;
00131 
00132   /* Get the symbol_section contents.  */
00133   if (symbol_section != input_section)
00134     {
00135       if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
00136        contents = elf_section_data (symbol_section)->this_hdr.contents;
00137       else
00138        {
00139          if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
00140                                       &contents))
00141            {
00142              if (contents != NULL)
00143               free (contents);
00144              return bfd_reloc_outofrange;
00145            }
00146        }
00147     }
00148 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
00149   start_ptr = contents + start;
00150   for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
00151     {
00152       for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
00153        ptr -= 2;
00154       ptr += 2;
00155       diff = (last_ptr - ptr) >> 1;
00156       cum_diff += diff & 1;
00157       cum_diff += diff;
00158     }
00159   /* Calculate the start / end values to load into rs / re minus four -
00160      so that will cancel out the four we would otherwise have to add to
00161      addr to get the value to subtract in order to get relative addressing.  */
00162   if (cum_diff >= 0)
00163     {
00164       start -= 4;
00165       end = (ptr + cum_diff * 2) - contents;
00166     }
00167   else
00168     {
00169       bfd_vma start0 = start - 4;
00170 
00171       while (start0 && IS_PPI (contents + start0))
00172        start0 -= 2;
00173       start0 = start - 2 - ((start - start0) & 2);
00174       start = start0 - cum_diff - 2;
00175       end = start0;
00176     }
00177 
00178   if (contents != NULL
00179       && elf_section_data (symbol_section)->this_hdr.contents != contents)
00180     free (contents);
00181 
00182   insn = bfd_get_16 (input_bfd, contents + addr);
00183 
00184   x = (insn & 0x200 ? end : start) - addr;
00185   if (input_section != symbol_section)
00186     x += ((symbol_section->output_section->vma + symbol_section->output_offset)
00187          - (input_section->output_section->vma
00188             + input_section->output_offset));
00189   x >>= 1;
00190   if (x < -128 || x > 127)
00191     return bfd_reloc_overflow;
00192 
00193   x = (insn & ~0xff) | (x & 0xff);
00194   bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
00195 
00196   return bfd_reloc_ok;
00197 }
00198 
00199 /* This function is used for normal relocs.  This used to be like the COFF
00200    function, and is almost certainly incorrect for other ELF targets.  */
00201 
00202 static bfd_reloc_status_type
00203 sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
00204              void *data, asection *input_section, bfd *output_bfd,
00205              char **error_message ATTRIBUTE_UNUSED)
00206 {
00207   unsigned long insn;
00208   bfd_vma sym_value;
00209   enum elf_sh_reloc_type r_type;
00210   bfd_vma addr = reloc_entry->address;
00211   bfd_byte *hit_data = addr + (bfd_byte *) data;
00212 
00213   r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
00214 
00215   if (output_bfd != NULL)
00216     {
00217       /* Partial linking--do nothing.  */
00218       reloc_entry->address += input_section->output_offset;
00219       return bfd_reloc_ok;
00220     }
00221 
00222   /* Almost all relocs have to do with relaxing.  If any work must be
00223      done for them, it has been done in sh_relax_section.  */
00224   if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
00225     return bfd_reloc_ok;
00226 
00227   if (symbol_in != NULL
00228       && bfd_is_und_section (symbol_in->section))
00229     return bfd_reloc_undefined;
00230 
00231   if (bfd_is_com_section (symbol_in->section))
00232     sym_value = 0;
00233   else
00234     sym_value = (symbol_in->value +
00235                symbol_in->section->output_section->vma +
00236                symbol_in->section->output_offset);
00237 
00238   switch (r_type)
00239     {
00240     case R_SH_DIR32:
00241       insn = bfd_get_32 (abfd, hit_data);
00242       insn += sym_value + reloc_entry->addend;
00243       bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
00244       break;
00245     case R_SH_IND12W:
00246       insn = bfd_get_16 (abfd, hit_data);
00247       sym_value += reloc_entry->addend;
00248       sym_value -= (input_section->output_section->vma
00249                   + input_section->output_offset
00250                   + addr
00251                   + 4);
00252       sym_value += (insn & 0xfff) << 1;
00253       if (insn & 0x800)
00254        sym_value -= 0x1000;
00255       insn = (insn & 0xf000) | (sym_value & 0xfff);
00256       bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
00257       if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
00258        return bfd_reloc_overflow;
00259       break;
00260     default:
00261       abort ();
00262       break;
00263     }
00264 
00265   return bfd_reloc_ok;
00266 }
00267 
00268 /* This function is used for relocs which are only used for relaxing,
00269    which the linker should otherwise ignore.  */
00270 
00271 static bfd_reloc_status_type
00272 sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
00273                    asymbol *symbol ATTRIBUTE_UNUSED,
00274                    void *data ATTRIBUTE_UNUSED, asection *input_section,
00275                    bfd *output_bfd,
00276                    char **error_message ATTRIBUTE_UNUSED)
00277 {
00278   if (output_bfd != NULL)
00279     reloc_entry->address += input_section->output_offset;
00280   return bfd_reloc_ok;
00281 }
00282 
00283 /* This structure is used to map BFD reloc codes to SH ELF relocs.  */
00284 
00285 struct elf_reloc_map
00286 {
00287   bfd_reloc_code_real_type bfd_reloc_val;
00288   unsigned char elf_reloc_val;
00289 };
00290 
00291 /* An array mapping BFD reloc codes to SH ELF relocs.  */
00292 
00293 static const struct elf_reloc_map sh_reloc_map[] =
00294 {
00295   { BFD_RELOC_NONE, R_SH_NONE },
00296   { BFD_RELOC_32, R_SH_DIR32 },
00297   { BFD_RELOC_16, R_SH_DIR16 },
00298   { BFD_RELOC_8, R_SH_DIR8 },
00299   { BFD_RELOC_CTOR, R_SH_DIR32 },
00300   { BFD_RELOC_32_PCREL, R_SH_REL32 },
00301   { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
00302   { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
00303   { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
00304   { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
00305   { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
00306   { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
00307   { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
00308   { BFD_RELOC_SH_USES, R_SH_USES },
00309   { BFD_RELOC_SH_COUNT, R_SH_COUNT },
00310   { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
00311   { BFD_RELOC_SH_CODE, R_SH_CODE },
00312   { BFD_RELOC_SH_DATA, R_SH_DATA },
00313   { BFD_RELOC_SH_LABEL, R_SH_LABEL },
00314   { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
00315   { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
00316   { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
00317   { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
00318   { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
00319   { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
00320   { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
00321   { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
00322   { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
00323   { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
00324   { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
00325   { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
00326   { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
00327   { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
00328   { BFD_RELOC_SH_COPY, R_SH_COPY },
00329   { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
00330   { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
00331   { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
00332   { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
00333   { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
00334   { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
00335 #ifdef INCLUDE_SHMEDIA
00336   { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
00337   { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
00338   { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
00339   { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
00340   { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
00341   { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
00342   { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
00343   { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
00344   { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
00345   { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
00346   { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
00347   { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
00348   { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
00349   { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
00350   { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
00351   { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
00352   { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
00353   { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
00354   { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
00355   { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
00356   { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
00357   { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
00358   { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
00359   { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
00360   { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
00361   { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
00362   { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
00363   { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
00364   { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
00365   { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
00366   { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
00367   { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
00368   { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
00369   { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
00370   { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
00371   { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
00372   { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
00373   { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
00374   { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
00375   { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
00376   { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
00377   { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
00378   { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
00379   { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
00380   { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
00381   { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
00382   { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
00383   { BFD_RELOC_64, R_SH_64 },
00384   { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
00385 #endif /* not INCLUDE_SHMEDIA */
00386 };
00387 
00388 /* Given a BFD reloc code, return the howto structure for the
00389    corresponding SH ELF reloc.  */
00390 
00391 static reloc_howto_type *
00392 sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
00393 {
00394   unsigned int i;
00395 
00396   for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
00397     {
00398       if (sh_reloc_map[i].bfd_reloc_val == code)
00399        return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
00400     }
00401 
00402   return NULL;
00403 }
00404 
00405 static reloc_howto_type *
00406 sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
00407 {
00408   unsigned int i;
00409 
00410   if (vxworks_object_p (abfd))
00411     {
00412       for (i = 0;
00413           i < (sizeof (sh_vxworks_howto_table)
00414               / sizeof (sh_vxworks_howto_table[0]));
00415           i++)
00416        if (sh_vxworks_howto_table[i].name != NULL
00417            && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
00418          return &sh_vxworks_howto_table[i];
00419     }
00420   else
00421     {
00422       for (i = 0;
00423           i < (sizeof (sh_elf_howto_table)
00424               / sizeof (sh_elf_howto_table[0]));
00425           i++)
00426        if (sh_elf_howto_table[i].name != NULL
00427            && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
00428          return &sh_elf_howto_table[i];
00429     }
00430 
00431   return NULL;
00432 }
00433 
00434 /* Given an ELF reloc, fill in the howto field of a relent.  */
00435 
00436 static void
00437 sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
00438 {
00439   unsigned int r;
00440 
00441   r = ELF32_R_TYPE (dst->r_info);
00442 
00443   BFD_ASSERT (r < (unsigned int) R_SH_max);
00444   BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
00445   BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
00446   BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_3 || r > R_SH_LAST_INVALID_RELOC_3);
00447   BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_4 || r > R_SH_LAST_INVALID_RELOC_4);
00448   BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_5 || r > R_SH_LAST_INVALID_RELOC_5);
00449 
00450   cache_ptr->howto = get_howto_table (abfd) + r;
00451 }
00452 
00453 /* This function handles relaxing for SH ELF.  See the corresponding
00454    function in coff-sh.c for a description of what this does.  FIXME:
00455    There is a lot of duplication here between this code and the COFF
00456    specific code.  The format of relocs and symbols is wound deeply
00457    into this code, but it would still be better if the duplication
00458    could be eliminated somehow.  Note in particular that although both
00459    functions use symbols like R_SH_CODE, those symbols have different
00460    values; in coff-sh.c they come from include/coff/sh.h, whereas here
00461    they come from enum elf_sh_reloc_type in include/elf/sh.h.  */
00462 
00463 static bfd_boolean
00464 sh_elf_relax_section (bfd *abfd, asection *sec,
00465                     struct bfd_link_info *link_info, bfd_boolean *again)
00466 {
00467   Elf_Internal_Shdr *symtab_hdr;
00468   Elf_Internal_Rela *internal_relocs;
00469   bfd_boolean have_code;
00470   Elf_Internal_Rela *irel, *irelend;
00471   bfd_byte *contents = NULL;
00472   Elf_Internal_Sym *isymbuf = NULL;
00473 
00474   *again = FALSE;
00475 
00476   if (link_info->relocatable
00477       || (sec->flags & SEC_RELOC) == 0
00478       || sec->reloc_count == 0)
00479     return TRUE;
00480 
00481 #ifdef INCLUDE_SHMEDIA
00482   if (elf_section_data (sec)->this_hdr.sh_flags
00483       & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
00484     {
00485       return TRUE;
00486     }
00487 #endif
00488 
00489   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00490 
00491   internal_relocs = (_bfd_elf_link_read_relocs
00492                    (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
00493                     link_info->keep_memory));
00494   if (internal_relocs == NULL)
00495     goto error_return;
00496 
00497   have_code = FALSE;
00498 
00499   irelend = internal_relocs + sec->reloc_count;
00500   for (irel = internal_relocs; irel < irelend; irel++)
00501     {
00502       bfd_vma laddr, paddr, symval;
00503       unsigned short insn;
00504       Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
00505       bfd_signed_vma foff;
00506 
00507       if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
00508        have_code = TRUE;
00509 
00510       if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
00511        continue;
00512 
00513       /* Get the section contents.  */
00514       if (contents == NULL)
00515        {
00516          if (elf_section_data (sec)->this_hdr.contents != NULL)
00517            contents = elf_section_data (sec)->this_hdr.contents;
00518          else
00519            {
00520              if (!bfd_malloc_and_get_section (abfd, sec, &contents))
00521               goto error_return;
00522            }
00523        }
00524 
00525       /* The r_addend field of the R_SH_USES reloc will point us to
00526         the register load.  The 4 is because the r_addend field is
00527         computed as though it were a jump offset, which are based
00528         from 4 bytes after the jump instruction.  */
00529       laddr = irel->r_offset + 4 + irel->r_addend;
00530       if (laddr >= sec->size)
00531        {
00532          (*_bfd_error_handler) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
00533                              abfd,
00534                              (unsigned long) irel->r_offset);
00535          continue;
00536        }
00537       insn = bfd_get_16 (abfd, contents + laddr);
00538 
00539       /* If the instruction is not mov.l NN,rN, we don't know what to
00540         do.  */
00541       if ((insn & 0xf000) != 0xd000)
00542        {
00543          ((*_bfd_error_handler)
00544           (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
00545            abfd, (unsigned long) irel->r_offset, insn));
00546          continue;
00547        }
00548 
00549       /* Get the address from which the register is being loaded.  The
00550         displacement in the mov.l instruction is quadrupled.  It is a
00551         displacement from four bytes after the movl instruction, but,
00552         before adding in the PC address, two least significant bits
00553         of the PC are cleared.  We assume that the section is aligned
00554         on a four byte boundary.  */
00555       paddr = insn & 0xff;
00556       paddr *= 4;
00557       paddr += (laddr + 4) &~ (bfd_vma) 3;
00558       if (paddr >= sec->size)
00559        {
00560          ((*_bfd_error_handler)
00561           (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
00562            abfd, (unsigned long) irel->r_offset));
00563          continue;
00564        }
00565 
00566       /* Get the reloc for the address from which the register is
00567         being loaded.  This reloc will tell us which function is
00568         actually being called.  */
00569       for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
00570        if (irelfn->r_offset == paddr
00571            && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
00572          break;
00573       if (irelfn >= irelend)
00574        {
00575          ((*_bfd_error_handler)
00576           (_("%B: 0x%lx: warning: could not find expected reloc"),
00577            abfd, (unsigned long) paddr));
00578          continue;
00579        }
00580 
00581       /* Read this BFD's symbols if we haven't done so already.  */
00582       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
00583        {
00584          isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
00585          if (isymbuf == NULL)
00586            isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
00587                                        symtab_hdr->sh_info, 0,
00588                                        NULL, NULL, NULL);
00589          if (isymbuf == NULL)
00590            goto error_return;
00591        }
00592 
00593       /* Get the value of the symbol referred to by the reloc.  */
00594       if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
00595        {
00596          /* A local symbol.  */
00597          Elf_Internal_Sym *isym;
00598 
00599          isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
00600          if (isym->st_shndx
00601              != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
00602            {
00603              ((*_bfd_error_handler)
00604               (_("%B: 0x%lx: warning: symbol in unexpected section"),
00605               abfd, (unsigned long) paddr));
00606              continue;
00607            }
00608 
00609          symval = (isym->st_value
00610                   + sec->output_section->vma
00611                   + sec->output_offset);
00612        }
00613       else
00614        {
00615          unsigned long indx;
00616          struct elf_link_hash_entry *h;
00617 
00618          indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
00619          h = elf_sym_hashes (abfd)[indx];
00620          BFD_ASSERT (h != NULL);
00621          if (h->root.type != bfd_link_hash_defined
00622              && h->root.type != bfd_link_hash_defweak)
00623            {
00624              /* This appears to be a reference to an undefined
00625                symbol.  Just ignore it--it will be caught by the
00626                regular reloc processing.  */
00627              continue;
00628            }
00629 
00630          symval = (h->root.u.def.value
00631                   + h->root.u.def.section->output_section->vma
00632                   + h->root.u.def.section->output_offset);
00633        }
00634 
00635       if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
00636        symval += bfd_get_32 (abfd, contents + paddr);
00637       else
00638        symval += irelfn->r_addend;
00639 
00640       /* See if this function call can be shortened.  */
00641       foff = (symval
00642              - (irel->r_offset
00643                + sec->output_section->vma
00644                + sec->output_offset
00645                + 4));
00646       /* A branch to an address beyond ours might be increased by an
00647         .align that doesn't move when bytes behind us are deleted.
00648         So, we add some slop in this calculation to allow for
00649         that.  */
00650       if (foff < -0x1000 || foff >= 0x1000 - 8)
00651        {
00652          /* After all that work, we can't shorten this function call.  */
00653          continue;
00654        }
00655 
00656       /* Shorten the function call.  */
00657 
00658       /* For simplicity of coding, we are going to modify the section
00659         contents, the section relocs, and the BFD symbol table.  We
00660         must tell the rest of the code not to free up this
00661         information.  It would be possible to instead create a table
00662         of changes which have to be made, as is done in coff-mips.c;
00663         that would be more work, but would require less memory when
00664         the linker is run.  */
00665 
00666       elf_section_data (sec)->relocs = internal_relocs;
00667       elf_section_data (sec)->this_hdr.contents = contents;
00668       symtab_hdr->contents = (unsigned char *) isymbuf;
00669 
00670       /* Replace the jsr with a bsr.  */
00671 
00672       /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
00673         replace the jsr with a bsr.  */
00674       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
00675       /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
00676         here, but that only checks if the symbol is an external symbol,
00677         not if the symbol is in a different section.  Besides, we need
00678         a consistent meaning for the relocation, so we just assume here that
00679         the value of the symbol is not available.  */
00680 
00681       /* We can't fully resolve this yet, because the external
00682         symbol value may be changed by future relaxing.  We let
00683         the final link phase handle it.  */
00684       bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
00685 
00686       irel->r_addend = -4;
00687 
00688       /* When we calculated the symbol "value" we had an offset in the
00689         DIR32's word in memory (we read and add it above).  However,
00690         the jsr we create does NOT have this offset encoded, so we
00691         have to add it to the addend to preserve it.  */
00692       irel->r_addend += bfd_get_32 (abfd, contents + paddr);
00693 
00694       /* See if there is another R_SH_USES reloc referring to the same
00695         register load.  */
00696       for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
00697        if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
00698            && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
00699          break;
00700       if (irelscan < irelend)
00701        {
00702          /* Some other function call depends upon this register load,
00703             and we have not yet converted that function call.
00704             Indeed, we may never be able to convert it.  There is
00705             nothing else we can do at this point.  */
00706          continue;
00707        }
00708 
00709       /* Look for a R_SH_COUNT reloc on the location where the
00710         function address is stored.  Do this before deleting any
00711         bytes, to avoid confusion about the address.  */
00712       for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
00713        if (irelcount->r_offset == paddr
00714            && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
00715          break;
00716 
00717       /* Delete the register load.  */
00718       if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
00719        goto error_return;
00720 
00721       /* That will change things, so, just in case it permits some
00722         other function call to come within range, we should relax
00723         again.  Note that this is not required, and it may be slow.  */
00724       *again = TRUE;
00725 
00726       /* Now check whether we got a COUNT reloc.  */
00727       if (irelcount >= irelend)
00728        {
00729          ((*_bfd_error_handler)
00730           (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
00731            abfd, (unsigned long) paddr));
00732          continue;
00733        }
00734 
00735       /* The number of uses is stored in the r_addend field.  We've
00736         just deleted one.  */
00737       if (irelcount->r_addend == 0)
00738        {
00739          ((*_bfd_error_handler) (_("%B: 0x%lx: warning: bad count"),
00740                               abfd,
00741                               (unsigned long) paddr));
00742          continue;
00743        }
00744 
00745       --irelcount->r_addend;
00746 
00747       /* If there are no more uses, we can delete the address.  Reload
00748         the address from irelfn, in case it was changed by the
00749         previous call to sh_elf_relax_delete_bytes.  */
00750       if (irelcount->r_addend == 0)
00751        {
00752          if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
00753            goto error_return;
00754        }
00755 
00756       /* We've done all we can with that function call.  */
00757     }
00758 
00759   /* Look for load and store instructions that we can align on four
00760      byte boundaries.  */
00761   if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
00762       && have_code)
00763     {
00764       bfd_boolean swapped;
00765 
00766       /* Get the section contents.  */
00767       if (contents == NULL)
00768        {
00769          if (elf_section_data (sec)->this_hdr.contents != NULL)
00770            contents = elf_section_data (sec)->this_hdr.contents;
00771          else
00772            {
00773              if (!bfd_malloc_and_get_section (abfd, sec, &contents))
00774               goto error_return;
00775            }
00776        }
00777 
00778       if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
00779                             &swapped))
00780        goto error_return;
00781 
00782       if (swapped)
00783        {
00784          elf_section_data (sec)->relocs = internal_relocs;
00785          elf_section_data (sec)->this_hdr.contents = contents;
00786          symtab_hdr->contents = (unsigned char *) isymbuf;
00787        }
00788     }
00789 
00790   if (isymbuf != NULL
00791       && symtab_hdr->contents != (unsigned char *) isymbuf)
00792     {
00793       if (! link_info->keep_memory)
00794        free (isymbuf);
00795       else
00796        {
00797          /* Cache the symbols for elf_link_input_bfd.  */
00798          symtab_hdr->contents = (unsigned char *) isymbuf;
00799        }
00800     }
00801 
00802   if (contents != NULL
00803       && elf_section_data (sec)->this_hdr.contents != contents)
00804     {
00805       if (! link_info->keep_memory)
00806        free (contents);
00807       else
00808        {
00809          /* Cache the section contents for elf_link_input_bfd.  */
00810          elf_section_data (sec)->this_hdr.contents = contents;
00811        }
00812     }
00813 
00814   if (internal_relocs != NULL
00815       && elf_section_data (sec)->relocs != internal_relocs)
00816     free (internal_relocs);
00817 
00818   return TRUE;
00819 
00820  error_return:
00821   if (isymbuf != NULL
00822       && symtab_hdr->contents != (unsigned char *) isymbuf)
00823     free (isymbuf);
00824   if (contents != NULL
00825       && elf_section_data (sec)->this_hdr.contents != contents)
00826     free (contents);
00827   if (internal_relocs != NULL
00828       && elf_section_data (sec)->relocs != internal_relocs)
00829     free (internal_relocs);
00830 
00831   return FALSE;
00832 }
00833 
00834 /* Delete some bytes from a section while relaxing.  FIXME: There is a
00835    lot of duplication between this function and sh_relax_delete_bytes
00836    in coff-sh.c.  */
00837 
00838 static bfd_boolean
00839 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
00840                         int count)
00841 {
00842   Elf_Internal_Shdr *symtab_hdr;
00843   unsigned int sec_shndx;
00844   bfd_byte *contents;
00845   Elf_Internal_Rela *irel, *irelend;
00846   Elf_Internal_Rela *irelalign;
00847   bfd_vma toaddr;
00848   Elf_Internal_Sym *isymbuf, *isym, *isymend;
00849   struct elf_link_hash_entry **sym_hashes;
00850   struct elf_link_hash_entry **end_hashes;
00851   unsigned int symcount;
00852   asection *o;
00853 
00854   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00855   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
00856 
00857   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
00858 
00859   contents = elf_section_data (sec)->this_hdr.contents;
00860 
00861   /* The deletion must stop at the next ALIGN reloc for an aligment
00862      power larger than the number of bytes we are deleting.  */
00863 
00864   irelalign = NULL;
00865   toaddr = sec->size;
00866 
00867   irel = elf_section_data (sec)->relocs;
00868   irelend = irel + sec->reloc_count;
00869   for (; irel < irelend; irel++)
00870     {
00871       if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
00872          && irel->r_offset > addr
00873          && count < (1 << irel->r_addend))
00874        {
00875          irelalign = irel;
00876          toaddr = irel->r_offset;
00877          break;
00878        }
00879     }
00880 
00881   /* Actually delete the bytes.  */
00882   memmove (contents + addr, contents + addr + count,
00883           (size_t) (toaddr - addr - count));
00884   if (irelalign == NULL)
00885     sec->size -= count;
00886   else
00887     {
00888       int i;
00889 
00890 #define NOP_OPCODE (0x0009)
00891 
00892       BFD_ASSERT ((count & 1) == 0);
00893       for (i = 0; i < count; i += 2)
00894        bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
00895     }
00896 
00897   /* Adjust all the relocs.  */
00898   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
00899     {
00900       bfd_vma nraddr, stop;
00901       bfd_vma start = 0;
00902       int insn = 0;
00903       int off, adjust, oinsn;
00904       bfd_signed_vma voff = 0;
00905       bfd_boolean overflow;
00906 
00907       /* Get the new reloc address.  */
00908       nraddr = irel->r_offset;
00909       if ((irel->r_offset > addr
00910           && irel->r_offset < toaddr)
00911          || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
00912              && irel->r_offset == toaddr))
00913        nraddr -= count;
00914 
00915       /* See if this reloc was for the bytes we have deleted, in which
00916         case we no longer care about it.  Don't delete relocs which
00917         represent addresses, though.  */
00918       if (irel->r_offset >= addr
00919          && irel->r_offset < addr + count
00920          && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
00921          && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
00922          && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
00923          && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
00924        irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
00925                                  (int) R_SH_NONE);
00926 
00927       /* If this is a PC relative reloc, see if the range it covers
00928         includes the bytes we have deleted.  */
00929       switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
00930        {
00931        default:
00932          break;
00933 
00934        case R_SH_DIR8WPN:
00935        case R_SH_IND12W:
00936        case R_SH_DIR8WPZ:
00937        case R_SH_DIR8WPL:
00938          start = irel->r_offset;
00939          insn = bfd_get_16 (abfd, contents + nraddr);
00940          break;
00941        }
00942 
00943       switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
00944        {
00945        default:
00946          start = stop = addr;
00947          break;
00948 
00949        case R_SH_DIR32:
00950          /* If this reloc is against a symbol defined in this
00951             section, and the symbol will not be adjusted below, we
00952             must check the addend to see it will put the value in
00953             range to be adjusted, and hence must be changed.  */
00954          if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
00955            {
00956              isym = isymbuf + ELF32_R_SYM (irel->r_info);
00957              if (isym->st_shndx == sec_shndx
00958                 && (isym->st_value <= addr
00959                     || isym->st_value >= toaddr))
00960               {
00961                 bfd_vma val;
00962 
00963                 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
00964                   {
00965                     val = bfd_get_32 (abfd, contents + nraddr);
00966                     val += isym->st_value;
00967                     if (val > addr && val < toaddr)
00968                      bfd_put_32 (abfd, val - count, contents + nraddr);
00969                   }
00970                 else
00971                   {
00972                     val = isym->st_value + irel->r_addend;
00973                     if (val > addr && val < toaddr)
00974                      irel->r_addend -= count;
00975                   }
00976               }
00977            }
00978          start = stop = addr;
00979          break;
00980 
00981        case R_SH_DIR8WPN:
00982          off = insn & 0xff;
00983          if (off & 0x80)
00984            off -= 0x100;
00985          stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
00986          break;
00987 
00988        case R_SH_IND12W:
00989          off = insn & 0xfff;
00990          if (! off)
00991            {
00992              /* This has been made by previous relaxation.  Since the
00993                relocation will be against an external symbol, the
00994                final relocation will just do the right thing.  */
00995              start = stop = addr;
00996            }
00997          else
00998            {
00999              if (off & 0x800)
01000               off -= 0x1000;
01001              stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
01002 
01003              /* The addend will be against the section symbol, thus
01004                for adjusting the addend, the relevant start is the
01005                start of the section.
01006                N.B. If we want to abandon in-place changes here and
01007                test directly using symbol + addend, we have to take into
01008                account that the addend has already been adjusted by -4.  */
01009              if (stop > addr && stop < toaddr)
01010               irel->r_addend -= count;
01011            }
01012          break;
01013 
01014        case R_SH_DIR8WPZ:
01015          off = insn & 0xff;
01016          stop = start + 4 + off * 2;
01017          break;
01018 
01019        case R_SH_DIR8WPL:
01020          off = insn & 0xff;
01021          stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
01022          break;
01023 
01024        case R_SH_SWITCH8:
01025        case R_SH_SWITCH16:
01026        case R_SH_SWITCH32:
01027          /* These relocs types represent
01028               .word L2-L1
01029             The r_addend field holds the difference between the reloc
01030             address and L1.  That is the start of the reloc, and
01031             adding in the contents gives us the top.  We must adjust
01032             both the r_offset field and the section contents.
01033             N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
01034             and the elf bfd r_offset is called r_vaddr.  */
01035 
01036          stop = irel->r_offset;
01037          start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
01038 
01039          if (start > addr
01040              && start < toaddr
01041              && (stop <= addr || stop >= toaddr))
01042            irel->r_addend += count;
01043          else if (stop > addr
01044                  && stop < toaddr
01045                  && (start <= addr || start >= toaddr))
01046            irel->r_addend -= count;
01047 
01048          if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
01049            voff = bfd_get_signed_16 (abfd, contents + nraddr);
01050          else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
01051            voff = bfd_get_8 (abfd, contents + nraddr);
01052          else
01053            voff = bfd_get_signed_32 (abfd, contents + nraddr);
01054          stop = (bfd_vma) ((bfd_signed_vma) start + voff);
01055 
01056          break;
01057 
01058        case R_SH_USES:
01059          start = irel->r_offset;
01060          stop = (bfd_vma) ((bfd_signed_vma) start
01061                          + (long) irel->r_addend
01062                          + 4);
01063          break;
01064        }
01065 
01066       if (start > addr
01067          && start < toaddr
01068          && (stop <= addr || stop >= toaddr))
01069        adjust = count;
01070       else if (stop > addr
01071               && stop < toaddr
01072               && (start <= addr || start >= toaddr))
01073        adjust = - count;
01074       else
01075        adjust = 0;
01076 
01077       if (adjust != 0)
01078        {
01079          oinsn = insn;
01080          overflow = FALSE;
01081          switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
01082            {
01083            default:
01084              abort ();
01085              break;
01086 
01087            case R_SH_DIR8WPN:
01088            case R_SH_DIR8WPZ:
01089              insn += adjust / 2;
01090              if ((oinsn & 0xff00) != (insn & 0xff00))
01091               overflow = TRUE;
01092              bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
01093              break;
01094 
01095            case R_SH_IND12W:
01096              insn += adjust / 2;
01097              if ((oinsn & 0xf000) != (insn & 0xf000))
01098               overflow = TRUE;
01099              bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
01100              break;
01101 
01102            case R_SH_DIR8WPL:
01103              BFD_ASSERT (adjust == count || count >= 4);
01104              if (count >= 4)
01105               insn += adjust / 4;
01106              else
01107               {
01108                 if ((irel->r_offset & 3) == 0)
01109                   ++insn;
01110               }
01111              if ((oinsn & 0xff00) != (insn & 0xff00))
01112               overflow = TRUE;
01113              bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
01114              break;
01115 
01116            case R_SH_SWITCH8:
01117              voff += adjust;
01118              if (voff < 0 || voff >= 0xff)
01119               overflow = TRUE;
01120              bfd_put_8 (abfd, voff, contents + nraddr);
01121              break;
01122 
01123            case R_SH_SWITCH16:
01124              voff += adjust;
01125              if (voff < - 0x8000 || voff >= 0x8000)
01126               overflow = TRUE;
01127              bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
01128              break;
01129 
01130            case R_SH_SWITCH32:
01131              voff += adjust;
01132              bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
01133              break;
01134 
01135            case R_SH_USES:
01136              irel->r_addend += adjust;
01137              break;
01138            }
01139 
01140          if (overflow)
01141            {
01142              ((*_bfd_error_handler)
01143               (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
01144               abfd, (unsigned long) irel->r_offset));
01145              bfd_set_error (bfd_error_bad_value);
01146              return FALSE;
01147            }
01148        }
01149 
01150       irel->r_offset = nraddr;
01151     }
01152 
01153   /* Look through all the other sections.  If there contain any IMM32
01154      relocs against internal symbols which we are not going to adjust
01155      below, we may need to adjust the addends.  */
01156   for (o = abfd->sections; o != NULL; o = o->next)
01157     {
01158       Elf_Internal_Rela *internal_relocs;
01159       Elf_Internal_Rela *irelscan, *irelscanend;
01160       bfd_byte *ocontents;
01161 
01162       if (o == sec
01163          || (o->flags & SEC_RELOC) == 0
01164          || o->reloc_count == 0)
01165        continue;
01166 
01167       /* We always cache the relocs.  Perhaps, if info->keep_memory is
01168         FALSE, we should free them, if we are permitted to, when we
01169         leave sh_coff_relax_section.  */
01170       internal_relocs = (_bfd_elf_link_read_relocs
01171                       (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
01172       if (internal_relocs == NULL)
01173        return FALSE;
01174 
01175       ocontents = NULL;
01176       irelscanend = internal_relocs + o->reloc_count;
01177       for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
01178        {
01179          /* Dwarf line numbers use R_SH_SWITCH32 relocs.  */
01180          if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
01181            {
01182              bfd_vma start, stop;
01183              bfd_signed_vma voff;
01184 
01185              if (ocontents == NULL)
01186               {
01187                 if (elf_section_data (o)->this_hdr.contents != NULL)
01188                   ocontents = elf_section_data (o)->this_hdr.contents;
01189                 else
01190                   {
01191                     /* We always cache the section contents.
01192                       Perhaps, if info->keep_memory is FALSE, we
01193                       should free them, if we are permitted to,
01194                       when we leave sh_coff_relax_section.  */
01195                     if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
01196                      {
01197                        if (ocontents != NULL)
01198                          free (ocontents);
01199                        return FALSE;
01200                      }
01201 
01202                     elf_section_data (o)->this_hdr.contents = ocontents;
01203                   }
01204               }
01205 
01206              stop = irelscan->r_offset;
01207              start
01208               = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
01209 
01210              /* STOP is in a different section, so it won't change.  */
01211              if (start > addr && start < toaddr)
01212               irelscan->r_addend += count;
01213 
01214              voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
01215              stop = (bfd_vma) ((bfd_signed_vma) start + voff);
01216 
01217              if (start > addr
01218                 && start < toaddr
01219                 && (stop <= addr || stop >= toaddr))
01220               bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
01221                                ocontents + irelscan->r_offset);
01222              else if (stop > addr
01223                      && stop < toaddr
01224                      && (start <= addr || start >= toaddr))
01225               bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
01226                                ocontents + irelscan->r_offset);
01227            }
01228 
01229          if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
01230            continue;
01231 
01232          if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
01233            continue;
01234 
01235 
01236          isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
01237          if (isym->st_shndx == sec_shndx
01238              && (isym->st_value <= addr
01239                 || isym->st_value >= toaddr))
01240            {
01241              bfd_vma val;
01242 
01243              if (ocontents == NULL)
01244               {
01245                 if (elf_section_data (o)->this_hdr.contents != NULL)
01246                   ocontents = elf_section_data (o)->this_hdr.contents;
01247                 else
01248                   {
01249                     /* We always cache the section contents.
01250                       Perhaps, if info->keep_memory is FALSE, we
01251                       should free them, if we are permitted to,
01252                       when we leave sh_coff_relax_section.  */
01253                     if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
01254                      {
01255                        if (ocontents != NULL)
01256                          free (ocontents);
01257                        return FALSE;
01258                      }
01259 
01260                     elf_section_data (o)->this_hdr.contents = ocontents;
01261                   }
01262               }
01263 
01264              val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
01265              val += isym->st_value;
01266              if (val > addr && val < toaddr)
01267               bfd_put_32 (abfd, val - count,
01268                          ocontents + irelscan->r_offset);
01269            }
01270        }
01271     }
01272 
01273   /* Adjust the local symbols defined in this section.  */
01274   isymend = isymbuf + symtab_hdr->sh_info;
01275   for (isym = isymbuf; isym < isymend; isym++)
01276     {
01277       if (isym->st_shndx == sec_shndx
01278          && isym->st_value > addr
01279          && isym->st_value < toaddr)
01280        isym->st_value -= count;
01281     }
01282 
01283   /* Now adjust the global symbols defined in this section.  */
01284   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
01285              - symtab_hdr->sh_info);
01286   sym_hashes = elf_sym_hashes (abfd);
01287   end_hashes = sym_hashes + symcount;
01288   for (; sym_hashes < end_hashes; sym_hashes++)
01289     {
01290       struct elf_link_hash_entry *sym_hash = *sym_hashes;
01291       if ((sym_hash->root.type == bfd_link_hash_defined
01292           || sym_hash->root.type == bfd_link_hash_defweak)
01293          && sym_hash->root.u.def.section == sec
01294          && sym_hash->root.u.def.value > addr
01295          && sym_hash->root.u.def.value < toaddr)
01296        {
01297          sym_hash->root.u.def.value -= count;
01298        }
01299     }
01300 
01301   /* See if we can move the ALIGN reloc forward.  We have adjusted
01302      r_offset for it already.  */
01303   if (irelalign != NULL)
01304     {
01305       bfd_vma alignto, alignaddr;
01306 
01307       alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
01308       alignaddr = BFD_ALIGN (irelalign->r_offset,
01309                           1 << irelalign->r_addend);
01310       if (alignto != alignaddr)
01311        {
01312          /* Tail recursion.  */
01313          return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
01314                                        (int) (alignto - alignaddr));
01315        }
01316     }
01317 
01318   return TRUE;
01319 }
01320 
01321 /* Look for loads and stores which we can align to four byte
01322    boundaries.  This is like sh_align_loads in coff-sh.c.  */
01323 
01324 static bfd_boolean
01325 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
01326                   Elf_Internal_Rela *internal_relocs,
01327                   bfd_byte *contents ATTRIBUTE_UNUSED,
01328                   bfd_boolean *pswapped)
01329 {
01330   Elf_Internal_Rela *irel, *irelend;
01331   bfd_vma *labels = NULL;
01332   bfd_vma *label, *label_end;
01333   bfd_size_type amt;
01334 
01335   *pswapped = FALSE;
01336 
01337   irelend = internal_relocs + sec->reloc_count;
01338 
01339   /* Get all the addresses with labels on them.  */
01340   amt = sec->reloc_count;
01341   amt *= sizeof (bfd_vma);
01342   labels = (bfd_vma *) bfd_malloc (amt);
01343   if (labels == NULL)
01344     goto error_return;
01345   label_end = labels;
01346   for (irel = internal_relocs; irel < irelend; irel++)
01347     {
01348       if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
01349        {
01350          *label_end = irel->r_offset;
01351          ++label_end;
01352        }
01353     }
01354 
01355   /* Note that the assembler currently always outputs relocs in
01356      address order.  If that ever changes, this code will need to sort
01357      the label values and the relocs.  */
01358 
01359   label = labels;
01360 
01361   for (irel = internal_relocs; irel < irelend; irel++)
01362     {
01363       bfd_vma start, stop;
01364 
01365       if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
01366        continue;
01367 
01368       start = irel->r_offset;
01369 
01370       for (irel++; irel < irelend; irel++)
01371        if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
01372          break;
01373       if (irel < irelend)
01374        stop = irel->r_offset;
01375       else
01376        stop = sec->size;
01377 
01378       if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
01379                                  internal_relocs, &label,
01380                                  label_end, start, stop, pswapped))
01381        goto error_return;
01382     }
01383 
01384   free (labels);
01385 
01386   return TRUE;
01387 
01388  error_return:
01389   if (labels != NULL)
01390     free (labels);
01391   return FALSE;
01392 }
01393 
01394 #ifndef SH64_ELF
01395 /* Swap two SH instructions.  This is like sh_swap_insns in coff-sh.c.  */
01396 
01397 static bfd_boolean
01398 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
01399                  bfd_byte *contents, bfd_vma addr)
01400 {
01401   Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
01402   unsigned short i1, i2;
01403   Elf_Internal_Rela *irel, *irelend;
01404 
01405   /* Swap the instructions themselves.  */
01406   i1 = bfd_get_16 (abfd, contents + addr);
01407   i2 = bfd_get_16 (abfd, contents + addr + 2);
01408   bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
01409   bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
01410 
01411   /* Adjust all reloc addresses.  */
01412   irelend = internal_relocs + sec->reloc_count;
01413   for (irel = internal_relocs; irel < irelend; irel++)
01414     {
01415       enum elf_sh_reloc_type type;
01416       int add;
01417 
01418       /* There are a few special types of relocs that we don't want to
01419         adjust.  These relocs do not apply to the instruction itself,
01420         but are only associated with the address.  */
01421       type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
01422       if (type == R_SH_ALIGN
01423          || type == R_SH_CODE
01424          || type == R_SH_DATA
01425          || type == R_SH_LABEL)
01426        continue;
01427 
01428       /* If an R_SH_USES reloc points to one of the addresses being
01429         swapped, we must adjust it.  It would be incorrect to do this
01430         for a jump, though, since we want to execute both
01431         instructions after the jump.  (We have avoided swapping
01432         around a label, so the jump will not wind up executing an
01433         instruction it shouldn't).  */
01434       if (type == R_SH_USES)
01435        {
01436          bfd_vma off;
01437 
01438          off = irel->r_offset + 4 + irel->r_addend;
01439          if (off == addr)
01440            irel->r_offset += 2;
01441          else if (off == addr + 2)
01442            irel->r_offset -= 2;
01443        }
01444 
01445       if (irel->r_offset == addr)
01446        {
01447          irel->r_offset += 2;
01448          add = -2;
01449        }
01450       else if (irel->r_offset == addr + 2)
01451        {
01452          irel->r_offset -= 2;
01453          add = 2;
01454        }
01455       else
01456        add = 0;
01457 
01458       if (add != 0)
01459        {
01460          bfd_byte *loc;
01461          unsigned short insn, oinsn;
01462          bfd_boolean overflow;
01463 
01464          loc = contents + irel->r_offset;
01465          overflow = FALSE;
01466          switch (type)
01467            {
01468            default:
01469              break;
01470 
01471            case R_SH_DIR8WPN:
01472            case R_SH_DIR8WPZ:
01473              insn = bfd_get_16 (abfd, loc);
01474              oinsn = insn;
01475              insn += add / 2;
01476              if ((oinsn & 0xff00) != (insn & 0xff00))
01477               overflow = TRUE;
01478              bfd_put_16 (abfd, (bfd_vma) insn, loc);
01479              break;
01480 
01481            case R_SH_IND12W:
01482              insn = bfd_get_16 (abfd, loc);
01483              oinsn = insn;
01484              insn += add / 2;
01485              if ((oinsn & 0xf000) != (insn & 0xf000))
01486               overflow = TRUE;
01487              bfd_put_16 (abfd, (bfd_vma) insn, loc);
01488              break;
01489 
01490            case R_SH_DIR8WPL:
01491              /* This reloc ignores the least significant 3 bits of
01492                the program counter before adding in the offset.
01493                This means that if ADDR is at an even address, the
01494                swap will not affect the offset.  If ADDR is an at an
01495                odd address, then the instruction will be crossing a
01496                four byte boundary, and must be adjusted.  */
01497              if ((addr & 3) != 0)
01498               {
01499                 insn = bfd_get_16 (abfd, loc);
01500                 oinsn = insn;
01501                 insn += add / 2;
01502                 if ((oinsn & 0xff00) != (insn & 0xff00))
01503                   overflow = TRUE;
01504                 bfd_put_16 (abfd, (bfd_vma) insn, loc);
01505               }
01506 
01507              break;
01508            }
01509 
01510          if (overflow)
01511            {
01512              ((*_bfd_error_handler)
01513               (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
01514               abfd, (unsigned long) irel->r_offset));
01515              bfd_set_error (bfd_error_bad_value);
01516              return FALSE;
01517            }
01518        }
01519     }
01520 
01521   return TRUE;
01522 }
01523 #endif /* defined SH64_ELF */
01524 
01525 /* Describes one of the various PLT styles.  */
01526 
01527 struct elf_sh_plt_info
01528 {
01529   /* The template for the first PLT entry, or NULL if there is no special
01530      first entry.  */
01531   const bfd_byte *plt0_entry;
01532 
01533   /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL.  */
01534   bfd_vma plt0_entry_size;
01535 
01536   /* Index I is the offset into PLT0_ENTRY of a pointer to
01537      _GLOBAL_OFFSET_TABLE_ + I * 4.  The value is MINUS_ONE
01538      if there is no such pointer.  */
01539   bfd_vma plt0_got_fields[3];
01540 
01541   /* The template for a symbol's PLT entry.  */
01542   const bfd_byte *symbol_entry;
01543 
01544   /* The size of SYMBOL_ENTRY in bytes.  */
01545   bfd_vma symbol_entry_size;
01546 
01547   /* Byte offsets of fields in SYMBOL_ENTRY.  Not all fields are used
01548      on all targets.  The comments by each member indicate the value
01549      that the field must hold.  */
01550   struct {
01551     bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
01552     bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
01553     bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
01554   } symbol_fields;
01555 
01556   /* The offset of the resolver stub from the start of SYMBOL_ENTRY.  */
01557   bfd_vma symbol_resolve_offset;
01558 };
01559 
01560 #ifdef INCLUDE_SHMEDIA
01561 
01562 /* The size in bytes of an entry in the procedure linkage table.  */
01563 
01564 #define ELF_PLT_ENTRY_SIZE 64
01565 
01566 /* First entry in an absolute procedure linkage table look like this.  */
01567 
01568 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
01569 {
01570   0xcc, 0x00, 0x01, 0x10, /* movi  .got.plt >> 16, r17 */
01571   0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
01572   0x89, 0x10, 0x09, 0x90, /* ld.l  r17, 8, r25 */
01573   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
01574   0x89, 0x10, 0x05, 0x10, /* ld.l  r17, 4, r17 */
01575   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
01576   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01577   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01578   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01579   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01580   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01581   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01582   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01583   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01584   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01585   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01586 };
01587 
01588 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
01589 {
01590   0x10, 0x01, 0x00, 0xcc, /* movi  .got.plt >> 16, r17 */
01591   0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
01592   0x90, 0x09, 0x10, 0x89, /* ld.l  r17, 8, r25 */
01593   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
01594   0x10, 0x05, 0x10, 0x89, /* ld.l  r17, 4, r17 */
01595   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
01596   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01597   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01598   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01599   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01600   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01601   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01602   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01603   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01604   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01605   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01606 };
01607 
01608 /* Sebsequent entries in an absolute procedure linkage table look like
01609    this.  */
01610 
01611 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
01612 {
01613   0xcc, 0x00, 0x01, 0x90, /* movi  nameN-in-GOT >> 16, r25 */
01614   0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
01615   0x89, 0x90, 0x01, 0x90, /* ld.l  r25, 0, r25 */
01616   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
01617   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
01618   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01619   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01620   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01621   0xcc, 0x00, 0x01, 0x90, /* movi  .PLT0 >> 16, r25 */
01622   0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
01623   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
01624   0xcc, 0x00, 0x01, 0x50, /* movi  reloc-offset >> 16, r21 */
01625   0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
01626   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
01627   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01628   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01629 };
01630 
01631 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
01632 {
01633   0x90, 0x01, 0x00, 0xcc, /* movi  nameN-in-GOT >> 16, r25 */
01634   0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
01635   0x90, 0x01, 0x90, 0x89, /* ld.l  r25, 0, r25 */
01636   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
01637   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
01638   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01639   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01640   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01641   0x90, 0x01, 0x00, 0xcc, /* movi  .PLT0 >> 16, r25 */
01642   0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
01643   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
01644   0x50, 0x01, 0x00, 0xcc, /* movi  reloc-offset >> 16, r21 */
01645   0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
01646   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
01647   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01648   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01649 };
01650 
01651 /* Entries in a PIC procedure linkage table look like this.  */
01652 
01653 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
01654 {
01655   0xcc, 0x00, 0x01, 0x90, /* movi  nameN@GOT >> 16, r25 */
01656   0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
01657   0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
01658   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
01659   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
01660   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01661   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01662   0x6f, 0xf0, 0xff, 0xf0, /* nop */
01663   0xce, 0x00, 0x01, 0x10, /* movi  -GOT_BIAS, r17 */
01664   0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
01665   0x89, 0x10, 0x09, 0x90, /* ld.l  r17, 8, r25 */
01666   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
01667   0x89, 0x10, 0x05, 0x10, /* ld.l  r17, 4, r17 */
01668   0xcc, 0x00, 0x01, 0x50, /* movi  reloc-offset >> 16, r21 */
01669   0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
01670   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
01671 };
01672 
01673 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
01674 {
01675   0x90, 0x01, 0x00, 0xcc, /* movi  nameN@GOT >> 16, r25 */
01676   0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
01677   0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
01678   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
01679   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
01680   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01681   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01682   0xf0, 0xff, 0xf0, 0x6f, /* nop */
01683   0x10, 0x01, 0x00, 0xce, /* movi  -GOT_BIAS, r17 */
01684   0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
01685   0x90, 0x09, 0x10, 0x89, /* ld.l  r17, 8, r25 */
01686   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
01687   0x10, 0x05, 0x10, 0x89, /* ld.l  r17, 4, r17 */
01688   0x50, 0x01, 0x00, 0xcc, /* movi  reloc-offset >> 16, r21 */
01689   0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
01690   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
01691 };
01692 
01693 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
01694   {
01695     {
01696       /* Big-endian non-PIC.  */
01697       elf_sh_plt0_entry_be,
01698       ELF_PLT_ENTRY_SIZE,
01699       { 0, MINUS_ONE, MINUS_ONE },
01700       elf_sh_plt_entry_be,
01701       ELF_PLT_ENTRY_SIZE,
01702       { 0, 32, 48 },
01703       33 /* includes ISA encoding */
01704     },
01705     {
01706       /* Little-endian non-PIC.  */
01707       elf_sh_plt0_entry_le,
01708       ELF_PLT_ENTRY_SIZE,
01709       { 0, MINUS_ONE, MINUS_ONE },
01710       elf_sh_plt_entry_le,
01711       ELF_PLT_ENTRY_SIZE,
01712       { 0, 32, 48 },
01713       33 /* includes ISA encoding */
01714     },
01715   },
01716   {
01717     {
01718       /* Big-endian PIC.  */
01719       elf_sh_plt0_entry_be,
01720       ELF_PLT_ENTRY_SIZE,
01721       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
01722       elf_sh_pic_plt_entry_be,
01723       ELF_PLT_ENTRY_SIZE,
01724       { 0, MINUS_ONE, 52 },
01725       33 /* includes ISA encoding */
01726     },
01727     {
01728       /* Little-endian PIC.  */
01729       elf_sh_plt0_entry_le,
01730       ELF_PLT_ENTRY_SIZE,
01731       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
01732       elf_sh_pic_plt_entry_le,
01733       ELF_PLT_ENTRY_SIZE,
01734       { 0, MINUS_ONE, 52 },
01735       33 /* includes ISA encoding */
01736     },
01737   }
01738 };
01739 
01740 /* Return offset of the linker in PLT0 entry.  */
01741 #define elf_sh_plt0_gotplt_offset(info) 0
01742 
01743 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
01744    VALUE is the field's value and CODE_P is true if VALUE refers to code,
01745    not data.
01746 
01747    On SH64, each 32-bit field is loaded by a movi/shori pair.  */
01748 
01749 inline static void
01750 install_plt_field (bfd *output_bfd, bfd_boolean code_p,
01751                  unsigned long value, bfd_byte *addr)
01752 {
01753   value |= code_p;
01754   bfd_put_32 (output_bfd,
01755              bfd_get_32 (output_bfd, addr)
01756              | ((value >> 6) & 0x3fffc00),
01757              addr);
01758   bfd_put_32 (output_bfd,
01759              bfd_get_32 (output_bfd, addr + 4)
01760              | ((value << 10) & 0x3fffc00),
01761              addr + 4);
01762 }
01763 
01764 /* Return the type of PLT associated with ABFD.  PIC_P is true if
01765    the object is position-independent.  */
01766 
01767 static const struct elf_sh_plt_info *
01768 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
01769 {
01770   return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
01771 }
01772 #else
01773 /* The size in bytes of an entry in the procedure linkage table.  */
01774 
01775 #define ELF_PLT_ENTRY_SIZE 28
01776 
01777 /* First entry in an absolute procedure linkage table look like this.  */
01778 
01779 /* Note - this code has been "optimised" not to use r2.  r2 is used by
01780    GCC to return the address of large structures, so it should not be
01781    corrupted here.  This does mean however, that this PLT does not conform
01782    to the SH PIC ABI.  That spec says that r0 contains the type of the PLT
01783    and r2 contains the GOT id.  This version stores the GOT id in r0 and
01784    ignores the type.  Loaders can easily detect this difference however,
01785    since the type will always be 0 or 8, and the GOT ids will always be
01786    greater than or equal to 12.  */
01787 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
01788 {
01789   0xd0, 0x05, /* mov.l 2f,r0 */
01790   0x60, 0x02, /* mov.l @r0,r0 */
01791   0x2f, 0x06, /* mov.l r0,@-r15 */
01792   0xd0, 0x03, /* mov.l 1f,r0 */
01793   0x60, 0x02, /* mov.l @r0,r0 */
01794   0x40, 0x2b, /* jmp @r0 */
01795   0x60, 0xf6, /*  mov.l @r15+,r0 */
01796   0x00, 0x09, /* nop */
01797   0x00, 0x09, /* nop */
01798   0x00, 0x09, /* nop */
01799   0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8.  */
01800   0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4.  */
01801 };
01802 
01803 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
01804 {
01805   0x05, 0xd0, /* mov.l 2f,r0 */
01806   0x02, 0x60, /* mov.l @r0,r0 */
01807   0x06, 0x2f, /* mov.l r0,@-r15 */
01808   0x03, 0xd0, /* mov.l 1f,r0 */
01809   0x02, 0x60, /* mov.l @r0,r0 */
01810   0x2b, 0x40, /* jmp @r0 */
01811   0xf6, 0x60, /*  mov.l @r15+,r0 */
01812   0x09, 0x00, /* nop */
01813   0x09, 0x00, /* nop */
01814   0x09, 0x00, /* nop */
01815   0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8.  */
01816   0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4.  */
01817 };
01818 
01819 /* Sebsequent entries in an absolute procedure linkage table look like
01820    this.  */
01821 
01822 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
01823 {
01824   0xd0, 0x04, /* mov.l 1f,r0 */
01825   0x60, 0x02, /* mov.l @(r0,r12),r0 */
01826   0xd1, 0x02, /* mov.l 0f,r1 */
01827   0x40, 0x2b,   /* jmp @r0 */
01828   0x60, 0x13, /*  mov r1,r0 */
01829   0xd1, 0x03, /* mov.l 2f,r1 */
01830   0x40, 0x2b, /* jmp @r0 */
01831   0x00, 0x09, /* nop */
01832   0, 0, 0, 0, /* 0: replaced with address of .PLT0.  */
01833   0, 0, 0, 0, /* 1: replaced with address of this symbol in .got.  */
01834   0, 0, 0, 0, /* 2: replaced with offset into relocation table.  */
01835 };
01836 
01837 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
01838 {
01839   0x04, 0xd0, /* mov.l 1f,r0 */
01840   0x02, 0x60, /* mov.l @r0,r0 */
01841   0x02, 0xd1, /* mov.l 0f,r1 */
01842   0x2b, 0x40,   /* jmp @r0 */
01843   0x13, 0x60, /*  mov r1,r0 */
01844   0x03, 0xd1, /* mov.l 2f,r1 */
01845   0x2b, 0x40, /* jmp @r0 */
01846   0x09, 0x00, /*  nop */
01847   0, 0, 0, 0, /* 0: replaced with address of .PLT0.  */
01848   0, 0, 0, 0, /* 1: replaced with address of this symbol in .got.  */
01849   0, 0, 0, 0, /* 2: replaced with offset into relocation table.  */
01850 };
01851 
01852 /* Entries in a PIC procedure linkage table look like this.  */
01853 
01854 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
01855 {
01856   0xd0, 0x04, /* mov.l 1f,r0 */
01857   0x00, 0xce, /* mov.l @(r0,r12),r0 */
01858   0x40, 0x2b, /* jmp @r0 */
01859   0x00, 0x09, /*  nop */
01860   0x50, 0xc2, /* mov.l @(8,r12),r0 */
01861   0xd1, 0x03, /* mov.l 2f,r1 */
01862   0x40, 0x2b, /* jmp @r0 */
01863   0x50, 0xc1, /*  mov.l @(4,r12),r0 */
01864   0x00, 0x09, /* nop */
01865   0x00, 0x09, /* nop */
01866   0, 0, 0, 0, /* 1: replaced with address of this symbol in .got.  */
01867   0, 0, 0, 0    /* 2: replaced with offset into relocation table.  */
01868 };
01869 
01870 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
01871 {
01872   0x04, 0xd0, /* mov.l 1f,r0 */
01873   0xce, 0x00, /* mov.l @(r0,r12),r0 */
01874   0x2b, 0x40, /* jmp @r0 */
01875   0x09, 0x00, /*  nop */
01876   0xc2, 0x50, /* mov.l @(8,r12),r0 */
01877   0x03, 0xd1, /* mov.l 2f,r1 */
01878   0x2b, 0x40, /* jmp @r0 */
01879   0xc1, 0x50, /*  mov.l @(4,r12),r0 */
01880   0x09, 0x00, /*  nop */
01881   0x09, 0x00, /* nop */
01882   0, 0, 0, 0, /* 1: replaced with address of this symbol in .got.  */
01883   0, 0, 0, 0    /* 2: replaced with offset into relocation table.  */
01884 };
01885 
01886 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
01887   {
01888     {
01889       /* Big-endian non-PIC.  */
01890       elf_sh_plt0_entry_be,
01891       ELF_PLT_ENTRY_SIZE,
01892       { MINUS_ONE, 24, 20 },
01893       elf_sh_plt_entry_be,
01894       ELF_PLT_ENTRY_SIZE,
01895       { 20, 16, 24 },
01896       8
01897     },
01898     {
01899       /* Little-endian non-PIC.  */
01900       elf_sh_plt0_entry_le,
01901       ELF_PLT_ENTRY_SIZE,
01902       { MINUS_ONE, 24, 20 },
01903       elf_sh_plt_entry_le,
01904       ELF_PLT_ENTRY_SIZE,
01905       { 20, 16, 24 },
01906       8
01907     },
01908   },
01909   {
01910     {
01911       /* Big-endian PIC.  */
01912       elf_sh_plt0_entry_be,
01913       ELF_PLT_ENTRY_SIZE,
01914       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
01915       elf_sh_pic_plt_entry_be,
01916       ELF_PLT_ENTRY_SIZE,
01917       { 20, MINUS_ONE, 24 },
01918       8
01919     },
01920     {
01921       /* Little-endian PIC.  */
01922       elf_sh_plt0_entry_le,
01923       ELF_PLT_ENTRY_SIZE,
01924       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
01925       elf_sh_pic_plt_entry_le,
01926       ELF_PLT_ENTRY_SIZE,
01927       { 20, MINUS_ONE, 24 },
01928       8
01929     },
01930   }
01931 };
01932 
01933 #define VXWORKS_PLT_HEADER_SIZE 12
01934 #define VXWORKS_PLT_ENTRY_SIZE 24
01935 
01936 static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
01937 {
01938   0xd1, 0x01, /* mov.l @(8,pc),r1 */
01939   0x61, 0x12, /* mov.l @r1,r1 */
01940   0x41, 0x2b, /* jmp @r1 */
01941   0x00, 0x09, /* nop */
01942   0, 0, 0, 0  /* 0: replaced with _GLOBAL_OFFSET_TABLE+8.  */
01943 };
01944 
01945 static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
01946 {
01947   0x01, 0xd1, /* mov.l @(8,pc),r1 */
01948   0x12, 0x61, /* mov.l @r1,r1 */
01949   0x2b, 0x41, /* jmp @r1 */
01950   0x09, 0x00, /* nop */
01951   0, 0, 0, 0  /* 0: replaced with _GLOBAL_OFFSET_TABLE+8.  */
01952 };
01953 
01954 static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
01955 {
01956   0xd0, 0x01, /* mov.l @(8,pc),r0 */
01957   0x60, 0x02, /* mov.l @r0,r0 */
01958   0x40, 0x2b, /* jmp @r0 */
01959   0x00, 0x09, /* nop */
01960   0, 0, 0, 0, /* 0: replaced with address of this symbol in .got.  */
01961   0xd0, 0x01, /* mov.l @(8,pc),r0 */
01962   0xa0, 0x00, /* bra PLT (We need to fix the offset.)  */
01963   0x00, 0x09, /* nop */
01964   0x00, 0x09, /* nop */
01965   0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
01966 };
01967 
01968 static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
01969 {
01970   0x01, 0xd0, /* mov.l @(8,pc),r0 */
01971   0x02, 0x60, /* mov.l @r0,r0 */
01972   0x2b, 0x40, /* jmp @r0 */
01973   0x09, 0x00, /* nop */
01974   0, 0, 0, 0, /* 0: replaced with address of this symbol in .got.  */
01975   0x01, 0xd0, /* mov.l @(8,pc),r0 */
01976   0x00, 0xa0, /* bra PLT (We need to fix the offset.)  */
01977   0x09, 0x00, /* nop */
01978   0x09, 0x00, /* nop */
01979   0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
01980 };
01981 
01982 static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
01983 {
01984   0xd0, 0x01, /* mov.l @(8,pc),r0 */
01985   0x00, 0xce, /* mov.l @(r0,r12),r0 */
01986   0x40, 0x2b, /* jmp @r0 */
01987   0x00, 0x09, /* nop */
01988   0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got.  */
01989   0xd0, 0x01, /* mov.l @(8,pc),r0 */
01990   0x51, 0xc2, /* mov.l @(8,r12),r1 */
01991   0x41, 0x2b, /* jmp @r1 */
01992   0x00, 0x09, /* nop */
01993   0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
01994 };
01995 
01996 static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
01997 {
01998   0x01, 0xd0, /* mov.l @(8,pc),r0 */
01999   0xce, 0x00, /* mov.l @(r0,r12),r0 */
02000   0x2b, 0x40, /* jmp @r0 */
02001   0x09, 0x00, /* nop */
02002   0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got.  */
02003   0x01, 0xd0, /* mov.l @(8,pc),r0 */
02004   0xc2, 0x51, /* mov.l @(8,r12),r1 */
02005   0x2b, 0x41, /* jmp @r1 */
02006   0x09, 0x00, /* nop */
02007   0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
02008 };
02009 
02010 static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
02011   {
02012     {
02013       /* Big-endian non-PIC.  */
02014       vxworks_sh_plt0_entry_be,
02015       VXWORKS_PLT_HEADER_SIZE,
02016       { MINUS_ONE, MINUS_ONE, 8 },
02017       vxworks_sh_plt_entry_be,
02018       VXWORKS_PLT_ENTRY_SIZE,
02019       { 8, 14, 20 },
02020       12
02021     },
02022     {
02023       /* Little-endian non-PIC.  */
02024       vxworks_sh_plt0_entry_le,
02025       VXWORKS_PLT_HEADER_SIZE,
02026       { MINUS_ONE, MINUS_ONE, 8 },
02027       vxworks_sh_plt_entry_le,
02028       VXWORKS_PLT_ENTRY_SIZE,
02029       { 8, 14, 20 },
02030       12
02031     },
02032   },
02033   {
02034     {
02035       /* Big-endian PIC.  */
02036       NULL,
02037       0,
02038       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
02039       vxworks_sh_pic_plt_entry_be,
02040       VXWORKS_PLT_ENTRY_SIZE,
02041       { 8, MINUS_ONE, 20 },
02042       12
02043     },
02044     {
02045       /* Little-endian PIC.  */
02046       NULL,
02047       0,
02048       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
02049       vxworks_sh_pic_plt_entry_le,
02050       VXWORKS_PLT_ENTRY_SIZE,
02051       { 8, MINUS_ONE, 20 },
02052       12
02053     },
02054   }
02055 };
02056 
02057 /* Return the type of PLT associated with ABFD.  PIC_P is true if
02058    the object is position-independent.  */
02059 
02060 static const struct elf_sh_plt_info *
02061 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
02062 {
02063   if (vxworks_object_p (abfd))
02064     return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
02065   return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
02066 }
02067 
02068 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
02069    VALUE is the field's value and CODE_P is true if VALUE refers to code,
02070    not data.  */
02071 
02072 inline static void
02073 install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
02074                  unsigned long value, bfd_byte *addr)
02075 {
02076   bfd_put_32 (output_bfd, value, addr);
02077 }
02078 #endif
02079 
02080 /* Return the index of the PLT entry at byte offset OFFSET.  */
02081 
02082 static bfd_vma
02083 get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
02084 {
02085   return (offset - info->plt0_entry_size) / info->symbol_entry_size;
02086 }
02087 
02088 /* Do the inverse operation.  */
02089 
02090 static bfd_vma
02091 get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma index)
02092 {
02093   return info->plt0_entry_size + (index * info->symbol_entry_size);
02094 }
02095 
02096 /* The sh linker needs to keep track of the number of relocs that it
02097    decides to copy as dynamic relocs in check_relocs for each symbol.
02098    This is so that it can later discard them if they are found to be
02099    unnecessary.  We store the information in a field extending the
02100    regular ELF linker hash table.  */
02101 
02102 struct elf_sh_dyn_relocs
02103 {
02104   struct elf_sh_dyn_relocs *next;
02105 
02106   /* The input section of the reloc.  */
02107   asection *sec;
02108 
02109   /* Total number of relocs copied for the input section.  */
02110   bfd_size_type count;
02111 
02112   /* Number of pc-relative relocs copied for the input section.  */
02113   bfd_size_type pc_count;
02114 };
02115 
02116 /* sh ELF linker hash entry.  */
02117 
02118 struct elf_sh_link_hash_entry
02119 {
02120   struct elf_link_hash_entry root;
02121 
02122 #ifdef INCLUDE_SHMEDIA
02123   union
02124   {
02125     bfd_signed_vma refcount;
02126     bfd_vma offset;
02127   } datalabel_got;
02128 #endif
02129 
02130   /* Track dynamic relocs copied for this symbol.  */
02131   struct elf_sh_dyn_relocs *dyn_relocs;
02132 
02133   bfd_signed_vma gotplt_refcount;
02134 
02135   enum {
02136     GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE
02137   } tls_type;
02138 };
02139 
02140 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
02141 
02142 struct sh_elf_obj_tdata
02143 {
02144   struct elf_obj_tdata root;
02145 
02146   /* tls_type for each local got entry.  */
02147   char *local_got_tls_type;
02148 };
02149 
02150 #define sh_elf_tdata(abfd) \
02151   ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
02152 
02153 #define sh_elf_local_got_tls_type(abfd) \
02154   (sh_elf_tdata (abfd)->local_got_tls_type)
02155 
02156 /* Override the generic function because we need to store sh_elf_obj_tdata
02157    as the specific tdata.  */
02158 
02159 static bfd_boolean
02160 sh_elf_mkobject (bfd *abfd)
02161 {
02162   if (abfd->tdata.any == NULL)
02163     {
02164       bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
02165       abfd->tdata.any = bfd_zalloc (abfd, amt);
02166       if (abfd->tdata.any == NULL)
02167        return FALSE;
02168     }
02169   return bfd_elf_mkobject (abfd);
02170 }
02171 
02172 /* sh ELF linker hash table.  */
02173 
02174 struct elf_sh_link_hash_table
02175 {
02176   struct elf_link_hash_table root;
02177 
02178   /* Short-cuts to get to dynamic linker sections.  */
02179   asection *sgot;
02180   asection *sgotplt;
02181   asection *srelgot;
02182   asection *splt;
02183   asection *srelplt;
02184   asection *sdynbss;
02185   asection *srelbss;
02186 
02187   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
02188   asection *srelplt2;
02189 
02190   /* Small local sym to section mapping cache.  */
02191   struct sym_sec_cache sym_sec;
02192 
02193   /* A counter or offset to track a TLS got entry.  */
02194   union
02195     {
02196       bfd_signed_vma refcount;
02197       bfd_vma offset;
02198     } tls_ldm_got;
02199 
02200   /* The type of PLT to use.  */
02201   const struct elf_sh_plt_info *plt_info;
02202 
02203   /* True if the target system is VxWorks.  */
02204   bfd_boolean vxworks_p;
02205 };
02206 
02207 /* Traverse an sh ELF linker hash table.  */
02208 
02209 #define sh_elf_link_hash_traverse(table, func, info)                  \
02210   (elf_link_hash_traverse                                      \
02211    (&(table)->root,                                            \
02212     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
02213     (info)))
02214 
02215 /* Get the sh ELF linker hash table from a link_info structure.  */
02216 
02217 #define sh_elf_hash_table(p) \
02218   ((struct elf_sh_link_hash_table *) ((p)->hash))
02219 
02220 /* Create an entry in an sh ELF linker hash table.  */
02221 
02222 static struct bfd_hash_entry *
02223 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
02224                        struct bfd_hash_table *table,
02225                        const char *string)
02226 {
02227   struct elf_sh_link_hash_entry *ret =
02228     (struct elf_sh_link_hash_entry *) entry;
02229 
02230   /* Allocate the structure if it has not already been allocated by a
02231      subclass.  */
02232   if (ret == (struct elf_sh_link_hash_entry *) NULL)
02233     ret = ((struct elf_sh_link_hash_entry *)
02234           bfd_hash_allocate (table,
02235                            sizeof (struct elf_sh_link_hash_entry)));
02236   if (ret == (struct elf_sh_link_hash_entry *) NULL)
02237     return (struct bfd_hash_entry *) ret;
02238 
02239   /* Call the allocation method of the superclass.  */
02240   ret = ((struct elf_sh_link_hash_entry *)
02241         _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
02242                                  table, string));
02243   if (ret != (struct elf_sh_link_hash_entry *) NULL)
02244     {
02245       ret->dyn_relocs = NULL;
02246       ret->gotplt_refcount = 0;
02247 #ifdef INCLUDE_SHMEDIA
02248       ret->datalabel_got.refcount = ret->root.got.refcount;
02249 #endif
02250       ret->tls_type = GOT_UNKNOWN;
02251     }
02252 
02253   return (struct bfd_hash_entry *) ret;
02254 }
02255 
02256 /* Create an sh ELF linker hash table.  */
02257 
02258 static struct bfd_link_hash_table *
02259 sh_elf_link_hash_table_create (bfd *abfd)
02260 {
02261   struct elf_sh_link_hash_table *ret;
02262   bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
02263 
02264   ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
02265   if (ret == (struct elf_sh_link_hash_table *) NULL)
02266     return NULL;
02267 
02268   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
02269                                   sh_elf_link_hash_newfunc,
02270                                   sizeof (struct elf_sh_link_hash_entry)))
02271     {
02272       free (ret);
02273       return NULL;
02274     }
02275 
02276   ret->sgot = NULL;
02277   ret->sgotplt = NULL;
02278   ret->srelgot = NULL;
02279   ret->splt = NULL;
02280   ret->srelplt = NULL;
02281   ret->sdynbss = NULL;
02282   ret->srelbss = NULL;
02283   ret->srelplt2 = NULL;
02284   ret->sym_sec.abfd = NULL;
02285   ret->tls_ldm_got.refcount = 0;
02286   ret->plt_info = NULL;
02287   ret->vxworks_p = vxworks_object_p (abfd);
02288 
02289   return &ret->root.root;
02290 }
02291 
02292 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
02293    shortcuts to them in our hash table.  */
02294 
02295 static bfd_boolean
02296 create_got_section (bfd *dynobj, struct bfd_link_info *info)
02297 {
02298   struct elf_sh_link_hash_table *htab;
02299 
02300   if (! _bfd_elf_create_got_section (dynobj, info))
02301     return FALSE;
02302 
02303   htab = sh_elf_hash_table (info);
02304   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
02305   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
02306   if (! htab->sgot || ! htab->sgotplt)
02307     abort ();
02308 
02309   htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
02310                                           (SEC_ALLOC | SEC_LOAD
02311                                           | SEC_HAS_CONTENTS
02312                                           | SEC_IN_MEMORY
02313                                           | SEC_LINKER_CREATED
02314                                           | SEC_READONLY));
02315   if (htab->srelgot == NULL
02316       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
02317     return FALSE;
02318   return TRUE;
02319 }
02320 
02321 /* Create dynamic sections when linking against a dynamic object.  */
02322 
02323 static bfd_boolean
02324 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
02325 {
02326   struct elf_sh_link_hash_table *htab;
02327   flagword flags, pltflags;
02328   register asection *s;
02329   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
02330   int ptralign = 0;
02331 
02332   switch (bed->s->arch_size)
02333     {
02334     case 32:
02335       ptralign = 2;
02336       break;
02337 
02338     case 64:
02339       ptralign = 3;
02340       break;
02341 
02342     default:
02343       bfd_set_error (bfd_error_bad_value);
02344       return FALSE;
02345     }
02346 
02347   htab = sh_elf_hash_table (info);
02348   if (htab->root.dynamic_sections_created)
02349     return TRUE;
02350 
02351   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
02352      .rel[a].bss sections.  */
02353 
02354   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
02355           | SEC_LINKER_CREATED);
02356 
02357   pltflags = flags;
02358   pltflags |= SEC_CODE;
02359   if (bed->plt_not_loaded)
02360     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
02361   if (bed->plt_readonly)
02362     pltflags |= SEC_READONLY;
02363 
02364   s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
02365   htab->splt = s;
02366   if (s == NULL
02367       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
02368     return FALSE;
02369 
02370   if (bed->want_plt_sym)
02371     {
02372       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
02373         .plt section.  */
02374       struct elf_link_hash_entry *h;
02375       struct bfd_link_hash_entry *bh = NULL;
02376 
02377       if (! (_bfd_generic_link_add_one_symbol
02378             (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
02379              (bfd_vma) 0, (const char *) NULL, FALSE,
02380              get_elf_backend_data (abfd)->collect, &bh)))
02381        return FALSE;
02382 
02383       h = (struct elf_link_hash_entry *) bh;
02384       h->def_regular = 1;
02385       h->type = STT_OBJECT;
02386       htab->root.hplt = h;
02387 
02388       if (info->shared
02389          && ! bfd_elf_link_record_dynamic_symbol (info, h))
02390        return FALSE;
02391     }
02392 
02393   s = bfd_make_section_with_flags (abfd,
02394                                bed->default_use_rela_p ? ".rela.plt" : ".rel.plt",
02395                                flags | SEC_READONLY);
02396   htab->srelplt = s;
02397   if (s == NULL
02398       || ! bfd_set_section_alignment (abfd, s, ptralign))
02399     return FALSE;
02400 
02401   if (htab->sgot == NULL
02402       && !create_got_section (abfd, info))
02403     return FALSE;
02404 
02405   {
02406     const char *secname;
02407     char *relname;
02408     flagword secflags;
02409     asection *sec;
02410 
02411     for (sec = abfd->sections; sec; sec = sec->next)
02412       {
02413        secflags = bfd_get_section_flags (abfd, sec);
02414        if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
02415            || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
02416          continue;
02417        secname = bfd_get_section_name (abfd, sec);
02418        relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
02419        strcpy (relname, ".rela");
02420        strcat (relname, secname);
02421        if (bfd_get_section_by_name (abfd, secname))
02422          continue;
02423        s = bfd_make_section_with_flags (abfd, relname,
02424                                     flags | SEC_READONLY);
02425        if (s == NULL
02426            || ! bfd_set_section_alignment (abfd, s, ptralign))
02427          return FALSE;
02428       }
02429   }
02430 
02431   if (bed->want_dynbss)
02432     {
02433       /* The .dynbss section is a place to put symbols which are defined
02434         by dynamic objects, are referenced by regular objects, and are
02435         not functions.  We must allocate space for them in the process
02436         image and use a R_*_COPY reloc to tell the dynamic linker to
02437         initialize them at run time.  The linker script puts the .dynbss
02438         section into the .bss section of the final image.  */
02439       s = bfd_make_section_with_flags (abfd, ".dynbss",
02440                                    SEC_ALLOC | SEC_LINKER_CREATED);
02441       htab->sdynbss = s;
02442       if (s == NULL)
02443        return FALSE;
02444 
02445       /* The .rel[a].bss section holds copy relocs.  This section is not
02446         normally needed.  We need to create it here, though, so that the
02447         linker will map it to an output section.  We can't just create it
02448         only if we need it, because we will not know whether we need it
02449         until we have seen all the input files, and the first time the
02450         main linker code calls BFD after examining all the input files
02451         (size_dynamic_sections) the input sections have already been
02452         mapped to the output sections.  If the section turns out not to
02453         be needed, we can discard it later.  We will never need this
02454         section when generating a shared object, since they do not use
02455         copy relocs.  */
02456       if (! info->shared)
02457        {
02458          s = bfd_make_section_with_flags (abfd,
02459                                       (bed->default_use_rela_p
02460                                        ? ".rela.bss" : ".rel.bss"),
02461                                       flags | SEC_READONLY);
02462          htab->srelbss = s;
02463          if (s == NULL
02464              || ! bfd_set_section_alignment (abfd, s, ptralign))
02465            return FALSE;
02466        }
02467     }
02468 
02469   if (htab->vxworks_p)
02470     {
02471       if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
02472        return FALSE;
02473     }
02474 
02475   return TRUE;
02476 }
02477 
02478 /* Adjust a symbol defined by a dynamic object and referenced by a
02479    regular object.  The current definition is in some section of the
02480    dynamic object, but we're not including those sections.  We have to
02481    change the definition to something the rest of the link can
02482    understand.  */
02483 
02484 static bfd_boolean
02485 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
02486                            struct elf_link_hash_entry *h)
02487 {
02488   struct elf_sh_link_hash_table *htab;
02489   struct elf_sh_link_hash_entry *eh;
02490   struct elf_sh_dyn_relocs *p;
02491   asection *s;
02492   unsigned int power_of_two;
02493 
02494   htab = sh_elf_hash_table (info);
02495 
02496   /* Make sure we know what is going on here.  */
02497   BFD_ASSERT (htab->root.dynobj != NULL
02498              && (h->needs_plt
02499                 || h->u.weakdef != NULL
02500                 || (h->def_dynamic
02501                     && h->ref_regular
02502                     && !h->def_regular)));
02503 
02504   /* If this is a function, put it in the procedure linkage table.  We
02505      will fill in the contents of the procedure linkage table later,
02506      when we know the address of the .got section.  */
02507   if (h->type == STT_FUNC
02508       || h->needs_plt)
02509     {
02510       if (h->plt.refcount <= 0
02511          || SYMBOL_CALLS_LOCAL (info, h)
02512          || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
02513              && h->root.type == bfd_link_hash_undefweak))
02514        {
02515          /* This case can occur if we saw a PLT reloc in an input
02516             file, but the symbol was never referred to by a dynamic
02517             object.  In such a case, we don't actually need to build
02518             a procedure linkage table, and we can just do a REL32
02519             reloc instead.  */
02520          h->plt.offset = (bfd_vma) -1;
02521          h->needs_plt = 0;
02522        }
02523 
02524       return TRUE;
02525     }
02526   else
02527     h->plt.offset = (bfd_vma) -1;
02528 
02529   /* If this is a weak symbol, and there is a real definition, the
02530      processor independent code will have arranged for us to see the
02531      real definition first, and we can just use the same value.  */
02532   if (h->u.weakdef != NULL)
02533     {
02534       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
02535                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
02536       h->root.u.def.section = h->u.weakdef->root.u.def.section;
02537       h->root.u.def.value = h->u.weakdef->root.u.def.value;
02538       if (info->nocopyreloc)
02539        h->non_got_ref = h->u.weakdef->non_got_ref;
02540       return TRUE;
02541     }
02542 
02543   /* This is a reference to a symbol defined by a dynamic object which
02544      is not a function.  */
02545 
02546   /* If we are creating a shared library, we must presume that the
02547      only references to the symbol are via the global offset table.
02548      For such cases we need not do anything here; the relocations will
02549      be handled correctly by relocate_section.  */
02550   if (info->shared)
02551     return TRUE;
02552 
02553   /* If there are no references to this symbol that do not use the
02554      GOT, we don't need to generate a copy reloc.  */
02555   if (!h->non_got_ref)
02556     return TRUE;
02557 
02558   /* If -z nocopyreloc was given, we won't generate them either.  */
02559   if (info->nocopyreloc)
02560     {
02561       h->non_got_ref = 0;
02562       return TRUE;
02563     }
02564 
02565   eh = (struct elf_sh_link_hash_entry *) h;
02566   for (p = eh->dyn_relocs; p != NULL; p = p->next)
02567     {
02568       s = p->sec->output_section;
02569       if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
02570        break;
02571     }
02572 
02573   /* If we didn't find any dynamic relocs in sections which needs the
02574      copy reloc, then we'll be keeping the dynamic relocs and avoiding
02575      the copy reloc.  */
02576   if (p == NULL)
02577     {
02578       h->non_got_ref = 0;
02579       return TRUE;
02580     }
02581 
02582   if (h->size == 0)
02583     {
02584       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
02585                           h->root.root.string);
02586       return TRUE;
02587     }
02588 
02589   /* We must allocate the symbol in our .dynbss section, which will
02590      become part of the .bss section of the executable.  There will be
02591      an entry for this symbol in the .dynsym section.  The dynamic
02592      object will contain position independent code, so all references
02593      from the dynamic object to this symbol will go through the global
02594      offset table.  The dynamic linker will use the .dynsym entry to
02595      determine the address it must put in the global offset table, so
02596      both the dynamic object and the regular object will refer to the
02597      same memory location for the variable.  */
02598 
02599   s = htab->sdynbss;
02600   BFD_ASSERT (s != NULL);
02601 
02602   /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
02603      copy the initial value out of the dynamic object and into the
02604      runtime process image.  We need to remember the offset into the
02605      .rela.bss section we are going to use.  */
02606   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
02607     {
02608       asection *srel;
02609 
02610       srel = htab->srelbss;
02611       BFD_ASSERT (srel != NULL);
02612       srel->size += sizeof (Elf32_External_Rela);
02613       h->needs_copy = 1;
02614     }
02615 
02616   /* We need to figure out the alignment required for this symbol.  I
02617      have no idea how ELF linkers handle this.  */
02618   power_of_two = bfd_log2 (h->size);
02619   if (power_of_two > 3)
02620     power_of_two = 3;
02621 
02622   /* Apply the required alignment.  */
02623   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
02624   if (power_of_two > bfd_get_section_alignment (htab->root.dynobj, s))
02625     {
02626       if (! bfd_set_section_alignment (htab->root.dynobj, s, power_of_two))
02627        return FALSE;
02628     }
02629 
02630   /* Define the symbol as being at this point in the section.  */
02631   h->root.u.def.section = s;
02632   h->root.u.def.value = s->size;
02633 
02634   /* Increment the section size to make room for the symbol.  */
02635   s->size += h->size;
02636 
02637   return TRUE;
02638 }
02639 
02640 /* Allocate space in .plt, .got and associated reloc sections for
02641    dynamic relocs.  */
02642 
02643 static bfd_boolean
02644 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
02645 {
02646   struct bfd_link_info *info;
02647   struct elf_sh_link_hash_table *htab;
02648   struct elf_sh_link_hash_entry *eh;
02649   struct elf_sh_dyn_relocs *p;
02650 
02651   if (h->root.type == bfd_link_hash_indirect)
02652     return TRUE;
02653 
02654   if (h->root.type == bfd_link_hash_warning)
02655     /* When warning symbols are created, they **replace** the "real"
02656        entry in the hash table, thus we never get to see the real
02657        symbol in a hash traversal.  So look at it now.  */
02658     h = (struct elf_link_hash_entry *) h->root.u.i.link;
02659 
02660   info = (struct bfd_link_info *) inf;
02661   htab = sh_elf_hash_table (info);
02662 
02663   eh = (struct elf_sh_link_hash_entry *) h;
02664   if ((h->got.refcount > 0
02665        || h->forced_local)
02666       && eh->gotplt_refcount > 0)
02667     {
02668       /* The symbol has been forced local, or we have some direct got refs,
02669         so treat all the gotplt refs as got refs. */
02670       h->got.refcount += eh->gotplt_refcount;
02671       if (h->plt.refcount >= eh->gotplt_refcount)
02672        h->plt.refcount -= eh->gotplt_refcount;
02673     }
02674 
02675   if (htab->root.dynamic_sections_created
02676       && h->plt.refcount > 0
02677       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
02678          || h->root.type != bfd_link_hash_undefweak))
02679     {
02680       /* Make sure this symbol is output as a dynamic symbol.
02681         Undefined weak syms won't yet be marked as dynamic.  */
02682       if (h->dynindx == -1
02683          && !h->forced_local)
02684        {
02685          if (! bfd_elf_link_record_dynamic_symbol (info, h))
02686            return FALSE;
02687        }
02688 
02689       if (info->shared
02690          || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
02691        {
02692          asection *s = htab->splt;
02693 
02694          /* If this is the first .plt entry, make room for the special
02695             first entry.  */
02696          if (s->size == 0)
02697            s->size += htab->plt_info->plt0_entry_size;
02698 
02699          h->plt.offset = s->size;
02700 
02701          /* If this symbol is not defined in a regular file, and we are
02702             not generating a shared library, then set the symbol to this
02703             location in the .plt.  This is required to make function
02704             pointers compare as equal between the normal executable and
02705             the shared library.  */
02706          if (! info->shared
02707              && !h->def_regular)
02708            {
02709              h->root.u.def.section = s;
02710              h->root.u.def.value = h->plt.offset;
02711            }
02712 
02713          /* Make room for this entry.  */
02714          s->size += htab->plt_info->symbol_entry_size;
02715 
02716          /* We also need to make an entry in the .got.plt section, which
02717             will be placed in the .got section by the linker script.  */
02718          htab->sgotplt->size += 4;
02719 
02720          /* We also need to make an entry in the .rel.plt section.  */
02721          htab->srelplt->size += sizeof (Elf32_External_Rela);
02722 
02723          if (htab->vxworks_p && !info->shared)
02724            {
02725              /* VxWorks executables have a second set of relocations
02726                for each PLT entry.  They go in a separate relocation
02727                section, which is processed by the kernel loader.  */
02728 
02729              /* There is a relocation for the initial PLT entry:
02730                an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_.  */
02731              if (h->plt.offset == htab->plt_info->plt0_entry_size)
02732               htab->srelplt2->size += sizeof (Elf32_External_Rela);
02733 
02734              /* There are two extra relocations for each subsequent
02735                PLT entry: an R_SH_DIR32 relocation for the GOT entry,
02736                and an R_SH_DIR32 relocation for the PLT entry.  */
02737              htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
02738            }
02739        }
02740       else
02741        {
02742          h->plt.offset = (bfd_vma) -1;
02743          h->needs_plt = 0;
02744        }
02745     }
02746   else
02747     {
02748       h->plt.offset = (bfd_vma) -1;
02749       h->needs_plt = 0;
02750     }
02751 
02752   if (h->got.refcount > 0)
02753     {
02754       asection *s;
02755       bfd_boolean dyn;
02756       int tls_type = sh_elf_hash_entry (h)->tls_type;
02757 
02758       /* Make sure this symbol is output as a dynamic symbol.
02759         Undefined weak syms won't yet be marked as dynamic.  */
02760       if (h->dynindx == -1
02761          && !h->forced_local)
02762        {
02763          if (! bfd_elf_link_record_dynamic_symbol (info, h))
02764            return FALSE;
02765        }
02766 
02767       s = htab->sgot;
02768       h->got.offset = s->size;
02769       s->size += 4;
02770       /* R_SH_TLS_GD needs 2 consecutive GOT slots.  */
02771       if (tls_type == GOT_TLS_GD)
02772        s->size += 4;
02773       dyn = htab->root.dynamic_sections_created;
02774       /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
02775         R_SH_TLS_GD needs one if local symbol and two if global.  */
02776       if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
02777          || (tls_type == GOT_TLS_IE && dyn))
02778        htab->srelgot->size += sizeof (Elf32_External_Rela);
02779       else if (tls_type == GOT_TLS_GD)
02780        htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
02781       else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
02782               || h->root.type != bfd_link_hash_undefweak)
02783               && (info->shared
02784                  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
02785        htab->srelgot->size += sizeof (Elf32_External_Rela);
02786     }
02787   else
02788     h->got.offset = (bfd_vma) -1;
02789 
02790 #ifdef INCLUDE_SHMEDIA
02791   if (eh->datalabel_got.refcount > 0)
02792     {
02793       asection *s;
02794       bfd_boolean dyn;
02795 
02796       /* Make sure this symbol is output as a dynamic symbol.
02797         Undefined weak syms won't yet be marked as dynamic.  */
02798       if (h->dynindx == -1
02799          && !h->forced_local)
02800        {
02801          if (! bfd_elf_link_record_dynamic_symbol (info, h))
02802            return FALSE;
02803        }
02804 
02805       s = htab->sgot;
02806       eh->datalabel_got.offset = s->size;
02807       s->size += 4;
02808       dyn = htab->root.dynamic_sections_created;
02809       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
02810        htab->srelgot->size += sizeof (Elf32_External_Rela);
02811     }
02812   else
02813     eh->datalabel_got.offset = (bfd_vma) -1;
02814 #endif
02815 
02816   if (eh->dyn_relocs == NULL)
02817     return TRUE;
02818 
02819   /* In the shared -Bsymbolic case, discard space allocated for
02820      dynamic pc-relative relocs against symbols which turn out to be
02821      defined in regular objects.  For the normal shared case, discard
02822      space for pc-relative relocs that have become local due to symbol
02823      visibility changes.  */
02824 
02825   if (info->shared)
02826     {
02827       if (SYMBOL_CALLS_LOCAL (info, h))
02828        {
02829          struct elf_sh_dyn_relocs **pp;
02830 
02831          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
02832            {
02833              p->count -= p->pc_count;
02834              p->pc_count = 0;
02835              if (p->count == 0)
02836               *pp = p->next;
02837              else
02838               pp = &p->next;
02839            }
02840        }
02841 
02842       /* Also discard relocs on undefined weak syms with non-default
02843         visibility.  */
02844       if (eh->dyn_relocs != NULL
02845          && h->root.type == bfd_link_hash_undefweak)
02846        {
02847          if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
02848            eh->dyn_relocs = NULL;
02849 
02850          /* Make sure undefined weak symbols are output as a dynamic
02851             symbol in PIEs.  */
02852          else if (h->dynindx == -1
02853                  && !h->forced_local)
02854            {
02855              if (! bfd_elf_link_record_dynamic_symbol (info, h))
02856               return FALSE;
02857            }
02858        }
02859     }
02860   else
02861     {
02862       /* For the non-shared case, discard space for relocs against
02863         symbols which turn out to need copy relocs or are not
02864         dynamic.  */
02865 
02866       if (!h->non_got_ref
02867          && ((h->def_dynamic
02868               && !h->def_regular)
02869              || (htab->root.dynamic_sections_created
02870                 && (h->root.type == bfd_link_hash_undefweak
02871                     || h->root.type == bfd_link_hash_undefined))))
02872        {
02873          /* Make sure this symbol is output as a dynamic symbol.
02874             Undefined weak syms won't yet be marked as dynamic.  */
02875          if (h->dynindx == -1
02876              && !h->forced_local)
02877            {
02878              if (! bfd_elf_link_record_dynamic_symbol (info, h))
02879               return FALSE;
02880            }
02881 
02882          /* If that succeeded, we know we'll be keeping all the
02883             relocs.  */
02884          if (h->dynindx != -1)
02885            goto keep;
02886        }
02887 
02888       eh->dyn_relocs = NULL;
02889 
02890     keep: ;
02891     }
02892 
02893   /* Finally, allocate space.  */
02894   for (p = eh->dyn_relocs; p != NULL; p = p->next)
02895     {
02896       asection *sreloc = elf_section_data (p->sec)->sreloc;
02897       sreloc->size += p->count * sizeof (Elf32_External_Rela);
02898     }
02899 
02900   return TRUE;
02901 }
02902 
02903 /* Find any dynamic relocs that apply to read-only sections.  */
02904 
02905 static bfd_boolean
02906 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
02907 {
02908   struct elf_sh_link_hash_entry *eh;
02909   struct elf_sh_dyn_relocs *p;
02910 
02911   if (h->root.type == bfd_link_hash_warning)
02912     h = (struct elf_link_hash_entry *) h->root.u.i.link;
02913 
02914   eh = (struct elf_sh_link_hash_entry *) h;
02915   for (p = eh->dyn_relocs; p != NULL; p = p->next)
02916     {
02917       asection *s = p->sec->output_section;
02918 
02919       if (s != NULL && (s->flags & SEC_READONLY) != 0)
02920        {
02921          struct bfd_link_info *info = (struct bfd_link_info *) inf;
02922 
02923          info->flags |= DF_TEXTREL;
02924 
02925          /* Not an error, just cut short the traversal.  */
02926          return FALSE;
02927        }
02928     }
02929   return TRUE;
02930 }
02931 
02932 /* This function is called after all the input files have been read,
02933    and the input sections have been assigned to output sections.
02934    It's a convenient place to determine the PLT style.  */
02935 
02936 static bfd_boolean
02937 sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
02938 {
02939   sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd, info->shared);
02940   return TRUE;
02941 }
02942 
02943 /* Set the sizes of the dynamic sections.  */
02944 
02945 static bfd_boolean
02946 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
02947                            struct bfd_link_info *info)
02948 {
02949   struct elf_sh_link_hash_table *htab;
02950   bfd *dynobj;
02951   asection *s;
02952   bfd_boolean relocs;
02953   bfd *ibfd;
02954 
02955   htab = sh_elf_hash_table (info);
02956   dynobj = htab->root.dynobj;
02957   BFD_ASSERT (dynobj != NULL);
02958 
02959   if (htab->root.dynamic_sections_created)
02960     {
02961       /* Set the contents of the .interp section to the interpreter.  */
02962       if (info->executable)
02963        {
02964          s = bfd_get_section_by_name (dynobj, ".interp");
02965          BFD_ASSERT (s != NULL);
02966          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
02967          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
02968        }
02969     }
02970 
02971   /* Set up .got offsets for local syms, and space for local dynamic
02972      relocs.  */
02973   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
02974     {
02975       bfd_signed_vma *local_got;
02976       bfd_signed_vma *end_local_got;
02977       char *local_tls_type;
02978       bfd_size_type locsymcount;
02979       Elf_Internal_Shdr *symtab_hdr;
02980       asection *srel;
02981 
02982       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
02983        continue;
02984 
02985       for (s = ibfd->sections; s != NULL; s = s->next)
02986        {
02987          struct elf_sh_dyn_relocs *p;
02988 
02989          for (p = ((struct elf_sh_dyn_relocs *)
02990                   elf_section_data (s)->local_dynrel);
02991               p != NULL;
02992               p = p->next)
02993            {
02994              if (! bfd_is_abs_section (p->sec)
02995                 && bfd_is_abs_section (p->sec->output_section))
02996               {
02997                 /* Input section has been discarded, either because
02998                    it is a copy of a linkonce section or due to
02999                    linker script /DISCARD/, so we'll be discarding
03000                    the relocs too.  */
03001               }
03002              else if (p->count != 0)
03003               {
03004                 srel = elf_section_data (p->sec)->sreloc;
03005                 srel->size += p->count * sizeof (Elf32_External_Rela);
03006                 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
03007                   info->flags |= DF_TEXTREL;
03008               }
03009            }
03010        }
03011 
03012       local_got = elf_local_got_refcounts (ibfd);
03013       if (!local_got)
03014        continue;
03015 
03016       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
03017       locsymcount = symtab_hdr->sh_info;
03018 #ifdef INCLUDE_SHMEDIA
03019       /* Count datalabel local GOT.  */
03020       locsymcount *= 2;
03021 #endif
03022       end_local_got = local_got + locsymcount;
03023       local_tls_type = sh_elf_local_got_tls_type (ibfd);
03024       s = htab->sgot;
03025       srel = htab->srelgot;
03026       for (; local_got < end_local_got; ++local_got)
03027        {
03028          if (*local_got > 0)
03029            {
03030              *local_got = s->size;
03031              s->size += 4;
03032              if (*local_tls_type == GOT_TLS_GD)
03033               s->size += 4;
03034              if (info->shared)
03035               srel->size += sizeof (Elf32_External_Rela);
03036            }
03037          else
03038            *local_got = (bfd_vma) -1;
03039          ++local_tls_type;
03040        }
03041     }
03042 
03043   if (htab->tls_ldm_got.refcount > 0)
03044     {
03045       /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
03046         relocs.  */
03047       htab->tls_ldm_got.offset = htab->sgot->size;
03048       htab->sgot->size += 8;
03049       htab->srelgot->size += sizeof (Elf32_External_Rela);
03050     }
03051   else
03052     htab->tls_ldm_got.offset = -1;
03053 
03054   /* Allocate global sym .plt and .got entries, and space for global
03055      sym dynamic relocs.  */
03056   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
03057 
03058   /* We now have determined the sizes of the various dynamic sections.
03059      Allocate memory for them.  */
03060   relocs = FALSE;
03061   for (s = dynobj->sections; s != NULL; s = s->next)
03062     {
03063       if ((s->flags & SEC_LINKER_CREATED) == 0)
03064        continue;
03065 
03066       if (s == htab->splt
03067          || s == htab->sgot
03068          || s == htab->sgotplt
03069          || s == htab->sdynbss)
03070        {
03071          /* Strip this section if we don't need it; see the
03072             comment below.  */
03073        }
03074       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
03075        {
03076          if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
03077            relocs = TRUE;
03078 
03079          /* We use the reloc_count field as a counter if we need
03080             to copy relocs into the output file.  */
03081          s->reloc_count = 0;
03082        }
03083       else
03084        {
03085          /* It's not one of our sections, so don't allocate space.  */
03086          continue;
03087        }
03088 
03089       if (s->size == 0)
03090        {
03091          /* If we don't need this section, strip it from the
03092             output file.  This is mostly to handle .rela.bss and
03093             .rela.plt.  We must create both sections in
03094             create_dynamic_sections, because they must be created
03095             before the linker maps input sections to output
03096             sections.  The linker does that before
03097             adjust_dynamic_symbol is called, and it is that
03098             function which decides whether anything needs to go
03099             into these sections.  */
03100 
03101          s->flags |= SEC_EXCLUDE;
03102          continue;
03103        }
03104 
03105       if ((s->flags & SEC_HAS_CONTENTS) == 0)
03106        continue;
03107 
03108       /* Allocate memory for the section contents.  We use bfd_zalloc
03109         here in case unused entries are not reclaimed before the
03110         section's contents are written out.  This should not happen,
03111         but this way if it does, we get a R_SH_NONE reloc instead
03112         of garbage.  */
03113       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
03114       if (s->contents == NULL)
03115        return FALSE;
03116     }
03117 
03118   if (htab->root.dynamic_sections_created)
03119     {
03120       /* Add some entries to the .dynamic section.  We fill in the
03121         values later, in sh_elf_finish_dynamic_sections, but we
03122         must add the entries now so that we get the correct size for
03123         the .dynamic section.  The DT_DEBUG entry is filled in by the
03124         dynamic linker and used by the debugger.  */
03125 #define add_dynamic_entry(TAG, VAL) \
03126   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
03127 
03128       if (info->executable)
03129        {
03130          if (! add_dynamic_entry (DT_DEBUG, 0))
03131            return FALSE;
03132        }
03133 
03134       if (htab->splt->size != 0)
03135        {
03136          if (! add_dynamic_entry (DT_PLTGOT, 0)
03137              || ! add_dynamic_entry (DT_PLTRELSZ, 0)
03138              || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
03139              || ! add_dynamic_entry (DT_JMPREL, 0))
03140            return FALSE;
03141        }
03142 
03143       if (relocs)
03144        {
03145          if (! add_dynamic_entry (DT_RELA, 0)
03146              || ! add_dynamic_entry (DT_RELASZ, 0)
03147              || ! add_dynamic_entry (DT_RELAENT,
03148                                   sizeof (Elf32_External_Rela)))
03149            return FALSE;
03150 
03151          /* If any dynamic relocs apply to a read-only section,
03152             then we need a DT_TEXTREL entry.  */
03153          if ((info->flags & DF_TEXTREL) == 0)
03154            elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
03155 
03156          if ((info->flags & DF_TEXTREL) != 0)
03157            {
03158              if (! add_dynamic_entry (DT_TEXTREL, 0))
03159               return FALSE;
03160            }
03161        }
03162     }
03163 #undef add_dynamic_entry
03164 
03165   return TRUE;
03166 }
03167 
03168 /* Relocate an SH ELF section.  */
03169 
03170 static bfd_boolean
03171 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
03172                       bfd *input_bfd, asection *input_section,
03173                       bfd_byte *contents, Elf_Internal_Rela *relocs,
03174                       Elf_Internal_Sym *local_syms,
03175                       asection **local_sections)
03176 {
03177   struct elf_sh_link_hash_table *htab;
03178   Elf_Internal_Shdr *symtab_hdr;
03179   struct elf_link_hash_entry **sym_hashes;
03180   Elf_Internal_Rela *rel, *relend;
03181   bfd *dynobj;
03182   bfd_vma *local_got_offsets;
03183   asection *sgot;
03184   asection *sgotplt;
03185   asection *splt;
03186   asection *sreloc;
03187   asection *srelgot;
03188 
03189   htab = sh_elf_hash_table (info);
03190   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
03191   sym_hashes = elf_sym_hashes (input_bfd);
03192   dynobj = htab->root.dynobj;
03193   local_got_offsets = elf_local_got_offsets (input_bfd);
03194 
03195   sgot = htab->sgot;
03196   sgotplt = htab->sgotplt;
03197   splt = htab->splt;
03198   sreloc = NULL;
03199   srelgot = NULL;
03200 
03201   rel = relocs;
03202   relend = relocs + input_section->reloc_count;
03203   for (; rel < relend; rel++)
03204     {
03205       int r_type;
03206       reloc_howto_type *howto;
03207       unsigned long r_symndx;
03208       Elf_Internal_Sym *sym;
03209       asection *sec;
03210       struct elf_link_hash_entry *h;
03211       bfd_vma relocation;
03212       bfd_vma addend = (bfd_vma) 0;
03213       bfd_reloc_status_type r;
03214       int seen_stt_datalabel = 0;
03215       bfd_vma off;
03216       int tls_type;
03217 
03218       r_symndx = ELF32_R_SYM (rel->r_info);
03219 
03220       r_type = ELF32_R_TYPE (rel->r_info);
03221 
03222       /* Many of the relocs are only used for relaxing, and are
03223         handled entirely by the relaxation code.  */
03224       if (r_type >= (int) R_SH_GNU_VTINHERIT
03225          && r_type <= (int) R_SH_LABEL)
03226        continue;
03227       if (r_type == (int) R_SH_NONE)
03228        continue;
03229 
03230       if (r_type < 0
03231          || r_type >= R_SH_max
03232          || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
03233              && r_type <= (int) R_SH_LAST_INVALID_RELOC)
03234          || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
03235              && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
03236          || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
03237              && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
03238          || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
03239              && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
03240          || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
03241              && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
03242        {
03243          bfd_set_error (bfd_error_bad_value);
03244          return FALSE;
03245        }
03246 
03247       howto = get_howto_table (output_bfd) + r_type;
03248 
03249       /* For relocs that aren't partial_inplace, we get the addend from
03250         the relocation.  */
03251       if (! howto->partial_inplace)
03252        addend = rel->r_addend;
03253 
03254       h = NULL;
03255       sym = NULL;
03256       sec = NULL;
03257       if (r_symndx < symtab_hdr->sh_info)
03258        {
03259          sym = local_syms + r_symndx;
03260          sec = local_sections[r_symndx];
03261          relocation = (sec->output_section->vma
03262                      + sec->output_offset
03263                      + sym->st_value);
03264          /* A local symbol never has STO_SH5_ISA32, so we don't need
03265             datalabel processing here.  Make sure this does not change
03266             without notice.  */
03267          if ((sym->st_other & STO_SH5_ISA32) != 0)
03268            ((*info->callbacks->reloc_dangerous)
03269             (info,
03270              _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
03271              input_bfd, input_section, rel->r_offset));
03272 
03273          if (sec != NULL && elf_discarded_section (sec))
03274            /* Handled below.  */
03275            ;
03276          else if (info->relocatable)
03277            {
03278              /* This is a relocatable link.  We don't have to change
03279                anything, unless the reloc is against a section symbol,
03280                in which case we have to adjust according to where the
03281                section symbol winds up in the output section.  */
03282              if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
03283               {
03284                 if (! howto->partial_inplace)
03285                   {
03286                     /* For relocations with the addend in the
03287                       relocation, we need just to update the addend.
03288                       All real relocs are of type partial_inplace; this
03289                       code is mostly for completeness.  */
03290                     rel->r_addend += sec->output_offset;
03291 
03292                     continue;
03293                   }
03294 
03295                 /* Relocs of type partial_inplace need to pick up the
03296                    contents in the contents and add the offset resulting
03297                    from the changed location of the section symbol.
03298                    Using _bfd_final_link_relocate (e.g. goto
03299                    final_link_relocate) here would be wrong, because
03300                    relocations marked pc_relative would get the current
03301                    location subtracted, and we must only do that at the
03302                    final link.  */
03303                 r = _bfd_relocate_contents (howto, input_bfd,
03304                                          sec->output_offset
03305                                          + sym->st_value,
03306                                          contents + rel->r_offset);
03307                 goto relocation_done;
03308               }
03309 
03310              continue;
03311            }
03312          else if (! howto->partial_inplace)
03313            {
03314              relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
03315              addend = rel->r_addend;
03316            }
03317          else if ((sec->flags & SEC_MERGE)
03318                  && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
03319            {
03320              asection *msec;
03321 
03322              if (howto->rightshift || howto->src_mask != 0xffffffff)
03323               {
03324                 (*_bfd_error_handler)
03325                   (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
03326                    input_bfd, input_section,
03327                    (long) rel->r_offset, howto->name);
03328                 return FALSE;
03329               }
03330 
03331              addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
03332              msec = sec;
03333              addend =
03334               _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
03335               - relocation;
03336              addend += msec->output_section->vma + msec->output_offset;
03337              bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
03338              addend = 0;
03339            }
03340        }
03341       else
03342        {
03343          /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro.  */
03344 
03345          relocation = 0;
03346          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
03347          while (h->root.type == bfd_link_hash_indirect
03348                || h->root.type == bfd_link_hash_warning)
03349            {
03350 #ifdef INCLUDE_SHMEDIA
03351              /* If the reference passes a symbol marked with
03352                STT_DATALABEL, then any STO_SH5_ISA32 on the final value
03353                doesn't count.  */
03354              seen_stt_datalabel |= h->type == STT_DATALABEL;
03355 #endif
03356              h = (struct elf_link_hash_entry *) h->root.u.i.link;
03357            }
03358          if (h->root.type == bfd_link_hash_defined
03359              || h->root.type == bfd_link_hash_defweak)
03360            {
03361              bfd_boolean dyn;
03362 
03363              dyn = htab->root.dynamic_sections_created;
03364              sec = h->root.u.def.section;
03365              /* In these cases, we don't need the relocation value.
03366                We check specially because in some obscure cases
03367                sec->output_section will be NULL.  */
03368              if (r_type == R_SH_GOTPC
03369                 || r_type == R_SH_GOTPC_LOW16
03370                 || r_type == R_SH_GOTPC_MEDLOW16
03371                 || r_type == R_SH_GOTPC_MEDHI16
03372                 || r_type == R_SH_GOTPC_HI16
03373                 || ((r_type == R_SH_PLT32
03374                      || r_type == R_SH_PLT_LOW16
03375                      || r_type == R_SH_PLT_MEDLOW16
03376                      || r_type == R_SH_PLT_MEDHI16
03377                      || r_type == R_SH_PLT_HI16)
03378                     && h->plt.offset != (bfd_vma) -1)
03379                 || ((r_type == R_SH_GOT32
03380                      || r_type == R_SH_GOT_LOW16
03381                      || r_type == R_SH_GOT_MEDLOW16
03382                      || r_type == R_SH_GOT_MEDHI16
03383                      || r_type == R_SH_GOT_HI16)
03384                     && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
03385                     && (! info->shared
03386                        || (! info->symbolic && h->dynindx != -1)
03387                        || !h->def_regular))
03388                 /* The cases above are those in which relocation is
03389                    overwritten in the switch block below.  The cases
03390                    below are those in which we must defer relocation
03391                    to run-time, because we can't resolve absolute
03392                    addresses when creating a shared library.  */
03393                 || (info->shared
03394                     && ((! info->symbolic && h->dynindx != -1)
03395                        || !h->def_regular)
03396                     && ((r_type == R_SH_DIR32
03397                         && !h->forced_local)
03398                        || (r_type == R_SH_REL32
03399                            && !SYMBOL_CALLS_LOCAL (info, h)))
03400                     && ((input_section->flags & SEC_ALLOC) != 0
03401                        /* DWARF will emit R_SH_DIR32 relocations in its
03402                           sections against symbols defined externally
03403                           in shared libraries.  We can't do anything
03404                           with them here.  */
03405                        || ((input_section->flags & SEC_DEBUGGING) != 0
03406                            && h->def_dynamic)))
03407                 /* Dynamic relocs are not propagated for SEC_DEBUGGING
03408                    sections because such sections are not SEC_ALLOC and
03409                    thus ld.so will not process them.  */
03410                 || (sec->output_section == NULL
03411                     && ((input_section->flags & SEC_DEBUGGING) != 0
03412                        && h->def_dynamic))
03413                 || (sec->output_section == NULL
03414                     && (sh_elf_hash_entry (h)->tls_type == GOT_TLS_IE
03415                        || sh_elf_hash_entry (h)->tls_type == GOT_TLS_GD)))
03416               ;
03417              else if (sec->output_section != NULL)
03418               relocation = ((h->root.u.def.value
03419                            + sec->output_section->vma
03420                            + sec->output_offset)
03421                            /* A STO_SH5_ISA32 causes a "bitor 1" to the
03422                              symbol value, unless we've seen
03423                              STT_DATALABEL on the way to it.  */
03424                            | ((h->other & STO_SH5_ISA32) != 0
03425                              && ! seen_stt_datalabel));
03426              else if (!info->relocatable)
03427               {
03428                 (*_bfd_error_handler)
03429                   (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
03430                    input_bfd,
03431                    input_section,
03432                    (long) rel->r_offset,
03433                    howto->name,
03434                    h->root.root.string);
03435                 return FALSE;
03436               }
03437            }
03438          else if (h->root.type == bfd_link_hash_undefweak)
03439            ;
03440          else if (info->unresolved_syms_in_objects == RM_IGNORE
03441                  && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
03442            ;
03443          else if (!info->relocatable)
03444            {
03445              if (! info->callbacks->undefined_symbol
03446                 (info, h->root.root.string, input_bfd,
03447                  input_section, rel->r_offset,
03448                  (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
03449                   || ELF_ST_VISIBILITY (h->other))))
03450               return FALSE;
03451            }
03452        }
03453 
03454       if (sec != NULL && elf_discarded_section (sec))
03455        {
03456          /* For relocs against symbols from removed linkonce sections,
03457             or sections discarded by a linker script, we just want the
03458             section contents zeroed.  Avoid any special processing.  */
03459          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
03460          rel->r_info = 0;
03461          rel->r_addend = 0;
03462          continue;
03463        }
03464 
03465       if (info->relocatable)
03466        continue;
03467 
03468       switch ((int) r_type)
03469        {
03470        final_link_relocate:
03471          /* COFF relocs don't use the addend. The addend is used for
03472             R_SH_DIR32 to be compatible with other compilers.  */
03473          r = _bfd_final_link_relocate (howto, input_bfd, input_section,
03474                                    contents, rel->r_offset,
03475                                    relocation, addend);
03476          break;
03477 
03478        case R_SH_IND12W:
03479          goto final_link_relocate;
03480 
03481        case R_SH_DIR8WPN:
03482        case R_SH_DIR8WPZ:
03483        case R_SH_DIR8WPL:
03484          /* If the reloc is against the start of this section, then
03485             the assembler has already taken care of it and the reloc
03486             is here only to assist in relaxing.  If the reloc is not
03487             against the start of this section, then it's against an
03488             external symbol and we must deal with it ourselves.  */
03489          if (input_section->output_section->vma + input_section->output_offset
03490              != relocation)
03491            {
03492              int disp = (relocation
03493                        - input_section->output_section->vma
03494                        - input_section->output_offset
03495                        - rel->r_offset);
03496              int mask = 0;
03497              switch (r_type)
03498               {
03499               case R_SH_DIR8WPN:
03500               case R_SH_DIR8WPZ: mask = 1; break;
03501               case R_SH_DIR8WPL: mask = 3; break;
03502               default: mask = 0; break;
03503               }
03504              if (disp & mask)
03505               {
03506                 ((*_bfd_error_handler)
03507                  (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
03508                   input_section->owner,
03509                   (unsigned long) rel->r_offset));
03510                 bfd_set_error (bfd_error_bad_value);
03511                 return FALSE;
03512               }
03513              relocation -= 4;
03514              goto final_link_relocate;
03515            }
03516          r = bfd_reloc_ok;
03517          break;
03518 
03519        default:
03520 #ifdef INCLUDE_SHMEDIA
03521          if (shmedia_prepare_reloc (info, input_bfd, input_section,
03522                                  contents, rel, &relocation))
03523            goto final_link_relocate;
03524 #endif
03525          bfd_set_error (bfd_error_bad_value);
03526          return FALSE;
03527 
03528        case R_SH_DIR16:
03529        case R_SH_DIR8:
03530        case R_SH_DIR8U:
03531        case R_SH_DIR8S:
03532        case R_SH_DIR4U:
03533          goto final_link_relocate;
03534 
03535        case R_SH_DIR8UL:
03536        case R_SH_DIR4UL:
03537          if (relocation & 3)
03538            {
03539              ((*_bfd_error_handler)
03540               (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
03541               input_section->owner,
03542               (unsigned long) rel->r_offset, howto->name, 
03543               (unsigned long) relocation));
03544              bfd_set_error (bfd_error_bad_value);
03545              return FALSE;
03546            }
03547          goto final_link_relocate;
03548 
03549        case R_SH_DIR8UW:
03550        case R_SH_DIR8SW:
03551        case R_SH_DIR4UW:
03552          if (relocation & 1)
03553            {
03554              ((*_bfd_error_handler)
03555               (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
03556               input_section->owner,
03557               (unsigned long) rel->r_offset, howto->name, 
03558               (unsigned long) relocation));
03559              bfd_set_error (bfd_error_bad_value);
03560              return FALSE;
03561            }
03562          goto final_link_relocate;
03563 
03564        case R_SH_PSHA:
03565          if ((signed int)relocation < -32
03566              || (signed int)relocation > 32)
03567            {
03568              ((*_bfd_error_handler)
03569               (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
03570               input_section->owner,
03571               (unsigned long) rel->r_offset,
03572               (unsigned long) relocation));
03573              bfd_set_error (bfd_error_bad_value);
03574              return FALSE;
03575            }
03576          goto final_link_relocate;
03577 
03578        case R_SH_PSHL:
03579          if ((signed int)relocation < -16
03580              || (signed int)relocation > 16)
03581            {
03582              ((*_bfd_error_handler)
03583               (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
03584               input_section->owner,
03585               (unsigned long) rel->r_offset,
03586               (unsigned long) relocation));
03587              bfd_set_error (bfd_error_bad_value);
03588              return FALSE;
03589            }
03590          goto final_link_relocate;
03591 
03592        case R_SH_DIR32:
03593        case R_SH_REL32:
03594 #ifdef INCLUDE_SHMEDIA
03595        case R_SH_IMM_LOW16_PCREL:
03596        case R_SH_IMM_MEDLOW16_PCREL:
03597        case R_SH_IMM_MEDHI16_PCREL:
03598        case R_SH_IMM_HI16_PCREL:
03599 #endif
03600          if (info->shared
03601              && (h == NULL
03602                 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
03603                 || h->root.type != bfd_link_hash_undefweak)
03604              && r_symndx != 0
03605              && (input_section->flags & SEC_ALLOC) != 0
03606              && (r_type == R_SH_DIR32
03607                 || !SYMBOL_CALLS_LOCAL (info, h)))
03608            {
03609              Elf_Internal_Rela outrel;
03610              bfd_byte *loc;
03611              bfd_boolean skip, relocate;
03612 
03613              /* When generating a shared object, these relocations
03614                are copied into the output file to be resolved at run
03615                time.  */
03616 
03617              if (sreloc == NULL)
03618               {
03619                 const char *name;
03620 
03621                 name = (bfd_elf_string_from_elf_section
03622                        (input_bfd,
03623                         elf_elfheader (input_bfd)->e_shstrndx,
03624                         elf_section_data (input_section)->rel_hdr.sh_name));
03625                 if (name == NULL)
03626                   return FALSE;
03627 
03628                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
03629                            && strcmp (bfd_get_section_name (input_bfd,
03630                                                         input_section),
03631                                     name + 5) == 0);
03632 
03633                 sreloc = bfd_get_section_by_name (dynobj, name);
03634                 BFD_ASSERT (sreloc != NULL);
03635               }
03636 
03637              skip = FALSE;
03638              relocate = FALSE;
03639 
03640              outrel.r_offset =
03641               _bfd_elf_section_offset (output_bfd, info, input_section,
03642                                     rel->r_offset);
03643              if (outrel.r_offset == (bfd_vma) -1)
03644               skip = TRUE;
03645              else if (outrel.r_offset == (bfd_vma) -2)
03646               skip = TRUE, relocate = TRUE;
03647              outrel.r_offset += (input_section->output_section->vma
03648                               + input_section->output_offset);
03649 
03650              if (skip)
03651               memset (&outrel, 0, sizeof outrel);
03652              else if (r_type == R_SH_REL32)
03653               {
03654                 BFD_ASSERT (h != NULL && h->dynindx != -1);
03655                 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
03656                 outrel.r_addend
03657                   = (howto->partial_inplace
03658                      ? bfd_get_32 (input_bfd, contents + rel->r_offset)
03659                      : addend);
03660               }
03661 #ifdef INCLUDE_SHMEDIA
03662              else if (r_type == R_SH_IMM_LOW16_PCREL
03663                      || r_type == R_SH_IMM_MEDLOW16_PCREL
03664                      || r_type == R_SH_IMM_MEDHI16_PCREL
03665                      || r_type == R_SH_IMM_HI16_PCREL)
03666               {
03667                 BFD_ASSERT (h != NULL && h->dynindx != -1);
03668                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
03669                 outrel.r_addend = addend;
03670               }
03671 #endif
03672              else
03673               {
03674                 /* h->dynindx may be -1 if this symbol was marked to
03675                    become local.  */
03676                 if (h == NULL
03677                     || ((info->symbolic || h->dynindx == -1)
03678                        && h->def_regular))
03679                   {
03680                     relocate = howto->partial_inplace;
03681                     outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
03682                   }
03683                 else
03684                   {
03685                     BFD_ASSERT (h->dynindx != -1);
03686                     outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
03687                   }
03688                 outrel.r_addend = relocation;
03689                 outrel.r_addend
03690                   += (howto->partial_inplace
03691                      ? bfd_get_32 (input_bfd, contents + rel->r_offset)
03692                      : addend);
03693               }
03694 
03695              loc = sreloc->contents;
03696              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
03697              bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
03698 
03699              /* If this reloc is against an external symbol, we do
03700                not want to fiddle with the addend.  Otherwise, we
03701                need to include the symbol value so that it becomes
03702                an addend for the dynamic reloc.  */
03703              if (! relocate)
03704               continue;
03705            }
03706          goto final_link_relocate;
03707 
03708        case R_SH_GOTPLT32:
03709 #ifdef INCLUDE_SHMEDIA
03710        case R_SH_GOTPLT_LOW16:
03711        case R_SH_GOTPLT_MEDLOW16:
03712        case R_SH_GOTPLT_MEDHI16:
03713        case R_SH_GOTPLT_HI16:
03714        case R_SH_GOTPLT10BY4:
03715        case R_SH_GOTPLT10BY8:
03716 #endif
03717          /* Relocation is to the entry for this symbol in the
03718             procedure linkage table.  */
03719 
03720          if (h == NULL
03721              || h->forced_local
03722              || ! info->shared
03723              || info->symbolic
03724              || h->dynindx == -1
03725              || h->plt.offset == (bfd_vma) -1
03726              || h->got.offset != (bfd_vma) -1)
03727            goto force_got;
03728 
03729          /* Relocation is to the entry for this symbol in the global
03730             offset table extension for the procedure linkage table.  */
03731 
03732          BFD_ASSERT (sgotplt != NULL);
03733          relocation = (sgotplt->output_offset
03734                      + (get_plt_index (htab->plt_info, h->plt.offset)
03735                         + 3) * 4);
03736 
03737 #ifdef GOT_BIAS
03738          relocation -= GOT_BIAS;
03739 #endif
03740 
03741          goto final_link_relocate;
03742 
03743        force_got:
03744        case R_SH_GOT32:
03745 #ifdef INCLUDE_SHMEDIA
03746        case R_SH_GOT_LOW16:
03747        case R_SH_GOT_MEDLOW16:
03748        case R_SH_GOT_MEDHI16:
03749        case R_SH_GOT_HI16:
03750        case R_SH_GOT10BY4:
03751        case R_SH_GOT10BY8:
03752 #endif
03753          /* Relocation is to the entry for this symbol in the global
03754             offset table.  */
03755 
03756          BFD_ASSERT (sgot != NULL);
03757 
03758          if (h != NULL)
03759            {
03760              bfd_boolean dyn;
03761 
03762              off = h->got.offset;
03763 #ifdef INCLUDE_SHMEDIA
03764              if (seen_stt_datalabel)
03765               {
03766                 struct elf_sh_link_hash_entry *hsh;
03767 
03768                 hsh = (struct elf_sh_link_hash_entry *)h;
03769                 off = hsh->datalabel_got.offset;
03770               }
03771 #endif
03772              BFD_ASSERT (off != (bfd_vma) -1);
03773 
03774              dyn = htab->root.dynamic_sections_created;
03775              if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
03776                 || (info->shared
03777                     && SYMBOL_REFERENCES_LOCAL (info, h))
03778                 || (ELF_ST_VISIBILITY (h->other)
03779                     && h->root.type == bfd_link_hash_undefweak))
03780               {
03781                 /* This is actually a static link, or it is a
03782                    -Bsymbolic link and the symbol is defined
03783                    locally, or the symbol was forced to be local
03784                    because of a version file.  We must initialize
03785                    this entry in the global offset table.  Since the
03786                    offset must always be a multiple of 4, we use the
03787                    least significant bit to record whether we have
03788                    initialized it already.
03789 
03790                    When doing a dynamic link, we create a .rela.got
03791                    relocation entry to initialize the value.  This
03792                    is done in the finish_dynamic_symbol routine.  */
03793                 if ((off & 1) != 0)
03794                   off &= ~1;
03795                 else
03796                   {
03797                     bfd_put_32 (output_bfd, relocation,
03798                               sgot->contents + off);
03799 #ifdef INCLUDE_SHMEDIA
03800                     if (seen_stt_datalabel)
03801                      {
03802                        struct elf_sh_link_hash_entry *hsh;
03803 
03804                        hsh = (struct elf_sh_link_hash_entry *)h;
03805                        hsh->datalabel_got.offset |= 1;
03806                      }
03807                     else
03808 #endif
03809                      h->got.offset |= 1;
03810                   }
03811               }
03812 
03813              relocation = sgot->output_offset + off;
03814            }
03815          else
03816            {
03817 #ifdef INCLUDE_SHMEDIA
03818              if (rel->r_addend)
03819               {
03820                 BFD_ASSERT (local_got_offsets != NULL
03821                            && (local_got_offsets[symtab_hdr->sh_info
03822                                               + r_symndx]
03823                               != (bfd_vma) -1));
03824 
03825                 off = local_got_offsets[symtab_hdr->sh_info
03826                                      + r_symndx];
03827               }
03828              else
03829               {
03830 #endif
03831              BFD_ASSERT (local_got_offsets != NULL
03832                        && local_got_offsets[r_symndx] != (bfd_vma) -1);
03833 
03834              off = local_got_offsets[r_symndx];
03835 #ifdef INCLUDE_SHMEDIA
03836               }
03837 #endif
03838 
03839              /* The offset must always be a multiple of 4.  We use
03840                the least significant bit to record whether we have
03841                already generated the necessary reloc.  */
03842              if ((off & 1) != 0)
03843               off &= ~1;
03844              else
03845               {
03846                 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
03847 
03848                 if (info->shared)
03849                   {
03850                     Elf_Internal_Rela outrel;
03851                     bfd_byte *loc;
03852 
03853                     if (srelgot == NULL)
03854                      {
03855                        srelgot = bfd_get_section_by_name (dynobj,
03856                                                       ".rela.got");
03857                        BFD_ASSERT (srelgot != NULL);
03858                      }
03859 
03860                     outrel.r_offset = (sgot->output_section->vma
03861                                     + sgot->output_offset
03862                                     + off);
03863                     outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
03864                     outrel.r_addend = relocation;
03865                     loc = srelgot->contents;
03866                     loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
03867                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
03868                   }
03869 
03870 #ifdef INCLUDE_SHMEDIA
03871                 if (rel->r_addend)
03872                   local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
03873                 else
03874 #endif
03875                   local_got_offsets[r_symndx] |= 1;
03876               }
03877 
03878              relocation = sgot->output_offset + off;
03879            }
03880 
03881 #ifdef GOT_BIAS
03882          relocation -= GOT_BIAS;
03883 #endif
03884 
03885          goto final_link_relocate;
03886 
03887        case R_SH_GOTOFF:
03888 #ifdef INCLUDE_SHMEDIA
03889        case R_SH_GOTOFF_LOW16:
03890        case R_SH_GOTOFF_MEDLOW16:
03891        case R_SH_GOTOFF_MEDHI16:
03892        case R_SH_GOTOFF_HI16:
03893 #endif
03894          /* Relocation is relative to the start of the global offset
03895             table.  */
03896 
03897          BFD_ASSERT (sgot != NULL);
03898 
03899          /* Note that sgot->output_offset is not involved in this
03900             calculation.  We always want the start of .got.  If we
03901             defined _GLOBAL_OFFSET_TABLE in a different way, as is
03902             permitted by the ABI, we might have to change this
03903             calculation.  */
03904          relocation -= sgot->output_section->vma;
03905 
03906 #ifdef GOT_BIAS
03907          relocation -= GOT_BIAS;
03908 #endif
03909 
03910          addend = rel->r_addend;
03911 
03912          goto final_link_relocate;
03913 
03914        case R_SH_GOTPC:
03915 #ifdef INCLUDE_SHMEDIA
03916        case R_SH_GOTPC_LOW16:
03917        case R_SH_GOTPC_MEDLOW16:
03918        case R_SH_GOTPC_MEDHI16:
03919        case R_SH_GOTPC_HI16:
03920 #endif
03921          /* Use global offset table as symbol value.  */
03922 
03923          BFD_ASSERT (sgot != NULL);
03924          relocation = sgot->output_section->vma;
03925 
03926 #ifdef GOT_BIAS
03927          relocation += GOT_BIAS;
03928 #endif
03929 
03930          addend = rel->r_addend;
03931 
03932          goto final_link_relocate;
03933 
03934        case R_SH_PLT32:
03935 #ifdef INCLUDE_SHMEDIA
03936        case R_SH_PLT_LOW16:
03937        case R_SH_PLT_MEDLOW16:
03938        case R_SH_PLT_MEDHI16:
03939        case R_SH_PLT_HI16:
03940 #endif
03941          /* Relocation is to the entry for this symbol in the
03942             procedure linkage table.  */
03943 
03944          /* Resolve a PLT reloc against a local symbol directly,
03945             without using the procedure linkage table.  */
03946          if (h == NULL)
03947            goto final_link_relocate;
03948 
03949          if (h->forced_local)
03950            goto final_link_relocate;
03951 
03952          if (h->plt.offset == (bfd_vma) -1)
03953            {
03954              /* We didn't make a PLT entry for this symbol.  This
03955                happens when statically linking PIC code, or when
03956                using -Bsymbolic.  */
03957              goto final_link_relocate;
03958            }
03959 
03960          BFD_ASSERT (splt != NULL);
03961          relocation = (splt->output_section->vma
03962                      + splt->output_offset
03963                      + h->plt.offset);
03964 
03965 #ifdef INCLUDE_SHMEDIA
03966          relocation++;
03967 #endif
03968 
03969          addend = rel->r_addend;
03970 
03971          goto final_link_relocate;
03972 
03973        case R_SH_LOOP_START:
03974          {
03975            static bfd_vma start, end;
03976 
03977            start = (relocation + rel->r_addend
03978                    - (sec->output_section->vma + sec->output_offset));
03979            r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
03980                                rel->r_offset, sec, start, end);
03981            break;
03982 
03983        case R_SH_LOOP_END:
03984            end = (relocation + rel->r_addend
03985                  - (sec->output_section->vma + sec->output_offset));
03986            r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
03987                                rel->r_offset, sec, start, end);
03988            break;
03989          }
03990 
03991        case R_SH_TLS_GD_32:
03992        case R_SH_TLS_IE_32:
03993          r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
03994          tls_type = GOT_UNKNOWN;
03995          if (h == NULL && local_got_offsets)
03996            tls_type = sh_elf_local_got_tls_type (input_bfd) [r_symndx];
03997          else if (h != NULL)
03998            {
03999              tls_type = sh_elf_hash_entry (h)->tls_type;
04000              if (! info->shared
04001                 && (h->dynindx == -1
04002                     || h->def_regular))
04003               r_type = R_SH_TLS_LE_32;
04004            }
04005 
04006          if (r_type == R_SH_TLS_GD_32 && tls_type == GOT_TLS_IE)
04007            r_type = R_SH_TLS_IE_32;
04008 
04009          if (r_type == R_SH_TLS_LE_32)
04010            {
04011              bfd_vma offset;
04012              unsigned short insn;
04013 
04014              if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
04015               {
04016                 /* GD->LE transition:
04017                      mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
04018                      jsr @r1; add r12,r4; bra 3f; nop; .align 2;
04019                      1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
04020                    We change it into:
04021                      mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
04022                      nop; nop; ...
04023                      1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:.  */
04024 
04025                 offset = rel->r_offset;
04026                 BFD_ASSERT (offset >= 16);
04027                 /* Size of GD instructions is 16 or 18.  */
04028                 offset -= 16;
04029                 insn = bfd_get_16 (input_bfd, contents + offset + 0);
04030                 if ((insn & 0xff00) == 0xc700)
04031                   {
04032                     BFD_ASSERT (offset >= 2);
04033                     offset -= 2;
04034                     insn = bfd_get_16 (input_bfd, contents + offset + 0);
04035                   }
04036 
04037                 BFD_ASSERT ((insn & 0xff00) == 0xd400);
04038                 insn = bfd_get_16 (input_bfd, contents + offset + 2);
04039                 BFD_ASSERT ((insn & 0xff00) == 0xc700);
04040                 insn = bfd_get_16 (input_bfd, contents + offset + 4);
04041                 BFD_ASSERT ((insn & 0xff00) == 0xd100);
04042                 insn = bfd_get_16 (input_bfd, contents + offset + 6);
04043                 BFD_ASSERT (insn == 0x310c);
04044                 insn = bfd_get_16 (input_bfd, contents + offset + 8);
04045                 BFD_ASSERT (insn == 0x410b);
04046                 insn = bfd_get_16 (input_bfd, contents + offset + 10);
04047                 BFD_ASSERT (insn == 0x34cc);
04048 
04049                 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
04050                 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
04051                 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
04052                 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
04053                 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
04054               }
04055              else
04056               {
04057                 int index;
04058 
04059                 /* IE->LE transition:
04060                    mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
04061                    bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
04062                    We change it into:
04063                    mov.l .Ln,rM; stc gbr,rN; nop; ...;
04064                    1: x@TPOFF; 2:.  */
04065 
04066                 offset = rel->r_offset;
04067                 BFD_ASSERT (offset >= 16);
04068                 /* Size of IE instructions is 10 or 12.  */
04069                 offset -= 10;
04070                 insn = bfd_get_16 (input_bfd, contents + offset + 0);
04071                 if ((insn & 0xf0ff) == 0x0012)
04072                   {
04073                     BFD_ASSERT (offset >= 2);
04074                     offset -= 2;
04075                     insn = bfd_get_16 (input_bfd, contents + offset + 0);
04076                   }
04077 
04078                 BFD_ASSERT ((insn & 0xff00) == 0xd000);
04079                 index = insn & 0x00ff;
04080                 insn = bfd_get_16 (input_bfd, contents + offset + 2);
04081                 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
04082                 insn = bfd_get_16 (input_bfd, contents + offset + 4);
04083                 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
04084                 insn = 0xd000 | (insn & 0x0f00) | index;
04085                 bfd_put_16 (output_bfd, insn, contents + offset + 0);
04086                 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
04087               }
04088 
04089              bfd_put_32 (output_bfd, tpoff (info, relocation),
04090                        contents + rel->r_offset);
04091              continue;
04092            }
04093 
04094          sgot = htab->sgot;
04095          if (sgot == NULL)
04096            abort ();
04097 
04098          if (h != NULL)
04099            off = h->got.offset;
04100          else
04101            {
04102              if (local_got_offsets == NULL)
04103               abort ();
04104 
04105              off = local_got_offsets[r_symndx];
04106            }
04107 
04108          /* Relocate R_SH_TLS_IE_32 directly when statically linking.  */
04109          if (r_type == R_SH_TLS_IE_32
04110              && ! htab->root.dynamic_sections_created)
04111            {
04112              off &= ~1;
04113              bfd_put_32 (output_bfd, tpoff (info, relocation),
04114                        sgot->contents + off);
04115              bfd_put_32 (output_bfd, sgot->output_offset + off,
04116                        contents + rel->r_offset);
04117              continue;
04118            }
04119 
04120          if ((off & 1) != 0)
04121            off &= ~1;
04122          else
04123            {
04124              Elf_Internal_Rela outrel;
04125              bfd_byte *loc;
04126              int dr_type, indx;
04127 
04128              if (srelgot == NULL)
04129               {
04130                 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
04131                 BFD_ASSERT (srelgot != NULL);
04132               }
04133 
04134              outrel.r_offset = (sgot->output_section->vma
04135                              + sgot->output_offset + off);
04136 
04137              if (h == NULL || h->dynindx == -1)
04138               indx = 0;
04139              else
04140               indx = h->dynindx;
04141 
04142              dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
04143                       R_SH_TLS_TPOFF32);
04144              if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
04145               outrel.r_addend = relocation - dtpoff_base (info);
04146              else
04147               outrel.r_addend = 0;
04148              outrel.r_info = ELF32_R_INFO (indx, dr_type);
04149              loc = srelgot->contents;
04150              loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
04151              bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
04152 
04153              if (r_type == R_SH_TLS_GD_32)
04154               {
04155                 if (indx == 0)
04156                   {
04157                     bfd_put_32 (output_bfd,
04158                               relocation - dtpoff_base (info),
04159                               sgot->contents + off + 4);
04160                   }
04161                 else
04162                   {
04163                     outrel.r_info = ELF32_R_INFO (indx,
04164                                               R_SH_TLS_DTPOFF32);
04165                     outrel.r_offset += 4;
04166                     outrel.r_addend = 0;
04167                     srelgot->reloc_count++;
04168                     loc += sizeof (Elf32_External_Rela);
04169                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
04170                   }
04171               }
04172 
04173              if (h != NULL)
04174               h->got.offset |= 1;
04175              else
04176               local_got_offsets[r_symndx] |= 1;
04177            }
04178 
04179          if (off >= (bfd_vma) -2)
04180            abort ();
04181 
04182          if (r_type == (int) ELF32_R_TYPE (rel->r_info))
04183            relocation = sgot->output_offset + off;
04184          else
04185            {
04186              bfd_vma offset;
04187              unsigned short insn;
04188 
04189              /* GD->IE transition:
04190                  mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
04191                  jsr @r1; add r12,r4; bra 3f; nop; .align 2;
04192                  1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
04193                We change it into:
04194                  mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
04195                  nop; nop; bra 3f; nop; .align 2;
04196                  1: .long x@TPOFF; 2:...; 3:.  */
04197 
04198              offset = rel->r_offset;
04199              BFD_ASSERT (offset >= 16);
04200              /* Size of GD instructions is 16 or 18.  */
04201              offset -= 16;
04202              insn = bfd_get_16 (input_bfd, contents + offset + 0);
04203              if ((insn & 0xff00) == 0xc700)
04204               {
04205                 BFD_ASSERT (offset >= 2);
04206                 offset -= 2;
04207                 insn = bfd_get_16 (input_bfd, contents + offset + 0);
04208               }
04209 
04210              BFD_ASSERT ((insn & 0xff00) == 0xd400);
04211 
04212              /* Replace mov.l 1f,R4 with mov.l 1f,r0.  */
04213              bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
04214 
04215              insn = bfd_get_16 (input_bfd, contents + offset + 2);
04216              BFD_ASSERT ((insn & 0xff00) == 0xc700);
04217              insn = bfd_get_16 (input_bfd, contents + offset + 4);
04218              BFD_ASSERT ((insn & 0xff00) == 0xd100);
04219              insn = bfd_get_16 (input_bfd, contents + offset + 6);
04220              BFD_ASSERT (insn == 0x310c);
04221              insn = bfd_get_16 (input_bfd, contents + offset + 8);
04222              BFD_ASSERT (insn == 0x410b);
04223              insn = bfd_get_16 (input_bfd, contents + offset + 10);
04224              BFD_ASSERT (insn == 0x34cc);
04225 
04226              bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
04227              bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
04228              bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
04229              bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
04230              bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
04231 
04232              bfd_put_32 (output_bfd, sgot->output_offset + off,
04233                        contents + rel->r_offset);
04234 
04235              continue;
04236          }
04237 
04238          addend = rel->r_addend;
04239 
04240          goto final_link_relocate;
04241 
04242        case R_SH_TLS_LD_32:
04243          if (! info->shared)
04244            {
04245              bfd_vma offset;
04246              unsigned short insn;
04247 
04248              /* LD->LE transition:
04249                  mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
04250                  jsr @r1; add r12,r4; bra 3f; nop; .align 2;
04251                  1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
04252                We change it into:
04253                  stc gbr,r0; nop; nop; nop;
04254                  nop; nop; bra 3f; ...; 3:.  */
04255 
04256              offset = rel->r_offset;
04257              BFD_ASSERT (offset >= 16);
04258              /* Size of LD instructions is 16 or 18.  */
04259              offset -= 16;
04260              insn = bfd_get_16 (input_bfd, contents + offset + 0);
04261              if ((insn & 0xff00) == 0xc700)
04262               {
04263                 BFD_ASSERT (offset >= 2);
04264                 offset -= 2;
04265                 insn = bfd_get_16 (input_bfd, contents + offset + 0);
04266               }
04267 
04268              BFD_ASSERT ((insn & 0xff00) == 0xd400);
04269              insn = bfd_get_16 (input_bfd, contents + offset + 2);
04270              BFD_ASSERT ((insn & 0xff00) == 0xc700);
04271              insn = bfd_get_16 (input_bfd, contents + offset + 4);
04272              BFD_ASSERT ((insn & 0xff00) == 0xd100);
04273              insn = bfd_get_16 (input_bfd, contents + offset + 6);
04274              BFD_ASSERT (insn == 0x310c);
04275              insn = bfd_get_16 (input_bfd, contents + offset + 8);
04276              BFD_ASSERT (insn == 0x410b);
04277              insn = bfd_get_16 (input_bfd, contents + offset + 10);
04278              BFD_ASSERT (insn == 0x34cc);
04279 
04280              bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
04281              bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
04282              bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
04283              bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
04284              bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
04285              bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
04286 
04287              continue;
04288            }
04289 
04290          sgot = htab->sgot;
04291          if (sgot == NULL)
04292            abort ();
04293 
04294          off = htab->tls_ldm_got.offset;
04295          if (off & 1)
04296            off &= ~1;
04297          else
04298            {
04299              Elf_Internal_Rela outrel;
04300              bfd_byte *loc;
04301 
04302              srelgot = htab->srelgot;
04303              if (srelgot == NULL)
04304               abort ();
04305 
04306              outrel.r_offset = (sgot->output_section->vma
04307                              + sgot->output_offset + off);
04308              outrel.r_addend = 0;
04309              outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
04310              loc = srelgot->contents;
04311              loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
04312              bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
04313              htab->tls_ldm_got.offset |= 1;
04314            }
04315 
04316          relocation = sgot->output_offset + off;
04317          addend = rel->r_addend;
04318 
04319          goto final_link_relocate;
04320 
04321        case R_SH_TLS_LDO_32:
04322          if (! info->shared)
04323            relocation = tpoff (info, relocation);
04324          else
04325            relocation -= dtpoff_base (info);
04326 
04327          addend = rel->r_addend;
04328          goto final_link_relocate;
04329 
04330        case R_SH_TLS_LE_32:
04331          {
04332            int indx;
04333            Elf_Internal_Rela outrel;
04334            bfd_byte *loc;
04335 
04336            if (! info->shared)
04337              {
04338               relocation = tpoff (info, relocation);
04339               addend = rel->r_addend;
04340               goto final_link_relocate;
04341              }
04342 
04343            if (sreloc == NULL)
04344              {
04345               const char *name;
04346 
04347               name = (bfd_elf_string_from_elf_section
04348                      (input_bfd,
04349                       elf_elfheader (input_bfd)->e_shstrndx,
04350                       elf_section_data (input_section)->rel_hdr.sh_name));
04351               if (name == NULL)
04352                 return FALSE;
04353 
04354               BFD_ASSERT (CONST_STRNEQ (name, ".rela")
04355                          && strcmp (bfd_get_section_name (input_bfd,
04356                                                       input_section),
04357                                    name + 5) == 0);
04358 
04359               sreloc = bfd_get_section_by_name (dynobj, name);
04360               BFD_ASSERT (sreloc != NULL);
04361              }
04362 
04363            if (h == NULL || h->dynindx == -1)
04364              indx = 0;
04365            else
04366              indx = h->dynindx;
04367 
04368            outrel.r_offset = (input_section->output_section->vma
04369                             + input_section->output_offset
04370                             + rel->r_offset);
04371            outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
04372            if (indx == 0)
04373              outrel.r_addend = relocation - dtpoff_base (info);
04374            else
04375              outrel.r_addend = 0;
04376 
04377            loc = sreloc->contents;
04378            loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
04379            bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
04380            continue;
04381          }
04382        }
04383 
04384     relocation_done:
04385       if (r != bfd_reloc_ok)
04386        {
04387          switch (r)
04388            {
04389            default:
04390            case bfd_reloc_outofrange:
04391              abort ();
04392            case bfd_reloc_overflow:
04393              {
04394               const char *name;
04395 
04396               if (h != NULL)
04397                 name = NULL;
04398               else
04399                 {
04400                   name = (bfd_elf_string_from_elf_section
04401                          (input_bfd, symtab_hdr->sh_link, sym->st_name));
04402                   if (name == NULL)
04403                     return FALSE;
04404                   if (*name == '\0')
04405                     name = bfd_section_name (input_bfd, sec);
04406                 }
04407               if (! ((*info->callbacks->reloc_overflow)
04408                      (info, (h ? &h->root : NULL), name, howto->name,
04409                      (bfd_vma) 0, input_bfd, input_section,
04410                      rel->r_offset)))
04411                 return FALSE;
04412              }
04413              break;
04414            }
04415        }
04416     }
04417 
04418   return TRUE;
04419 }
04420 
04421 /* This is a version of bfd_generic_get_relocated_section_contents
04422    which uses sh_elf_relocate_section.  */
04423 
04424 static bfd_byte *
04425 sh_elf_get_relocated_section_contents (bfd *output_bfd,
04426                                    struct bfd_link_info *link_info,
04427                                    struct bfd_link_order *link_order,
04428                                    bfd_byte *data,
04429                                    bfd_boolean relocatable,
04430                                    asymbol **symbols)
04431 {
04432   Elf_Internal_Shdr *symtab_hdr;
04433   asection *input_section = link_order->u.indirect.section;
04434   bfd *input_bfd = input_section->owner;
04435   asection **sections = NULL;
04436   Elf_Internal_Rela *internal_relocs = NULL;
04437   Elf_Internal_Sym *isymbuf = NULL;
04438 
04439   /* We only need to handle the case of relaxing, or of having a
04440      particular set of section contents, specially.  */
04441   if (relocatable
04442       || elf_section_data (input_section)->this_hdr.contents == NULL)
04443     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
04444                                                  link_order, data,
04445                                                  relocatable,
04446                                                  symbols);
04447 
04448   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
04449 
04450   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
04451          (size_t) input_section->size);
04452 
04453   if ((input_section->flags & SEC_RELOC) != 0
04454       && input_section->reloc_count > 0)
04455     {
04456       asection **secpp;
04457       Elf_Internal_Sym *isym, *isymend;
04458       bfd_size_type amt;
04459 
04460       internal_relocs = (_bfd_elf_link_read_relocs
04461                       (input_bfd, input_section, NULL,
04462                        (Elf_Internal_Rela *) NULL, FALSE));
04463       if (internal_relocs == NULL)
04464        goto error_return;
04465 
04466       if (symtab_hdr->sh_info != 0)
04467        {
04468          isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
04469          if (isymbuf == NULL)
04470            isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
04471                                        symtab_hdr->sh_info, 0,
04472                                        NULL, NULL, NULL);
04473          if (isymbuf == NULL)
04474            goto error_return;
04475        }
04476 
04477       amt = symtab_hdr->sh_info;
04478       amt *= sizeof (asection *);
04479       sections = (asection **) bfd_malloc (amt);
04480       if (sections == NULL && amt != 0)
04481        goto error_return;
04482 
04483       isymend = isymbuf + symtab_hdr->sh_info;
04484       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
04485        {
04486          asection *isec;
04487 
04488          if (isym->st_shndx == SHN_UNDEF)
04489            isec = bfd_und_section_ptr;
04490          else if (isym->st_shndx == SHN_ABS)
04491            isec = bfd_abs_section_ptr;
04492          else if (isym->st_shndx == SHN_COMMON)
04493            isec = bfd_com_section_ptr;
04494          else
04495            isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
04496 
04497          *secpp = isec;
04498        }
04499 
04500       if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
04501                                  input_section, data, internal_relocs,
04502                                  isymbuf, sections))
04503        goto error_return;
04504 
04505       if (sections != NULL)
04506        free (sections);
04507       if (isymbuf != NULL
04508          && symtab_hdr->contents != (unsigned char *) isymbuf)
04509        free (isymbuf);
04510       if (elf_section_data (input_section)->relocs != internal_relocs)
04511        free (internal_relocs);
04512     }
04513 
04514   return data;
04515 
04516  error_return:
04517   if (sections != NULL)
04518     free (sections);
04519   if (isymbuf != NULL
04520       && symtab_hdr->contents != (unsigned char *) isymbuf)
04521     free (isymbuf);
04522   if (internal_relocs != NULL
04523       && elf_section_data (input_section)->relocs != internal_relocs)
04524     free (internal_relocs);
04525   return NULL;
04526 }
04527 
04528 /* Return the base VMA address which should be subtracted from real addresses
04529    when resolving @dtpoff relocation.
04530    This is PT_TLS segment p_vaddr.  */
04531 
04532 static bfd_vma
04533 dtpoff_base (struct bfd_link_info *info)
04534 {
04535   /* If tls_sec is NULL, we should have signalled an error already.  */
04536   if (elf_hash_table (info)->tls_sec == NULL)
04537     return 0;
04538   return elf_hash_table (info)->tls_sec->vma;
04539 }
04540 
04541 /* Return the relocation value for R_SH_TLS_TPOFF32..  */
04542 
04543 static bfd_vma
04544 tpoff (struct bfd_link_info *info, bfd_vma address)
04545 {
04546   /* If tls_sec is NULL, we should have signalled an error already.  */
04547   if (elf_hash_table (info)->tls_sec == NULL)
04548     return 0;
04549   /* SH TLS ABI is variant I and static TLS block start just after tcbhead
04550      structure which has 2 pointer fields.  */
04551   return (address - elf_hash_table (info)->tls_sec->vma
04552          + align_power ((bfd_vma) 8,
04553                       elf_hash_table (info)->tls_sec->alignment_power));
04554 }
04555 
04556 static asection *
04557 sh_elf_gc_mark_hook (asection *sec,
04558                    struct bfd_link_info *info,
04559                    Elf_Internal_Rela *rel,
04560                    struct elf_link_hash_entry *h,
04561                    Elf_Internal_Sym *sym)
04562 {
04563   if (h != NULL)
04564     switch (ELF32_R_TYPE (rel->r_info))
04565       {
04566       case R_SH_GNU_VTINHERIT:
04567       case R_SH_GNU_VTENTRY:
04568        return NULL;
04569       }
04570 
04571   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
04572 }
04573 
04574 /* Update the got entry reference counts for the section being removed.  */
04575 
04576 static bfd_boolean
04577 sh_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
04578                     asection *sec, const Elf_Internal_Rela *relocs)
04579 {
04580   Elf_Internal_Shdr *symtab_hdr;
04581   struct elf_link_hash_entry **sym_hashes;
04582   bfd_signed_vma *local_got_refcounts;
04583   const Elf_Internal_Rela *rel, *relend;
04584 
04585   elf_section_data (sec)->local_dynrel = NULL;
04586 
04587   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
04588   sym_hashes = elf_sym_hashes (abfd);
04589   local_got_refcounts = elf_local_got_refcounts (abfd);
04590 
04591   relend = relocs + sec->reloc_count;
04592   for (rel = relocs; rel < relend; rel++)
04593     {
04594       unsigned long r_symndx;
04595       unsigned int r_type;
04596       struct elf_link_hash_entry *h = NULL;
04597 #ifdef INCLUDE_SHMEDIA
04598       int seen_stt_datalabel = 0;
04599 #endif
04600 
04601       r_symndx = ELF32_R_SYM (rel->r_info);
04602       if (r_symndx >= symtab_hdr->sh_info)
04603        {
04604          struct elf_sh_link_hash_entry *eh;
04605          struct elf_sh_dyn_relocs **pp;
04606          struct elf_sh_dyn_relocs *p;
04607 
04608          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
04609          while (h->root.type == bfd_link_hash_indirect
04610                || h->root.type == bfd_link_hash_warning)
04611            {
04612 #ifdef INCLUDE_SHMEDIA
04613              seen_stt_datalabel |= h->type == STT_DATALABEL;
04614 #endif
04615              h = (struct elf_link_hash_entry *) h->root.u.i.link;
04616            }
04617          eh = (struct elf_sh_link_hash_entry *) h;
04618          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
04619            if (p->sec == sec)
04620              {
04621               /* Everything must go for SEC.  */
04622               *pp = p->next;
04623               break;
04624              }
04625        }
04626 
04627       r_type = ELF32_R_TYPE (rel->r_info);
04628       switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL))
04629        {
04630        case R_SH_TLS_LD_32:
04631          if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
04632            sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
04633          break;
04634 
04635        case R_SH_GOT32:
04636        case R_SH_GOTOFF:
04637        case R_SH_GOTPC:
04638 #ifdef INCLUDE_SHMEDIA
04639        case R_SH_GOT_LOW16:
04640        case R_SH_GOT_MEDLOW16:
04641        case R_SH_GOT_MEDHI16:
04642        case R_SH_GOT_HI16:
04643        case R_SH_GOT10BY4:
04644        case R_SH_GOT10BY8:
04645        case R_SH_GOTOFF_LOW16:
04646        case R_SH_GOTOFF_MEDLOW16:
04647        case R_SH_GOTOFF_MEDHI16:
04648        case R_SH_GOTOFF_HI16:
04649        case R_SH_GOTPC_LOW16:
04650        case R_SH_GOTPC_MEDLOW16:
04651        case R_SH_GOTPC_MEDHI16:
04652        case R_SH_GOTPC_HI16:
04653 #endif
04654        case R_SH_TLS_GD_32:
04655        case R_SH_TLS_IE_32:
04656          if (h != NULL)
04657            {
04658 #ifdef INCLUDE_SHMEDIA
04659              if (seen_stt_datalabel)
04660               {
04661                 struct elf_sh_link_hash_entry *eh;
04662                 eh = (struct elf_sh_link_hash_entry *) h;
04663                 if (eh->datalabel_got.refcount > 0)
04664                   eh->datalabel_got.refcount -= 1;
04665               }
04666              else
04667 #endif
04668               if (h->got.refcount > 0)
04669                 h->got.refcount -= 1;
04670            }
04671          else if (local_got_refcounts != NULL)
04672            {
04673 #ifdef INCLUDE_SHMEDIA
04674              if (rel->r_addend & 1)
04675               {
04676                 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
04677                   local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
04678               }
04679              else
04680 #endif
04681               if (local_got_refcounts[r_symndx] > 0)
04682                 local_got_refcounts[r_symndx] -= 1;
04683            }
04684          break;
04685 
04686        case R_SH_DIR32:
04687        case R_SH_REL32:
04688          if (info->shared)
04689            break;
04690          /* Fall thru */
04691 
04692        case R_SH_PLT32:
04693 #ifdef INCLUDE_SHMEDIA
04694        case R_SH_PLT_LOW16:
04695        case R_SH_PLT_MEDLOW16:
04696        case R_SH_PLT_MEDHI16:
04697        case R_SH_PLT_HI16:
04698 #endif
04699          if (h != NULL)
04700            {
04701              if (h->plt.refcount > 0)
04702               h->plt.refcount -= 1;
04703            }
04704          break;
04705 
04706        case R_SH_GOTPLT32:
04707 #ifdef INCLUDE_SHMEDIA
04708        case R_SH_GOTPLT_LOW16:
04709        case R_SH_GOTPLT_MEDLOW16:
04710        case R_SH_GOTPLT_MEDHI16:
04711        case R_SH_GOTPLT_HI16:
04712        case R_SH_GOTPLT10BY4:
04713        case R_SH_GOTPLT10BY8:
04714 #endif
04715          if (h != NULL)
04716            {
04717              struct elf_sh_link_hash_entry *eh;
04718              eh = (struct elf_sh_link_hash_entry *) h;
04719              if (eh->gotplt_refcount > 0)
04720               {
04721                 eh->gotplt_refcount -= 1;
04722                 if (h->plt.refcount > 0)
04723                   h->plt.refcount -= 1;
04724               }
04725 #ifdef INCLUDE_SHMEDIA
04726              else if (seen_stt_datalabel)
04727               {
04728                 if (eh->datalabel_got.refcount > 0)
04729                   eh->datalabel_got.refcount -= 1;
04730               }
04731 #endif
04732              else if (h->got.refcount > 0)
04733               h->got.refcount -= 1;
04734            }
04735          else if (local_got_refcounts != NULL)
04736            {
04737 #ifdef INCLUDE_SHMEDIA
04738              if (rel->r_addend & 1)
04739               {
04740                 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
04741                   local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
04742               }
04743              else
04744 #endif
04745               if (local_got_refcounts[r_symndx] > 0)
04746                 local_got_refcounts[r_symndx] -= 1;
04747            }
04748          break;
04749 
04750        default:
04751          break;
04752        }
04753     }
04754 
04755   return TRUE;
04756 }
04757 
04758 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
04759 
04760 static void
04761 sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
04762                           struct elf_link_hash_entry *dir,
04763                           struct elf_link_hash_entry *ind)
04764 {
04765   struct elf_sh_link_hash_entry *edir, *eind;
04766 
04767   edir = (struct elf_sh_link_hash_entry *) dir;
04768   eind = (struct elf_sh_link_hash_entry *) ind;
04769 
04770   if (eind->dyn_relocs != NULL)
04771     {
04772       if (edir->dyn_relocs != NULL)
04773        {
04774          struct elf_sh_dyn_relocs **pp;
04775          struct elf_sh_dyn_relocs *p;
04776 
04777          /* Add reloc counts against the indirect sym to the direct sym
04778             list.  Merge any entries against the same section.  */
04779          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
04780            {
04781              struct elf_sh_dyn_relocs *q;
04782 
04783              for (q = edir->dyn_relocs; q != NULL; q = q->next)
04784               if (q->sec == p->sec)
04785                 {
04786                   q->pc_count += p->pc_count;
04787                   q->count += p->count;
04788                   *pp = p->next;
04789                   break;
04790                 }
04791              if (q == NULL)
04792               pp = &p->next;
04793            }
04794          *pp = edir->dyn_relocs;
04795        }
04796 
04797       edir->dyn_relocs = eind->dyn_relocs;
04798       eind->dyn_relocs = NULL;
04799     }
04800   edir->gotplt_refcount = eind->gotplt_refcount;
04801   eind->gotplt_refcount = 0;
04802 #ifdef INCLUDE_SHMEDIA
04803   edir->datalabel_got.refcount += eind->datalabel_got.refcount;
04804   eind->datalabel_got.refcount = 0;
04805 #endif
04806 
04807   if (ind->root.type == bfd_link_hash_indirect
04808       && dir->got.refcount <= 0)
04809     {
04810       edir->tls_type = eind->tls_type;
04811       eind->tls_type = GOT_UNKNOWN;
04812     }
04813 
04814   if (ind->root.type != bfd_link_hash_indirect
04815       && dir->dynamic_adjusted)
04816     {
04817       /* If called to transfer flags for a weakdef during processing
04818         of elf_adjust_dynamic_symbol, don't copy non_got_ref.
04819         We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
04820       dir->ref_dynamic |= ind->ref_dynamic;
04821       dir->ref_regular |= ind->ref_regular;
04822       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
04823       dir->needs_plt |= ind->needs_plt;
04824     }
04825   else
04826     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
04827 }
04828 
04829 static int
04830 sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
04831                          int is_local)
04832 {
04833   if (info->shared)
04834     return r_type;
04835 
04836   switch (r_type)
04837     {
04838     case R_SH_TLS_GD_32:
04839     case R_SH_TLS_IE_32:
04840       if (is_local)
04841        return R_SH_TLS_LE_32;
04842       return R_SH_TLS_IE_32;
04843     case R_SH_TLS_LD_32:
04844       return R_SH_TLS_LE_32;
04845     }
04846 
04847   return r_type;
04848 }
04849 
04850 /* Look through the relocs for a section during the first phase.
04851    Since we don't do .gots or .plts, we just need to consider the
04852    virtual table relocs for gc.  */
04853 
04854 static bfd_boolean
04855 sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
04856                    const Elf_Internal_Rela *relocs)
04857 {
04858   Elf_Internal_Shdr *symtab_hdr;
04859   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
04860   struct elf_sh_link_hash_table *htab;
04861   const Elf_Internal_Rela *rel;
04862   const Elf_Internal_Rela *rel_end;
04863   bfd_vma *local_got_offsets;
04864   asection *sgot;
04865   asection *srelgot;
04866   asection *sreloc;
04867   unsigned int r_type;
04868   int tls_type, old_tls_type;
04869 
04870   sgot = NULL;
04871   srelgot = NULL;
04872   sreloc = NULL;
04873 
04874   if (info->relocatable)
04875     return TRUE;
04876 
04877   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
04878   sym_hashes = elf_sym_hashes (abfd);
04879   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
04880   if (!elf_bad_symtab (abfd))
04881     sym_hashes_end -= symtab_hdr->sh_info;
04882 
04883   htab = sh_elf_hash_table (info);
04884   local_got_offsets = elf_local_got_offsets (abfd);
04885 
04886   rel_end = relocs + sec->reloc_count;
04887   for (rel = relocs; rel < rel_end; rel++)
04888     {
04889       struct elf_link_hash_entry *h;
04890       unsigned long r_symndx;
04891 #ifdef INCLUDE_SHMEDIA
04892       int seen_stt_datalabel = 0;
04893 #endif
04894 
04895       r_symndx = ELF32_R_SYM (rel->r_info);
04896       r_type = ELF32_R_TYPE (rel->r_info);
04897 
04898       if (r_symndx < symtab_hdr->sh_info)
04899        h = NULL;
04900       else
04901        {
04902          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
04903          while (h->root.type == bfd_link_hash_indirect
04904                || h->root.type == bfd_link_hash_warning)
04905            {
04906 #ifdef INCLUDE_SHMEDIA
04907              seen_stt_datalabel |= h->type == STT_DATALABEL;
04908 #endif
04909              h = (struct elf_link_hash_entry *) h->root.u.i.link;
04910            }
04911        }
04912 
04913       r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
04914       if (! info->shared
04915          && r_type == R_SH_TLS_IE_32
04916          && h != NULL
04917          && h->root.type != bfd_link_hash_undefined
04918          && h->root.type != bfd_link_hash_undefweak
04919          && (h->dynindx == -1
04920              || h->def_regular))
04921        r_type = R_SH_TLS_LE_32;
04922 
04923       /* Some relocs require a global offset table.  */
04924       if (htab->sgot == NULL)
04925        {
04926          switch (r_type)
04927            {
04928            case R_SH_GOTPLT32:
04929            case R_SH_GOT32:
04930            case R_SH_GOTOFF:
04931            case R_SH_GOTPC:
04932 #ifdef INCLUDE_SHMEDIA
04933            case R_SH_GOTPLT_LOW16:
04934            case R_SH_GOTPLT_MEDLOW16:
04935            case R_SH_GOTPLT_MEDHI16:
04936            case R_SH_GOTPLT_HI16:
04937            case R_SH_GOTPLT10BY4:
04938            case R_SH_GOTPLT10BY8:
04939            case R_SH_GOT_LOW16:
04940            case R_SH_GOT_MEDLOW16:
04941            case R_SH_GOT_MEDHI16:
04942            case R_SH_GOT_HI16:
04943            case R_SH_GOT10BY4:
04944            case R_SH_GOT10BY8:
04945            case R_SH_GOTOFF_LOW16:
04946            case R_SH_GOTOFF_MEDLOW16:
04947            case R_SH_GOTOFF_MEDHI16:
04948            case R_SH_GOTOFF_HI16:
04949            case R_SH_GOTPC_LOW16:
04950            case R_SH_GOTPC_MEDLOW16:
04951            case R_SH_GOTPC_MEDHI16:
04952            case R_SH_GOTPC_HI16:
04953 #endif
04954            case R_SH_TLS_GD_32:
04955            case R_SH_TLS_LD_32:
04956            case R_SH_TLS_IE_32:
04957              if (htab->sgot == NULL)
04958               {
04959                 if (htab->root.dynobj == NULL)
04960                   htab->root.dynobj = abfd;
04961                 if (!create_got_section (htab->root.dynobj, info))
04962                   return FALSE;
04963               }
04964              break;
04965 
04966            default:
04967              break;
04968            }
04969        }
04970 
04971       switch (r_type)
04972        {
04973          /* This relocation describes the C++ object vtable hierarchy.
04974             Reconstruct it for later use during GC.  */
04975        case R_SH_GNU_VTINHERIT:
04976          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
04977            return FALSE;
04978          break;
04979 
04980          /* This relocation describes which C++ vtable entries are actually
04981             used.  Record for later use during GC.  */
04982        case R_SH_GNU_VTENTRY:
04983          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
04984            return FALSE;
04985          break;
04986 
04987        case R_SH_TLS_IE_32:
04988          if (info->shared)
04989            info->flags |= DF_STATIC_TLS;
04990 
04991          /* FALLTHROUGH */
04992        force_got:
04993        case R_SH_TLS_GD_32:
04994        case R_SH_GOT32:
04995 #ifdef INCLUDE_SHMEDIA
04996        case R_SH_GOT_LOW16:
04997        case R_SH_GOT_MEDLOW16:
04998        case R_SH_GOT_MEDHI16:
04999        case R_SH_GOT_HI16:
05000        case R_SH_GOT10BY4:
05001        case R_SH_GOT10BY8:
05002 #endif
05003          switch (r_type)
05004            {
05005            default:
05006              tls_type = GOT_NORMAL;
05007              break;
05008            case R_SH_TLS_GD_32:
05009              tls_type = GOT_TLS_GD;
05010              break;
05011            case R_SH_TLS_IE_32:
05012              tls_type = GOT_TLS_IE;
05013              break;
05014            }
05015 
05016          if (h != NULL)
05017            {
05018 #ifdef INCLUDE_SHMEDIA
05019              if (seen_stt_datalabel)
05020               {
05021                 struct elf_sh_link_hash_entry *eh
05022                   = (struct elf_sh_link_hash_entry *) h;
05023 
05024                 eh->datalabel_got.refcount += 1;
05025               }
05026              else
05027 #endif
05028               h->got.refcount += 1;
05029              old_tls_type = sh_elf_hash_entry (h)->tls_type;
05030            }
05031          else
05032            {
05033              bfd_signed_vma *local_got_refcounts;
05034 
05035              /* This is a global offset table entry for a local
05036                symbol.  */
05037              local_got_refcounts = elf_local_got_refcounts (abfd);
05038              if (local_got_refcounts == NULL)
05039               {
05040                 bfd_size_type size;
05041 
05042                 size = symtab_hdr->sh_info;
05043                 size *= sizeof (bfd_signed_vma);
05044 #ifdef INCLUDE_SHMEDIA
05045                 /* Reserve space for both the datalabel and
05046                    codelabel local GOT offsets.  */
05047                 size *= 2;
05048 #endif
05049                 size += symtab_hdr->sh_info;
05050                 local_got_refcounts = ((bfd_signed_vma *)
05051                                     bfd_zalloc (abfd, size));
05052                 if (local_got_refcounts == NULL)
05053                   return FALSE;
05054                 elf_local_got_refcounts (abfd) = local_got_refcounts;
05055 #ifdef        INCLUDE_SHMEDIA
05056                 /* Take care of both the datalabel and codelabel local
05057                    GOT offsets.  */
05058                 sh_elf_local_got_tls_type (abfd)
05059                   = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
05060 #else
05061                 sh_elf_local_got_tls_type (abfd)
05062                   = (char *) (local_got_refcounts + symtab_hdr->sh_info);
05063 #endif
05064               }
05065 #ifdef INCLUDE_SHMEDIA
05066              if (rel->r_addend & 1)
05067               local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
05068              else
05069 #endif
05070               local_got_refcounts[r_symndx] += 1;
05071              old_tls_type = sh_elf_local_got_tls_type (abfd) [r_symndx];
05072            }
05073 
05074          /* If a TLS symbol is accessed using IE at least once,
05075             there is no point to use dynamic model for it.  */
05076          if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
05077              && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
05078            {
05079              if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
05080               tls_type = GOT_TLS_IE;
05081              else
05082               {
05083                 (*_bfd_error_handler)
05084                   (_("%B: `%s' accessed both as normal and thread local symbol"),
05085                    abfd, h->root.root.string);
05086                 return FALSE;
05087               }
05088            }
05089 
05090          if (old_tls_type != tls_type)
05091            {
05092              if (h != NULL)
05093               sh_elf_hash_entry (h)->tls_type = tls_type;
05094              else
05095               sh_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
05096            }
05097 
05098          break;
05099 
05100        case R_SH_TLS_LD_32:
05101          sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
05102          break;
05103 
05104        case R_SH_GOTPLT32:
05105 #ifdef INCLUDE_SHMEDIA
05106        case R_SH_GOTPLT_LOW16:
05107        case R_SH_GOTPLT_MEDLOW16:
05108        case R_SH_GOTPLT_MEDHI16:
05109        case R_SH_GOTPLT_HI16:
05110        case R_SH_GOTPLT10BY4:
05111        case R_SH_GOTPLT10BY8:
05112 #endif
05113          /* If this is a local symbol, we resolve it directly without
05114             creating a procedure linkage table entry.  */
05115 
05116          if (h == NULL
05117              || h->forced_local
05118              || ! info->shared
05119              || info->symbolic
05120              || h->dynindx == -1)
05121            goto force_got;
05122 
05123          h->needs_plt = 1;
05124          h->plt.refcount += 1;
05125          ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
05126 
05127          break;
05128 
05129        case R_SH_PLT32:
05130 #ifdef INCLUDE_SHMEDIA
05131        case R_SH_PLT_LOW16:
05132        case R_SH_PLT_MEDLOW16:
05133        case R_SH_PLT_MEDHI16:
05134        case R_SH_PLT_HI16:
05135 #endif
05136          /* This symbol requires a procedure linkage table entry.  We
05137             actually build the entry in adjust_dynamic_symbol,
05138             because this might be a case of linking PIC code which is
05139             never referenced by a dynamic object, in which case we
05140             don't need to generate a procedure linkage table entry
05141             after all.  */
05142 
05143          /* If this is a local symbol, we resolve it directly without
05144             creating a procedure linkage table entry.  */
05145          if (h == NULL)
05146            continue;
05147 
05148          if (h->forced_local)
05149            break;
05150 
05151          h->needs_plt = 1;
05152          h->plt.refcount += 1;
05153          break;
05154 
05155        case R_SH_DIR32:
05156        case R_SH_REL32:
05157 #ifdef INCLUDE_SHMEDIA
05158        case R_SH_IMM_LOW16_PCREL:
05159        case R_SH_IMM_MEDLOW16_PCREL:
05160        case R_SH_IMM_MEDHI16_PCREL:
05161        case R_SH_IMM_HI16_PCREL:
05162 #endif
05163          if (h != NULL && ! info->shared)
05164            {
05165              h->non_got_ref = 1;
05166              h->plt.refcount += 1;
05167            }
05168 
05169          /* If we are creating a shared library, and this is a reloc
05170             against a global symbol, or a non PC relative reloc
05171             against a local symbol, then we need to copy the reloc
05172             into the shared library.  However, if we are linking with
05173             -Bsymbolic, we do not need to copy a reloc against a
05174             global symbol which is defined in an object we are
05175             including in the link (i.e., DEF_REGULAR is set).  At
05176             this point we have not seen all the input files, so it is
05177             possible that DEF_REGULAR is not set now but will be set
05178             later (it is never cleared).  We account for that
05179             possibility below by storing information in the
05180             dyn_relocs field of the hash table entry. A similar
05181             situation occurs when creating shared libraries and symbol
05182             visibility changes render the symbol local.
05183 
05184             If on the other hand, we are creating an executable, we
05185             may need to keep relocations for symbols satisfied by a
05186             dynamic library if we manage to avoid copy relocs for the
05187             symbol.  */
05188          if ((info->shared
05189               && (sec->flags & SEC_ALLOC) != 0
05190               && (r_type != R_SH_REL32
05191                  || (h != NULL
05192                      && (! info->symbolic
05193                         || h->root.type == bfd_link_hash_defweak
05194                         || !h->def_regular))))
05195              || (! info->shared
05196                 && (sec->flags & SEC_ALLOC) != 0
05197                 && h != NULL
05198                 && (h->root.type == bfd_link_hash_defweak
05199                     || !h->def_regular)))
05200            {
05201              struct elf_sh_dyn_relocs *p;
05202              struct elf_sh_dyn_relocs **head;
05203 
05204              if (htab->root.dynobj == NULL)
05205               htab->root.dynobj = abfd;
05206 
05207              /* When creating a shared object, we must copy these
05208                reloc types into the output file.  We create a reloc
05209                section in dynobj and make room for this reloc.  */
05210              if (sreloc == NULL)
05211               {
05212                 const char *name;
05213 
05214                 name = (bfd_elf_string_from_elf_section
05215                        (abfd,
05216                         elf_elfheader (abfd)->e_shstrndx,
05217                         elf_section_data (sec)->rel_hdr.sh_name));
05218                 if (name == NULL)
05219                   return FALSE;
05220 
05221                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
05222                            && strcmp (bfd_get_section_name (abfd, sec),
05223                                     name + 5) == 0);
05224 
05225                 sreloc = bfd_get_section_by_name (htab->root.dynobj, name);
05226                 if (sreloc == NULL)
05227                   {
05228                     flagword flags;
05229 
05230                     flags = (SEC_HAS_CONTENTS | SEC_READONLY
05231                             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
05232                     if ((sec->flags & SEC_ALLOC) != 0)
05233                      flags |= SEC_ALLOC | SEC_LOAD;
05234                     sreloc = bfd_make_section_with_flags (htab->root.dynobj,
05235                                                      name,
05236                                                      flags);
05237                     if (sreloc == NULL
05238                        || ! bfd_set_section_alignment (htab->root.dynobj,
05239                                                    sreloc, 2))
05240                      return FALSE;
05241                   }
05242                 elf_section_data (sec)->sreloc = sreloc;
05243               }
05244 
05245              /* If this is a global symbol, we count the number of
05246                relocations we need for this symbol.  */
05247              if (h != NULL)
05248               head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
05249              else
05250               {
05251                 asection *s;
05252                 void *vpp;
05253 
05254                 /* Track dynamic relocs needed for local syms too.  */
05255                 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
05256                                            sec, r_symndx);
05257                 if (s == NULL)
05258                   return FALSE;
05259 
05260                 vpp = &elf_section_data (s)->local_dynrel;
05261                 head = (struct elf_sh_dyn_relocs **) vpp;
05262               }
05263 
05264              p = *head;
05265              if (p == NULL || p->sec != sec)
05266               {
05267                 bfd_size_type amt = sizeof (*p);
05268                 p = bfd_alloc (htab->root.dynobj, amt);
05269                 if (p == NULL)
05270                   return FALSE;
05271                 p->next = *head;
05272                 *head = p;
05273                 p->sec = sec;
05274                 p->count = 0;
05275                 p->pc_count = 0;
05276               }
05277 
05278              p->count += 1;
05279              if (r_type == R_SH_REL32
05280 #ifdef INCLUDE_SHMEDIA
05281                 || r_type == R_SH_IMM_LOW16_PCREL
05282                 || r_type == R_SH_IMM_MEDLOW16_PCREL
05283                 || r_type == R_SH_IMM_MEDHI16_PCREL
05284                 || r_type == R_SH_IMM_HI16_PCREL
05285 #endif
05286                 )
05287               p->pc_count += 1;
05288            }
05289 
05290          break;
05291 
05292        case R_SH_TLS_LE_32:
05293          if (info->shared)
05294            {
05295              (*_bfd_error_handler)
05296               (_("%B: TLS local exec code cannot be linked into shared objects"),
05297                abfd);
05298              return FALSE;
05299            }
05300 
05301          break;
05302 
05303        case R_SH_TLS_LDO_32:
05304          /* Nothing to do.  */
05305          break;
05306 
05307        default:
05308          break;
05309        }
05310     }
05311 
05312   return TRUE;
05313 }
05314 
05315 #ifndef sh_elf_set_mach_from_flags
05316 static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
05317 
05318 static bfd_boolean
05319 sh_elf_set_mach_from_flags (bfd *abfd)
05320 {
05321   flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
05322 
05323   if (flags >= sizeof(sh_ef_bfd_table))
05324     return FALSE;
05325 
05326   if (sh_ef_bfd_table[flags] == 0)
05327     return FALSE;
05328   
05329   bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
05330 
05331   return TRUE;
05332 }
05333 
05334 
05335 /* Reverse table lookup for sh_ef_bfd_table[].
05336    Given a bfd MACH value from archures.c
05337    return the equivalent ELF flags from the table.
05338    Return -1 if no match is found.  */
05339 
05340 int
05341 sh_elf_get_flags_from_mach (unsigned long mach)
05342 {
05343   int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
05344   
05345   for (; i>0; i--)
05346     if (sh_ef_bfd_table[i] == mach)
05347       return i;
05348   
05349   /* shouldn't get here */
05350   BFD_FAIL();
05351 
05352   return -1;
05353 }
05354 #endif /* not sh_elf_set_mach_from_flags */
05355 
05356 #ifndef sh_elf_set_private_flags
05357 /* Function to keep SH specific file flags.  */
05358 
05359 static bfd_boolean
05360 sh_elf_set_private_flags (bfd *abfd, flagword flags)
05361 {
05362   BFD_ASSERT (! elf_flags_init (abfd)
05363              || elf_elfheader (abfd)->e_flags == flags);
05364 
05365   elf_elfheader (abfd)->e_flags = flags;
05366   elf_flags_init (abfd) = TRUE;
05367   return sh_elf_set_mach_from_flags (abfd);
05368 }
05369 #endif /* not sh_elf_set_private_flags */
05370 
05371 #ifndef sh_elf_copy_private_data
05372 /* Copy backend specific data from one object module to another */
05373 
05374 static bfd_boolean
05375 sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
05376 {
05377   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
05378       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
05379     return TRUE;
05380 
05381   return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
05382 }
05383 #endif /* not sh_elf_copy_private_data */
05384 
05385 #ifndef sh_elf_merge_private_data
05386 
05387 /* This function returns the ELF architecture number that
05388    corresponds to the given arch_sh* flags.  */
05389 
05390 int
05391 sh_find_elf_flags (unsigned int arch_set)
05392 {
05393   extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
05394   unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
05395 
05396   return sh_elf_get_flags_from_mach (bfd_mach);
05397 }
05398 
05399 /* This routine initialises the elf flags when required and
05400    calls sh_merge_bfd_arch() to check dsp/fpu compatibility.  */
05401 
05402 static bfd_boolean
05403 sh_elf_merge_private_data (bfd *ibfd, bfd *obfd)
05404 {
05405   extern bfd_boolean sh_merge_bfd_arch (bfd *, bfd *);
05406 
05407   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
05408       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
05409     return TRUE;
05410 
05411   if (! elf_flags_init (obfd))
05412     {
05413       /* This happens when ld starts out with a 'blank' output file.  */
05414       elf_flags_init (obfd) = TRUE;
05415       elf_elfheader (obfd)->e_flags = EF_SH1;
05416       sh_elf_set_mach_from_flags (obfd);
05417     }
05418 
05419   if (! sh_merge_bfd_arch (ibfd, obfd))
05420     {
05421       _bfd_error_handler ("%B: uses instructions which are incompatible "
05422                        "with instructions used in previous modules",
05423                        ibfd);
05424       bfd_set_error (bfd_error_bad_value);
05425       return FALSE;
05426     }
05427 
05428   elf_elfheader (obfd)->e_flags =
05429     sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
05430   
05431   return TRUE;
05432 }
05433 #endif /* not sh_elf_merge_private_data */
05434 
05435 /* Override the generic function because we need to store sh_elf_obj_tdata
05436    as the specific tdata.  We set also the machine architecture from flags
05437    here.  */
05438 
05439 static bfd_boolean
05440 sh_elf_object_p (bfd *abfd)
05441 {
05442   return sh_elf_set_mach_from_flags (abfd);
05443 }
05444 
05445 /* Finish up dynamic symbol handling.  We set the contents of various
05446    dynamic sections here.  */
05447 
05448 static bfd_boolean
05449 sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
05450                            struct elf_link_hash_entry *h,
05451                            Elf_Internal_Sym *sym)
05452 {
05453   struct elf_sh_link_hash_table *htab;
05454 
05455   htab = sh_elf_hash_table (info);
05456 
05457   if (h->plt.offset != (bfd_vma) -1)
05458     {
05459       asection *splt;
05460       asection *sgot;
05461       asection *srel;
05462 
05463       bfd_vma plt_index;
05464       bfd_vma got_offset;
05465       Elf_Internal_Rela rel;
05466       bfd_byte *loc;
05467 
05468       /* This symbol has an entry in the procedure linkage table.  Set
05469         it up.  */
05470 
05471       BFD_ASSERT (h->dynindx != -1);
05472 
05473       splt = htab->splt;
05474       sgot = htab->sgotplt;
05475       srel = htab->srelplt;
05476       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
05477 
05478       /* Get the index in the procedure linkage table which
05479         corresponds to this symbol.  This is the index of this symbol
05480         in all the symbols for which we are making plt entries.  The
05481         first entry in the procedure linkage table is reserved.  */
05482       plt_index = get_plt_index (htab->plt_info, h->plt.offset);
05483 
05484       /* Get the offset into the .got table of the entry that
05485         corresponds to this function.  Each .got entry is 4 bytes.
05486         The first three are reserved.  */
05487       got_offset = (plt_index + 3) * 4;
05488 
05489 #ifdef GOT_BIAS
05490       if (info->shared)
05491        got_offset -= GOT_BIAS;
05492 #endif
05493 
05494       /* Fill in the entry in the procedure linkage table.  */
05495       memcpy (splt->contents + h->plt.offset,
05496              htab->plt_info->symbol_entry,
05497              htab->plt_info->symbol_entry_size);
05498 
05499       if (info->shared)
05500        install_plt_field (output_bfd, FALSE, got_offset,
05501                         (splt->contents
05502                          + h->plt.offset
05503                          + htab->plt_info->symbol_fields.got_entry));
05504       else
05505        {
05506          install_plt_field (output_bfd, FALSE,
05507                           (sgot->output_section->vma
05508                            + sgot->output_offset
05509                            + got_offset),
05510                           (splt->contents
05511                            + h->plt.offset
05512                            + htab->plt_info->symbol_fields.got_entry));
05513          if (htab->vxworks_p)
05514            {
05515              unsigned int reachable_plts, plts_per_4k;
05516              int distance;
05517 
05518              /* Divide the PLT into groups.  The first group contains
05519                REACHABLE_PLTS entries and the other groups contain
05520                PLTS_PER_4K entries.  Entries in the first group can
05521                branch directly to .plt; those in later groups branch
05522                to the last element of the previous group.  */
05523              /* ??? It would be better to create multiple copies of
05524                the common resolver stub.  */
05525              reachable_plts = ((4096
05526                              - htab->plt_info->plt0_entry_size
05527                              - (htab->plt_info->symbol_fields.plt + 4))
05528                             / htab->plt_info->symbol_entry_size) + 1;
05529              plts_per_4k = (4096 / htab->plt_info->symbol_entry_size);
05530              if (plt_index < reachable_plts)
05531               distance = -(h->plt.offset
05532                           + htab->plt_info->symbol_fields.plt);
05533              else
05534               distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
05535                           * htab->plt_info->symbol_entry_size);
05536 
05537              /* Install the 'bra' with this offset.  */
05538              bfd_put_16 (output_bfd,
05539                        0xa000 | (0x0fff & ((distance - 4) / 2)),
05540                        (splt->contents
05541                         + h->plt.offset
05542                         + htab->plt_info->symbol_fields.plt));
05543            }
05544          else
05545            install_plt_field (output_bfd, TRUE,
05546                             splt->output_section->vma + splt->output_offset,
05547                             (splt->contents
05548                             + h->plt.offset
05549                             + htab->plt_info->symbol_fields.plt));
05550        }
05551 
05552 #ifdef GOT_BIAS
05553       if (info->shared)
05554        got_offset += GOT_BIAS;
05555 #endif
05556 
05557       install_plt_field (output_bfd, FALSE,
05558                       plt_index * sizeof (Elf32_External_Rela),
05559                       (splt->contents
05560                        + h->plt.offset
05561                        + htab->plt_info->symbol_fields.reloc_offset));
05562 
05563       /* Fill in the entry in the global offset table.  */
05564       bfd_put_32 (output_bfd,
05565                 (splt->output_section->vma
05566                  + splt->output_offset
05567                  + h->plt.offset
05568                  + htab->plt_info->symbol_resolve_offset),
05569                 sgot->contents + got_offset);
05570 
05571       /* Fill in the entry in the .rela.plt section.  */
05572       rel.r_offset = (sgot->output_section->vma
05573                     + sgot->output_offset
05574                     + got_offset);
05575       rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
05576       rel.r_addend = 0;
05577 #ifdef GOT_BIAS
05578       rel.r_addend = GOT_BIAS;
05579 #endif
05580       loc = srel->contents + plt_index * sizeof (Elf32_External_Rela);
05581       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
05582 
05583       if (htab->vxworks_p && !info->shared)
05584        {
05585          /* Create the .rela.plt.unloaded relocations for this PLT entry.
05586             Begin by pointing LOC to the first such relocation.  */
05587          loc = (htab->srelplt2->contents
05588                + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
05589 
05590          /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
05591             for the PLT entry's pointer to the .got.plt entry.  */
05592          rel.r_offset = (htab->splt->output_section->vma
05593                        + htab->splt->output_offset
05594                        + h->plt.offset
05595                        + htab->plt_info->symbol_fields.got_entry);
05596          rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
05597          rel.r_addend = got_offset;
05598          bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
05599          loc += sizeof (Elf32_External_Rela);
05600 
05601          /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
05602             the .got.plt entry, which initially points to .plt.  */
05603          rel.r_offset = (htab->sgotplt->output_section->vma
05604                        + htab->sgotplt->output_offset
05605                        + got_offset);
05606          rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
05607          rel.r_addend = 0;
05608          bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
05609        }
05610 
05611       if (!h->def_regular)
05612        {
05613          /* Mark the symbol as undefined, rather than as defined in
05614             the .plt section.  Leave the value alone.  */
05615          sym->st_shndx = SHN_UNDEF;
05616        }
05617     }
05618 
05619   if (h->got.offset != (bfd_vma) -1
05620       && sh_elf_hash_entry (h)->tls_type != GOT_TLS_GD
05621       && sh_elf_hash_entry (h)->tls_type != GOT_TLS_IE)
05622     {
05623       asection *sgot;
05624       asection *srel;
05625       Elf_Internal_Rela rel;
05626       bfd_byte *loc;
05627 
05628       /* This symbol has an entry in the global offset table.  Set it
05629         up.  */
05630 
05631       sgot = htab->sgot;
05632       srel = htab->srelgot;
05633       BFD_ASSERT (sgot != NULL && srel != NULL);
05634 
05635       rel.r_offset = (sgot->output_section->vma
05636                     + sgot->output_offset
05637                     + (h->got.offset &~ (bfd_vma) 1));
05638 
05639       /* If this is a static link, or it is a -Bsymbolic link and the
05640         symbol is defined locally or was forced to be local because
05641         of a version file, we just want to emit a RELATIVE reloc.
05642         The entry in the global offset table will already have been
05643         initialized in the relocate_section function.  */
05644       if (info->shared
05645          && SYMBOL_REFERENCES_LOCAL (info, h))
05646        {
05647          rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
05648          rel.r_addend = (h->root.u.def.value
05649                        + h->root.u.def.section->output_section->vma
05650                        + h->root.u.def.section->output_offset);
05651        }
05652       else
05653        {
05654          bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
05655          rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
05656          rel.r_addend = 0;
05657        }
05658 
05659       loc = srel->contents;
05660       loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
05661       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
05662     }
05663 
05664 #ifdef INCLUDE_SHMEDIA
05665   {
05666     struct elf_sh_link_hash_entry *eh;
05667 
05668     eh = (struct elf_sh_link_hash_entry *) h;
05669     if (eh->datalabel_got.offset != (bfd_vma) -1)
05670       {
05671        asection *sgot;
05672        asection *srel;
05673        Elf_Internal_Rela rel;
05674        bfd_byte *loc;
05675 
05676        /* This symbol has a datalabel entry in the global offset table.
05677           Set it up.  */
05678 
05679        sgot = htab->sgot;
05680        srel = htab->srelgot;
05681        BFD_ASSERT (sgot != NULL && srel != NULL);
05682 
05683        rel.r_offset = (sgot->output_section->vma
05684                      + sgot->output_offset
05685                      + (eh->datalabel_got.offset &~ (bfd_vma) 1));
05686 
05687        /* If this is a static link, or it is a -Bsymbolic link and the
05688           symbol is defined locally or was forced to be local because
05689           of a version file, we just want to emit a RELATIVE reloc.
05690           The entry in the global offset table will already have been
05691           initialized in the relocate_section function.  */
05692        if (info->shared
05693            && SYMBOL_REFERENCES_LOCAL (info, h))
05694          {
05695            rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
05696            rel.r_addend = (h->root.u.def.value
05697                          + h->root.u.def.section->output_section->vma
05698                          + h->root.u.def.section->output_offset);
05699          }
05700        else
05701          {
05702            bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
05703                      + eh->datalabel_got.offset);
05704            rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
05705            rel.r_addend = 0;
05706          }
05707 
05708        loc = srel->contents;
05709        loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
05710        bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
05711       }
05712   }
05713 #endif
05714 
05715   if (h->needs_copy)
05716     {
05717       asection *s;
05718       Elf_Internal_Rela rel;
05719       bfd_byte *loc;
05720 
05721       /* This symbol needs a copy reloc.  Set it up.  */
05722 
05723       BFD_ASSERT (h->dynindx != -1
05724                 && (h->root.type == bfd_link_hash_defined
05725                     || h->root.type == bfd_link_hash_defweak));
05726 
05727       s = bfd_get_section_by_name (h->root.u.def.section->owner,
05728                                ".rela.bss");
05729       BFD_ASSERT (s != NULL);
05730 
05731       rel.r_offset = (h->root.u.def.value
05732                     + h->root.u.def.section->output_section->vma
05733                     + h->root.u.def.section->output_offset);
05734       rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
05735       rel.r_addend = 0;
05736       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
05737       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
05738     }
05739 
05740   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
05741      _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
05742      ".got" section.  */
05743   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
05744       || (!htab->vxworks_p && h == htab->root.hgot))
05745     sym->st_shndx = SHN_ABS;
05746 
05747   return TRUE;
05748 }
05749 
05750 /* Finish up the dynamic sections.  */
05751 
05752 static bfd_boolean
05753 sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
05754 {
05755   struct elf_sh_link_hash_table *htab;
05756   asection *sgot;
05757   asection *sdyn;
05758 
05759   htab = sh_elf_hash_table (info);
05760   sgot = htab->sgotplt;
05761   sdyn = bfd_get_section_by_name (htab->root.dynobj, ".dynamic");
05762 
05763   if (htab->root.dynamic_sections_created)
05764     {
05765       asection *splt;
05766       Elf32_External_Dyn *dyncon, *dynconend;
05767 
05768       BFD_ASSERT (sgot != NULL && sdyn != NULL);
05769 
05770       dyncon = (Elf32_External_Dyn *) sdyn->contents;
05771       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
05772       for (; dyncon < dynconend; dyncon++)
05773        {
05774          Elf_Internal_Dyn dyn;
05775          asection *s;
05776 #ifdef INCLUDE_SHMEDIA
05777          const char *name;
05778 #endif
05779 
05780          bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
05781 
05782          switch (dyn.d_tag)
05783            {
05784            default:
05785              break;
05786 
05787 #ifdef INCLUDE_SHMEDIA
05788            case DT_INIT:
05789              name = info->init_function;
05790              goto get_sym;
05791 
05792            case DT_FINI:
05793              name = info->fini_function;
05794            get_sym:
05795              if (dyn.d_un.d_val != 0)
05796               {
05797                 struct elf_link_hash_entry *h;
05798 
05799                 h = elf_link_hash_lookup (&htab->root, name,
05800                                        FALSE, FALSE, TRUE);
05801                 if (h != NULL && (h->other & STO_SH5_ISA32))
05802                   {
05803                     dyn.d_un.d_val |= 1;
05804                     bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
05805                   }
05806               }
05807              break;
05808 #endif
05809 
05810            case DT_PLTGOT:
05811              s = htab->sgot->output_section;
05812              goto get_vma;
05813 
05814            case DT_JMPREL:
05815              s = htab->srelplt->output_section;
05816            get_vma:
05817              BFD_ASSERT (s != NULL);
05818              dyn.d_un.d_ptr = s->vma;
05819              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
05820              break;
05821 
05822            case DT_PLTRELSZ:
05823              s = htab->srelplt->output_section;
05824              BFD_ASSERT (s != NULL);
05825              dyn.d_un.d_val = s->size;
05826              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
05827              break;
05828 
05829            case DT_RELASZ:
05830              /* My reading of the SVR4 ABI indicates that the
05831                procedure linkage table relocs (DT_JMPREL) should be
05832                included in the overall relocs (DT_RELA).  This is
05833                what Solaris does.  However, UnixWare can not handle
05834                that case.  Therefore, we override the DT_RELASZ entry
05835                here to make it not include the JMPREL relocs.  Since
05836                the linker script arranges for .rela.plt to follow all
05837                other relocation sections, we don't have to worry
05838                about changing the DT_RELA entry.  */
05839              if (htab->srelplt != NULL)
05840               {
05841                 s = htab->srelplt->output_section;
05842                 dyn.d_un.d_val -= s->size;
05843               }
05844              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
05845              break;
05846            }
05847        }
05848 
05849       /* Fill in the first entry in the procedure linkage table.  */
05850       splt = htab->splt;
05851       if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
05852        {
05853          unsigned int i;
05854 
05855          memcpy (splt->contents,
05856                 htab->plt_info->plt0_entry,
05857                 htab->plt_info->plt0_entry_size);
05858          for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
05859            if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
05860              install_plt_field (output_bfd, FALSE,
05861                              (sgot->output_section->vma
05862                               + sgot->output_offset
05863                               + (i * 4)),
05864                              (splt->contents
05865                               + htab->plt_info->plt0_got_fields[i]));
05866 
05867          if (htab->vxworks_p)
05868            {
05869              /* Finalize the .rela.plt.unloaded contents.  */
05870              Elf_Internal_Rela rel;
05871              bfd_byte *loc;
05872 
05873              /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
05874                first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8.  */
05875              loc = htab->srelplt2->contents;
05876              rel.r_offset = (splt->output_section->vma
05877                            + splt->output_offset
05878                            + htab->plt_info->plt0_got_fields[2]);
05879              rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
05880              rel.r_addend = 8;
05881              bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
05882              loc += sizeof (Elf32_External_Rela);
05883 
05884              /* Fix up the remaining .rela.plt.unloaded relocations.
05885                They may have the wrong symbol index for _G_O_T_ or
05886                _P_L_T_ depending on the order in which symbols were
05887                output.  */
05888              while (loc < htab->srelplt2->contents + htab->srelplt2->size)
05889               {
05890                 /* The PLT entry's pointer to the .got.plt slot.  */
05891                 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
05892                 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
05893                                         R_SH_DIR32);
05894                 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
05895                 loc += sizeof (Elf32_External_Rela);
05896 
05897                 /* The .got.plt slot's pointer to .plt.  */
05898                 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
05899                 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
05900                                         R_SH_DIR32);
05901                 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
05902                 loc += sizeof (Elf32_External_Rela);
05903               }
05904            }
05905 
05906          /* UnixWare sets the entsize of .plt to 4, although that doesn't
05907             really seem like the right value.  */
05908          elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
05909        }
05910     }
05911 
05912   /* Fill in the first three entries in the global offset table.  */
05913   if (sgot && sgot->size > 0)
05914     {
05915       if (sdyn == NULL)
05916        bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
05917       else
05918        bfd_put_32 (output_bfd,
05919                   sdyn->output_section->vma + sdyn->output_offset,
05920                   sgot->contents);
05921       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
05922       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
05923 
05924       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
05925     }
05926 
05927   return TRUE;
05928 }
05929 
05930 static enum elf_reloc_type_class
05931 sh_elf_reloc_type_class (const Elf_Internal_Rela *rela)
05932 {
05933   switch ((int) ELF32_R_TYPE (rela->r_info))
05934     {
05935     case R_SH_RELATIVE:
05936       return reloc_class_relative;
05937     case R_SH_JMP_SLOT:
05938       return reloc_class_plt;
05939     case R_SH_COPY:
05940       return reloc_class_copy;
05941     default:
05942       return reloc_class_normal;
05943     }
05944 }
05945 
05946 #if !defined SH_TARGET_ALREADY_DEFINED
05947 /* Support for Linux core dump NOTE sections.  */
05948 
05949 static bfd_boolean
05950 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
05951 {
05952   int offset;
05953   unsigned int size;
05954 
05955   switch (note->descsz)
05956     {
05957       default:
05958        return FALSE;
05959 
05960       case 168:             /* Linux/SH */
05961        /* pr_cursig */
05962        elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
05963 
05964        /* pr_pid */
05965        elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
05966 
05967        /* pr_reg */
05968        offset = 72;
05969        size = 92;
05970 
05971        break;
05972     }
05973 
05974   /* Make a ".reg/999" section.  */
05975   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
05976                                      size, note->descpos + offset);
05977 }
05978 
05979 static bfd_boolean
05980 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
05981 {
05982   switch (note->descsz)
05983     {
05984       default:
05985        return FALSE;
05986 
05987       case 124:             /* Linux/SH elf_prpsinfo */
05988        elf_tdata (abfd)->core_program
05989         = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
05990        elf_tdata (abfd)->core_command
05991         = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
05992     }
05993 
05994   /* Note that for some reason, a spurious space is tacked
05995      onto the end of the args in some (at least one anyway)
05996      implementations, so strip it off if it exists.  */
05997 
05998   {
05999     char *command = elf_tdata (abfd)->core_command;
06000     int n = strlen (command);
06001 
06002     if (0 < n && command[n - 1] == ' ')
06003       command[n - 1] = '\0';
06004   }
06005 
06006   return TRUE;
06007 }
06008 #endif /* not SH_TARGET_ALREADY_DEFINED */
06009 
06010  
06011 /* Return address for Ith PLT stub in section PLT, for relocation REL
06012    or (bfd_vma) -1 if it should not be included.  */
06013 
06014 static bfd_vma
06015 sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
06016                   const arelent *rel ATTRIBUTE_UNUSED)
06017 {
06018   const struct elf_sh_plt_info *plt_info;
06019 
06020   plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
06021   return plt->vma + get_plt_offset (plt_info, i);
06022 }
06023 
06024 #if !defined SH_TARGET_ALREADY_DEFINED
06025 #define TARGET_BIG_SYM             bfd_elf32_sh_vec
06026 #define TARGET_BIG_NAME            "elf32-sh"
06027 #define TARGET_LITTLE_SYM   bfd_elf32_shl_vec
06028 #define TARGET_LITTLE_NAME  "elf32-shl"
06029 #endif
06030 
06031 #define ELF_ARCH            bfd_arch_sh
06032 #define ELF_MACHINE_CODE    EM_SH
06033 #ifdef __QNXTARGET__
06034 #define ELF_MAXPAGESIZE            0x1000
06035 #else
06036 #define ELF_MAXPAGESIZE            0x80
06037 #endif
06038 
06039 #define elf_symbol_leading_char '_'
06040 
06041 #define bfd_elf32_bfd_reloc_type_lookup   sh_elf_reloc_type_lookup
06042 #define bfd_elf32_bfd_reloc_name_lookup \
06043                                    sh_elf_reloc_name_lookup
06044 #define elf_info_to_howto          sh_elf_info_to_howto
06045 #define bfd_elf32_bfd_relax_section       sh_elf_relax_section
06046 #define elf_backend_relocate_section      sh_elf_relocate_section
06047 #define bfd_elf32_bfd_get_relocated_section_contents \
06048                                    sh_elf_get_relocated_section_contents
06049 #define bfd_elf32_mkobject         sh_elf_mkobject
06050 #define elf_backend_object_p              sh_elf_object_p
06051 #define bfd_elf32_bfd_set_private_bfd_flags \
06052                                    sh_elf_set_private_flags
06053 #define bfd_elf32_bfd_copy_private_bfd_data \
06054                                    sh_elf_copy_private_data
06055 #define bfd_elf32_bfd_merge_private_bfd_data \
06056                                    sh_elf_merge_private_data
06057 
06058 #define elf_backend_gc_mark_hook   sh_elf_gc_mark_hook
06059 #define elf_backend_gc_sweep_hook  sh_elf_gc_sweep_hook
06060 #define elf_backend_check_relocs   sh_elf_check_relocs
06061 #define elf_backend_copy_indirect_symbol \
06062                                    sh_elf_copy_indirect_symbol
06063 #define elf_backend_create_dynamic_sections \
06064                                    sh_elf_create_dynamic_sections
06065 #define bfd_elf32_bfd_link_hash_table_create \
06066                                    sh_elf_link_hash_table_create
06067 #define elf_backend_adjust_dynamic_symbol \
06068                                    sh_elf_adjust_dynamic_symbol
06069 #define elf_backend_always_size_sections \
06070                                    sh_elf_always_size_sections
06071 #define elf_backend_size_dynamic_sections \
06072                                    sh_elf_size_dynamic_sections
06073 #define elf_backend_omit_section_dynsym \
06074   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
06075 #define elf_backend_finish_dynamic_symbol \
06076                                    sh_elf_finish_dynamic_symbol
06077 #define elf_backend_finish_dynamic_sections \
06078                                    sh_elf_finish_dynamic_sections
06079 #define elf_backend_reloc_type_class      sh_elf_reloc_type_class
06080 #define elf_backend_plt_sym_val           sh_elf_plt_sym_val
06081 
06082 #define elf_backend_can_gc_sections       1
06083 #define elf_backend_can_refcount   1
06084 #define elf_backend_want_got_plt   1
06085 #define elf_backend_plt_readonly   1
06086 #define elf_backend_want_plt_sym   0
06087 #define elf_backend_got_header_size       12
06088 
06089 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
06090 
06091 #include "elf32-target.h"
06092 
06093 /* NetBSD support.  */
06094 #undef TARGET_BIG_SYM
06095 #define       TARGET_BIG_SYM                     bfd_elf32_shnbsd_vec
06096 #undef TARGET_BIG_NAME
06097 #define       TARGET_BIG_NAME                    "elf32-sh-nbsd"
06098 #undef TARGET_LITTLE_SYM
06099 #define       TARGET_LITTLE_SYM           bfd_elf32_shlnbsd_vec
06100 #undef TARGET_LITTLE_NAME
06101 #define       TARGET_LITTLE_NAME          "elf32-shl-nbsd"
06102 #undef ELF_MAXPAGESIZE
06103 #define       ELF_MAXPAGESIZE                    0x10000
06104 #undef ELF_COMMONPAGESIZE
06105 #undef elf_symbol_leading_char
06106 #define       elf_symbol_leading_char            0
06107 #undef elf32_bed
06108 #define       elf32_bed                   elf32_sh_nbsd_bed
06109 
06110 #include "elf32-target.h"
06111 
06112 
06113 /* Linux support.  */
06114 #undef TARGET_BIG_SYM
06115 #define       TARGET_BIG_SYM                     bfd_elf32_shblin_vec
06116 #undef TARGET_BIG_NAME
06117 #define       TARGET_BIG_NAME                    "elf32-shbig-linux"
06118 #undef TARGET_LITTLE_SYM
06119 #define       TARGET_LITTLE_SYM           bfd_elf32_shlin_vec
06120 #undef TARGET_LITTLE_NAME
06121 #define       TARGET_LITTLE_NAME          "elf32-sh-linux"
06122 #undef ELF_COMMONPAGESIZE
06123 #define       ELF_COMMONPAGESIZE          0x1000
06124 
06125 #undef elf_backend_grok_prstatus
06126 #define       elf_backend_grok_prstatus   elf32_shlin_grok_prstatus
06127 #undef elf_backend_grok_psinfo
06128 #define       elf_backend_grok_psinfo            elf32_shlin_grok_psinfo
06129 #undef elf32_bed
06130 #define       elf32_bed                   elf32_sh_lin_bed
06131 
06132 #include "elf32-target.h"
06133 
06134 #undef TARGET_BIG_SYM
06135 #define       TARGET_BIG_SYM                     bfd_elf32_shvxworks_vec
06136 #undef TARGET_BIG_NAME
06137 #define       TARGET_BIG_NAME                    "elf32-sh-vxworks"
06138 #undef TARGET_LITTLE_SYM
06139 #define       TARGET_LITTLE_SYM           bfd_elf32_shlvxworks_vec
06140 #undef TARGET_LITTLE_NAME
06141 #define       TARGET_LITTLE_NAME          "elf32-shl-vxworks"
06142 #undef elf32_bed
06143 #define       elf32_bed                   elf32_sh_vxworks_bed
06144 
06145 #undef elf_backend_want_plt_sym
06146 #define       elf_backend_want_plt_sym    1
06147 #undef elf_symbol_leading_char
06148 #define       elf_symbol_leading_char            '_'
06149 #define       elf_backend_want_got_underscore 1
06150 #undef elf_backend_grok_prstatus
06151 #undef elf_backend_grok_psinfo
06152 #undef elf_backend_add_symbol_hook
06153 #define       elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
06154 #undef elf_backend_link_output_symbol_hook
06155 #define       elf_backend_link_output_symbol_hook \
06156                                    elf_vxworks_link_output_symbol_hook
06157 #undef elf_backend_emit_relocs
06158 #define       elf_backend_emit_relocs            elf_vxworks_emit_relocs
06159 #undef elf_backend_final_write_processing
06160 #define       elf_backend_final_write_processing \
06161                                    elf_vxworks_final_write_processing
06162 #undef ELF_MAXPAGESIZE
06163 #define       ELF_MAXPAGESIZE                    0x1000
06164 #undef ELF_COMMONPAGESIZE
06165 
06166 #include "elf32-target.h"
06167 
06168 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */