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