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