elf_swap_symbol_in args should be "const PTR", not "const PTR *".
[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,
941                                                     (const PTR) esym,
942                                                     (const PTR) shndx,
943                                                     &isym);
944
945                           if (isym.st_shndx == SHN_UNDEF)
946                             sym_sec = bfd_und_section_ptr;
947                           else if (isym.st_shndx == SHN_ABS)
948                             sym_sec = bfd_abs_section_ptr;
949                           else if (isym.st_shndx == SHN_COMMON)
950                             sym_sec = bfd_com_section_ptr;
951                           else
952                             sym_sec
953                               = bfd_section_from_elf_index (input_bfd,
954                                                             isym.st_shndx);
955
956                           sym_name
957                             = bfd_elf_string_from_elf_section (input_bfd,
958                                                                (symtab_hdr
959                                                                 ->sh_link),
960                                                                isym.st_name);
961
962                           /* If it isn't a function, then we don't care
963                              about it.  */
964                           if (r_index < symtab_hdr->sh_info
965                               && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
966                             continue;
967
968                           /* Tack on an ID so we can uniquely identify this
969                              local symbol in the global hash table.  */
970                           amt = strlen (sym_name) + 10;
971                           new_name = bfd_malloc (amt);
972                           if (new_name == 0)
973                             goto error_return;
974
975                           sprintf (new_name, "%s_%08x",
976                                    sym_name, (int) sym_sec);
977                           sym_name = new_name;
978
979                           elftab = &hash_table->static_hash_table->root;
980                           hash = ((struct elf32_mn10300_link_hash_entry *)
981                                   elf_link_hash_lookup (elftab, sym_name,
982                                                         true, true, false));
983                           free (new_name);
984                         }
985                       else
986                         {
987                           r_index -= symtab_hdr->sh_info;
988                           hash = (struct elf32_mn10300_link_hash_entry *)
989                                    elf_sym_hashes (input_bfd)[r_index];
990                         }
991
992                       /* If this is not a "call" instruction, then we
993                          should convert "call" instructions to "calls"
994                          instructions.  */
995                       code = bfd_get_8 (input_bfd,
996                                         contents + irel->r_offset - 1);
997                       if (code != 0xdd && code != 0xcd)
998                         hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
999
1000                       /* If this is a jump/call, then bump the direct_calls
1001                          counter.  Else force "call" to "calls" conversions.  */
1002                       if (r_type == R_MN10300_PCREL32
1003                           || r_type == R_MN10300_PCREL16)
1004                         hash->direct_calls++;
1005                       else
1006                         hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1007                     }
1008                 }
1009
1010               /* Now look at the actual contents to get the stack size,
1011                  and a list of what registers were saved in the prologue
1012                  (ie movm_args).  */
1013               if ((section->flags & SEC_CODE) != 0)
1014                 {
1015
1016                   Elf32_External_Sym *esym, *esymend;
1017                   Elf_External_Sym_Shndx *shndx;
1018                   int idx;
1019                   unsigned int sec_shndx;
1020
1021                   sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1022                                                                  section);
1023
1024                   /* Look at each function defined in this section and
1025                      update info for that function.  */
1026                   for (esym = extsyms, esymend = esym + symtab_hdr->sh_info,
1027                          shndx = shndx_buf;
1028                        esym < esymend;
1029                        esym++, shndx = (shndx ? shndx + 1 : NULL))
1030                     {
1031                       Elf_Internal_Sym isym;
1032
1033                       bfd_elf32_swap_symbol_in (input_bfd, (const PTR) esym,
1034                                                 (const PTR) shndx, &isym);
1035                       if (isym.st_shndx == sec_shndx
1036                           && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1037                         {
1038                           struct elf_link_hash_table *elftab;
1039                           bfd_size_type amt;
1040
1041                           if (isym.st_shndx == SHN_UNDEF)
1042                             sym_sec = bfd_und_section_ptr;
1043                           else if (isym.st_shndx == SHN_ABS)
1044                             sym_sec = bfd_abs_section_ptr;
1045                           else if (isym.st_shndx == SHN_COMMON)
1046                             sym_sec = bfd_com_section_ptr;
1047                           else
1048                             sym_sec
1049                               = bfd_section_from_elf_index (input_bfd,
1050                                                             isym.st_shndx);
1051
1052                           sym_name = (bfd_elf_string_from_elf_section
1053                                       (input_bfd, symtab_hdr->sh_link,
1054                                        isym.st_name));
1055
1056                           /* Tack on an ID so we can uniquely identify this
1057                              local symbol in the global hash table.  */
1058                           amt = strlen (sym_name) + 10;
1059                           new_name = bfd_malloc (amt);
1060                           if (new_name == 0)
1061                             goto error_return;
1062
1063                           sprintf (new_name, "%s_%08x",
1064                                    sym_name, (int) sym_sec);
1065                           sym_name = new_name;
1066
1067                           elftab = &hash_table->static_hash_table->root;
1068                           hash = ((struct elf32_mn10300_link_hash_entry *)
1069                                   elf_link_hash_lookup (elftab, sym_name,
1070                                                         true, true, false));
1071                           free (new_name);
1072                           compute_function_info (input_bfd, hash,
1073                                                  isym.st_value, contents);
1074                         }
1075                     }
1076
1077                   esym = extsyms + symtab_hdr->sh_info;
1078                   esymend = extsyms + (symtab_hdr->sh_size
1079                                        / sizeof (Elf32_External_Sym));
1080                   for (idx = 0; esym < esymend; esym++, idx++)
1081                     {
1082                       Elf_Internal_Sym isym;
1083
1084                       hash = (struct elf32_mn10300_link_hash_entry *)
1085                                elf_sym_hashes (input_bfd)[idx];
1086                       if ((hash->root.root.type == bfd_link_hash_defined
1087                            || hash->root.root.type == bfd_link_hash_defweak)
1088                           && hash->root.root.u.def.section == section
1089                           && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1090                         compute_function_info (input_bfd, hash,
1091                                                (hash)->root.root.u.def.value,
1092                                                contents);
1093                     }
1094                 }
1095
1096               /* Cache or free any memory we allocated for the relocs.  */
1097               if (free_relocs != NULL)
1098                 {
1099                   free (free_relocs);
1100                   free_relocs = NULL;
1101                 }
1102
1103               /* Cache or free any memory we allocated for the contents.  */
1104               if (free_contents != NULL)
1105                 {
1106                   if (! link_info->keep_memory)
1107                     free (free_contents);
1108                   else
1109                     {
1110                       /* Cache the section contents for elf_link_input_bfd.  */
1111                       elf_section_data (section)->this_hdr.contents = contents;
1112                     }
1113                   free_contents = NULL;
1114                 }
1115             }
1116
1117           if (shndx_buf != NULL)
1118             {
1119               free (shndx_buf);
1120               shndx_buf = NULL;
1121             }
1122
1123           /* Cache or free any memory we allocated for the symbols.  */
1124           if (free_extsyms != NULL)
1125             {
1126               if (! link_info->keep_memory)
1127                 free (free_extsyms);
1128               else
1129                 {
1130                   /* Cache the symbols for elf_link_input_bfd.  */
1131                   symtab_hdr->contents = (unsigned char *) extsyms;
1132                 }
1133               free_extsyms = NULL;
1134             }
1135         }
1136
1137       /* Now iterate on each symbol in the hash table and perform
1138          the final initialization steps on each.  */
1139       elf32_mn10300_link_hash_traverse (hash_table,
1140                                         elf32_mn10300_finish_hash_table_entry,
1141                                         NULL);
1142       elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1143                                         elf32_mn10300_finish_hash_table_entry,
1144                                         NULL);
1145
1146       /* All entries in the hash table are fully initialized.  */
1147       hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1148
1149       /* Now that everything has been initialized, go through each
1150          code section and delete any prologue insns which will be
1151          redundant because their operations will be performed by
1152          a "call" instruction.  */
1153       for (input_bfd = link_info->input_bfds;
1154            input_bfd != NULL;
1155            input_bfd = input_bfd->link_next)
1156         {
1157           asection *section;
1158
1159           /* We're going to need all the local symbols for each bfd.  */
1160           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1161           shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
1162
1163           /* Get cached copy if it exists.  */
1164           if (symtab_hdr->contents != NULL)
1165             extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1166           else
1167             {
1168               /* Go get them off disk.  */
1169               bfd_size_type amt;
1170
1171               amt = symtab_hdr->sh_info;
1172               amt *= sizeof (Elf32_External_Sym);
1173               extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
1174               if (extsyms == NULL)
1175                 goto error_return;
1176               free_extsyms = extsyms;
1177               if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1178                   || bfd_bread ((PTR) extsyms, amt, input_bfd) != amt)
1179                 goto error_return;
1180               symtab_hdr->contents = (bfd_byte *) extsyms;
1181             }
1182
1183           if (shndx_hdr->sh_size != 0)
1184             {
1185               bfd_size_type amt;
1186
1187               amt = symtab_hdr->sh_info;
1188               amt *= sizeof (Elf_External_Sym_Shndx);
1189               shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1190               if (shndx_buf == NULL)
1191                 goto error_return;
1192               if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1193                   || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
1194                 goto error_return;
1195               shndx_hdr->contents = (bfd_byte *) shndx_buf;
1196             }
1197
1198           /* Walk over each section in this bfd.  */
1199           for (section = input_bfd->sections;
1200                section != NULL;
1201                section = section->next)
1202             {
1203               unsigned int sec_shndx;
1204               Elf32_External_Sym *esym, *esymend;
1205               Elf_External_Sym_Shndx *shndx;
1206               unsigned int idx;
1207
1208               /* Skip non-code sections and empty sections.  */
1209               if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1210                 continue;
1211
1212               if (section->reloc_count != 0)
1213                 {
1214                   /* Get a copy of the native relocations.  */
1215                   internal_relocs = (_bfd_elf32_link_read_relocs
1216                                      (input_bfd, section, (PTR) NULL,
1217                                       (Elf_Internal_Rela *) NULL,
1218                                       link_info->keep_memory));
1219                   if (internal_relocs == NULL)
1220                     goto error_return;
1221                   if (! link_info->keep_memory)
1222                     free_relocs = internal_relocs;
1223                 }
1224
1225               /* Get cached copy of section contents if it exists.  */
1226               if (elf_section_data (section)->this_hdr.contents != NULL)
1227                 contents = elf_section_data (section)->this_hdr.contents;
1228               else
1229                 {
1230                   /* Go get them off disk.  */
1231                   contents = (bfd_byte *) bfd_malloc (section->_raw_size);
1232                   if (contents == NULL)
1233                     goto error_return;
1234                   free_contents = contents;
1235
1236                   if (!bfd_get_section_contents (input_bfd, section,
1237                                                  contents, (file_ptr) 0,
1238                                                  section->_raw_size))
1239                     goto error_return;
1240                 }
1241
1242               sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1243                                                              section);
1244
1245               /* Now look for any function in this section which needs
1246                  insns deleted from its prologue.  */
1247               for (esym = extsyms, esymend = esym + symtab_hdr->sh_info,
1248                      shndx = shndx_buf;
1249                    esym < esymend;
1250                    esym++, shndx = (shndx ? shndx + 1 : NULL))
1251                 {
1252                   Elf_Internal_Sym isym;
1253                   struct elf32_mn10300_link_hash_entry *sym_hash;
1254                   asection *sym_sec = NULL;
1255                   const char *sym_name;
1256                   char *new_name;
1257                   struct elf_link_hash_table *elftab;
1258                   bfd_size_type amt;
1259
1260                   bfd_elf32_swap_symbol_in (input_bfd, (const PTR) esym,
1261                                             (const PTR) shndx, &isym);
1262
1263                   if (isym.st_shndx != sec_shndx)
1264                     continue;
1265
1266                   if (isym.st_shndx == SHN_UNDEF)
1267                     sym_sec = bfd_und_section_ptr;
1268                   else if (isym.st_shndx == SHN_ABS)
1269                     sym_sec = bfd_abs_section_ptr;
1270                   else if (isym.st_shndx == SHN_COMMON)
1271                     sym_sec = bfd_com_section_ptr;
1272                   else
1273                     sym_sec
1274                       = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1275
1276                   sym_name
1277                     = bfd_elf_string_from_elf_section (input_bfd,
1278                                                        symtab_hdr->sh_link,
1279                                                        isym.st_name);
1280
1281                   /* Tack on an ID so we can uniquely identify this
1282                      local symbol in the global hash table.  */
1283                   amt = strlen (sym_name) + 10;
1284                   new_name = bfd_malloc (amt);
1285                   if (new_name == 0)
1286                     goto error_return;
1287                   sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1288                   sym_name = new_name;
1289
1290                   elftab = &hash_table->static_hash_table->root;
1291                   sym_hash = ((struct elf32_mn10300_link_hash_entry *)
1292                               elf_link_hash_lookup (elftab, sym_name,
1293                                                     false, false, false));
1294
1295                   free (new_name);
1296                   if (sym_hash == NULL)
1297                     continue;
1298
1299                   if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1300                       && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1301                     {
1302                       int bytes = 0;
1303
1304                       /* Note that we've changed things.  */
1305                       elf_section_data (section)->relocs = internal_relocs;
1306                       free_relocs = NULL;
1307
1308                       elf_section_data (section)->this_hdr.contents = contents;
1309                       free_contents = NULL;
1310
1311                       free_extsyms = NULL;
1312
1313                       /* Count how many bytes we're going to delete.  */
1314                       if (sym_hash->movm_args)
1315                         bytes += 2;
1316
1317                       if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1318                         bytes += 3;
1319                       else if (sym_hash->stack_size
1320                                && sym_hash->stack_size < 256)
1321                         bytes += 4;
1322
1323                       /* Note that we've deleted prologue bytes for this
1324                          function.  */
1325                       sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1326
1327                       /* Actually delete the bytes.  */
1328                       if (!mn10300_elf_relax_delete_bytes (input_bfd,
1329                                                            section,
1330                                                            isym.st_value,
1331                                                            bytes))
1332                         goto error_return;
1333
1334                       /* Something changed.  Not strictly necessary, but
1335                          may lead to more relaxing opportunities.  */
1336                       *again = true;
1337                     }
1338                 }
1339
1340               /* Look for any global functions in this section which
1341                  need insns deleted from their prologues.  */
1342               for (idx = 0;
1343                    idx < (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1344                           - symtab_hdr->sh_info);
1345                    idx++)
1346                 {
1347                   struct elf32_mn10300_link_hash_entry *sym_hash;
1348
1349                   sym_hash = (struct elf32_mn10300_link_hash_entry *)
1350                                (elf_sym_hashes (input_bfd)[idx]);
1351                   if ((sym_hash->root.root.type == bfd_link_hash_defined
1352                        || sym_hash->root.root.type == bfd_link_hash_defweak)
1353                       && sym_hash->root.root.u.def.section == section
1354                       && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1355                       && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1356                     {
1357                       int bytes = 0;
1358                       bfd_vma symval;
1359
1360                       /* Note that we've changed things.  */
1361                       elf_section_data (section)->relocs = internal_relocs;
1362                       free_relocs = NULL;
1363
1364                       elf_section_data (section)->this_hdr.contents = contents;
1365                       free_contents = NULL;
1366
1367                       free_extsyms = NULL;
1368
1369                       /* Count how many bytes we're going to delete.  */
1370                       if (sym_hash->movm_args)
1371                         bytes += 2;
1372
1373                       if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1374                         bytes += 3;
1375                       else if (sym_hash->stack_size
1376                                && sym_hash->stack_size < 256)
1377                         bytes += 4;
1378
1379                       /* Note that we've deleted prologue bytes for this
1380                          function.  */
1381                       sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1382
1383                       /* Actually delete the bytes.  */
1384                       symval = sym_hash->root.root.u.def.value;
1385                       if (!mn10300_elf_relax_delete_bytes (input_bfd,
1386                                                            section,
1387                                                            symval,
1388                                                            bytes))
1389                         goto error_return;
1390
1391                       /* Something changed.  Not strictly necessary, but
1392                          may lead to more relaxing opportunities.  */
1393                       *again = true;
1394                     }
1395                 }
1396
1397               /* Cache or free any memory we allocated for the relocs.  */
1398               if (free_relocs != NULL)
1399                 {
1400                   free (free_relocs);
1401                   free_relocs = NULL;
1402                 }
1403
1404               /* Cache or free any memory we allocated for the contents.  */
1405               if (free_contents != NULL)
1406                 {
1407                   if (! link_info->keep_memory)
1408                     free (free_contents);
1409                   else
1410                     {
1411                       /* Cache the section contents for elf_link_input_bfd.  */
1412                       elf_section_data (section)->this_hdr.contents = contents;
1413                     }
1414                   free_contents = NULL;
1415                 }
1416             }
1417
1418           if (shndx_buf != NULL)
1419             {
1420               shndx_hdr->contents = NULL;
1421               free (shndx_buf);
1422               shndx_buf = NULL;
1423             }
1424
1425           /* Cache or free any memory we allocated for the symbols.  */
1426           if (free_extsyms != NULL)
1427             {
1428               if (! link_info->keep_memory)
1429                 {
1430                   symtab_hdr->contents = NULL;
1431                   free (free_extsyms);
1432                 }
1433               free_extsyms = NULL;
1434             }
1435         }
1436     }
1437
1438   /* (Re)initialize for the basic instruction shortening/relaxing pass.  */
1439   contents = NULL;
1440   extsyms = NULL;
1441   internal_relocs = NULL;
1442   free_relocs = NULL;
1443   free_contents = NULL;
1444   free_extsyms = NULL;
1445
1446   /* We don't have to do anything for a relocateable link, if
1447      this section does not have relocs, or if this is not a
1448      code section.  */
1449   if (link_info->relocateable
1450       || (sec->flags & SEC_RELOC) == 0
1451       || sec->reloc_count == 0
1452       || (sec->flags & SEC_CODE) == 0)
1453     return true;
1454
1455   /* If this is the first time we have been called for this section,
1456      initialize the cooked size.  */
1457   if (sec->_cooked_size == 0)
1458     sec->_cooked_size = sec->_raw_size;
1459
1460   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1461   shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1462
1463   /* Get a copy of the native relocations.  */
1464   internal_relocs = (_bfd_elf32_link_read_relocs
1465                      (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1466                       link_info->keep_memory));
1467   if (internal_relocs == NULL)
1468     goto error_return;
1469   if (! link_info->keep_memory)
1470     free_relocs = internal_relocs;
1471
1472   /* Walk through them looking for relaxing opportunities.  */
1473   irelend = internal_relocs + sec->reloc_count;
1474   for (irel = internal_relocs; irel < irelend; irel++)
1475     {
1476       bfd_vma symval;
1477       struct elf32_mn10300_link_hash_entry *h = NULL;
1478
1479       /* If this isn't something that can be relaxed, then ignore
1480          this reloc.  */
1481       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1482           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1483           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1484         continue;
1485
1486       /* Get the section contents if we haven't done so already.  */
1487       if (contents == NULL)
1488         {
1489           /* Get cached copy if it exists.  */
1490           if (elf_section_data (sec)->this_hdr.contents != NULL)
1491             contents = elf_section_data (sec)->this_hdr.contents;
1492           else
1493             {
1494               /* Go get them off disk.  */
1495               contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1496               if (contents == NULL)
1497                 goto error_return;
1498               free_contents = contents;
1499
1500               if (! bfd_get_section_contents (abfd, sec, contents,
1501                                               (file_ptr) 0, sec->_raw_size))
1502                 goto error_return;
1503             }
1504         }
1505
1506       /* Read this BFD's symbols if we haven't done so already.  */
1507       if (extsyms == NULL)
1508         {
1509           /* Get cached copy if it exists.  */
1510           if (symtab_hdr->contents != NULL)
1511             extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1512           else
1513             {
1514               /* Go get them off disk.  */
1515               bfd_size_type amt;
1516
1517               amt = symtab_hdr->sh_info;
1518               amt *= sizeof (Elf32_External_Sym);
1519               extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
1520               if (extsyms == NULL)
1521                 goto error_return;
1522               free_extsyms = extsyms;
1523               if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1524                   || bfd_bread ((PTR) extsyms, amt, abfd) != amt)
1525                 goto error_return;
1526               symtab_hdr->contents = (bfd_byte *) extsyms;
1527             }
1528
1529           if (shndx_hdr->sh_size != 0)
1530             {
1531               bfd_size_type amt;
1532
1533               amt = symtab_hdr->sh_info;
1534               amt *= sizeof (Elf_External_Sym_Shndx);
1535               shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1536               if (shndx_buf == NULL)
1537                 goto error_return;
1538               if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1539                   || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1540                 goto error_return;
1541               shndx_hdr->contents = (bfd_byte *) shndx_buf;
1542             }
1543         }
1544
1545       /* Get the value of the symbol referred to by the reloc.  */
1546       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1547         {
1548           Elf32_External_Sym *esym;
1549           Elf_External_Sym_Shndx *shndx;
1550           Elf_Internal_Sym isym;
1551           asection *sym_sec = NULL;
1552           const char *sym_name;
1553           char *new_name;
1554
1555           /* A local symbol.  */
1556           esym = extsyms + ELF32_R_SYM (irel->r_info);
1557           shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (irel->r_info) : 0);
1558           bfd_elf32_swap_symbol_in (abfd, (const PTR) esym,
1559                                     (const PTR) shndx, &isym);
1560
1561           if (isym.st_shndx == SHN_UNDEF)
1562             sym_sec = bfd_und_section_ptr;
1563           else if (isym.st_shndx == SHN_ABS)
1564             sym_sec = bfd_abs_section_ptr;
1565           else if (isym.st_shndx == SHN_COMMON)
1566             sym_sec = bfd_com_section_ptr;
1567           else
1568             sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1569
1570           symval = (isym.st_value
1571                     + sym_sec->output_section->vma
1572                     + sym_sec->output_offset);
1573           sym_name = bfd_elf_string_from_elf_section (abfd,
1574                                                       symtab_hdr->sh_link,
1575                                                       isym.st_name);
1576
1577           /* Tack on an ID so we can uniquely identify this
1578              local symbol in the global hash table.  */
1579           new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
1580           if (new_name == 0)
1581             goto error_return;
1582           sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1583           sym_name = new_name;
1584
1585           h = (struct elf32_mn10300_link_hash_entry *)
1586                 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1587                                       sym_name, false, false, false);
1588           free (new_name);
1589         }
1590       else
1591         {
1592           unsigned long indx;
1593
1594           /* An external symbol.  */
1595           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1596           h = (struct elf32_mn10300_link_hash_entry *)
1597                 (elf_sym_hashes (abfd)[indx]);
1598           BFD_ASSERT (h != NULL);
1599           if (h->root.root.type != bfd_link_hash_defined
1600               && h->root.root.type != bfd_link_hash_defweak)
1601             {
1602               /* This appears to be a reference to an undefined
1603                 symbol.  Just ignore it--it will be caught by the
1604                 regular reloc processing.  */
1605               continue;
1606             }
1607
1608           symval = (h->root.root.u.def.value
1609                     + h->root.root.u.def.section->output_section->vma
1610                     + h->root.root.u.def.section->output_offset);
1611         }
1612
1613       /* For simplicity of coding, we are going to modify the section
1614          contents, the section relocs, and the BFD symbol table.  We
1615          must tell the rest of the code not to free up this
1616          information.  It would be possible to instead create a table
1617          of changes which have to be made, as is done in coff-mips.c;
1618          that would be more work, but would require less memory when
1619          the linker is run.  */
1620
1621       /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1622          branch/call, also deal with "call" -> "calls" conversions and
1623          insertion of prologue data into "call" instructions.  */
1624       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1625         {
1626           bfd_vma value = symval;
1627
1628           /* If we've got a "call" instruction that needs to be turned
1629              into a "calls" instruction, do so now.  It saves a byte.  */
1630           if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1631             {
1632               unsigned char code;
1633
1634               /* Get the opcode.  */
1635               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1636
1637               /* Make sure we're working with a "call" instruction!  */
1638               if (code == 0xdd)
1639                 {
1640                   /* Note that we've changed the relocs, section contents,
1641                      etc.  */
1642                   elf_section_data (sec)->relocs = internal_relocs;
1643                   free_relocs = NULL;
1644
1645                   elf_section_data (sec)->this_hdr.contents = contents;
1646                   free_contents = NULL;
1647
1648                   free_extsyms = NULL;
1649
1650                   /* Fix the opcode.  */
1651                   bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1652                   bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1653
1654                   /* Fix irel->r_offset and irel->r_addend.  */
1655                   irel->r_offset += 1;
1656                   irel->r_addend += 1;
1657
1658                   /* Delete one byte of data.  */
1659                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1660                                                        irel->r_offset + 3, 1))
1661                     goto error_return;
1662
1663                   /* That will change things, so, we should relax again.
1664                      Note that this is not required, and it may be slow.  */
1665                   *again = true;
1666                 }
1667             }
1668           else if (h)
1669             {
1670               /* We've got a "call" instruction which needs some data
1671                  from target function filled in.  */
1672               unsigned char code;
1673
1674               /* Get the opcode.  */
1675               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1676
1677               /* Insert data from the target function into the "call"
1678                  instruction if needed.  */
1679               if (code == 0xdd)
1680                 {
1681                   bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1682                   bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1683                              contents + irel->r_offset + 5);
1684                 }
1685             }
1686
1687           /* Deal with pc-relative gunk.  */
1688           value -= (sec->output_section->vma + sec->output_offset);
1689           value -= irel->r_offset;
1690           value += irel->r_addend;
1691
1692           /* See if the value will fit in 16 bits, note the high value is
1693              0x7fff + 2 as the target will be two bytes closer if we are
1694              able to relax.  */
1695           if ((long) value < 0x8001 && (long) value > -0x8000)
1696             {
1697               unsigned char code;
1698
1699               /* Get the opcode.  */
1700               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1701
1702               if (code != 0xdc && code != 0xdd && code != 0xff)
1703                 continue;
1704
1705               /* Note that we've changed the relocs, section contents, etc.  */
1706               elf_section_data (sec)->relocs = internal_relocs;
1707               free_relocs = NULL;
1708
1709               elf_section_data (sec)->this_hdr.contents = contents;
1710               free_contents = NULL;
1711
1712               free_extsyms = NULL;
1713
1714               /* Fix the opcode.  */
1715               if (code == 0xdc)
1716                 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1717               else if (code == 0xdd)
1718                 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1719               else if (code == 0xff)
1720                 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1721
1722               /* Fix the relocation's type.  */
1723               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1724                                            R_MN10300_PCREL16);
1725
1726               /* Delete two bytes of data.  */
1727               if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1728                                                    irel->r_offset + 1, 2))
1729                 goto error_return;
1730
1731               /* That will change things, so, we should relax again.
1732                  Note that this is not required, and it may be slow.  */
1733               *again = true;
1734             }
1735         }
1736
1737       /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1738          branch.  */
1739       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1740         {
1741           bfd_vma value = symval;
1742
1743           /* If we've got a "call" instruction that needs to be turned
1744              into a "calls" instruction, do so now.  It saves a byte.  */
1745           if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1746             {
1747               unsigned char code;
1748
1749               /* Get the opcode.  */
1750               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1751
1752               /* Make sure we're working with a "call" instruction!  */
1753               if (code == 0xcd)
1754                 {
1755                   /* Note that we've changed the relocs, section contents,
1756                      etc.  */
1757                   elf_section_data (sec)->relocs = internal_relocs;
1758                   free_relocs = NULL;
1759
1760                   elf_section_data (sec)->this_hdr.contents = contents;
1761                   free_contents = NULL;
1762
1763                   free_extsyms = NULL;
1764
1765                   /* Fix the opcode.  */
1766                   bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1767                   bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1768
1769                   /* Fix irel->r_offset and irel->r_addend.  */
1770                   irel->r_offset += 1;
1771                   irel->r_addend += 1;
1772
1773                   /* Delete one byte of data.  */
1774                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1775                                                        irel->r_offset + 1, 1))
1776                     goto error_return;
1777
1778                   /* That will change things, so, we should relax again.
1779                      Note that this is not required, and it may be slow.  */
1780                   *again = true;
1781                 }
1782             }
1783           else if (h)
1784             {
1785               unsigned char code;
1786
1787               /* Get the opcode.  */
1788               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1789
1790               /* Insert data from the target function into the "call"
1791                  instruction if needed.  */
1792               if (code == 0xcd)
1793                 {
1794                   bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1795                   bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1796                              contents + irel->r_offset + 3);
1797                 }
1798             }
1799
1800           /* Deal with pc-relative gunk.  */
1801           value -= (sec->output_section->vma + sec->output_offset);
1802           value -= irel->r_offset;
1803           value += irel->r_addend;
1804
1805           /* See if the value will fit in 8 bits, note the high value is
1806              0x7f + 1 as the target will be one bytes closer if we are
1807              able to relax.  */
1808           if ((long) value < 0x80 && (long) value > -0x80)
1809             {
1810               unsigned char code;
1811
1812               /* Get the opcode.  */
1813               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1814
1815               if (code != 0xcc)
1816                 continue;
1817
1818               /* Note that we've changed the relocs, section contents, etc.  */
1819               elf_section_data (sec)->relocs = internal_relocs;
1820               free_relocs = NULL;
1821
1822               elf_section_data (sec)->this_hdr.contents = contents;
1823               free_contents = NULL;
1824
1825               free_extsyms = NULL;
1826
1827               /* Fix the opcode.  */
1828               bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1829
1830               /* Fix the relocation's type.  */
1831               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1832                                            R_MN10300_PCREL8);
1833
1834               /* Delete one byte of data.  */
1835               if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1836                                                    irel->r_offset + 1, 1))
1837                 goto error_return;
1838
1839               /* That will change things, so, we should relax again.
1840                  Note that this is not required, and it may be slow.  */
1841               *again = true;
1842             }
1843         }
1844
1845       /* Try to eliminate an unconditional 8 bit pc-relative branch
1846          which immediately follows a conditional 8 bit pc-relative
1847          branch around the unconditional branch.
1848
1849             original:           new:
1850             bCC lab1            bCC' lab2
1851             bra lab2
1852            lab1:               lab1:
1853
1854          This happens when the bCC can't reach lab2 at assembly time,
1855          but due to other relaxations it can reach at link time.  */
1856       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1857         {
1858           Elf_Internal_Rela *nrel;
1859           bfd_vma value = symval;
1860           unsigned char code;
1861
1862           /* Deal with pc-relative gunk.  */
1863           value -= (sec->output_section->vma + sec->output_offset);
1864           value -= irel->r_offset;
1865           value += irel->r_addend;
1866
1867           /* Do nothing if this reloc is the last byte in the section.  */
1868           if (irel->r_offset == sec->_cooked_size)
1869             continue;
1870
1871           /* See if the next instruction is an unconditional pc-relative
1872              branch, more often than not this test will fail, so we
1873              test it first to speed things up.  */
1874           code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1875           if (code != 0xca)
1876             continue;
1877
1878           /* Also make sure the next relocation applies to the next
1879              instruction and that it's a pc-relative 8 bit branch.  */
1880           nrel = irel + 1;
1881           if (nrel == irelend
1882               || irel->r_offset + 2 != nrel->r_offset
1883               || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1884             continue;
1885
1886           /* Make sure our destination immediately follows the
1887              unconditional branch.  */
1888           if (symval != (sec->output_section->vma + sec->output_offset
1889                          + irel->r_offset + 3))
1890             continue;
1891
1892           /* Now make sure we are a conditional branch.  This may not
1893              be necessary, but why take the chance.
1894
1895              Note these checks assume that R_MN10300_PCREL8 relocs
1896              only occur on bCC and bCCx insns.  If they occured
1897              elsewhere, we'd need to know the start of this insn
1898              for this check to be accurate.  */
1899           code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1900           if (code != 0xc0 && code != 0xc1 && code != 0xc2
1901               && code != 0xc3 && code != 0xc4 && code != 0xc5
1902               && code != 0xc6 && code != 0xc7 && code != 0xc8
1903               && code != 0xc9 && code != 0xe8 && code != 0xe9
1904               && code != 0xea && code != 0xeb)
1905             continue;
1906
1907           /* We also have to be sure there is no symbol/label
1908              at the unconditional branch.  */
1909           if (mn10300_elf_symbol_address_p (abfd, sec, irel->r_offset + 1))
1910             continue;
1911
1912           /* Note that we've changed the relocs, section contents, etc.  */
1913           elf_section_data (sec)->relocs = internal_relocs;
1914           free_relocs = NULL;
1915
1916           elf_section_data (sec)->this_hdr.contents = contents;
1917           free_contents = NULL;
1918
1919           free_extsyms = NULL;
1920
1921           /* Reverse the condition of the first branch.  */
1922           switch (code)
1923             {
1924             case 0xc8:
1925               code = 0xc9;
1926               break;
1927             case 0xc9:
1928               code = 0xc8;
1929               break;
1930             case 0xc0:
1931               code = 0xc2;
1932               break;
1933             case 0xc2:
1934               code = 0xc0;
1935               break;
1936             case 0xc3:
1937               code = 0xc1;
1938               break;
1939             case 0xc1:
1940               code = 0xc3;
1941               break;
1942             case 0xc4:
1943               code = 0xc6;
1944               break;
1945             case 0xc6:
1946               code = 0xc4;
1947               break;
1948             case 0xc7:
1949               code = 0xc5;
1950               break;
1951             case 0xc5:
1952               code = 0xc7;
1953               break;
1954             case 0xe8:
1955               code = 0xe9;
1956               break;
1957             case 0x9d:
1958               code = 0xe8;
1959               break;
1960             case 0xea:
1961               code = 0xeb;
1962               break;
1963             case 0xeb:
1964               code = 0xea;
1965               break;
1966             }
1967           bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1968
1969           /* Set the reloc type and symbol for the first branch
1970              from the second branch.  */
1971           irel->r_info = nrel->r_info;
1972
1973           /* Make the reloc for the second branch a null reloc.  */
1974           nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1975                                        R_MN10300_NONE);
1976
1977           /* Delete two bytes of data.  */
1978           if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1979                                                irel->r_offset + 1, 2))
1980             goto error_return;
1981
1982           /* That will change things, so, we should relax again.
1983              Note that this is not required, and it may be slow.  */
1984           *again = true;
1985         }
1986
1987       /* Try to turn a 24 immediate, displacement or absolute address
1988          into a 8 immediate, displacement or absolute address.  */
1989       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
1990         {
1991           bfd_vma value = symval;
1992           value += irel->r_addend;
1993
1994           /* See if the value will fit in 8 bits.  */
1995           if ((long) value < 0x7f && (long) value > -0x80)
1996             {
1997               unsigned char code;
1998
1999               /* AM33 insns which have 24 operands are 6 bytes long and
2000                  will have 0xfd as the first byte.  */
2001
2002               /* Get the first opcode.  */
2003               code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2004
2005               if (code == 0xfd)
2006                 {
2007                   /* Get the second opcode.  */
2008                   code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2009
2010                   /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2011                      equivalent instructions exists.  */
2012                   if (code != 0x6b && code != 0x7b
2013                       && code != 0x8b && code != 0x9b
2014                       && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2015                           || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2016                           || (code & 0x0f) == 0x0e))
2017                     {
2018                       /* Not safe if the high bit is on as relaxing may
2019                          move the value out of high mem and thus not fit
2020                          in a signed 8bit value.  This is currently over
2021                          conservative.  */
2022                       if ((value & 0x80) == 0)
2023                         {
2024                           /* Note that we've changed the relocation contents,
2025                              etc.  */
2026                           elf_section_data (sec)->relocs = internal_relocs;
2027                           free_relocs = NULL;
2028
2029                           elf_section_data (sec)->this_hdr.contents = contents;
2030                           free_contents = NULL;
2031
2032                           free_extsyms = NULL;
2033
2034                           /* Fix the opcode.  */
2035                           bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
2036                           bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2037
2038                           /* Fix the relocation's type.  */
2039                           irel->r_info =
2040                             ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2041                                           R_MN10300_8);
2042
2043                           /* Delete two bytes of data.  */
2044                           if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2045                                                                irel->r_offset + 1, 2))
2046                             goto error_return;
2047
2048                           /* That will change things, so, we should relax
2049                              again.  Note that this is not required, and it
2050                              may be slow.  */
2051                           *again = true;
2052                           break;
2053                         }
2054                     }
2055                 }
2056             }
2057         }
2058
2059       /* Try to turn a 32bit immediate, displacement or absolute address
2060          into a 16bit immediate, displacement or absolute address.  */
2061       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
2062         {
2063           bfd_vma value = symval;
2064           value += irel->r_addend;
2065
2066           /* See if the value will fit in 24 bits.
2067              We allow any 16bit match here.  We prune those we can't
2068              handle below.  */
2069           if ((long) value < 0x7fffff && (long) value > -0x800000)
2070             {
2071               unsigned char code;
2072
2073               /* AM33 insns which have 32bit operands are 7 bytes long and
2074                  will have 0xfe as the first byte.  */
2075
2076               /* Get the first opcode.  */
2077               code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2078
2079               if (code == 0xfe)
2080                 {
2081                   /* Get the second opcode.  */
2082                   code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2083
2084                   /* All the am33 32 -> 24 relaxing possibilities.  */
2085                   /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2086                      equivalent instructions exists.  */
2087                   if (code != 0x6b && code != 0x7b
2088                       && code != 0x8b && code != 0x9b
2089                       && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2090                           || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2091                           || (code & 0x0f) == 0x0e))
2092                     {
2093                       /* Not safe if the high bit is on as relaxing may
2094                          move the value out of high mem and thus not fit
2095                          in a signed 16bit value.  This is currently over
2096                          conservative.  */
2097                       if ((value & 0x8000) == 0)
2098                         {
2099                           /* Note that we've changed the relocation contents,
2100                              etc.  */
2101                           elf_section_data (sec)->relocs = internal_relocs;
2102                           free_relocs = NULL;
2103
2104                           elf_section_data (sec)->this_hdr.contents = contents;
2105                           free_contents = NULL;
2106
2107                           free_extsyms = NULL;
2108
2109                           /* Fix the opcode.  */
2110                           bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2111                           bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2112
2113                           /* Fix the relocation's type.  */
2114                           irel->r_info =
2115                             ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2116                                           R_MN10300_24);
2117
2118                           /* Delete one byte of data.  */
2119                           if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2120                                                                irel->r_offset + 3, 1))
2121                             goto error_return;
2122
2123                           /* That will change things, so, we should relax
2124                              again.  Note that this is not required, and it
2125                              may be slow.  */
2126                           *again = true;
2127                           break;
2128                         }
2129                     }
2130                 }
2131             }
2132
2133           /* See if the value will fit in 16 bits.
2134              We allow any 16bit match here.  We prune those we can't
2135              handle below.  */
2136           if ((long) value < 0x7fff && (long) value > -0x8000)
2137             {
2138               unsigned char code;
2139
2140               /* Most insns which have 32bit operands are 6 bytes long;
2141                  exceptions are pcrel insns and bit insns.
2142
2143                  We handle pcrel insns above.  We don't bother trying
2144                  to handle the bit insns here.
2145
2146                  The first byte of the remaining insns will be 0xfc.  */
2147
2148               /* Get the first opcode.  */
2149               code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2150
2151               if (code != 0xfc)
2152                 continue;
2153
2154               /* Get the second opcode.  */
2155               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2156
2157               if ((code & 0xf0) < 0x80)
2158                 switch (code & 0xf0)
2159                   {
2160                   /* mov (d32,am),dn   -> mov (d32,am),dn
2161                      mov dm,(d32,am)   -> mov dn,(d32,am)
2162                      mov (d32,am),an   -> mov (d32,am),an
2163                      mov dm,(d32,am)   -> mov dn,(d32,am)
2164                      movbu (d32,am),dn -> movbu (d32,am),dn
2165                      movbu dm,(d32,am) -> movbu dn,(d32,am)
2166                      movhu (d32,am),dn -> movhu (d32,am),dn
2167                      movhu dm,(d32,am) -> movhu dn,(d32,am) */
2168                   case 0x00:
2169                   case 0x10:
2170                   case 0x20:
2171                   case 0x30:
2172                   case 0x40:
2173                   case 0x50:
2174                   case 0x60:
2175                   case 0x70:
2176                     /* Not safe if the high bit is on as relaxing may
2177                        move the value out of high mem and thus not fit
2178                        in a signed 16bit value.  */
2179                     if (code == 0xcc
2180                         && (value & 0x8000))
2181                       continue;
2182
2183                     /* Note that we've changed the relocation contents, etc.  */
2184                     elf_section_data (sec)->relocs = internal_relocs;
2185                     free_relocs = NULL;
2186
2187                     elf_section_data (sec)->this_hdr.contents = contents;
2188                     free_contents = NULL;
2189
2190                     free_extsyms = NULL;
2191
2192                     /* Fix the opcode.  */
2193                     bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2194                     bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2195
2196                     /* Fix the relocation's type.  */
2197                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2198                                                  R_MN10300_16);
2199
2200                     /* Delete two bytes of data.  */
2201                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2202                                                          irel->r_offset + 2, 2))
2203                       goto error_return;
2204
2205                     /* That will change things, so, we should relax again.
2206                        Note that this is not required, and it may be slow.  */
2207                     *again = true;
2208                     break;
2209                   }
2210               else if ((code & 0xf0) == 0x80
2211                        || (code & 0xf0) == 0x90)
2212                 switch (code & 0xf3)
2213                   {
2214                   /* mov dn,(abs32)   -> mov dn,(abs16)
2215                      movbu dn,(abs32) -> movbu dn,(abs16)
2216                      movhu dn,(abs32) -> movhu dn,(abs16)  */
2217                   case 0x81:
2218                   case 0x82:
2219                   case 0x83:
2220                     /* Note that we've changed the relocation contents, etc.  */
2221                     elf_section_data (sec)->relocs = internal_relocs;
2222                     free_relocs = NULL;
2223
2224                     elf_section_data (sec)->this_hdr.contents = contents;
2225                     free_contents = NULL;
2226
2227                     free_extsyms = NULL;
2228
2229                     if ((code & 0xf3) == 0x81)
2230                       code = 0x01 + (code & 0x0c);
2231                     else if ((code & 0xf3) == 0x82)
2232                       code = 0x02 + (code & 0x0c);
2233                     else if ((code & 0xf3) == 0x83)
2234                       code = 0x03 + (code & 0x0c);
2235                     else
2236                       abort ();
2237
2238                     /* Fix the opcode.  */
2239                     bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2240
2241                     /* Fix the relocation's type.  */
2242                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2243                                                  R_MN10300_16);
2244
2245                     /* The opcode got shorter too, so we have to fix the
2246                        addend and offset too!  */
2247                     irel->r_offset -= 1;
2248
2249                     /* Delete three bytes of data.  */
2250                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2251                                                          irel->r_offset + 1, 3))
2252                       goto error_return;
2253
2254                     /* That will change things, so, we should relax again.
2255                        Note that this is not required, and it may be slow.  */
2256                     *again = true;
2257                     break;
2258
2259                   /* mov am,(abs32)    -> mov am,(abs16)
2260                      mov am,(d32,sp)   -> mov am,(d16,sp)
2261                      mov dm,(d32,sp)   -> mov dm,(d32,sp)
2262                      movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2263                      movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2264                   case 0x80:
2265                   case 0x90:
2266                   case 0x91:
2267                   case 0x92:
2268                   case 0x93:
2269                     /* sp-based offsets are zero-extended.  */
2270                     if (code >= 0x90 && code <= 0x93
2271                         && (long)value < 0)
2272                       continue;
2273
2274                     /* Note that we've changed the relocation contents, etc.  */
2275                     elf_section_data (sec)->relocs = internal_relocs;
2276                     free_relocs = NULL;
2277
2278                     elf_section_data (sec)->this_hdr.contents = contents;
2279                     free_contents = NULL;
2280
2281                     free_extsyms = NULL;
2282
2283                     /* Fix the opcode.  */
2284                     bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2285                     bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2286
2287                     /* Fix the relocation's type.  */
2288                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2289                                                  R_MN10300_16);
2290
2291                     /* Delete two bytes of data.  */
2292                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2293                                                          irel->r_offset + 2, 2))
2294                       goto error_return;
2295
2296                     /* That will change things, so, we should relax again.
2297                        Note that this is not required, and it may be slow.  */
2298                     *again = true;
2299                     break;
2300                   }
2301               else if ((code & 0xf0) < 0xf0)
2302                 switch (code & 0xfc)
2303                   {
2304                   /* mov imm32,dn     -> mov imm16,dn
2305                      mov imm32,an     -> mov imm16,an
2306                      mov (abs32),dn   -> mov (abs16),dn
2307                      movbu (abs32),dn -> movbu (abs16),dn
2308                      movhu (abs32),dn -> movhu (abs16),dn  */
2309                   case 0xcc:
2310                   case 0xdc:
2311                   case 0xa4:
2312                   case 0xa8:
2313                   case 0xac:
2314                     /* Not safe if the high bit is on as relaxing may
2315                        move the value out of high mem and thus not fit
2316                        in a signed 16bit value.  */
2317                     if (code == 0xcc
2318                         && (value & 0x8000))
2319                       continue;
2320
2321                     /* mov imm16, an zero-extends the immediate.  */
2322                     if (code == 0xdc
2323                         && (long)value < 0)
2324                       continue;
2325
2326                     /* Note that we've changed the relocation contents, etc.  */
2327                     elf_section_data (sec)->relocs = internal_relocs;
2328                     free_relocs = NULL;
2329
2330                     elf_section_data (sec)->this_hdr.contents = contents;
2331                     free_contents = NULL;
2332
2333                     free_extsyms = NULL;
2334
2335                     if ((code & 0xfc) == 0xcc)
2336                       code = 0x2c + (code & 0x03);
2337                     else if ((code & 0xfc) == 0xdc)
2338                       code = 0x24 + (code & 0x03);
2339                     else if ((code & 0xfc) == 0xa4)
2340                       code = 0x30 + (code & 0x03);
2341                     else if ((code & 0xfc) == 0xa8)
2342                       code = 0x34 + (code & 0x03);
2343                     else if ((code & 0xfc) == 0xac)
2344                       code = 0x38 + (code & 0x03);
2345                     else
2346                       abort ();
2347
2348                     /* Fix the opcode.  */
2349                     bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2350
2351                     /* Fix the relocation's type.  */
2352                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2353                                                  R_MN10300_16);
2354
2355                     /* The opcode got shorter too, so we have to fix the
2356                        addend and offset too!  */
2357                     irel->r_offset -= 1;
2358
2359                     /* Delete three bytes of data.  */
2360                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2361                                                          irel->r_offset + 1, 3))
2362                       goto error_return;
2363
2364                     /* That will change things, so, we should relax again.
2365                        Note that this is not required, and it may be slow.  */
2366                     *again = true;
2367                     break;
2368
2369                   /* mov (abs32),an    -> mov (abs16),an
2370                      mov (d32,sp),an   -> mov (d16,sp),an
2371                      mov (d32,sp),dn   -> mov (d16,sp),dn
2372                      movbu (d32,sp),dn -> movbu (d16,sp),dn
2373                      movhu (d32,sp),dn -> movhu (d16,sp),dn
2374                      add imm32,dn      -> add imm16,dn
2375                      cmp imm32,dn      -> cmp imm16,dn
2376                      add imm32,an      -> add imm16,an
2377                      cmp imm32,an      -> cmp imm16,an
2378                      and imm32,dn      -> and imm16,dn
2379                      or imm32,dn       -> or imm16,dn
2380                      xor imm32,dn      -> xor imm16,dn
2381                      btst imm32,dn     -> btst imm16,dn */
2382
2383                   case 0xa0:
2384                   case 0xb0:
2385                   case 0xb1:
2386                   case 0xb2:
2387                   case 0xb3:
2388                   case 0xc0:
2389                   case 0xc8:
2390
2391                   case 0xd0:
2392                   case 0xd8:
2393                   case 0xe0:
2394                   case 0xe1:
2395                   case 0xe2:
2396                   case 0xe3:
2397                     /* cmp imm16, an zero-extends the immediate.  */
2398                     if (code == 0xdc
2399                         && (long)value < 0)
2400                       continue;
2401
2402                     /* So do sp-based offsets.  */
2403                     if (code >= 0xb0 && code <= 0xb3
2404                         && (long)value < 0)
2405                       continue;
2406
2407                     /* Note that we've changed the relocation contents, etc.  */
2408                     elf_section_data (sec)->relocs = internal_relocs;
2409                     free_relocs = NULL;
2410
2411                     elf_section_data (sec)->this_hdr.contents = contents;
2412                     free_contents = NULL;
2413
2414                     free_extsyms = NULL;
2415
2416                     /* Fix the opcode.  */
2417                     bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2418                     bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2419
2420                     /* Fix the relocation's type.  */
2421                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2422                                                  R_MN10300_16);
2423
2424                     /* Delete two bytes of data.  */
2425                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2426                                                          irel->r_offset + 2, 2))
2427                       goto error_return;
2428
2429                     /* That will change things, so, we should relax again.
2430                        Note that this is not required, and it may be slow.  */
2431                     *again = true;
2432                     break;
2433                   }
2434               else if (code == 0xfe)
2435                 {
2436                   /* add imm32,sp -> add imm16,sp  */
2437
2438                   /* Note that we've changed the relocation contents, etc.  */
2439                   elf_section_data (sec)->relocs = internal_relocs;
2440                   free_relocs = NULL;
2441
2442                   elf_section_data (sec)->this_hdr.contents = contents;
2443                   free_contents = NULL;
2444
2445                   free_extsyms = NULL;
2446
2447                   /* Fix the opcode.  */
2448                   bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2449                   bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2450
2451                   /* Fix the relocation's type.  */
2452                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2453                                                R_MN10300_16);
2454
2455                   /* Delete two bytes of data.  */
2456                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2457                                                        irel->r_offset + 2, 2))
2458                     goto error_return;
2459
2460                   /* That will change things, so, we should relax again.
2461                      Note that this is not required, and it may be slow.  */
2462                   *again = true;
2463                   break;
2464                 }
2465             }
2466         }
2467     }
2468
2469   if (free_relocs != NULL)
2470     free (free_relocs);
2471
2472   if (free_contents != NULL)
2473     {
2474       if (! link_info->keep_memory)
2475         free (free_contents);
2476       else
2477         {
2478           /* Cache the section contents for elf_link_input_bfd.  */
2479           elf_section_data (sec)->this_hdr.contents = contents;
2480         }
2481     }
2482
2483   if (shndx_buf != NULL)
2484     {
2485       shndx_hdr->contents = NULL;
2486       free (shndx_buf);
2487     }
2488
2489   if (free_extsyms != NULL)
2490     {
2491       if (! link_info->keep_memory)
2492         {
2493           symtab_hdr->contents = NULL;
2494           free (free_extsyms);
2495         }
2496     }
2497
2498   return true;
2499
2500  error_return:
2501   if (free_relocs != NULL)
2502     free (free_relocs);
2503   if (free_contents != NULL)
2504     free (free_contents);
2505   if (shndx_buf != NULL)
2506     {
2507       shndx_hdr->contents = NULL;
2508       free (shndx_buf);
2509     }
2510   if (free_extsyms != NULL)
2511     {
2512       symtab_hdr->contents = NULL;
2513       free (free_extsyms);
2514     }
2515
2516   return false;
2517 }
2518
2519 /* Compute the stack size and movm arguments for the function
2520    referred to by HASH at address ADDR in section with
2521    contents CONTENTS, store the information in the hash table.  */
2522 static void
2523 compute_function_info (abfd, hash, addr, contents)
2524      bfd *abfd;
2525      struct elf32_mn10300_link_hash_entry *hash;
2526      bfd_vma addr;
2527      unsigned char *contents;
2528 {
2529   unsigned char byte1, byte2;
2530   /* We only care about a very small subset of the possible prologue
2531      sequences here.  Basically we look for:
2532
2533      movm [d2,d3,a2,a3],sp (optional)
2534      add <size>,sp (optional, and only for sizes which fit in an unsigned
2535                     8 bit number)
2536
2537      If we find anything else, we quit.  */
2538
2539   /* Look for movm [regs],sp */
2540   byte1 = bfd_get_8 (abfd, contents + addr);
2541   byte2 = bfd_get_8 (abfd, contents + addr + 1);
2542
2543   if (byte1 == 0xcf)
2544     {
2545       hash->movm_args = byte2;
2546       addr += 2;
2547       byte1 = bfd_get_8 (abfd, contents + addr);
2548       byte2 = bfd_get_8 (abfd, contents + addr + 1);
2549     }
2550
2551   /* Now figure out how much stack space will be allocated by the movm
2552      instruction.  We need this kept separate from the funtion's normal
2553      stack space.  */
2554   if (hash->movm_args)
2555     {
2556       /* Space for d2.  */
2557       if (hash->movm_args & 0x80)
2558         hash->movm_stack_size += 4;
2559
2560       /* Space for d3.  */
2561       if (hash->movm_args & 0x40)
2562         hash->movm_stack_size += 4;
2563
2564       /* Space for a2.  */
2565       if (hash->movm_args & 0x20)
2566         hash->movm_stack_size += 4;
2567
2568       /* Space for a3.  */
2569       if (hash->movm_args & 0x10)
2570         hash->movm_stack_size += 4;
2571
2572       /* "other" space.  d0, d1, a0, a1, mdr, lir, lar, 4 byte pad.  */
2573       if (hash->movm_args & 0x08)
2574         hash->movm_stack_size += 8 * 4;
2575
2576       if (bfd_get_mach (abfd) == bfd_mach_am33)
2577         {
2578           /* "exother" space.  e0, e1, mdrq, mcrh, mcrl, mcvf */
2579           if (hash->movm_args & 0x1)
2580             hash->movm_stack_size += 6 * 4;
2581
2582           /* exreg1 space.  e4, e5, e6, e7 */
2583           if (hash->movm_args & 0x2)
2584             hash->movm_stack_size += 4 * 4;
2585
2586           /* exreg0 space.  e2, e3  */
2587           if (hash->movm_args & 0x4)
2588             hash->movm_stack_size += 2 * 4;
2589         }
2590     }
2591
2592   /* Now look for the two stack adjustment variants.  */
2593   if (byte1 == 0xf8 && byte2 == 0xfe)
2594     {
2595       int temp = bfd_get_8 (abfd, contents + addr + 2);
2596       temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2597
2598       hash->stack_size = -temp;
2599     }
2600   else if (byte1 == 0xfa && byte2 == 0xfe)
2601     {
2602       int temp = bfd_get_16 (abfd, contents + addr + 2);
2603       temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2604       temp = -temp;
2605
2606       if (temp < 255)
2607         hash->stack_size = temp;
2608     }
2609
2610   /* If the total stack to be allocated by the call instruction is more
2611      than 255 bytes, then we can't remove the stack adjustment by using
2612      "call" (we might still be able to remove the "movm" instruction.  */
2613   if (hash->stack_size + hash->movm_stack_size > 255)
2614     hash->stack_size = 0;
2615
2616   return;
2617 }
2618
2619 /* Delete some bytes from a section while relaxing.  */
2620
2621 static boolean
2622 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2623      bfd *abfd;
2624      asection *sec;
2625      bfd_vma addr;
2626      int count;
2627 {
2628   Elf_Internal_Shdr *symtab_hdr;
2629   Elf_Internal_Shdr *shndx_hdr;
2630   Elf32_External_Sym *extsyms;
2631   unsigned int sec_shndx;
2632   bfd_byte *contents;
2633   Elf_Internal_Rela *irel, *irelend;
2634   Elf_Internal_Rela *irelalign;
2635   bfd_vma toaddr;
2636   Elf32_External_Sym *esym, *esymend;
2637   Elf_External_Sym_Shndx *shndx;
2638   struct elf_link_hash_entry **sym_hashes;
2639   struct elf_link_hash_entry **end_hashes;
2640   unsigned int symcount;
2641
2642   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2643   extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2644
2645   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2646
2647   contents = elf_section_data (sec)->this_hdr.contents;
2648
2649   /* The deletion must stop at the next ALIGN reloc for an aligment
2650      power larger than the number of bytes we are deleting.  */
2651
2652   irelalign = NULL;
2653   toaddr = sec->_cooked_size;
2654
2655   irel = elf_section_data (sec)->relocs;
2656   irelend = irel + sec->reloc_count;
2657
2658   /* Actually delete the bytes.  */
2659   memmove (contents + addr, contents + addr + count,
2660            (size_t) (toaddr - addr - count));
2661   sec->_cooked_size -= count;
2662
2663   /* Adjust all the relocs.  */
2664   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2665     {
2666       /* Get the new reloc address.  */
2667       if ((irel->r_offset > addr
2668            && irel->r_offset < toaddr))
2669         irel->r_offset -= count;
2670     }
2671
2672   /* Adjust the local symbols defined in this section.  */
2673   shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2674   shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2675   esym = extsyms;
2676   esymend = esym + symtab_hdr->sh_info;
2677   for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
2678     {
2679       Elf_Internal_Sym isym;
2680       Elf_External_Sym_Shndx dummy;
2681
2682       bfd_elf32_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx,
2683                                 &isym);
2684
2685       if (isym.st_shndx == sec_shndx
2686           && isym.st_value > addr
2687           && isym.st_value < toaddr)
2688         {
2689           isym.st_value -= count;
2690           bfd_elf32_swap_symbol_out (abfd, &isym, (PTR) esym, (PTR) &dummy);
2691         }
2692     }
2693
2694   /* Now adjust the global symbols defined in this section.  */
2695   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2696               - symtab_hdr->sh_info);
2697   sym_hashes = elf_sym_hashes (abfd);
2698   end_hashes = sym_hashes + symcount;
2699   for (; sym_hashes < end_hashes; sym_hashes++)
2700     {
2701       struct elf_link_hash_entry *sym_hash = *sym_hashes;
2702       if ((sym_hash->root.type == bfd_link_hash_defined
2703            || sym_hash->root.type == bfd_link_hash_defweak)
2704           && sym_hash->root.u.def.section == sec
2705           && sym_hash->root.u.def.value > addr
2706           && sym_hash->root.u.def.value < toaddr)
2707         {
2708           sym_hash->root.u.def.value -= count;
2709         }
2710     }
2711
2712   return true;
2713 }
2714
2715 /* Return true if a symbol exists at the given address, else return
2716    false.  */
2717 static boolean
2718 mn10300_elf_symbol_address_p (abfd, sec, addr)
2719      bfd *abfd;
2720      asection *sec;
2721      bfd_vma addr;
2722 {
2723   Elf_Internal_Shdr *symtab_hdr;
2724   Elf_Internal_Shdr *shndx_hdr;
2725   unsigned int sec_shndx;
2726   Elf32_External_Sym *esym, *esymend;
2727   Elf_External_Sym_Shndx *shndx;
2728   struct elf_link_hash_entry **sym_hashes;
2729   struct elf_link_hash_entry **end_hashes;
2730   unsigned int symcount;
2731
2732   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2733
2734   /* Examine all the symbols.  */
2735   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2736   shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2737   shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2738   esym = (Elf32_External_Sym *) symtab_hdr->contents;
2739   esymend = esym + symtab_hdr->sh_info;
2740   for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
2741     {
2742       Elf_Internal_Sym isym;
2743
2744       bfd_elf32_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx,
2745                                 &isym);
2746
2747       if (isym.st_shndx == sec_shndx
2748           && isym.st_value == addr)
2749         return true;
2750     }
2751
2752   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2753               - symtab_hdr->sh_info);
2754   sym_hashes = elf_sym_hashes (abfd);
2755   end_hashes = sym_hashes + symcount;
2756   for (; sym_hashes < end_hashes; sym_hashes++)
2757     {
2758       struct elf_link_hash_entry *sym_hash = *sym_hashes;
2759       if ((sym_hash->root.type == bfd_link_hash_defined
2760            || sym_hash->root.type == bfd_link_hash_defweak)
2761           && sym_hash->root.u.def.section == sec
2762           && sym_hash->root.u.def.value == addr)
2763         return true;
2764     }
2765
2766   return false;
2767 }
2768
2769 /* This is a version of bfd_generic_get_relocated_section_contents
2770    which uses mn10300_elf_relocate_section.  */
2771
2772 static bfd_byte *
2773 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2774                                             data, relocateable, symbols)
2775      bfd *output_bfd;
2776      struct bfd_link_info *link_info;
2777      struct bfd_link_order *link_order;
2778      bfd_byte *data;
2779      boolean relocateable;
2780      asymbol **symbols;
2781 {
2782   Elf_Internal_Shdr *symtab_hdr;
2783   Elf_Internal_Shdr *shndx_hdr;
2784   asection *input_section = link_order->u.indirect.section;
2785   bfd *input_bfd = input_section->owner;
2786   asection **sections = NULL;
2787   Elf_Internal_Rela *internal_relocs = NULL;
2788   Elf32_External_Sym *external_syms = NULL;
2789   Elf_External_Sym_Shndx *shndx_buf = NULL;
2790   Elf_External_Sym_Shndx *shndx;
2791   Elf_Internal_Sym *internal_syms = NULL;
2792
2793   /* We only need to handle the case of relaxing, or of having a
2794      particular set of section contents, specially.  */
2795   if (relocateable
2796       || elf_section_data (input_section)->this_hdr.contents == NULL)
2797     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2798                                                        link_order, data,
2799                                                        relocateable,
2800                                                        symbols);
2801
2802   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2803   shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
2804
2805   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2806           (size_t) input_section->_raw_size);
2807
2808   if ((input_section->flags & SEC_RELOC) != 0
2809       && input_section->reloc_count > 0)
2810     {
2811       Elf_Internal_Sym *isymp;
2812       asection **secpp;
2813       Elf32_External_Sym *esym, *esymend;
2814       bfd_size_type amt;
2815
2816       if (symtab_hdr->contents != NULL)
2817         external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2818       else if (symtab_hdr->sh_info != 0)
2819         {
2820           amt = symtab_hdr->sh_info;
2821           amt *= sizeof (Elf32_External_Sym);
2822           external_syms = (Elf32_External_Sym *) bfd_malloc (amt);
2823           if (external_syms == NULL)
2824             goto error_return;
2825           if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2826               || bfd_bread ((PTR) external_syms, amt, input_bfd) != amt)
2827             goto error_return;
2828         }
2829
2830       if (symtab_hdr->sh_info != 0 && shndx_hdr->sh_size != 0)
2831         {
2832           amt = symtab_hdr->sh_info;
2833           amt *= sizeof (Elf_External_Sym_Shndx);
2834           shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2835           if (shndx_buf == NULL)
2836             goto error_return;
2837           if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2838               || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
2839             goto error_return;
2840         }
2841
2842       internal_relocs = (_bfd_elf32_link_read_relocs
2843                          (input_bfd, input_section, (PTR) NULL,
2844                           (Elf_Internal_Rela *) NULL, false));
2845       if (internal_relocs == NULL)
2846         goto error_return;
2847
2848       amt = symtab_hdr->sh_info;
2849       amt *= sizeof (Elf_Internal_Sym);
2850       internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
2851       if (internal_syms == NULL && amt != 0)
2852         goto error_return;
2853
2854       amt = symtab_hdr->sh_info;
2855       amt *= sizeof (asection *);
2856       sections = (asection **) bfd_malloc (amt);
2857       if (sections == NULL && amt != 0)
2858         goto error_return;
2859
2860       for (isymp = internal_syms, secpp = sections, shndx = shndx_buf,
2861              esym = external_syms, esymend = esym + symtab_hdr->sh_info;
2862            esym < esymend;
2863            ++esym, ++isymp, ++secpp, shndx = (shndx ? shndx + 1 : NULL))
2864         {
2865           asection *isec;
2866
2867           bfd_elf32_swap_symbol_in (input_bfd, (const PTR) esym,
2868                                     (const PTR) shndx, isymp);
2869
2870           if (isymp->st_shndx == SHN_UNDEF)
2871             isec = bfd_und_section_ptr;
2872           else if (isymp->st_shndx == SHN_ABS)
2873             isec = bfd_abs_section_ptr;
2874           else if (isymp->st_shndx == SHN_COMMON)
2875             isec = bfd_com_section_ptr;
2876           else
2877             isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2878
2879           *secpp = isec;
2880         }
2881
2882       if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2883                                      input_section, data, internal_relocs,
2884                                      internal_syms, sections))
2885         goto error_return;
2886
2887       if (sections != NULL)
2888         free (sections);
2889       if (internal_syms != NULL)
2890         free (internal_syms);
2891       if (shndx_buf != NULL)
2892         free (shndx_buf);
2893       if (external_syms != NULL && symtab_hdr->contents == NULL)
2894         free (external_syms);
2895       if (internal_relocs != elf_section_data (input_section)->relocs)
2896         free (internal_relocs);
2897     }
2898
2899   return data;
2900
2901  error_return:
2902   if (internal_relocs != NULL
2903       && internal_relocs != elf_section_data (input_section)->relocs)
2904     free (internal_relocs);
2905   if (shndx_buf != NULL)
2906     free (shndx_buf);
2907   if (external_syms != NULL && symtab_hdr->contents == NULL)
2908     free (external_syms);
2909   if (internal_syms != NULL)
2910     free (internal_syms);
2911   if (sections != NULL)
2912     free (sections);
2913   return NULL;
2914 }
2915
2916 /* Assorted hash table functions.  */
2917
2918 /* Initialize an entry in the link hash table.  */
2919
2920 /* Create an entry in an MN10300 ELF linker hash table.  */
2921
2922 static struct bfd_hash_entry *
2923 elf32_mn10300_link_hash_newfunc (entry, table, string)
2924      struct bfd_hash_entry *entry;
2925      struct bfd_hash_table *table;
2926      const char *string;
2927 {
2928   struct elf32_mn10300_link_hash_entry *ret =
2929     (struct elf32_mn10300_link_hash_entry *) entry;
2930
2931   /* Allocate the structure if it has not already been allocated by a
2932      subclass.  */
2933   if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2934     ret = ((struct elf32_mn10300_link_hash_entry *)
2935            bfd_hash_allocate (table,
2936                               sizeof (struct elf32_mn10300_link_hash_entry)));
2937   if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2938     return (struct bfd_hash_entry *) ret;
2939
2940   /* Call the allocation method of the superclass.  */
2941   ret = ((struct elf32_mn10300_link_hash_entry *)
2942          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2943                                      table, string));
2944   if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2945     {
2946       ret->direct_calls = 0;
2947       ret->stack_size = 0;
2948       ret->movm_stack_size = 0;
2949       ret->flags = 0;
2950       ret->movm_args = 0;
2951     }
2952
2953   return (struct bfd_hash_entry *) ret;
2954 }
2955
2956 /* Create an mn10300 ELF linker hash table.  */
2957
2958 static struct bfd_link_hash_table *
2959 elf32_mn10300_link_hash_table_create (abfd)
2960      bfd *abfd;
2961 {
2962   struct elf32_mn10300_link_hash_table *ret;
2963   bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
2964
2965   ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
2966   if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2967     return NULL;
2968
2969   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2970                                        elf32_mn10300_link_hash_newfunc))
2971     {
2972       free (ret);
2973       return NULL;
2974     }
2975
2976   ret->flags = 0;
2977   amt = sizeof (struct elf_link_hash_table);
2978   ret->static_hash_table
2979     = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
2980   if (ret->static_hash_table == NULL)
2981     {
2982       free (ret);
2983       return NULL;
2984     }
2985
2986   if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2987                                        elf32_mn10300_link_hash_newfunc))
2988     {
2989       free (ret->static_hash_table);
2990       free (ret);
2991       return NULL;
2992     }
2993   return &ret->root.root;
2994 }
2995
2996 /* Free an mn10300 ELF linker hash table.  */
2997
2998 static void
2999 elf32_mn10300_link_hash_table_free (hash)
3000      struct bfd_link_hash_table *hash;
3001 {
3002   struct elf32_mn10300_link_hash_table *ret
3003     = (struct elf32_mn10300_link_hash_table *) hash;
3004
3005   _bfd_generic_link_hash_table_free
3006     ((struct bfd_link_hash_table *) ret->static_hash_table);
3007   _bfd_generic_link_hash_table_free
3008     ((struct bfd_link_hash_table *) ret);
3009 }
3010
3011 static unsigned long
3012 elf_mn10300_mach (flags)
3013      flagword flags;
3014 {
3015   switch (flags & EF_MN10300_MACH)
3016     {
3017     case E_MN10300_MACH_MN10300:
3018     default:
3019       return bfd_mach_mn10300;
3020
3021     case E_MN10300_MACH_AM33:
3022       return bfd_mach_am33;
3023     }
3024 }
3025
3026 /* The final processing done just before writing out a MN10300 ELF object
3027    file.  This gets the MN10300 architecture right based on the machine
3028    number.  */
3029
3030 void
3031 _bfd_mn10300_elf_final_write_processing (abfd, linker)
3032      bfd *abfd;
3033      boolean linker ATTRIBUTE_UNUSED;
3034 {
3035   unsigned long val;
3036
3037   switch (bfd_get_mach (abfd))
3038     {
3039     default:
3040     case bfd_mach_mn10300:
3041       val = E_MN10300_MACH_MN10300;
3042       break;
3043
3044     case bfd_mach_am33:
3045       val = E_MN10300_MACH_AM33;
3046       break;
3047     }
3048
3049   elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
3050   elf_elfheader (abfd)->e_flags |= val;
3051 }
3052
3053 boolean
3054 _bfd_mn10300_elf_object_p (abfd)
3055      bfd *abfd;
3056 {
3057   bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
3058                              elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
3059   return true;
3060 }
3061
3062 /* Merge backend specific data from an object file to the output
3063    object file when linking.  */
3064
3065 boolean
3066 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
3067      bfd *ibfd;
3068      bfd *obfd;
3069 {
3070   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3071       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3072     return true;
3073
3074   if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3075       && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
3076     {
3077       if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3078                                bfd_get_mach (ibfd)))
3079         return false;
3080     }
3081
3082   return true;
3083 }
3084
3085 #define TARGET_LITTLE_SYM       bfd_elf32_mn10300_vec
3086 #define TARGET_LITTLE_NAME      "elf32-mn10300"
3087 #define ELF_ARCH                bfd_arch_mn10300
3088 #define ELF_MACHINE_CODE        EM_MN10300
3089 #define ELF_MACHINE_ALT1        EM_CYGNUS_MN10300
3090 #define ELF_MAXPAGESIZE         0x1000
3091
3092 #define elf_info_to_howto               mn10300_info_to_howto
3093 #define elf_info_to_howto_rel           0
3094 #define elf_backend_can_gc_sections     1
3095 #define elf_backend_rela_normal         1
3096 #define elf_backend_check_relocs        mn10300_elf_check_relocs
3097 #define elf_backend_gc_mark_hook        mn10300_elf_gc_mark_hook
3098 #define elf_backend_relocate_section    mn10300_elf_relocate_section
3099 #define bfd_elf32_bfd_relax_section     mn10300_elf_relax_section
3100 #define bfd_elf32_bfd_get_relocated_section_contents \
3101                                 mn10300_elf_get_relocated_section_contents
3102 #define bfd_elf32_bfd_link_hash_table_create \
3103                                 elf32_mn10300_link_hash_table_create
3104 #define bfd_elf32_bfd_link_hash_table_free \
3105                                 elf32_mn10300_link_hash_table_free
3106
3107 #define elf_symbol_leading_char '_'
3108
3109 /* So we can set bits in e_flags.  */
3110 #define elf_backend_final_write_processing \
3111                                         _bfd_mn10300_elf_final_write_processing
3112 #define elf_backend_object_p            _bfd_mn10300_elf_object_p
3113
3114 #define bfd_elf32_bfd_merge_private_bfd_data \
3115                                         _bfd_mn10300_elf_merge_private_bfd_data
3116
3117 #include "elf32-target.h"