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