Let ELF header now define the macros to get the enum.
[platform/upstream/binutils.git] / bfd / elf-m10300.c
1 /* Matsushita 10300 specific support for 32-bit ELF
2    Copyright (C) 1996, 1997, 1998 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 2 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 "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24 #include "elf/mn10300.h"
25
26 struct elf32_mn10300_link_hash_entry
27 {
28   /* The basic elf link hash table entry.  */
29   struct elf_link_hash_entry root;
30
31   /* For function symbols, the number of times this function is
32      called directly (ie by name).  */
33   unsigned int direct_calls;
34
35   /* For function symbols, the size of this function's stack
36      (if <= 255 bytes).  We stuff this into "call" instructions
37      to this target when it's valid and profitable to do so.  */
38   unsigned char stack_size;
39
40   /* For function symbols, arguments (if any) for movm instruction
41      in the prologue.  We stuff this value into "call" instructions
42      to the target when it's valid and profitable to do so.  */
43   unsigned char movm_args;
44
45 /* When set, convert all "call" instructions to this target into "calls"
46    instructions.  */
47 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
48
49 /* Used to mark functions which have had redundant parts of their
50    prologue deleted.  */
51 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
52   unsigned char flags;
53 };
54
55 /* We derive a hash table from the main elf linker hash table so
56    we can store state variables and a secondary hash table without
57    resorting to global variables.  */
58 struct elf32_mn10300_link_hash_table
59 {
60   /* The main hash table.  */
61   struct elf_link_hash_table root;
62
63   /* A hash table for static functions.  We could derive a new hash table
64      instead of using the full elf32_mn10300_link_hash_table if we wanted
65      to save some memory.  */
66   struct elf32_mn10300_link_hash_table *static_hash_table;
67
68   /* Random linker state flags.  */
69 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
70   char flags;
71 };
72
73 /* For MN10300 linker hash table.  */
74
75 /* Get the MN10300 ELF linker hash table from a link_info structure.  */
76
77 #define elf32_mn10300_hash_table(p) \
78   ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
79
80 #define elf32_mn10300_link_hash_traverse(table, func, info)             \
81   (elf_link_hash_traverse                                               \
82    (&(table)->root,                                                     \
83     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
84     (info)))
85
86 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
87   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
88 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
89   PARAMS ((bfd *));
90
91 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
92   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
93 static void mn10300_info_to_howto
94   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
95 static boolean mn10300_elf_check_relocs
96   PARAMS ((bfd *, struct bfd_link_info *, asection *,
97            const Elf_Internal_Rela *));
98 static asection *mn10300_elf_gc_mark_hook
99   PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *,
100            struct elf_link_hash_entry *, Elf_Internal_Sym *));
101 static boolean mn10300_elf_relax_delete_bytes
102   PARAMS ((bfd *, asection *, bfd_vma, int));
103 static boolean mn10300_elf_symbol_address_p
104   PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
105 static boolean elf32_mn10300_finish_hash_table_entry
106   PARAMS ((struct bfd_hash_entry *, PTR));
107 static void compute_function_info
108   PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
109            bfd_vma, unsigned char *));
110
111 /* We have to use RELA instructions since md_apply_fix3 in the assembler
112    does absolutely nothing.  */
113 #define USE_RELA
114
115
116 static reloc_howto_type elf_mn10300_howto_table[] =
117 {
118   /* Dummy relocation.  Does nothing.  */
119   HOWTO (R_MN10300_NONE,
120          0,
121          2,
122          16,
123          false,
124          0,
125          complain_overflow_bitfield,
126          bfd_elf_generic_reloc,
127          "R_MN10300_NONE",
128          false,
129          0,
130          0,
131          false),
132   /* Standard 32 bit reloc.  */
133   HOWTO (R_MN10300_32,
134          0,
135          2,
136          32,
137          false,
138          0,
139          complain_overflow_bitfield,
140          bfd_elf_generic_reloc,
141          "R_MN10300_32",
142          false,
143          0xffffffff,
144          0xffffffff,
145          false),
146   /* Standard 16 bit reloc.  */
147   HOWTO (R_MN10300_16,
148          0,
149          1,
150          16,
151          false,
152          0,
153          complain_overflow_bitfield,
154          bfd_elf_generic_reloc,
155          "R_MN10300_16",
156          false,
157          0xffff,
158          0xffff,
159          false),
160   /* Standard 8 bit reloc.  */
161   HOWTO (R_MN10300_8,
162          0,
163          0,
164          8,
165          false,
166          0,
167          complain_overflow_bitfield,
168          bfd_elf_generic_reloc,
169          "R_MN10300_8",
170          false,
171          0xff,
172          0xff,
173          false),
174   /* Standard 32bit pc-relative reloc.  */
175   HOWTO (R_MN10300_PCREL32,
176          0,
177          2,
178          32,
179          true,
180          0,
181          complain_overflow_bitfield,
182          bfd_elf_generic_reloc,
183          "R_MN10300_PCREL32",
184          false,
185          0xffffffff,
186          0xffffffff,
187          true),
188   /* Standard 16bit pc-relative reloc.  */
189   HOWTO (R_MN10300_PCREL16,
190          0,
191          1,
192          16,
193          true,
194          0,
195          complain_overflow_bitfield,
196          bfd_elf_generic_reloc,
197          "R_MN10300_PCREL16",
198          false,
199          0xffff,
200          0xffff,
201          true),
202   /* Standard 8 pc-relative reloc.  */
203   HOWTO (R_MN10300_PCREL8,
204          0,
205          0,
206          8,
207          true,
208          0,
209          complain_overflow_bitfield,
210          bfd_elf_generic_reloc,
211          "R_MN10300_PCREL8",
212          false,
213          0xff,
214          0xff,
215          true),
216
217   /* GNU extension to record C++ vtable hierarchy */
218   HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
219          0,                     /* rightshift */
220          0,                     /* size (0 = byte, 1 = short, 2 = long) */
221          0,                     /* bitsize */
222          false,                 /* pc_relative */
223          0,                     /* bitpos */
224          complain_overflow_dont, /* complain_on_overflow */
225          NULL,                  /* special_function */
226          "R_MN10300_GNU_VTINHERIT", /* name */
227          false,                 /* partial_inplace */
228          0,                     /* src_mask */
229          0,                     /* dst_mask */
230          false),                /* pcrel_offset */
231
232   /* GNU extension to record C++ vtable member usage */
233   HOWTO (R_MN10300_GNU_VTENTRY, /* type */
234          0,                     /* rightshift */
235          0,                     /* size (0 = byte, 1 = short, 2 = long) */
236          0,                     /* bitsize */
237          false,                 /* pc_relative */
238          0,                     /* bitpos */
239          complain_overflow_dont, /* complain_on_overflow */
240          NULL,                  /* special_function */
241          "R_MN10300_GNU_VTENTRY", /* name */
242          false,                 /* partial_inplace */
243          0,                     /* src_mask */
244          0,                     /* dst_mask */
245          false),                /* pcrel_offset */
246 };
247
248 struct mn10300_reloc_map
249 {
250   unsigned char bfd_reloc_val;
251   unsigned char elf_reloc_val;
252 };
253
254 static const struct mn10300_reloc_map mn10300_reloc_map[] =
255 {
256   { BFD_RELOC_NONE, R_MN10300_NONE, },
257   { BFD_RELOC_32, R_MN10300_32, },
258   { BFD_RELOC_16, R_MN10300_16, },
259   { BFD_RELOC_8, R_MN10300_8, },
260   { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
261   { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
262   { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
263   { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
264   { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
265 };
266
267 static reloc_howto_type *
268 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
269      bfd *abfd;
270      bfd_reloc_code_real_type code;
271 {
272   unsigned int i;
273
274   for (i = 0;
275        i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
276        i++)
277     {
278       if (mn10300_reloc_map[i].bfd_reloc_val == code)
279         return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
280     }
281
282   return NULL;
283 }
284
285 /* Set the howto pointer for an MN10300 ELF reloc.  */
286
287 static void
288 mn10300_info_to_howto (abfd, cache_ptr, dst)
289      bfd *abfd;
290      arelent *cache_ptr;
291      Elf32_Internal_Rela *dst;
292 {
293   unsigned int r_type;
294
295   r_type = ELF32_R_TYPE (dst->r_info);
296   BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
297   cache_ptr->howto = &elf_mn10300_howto_table[r_type];
298 }
299
300 /* Look through the relocs for a section during the first phase.
301    Since we don't do .gots or .plts, we just need to consider the
302    virtual table relocs for gc.  */
303
304 static boolean
305 mn10300_elf_check_relocs (abfd, info, sec, relocs)
306      bfd *abfd;
307      struct bfd_link_info *info;
308      asection *sec;
309      const Elf_Internal_Rela *relocs;
310 {
311   Elf_Internal_Shdr *symtab_hdr;
312   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
313   const Elf_Internal_Rela *rel;
314   const Elf_Internal_Rela *rel_end;
315
316   if (info->relocateable)
317     return true;
318
319   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
320   sym_hashes = elf_sym_hashes (abfd);
321   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
322   if (!elf_bad_symtab (abfd))
323     sym_hashes_end -= symtab_hdr->sh_info;
324
325   rel_end = relocs + sec->reloc_count;
326   for (rel = relocs; rel < rel_end; rel++)
327     {
328       struct elf_link_hash_entry *h;
329       unsigned long r_symndx;
330
331       r_symndx = ELF32_R_SYM (rel->r_info);
332       if (r_symndx < symtab_hdr->sh_info)
333         h = NULL;
334       else
335         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
336
337       switch (ELF32_R_TYPE (rel->r_info))
338         {
339         /* This relocation describes the C++ object vtable hierarchy.
340            Reconstruct it for later use during GC.  */
341         case R_MN10300_GNU_VTINHERIT:
342           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
343             return false;
344           break;
345
346         /* This relocation describes which C++ vtable entries are actually
347            used.  Record for later use during GC.  */
348         case R_MN10300_GNU_VTENTRY:
349           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
350             return false;
351           break;
352         }
353     }
354
355   return true;
356 }
357
358 /* Return the section that should be marked against GC for a given
359    relocation.  */
360
361 static asection *
362 mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym)
363      bfd *abfd;
364      struct bfd_link_info *info;
365      Elf_Internal_Rela *rel;
366      struct elf_link_hash_entry *h;
367      Elf_Internal_Sym *sym;
368 {
369   if (h != NULL)
370     {
371       switch (ELF32_R_TYPE (rel->r_info))
372         {
373         case R_MN10300_GNU_VTINHERIT:
374         case R_MN10300_GNU_VTENTRY:
375           break;
376
377         default:
378           switch (h->root.type)
379             {
380             case bfd_link_hash_defined:
381             case bfd_link_hash_defweak:
382               return h->root.u.def.section;
383
384             case bfd_link_hash_common:
385               return h->root.u.c.p->section;
386             }
387         }
388     }
389   else
390     {
391       if (!(elf_bad_symtab (abfd)
392             && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
393           && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
394                 && sym->st_shndx != SHN_COMMON))
395         {
396           return bfd_section_from_elf_index (abfd, sym->st_shndx);
397         }
398     }
399
400   return NULL;
401 }
402
403 /* Perform a relocation as part of a final link.  */
404 static bfd_reloc_status_type
405 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
406                                  input_section, contents, offset, value,
407                                  addend, info, sym_sec, is_local)
408      reloc_howto_type *howto;
409      bfd *input_bfd;
410      bfd *output_bfd;
411      asection *input_section;
412      bfd_byte *contents;
413      bfd_vma offset;
414      bfd_vma value;
415      bfd_vma addend;
416      struct bfd_link_info *info;
417      asection *sym_sec;
418      int is_local;
419 {
420   unsigned long r_type = howto->type;
421   bfd_byte *hit_data = contents + offset;
422
423   switch (r_type)
424     {
425     case R_MN10300_NONE:
426       return bfd_reloc_ok;
427
428     case R_MN10300_32:
429       value += addend;
430       bfd_put_32 (input_bfd, value, hit_data);
431       return bfd_reloc_ok;
432
433     case R_MN10300_16:
434       value += addend;
435
436       if ((long)value > 0x7fff || (long)value < -0x8000)
437         return bfd_reloc_overflow;
438
439       bfd_put_16 (input_bfd, value, hit_data);
440       return bfd_reloc_ok;
441
442     case R_MN10300_8:
443       value += addend;
444
445       if ((long)value > 0x7f || (long)value < -0x80)
446         return bfd_reloc_overflow;
447
448       bfd_put_8 (input_bfd, value, hit_data);
449       return bfd_reloc_ok;
450
451     case R_MN10300_PCREL8:
452       value -= (input_section->output_section->vma
453                 + input_section->output_offset);
454       value -= offset;
455       value += addend;
456
457       if ((long)value > 0xff || (long)value < -0x100)
458         return bfd_reloc_overflow;
459
460       bfd_put_8 (input_bfd, value, hit_data);
461       return bfd_reloc_ok;
462
463     case R_MN10300_PCREL16:
464       value -= (input_section->output_section->vma
465                 + input_section->output_offset);
466       value -= offset;
467       value += addend;
468
469       if ((long)value > 0xffff || (long)value < -0x10000)
470         return bfd_reloc_overflow;
471
472       bfd_put_16 (input_bfd, value, hit_data);
473       return bfd_reloc_ok;
474
475     case R_MN10300_PCREL32:
476       value -= (input_section->output_section->vma
477                 + input_section->output_offset);
478       value -= offset;
479       value += addend;
480
481       bfd_put_32 (input_bfd, value, hit_data);
482       return bfd_reloc_ok;
483
484     case R_MN10300_GNU_VTINHERIT:
485     case R_MN10300_GNU_VTENTRY:
486       return bfd_reloc_ok;
487
488     default:
489       return bfd_reloc_notsupported;
490     }
491 }
492
493 \f
494 /* Relocate an MN10300 ELF section.  */
495 static boolean
496 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
497                               contents, relocs, local_syms, local_sections)
498      bfd *output_bfd;
499      struct bfd_link_info *info;
500      bfd *input_bfd;
501      asection *input_section;
502      bfd_byte *contents;
503      Elf_Internal_Rela *relocs;
504      Elf_Internal_Sym *local_syms;
505      asection **local_sections;
506 {
507   Elf_Internal_Shdr *symtab_hdr;
508   struct elf32_mn10300_link_hash_entry **sym_hashes;
509   Elf_Internal_Rela *rel, *relend;
510
511   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
512   sym_hashes = (struct elf32_mn10300_link_hash_entry **)
513                  (elf_sym_hashes (input_bfd));
514
515   rel = relocs;
516   relend = relocs + input_section->reloc_count;
517   for (; rel < relend; rel++)
518     {
519       int r_type;
520       reloc_howto_type *howto;
521       unsigned long r_symndx;
522       Elf_Internal_Sym *sym;
523       asection *sec;
524       struct elf32_mn10300_link_hash_entry *h;
525       bfd_vma relocation;
526       bfd_reloc_status_type r;
527
528       r_symndx = ELF32_R_SYM (rel->r_info);
529       r_type = ELF32_R_TYPE (rel->r_info);
530       howto = elf_mn10300_howto_table + r_type;
531
532       /* Just skip the vtable gc relocs.  */
533       if (r_type == R_MN10300_GNU_VTINHERIT
534           || r_type == R_MN10300_GNU_VTENTRY)
535         continue;
536
537       if (info->relocateable)
538         {
539           /* This is a relocateable link.  We don't have to change
540              anything, unless the reloc is against a section symbol,
541              in which case we have to adjust according to where the
542              section symbol winds up in the output section.  */
543           if (r_symndx < symtab_hdr->sh_info)
544             {
545               sym = local_syms + r_symndx;
546               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
547                 {
548                   sec = local_sections[r_symndx];
549                   rel->r_addend += sec->output_offset + sym->st_value;
550                 }
551             }
552
553           continue;
554         }
555
556       /* This is a final link.  */
557       h = NULL;
558       sym = NULL;
559       sec = NULL;
560       if (r_symndx < symtab_hdr->sh_info)
561         {
562           sym = local_syms + r_symndx;
563           sec = local_sections[r_symndx];
564           relocation = (sec->output_section->vma
565                         + sec->output_offset
566                         + sym->st_value);
567         }
568       else
569         {
570           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
571           while (h->root.type == bfd_link_hash_indirect
572                  || h->root.type == bfd_link_hash_warning)
573             h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
574           if (h->root.root.type == bfd_link_hash_defined
575               || h->root.root.type == bfd_link_hash_defweak)
576             {
577               sec = h->root.root.u.def.section;
578               relocation = (h->root.root.u.def.value
579                             + sec->output_section->vma
580                             + sec->output_offset);
581             }
582           else if (h->root.root.type == bfd_link_hash_undefweak)
583             relocation = 0;
584           else
585             {
586               if (! ((*info->callbacks->undefined_symbol)
587                      (info, h->root.root.root.string, input_bfd,
588                       input_section, rel->r_offset)))
589                 return false;
590               relocation = 0;
591             }
592         }
593
594       r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
595                                            input_section,
596                                            contents, rel->r_offset,
597                                            relocation, rel->r_addend,
598                                            info, sec, h == NULL);
599
600       if (r != bfd_reloc_ok)
601         {
602           const char *name;
603           const char *msg = (const char *)0;
604
605           if (h != NULL)
606             name = h->root.root.root.string;
607           else
608             {
609               name = (bfd_elf_string_from_elf_section
610                       (input_bfd, symtab_hdr->sh_link, sym->st_name));
611               if (name == NULL || *name == '\0')
612                 name = bfd_section_name (input_bfd, sec);
613             }
614
615           switch (r)
616             {
617             case bfd_reloc_overflow:
618               if (! ((*info->callbacks->reloc_overflow)
619                      (info, name, howto->name, (bfd_vma) 0,
620                       input_bfd, input_section, rel->r_offset)))
621                 return false;
622               break;
623
624             case bfd_reloc_undefined:
625               if (! ((*info->callbacks->undefined_symbol)
626                      (info, name, input_bfd, input_section,
627                       rel->r_offset)))
628                 return false;
629               break;
630
631             case bfd_reloc_outofrange:
632               msg = _("internal error: out of range error");
633               goto common_error;
634
635             case bfd_reloc_notsupported:
636               msg = _("internal error: unsupported relocation error");
637               goto common_error;
638
639             case bfd_reloc_dangerous:
640               msg = _("internal error: dangerous error");
641               goto common_error;
642
643             default:
644               msg = _("internal error: unknown error");
645               /* fall through */
646
647             common_error:
648               if (!((*info->callbacks->warning)
649                     (info, msg, name, input_bfd, input_section,
650                      rel->r_offset)))
651                 return false;
652               break;
653             }
654         }
655     }
656
657   return true;
658 }
659
660 /* Finish initializing one hash table entry.  */
661 static boolean
662 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
663      struct bfd_hash_entry *gen_entry;
664      PTR in_args;
665 {
666   struct elf32_mn10300_link_hash_entry *entry;
667   unsigned int byte_count = 0;
668
669   entry = (struct elf32_mn10300_link_hash_entry *)gen_entry;
670
671   /* If we already know we want to convert "call" to "calls" for calls
672      to this symbol, then return now.  */
673   if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
674     return true;
675
676   /* If there are no named calls to this symbol, or there's nothing we
677      can move from the function itself into the "call" instruction, then
678      note that all "call" instructions should be converted into "calls"
679      instructions and return.  */
680   if (entry->direct_calls == 0
681       || (entry->stack_size == 0 && entry->movm_args == 0))
682     {
683       /* Make a note that we should convert "call" instructions to "calls"
684          instructions for calls to this symbol.  */
685       entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
686       return true;
687     }
688
689   /* We may be able to move some instructions from the function itself into
690      the "call" instruction.  Count how many bytes we might be able to
691      eliminate in the function itself.  */
692
693   /* A movm instruction is two bytes.  */
694   if (entry->movm_args)
695     byte_count += 2;
696
697   /* Count the insn to allocate stack space too.  */
698   if (entry->stack_size > 0 && entry->stack_size <= 128)
699     byte_count += 3;
700   else if (entry->stack_size > 0 && entry->stack_size < 256)
701     byte_count += 4;
702
703   /* If using "call" will result in larger code, then turn all
704      the associated "call" instructions into "calls" instrutions.  */
705   if (byte_count < entry->direct_calls)
706     entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
707
708   /* This routine never fails.  */
709   return true;
710 }
711
712 /* This function handles relaxing for the mn10300.
713
714    There's quite a few relaxing opportunites available on the mn10300:
715
716         * calls:32 -> calls:16                                     2 bytes
717         * call:32  -> call:16                                      2 bytes
718
719         * call:32 -> calls:32                                      1 byte
720         * call:16 -> calls:16                                      1 byte
721                 * These are done anytime using "calls" would result
722                 in smaller code, or when necessary to preserve the
723                 meaning of the program.
724
725         * call:32                                                  varies
726         * call:16
727                 * In some circumstances we can move instructions
728                 from a function prologue into a "call" instruction.
729                 This is only done if the resulting code is no larger
730                 than the original code.
731
732
733         * jmp:32 -> jmp:16                                         2 bytes
734         * jmp:16 -> bra:8                                          1 byte
735
736                 * If the previous instruction is a conditional branch
737                 around the jump/bra, we may be able to reverse its condition
738                 and change its target to the jump's target.  The jump/bra
739                 can then be deleted.                               2 bytes
740
741         * mov abs32 -> mov abs16                                   1 or 2 bytes
742
743         * Most instructions which accept imm32 can relax to imm16  1 or 2 bytes
744         - Most instructions which accept imm16 can relax to imm8   1 or 2 bytes
745
746         * Most instructions which accept d32 can relax to d16      1 or 2 bytes
747         - Most instructions which accept d16 can relax to d8       1 or 2 bytes
748
749         We don't handle imm16->imm8 or d16->d8 as they're very rare
750         and somewhat more difficult to support.  */
751
752 static boolean
753 mn10300_elf_relax_section (abfd, sec, link_info, again)
754      bfd *abfd;
755      asection *sec;
756      struct bfd_link_info *link_info;
757      boolean *again;
758 {
759   Elf_Internal_Shdr *symtab_hdr;
760   Elf_Internal_Rela *internal_relocs = NULL;
761   Elf_Internal_Rela *free_relocs = NULL;
762   Elf_Internal_Rela *irel, *irelend;
763   bfd_byte *contents = NULL;
764   bfd_byte *free_contents = NULL;
765   Elf32_External_Sym *extsyms = NULL;
766   Elf32_External_Sym *free_extsyms = NULL;
767   struct elf32_mn10300_link_hash_table *hash_table;
768
769   /* Assume nothing changes.  */
770   *again = false;
771
772   /* We need a pointer to the mn10300 specific hash table.  */
773   hash_table = elf32_mn10300_hash_table (link_info);
774
775   /* Initialize fields in each hash table entry the first time through.  */
776   if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
777     {
778       bfd *input_bfd;
779
780       /* Iterate over all the input bfds.  */
781       for (input_bfd = link_info->input_bfds;
782            input_bfd != NULL;
783            input_bfd = input_bfd->link_next)
784         {
785           asection *section;
786
787           /* We're going to need all the symbols for each bfd.  */
788           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
789
790           /* Get cached copy if it exists.  */
791           if (symtab_hdr->contents != NULL)
792             extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
793           else
794             {
795               /* Go get them off disk.  */
796               extsyms = ((Elf32_External_Sym *)
797                          bfd_malloc (symtab_hdr->sh_size));
798               if (extsyms == NULL)
799                 goto error_return;
800               free_extsyms = extsyms;
801               if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
802                   || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
803                       != symtab_hdr->sh_size))
804                 goto error_return;
805             }
806
807           /* Iterate over each section in this bfd.  */
808           for (section = input_bfd->sections;
809                section != NULL;
810                section = section->next)
811             {
812               struct elf32_mn10300_link_hash_entry *hash;
813               Elf_Internal_Sym *sym;
814               asection *sym_sec;
815               const char *sym_name;
816               char *new_name;
817               Elf_Internal_Shdr *hdr;
818
819               /* Get cached copy of section contents if it exists.  */
820               if (elf_section_data (section)->this_hdr.contents != NULL)
821                 contents = elf_section_data (section)->this_hdr.contents;
822               else if (section->_raw_size != 0)
823                 {
824                   /* Go get them off disk.  */
825                   contents = (bfd_byte *)bfd_malloc (section->_raw_size);
826                   if (contents == NULL)
827                     goto error_return;
828                   free_contents = contents;
829
830                   if (!bfd_get_section_contents (input_bfd, section,
831                                                  contents, (file_ptr) 0,
832                                                  section->_raw_size))
833                     goto error_return;
834                 }
835               else
836                 {
837                   contents = NULL;
838                   free_contents = NULL;
839                 }
840
841               /* If there aren't any relocs, then there's nothing to do.  */
842               if ((section->flags & SEC_RELOC) != 0
843                   && section->reloc_count != 0)
844                 {
845
846                   /* Get a copy of the native relocations.  */
847                   internal_relocs = (_bfd_elf32_link_read_relocs
848                                      (input_bfd, section, (PTR) NULL,
849                                       (Elf_Internal_Rela *) NULL,
850                                       link_info->keep_memory));
851                   if (internal_relocs == NULL)
852                     goto error_return;
853                   if (! link_info->keep_memory)
854                     free_relocs = internal_relocs;
855
856                   /* Now examine each relocation.  */
857                   irel = internal_relocs;
858                   irelend = irel + section->reloc_count;
859                   for (; irel < irelend; irel++)
860                     {
861                       long r_type;
862                       unsigned long r_index;
863                       unsigned char code;
864
865                       r_type = ELF32_R_TYPE (irel->r_info);
866                       r_index = ELF32_R_SYM (irel->r_info);
867
868                       if (r_type < 0 || r_type >= (int)R_MN10300_MAX)
869                         goto error_return;
870
871                       /* We need the name and hash table entry of the target
872                          symbol!  */
873                       hash = NULL;
874                       sym = NULL;
875                       sym_sec = NULL;
876
877                       if (r_index < symtab_hdr->sh_info)
878                         {
879                           /* A local symbol.  */
880                           Elf_Internal_Sym isym;
881
882                           bfd_elf32_swap_symbol_in (input_bfd,
883                                                     extsyms + r_index, &isym);
884
885                           if (isym.st_shndx == SHN_UNDEF)
886                             sym_sec = bfd_und_section_ptr;
887                           else if (isym.st_shndx > 0
888                                    && isym.st_shndx < SHN_LORESERVE)
889                             sym_sec
890                               = bfd_section_from_elf_index (input_bfd,
891                                                             isym.st_shndx);
892                           else if (isym.st_shndx == SHN_ABS)
893                             sym_sec = bfd_abs_section_ptr;
894                           else if (isym.st_shndx == SHN_COMMON)
895                             sym_sec = bfd_com_section_ptr;
896
897                           sym_name = bfd_elf_string_from_elf_section (input_bfd,
898                                                            symtab_hdr->sh_link,
899                                                            isym.st_name);
900
901                           /* If it isn't a function, then we don't care
902                              about it.  */
903                           if (r_index < symtab_hdr->sh_info
904                               && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
905                             continue;
906
907                           /* Tack on an ID so we can uniquely identify this
908                              local symbol in the global hash table.  */
909                           new_name = alloca (strlen (sym_name) + 10);
910                           sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
911                           sym_name = new_name;
912
913                           hash = (struct elf32_mn10300_link_hash_entry *)
914                                    elf_link_hash_lookup (&hash_table->static_hash_table->root,
915                                                          sym_name, true,
916                                                          true, false);
917                         }
918                       else
919                         {
920                           r_index -= symtab_hdr->sh_info;
921                           hash = (struct elf32_mn10300_link_hash_entry *)
922                                    elf_sym_hashes (input_bfd)[r_index];
923                         }
924
925                       /* If this is not a "call" instruction, then we
926                          should convert "call" instructions to "calls"
927                          instructions.  */
928                       code = bfd_get_8 (input_bfd,
929                                         contents + irel->r_offset - 1);
930                       if (code != 0xdd && code != 0xcd)
931                         hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
932
933                       /* If this is a jump/call, then bump the direct_calls
934                          counter.  Else force "call" to "calls" conversions.  */
935                       if (r_type == R_MN10300_PCREL32
936                           || r_type == R_MN10300_PCREL16)
937                         hash->direct_calls++;
938                       else
939                         hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
940                     }
941                 }
942
943               /* Now look at the actual contents to get the stack size,
944                  and a list of what registers were saved in the prologue
945                  (ie movm_args).  */
946               if ((section->flags & SEC_CODE) != 0)
947                 {
948
949                   Elf32_External_Sym *esym, *esymend;
950                   int idx, shndx;
951
952                   shndx = _bfd_elf_section_from_bfd_section (input_bfd,
953                                                              section);
954
955
956                   /* Look at each function defined in this section and
957                      update info for that function.  */
958                   esym = extsyms;
959                   esymend = esym + symtab_hdr->sh_info;
960                   for (; esym < esymend; esym++)
961                     {
962                       Elf_Internal_Sym isym;
963
964                       bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
965                       if (isym.st_shndx == shndx
966                           && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
967                         {
968                           if (isym.st_shndx == SHN_UNDEF)
969                             sym_sec = bfd_und_section_ptr;
970                           else if (isym.st_shndx > 0
971                                    && isym.st_shndx < SHN_LORESERVE)
972                             sym_sec
973                               = bfd_section_from_elf_index (input_bfd,
974                                                             isym.st_shndx);
975                           else if (isym.st_shndx == SHN_ABS)
976                             sym_sec = bfd_abs_section_ptr;
977                           else if (isym.st_shndx == SHN_COMMON)
978                             sym_sec = bfd_com_section_ptr;
979
980                           sym_name = bfd_elf_string_from_elf_section (input_bfd,
981                                                         symtab_hdr->sh_link,
982                                                         isym.st_name);
983
984                           /* Tack on an ID so we can uniquely identify this
985                              local symbol in the global hash table.  */
986                           new_name = alloca (strlen (sym_name) + 10);
987                           sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
988                           sym_name = new_name;
989
990                           hash = (struct elf32_mn10300_link_hash_entry *)
991                                     elf_link_hash_lookup (&hash_table->static_hash_table->root,
992                                                           sym_name, true,
993                                                           true, false);
994                           compute_function_info (input_bfd, hash,
995                                                  isym.st_value, contents);
996                         }
997                     }
998
999                   esym = extsyms + symtab_hdr->sh_info;
1000                   esymend = extsyms + (symtab_hdr->sh_size
1001                                        / sizeof (Elf32_External_Sym));
1002                   for (idx = 0; esym < esymend; esym++, idx++)
1003                     {
1004                       Elf_Internal_Sym isym;
1005
1006                       bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1007                       hash = (struct elf32_mn10300_link_hash_entry *)
1008                                elf_sym_hashes (input_bfd)[idx];
1009                       if (isym.st_shndx == shndx
1010                           && ELF_ST_TYPE (isym.st_info) == STT_FUNC
1011                           && (hash)->root.root.u.def.section == section
1012                           && ((hash)->root.root.type == bfd_link_hash_defined
1013                               || (hash)->root.root.type == bfd_link_hash_defweak))
1014                         compute_function_info (input_bfd, hash,
1015                                                (hash)->root.root.u.def.value,
1016                                                contents);
1017                     }
1018                 }
1019
1020               /* Cache or free any memory we allocated for the relocs.  */
1021               if (free_relocs != NULL)
1022                 {
1023                   free (free_relocs);
1024                   free_relocs = NULL;
1025                 }
1026
1027               /* Cache or free any memory we allocated for the contents.  */
1028               if (free_contents != NULL)
1029                 {
1030                   if (! link_info->keep_memory)
1031                     free (free_contents);
1032                   else
1033                     {
1034                       /* Cache the section contents for elf_link_input_bfd.  */
1035                       elf_section_data (section)->this_hdr.contents = contents;
1036                     }
1037                   free_contents = NULL;
1038                 }
1039             }
1040
1041           /* Cache or free any memory we allocated for the symbols.  */
1042           if (free_extsyms != NULL)
1043             {
1044               if (! link_info->keep_memory)
1045                 free (free_extsyms);
1046               else
1047                 {
1048                   /* Cache the symbols for elf_link_input_bfd.  */
1049                   symtab_hdr->contents = extsyms;
1050                 }
1051               free_extsyms = NULL;
1052             }
1053         }
1054
1055       /* Now iterate on each symbol in the hash table and perform
1056          the final initialization steps on each.  */
1057       elf32_mn10300_link_hash_traverse (hash_table,
1058                                         elf32_mn10300_finish_hash_table_entry,
1059                                         NULL);
1060       elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1061                                         elf32_mn10300_finish_hash_table_entry,
1062                                         NULL);
1063
1064       /* All entries in the hash table are fully initialized.  */
1065       hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1066
1067       /* Now that everything has been initialized, go through each
1068          code section and delete any prologue insns which will be
1069          redundant because their operations will be performed by
1070          a "call" instruction.  */
1071       for (input_bfd = link_info->input_bfds;
1072            input_bfd != NULL;
1073            input_bfd = input_bfd->link_next)
1074         {
1075           asection *section;
1076
1077           /* We're going to need all the symbols for each bfd.  */
1078           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1079
1080           /* Get cached copy if it exists.  */
1081           if (symtab_hdr->contents != NULL)
1082             extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1083           else
1084             {
1085               /* Go get them off disk.  */
1086               extsyms = ((Elf32_External_Sym *)
1087                          bfd_malloc (symtab_hdr->sh_size));
1088               if (extsyms == NULL)
1089                 goto error_return;
1090               free_extsyms = extsyms;
1091               if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1092                   || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
1093                       != symtab_hdr->sh_size))
1094                 goto error_return;
1095             }
1096
1097           /* Walk over each section in this bfd.  */
1098           for (section = input_bfd->sections;
1099                section != NULL;
1100                section = section->next)
1101             {
1102               int shndx;
1103               Elf32_External_Sym *esym, *esymend;
1104               int idx;
1105
1106               /* Skip non-code sections and empty sections.  */
1107               if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1108                 continue;
1109
1110               if (section->reloc_count != 0)
1111                 {
1112                   /* Get a copy of the native relocations.  */
1113                   internal_relocs = (_bfd_elf32_link_read_relocs
1114                                      (input_bfd, section, (PTR) NULL,
1115                                       (Elf_Internal_Rela *) NULL,
1116                                       link_info->keep_memory));
1117                   if (internal_relocs == NULL)
1118                     goto error_return;
1119                   if (! link_info->keep_memory)
1120                     free_relocs = internal_relocs;
1121                 }
1122
1123               /* Get cached copy of section contents if it exists.  */
1124               if (elf_section_data (section)->this_hdr.contents != NULL)
1125                 contents = elf_section_data (section)->this_hdr.contents;
1126               else
1127                 {
1128                   /* Go get them off disk.  */
1129                   contents = (bfd_byte *)bfd_malloc (section->_raw_size);
1130                   if (contents == NULL)
1131                     goto error_return;
1132                   free_contents = contents;
1133
1134                   if (!bfd_get_section_contents (input_bfd, section,
1135                                                  contents, (file_ptr) 0,
1136                                                  section->_raw_size))
1137                     goto error_return;
1138                 }
1139
1140
1141               shndx = _bfd_elf_section_from_bfd_section (input_bfd, section);
1142
1143               /* Now look for any function in this section which needs
1144                  insns deleted from its prologue.  */
1145               esym = extsyms;
1146               esymend = esym + symtab_hdr->sh_info;
1147               for (; esym < esymend; esym++)
1148                 {
1149                   Elf_Internal_Sym isym;
1150                   struct elf32_mn10300_link_hash_entry *sym_hash;
1151                   asection *sym_sec;
1152                   const char *sym_name;
1153                   Elf_Internal_Shdr *hdr;
1154                   char *new_name;
1155
1156                   bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1157
1158                   if (isym.st_shndx != shndx)
1159                     continue;
1160
1161                   if (isym.st_shndx == SHN_UNDEF)
1162                     sym_sec = bfd_und_section_ptr;
1163                   else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1164                     sym_sec
1165                       = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1166                   else if (isym.st_shndx == SHN_ABS)
1167                     sym_sec = bfd_abs_section_ptr;
1168                   else if (isym.st_shndx == SHN_COMMON)
1169                     sym_sec = bfd_com_section_ptr;
1170
1171                   sym_name = bfd_elf_string_from_elf_section (input_bfd,
1172                                                         symtab_hdr->sh_link,
1173                                                         isym.st_name);
1174
1175                   /* Tack on an ID so we can uniquely identify this
1176                      local symbol in the global hash table.  */
1177                   new_name = alloca (strlen (sym_name) + 10);
1178                   sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1179                   sym_name = new_name;
1180
1181                   sym_hash = (struct elf32_mn10300_link_hash_entry *)
1182                             elf_link_hash_lookup (&hash_table->static_hash_table->root,
1183                                                   sym_name, false,
1184                                                   false, false);
1185
1186                   if (sym_hash == NULL)
1187                     continue;
1188
1189                   if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1190                       && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1191                     {
1192                       int bytes = 0;
1193
1194                       /* Note that we've changed things.  */
1195                       elf_section_data (section)->relocs = internal_relocs;
1196                       free_relocs = NULL;
1197
1198                       elf_section_data (section)->this_hdr.contents = contents;
1199                       free_contents = NULL;
1200
1201                       symtab_hdr->contents = (bfd_byte *)extsyms;
1202                       free_extsyms = NULL;
1203
1204                       /* Count how many bytes we're going to delete.  */
1205                       if (sym_hash->movm_args)
1206                         bytes += 2;
1207
1208                       if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1209                         bytes += 3;
1210                       else if (sym_hash->stack_size
1211                                && sym_hash->stack_size < 256)
1212                         bytes += 4;
1213
1214                       /* Note that we've deleted prologue bytes for this
1215                          function.  */
1216                       sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1217
1218                       /* Actually delete the bytes.  */
1219                       if (!mn10300_elf_relax_delete_bytes (input_bfd,
1220                                                            section,
1221                                                            isym.st_value,
1222                                                            bytes))
1223                         goto error_return;
1224
1225                       /* Something changed.  Not strictly necessary, but
1226                          may lead to more relaxing opportunities.  */
1227                       *again = true;
1228                     }
1229                 }
1230
1231               /* Look for any global functions in this section which
1232                  need insns deleted from their prologues.  */
1233               esym = extsyms + symtab_hdr->sh_info;
1234               esymend = extsyms + (symtab_hdr->sh_size
1235                                    / sizeof (Elf32_External_Sym));
1236               for (idx = 0; esym < esymend; esym++, idx++)
1237                 {
1238                   Elf_Internal_Sym isym;
1239                   struct elf32_mn10300_link_hash_entry *sym_hash;
1240
1241                   bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1242                   sym_hash = (struct elf32_mn10300_link_hash_entry *)
1243                                (elf_sym_hashes (input_bfd)[idx]);
1244                   if (isym.st_shndx == shndx
1245                       && (sym_hash)->root.root.u.def.section == section
1246                       && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1247                       && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1248                     {
1249                       int bytes = 0;
1250
1251                       /* Note that we've changed things.  */
1252                       elf_section_data (section)->relocs = internal_relocs;
1253                       free_relocs = NULL;
1254
1255                       elf_section_data (section)->this_hdr.contents = contents;
1256                       free_contents = NULL;
1257
1258                       symtab_hdr->contents = (bfd_byte *)extsyms;
1259                       free_extsyms = NULL;
1260
1261                       /* Count how many bytes we're going to delete.  */
1262                       if (sym_hash->movm_args)
1263                         bytes += 2;
1264
1265                       if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1266                         bytes += 3;
1267                       else if (sym_hash->stack_size
1268                                && sym_hash->stack_size < 256)
1269                         bytes += 4;
1270
1271                       /* Note that we've deleted prologue bytes for this
1272                          function.  */
1273                       sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1274
1275                       /* Actually delete the bytes.  */
1276                       if (!mn10300_elf_relax_delete_bytes (input_bfd,
1277                                                            section,
1278                                                            (sym_hash)->root.root.u.def.value,
1279                                                            bytes))
1280                         goto error_return;
1281
1282                       /* Something changed.  Not strictly necessary, but
1283                          may lead to more relaxing opportunities.  */
1284                       *again = true;
1285                     }
1286                 }
1287
1288               /* Cache or free any memory we allocated for the relocs.  */
1289               if (free_relocs != NULL)
1290                 {
1291                   free (free_relocs);
1292                   free_relocs = NULL;
1293                 }
1294
1295               /* Cache or free any memory we allocated for the contents.  */
1296               if (free_contents != NULL)
1297                 {
1298                   if (! link_info->keep_memory)
1299                     free (free_contents);
1300                   else
1301                     {
1302                       /* Cache the section contents for elf_link_input_bfd.  */
1303                       elf_section_data (section)->this_hdr.contents = contents;
1304                     }
1305                   free_contents = NULL;
1306                 }
1307             }
1308
1309           /* Cache or free any memory we allocated for the symbols.  */
1310           if (free_extsyms != NULL)
1311             {
1312               if (! link_info->keep_memory)
1313                 free (free_extsyms);
1314               else
1315                 {
1316                   /* Cache the symbols for elf_link_input_bfd.  */
1317                   symtab_hdr->contents = extsyms;
1318                 }
1319               free_extsyms = NULL;
1320             }
1321         }
1322     }
1323
1324
1325   /* (Re)initialize for the basic instruction shortening/relaxing pass.  */
1326   contents = NULL;
1327   extsyms = NULL;
1328   internal_relocs = NULL;
1329   free_relocs = NULL;
1330   free_contents = NULL;
1331   free_extsyms = NULL;
1332
1333   /* We don't have to do anything for a relocateable link, if
1334      this section does not have relocs, or if this is not a
1335      code section.  */
1336   if (link_info->relocateable
1337       || (sec->flags & SEC_RELOC) == 0
1338       || sec->reloc_count == 0
1339       || (sec->flags & SEC_CODE) == 0)
1340     return true;
1341
1342   /* If this is the first time we have been called for this section,
1343      initialize the cooked size.  */
1344   if (sec->_cooked_size == 0)
1345     sec->_cooked_size = sec->_raw_size;
1346
1347   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1348
1349   /* Get a copy of the native relocations.  */
1350   internal_relocs = (_bfd_elf32_link_read_relocs
1351                      (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1352                       link_info->keep_memory));
1353   if (internal_relocs == NULL)
1354     goto error_return;
1355   if (! link_info->keep_memory)
1356     free_relocs = internal_relocs;
1357
1358   /* Walk through them looking for relaxing opportunities.  */
1359   irelend = internal_relocs + sec->reloc_count;
1360   for (irel = internal_relocs; irel < irelend; irel++)
1361     {
1362       bfd_vma symval;
1363       struct elf32_mn10300_link_hash_entry *h = NULL;
1364
1365       /* If this isn't something that can be relaxed, then ignore
1366          this reloc.  */
1367       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1368           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1369           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1370         continue;
1371
1372       /* Get the section contents if we haven't done so already.  */
1373       if (contents == NULL)
1374         {
1375           /* Get cached copy if it exists.  */
1376           if (elf_section_data (sec)->this_hdr.contents != NULL)
1377             contents = elf_section_data (sec)->this_hdr.contents;
1378           else
1379             {
1380               /* Go get them off disk.  */
1381               contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1382               if (contents == NULL)
1383                 goto error_return;
1384               free_contents = contents;
1385
1386               if (! bfd_get_section_contents (abfd, sec, contents,
1387                                               (file_ptr) 0, sec->_raw_size))
1388                 goto error_return;
1389             }
1390         }
1391
1392       /* Read this BFD's symbols if we haven't done so already.  */
1393       if (extsyms == NULL)
1394         {
1395           /* Get cached copy if it exists.  */
1396           if (symtab_hdr->contents != NULL)
1397             extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1398           else
1399             {
1400               /* Go get them off disk.  */
1401               extsyms = ((Elf32_External_Sym *)
1402                          bfd_malloc (symtab_hdr->sh_size));
1403               if (extsyms == NULL)
1404                 goto error_return;
1405               free_extsyms = extsyms;
1406               if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1407                   || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1408                       != symtab_hdr->sh_size))
1409                 goto error_return;
1410             }
1411         }
1412
1413       /* Get the value of the symbol referred to by the reloc.  */
1414       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1415         {
1416           Elf_Internal_Sym isym;
1417           asection *sym_sec;
1418           Elf_Internal_Shdr *hdr;
1419           const char *sym_name;
1420           char *new_name;
1421
1422           /* A local symbol.  */
1423           bfd_elf32_swap_symbol_in (abfd,
1424                                     extsyms + ELF32_R_SYM (irel->r_info),
1425                                     &isym);
1426
1427           if (isym.st_shndx == SHN_UNDEF)
1428             sym_sec = bfd_und_section_ptr;
1429           else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1430             sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1431           else if (isym.st_shndx == SHN_ABS)
1432             sym_sec = bfd_abs_section_ptr;
1433           else if (isym.st_shndx == SHN_COMMON)
1434             sym_sec = bfd_com_section_ptr;
1435
1436           symval = (isym.st_value
1437                     + sym_sec->output_section->vma
1438                     + sym_sec->output_offset);
1439           sym_name = bfd_elf_string_from_elf_section (abfd,
1440                                                       symtab_hdr->sh_link,
1441                                                       isym.st_name);
1442
1443           /* Tack on an ID so we can uniquely identify this
1444              local symbol in the global hash table.  */
1445           new_name = alloca (strlen (sym_name) + 10);
1446           sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1447           sym_name = new_name;
1448
1449           h = (struct elf32_mn10300_link_hash_entry *)
1450                 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1451                                       sym_name, false, false, false);
1452         }
1453       else
1454         {
1455           unsigned long indx;
1456
1457           /* An external symbol.  */
1458           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1459           h = (struct elf32_mn10300_link_hash_entry *)
1460                 (elf_sym_hashes (abfd)[indx]);
1461           BFD_ASSERT (h != NULL);
1462           if (h->root.root.type != bfd_link_hash_defined
1463               && h->root.root.type != bfd_link_hash_defweak)
1464             {
1465               /* This appears to be a reference to an undefined
1466                 symbol.  Just ignore it--it will be caught by the
1467                 regular reloc processing.  */
1468               continue;
1469             }
1470
1471           symval = (h->root.root.u.def.value
1472                     + h->root.root.u.def.section->output_section->vma
1473                     + h->root.root.u.def.section->output_offset);
1474         }
1475
1476       /* For simplicity of coding, we are going to modify the section
1477          contents, the section relocs, and the BFD symbol table.  We
1478          must tell the rest of the code not to free up this
1479          information.  It would be possible to instead create a table
1480          of changes which have to be made, as is done in coff-mips.c;
1481          that would be more work, but would require less memory when
1482          the linker is run.  */
1483
1484       /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1485          branch/call, also deal with "call" -> "calls" conversions and
1486          insertion of prologue data into "call" instructions.  */
1487       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1488         {
1489           bfd_vma value = symval;
1490
1491           /* If we've got a "call" instruction that needs to be turned
1492              into a "calls" instruction, do so now.  It saves a byte.  */
1493           if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1494             {
1495               unsigned char code;
1496
1497               /* Get the opcode.  */
1498               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1499
1500               /* Make sure we're working with a "call" instruction!  */
1501               if (code == 0xdd)
1502                 {
1503                   /* Note that we've changed the relocs, section contents,
1504                      etc.  */
1505                   elf_section_data (sec)->relocs = internal_relocs;
1506                   free_relocs = NULL;
1507
1508                   elf_section_data (sec)->this_hdr.contents = contents;
1509                   free_contents = NULL;
1510
1511                   symtab_hdr->contents = (bfd_byte *) extsyms;
1512                   free_extsyms = NULL;
1513
1514                   /* Fix the opcode.  */
1515                   bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1516                   bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1517
1518                   /* Fix irel->r_offset and irel->r_addend.  */
1519                   irel->r_offset += 1;
1520                   irel->r_addend += 1;
1521
1522                   /* Delete one byte of data.  */
1523                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1524                                                        irel->r_offset + 3, 1))
1525                     goto error_return;
1526
1527                   /* That will change things, so, we should relax again.
1528                      Note that this is not required, and it may be slow.  */
1529                   *again = true;
1530                 }
1531             }
1532           else if (h)
1533             {
1534               /* We've got a "call" instruction which needs some data
1535                  from target function filled in.  */
1536               unsigned char code;
1537
1538               /* Get the opcode.  */
1539               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1540
1541               /* Insert data from the target function into the "call"
1542                  instruction if needed.  */
1543               if (code == 0xdd)
1544                 {
1545                   bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1546                   bfd_put_8 (abfd, h->stack_size,
1547                              contents + irel->r_offset + 5);
1548                 }
1549             }
1550
1551           /* Deal with pc-relative gunk.  */
1552           value -= (sec->output_section->vma + sec->output_offset);
1553           value -= irel->r_offset;
1554           value += irel->r_addend;
1555
1556           /* See if the value will fit in 16 bits, note the high value is
1557              0x7fff + 2 as the target will be two bytes closer if we are
1558              able to relax.  */
1559           if ((long)value < 0x8001 && (long)value > -0x8000)
1560             {
1561               unsigned char code;
1562
1563               /* Get the opcode.  */
1564               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1565
1566               if (code != 0xdc && code != 0xdd && code != 0xff)
1567                 continue;
1568
1569               /* Note that we've changed the relocs, section contents, etc.  */
1570               elf_section_data (sec)->relocs = internal_relocs;
1571               free_relocs = NULL;
1572
1573               elf_section_data (sec)->this_hdr.contents = contents;
1574               free_contents = NULL;
1575
1576               symtab_hdr->contents = (bfd_byte *) extsyms;
1577               free_extsyms = NULL;
1578
1579               /* Fix the opcode.  */
1580               if (code == 0xdc)
1581                 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1582               else if (code == 0xdd)
1583                 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1584               else if (code == 0xff)
1585                 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1586
1587               /* Fix the relocation's type.  */
1588               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1589                                            R_MN10300_PCREL16);
1590
1591               /* Delete two bytes of data.  */
1592               if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1593                                                    irel->r_offset + 1, 2))
1594                 goto error_return;
1595
1596               /* That will change things, so, we should relax again.
1597                  Note that this is not required, and it may be slow.  */
1598               *again = true;
1599             }
1600         }
1601
1602       /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1603          branch.  */
1604       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1605         {
1606           bfd_vma value = symval;
1607
1608           /* If we've got a "call" instruction that needs to be turned
1609              into a "calls" instruction, do so now.  It saves a byte.  */
1610           if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1611             {
1612               unsigned char code;
1613
1614               /* Get the opcode.  */
1615               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1616
1617               /* Make sure we're working with a "call" instruction!  */
1618               if (code == 0xcd)
1619                 {
1620                   /* Note that we've changed the relocs, section contents,
1621                      etc.  */
1622                   elf_section_data (sec)->relocs = internal_relocs;
1623                   free_relocs = NULL;
1624
1625                   elf_section_data (sec)->this_hdr.contents = contents;
1626                   free_contents = NULL;
1627
1628                   symtab_hdr->contents = (bfd_byte *) extsyms;
1629                   free_extsyms = NULL;
1630
1631                   /* Fix the opcode.  */
1632                   bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1633                   bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1634
1635                   /* Fix irel->r_offset and irel->r_addend.  */
1636                   irel->r_offset += 1;
1637                   irel->r_addend += 1;
1638
1639                   /* Delete one byte of data.  */
1640                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1641                                                        irel->r_offset + 1, 1))
1642                     goto error_return;
1643
1644                   /* That will change things, so, we should relax again.
1645                      Note that this is not required, and it may be slow.  */
1646                   *again = true;
1647                 }
1648             }
1649           else if (h)
1650             {
1651               unsigned char code;
1652
1653               /* Get the opcode.  */
1654               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1655
1656               /* Insert data from the target function into the "call"
1657                  instruction if needed.  */
1658               if (code == 0xcd)
1659                 {
1660                   bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1661                   bfd_put_8 (abfd, h->stack_size,
1662                              contents + irel->r_offset + 3);
1663                 }
1664             }
1665
1666           /* Deal with pc-relative gunk.  */
1667           value -= (sec->output_section->vma + sec->output_offset);
1668           value -= irel->r_offset;
1669           value += irel->r_addend;
1670
1671           /* See if the value will fit in 8 bits, note the high value is
1672              0x7f + 1 as the target will be one bytes closer if we are
1673              able to relax.  */
1674           if ((long)value < 0x80 && (long)value > -0x80)
1675             {
1676               unsigned char code;
1677
1678               /* Get the opcode.  */
1679               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1680
1681               if (code != 0xcc)
1682                 continue;
1683
1684               /* Note that we've changed the relocs, section contents, etc.  */
1685               elf_section_data (sec)->relocs = internal_relocs;
1686               free_relocs = NULL;
1687
1688               elf_section_data (sec)->this_hdr.contents = contents;
1689               free_contents = NULL;
1690
1691               symtab_hdr->contents = (bfd_byte *) extsyms;
1692               free_extsyms = NULL;
1693
1694               /* Fix the opcode.  */
1695               bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1696
1697               /* Fix the relocation's type.  */
1698               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1699                                            R_MN10300_PCREL8);
1700
1701               /* Delete one byte of data.  */
1702               if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1703                                                    irel->r_offset + 1, 1))
1704                 goto error_return;
1705
1706               /* That will change things, so, we should relax again.
1707                  Note that this is not required, and it may be slow.  */
1708               *again = true;
1709             }
1710         }
1711
1712       /* Try to eliminate an unconditional 8 bit pc-relative branch
1713          which immediately follows a conditional 8 bit pc-relative
1714          branch around the unconditional branch.
1715
1716             original:           new:
1717             bCC lab1            bCC' lab2
1718             bra lab2
1719            lab1:               lab1:
1720
1721
1722          This happens when the bCC can't reach lab2 at assembly time,
1723          but due to other relaxations it can reach at link time.  */
1724       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1725         {
1726           Elf_Internal_Rela *nrel;
1727           bfd_vma value = symval;
1728           unsigned char code;
1729
1730           /* Deal with pc-relative gunk.  */
1731           value -= (sec->output_section->vma + sec->output_offset);
1732           value -= irel->r_offset;
1733           value += irel->r_addend;
1734
1735           /* Do nothing if this reloc is the last byte in the section.  */
1736           if (irel->r_offset == sec->_cooked_size)
1737             continue;
1738
1739           /* See if the next instruction is an unconditional pc-relative
1740              branch, more often than not this test will fail, so we
1741              test it first to speed things up.  */
1742           code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1743           if (code != 0xca)
1744             continue;
1745
1746           /* Also make sure the next relocation applies to the next
1747              instruction and that it's a pc-relative 8 bit branch.  */
1748           nrel = irel + 1;
1749           if (nrel == irelend
1750               || irel->r_offset + 2 != nrel->r_offset
1751               || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1752             continue;
1753
1754           /* Make sure our destination immediately follows the
1755              unconditional branch.  */
1756           if (symval != (sec->output_section->vma + sec->output_offset
1757                          + irel->r_offset + 3))
1758             continue;
1759
1760           /* Now make sure we are a conditional branch.  This may not
1761              be necessary, but why take the chance.
1762
1763              Note these checks assume that R_MN10300_PCREL8 relocs
1764              only occur on bCC and bCCx insns.  If they occured
1765              elsewhere, we'd need to know the start of this insn
1766              for this check to be accurate.  */
1767           code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1768           if (code != 0xc0 && code != 0xc1 && code != 0xc2
1769               && code != 0xc3 && code != 0xc4 && code != 0xc5
1770               && code != 0xc6 && code != 0xc7 && code != 0xc8
1771               && code != 0xc9 && code != 0xe8 && code != 0xe9
1772               && code != 0xea && code != 0xeb)
1773             continue;
1774
1775           /* We also have to be sure there is no symbol/label
1776              at the unconditional branch.  */
1777           if (mn10300_elf_symbol_address_p (abfd, sec, extsyms,
1778                                             irel->r_offset + 1))
1779             continue;
1780
1781           /* Note that we've changed the relocs, section contents, etc.  */
1782           elf_section_data (sec)->relocs = internal_relocs;
1783           free_relocs = NULL;
1784
1785           elf_section_data (sec)->this_hdr.contents = contents;
1786           free_contents = NULL;
1787
1788           symtab_hdr->contents = (bfd_byte *) extsyms;
1789           free_extsyms = NULL;
1790
1791           /* Reverse the condition of the first branch.  */
1792           switch (code)
1793             {
1794               case 0xc8:
1795                 code = 0xc9;
1796                 break;
1797               case 0xc9:
1798                 code = 0xc8;
1799                 break;
1800               case 0xc0:
1801                 code = 0xc2;
1802                 break;
1803               case 0xc2:
1804                 code = 0xc0;
1805                 break;
1806               case 0xc3:
1807                 code = 0xc1;
1808                 break;
1809               case 0xc1:
1810                 code = 0xc3;
1811                 break;
1812               case 0xc4:
1813                 code = 0xc6;
1814                 break;
1815               case 0xc6:
1816                 code = 0xc4;
1817                 break;
1818               case 0xc7:
1819                 code = 0xc5;
1820                 break;
1821               case 0xc5:
1822                 code = 0xc7;
1823                 break;
1824               case 0xe8:
1825                 code = 0xe9;
1826                 break;
1827               case 0x9d:
1828                 code = 0xe8;
1829                 break;
1830               case 0xea:
1831                 code = 0xeb;
1832                 break;
1833               case 0xeb:
1834                 code = 0xea;
1835                 break;
1836             }
1837           bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1838
1839           /* Set the reloc type and symbol for the first branch
1840              from the second branch.  */
1841           irel->r_info = nrel->r_info;
1842
1843           /* Make the reloc for the second branch a null reloc.  */
1844           nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1845                                        R_MN10300_NONE);
1846
1847           /* Delete two bytes of data.  */
1848           if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1849                                                irel->r_offset + 1, 2))
1850             goto error_return;
1851
1852           /* That will change things, so, we should relax again.
1853              Note that this is not required, and it may be slow.  */
1854           *again = true;
1855         }
1856
1857       /* Try to turn a 32bit immediate, displacement or absolute address
1858          into a 16bit immediate, displacement or absolute address.  */
1859       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1860         {
1861           bfd_vma value = symval;
1862           value += irel->r_addend;
1863
1864           /* See if the value will fit in 16 bits.
1865              We allow any 16bit match here.  We prune those we can't
1866              handle below.  */
1867           if ((long)value < 0x7fff && (long)value > -0x8000)
1868             {
1869               unsigned char code;
1870
1871               /* Most insns which have 32bit operands are 6 bytes long;
1872                  exceptions are pcrel insns and bit insns.
1873
1874                  We handle pcrel insns above.  We don't bother trying
1875                  to handle the bit insns here.
1876
1877                  The first byte of the remaining insns will be 0xfc.  */
1878
1879               /* Get the first opcode.  */
1880               code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1881
1882               if (code != 0xfc)
1883                 continue;
1884
1885               /* Get the second opcode.  */
1886               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1887
1888               if ((code & 0xf0) < 0x80)
1889                 switch (code & 0xf0)
1890                   {
1891                   /* mov (d32,am),dn   -> mov (d32,am),dn
1892                      mov dm,(d32,am)   -> mov dn,(d32,am)
1893                      mov (d32,am),an   -> mov (d32,am),an
1894                      mov dm,(d32,am)   -> mov dn,(d32,am)
1895                      movbu (d32,am),dn -> movbu (d32,am),dn
1896                      movbu dm,(d32,am) -> movbu dn,(d32,am)
1897                      movhu (d32,am),dn -> movhu (d32,am),dn
1898                      movhu dm,(d32,am) -> movhu dn,(d32,am) */
1899                   case 0x00:
1900                   case 0x10:
1901                   case 0x20:
1902                   case 0x30:
1903                   case 0x40:
1904                   case 0x50:
1905                   case 0x60:
1906                   case 0x70:
1907                     /* Not safe if the high bit is on as relaxing may
1908                        move the value out of high mem and thus not fit
1909                        in a signed 16bit value.  */
1910                     if (code == 0xcc
1911                         && (value & 0x8000))
1912                       continue;
1913
1914                     /* Note that we've changed the relocation contents, etc.  */
1915                     elf_section_data (sec)->relocs = internal_relocs;
1916                     free_relocs = NULL;
1917
1918                     elf_section_data (sec)->this_hdr.contents = contents;
1919                     free_contents = NULL;
1920
1921                     symtab_hdr->contents = (bfd_byte *) extsyms;
1922                     free_extsyms = NULL;
1923
1924                     /* Fix the opcode.  */
1925                     bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1926                     bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1927
1928                     /* Fix the relocation's type.  */
1929                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1930                                                  R_MN10300_16);
1931
1932                     /* Delete two bytes of data.  */
1933                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1934                                                          irel->r_offset + 2, 2))
1935                       goto error_return;
1936
1937                     /* That will change things, so, we should relax again.
1938                        Note that this is not required, and it may be slow.  */
1939                     *again = true;
1940                     break;
1941                   }
1942               else if ((code & 0xf0) == 0x80
1943                        || (code & 0xf0) == 0x90)
1944                 switch (code & 0xf3)
1945                   {
1946                   /* mov dn,(abs32)   -> mov dn,(abs16)
1947                      movbu dn,(abs32) -> movbu dn,(abs16)
1948                      movhu dn,(abs32) -> movhu dn,(abs16)  */
1949                   case 0x81:
1950                   case 0x82:
1951                   case 0x83:
1952                     /* Note that we've changed the relocation contents, etc.  */
1953                     elf_section_data (sec)->relocs = internal_relocs;
1954                     free_relocs = NULL;
1955
1956                     elf_section_data (sec)->this_hdr.contents = contents;
1957                     free_contents = NULL;
1958
1959                     symtab_hdr->contents = (bfd_byte *) extsyms;
1960                     free_extsyms = NULL;
1961
1962                     if ((code & 0xf3) == 0x81)
1963                       code = 0x01 + (code & 0x0c);
1964                     else if ((code & 0xf3) == 0x82)
1965                       code = 0x02 + (code & 0x0c);
1966                     else if ((code & 0xf3) == 0x83)
1967                       code = 0x03 + (code & 0x0c);
1968                     else
1969                       abort ();
1970
1971                     /* Fix the opcode.  */
1972                     bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1973
1974                     /* Fix the relocation's type.  */
1975                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1976                                                  R_MN10300_16);
1977
1978                     /* The opcode got shorter too, so we have to fix the
1979                        addend and offset too!  */
1980                     irel->r_offset -= 1;
1981
1982                     /* Delete three bytes of data.  */
1983                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1984                                                          irel->r_offset + 1, 3))
1985                       goto error_return;
1986
1987                     /* That will change things, so, we should relax again.
1988                        Note that this is not required, and it may be slow.  */
1989                     *again = true;
1990                     break;
1991
1992                   /* mov am,(abs32)    -> mov am,(abs16)
1993                      mov am,(d32,sp)   -> mov am,(d16,sp)
1994                      mov dm,(d32,sp)   -> mov dm,(d32,sp)
1995                      movbu dm,(d32,sp) -> movbu dm,(d32,sp)
1996                      movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
1997                   case 0x80:
1998                   case 0x90:
1999                   case 0x91:
2000                   case 0x92:
2001                   case 0x93:
2002                     /* Note that we've changed the relocation contents, etc.  */
2003                     elf_section_data (sec)->relocs = internal_relocs;
2004                     free_relocs = NULL;
2005
2006                     elf_section_data (sec)->this_hdr.contents = contents;
2007                     free_contents = NULL;
2008
2009                     symtab_hdr->contents = (bfd_byte *) extsyms;
2010                     free_extsyms = NULL;
2011
2012                     /* Fix the opcode.  */
2013                     bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2014                     bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2015
2016                     /* Fix the relocation's type.  */
2017                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2018                                                  R_MN10300_16);
2019
2020                     /* Delete two bytes of data.  */
2021                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2022                                                          irel->r_offset + 2, 2))
2023                       goto error_return;
2024
2025                     /* That will change things, so, we should relax again.
2026                        Note that this is not required, and it may be slow.  */
2027                     *again = true;
2028                     break;
2029                   }
2030               else if ((code & 0xf0) < 0xf0)
2031                 switch (code & 0xfc)
2032                   {
2033                   /* mov imm32,dn     -> mov imm16,dn
2034                      mov imm32,an     -> mov imm16,an
2035                      mov (abs32),dn   -> mov (abs16),dn
2036                      movbu (abs32),dn -> movbu (abs16),dn
2037                      movhu (abs32),dn -> movhu (abs16),dn  */
2038                   case 0xcc:
2039                   case 0xdc:
2040                   case 0xa4:
2041                   case 0xa8:
2042                   case 0xac:
2043                     /* Not safe if the high bit is on as relaxing may
2044                        move the value out of high mem and thus not fit
2045                        in a signed 16bit value.  */
2046                     if (code == 0xcc
2047                         && (value & 0x8000))
2048                       continue;
2049
2050                     /* Note that we've changed the relocation contents, etc.  */
2051                     elf_section_data (sec)->relocs = internal_relocs;
2052                     free_relocs = NULL;
2053
2054                     elf_section_data (sec)->this_hdr.contents = contents;
2055                     free_contents = NULL;
2056
2057                     symtab_hdr->contents = (bfd_byte *) extsyms;
2058                     free_extsyms = NULL;
2059
2060                     if ((code & 0xfc) == 0xcc)
2061                       code = 0x2c + (code & 0x03);
2062                     else if ((code & 0xfc) == 0xdc)
2063                       code = 0x24 + (code & 0x03);
2064                     else if ((code & 0xfc) == 0xa4)
2065                       code = 0x30 + (code & 0x03);
2066                     else if ((code & 0xfc) == 0xa8)
2067                       code = 0x34 + (code & 0x03);
2068                     else if ((code & 0xfc) == 0xac)
2069                       code = 0x38 + (code & 0x03);
2070                     else
2071                       abort ();
2072
2073                     /* Fix the opcode.  */
2074                     bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2075
2076                     /* Fix the relocation's type.  */
2077                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2078                                                  R_MN10300_16);
2079
2080                     /* The opcode got shorter too, so we have to fix the
2081                        addend and offset too!  */
2082                     irel->r_offset -= 1;
2083
2084                     /* Delete three bytes of data.  */
2085                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2086                                                          irel->r_offset + 1, 3))
2087                       goto error_return;
2088
2089                     /* That will change things, so, we should relax again.
2090                        Note that this is not required, and it may be slow.  */
2091                     *again = true;
2092                     break;
2093
2094                   /* mov (abs32),an    -> mov (abs16),an
2095                      mov (d32,sp),an   -> mov (d32,sp),an
2096                      mov (d32,sp),dn   -> mov (d32,sp),dn
2097                      movbu (d32,sp),dn -> movbu (d32,sp),dn
2098                      movhu (d32,sp),dn -> movhu (d32,sp),dn
2099                      add imm32,dn      -> add imm16,dn
2100                      cmp imm32,dn      -> cmp imm16,dn
2101                      add imm32,an      -> add imm16,an
2102                      cmp imm32,an      -> cmp imm16,an
2103                      and imm32,dn      -> and imm32,dn
2104                      or imm32,dn       -> or imm32,dn
2105                      xor imm32,dn      -> xor imm32,dn
2106                      btst imm32,dn     -> btst imm32,dn */
2107
2108                   case 0xa0:
2109                   case 0xb0:
2110                   case 0xb1:
2111                   case 0xb2:
2112                   case 0xb3:
2113                   case 0xc0:
2114                   case 0xc8:
2115
2116                   case 0xd0:
2117                   case 0xd8:
2118                   case 0xe0:
2119                   case 0xe1:
2120                   case 0xe2:
2121                   case 0xe3:
2122                     /* Note that we've changed the relocation contents, etc.  */
2123                     elf_section_data (sec)->relocs = internal_relocs;
2124                     free_relocs = NULL;
2125
2126                     elf_section_data (sec)->this_hdr.contents = contents;
2127                     free_contents = NULL;
2128
2129                     symtab_hdr->contents = (bfd_byte *) extsyms;
2130                     free_extsyms = NULL;
2131
2132                     /* Fix the opcode.  */
2133                     bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2134                     bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2135
2136                     /* Fix the relocation's type.  */
2137                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2138                                                  R_MN10300_16);
2139
2140                     /* Delete two bytes of data.  */
2141                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2142                                                          irel->r_offset + 2, 2))
2143                       goto error_return;
2144
2145                     /* That will change things, so, we should relax again.
2146                        Note that this is not required, and it may be slow.  */
2147                     *again = true;
2148                     break;
2149                   }
2150               else if (code == 0xfe)
2151                 {
2152                   /* add imm32,sp -> add imm16,sp  */
2153
2154                   /* Note that we've changed the relocation contents, etc.  */
2155                   elf_section_data (sec)->relocs = internal_relocs;
2156                   free_relocs = NULL;
2157
2158                   elf_section_data (sec)->this_hdr.contents = contents;
2159                   free_contents = NULL;
2160
2161                   symtab_hdr->contents = (bfd_byte *) extsyms;
2162                   free_extsyms = NULL;
2163
2164                   /* Fix the opcode.  */
2165                   bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2166                   bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2167
2168                   /* Fix the relocation's type.  */
2169                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2170                                                  R_MN10300_16);
2171
2172                   /* Delete two bytes of data.  */
2173                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2174                                                        irel->r_offset + 2, 2))
2175                     goto error_return;
2176
2177                   /* That will change things, so, we should relax again.
2178                      Note that this is not required, and it may be slow.  */
2179                   *again = true;
2180                   break;
2181                 }
2182             }
2183         }
2184     }
2185
2186   if (free_relocs != NULL)
2187     {
2188       free (free_relocs);
2189       free_relocs = NULL;
2190     }
2191
2192   if (free_contents != NULL)
2193     {
2194       if (! link_info->keep_memory)
2195         free (free_contents);
2196       else
2197         {
2198           /* Cache the section contents for elf_link_input_bfd.  */
2199           elf_section_data (sec)->this_hdr.contents = contents;
2200         }
2201       free_contents = NULL;
2202     }
2203
2204   if (free_extsyms != NULL)
2205     {
2206       if (! link_info->keep_memory)
2207         free (free_extsyms);
2208       else
2209         {
2210           /* Cache the symbols for elf_link_input_bfd.  */
2211           symtab_hdr->contents = extsyms;
2212         }
2213       free_extsyms = NULL;
2214     }
2215
2216   return true;
2217
2218  error_return:
2219   if (free_relocs != NULL)
2220     free (free_relocs);
2221   if (free_contents != NULL)
2222     free (free_contents);
2223   if (free_extsyms != NULL)
2224     free (free_extsyms);
2225   return false;
2226 }
2227
2228 /* Compute the stack size and movm arguments for the function
2229    referred to by HASH at address ADDR in section with
2230    contents CONTENTS, store the information in the hash table.  */
2231 static void
2232 compute_function_info (abfd, hash, addr, contents)
2233      bfd *abfd;
2234      struct elf32_mn10300_link_hash_entry *hash;
2235      bfd_vma addr;
2236      unsigned char *contents;
2237 {
2238   unsigned char byte1, byte2;
2239   /* We only care about a very small subset of the possible prologue
2240      sequences here.  Basically we look for:
2241
2242      movm [d2,d3,a2,a3],sp (optional)
2243      add <size>,sp (optional, and only for sizes which fit in an unsigned
2244                     8 bit number)
2245
2246      If we find anything else, we quit.  */
2247
2248   /* Look for movm [regs],sp */
2249   byte1 = bfd_get_8 (abfd, contents + addr);
2250   byte2 = bfd_get_8 (abfd, contents + addr + 1);
2251
2252   if (byte1 == 0xcf)
2253     {
2254       hash->movm_args = byte2;
2255       addr += 2;
2256       byte1 = bfd_get_8 (abfd, contents + addr);
2257       byte2 = bfd_get_8 (abfd, contents + addr + 1);
2258     }
2259
2260   /* Now look for the two stack adjustment variants.  */
2261   if (byte1 == 0xf8 && byte2 == 0xfe)
2262     {
2263       int temp = bfd_get_8 (abfd, contents + addr + 2);
2264       temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2265
2266       hash->stack_size = -temp;
2267     }
2268   else if (byte1 == 0xfa && byte2 == 0xfe)
2269     {
2270       int temp = bfd_get_16 (abfd, contents + addr + 2);
2271       temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2272       temp = -temp;
2273
2274       if (temp <= 255)
2275         hash->stack_size = temp;
2276     }
2277   return;
2278 }
2279
2280 /* Delete some bytes from a section while relaxing.  */
2281
2282 static boolean
2283 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2284      bfd *abfd;
2285      asection *sec;
2286      bfd_vma addr;
2287      int count;
2288 {
2289   Elf_Internal_Shdr *symtab_hdr;
2290   Elf32_External_Sym *extsyms;
2291   int shndx, index;
2292   bfd_byte *contents;
2293   Elf_Internal_Rela *irel, *irelend;
2294   Elf_Internal_Rela *irelalign;
2295   bfd_vma toaddr;
2296   Elf32_External_Sym *esym, *esymend;
2297   struct elf32_mn10300_link_hash_entry *sym_hash;
2298
2299   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2300   extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2301
2302   shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2303
2304   contents = elf_section_data (sec)->this_hdr.contents;
2305
2306   /* The deletion must stop at the next ALIGN reloc for an aligment
2307      power larger than the number of bytes we are deleting.  */
2308
2309   irelalign = NULL;
2310   toaddr = sec->_cooked_size;
2311
2312   irel = elf_section_data (sec)->relocs;
2313   irelend = irel + sec->reloc_count;
2314
2315   /* Actually delete the bytes.  */
2316   memmove (contents + addr, contents + addr + count, toaddr - addr - count);
2317   sec->_cooked_size -= count;
2318
2319   /* Adjust all the relocs.  */
2320   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2321     {
2322       /* Get the new reloc address.  */
2323       if ((irel->r_offset > addr
2324            && irel->r_offset < toaddr))
2325         irel->r_offset -= count;
2326     }
2327
2328   /* Adjust the local symbols defined in this section.  */
2329   esym = extsyms;
2330   esymend = esym + symtab_hdr->sh_info;
2331   for (; esym < esymend; esym++)
2332     {
2333       Elf_Internal_Sym isym;
2334
2335       bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2336
2337       if (isym.st_shndx == shndx
2338           && isym.st_value > addr
2339           && isym.st_value < toaddr)
2340         {
2341           isym.st_value -= count;
2342           bfd_elf32_swap_symbol_out (abfd, &isym, esym);
2343         }
2344     }
2345
2346   /* Now adjust the global symbols defined in this section.  */
2347   esym = extsyms + symtab_hdr->sh_info;
2348   esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2349   for (index = 0; esym < esymend; esym++, index++)
2350     {
2351       Elf_Internal_Sym isym;
2352
2353       bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2354       sym_hash = (struct elf32_mn10300_link_hash_entry *)
2355                    (elf_sym_hashes (abfd)[index]);
2356       if (isym.st_shndx == shndx
2357           && ((sym_hash)->root.root.type == bfd_link_hash_defined
2358               || (sym_hash)->root.root.type == bfd_link_hash_defweak)
2359           && (sym_hash)->root.root.u.def.section == sec
2360           && (sym_hash)->root.root.u.def.value > addr
2361           && (sym_hash)->root.root.u.def.value < toaddr)
2362         {
2363           (sym_hash)->root.root.u.def.value -= count;
2364         }
2365     }
2366
2367   return true;
2368 }
2369
2370 /* Return true if a symbol exists at the given address, else return
2371    false.  */
2372 static boolean
2373 mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr)
2374      bfd *abfd;
2375      asection *sec;
2376      Elf32_External_Sym *extsyms;
2377      bfd_vma addr;
2378 {
2379   Elf_Internal_Shdr *symtab_hdr;
2380   int shndx;
2381   Elf32_External_Sym *esym, *esymend;
2382   struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end;
2383
2384   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2385   shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2386
2387   /* Examine all the symbols.  */
2388   esym = extsyms;
2389   esymend = esym + symtab_hdr->sh_info;
2390   for (; esym < esymend; esym++)
2391     {
2392       Elf_Internal_Sym isym;
2393
2394       bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2395
2396       if (isym.st_shndx == shndx
2397           && isym.st_value == addr)
2398         return true;
2399     }
2400
2401   sym_hash = (struct elf32_mn10300_link_hash_entry **)(elf_sym_hashes (abfd));
2402   sym_hash_end = (sym_hash
2403                   + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2404                      - symtab_hdr->sh_info));
2405   for (; sym_hash < sym_hash_end; sym_hash++)
2406     {
2407       if (((*sym_hash)->root.root.type == bfd_link_hash_defined
2408            || (*sym_hash)->root.root.type == bfd_link_hash_defweak)
2409           && (*sym_hash)->root.root.u.def.section == sec
2410           && (*sym_hash)->root.root.u.def.value == addr)
2411         return true;
2412     }
2413   return false;
2414 }
2415
2416 /* This is a version of bfd_generic_get_relocated_section_contents
2417    which uses mn10300_elf_relocate_section.  */
2418
2419 static bfd_byte *
2420 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2421                                             data, relocateable, symbols)
2422      bfd *output_bfd;
2423      struct bfd_link_info *link_info;
2424      struct bfd_link_order *link_order;
2425      bfd_byte *data;
2426      boolean relocateable;
2427      asymbol **symbols;
2428 {
2429   Elf_Internal_Shdr *symtab_hdr;
2430   asection *input_section = link_order->u.indirect.section;
2431   bfd *input_bfd = input_section->owner;
2432   asection **sections = NULL;
2433   Elf_Internal_Rela *internal_relocs = NULL;
2434   Elf32_External_Sym *external_syms = NULL;
2435   Elf_Internal_Sym *internal_syms = NULL;
2436
2437   /* We only need to handle the case of relaxing, or of having a
2438      particular set of section contents, specially.  */
2439   if (relocateable
2440       || elf_section_data (input_section)->this_hdr.contents == NULL)
2441     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2442                                                        link_order, data,
2443                                                        relocateable,
2444                                                        symbols);
2445
2446   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2447
2448   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2449           input_section->_raw_size);
2450
2451   if ((input_section->flags & SEC_RELOC) != 0
2452       && input_section->reloc_count > 0)
2453     {
2454       Elf_Internal_Sym *isymp;
2455       asection **secpp;
2456       Elf32_External_Sym *esym, *esymend;
2457
2458       if (symtab_hdr->contents != NULL)
2459         external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2460       else
2461         {
2462           external_syms = ((Elf32_External_Sym *)
2463                            bfd_malloc (symtab_hdr->sh_info
2464                                        * sizeof (Elf32_External_Sym)));
2465           if (external_syms == NULL && symtab_hdr->sh_info > 0)
2466             goto error_return;
2467           if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2468               || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
2469                             symtab_hdr->sh_info, input_bfd)
2470                   != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2471             goto error_return;
2472         }
2473
2474       internal_relocs = (_bfd_elf32_link_read_relocs
2475                          (input_bfd, input_section, (PTR) NULL,
2476                           (Elf_Internal_Rela *) NULL, false));
2477       if (internal_relocs == NULL)
2478         goto error_return;
2479
2480       internal_syms = ((Elf_Internal_Sym *)
2481                        bfd_malloc (symtab_hdr->sh_info
2482                                    * sizeof (Elf_Internal_Sym)));
2483       if (internal_syms == NULL && symtab_hdr->sh_info > 0)
2484         goto error_return;
2485
2486       sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2487                                            * sizeof (asection *));
2488       if (sections == NULL && symtab_hdr->sh_info > 0)
2489         goto error_return;
2490
2491       isymp = internal_syms;
2492       secpp = sections;
2493       esym = external_syms;
2494       esymend = esym + symtab_hdr->sh_info;
2495       for (; esym < esymend; ++esym, ++isymp, ++secpp)
2496         {
2497           asection *isec;
2498
2499           bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2500
2501           if (isymp->st_shndx == SHN_UNDEF)
2502             isec = bfd_und_section_ptr;
2503           else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2504             isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2505           else if (isymp->st_shndx == SHN_ABS)
2506             isec = bfd_abs_section_ptr;
2507           else if (isymp->st_shndx == SHN_COMMON)
2508             isec = bfd_com_section_ptr;
2509           else
2510             {
2511               /* Who knows?  */
2512               isec = NULL;
2513             }
2514
2515           *secpp = isec;
2516         }
2517
2518       if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2519                                      input_section, data, internal_relocs,
2520                                      internal_syms, sections))
2521         goto error_return;
2522
2523       if (sections != NULL)
2524         free (sections);
2525       sections = NULL;
2526       if (internal_syms != NULL)
2527         free (internal_syms);
2528       internal_syms = NULL;
2529       if (external_syms != NULL && symtab_hdr->contents == NULL)
2530         free (external_syms);
2531       external_syms = NULL;
2532       if (internal_relocs != elf_section_data (input_section)->relocs)
2533         free (internal_relocs);
2534       internal_relocs = NULL;
2535     }
2536
2537   return data;
2538
2539  error_return:
2540   if (internal_relocs != NULL
2541       && internal_relocs != elf_section_data (input_section)->relocs)
2542     free (internal_relocs);
2543   if (external_syms != NULL && symtab_hdr->contents == NULL)
2544     free (external_syms);
2545   if (internal_syms != NULL)
2546     free (internal_syms);
2547   if (sections != NULL)
2548     free (sections);
2549   return NULL;
2550 }
2551
2552 /* Assorted hash table functions.  */
2553
2554 /* Initialize an entry in the link hash table.  */
2555
2556 /* Create an entry in an MN10300 ELF linker hash table.  */
2557
2558 static struct bfd_hash_entry *
2559 elf32_mn10300_link_hash_newfunc (entry, table, string)
2560      struct bfd_hash_entry *entry;
2561      struct bfd_hash_table *table;
2562      const char *string;
2563 {
2564   struct elf32_mn10300_link_hash_entry *ret =
2565     (struct elf32_mn10300_link_hash_entry *) entry;
2566
2567   /* Allocate the structure if it has not already been allocated by a
2568      subclass.  */
2569   if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2570     ret = ((struct elf32_mn10300_link_hash_entry *)
2571            bfd_hash_allocate (table,
2572                               sizeof (struct elf32_mn10300_link_hash_entry)));
2573   if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2574     return (struct bfd_hash_entry *) ret;
2575
2576   /* Call the allocation method of the superclass.  */
2577   ret = ((struct elf32_mn10300_link_hash_entry *)
2578          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2579                                      table, string));
2580   if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2581     {
2582       ret->direct_calls = 0;
2583       ret->stack_size = 0;
2584       ret->flags = 0;
2585       ret->movm_args = 0;
2586     }
2587
2588   return (struct bfd_hash_entry *) ret;
2589 }
2590
2591 /* Create an mn10300 ELF linker hash table.  */
2592
2593 static struct bfd_link_hash_table *
2594 elf32_mn10300_link_hash_table_create (abfd)
2595      bfd *abfd;
2596 {
2597   struct elf32_mn10300_link_hash_table *ret;
2598
2599   ret = ((struct elf32_mn10300_link_hash_table *)
2600          bfd_alloc (abfd, sizeof (struct elf32_mn10300_link_hash_table)));
2601   if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2602     return NULL;
2603
2604   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2605                                        elf32_mn10300_link_hash_newfunc))
2606     {
2607       bfd_release (abfd, ret);
2608       return NULL;
2609     }
2610
2611   ret->flags = 0;
2612   ret->static_hash_table
2613     = ((struct elf32_mn10300_link_hash_table *)
2614        bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
2615   if (ret->static_hash_table == NULL)
2616     {
2617       bfd_release (abfd, ret);
2618       return NULL;
2619     }
2620
2621   if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2622                                        elf32_mn10300_link_hash_newfunc))
2623     {
2624       bfd_release (abfd, ret->static_hash_table);
2625       bfd_release (abfd, ret);
2626       return NULL;
2627     }
2628   return &ret->root.root;
2629 }
2630
2631 static int
2632 elf_mn10300_mach (flags)
2633      flagword flags;
2634 {
2635   switch (flags & EF_MN10300_MACH)
2636     {
2637       case E_MN10300_MACH_MN10300:
2638       default:
2639         return bfd_mach_mn10300;
2640
2641       /* start-sanitize-am33 */
2642       case E_MN10300_MACH_AM33:
2643         return bfd_mach_am33;
2644       /* end-sanitize-am33 */
2645     }
2646 }
2647
2648 /* The final processing done just before writing out a MN10300 ELF object
2649    file.  This gets the MN10300 architecture right based on the machine
2650    number.  */
2651
2652 /*ARGSUSED*/
2653 void
2654 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2655      bfd *abfd;
2656      boolean linker;
2657 {
2658   unsigned long val;
2659   unsigned int i;
2660   Elf_Internal_Shdr **hdrpp;
2661   const char *name;
2662   asection *sec;
2663
2664   switch (bfd_get_mach (abfd))
2665     {
2666       default:
2667       case bfd_mach_mn10300:
2668         val = E_MN10300_MACH_MN10300;
2669         break;
2670
2671       /* start-sanitize-am33 */
2672       case bfd_mach_am33:
2673         val = E_MN10300_MACH_AM33;
2674         break;
2675       /* end-sanitize-am33 */
2676     }
2677
2678   elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2679   elf_elfheader (abfd)->e_flags |= val;
2680 }
2681
2682 boolean
2683 _bfd_mn10300_elf_object_p (abfd)
2684      bfd *abfd;
2685 {
2686   bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2687                              elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2688   return true;
2689 }
2690
2691 /* Merge backend specific data from an object file to the output
2692    object file when linking.  */
2693
2694 boolean
2695 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2696      bfd *ibfd;
2697      bfd *obfd;
2698 {
2699   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2700       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2701     return true;
2702
2703   if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2704       && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2705     {
2706       if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2707                                bfd_get_mach (ibfd)))
2708         return false;
2709     }
2710
2711   return true;
2712 }
2713
2714
2715 #define TARGET_LITTLE_SYM       bfd_elf32_mn10300_vec
2716 #define TARGET_LITTLE_NAME      "elf32-mn10300"
2717 #define ELF_ARCH                bfd_arch_mn10300
2718 #define ELF_MACHINE_CODE        EM_CYGNUS_MN10300
2719 #define ELF_MAXPAGESIZE         0x1000
2720
2721 #define elf_info_to_howto               mn10300_info_to_howto
2722 #define elf_info_to_howto_rel           0
2723 #define elf_backend_can_gc_sections     1
2724 #define elf_backend_check_relocs        mn10300_elf_check_relocs
2725 #define elf_backend_gc_mark_hook        mn10300_elf_gc_mark_hook
2726 #define elf_backend_relocate_section    mn10300_elf_relocate_section
2727 #define bfd_elf32_bfd_relax_section     mn10300_elf_relax_section
2728 #define bfd_elf32_bfd_get_relocated_section_contents \
2729                                 mn10300_elf_get_relocated_section_contents
2730 #define bfd_elf32_bfd_link_hash_table_create \
2731                                 elf32_mn10300_link_hash_table_create
2732
2733 #define elf_symbol_leading_char '_'
2734
2735 /* So we can set bits in e_flags.  */
2736 #define elf_backend_final_write_processing \
2737                                         _bfd_mn10300_elf_final_write_processing
2738 #define elf_backend_object_p            _bfd_mn10300_elf_object_p
2739
2740 #define bfd_elf32_bfd_merge_private_bfd_data \
2741                                         _bfd_mn10300_elf_merge_private_bfd_data
2742
2743
2744 #include "elf32-target.h"