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