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