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