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