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