Home | History | Annotate | Download | only in bfd
      1 /* M16C/M32C specific support for 32-bit ELF.
      2    Copyright (C) 2005-2016 Free Software Foundation, Inc.
      3 
      4    This file is part of BFD, the Binary File Descriptor library.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program; if not, write to the Free Software
     18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
     19 
     20 #include "sysdep.h"
     21 #include "bfd.h"
     22 #include "libbfd.h"
     23 #include "elf-bfd.h"
     24 #include "elf/m32c.h"
     25 #include "libiberty.h"
     26 
     27 /* Forward declarations.  */
     28 static reloc_howto_type * m32c_reloc_type_lookup
     29   (bfd *, bfd_reloc_code_real_type);
     30 static void m32c_info_to_howto_rela
     31   (bfd *, arelent *, Elf_Internal_Rela *);
     32 static bfd_boolean m32c_elf_relocate_section
     33   (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
     34 static bfd_boolean m32c_elf_check_relocs
     35   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
     36 static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
     37 #ifdef DEBUG
     38 char * m32c_get_reloc (long reloc);
     39 void dump_symtab (bfd *, void *, void *);
     40 #endif
     41 static bfd_boolean m32c_elf_relax_section
     42 (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again);
     43 static bfd_reloc_status_type m32c_apply_reloc_24
     44   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     45 
     46 
     47 static reloc_howto_type m32c_elf_howto_table [] =
     48 {
     49   /* This reloc does nothing.  */
     50   HOWTO (R_M32C_NONE,		/* type */
     51 	 0,			/* rightshift */
     52 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
     53 	 0,			/* bitsize */
     54 	 FALSE,			/* pc_relative */
     55 	 0,			/* bitpos */
     56 	 complain_overflow_dont, /* complain_on_overflow */
     57 	 bfd_elf_generic_reloc,	/* special_function */
     58 	 "R_M32C_NONE",		/* name */
     59 	 FALSE,			/* partial_inplace */
     60 	 0,			/* src_mask */
     61 	 0,			/* dst_mask */
     62 	 FALSE),		/* pcrel_offset */
     63 
     64   /* GCC intentionally overflows these next two in order to work
     65      around limitations in the addressing modes, so don't complain
     66      about overflow.  */
     67   HOWTO (R_M32C_16,		/* type */
     68 	 0,			/* rightshift */
     69 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
     70 	 16,			/* bitsize */
     71 	 FALSE,			/* pc_relative */
     72 	 0,			/* bitpos */
     73 	 complain_overflow_dont, /* complain_on_overflow */
     74 	 bfd_elf_generic_reloc,	/* special_function */
     75 	 "R_M32C_16",		/* name */
     76 	 FALSE,			/* partial_inplace */
     77 	 0,			/* src_mask */
     78 	 0xffff,		/* dst_mask */
     79 	 FALSE),		/* pcrel_offset */
     80 
     81   HOWTO (R_M32C_24,		/* type */
     82 	 0,			/* rightshift */
     83 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
     84 	 24,			/* bitsize */
     85 	 FALSE,			/* pc_relative */
     86 	 0,			/* bitpos */
     87 	 complain_overflow_dont, /* complain_on_overflow */
     88 	 m32c_apply_reloc_24,	/* special_function */
     89 	 "R_M32C_24",		/* name */
     90 	 FALSE,			/* partial_inplace */
     91 	 0,			/* src_mask */
     92 	 0xffffff,		/* dst_mask */
     93 	 FALSE),		/* pcrel_offset */
     94 
     95   HOWTO (R_M32C_32,		/* type */
     96 	 0,			/* rightshift */
     97 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
     98 	 32,			/* bitsize */
     99 	 FALSE,			/* pc_relative */
    100 	 0,			/* bitpos */
    101 	 complain_overflow_bitfield, /* complain_on_overflow */
    102 	 bfd_elf_generic_reloc,	/* special_function */
    103 	 "R_M32C_32",		/* name */
    104 	 FALSE,			/* partial_inplace */
    105 	 0,			/* src_mask */
    106 	 0xffffffff,		/* dst_mask */
    107 	 FALSE),		/* pcrel_offset */
    108 
    109   HOWTO (R_M32C_8_PCREL,	/* type */
    110 	 0,			/* rightshift */
    111 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    112 	 8,			/* bitsize */
    113 	 TRUE,			/* pc_relative */
    114 	 0,			/* bitpos */
    115 	 complain_overflow_signed, /* complain_on_overflow */
    116 	 bfd_elf_generic_reloc,	/* special_function */
    117 	 "R_M32C_8_PCREL",	/* name */
    118 	 FALSE,			/* partial_inplace */
    119 	 0,     		/* src_mask */
    120 	 0xff,   		/* dst_mask */
    121 	 TRUE), 		/* pcrel_offset */
    122 
    123   HOWTO (R_M32C_16_PCREL,	/* type */
    124 	 0,			/* rightshift */
    125 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    126 	 16,			/* bitsize */
    127 	 TRUE,			/* pc_relative */
    128 	 0,			/* bitpos */
    129 	 complain_overflow_signed, /* complain_on_overflow */
    130 	 bfd_elf_generic_reloc,	/* special_function */
    131 	 "R_M32C_16_PCREL",	/* name */
    132 	 FALSE,			/* partial_inplace */
    133 	 0,     		/* src_mask */
    134 	 0xffff,             	/* dst_mask */
    135 	 TRUE), 		/* pcrel_offset */
    136 
    137   HOWTO (R_M32C_8,		/* type */
    138 	 0,			/* rightshift */
    139 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    140 	 8,			/* bitsize */
    141 	 FALSE,			/* pc_relative */
    142 	 0,			/* bitpos */
    143 	 complain_overflow_unsigned, /* complain_on_overflow */
    144 	 bfd_elf_generic_reloc,	/* special_function */
    145 	 "R_M32C_8",		/* name */
    146 	 FALSE,			/* partial_inplace */
    147 	 0,     		/* src_mask */
    148 	 0xff,			/* dst_mask */
    149 	 FALSE), 		/* pcrel_offset */
    150 
    151   HOWTO (R_M32C_LO16,		/* type */
    152 	 0,			/* rightshift */
    153 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    154 	 16,			/* bitsize */
    155 	 FALSE,			/* pc_relative */
    156 	 0,			/* bitpos */
    157 	 complain_overflow_dont, /* complain_on_overflow */
    158 	 bfd_elf_generic_reloc,	/* special_function */
    159 	 "R_M32C_LO16",		/* name */
    160 	 FALSE,			/* partial_inplace */
    161 	 0,     		/* src_mask */
    162 	 0xffff,		/* dst_mask */
    163 	 FALSE), 		/* pcrel_offset */
    164 
    165   HOWTO (R_M32C_HI8,		/* type */
    166 	 0,			/* rightshift */
    167 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    168 	 8,			/* bitsize */
    169 	 FALSE,			/* pc_relative */
    170 	 0,			/* bitpos */
    171 	 complain_overflow_dont, /* complain_on_overflow */
    172 	 bfd_elf_generic_reloc,	/* special_function */
    173 	 "R_M32C_HI8",		/* name */
    174 	 FALSE,			/* partial_inplace */
    175 	 0,     		/* src_mask */
    176 	 0xff,			/* dst_mask */
    177 	 FALSE), 		/* pcrel_offset */
    178 
    179   HOWTO (R_M32C_HI16,		/* type */
    180 	 0,			/* rightshift */
    181 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    182 	 16,			/* bitsize */
    183 	 FALSE,			/* pc_relative */
    184 	 0,			/* bitpos */
    185 	 complain_overflow_dont, /* complain_on_overflow */
    186 	 bfd_elf_generic_reloc,	/* special_function */
    187 	 "R_M32C_HI16",		/* name */
    188 	 FALSE,			/* partial_inplace */
    189 	 0,     		/* src_mask */
    190 	 0xffff,		/* dst_mask */
    191 	 FALSE), 		/* pcrel_offset */
    192 
    193   HOWTO (R_M32C_RL_JUMP,	/* type */
    194 	 0,			/* rightshift */
    195 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    196 	 0,			/* bitsize */
    197 	 FALSE,			/* pc_relative */
    198 	 0,			/* bitpos */
    199 	 complain_overflow_signed, /* complain_on_overflow */
    200 	 bfd_elf_generic_reloc,	/* special_function */
    201 	 "R_M32C_RL_JUMP",	/* name */
    202 	 FALSE,			/* partial_inplace */
    203 	 0,     		/* src_mask */
    204 	 0,   			/* dst_mask */
    205 	 FALSE), 		/* pcrel_offset */
    206 
    207   HOWTO (R_M32C_RL_1ADDR,	/* type */
    208 	 0,			/* rightshift */
    209 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    210 	 0,			/* bitsize */
    211 	 FALSE,			/* pc_relative */
    212 	 0,			/* bitpos */
    213 	 complain_overflow_signed, /* complain_on_overflow */
    214 	 bfd_elf_generic_reloc,	/* special_function */
    215 	 "R_M32C_RL_1ADDR",	/* name */
    216 	 FALSE,			/* partial_inplace */
    217 	 0,     		/* src_mask */
    218 	 0,   			/* dst_mask */
    219 	 FALSE), 		/* pcrel_offset */
    220 
    221   HOWTO (R_M32C_RL_2ADDR,	/* type */
    222 	 0,			/* rightshift */
    223 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    224 	 0,			/* bitsize */
    225 	 FALSE,			/* pc_relative */
    226 	 0,			/* bitpos */
    227 	 complain_overflow_signed, /* complain_on_overflow */
    228 	 bfd_elf_generic_reloc,	/* special_function */
    229 	 "R_M32C_RL_2ADDR",	/* name */
    230 	 FALSE,			/* partial_inplace */
    231 	 0,     		/* src_mask */
    232 	 0,   			/* dst_mask */
    233 	 FALSE), 		/* pcrel_offset */
    234 
    235 };
    236 
    237 /* Map BFD reloc types to M32C ELF reloc types.  */
    239 
    240 struct m32c_reloc_map
    241 {
    242   bfd_reloc_code_real_type bfd_reloc_val;
    243   unsigned int m32c_reloc_val;
    244 };
    245 
    246 static const struct m32c_reloc_map m32c_reloc_map [] =
    247 {
    248   { BFD_RELOC_NONE,		R_M32C_NONE },
    249   { BFD_RELOC_16,		R_M32C_16 },
    250   { BFD_RELOC_24,               R_M32C_24 },
    251   { BFD_RELOC_32,		R_M32C_32 },
    252   { BFD_RELOC_8_PCREL,          R_M32C_8_PCREL },
    253   { BFD_RELOC_16_PCREL,         R_M32C_16_PCREL },
    254   { BFD_RELOC_8,		R_M32C_8 },
    255   { BFD_RELOC_LO16,		R_M32C_LO16 },
    256   { BFD_RELOC_HI16,		R_M32C_HI16 },
    257   { BFD_RELOC_M32C_HI8,		R_M32C_HI8 },
    258   { BFD_RELOC_M32C_RL_JUMP,	R_M32C_RL_JUMP },
    259   { BFD_RELOC_M32C_RL_1ADDR,	R_M32C_RL_1ADDR },
    260   { BFD_RELOC_M32C_RL_2ADDR,	R_M32C_RL_2ADDR }
    261 };
    262 
    263 static reloc_howto_type *
    264 m32c_reloc_type_lookup
    265     (bfd *                    abfd ATTRIBUTE_UNUSED,
    266      bfd_reloc_code_real_type code)
    267 {
    268   unsigned int i;
    269 
    270   for (i = ARRAY_SIZE (m32c_reloc_map); i--;)
    271     if (m32c_reloc_map [i].bfd_reloc_val == code)
    272       return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
    273 
    274   return NULL;
    275 }
    276 
    277 static reloc_howto_type *
    278 m32c_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
    279 {
    280   unsigned int i;
    281 
    282   for (i = 0;
    283        i < sizeof (m32c_elf_howto_table) / sizeof (m32c_elf_howto_table[0]);
    284        i++)
    285     if (m32c_elf_howto_table[i].name != NULL
    286 	&& strcasecmp (m32c_elf_howto_table[i].name, r_name) == 0)
    287       return &m32c_elf_howto_table[i];
    288 
    289   return NULL;
    290 }
    291 
    292 /* Set the howto pointer for an M32C ELF reloc.  */
    293 
    294 static void
    295 m32c_info_to_howto_rela
    296     (bfd *               abfd ATTRIBUTE_UNUSED,
    297      arelent *           cache_ptr,
    298      Elf_Internal_Rela * dst)
    299 {
    300   unsigned int r_type;
    301 
    302   r_type = ELF32_R_TYPE (dst->r_info);
    303   if (r_type >= (unsigned int) R_M32C_max)
    304     {
    305       _bfd_error_handler (_("%B: invalid M32C reloc number: %d"), abfd, r_type);
    306       r_type = 0;
    307     }
    308   cache_ptr->howto = & m32c_elf_howto_table [r_type];
    309 }
    310 
    311 
    312 
    314 /* Apply R_M32C_24 relocations.  We have to do this because it's not a
    315    power-of-two size, and the generic code may think it overruns the
    316    section if it's right at the end.
    317 
    318    Must return something other than bfd_reloc_continue to avoid the
    319    above problem.  Typical return values include bfd_reloc_ok or
    320    bfd_reloc_overflow.
    321 */
    322 
    323 static bfd_reloc_status_type m32c_apply_reloc_24 (bfd *abfd ATTRIBUTE_UNUSED,
    324 						  arelent *reloc_entry,
    325 						  asymbol *symbol,
    326 						  void *vdata_start ATTRIBUTE_UNUSED,
    327 						  asection *input_section,
    328 						  bfd *ibfd ATTRIBUTE_UNUSED,
    329 						  char **error_msg ATTRIBUTE_UNUSED)
    330 {
    331   bfd_vma relocation;
    332   bfd_reloc_status_type s;
    333 
    334   s = bfd_elf_generic_reloc (abfd, reloc_entry, symbol,
    335 			     vdata_start,
    336 			     input_section, ibfd, error_msg);
    337   if (s != bfd_reloc_continue)
    338     return s;
    339 
    340   /* Get symbol value.  (Common symbols are special.)  */
    341   if (bfd_is_com_section (symbol->section))
    342     relocation = 0;
    343   else
    344     relocation = symbol->value;
    345 
    346   relocation += symbol->section->output_offset;
    347 
    348   /* Add in supplied addend.  */
    349   relocation += reloc_entry->addend;
    350 
    351   reloc_entry->addend = relocation;
    352   reloc_entry->address += input_section->output_offset;
    353   return bfd_reloc_ok;
    354 }
    355 
    356 /* Relocate an M32C ELF section.
    357    There is some attempt to make this function usable for many architectures,
    358    both USE_REL and USE_RELA ['twould be nice if such a critter existed],
    359    if only to serve as a learning tool.
    360 
    361    The RELOCATE_SECTION function is called by the new ELF backend linker
    362    to handle the relocations for a section.
    363 
    364    The relocs are always passed as Rela structures; if the section
    365    actually uses Rel structures, the r_addend field will always be
    366    zero.
    367 
    368    This function is responsible for adjusting the section contents as
    369    necessary, and (if using Rela relocs and generating a relocatable
    370    output file) adjusting the reloc addend as necessary.
    371 
    372    This function does not have to worry about setting the reloc
    373    address or the reloc symbol index.
    374 
    375    LOCAL_SYMS is a pointer to the swapped in local symbols.
    376 
    377    LOCAL_SECTIONS is an array giving the section in the input file
    378    corresponding to the st_shndx field of each local symbol.
    379 
    380    The global hash table entry for the global symbols can be found
    381    via elf_sym_hashes (input_bfd).
    382 
    383    When generating relocatable output, this function must handle
    384    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
    385    going to be the section symbol corresponding to the output
    386    section, which means that the addend must be adjusted
    387    accordingly.  */
    388 
    389 static bfd_boolean
    390 m32c_elf_relocate_section
    391     (bfd *                   output_bfd ATTRIBUTE_UNUSED,
    392      struct bfd_link_info *  info,
    393      bfd *                   input_bfd,
    394      asection *              input_section,
    395      bfd_byte *              contents,
    396      Elf_Internal_Rela *     relocs,
    397      Elf_Internal_Sym *      local_syms,
    398      asection **             local_sections)
    399 {
    400   Elf_Internal_Shdr *           symtab_hdr;
    401   struct elf_link_hash_entry ** sym_hashes;
    402   Elf_Internal_Rela *           rel;
    403   Elf_Internal_Rela *           relend;
    404   bfd *dynobj;
    405   asection *splt;
    406 
    407   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
    408   sym_hashes = elf_sym_hashes (input_bfd);
    409   relend     = relocs + input_section->reloc_count;
    410 
    411   dynobj = elf_hash_table (info)->dynobj;
    412   splt = NULL;
    413   if (dynobj != NULL)
    414     splt = bfd_get_linker_section (dynobj, ".plt");
    415 
    416   for (rel = relocs; rel < relend; rel ++)
    417     {
    418       reloc_howto_type *           howto;
    419       unsigned long                r_symndx;
    420       Elf_Internal_Sym *           sym;
    421       asection *                   sec;
    422       struct elf_link_hash_entry * h;
    423       bfd_vma                      relocation;
    424       bfd_reloc_status_type        r;
    425       const char *                 name = NULL;
    426       int                          r_type;
    427 
    428       r_type = ELF32_R_TYPE (rel->r_info);
    429 
    430       /* These are only used for relaxing; we don't actually relocate
    431 	 anything with them, so skip them.  */
    432       if (r_type == R_M32C_RL_JUMP
    433 	  || r_type == R_M32C_RL_1ADDR
    434 	  || r_type == R_M32C_RL_2ADDR)
    435 	continue;
    436 
    437       r_symndx = ELF32_R_SYM (rel->r_info);
    438 
    439       howto  = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
    440       h      = NULL;
    441       sym    = NULL;
    442       sec    = NULL;
    443       relocation = 0;
    444 
    445       if (r_symndx < symtab_hdr->sh_info)
    446 	{
    447 	  sym = local_syms + r_symndx;
    448 	  sec = local_sections [r_symndx];
    449 	  relocation = (sec->output_section->vma
    450 			+ sec->output_offset
    451 			+ sym->st_value);
    452 
    453 	  name = bfd_elf_string_from_elf_section
    454 	    (input_bfd, symtab_hdr->sh_link, sym->st_name);
    455 	  name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
    456 	}
    457       else
    458 	{
    459 	  h = sym_hashes [r_symndx - symtab_hdr->sh_info];
    460 
    461 	  if (info->wrap_hash != NULL
    462 	      && (input_section->flags & SEC_DEBUGGING) != 0)
    463 	    h = ((struct elf_link_hash_entry *)
    464 		 unwrap_hash_lookup (info, input_bfd, &h->root));
    465 
    466 	  while (h->root.type == bfd_link_hash_indirect
    467 		 || h->root.type == bfd_link_hash_warning)
    468 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
    469 
    470 	  name = h->root.root.string;
    471 
    472 	  if (h->root.type == bfd_link_hash_defined
    473 	      || h->root.type == bfd_link_hash_defweak)
    474 	    {
    475 	      sec = h->root.u.def.section;
    476 	      relocation = (h->root.u.def.value
    477 			    + sec->output_section->vma
    478 			    + sec->output_offset);
    479 	    }
    480 	  else if (h->root.type == bfd_link_hash_undefweak)
    481 	    ;
    482 	  else if (!bfd_link_relocatable (info))
    483 	    (*info->callbacks->undefined_symbol) (info, h->root.root.string,
    484 						  input_bfd, input_section,
    485 						  rel->r_offset, TRUE);
    486 	}
    487 
    488       if (sec != NULL && discarded_section (sec))
    489 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
    490 					 rel, 1, relend, howto, 0, contents);
    491 
    492       if (bfd_link_relocatable (info))
    493 	{
    494 	  /* This is a relocatable link.  We don't have to change
    495              anything, unless the reloc is against a section symbol,
    496              in which case we have to adjust according to where the
    497              section symbol winds up in the output section.  */
    498 	  if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
    499 	    rel->r_addend += sec->output_offset;
    500 	  continue;
    501 	}
    502 
    503       switch (ELF32_R_TYPE (rel->r_info))
    504 	{
    505 	case R_M32C_16:
    506 	  {
    507 	    bfd_vma *plt_offset;
    508 
    509 	    if (h != NULL)
    510 	      plt_offset = &h->plt.offset;
    511 	    else
    512 	      plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
    513 
    514 	    /*	    printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
    515 		    relocation, *plt_offset);*/
    516 	    if (relocation <= 0xffff)
    517 	      {
    518 	        /* If the symbol is in range for a 16-bit address, we should
    519 		   have deallocated the plt entry in relax_section.  */
    520 	        BFD_ASSERT (*plt_offset == (bfd_vma) -1);
    521 	      }
    522 	    else
    523 	      {
    524 		/* If the symbol is out of range for a 16-bit address,
    525 		   we must have allocated a plt entry.  */
    526 		BFD_ASSERT (*plt_offset != (bfd_vma) -1);
    527 
    528 		/* If this is the first time we've processed this symbol,
    529 		   fill in the plt entry with the correct symbol address.  */
    530 		if ((*plt_offset & 1) == 0)
    531 		  {
    532 		    unsigned int x;
    533 
    534 		    x = 0x000000fc;  /* jmpf */
    535 		    x |= (relocation << 8) & 0xffffff00;
    536 		    bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
    537 		    *plt_offset |= 1;
    538 		  }
    539 
    540 		relocation = (splt->output_section->vma
    541 			      + splt->output_offset
    542 			      + (*plt_offset & -2));
    543 		if (name)
    544 		{
    545 		  char *newname = bfd_malloc (strlen(name)+5);
    546 		  strcpy (newname, name);
    547 		  strcat(newname, ".plt");
    548 		  _bfd_generic_link_add_one_symbol (info,
    549 						    input_bfd,
    550 						    newname,
    551 						    BSF_FUNCTION | BSF_WEAK,
    552 						    splt,
    553 						    (*plt_offset & -2),
    554 						    0,
    555 						    1,
    556 						    0,
    557 						    0);
    558 		}
    559 	      }
    560 	  }
    561 	  break;
    562 
    563 	case R_M32C_HI8:
    564 	case R_M32C_HI16:
    565 	  relocation >>= 16;
    566 	  break;
    567 	}
    568 
    569 #if 0
    570       printf ("relocate %s at %06lx relocation %06lx addend %ld  ",
    571 	      m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
    572 	      rel->r_offset + input_section->output_section->vma + input_section->output_offset,
    573 	      relocation, rel->r_addend);
    574       {
    575 	int i;
    576 	for (i=0; i<4; i++)
    577 	  printf (" %02x", contents[rel->r_offset+i]);
    578 	printf ("\n");
    579       }
    580 #endif
    581       switch (ELF32_R_TYPE(rel->r_info))
    582 	{
    583 	case R_M32C_24:
    584 	  /* Like m32c_apply_reloc_24, we must handle this one separately.  */
    585 	  relocation += rel->r_addend;
    586 
    587 	  /* Sanity check the address.  */
    588 	  if (rel->r_offset + 3
    589 	      > bfd_get_section_limit_octets (input_bfd, input_section))
    590 	    r = bfd_reloc_outofrange;
    591 	  else
    592 	    {
    593 	      bfd_put_8 (input_bfd, relocation & 0xff, contents + rel->r_offset);
    594 	      bfd_put_8 (input_bfd, (relocation >> 8) & 0xff, contents + rel->r_offset + 1);
    595 	      bfd_put_8 (input_bfd, (relocation >> 16) & 0xff, contents + rel->r_offset + 2);
    596 	      r = bfd_reloc_ok;
    597 	    }
    598 
    599 	  break;
    600 
    601 	default:
    602 	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
    603 					contents, rel->r_offset, relocation,
    604 					rel->r_addend);
    605 	  break;
    606 	}
    607 
    608       if (r != bfd_reloc_ok)
    609 	{
    610 	  const char * msg = (const char *) NULL;
    611 
    612 	  switch (r)
    613 	    {
    614 	    case bfd_reloc_overflow:
    615 	      (*info->callbacks->reloc_overflow)
    616 		(info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
    617 		 input_bfd, input_section, rel->r_offset);
    618 	      break;
    619 
    620 	    case bfd_reloc_undefined:
    621 	      (*info->callbacks->undefined_symbol)
    622 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
    623 	      break;
    624 
    625 	    case bfd_reloc_outofrange:
    626 	      msg = _("internal error: out of range error");
    627 	      break;
    628 
    629 	    case bfd_reloc_notsupported:
    630 	      msg = _("internal error: unsupported relocation error");
    631 	      break;
    632 
    633 	    case bfd_reloc_dangerous:
    634 	      msg = _("internal error: dangerous relocation");
    635 	      break;
    636 
    637 	    default:
    638 	      msg = _("internal error: unknown error");
    639 	      break;
    640 	    }
    641 
    642 	  if (msg)
    643 	    (*info->callbacks->warning) (info, msg, name, input_bfd,
    644 					 input_section, rel->r_offset);
    645 	}
    646     }
    647 
    648   return TRUE;
    649 }
    650 
    651 /* We support 16-bit pointers to code above 64k by generating a thunk
    653    below 64k containing a JMP instruction to the final address.  */
    654 
    655 static bfd_boolean
    656 m32c_elf_check_relocs
    657     (bfd *                     abfd,
    658      struct bfd_link_info *    info,
    659      asection *                sec,
    660      const Elf_Internal_Rela * relocs)
    661 {
    662   Elf_Internal_Shdr *           symtab_hdr;
    663   struct elf_link_hash_entry ** sym_hashes;
    664   const Elf_Internal_Rela *     rel;
    665   const Elf_Internal_Rela *     rel_end;
    666   bfd_vma *local_plt_offsets;
    667   asection *splt;
    668   bfd *dynobj;
    669 
    670   if (bfd_link_relocatable (info))
    671     return TRUE;
    672 
    673   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    674   sym_hashes = elf_sym_hashes (abfd);
    675   local_plt_offsets = elf_local_got_offsets (abfd);
    676   splt = NULL;
    677   dynobj = elf_hash_table(info)->dynobj;
    678 
    679   rel_end = relocs + sec->reloc_count;
    680   for (rel = relocs; rel < rel_end; rel++)
    681     {
    682       struct elf_link_hash_entry *h;
    683       unsigned long r_symndx;
    684       bfd_vma *offset;
    685 
    686       r_symndx = ELF32_R_SYM (rel->r_info);
    687       if (r_symndx < symtab_hdr->sh_info)
    688         h = NULL;
    689       else
    690 	{
    691 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
    692 	  while (h->root.type == bfd_link_hash_indirect
    693 		 || h->root.type == bfd_link_hash_warning)
    694 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
    695 
    696 	  /* PR15323, ref flags aren't set for references in the same
    697 	     object.  */
    698 	  h->root.non_ir_ref = 1;
    699 	}
    700 
    701       switch (ELF32_R_TYPE (rel->r_info))
    702         {
    703 	  /* This relocation describes a 16-bit pointer to a function.
    704 	     We may need to allocate a thunk in low memory; reserve memory
    705 	     for it now.  */
    706 	case R_M32C_16:
    707 	  if (dynobj == NULL)
    708 	    elf_hash_table (info)->dynobj = dynobj = abfd;
    709 	  if (splt == NULL)
    710 	    {
    711 	      splt = bfd_get_linker_section (dynobj, ".plt");
    712 	      if (splt == NULL)
    713 		{
    714 		  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
    715 				    | SEC_IN_MEMORY | SEC_LINKER_CREATED
    716 				    | SEC_READONLY | SEC_CODE);
    717 		  splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
    718 							     flags);
    719 		  if (splt == NULL
    720 		      || ! bfd_set_section_alignment (dynobj, splt, 1))
    721 		    return FALSE;
    722 		}
    723 	    }
    724 
    725 	  if (h != NULL)
    726 	    offset = &h->plt.offset;
    727 	  else
    728 	    {
    729 	      if (local_plt_offsets == NULL)
    730 		{
    731 		  size_t size;
    732 		  unsigned int i;
    733 
    734 		  size = symtab_hdr->sh_info * sizeof (bfd_vma);
    735 		  local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
    736 		  if (local_plt_offsets == NULL)
    737 		    return FALSE;
    738 		  elf_local_got_offsets (abfd) = local_plt_offsets;
    739 
    740 		  for (i = 0; i < symtab_hdr->sh_info; i++)
    741 		    local_plt_offsets[i] = (bfd_vma) -1;
    742 		}
    743 	      offset = &local_plt_offsets[r_symndx];
    744 	    }
    745 
    746 	  if (*offset == (bfd_vma) -1)
    747 	    {
    748 	      *offset = splt->size;
    749 	      splt->size += 4;
    750 	    }
    751 	  break;
    752         }
    753     }
    754 
    755   return TRUE;
    756 }
    757 
    758 /* This must exist if dynobj is ever set.  */
    759 
    760 static bfd_boolean
    761 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
    762                                   struct bfd_link_info *info)
    763 {
    764   bfd *dynobj;
    765   asection *splt;
    766 
    767   /* As an extra sanity check, verify that all plt entries have
    768      been filled in.  */
    769 
    770   if ((dynobj = elf_hash_table (info)->dynobj) != NULL
    771       && (splt = bfd_get_linker_section (dynobj, ".plt")) != NULL)
    772     {
    773       bfd_byte *contents = splt->contents;
    774       unsigned int i, size = splt->size;
    775       for (i = 0; i < size; i += 4)
    776 	{
    777 	  unsigned int x = bfd_get_32 (dynobj, contents + i);
    778 	  BFD_ASSERT (x != 0);
    779 	}
    780     }
    781 
    782   return TRUE;
    783 }
    784 
    785 static bfd_boolean
    786 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
    787                                struct bfd_link_info *info)
    788 {
    789   bfd *dynobj;
    790   asection *splt;
    791 
    792   if (bfd_link_relocatable (info))
    793     return TRUE;
    794 
    795   dynobj = elf_hash_table (info)->dynobj;
    796   if (dynobj == NULL)
    797     return TRUE;
    798 
    799   splt = bfd_get_linker_section (dynobj, ".plt");
    800   BFD_ASSERT (splt != NULL);
    801 
    802   splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
    803   if (splt->contents == NULL)
    804     return FALSE;
    805 
    806   return TRUE;
    807 }
    808 
    809 /* Function to set the ELF flag bits.  */
    811 
    812 static bfd_boolean
    813 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
    814 {
    815   elf_elfheader (abfd)->e_flags = flags;
    816   elf_flags_init (abfd) = TRUE;
    817   return TRUE;
    818 }
    819 
    820 /* Merge backend specific data from an object file to the output
    821    object file when linking.  */
    822 
    823 static bfd_boolean
    824 m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
    825 {
    826   flagword old_flags, old_partial;
    827   flagword new_flags, new_partial;
    828   bfd_boolean error = FALSE;
    829   char new_opt[80];
    830   char old_opt[80];
    831 
    832   new_opt[0] = old_opt[0] = '\0';
    833   new_flags = elf_elfheader (ibfd)->e_flags;
    834   old_flags = elf_elfheader (obfd)->e_flags;
    835 
    836 #ifdef DEBUG
    837   (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
    838 			 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
    839 			 bfd_get_filename (ibfd));
    840 #endif
    841 
    842   if (!elf_flags_init (obfd))
    843     {
    844       /* First call, no flags set.  */
    845       elf_flags_init (obfd) = TRUE;
    846       elf_elfheader (obfd)->e_flags = new_flags;
    847     }
    848 
    849   else if (new_flags == old_flags)
    850     /* Compatible flags are ok.	 */
    851     ;
    852 
    853   else		/* Possibly incompatible flags.	 */
    854     {
    855       /* Warn if different cpu is used (allow a specific cpu to override
    856 	 the generic cpu).  */
    857       new_partial = (new_flags & EF_M32C_CPU_MASK);
    858       old_partial = (old_flags & EF_M32C_CPU_MASK);
    859       if (new_partial == old_partial)
    860 	;
    861 
    862       else
    863 	{
    864 	  switch (new_partial)
    865 	    {
    866 	    default:		  strcat (new_opt, " -m16c");	break;
    867 	    case EF_M32C_CPU_M16C:	strcat (new_opt, " -m16c");  break;
    868 	    case EF_M32C_CPU_M32C:  strcat (new_opt, " -m32c");  break;
    869 	    }
    870 
    871 	  switch (old_partial)
    872 	    {
    873 	    default:		  strcat (old_opt, " -m16c");	break;
    874 	    case EF_M32C_CPU_M16C:	strcat (old_opt, " -m16c");  break;
    875 	    case EF_M32C_CPU_M32C:  strcat (old_opt, " -m32c");  break;
    876 	    }
    877 	}
    878 
    879       /* Print out any mismatches from above.  */
    880       if (new_opt[0])
    881 	{
    882 	  error = TRUE;
    883 	  (*_bfd_error_handler)
    884 	    (_("%s: compiled with %s and linked with modules compiled with %s"),
    885 	     bfd_get_filename (ibfd), new_opt, old_opt);
    886 	}
    887 
    888       new_flags &= ~ EF_M32C_ALL_FLAGS;
    889       old_flags &= ~ EF_M32C_ALL_FLAGS;
    890 
    891       /* Warn about any other mismatches.  */
    892       if (new_flags != old_flags)
    893 	{
    894 	  error = TRUE;
    895 	  (*_bfd_error_handler)
    896 	    (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
    897 	     bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
    898 	}
    899     }
    900 
    901   if (error)
    902     bfd_set_error (bfd_error_bad_value);
    903 
    904   return !error;
    905 }
    906 
    907 
    908 static bfd_boolean
    910 m32c_elf_print_private_bfd_data (bfd *abfd, void *ptr)
    911 {
    912   FILE *file = (FILE *) ptr;
    913   flagword flags;
    914 
    915   BFD_ASSERT (abfd != NULL && ptr != NULL);
    916 
    917   /* Print normal ELF private data.  */
    918   _bfd_elf_print_private_bfd_data (abfd, ptr);
    919 
    920   flags = elf_elfheader (abfd)->e_flags;
    921   fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
    922 
    923   switch (flags & EF_M32C_CPU_MASK)
    924     {
    925     default:							break;
    926     case EF_M32C_CPU_M16C:	fprintf (file, " -m16c");	break;
    927     case EF_M32C_CPU_M32C:  fprintf (file, " -m32c");	break;
    928     }
    929 
    930   fputc ('\n', file);
    931   return TRUE;
    932 }
    933 
    934 /* Return the MACH for an e_flags value.  */
    935 
    936 static int
    937 elf32_m32c_machine (bfd *abfd)
    938 {
    939   switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
    940     {
    941     case EF_M32C_CPU_M16C:	return bfd_mach_m16c;
    942     case EF_M32C_CPU_M32C:  	return bfd_mach_m32c;
    943     }
    944 
    945   return bfd_mach_m16c;
    946 }
    947 
    948 static bfd_boolean
    949 m32c_elf_object_p (bfd *abfd)
    950 {
    951   bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
    952 			     elf32_m32c_machine (abfd));
    953   return TRUE;
    954 }
    955 
    956 
    958 #ifdef DEBUG
    959 void
    960 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
    961 {
    962   size_t locsymcount;
    963   Elf_Internal_Sym *isymbuf;
    964   Elf_Internal_Sym *isymend;
    965   Elf_Internal_Sym *isym;
    966   Elf_Internal_Shdr *symtab_hdr;
    967   bfd_boolean free_internal = 0, free_external = 0;
    968   char * st_info_str;
    969   char * st_info_stb_str;
    970   char * st_other_str;
    971   char * st_shndx_str;
    972 
    973   if (! internal_syms)
    974     {
    975       internal_syms = bfd_malloc (1000);
    976       free_internal = 1;
    977     }
    978   if (! external_syms)
    979     {
    980       external_syms = bfd_malloc (1000);
    981       free_external = 1;
    982     }
    983 
    984   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    985   locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
    986   if (free_internal)
    987     isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
    988 				    symtab_hdr->sh_info, 0,
    989 				    internal_syms, external_syms, NULL);
    990   else
    991     isymbuf = internal_syms;
    992   isymend = isymbuf + locsymcount;
    993 
    994   for (isym = isymbuf ; isym < isymend ; isym++)
    995     {
    996       switch (ELF_ST_TYPE (isym->st_info))
    997 	{
    998 	case STT_FUNC:
    999 	  st_info_str = "STT_FUNC";
   1000 	  break;
   1001 
   1002 	case STT_SECTION:
   1003 	  st_info_str = "STT_SECTION";
   1004 	  break;
   1005 
   1006 	case STT_FILE:
   1007 	  st_info_str = "STT_FILE";
   1008 	  break;
   1009 
   1010 	case STT_OBJECT:
   1011 	  st_info_str = "STT_OBJECT";
   1012 	  break;
   1013 
   1014 	case STT_TLS:
   1015 	  st_info_str = "STT_TLS";
   1016 	  break;
   1017 
   1018 	default:
   1019 	  st_info_str = "";
   1020 	}
   1021 
   1022       switch (ELF_ST_BIND (isym->st_info))
   1023 	{
   1024 	case STB_LOCAL:
   1025 	  st_info_stb_str = "STB_LOCAL";
   1026 	  break;
   1027 
   1028 	case STB_GLOBAL:
   1029 	  st_info_stb_str = "STB_GLOBAL";
   1030 	  break;
   1031 
   1032 	default:
   1033 	  st_info_stb_str = "";
   1034 	}
   1035 
   1036       switch (ELF_ST_VISIBILITY (isym->st_other))
   1037 	{
   1038 	case STV_DEFAULT:
   1039 	  st_other_str = "STV_DEFAULT";
   1040 	  break;
   1041 
   1042 	case STV_INTERNAL:
   1043 	  st_other_str = "STV_INTERNAL";
   1044 	  break;
   1045 
   1046 	case STV_PROTECTED:
   1047 	  st_other_str = "STV_PROTECTED";
   1048 	  break;
   1049 
   1050 	default:
   1051 	  st_other_str = "";
   1052 	}
   1053 
   1054       switch (isym->st_shndx)
   1055 	{
   1056 	case SHN_ABS:
   1057 	  st_shndx_str = "SHN_ABS";
   1058 	  break;
   1059 
   1060 	case SHN_COMMON:
   1061 	  st_shndx_str = "SHN_COMMON";
   1062 	  break;
   1063 
   1064 	case SHN_UNDEF:
   1065 	  st_shndx_str = "SHN_UNDEF";
   1066 	  break;
   1067 
   1068 	default:
   1069 	  st_shndx_str = "";
   1070 	}
   1071 
   1072       printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
   1073 	      "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
   1074 	      isym,
   1075 	      (unsigned long) isym->st_value,
   1076 	      (unsigned long) isym->st_size,
   1077 	      isym->st_name,
   1078 	      bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
   1079 					       isym->st_name),
   1080 	      isym->st_info, st_info_str, st_info_stb_str,
   1081 	      isym->st_other, st_other_str,
   1082 	      isym->st_shndx, st_shndx_str);
   1083     }
   1084   if (free_internal)
   1085     free (internal_syms);
   1086   if (free_external)
   1087     free (external_syms);
   1088 }
   1089 
   1090 char *
   1091 m32c_get_reloc (long reloc)
   1092 {
   1093   if (0 <= reloc && reloc < R_M32C_max)
   1094     return m32c_elf_howto_table[reloc].name;
   1095   else
   1096     return "";
   1097 }
   1098 #endif /* DEBUG */
   1099 
   1100 /* Handle relaxing.  */
   1101 
   1102 /* A subroutine of m32c_elf_relax_section.  If the global symbol H
   1103    is within the low 64k, remove any entry for it in the plt.  */
   1104 
   1105 struct relax_plt_data
   1106 {
   1107   asection *splt;
   1108   bfd_boolean *again;
   1109 };
   1110 
   1111 static bfd_boolean
   1112 m32c_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
   1113 {
   1114   struct relax_plt_data *data = (struct relax_plt_data *) xdata;
   1115 
   1116   if (h->plt.offset != (bfd_vma) -1)
   1117     {
   1118       bfd_vma address;
   1119 
   1120       if (h->root.type == bfd_link_hash_undefined
   1121 	  || h->root.type == bfd_link_hash_undefweak)
   1122 	address = 0;
   1123       else
   1124 	address = (h->root.u.def.section->output_section->vma
   1125 		   + h->root.u.def.section->output_offset
   1126 		   + h->root.u.def.value);
   1127 
   1128       if (address <= 0xffff)
   1129 	{
   1130 	  h->plt.offset = -1;
   1131 	  data->splt->size -= 4;
   1132 	  *data->again = TRUE;
   1133 	}
   1134     }
   1135 
   1136   return TRUE;
   1137 }
   1138 
   1139 /* A subroutine of m32c_elf_relax_section.  If the global symbol H
   1140    previously had a plt entry, give it a new entry offset.  */
   1141 
   1142 static bfd_boolean
   1143 m32c_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
   1144 {
   1145   bfd_vma *entry = (bfd_vma *) xdata;
   1146 
   1147   if (h->plt.offset != (bfd_vma) -1)
   1148     {
   1149       h->plt.offset = *entry;
   1150       *entry += 4;
   1151     }
   1152 
   1153   return TRUE;
   1154 }
   1155 
   1156 static bfd_boolean
   1157 m32c_elf_relax_plt_section (asection *splt,
   1158                             struct bfd_link_info *info,
   1159                             bfd_boolean *again)
   1160 {
   1161   struct relax_plt_data relax_plt_data;
   1162   bfd *ibfd;
   1163 
   1164   /* Assume nothing changes.  */
   1165   *again = FALSE;
   1166 
   1167   if (bfd_link_relocatable (info))
   1168     return TRUE;
   1169 
   1170   /* Quick check for an empty plt.  */
   1171   if (splt->size == 0)
   1172     return TRUE;
   1173 
   1174   /* Map across all global symbols; see which ones happen to
   1175      fall in the low 64k.  */
   1176   relax_plt_data.splt = splt;
   1177   relax_plt_data.again = again;
   1178   elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
   1179 			  &relax_plt_data);
   1180 
   1181   /* Likewise for local symbols, though that's somewhat less convenient
   1182      as we have to walk the list of input bfds and swap in symbol data.  */
   1183   for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
   1184     {
   1185       bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
   1186       Elf_Internal_Shdr *symtab_hdr;
   1187       Elf_Internal_Sym *isymbuf = NULL;
   1188       unsigned int idx;
   1189 
   1190       if (! local_plt_offsets)
   1191 	continue;
   1192 
   1193       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
   1194       if (symtab_hdr->sh_info != 0)
   1195 	{
   1196 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   1197 	  if (isymbuf == NULL)
   1198 	    isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   1199 					    symtab_hdr->sh_info, 0,
   1200 					    NULL, NULL, NULL);
   1201 	  if (isymbuf == NULL)
   1202 	    return FALSE;
   1203 	}
   1204 
   1205       for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
   1206 	{
   1207 	  Elf_Internal_Sym *isym;
   1208 	  asection *tsec;
   1209 	  bfd_vma address;
   1210 
   1211 	  if (local_plt_offsets[idx] == (bfd_vma) -1)
   1212 	    continue;
   1213 
   1214 	  isym = &isymbuf[idx];
   1215 	  if (isym->st_shndx == SHN_UNDEF)
   1216 	    continue;
   1217 	  else if (isym->st_shndx == SHN_ABS)
   1218 	    tsec = bfd_abs_section_ptr;
   1219 	  else if (isym->st_shndx == SHN_COMMON)
   1220 	    tsec = bfd_com_section_ptr;
   1221 	  else
   1222 	    tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
   1223 
   1224 	  address = (tsec->output_section->vma
   1225 		     + tsec->output_offset
   1226 		     + isym->st_value);
   1227 	  if (address <= 0xffff)
   1228 	    {
   1229 	      local_plt_offsets[idx] = -1;
   1230 	      splt->size -= 4;
   1231 	      *again = TRUE;
   1232 	    }
   1233 	}
   1234 
   1235       if (isymbuf != NULL
   1236 	  && symtab_hdr->contents != (unsigned char *) isymbuf)
   1237 	{
   1238 	  if (! info->keep_memory)
   1239 	    free (isymbuf);
   1240 	  else
   1241 	    {
   1242 	      /* Cache the symbols for elf_link_input_bfd.  */
   1243 	      symtab_hdr->contents = (unsigned char *) isymbuf;
   1244 	    }
   1245 	}
   1246     }
   1247 
   1248   /* If we changed anything, walk the symbols again to reallocate
   1249      .plt entry addresses.  */
   1250   if (*again && splt->size > 0)
   1251     {
   1252       bfd_vma entry = 0;
   1253 
   1254       elf_link_hash_traverse (elf_hash_table (info),
   1255 			      m32c_relax_plt_realloc, &entry);
   1256 
   1257       for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
   1258 	{
   1259 	  bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
   1260 	  unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
   1261 	  unsigned int idx;
   1262 
   1263 	  if (! local_plt_offsets)
   1264 	    continue;
   1265 
   1266 	  for (idx = 0; idx < nlocals; ++idx)
   1267 	    if (local_plt_offsets[idx] != (bfd_vma) -1)
   1268 	      {
   1269 	        local_plt_offsets[idx] = entry;
   1270 		entry += 4;
   1271 	      }
   1272 	}
   1273     }
   1274 
   1275   return TRUE;
   1276 }
   1277 
   1278 static int
   1279 compare_reloc (const void *e1, const void *e2)
   1280 {
   1281   const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
   1282   const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
   1283 
   1284   if (i1->r_offset == i2->r_offset)
   1285     return 0;
   1286   else
   1287     return i1->r_offset < i2->r_offset ? -1 : 1;
   1288 }
   1289 
   1290 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
   1291 static bfd_vma
   1292 m32c_offset_for_reloc (bfd *abfd,
   1293 		       Elf_Internal_Rela *rel,
   1294 		       Elf_Internal_Shdr *symtab_hdr,
   1295 		       Elf_External_Sym_Shndx *shndx_buf ATTRIBUTE_UNUSED,
   1296 		       Elf_Internal_Sym *intsyms)
   1297 {
   1298   bfd_vma symval;
   1299 
   1300   /* Get the value of the symbol referred to by the reloc.  */
   1301   if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
   1302     {
   1303       /* A local symbol.  */
   1304       Elf_Internal_Sym *isym;
   1305       asection *ssec;
   1306 
   1307       isym = intsyms + ELF32_R_SYM (rel->r_info);
   1308       ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   1309       symval = isym->st_value;
   1310       if (ssec)
   1311 	symval += ssec->output_section->vma
   1312 	  + ssec->output_offset;
   1313     }
   1314   else
   1315     {
   1316       unsigned long indx;
   1317       struct elf_link_hash_entry *h;
   1318 
   1319       /* An external symbol.  */
   1320       indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
   1321       h = elf_sym_hashes (abfd)[indx];
   1322       BFD_ASSERT (h != NULL);
   1323 
   1324       if (h->root.type != bfd_link_hash_defined
   1325 	  && h->root.type != bfd_link_hash_defweak)
   1326 	/* This appears to be a reference to an undefined
   1327 	   symbol.  Just ignore it--it will be caught by the
   1328 	   regular reloc processing.  */
   1329 	return 0;
   1330 
   1331       symval = (h->root.u.def.value
   1332 		+ h->root.u.def.section->output_section->vma
   1333 		+ h->root.u.def.section->output_offset);
   1334     }
   1335   return symval;
   1336 }
   1337 
   1338 static int bytes_saved = 0;
   1339 
   1340 static int bytes_to_reloc[] = {
   1341   R_M32C_NONE,
   1342   R_M32C_8,
   1343   R_M32C_16,
   1344   R_M32C_24,
   1345   R_M32C_32
   1346 };
   1347 
   1348 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for.  */
   1349 
   1350 /* Mask for the number of relocs associated with this insn.  */
   1351 #define RLA_RELOCS		0x0000000f
   1352 /* Number of bytes gas emitted (before gas's relaxing) */
   1353 #define RLA_NBYTES		0x00000ff0
   1354 
   1355 /* If the displacement is within the given range and the new encoding
   1356    differs from the old encoding (the index), then the insn can be
   1357    relaxed to the new encoding.  */
   1358 typedef struct {
   1359   int bytes;
   1360   unsigned int max_disp;
   1361   unsigned char new_encoding;
   1362 } EncodingTable;
   1363 
   1364 static EncodingTable m16c_addr_encodings[] = {
   1365   { 0,   0,  0 }, /* R0 */
   1366   { 0,   0,  1 }, /* R1 */
   1367   { 0,   0,  2 }, /* R2 */
   1368   { 0,   0,  3 }, /* R3 */
   1369   { 0,   0,  4 }, /* A0 */
   1370   { 0,   0,  5 }, /* A1 */
   1371   { 0,   0,  6 }, /* [A0] */
   1372   { 0,   0,  7 }, /* [A1] */
   1373   { 1,   0,  6 }, /* udsp:8[A0] */
   1374   { 1,   0,  7 }, /* udsp:8[A1] */
   1375   { 1,   0, 10 }, /* udsp:8[SB] */
   1376   { 1,   0, 11 }, /* sdsp:8[FB] */
   1377   { 2, 255,  8 }, /* udsp:16[A0] */
   1378   { 2, 255,  9 }, /* udsp:16[A1] */
   1379   { 2, 255, 10 }, /* udsp:16[SB] */
   1380   { 2,   0, 15 }, /* abs:16 */
   1381 };
   1382 
   1383 static EncodingTable m16c_jmpaddr_encodings[] = {
   1384   { 0,   0,  0 }, /* R0 */
   1385   { 0,   0,  1 }, /* R1 */
   1386   { 0,   0,  2 }, /* R2 */
   1387   { 0,   0,  3 }, /* R3 */
   1388   { 0,   0,  4 }, /* A0 */
   1389   { 0,   0,  5 }, /* A1 */
   1390   { 0,   0,  6 }, /* [A0] */
   1391   { 0,   0,  7 }, /* [A1] */
   1392   { 1,   0,  6 }, /* udsp:8[A0] */
   1393   { 1,   0,  7 }, /* udsp:8[A1] */
   1394   { 1,   0, 10 }, /* udsp:8[SB] */
   1395   { 1,   0, 11 }, /* sdsp:8[FB] */
   1396   { 3, 255,  8 }, /* udsp:20[A0] */
   1397   { 3, 255,  9 }, /* udsp:20[A1] */
   1398   { 2, 255, 10 }, /* udsp:16[SB] */
   1399   { 2,   0, 15 }, /* abs:16 */
   1400 };
   1401 
   1402 static EncodingTable m32c_addr_encodings[] = {
   1403   { 0,     0,  0 }, /* [A0] */
   1404   { 0,     0,  1 }, /* [A1] */
   1405   { 0,     0,  2 }, /* A0 */
   1406   { 0,     0,  3 }, /* A1 */
   1407   { 1,     0,  0 }, /* udsp:8[A0] */
   1408   { 1,     0,  1 }, /* udsp:8[A1] */
   1409   { 1,     0,  6 }, /* udsp:8[SB] */
   1410   { 1,     0,  7 }, /* sdsp:8[FB] */
   1411   { 2,   255,  4 }, /* udsp:16[A0] */
   1412   { 2,   255,  5 }, /* udsp:16[A1] */
   1413   { 2,   255,  6 }, /* udsp:16[SB] */
   1414   { 2,   127,  7 }, /* sdsp:16[FB] */
   1415   { 3, 65535, 8 }, /* udsp:24[A0] */
   1416   { 3, 65535, 9 }, /* udsp:24[A1] */
   1417   { 3, 65535, 15 }, /* abs24 */
   1418   { 2,     0, 15 }, /* abs16 */
   1419   { 0,     0, 16 }, /* R2 */
   1420   { 0,     0, 17 }, /* R3 */
   1421   { 0,     0, 18 }, /* R0 */
   1422   { 0,     0, 19 }, /* R1 */
   1423   { 0,     0, 20 }, /*  */
   1424   { 0,     0, 21 }, /*  */
   1425   { 0,     0, 22 }, /*  */
   1426   { 0,     0, 23 }, /*  */
   1427   { 0,     0, 24 }, /*  */
   1428   { 0,     0, 25 }, /*  */
   1429   { 0,     0, 26 }, /*  */
   1430   { 0,     0, 27 }, /*  */
   1431   { 0,     0, 28 }, /*  */
   1432   { 0,     0, 29 }, /*  */
   1433   { 0,     0, 30 }, /*  */
   1434   { 0,     0, 31 }, /*  */
   1435 };
   1436 
   1437 static bfd_boolean
   1438 m32c_elf_relax_section
   1439     (bfd *                  abfd,
   1440      asection *             sec,
   1441      struct bfd_link_info * link_info,
   1442      bfd_boolean *          again)
   1443 {
   1444   Elf_Internal_Shdr *symtab_hdr;
   1445   Elf_Internal_Shdr *shndx_hdr;
   1446   Elf_Internal_Rela *internal_relocs;
   1447   Elf_Internal_Rela *free_relocs = NULL;
   1448   Elf_Internal_Rela *irel, *irelend, *srel;
   1449   bfd_byte * contents = NULL;
   1450   bfd_byte * free_contents = NULL;
   1451   Elf_Internal_Sym *intsyms = NULL;
   1452   Elf_Internal_Sym *free_intsyms = NULL;
   1453   Elf_External_Sym_Shndx *shndx_buf = NULL;
   1454   int machine;
   1455 
   1456   if (abfd == elf_hash_table (link_info)->dynobj
   1457       && (sec->flags & SEC_LINKER_CREATED) != 0
   1458       && strcmp (sec->name, ".plt") == 0)
   1459     return m32c_elf_relax_plt_section (sec, link_info, again);
   1460 
   1461   /* Assume nothing changes.  */
   1462   *again = FALSE;
   1463 
   1464   machine = elf32_m32c_machine (abfd);
   1465 
   1466   /* We don't have to do anything for a relocatable link, if
   1467      this section does not have relocs, or if this is not a
   1468      code section.  */
   1469   if (bfd_link_relocatable (link_info)
   1470       || (sec->flags & SEC_RELOC) == 0
   1471       || sec->reloc_count == 0
   1472       || (sec->flags & SEC_CODE) == 0)
   1473     return TRUE;
   1474 
   1475   symtab_hdr = & elf_symtab_hdr (abfd);
   1476   if (elf_symtab_shndx_list (abfd))
   1477     shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   1478   else
   1479     shndx_hdr = NULL;
   1480 
   1481   /* Get the section contents.  */
   1482   if (elf_section_data (sec)->this_hdr.contents != NULL)
   1483     contents = elf_section_data (sec)->this_hdr.contents;
   1484   /* Go get them off disk.  */
   1485   else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
   1486     goto error_return;
   1487 
   1488   /* Read this BFD's symbols.  */
   1489   /* Get cached copy if it exists.  */
   1490   if (symtab_hdr->contents != NULL)
   1491     {
   1492       intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   1493     }
   1494   else
   1495     {
   1496       intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
   1497       symtab_hdr->contents = (bfd_byte *) intsyms;
   1498     }
   1499 
   1500   if (shndx_hdr && shndx_hdr->sh_size != 0)
   1501     {
   1502       bfd_size_type amt;
   1503 
   1504       amt = symtab_hdr->sh_info;
   1505       amt *= sizeof (Elf_External_Sym_Shndx);
   1506       shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
   1507       if (shndx_buf == NULL)
   1508 	goto error_return;
   1509       if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
   1510 	  || bfd_bread (shndx_buf, amt, abfd) != amt)
   1511 	goto error_return;
   1512       shndx_hdr->contents = (bfd_byte *) shndx_buf;
   1513     }
   1514 
   1515   /* Get a copy of the native relocations.  */
   1516   internal_relocs = (_bfd_elf_link_read_relocs
   1517 		     (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
   1518 		      link_info->keep_memory));
   1519   if (internal_relocs == NULL)
   1520     goto error_return;
   1521   if (! link_info->keep_memory)
   1522     free_relocs = internal_relocs;
   1523 
   1524   /* The RL_ relocs must be just before the operand relocs they go
   1525      with, so we must sort them to guarantee this.  */
   1526   qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
   1527          compare_reloc);
   1528 
   1529   /* Walk through them looking for relaxing opportunities.  */
   1530   irelend = internal_relocs + sec->reloc_count;
   1531 
   1532   for (irel = internal_relocs; irel < irelend; irel++)
   1533     {
   1534       bfd_vma symval;
   1535       unsigned char *insn, *gap, *einsn;
   1536       bfd_vma pc;
   1537       bfd_signed_vma pcrel;
   1538       int relax_relocs;
   1539       int gap_size;
   1540       int new_type;
   1541       int posn;
   1542       int enc;
   1543       EncodingTable *enctbl;
   1544       EncodingTable *e;
   1545 
   1546       if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
   1547 	  && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
   1548 	  && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
   1549 	continue;
   1550 
   1551       srel = irel;
   1552 
   1553       /* There will always be room for the relaxed insn, since it is smaller
   1554 	 than the one it would replace.  */
   1555       BFD_ASSERT (irel->r_offset < sec->size);
   1556 
   1557       insn = contents + irel->r_offset;
   1558       relax_relocs = irel->r_addend % 16;
   1559 
   1560       /* Ok, we only have three relocs we care about, and they're all
   1561 	 fake.  The lower four bits of the addend is always the number
   1562 	 of following relocs (hence the qsort above) that are assigned
   1563 	 to this opcode.  The next 8 bits of the addend indicates the
   1564 	 number of bytes in the insn.  We use the rest of them
   1565 	 ourselves as flags for the more expensive operations (defines
   1566 	 above).  The three relocs are:
   1567 
   1568 	 RL_JUMP: This marks all direct jump insns.  We check the
   1569 		displacement and replace them with shorter jumps if
   1570 		they're in range.  We also use this to find JMP.S
   1571 		insns and manually shorten them when we delete bytes.
   1572 		We have to decode these insns to figure out what to
   1573 		do.
   1574 
   1575 	 RL_1ADDR: This is a :G or :Q insn, which has a single
   1576 		"standard" operand.  We have to extract the type
   1577 		field, see if it's a wide displacement, then figure
   1578 		out if we can replace it with a narrow displacement.
   1579 		We don't have to decode these insns.
   1580 
   1581 	 RL_2ADDR: Similarly, but two "standard" operands.  Note that
   1582 		r_addend may still be 1, as standard operands don't
   1583 		always have displacements.  Gas shouldn't give us one
   1584 		with zero operands, but since we don't know which one
   1585 		has the displacement, we check them both anyway.
   1586 
   1587 	 These all point to the beginning of the insn itself, not the
   1588 	 operands.
   1589 
   1590 	 Note that we only relax one step at a time, relying on the
   1591 	 linker to call us repeatedly.  Thus, there is no code for
   1592 	 JMP.A->JMP.B although that will happen in two steps.
   1593 	 Likewise, for 2ADDR relaxes, we do one operand per cycle.
   1594       */
   1595 
   1596       /* Get the value of the symbol referred to by the reloc.  Just
   1597          in case this is the last reloc in the list, use the RL's
   1598          addend to choose between this reloc (no addend) or the next
   1599          (yes addend, which means at least one following reloc).  */
   1600       srel = irel + (relax_relocs ? 1 : 0);
   1601       symval = OFFSET_FOR_RELOC (srel);
   1602 
   1603       /* Setting gap_size nonzero is the flag which means "something
   1604 	 shrunk".  */
   1605       gap_size = 0;
   1606       gap = NULL;
   1607       new_type = ELF32_R_TYPE(srel->r_info);
   1608 
   1609       pc = sec->output_section->vma + sec->output_offset
   1610 	+ srel->r_offset;
   1611       pcrel = symval - pc + srel->r_addend;
   1612 
   1613       if (machine == bfd_mach_m16c)
   1614 	{
   1615 	  /* R8C / M16C */
   1616 
   1617 	  switch (ELF32_R_TYPE(irel->r_info))
   1618 	    {
   1619 
   1620 	    case R_M32C_RL_JUMP:
   1621 	      switch (insn[0])
   1622 		{
   1623 		case 0xfe: /* jmp.b */
   1624 		  if (pcrel >= 2 && pcrel <= 9)
   1625 		    {
   1626 		      /* Relax JMP.B -> JMP.S.  We need to get rid of
   1627 			 the following reloc though. */
   1628 		      insn[0] = 0x60 | (pcrel - 2);
   1629 		      new_type = R_M32C_NONE;
   1630 		      irel->r_addend = 0x10;
   1631 		      gap_size = 1;
   1632 		      gap = insn + 1;
   1633 		    }
   1634 		  break;
   1635 
   1636 		case 0xf4: /* jmp.w */
   1637 		  /* 128 is allowed because it will be one byte closer
   1638 		     after relaxing.  Likewise for all other pc-rel
   1639 		     jumps.  */
   1640 		  if (pcrel <= 128 && pcrel >= -128)
   1641 		    {
   1642 		      /* Relax JMP.W -> JMP.B */
   1643 		      insn[0] = 0xfe;
   1644 		      insn[1] = 0;
   1645 		      new_type = R_M32C_8_PCREL;
   1646 		      gap_size = 1;
   1647 		      gap = insn + 2;
   1648 		    }
   1649 		  break;
   1650 
   1651 		case 0xfc: /* jmp.a */
   1652 		  if (pcrel <= 32768 && pcrel >= -32768)
   1653 		    {
   1654 		      /* Relax JMP.A -> JMP.W */
   1655 		      insn[0] = 0xf4;
   1656 		      insn[1] = 0;
   1657 		      insn[2] = 0;
   1658 		      new_type = R_M32C_16_PCREL;
   1659 		      gap_size = 1;
   1660 		      gap = insn + 3;
   1661 		    }
   1662 		  break;
   1663 
   1664 		case 0xfd: /* jsr.a */
   1665 		  if (pcrel <= 32768 && pcrel >= -32768)
   1666 		    {
   1667 		      /* Relax JSR.A -> JSR.W */
   1668 		      insn[0] = 0xf5;
   1669 		      insn[1] = 0;
   1670 		      insn[2] = 0;
   1671 		      new_type = R_M32C_16_PCREL;
   1672 		      gap_size = 1;
   1673 		      gap = insn + 3;
   1674 		    }
   1675 		  break;
   1676 		}
   1677 	      break;
   1678 
   1679 	    case R_M32C_RL_2ADDR:
   1680 	      /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
   1681 
   1682 	      enctbl = m16c_addr_encodings;
   1683 	      posn = 2;
   1684 	      enc = (insn[1] >> 4) & 0x0f;
   1685 	      e = & enctbl[enc];
   1686 
   1687 	      if (srel->r_offset == irel->r_offset + posn
   1688 		  && e->new_encoding != enc
   1689 		  && symval <= e->max_disp)
   1690 		{
   1691 		  insn[1] &= 0x0f;
   1692 		  insn[1] |= e->new_encoding << 4;
   1693 		  gap_size = e->bytes - enctbl[e->new_encoding].bytes;
   1694 		  gap = insn + posn + enctbl[e->new_encoding].bytes;
   1695 		  new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
   1696 		  break;
   1697 		}
   1698 	      if (relax_relocs == 2)
   1699 		srel ++;
   1700 	      posn += e->bytes;
   1701 
   1702 	      goto try_1addr_16;
   1703 
   1704 	    case R_M32C_RL_1ADDR:
   1705 	      /* xxxx xxxx xxxx dest [disp] */
   1706 
   1707 	      enctbl = m16c_addr_encodings;
   1708 	      posn = 2;
   1709 
   1710 	      /* Check the opcode for jumps.  We know it's safe to
   1711 		 do this because all 2ADDR insns are at least two
   1712 		 bytes long.  */
   1713 	      enc = insn[0] * 256 + insn[1];
   1714 	      enc &= 0xfff0;
   1715 	      if (enc == 0x7d20
   1716 		  || enc == 0x7d00
   1717 		  || enc == 0x7d30
   1718 		  || enc == 0x7d10)
   1719 		{
   1720 		  enctbl = m16c_jmpaddr_encodings;
   1721 		}
   1722 
   1723 	    try_1addr_16:
   1724 	      /* srel, posn, and enc must be set here.  */
   1725 
   1726 	      symval = OFFSET_FOR_RELOC (srel);
   1727 	      enc = insn[1] & 0x0f;
   1728 	      e = & enctbl[enc];
   1729 
   1730 	      if (srel->r_offset == irel->r_offset + posn
   1731 		  && e->new_encoding != enc
   1732 		  && symval <= e->max_disp)
   1733 		{
   1734 		  insn[1] &= 0xf0;
   1735 		  insn[1] |= e->new_encoding;
   1736 		  gap_size = e->bytes - enctbl[e->new_encoding].bytes;
   1737 		  gap = insn + posn + enctbl[e->new_encoding].bytes;
   1738 		  new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
   1739 		  break;
   1740 		}
   1741 
   1742 	      break;
   1743 
   1744 	    } /* Ends switch (reloc type) for m16c.  */
   1745 	}
   1746       else /* machine == bfd_mach_m32c */
   1747 	{
   1748 	  /* M32CM / M32C */
   1749 
   1750 	  switch (ELF32_R_TYPE(irel->r_info))
   1751 	    {
   1752 
   1753 	    case R_M32C_RL_JUMP:
   1754 	      switch (insn[0])
   1755 		{
   1756 		case 0xbb: /* jmp.b */
   1757 		  if (pcrel >= 2 && pcrel <= 9)
   1758 		    {
   1759 		      int p = pcrel - 2;
   1760 		      /* Relax JMP.B -> JMP.S.  We need to get rid of
   1761 			 the following reloc though. */
   1762 		      insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
   1763 		      new_type = R_M32C_NONE;
   1764 		      irel->r_addend = 0x10;
   1765 		      gap_size = 1;
   1766 		      gap = insn + 1;
   1767 		    }
   1768 		  break;
   1769 
   1770 		case 0xce: /* jmp.w */
   1771 		  if (pcrel <= 128 && pcrel >= -128)
   1772 		    {
   1773 		      /* Relax JMP.W -> JMP.B */
   1774 		      insn[0] = 0xbb;
   1775 		      insn[1] = 0;
   1776 		      new_type = R_M32C_8_PCREL;
   1777 		      gap_size = 1;
   1778 		      gap = insn + 2;
   1779 		    }
   1780 		  break;
   1781 
   1782 		case 0xcc: /* jmp.a */
   1783 		  if (pcrel <= 32768 && pcrel >= -32768)
   1784 		    {
   1785 		      /* Relax JMP.A -> JMP.W */
   1786 		      insn[0] = 0xce;
   1787 		      insn[1] = 0;
   1788 		      insn[2] = 0;
   1789 		      new_type = R_M32C_16_PCREL;
   1790 		      gap_size = 1;
   1791 		      gap = insn + 3;
   1792 		    }
   1793 		  break;
   1794 
   1795 		case 0xcd: /* jsr.a */
   1796 		  if (pcrel <= 32768 && pcrel >= -32768)
   1797 		    {
   1798 		      /* Relax JSR.A -> JSR.W */
   1799 		      insn[0] = 0xcf;
   1800 		      insn[1] = 0;
   1801 		      insn[2] = 0;
   1802 		      new_type = R_M32C_16_PCREL;
   1803 		      gap_size = 1;
   1804 		      gap = insn + 3;
   1805 		    }
   1806 		  break;
   1807 		}
   1808 	      break;
   1809 
   1810 	    case R_M32C_RL_2ADDR:
   1811 	      /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
   1812 
   1813 	      einsn = insn;
   1814 	      posn = 2;
   1815 	      if (einsn[0] == 1)
   1816 		{
   1817 		  /* prefix; remove it as far as the RL reloc is concerned.  */
   1818 		  einsn ++;
   1819 		  posn ++;
   1820 		}
   1821 
   1822 	      enctbl = m32c_addr_encodings;
   1823 	      enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
   1824 	      e = & enctbl[enc];
   1825 
   1826 	      if (srel->r_offset == irel->r_offset + posn
   1827 		  && e->new_encoding != enc
   1828 		  && symval <= e->max_disp)
   1829 		{
   1830 		  einsn[0] &= 0x8f;
   1831 		  einsn[0] |= (e->new_encoding & 0x1c) << 2;
   1832 		  einsn[1] &= 0xcf;
   1833 		  einsn[1] |= (e->new_encoding & 0x03) << 4;
   1834 		  gap_size = e->bytes - enctbl[e->new_encoding].bytes;
   1835 		  gap = insn + posn + enctbl[e->new_encoding].bytes;
   1836 		  new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
   1837 		  break;
   1838 		}
   1839 	      if (relax_relocs == 2)
   1840 		  srel ++;
   1841 	      posn += e->bytes;
   1842 
   1843 	      goto try_1addr_32;
   1844 
   1845 	    case R_M32C_RL_1ADDR:
   1846 	      /* xxxx DDDx DDxx xxxx [disp] */
   1847 
   1848 	      einsn = insn;
   1849 	      posn = 2;
   1850 	      if (einsn[0] == 1)
   1851 		{
   1852 		  /* prefix; remove it as far as the RL reloc is concerned.  */
   1853 		  einsn ++;
   1854 		  posn ++;
   1855 		}
   1856 
   1857 	      enctbl = m32c_addr_encodings;
   1858 
   1859 	    try_1addr_32:
   1860 	      /* srel, posn, and enc must be set here.  */
   1861 
   1862 	      symval = OFFSET_FOR_RELOC (srel);
   1863 	      enc = ((einsn[0] & 0x0e) << 1) |  ((einsn[1] & 0xc0) >> 6);
   1864 	      e = & enctbl[enc];
   1865 
   1866 	      if (srel->r_offset == irel->r_offset + posn
   1867 		  && e->new_encoding != enc
   1868 		  && symval <= e->max_disp)
   1869 		{
   1870 		  einsn[0] &= 0xf1;
   1871 		  einsn[0] |= (e->new_encoding & 0x1c) >> 1;
   1872 		  einsn[1] &= 0x3f;
   1873 		  einsn[1] |= (e->new_encoding & 0x03) << 6;
   1874 		  gap_size = e->bytes - enctbl[e->new_encoding].bytes;
   1875 		  gap = insn + posn + enctbl[e->new_encoding].bytes;
   1876 		  new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
   1877 		  break;
   1878 		}
   1879 
   1880 	      break;
   1881 
   1882 	    } /* Ends switch (reloc type) for m32c.  */
   1883 	}
   1884 
   1885       if (gap_size == 0)
   1886 	continue;
   1887 
   1888       *again = TRUE;
   1889 
   1890       srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
   1891 
   1892       /* Note that we've changed the relocs, section contents, etc.  */
   1893       elf_section_data (sec)->relocs = internal_relocs;
   1894       free_relocs = NULL;
   1895 
   1896       elf_section_data (sec)->this_hdr.contents = contents;
   1897       free_contents = NULL;
   1898 
   1899       symtab_hdr->contents = (bfd_byte *) intsyms;
   1900       free_intsyms = NULL;
   1901 
   1902       bytes_saved += gap_size;
   1903 
   1904       if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
   1905 	goto error_return;
   1906 
   1907     } /* next relocation */
   1908 
   1909   if (free_relocs != NULL)
   1910     {
   1911       free (free_relocs);
   1912       free_relocs = NULL;
   1913     }
   1914 
   1915   if (free_contents != NULL)
   1916     {
   1917       if (! link_info->keep_memory)
   1918 	free (free_contents);
   1919       /* Cache the section contents for elf_link_input_bfd.  */
   1920       else
   1921 	elf_section_data (sec)->this_hdr.contents = contents;
   1922 
   1923       free_contents = NULL;
   1924     }
   1925 
   1926   if (shndx_buf != NULL)
   1927     {
   1928       shndx_hdr->contents = NULL;
   1929       free (shndx_buf);
   1930     }
   1931 
   1932   if (free_intsyms != NULL)
   1933     {
   1934       if (! link_info->keep_memory)
   1935 	free (free_intsyms);
   1936       /* Cache the symbols for elf_link_input_bfd.  */
   1937       else
   1938 	{
   1939 	symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
   1940 	}
   1941 
   1942       free_intsyms = NULL;
   1943     }
   1944 
   1945   return TRUE;
   1946 
   1947  error_return:
   1948   if (free_relocs != NULL)
   1949     free (free_relocs);
   1950   if (free_contents != NULL)
   1951     free (free_contents);
   1952   if (shndx_buf != NULL)
   1953     {
   1954       shndx_hdr->contents = NULL;
   1955       free (shndx_buf);
   1956     }
   1957   if (free_intsyms != NULL)
   1958     free (free_intsyms);
   1959   return FALSE;
   1960 }
   1961 
   1962 /* Delete some bytes from a section while relaxing.  */
   1963 
   1964 static bfd_boolean
   1965 m32c_elf_relax_delete_bytes
   1966  (bfd *      abfd,
   1967   asection * sec,
   1968   bfd_vma    addr,
   1969   int        count)
   1970 {
   1971   Elf_Internal_Shdr *symtab_hdr;
   1972   Elf_Internal_Shdr *shndx_hdr;
   1973   int sec_shndx;
   1974   bfd_byte *contents;
   1975   Elf_Internal_Rela *irel;
   1976   Elf_Internal_Rela *irelend;
   1977   bfd_vma toaddr;
   1978   Elf_Internal_Sym *isym;
   1979   Elf_Internal_Sym *isymend;
   1980   Elf_Internal_Sym *intsyms;
   1981   Elf_External_Sym_Shndx *shndx_buf;
   1982   Elf_External_Sym_Shndx *shndx;
   1983   struct elf_link_hash_entry ** sym_hashes;
   1984   struct elf_link_hash_entry ** end_hashes;
   1985   unsigned int                  symcount;
   1986 
   1987   contents   = elf_section_data (sec)->this_hdr.contents;
   1988 
   1989   toaddr = sec->size;
   1990 
   1991   irel = elf_section_data (sec)->relocs;
   1992   irelend = irel + sec->reloc_count;
   1993 
   1994   /* Actually delete the bytes.  */
   1995   memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
   1996   sec->size -= count;
   1997 
   1998   /* Adjust all the relocs.  */
   1999   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
   2000     {
   2001       /* Get the new reloc address.  */
   2002       if (irel->r_offset > addr && irel->r_offset < toaddr)
   2003 	irel->r_offset -= count;
   2004 
   2005       if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
   2006 	  && irel->r_addend == 0x10 /* one byte insn, no relocs */
   2007 	  && irel->r_offset + 1 < addr
   2008 	  && irel->r_offset + 7 > addr)
   2009 	{
   2010 	  bfd_vma disp;
   2011 	  unsigned char *insn = &contents[irel->r_offset];
   2012 	  disp = *insn;
   2013 	  /* This is a JMP.S, which we have to manually update. */
   2014 	  if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
   2015 	    {
   2016 	      if ((*insn & 0xf8) != 0x60)
   2017 		continue;
   2018 	      disp = (disp & 7);
   2019 	    }
   2020 	  else
   2021 	    {
   2022 	      if ((*insn & 0xce) != 0x4a)
   2023 		continue;
   2024 	      disp = ((disp & 0x30) >> 3) | (disp & 1);
   2025 	    }
   2026 	  if (irel->r_offset + disp + 2 >= addr+count)
   2027 	    {
   2028 	      disp -= count;
   2029 	      if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
   2030 		{
   2031 		  *insn = (*insn & 0xf8) | disp;
   2032 		}
   2033 	      else
   2034 		{
   2035 		  *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
   2036 		}
   2037 	    }
   2038 	}
   2039     }
   2040 
   2041   /* Adjust the local symbols defined in this section.  */
   2042   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
   2043   intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   2044   isym = intsyms;
   2045   isymend = isym + symtab_hdr->sh_info;
   2046 
   2047   sec_shndx  = _bfd_elf_section_from_bfd_section (abfd, sec);
   2048   if (elf_symtab_shndx_list (abfd))
   2049     {
   2050       shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   2051       shndx_buf  = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
   2052     }
   2053   else
   2054     {
   2055       shndx_hdr = NULL;
   2056       shndx_buf = NULL;
   2057     }
   2058   shndx = shndx_buf;
   2059 
   2060   for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
   2061     {
   2062       /* If the symbol is in the range of memory we just moved, we
   2063 	 have to adjust its value.  */
   2064       if ((int) isym->st_shndx == sec_shndx
   2065 	  && isym->st_value > addr
   2066 	  && isym->st_value < toaddr)
   2067 	{
   2068 	  isym->st_value -= count;
   2069 	}
   2070       /* If the symbol *spans* the bytes we just deleted (i.e. it's
   2071 	 *end* is in the moved bytes but it's *start* isn't), then we
   2072 	 must adjust its size.  */
   2073       if ((int) isym->st_shndx == sec_shndx
   2074 	    && isym->st_value < addr
   2075 	  && isym->st_value + isym->st_size > addr
   2076 	  && isym->st_value + isym->st_size < toaddr)
   2077 	{
   2078 	  isym->st_size -= count;
   2079 	}
   2080     }
   2081 
   2082   /* Now adjust the global symbols defined in this section.  */
   2083   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   2084 	      - symtab_hdr->sh_info);
   2085   sym_hashes = elf_sym_hashes (abfd);
   2086   //  sym_hashes += symtab_hdr->sh_info;
   2087   end_hashes = sym_hashes + symcount;
   2088 
   2089   for (; sym_hashes < end_hashes; sym_hashes ++)
   2090     {
   2091       struct elf_link_hash_entry * sym_hash = * sym_hashes;
   2092 
   2093       if (sym_hash &&
   2094 	  (sym_hash->root.type == bfd_link_hash_defined
   2095 	   || sym_hash->root.type == bfd_link_hash_defweak)
   2096 	  && sym_hash->root.u.def.section == sec)
   2097 	{
   2098 	  if (sym_hash->root.u.def.value > addr
   2099 	      && sym_hash->root.u.def.value < toaddr)
   2100 	    {
   2101 	      sym_hash->root.u.def.value -= count;
   2102 	    }
   2103 	  if (sym_hash->root.u.def.value < addr
   2104 	      && sym_hash->root.u.def.value + sym_hash->size > addr
   2105 	      && sym_hash->root.u.def.value + sym_hash->size < toaddr)
   2106 	    {
   2107 	      sym_hash->size -= count;
   2108 	    }
   2109 	}
   2110     }
   2111 
   2112   return TRUE;
   2113 }
   2114 
   2115 /* This is for versions of gcc prior to 4.3.  */
   2117 static unsigned int
   2118 _bfd_m32c_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
   2119 {
   2120   if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C)
   2121     return 2;
   2122   return 4;
   2123 }
   2124 
   2125 
   2126 
   2128 #define ELF_ARCH		bfd_arch_m32c
   2129 #define ELF_MACHINE_CODE	EM_M32C
   2130 #define ELF_MACHINE_ALT1	EM_M32C_OLD
   2131 #define ELF_MAXPAGESIZE		0x100
   2132 
   2133 #if 0
   2134 #define TARGET_BIG_SYM		m32c_elf32_vec
   2135 #define TARGET_BIG_NAME		"elf32-m32c"
   2136 #else
   2137 #define TARGET_LITTLE_SYM		m32c_elf32_vec
   2138 #define TARGET_LITTLE_NAME		"elf32-m32c"
   2139 #endif
   2140 
   2141 #define elf_info_to_howto_rel			NULL
   2142 #define elf_info_to_howto			m32c_info_to_howto_rela
   2143 #define elf_backend_object_p			m32c_elf_object_p
   2144 #define elf_backend_relocate_section		m32c_elf_relocate_section
   2145 #define elf_backend_check_relocs                m32c_elf_check_relocs
   2146 #define elf_backend_object_p			m32c_elf_object_p
   2147 #define elf_symbol_leading_char                 ('_')
   2148 #define elf_backend_always_size_sections \
   2149   m32c_elf_always_size_sections
   2150 #define elf_backend_finish_dynamic_sections \
   2151   m32c_elf_finish_dynamic_sections
   2152 
   2153 #define elf_backend_can_gc_sections		1
   2154 #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
   2155 
   2156 #define bfd_elf32_bfd_reloc_type_lookup		m32c_reloc_type_lookup
   2157 #define bfd_elf32_bfd_reloc_name_lookup	m32c_reloc_name_lookup
   2158 #define bfd_elf32_bfd_relax_section		m32c_elf_relax_section
   2159 #define bfd_elf32_bfd_set_private_flags		m32c_elf_set_private_flags
   2160 #define bfd_elf32_bfd_merge_private_bfd_data	m32c_elf_merge_private_bfd_data
   2161 #define bfd_elf32_bfd_print_private_bfd_data	m32c_elf_print_private_bfd_data
   2162 
   2163 #include "elf32-target.h"
   2164