1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "elf/mn10300.h"
27 #include "libiberty.h"
29 /* The mn10300 linker needs to keep track of the number of relocs that
30 it decides to copy in check_relocs for each symbol. This is so
31 that it can discard PC relative relocs if it doesn't need them when
32 linking with -Bsymbolic. We store the information in a field
33 extending the regular ELF linker hash table. */
35 struct elf32_mn10300_link_hash_entry
37 /* The basic elf link hash table entry. */
38 struct elf_link_hash_entry root;
40 /* For function symbols, the number of times this function is
41 called directly (ie by name). */
42 unsigned int direct_calls;
44 /* For function symbols, the size of this function's stack
45 (if <= 255 bytes). We stuff this into "call" instructions
46 to this target when it's valid and profitable to do so.
48 This does not include stack allocated by movm! */
49 unsigned char stack_size;
51 /* For function symbols, arguments (if any) for movm instruction
52 in the prologue. We stuff this value into "call" instructions
53 to the target when it's valid and profitable to do so. */
54 unsigned char movm_args;
56 /* For function symbols, the amount of stack space that would be allocated
57 by the movm instruction. This is redundant with movm_args, but we
58 add it to the hash table to avoid computing it over and over. */
59 unsigned char movm_stack_size;
61 /* When set, convert all "call" instructions to this target into "calls"
63 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
65 /* Used to mark functions which have had redundant parts of their
67 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
70 /* Calculated value. */
74 /* We derive a hash table from the main elf linker hash table so
75 we can store state variables and a secondary hash table without
76 resorting to global variables. */
77 struct elf32_mn10300_link_hash_table
79 /* The main hash table. */
80 struct elf_link_hash_table root;
82 /* A hash table for static functions. We could derive a new hash table
83 instead of using the full elf32_mn10300_link_hash_table if we wanted
84 to save some memory. */
85 struct elf32_mn10300_link_hash_table *static_hash_table;
87 /* Random linker state flags. */
88 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
93 #define streq(a, b) (strcmp ((a),(b)) == 0)
96 /* For MN10300 linker hash table. */
98 /* Get the MN10300 ELF linker hash table from a link_info structure. */
100 #define elf32_mn10300_hash_table(p) \
101 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
102 == MN10300_ELF_DATA ? ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) : NULL)
104 #define elf32_mn10300_link_hash_traverse(table, func, info) \
105 (elf_link_hash_traverse \
107 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
110 static reloc_howto_type elf_mn10300_howto_table[] =
112 /* Dummy relocation. Does nothing. */
113 HOWTO (R_MN10300_NONE,
119 complain_overflow_bitfield,
120 bfd_elf_generic_reloc,
126 /* Standard 32 bit reloc. */
133 complain_overflow_bitfield,
134 bfd_elf_generic_reloc,
140 /* Standard 16 bit reloc. */
147 complain_overflow_bitfield,
148 bfd_elf_generic_reloc,
154 /* Standard 8 bit reloc. */
161 complain_overflow_bitfield,
162 bfd_elf_generic_reloc,
168 /* Standard 32bit pc-relative reloc. */
169 HOWTO (R_MN10300_PCREL32,
175 complain_overflow_bitfield,
176 bfd_elf_generic_reloc,
182 /* Standard 16bit pc-relative reloc. */
183 HOWTO (R_MN10300_PCREL16,
189 complain_overflow_bitfield,
190 bfd_elf_generic_reloc,
196 /* Standard 8 pc-relative reloc. */
197 HOWTO (R_MN10300_PCREL8,
203 complain_overflow_bitfield,
204 bfd_elf_generic_reloc,
211 /* GNU extension to record C++ vtable hierarchy. */
212 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
214 0, /* size (0 = byte, 1 = short, 2 = long) */
216 FALSE, /* pc_relative */
218 complain_overflow_dont, /* complain_on_overflow */
219 NULL, /* special_function */
220 "R_MN10300_GNU_VTINHERIT", /* name */
221 FALSE, /* partial_inplace */
224 FALSE), /* pcrel_offset */
226 /* GNU extension to record C++ vtable member usage */
227 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
229 0, /* size (0 = byte, 1 = short, 2 = long) */
231 FALSE, /* pc_relative */
233 complain_overflow_dont, /* complain_on_overflow */
234 NULL, /* special_function */
235 "R_MN10300_GNU_VTENTRY", /* name */
236 FALSE, /* partial_inplace */
239 FALSE), /* pcrel_offset */
241 /* Standard 24 bit reloc. */
248 complain_overflow_bitfield,
249 bfd_elf_generic_reloc,
255 HOWTO (R_MN10300_GOTPC32, /* type */
257 2, /* size (0 = byte, 1 = short, 2 = long) */
259 TRUE, /* pc_relative */
261 complain_overflow_bitfield, /* complain_on_overflow */
262 bfd_elf_generic_reloc, /* */
263 "R_MN10300_GOTPC32", /* name */
264 FALSE, /* partial_inplace */
265 0xffffffff, /* src_mask */
266 0xffffffff, /* dst_mask */
267 TRUE), /* pcrel_offset */
269 HOWTO (R_MN10300_GOTPC16, /* type */
271 1, /* size (0 = byte, 1 = short, 2 = long) */
273 TRUE, /* pc_relative */
275 complain_overflow_bitfield, /* complain_on_overflow */
276 bfd_elf_generic_reloc, /* */
277 "R_MN10300_GOTPC16", /* name */
278 FALSE, /* partial_inplace */
279 0xffff, /* src_mask */
280 0xffff, /* dst_mask */
281 TRUE), /* pcrel_offset */
283 HOWTO (R_MN10300_GOTOFF32, /* type */
285 2, /* size (0 = byte, 1 = short, 2 = long) */
287 FALSE, /* pc_relative */
289 complain_overflow_bitfield, /* complain_on_overflow */
290 bfd_elf_generic_reloc, /* */
291 "R_MN10300_GOTOFF32", /* name */
292 FALSE, /* partial_inplace */
293 0xffffffff, /* src_mask */
294 0xffffffff, /* dst_mask */
295 FALSE), /* pcrel_offset */
297 HOWTO (R_MN10300_GOTOFF24, /* type */
299 2, /* size (0 = byte, 1 = short, 2 = long) */
301 FALSE, /* pc_relative */
303 complain_overflow_bitfield, /* complain_on_overflow */
304 bfd_elf_generic_reloc, /* */
305 "R_MN10300_GOTOFF24", /* name */
306 FALSE, /* partial_inplace */
307 0xffffff, /* src_mask */
308 0xffffff, /* dst_mask */
309 FALSE), /* pcrel_offset */
311 HOWTO (R_MN10300_GOTOFF16, /* type */
313 1, /* size (0 = byte, 1 = short, 2 = long) */
315 FALSE, /* pc_relative */
317 complain_overflow_bitfield, /* complain_on_overflow */
318 bfd_elf_generic_reloc, /* */
319 "R_MN10300_GOTOFF16", /* name */
320 FALSE, /* partial_inplace */
321 0xffff, /* src_mask */
322 0xffff, /* dst_mask */
323 FALSE), /* pcrel_offset */
325 HOWTO (R_MN10300_PLT32, /* type */
327 2, /* size (0 = byte, 1 = short, 2 = long) */
329 TRUE, /* pc_relative */
331 complain_overflow_bitfield, /* complain_on_overflow */
332 bfd_elf_generic_reloc, /* */
333 "R_MN10300_PLT32", /* name */
334 FALSE, /* partial_inplace */
335 0xffffffff, /* src_mask */
336 0xffffffff, /* dst_mask */
337 TRUE), /* pcrel_offset */
339 HOWTO (R_MN10300_PLT16, /* type */
341 1, /* size (0 = byte, 1 = short, 2 = long) */
343 TRUE, /* pc_relative */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 bfd_elf_generic_reloc, /* */
347 "R_MN10300_PLT16", /* name */
348 FALSE, /* partial_inplace */
349 0xffff, /* src_mask */
350 0xffff, /* dst_mask */
351 TRUE), /* pcrel_offset */
353 HOWTO (R_MN10300_GOT32, /* type */
355 2, /* size (0 = byte, 1 = short, 2 = long) */
357 FALSE, /* pc_relative */
359 complain_overflow_bitfield, /* complain_on_overflow */
360 bfd_elf_generic_reloc, /* */
361 "R_MN10300_GOT32", /* name */
362 FALSE, /* partial_inplace */
363 0xffffffff, /* src_mask */
364 0xffffffff, /* dst_mask */
365 FALSE), /* pcrel_offset */
367 HOWTO (R_MN10300_GOT24, /* type */
369 2, /* size (0 = byte, 1 = short, 2 = long) */
371 FALSE, /* pc_relative */
373 complain_overflow_bitfield, /* complain_on_overflow */
374 bfd_elf_generic_reloc, /* */
375 "R_MN10300_GOT24", /* name */
376 FALSE, /* partial_inplace */
377 0xffffffff, /* src_mask */
378 0xffffffff, /* dst_mask */
379 FALSE), /* pcrel_offset */
381 HOWTO (R_MN10300_GOT16, /* type */
383 1, /* size (0 = byte, 1 = short, 2 = long) */
385 FALSE, /* pc_relative */
387 complain_overflow_bitfield, /* complain_on_overflow */
388 bfd_elf_generic_reloc, /* */
389 "R_MN10300_GOT16", /* name */
390 FALSE, /* partial_inplace */
391 0xffffffff, /* src_mask */
392 0xffffffff, /* dst_mask */
393 FALSE), /* pcrel_offset */
395 HOWTO (R_MN10300_COPY, /* type */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
399 FALSE, /* pc_relative */
401 complain_overflow_bitfield, /* complain_on_overflow */
402 bfd_elf_generic_reloc, /* */
403 "R_MN10300_COPY", /* name */
404 FALSE, /* partial_inplace */
405 0xffffffff, /* src_mask */
406 0xffffffff, /* dst_mask */
407 FALSE), /* pcrel_offset */
409 HOWTO (R_MN10300_GLOB_DAT, /* type */
411 2, /* size (0 = byte, 1 = short, 2 = long) */
413 FALSE, /* pc_relative */
415 complain_overflow_bitfield, /* complain_on_overflow */
416 bfd_elf_generic_reloc, /* */
417 "R_MN10300_GLOB_DAT", /* name */
418 FALSE, /* partial_inplace */
419 0xffffffff, /* src_mask */
420 0xffffffff, /* dst_mask */
421 FALSE), /* pcrel_offset */
423 HOWTO (R_MN10300_JMP_SLOT, /* type */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
427 FALSE, /* pc_relative */
429 complain_overflow_bitfield, /* complain_on_overflow */
430 bfd_elf_generic_reloc, /* */
431 "R_MN10300_JMP_SLOT", /* name */
432 FALSE, /* partial_inplace */
433 0xffffffff, /* src_mask */
434 0xffffffff, /* dst_mask */
435 FALSE), /* pcrel_offset */
437 HOWTO (R_MN10300_RELATIVE, /* type */
439 2, /* size (0 = byte, 1 = short, 2 = long) */
441 FALSE, /* pc_relative */
443 complain_overflow_bitfield, /* complain_on_overflow */
444 bfd_elf_generic_reloc, /* */
445 "R_MN10300_RELATIVE", /* name */
446 FALSE, /* partial_inplace */
447 0xffffffff, /* src_mask */
448 0xffffffff, /* dst_mask */
449 FALSE), /* pcrel_offset */
461 HOWTO (R_MN10300_SYM_DIFF, /* type */
463 2, /* size (0 = byte, 1 = short, 2 = long) */
465 FALSE, /* pc_relative */
467 complain_overflow_dont,/* complain_on_overflow */
468 NULL, /* special handler. */
469 "R_MN10300_SYM_DIFF", /* name */
470 FALSE, /* partial_inplace */
471 0xffffffff, /* src_mask */
472 0xffffffff, /* dst_mask */
473 FALSE), /* pcrel_offset */
475 HOWTO (R_MN10300_ALIGN, /* type */
477 0, /* size (0 = byte, 1 = short, 2 = long) */
479 FALSE, /* pc_relative */
481 complain_overflow_dont,/* complain_on_overflow */
482 NULL, /* special handler. */
483 "R_MN10300_ALIGN", /* name */
484 FALSE, /* partial_inplace */
487 FALSE) /* pcrel_offset */
490 struct mn10300_reloc_map
492 bfd_reloc_code_real_type bfd_reloc_val;
493 unsigned char elf_reloc_val;
496 static const struct mn10300_reloc_map mn10300_reloc_map[] =
498 { BFD_RELOC_NONE, R_MN10300_NONE, },
499 { BFD_RELOC_32, R_MN10300_32, },
500 { BFD_RELOC_16, R_MN10300_16, },
501 { BFD_RELOC_8, R_MN10300_8, },
502 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
503 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
504 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
505 { BFD_RELOC_24, R_MN10300_24, },
506 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
507 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
508 { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
509 { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
510 { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
511 { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
512 { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
513 { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
514 { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
515 { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
516 { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
517 { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
518 { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
519 { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
520 { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
521 { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
522 { BFD_RELOC_MN10300_SYM_DIFF, R_MN10300_SYM_DIFF },
523 { BFD_RELOC_MN10300_ALIGN, R_MN10300_ALIGN }
526 /* Create the GOT section. */
529 _bfd_mn10300_elf_create_got_section (bfd * abfd,
530 struct bfd_link_info * info)
535 struct elf_link_hash_entry * h;
536 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
539 /* This function may be called more than once. */
540 if (bfd_get_section_by_name (abfd, ".got") != NULL)
543 switch (bed->s->arch_size)
554 bfd_set_error (bfd_error_bad_value);
558 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
559 | SEC_LINKER_CREATED);
562 pltflags |= SEC_CODE;
563 if (bed->plt_not_loaded)
564 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
565 if (bed->plt_readonly)
566 pltflags |= SEC_READONLY;
568 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
570 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
573 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
575 if (bed->want_plt_sym)
577 h = _bfd_elf_define_linkage_sym (abfd, info, s,
578 "_PROCEDURE_LINKAGE_TABLE_");
579 elf_hash_table (info)->hplt = h;
584 s = bfd_make_section_with_flags (abfd, ".got", flags);
586 || ! bfd_set_section_alignment (abfd, s, ptralign))
589 if (bed->want_got_plt)
591 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
593 || ! bfd_set_section_alignment (abfd, s, ptralign))
597 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
598 (or .got.plt) section. We don't do this in the linker script
599 because we don't want to define the symbol if we are not creating
600 a global offset table. */
601 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
602 elf_hash_table (info)->hgot = h;
606 /* The first bit of the global offset table is the header. */
607 s->size += bed->got_header_size;
612 static reloc_howto_type *
613 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
614 bfd_reloc_code_real_type code)
618 for (i = ARRAY_SIZE (mn10300_reloc_map); i--;)
619 if (mn10300_reloc_map[i].bfd_reloc_val == code)
620 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
625 static reloc_howto_type *
626 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
631 for (i = ARRAY_SIZE (elf_mn10300_howto_table); i--;)
632 if (elf_mn10300_howto_table[i].name != NULL
633 && strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0)
634 return elf_mn10300_howto_table + i;
639 /* Set the howto pointer for an MN10300 ELF reloc. */
642 mn10300_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
644 Elf_Internal_Rela *dst)
648 r_type = ELF32_R_TYPE (dst->r_info);
649 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
650 cache_ptr->howto = elf_mn10300_howto_table + r_type;
653 /* Look through the relocs for a section during the first phase.
654 Since we don't do .gots or .plts, we just need to consider the
655 virtual table relocs for gc. */
658 mn10300_elf_check_relocs (bfd *abfd,
659 struct bfd_link_info *info,
661 const Elf_Internal_Rela *relocs)
663 bfd_boolean sym_diff_reloc_seen;
664 Elf_Internal_Shdr *symtab_hdr;
665 Elf_Internal_Sym * isymbuf = NULL;
666 struct elf_link_hash_entry **sym_hashes;
667 const Elf_Internal_Rela *rel;
668 const Elf_Internal_Rela *rel_end;
670 bfd_vma * local_got_offsets;
674 bfd_boolean result = FALSE;
680 if (info->relocatable)
683 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
684 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
685 sym_hashes = elf_sym_hashes (abfd);
687 dynobj = elf_hash_table (info)->dynobj;
688 local_got_offsets = elf_local_got_offsets (abfd);
689 rel_end = relocs + sec->reloc_count;
690 sym_diff_reloc_seen = FALSE;
692 for (rel = relocs; rel < rel_end; rel++)
694 struct elf_link_hash_entry *h;
695 unsigned long r_symndx;
698 r_symndx = ELF32_R_SYM (rel->r_info);
699 if (r_symndx < symtab_hdr->sh_info)
703 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
704 while (h->root.type == bfd_link_hash_indirect
705 || h->root.type == bfd_link_hash_warning)
706 h = (struct elf_link_hash_entry *) h->root.u.i.link;
709 r_type = ELF32_R_TYPE (rel->r_info);
711 /* Some relocs require a global offset table. */
716 case R_MN10300_GOT32:
717 case R_MN10300_GOT24:
718 case R_MN10300_GOT16:
719 case R_MN10300_GOTOFF32:
720 case R_MN10300_GOTOFF24:
721 case R_MN10300_GOTOFF16:
722 case R_MN10300_GOTPC32:
723 case R_MN10300_GOTPC16:
724 elf_hash_table (info)->dynobj = dynobj = abfd;
725 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
736 /* This relocation describes the C++ object vtable hierarchy.
737 Reconstruct it for later use during GC. */
738 case R_MN10300_GNU_VTINHERIT:
739 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
743 /* This relocation describes which C++ vtable entries are actually
744 used. Record for later use during GC. */
745 case R_MN10300_GNU_VTENTRY:
746 BFD_ASSERT (h != NULL);
748 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
752 case R_MN10300_GOT32:
753 case R_MN10300_GOT24:
754 case R_MN10300_GOT16:
755 /* This symbol requires a global offset table entry. */
759 sgot = bfd_get_section_by_name (dynobj, ".got");
760 BFD_ASSERT (sgot != NULL);
764 && (h != NULL || info->shared))
766 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
769 srelgot = bfd_make_section_with_flags (dynobj,
778 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
785 if (h->got.offset != (bfd_vma) -1)
786 /* We have already allocated space in the .got. */
789 h->got.offset = sgot->size;
791 /* Make sure this symbol is output as a dynamic symbol. */
792 if (h->dynindx == -1)
794 if (! bfd_elf_link_record_dynamic_symbol (info, h))
798 srelgot->size += sizeof (Elf32_External_Rela);
802 /* This is a global offset table entry for a local
804 if (local_got_offsets == NULL)
809 size = symtab_hdr->sh_info * sizeof (bfd_vma);
810 local_got_offsets = bfd_alloc (abfd, size);
812 if (local_got_offsets == NULL)
815 elf_local_got_offsets (abfd) = local_got_offsets;
817 for (i = 0; i < symtab_hdr->sh_info; i++)
818 local_got_offsets[i] = (bfd_vma) -1;
821 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
822 /* We have already allocated space in the .got. */
825 local_got_offsets[r_symndx] = sgot->size;
828 /* If we are generating a shared object, we need to
829 output a R_MN10300_RELATIVE reloc so that the dynamic
830 linker can adjust this GOT entry. */
831 srelgot->size += sizeof (Elf32_External_Rela);
837 case R_MN10300_PLT32:
838 case R_MN10300_PLT16:
839 /* This symbol requires a procedure linkage table entry. We
840 actually build the entry in adjust_dynamic_symbol,
841 because this might be a case of linking PIC code which is
842 never referenced by a dynamic object, in which case we
843 don't need to generate a procedure linkage table entry
846 /* If this is a local symbol, we resolve it directly without
847 creating a procedure linkage table entry. */
851 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
852 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
861 case R_MN10300_PCREL32:
862 case R_MN10300_PCREL16:
863 case R_MN10300_PCREL8:
868 case R_MN10300_SYM_DIFF:
869 sym_diff_reloc_seen = TRUE;
876 /* If we are creating a shared library, then we
877 need to copy the reloc into the shared library. */
879 && (sec->flags & SEC_ALLOC) != 0
880 /* Do not generate a dynamic reloc for a
881 reloc associated with a SYM_DIFF operation. */
882 && ! sym_diff_reloc_seen)
884 asection * sym_section = NULL;
886 /* Find the section containing the
887 symbol involved in the relocation. */
890 Elf_Internal_Sym * isym;
893 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
894 symtab_hdr->sh_info, 0,
898 isym = isymbuf + r_symndx;
899 /* All we care about is whether this local symbol is absolute. */
900 if (isym->st_shndx == SHN_ABS)
901 sym_section = bfd_abs_section_ptr;
906 if (h->root.type == bfd_link_hash_defined
907 || h->root.type == bfd_link_hash_defweak)
908 sym_section = h->root.u.def.section;
911 /* If the symbol is absolute then the relocation can
912 be resolved during linking and there is no need for
914 if (sym_section != bfd_abs_section_ptr)
916 /* When creating a shared object, we must copy these
917 reloc types into the output file. We create a reloc
918 section in dynobj and make room for this reloc. */
921 sreloc = _bfd_elf_make_dynamic_reloc_section
922 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
927 sreloc->size += sizeof (Elf32_External_Rela);
934 if (ELF32_R_TYPE (rel->r_info) != R_MN10300_SYM_DIFF)
935 sym_diff_reloc_seen = FALSE;
946 /* Return the section that should be marked against GC for a given
950 mn10300_elf_gc_mark_hook (asection *sec,
951 struct bfd_link_info *info,
952 Elf_Internal_Rela *rel,
953 struct elf_link_hash_entry *h,
954 Elf_Internal_Sym *sym)
957 switch (ELF32_R_TYPE (rel->r_info))
959 case R_MN10300_GNU_VTINHERIT:
960 case R_MN10300_GNU_VTENTRY:
964 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
967 /* Perform a relocation as part of a final link. */
969 static bfd_reloc_status_type
970 mn10300_elf_final_link_relocate (reloc_howto_type *howto,
972 bfd *output_bfd ATTRIBUTE_UNUSED,
973 asection *input_section,
978 struct elf_link_hash_entry * h,
979 unsigned long symndx,
980 struct bfd_link_info *info,
981 asection *sym_sec ATTRIBUTE_UNUSED,
982 int is_local ATTRIBUTE_UNUSED)
984 static asection * sym_diff_section;
985 static bfd_vma sym_diff_value;
986 bfd_boolean is_sym_diff_reloc;
987 unsigned long r_type = howto->type;
988 bfd_byte * hit_data = contents + offset;
994 dynobj = elf_hash_table (info)->dynobj;
1004 case R_MN10300_PCREL8:
1005 case R_MN10300_PCREL16:
1006 case R_MN10300_PCREL32:
1007 case R_MN10300_GOTOFF32:
1008 case R_MN10300_GOTOFF24:
1009 case R_MN10300_GOTOFF16:
1011 && (input_section->flags & SEC_ALLOC) != 0
1013 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1014 return bfd_reloc_dangerous;
1017 is_sym_diff_reloc = FALSE;
1018 if (sym_diff_section != NULL)
1020 BFD_ASSERT (sym_diff_section == input_section);
1028 value -= sym_diff_value;
1029 /* If we are computing a 32-bit value for the location lists
1030 and the result is 0 then we add one to the value. A zero
1031 value can result because of linker relaxation deleteing
1032 prologue instructions and using a value of 1 (for the begin
1033 and end offsets in the location list entry) results in a
1034 nul entry which does not prevent the following entries from
1036 if (r_type == R_MN10300_32
1038 && strcmp (input_section->name, ".debug_loc") == 0)
1040 sym_diff_section = NULL;
1041 is_sym_diff_reloc = TRUE;
1045 sym_diff_section = NULL;
1052 case R_MN10300_SYM_DIFF:
1053 BFD_ASSERT (addend == 0);
1054 /* Cache the input section and value.
1055 The offset is unreliable, since relaxation may
1056 have reduced the following reloc's offset. */
1057 sym_diff_section = input_section;
1058 sym_diff_value = value;
1059 return bfd_reloc_ok;
1061 case R_MN10300_ALIGN:
1062 case R_MN10300_NONE:
1063 return bfd_reloc_ok;
1067 /* Do not generate relocs when an R_MN10300_32 has been used
1068 with an R_MN10300_SYM_DIFF to compute a difference of two
1070 && is_sym_diff_reloc == FALSE
1071 /* Also, do not generate a reloc when the symbol associated
1072 with the R_MN10300_32 reloc is absolute - there is no
1073 need for a run time computation in this case. */
1074 && sym_sec != bfd_abs_section_ptr
1075 /* If the section is not going to be allocated at load time
1076 then there is no need to generate relocs for it. */
1077 && (input_section->flags & SEC_ALLOC) != 0)
1079 Elf_Internal_Rela outrel;
1080 bfd_boolean skip, relocate;
1082 /* When generating a shared object, these relocations are
1083 copied into the output file to be resolved at run
1087 sreloc = _bfd_elf_get_dynamic_reloc_section
1088 (input_bfd, input_section, /*rela?*/ TRUE);
1095 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1096 input_section, offset);
1097 if (outrel.r_offset == (bfd_vma) -1)
1100 outrel.r_offset += (input_section->output_section->vma
1101 + input_section->output_offset);
1105 memset (&outrel, 0, sizeof outrel);
1110 /* h->dynindx may be -1 if this symbol was marked to
1113 || SYMBOL_REFERENCES_LOCAL (info, h))
1116 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1117 outrel.r_addend = value + addend;
1121 BFD_ASSERT (h->dynindx != -1);
1123 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1124 outrel.r_addend = value + addend;
1128 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1129 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1130 + sreloc->reloc_count));
1131 ++sreloc->reloc_count;
1133 /* If this reloc is against an external symbol, we do
1134 not want to fiddle with the addend. Otherwise, we
1135 need to include the symbol value so that it becomes
1136 an addend for the dynamic reloc. */
1138 return bfd_reloc_ok;
1141 bfd_put_32 (input_bfd, value, hit_data);
1142 return bfd_reloc_ok;
1147 if ((long) value > 0x7fffff || (long) value < -0x800000)
1148 return bfd_reloc_overflow;
1150 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1151 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1152 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1153 return bfd_reloc_ok;
1158 if ((long) value > 0x7fff || (long) value < -0x8000)
1159 return bfd_reloc_overflow;
1161 bfd_put_16 (input_bfd, value, hit_data);
1162 return bfd_reloc_ok;
1167 if ((long) value > 0x7f || (long) value < -0x80)
1168 return bfd_reloc_overflow;
1170 bfd_put_8 (input_bfd, value, hit_data);
1171 return bfd_reloc_ok;
1173 case R_MN10300_PCREL8:
1174 value -= (input_section->output_section->vma
1175 + input_section->output_offset);
1179 if ((long) value > 0x7f || (long) value < -0x80)
1180 return bfd_reloc_overflow;
1182 bfd_put_8 (input_bfd, value, hit_data);
1183 return bfd_reloc_ok;
1185 case R_MN10300_PCREL16:
1186 value -= (input_section->output_section->vma
1187 + input_section->output_offset);
1191 if ((long) value > 0x7fff || (long) value < -0x8000)
1192 return bfd_reloc_overflow;
1194 bfd_put_16 (input_bfd, value, hit_data);
1195 return bfd_reloc_ok;
1197 case R_MN10300_PCREL32:
1198 value -= (input_section->output_section->vma
1199 + input_section->output_offset);
1203 bfd_put_32 (input_bfd, value, hit_data);
1204 return bfd_reloc_ok;
1206 case R_MN10300_GNU_VTINHERIT:
1207 case R_MN10300_GNU_VTENTRY:
1208 return bfd_reloc_ok;
1210 case R_MN10300_GOTPC32:
1211 /* Use global offset table as symbol value. */
1212 value = bfd_get_section_by_name (dynobj,
1213 ".got")->output_section->vma;
1214 value -= (input_section->output_section->vma
1215 + input_section->output_offset);
1219 bfd_put_32 (input_bfd, value, hit_data);
1220 return bfd_reloc_ok;
1222 case R_MN10300_GOTPC16:
1223 /* Use global offset table as symbol value. */
1224 value = bfd_get_section_by_name (dynobj,
1225 ".got")->output_section->vma;
1226 value -= (input_section->output_section->vma
1227 + input_section->output_offset);
1231 if ((long) value > 0x7fff || (long) value < -0x8000)
1232 return bfd_reloc_overflow;
1234 bfd_put_16 (input_bfd, value, hit_data);
1235 return bfd_reloc_ok;
1237 case R_MN10300_GOTOFF32:
1238 value -= bfd_get_section_by_name (dynobj,
1239 ".got")->output_section->vma;
1242 bfd_put_32 (input_bfd, value, hit_data);
1243 return bfd_reloc_ok;
1245 case R_MN10300_GOTOFF24:
1246 value -= bfd_get_section_by_name (dynobj,
1247 ".got")->output_section->vma;
1250 if ((long) value > 0x7fffff || (long) value < -0x800000)
1251 return bfd_reloc_overflow;
1253 bfd_put_8 (input_bfd, value, hit_data);
1254 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1255 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1256 return bfd_reloc_ok;
1258 case R_MN10300_GOTOFF16:
1259 value -= bfd_get_section_by_name (dynobj,
1260 ".got")->output_section->vma;
1263 if ((long) value > 0x7fff || (long) value < -0x8000)
1264 return bfd_reloc_overflow;
1266 bfd_put_16 (input_bfd, value, hit_data);
1267 return bfd_reloc_ok;
1269 case R_MN10300_PLT32:
1271 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1272 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1273 && h->plt.offset != (bfd_vma) -1)
1275 splt = bfd_get_section_by_name (dynobj, ".plt");
1277 value = (splt->output_section->vma
1278 + splt->output_offset
1279 + h->plt.offset) - value;
1282 value -= (input_section->output_section->vma
1283 + input_section->output_offset);
1287 bfd_put_32 (input_bfd, value, hit_data);
1288 return bfd_reloc_ok;
1290 case R_MN10300_PLT16:
1292 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1293 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1294 && h->plt.offset != (bfd_vma) -1)
1296 splt = bfd_get_section_by_name (dynobj, ".plt");
1298 value = (splt->output_section->vma
1299 + splt->output_offset
1300 + h->plt.offset) - value;
1303 value -= (input_section->output_section->vma
1304 + input_section->output_offset);
1308 if ((long) value > 0x7fff || (long) value < -0x8000)
1309 return bfd_reloc_overflow;
1311 bfd_put_16 (input_bfd, value, hit_data);
1312 return bfd_reloc_ok;
1314 case R_MN10300_GOT32:
1315 case R_MN10300_GOT24:
1316 case R_MN10300_GOT16:
1318 sgot = bfd_get_section_by_name (dynobj, ".got");
1324 off = h->got.offset;
1325 BFD_ASSERT (off != (bfd_vma) -1);
1327 if (! elf_hash_table (info)->dynamic_sections_created
1328 || SYMBOL_REFERENCES_LOCAL (info, h))
1329 /* This is actually a static link, or it is a
1330 -Bsymbolic link and the symbol is defined
1331 locally, or the symbol was forced to be local
1332 because of a version file. We must initialize
1333 this entry in the global offset table.
1335 When doing a dynamic link, we create a .rela.got
1336 relocation entry to initialize the value. This
1337 is done in the finish_dynamic_symbol routine. */
1338 bfd_put_32 (output_bfd, value,
1339 sgot->contents + off);
1341 value = sgot->output_offset + off;
1347 off = elf_local_got_offsets (input_bfd)[symndx];
1349 bfd_put_32 (output_bfd, value, sgot->contents + off);
1354 Elf_Internal_Rela outrel;
1356 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1357 BFD_ASSERT (srelgot != NULL);
1359 outrel.r_offset = (sgot->output_section->vma
1360 + sgot->output_offset
1362 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1363 outrel.r_addend = value;
1364 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1365 (bfd_byte *) (((Elf32_External_Rela *)
1367 + srelgot->reloc_count));
1368 ++ srelgot->reloc_count;
1371 value = sgot->output_offset + off;
1377 if (r_type == R_MN10300_GOT32)
1379 bfd_put_32 (input_bfd, value, hit_data);
1380 return bfd_reloc_ok;
1382 else if (r_type == R_MN10300_GOT24)
1384 if ((long) value > 0x7fffff || (long) value < -0x800000)
1385 return bfd_reloc_overflow;
1387 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1388 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1389 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1390 return bfd_reloc_ok;
1392 else if (r_type == R_MN10300_GOT16)
1394 if ((long) value > 0x7fff || (long) value < -0x8000)
1395 return bfd_reloc_overflow;
1397 bfd_put_16 (input_bfd, value, hit_data);
1398 return bfd_reloc_ok;
1403 return bfd_reloc_notsupported;
1407 /* Relocate an MN10300 ELF section. */
1410 mn10300_elf_relocate_section (bfd *output_bfd,
1411 struct bfd_link_info *info,
1413 asection *input_section,
1415 Elf_Internal_Rela *relocs,
1416 Elf_Internal_Sym *local_syms,
1417 asection **local_sections)
1419 Elf_Internal_Shdr *symtab_hdr;
1420 struct elf_link_hash_entry **sym_hashes;
1421 Elf_Internal_Rela *rel, *relend;
1423 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1424 sym_hashes = elf_sym_hashes (input_bfd);
1427 relend = relocs + input_section->reloc_count;
1428 for (; rel < relend; rel++)
1431 reloc_howto_type *howto;
1432 unsigned long r_symndx;
1433 Elf_Internal_Sym *sym;
1435 struct elf32_mn10300_link_hash_entry *h;
1437 bfd_reloc_status_type r;
1439 r_symndx = ELF32_R_SYM (rel->r_info);
1440 r_type = ELF32_R_TYPE (rel->r_info);
1441 howto = elf_mn10300_howto_table + r_type;
1443 /* Just skip the vtable gc relocs. */
1444 if (r_type == R_MN10300_GNU_VTINHERIT
1445 || r_type == R_MN10300_GNU_VTENTRY)
1451 if (r_symndx < symtab_hdr->sh_info)
1453 sym = local_syms + r_symndx;
1454 sec = local_sections[r_symndx];
1455 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1459 bfd_boolean unresolved_reloc;
1461 struct elf_link_hash_entry *hh;
1463 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1464 r_symndx, symtab_hdr, sym_hashes,
1465 hh, sec, relocation,
1466 unresolved_reloc, warned);
1468 h = (struct elf32_mn10300_link_hash_entry *) hh;
1470 if ((h->root.root.type == bfd_link_hash_defined
1471 || h->root.root.type == bfd_link_hash_defweak)
1472 && ( r_type == R_MN10300_GOTPC32
1473 || r_type == R_MN10300_GOTPC16
1474 || (( r_type == R_MN10300_PLT32
1475 || r_type == R_MN10300_PLT16)
1476 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1477 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1478 && h->root.plt.offset != (bfd_vma) -1)
1479 || (( r_type == R_MN10300_GOT32
1480 || r_type == R_MN10300_GOT24
1481 || r_type == R_MN10300_GOT16)
1482 && elf_hash_table (info)->dynamic_sections_created
1483 && !SYMBOL_REFERENCES_LOCAL (info, hh))
1484 || (r_type == R_MN10300_32
1485 /* _32 relocs in executables force _COPY relocs,
1486 such that the address of the symbol ends up
1488 && !info->executable
1489 && !SYMBOL_REFERENCES_LOCAL (info, hh)
1490 && ((input_section->flags & SEC_ALLOC) != 0
1491 /* DWARF will emit R_MN10300_32 relocations
1492 in its sections against symbols defined
1493 externally in shared libraries. We can't
1494 do anything with them here. */
1495 || ((input_section->flags & SEC_DEBUGGING) != 0
1496 && h->root.def_dynamic)))))
1497 /* In these cases, we don't need the relocation
1498 value. We check specially because in some
1499 obscure cases sec->output_section will be NULL. */
1502 else if (!info->relocatable && unresolved_reloc)
1503 (*_bfd_error_handler)
1504 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1507 (long) rel->r_offset,
1509 h->root.root.root.string);
1512 if (sec != NULL && elf_discarded_section (sec))
1513 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1514 rel, relend, howto, contents);
1516 if (info->relocatable)
1519 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1521 contents, rel->r_offset,
1522 relocation, rel->r_addend,
1523 (struct elf_link_hash_entry *) h,
1525 info, sec, h == NULL);
1527 if (r != bfd_reloc_ok)
1530 const char *msg = NULL;
1533 name = h->root.root.root.string;
1536 name = (bfd_elf_string_from_elf_section
1537 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1538 if (name == NULL || *name == '\0')
1539 name = bfd_section_name (input_bfd, sec);
1544 case bfd_reloc_overflow:
1545 if (! ((*info->callbacks->reloc_overflow)
1546 (info, (h ? &h->root.root : NULL), name,
1547 howto->name, (bfd_vma) 0, input_bfd,
1548 input_section, rel->r_offset)))
1552 case bfd_reloc_undefined:
1553 if (! ((*info->callbacks->undefined_symbol)
1554 (info, name, input_bfd, input_section,
1555 rel->r_offset, TRUE)))
1559 case bfd_reloc_outofrange:
1560 msg = _("internal error: out of range error");
1563 case bfd_reloc_notsupported:
1564 msg = _("internal error: unsupported relocation error");
1567 case bfd_reloc_dangerous:
1568 if (r_type == R_MN10300_PCREL32)
1569 msg = _("error: inappropriate relocation type for shared"
1570 " library (did you forget -fpic?)");
1572 msg = _("internal error: suspicious relocation type used"
1573 " in shared library");
1577 msg = _("internal error: unknown error");
1581 if (!((*info->callbacks->warning)
1582 (info, msg, name, input_bfd, input_section,
1593 /* Finish initializing one hash table entry. */
1596 elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry,
1599 struct elf32_mn10300_link_hash_entry *entry;
1600 struct bfd_link_info *link_info = (struct bfd_link_info *) in_args;
1601 unsigned int byte_count = 0;
1603 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1605 /* If we already know we want to convert "call" to "calls" for calls
1606 to this symbol, then return now. */
1607 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1610 /* If there are no named calls to this symbol, or there's nothing we
1611 can move from the function itself into the "call" instruction,
1612 then note that all "call" instructions should be converted into
1613 "calls" instructions and return. If a symbol is available for
1614 dynamic symbol resolution (overridable or overriding), avoid
1615 custom calling conventions. */
1616 if (entry->direct_calls == 0
1617 || (entry->stack_size == 0 && entry->movm_args == 0)
1618 || (elf_hash_table (link_info)->dynamic_sections_created
1619 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1620 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1622 /* Make a note that we should convert "call" instructions to "calls"
1623 instructions for calls to this symbol. */
1624 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1628 /* We may be able to move some instructions from the function itself into
1629 the "call" instruction. Count how many bytes we might be able to
1630 eliminate in the function itself. */
1632 /* A movm instruction is two bytes. */
1633 if (entry->movm_args)
1636 /* Count the insn to allocate stack space too. */
1637 if (entry->stack_size > 0)
1639 if (entry->stack_size <= 128)
1645 /* If using "call" will result in larger code, then turn all
1646 the associated "call" instructions into "calls" instructions. */
1647 if (byte_count < entry->direct_calls)
1648 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1650 /* This routine never fails. */
1654 /* Used to count hash table entries. */
1657 elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
1660 int *count = (int *) in_args;
1666 /* Used to enumerate hash table entries into a linear array. */
1669 elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
1672 struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
1679 /* Used to sort the array created by the above. */
1682 sort_by_value (const void *va, const void *vb)
1684 struct elf32_mn10300_link_hash_entry *a
1685 = *(struct elf32_mn10300_link_hash_entry **) va;
1686 struct elf32_mn10300_link_hash_entry *b
1687 = *(struct elf32_mn10300_link_hash_entry **) vb;
1689 return a->value - b->value;
1692 /* Compute the stack size and movm arguments for the function
1693 referred to by HASH at address ADDR in section with
1694 contents CONTENTS, store the information in the hash table. */
1697 compute_function_info (bfd *abfd,
1698 struct elf32_mn10300_link_hash_entry *hash,
1700 unsigned char *contents)
1702 unsigned char byte1, byte2;
1703 /* We only care about a very small subset of the possible prologue
1704 sequences here. Basically we look for:
1706 movm [d2,d3,a2,a3],sp (optional)
1707 add <size>,sp (optional, and only for sizes which fit in an unsigned
1710 If we find anything else, we quit. */
1712 /* Look for movm [regs],sp. */
1713 byte1 = bfd_get_8 (abfd, contents + addr);
1714 byte2 = bfd_get_8 (abfd, contents + addr + 1);
1718 hash->movm_args = byte2;
1720 byte1 = bfd_get_8 (abfd, contents + addr);
1721 byte2 = bfd_get_8 (abfd, contents + addr + 1);
1724 /* Now figure out how much stack space will be allocated by the movm
1725 instruction. We need this kept separate from the function's normal
1727 if (hash->movm_args)
1730 if (hash->movm_args & 0x80)
1731 hash->movm_stack_size += 4;
1734 if (hash->movm_args & 0x40)
1735 hash->movm_stack_size += 4;
1738 if (hash->movm_args & 0x20)
1739 hash->movm_stack_size += 4;
1742 if (hash->movm_args & 0x10)
1743 hash->movm_stack_size += 4;
1745 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
1746 if (hash->movm_args & 0x08)
1747 hash->movm_stack_size += 8 * 4;
1749 if (bfd_get_mach (abfd) == bfd_mach_am33
1750 || bfd_get_mach (abfd) == bfd_mach_am33_2)
1752 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
1753 if (hash->movm_args & 0x1)
1754 hash->movm_stack_size += 6 * 4;
1756 /* exreg1 space. e4, e5, e6, e7 */
1757 if (hash->movm_args & 0x2)
1758 hash->movm_stack_size += 4 * 4;
1760 /* exreg0 space. e2, e3 */
1761 if (hash->movm_args & 0x4)
1762 hash->movm_stack_size += 2 * 4;
1766 /* Now look for the two stack adjustment variants. */
1767 if (byte1 == 0xf8 && byte2 == 0xfe)
1769 int temp = bfd_get_8 (abfd, contents + addr + 2);
1770 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
1772 hash->stack_size = -temp;
1774 else if (byte1 == 0xfa && byte2 == 0xfe)
1776 int temp = bfd_get_16 (abfd, contents + addr + 2);
1777 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
1781 hash->stack_size = temp;
1784 /* If the total stack to be allocated by the call instruction is more
1785 than 255 bytes, then we can't remove the stack adjustment by using
1786 "call" (we might still be able to remove the "movm" instruction. */
1787 if (hash->stack_size + hash->movm_stack_size > 255)
1788 hash->stack_size = 0;
1791 /* Delete some bytes from a section while relaxing. */
1794 mn10300_elf_relax_delete_bytes (bfd *abfd,
1799 Elf_Internal_Shdr *symtab_hdr;
1800 unsigned int sec_shndx;
1802 Elf_Internal_Rela *irel, *irelend;
1803 Elf_Internal_Rela *irelalign;
1805 Elf_Internal_Sym *isym, *isymend;
1806 struct elf_link_hash_entry **sym_hashes;
1807 struct elf_link_hash_entry **end_hashes;
1808 unsigned int symcount;
1810 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1812 contents = elf_section_data (sec)->this_hdr.contents;
1817 irel = elf_section_data (sec)->relocs;
1818 irelend = irel + sec->reloc_count;
1820 if (sec->reloc_count > 0)
1822 /* If there is an align reloc at the end of the section ignore it.
1823 GAS creates these relocs for reasons of its own, and they just
1824 serve to keep the section artifically inflated. */
1825 if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN)
1828 /* The deletion must stop at the next ALIGN reloc for an aligment
1829 power larger than, or not a multiple of, the number of bytes we
1831 for (; irel < irelend; irel++)
1833 int alignment = 1 << irel->r_addend;
1835 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1836 && irel->r_offset > addr
1837 && irel->r_offset < toaddr
1838 && (count < alignment
1839 || alignment % count != 0))
1842 toaddr = irel->r_offset;
1848 /* Actually delete the bytes. */
1849 memmove (contents + addr, contents + addr + count,
1850 (size_t) (toaddr - addr - count));
1852 /* Adjust the section's size if we are shrinking it, or else
1853 pad the bytes between the end of the shrunken region and
1854 the start of the next region with NOP codes. */
1855 if (irelalign == NULL)
1858 /* Include symbols at the end of the section, but
1859 not at the end of a sub-region of the section. */
1866 #define NOP_OPCODE 0xcb
1868 for (i = 0; i < count; i ++)
1869 bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
1872 /* Adjust all the relocs. */
1873 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1875 /* Get the new reloc address. */
1876 if ((irel->r_offset > addr
1877 && irel->r_offset < toaddr)
1878 || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1879 && irel->r_offset == toaddr))
1880 irel->r_offset -= count;
1883 /* Adjust the local symbols in the section, reducing their value
1884 by the number of bytes deleted. Note - symbols within the deleted
1885 region are moved to the address of the start of the region, which
1886 actually means that they will address the byte beyond the end of
1887 the region once the deletion has been completed. */
1888 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1889 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1890 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1892 if (isym->st_shndx == sec_shndx
1893 && isym->st_value > addr
1894 && isym->st_value < toaddr)
1896 if (isym->st_value < addr + count)
1897 isym->st_value = addr;
1899 isym->st_value -= count;
1901 /* Adjust the function symbol's size as well. */
1902 else if (isym->st_shndx == sec_shndx
1903 && ELF_ST_TYPE (isym->st_info) == STT_FUNC
1904 && isym->st_value + isym->st_size > addr
1905 && isym->st_value + isym->st_size < toaddr)
1906 isym->st_size -= count;
1909 /* Now adjust the global symbols defined in this section. */
1910 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1911 - symtab_hdr->sh_info);
1912 sym_hashes = elf_sym_hashes (abfd);
1913 end_hashes = sym_hashes + symcount;
1914 for (; sym_hashes < end_hashes; sym_hashes++)
1916 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1918 if ((sym_hash->root.type == bfd_link_hash_defined
1919 || sym_hash->root.type == bfd_link_hash_defweak)
1920 && sym_hash->root.u.def.section == sec
1921 && sym_hash->root.u.def.value > addr
1922 && sym_hash->root.u.def.value < toaddr)
1924 if (sym_hash->root.u.def.value < addr + count)
1925 sym_hash->root.u.def.value = addr;
1927 sym_hash->root.u.def.value -= count;
1929 /* Adjust the function symbol's size as well. */
1930 else if (sym_hash->root.type == bfd_link_hash_defined
1931 && sym_hash->root.u.def.section == sec
1932 && sym_hash->type == STT_FUNC
1933 && sym_hash->root.u.def.value + sym_hash->size > addr
1934 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1935 sym_hash->size -= count;
1938 /* See if we can move the ALIGN reloc forward.
1939 We have adjusted r_offset for it already. */
1940 if (irelalign != NULL)
1942 bfd_vma alignto, alignaddr;
1944 if ((int) irelalign->r_addend > 0)
1946 /* This is the old address. */
1947 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1948 /* This is where the align points to now. */
1949 alignaddr = BFD_ALIGN (irelalign->r_offset,
1950 1 << irelalign->r_addend);
1951 if (alignaddr < alignto)
1952 /* Tail recursion. */
1953 return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr,
1954 (int) (alignto - alignaddr));
1961 /* Return TRUE if a symbol exists at the given address, else return
1965 mn10300_elf_symbol_address_p (bfd *abfd,
1967 Elf_Internal_Sym *isym,
1970 Elf_Internal_Shdr *symtab_hdr;
1971 unsigned int sec_shndx;
1972 Elf_Internal_Sym *isymend;
1973 struct elf_link_hash_entry **sym_hashes;
1974 struct elf_link_hash_entry **end_hashes;
1975 unsigned int symcount;
1977 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1979 /* Examine all the symbols. */
1980 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1981 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1982 if (isym->st_shndx == sec_shndx
1983 && isym->st_value == addr)
1986 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1987 - symtab_hdr->sh_info);
1988 sym_hashes = elf_sym_hashes (abfd);
1989 end_hashes = sym_hashes + symcount;
1990 for (; sym_hashes < end_hashes; sym_hashes++)
1992 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1994 if ((sym_hash->root.type == bfd_link_hash_defined
1995 || sym_hash->root.type == bfd_link_hash_defweak)
1996 && sym_hash->root.u.def.section == sec
1997 && sym_hash->root.u.def.value == addr)
2004 /* This function handles relaxing for the mn10300.
2006 There are quite a few relaxing opportunities available on the mn10300:
2008 * calls:32 -> calls:16 2 bytes
2009 * call:32 -> call:16 2 bytes
2011 * call:32 -> calls:32 1 byte
2012 * call:16 -> calls:16 1 byte
2013 * These are done anytime using "calls" would result
2014 in smaller code, or when necessary to preserve the
2015 meaning of the program.
2019 * In some circumstances we can move instructions
2020 from a function prologue into a "call" instruction.
2021 This is only done if the resulting code is no larger
2022 than the original code.
2024 * jmp:32 -> jmp:16 2 bytes
2025 * jmp:16 -> bra:8 1 byte
2027 * If the previous instruction is a conditional branch
2028 around the jump/bra, we may be able to reverse its condition
2029 and change its target to the jump's target. The jump/bra
2030 can then be deleted. 2 bytes
2032 * mov abs32 -> mov abs16 1 or 2 bytes
2034 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
2035 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
2037 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
2038 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
2040 We don't handle imm16->imm8 or d16->d8 as they're very rare
2041 and somewhat more difficult to support. */
2044 mn10300_elf_relax_section (bfd *abfd,
2046 struct bfd_link_info *link_info,
2049 Elf_Internal_Shdr *symtab_hdr;
2050 Elf_Internal_Rela *internal_relocs = NULL;
2051 Elf_Internal_Rela *irel, *irelend;
2052 bfd_byte *contents = NULL;
2053 Elf_Internal_Sym *isymbuf = NULL;
2054 struct elf32_mn10300_link_hash_table *hash_table;
2055 asection *section = sec;
2056 bfd_vma align_gap_adjustment;
2058 if (link_info->relocatable)
2059 (*link_info->callbacks->einfo)
2060 (_("%P%F: --relax and -r may not be used together\n"));
2062 /* Assume nothing changes. */
2065 /* We need a pointer to the mn10300 specific hash table. */
2066 hash_table = elf32_mn10300_hash_table (link_info);
2067 if (hash_table == NULL)
2070 /* Initialize fields in each hash table entry the first time through. */
2071 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
2075 /* Iterate over all the input bfds. */
2076 for (input_bfd = link_info->input_bfds;
2078 input_bfd = input_bfd->link_next)
2080 /* We're going to need all the symbols for each bfd. */
2081 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2082 if (symtab_hdr->sh_info != 0)
2084 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2085 if (isymbuf == NULL)
2086 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2087 symtab_hdr->sh_info, 0,
2089 if (isymbuf == NULL)
2093 /* Iterate over each section in this bfd. */
2094 for (section = input_bfd->sections;
2096 section = section->next)
2098 struct elf32_mn10300_link_hash_entry *hash;
2099 asection *sym_sec = NULL;
2100 const char *sym_name;
2103 /* If there's nothing to do in this section, skip it. */
2104 if (! ((section->flags & SEC_RELOC) != 0
2105 && section->reloc_count != 0))
2107 if ((section->flags & SEC_ALLOC) == 0)
2110 /* Get cached copy of section contents if it exists. */
2111 if (elf_section_data (section)->this_hdr.contents != NULL)
2112 contents = elf_section_data (section)->this_hdr.contents;
2113 else if (section->size != 0)
2115 /* Go get them off disk. */
2116 if (!bfd_malloc_and_get_section (input_bfd, section,
2123 /* If there aren't any relocs, then there's nothing to do. */
2124 if ((section->flags & SEC_RELOC) != 0
2125 && section->reloc_count != 0)
2127 /* Get a copy of the native relocations. */
2128 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2130 link_info->keep_memory);
2131 if (internal_relocs == NULL)
2134 /* Now examine each relocation. */
2135 irel = internal_relocs;
2136 irelend = irel + section->reloc_count;
2137 for (; irel < irelend; irel++)
2140 unsigned long r_index;
2143 r_type = ELF32_R_TYPE (irel->r_info);
2144 r_index = ELF32_R_SYM (irel->r_info);
2146 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
2149 /* We need the name and hash table entry of the target
2154 if (r_index < symtab_hdr->sh_info)
2156 /* A local symbol. */
2157 Elf_Internal_Sym *isym;
2158 struct elf_link_hash_table *elftab;
2161 isym = isymbuf + r_index;
2162 if (isym->st_shndx == SHN_UNDEF)
2163 sym_sec = bfd_und_section_ptr;
2164 else if (isym->st_shndx == SHN_ABS)
2165 sym_sec = bfd_abs_section_ptr;
2166 else if (isym->st_shndx == SHN_COMMON)
2167 sym_sec = bfd_com_section_ptr;
2170 = bfd_section_from_elf_index (input_bfd,
2174 = bfd_elf_string_from_elf_section (input_bfd,
2179 /* If it isn't a function, then we don't care
2181 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
2184 /* Tack on an ID so we can uniquely identify this
2185 local symbol in the global hash table. */
2186 amt = strlen (sym_name) + 10;
2187 new_name = bfd_malloc (amt);
2188 if (new_name == NULL)
2191 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2192 sym_name = new_name;
2194 elftab = &hash_table->static_hash_table->root;
2195 hash = ((struct elf32_mn10300_link_hash_entry *)
2196 elf_link_hash_lookup (elftab, sym_name,
2197 TRUE, TRUE, FALSE));
2202 r_index -= symtab_hdr->sh_info;
2203 hash = (struct elf32_mn10300_link_hash_entry *)
2204 elf_sym_hashes (input_bfd)[r_index];
2207 sym_name = hash->root.root.root.string;
2208 if ((section->flags & SEC_CODE) != 0)
2210 /* If this is not a "call" instruction, then we
2211 should convert "call" instructions to "calls"
2213 code = bfd_get_8 (input_bfd,
2214 contents + irel->r_offset - 1);
2215 if (code != 0xdd && code != 0xcd)
2216 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2219 /* If this is a jump/call, then bump the
2220 direct_calls counter. Else force "call" to
2221 "calls" conversions. */
2222 if (r_type == R_MN10300_PCREL32
2223 || r_type == R_MN10300_PLT32
2224 || r_type == R_MN10300_PLT16
2225 || r_type == R_MN10300_PCREL16)
2226 hash->direct_calls++;
2228 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2232 /* Now look at the actual contents to get the stack size,
2233 and a list of what registers were saved in the prologue
2235 if ((section->flags & SEC_CODE) != 0)
2237 Elf_Internal_Sym *isym, *isymend;
2238 unsigned int sec_shndx;
2239 struct elf_link_hash_entry **hashes;
2240 struct elf_link_hash_entry **end_hashes;
2241 unsigned int symcount;
2243 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2246 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2247 - symtab_hdr->sh_info);
2248 hashes = elf_sym_hashes (input_bfd);
2249 end_hashes = hashes + symcount;
2251 /* Look at each function defined in this section and
2252 update info for that function. */
2253 isymend = isymbuf + symtab_hdr->sh_info;
2254 for (isym = isymbuf; isym < isymend; isym++)
2256 if (isym->st_shndx == sec_shndx
2257 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
2259 struct elf_link_hash_table *elftab;
2261 struct elf_link_hash_entry **lhashes = hashes;
2263 /* Skip a local symbol if it aliases a
2265 for (; lhashes < end_hashes; lhashes++)
2267 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
2268 if ((hash->root.root.type == bfd_link_hash_defined
2269 || hash->root.root.type == bfd_link_hash_defweak)
2270 && hash->root.root.u.def.section == section
2271 && hash->root.type == STT_FUNC
2272 && hash->root.root.u.def.value == isym->st_value)
2275 if (lhashes != end_hashes)
2278 if (isym->st_shndx == SHN_UNDEF)
2279 sym_sec = bfd_und_section_ptr;
2280 else if (isym->st_shndx == SHN_ABS)
2281 sym_sec = bfd_abs_section_ptr;
2282 else if (isym->st_shndx == SHN_COMMON)
2283 sym_sec = bfd_com_section_ptr;
2286 = bfd_section_from_elf_index (input_bfd,
2289 sym_name = (bfd_elf_string_from_elf_section
2290 (input_bfd, symtab_hdr->sh_link,
2293 /* Tack on an ID so we can uniquely identify this
2294 local symbol in the global hash table. */
2295 amt = strlen (sym_name) + 10;
2296 new_name = bfd_malloc (amt);
2297 if (new_name == NULL)
2300 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2301 sym_name = new_name;
2303 elftab = &hash_table->static_hash_table->root;
2304 hash = ((struct elf32_mn10300_link_hash_entry *)
2305 elf_link_hash_lookup (elftab, sym_name,
2306 TRUE, TRUE, FALSE));
2308 compute_function_info (input_bfd, hash,
2309 isym->st_value, contents);
2310 hash->value = isym->st_value;
2314 for (; hashes < end_hashes; hashes++)
2316 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2317 if ((hash->root.root.type == bfd_link_hash_defined
2318 || hash->root.root.type == bfd_link_hash_defweak)
2319 && hash->root.root.u.def.section == section
2320 && hash->root.type == STT_FUNC)
2321 compute_function_info (input_bfd, hash,
2322 (hash)->root.root.u.def.value,
2327 /* Cache or free any memory we allocated for the relocs. */
2328 if (internal_relocs != NULL
2329 && elf_section_data (section)->relocs != internal_relocs)
2330 free (internal_relocs);
2331 internal_relocs = NULL;
2333 /* Cache or free any memory we allocated for the contents. */
2334 if (contents != NULL
2335 && elf_section_data (section)->this_hdr.contents != contents)
2337 if (! link_info->keep_memory)
2341 /* Cache the section contents for elf_link_input_bfd. */
2342 elf_section_data (section)->this_hdr.contents = contents;
2348 /* Cache or free any memory we allocated for the symbols. */
2350 && symtab_hdr->contents != (unsigned char *) isymbuf)
2352 if (! link_info->keep_memory)
2356 /* Cache the symbols for elf_link_input_bfd. */
2357 symtab_hdr->contents = (unsigned char *) isymbuf;
2363 /* Now iterate on each symbol in the hash table and perform
2364 the final initialization steps on each. */
2365 elf32_mn10300_link_hash_traverse (hash_table,
2366 elf32_mn10300_finish_hash_table_entry,
2368 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2369 elf32_mn10300_finish_hash_table_entry,
2373 /* This section of code collects all our local symbols, sorts
2374 them by value, and looks for multiple symbols referring to
2375 the same address. For those symbols, the flags are merged.
2376 At this point, the only flag that can be set is
2377 MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2379 int static_count = 0, i;
2380 struct elf32_mn10300_link_hash_entry **entries;
2381 struct elf32_mn10300_link_hash_entry **ptr;
2383 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2384 elf32_mn10300_count_hash_table_entries,
2387 entries = bfd_malloc (static_count * sizeof (* ptr));
2390 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2391 elf32_mn10300_list_hash_table_entries,
2394 qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
2396 for (i = 0; i < static_count - 1; i++)
2397 if (entries[i]->value && entries[i]->value == entries[i+1]->value)
2399 int v = entries[i]->flags;
2402 for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
2403 v |= entries[j]->flags;
2405 for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
2406 entries[j]->flags = v;
2412 /* All entries in the hash table are fully initialized. */
2413 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
2415 /* Now that everything has been initialized, go through each
2416 code section and delete any prologue insns which will be
2417 redundant because their operations will be performed by
2418 a "call" instruction. */
2419 for (input_bfd = link_info->input_bfds;
2421 input_bfd = input_bfd->link_next)
2423 /* We're going to need all the local symbols for each bfd. */
2424 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2425 if (symtab_hdr->sh_info != 0)
2427 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2428 if (isymbuf == NULL)
2429 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2430 symtab_hdr->sh_info, 0,
2432 if (isymbuf == NULL)
2436 /* Walk over each section in this bfd. */
2437 for (section = input_bfd->sections;
2439 section = section->next)
2441 unsigned int sec_shndx;
2442 Elf_Internal_Sym *isym, *isymend;
2443 struct elf_link_hash_entry **hashes;
2444 struct elf_link_hash_entry **end_hashes;
2445 unsigned int symcount;
2447 /* Skip non-code sections and empty sections. */
2448 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2451 if (section->reloc_count != 0)
2453 /* Get a copy of the native relocations. */
2454 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2456 link_info->keep_memory);
2457 if (internal_relocs == NULL)
2461 /* Get cached copy of section contents if it exists. */
2462 if (elf_section_data (section)->this_hdr.contents != NULL)
2463 contents = elf_section_data (section)->this_hdr.contents;
2466 /* Go get them off disk. */
2467 if (!bfd_malloc_and_get_section (input_bfd, section,
2472 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2475 /* Now look for any function in this section which needs
2476 insns deleted from its prologue. */
2477 isymend = isymbuf + symtab_hdr->sh_info;
2478 for (isym = isymbuf; isym < isymend; isym++)
2480 struct elf32_mn10300_link_hash_entry *sym_hash;
2481 asection *sym_sec = NULL;
2482 const char *sym_name;
2484 struct elf_link_hash_table *elftab;
2487 if (isym->st_shndx != sec_shndx)
2490 if (isym->st_shndx == SHN_UNDEF)
2491 sym_sec = bfd_und_section_ptr;
2492 else if (isym->st_shndx == SHN_ABS)
2493 sym_sec = bfd_abs_section_ptr;
2494 else if (isym->st_shndx == SHN_COMMON)
2495 sym_sec = bfd_com_section_ptr;
2498 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2501 = bfd_elf_string_from_elf_section (input_bfd,
2502 symtab_hdr->sh_link,
2505 /* Tack on an ID so we can uniquely identify this
2506 local symbol in the global hash table. */
2507 amt = strlen (sym_name) + 10;
2508 new_name = bfd_malloc (amt);
2509 if (new_name == NULL)
2511 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2512 sym_name = new_name;
2514 elftab = & hash_table->static_hash_table->root;
2515 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2516 elf_link_hash_lookup (elftab, sym_name,
2517 FALSE, FALSE, FALSE);
2520 if (sym_hash == NULL)
2523 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2524 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2528 /* Note that we've changed things. */
2529 elf_section_data (section)->relocs = internal_relocs;
2530 elf_section_data (section)->this_hdr.contents = contents;
2531 symtab_hdr->contents = (unsigned char *) isymbuf;
2533 /* Count how many bytes we're going to delete. */
2534 if (sym_hash->movm_args)
2537 if (sym_hash->stack_size > 0)
2539 if (sym_hash->stack_size <= 128)
2545 /* Note that we've deleted prologue bytes for this
2547 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2549 /* Actually delete the bytes. */
2550 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2556 /* Something changed. Not strictly necessary, but
2557 may lead to more relaxing opportunities. */
2562 /* Look for any global functions in this section which
2563 need insns deleted from their prologues. */
2564 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2565 - symtab_hdr->sh_info);
2566 hashes = elf_sym_hashes (input_bfd);
2567 end_hashes = hashes + symcount;
2568 for (; hashes < end_hashes; hashes++)
2570 struct elf32_mn10300_link_hash_entry *sym_hash;
2572 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2573 if ((sym_hash->root.root.type == bfd_link_hash_defined
2574 || sym_hash->root.root.type == bfd_link_hash_defweak)
2575 && sym_hash->root.root.u.def.section == section
2576 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2577 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2582 /* Note that we've changed things. */
2583 elf_section_data (section)->relocs = internal_relocs;
2584 elf_section_data (section)->this_hdr.contents = contents;
2585 symtab_hdr->contents = (unsigned char *) isymbuf;
2587 /* Count how many bytes we're going to delete. */
2588 if (sym_hash->movm_args)
2591 if (sym_hash->stack_size > 0)
2593 if (sym_hash->stack_size <= 128)
2599 /* Note that we've deleted prologue bytes for this
2601 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2603 /* Actually delete the bytes. */
2604 symval = sym_hash->root.root.u.def.value;
2605 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2611 /* Something changed. Not strictly necessary, but
2612 may lead to more relaxing opportunities. */
2617 /* Cache or free any memory we allocated for the relocs. */
2618 if (internal_relocs != NULL
2619 && elf_section_data (section)->relocs != internal_relocs)
2620 free (internal_relocs);
2621 internal_relocs = NULL;
2623 /* Cache or free any memory we allocated for the contents. */
2624 if (contents != NULL
2625 && elf_section_data (section)->this_hdr.contents != contents)
2627 if (! link_info->keep_memory)
2630 /* Cache the section contents for elf_link_input_bfd. */
2631 elf_section_data (section)->this_hdr.contents = contents;
2636 /* Cache or free any memory we allocated for the symbols. */
2638 && symtab_hdr->contents != (unsigned char *) isymbuf)
2640 if (! link_info->keep_memory)
2643 /* Cache the symbols for elf_link_input_bfd. */
2644 symtab_hdr->contents = (unsigned char *) isymbuf;
2650 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2652 internal_relocs = NULL;
2654 /* For error_return. */
2657 /* We don't have to do anything for a relocatable link, if
2658 this section does not have relocs, or if this is not a
2660 if (link_info->relocatable
2661 || (sec->flags & SEC_RELOC) == 0
2662 || sec->reloc_count == 0
2663 || (sec->flags & SEC_CODE) == 0)
2666 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2668 /* Get a copy of the native relocations. */
2669 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2670 link_info->keep_memory);
2671 if (internal_relocs == NULL)
2674 /* Scan for worst case alignment gap changes. Note that this logic
2675 is not ideal; what we should do is run this scan for every
2676 opcode/address range and adjust accordingly, but that's
2677 expensive. Worst case is that for an alignment of N bytes, we
2678 move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
2679 all before it. Plus, this still doesn't cover cross-section
2680 jumps with section alignment. */
2681 irelend = internal_relocs + sec->reloc_count;
2682 align_gap_adjustment = 0;
2683 for (irel = internal_relocs; irel < irelend; irel++)
2685 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
2687 bfd_vma adj = 1 << irel->r_addend;
2688 bfd_vma aend = irel->r_offset;
2690 aend = BFD_ALIGN (aend, 1 << irel->r_addend);
2691 adj = 2 * adj - adj - 1;
2693 /* Record the biggest adjustmnet. Skip any alignment at the
2694 end of our section. */
2695 if (align_gap_adjustment < adj
2696 && aend < sec->output_section->vma + sec->output_offset + sec->size)
2697 align_gap_adjustment = adj;
2701 /* Walk through them looking for relaxing opportunities. */
2702 irelend = internal_relocs + sec->reloc_count;
2703 for (irel = internal_relocs; irel < irelend; irel++)
2706 bfd_signed_vma jump_offset;
2707 asection *sym_sec = NULL;
2708 struct elf32_mn10300_link_hash_entry *h = NULL;
2710 /* If this isn't something that can be relaxed, then ignore
2712 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2713 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2714 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2717 /* Get the section contents if we haven't done so already. */
2718 if (contents == NULL)
2720 /* Get cached copy if it exists. */
2721 if (elf_section_data (sec)->this_hdr.contents != NULL)
2722 contents = elf_section_data (sec)->this_hdr.contents;
2725 /* Go get them off disk. */
2726 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2731 /* Read this BFD's symbols if we haven't done so already. */
2732 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2734 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2735 if (isymbuf == NULL)
2736 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2737 symtab_hdr->sh_info, 0,
2739 if (isymbuf == NULL)
2743 /* Get the value of the symbol referred to by the reloc. */
2744 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2746 Elf_Internal_Sym *isym;
2747 const char *sym_name;
2750 /* A local symbol. */
2751 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2752 if (isym->st_shndx == SHN_UNDEF)
2753 sym_sec = bfd_und_section_ptr;
2754 else if (isym->st_shndx == SHN_ABS)
2755 sym_sec = bfd_abs_section_ptr;
2756 else if (isym->st_shndx == SHN_COMMON)
2757 sym_sec = bfd_com_section_ptr;
2759 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2761 sym_name = bfd_elf_string_from_elf_section (abfd,
2762 symtab_hdr->sh_link,
2765 if ((sym_sec->flags & SEC_MERGE)
2766 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2768 symval = isym->st_value;
2770 /* GAS may reduce relocations against symbols in SEC_MERGE
2771 sections to a relocation against the section symbol when
2772 the original addend was zero. When the reloc is against
2773 a section symbol we should include the addend in the
2774 offset passed to _bfd_merged_section_offset, since the
2775 location of interest is the original symbol. On the
2776 other hand, an access to "sym+addend" where "sym" is not
2777 a section symbol should not include the addend; Such an
2778 access is presumed to be an offset from "sym"; The
2779 location of interest is just "sym". */
2780 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
2781 symval += irel->r_addend;
2783 symval = _bfd_merged_section_offset (abfd, & sym_sec,
2784 elf_section_data (sym_sec)->sec_info,
2787 if (ELF_ST_TYPE (isym->st_info) != STT_SECTION)
2788 symval += irel->r_addend;
2790 symval += sym_sec->output_section->vma
2791 + sym_sec->output_offset - irel->r_addend;
2794 symval = (isym->st_value
2795 + sym_sec->output_section->vma
2796 + sym_sec->output_offset);
2798 /* Tack on an ID so we can uniquely identify this
2799 local symbol in the global hash table. */
2800 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2801 if (new_name == NULL)
2803 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2804 sym_name = new_name;
2806 h = (struct elf32_mn10300_link_hash_entry *)
2807 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2808 sym_name, FALSE, FALSE, FALSE);
2815 /* An external symbol. */
2816 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2817 h = (struct elf32_mn10300_link_hash_entry *)
2818 (elf_sym_hashes (abfd)[indx]);
2819 BFD_ASSERT (h != NULL);
2820 if (h->root.root.type != bfd_link_hash_defined
2821 && h->root.root.type != bfd_link_hash_defweak)
2822 /* This appears to be a reference to an undefined
2823 symbol. Just ignore it--it will be caught by the
2824 regular reloc processing. */
2827 /* Check for a reference to a discarded symbol and ignore it. */
2828 if (h->root.root.u.def.section->output_section == NULL)
2831 sym_sec = h->root.root.u.def.section->output_section;
2833 symval = (h->root.root.u.def.value
2834 + h->root.root.u.def.section->output_section->vma
2835 + h->root.root.u.def.section->output_offset);
2838 /* For simplicity of coding, we are going to modify the section
2839 contents, the section relocs, and the BFD symbol table. We
2840 must tell the rest of the code not to free up this
2841 information. It would be possible to instead create a table
2842 of changes which have to be made, as is done in coff-mips.c;
2843 that would be more work, but would require less memory when
2844 the linker is run. */
2846 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2847 branch/call, also deal with "call" -> "calls" conversions and
2848 insertion of prologue data into "call" instructions. */
2849 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2850 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2852 bfd_vma value = symval;
2854 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2856 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2857 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2858 && h->root.plt.offset != (bfd_vma) -1)
2862 splt = bfd_get_section_by_name (elf_hash_table (link_info)
2865 value = ((splt->output_section->vma
2866 + splt->output_offset
2867 + h->root.plt.offset)
2868 - (sec->output_section->vma
2869 + sec->output_offset
2873 /* If we've got a "call" instruction that needs to be turned
2874 into a "calls" instruction, do so now. It saves a byte. */
2875 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2879 /* Get the opcode. */
2880 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2882 /* Make sure we're working with a "call" instruction! */
2885 /* Note that we've changed the relocs, section contents,
2887 elf_section_data (sec)->relocs = internal_relocs;
2888 elf_section_data (sec)->this_hdr.contents = contents;
2889 symtab_hdr->contents = (unsigned char *) isymbuf;
2891 /* Fix the opcode. */
2892 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2893 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2895 /* Fix irel->r_offset and irel->r_addend. */
2896 irel->r_offset += 1;
2897 irel->r_addend += 1;
2899 /* Delete one byte of data. */
2900 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2901 irel->r_offset + 3, 1))
2904 /* That will change things, so, we should relax again.
2905 Note that this is not required, and it may be slow. */
2911 /* We've got a "call" instruction which needs some data
2912 from target function filled in. */
2915 /* Get the opcode. */
2916 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2918 /* Insert data from the target function into the "call"
2919 instruction if needed. */
2922 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2923 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2924 contents + irel->r_offset + 5);
2928 /* Deal with pc-relative gunk. */
2929 value -= (sec->output_section->vma + sec->output_offset);
2930 value -= irel->r_offset;
2931 value += irel->r_addend;
2933 /* See if the value will fit in 16 bits, note the high value is
2934 0x7fff + 2 as the target will be two bytes closer if we are
2935 able to relax, if it's in the same section. */
2936 if (sec->output_section == sym_sec->output_section)
2937 jump_offset = 0x8001;
2939 jump_offset = 0x7fff;
2941 /* Account for jumps across alignment boundaries using
2942 align_gap_adjustment. */
2943 if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
2944 && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
2948 /* Get the opcode. */
2949 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2951 if (code != 0xdc && code != 0xdd && code != 0xff)
2954 /* Note that we've changed the relocs, section contents, etc. */
2955 elf_section_data (sec)->relocs = internal_relocs;
2956 elf_section_data (sec)->this_hdr.contents = contents;
2957 symtab_hdr->contents = (unsigned char *) isymbuf;
2959 /* Fix the opcode. */
2961 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2962 else if (code == 0xdd)
2963 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2964 else if (code == 0xff)
2965 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2967 /* Fix the relocation's type. */
2968 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2969 (ELF32_R_TYPE (irel->r_info)
2970 == (int) R_MN10300_PLT32)
2974 /* Delete two bytes of data. */
2975 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2976 irel->r_offset + 1, 2))
2979 /* That will change things, so, we should relax again.
2980 Note that this is not required, and it may be slow. */
2985 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2987 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2989 bfd_vma value = symval;
2991 /* If we've got a "call" instruction that needs to be turned
2992 into a "calls" instruction, do so now. It saves a byte. */
2993 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2997 /* Get the opcode. */
2998 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3000 /* Make sure we're working with a "call" instruction! */
3003 /* Note that we've changed the relocs, section contents,
3005 elf_section_data (sec)->relocs = internal_relocs;
3006 elf_section_data (sec)->this_hdr.contents = contents;
3007 symtab_hdr->contents = (unsigned char *) isymbuf;
3009 /* Fix the opcode. */
3010 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
3011 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3013 /* Fix irel->r_offset and irel->r_addend. */
3014 irel->r_offset += 1;
3015 irel->r_addend += 1;
3017 /* Delete one byte of data. */
3018 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3019 irel->r_offset + 1, 1))
3022 /* That will change things, so, we should relax again.
3023 Note that this is not required, and it may be slow. */
3031 /* Get the opcode. */
3032 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3034 /* Insert data from the target function into the "call"
3035 instruction if needed. */
3038 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
3039 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3040 contents + irel->r_offset + 3);
3044 /* Deal with pc-relative gunk. */
3045 value -= (sec->output_section->vma + sec->output_offset);
3046 value -= irel->r_offset;
3047 value += irel->r_addend;
3049 /* See if the value will fit in 8 bits, note the high value is
3050 0x7f + 1 as the target will be one bytes closer if we are
3052 if ((long) value < 0x80 && (long) value > -0x80)
3056 /* Get the opcode. */
3057 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3062 /* Note that we've changed the relocs, section contents, etc. */
3063 elf_section_data (sec)->relocs = internal_relocs;
3064 elf_section_data (sec)->this_hdr.contents = contents;
3065 symtab_hdr->contents = (unsigned char *) isymbuf;
3067 /* Fix the opcode. */
3068 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
3070 /* Fix the relocation's type. */
3071 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3074 /* Delete one byte of data. */
3075 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3076 irel->r_offset + 1, 1))
3079 /* That will change things, so, we should relax again.
3080 Note that this is not required, and it may be slow. */
3085 /* Try to eliminate an unconditional 8 bit pc-relative branch
3086 which immediately follows a conditional 8 bit pc-relative
3087 branch around the unconditional branch.
3094 This happens when the bCC can't reach lab2 at assembly time,
3095 but due to other relaxations it can reach at link time. */
3096 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
3098 Elf_Internal_Rela *nrel;
3099 bfd_vma value = symval;
3102 /* Deal with pc-relative gunk. */
3103 value -= (sec->output_section->vma + sec->output_offset);
3104 value -= irel->r_offset;
3105 value += irel->r_addend;
3107 /* Do nothing if this reloc is the last byte in the section. */
3108 if (irel->r_offset == sec->size)
3111 /* See if the next instruction is an unconditional pc-relative
3112 branch, more often than not this test will fail, so we
3113 test it first to speed things up. */
3114 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
3118 /* Also make sure the next relocation applies to the next
3119 instruction and that it's a pc-relative 8 bit branch. */
3122 || irel->r_offset + 2 != nrel->r_offset
3123 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
3126 /* Make sure our destination immediately follows the
3127 unconditional branch. */
3128 if (symval != (sec->output_section->vma + sec->output_offset
3129 + irel->r_offset + 3))
3132 /* Now make sure we are a conditional branch. This may not
3133 be necessary, but why take the chance.
3135 Note these checks assume that R_MN10300_PCREL8 relocs
3136 only occur on bCC and bCCx insns. If they occured
3137 elsewhere, we'd need to know the start of this insn
3138 for this check to be accurate. */
3139 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3140 if (code != 0xc0 && code != 0xc1 && code != 0xc2
3141 && code != 0xc3 && code != 0xc4 && code != 0xc5
3142 && code != 0xc6 && code != 0xc7 && code != 0xc8
3143 && code != 0xc9 && code != 0xe8 && code != 0xe9
3144 && code != 0xea && code != 0xeb)
3147 /* We also have to be sure there is no symbol/label
3148 at the unconditional branch. */
3149 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
3150 irel->r_offset + 1))
3153 /* Note that we've changed the relocs, section contents, etc. */
3154 elf_section_data (sec)->relocs = internal_relocs;
3155 elf_section_data (sec)->this_hdr.contents = contents;
3156 symtab_hdr->contents = (unsigned char *) isymbuf;
3158 /* Reverse the condition of the first branch. */
3204 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3206 /* Set the reloc type and symbol for the first branch
3207 from the second branch. */
3208 irel->r_info = nrel->r_info;
3210 /* Make the reloc for the second branch a null reloc. */
3211 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3214 /* Delete two bytes of data. */
3215 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3216 irel->r_offset + 1, 2))
3219 /* That will change things, so, we should relax again.
3220 Note that this is not required, and it may be slow. */
3224 /* Try to turn a 24 immediate, displacement or absolute address
3225 into a 8 immediate, displacement or absolute address. */
3226 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
3228 bfd_vma value = symval;
3229 value += irel->r_addend;
3231 /* See if the value will fit in 8 bits. */
3232 if ((long) value < 0x7f && (long) value > -0x80)
3236 /* AM33 insns which have 24 operands are 6 bytes long and
3237 will have 0xfd as the first byte. */
3239 /* Get the first opcode. */
3240 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3244 /* Get the second opcode. */
3245 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3247 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3248 equivalent instructions exists. */
3249 if (code != 0x6b && code != 0x7b
3250 && code != 0x8b && code != 0x9b
3251 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3252 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3253 || (code & 0x0f) == 0x0e))
3255 /* Not safe if the high bit is on as relaxing may
3256 move the value out of high mem and thus not fit
3257 in a signed 8bit value. This is currently over
3259 if ((value & 0x80) == 0)
3261 /* Note that we've changed the relocation contents,
3263 elf_section_data (sec)->relocs = internal_relocs;
3264 elf_section_data (sec)->this_hdr.contents = contents;
3265 symtab_hdr->contents = (unsigned char *) isymbuf;
3267 /* Fix the opcode. */
3268 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
3269 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3271 /* Fix the relocation's type. */
3273 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3276 /* Delete two bytes of data. */
3277 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3278 irel->r_offset + 1, 2))
3281 /* That will change things, so, we should relax
3282 again. Note that this is not required, and it
3292 /* Try to turn a 32bit immediate, displacement or absolute address
3293 into a 16bit immediate, displacement or absolute address. */
3294 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
3295 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
3296 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3298 bfd_vma value = symval;
3300 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
3304 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
3307 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
3309 value = sgot->output_offset;
3312 value += h->root.got.offset;
3314 value += (elf_local_got_offsets
3315 (abfd)[ELF32_R_SYM (irel->r_info)]);
3317 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3318 value -= sgot->output_section->vma;
3319 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3320 value = (sgot->output_section->vma
3321 - (sec->output_section->vma
3322 + sec->output_offset
3328 value += irel->r_addend;
3330 /* See if the value will fit in 24 bits.
3331 We allow any 16bit match here. We prune those we can't
3333 if ((long) value < 0x7fffff && (long) value > -0x800000)
3337 /* AM33 insns which have 32bit operands are 7 bytes long and
3338 will have 0xfe as the first byte. */
3340 /* Get the first opcode. */
3341 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3345 /* Get the second opcode. */
3346 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3348 /* All the am33 32 -> 24 relaxing possibilities. */
3349 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3350 equivalent instructions exists. */
3351 if (code != 0x6b && code != 0x7b
3352 && code != 0x8b && code != 0x9b
3353 && (ELF32_R_TYPE (irel->r_info)
3354 != (int) R_MN10300_GOTPC32)
3355 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3356 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3357 || (code & 0x0f) == 0x0e))
3359 /* Not safe if the high bit is on as relaxing may
3360 move the value out of high mem and thus not fit
3361 in a signed 16bit value. This is currently over
3363 if ((value & 0x8000) == 0)
3365 /* Note that we've changed the relocation contents,
3367 elf_section_data (sec)->relocs = internal_relocs;
3368 elf_section_data (sec)->this_hdr.contents = contents;
3369 symtab_hdr->contents = (unsigned char *) isymbuf;
3371 /* Fix the opcode. */
3372 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
3373 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3375 /* Fix the relocation's type. */
3377 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3378 (ELF32_R_TYPE (irel->r_info)
3379 == (int) R_MN10300_GOTOFF32)
3380 ? R_MN10300_GOTOFF24
3381 : (ELF32_R_TYPE (irel->r_info)
3382 == (int) R_MN10300_GOT32)
3386 /* Delete one byte of data. */
3387 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3388 irel->r_offset + 3, 1))
3391 /* That will change things, so, we should relax
3392 again. Note that this is not required, and it
3401 /* See if the value will fit in 16 bits.
3402 We allow any 16bit match here. We prune those we can't
3404 if ((long) value < 0x7fff && (long) value > -0x8000)
3408 /* Most insns which have 32bit operands are 6 bytes long;
3409 exceptions are pcrel insns and bit insns.
3411 We handle pcrel insns above. We don't bother trying
3412 to handle the bit insns here.
3414 The first byte of the remaining insns will be 0xfc. */
3416 /* Get the first opcode. */
3417 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3422 /* Get the second opcode. */
3423 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3425 if ((code & 0xf0) < 0x80)
3426 switch (code & 0xf0)
3428 /* mov (d32,am),dn -> mov (d32,am),dn
3429 mov dm,(d32,am) -> mov dn,(d32,am)
3430 mov (d32,am),an -> mov (d32,am),an
3431 mov dm,(d32,am) -> mov dn,(d32,am)
3432 movbu (d32,am),dn -> movbu (d32,am),dn
3433 movbu dm,(d32,am) -> movbu dn,(d32,am)
3434 movhu (d32,am),dn -> movhu (d32,am),dn
3435 movhu dm,(d32,am) -> movhu dn,(d32,am) */
3444 /* Not safe if the high bit is on as relaxing may
3445 move the value out of high mem and thus not fit
3446 in a signed 16bit value. */
3448 && (value & 0x8000))
3451 /* Note that we've changed the relocation contents, etc. */
3452 elf_section_data (sec)->relocs = internal_relocs;
3453 elf_section_data (sec)->this_hdr.contents = contents;
3454 symtab_hdr->contents = (unsigned char *) isymbuf;
3456 /* Fix the opcode. */
3457 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3458 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3460 /* Fix the relocation's type. */
3461 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3462 (ELF32_R_TYPE (irel->r_info)
3463 == (int) R_MN10300_GOTOFF32)
3464 ? R_MN10300_GOTOFF16
3465 : (ELF32_R_TYPE (irel->r_info)
3466 == (int) R_MN10300_GOT32)
3468 : (ELF32_R_TYPE (irel->r_info)
3469 == (int) R_MN10300_GOTPC32)
3470 ? R_MN10300_GOTPC16 :
3473 /* Delete two bytes of data. */
3474 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3475 irel->r_offset + 2, 2))
3478 /* That will change things, so, we should relax again.
3479 Note that this is not required, and it may be slow. */
3483 else if ((code & 0xf0) == 0x80
3484 || (code & 0xf0) == 0x90)
3485 switch (code & 0xf3)
3487 /* mov dn,(abs32) -> mov dn,(abs16)
3488 movbu dn,(abs32) -> movbu dn,(abs16)
3489 movhu dn,(abs32) -> movhu dn,(abs16) */
3493 /* Note that we've changed the relocation contents, etc. */
3494 elf_section_data (sec)->relocs = internal_relocs;
3495 elf_section_data (sec)->this_hdr.contents = contents;
3496 symtab_hdr->contents = (unsigned char *) isymbuf;
3498 if ((code & 0xf3) == 0x81)
3499 code = 0x01 + (code & 0x0c);
3500 else if ((code & 0xf3) == 0x82)
3501 code = 0x02 + (code & 0x0c);
3502 else if ((code & 0xf3) == 0x83)
3503 code = 0x03 + (code & 0x0c);
3507 /* Fix the opcode. */
3508 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3510 /* Fix the relocation's type. */
3511 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3512 (ELF32_R_TYPE (irel->r_info)
3513 == (int) R_MN10300_GOTOFF32)
3514 ? R_MN10300_GOTOFF16
3515 : (ELF32_R_TYPE (irel->r_info)
3516 == (int) R_MN10300_GOT32)
3518 : (ELF32_R_TYPE (irel->r_info)
3519 == (int) R_MN10300_GOTPC32)
3520 ? R_MN10300_GOTPC16 :
3523 /* The opcode got shorter too, so we have to fix the
3524 addend and offset too! */
3525 irel->r_offset -= 1;
3527 /* Delete three bytes of data. */
3528 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3529 irel->r_offset + 1, 3))
3532 /* That will change things, so, we should relax again.
3533 Note that this is not required, and it may be slow. */
3537 /* mov am,(abs32) -> mov am,(abs16)
3538 mov am,(d32,sp) -> mov am,(d16,sp)
3539 mov dm,(d32,sp) -> mov dm,(d32,sp)
3540 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3541 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3547 /* sp-based offsets are zero-extended. */
3548 if (code >= 0x90 && code <= 0x93
3549 && (long) value < 0)
3552 /* Note that we've changed the relocation contents, etc. */
3553 elf_section_data (sec)->relocs = internal_relocs;
3554 elf_section_data (sec)->this_hdr.contents = contents;
3555 symtab_hdr->contents = (unsigned char *) isymbuf;
3557 /* Fix the opcode. */
3558 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3559 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3561 /* Fix the relocation's type. */
3562 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3563 (ELF32_R_TYPE (irel->r_info)
3564 == (int) R_MN10300_GOTOFF32)
3565 ? R_MN10300_GOTOFF16
3566 : (ELF32_R_TYPE (irel->r_info)
3567 == (int) R_MN10300_GOT32)
3569 : (ELF32_R_TYPE (irel->r_info)
3570 == (int) R_MN10300_GOTPC32)
3571 ? R_MN10300_GOTPC16 :
3574 /* Delete two bytes of data. */
3575 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3576 irel->r_offset + 2, 2))
3579 /* That will change things, so, we should relax again.
3580 Note that this is not required, and it may be slow. */
3584 else if ((code & 0xf0) < 0xf0)
3585 switch (code & 0xfc)
3587 /* mov imm32,dn -> mov imm16,dn
3588 mov imm32,an -> mov imm16,an
3589 mov (abs32),dn -> mov (abs16),dn
3590 movbu (abs32),dn -> movbu (abs16),dn
3591 movhu (abs32),dn -> movhu (abs16),dn */
3597 /* Not safe if the high bit is on as relaxing may
3598 move the value out of high mem and thus not fit
3599 in a signed 16bit value. */
3601 && (value & 0x8000))
3604 /* mov imm16, an zero-extends the immediate. */
3606 && (long) value < 0)
3609 /* Note that we've changed the relocation contents, etc. */
3610 elf_section_data (sec)->relocs = internal_relocs;
3611 elf_section_data (sec)->this_hdr.contents = contents;
3612 symtab_hdr->contents = (unsigned char *) isymbuf;
3614 if ((code & 0xfc) == 0xcc)
3615 code = 0x2c + (code & 0x03);
3616 else if ((code & 0xfc) == 0xdc)
3617 code = 0x24 + (code & 0x03);
3618 else if ((code & 0xfc) == 0xa4)
3619 code = 0x30 + (code & 0x03);
3620 else if ((code & 0xfc) == 0xa8)
3621 code = 0x34 + (code & 0x03);
3622 else if ((code & 0xfc) == 0xac)
3623 code = 0x38 + (code & 0x03);
3627 /* Fix the opcode. */
3628 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3630 /* Fix the relocation's type. */
3631 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3632 (ELF32_R_TYPE (irel->r_info)
3633 == (int) R_MN10300_GOTOFF32)
3634 ? R_MN10300_GOTOFF16
3635 : (ELF32_R_TYPE (irel->r_info)
3636 == (int) R_MN10300_GOT32)
3638 : (ELF32_R_TYPE (irel->r_info)
3639 == (int) R_MN10300_GOTPC32)
3640 ? R_MN10300_GOTPC16 :
3643 /* The opcode got shorter too, so we have to fix the
3644 addend and offset too! */
3645 irel->r_offset -= 1;
3647 /* Delete three bytes of data. */
3648 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3649 irel->r_offset + 1, 3))
3652 /* That will change things, so, we should relax again.
3653 Note that this is not required, and it may be slow. */
3657 /* mov (abs32),an -> mov (abs16),an
3658 mov (d32,sp),an -> mov (d16,sp),an
3659 mov (d32,sp),dn -> mov (d16,sp),dn
3660 movbu (d32,sp),dn -> movbu (d16,sp),dn
3661 movhu (d32,sp),dn -> movhu (d16,sp),dn
3662 add imm32,dn -> add imm16,dn
3663 cmp imm32,dn -> cmp imm16,dn
3664 add imm32,an -> add imm16,an
3665 cmp imm32,an -> cmp imm16,an
3666 and imm32,dn -> and imm16,dn
3667 or imm32,dn -> or imm16,dn
3668 xor imm32,dn -> xor imm16,dn
3669 btst imm32,dn -> btst imm16,dn */
3685 /* cmp imm16, an zero-extends the immediate. */
3687 && (long) value < 0)
3690 /* So do sp-based offsets. */
3691 if (code >= 0xb0 && code <= 0xb3
3692 && (long) value < 0)
3695 /* Note that we've changed the relocation contents, etc. */
3696 elf_section_data (sec)->relocs = internal_relocs;
3697 elf_section_data (sec)->this_hdr.contents = contents;
3698 symtab_hdr->contents = (unsigned char *) isymbuf;
3700 /* Fix the opcode. */
3701 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3702 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3704 /* Fix the relocation's type. */
3705 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3706 (ELF32_R_TYPE (irel->r_info)
3707 == (int) R_MN10300_GOTOFF32)
3708 ? R_MN10300_GOTOFF16
3709 : (ELF32_R_TYPE (irel->r_info)
3710 == (int) R_MN10300_GOT32)
3712 : (ELF32_R_TYPE (irel->r_info)
3713 == (int) R_MN10300_GOTPC32)
3714 ? R_MN10300_GOTPC16 :
3717 /* Delete two bytes of data. */
3718 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3719 irel->r_offset + 2, 2))
3722 /* That will change things, so, we should relax again.
3723 Note that this is not required, and it may be slow. */
3727 else if (code == 0xfe)
3729 /* add imm32,sp -> add imm16,sp */
3731 /* Note that we've changed the relocation contents, etc. */
3732 elf_section_data (sec)->relocs = internal_relocs;
3733 elf_section_data (sec)->this_hdr.contents = contents;
3734 symtab_hdr->contents = (unsigned char *) isymbuf;
3736 /* Fix the opcode. */
3737 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3738 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3740 /* Fix the relocation's type. */
3741 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3742 (ELF32_R_TYPE (irel->r_info)
3743 == (int) R_MN10300_GOT32)
3745 : (ELF32_R_TYPE (irel->r_info)
3746 == (int) R_MN10300_GOTOFF32)
3747 ? R_MN10300_GOTOFF16
3748 : (ELF32_R_TYPE (irel->r_info)
3749 == (int) R_MN10300_GOTPC32)
3750 ? R_MN10300_GOTPC16 :
3753 /* Delete two bytes of data. */
3754 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3755 irel->r_offset + 2, 2))
3758 /* That will change things, so, we should relax again.
3759 Note that this is not required, and it may be slow. */
3768 && symtab_hdr->contents != (unsigned char *) isymbuf)
3770 if (! link_info->keep_memory)
3774 /* Cache the symbols for elf_link_input_bfd. */
3775 symtab_hdr->contents = (unsigned char *) isymbuf;
3779 if (contents != NULL
3780 && elf_section_data (sec)->this_hdr.contents != contents)
3782 if (! link_info->keep_memory)
3786 /* Cache the section contents for elf_link_input_bfd. */
3787 elf_section_data (sec)->this_hdr.contents = contents;
3791 if (internal_relocs != NULL
3792 && elf_section_data (sec)->relocs != internal_relocs)
3793 free (internal_relocs);
3799 && symtab_hdr->contents != (unsigned char *) isymbuf)
3801 if (contents != NULL
3802 && elf_section_data (section)->this_hdr.contents != contents)
3804 if (internal_relocs != NULL
3805 && elf_section_data (section)->relocs != internal_relocs)
3806 free (internal_relocs);
3811 /* This is a version of bfd_generic_get_relocated_section_contents
3812 which uses mn10300_elf_relocate_section. */
3815 mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
3816 struct bfd_link_info *link_info,
3817 struct bfd_link_order *link_order,
3819 bfd_boolean relocatable,
3822 Elf_Internal_Shdr *symtab_hdr;
3823 asection *input_section = link_order->u.indirect.section;
3824 bfd *input_bfd = input_section->owner;
3825 asection **sections = NULL;
3826 Elf_Internal_Rela *internal_relocs = NULL;
3827 Elf_Internal_Sym *isymbuf = NULL;
3829 /* We only need to handle the case of relaxing, or of having a
3830 particular set of section contents, specially. */
3832 || elf_section_data (input_section)->this_hdr.contents == NULL)
3833 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3838 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3840 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3841 (size_t) input_section->size);
3843 if ((input_section->flags & SEC_RELOC) != 0
3844 && input_section->reloc_count > 0)
3847 Elf_Internal_Sym *isym, *isymend;
3850 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
3852 if (internal_relocs == NULL)
3855 if (symtab_hdr->sh_info != 0)
3857 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3858 if (isymbuf == NULL)
3859 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3860 symtab_hdr->sh_info, 0,
3862 if (isymbuf == NULL)
3866 amt = symtab_hdr->sh_info;
3867 amt *= sizeof (asection *);
3868 sections = bfd_malloc (amt);
3869 if (sections == NULL && amt != 0)
3872 isymend = isymbuf + symtab_hdr->sh_info;
3873 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3877 if (isym->st_shndx == SHN_UNDEF)
3878 isec = bfd_und_section_ptr;
3879 else if (isym->st_shndx == SHN_ABS)
3880 isec = bfd_abs_section_ptr;
3881 else if (isym->st_shndx == SHN_COMMON)
3882 isec = bfd_com_section_ptr;
3884 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3889 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3890 input_section, data, internal_relocs,
3894 if (sections != NULL)
3896 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3898 if (internal_relocs != elf_section_data (input_section)->relocs)
3899 free (internal_relocs);
3905 if (sections != NULL)
3907 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3909 if (internal_relocs != NULL
3910 && internal_relocs != elf_section_data (input_section)->relocs)
3911 free (internal_relocs);
3915 /* Assorted hash table functions. */
3917 /* Initialize an entry in the link hash table. */
3919 /* Create an entry in an MN10300 ELF linker hash table. */
3921 static struct bfd_hash_entry *
3922 elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
3923 struct bfd_hash_table *table,
3926 struct elf32_mn10300_link_hash_entry *ret =
3927 (struct elf32_mn10300_link_hash_entry *) entry;
3929 /* Allocate the structure if it has not already been allocated by a
3932 ret = (struct elf32_mn10300_link_hash_entry *)
3933 bfd_hash_allocate (table, sizeof (* ret));
3935 return (struct bfd_hash_entry *) ret;
3937 /* Call the allocation method of the superclass. */
3938 ret = (struct elf32_mn10300_link_hash_entry *)
3939 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3943 ret->direct_calls = 0;
3944 ret->stack_size = 0;
3946 ret->movm_stack_size = 0;
3951 return (struct bfd_hash_entry *) ret;
3954 /* Create an mn10300 ELF linker hash table. */
3956 static struct bfd_link_hash_table *
3957 elf32_mn10300_link_hash_table_create (bfd *abfd)
3959 struct elf32_mn10300_link_hash_table *ret;
3960 bfd_size_type amt = sizeof (* ret);
3962 ret = bfd_malloc (amt);
3966 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3967 elf32_mn10300_link_hash_newfunc,
3968 sizeof (struct elf32_mn10300_link_hash_entry),
3976 amt = sizeof (struct elf_link_hash_table);
3977 ret->static_hash_table = bfd_malloc (amt);
3978 if (ret->static_hash_table == NULL)
3984 if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3985 elf32_mn10300_link_hash_newfunc,
3986 sizeof (struct elf32_mn10300_link_hash_entry),
3989 free (ret->static_hash_table);
3993 return & ret->root.root;
3996 /* Free an mn10300 ELF linker hash table. */
3999 elf32_mn10300_link_hash_table_free (struct bfd_link_hash_table *hash)
4001 struct elf32_mn10300_link_hash_table *ret
4002 = (struct elf32_mn10300_link_hash_table *) hash;
4004 _bfd_generic_link_hash_table_free
4005 ((struct bfd_link_hash_table *) ret->static_hash_table);
4006 _bfd_generic_link_hash_table_free
4007 ((struct bfd_link_hash_table *) ret);
4010 static unsigned long
4011 elf_mn10300_mach (flagword flags)
4013 switch (flags & EF_MN10300_MACH)
4015 case E_MN10300_MACH_MN10300:
4017 return bfd_mach_mn10300;
4019 case E_MN10300_MACH_AM33:
4020 return bfd_mach_am33;
4022 case E_MN10300_MACH_AM33_2:
4023 return bfd_mach_am33_2;
4027 /* The final processing done just before writing out a MN10300 ELF object
4028 file. This gets the MN10300 architecture right based on the machine
4032 _bfd_mn10300_elf_final_write_processing (bfd *abfd,
4033 bfd_boolean linker ATTRIBUTE_UNUSED)
4037 switch (bfd_get_mach (abfd))
4040 case bfd_mach_mn10300:
4041 val = E_MN10300_MACH_MN10300;
4045 val = E_MN10300_MACH_AM33;
4048 case bfd_mach_am33_2:
4049 val = E_MN10300_MACH_AM33_2;
4053 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
4054 elf_elfheader (abfd)->e_flags |= val;
4058 _bfd_mn10300_elf_object_p (bfd *abfd)
4060 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
4061 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
4065 /* Merge backend specific data from an object file to the output
4066 object file when linking. */
4069 _bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4071 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4072 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4075 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4076 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
4078 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4079 bfd_get_mach (ibfd)))
4086 #define PLT0_ENTRY_SIZE 15
4087 #define PLT_ENTRY_SIZE 20
4088 #define PIC_PLT_ENTRY_SIZE 24
4090 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
4092 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
4093 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
4094 0xf0, 0xf4, /* jmp (a0) */
4097 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
4099 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
4100 0xf0, 0xf4, /* jmp (a0) */
4101 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4102 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
4105 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
4107 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
4108 0xf0, 0xf4, /* jmp (a0) */
4109 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4110 0xf8, 0x22, 8, /* mov (8,a2),a0 */
4111 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
4112 0xf0, 0xf4, /* jmp (a0) */
4115 /* Return size of the first PLT entry. */
4116 #define elf_mn10300_sizeof_plt0(info) \
4117 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4119 /* Return size of a PLT entry. */
4120 #define elf_mn10300_sizeof_plt(info) \
4121 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4123 /* Return offset of the PLT0 address in an absolute PLT entry. */
4124 #define elf_mn10300_plt_plt0_offset(info) 16
4126 /* Return offset of the linker in PLT0 entry. */
4127 #define elf_mn10300_plt0_linker_offset(info) 2
4129 /* Return offset of the GOT id in PLT0 entry. */
4130 #define elf_mn10300_plt0_gotid_offset(info) 9
4132 /* Return offset of the temporary in PLT entry. */
4133 #define elf_mn10300_plt_temp_offset(info) 8
4135 /* Return offset of the symbol in PLT entry. */
4136 #define elf_mn10300_plt_symbol_offset(info) 2
4138 /* Return offset of the relocation in PLT entry. */
4139 #define elf_mn10300_plt_reloc_offset(info) 11
4141 /* The name of the dynamic interpreter. This is put in the .interp
4144 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4146 /* Create dynamic sections when linking against a dynamic object. */
4149 _bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4153 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
4156 switch (bed->s->arch_size)
4167 bfd_set_error (bfd_error_bad_value);
4171 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4172 .rel[a].bss sections. */
4173 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4174 | SEC_LINKER_CREATED);
4176 s = bfd_make_section_with_flags (abfd,
4177 (bed->default_use_rela_p
4178 ? ".rela.plt" : ".rel.plt"),
4179 flags | SEC_READONLY);
4181 || ! bfd_set_section_alignment (abfd, s, ptralign))
4184 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4188 const char * secname;
4193 for (sec = abfd->sections; sec; sec = sec->next)
4195 secflags = bfd_get_section_flags (abfd, sec);
4196 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
4197 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
4200 secname = bfd_get_section_name (abfd, sec);
4201 relname = bfd_malloc (strlen (secname) + 6);
4202 strcpy (relname, ".rela");
4203 strcat (relname, secname);
4205 s = bfd_make_section_with_flags (abfd, relname,
4206 flags | SEC_READONLY);
4208 || ! bfd_set_section_alignment (abfd, s, ptralign))
4213 if (bed->want_dynbss)
4215 /* The .dynbss section is a place to put symbols which are defined
4216 by dynamic objects, are referenced by regular objects, and are
4217 not functions. We must allocate space for them in the process
4218 image and use a R_*_COPY reloc to tell the dynamic linker to
4219 initialize them at run time. The linker script puts the .dynbss
4220 section into the .bss section of the final image. */
4221 s = bfd_make_section_with_flags (abfd, ".dynbss",
4222 SEC_ALLOC | SEC_LINKER_CREATED);
4226 /* The .rel[a].bss section holds copy relocs. This section is not
4227 normally needed. We need to create it here, though, so that the
4228 linker will map it to an output section. We can't just create it
4229 only if we need it, because we will not know whether we need it
4230 until we have seen all the input files, and the first time the
4231 main linker code calls BFD after examining all the input files
4232 (size_dynamic_sections) the input sections have already been
4233 mapped to the output sections. If the section turns out not to
4234 be needed, we can discard it later. We will never need this
4235 section when generating a shared object, since they do not use
4239 s = bfd_make_section_with_flags (abfd,
4240 (bed->default_use_rela_p
4241 ? ".rela.bss" : ".rel.bss"),
4242 flags | SEC_READONLY);
4244 || ! bfd_set_section_alignment (abfd, s, ptralign))
4252 /* Adjust a symbol defined by a dynamic object and referenced by a
4253 regular object. The current definition is in some section of the
4254 dynamic object, but we're not including those sections. We have to
4255 change the definition to something the rest of the link can
4259 _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
4260 struct elf_link_hash_entry * h)
4265 dynobj = elf_hash_table (info)->dynobj;
4267 /* Make sure we know what is going on here. */
4268 BFD_ASSERT (dynobj != NULL
4270 || h->u.weakdef != NULL
4273 && !h->def_regular)));
4275 /* If this is a function, put it in the procedure linkage table. We
4276 will fill in the contents of the procedure linkage table later,
4277 when we know the address of the .got section. */
4278 if (h->type == STT_FUNC
4285 /* This case can occur if we saw a PLT reloc in an input
4286 file, but the symbol was never referred to by a dynamic
4287 object. In such a case, we don't actually need to build
4288 a procedure linkage table, and we can just do a REL32
4290 BFD_ASSERT (h->needs_plt);
4294 /* Make sure this symbol is output as a dynamic symbol. */
4295 if (h->dynindx == -1)
4297 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4301 s = bfd_get_section_by_name (dynobj, ".plt");
4302 BFD_ASSERT (s != NULL);
4304 /* If this is the first .plt entry, make room for the special
4307 s->size += elf_mn10300_sizeof_plt0 (info);
4309 /* If this symbol is not defined in a regular file, and we are
4310 not generating a shared library, then set the symbol to this
4311 location in the .plt. This is required to make function
4312 pointers compare as equal between the normal executable and
4313 the shared library. */
4317 h->root.u.def.section = s;
4318 h->root.u.def.value = s->size;
4321 h->plt.offset = s->size;
4323 /* Make room for this entry. */
4324 s->size += elf_mn10300_sizeof_plt (info);
4326 /* We also need to make an entry in the .got.plt section, which
4327 will be placed in the .got section by the linker script. */
4328 s = bfd_get_section_by_name (dynobj, ".got.plt");
4329 BFD_ASSERT (s != NULL);
4332 /* We also need to make an entry in the .rela.plt section. */
4333 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4334 BFD_ASSERT (s != NULL);
4335 s->size += sizeof (Elf32_External_Rela);
4340 /* If this is a weak symbol, and there is a real definition, the
4341 processor independent code will have arranged for us to see the
4342 real definition first, and we can just use the same value. */
4343 if (h->u.weakdef != NULL)
4345 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4346 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4347 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4348 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4352 /* This is a reference to a symbol defined by a dynamic object which
4353 is not a function. */
4355 /* If we are creating a shared library, we must presume that the
4356 only references to the symbol are via the global offset table.
4357 For such cases we need not do anything here; the relocations will
4358 be handled correctly by relocate_section. */
4362 /* If there are no references to this symbol that do not use the
4363 GOT, we don't need to generate a copy reloc. */
4364 if (!h->non_got_ref)
4369 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4370 h->root.root.string);
4374 /* We must allocate the symbol in our .dynbss section, which will
4375 become part of the .bss section of the executable. There will be
4376 an entry for this symbol in the .dynsym section. The dynamic
4377 object will contain position independent code, so all references
4378 from the dynamic object to this symbol will go through the global
4379 offset table. The dynamic linker will use the .dynsym entry to
4380 determine the address it must put in the global offset table, so
4381 both the dynamic object and the regular object will refer to the
4382 same memory location for the variable. */
4384 s = bfd_get_section_by_name (dynobj, ".dynbss");
4385 BFD_ASSERT (s != NULL);
4387 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4388 copy the initial value out of the dynamic object and into the
4389 runtime process image. We need to remember the offset into the
4390 .rela.bss section we are going to use. */
4391 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4395 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4396 BFD_ASSERT (srel != NULL);
4397 srel->size += sizeof (Elf32_External_Rela);
4401 return _bfd_elf_adjust_dynamic_copy (h, s);
4404 /* Set the sizes of the dynamic sections. */
4407 _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
4408 struct bfd_link_info * info)
4414 bfd_boolean reltext;
4416 dynobj = elf_hash_table (info)->dynobj;
4417 BFD_ASSERT (dynobj != NULL);
4419 if (elf_hash_table (info)->dynamic_sections_created)
4421 /* Set the contents of the .interp section to the interpreter. */
4422 if (info->executable)
4424 s = bfd_get_section_by_name (dynobj, ".interp");
4425 BFD_ASSERT (s != NULL);
4426 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4427 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4432 /* We may have created entries in the .rela.got section.
4433 However, if we are not creating the dynamic sections, we will
4434 not actually use these entries. Reset the size of .rela.got,
4435 which will cause it to get stripped from the output file
4437 s = bfd_get_section_by_name (dynobj, ".rela.got");
4442 /* The check_relocs and adjust_dynamic_symbol entry points have
4443 determined the sizes of the various dynamic sections. Allocate
4448 for (s = dynobj->sections; s != NULL; s = s->next)
4452 if ((s->flags & SEC_LINKER_CREATED) == 0)
4455 /* It's OK to base decisions on the section name, because none
4456 of the dynobj section names depend upon the input files. */
4457 name = bfd_get_section_name (dynobj, s);
4459 if (streq (name, ".plt"))
4461 /* Remember whether there is a PLT. */
4464 else if (CONST_STRNEQ (name, ".rela"))
4470 /* Remember whether there are any reloc sections other
4472 if (! streq (name, ".rela.plt"))
4474 const char * outname;
4478 /* If this relocation section applies to a read only
4479 section, then we probably need a DT_TEXTREL
4480 entry. The entries in the .rela.plt section
4481 really apply to the .got section, which we
4482 created ourselves and so know is not readonly. */
4483 outname = bfd_get_section_name (output_bfd,
4485 target = bfd_get_section_by_name (output_bfd, outname + 5);
4487 && (target->flags & SEC_READONLY) != 0
4488 && (target->flags & SEC_ALLOC) != 0)
4492 /* We use the reloc_count field as a counter if we need
4493 to copy relocs into the output file. */
4497 else if (! CONST_STRNEQ (name, ".got")
4498 && ! streq (name, ".dynbss"))
4499 /* It's not one of our sections, so don't allocate space. */
4504 /* If we don't need this section, strip it from the
4505 output file. This is mostly to handle .rela.bss and
4506 .rela.plt. We must create both sections in
4507 create_dynamic_sections, because they must be created
4508 before the linker maps input sections to output
4509 sections. The linker does that before
4510 adjust_dynamic_symbol is called, and it is that
4511 function which decides whether anything needs to go
4512 into these sections. */
4513 s->flags |= SEC_EXCLUDE;
4517 if ((s->flags & SEC_HAS_CONTENTS) == 0)
4520 /* Allocate memory for the section contents. We use bfd_zalloc
4521 here in case unused entries are not reclaimed before the
4522 section's contents are written out. This should not happen,
4523 but this way if it does, we get a R_MN10300_NONE reloc
4524 instead of garbage. */
4525 s->contents = bfd_zalloc (dynobj, s->size);
4526 if (s->contents == NULL)
4530 if (elf_hash_table (info)->dynamic_sections_created)
4532 /* Add some entries to the .dynamic section. We fill in the
4533 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4534 but we must add the entries now so that we get the correct
4535 size for the .dynamic section. The DT_DEBUG entry is filled
4536 in by the dynamic linker and used by the debugger. */
4539 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4545 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4546 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4547 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4548 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4554 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4555 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4556 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4557 sizeof (Elf32_External_Rela)))
4563 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4571 /* Finish up dynamic symbol handling. We set the contents of various
4572 dynamic sections here. */
4575 _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
4576 struct bfd_link_info * info,
4577 struct elf_link_hash_entry * h,
4578 Elf_Internal_Sym * sym)
4582 dynobj = elf_hash_table (info)->dynobj;
4584 if (h->plt.offset != (bfd_vma) -1)
4591 Elf_Internal_Rela rel;
4593 /* This symbol has an entry in the procedure linkage table. Set
4596 BFD_ASSERT (h->dynindx != -1);
4598 splt = bfd_get_section_by_name (dynobj, ".plt");
4599 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4600 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4601 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4603 /* Get the index in the procedure linkage table which
4604 corresponds to this symbol. This is the index of this symbol
4605 in all the symbols for which we are making plt entries. The
4606 first entry in the procedure linkage table is reserved. */
4607 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4608 / elf_mn10300_sizeof_plt (info));
4610 /* Get the offset into the .got table of the entry that
4611 corresponds to this function. Each .got entry is 4 bytes.
4612 The first three are reserved. */
4613 got_offset = (plt_index + 3) * 4;
4615 /* Fill in the entry in the procedure linkage table. */
4618 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4619 elf_mn10300_sizeof_plt (info));
4620 bfd_put_32 (output_bfd,
4621 (sgot->output_section->vma
4622 + sgot->output_offset
4624 (splt->contents + h->plt.offset
4625 + elf_mn10300_plt_symbol_offset (info)));
4627 bfd_put_32 (output_bfd,
4628 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4629 (splt->contents + h->plt.offset
4630 + elf_mn10300_plt_plt0_offset (info)));
4634 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4635 elf_mn10300_sizeof_plt (info));
4637 bfd_put_32 (output_bfd, got_offset,
4638 (splt->contents + h->plt.offset
4639 + elf_mn10300_plt_symbol_offset (info)));
4642 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4643 (splt->contents + h->plt.offset
4644 + elf_mn10300_plt_reloc_offset (info)));
4646 /* Fill in the entry in the global offset table. */
4647 bfd_put_32 (output_bfd,
4648 (splt->output_section->vma
4649 + splt->output_offset
4651 + elf_mn10300_plt_temp_offset (info)),
4652 sgot->contents + got_offset);
4654 /* Fill in the entry in the .rela.plt section. */
4655 rel.r_offset = (sgot->output_section->vma
4656 + sgot->output_offset
4658 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4660 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4661 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4664 if (!h->def_regular)
4665 /* Mark the symbol as undefined, rather than as defined in
4666 the .plt section. Leave the value alone. */
4667 sym->st_shndx = SHN_UNDEF;
4670 if (h->got.offset != (bfd_vma) -1)
4674 Elf_Internal_Rela rel;
4676 /* This symbol has an entry in the global offset table. Set it up. */
4677 sgot = bfd_get_section_by_name (dynobj, ".got");
4678 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4679 BFD_ASSERT (sgot != NULL && srel != NULL);
4681 rel.r_offset = (sgot->output_section->vma
4682 + sgot->output_offset
4683 + (h->got.offset & ~1));
4685 /* If this is a -Bsymbolic link, and the symbol is defined
4686 locally, we just want to emit a RELATIVE reloc. Likewise if
4687 the symbol was forced to be local because of a version file.
4688 The entry in the global offset table will already have been
4689 initialized in the relocate_section function. */
4691 && (info->symbolic || h->dynindx == -1)
4694 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4695 rel.r_addend = (h->root.u.def.value
4696 + h->root.u.def.section->output_section->vma
4697 + h->root.u.def.section->output_offset);
4701 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4702 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4706 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4707 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4708 + srel->reloc_count));
4709 ++ srel->reloc_count;
4715 Elf_Internal_Rela rel;
4717 /* This symbol needs a copy reloc. Set it up. */
4718 BFD_ASSERT (h->dynindx != -1
4719 && (h->root.type == bfd_link_hash_defined
4720 || h->root.type == bfd_link_hash_defweak));
4722 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4724 BFD_ASSERT (s != NULL);
4726 rel.r_offset = (h->root.u.def.value
4727 + h->root.u.def.section->output_section->vma
4728 + h->root.u.def.section->output_offset);
4729 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4731 bfd_elf32_swap_reloca_out (output_bfd, & rel,
4732 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4737 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4738 if (streq (h->root.root.string, "_DYNAMIC")
4739 || h == elf_hash_table (info)->hgot)
4740 sym->st_shndx = SHN_ABS;
4745 /* Finish up the dynamic sections. */
4748 _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
4749 struct bfd_link_info * info)
4755 dynobj = elf_hash_table (info)->dynobj;
4757 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4758 BFD_ASSERT (sgot != NULL);
4759 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4761 if (elf_hash_table (info)->dynamic_sections_created)
4764 Elf32_External_Dyn * dyncon;
4765 Elf32_External_Dyn * dynconend;
4767 BFD_ASSERT (sdyn != NULL);
4769 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4770 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4772 for (; dyncon < dynconend; dyncon++)
4774 Elf_Internal_Dyn dyn;
4778 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4792 s = bfd_get_section_by_name (output_bfd, name);
4793 BFD_ASSERT (s != NULL);
4794 dyn.d_un.d_ptr = s->vma;
4795 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4799 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4800 BFD_ASSERT (s != NULL);
4801 dyn.d_un.d_val = s->size;
4802 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4806 /* My reading of the SVR4 ABI indicates that the
4807 procedure linkage table relocs (DT_JMPREL) should be
4808 included in the overall relocs (DT_RELA). This is
4809 what Solaris does. However, UnixWare can not handle
4810 that case. Therefore, we override the DT_RELASZ entry
4811 here to make it not include the JMPREL relocs. Since
4812 the linker script arranges for .rela.plt to follow all
4813 other relocation sections, we don't have to worry
4814 about changing the DT_RELA entry. */
4815 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4817 dyn.d_un.d_val -= s->size;
4818 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4823 /* Fill in the first entry in the procedure linkage table. */
4824 splt = bfd_get_section_by_name (dynobj, ".plt");
4825 if (splt && splt->size > 0)
4829 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4830 elf_mn10300_sizeof_plt (info));
4834 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4835 bfd_put_32 (output_bfd,
4836 sgot->output_section->vma + sgot->output_offset + 4,
4837 splt->contents + elf_mn10300_plt0_gotid_offset (info));
4838 bfd_put_32 (output_bfd,
4839 sgot->output_section->vma + sgot->output_offset + 8,
4840 splt->contents + elf_mn10300_plt0_linker_offset (info));
4843 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4844 really seem like the right value. */
4845 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4849 /* Fill in the first three entries in the global offset table. */
4853 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4855 bfd_put_32 (output_bfd,
4856 sdyn->output_section->vma + sdyn->output_offset,
4858 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4859 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4862 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4867 /* Classify relocation types, such that combreloc can sort them
4870 static enum elf_reloc_type_class
4871 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4873 switch ((int) ELF32_R_TYPE (rela->r_info))
4875 case R_MN10300_RELATIVE: return reloc_class_relative;
4876 case R_MN10300_JMP_SLOT: return reloc_class_plt;
4877 case R_MN10300_COPY: return reloc_class_copy;
4878 default: return reloc_class_normal;
4882 /* Allocate space for an MN10300 extension to the bfd elf data structure. */
4885 mn10300_elf_mkobject (bfd *abfd)
4887 /* We do not actually need any extra room in the bfd elf data structure.
4888 But we do need the object_id of the structure to be set to
4889 MN10300_ELF_DATA so that elflink.c:elf_link_add_object_symols() will call
4890 our mn10300_elf_check_relocs function which will then allocate space in
4891 the .got section for any GOT based relocs. */
4892 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
4896 #define bfd_elf32_mkobject mn10300_elf_mkobject
4899 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4900 #define TARGET_LITTLE_NAME "elf32-mn10300"
4901 #define ELF_ARCH bfd_arch_mn10300
4902 #define ELF_TARGET_ID MN10300_ELF_DATA
4903 #define ELF_MACHINE_CODE EM_MN10300
4904 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4905 #define ELF_MAXPAGESIZE 0x1000
4908 #define elf_info_to_howto mn10300_info_to_howto
4909 #define elf_info_to_howto_rel 0
4910 #define elf_backend_can_gc_sections 1
4911 #define elf_backend_rela_normal 1
4912 #define elf_backend_check_relocs mn10300_elf_check_relocs
4913 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4914 #define elf_backend_relocate_section mn10300_elf_relocate_section
4915 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4916 #define bfd_elf32_bfd_get_relocated_section_contents \
4917 mn10300_elf_get_relocated_section_contents
4918 #define bfd_elf32_bfd_link_hash_table_create \
4919 elf32_mn10300_link_hash_table_create
4920 #define bfd_elf32_bfd_link_hash_table_free \
4921 elf32_mn10300_link_hash_table_free
4923 #ifndef elf_symbol_leading_char
4924 #define elf_symbol_leading_char '_'
4927 /* So we can set bits in e_flags. */
4928 #define elf_backend_final_write_processing \
4929 _bfd_mn10300_elf_final_write_processing
4930 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4932 #define bfd_elf32_bfd_merge_private_bfd_data \
4933 _bfd_mn10300_elf_merge_private_bfd_data
4935 #define elf_backend_can_gc_sections 1
4936 #define elf_backend_create_dynamic_sections \
4937 _bfd_mn10300_elf_create_dynamic_sections
4938 #define elf_backend_adjust_dynamic_symbol \
4939 _bfd_mn10300_elf_adjust_dynamic_symbol
4940 #define elf_backend_size_dynamic_sections \
4941 _bfd_mn10300_elf_size_dynamic_sections
4942 #define elf_backend_omit_section_dynsym \
4943 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4944 #define elf_backend_finish_dynamic_symbol \
4945 _bfd_mn10300_elf_finish_dynamic_symbol
4946 #define elf_backend_finish_dynamic_sections \
4947 _bfd_mn10300_elf_finish_dynamic_sections
4949 #define elf_backend_reloc_type_class \
4950 _bfd_mn10300_elf_reloc_type_class
4952 #define elf_backend_want_got_plt 1
4953 #define elf_backend_plt_readonly 1
4954 #define elf_backend_want_plt_sym 0
4955 #define elf_backend_got_header_size 12
4957 #include "elf32-target.h"