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