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