Initial revision
[binutils.git] / bfd / elf-m10300.c
blob9ba09a88d61d8689b1920c25a93b1181613cc535
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997, 1998 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;
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;
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;
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;
413 else
415 if (!(elf_bad_symtab (abfd)
416 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
417 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
418 && sym->st_shndx != SHN_COMMON))
420 return bfd_section_from_elf_index (abfd, sym->st_shndx);
424 return NULL;
427 /* Perform a relocation as part of a final link. */
428 static bfd_reloc_status_type
429 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
430 input_section, contents, offset, value,
431 addend, info, sym_sec, is_local)
432 reloc_howto_type *howto;
433 bfd *input_bfd;
434 bfd *output_bfd;
435 asection *input_section;
436 bfd_byte *contents;
437 bfd_vma offset;
438 bfd_vma value;
439 bfd_vma addend;
440 struct bfd_link_info *info;
441 asection *sym_sec;
442 int is_local;
444 unsigned long r_type = howto->type;
445 bfd_byte *hit_data = contents + offset;
447 switch (r_type)
449 case R_MN10300_NONE:
450 return bfd_reloc_ok;
452 case R_MN10300_32:
453 value += addend;
454 bfd_put_32 (input_bfd, value, hit_data);
455 return bfd_reloc_ok;
457 case R_MN10300_24:
458 value += addend;
460 if ((long)value > 0x7fffff || (long)value < -0x800000)
461 return bfd_reloc_overflow;
463 bfd_put_8 (input_bfd, value & 0xff, hit_data);
464 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
465 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
466 return bfd_reloc_ok;
468 case R_MN10300_16:
469 value += addend;
471 if ((long)value > 0x7fff || (long)value < -0x8000)
472 return bfd_reloc_overflow;
474 bfd_put_16 (input_bfd, value, hit_data);
475 return bfd_reloc_ok;
477 case R_MN10300_8:
478 value += addend;
480 if ((long)value > 0x7f || (long)value < -0x80)
481 return bfd_reloc_overflow;
483 bfd_put_8 (input_bfd, value, hit_data);
484 return bfd_reloc_ok;
486 case R_MN10300_PCREL8:
487 value -= (input_section->output_section->vma
488 + input_section->output_offset);
489 value -= offset;
490 value += addend;
492 if ((long)value > 0xff || (long)value < -0x100)
493 return bfd_reloc_overflow;
495 bfd_put_8 (input_bfd, value, hit_data);
496 return bfd_reloc_ok;
498 case R_MN10300_PCREL16:
499 value -= (input_section->output_section->vma
500 + input_section->output_offset);
501 value -= offset;
502 value += addend;
504 if ((long)value > 0xffff || (long)value < -0x10000)
505 return bfd_reloc_overflow;
507 bfd_put_16 (input_bfd, value, hit_data);
508 return bfd_reloc_ok;
510 case R_MN10300_PCREL32:
511 value -= (input_section->output_section->vma
512 + input_section->output_offset);
513 value -= offset;
514 value += addend;
516 bfd_put_32 (input_bfd, value, hit_data);
517 return bfd_reloc_ok;
519 case R_MN10300_GNU_VTINHERIT:
520 case R_MN10300_GNU_VTENTRY:
521 return bfd_reloc_ok;
523 default:
524 return bfd_reloc_notsupported;
529 /* Relocate an MN10300 ELF section. */
530 static boolean
531 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
532 contents, relocs, local_syms, local_sections)
533 bfd *output_bfd;
534 struct bfd_link_info *info;
535 bfd *input_bfd;
536 asection *input_section;
537 bfd_byte *contents;
538 Elf_Internal_Rela *relocs;
539 Elf_Internal_Sym *local_syms;
540 asection **local_sections;
542 Elf_Internal_Shdr *symtab_hdr;
543 struct elf32_mn10300_link_hash_entry **sym_hashes;
544 Elf_Internal_Rela *rel, *relend;
546 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
547 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
548 (elf_sym_hashes (input_bfd));
550 rel = relocs;
551 relend = relocs + input_section->reloc_count;
552 for (; rel < relend; rel++)
554 int r_type;
555 reloc_howto_type *howto;
556 unsigned long r_symndx;
557 Elf_Internal_Sym *sym;
558 asection *sec;
559 struct elf32_mn10300_link_hash_entry *h;
560 bfd_vma relocation;
561 bfd_reloc_status_type r;
563 r_symndx = ELF32_R_SYM (rel->r_info);
564 r_type = ELF32_R_TYPE (rel->r_info);
565 howto = elf_mn10300_howto_table + r_type;
567 /* Just skip the vtable gc relocs. */
568 if (r_type == R_MN10300_GNU_VTINHERIT
569 || r_type == R_MN10300_GNU_VTENTRY)
570 continue;
572 if (info->relocateable)
574 /* This is a relocateable link. We don't have to change
575 anything, unless the reloc is against a section symbol,
576 in which case we have to adjust according to where the
577 section symbol winds up in the output section. */
578 if (r_symndx < symtab_hdr->sh_info)
580 sym = local_syms + r_symndx;
581 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
583 sec = local_sections[r_symndx];
584 rel->r_addend += sec->output_offset + sym->st_value;
588 continue;
591 /* This is a final link. */
592 h = NULL;
593 sym = NULL;
594 sec = NULL;
595 if (r_symndx < symtab_hdr->sh_info)
597 sym = local_syms + r_symndx;
598 sec = local_sections[r_symndx];
599 relocation = (sec->output_section->vma
600 + sec->output_offset
601 + sym->st_value);
603 else
605 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
606 while (h->root.type == bfd_link_hash_indirect
607 || h->root.type == bfd_link_hash_warning)
608 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
609 if (h->root.root.type == bfd_link_hash_defined
610 || h->root.root.type == bfd_link_hash_defweak)
612 sec = h->root.root.u.def.section;
613 relocation = (h->root.root.u.def.value
614 + sec->output_section->vma
615 + sec->output_offset);
617 else if (h->root.root.type == bfd_link_hash_undefweak)
618 relocation = 0;
619 else
621 if (! ((*info->callbacks->undefined_symbol)
622 (info, h->root.root.root.string, input_bfd,
623 input_section, rel->r_offset)))
624 return false;
625 relocation = 0;
629 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
630 input_section,
631 contents, rel->r_offset,
632 relocation, rel->r_addend,
633 info, sec, h == NULL);
635 if (r != bfd_reloc_ok)
637 const char *name;
638 const char *msg = (const char *)0;
640 if (h != NULL)
641 name = h->root.root.root.string;
642 else
644 name = (bfd_elf_string_from_elf_section
645 (input_bfd, symtab_hdr->sh_link, sym->st_name));
646 if (name == NULL || *name == '\0')
647 name = bfd_section_name (input_bfd, sec);
650 switch (r)
652 case bfd_reloc_overflow:
653 if (! ((*info->callbacks->reloc_overflow)
654 (info, name, howto->name, (bfd_vma) 0,
655 input_bfd, input_section, rel->r_offset)))
656 return false;
657 break;
659 case bfd_reloc_undefined:
660 if (! ((*info->callbacks->undefined_symbol)
661 (info, name, input_bfd, input_section,
662 rel->r_offset)))
663 return false;
664 break;
666 case bfd_reloc_outofrange:
667 msg = _("internal error: out of range error");
668 goto common_error;
670 case bfd_reloc_notsupported:
671 msg = _("internal error: unsupported relocation error");
672 goto common_error;
674 case bfd_reloc_dangerous:
675 msg = _("internal error: dangerous error");
676 goto common_error;
678 default:
679 msg = _("internal error: unknown error");
680 /* fall through */
682 common_error:
683 if (!((*info->callbacks->warning)
684 (info, msg, name, input_bfd, input_section,
685 rel->r_offset)))
686 return false;
687 break;
692 return true;
695 /* Finish initializing one hash table entry. */
696 static boolean
697 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
698 struct bfd_hash_entry *gen_entry;
699 PTR in_args;
701 struct elf32_mn10300_link_hash_entry *entry;
702 unsigned int byte_count = 0;
704 entry = (struct elf32_mn10300_link_hash_entry *)gen_entry;
706 /* If we already know we want to convert "call" to "calls" for calls
707 to this symbol, then return now. */
708 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
709 return true;
711 /* If there are no named calls to this symbol, or there's nothing we
712 can move from the function itself into the "call" instruction, then
713 note that all "call" instructions should be converted into "calls"
714 instructions and return. */
715 if (entry->direct_calls == 0
716 || (entry->stack_size == 0 && entry->movm_args == 0))
718 /* Make a note that we should convert "call" instructions to "calls"
719 instructions for calls to this symbol. */
720 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
721 return true;
724 /* We may be able to move some instructions from the function itself into
725 the "call" instruction. Count how many bytes we might be able to
726 eliminate in the function itself. */
728 /* A movm instruction is two bytes. */
729 if (entry->movm_args)
730 byte_count += 2;
732 /* Count the insn to allocate stack space too. */
733 if (entry->stack_size > 0 && entry->stack_size <= 128)
734 byte_count += 3;
735 else if (entry->stack_size > 0 && entry->stack_size < 256)
736 byte_count += 4;
738 /* If using "call" will result in larger code, then turn all
739 the associated "call" instructions into "calls" instrutions. */
740 if (byte_count < entry->direct_calls)
741 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
743 /* This routine never fails. */
744 return true;
747 /* This function handles relaxing for the mn10300.
749 There's quite a few relaxing opportunites available on the mn10300:
751 * calls:32 -> calls:16 2 bytes
752 * call:32 -> call:16 2 bytes
754 * call:32 -> calls:32 1 byte
755 * call:16 -> calls:16 1 byte
756 * These are done anytime using "calls" would result
757 in smaller code, or when necessary to preserve the
758 meaning of the program.
760 * call:32 varies
761 * call:16
762 * In some circumstances we can move instructions
763 from a function prologue into a "call" instruction.
764 This is only done if the resulting code is no larger
765 than the original code.
768 * jmp:32 -> jmp:16 2 bytes
769 * jmp:16 -> bra:8 1 byte
771 * If the previous instruction is a conditional branch
772 around the jump/bra, we may be able to reverse its condition
773 and change its target to the jump's target. The jump/bra
774 can then be deleted. 2 bytes
776 * mov abs32 -> mov abs16 1 or 2 bytes
778 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
779 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
781 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
782 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
784 We don't handle imm16->imm8 or d16->d8 as they're very rare
785 and somewhat more difficult to support. */
787 static boolean
788 mn10300_elf_relax_section (abfd, sec, link_info, again)
789 bfd *abfd;
790 asection *sec;
791 struct bfd_link_info *link_info;
792 boolean *again;
794 Elf_Internal_Shdr *symtab_hdr;
795 Elf_Internal_Rela *internal_relocs = NULL;
796 Elf_Internal_Rela *free_relocs = NULL;
797 Elf_Internal_Rela *irel, *irelend;
798 bfd_byte *contents = NULL;
799 bfd_byte *free_contents = NULL;
800 Elf32_External_Sym *extsyms = NULL;
801 Elf32_External_Sym *free_extsyms = NULL;
802 struct elf32_mn10300_link_hash_table *hash_table;
804 /* Assume nothing changes. */
805 *again = false;
807 /* We need a pointer to the mn10300 specific hash table. */
808 hash_table = elf32_mn10300_hash_table (link_info);
810 /* Initialize fields in each hash table entry the first time through. */
811 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
813 bfd *input_bfd;
815 /* Iterate over all the input bfds. */
816 for (input_bfd = link_info->input_bfds;
817 input_bfd != NULL;
818 input_bfd = input_bfd->link_next)
820 asection *section;
822 /* We're going to need all the symbols for each bfd. */
823 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
825 /* Get cached copy if it exists. */
826 if (symtab_hdr->contents != NULL)
827 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
828 else
830 /* Go get them off disk. */
831 extsyms = ((Elf32_External_Sym *)
832 bfd_malloc (symtab_hdr->sh_size));
833 if (extsyms == NULL)
834 goto error_return;
835 free_extsyms = extsyms;
836 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
837 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
838 != symtab_hdr->sh_size))
839 goto error_return;
842 /* Iterate over each section in this bfd. */
843 for (section = input_bfd->sections;
844 section != NULL;
845 section = section->next)
847 struct elf32_mn10300_link_hash_entry *hash;
848 Elf_Internal_Sym *sym;
849 asection *sym_sec;
850 const char *sym_name;
851 char *new_name;
852 Elf_Internal_Shdr *hdr;
854 /* Get cached copy of section contents if it exists. */
855 if (elf_section_data (section)->this_hdr.contents != NULL)
856 contents = elf_section_data (section)->this_hdr.contents;
857 else if (section->_raw_size != 0)
859 /* Go get them off disk. */
860 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
861 if (contents == NULL)
862 goto error_return;
863 free_contents = contents;
865 if (!bfd_get_section_contents (input_bfd, section,
866 contents, (file_ptr) 0,
867 section->_raw_size))
868 goto error_return;
870 else
872 contents = NULL;
873 free_contents = NULL;
876 /* If there aren't any relocs, then there's nothing to do. */
877 if ((section->flags & SEC_RELOC) != 0
878 && section->reloc_count != 0)
881 /* Get a copy of the native relocations. */
882 internal_relocs = (_bfd_elf32_link_read_relocs
883 (input_bfd, section, (PTR) NULL,
884 (Elf_Internal_Rela *) NULL,
885 link_info->keep_memory));
886 if (internal_relocs == NULL)
887 goto error_return;
888 if (! link_info->keep_memory)
889 free_relocs = internal_relocs;
891 /* Now examine each relocation. */
892 irel = internal_relocs;
893 irelend = irel + section->reloc_count;
894 for (; irel < irelend; irel++)
896 long r_type;
897 unsigned long r_index;
898 unsigned char code;
900 r_type = ELF32_R_TYPE (irel->r_info);
901 r_index = ELF32_R_SYM (irel->r_info);
903 if (r_type < 0 || r_type >= (int)R_MN10300_MAX)
904 goto error_return;
906 /* We need the name and hash table entry of the target
907 symbol! */
908 hash = NULL;
909 sym = NULL;
910 sym_sec = NULL;
912 if (r_index < symtab_hdr->sh_info)
914 /* A local symbol. */
915 Elf_Internal_Sym isym;
917 bfd_elf32_swap_symbol_in (input_bfd,
918 extsyms + r_index, &isym);
920 if (isym.st_shndx == SHN_UNDEF)
921 sym_sec = bfd_und_section_ptr;
922 else if (isym.st_shndx > 0
923 && isym.st_shndx < SHN_LORESERVE)
924 sym_sec
925 = bfd_section_from_elf_index (input_bfd,
926 isym.st_shndx);
927 else if (isym.st_shndx == SHN_ABS)
928 sym_sec = bfd_abs_section_ptr;
929 else if (isym.st_shndx == SHN_COMMON)
930 sym_sec = bfd_com_section_ptr;
932 sym_name = bfd_elf_string_from_elf_section (input_bfd,
933 symtab_hdr->sh_link,
934 isym.st_name);
936 /* If it isn't a function, then we don't care
937 about it. */
938 if (r_index < symtab_hdr->sh_info
939 && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
940 continue;
942 /* Tack on an ID so we can uniquely identify this
943 local symbol in the global hash table. */
944 new_name = bfd_malloc (strlen (sym_name) + 10);
945 if (new_name == 0)
946 goto error_return;
948 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
949 sym_name = new_name;
951 hash = (struct elf32_mn10300_link_hash_entry *)
952 elf_link_hash_lookup (&hash_table->static_hash_table->root,
953 sym_name, true,
954 true, false);
955 free (new_name);
957 else
959 r_index -= symtab_hdr->sh_info;
960 hash = (struct elf32_mn10300_link_hash_entry *)
961 elf_sym_hashes (input_bfd)[r_index];
964 /* If this is not a "call" instruction, then we
965 should convert "call" instructions to "calls"
966 instructions. */
967 code = bfd_get_8 (input_bfd,
968 contents + irel->r_offset - 1);
969 if (code != 0xdd && code != 0xcd)
970 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
972 /* If this is a jump/call, then bump the direct_calls
973 counter. Else force "call" to "calls" conversions. */
974 if (r_type == R_MN10300_PCREL32
975 || r_type == R_MN10300_PCREL16)
976 hash->direct_calls++;
977 else
978 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
982 /* Now look at the actual contents to get the stack size,
983 and a list of what registers were saved in the prologue
984 (ie movm_args). */
985 if ((section->flags & SEC_CODE) != 0)
988 Elf32_External_Sym *esym, *esymend;
989 int idx, shndx;
991 shndx = _bfd_elf_section_from_bfd_section (input_bfd,
992 section);
995 /* Look at each function defined in this section and
996 update info for that function. */
997 esym = extsyms;
998 esymend = esym + symtab_hdr->sh_info;
999 for (; esym < esymend; esym++)
1001 Elf_Internal_Sym isym;
1003 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1004 if (isym.st_shndx == shndx
1005 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1007 if (isym.st_shndx == SHN_UNDEF)
1008 sym_sec = bfd_und_section_ptr;
1009 else if (isym.st_shndx > 0
1010 && isym.st_shndx < SHN_LORESERVE)
1011 sym_sec
1012 = bfd_section_from_elf_index (input_bfd,
1013 isym.st_shndx);
1014 else if (isym.st_shndx == SHN_ABS)
1015 sym_sec = bfd_abs_section_ptr;
1016 else if (isym.st_shndx == SHN_COMMON)
1017 sym_sec = bfd_com_section_ptr;
1019 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1020 symtab_hdr->sh_link,
1021 isym.st_name);
1023 /* Tack on an ID so we can uniquely identify this
1024 local symbol in the global hash table. */
1025 new_name = bfd_malloc (strlen (sym_name) + 10);
1026 if (new_name == 0)
1027 goto error_return;
1029 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1030 sym_name = new_name;
1032 hash = (struct elf32_mn10300_link_hash_entry *)
1033 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1034 sym_name, true,
1035 true, false);
1036 free (new_name);
1037 compute_function_info (input_bfd, hash,
1038 isym.st_value, contents);
1042 esym = extsyms + symtab_hdr->sh_info;
1043 esymend = extsyms + (symtab_hdr->sh_size
1044 / sizeof (Elf32_External_Sym));
1045 for (idx = 0; esym < esymend; esym++, idx++)
1047 Elf_Internal_Sym isym;
1049 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1050 hash = (struct elf32_mn10300_link_hash_entry *)
1051 elf_sym_hashes (input_bfd)[idx];
1052 if (isym.st_shndx == shndx
1053 && ELF_ST_TYPE (isym.st_info) == STT_FUNC
1054 && (hash)->root.root.u.def.section == section
1055 && ((hash)->root.root.type == bfd_link_hash_defined
1056 || (hash)->root.root.type == bfd_link_hash_defweak))
1057 compute_function_info (input_bfd, hash,
1058 (hash)->root.root.u.def.value,
1059 contents);
1063 /* Cache or free any memory we allocated for the relocs. */
1064 if (free_relocs != NULL)
1066 free (free_relocs);
1067 free_relocs = NULL;
1070 /* Cache or free any memory we allocated for the contents. */
1071 if (free_contents != NULL)
1073 if (! link_info->keep_memory)
1074 free (free_contents);
1075 else
1077 /* Cache the section contents for elf_link_input_bfd. */
1078 elf_section_data (section)->this_hdr.contents = contents;
1080 free_contents = NULL;
1084 /* Cache or free any memory we allocated for the symbols. */
1085 if (free_extsyms != NULL)
1087 if (! link_info->keep_memory)
1088 free (free_extsyms);
1089 else
1091 /* Cache the symbols for elf_link_input_bfd. */
1092 symtab_hdr->contents = extsyms;
1094 free_extsyms = NULL;
1098 /* Now iterate on each symbol in the hash table and perform
1099 the final initialization steps on each. */
1100 elf32_mn10300_link_hash_traverse (hash_table,
1101 elf32_mn10300_finish_hash_table_entry,
1102 NULL);
1103 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1104 elf32_mn10300_finish_hash_table_entry,
1105 NULL);
1107 /* All entries in the hash table are fully initialized. */
1108 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1110 /* Now that everything has been initialized, go through each
1111 code section and delete any prologue insns which will be
1112 redundant because their operations will be performed by
1113 a "call" instruction. */
1114 for (input_bfd = link_info->input_bfds;
1115 input_bfd != NULL;
1116 input_bfd = input_bfd->link_next)
1118 asection *section;
1120 /* We're going to need all the symbols for each bfd. */
1121 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1123 /* Get cached copy if it exists. */
1124 if (symtab_hdr->contents != NULL)
1125 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1126 else
1128 /* Go get them off disk. */
1129 extsyms = ((Elf32_External_Sym *)
1130 bfd_malloc (symtab_hdr->sh_size));
1131 if (extsyms == NULL)
1132 goto error_return;
1133 free_extsyms = extsyms;
1134 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1135 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
1136 != symtab_hdr->sh_size))
1137 goto error_return;
1140 /* Walk over each section in this bfd. */
1141 for (section = input_bfd->sections;
1142 section != NULL;
1143 section = section->next)
1145 int shndx;
1146 Elf32_External_Sym *esym, *esymend;
1147 int idx;
1149 /* Skip non-code sections and empty sections. */
1150 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1151 continue;
1153 if (section->reloc_count != 0)
1155 /* Get a copy of the native relocations. */
1156 internal_relocs = (_bfd_elf32_link_read_relocs
1157 (input_bfd, section, (PTR) NULL,
1158 (Elf_Internal_Rela *) NULL,
1159 link_info->keep_memory));
1160 if (internal_relocs == NULL)
1161 goto error_return;
1162 if (! link_info->keep_memory)
1163 free_relocs = internal_relocs;
1166 /* Get cached copy of section contents if it exists. */
1167 if (elf_section_data (section)->this_hdr.contents != NULL)
1168 contents = elf_section_data (section)->this_hdr.contents;
1169 else
1171 /* Go get them off disk. */
1172 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
1173 if (contents == NULL)
1174 goto error_return;
1175 free_contents = contents;
1177 if (!bfd_get_section_contents (input_bfd, section,
1178 contents, (file_ptr) 0,
1179 section->_raw_size))
1180 goto error_return;
1184 shndx = _bfd_elf_section_from_bfd_section (input_bfd, section);
1186 /* Now look for any function in this section which needs
1187 insns deleted from its prologue. */
1188 esym = extsyms;
1189 esymend = esym + symtab_hdr->sh_info;
1190 for (; esym < esymend; esym++)
1192 Elf_Internal_Sym isym;
1193 struct elf32_mn10300_link_hash_entry *sym_hash;
1194 asection *sym_sec;
1195 const char *sym_name;
1196 Elf_Internal_Shdr *hdr;
1197 char *new_name;
1199 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1201 if (isym.st_shndx != shndx)
1202 continue;
1204 if (isym.st_shndx == SHN_UNDEF)
1205 sym_sec = bfd_und_section_ptr;
1206 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1207 sym_sec
1208 = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1209 else if (isym.st_shndx == SHN_ABS)
1210 sym_sec = bfd_abs_section_ptr;
1211 else if (isym.st_shndx == SHN_COMMON)
1212 sym_sec = bfd_com_section_ptr;
1214 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1215 symtab_hdr->sh_link,
1216 isym.st_name);
1218 /* Tack on an ID so we can uniquely identify this
1219 local symbol in the global hash table. */
1220 new_name = bfd_malloc (strlen (sym_name) + 10);
1221 if (new_name == 0)
1222 goto error_return;
1223 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1224 sym_name = new_name;
1226 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1227 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1228 sym_name, false,
1229 false, false);
1231 free (new_name);
1232 if (sym_hash == NULL)
1233 continue;
1235 if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1236 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1238 int bytes = 0;
1240 /* Note that we've changed things. */
1241 elf_section_data (section)->relocs = internal_relocs;
1242 free_relocs = NULL;
1244 elf_section_data (section)->this_hdr.contents = contents;
1245 free_contents = NULL;
1247 symtab_hdr->contents = (bfd_byte *)extsyms;
1248 free_extsyms = NULL;
1250 /* Count how many bytes we're going to delete. */
1251 if (sym_hash->movm_args)
1252 bytes += 2;
1254 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1255 bytes += 3;
1256 else if (sym_hash->stack_size
1257 && sym_hash->stack_size < 256)
1258 bytes += 4;
1260 /* Note that we've deleted prologue bytes for this
1261 function. */
1262 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1264 /* Actually delete the bytes. */
1265 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1266 section,
1267 isym.st_value,
1268 bytes))
1269 goto error_return;
1271 /* Something changed. Not strictly necessary, but
1272 may lead to more relaxing opportunities. */
1273 *again = true;
1277 /* Look for any global functions in this section which
1278 need insns deleted from their prologues. */
1279 esym = extsyms + symtab_hdr->sh_info;
1280 esymend = extsyms + (symtab_hdr->sh_size
1281 / sizeof (Elf32_External_Sym));
1282 for (idx = 0; esym < esymend; esym++, idx++)
1284 Elf_Internal_Sym isym;
1285 struct elf32_mn10300_link_hash_entry *sym_hash;
1287 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1288 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1289 (elf_sym_hashes (input_bfd)[idx]);
1290 if (isym.st_shndx == shndx
1291 && (sym_hash)->root.root.u.def.section == section
1292 && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1293 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1295 int bytes = 0;
1297 /* Note that we've changed things. */
1298 elf_section_data (section)->relocs = internal_relocs;
1299 free_relocs = NULL;
1301 elf_section_data (section)->this_hdr.contents = contents;
1302 free_contents = NULL;
1304 symtab_hdr->contents = (bfd_byte *)extsyms;
1305 free_extsyms = NULL;
1307 /* Count how many bytes we're going to delete. */
1308 if (sym_hash->movm_args)
1309 bytes += 2;
1311 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1312 bytes += 3;
1313 else if (sym_hash->stack_size
1314 && sym_hash->stack_size < 256)
1315 bytes += 4;
1317 /* Note that we've deleted prologue bytes for this
1318 function. */
1319 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1321 /* Actually delete the bytes. */
1322 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1323 section,
1324 (sym_hash)->root.root.u.def.value,
1325 bytes))
1326 goto error_return;
1328 /* Something changed. Not strictly necessary, but
1329 may lead to more relaxing opportunities. */
1330 *again = true;
1334 /* Cache or free any memory we allocated for the relocs. */
1335 if (free_relocs != NULL)
1337 free (free_relocs);
1338 free_relocs = NULL;
1341 /* Cache or free any memory we allocated for the contents. */
1342 if (free_contents != NULL)
1344 if (! link_info->keep_memory)
1345 free (free_contents);
1346 else
1348 /* Cache the section contents for elf_link_input_bfd. */
1349 elf_section_data (section)->this_hdr.contents = contents;
1351 free_contents = NULL;
1355 /* Cache or free any memory we allocated for the symbols. */
1356 if (free_extsyms != NULL)
1358 if (! link_info->keep_memory)
1359 free (free_extsyms);
1360 else
1362 /* Cache the symbols for elf_link_input_bfd. */
1363 symtab_hdr->contents = extsyms;
1365 free_extsyms = NULL;
1371 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1372 contents = NULL;
1373 extsyms = NULL;
1374 internal_relocs = NULL;
1375 free_relocs = NULL;
1376 free_contents = NULL;
1377 free_extsyms = NULL;
1379 /* We don't have to do anything for a relocateable link, if
1380 this section does not have relocs, or if this is not a
1381 code section. */
1382 if (link_info->relocateable
1383 || (sec->flags & SEC_RELOC) == 0
1384 || sec->reloc_count == 0
1385 || (sec->flags & SEC_CODE) == 0)
1386 return true;
1388 /* If this is the first time we have been called for this section,
1389 initialize the cooked size. */
1390 if (sec->_cooked_size == 0)
1391 sec->_cooked_size = sec->_raw_size;
1393 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1395 /* Get a copy of the native relocations. */
1396 internal_relocs = (_bfd_elf32_link_read_relocs
1397 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1398 link_info->keep_memory));
1399 if (internal_relocs == NULL)
1400 goto error_return;
1401 if (! link_info->keep_memory)
1402 free_relocs = internal_relocs;
1404 /* Walk through them looking for relaxing opportunities. */
1405 irelend = internal_relocs + sec->reloc_count;
1406 for (irel = internal_relocs; irel < irelend; irel++)
1408 bfd_vma symval;
1409 struct elf32_mn10300_link_hash_entry *h = NULL;
1411 /* If this isn't something that can be relaxed, then ignore
1412 this reloc. */
1413 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1414 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1415 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1416 continue;
1418 /* Get the section contents if we haven't done so already. */
1419 if (contents == NULL)
1421 /* Get cached copy if it exists. */
1422 if (elf_section_data (sec)->this_hdr.contents != NULL)
1423 contents = elf_section_data (sec)->this_hdr.contents;
1424 else
1426 /* Go get them off disk. */
1427 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1428 if (contents == NULL)
1429 goto error_return;
1430 free_contents = contents;
1432 if (! bfd_get_section_contents (abfd, sec, contents,
1433 (file_ptr) 0, sec->_raw_size))
1434 goto error_return;
1438 /* Read this BFD's symbols if we haven't done so already. */
1439 if (extsyms == NULL)
1441 /* Get cached copy if it exists. */
1442 if (symtab_hdr->contents != NULL)
1443 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1444 else
1446 /* Go get them off disk. */
1447 extsyms = ((Elf32_External_Sym *)
1448 bfd_malloc (symtab_hdr->sh_size));
1449 if (extsyms == NULL)
1450 goto error_return;
1451 free_extsyms = extsyms;
1452 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1453 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1454 != symtab_hdr->sh_size))
1455 goto error_return;
1459 /* Get the value of the symbol referred to by the reloc. */
1460 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1462 Elf_Internal_Sym isym;
1463 asection *sym_sec;
1464 Elf_Internal_Shdr *hdr;
1465 const char *sym_name;
1466 char *new_name;
1468 /* A local symbol. */
1469 bfd_elf32_swap_symbol_in (abfd,
1470 extsyms + ELF32_R_SYM (irel->r_info),
1471 &isym);
1473 if (isym.st_shndx == SHN_UNDEF)
1474 sym_sec = bfd_und_section_ptr;
1475 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1476 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1477 else if (isym.st_shndx == SHN_ABS)
1478 sym_sec = bfd_abs_section_ptr;
1479 else if (isym.st_shndx == SHN_COMMON)
1480 sym_sec = bfd_com_section_ptr;
1482 symval = (isym.st_value
1483 + sym_sec->output_section->vma
1484 + sym_sec->output_offset);
1485 sym_name = bfd_elf_string_from_elf_section (abfd,
1486 symtab_hdr->sh_link,
1487 isym.st_name);
1489 /* Tack on an ID so we can uniquely identify this
1490 local symbol in the global hash table. */
1491 new_name = bfd_malloc (strlen (sym_name) + 10);
1492 if (new_name == 0)
1493 goto error_return;
1494 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1495 sym_name = new_name;
1497 h = (struct elf32_mn10300_link_hash_entry *)
1498 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1499 sym_name, false, false, false);
1500 free (new_name);
1502 else
1504 unsigned long indx;
1506 /* An external symbol. */
1507 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1508 h = (struct elf32_mn10300_link_hash_entry *)
1509 (elf_sym_hashes (abfd)[indx]);
1510 BFD_ASSERT (h != NULL);
1511 if (h->root.root.type != bfd_link_hash_defined
1512 && h->root.root.type != bfd_link_hash_defweak)
1514 /* This appears to be a reference to an undefined
1515 symbol. Just ignore it--it will be caught by the
1516 regular reloc processing. */
1517 continue;
1520 symval = (h->root.root.u.def.value
1521 + h->root.root.u.def.section->output_section->vma
1522 + h->root.root.u.def.section->output_offset);
1525 /* For simplicity of coding, we are going to modify the section
1526 contents, the section relocs, and the BFD symbol table. We
1527 must tell the rest of the code not to free up this
1528 information. It would be possible to instead create a table
1529 of changes which have to be made, as is done in coff-mips.c;
1530 that would be more work, but would require less memory when
1531 the linker is run. */
1533 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1534 branch/call, also deal with "call" -> "calls" conversions and
1535 insertion of prologue data into "call" instructions. */
1536 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1538 bfd_vma value = symval;
1540 /* If we've got a "call" instruction that needs to be turned
1541 into a "calls" instruction, do so now. It saves a byte. */
1542 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1544 unsigned char code;
1546 /* Get the opcode. */
1547 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1549 /* Make sure we're working with a "call" instruction! */
1550 if (code == 0xdd)
1552 /* Note that we've changed the relocs, section contents,
1553 etc. */
1554 elf_section_data (sec)->relocs = internal_relocs;
1555 free_relocs = NULL;
1557 elf_section_data (sec)->this_hdr.contents = contents;
1558 free_contents = NULL;
1560 symtab_hdr->contents = (bfd_byte *) extsyms;
1561 free_extsyms = NULL;
1563 /* Fix the opcode. */
1564 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1565 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1567 /* Fix irel->r_offset and irel->r_addend. */
1568 irel->r_offset += 1;
1569 irel->r_addend += 1;
1571 /* Delete one byte of data. */
1572 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1573 irel->r_offset + 3, 1))
1574 goto error_return;
1576 /* That will change things, so, we should relax again.
1577 Note that this is not required, and it may be slow. */
1578 *again = true;
1581 else if (h)
1583 /* We've got a "call" instruction which needs some data
1584 from target function filled in. */
1585 unsigned char code;
1587 /* Get the opcode. */
1588 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1590 /* Insert data from the target function into the "call"
1591 instruction if needed. */
1592 if (code == 0xdd)
1594 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1595 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1596 contents + irel->r_offset + 5);
1600 /* Deal with pc-relative gunk. */
1601 value -= (sec->output_section->vma + sec->output_offset);
1602 value -= irel->r_offset;
1603 value += irel->r_addend;
1605 /* See if the value will fit in 16 bits, note the high value is
1606 0x7fff + 2 as the target will be two bytes closer if we are
1607 able to relax. */
1608 if ((long)value < 0x8001 && (long)value > -0x8000)
1610 unsigned char code;
1612 /* Get the opcode. */
1613 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1615 if (code != 0xdc && code != 0xdd && code != 0xff)
1616 continue;
1618 /* Note that we've changed the relocs, section contents, etc. */
1619 elf_section_data (sec)->relocs = internal_relocs;
1620 free_relocs = NULL;
1622 elf_section_data (sec)->this_hdr.contents = contents;
1623 free_contents = NULL;
1625 symtab_hdr->contents = (bfd_byte *) extsyms;
1626 free_extsyms = NULL;
1628 /* Fix the opcode. */
1629 if (code == 0xdc)
1630 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1631 else if (code == 0xdd)
1632 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1633 else if (code == 0xff)
1634 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1636 /* Fix the relocation's type. */
1637 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1638 R_MN10300_PCREL16);
1640 /* Delete two bytes of data. */
1641 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1642 irel->r_offset + 1, 2))
1643 goto error_return;
1645 /* That will change things, so, we should relax again.
1646 Note that this is not required, and it may be slow. */
1647 *again = true;
1651 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1652 branch. */
1653 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1655 bfd_vma value = symval;
1657 /* If we've got a "call" instruction that needs to be turned
1658 into a "calls" instruction, do so now. It saves a byte. */
1659 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1661 unsigned char code;
1663 /* Get the opcode. */
1664 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1666 /* Make sure we're working with a "call" instruction! */
1667 if (code == 0xcd)
1669 /* Note that we've changed the relocs, section contents,
1670 etc. */
1671 elf_section_data (sec)->relocs = internal_relocs;
1672 free_relocs = NULL;
1674 elf_section_data (sec)->this_hdr.contents = contents;
1675 free_contents = NULL;
1677 symtab_hdr->contents = (bfd_byte *) extsyms;
1678 free_extsyms = NULL;
1680 /* Fix the opcode. */
1681 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1682 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1684 /* Fix irel->r_offset and irel->r_addend. */
1685 irel->r_offset += 1;
1686 irel->r_addend += 1;
1688 /* Delete one byte of data. */
1689 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1690 irel->r_offset + 1, 1))
1691 goto error_return;
1693 /* That will change things, so, we should relax again.
1694 Note that this is not required, and it may be slow. */
1695 *again = true;
1698 else if (h)
1700 unsigned char code;
1702 /* Get the opcode. */
1703 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1705 /* Insert data from the target function into the "call"
1706 instruction if needed. */
1707 if (code == 0xcd)
1709 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1710 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1711 contents + irel->r_offset + 3);
1715 /* Deal with pc-relative gunk. */
1716 value -= (sec->output_section->vma + sec->output_offset);
1717 value -= irel->r_offset;
1718 value += irel->r_addend;
1720 /* See if the value will fit in 8 bits, note the high value is
1721 0x7f + 1 as the target will be one bytes closer if we are
1722 able to relax. */
1723 if ((long)value < 0x80 && (long)value > -0x80)
1725 unsigned char code;
1727 /* Get the opcode. */
1728 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1730 if (code != 0xcc)
1731 continue;
1733 /* Note that we've changed the relocs, section contents, etc. */
1734 elf_section_data (sec)->relocs = internal_relocs;
1735 free_relocs = NULL;
1737 elf_section_data (sec)->this_hdr.contents = contents;
1738 free_contents = NULL;
1740 symtab_hdr->contents = (bfd_byte *) extsyms;
1741 free_extsyms = NULL;
1743 /* Fix the opcode. */
1744 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1746 /* Fix the relocation's type. */
1747 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1748 R_MN10300_PCREL8);
1750 /* Delete one byte of data. */
1751 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1752 irel->r_offset + 1, 1))
1753 goto error_return;
1755 /* That will change things, so, we should relax again.
1756 Note that this is not required, and it may be slow. */
1757 *again = true;
1761 /* Try to eliminate an unconditional 8 bit pc-relative branch
1762 which immediately follows a conditional 8 bit pc-relative
1763 branch around the unconditional branch.
1765 original: new:
1766 bCC lab1 bCC' lab2
1767 bra lab2
1768 lab1: lab1:
1771 This happens when the bCC can't reach lab2 at assembly time,
1772 but due to other relaxations it can reach at link time. */
1773 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1775 Elf_Internal_Rela *nrel;
1776 bfd_vma value = symval;
1777 unsigned char code;
1779 /* Deal with pc-relative gunk. */
1780 value -= (sec->output_section->vma + sec->output_offset);
1781 value -= irel->r_offset;
1782 value += irel->r_addend;
1784 /* Do nothing if this reloc is the last byte in the section. */
1785 if (irel->r_offset == sec->_cooked_size)
1786 continue;
1788 /* See if the next instruction is an unconditional pc-relative
1789 branch, more often than not this test will fail, so we
1790 test it first to speed things up. */
1791 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1792 if (code != 0xca)
1793 continue;
1795 /* Also make sure the next relocation applies to the next
1796 instruction and that it's a pc-relative 8 bit branch. */
1797 nrel = irel + 1;
1798 if (nrel == irelend
1799 || irel->r_offset + 2 != nrel->r_offset
1800 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1801 continue;
1803 /* Make sure our destination immediately follows the
1804 unconditional branch. */
1805 if (symval != (sec->output_section->vma + sec->output_offset
1806 + irel->r_offset + 3))
1807 continue;
1809 /* Now make sure we are a conditional branch. This may not
1810 be necessary, but why take the chance.
1812 Note these checks assume that R_MN10300_PCREL8 relocs
1813 only occur on bCC and bCCx insns. If they occured
1814 elsewhere, we'd need to know the start of this insn
1815 for this check to be accurate. */
1816 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1817 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1818 && code != 0xc3 && code != 0xc4 && code != 0xc5
1819 && code != 0xc6 && code != 0xc7 && code != 0xc8
1820 && code != 0xc9 && code != 0xe8 && code != 0xe9
1821 && code != 0xea && code != 0xeb)
1822 continue;
1824 /* We also have to be sure there is no symbol/label
1825 at the unconditional branch. */
1826 if (mn10300_elf_symbol_address_p (abfd, sec, extsyms,
1827 irel->r_offset + 1))
1828 continue;
1830 /* Note that we've changed the relocs, section contents, etc. */
1831 elf_section_data (sec)->relocs = internal_relocs;
1832 free_relocs = NULL;
1834 elf_section_data (sec)->this_hdr.contents = contents;
1835 free_contents = NULL;
1837 symtab_hdr->contents = (bfd_byte *) extsyms;
1838 free_extsyms = NULL;
1840 /* Reverse the condition of the first branch. */
1841 switch (code)
1843 case 0xc8:
1844 code = 0xc9;
1845 break;
1846 case 0xc9:
1847 code = 0xc8;
1848 break;
1849 case 0xc0:
1850 code = 0xc2;
1851 break;
1852 case 0xc2:
1853 code = 0xc0;
1854 break;
1855 case 0xc3:
1856 code = 0xc1;
1857 break;
1858 case 0xc1:
1859 code = 0xc3;
1860 break;
1861 case 0xc4:
1862 code = 0xc6;
1863 break;
1864 case 0xc6:
1865 code = 0xc4;
1866 break;
1867 case 0xc7:
1868 code = 0xc5;
1869 break;
1870 case 0xc5:
1871 code = 0xc7;
1872 break;
1873 case 0xe8:
1874 code = 0xe9;
1875 break;
1876 case 0x9d:
1877 code = 0xe8;
1878 break;
1879 case 0xea:
1880 code = 0xeb;
1881 break;
1882 case 0xeb:
1883 code = 0xea;
1884 break;
1886 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1888 /* Set the reloc type and symbol for the first branch
1889 from the second branch. */
1890 irel->r_info = nrel->r_info;
1892 /* Make the reloc for the second branch a null reloc. */
1893 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1894 R_MN10300_NONE);
1896 /* Delete two bytes of data. */
1897 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1898 irel->r_offset + 1, 2))
1899 goto error_return;
1901 /* That will change things, so, we should relax again.
1902 Note that this is not required, and it may be slow. */
1903 *again = true;
1907 /* Try to turn a 32bit immediate, displacement or absolute address
1908 into a 16bit immediate, displacement or absolute address. */
1909 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1911 bfd_vma value = symval;
1912 value += irel->r_addend;
1915 /* See if the value will fit in 16 bits.
1916 We allow any 16bit match here. We prune those we can't
1917 handle below. */
1918 if ((long)value < 0x7fff && (long)value > -0x8000)
1920 unsigned char code;
1922 /* Most insns which have 32bit operands are 6 bytes long;
1923 exceptions are pcrel insns and bit insns.
1925 We handle pcrel insns above. We don't bother trying
1926 to handle the bit insns here.
1928 The first byte of the remaining insns will be 0xfc. */
1930 /* Get the first opcode. */
1931 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1933 if (code != 0xfc)
1934 continue;
1936 /* Get the second opcode. */
1937 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1939 if ((code & 0xf0) < 0x80)
1940 switch (code & 0xf0)
1942 /* mov (d32,am),dn -> mov (d32,am),dn
1943 mov dm,(d32,am) -> mov dn,(d32,am)
1944 mov (d32,am),an -> mov (d32,am),an
1945 mov dm,(d32,am) -> mov dn,(d32,am)
1946 movbu (d32,am),dn -> movbu (d32,am),dn
1947 movbu dm,(d32,am) -> movbu dn,(d32,am)
1948 movhu (d32,am),dn -> movhu (d32,am),dn
1949 movhu dm,(d32,am) -> movhu dn,(d32,am) */
1950 case 0x00:
1951 case 0x10:
1952 case 0x20:
1953 case 0x30:
1954 case 0x40:
1955 case 0x50:
1956 case 0x60:
1957 case 0x70:
1958 /* Not safe if the high bit is on as relaxing may
1959 move the value out of high mem and thus not fit
1960 in a signed 16bit value. */
1961 if (code == 0xcc
1962 && (value & 0x8000))
1963 continue;
1965 /* Note that we've changed the relocation contents, etc. */
1966 elf_section_data (sec)->relocs = internal_relocs;
1967 free_relocs = NULL;
1969 elf_section_data (sec)->this_hdr.contents = contents;
1970 free_contents = NULL;
1972 symtab_hdr->contents = (bfd_byte *) extsyms;
1973 free_extsyms = NULL;
1975 /* Fix the opcode. */
1976 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1977 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1979 /* Fix the relocation's type. */
1980 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1981 R_MN10300_16);
1983 /* Delete two bytes of data. */
1984 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1985 irel->r_offset + 2, 2))
1986 goto error_return;
1988 /* That will change things, so, we should relax again.
1989 Note that this is not required, and it may be slow. */
1990 *again = true;
1991 break;
1993 else if ((code & 0xf0) == 0x80
1994 || (code & 0xf0) == 0x90)
1995 switch (code & 0xf3)
1997 /* mov dn,(abs32) -> mov dn,(abs16)
1998 movbu dn,(abs32) -> movbu dn,(abs16)
1999 movhu dn,(abs32) -> movhu dn,(abs16) */
2000 case 0x81:
2001 case 0x82:
2002 case 0x83:
2003 /* Note that we've changed the relocation contents, etc. */
2004 elf_section_data (sec)->relocs = internal_relocs;
2005 free_relocs = NULL;
2007 elf_section_data (sec)->this_hdr.contents = contents;
2008 free_contents = NULL;
2010 symtab_hdr->contents = (bfd_byte *) extsyms;
2011 free_extsyms = NULL;
2013 if ((code & 0xf3) == 0x81)
2014 code = 0x01 + (code & 0x0c);
2015 else if ((code & 0xf3) == 0x82)
2016 code = 0x02 + (code & 0x0c);
2017 else if ((code & 0xf3) == 0x83)
2018 code = 0x03 + (code & 0x0c);
2019 else
2020 abort ();
2022 /* Fix the opcode. */
2023 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2025 /* Fix the relocation's type. */
2026 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2027 R_MN10300_16);
2029 /* The opcode got shorter too, so we have to fix the
2030 addend and offset too! */
2031 irel->r_offset -= 1;
2033 /* Delete three bytes of data. */
2034 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2035 irel->r_offset + 1, 3))
2036 goto error_return;
2038 /* That will change things, so, we should relax again.
2039 Note that this is not required, and it may be slow. */
2040 *again = true;
2041 break;
2043 /* mov am,(abs32) -> mov am,(abs16)
2044 mov am,(d32,sp) -> mov am,(d16,sp)
2045 mov dm,(d32,sp) -> mov dm,(d32,sp)
2046 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2047 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2048 case 0x80:
2049 case 0x90:
2050 case 0x91:
2051 case 0x92:
2052 case 0x93:
2053 /* Note that we've changed the relocation contents, etc. */
2054 elf_section_data (sec)->relocs = internal_relocs;
2055 free_relocs = NULL;
2057 elf_section_data (sec)->this_hdr.contents = contents;
2058 free_contents = NULL;
2060 symtab_hdr->contents = (bfd_byte *) extsyms;
2061 free_extsyms = NULL;
2063 /* Fix the opcode. */
2064 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2065 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2067 /* Fix the relocation's type. */
2068 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2069 R_MN10300_16);
2071 /* Delete two bytes of data. */
2072 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2073 irel->r_offset + 2, 2))
2074 goto error_return;
2076 /* That will change things, so, we should relax again.
2077 Note that this is not required, and it may be slow. */
2078 *again = true;
2079 break;
2081 else if ((code & 0xf0) < 0xf0)
2082 switch (code & 0xfc)
2084 /* mov imm32,dn -> mov imm16,dn
2085 mov imm32,an -> mov imm16,an
2086 mov (abs32),dn -> mov (abs16),dn
2087 movbu (abs32),dn -> movbu (abs16),dn
2088 movhu (abs32),dn -> movhu (abs16),dn */
2089 case 0xcc:
2090 case 0xdc:
2091 case 0xa4:
2092 case 0xa8:
2093 case 0xac:
2094 /* Not safe if the high bit is on as relaxing may
2095 move the value out of high mem and thus not fit
2096 in a signed 16bit value. */
2097 if (code == 0xcc
2098 && (value & 0x8000))
2099 continue;
2101 /* Note that we've changed the relocation contents, etc. */
2102 elf_section_data (sec)->relocs = internal_relocs;
2103 free_relocs = NULL;
2105 elf_section_data (sec)->this_hdr.contents = contents;
2106 free_contents = NULL;
2108 symtab_hdr->contents = (bfd_byte *) extsyms;
2109 free_extsyms = NULL;
2111 if ((code & 0xfc) == 0xcc)
2112 code = 0x2c + (code & 0x03);
2113 else if ((code & 0xfc) == 0xdc)
2114 code = 0x24 + (code & 0x03);
2115 else if ((code & 0xfc) == 0xa4)
2116 code = 0x30 + (code & 0x03);
2117 else if ((code & 0xfc) == 0xa8)
2118 code = 0x34 + (code & 0x03);
2119 else if ((code & 0xfc) == 0xac)
2120 code = 0x38 + (code & 0x03);
2121 else
2122 abort ();
2124 /* Fix the opcode. */
2125 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2127 /* Fix the relocation's type. */
2128 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2129 R_MN10300_16);
2131 /* The opcode got shorter too, so we have to fix the
2132 addend and offset too! */
2133 irel->r_offset -= 1;
2135 /* Delete three bytes of data. */
2136 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2137 irel->r_offset + 1, 3))
2138 goto error_return;
2140 /* That will change things, so, we should relax again.
2141 Note that this is not required, and it may be slow. */
2142 *again = true;
2143 break;
2145 /* mov (abs32),an -> mov (abs16),an
2146 mov (d32,sp),an -> mov (d32,sp),an
2147 mov (d32,sp),dn -> mov (d32,sp),dn
2148 movbu (d32,sp),dn -> movbu (d32,sp),dn
2149 movhu (d32,sp),dn -> movhu (d32,sp),dn
2150 add imm32,dn -> add imm16,dn
2151 cmp imm32,dn -> cmp imm16,dn
2152 add imm32,an -> add imm16,an
2153 cmp imm32,an -> cmp imm16,an
2154 and imm32,dn -> and imm32,dn
2155 or imm32,dn -> or imm32,dn
2156 xor imm32,dn -> xor imm32,dn
2157 btst imm32,dn -> btst imm32,dn */
2159 case 0xa0:
2160 case 0xb0:
2161 case 0xb1:
2162 case 0xb2:
2163 case 0xb3:
2164 case 0xc0:
2165 case 0xc8:
2167 case 0xd0:
2168 case 0xd8:
2169 case 0xe0:
2170 case 0xe1:
2171 case 0xe2:
2172 case 0xe3:
2173 /* Note that we've changed the relocation contents, etc. */
2174 elf_section_data (sec)->relocs = internal_relocs;
2175 free_relocs = NULL;
2177 elf_section_data (sec)->this_hdr.contents = contents;
2178 free_contents = NULL;
2180 symtab_hdr->contents = (bfd_byte *) extsyms;
2181 free_extsyms = NULL;
2183 /* Fix the opcode. */
2184 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2185 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2187 /* Fix the relocation's type. */
2188 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2189 R_MN10300_16);
2191 /* Delete two bytes of data. */
2192 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2193 irel->r_offset + 2, 2))
2194 goto error_return;
2196 /* That will change things, so, we should relax again.
2197 Note that this is not required, and it may be slow. */
2198 *again = true;
2199 break;
2201 else if (code == 0xfe)
2203 /* add imm32,sp -> add imm16,sp */
2205 /* Note that we've changed the relocation contents, etc. */
2206 elf_section_data (sec)->relocs = internal_relocs;
2207 free_relocs = NULL;
2209 elf_section_data (sec)->this_hdr.contents = contents;
2210 free_contents = NULL;
2212 symtab_hdr->contents = (bfd_byte *) extsyms;
2213 free_extsyms = NULL;
2215 /* Fix the opcode. */
2216 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2217 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2219 /* Fix the relocation's type. */
2220 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2221 R_MN10300_16);
2223 /* Delete two bytes of data. */
2224 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2225 irel->r_offset + 2, 2))
2226 goto error_return;
2228 /* That will change things, so, we should relax again.
2229 Note that this is not required, and it may be slow. */
2230 *again = true;
2231 break;
2237 if (free_relocs != NULL)
2239 free (free_relocs);
2240 free_relocs = NULL;
2243 if (free_contents != NULL)
2245 if (! link_info->keep_memory)
2246 free (free_contents);
2247 else
2249 /* Cache the section contents for elf_link_input_bfd. */
2250 elf_section_data (sec)->this_hdr.contents = contents;
2252 free_contents = NULL;
2255 if (free_extsyms != NULL)
2257 if (! link_info->keep_memory)
2258 free (free_extsyms);
2259 else
2261 /* Cache the symbols for elf_link_input_bfd. */
2262 symtab_hdr->contents = extsyms;
2264 free_extsyms = NULL;
2267 return true;
2269 error_return:
2270 if (free_relocs != NULL)
2271 free (free_relocs);
2272 if (free_contents != NULL)
2273 free (free_contents);
2274 if (free_extsyms != NULL)
2275 free (free_extsyms);
2276 return false;
2279 /* Compute the stack size and movm arguments for the function
2280 referred to by HASH at address ADDR in section with
2281 contents CONTENTS, store the information in the hash table. */
2282 static void
2283 compute_function_info (abfd, hash, addr, contents)
2284 bfd *abfd;
2285 struct elf32_mn10300_link_hash_entry *hash;
2286 bfd_vma addr;
2287 unsigned char *contents;
2289 unsigned char byte1, byte2;
2290 /* We only care about a very small subset of the possible prologue
2291 sequences here. Basically we look for:
2293 movm [d2,d3,a2,a3],sp (optional)
2294 add <size>,sp (optional, and only for sizes which fit in an unsigned
2295 8 bit number)
2297 If we find anything else, we quit. */
2299 /* Look for movm [regs],sp */
2300 byte1 = bfd_get_8 (abfd, contents + addr);
2301 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2303 if (byte1 == 0xcf)
2305 hash->movm_args = byte2;
2306 addr += 2;
2307 byte1 = bfd_get_8 (abfd, contents + addr);
2308 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2311 /* Now figure out how much stack space will be allocated by the movm
2312 instruction. We need this kept separate from the funtion's normal
2313 stack space. */
2314 if (hash->movm_args)
2316 /* Space for d2. */
2317 if (hash->movm_args & 0x80)
2318 hash->movm_stack_size += 4;
2320 /* Space for d3. */
2321 if (hash->movm_args & 0x40)
2322 hash->movm_stack_size += 4;
2324 /* Space for a2. */
2325 if (hash->movm_args & 0x20)
2326 hash->movm_stack_size += 4;
2328 /* Space for a3. */
2329 if (hash->movm_args & 0x10)
2330 hash->movm_stack_size += 4;
2332 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2333 if (hash->movm_args & 0x08)
2334 hash->movm_stack_size += 8 * 4;
2338 /* Now look for the two stack adjustment variants. */
2339 if (byte1 == 0xf8 && byte2 == 0xfe)
2341 int temp = bfd_get_8 (abfd, contents + addr + 2);
2342 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2344 hash->stack_size = -temp;
2346 else if (byte1 == 0xfa && byte2 == 0xfe)
2348 int temp = bfd_get_16 (abfd, contents + addr + 2);
2349 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2350 temp = -temp;
2352 if (temp < 255)
2353 hash->stack_size = temp;
2356 /* If the total stack to be allocated by the call instruction is more
2357 than 255 bytes, then we can't remove the stack adjustment by using
2358 "call" (we might still be able to remove the "movm" instruction. */
2359 if (hash->stack_size + hash->movm_stack_size > 255)
2360 hash->stack_size = 0;
2362 return;
2365 /* Delete some bytes from a section while relaxing. */
2367 static boolean
2368 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2369 bfd *abfd;
2370 asection *sec;
2371 bfd_vma addr;
2372 int count;
2374 Elf_Internal_Shdr *symtab_hdr;
2375 Elf32_External_Sym *extsyms;
2376 int shndx, index;
2377 bfd_byte *contents;
2378 Elf_Internal_Rela *irel, *irelend;
2379 Elf_Internal_Rela *irelalign;
2380 bfd_vma toaddr;
2381 Elf32_External_Sym *esym, *esymend;
2382 struct elf32_mn10300_link_hash_entry *sym_hash;
2384 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2385 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2387 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2389 contents = elf_section_data (sec)->this_hdr.contents;
2391 /* The deletion must stop at the next ALIGN reloc for an aligment
2392 power larger than the number of bytes we are deleting. */
2394 irelalign = NULL;
2395 toaddr = sec->_cooked_size;
2397 irel = elf_section_data (sec)->relocs;
2398 irelend = irel + sec->reloc_count;
2400 /* Actually delete the bytes. */
2401 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
2402 sec->_cooked_size -= count;
2404 /* Adjust all the relocs. */
2405 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2407 /* Get the new reloc address. */
2408 if ((irel->r_offset > addr
2409 && irel->r_offset < toaddr))
2410 irel->r_offset -= count;
2413 /* Adjust the local symbols defined in this section. */
2414 esym = extsyms;
2415 esymend = esym + symtab_hdr->sh_info;
2416 for (; esym < esymend; esym++)
2418 Elf_Internal_Sym isym;
2420 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2422 if (isym.st_shndx == shndx
2423 && isym.st_value > addr
2424 && isym.st_value < toaddr)
2426 isym.st_value -= count;
2427 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
2431 /* Now adjust the global symbols defined in this section. */
2432 esym = extsyms + symtab_hdr->sh_info;
2433 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2434 for (index = 0; esym < esymend; esym++, index++)
2436 Elf_Internal_Sym isym;
2438 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2439 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2440 (elf_sym_hashes (abfd)[index]);
2441 if (isym.st_shndx == shndx
2442 && ((sym_hash)->root.root.type == bfd_link_hash_defined
2443 || (sym_hash)->root.root.type == bfd_link_hash_defweak)
2444 && (sym_hash)->root.root.u.def.section == sec
2445 && (sym_hash)->root.root.u.def.value > addr
2446 && (sym_hash)->root.root.u.def.value < toaddr)
2448 (sym_hash)->root.root.u.def.value -= count;
2452 return true;
2455 /* Return true if a symbol exists at the given address, else return
2456 false. */
2457 static boolean
2458 mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr)
2459 bfd *abfd;
2460 asection *sec;
2461 Elf32_External_Sym *extsyms;
2462 bfd_vma addr;
2464 Elf_Internal_Shdr *symtab_hdr;
2465 int shndx;
2466 Elf32_External_Sym *esym, *esymend;
2467 struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end;
2469 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2470 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2472 /* Examine all the symbols. */
2473 esym = extsyms;
2474 esymend = esym + symtab_hdr->sh_info;
2475 for (; esym < esymend; esym++)
2477 Elf_Internal_Sym isym;
2479 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2481 if (isym.st_shndx == shndx
2482 && isym.st_value == addr)
2483 return true;
2486 sym_hash = (struct elf32_mn10300_link_hash_entry **)(elf_sym_hashes (abfd));
2487 sym_hash_end = (sym_hash
2488 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2489 - symtab_hdr->sh_info));
2490 for (; sym_hash < sym_hash_end; sym_hash++)
2492 if (((*sym_hash)->root.root.type == bfd_link_hash_defined
2493 || (*sym_hash)->root.root.type == bfd_link_hash_defweak)
2494 && (*sym_hash)->root.root.u.def.section == sec
2495 && (*sym_hash)->root.root.u.def.value == addr)
2496 return true;
2498 return false;
2501 /* This is a version of bfd_generic_get_relocated_section_contents
2502 which uses mn10300_elf_relocate_section. */
2504 static bfd_byte *
2505 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2506 data, relocateable, symbols)
2507 bfd *output_bfd;
2508 struct bfd_link_info *link_info;
2509 struct bfd_link_order *link_order;
2510 bfd_byte *data;
2511 boolean relocateable;
2512 asymbol **symbols;
2514 Elf_Internal_Shdr *symtab_hdr;
2515 asection *input_section = link_order->u.indirect.section;
2516 bfd *input_bfd = input_section->owner;
2517 asection **sections = NULL;
2518 Elf_Internal_Rela *internal_relocs = NULL;
2519 Elf32_External_Sym *external_syms = NULL;
2520 Elf_Internal_Sym *internal_syms = NULL;
2522 /* We only need to handle the case of relaxing, or of having a
2523 particular set of section contents, specially. */
2524 if (relocateable
2525 || elf_section_data (input_section)->this_hdr.contents == NULL)
2526 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2527 link_order, data,
2528 relocateable,
2529 symbols);
2531 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2533 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2534 input_section->_raw_size);
2536 if ((input_section->flags & SEC_RELOC) != 0
2537 && input_section->reloc_count > 0)
2539 Elf_Internal_Sym *isymp;
2540 asection **secpp;
2541 Elf32_External_Sym *esym, *esymend;
2543 if (symtab_hdr->contents != NULL)
2544 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2545 else
2547 external_syms = ((Elf32_External_Sym *)
2548 bfd_malloc (symtab_hdr->sh_info
2549 * sizeof (Elf32_External_Sym)));
2550 if (external_syms == NULL && symtab_hdr->sh_info > 0)
2551 goto error_return;
2552 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2553 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
2554 symtab_hdr->sh_info, input_bfd)
2555 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2556 goto error_return;
2559 internal_relocs = (_bfd_elf32_link_read_relocs
2560 (input_bfd, input_section, (PTR) NULL,
2561 (Elf_Internal_Rela *) NULL, false));
2562 if (internal_relocs == NULL)
2563 goto error_return;
2565 internal_syms = ((Elf_Internal_Sym *)
2566 bfd_malloc (symtab_hdr->sh_info
2567 * sizeof (Elf_Internal_Sym)));
2568 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
2569 goto error_return;
2571 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2572 * sizeof (asection *));
2573 if (sections == NULL && symtab_hdr->sh_info > 0)
2574 goto error_return;
2576 isymp = internal_syms;
2577 secpp = sections;
2578 esym = external_syms;
2579 esymend = esym + symtab_hdr->sh_info;
2580 for (; esym < esymend; ++esym, ++isymp, ++secpp)
2582 asection *isec;
2584 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2586 if (isymp->st_shndx == SHN_UNDEF)
2587 isec = bfd_und_section_ptr;
2588 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2589 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2590 else if (isymp->st_shndx == SHN_ABS)
2591 isec = bfd_abs_section_ptr;
2592 else if (isymp->st_shndx == SHN_COMMON)
2593 isec = bfd_com_section_ptr;
2594 else
2596 /* Who knows? */
2597 isec = NULL;
2600 *secpp = isec;
2603 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2604 input_section, data, internal_relocs,
2605 internal_syms, sections))
2606 goto error_return;
2608 if (sections != NULL)
2609 free (sections);
2610 sections = NULL;
2611 if (internal_syms != NULL)
2612 free (internal_syms);
2613 internal_syms = NULL;
2614 if (external_syms != NULL && symtab_hdr->contents == NULL)
2615 free (external_syms);
2616 external_syms = NULL;
2617 if (internal_relocs != elf_section_data (input_section)->relocs)
2618 free (internal_relocs);
2619 internal_relocs = NULL;
2622 return data;
2624 error_return:
2625 if (internal_relocs != NULL
2626 && internal_relocs != elf_section_data (input_section)->relocs)
2627 free (internal_relocs);
2628 if (external_syms != NULL && symtab_hdr->contents == NULL)
2629 free (external_syms);
2630 if (internal_syms != NULL)
2631 free (internal_syms);
2632 if (sections != NULL)
2633 free (sections);
2634 return NULL;
2637 /* Assorted hash table functions. */
2639 /* Initialize an entry in the link hash table. */
2641 /* Create an entry in an MN10300 ELF linker hash table. */
2643 static struct bfd_hash_entry *
2644 elf32_mn10300_link_hash_newfunc (entry, table, string)
2645 struct bfd_hash_entry *entry;
2646 struct bfd_hash_table *table;
2647 const char *string;
2649 struct elf32_mn10300_link_hash_entry *ret =
2650 (struct elf32_mn10300_link_hash_entry *) entry;
2652 /* Allocate the structure if it has not already been allocated by a
2653 subclass. */
2654 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2655 ret = ((struct elf32_mn10300_link_hash_entry *)
2656 bfd_hash_allocate (table,
2657 sizeof (struct elf32_mn10300_link_hash_entry)));
2658 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2659 return (struct bfd_hash_entry *) ret;
2661 /* Call the allocation method of the superclass. */
2662 ret = ((struct elf32_mn10300_link_hash_entry *)
2663 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2664 table, string));
2665 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2667 ret->direct_calls = 0;
2668 ret->stack_size = 0;
2669 ret->movm_stack_size = 0;
2670 ret->flags = 0;
2671 ret->movm_args = 0;
2674 return (struct bfd_hash_entry *) ret;
2677 /* Create an mn10300 ELF linker hash table. */
2679 static struct bfd_link_hash_table *
2680 elf32_mn10300_link_hash_table_create (abfd)
2681 bfd *abfd;
2683 struct elf32_mn10300_link_hash_table *ret;
2685 ret = ((struct elf32_mn10300_link_hash_table *)
2686 bfd_alloc (abfd, sizeof (struct elf32_mn10300_link_hash_table)));
2687 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2688 return NULL;
2690 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2691 elf32_mn10300_link_hash_newfunc))
2693 bfd_release (abfd, ret);
2694 return NULL;
2697 ret->flags = 0;
2698 ret->static_hash_table
2699 = ((struct elf32_mn10300_link_hash_table *)
2700 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
2701 if (ret->static_hash_table == NULL)
2703 bfd_release (abfd, ret);
2704 return NULL;
2707 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2708 elf32_mn10300_link_hash_newfunc))
2710 bfd_release (abfd, ret->static_hash_table);
2711 bfd_release (abfd, ret);
2712 return NULL;
2714 return &ret->root.root;
2717 static int
2718 elf_mn10300_mach (flags)
2719 flagword flags;
2721 switch (flags & EF_MN10300_MACH)
2723 case E_MN10300_MACH_MN10300:
2724 default:
2725 return bfd_mach_mn10300;
2730 /* The final processing done just before writing out a MN10300 ELF object
2731 file. This gets the MN10300 architecture right based on the machine
2732 number. */
2734 /*ARGSUSED*/
2735 void
2736 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2737 bfd *abfd;
2738 boolean linker;
2740 unsigned long val;
2741 unsigned int i;
2742 Elf_Internal_Shdr **hdrpp;
2743 const char *name;
2744 asection *sec;
2746 switch (bfd_get_mach (abfd))
2748 default:
2749 case bfd_mach_mn10300:
2750 val = E_MN10300_MACH_MN10300;
2751 break;
2755 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2756 elf_elfheader (abfd)->e_flags |= val;
2759 boolean
2760 _bfd_mn10300_elf_object_p (abfd)
2761 bfd *abfd;
2763 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2764 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2765 return true;
2768 /* Merge backend specific data from an object file to the output
2769 object file when linking. */
2771 boolean
2772 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2773 bfd *ibfd;
2774 bfd *obfd;
2776 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2777 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2778 return true;
2780 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2781 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2783 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2784 bfd_get_mach (ibfd)))
2785 return false;
2788 return true;
2792 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2793 #define TARGET_LITTLE_NAME "elf32-mn10300"
2794 #define ELF_ARCH bfd_arch_mn10300
2795 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
2796 #define ELF_MAXPAGESIZE 0x1000
2798 #define elf_info_to_howto mn10300_info_to_howto
2799 #define elf_info_to_howto_rel 0
2800 #define elf_backend_can_gc_sections 1
2801 #define elf_backend_check_relocs mn10300_elf_check_relocs
2802 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2803 #define elf_backend_relocate_section mn10300_elf_relocate_section
2804 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2805 #define bfd_elf32_bfd_get_relocated_section_contents \
2806 mn10300_elf_get_relocated_section_contents
2807 #define bfd_elf32_bfd_link_hash_table_create \
2808 elf32_mn10300_link_hash_table_create
2810 #define elf_symbol_leading_char '_'
2812 /* So we can set bits in e_flags. */
2813 #define elf_backend_final_write_processing \
2814 _bfd_mn10300_elf_final_write_processing
2815 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2817 #define bfd_elf32_bfd_merge_private_bfd_data \
2818 _bfd_mn10300_elf_merge_private_bfd_data
2821 #include "elf32-target.h"