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