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