* elf64-alpha.c (elf64_alpha_relocate_section) [BRSGP]: A target
[binutils.git] / bfd / elf-m10300.c
blobe006ac2581b47866dfdd3ca58e1f6354e6f5d1e8
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/mn10300.h"
27 static bfd_reloc_status_type mn10300_elf_final_link_relocate
28 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
29 bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *,
30 asection *, int));
31 static boolean mn10300_elf_relocate_section
32 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
33 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
34 static boolean mn10300_elf_relax_section
35 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
36 static bfd_byte * mn10300_elf_get_relocated_section_contents
37 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
38 bfd_byte *, boolean, asymbol **));
39 static unsigned long elf_mn10300_mach PARAMS ((flagword));
41 void _bfd_mn10300_elf_final_write_processing PARAMS ((bfd *, boolean));
42 boolean _bfd_mn10300_elf_object_p PARAMS ((bfd *));
43 boolean _bfd_mn10300_elf_merge_private_bfd_data PARAMS ((bfd *,bfd *));
45 struct elf32_mn10300_link_hash_entry {
46 /* The basic elf link hash table entry. */
47 struct elf_link_hash_entry root;
49 /* For function symbols, the number of times this function is
50 called directly (ie by name). */
51 unsigned int direct_calls;
53 /* For function symbols, the size of this function's stack
54 (if <= 255 bytes). We stuff this into "call" instructions
55 to this target when it's valid and profitable to do so.
57 This does not include stack allocated by movm! */
58 unsigned char stack_size;
60 /* For function symbols, arguments (if any) for movm instruction
61 in the prologue. We stuff this value into "call" instructions
62 to the target when it's valid and profitable to do so. */
63 unsigned char movm_args;
65 /* For funtion symbols, the amount of stack space that would be allocated
66 by the movm instruction. This is redundant with movm_args, but we
67 add it to the hash table to avoid computing it over and over. */
68 unsigned char movm_stack_size;
70 /* When set, convert all "call" instructions to this target into "calls"
71 instructions. */
72 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
74 /* Used to mark functions which have had redundant parts of their
75 prologue deleted. */
76 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
77 unsigned char flags;
80 /* We derive a hash table from the main elf linker hash table so
81 we can store state variables and a secondary hash table without
82 resorting to global variables. */
83 struct elf32_mn10300_link_hash_table {
84 /* The main hash table. */
85 struct elf_link_hash_table root;
87 /* A hash table for static functions. We could derive a new hash table
88 instead of using the full elf32_mn10300_link_hash_table if we wanted
89 to save some memory. */
90 struct elf32_mn10300_link_hash_table *static_hash_table;
92 /* Random linker state flags. */
93 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
94 char flags;
97 /* For MN10300 linker hash table. */
99 /* Get the MN10300 ELF linker hash table from a link_info structure. */
101 #define elf32_mn10300_hash_table(p) \
102 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
104 #define elf32_mn10300_link_hash_traverse(table, func, info) \
105 (elf_link_hash_traverse \
106 (&(table)->root, \
107 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
108 (info)))
110 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
111 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
112 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
113 PARAMS ((bfd *));
115 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
116 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
117 static void mn10300_info_to_howto
118 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
119 static boolean mn10300_elf_check_relocs
120 PARAMS ((bfd *, struct bfd_link_info *, asection *,
121 const Elf_Internal_Rela *));
122 static asection *mn10300_elf_gc_mark_hook
123 PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *,
124 struct elf_link_hash_entry *, Elf_Internal_Sym *));
125 static boolean mn10300_elf_relax_delete_bytes
126 PARAMS ((bfd *, asection *, bfd_vma, int));
127 static boolean mn10300_elf_symbol_address_p
128 PARAMS ((bfd *, asection *, bfd_vma));
129 static boolean elf32_mn10300_finish_hash_table_entry
130 PARAMS ((struct bfd_hash_entry *, PTR));
131 static void compute_function_info
132 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
133 bfd_vma, unsigned char *));
135 /* We have to use RELA instructions since md_apply_fix3 in the assembler
136 does absolutely nothing. */
137 #define USE_RELA
139 static reloc_howto_type elf_mn10300_howto_table[] = {
140 /* Dummy relocation. Does nothing. */
141 HOWTO (R_MN10300_NONE,
145 false,
147 complain_overflow_bitfield,
148 bfd_elf_generic_reloc,
149 "R_MN10300_NONE",
150 false,
153 false),
154 /* Standard 32 bit reloc. */
155 HOWTO (R_MN10300_32,
159 false,
161 complain_overflow_bitfield,
162 bfd_elf_generic_reloc,
163 "R_MN10300_32",
164 false,
165 0xffffffff,
166 0xffffffff,
167 false),
168 /* Standard 16 bit reloc. */
169 HOWTO (R_MN10300_16,
173 false,
175 complain_overflow_bitfield,
176 bfd_elf_generic_reloc,
177 "R_MN10300_16",
178 false,
179 0xffff,
180 0xffff,
181 false),
182 /* Standard 8 bit reloc. */
183 HOWTO (R_MN10300_8,
187 false,
189 complain_overflow_bitfield,
190 bfd_elf_generic_reloc,
191 "R_MN10300_8",
192 false,
193 0xff,
194 0xff,
195 false),
196 /* Standard 32bit pc-relative reloc. */
197 HOWTO (R_MN10300_PCREL32,
201 true,
203 complain_overflow_bitfield,
204 bfd_elf_generic_reloc,
205 "R_MN10300_PCREL32",
206 false,
207 0xffffffff,
208 0xffffffff,
209 true),
210 /* Standard 16bit pc-relative reloc. */
211 HOWTO (R_MN10300_PCREL16,
215 true,
217 complain_overflow_bitfield,
218 bfd_elf_generic_reloc,
219 "R_MN10300_PCREL16",
220 false,
221 0xffff,
222 0xffff,
223 true),
224 /* Standard 8 pc-relative reloc. */
225 HOWTO (R_MN10300_PCREL8,
229 true,
231 complain_overflow_bitfield,
232 bfd_elf_generic_reloc,
233 "R_MN10300_PCREL8",
234 false,
235 0xff,
236 0xff,
237 true),
239 /* GNU extension to record C++ vtable hierarchy */
240 HOWTO (R_MN10300_GNU_VTINHERIT, /* 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_VTINHERIT", /* name */
249 false, /* partial_inplace */
250 0, /* src_mask */
251 0, /* dst_mask */
252 false), /* pcrel_offset */
254 /* GNU extension to record C++ vtable member usage */
255 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
256 0, /* rightshift */
257 0, /* size (0 = byte, 1 = short, 2 = long) */
258 0, /* bitsize */
259 false, /* pc_relative */
260 0, /* bitpos */
261 complain_overflow_dont, /* complain_on_overflow */
262 NULL, /* special_function */
263 "R_MN10300_GNU_VTENTRY", /* name */
264 false, /* partial_inplace */
265 0, /* src_mask */
266 0, /* dst_mask */
267 false), /* pcrel_offset */
269 /* Standard 24 bit reloc. */
270 HOWTO (R_MN10300_24,
274 false,
276 complain_overflow_bitfield,
277 bfd_elf_generic_reloc,
278 "R_MN10300_24",
279 false,
280 0xffffff,
281 0xffffff,
282 false),
285 struct mn10300_reloc_map {
286 bfd_reloc_code_real_type bfd_reloc_val;
287 unsigned char elf_reloc_val;
290 static const struct mn10300_reloc_map mn10300_reloc_map[] = {
291 { BFD_RELOC_NONE, R_MN10300_NONE, },
292 { BFD_RELOC_32, R_MN10300_32, },
293 { BFD_RELOC_16, R_MN10300_16, },
294 { BFD_RELOC_8, R_MN10300_8, },
295 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
296 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
297 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
298 { BFD_RELOC_24, R_MN10300_24, },
299 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
300 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
303 static reloc_howto_type *
304 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
305 bfd *abfd ATTRIBUTE_UNUSED;
306 bfd_reloc_code_real_type code;
308 unsigned int i;
310 for (i = 0;
311 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
312 i++)
314 if (mn10300_reloc_map[i].bfd_reloc_val == code)
315 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
318 return NULL;
321 /* Set the howto pointer for an MN10300 ELF reloc. */
323 static void
324 mn10300_info_to_howto (abfd, cache_ptr, dst)
325 bfd *abfd ATTRIBUTE_UNUSED;
326 arelent *cache_ptr;
327 Elf32_Internal_Rela *dst;
329 unsigned int r_type;
331 r_type = ELF32_R_TYPE (dst->r_info);
332 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
333 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
336 /* Look through the relocs for a section during the first phase.
337 Since we don't do .gots or .plts, we just need to consider the
338 virtual table relocs for gc. */
340 static boolean
341 mn10300_elf_check_relocs (abfd, info, sec, relocs)
342 bfd *abfd;
343 struct bfd_link_info *info;
344 asection *sec;
345 const Elf_Internal_Rela *relocs;
347 Elf_Internal_Shdr *symtab_hdr;
348 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
349 const Elf_Internal_Rela *rel;
350 const Elf_Internal_Rela *rel_end;
352 if (info->relocateable)
353 return true;
355 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
356 sym_hashes = elf_sym_hashes (abfd);
357 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
358 if (!elf_bad_symtab (abfd))
359 sym_hashes_end -= symtab_hdr->sh_info;
361 rel_end = relocs + sec->reloc_count;
362 for (rel = relocs; rel < rel_end; rel++)
364 struct elf_link_hash_entry *h;
365 unsigned long r_symndx;
367 r_symndx = ELF32_R_SYM (rel->r_info);
368 if (r_symndx < symtab_hdr->sh_info)
369 h = NULL;
370 else
371 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
373 switch (ELF32_R_TYPE (rel->r_info))
375 /* This relocation describes the C++ object vtable hierarchy.
376 Reconstruct it for later use during GC. */
377 case R_MN10300_GNU_VTINHERIT:
378 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
379 return false;
380 break;
382 /* This relocation describes which C++ vtable entries are actually
383 used. Record for later use during GC. */
384 case R_MN10300_GNU_VTENTRY:
385 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
386 return false;
387 break;
391 return true;
394 /* Return the section that should be marked against GC for a given
395 relocation. */
397 static asection *
398 mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym)
399 bfd *abfd;
400 struct bfd_link_info *info ATTRIBUTE_UNUSED;
401 Elf_Internal_Rela *rel;
402 struct elf_link_hash_entry *h;
403 Elf_Internal_Sym *sym;
405 if (h != NULL)
407 switch (ELF32_R_TYPE (rel->r_info))
409 case R_MN10300_GNU_VTINHERIT:
410 case R_MN10300_GNU_VTENTRY:
411 break;
413 default:
414 switch (h->root.type)
416 case bfd_link_hash_defined:
417 case bfd_link_hash_defweak:
418 return h->root.u.def.section;
420 case bfd_link_hash_common:
421 return h->root.u.c.p->section;
423 default:
424 break;
428 else
430 return bfd_section_from_elf_index (abfd, sym->st_shndx);
433 return NULL;
436 /* Perform a relocation as part of a final link. */
437 static bfd_reloc_status_type
438 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
439 input_section, contents, offset, value,
440 addend, info, sym_sec, is_local)
441 reloc_howto_type *howto;
442 bfd *input_bfd;
443 bfd *output_bfd ATTRIBUTE_UNUSED;
444 asection *input_section;
445 bfd_byte *contents;
446 bfd_vma offset;
447 bfd_vma value;
448 bfd_vma addend;
449 struct bfd_link_info *info ATTRIBUTE_UNUSED;
450 asection *sym_sec ATTRIBUTE_UNUSED;
451 int is_local ATTRIBUTE_UNUSED;
453 unsigned long r_type = howto->type;
454 bfd_byte *hit_data = contents + offset;
456 switch (r_type)
458 case R_MN10300_NONE:
459 return bfd_reloc_ok;
461 case R_MN10300_32:
462 value += addend;
463 bfd_put_32 (input_bfd, value, hit_data);
464 return bfd_reloc_ok;
466 case R_MN10300_24:
467 value += addend;
469 if ((long) value > 0x7fffff || (long) value < -0x800000)
470 return bfd_reloc_overflow;
472 bfd_put_8 (input_bfd, value & 0xff, hit_data);
473 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
474 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
475 return bfd_reloc_ok;
477 case R_MN10300_16:
478 value += addend;
480 if ((long) value > 0x7fff || (long) value < -0x8000)
481 return bfd_reloc_overflow;
483 bfd_put_16 (input_bfd, value, hit_data);
484 return bfd_reloc_ok;
486 case R_MN10300_8:
487 value += addend;
489 if ((long) value > 0x7f || (long) value < -0x80)
490 return bfd_reloc_overflow;
492 bfd_put_8 (input_bfd, value, hit_data);
493 return bfd_reloc_ok;
495 case R_MN10300_PCREL8:
496 value -= (input_section->output_section->vma
497 + input_section->output_offset);
498 value -= offset;
499 value += addend;
501 if ((long) value > 0xff || (long) value < -0x100)
502 return bfd_reloc_overflow;
504 bfd_put_8 (input_bfd, value, hit_data);
505 return bfd_reloc_ok;
507 case R_MN10300_PCREL16:
508 value -= (input_section->output_section->vma
509 + input_section->output_offset);
510 value -= offset;
511 value += addend;
513 if ((long) value > 0xffff || (long) value < -0x10000)
514 return bfd_reloc_overflow;
516 bfd_put_16 (input_bfd, value, hit_data);
517 return bfd_reloc_ok;
519 case R_MN10300_PCREL32:
520 value -= (input_section->output_section->vma
521 + input_section->output_offset);
522 value -= offset;
523 value += addend;
525 bfd_put_32 (input_bfd, value, hit_data);
526 return bfd_reloc_ok;
528 case R_MN10300_GNU_VTINHERIT:
529 case R_MN10300_GNU_VTENTRY:
530 return bfd_reloc_ok;
532 default:
533 return bfd_reloc_notsupported;
537 /* Relocate an MN10300 ELF section. */
538 static boolean
539 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
540 contents, relocs, local_syms, local_sections)
541 bfd *output_bfd;
542 struct bfd_link_info *info;
543 bfd *input_bfd;
544 asection *input_section;
545 bfd_byte *contents;
546 Elf_Internal_Rela *relocs;
547 Elf_Internal_Sym *local_syms;
548 asection **local_sections;
550 Elf_Internal_Shdr *symtab_hdr;
551 struct elf32_mn10300_link_hash_entry **sym_hashes;
552 Elf_Internal_Rela *rel, *relend;
554 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
555 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
556 (elf_sym_hashes (input_bfd));
558 rel = relocs;
559 relend = relocs + input_section->reloc_count;
560 for (; rel < relend; rel++)
562 int r_type;
563 reloc_howto_type *howto;
564 unsigned long r_symndx;
565 Elf_Internal_Sym *sym;
566 asection *sec;
567 struct elf32_mn10300_link_hash_entry *h;
568 bfd_vma relocation;
569 bfd_reloc_status_type r;
571 r_symndx = ELF32_R_SYM (rel->r_info);
572 r_type = ELF32_R_TYPE (rel->r_info);
573 howto = elf_mn10300_howto_table + r_type;
575 /* Just skip the vtable gc relocs. */
576 if (r_type == R_MN10300_GNU_VTINHERIT
577 || r_type == R_MN10300_GNU_VTENTRY)
578 continue;
580 if (info->relocateable)
582 /* This is a relocateable link. We don't have to change
583 anything, unless the reloc is against a section symbol,
584 in which case we have to adjust according to where the
585 section symbol winds up in the output section. */
586 if (r_symndx < symtab_hdr->sh_info)
588 sym = local_syms + r_symndx;
589 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
591 sec = local_sections[r_symndx];
592 rel->r_addend += sec->output_offset + sym->st_value;
596 continue;
599 /* This is a final link. */
600 h = NULL;
601 sym = NULL;
602 sec = NULL;
603 if (r_symndx < symtab_hdr->sh_info)
605 sym = local_syms + r_symndx;
606 sec = local_sections[r_symndx];
607 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
609 else
611 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
612 while (h->root.root.type == bfd_link_hash_indirect
613 || h->root.root.type == bfd_link_hash_warning)
614 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
615 if (h->root.root.type == bfd_link_hash_defined
616 || h->root.root.type == bfd_link_hash_defweak)
618 sec = h->root.root.u.def.section;
619 relocation = (h->root.root.u.def.value
620 + sec->output_section->vma
621 + sec->output_offset);
623 else if (h->root.root.type == bfd_link_hash_undefweak)
624 relocation = 0;
625 else
627 if (! ((*info->callbacks->undefined_symbol)
628 (info, h->root.root.root.string, input_bfd,
629 input_section, rel->r_offset, true)))
630 return false;
631 relocation = 0;
635 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
636 input_section,
637 contents, rel->r_offset,
638 relocation, rel->r_addend,
639 info, sec, h == NULL);
641 if (r != bfd_reloc_ok)
643 const char *name;
644 const char *msg = (const char *) 0;
646 if (h != NULL)
647 name = h->root.root.root.string;
648 else
650 name = (bfd_elf_string_from_elf_section
651 (input_bfd, symtab_hdr->sh_link, sym->st_name));
652 if (name == NULL || *name == '\0')
653 name = bfd_section_name (input_bfd, sec);
656 switch (r)
658 case bfd_reloc_overflow:
659 if (! ((*info->callbacks->reloc_overflow)
660 (info, name, howto->name, (bfd_vma) 0,
661 input_bfd, input_section, rel->r_offset)))
662 return false;
663 break;
665 case bfd_reloc_undefined:
666 if (! ((*info->callbacks->undefined_symbol)
667 (info, name, input_bfd, input_section,
668 rel->r_offset, true)))
669 return false;
670 break;
672 case bfd_reloc_outofrange:
673 msg = _("internal error: out of range error");
674 goto common_error;
676 case bfd_reloc_notsupported:
677 msg = _("internal error: unsupported relocation error");
678 goto common_error;
680 case bfd_reloc_dangerous:
681 msg = _("internal error: dangerous error");
682 goto common_error;
684 default:
685 msg = _("internal error: unknown error");
686 /* fall through */
688 common_error:
689 if (!((*info->callbacks->warning)
690 (info, msg, name, input_bfd, input_section,
691 rel->r_offset)))
692 return false;
693 break;
698 return true;
701 /* Finish initializing one hash table entry. */
702 static boolean
703 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
704 struct bfd_hash_entry *gen_entry;
705 PTR in_args ATTRIBUTE_UNUSED;
707 struct elf32_mn10300_link_hash_entry *entry;
708 unsigned int byte_count = 0;
710 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
712 /* If we already know we want to convert "call" to "calls" for calls
713 to this symbol, then return now. */
714 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
715 return true;
717 /* If there are no named calls to this symbol, or there's nothing we
718 can move from the function itself into the "call" instruction, then
719 note that all "call" instructions should be converted into "calls"
720 instructions and return. */
721 if (entry->direct_calls == 0
722 || (entry->stack_size == 0 && entry->movm_args == 0))
724 /* Make a note that we should convert "call" instructions to "calls"
725 instructions for calls to this symbol. */
726 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
727 return true;
730 /* We may be able to move some instructions from the function itself into
731 the "call" instruction. Count how many bytes we might be able to
732 eliminate in the function itself. */
734 /* A movm instruction is two bytes. */
735 if (entry->movm_args)
736 byte_count += 2;
738 /* Count the insn to allocate stack space too. */
739 if (entry->stack_size > 0 && entry->stack_size <= 128)
740 byte_count += 3;
741 else if (entry->stack_size > 0 && entry->stack_size < 256)
742 byte_count += 4;
744 /* If using "call" will result in larger code, then turn all
745 the associated "call" instructions into "calls" instrutions. */
746 if (byte_count < entry->direct_calls)
747 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
749 /* This routine never fails. */
750 return true;
753 /* This function handles relaxing for the mn10300.
755 There's quite a few relaxing opportunites available on the mn10300:
757 * calls:32 -> calls:16 2 bytes
758 * call:32 -> call:16 2 bytes
760 * call:32 -> calls:32 1 byte
761 * call:16 -> calls:16 1 byte
762 * These are done anytime using "calls" would result
763 in smaller code, or when necessary to preserve the
764 meaning of the program.
766 * call:32 varies
767 * call:16
768 * In some circumstances we can move instructions
769 from a function prologue into a "call" instruction.
770 This is only done if the resulting code is no larger
771 than the original code.
773 * jmp:32 -> jmp:16 2 bytes
774 * jmp:16 -> bra:8 1 byte
776 * If the previous instruction is a conditional branch
777 around the jump/bra, we may be able to reverse its condition
778 and change its target to the jump's target. The jump/bra
779 can then be deleted. 2 bytes
781 * mov abs32 -> mov abs16 1 or 2 bytes
783 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
784 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
786 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
787 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
789 We don't handle imm16->imm8 or d16->d8 as they're very rare
790 and somewhat more difficult to support. */
792 static boolean
793 mn10300_elf_relax_section (abfd, sec, link_info, again)
794 bfd *abfd;
795 asection *sec;
796 struct bfd_link_info *link_info;
797 boolean *again;
799 Elf_Internal_Shdr *symtab_hdr;
800 Elf_Internal_Shdr *shndx_hdr;
801 Elf_Internal_Rela *internal_relocs = NULL;
802 Elf_Internal_Rela *free_relocs = NULL;
803 Elf_Internal_Rela *irel, *irelend;
804 bfd_byte *contents = NULL;
805 bfd_byte *free_contents = NULL;
806 Elf32_External_Sym *extsyms = NULL;
807 Elf32_External_Sym *free_extsyms = NULL;
808 Elf_External_Sym_Shndx *shndx_buf = NULL;
809 struct elf32_mn10300_link_hash_table *hash_table;
811 /* Assume nothing changes. */
812 *again = false;
814 /* We need a pointer to the mn10300 specific hash table. */
815 hash_table = elf32_mn10300_hash_table (link_info);
817 /* Initialize fields in each hash table entry the first time through. */
818 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
820 bfd *input_bfd;
822 /* Iterate over all the input bfds. */
823 for (input_bfd = link_info->input_bfds;
824 input_bfd != NULL;
825 input_bfd = input_bfd->link_next)
827 asection *section;
829 /* We're going to need all the symbols for each bfd. */
830 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
831 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
833 /* Get cached copy if it exists. */
834 if (symtab_hdr->contents != NULL)
835 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
836 else
838 /* Go get them off disk. */
839 bfd_size_type amt;
841 amt = symtab_hdr->sh_info;
842 amt *= sizeof (Elf32_External_Sym);
843 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
844 if (extsyms == NULL)
845 goto error_return;
846 free_extsyms = extsyms;
847 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
848 || bfd_bread ((PTR) extsyms, amt, input_bfd) != amt)
849 goto error_return;
852 if (shndx_hdr->sh_size != 0)
854 bfd_size_type amt;
856 amt = symtab_hdr->sh_info;
857 amt *= sizeof (Elf_External_Sym_Shndx);
858 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
859 if (shndx_buf == NULL)
860 goto error_return;
861 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
862 || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
863 goto error_return;
866 /* Iterate over each section in this bfd. */
867 for (section = input_bfd->sections;
868 section != NULL;
869 section = section->next)
871 struct elf32_mn10300_link_hash_entry *hash;
872 Elf_Internal_Sym *sym;
873 asection *sym_sec = NULL;
874 const char *sym_name;
875 char *new_name;
877 /* If there's nothing to do in this section, skip it. */
878 if (! (((section->flags & SEC_RELOC) != 0
879 && section->reloc_count != 0)
880 || (section->flags & SEC_CODE) != 0))
881 continue;
883 /* Get cached copy of section contents if it exists. */
884 if (elf_section_data (section)->this_hdr.contents != NULL)
885 contents = elf_section_data (section)->this_hdr.contents;
886 else if (section->_raw_size != 0)
888 /* Go get them off disk. */
889 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
890 if (contents == NULL)
891 goto error_return;
892 free_contents = contents;
894 if (!bfd_get_section_contents (input_bfd, section,
895 contents, (file_ptr) 0,
896 section->_raw_size))
897 goto error_return;
899 else
901 contents = NULL;
902 free_contents = NULL;
905 /* If there aren't any relocs, then there's nothing to do. */
906 if ((section->flags & SEC_RELOC) != 0
907 && section->reloc_count != 0)
910 /* Get a copy of the native relocations. */
911 internal_relocs = (_bfd_elf32_link_read_relocs
912 (input_bfd, section, (PTR) NULL,
913 (Elf_Internal_Rela *) NULL,
914 link_info->keep_memory));
915 if (internal_relocs == NULL)
916 goto error_return;
917 if (! link_info->keep_memory)
918 free_relocs = internal_relocs;
920 /* Now examine each relocation. */
921 irel = internal_relocs;
922 irelend = irel + section->reloc_count;
923 for (; irel < irelend; irel++)
925 long r_type;
926 unsigned long r_index;
927 unsigned char code;
929 r_type = ELF32_R_TYPE (irel->r_info);
930 r_index = ELF32_R_SYM (irel->r_info);
932 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
933 goto error_return;
935 /* We need the name and hash table entry of the target
936 symbol! */
937 hash = NULL;
938 sym = NULL;
939 sym_sec = NULL;
941 if (r_index < symtab_hdr->sh_info)
943 /* A local symbol. */
944 Elf32_External_Sym *esym;
945 Elf_External_Sym_Shndx *shndx;
946 Elf_Internal_Sym isym;
947 struct elf_link_hash_table *elftab;
948 bfd_size_type amt;
950 esym = extsyms + r_index;
951 shndx = shndx_buf + (shndx_buf ? r_index : 0);
952 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx,
953 &isym);
955 if (isym.st_shndx == SHN_UNDEF)
956 sym_sec = bfd_und_section_ptr;
957 else if (isym.st_shndx == SHN_ABS)
958 sym_sec = bfd_abs_section_ptr;
959 else if (isym.st_shndx == SHN_COMMON)
960 sym_sec = bfd_com_section_ptr;
961 else
962 sym_sec
963 = bfd_section_from_elf_index (input_bfd,
964 isym.st_shndx);
966 sym_name
967 = bfd_elf_string_from_elf_section (input_bfd,
968 (symtab_hdr
969 ->sh_link),
970 isym.st_name);
972 /* If it isn't a function, then we don't care
973 about it. */
974 if (r_index < symtab_hdr->sh_info
975 && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
976 continue;
978 /* Tack on an ID so we can uniquely identify this
979 local symbol in the global hash table. */
980 amt = strlen (sym_name) + 10;
981 new_name = bfd_malloc (amt);
982 if (new_name == 0)
983 goto error_return;
985 sprintf (new_name, "%s_%08x",
986 sym_name, (int) sym_sec);
987 sym_name = new_name;
989 elftab = &hash_table->static_hash_table->root;
990 hash = ((struct elf32_mn10300_link_hash_entry *)
991 elf_link_hash_lookup (elftab, sym_name,
992 true, true, false));
993 free (new_name);
995 else
997 r_index -= symtab_hdr->sh_info;
998 hash = (struct elf32_mn10300_link_hash_entry *)
999 elf_sym_hashes (input_bfd)[r_index];
1002 /* If this is not a "call" instruction, then we
1003 should convert "call" instructions to "calls"
1004 instructions. */
1005 code = bfd_get_8 (input_bfd,
1006 contents + irel->r_offset - 1);
1007 if (code != 0xdd && code != 0xcd)
1008 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1010 /* If this is a jump/call, then bump the direct_calls
1011 counter. Else force "call" to "calls" conversions. */
1012 if (r_type == R_MN10300_PCREL32
1013 || r_type == R_MN10300_PCREL16)
1014 hash->direct_calls++;
1015 else
1016 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1020 /* Now look at the actual contents to get the stack size,
1021 and a list of what registers were saved in the prologue
1022 (ie movm_args). */
1023 if ((section->flags & SEC_CODE) != 0)
1026 Elf32_External_Sym *esym, *esymend;
1027 Elf_External_Sym_Shndx *shndx;
1028 int idx;
1029 unsigned int sec_shndx;
1031 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1032 section);
1034 /* Look at each function defined in this section and
1035 update info for that function. */
1036 for (esym = extsyms, esymend = esym + symtab_hdr->sh_info,
1037 shndx = shndx_buf;
1038 esym < esymend;
1039 esym++, shndx = (shndx ? shndx + 1 : NULL))
1041 Elf_Internal_Sym isym;
1043 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx, &isym);
1044 if (isym.st_shndx == sec_shndx
1045 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1047 struct elf_link_hash_table *elftab;
1048 bfd_size_type amt;
1050 if (isym.st_shndx == SHN_UNDEF)
1051 sym_sec = bfd_und_section_ptr;
1052 else if (isym.st_shndx == SHN_ABS)
1053 sym_sec = bfd_abs_section_ptr;
1054 else if (isym.st_shndx == SHN_COMMON)
1055 sym_sec = bfd_com_section_ptr;
1056 else
1057 sym_sec
1058 = bfd_section_from_elf_index (input_bfd,
1059 isym.st_shndx);
1061 sym_name = (bfd_elf_string_from_elf_section
1062 (input_bfd, symtab_hdr->sh_link,
1063 isym.st_name));
1065 /* Tack on an ID so we can uniquely identify this
1066 local symbol in the global hash table. */
1067 amt = strlen (sym_name) + 10;
1068 new_name = bfd_malloc (amt);
1069 if (new_name == 0)
1070 goto error_return;
1072 sprintf (new_name, "%s_%08x",
1073 sym_name, (int) sym_sec);
1074 sym_name = new_name;
1076 elftab = &hash_table->static_hash_table->root;
1077 hash = ((struct elf32_mn10300_link_hash_entry *)
1078 elf_link_hash_lookup (elftab, sym_name,
1079 true, true, false));
1080 free (new_name);
1081 compute_function_info (input_bfd, hash,
1082 isym.st_value, contents);
1086 esym = extsyms + symtab_hdr->sh_info;
1087 esymend = extsyms + (symtab_hdr->sh_size
1088 / sizeof (Elf32_External_Sym));
1089 for (idx = 0; esym < esymend; esym++, idx++)
1091 Elf_Internal_Sym isym;
1093 hash = (struct elf32_mn10300_link_hash_entry *)
1094 elf_sym_hashes (input_bfd)[idx];
1095 if ((hash->root.root.type == bfd_link_hash_defined
1096 || hash->root.root.type == bfd_link_hash_defweak)
1097 && hash->root.root.u.def.section == section
1098 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1099 compute_function_info (input_bfd, hash,
1100 (hash)->root.root.u.def.value,
1101 contents);
1105 /* Cache or free any memory we allocated for the relocs. */
1106 if (free_relocs != NULL)
1108 free (free_relocs);
1109 free_relocs = NULL;
1112 /* Cache or free any memory we allocated for the contents. */
1113 if (free_contents != NULL)
1115 if (! link_info->keep_memory)
1116 free (free_contents);
1117 else
1119 /* Cache the section contents for elf_link_input_bfd. */
1120 elf_section_data (section)->this_hdr.contents = contents;
1122 free_contents = NULL;
1126 if (shndx_buf != NULL)
1128 free (shndx_buf);
1129 shndx_buf = NULL;
1132 /* Cache or free any memory we allocated for the symbols. */
1133 if (free_extsyms != NULL)
1135 if (! link_info->keep_memory)
1136 free (free_extsyms);
1137 else
1139 /* Cache the symbols for elf_link_input_bfd. */
1140 symtab_hdr->contents = (unsigned char *) extsyms;
1142 free_extsyms = NULL;
1146 /* Now iterate on each symbol in the hash table and perform
1147 the final initialization steps on each. */
1148 elf32_mn10300_link_hash_traverse (hash_table,
1149 elf32_mn10300_finish_hash_table_entry,
1150 NULL);
1151 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1152 elf32_mn10300_finish_hash_table_entry,
1153 NULL);
1155 /* All entries in the hash table are fully initialized. */
1156 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1158 /* Now that everything has been initialized, go through each
1159 code section and delete any prologue insns which will be
1160 redundant because their operations will be performed by
1161 a "call" instruction. */
1162 for (input_bfd = link_info->input_bfds;
1163 input_bfd != NULL;
1164 input_bfd = input_bfd->link_next)
1166 asection *section;
1168 /* We're going to need all the local symbols for each bfd. */
1169 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1170 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
1172 /* Get cached copy if it exists. */
1173 if (symtab_hdr->contents != NULL)
1174 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1175 else
1177 /* Go get them off disk. */
1178 bfd_size_type amt;
1180 amt = symtab_hdr->sh_info;
1181 amt *= sizeof (Elf32_External_Sym);
1182 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
1183 if (extsyms == NULL)
1184 goto error_return;
1185 free_extsyms = extsyms;
1186 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1187 || bfd_bread ((PTR) extsyms, amt, input_bfd) != amt)
1188 goto error_return;
1189 symtab_hdr->contents = (bfd_byte *) extsyms;
1192 if (shndx_hdr->sh_size != 0)
1194 bfd_size_type amt;
1196 amt = symtab_hdr->sh_info;
1197 amt *= sizeof (Elf_External_Sym_Shndx);
1198 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1199 if (shndx_buf == NULL)
1200 goto error_return;
1201 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1202 || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
1203 goto error_return;
1204 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1207 /* Walk over each section in this bfd. */
1208 for (section = input_bfd->sections;
1209 section != NULL;
1210 section = section->next)
1212 unsigned int sec_shndx;
1213 Elf32_External_Sym *esym, *esymend;
1214 Elf_External_Sym_Shndx *shndx;
1215 unsigned int idx;
1217 /* Skip non-code sections and empty sections. */
1218 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1219 continue;
1221 if (section->reloc_count != 0)
1223 /* Get a copy of the native relocations. */
1224 internal_relocs = (_bfd_elf32_link_read_relocs
1225 (input_bfd, section, (PTR) NULL,
1226 (Elf_Internal_Rela *) NULL,
1227 link_info->keep_memory));
1228 if (internal_relocs == NULL)
1229 goto error_return;
1230 if (! link_info->keep_memory)
1231 free_relocs = internal_relocs;
1234 /* Get cached copy of section contents if it exists. */
1235 if (elf_section_data (section)->this_hdr.contents != NULL)
1236 contents = elf_section_data (section)->this_hdr.contents;
1237 else
1239 /* Go get them off disk. */
1240 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
1241 if (contents == NULL)
1242 goto error_return;
1243 free_contents = contents;
1245 if (!bfd_get_section_contents (input_bfd, section,
1246 contents, (file_ptr) 0,
1247 section->_raw_size))
1248 goto error_return;
1251 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1252 section);
1254 /* Now look for any function in this section which needs
1255 insns deleted from its prologue. */
1256 for (esym = extsyms, esymend = esym + symtab_hdr->sh_info,
1257 shndx = shndx_buf;
1258 esym < esymend;
1259 esym++, shndx = (shndx ? shndx + 1 : NULL))
1261 Elf_Internal_Sym isym;
1262 struct elf32_mn10300_link_hash_entry *sym_hash;
1263 asection *sym_sec = NULL;
1264 const char *sym_name;
1265 char *new_name;
1266 struct elf_link_hash_table *elftab;
1267 bfd_size_type amt;
1269 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx, &isym);
1271 if (isym.st_shndx != sec_shndx)
1272 continue;
1274 if (isym.st_shndx == SHN_UNDEF)
1275 sym_sec = bfd_und_section_ptr;
1276 else if (isym.st_shndx == SHN_ABS)
1277 sym_sec = bfd_abs_section_ptr;
1278 else if (isym.st_shndx == SHN_COMMON)
1279 sym_sec = bfd_com_section_ptr;
1280 else
1281 sym_sec
1282 = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1284 sym_name
1285 = bfd_elf_string_from_elf_section (input_bfd,
1286 symtab_hdr->sh_link,
1287 isym.st_name);
1289 /* Tack on an ID so we can uniquely identify this
1290 local symbol in the global hash table. */
1291 amt = strlen (sym_name) + 10;
1292 new_name = bfd_malloc (amt);
1293 if (new_name == 0)
1294 goto error_return;
1295 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1296 sym_name = new_name;
1298 elftab = &hash_table->static_hash_table->root;
1299 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
1300 elf_link_hash_lookup (elftab, sym_name,
1301 false, false, false));
1303 free (new_name);
1304 if (sym_hash == NULL)
1305 continue;
1307 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1308 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1310 int bytes = 0;
1312 /* Note that we've changed things. */
1313 elf_section_data (section)->relocs = internal_relocs;
1314 free_relocs = NULL;
1316 elf_section_data (section)->this_hdr.contents = contents;
1317 free_contents = NULL;
1319 free_extsyms = NULL;
1321 /* Count how many bytes we're going to delete. */
1322 if (sym_hash->movm_args)
1323 bytes += 2;
1325 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1326 bytes += 3;
1327 else if (sym_hash->stack_size
1328 && sym_hash->stack_size < 256)
1329 bytes += 4;
1331 /* Note that we've deleted prologue bytes for this
1332 function. */
1333 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1335 /* Actually delete the bytes. */
1336 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1337 section,
1338 isym.st_value,
1339 bytes))
1340 goto error_return;
1342 /* Something changed. Not strictly necessary, but
1343 may lead to more relaxing opportunities. */
1344 *again = true;
1348 /* Look for any global functions in this section which
1349 need insns deleted from their prologues. */
1350 for (idx = 0;
1351 idx < (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1352 - symtab_hdr->sh_info);
1353 idx++)
1355 struct elf32_mn10300_link_hash_entry *sym_hash;
1357 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1358 (elf_sym_hashes (input_bfd)[idx]);
1359 if ((sym_hash->root.root.type == bfd_link_hash_defined
1360 || sym_hash->root.root.type == bfd_link_hash_defweak)
1361 && sym_hash->root.root.u.def.section == section
1362 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1363 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1365 int bytes = 0;
1366 bfd_vma symval;
1368 /* Note that we've changed things. */
1369 elf_section_data (section)->relocs = internal_relocs;
1370 free_relocs = NULL;
1372 elf_section_data (section)->this_hdr.contents = contents;
1373 free_contents = NULL;
1375 free_extsyms = NULL;
1377 /* Count how many bytes we're going to delete. */
1378 if (sym_hash->movm_args)
1379 bytes += 2;
1381 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1382 bytes += 3;
1383 else if (sym_hash->stack_size
1384 && sym_hash->stack_size < 256)
1385 bytes += 4;
1387 /* Note that we've deleted prologue bytes for this
1388 function. */
1389 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1391 /* Actually delete the bytes. */
1392 symval = sym_hash->root.root.u.def.value;
1393 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1394 section,
1395 symval,
1396 bytes))
1397 goto error_return;
1399 /* Something changed. Not strictly necessary, but
1400 may lead to more relaxing opportunities. */
1401 *again = true;
1405 /* Cache or free any memory we allocated for the relocs. */
1406 if (free_relocs != NULL)
1408 free (free_relocs);
1409 free_relocs = NULL;
1412 /* Cache or free any memory we allocated for the contents. */
1413 if (free_contents != NULL)
1415 if (! link_info->keep_memory)
1416 free (free_contents);
1417 else
1419 /* Cache the section contents for elf_link_input_bfd. */
1420 elf_section_data (section)->this_hdr.contents = contents;
1422 free_contents = NULL;
1426 if (shndx_buf != NULL)
1428 shndx_hdr->contents = NULL;
1429 free (shndx_buf);
1430 shndx_buf = NULL;
1433 /* Cache or free any memory we allocated for the symbols. */
1434 if (free_extsyms != NULL)
1436 if (! link_info->keep_memory)
1438 symtab_hdr->contents = NULL;
1439 free (free_extsyms);
1441 free_extsyms = NULL;
1446 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1447 contents = NULL;
1448 extsyms = NULL;
1449 internal_relocs = NULL;
1450 free_relocs = NULL;
1451 free_contents = NULL;
1452 free_extsyms = NULL;
1454 /* We don't have to do anything for a relocateable link, if
1455 this section does not have relocs, or if this is not a
1456 code section. */
1457 if (link_info->relocateable
1458 || (sec->flags & SEC_RELOC) == 0
1459 || sec->reloc_count == 0
1460 || (sec->flags & SEC_CODE) == 0)
1461 return true;
1463 /* If this is the first time we have been called for this section,
1464 initialize the cooked size. */
1465 if (sec->_cooked_size == 0)
1466 sec->_cooked_size = sec->_raw_size;
1468 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1469 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1471 /* Get a copy of the native relocations. */
1472 internal_relocs = (_bfd_elf32_link_read_relocs
1473 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1474 link_info->keep_memory));
1475 if (internal_relocs == NULL)
1476 goto error_return;
1477 if (! link_info->keep_memory)
1478 free_relocs = internal_relocs;
1480 /* Walk through them looking for relaxing opportunities. */
1481 irelend = internal_relocs + sec->reloc_count;
1482 for (irel = internal_relocs; irel < irelend; irel++)
1484 bfd_vma symval;
1485 struct elf32_mn10300_link_hash_entry *h = NULL;
1487 /* If this isn't something that can be relaxed, then ignore
1488 this reloc. */
1489 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1490 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1491 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1492 continue;
1494 /* Get the section contents if we haven't done so already. */
1495 if (contents == NULL)
1497 /* Get cached copy if it exists. */
1498 if (elf_section_data (sec)->this_hdr.contents != NULL)
1499 contents = elf_section_data (sec)->this_hdr.contents;
1500 else
1502 /* Go get them off disk. */
1503 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1504 if (contents == NULL)
1505 goto error_return;
1506 free_contents = contents;
1508 if (! bfd_get_section_contents (abfd, sec, contents,
1509 (file_ptr) 0, sec->_raw_size))
1510 goto error_return;
1514 /* Read this BFD's symbols if we haven't done so already. */
1515 if (extsyms == NULL)
1517 /* Get cached copy if it exists. */
1518 if (symtab_hdr->contents != NULL)
1519 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1520 else
1522 /* Go get them off disk. */
1523 bfd_size_type amt;
1525 amt = symtab_hdr->sh_info;
1526 amt *= sizeof (Elf32_External_Sym);
1527 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
1528 if (extsyms == NULL)
1529 goto error_return;
1530 free_extsyms = extsyms;
1531 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1532 || bfd_bread ((PTR) extsyms, amt, abfd) != amt)
1533 goto error_return;
1534 symtab_hdr->contents = (bfd_byte *) extsyms;
1537 if (shndx_hdr->sh_size != 0)
1539 bfd_size_type amt;
1541 amt = symtab_hdr->sh_info;
1542 amt *= sizeof (Elf_External_Sym_Shndx);
1543 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1544 if (shndx_buf == NULL)
1545 goto error_return;
1546 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1547 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1548 goto error_return;
1549 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1553 /* Get the value of the symbol referred to by the reloc. */
1554 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1556 Elf32_External_Sym *esym;
1557 Elf_External_Sym_Shndx *shndx;
1558 Elf_Internal_Sym isym;
1559 asection *sym_sec = NULL;
1560 const char *sym_name;
1561 char *new_name;
1563 /* A local symbol. */
1564 esym = extsyms + ELF32_R_SYM (irel->r_info);
1565 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (irel->r_info) : 0);
1566 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
1568 if (isym.st_shndx == SHN_UNDEF)
1569 sym_sec = bfd_und_section_ptr;
1570 else if (isym.st_shndx == SHN_ABS)
1571 sym_sec = bfd_abs_section_ptr;
1572 else if (isym.st_shndx == SHN_COMMON)
1573 sym_sec = bfd_com_section_ptr;
1574 else
1575 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1577 symval = (isym.st_value
1578 + sym_sec->output_section->vma
1579 + sym_sec->output_offset);
1580 sym_name = bfd_elf_string_from_elf_section (abfd,
1581 symtab_hdr->sh_link,
1582 isym.st_name);
1584 /* Tack on an ID so we can uniquely identify this
1585 local symbol in the global hash table. */
1586 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
1587 if (new_name == 0)
1588 goto error_return;
1589 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1590 sym_name = new_name;
1592 h = (struct elf32_mn10300_link_hash_entry *)
1593 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1594 sym_name, false, false, false);
1595 free (new_name);
1597 else
1599 unsigned long indx;
1601 /* An external symbol. */
1602 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1603 h = (struct elf32_mn10300_link_hash_entry *)
1604 (elf_sym_hashes (abfd)[indx]);
1605 BFD_ASSERT (h != NULL);
1606 if (h->root.root.type != bfd_link_hash_defined
1607 && h->root.root.type != bfd_link_hash_defweak)
1609 /* This appears to be a reference to an undefined
1610 symbol. Just ignore it--it will be caught by the
1611 regular reloc processing. */
1612 continue;
1615 symval = (h->root.root.u.def.value
1616 + h->root.root.u.def.section->output_section->vma
1617 + h->root.root.u.def.section->output_offset);
1620 /* For simplicity of coding, we are going to modify the section
1621 contents, the section relocs, and the BFD symbol table. We
1622 must tell the rest of the code not to free up this
1623 information. It would be possible to instead create a table
1624 of changes which have to be made, as is done in coff-mips.c;
1625 that would be more work, but would require less memory when
1626 the linker is run. */
1628 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1629 branch/call, also deal with "call" -> "calls" conversions and
1630 insertion of prologue data into "call" instructions. */
1631 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1633 bfd_vma value = symval;
1635 /* If we've got a "call" instruction that needs to be turned
1636 into a "calls" instruction, do so now. It saves a byte. */
1637 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1639 unsigned char code;
1641 /* Get the opcode. */
1642 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1644 /* Make sure we're working with a "call" instruction! */
1645 if (code == 0xdd)
1647 /* Note that we've changed the relocs, section contents,
1648 etc. */
1649 elf_section_data (sec)->relocs = internal_relocs;
1650 free_relocs = NULL;
1652 elf_section_data (sec)->this_hdr.contents = contents;
1653 free_contents = NULL;
1655 free_extsyms = NULL;
1657 /* Fix the opcode. */
1658 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1659 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1661 /* Fix irel->r_offset and irel->r_addend. */
1662 irel->r_offset += 1;
1663 irel->r_addend += 1;
1665 /* Delete one byte of data. */
1666 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1667 irel->r_offset + 3, 1))
1668 goto error_return;
1670 /* That will change things, so, we should relax again.
1671 Note that this is not required, and it may be slow. */
1672 *again = true;
1675 else if (h)
1677 /* We've got a "call" instruction which needs some data
1678 from target function filled in. */
1679 unsigned char code;
1681 /* Get the opcode. */
1682 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1684 /* Insert data from the target function into the "call"
1685 instruction if needed. */
1686 if (code == 0xdd)
1688 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1689 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1690 contents + irel->r_offset + 5);
1694 /* Deal with pc-relative gunk. */
1695 value -= (sec->output_section->vma + sec->output_offset);
1696 value -= irel->r_offset;
1697 value += irel->r_addend;
1699 /* See if the value will fit in 16 bits, note the high value is
1700 0x7fff + 2 as the target will be two bytes closer if we are
1701 able to relax. */
1702 if ((long) value < 0x8001 && (long) value > -0x8000)
1704 unsigned char code;
1706 /* Get the opcode. */
1707 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1709 if (code != 0xdc && code != 0xdd && code != 0xff)
1710 continue;
1712 /* Note that we've changed the relocs, section contents, etc. */
1713 elf_section_data (sec)->relocs = internal_relocs;
1714 free_relocs = NULL;
1716 elf_section_data (sec)->this_hdr.contents = contents;
1717 free_contents = NULL;
1719 free_extsyms = NULL;
1721 /* Fix the opcode. */
1722 if (code == 0xdc)
1723 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1724 else if (code == 0xdd)
1725 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1726 else if (code == 0xff)
1727 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1729 /* Fix the relocation's type. */
1730 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1731 R_MN10300_PCREL16);
1733 /* Delete two bytes of data. */
1734 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1735 irel->r_offset + 1, 2))
1736 goto error_return;
1738 /* That will change things, so, we should relax again.
1739 Note that this is not required, and it may be slow. */
1740 *again = true;
1744 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1745 branch. */
1746 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1748 bfd_vma value = symval;
1750 /* If we've got a "call" instruction that needs to be turned
1751 into a "calls" instruction, do so now. It saves a byte. */
1752 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1754 unsigned char code;
1756 /* Get the opcode. */
1757 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1759 /* Make sure we're working with a "call" instruction! */
1760 if (code == 0xcd)
1762 /* Note that we've changed the relocs, section contents,
1763 etc. */
1764 elf_section_data (sec)->relocs = internal_relocs;
1765 free_relocs = NULL;
1767 elf_section_data (sec)->this_hdr.contents = contents;
1768 free_contents = NULL;
1770 free_extsyms = NULL;
1772 /* Fix the opcode. */
1773 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1774 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1776 /* Fix irel->r_offset and irel->r_addend. */
1777 irel->r_offset += 1;
1778 irel->r_addend += 1;
1780 /* Delete one byte of data. */
1781 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1782 irel->r_offset + 1, 1))
1783 goto error_return;
1785 /* That will change things, so, we should relax again.
1786 Note that this is not required, and it may be slow. */
1787 *again = true;
1790 else if (h)
1792 unsigned char code;
1794 /* Get the opcode. */
1795 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1797 /* Insert data from the target function into the "call"
1798 instruction if needed. */
1799 if (code == 0xcd)
1801 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1802 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1803 contents + irel->r_offset + 3);
1807 /* Deal with pc-relative gunk. */
1808 value -= (sec->output_section->vma + sec->output_offset);
1809 value -= irel->r_offset;
1810 value += irel->r_addend;
1812 /* See if the value will fit in 8 bits, note the high value is
1813 0x7f + 1 as the target will be one bytes closer if we are
1814 able to relax. */
1815 if ((long) value < 0x80 && (long) value > -0x80)
1817 unsigned char code;
1819 /* Get the opcode. */
1820 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1822 if (code != 0xcc)
1823 continue;
1825 /* Note that we've changed the relocs, section contents, etc. */
1826 elf_section_data (sec)->relocs = internal_relocs;
1827 free_relocs = NULL;
1829 elf_section_data (sec)->this_hdr.contents = contents;
1830 free_contents = NULL;
1832 free_extsyms = NULL;
1834 /* Fix the opcode. */
1835 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1837 /* Fix the relocation's type. */
1838 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1839 R_MN10300_PCREL8);
1841 /* Delete one byte of data. */
1842 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1843 irel->r_offset + 1, 1))
1844 goto error_return;
1846 /* That will change things, so, we should relax again.
1847 Note that this is not required, and it may be slow. */
1848 *again = true;
1852 /* Try to eliminate an unconditional 8 bit pc-relative branch
1853 which immediately follows a conditional 8 bit pc-relative
1854 branch around the unconditional branch.
1856 original: new:
1857 bCC lab1 bCC' lab2
1858 bra lab2
1859 lab1: lab1:
1861 This happens when the bCC can't reach lab2 at assembly time,
1862 but due to other relaxations it can reach at link time. */
1863 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1865 Elf_Internal_Rela *nrel;
1866 bfd_vma value = symval;
1867 unsigned char code;
1869 /* Deal with pc-relative gunk. */
1870 value -= (sec->output_section->vma + sec->output_offset);
1871 value -= irel->r_offset;
1872 value += irel->r_addend;
1874 /* Do nothing if this reloc is the last byte in the section. */
1875 if (irel->r_offset == sec->_cooked_size)
1876 continue;
1878 /* See if the next instruction is an unconditional pc-relative
1879 branch, more often than not this test will fail, so we
1880 test it first to speed things up. */
1881 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1882 if (code != 0xca)
1883 continue;
1885 /* Also make sure the next relocation applies to the next
1886 instruction and that it's a pc-relative 8 bit branch. */
1887 nrel = irel + 1;
1888 if (nrel == irelend
1889 || irel->r_offset + 2 != nrel->r_offset
1890 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1891 continue;
1893 /* Make sure our destination immediately follows the
1894 unconditional branch. */
1895 if (symval != (sec->output_section->vma + sec->output_offset
1896 + irel->r_offset + 3))
1897 continue;
1899 /* Now make sure we are a conditional branch. This may not
1900 be necessary, but why take the chance.
1902 Note these checks assume that R_MN10300_PCREL8 relocs
1903 only occur on bCC and bCCx insns. If they occured
1904 elsewhere, we'd need to know the start of this insn
1905 for this check to be accurate. */
1906 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1907 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1908 && code != 0xc3 && code != 0xc4 && code != 0xc5
1909 && code != 0xc6 && code != 0xc7 && code != 0xc8
1910 && code != 0xc9 && code != 0xe8 && code != 0xe9
1911 && code != 0xea && code != 0xeb)
1912 continue;
1914 /* We also have to be sure there is no symbol/label
1915 at the unconditional branch. */
1916 if (mn10300_elf_symbol_address_p (abfd, sec, irel->r_offset + 1))
1917 continue;
1919 /* Note that we've changed the relocs, section contents, etc. */
1920 elf_section_data (sec)->relocs = internal_relocs;
1921 free_relocs = NULL;
1923 elf_section_data (sec)->this_hdr.contents = contents;
1924 free_contents = NULL;
1926 free_extsyms = NULL;
1928 /* Reverse the condition of the first branch. */
1929 switch (code)
1931 case 0xc8:
1932 code = 0xc9;
1933 break;
1934 case 0xc9:
1935 code = 0xc8;
1936 break;
1937 case 0xc0:
1938 code = 0xc2;
1939 break;
1940 case 0xc2:
1941 code = 0xc0;
1942 break;
1943 case 0xc3:
1944 code = 0xc1;
1945 break;
1946 case 0xc1:
1947 code = 0xc3;
1948 break;
1949 case 0xc4:
1950 code = 0xc6;
1951 break;
1952 case 0xc6:
1953 code = 0xc4;
1954 break;
1955 case 0xc7:
1956 code = 0xc5;
1957 break;
1958 case 0xc5:
1959 code = 0xc7;
1960 break;
1961 case 0xe8:
1962 code = 0xe9;
1963 break;
1964 case 0x9d:
1965 code = 0xe8;
1966 break;
1967 case 0xea:
1968 code = 0xeb;
1969 break;
1970 case 0xeb:
1971 code = 0xea;
1972 break;
1974 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1976 /* Set the reloc type and symbol for the first branch
1977 from the second branch. */
1978 irel->r_info = nrel->r_info;
1980 /* Make the reloc for the second branch a null reloc. */
1981 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1982 R_MN10300_NONE);
1984 /* Delete two bytes of data. */
1985 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1986 irel->r_offset + 1, 2))
1987 goto error_return;
1989 /* That will change things, so, we should relax again.
1990 Note that this is not required, and it may be slow. */
1991 *again = true;
1994 /* Try to turn a 24 immediate, displacement or absolute address
1995 into a 8 immediate, displacement or absolute address. */
1996 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
1998 bfd_vma value = symval;
1999 value += irel->r_addend;
2001 /* See if the value will fit in 8 bits. */
2002 if ((long) value < 0x7f && (long) value > -0x80)
2004 unsigned char code;
2006 /* AM33 insns which have 24 operands are 6 bytes long and
2007 will have 0xfd as the first byte. */
2009 /* Get the first opcode. */
2010 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2012 if (code == 0xfd)
2014 /* Get the second opcode. */
2015 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2017 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2018 equivalent instructions exists. */
2019 if (code != 0x6b && code != 0x7b
2020 && code != 0x8b && code != 0x9b
2021 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2022 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2023 || (code & 0x0f) == 0x0e))
2025 /* Not safe if the high bit is on as relaxing may
2026 move the value out of high mem and thus not fit
2027 in a signed 8bit value. This is currently over
2028 conservative. */
2029 if ((value & 0x80) == 0)
2031 /* Note that we've changed the relocation contents,
2032 etc. */
2033 elf_section_data (sec)->relocs = internal_relocs;
2034 free_relocs = NULL;
2036 elf_section_data (sec)->this_hdr.contents = contents;
2037 free_contents = NULL;
2039 free_extsyms = NULL;
2041 /* Fix the opcode. */
2042 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
2043 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2045 /* Fix the relocation's type. */
2046 irel->r_info =
2047 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2048 R_MN10300_8);
2050 /* Delete two bytes of data. */
2051 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2052 irel->r_offset + 1, 2))
2053 goto error_return;
2055 /* That will change things, so, we should relax
2056 again. Note that this is not required, and it
2057 may be slow. */
2058 *again = true;
2059 break;
2066 /* Try to turn a 32bit immediate, displacement or absolute address
2067 into a 16bit immediate, displacement or absolute address. */
2068 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
2070 bfd_vma value = symval;
2071 value += irel->r_addend;
2073 /* See if the value will fit in 24 bits.
2074 We allow any 16bit match here. We prune those we can't
2075 handle below. */
2076 if ((long) value < 0x7fffff && (long) value > -0x800000)
2078 unsigned char code;
2080 /* AM33 insns which have 32bit operands are 7 bytes long and
2081 will have 0xfe as the first byte. */
2083 /* Get the first opcode. */
2084 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2086 if (code == 0xfe)
2088 /* Get the second opcode. */
2089 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2091 /* All the am33 32 -> 24 relaxing possibilities. */
2092 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2093 equivalent instructions exists. */
2094 if (code != 0x6b && code != 0x7b
2095 && code != 0x8b && code != 0x9b
2096 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2097 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2098 || (code & 0x0f) == 0x0e))
2100 /* Not safe if the high bit is on as relaxing may
2101 move the value out of high mem and thus not fit
2102 in a signed 16bit value. This is currently over
2103 conservative. */
2104 if ((value & 0x8000) == 0)
2106 /* Note that we've changed the relocation contents,
2107 etc. */
2108 elf_section_data (sec)->relocs = internal_relocs;
2109 free_relocs = NULL;
2111 elf_section_data (sec)->this_hdr.contents = contents;
2112 free_contents = NULL;
2114 free_extsyms = NULL;
2116 /* Fix the opcode. */
2117 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2118 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2120 /* Fix the relocation's type. */
2121 irel->r_info =
2122 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2123 R_MN10300_24);
2125 /* Delete one byte of data. */
2126 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2127 irel->r_offset + 3, 1))
2128 goto error_return;
2130 /* That will change things, so, we should relax
2131 again. Note that this is not required, and it
2132 may be slow. */
2133 *again = true;
2134 break;
2140 /* See if the value will fit in 16 bits.
2141 We allow any 16bit match here. We prune those we can't
2142 handle below. */
2143 if ((long) value < 0x7fff && (long) value > -0x8000)
2145 unsigned char code;
2147 /* Most insns which have 32bit operands are 6 bytes long;
2148 exceptions are pcrel insns and bit insns.
2150 We handle pcrel insns above. We don't bother trying
2151 to handle the bit insns here.
2153 The first byte of the remaining insns will be 0xfc. */
2155 /* Get the first opcode. */
2156 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2158 if (code != 0xfc)
2159 continue;
2161 /* Get the second opcode. */
2162 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2164 if ((code & 0xf0) < 0x80)
2165 switch (code & 0xf0)
2167 /* mov (d32,am),dn -> mov (d32,am),dn
2168 mov dm,(d32,am) -> mov dn,(d32,am)
2169 mov (d32,am),an -> mov (d32,am),an
2170 mov dm,(d32,am) -> mov dn,(d32,am)
2171 movbu (d32,am),dn -> movbu (d32,am),dn
2172 movbu dm,(d32,am) -> movbu dn,(d32,am)
2173 movhu (d32,am),dn -> movhu (d32,am),dn
2174 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2175 case 0x00:
2176 case 0x10:
2177 case 0x20:
2178 case 0x30:
2179 case 0x40:
2180 case 0x50:
2181 case 0x60:
2182 case 0x70:
2183 /* Not safe if the high bit is on as relaxing may
2184 move the value out of high mem and thus not fit
2185 in a signed 16bit value. */
2186 if (code == 0xcc
2187 && (value & 0x8000))
2188 continue;
2190 /* Note that we've changed the relocation contents, etc. */
2191 elf_section_data (sec)->relocs = internal_relocs;
2192 free_relocs = NULL;
2194 elf_section_data (sec)->this_hdr.contents = contents;
2195 free_contents = NULL;
2197 free_extsyms = NULL;
2199 /* Fix the opcode. */
2200 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2201 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2203 /* Fix the relocation's type. */
2204 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2205 R_MN10300_16);
2207 /* Delete two bytes of data. */
2208 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2209 irel->r_offset + 2, 2))
2210 goto error_return;
2212 /* That will change things, so, we should relax again.
2213 Note that this is not required, and it may be slow. */
2214 *again = true;
2215 break;
2217 else if ((code & 0xf0) == 0x80
2218 || (code & 0xf0) == 0x90)
2219 switch (code & 0xf3)
2221 /* mov dn,(abs32) -> mov dn,(abs16)
2222 movbu dn,(abs32) -> movbu dn,(abs16)
2223 movhu dn,(abs32) -> movhu dn,(abs16) */
2224 case 0x81:
2225 case 0x82:
2226 case 0x83:
2227 /* Note that we've changed the relocation contents, etc. */
2228 elf_section_data (sec)->relocs = internal_relocs;
2229 free_relocs = NULL;
2231 elf_section_data (sec)->this_hdr.contents = contents;
2232 free_contents = NULL;
2234 free_extsyms = NULL;
2236 if ((code & 0xf3) == 0x81)
2237 code = 0x01 + (code & 0x0c);
2238 else if ((code & 0xf3) == 0x82)
2239 code = 0x02 + (code & 0x0c);
2240 else if ((code & 0xf3) == 0x83)
2241 code = 0x03 + (code & 0x0c);
2242 else
2243 abort ();
2245 /* Fix the opcode. */
2246 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2248 /* Fix the relocation's type. */
2249 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2250 R_MN10300_16);
2252 /* The opcode got shorter too, so we have to fix the
2253 addend and offset too! */
2254 irel->r_offset -= 1;
2256 /* Delete three bytes of data. */
2257 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2258 irel->r_offset + 1, 3))
2259 goto error_return;
2261 /* That will change things, so, we should relax again.
2262 Note that this is not required, and it may be slow. */
2263 *again = true;
2264 break;
2266 /* mov am,(abs32) -> mov am,(abs16)
2267 mov am,(d32,sp) -> mov am,(d16,sp)
2268 mov dm,(d32,sp) -> mov dm,(d32,sp)
2269 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2270 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2271 case 0x80:
2272 case 0x90:
2273 case 0x91:
2274 case 0x92:
2275 case 0x93:
2276 /* sp-based offsets are zero-extended. */
2277 if (code >= 0x90 && code <= 0x93
2278 && (long)value < 0)
2279 continue;
2281 /* Note that we've changed the relocation contents, etc. */
2282 elf_section_data (sec)->relocs = internal_relocs;
2283 free_relocs = NULL;
2285 elf_section_data (sec)->this_hdr.contents = contents;
2286 free_contents = NULL;
2288 free_extsyms = NULL;
2290 /* Fix the opcode. */
2291 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2292 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2294 /* Fix the relocation's type. */
2295 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2296 R_MN10300_16);
2298 /* Delete two bytes of data. */
2299 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2300 irel->r_offset + 2, 2))
2301 goto error_return;
2303 /* That will change things, so, we should relax again.
2304 Note that this is not required, and it may be slow. */
2305 *again = true;
2306 break;
2308 else if ((code & 0xf0) < 0xf0)
2309 switch (code & 0xfc)
2311 /* mov imm32,dn -> mov imm16,dn
2312 mov imm32,an -> mov imm16,an
2313 mov (abs32),dn -> mov (abs16),dn
2314 movbu (abs32),dn -> movbu (abs16),dn
2315 movhu (abs32),dn -> movhu (abs16),dn */
2316 case 0xcc:
2317 case 0xdc:
2318 case 0xa4:
2319 case 0xa8:
2320 case 0xac:
2321 /* Not safe if the high bit is on as relaxing may
2322 move the value out of high mem and thus not fit
2323 in a signed 16bit value. */
2324 if (code == 0xcc
2325 && (value & 0x8000))
2326 continue;
2328 /* mov imm16, an zero-extends the immediate. */
2329 if (code == 0xdc
2330 && (long)value < 0)
2331 continue;
2333 /* Note that we've changed the relocation contents, etc. */
2334 elf_section_data (sec)->relocs = internal_relocs;
2335 free_relocs = NULL;
2337 elf_section_data (sec)->this_hdr.contents = contents;
2338 free_contents = NULL;
2340 free_extsyms = NULL;
2342 if ((code & 0xfc) == 0xcc)
2343 code = 0x2c + (code & 0x03);
2344 else if ((code & 0xfc) == 0xdc)
2345 code = 0x24 + (code & 0x03);
2346 else if ((code & 0xfc) == 0xa4)
2347 code = 0x30 + (code & 0x03);
2348 else if ((code & 0xfc) == 0xa8)
2349 code = 0x34 + (code & 0x03);
2350 else if ((code & 0xfc) == 0xac)
2351 code = 0x38 + (code & 0x03);
2352 else
2353 abort ();
2355 /* Fix the opcode. */
2356 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2358 /* Fix the relocation's type. */
2359 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2360 R_MN10300_16);
2362 /* The opcode got shorter too, so we have to fix the
2363 addend and offset too! */
2364 irel->r_offset -= 1;
2366 /* Delete three bytes of data. */
2367 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2368 irel->r_offset + 1, 3))
2369 goto error_return;
2371 /* That will change things, so, we should relax again.
2372 Note that this is not required, and it may be slow. */
2373 *again = true;
2374 break;
2376 /* mov (abs32),an -> mov (abs16),an
2377 mov (d32,sp),an -> mov (d16,sp),an
2378 mov (d32,sp),dn -> mov (d16,sp),dn
2379 movbu (d32,sp),dn -> movbu (d16,sp),dn
2380 movhu (d32,sp),dn -> movhu (d16,sp),dn
2381 add imm32,dn -> add imm16,dn
2382 cmp imm32,dn -> cmp imm16,dn
2383 add imm32,an -> add imm16,an
2384 cmp imm32,an -> cmp imm16,an
2385 and imm32,dn -> and imm16,dn
2386 or imm32,dn -> or imm16,dn
2387 xor imm32,dn -> xor imm16,dn
2388 btst imm32,dn -> btst imm16,dn */
2390 case 0xa0:
2391 case 0xb0:
2392 case 0xb1:
2393 case 0xb2:
2394 case 0xb3:
2395 case 0xc0:
2396 case 0xc8:
2398 case 0xd0:
2399 case 0xd8:
2400 case 0xe0:
2401 case 0xe1:
2402 case 0xe2:
2403 case 0xe3:
2404 /* cmp imm16, an zero-extends the immediate. */
2405 if (code == 0xdc
2406 && (long)value < 0)
2407 continue;
2409 /* So do sp-based offsets. */
2410 if (code >= 0xb0 && code <= 0xb3
2411 && (long)value < 0)
2412 continue;
2414 /* Note that we've changed the relocation contents, etc. */
2415 elf_section_data (sec)->relocs = internal_relocs;
2416 free_relocs = NULL;
2418 elf_section_data (sec)->this_hdr.contents = contents;
2419 free_contents = NULL;
2421 free_extsyms = NULL;
2423 /* Fix the opcode. */
2424 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2425 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2427 /* Fix the relocation's type. */
2428 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2429 R_MN10300_16);
2431 /* Delete two bytes of data. */
2432 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2433 irel->r_offset + 2, 2))
2434 goto error_return;
2436 /* That will change things, so, we should relax again.
2437 Note that this is not required, and it may be slow. */
2438 *again = true;
2439 break;
2441 else if (code == 0xfe)
2443 /* add imm32,sp -> add imm16,sp */
2445 /* Note that we've changed the relocation contents, etc. */
2446 elf_section_data (sec)->relocs = internal_relocs;
2447 free_relocs = NULL;
2449 elf_section_data (sec)->this_hdr.contents = contents;
2450 free_contents = NULL;
2452 free_extsyms = NULL;
2454 /* Fix the opcode. */
2455 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2456 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2458 /* Fix the relocation's type. */
2459 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2460 R_MN10300_16);
2462 /* Delete two bytes of data. */
2463 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2464 irel->r_offset + 2, 2))
2465 goto error_return;
2467 /* That will change things, so, we should relax again.
2468 Note that this is not required, and it may be slow. */
2469 *again = true;
2470 break;
2476 if (free_relocs != NULL)
2477 free (free_relocs);
2479 if (free_contents != NULL)
2481 if (! link_info->keep_memory)
2482 free (free_contents);
2483 else
2485 /* Cache the section contents for elf_link_input_bfd. */
2486 elf_section_data (sec)->this_hdr.contents = contents;
2490 if (shndx_buf != NULL)
2492 shndx_hdr->contents = NULL;
2493 free (shndx_buf);
2496 if (free_extsyms != NULL)
2498 if (! link_info->keep_memory)
2500 symtab_hdr->contents = NULL;
2501 free (free_extsyms);
2505 return true;
2507 error_return:
2508 if (free_relocs != NULL)
2509 free (free_relocs);
2510 if (free_contents != NULL)
2511 free (free_contents);
2512 if (shndx_buf != NULL)
2514 shndx_hdr->contents = NULL;
2515 free (shndx_buf);
2517 if (free_extsyms != NULL)
2519 symtab_hdr->contents = NULL;
2520 free (free_extsyms);
2523 return false;
2526 /* Compute the stack size and movm arguments for the function
2527 referred to by HASH at address ADDR in section with
2528 contents CONTENTS, store the information in the hash table. */
2529 static void
2530 compute_function_info (abfd, hash, addr, contents)
2531 bfd *abfd;
2532 struct elf32_mn10300_link_hash_entry *hash;
2533 bfd_vma addr;
2534 unsigned char *contents;
2536 unsigned char byte1, byte2;
2537 /* We only care about a very small subset of the possible prologue
2538 sequences here. Basically we look for:
2540 movm [d2,d3,a2,a3],sp (optional)
2541 add <size>,sp (optional, and only for sizes which fit in an unsigned
2542 8 bit number)
2544 If we find anything else, we quit. */
2546 /* Look for movm [regs],sp */
2547 byte1 = bfd_get_8 (abfd, contents + addr);
2548 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2550 if (byte1 == 0xcf)
2552 hash->movm_args = byte2;
2553 addr += 2;
2554 byte1 = bfd_get_8 (abfd, contents + addr);
2555 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2558 /* Now figure out how much stack space will be allocated by the movm
2559 instruction. We need this kept separate from the funtion's normal
2560 stack space. */
2561 if (hash->movm_args)
2563 /* Space for d2. */
2564 if (hash->movm_args & 0x80)
2565 hash->movm_stack_size += 4;
2567 /* Space for d3. */
2568 if (hash->movm_args & 0x40)
2569 hash->movm_stack_size += 4;
2571 /* Space for a2. */
2572 if (hash->movm_args & 0x20)
2573 hash->movm_stack_size += 4;
2575 /* Space for a3. */
2576 if (hash->movm_args & 0x10)
2577 hash->movm_stack_size += 4;
2579 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2580 if (hash->movm_args & 0x08)
2581 hash->movm_stack_size += 8 * 4;
2583 if (bfd_get_mach (abfd) == bfd_mach_am33)
2585 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2586 if (hash->movm_args & 0x1)
2587 hash->movm_stack_size += 6 * 4;
2589 /* exreg1 space. e4, e5, e6, e7 */
2590 if (hash->movm_args & 0x2)
2591 hash->movm_stack_size += 4 * 4;
2593 /* exreg0 space. e2, e3 */
2594 if (hash->movm_args & 0x4)
2595 hash->movm_stack_size += 2 * 4;
2599 /* Now look for the two stack adjustment variants. */
2600 if (byte1 == 0xf8 && byte2 == 0xfe)
2602 int temp = bfd_get_8 (abfd, contents + addr + 2);
2603 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2605 hash->stack_size = -temp;
2607 else if (byte1 == 0xfa && byte2 == 0xfe)
2609 int temp = bfd_get_16 (abfd, contents + addr + 2);
2610 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2611 temp = -temp;
2613 if (temp < 255)
2614 hash->stack_size = temp;
2617 /* If the total stack to be allocated by the call instruction is more
2618 than 255 bytes, then we can't remove the stack adjustment by using
2619 "call" (we might still be able to remove the "movm" instruction. */
2620 if (hash->stack_size + hash->movm_stack_size > 255)
2621 hash->stack_size = 0;
2623 return;
2626 /* Delete some bytes from a section while relaxing. */
2628 static boolean
2629 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2630 bfd *abfd;
2631 asection *sec;
2632 bfd_vma addr;
2633 int count;
2635 Elf_Internal_Shdr *symtab_hdr;
2636 Elf_Internal_Shdr *shndx_hdr;
2637 Elf32_External_Sym *extsyms;
2638 unsigned int sec_shndx;
2639 bfd_byte *contents;
2640 Elf_Internal_Rela *irel, *irelend;
2641 Elf_Internal_Rela *irelalign;
2642 bfd_vma toaddr;
2643 Elf32_External_Sym *esym, *esymend;
2644 Elf_External_Sym_Shndx *shndx;
2645 struct elf_link_hash_entry **sym_hashes;
2646 struct elf_link_hash_entry **end_hashes;
2647 unsigned int symcount;
2649 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2650 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2652 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2654 contents = elf_section_data (sec)->this_hdr.contents;
2656 /* The deletion must stop at the next ALIGN reloc for an aligment
2657 power larger than the number of bytes we are deleting. */
2659 irelalign = NULL;
2660 toaddr = sec->_cooked_size;
2662 irel = elf_section_data (sec)->relocs;
2663 irelend = irel + sec->reloc_count;
2665 /* Actually delete the bytes. */
2666 memmove (contents + addr, contents + addr + count,
2667 (size_t) (toaddr - addr - count));
2668 sec->_cooked_size -= count;
2670 /* Adjust all the relocs. */
2671 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2673 /* Get the new reloc address. */
2674 if ((irel->r_offset > addr
2675 && irel->r_offset < toaddr))
2676 irel->r_offset -= count;
2679 /* Adjust the local symbols defined in this section. */
2680 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2681 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2682 esym = extsyms;
2683 esymend = esym + symtab_hdr->sh_info;
2684 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
2686 Elf_Internal_Sym isym;
2687 Elf_External_Sym_Shndx dummy;
2689 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
2691 if (isym.st_shndx == sec_shndx
2692 && isym.st_value > addr
2693 && isym.st_value < toaddr)
2695 isym.st_value -= count;
2696 bfd_elf32_swap_symbol_out (abfd, &isym, (PTR) esym, (PTR) &dummy);
2700 /* Now adjust the global symbols defined in this section. */
2701 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2702 - symtab_hdr->sh_info);
2703 sym_hashes = elf_sym_hashes (abfd);
2704 end_hashes = sym_hashes + symcount;
2705 for (; sym_hashes < end_hashes; sym_hashes++)
2707 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2708 if ((sym_hash->root.type == bfd_link_hash_defined
2709 || sym_hash->root.type == bfd_link_hash_defweak)
2710 && sym_hash->root.u.def.section == sec
2711 && sym_hash->root.u.def.value > addr
2712 && sym_hash->root.u.def.value < toaddr)
2714 sym_hash->root.u.def.value -= count;
2718 return true;
2721 /* Return true if a symbol exists at the given address, else return
2722 false. */
2723 static boolean
2724 mn10300_elf_symbol_address_p (abfd, sec, addr)
2725 bfd *abfd;
2726 asection *sec;
2727 bfd_vma addr;
2729 Elf_Internal_Shdr *symtab_hdr;
2730 Elf_Internal_Shdr *shndx_hdr;
2731 unsigned int sec_shndx;
2732 Elf32_External_Sym *esym, *esymend;
2733 Elf_External_Sym_Shndx *shndx;
2734 struct elf_link_hash_entry **sym_hashes;
2735 struct elf_link_hash_entry **end_hashes;
2736 unsigned int symcount;
2738 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2740 /* Examine all the symbols. */
2741 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2742 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2743 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2744 esym = (Elf32_External_Sym *) symtab_hdr->contents;
2745 esymend = esym + symtab_hdr->sh_info;
2746 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
2748 Elf_Internal_Sym isym;
2750 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
2752 if (isym.st_shndx == sec_shndx
2753 && isym.st_value == addr)
2754 return true;
2757 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2758 - symtab_hdr->sh_info);
2759 sym_hashes = elf_sym_hashes (abfd);
2760 end_hashes = sym_hashes + symcount;
2761 for (; sym_hashes < end_hashes; sym_hashes++)
2763 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2764 if ((sym_hash->root.type == bfd_link_hash_defined
2765 || sym_hash->root.type == bfd_link_hash_defweak)
2766 && sym_hash->root.u.def.section == sec
2767 && sym_hash->root.u.def.value == addr)
2768 return true;
2771 return false;
2774 /* This is a version of bfd_generic_get_relocated_section_contents
2775 which uses mn10300_elf_relocate_section. */
2777 static bfd_byte *
2778 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2779 data, relocateable, symbols)
2780 bfd *output_bfd;
2781 struct bfd_link_info *link_info;
2782 struct bfd_link_order *link_order;
2783 bfd_byte *data;
2784 boolean relocateable;
2785 asymbol **symbols;
2787 Elf_Internal_Shdr *symtab_hdr;
2788 Elf_Internal_Shdr *shndx_hdr;
2789 asection *input_section = link_order->u.indirect.section;
2790 bfd *input_bfd = input_section->owner;
2791 asection **sections = NULL;
2792 Elf_Internal_Rela *internal_relocs = NULL;
2793 Elf32_External_Sym *external_syms = NULL;
2794 Elf_External_Sym_Shndx *shndx_buf = NULL;
2795 Elf_External_Sym_Shndx *shndx;
2796 Elf_Internal_Sym *internal_syms = NULL;
2798 /* We only need to handle the case of relaxing, or of having a
2799 particular set of section contents, specially. */
2800 if (relocateable
2801 || elf_section_data (input_section)->this_hdr.contents == NULL)
2802 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2803 link_order, data,
2804 relocateable,
2805 symbols);
2807 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2808 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
2810 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2811 (size_t) input_section->_raw_size);
2813 if ((input_section->flags & SEC_RELOC) != 0
2814 && input_section->reloc_count > 0)
2816 Elf_Internal_Sym *isymp;
2817 asection **secpp;
2818 Elf32_External_Sym *esym, *esymend;
2819 bfd_size_type amt;
2821 if (symtab_hdr->contents != NULL)
2822 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2823 else if (symtab_hdr->sh_info != 0)
2825 amt = symtab_hdr->sh_info;
2826 amt *= sizeof (Elf32_External_Sym);
2827 external_syms = (Elf32_External_Sym *) bfd_malloc (amt);
2828 if (external_syms == NULL)
2829 goto error_return;
2830 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2831 || bfd_bread ((PTR) external_syms, amt, input_bfd) != amt)
2832 goto error_return;
2835 if (symtab_hdr->sh_info != 0 && shndx_hdr->sh_size != 0)
2837 amt = symtab_hdr->sh_info;
2838 amt *= sizeof (Elf_External_Sym_Shndx);
2839 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2840 if (shndx_buf == NULL)
2841 goto error_return;
2842 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2843 || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
2844 goto error_return;
2847 internal_relocs = (_bfd_elf32_link_read_relocs
2848 (input_bfd, input_section, (PTR) NULL,
2849 (Elf_Internal_Rela *) NULL, false));
2850 if (internal_relocs == NULL)
2851 goto error_return;
2853 amt = symtab_hdr->sh_info;
2854 amt *= sizeof (Elf_Internal_Sym);
2855 internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
2856 if (internal_syms == NULL && amt != 0)
2857 goto error_return;
2859 amt = symtab_hdr->sh_info;
2860 amt *= sizeof (asection *);
2861 sections = (asection **) bfd_malloc (amt);
2862 if (sections == NULL && amt != 0)
2863 goto error_return;
2865 for (isymp = internal_syms, secpp = sections, shndx = shndx_buf,
2866 esym = external_syms, esymend = esym + symtab_hdr->sh_info;
2867 esym < esymend;
2868 ++esym, ++isymp, ++secpp, shndx = (shndx ? shndx + 1 : NULL))
2870 asection *isec;
2872 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx, isymp);
2874 if (isymp->st_shndx == SHN_UNDEF)
2875 isec = bfd_und_section_ptr;
2876 else if (isymp->st_shndx == SHN_ABS)
2877 isec = bfd_abs_section_ptr;
2878 else if (isymp->st_shndx == SHN_COMMON)
2879 isec = bfd_com_section_ptr;
2880 else
2881 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2883 *secpp = isec;
2886 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2887 input_section, data, internal_relocs,
2888 internal_syms, sections))
2889 goto error_return;
2891 if (sections != NULL)
2892 free (sections);
2893 if (internal_syms != NULL)
2894 free (internal_syms);
2895 if (shndx_buf != NULL)
2896 free (shndx_buf);
2897 if (external_syms != NULL && symtab_hdr->contents == NULL)
2898 free (external_syms);
2899 if (internal_relocs != elf_section_data (input_section)->relocs)
2900 free (internal_relocs);
2903 return data;
2905 error_return:
2906 if (internal_relocs != NULL
2907 && internal_relocs != elf_section_data (input_section)->relocs)
2908 free (internal_relocs);
2909 if (shndx_buf != NULL)
2910 free (shndx_buf);
2911 if (external_syms != NULL && symtab_hdr->contents == NULL)
2912 free (external_syms);
2913 if (internal_syms != NULL)
2914 free (internal_syms);
2915 if (sections != NULL)
2916 free (sections);
2917 return NULL;
2920 /* Assorted hash table functions. */
2922 /* Initialize an entry in the link hash table. */
2924 /* Create an entry in an MN10300 ELF linker hash table. */
2926 static struct bfd_hash_entry *
2927 elf32_mn10300_link_hash_newfunc (entry, table, string)
2928 struct bfd_hash_entry *entry;
2929 struct bfd_hash_table *table;
2930 const char *string;
2932 struct elf32_mn10300_link_hash_entry *ret =
2933 (struct elf32_mn10300_link_hash_entry *) entry;
2935 /* Allocate the structure if it has not already been allocated by a
2936 subclass. */
2937 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2938 ret = ((struct elf32_mn10300_link_hash_entry *)
2939 bfd_hash_allocate (table,
2940 sizeof (struct elf32_mn10300_link_hash_entry)));
2941 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2942 return (struct bfd_hash_entry *) ret;
2944 /* Call the allocation method of the superclass. */
2945 ret = ((struct elf32_mn10300_link_hash_entry *)
2946 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2947 table, string));
2948 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2950 ret->direct_calls = 0;
2951 ret->stack_size = 0;
2952 ret->movm_stack_size = 0;
2953 ret->flags = 0;
2954 ret->movm_args = 0;
2957 return (struct bfd_hash_entry *) ret;
2960 /* Create an mn10300 ELF linker hash table. */
2962 static struct bfd_link_hash_table *
2963 elf32_mn10300_link_hash_table_create (abfd)
2964 bfd *abfd;
2966 struct elf32_mn10300_link_hash_table *ret;
2967 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
2969 ret = (struct elf32_mn10300_link_hash_table *) bfd_alloc (abfd, amt);
2970 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2971 return NULL;
2973 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2974 elf32_mn10300_link_hash_newfunc))
2976 bfd_release (abfd, ret);
2977 return NULL;
2980 ret->flags = 0;
2981 amt = sizeof (struct elf_link_hash_table);
2982 ret->static_hash_table
2983 = (struct elf32_mn10300_link_hash_table *) bfd_alloc (abfd, amt);
2984 if (ret->static_hash_table == NULL)
2986 bfd_release (abfd, ret);
2987 return NULL;
2990 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2991 elf32_mn10300_link_hash_newfunc))
2993 bfd_release (abfd, ret->static_hash_table);
2994 bfd_release (abfd, ret);
2995 return NULL;
2997 return &ret->root.root;
3000 static unsigned long
3001 elf_mn10300_mach (flags)
3002 flagword flags;
3004 switch (flags & EF_MN10300_MACH)
3006 case E_MN10300_MACH_MN10300:
3007 default:
3008 return bfd_mach_mn10300;
3010 case E_MN10300_MACH_AM33:
3011 return bfd_mach_am33;
3015 /* The final processing done just before writing out a MN10300 ELF object
3016 file. This gets the MN10300 architecture right based on the machine
3017 number. */
3019 void
3020 _bfd_mn10300_elf_final_write_processing (abfd, linker)
3021 bfd *abfd;
3022 boolean linker ATTRIBUTE_UNUSED;
3024 unsigned long val;
3026 switch (bfd_get_mach (abfd))
3028 default:
3029 case bfd_mach_mn10300:
3030 val = E_MN10300_MACH_MN10300;
3031 break;
3033 case bfd_mach_am33:
3034 val = E_MN10300_MACH_AM33;
3035 break;
3038 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
3039 elf_elfheader (abfd)->e_flags |= val;
3042 boolean
3043 _bfd_mn10300_elf_object_p (abfd)
3044 bfd *abfd;
3046 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
3047 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
3048 return true;
3051 /* Merge backend specific data from an object file to the output
3052 object file when linking. */
3054 boolean
3055 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
3056 bfd *ibfd;
3057 bfd *obfd;
3059 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3060 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3061 return true;
3063 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3064 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
3066 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3067 bfd_get_mach (ibfd)))
3068 return false;
3071 return true;
3074 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
3075 #define TARGET_LITTLE_NAME "elf32-mn10300"
3076 #define ELF_ARCH bfd_arch_mn10300
3077 #define ELF_MACHINE_CODE EM_MN10300
3078 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
3079 #define ELF_MAXPAGESIZE 0x1000
3081 #define elf_info_to_howto mn10300_info_to_howto
3082 #define elf_info_to_howto_rel 0
3083 #define elf_backend_can_gc_sections 1
3084 #define elf_backend_check_relocs mn10300_elf_check_relocs
3085 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
3086 #define elf_backend_relocate_section mn10300_elf_relocate_section
3087 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
3088 #define bfd_elf32_bfd_get_relocated_section_contents \
3089 mn10300_elf_get_relocated_section_contents
3090 #define bfd_elf32_bfd_link_hash_table_create \
3091 elf32_mn10300_link_hash_table_create
3093 #define elf_symbol_leading_char '_'
3095 /* So we can set bits in e_flags. */
3096 #define elf_backend_final_write_processing \
3097 _bfd_mn10300_elf_final_write_processing
3098 #define elf_backend_object_p _bfd_mn10300_elf_object_p
3100 #define bfd_elf32_bfd_merge_private_bfd_data \
3101 _bfd_mn10300_elf_merge_private_bfd_data
3103 #include "elf32-target.h"