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