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 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
;
990 bfd_vma
* local_got_offsets
;
995 dynobj
= elf_hash_table (info
)->dynobj
;
996 local_got_offsets
= elf_local_got_offsets (input_bfd
);
1007 case R_MN10300_PCREL8
:
1008 case R_MN10300_PCREL16
:
1009 case R_MN10300_PCREL32
:
1010 case R_MN10300_GOTOFF32
:
1011 case R_MN10300_GOTOFF24
:
1012 case R_MN10300_GOTOFF16
:
1014 && (input_section
->flags
& SEC_ALLOC
) != 0
1016 && ! SYMBOL_REFERENCES_LOCAL (info
, h
))
1017 return bfd_reloc_dangerous
;
1020 is_sym_diff_reloc
= FALSE
;
1021 if (sym_diff_section
!= NULL
)
1023 BFD_ASSERT (sym_diff_section
== input_section
);
1031 value
-= sym_diff_value
;
1032 /* If we are computing a 32-bit value for the location lists
1033 and the result is 0 then we add one to the value. A zero
1034 value can result because of linker relaxation deleteing
1035 prologue instructions and using a value of 1 (for the begin
1036 and end offsets in the location list entry) results in a
1037 nul entry which does not prevent the following entries from
1039 if (r_type
== R_MN10300_32
1041 && strcmp (input_section
->name
, ".debug_loc") == 0)
1043 sym_diff_section
= NULL
;
1044 is_sym_diff_reloc
= TRUE
;
1048 sym_diff_section
= NULL
;
1055 case R_MN10300_SYM_DIFF
:
1056 BFD_ASSERT (addend
== 0);
1057 /* Cache the input section and value.
1058 The offset is unreliable, since relaxation may
1059 have reduced the following reloc's offset. */
1060 sym_diff_section
= input_section
;
1061 sym_diff_value
= value
;
1062 return bfd_reloc_ok
;
1064 case R_MN10300_ALIGN
:
1065 case R_MN10300_NONE
:
1066 return bfd_reloc_ok
;
1070 /* Do not generate relocs when an R_MN10300_32 has been used
1071 with an R_MN10300_SYM_DIFF to compute a difference of two
1073 && is_sym_diff_reloc
== FALSE
1074 /* Also, do not generate a reloc when the symbol associated
1075 with the R_MN10300_32 reloc is absolute - there is no
1076 need for a run time computation in this case. */
1077 && sym_sec
!= bfd_abs_section_ptr
1078 /* If the section is not going to be allocated at load time
1079 then there is no need to generate relocs for it. */
1080 && (input_section
->flags
& SEC_ALLOC
) != 0)
1082 Elf_Internal_Rela outrel
;
1083 bfd_boolean skip
, relocate
;
1085 /* When generating a shared object, these relocations are
1086 copied into the output file to be resolved at run
1090 sreloc
= _bfd_elf_get_dynamic_reloc_section
1091 (input_bfd
, input_section
, /*rela?*/ TRUE
);
1098 outrel
.r_offset
= _bfd_elf_section_offset (input_bfd
, info
,
1099 input_section
, offset
);
1100 if (outrel
.r_offset
== (bfd_vma
) -1)
1103 outrel
.r_offset
+= (input_section
->output_section
->vma
1104 + input_section
->output_offset
);
1108 memset (&outrel
, 0, sizeof outrel
);
1113 /* h->dynindx may be -1 if this symbol was marked to
1116 || SYMBOL_REFERENCES_LOCAL (info
, h
))
1119 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
1120 outrel
.r_addend
= value
+ addend
;
1124 BFD_ASSERT (h
->dynindx
!= -1);
1126 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_32
);
1127 outrel
.r_addend
= value
+ addend
;
1131 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1132 (bfd_byte
*) (((Elf32_External_Rela
*) sreloc
->contents
)
1133 + sreloc
->reloc_count
));
1134 ++sreloc
->reloc_count
;
1136 /* If this reloc is against an external symbol, we do
1137 not want to fiddle with the addend. Otherwise, we
1138 need to include the symbol value so that it becomes
1139 an addend for the dynamic reloc. */
1141 return bfd_reloc_ok
;
1144 bfd_put_32 (input_bfd
, value
, hit_data
);
1145 return bfd_reloc_ok
;
1150 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1151 return bfd_reloc_overflow
;
1153 bfd_put_8 (input_bfd
, value
& 0xff, hit_data
);
1154 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1155 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1156 return bfd_reloc_ok
;
1161 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1162 return bfd_reloc_overflow
;
1164 bfd_put_16 (input_bfd
, value
, hit_data
);
1165 return bfd_reloc_ok
;
1170 if ((long) value
> 0x7f || (long) value
< -0x80)
1171 return bfd_reloc_overflow
;
1173 bfd_put_8 (input_bfd
, value
, hit_data
);
1174 return bfd_reloc_ok
;
1176 case R_MN10300_PCREL8
:
1177 value
-= (input_section
->output_section
->vma
1178 + input_section
->output_offset
);
1182 if ((long) value
> 0x7f || (long) value
< -0x80)
1183 return bfd_reloc_overflow
;
1185 bfd_put_8 (input_bfd
, value
, hit_data
);
1186 return bfd_reloc_ok
;
1188 case R_MN10300_PCREL16
:
1189 value
-= (input_section
->output_section
->vma
1190 + input_section
->output_offset
);
1194 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1195 return bfd_reloc_overflow
;
1197 bfd_put_16 (input_bfd
, value
, hit_data
);
1198 return bfd_reloc_ok
;
1200 case R_MN10300_PCREL32
:
1201 value
-= (input_section
->output_section
->vma
1202 + input_section
->output_offset
);
1206 bfd_put_32 (input_bfd
, value
, hit_data
);
1207 return bfd_reloc_ok
;
1209 case R_MN10300_GNU_VTINHERIT
:
1210 case R_MN10300_GNU_VTENTRY
:
1211 return bfd_reloc_ok
;
1213 case R_MN10300_GOTPC32
:
1214 /* Use global offset table as symbol value. */
1215 value
= bfd_get_section_by_name (dynobj
,
1216 ".got")->output_section
->vma
;
1217 value
-= (input_section
->output_section
->vma
1218 + input_section
->output_offset
);
1222 bfd_put_32 (input_bfd
, value
, hit_data
);
1223 return bfd_reloc_ok
;
1225 case R_MN10300_GOTPC16
:
1226 /* Use global offset table as symbol value. */
1227 value
= bfd_get_section_by_name (dynobj
,
1228 ".got")->output_section
->vma
;
1229 value
-= (input_section
->output_section
->vma
1230 + input_section
->output_offset
);
1234 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1235 return bfd_reloc_overflow
;
1237 bfd_put_16 (input_bfd
, value
, hit_data
);
1238 return bfd_reloc_ok
;
1240 case R_MN10300_GOTOFF32
:
1241 value
-= bfd_get_section_by_name (dynobj
,
1242 ".got")->output_section
->vma
;
1245 bfd_put_32 (input_bfd
, value
, hit_data
);
1246 return bfd_reloc_ok
;
1248 case R_MN10300_GOTOFF24
:
1249 value
-= bfd_get_section_by_name (dynobj
,
1250 ".got")->output_section
->vma
;
1253 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1254 return bfd_reloc_overflow
;
1256 bfd_put_8 (input_bfd
, value
, hit_data
);
1257 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1258 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1259 return bfd_reloc_ok
;
1261 case R_MN10300_GOTOFF16
:
1262 value
-= bfd_get_section_by_name (dynobj
,
1263 ".got")->output_section
->vma
;
1266 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1267 return bfd_reloc_overflow
;
1269 bfd_put_16 (input_bfd
, value
, hit_data
);
1270 return bfd_reloc_ok
;
1272 case R_MN10300_PLT32
:
1274 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
1275 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
1276 && h
->plt
.offset
!= (bfd_vma
) -1)
1278 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1280 value
= (splt
->output_section
->vma
1281 + splt
->output_offset
1282 + h
->plt
.offset
) - value
;
1285 value
-= (input_section
->output_section
->vma
1286 + input_section
->output_offset
);
1290 bfd_put_32 (input_bfd
, value
, hit_data
);
1291 return bfd_reloc_ok
;
1293 case R_MN10300_PLT16
:
1295 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
1296 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
1297 && h
->plt
.offset
!= (bfd_vma
) -1)
1299 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1301 value
= (splt
->output_section
->vma
1302 + splt
->output_offset
1303 + h
->plt
.offset
) - value
;
1306 value
-= (input_section
->output_section
->vma
1307 + input_section
->output_offset
);
1311 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1312 return bfd_reloc_overflow
;
1314 bfd_put_16 (input_bfd
, value
, hit_data
);
1315 return bfd_reloc_ok
;
1317 case R_MN10300_GOT32
:
1318 case R_MN10300_GOT24
:
1319 case R_MN10300_GOT16
:
1321 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1327 off
= h
->got
.offset
;
1328 BFD_ASSERT (off
!= (bfd_vma
) -1);
1330 if (! elf_hash_table (info
)->dynamic_sections_created
1331 || SYMBOL_REFERENCES_LOCAL (info
, h
))
1332 /* This is actually a static link, or it is a
1333 -Bsymbolic link and the symbol is defined
1334 locally, or the symbol was forced to be local
1335 because of a version file. We must initialize
1336 this entry in the global offset table.
1338 When doing a dynamic link, we create a .rela.got
1339 relocation entry to initialize the value. This
1340 is done in the finish_dynamic_symbol routine. */
1341 bfd_put_32 (output_bfd
, value
,
1342 sgot
->contents
+ off
);
1344 value
= sgot
->output_offset
+ off
;
1350 off
= elf_local_got_offsets (input_bfd
)[symndx
];
1352 bfd_put_32 (output_bfd
, value
, sgot
->contents
+ off
);
1357 Elf_Internal_Rela outrel
;
1359 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
1360 BFD_ASSERT (srelgot
!= NULL
);
1362 outrel
.r_offset
= (sgot
->output_section
->vma
1363 + sgot
->output_offset
1365 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
1366 outrel
.r_addend
= value
;
1367 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1368 (bfd_byte
*) (((Elf32_External_Rela
*)
1370 + srelgot
->reloc_count
));
1371 ++ srelgot
->reloc_count
;
1374 value
= sgot
->output_offset
+ off
;
1380 if (r_type
== R_MN10300_GOT32
)
1382 bfd_put_32 (input_bfd
, value
, hit_data
);
1383 return bfd_reloc_ok
;
1385 else if (r_type
== R_MN10300_GOT24
)
1387 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1388 return bfd_reloc_overflow
;
1390 bfd_put_8 (input_bfd
, value
& 0xff, hit_data
);
1391 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1392 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1393 return bfd_reloc_ok
;
1395 else if (r_type
== R_MN10300_GOT16
)
1397 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1398 return bfd_reloc_overflow
;
1400 bfd_put_16 (input_bfd
, value
, hit_data
);
1401 return bfd_reloc_ok
;
1406 return bfd_reloc_notsupported
;
1410 /* Relocate an MN10300 ELF section. */
1413 mn10300_elf_relocate_section (bfd
*output_bfd
,
1414 struct bfd_link_info
*info
,
1416 asection
*input_section
,
1418 Elf_Internal_Rela
*relocs
,
1419 Elf_Internal_Sym
*local_syms
,
1420 asection
**local_sections
)
1422 Elf_Internal_Shdr
*symtab_hdr
;
1423 struct elf_link_hash_entry
**sym_hashes
;
1424 Elf_Internal_Rela
*rel
, *relend
;
1426 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1427 sym_hashes
= elf_sym_hashes (input_bfd
);
1430 relend
= relocs
+ input_section
->reloc_count
;
1431 for (; rel
< relend
; rel
++)
1434 reloc_howto_type
*howto
;
1435 unsigned long r_symndx
;
1436 Elf_Internal_Sym
*sym
;
1438 struct elf32_mn10300_link_hash_entry
*h
;
1440 bfd_reloc_status_type r
;
1442 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1443 r_type
= ELF32_R_TYPE (rel
->r_info
);
1444 howto
= elf_mn10300_howto_table
+ r_type
;
1446 /* Just skip the vtable gc relocs. */
1447 if (r_type
== R_MN10300_GNU_VTINHERIT
1448 || r_type
== R_MN10300_GNU_VTENTRY
)
1454 if (r_symndx
< symtab_hdr
->sh_info
)
1456 sym
= local_syms
+ r_symndx
;
1457 sec
= local_sections
[r_symndx
];
1458 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1462 bfd_boolean unresolved_reloc
;
1464 struct elf_link_hash_entry
*hh
;
1466 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1467 r_symndx
, symtab_hdr
, sym_hashes
,
1468 hh
, sec
, relocation
,
1469 unresolved_reloc
, warned
);
1471 h
= (struct elf32_mn10300_link_hash_entry
*) hh
;
1473 if ((h
->root
.root
.type
== bfd_link_hash_defined
1474 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1475 && ( r_type
== R_MN10300_GOTPC32
1476 || r_type
== R_MN10300_GOTPC16
1477 || (( r_type
== R_MN10300_PLT32
1478 || r_type
== R_MN10300_PLT16
)
1479 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_INTERNAL
1480 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_HIDDEN
1481 && h
->root
.plt
.offset
!= (bfd_vma
) -1)
1482 || (( r_type
== R_MN10300_GOT32
1483 || r_type
== R_MN10300_GOT24
1484 || r_type
== R_MN10300_GOT16
)
1485 && elf_hash_table (info
)->dynamic_sections_created
1486 && !SYMBOL_REFERENCES_LOCAL (info
, hh
))
1487 || (r_type
== R_MN10300_32
1488 /* _32 relocs in executables force _COPY relocs,
1489 such that the address of the symbol ends up
1491 && !info
->executable
1492 && !SYMBOL_REFERENCES_LOCAL (info
, hh
)
1493 && ((input_section
->flags
& SEC_ALLOC
) != 0
1494 /* DWARF will emit R_MN10300_32 relocations
1495 in its sections against symbols defined
1496 externally in shared libraries. We can't
1497 do anything with them here. */
1498 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
1499 && h
->root
.def_dynamic
)))))
1500 /* In these cases, we don't need the relocation
1501 value. We check specially because in some
1502 obscure cases sec->output_section will be NULL. */
1505 else if (!info
->relocatable
&& unresolved_reloc
)
1506 (*_bfd_error_handler
)
1507 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1510 (long) rel
->r_offset
,
1512 h
->root
.root
.root
.string
);
1515 if (sec
!= NULL
&& elf_discarded_section (sec
))
1517 /* For relocs against symbols from removed linkonce sections,
1518 or sections discarded by a linker script, we just want the
1519 section contents zeroed. Avoid any special processing. */
1520 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
1526 if (info
->relocatable
)
1529 r
= mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
1531 contents
, rel
->r_offset
,
1532 relocation
, rel
->r_addend
,
1533 (struct elf_link_hash_entry
*) h
,
1535 info
, sec
, h
== NULL
);
1537 if (r
!= bfd_reloc_ok
)
1540 const char *msg
= NULL
;
1543 name
= h
->root
.root
.root
.string
;
1546 name
= (bfd_elf_string_from_elf_section
1547 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1548 if (name
== NULL
|| *name
== '\0')
1549 name
= bfd_section_name (input_bfd
, sec
);
1554 case bfd_reloc_overflow
:
1555 if (! ((*info
->callbacks
->reloc_overflow
)
1556 (info
, (h
? &h
->root
.root
: NULL
), name
,
1557 howto
->name
, (bfd_vma
) 0, input_bfd
,
1558 input_section
, rel
->r_offset
)))
1562 case bfd_reloc_undefined
:
1563 if (! ((*info
->callbacks
->undefined_symbol
)
1564 (info
, name
, input_bfd
, input_section
,
1565 rel
->r_offset
, TRUE
)))
1569 case bfd_reloc_outofrange
:
1570 msg
= _("internal error: out of range error");
1573 case bfd_reloc_notsupported
:
1574 msg
= _("internal error: unsupported relocation error");
1577 case bfd_reloc_dangerous
:
1578 if (r_type
== R_MN10300_PCREL32
)
1579 msg
= _("error: inappropriate relocation type for shared"
1580 " library (did you forget -fpic?)");
1582 msg
= _("internal error: suspicious relocation type used"
1583 " in shared library");
1587 msg
= _("internal error: unknown error");
1591 if (!((*info
->callbacks
->warning
)
1592 (info
, msg
, name
, input_bfd
, input_section
,
1603 /* Finish initializing one hash table entry. */
1606 elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry
*gen_entry
,
1609 struct elf32_mn10300_link_hash_entry
*entry
;
1610 struct bfd_link_info
*link_info
= (struct bfd_link_info
*) in_args
;
1611 unsigned int byte_count
= 0;
1613 entry
= (struct elf32_mn10300_link_hash_entry
*) gen_entry
;
1615 if (entry
->root
.root
.type
== bfd_link_hash_warning
)
1616 entry
= (struct elf32_mn10300_link_hash_entry
*) entry
->root
.root
.u
.i
.link
;
1618 /* If we already know we want to convert "call" to "calls" for calls
1619 to this symbol, then return now. */
1620 if (entry
->flags
== MN10300_CONVERT_CALL_TO_CALLS
)
1623 /* If there are no named calls to this symbol, or there's nothing we
1624 can move from the function itself into the "call" instruction,
1625 then note that all "call" instructions should be converted into
1626 "calls" instructions and return. If a symbol is available for
1627 dynamic symbol resolution (overridable or overriding), avoid
1628 custom calling conventions. */
1629 if (entry
->direct_calls
== 0
1630 || (entry
->stack_size
== 0 && entry
->movm_args
== 0)
1631 || (elf_hash_table (link_info
)->dynamic_sections_created
1632 && ELF_ST_VISIBILITY (entry
->root
.other
) != STV_INTERNAL
1633 && ELF_ST_VISIBILITY (entry
->root
.other
) != STV_HIDDEN
))
1635 /* Make a note that we should convert "call" instructions to "calls"
1636 instructions for calls to this symbol. */
1637 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
1641 /* We may be able to move some instructions from the function itself into
1642 the "call" instruction. Count how many bytes we might be able to
1643 eliminate in the function itself. */
1645 /* A movm instruction is two bytes. */
1646 if (entry
->movm_args
)
1649 /* Count the insn to allocate stack space too. */
1650 if (entry
->stack_size
> 0)
1652 if (entry
->stack_size
<= 128)
1658 /* If using "call" will result in larger code, then turn all
1659 the associated "call" instructions into "calls" instructions. */
1660 if (byte_count
< entry
->direct_calls
)
1661 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
1663 /* This routine never fails. */
1667 /* Used to count hash table entries. */
1670 elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry
*gen_entry ATTRIBUTE_UNUSED
,
1673 int *count
= (int *) in_args
;
1679 /* Used to enumerate hash table entries into a linear array. */
1682 elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry
*gen_entry
,
1685 struct bfd_hash_entry
***ptr
= (struct bfd_hash_entry
***) in_args
;
1692 /* Used to sort the array created by the above. */
1695 sort_by_value (const void *va
, const void *vb
)
1697 struct elf32_mn10300_link_hash_entry
*a
1698 = *(struct elf32_mn10300_link_hash_entry
**) va
;
1699 struct elf32_mn10300_link_hash_entry
*b
1700 = *(struct elf32_mn10300_link_hash_entry
**) vb
;
1702 return a
->value
- b
->value
;
1705 /* Compute the stack size and movm arguments for the function
1706 referred to by HASH at address ADDR in section with
1707 contents CONTENTS, store the information in the hash table. */
1710 compute_function_info (bfd
*abfd
,
1711 struct elf32_mn10300_link_hash_entry
*hash
,
1713 unsigned char *contents
)
1715 unsigned char byte1
, byte2
;
1716 /* We only care about a very small subset of the possible prologue
1717 sequences here. Basically we look for:
1719 movm [d2,d3,a2,a3],sp (optional)
1720 add <size>,sp (optional, and only for sizes which fit in an unsigned
1723 If we find anything else, we quit. */
1725 /* Look for movm [regs],sp. */
1726 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
1727 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
1731 hash
->movm_args
= byte2
;
1733 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
1734 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
1737 /* Now figure out how much stack space will be allocated by the movm
1738 instruction. We need this kept separate from the function's normal
1740 if (hash
->movm_args
)
1743 if (hash
->movm_args
& 0x80)
1744 hash
->movm_stack_size
+= 4;
1747 if (hash
->movm_args
& 0x40)
1748 hash
->movm_stack_size
+= 4;
1751 if (hash
->movm_args
& 0x20)
1752 hash
->movm_stack_size
+= 4;
1755 if (hash
->movm_args
& 0x10)
1756 hash
->movm_stack_size
+= 4;
1758 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
1759 if (hash
->movm_args
& 0x08)
1760 hash
->movm_stack_size
+= 8 * 4;
1762 if (bfd_get_mach (abfd
) == bfd_mach_am33
1763 || bfd_get_mach (abfd
) == bfd_mach_am33_2
)
1765 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
1766 if (hash
->movm_args
& 0x1)
1767 hash
->movm_stack_size
+= 6 * 4;
1769 /* exreg1 space. e4, e5, e6, e7 */
1770 if (hash
->movm_args
& 0x2)
1771 hash
->movm_stack_size
+= 4 * 4;
1773 /* exreg0 space. e2, e3 */
1774 if (hash
->movm_args
& 0x4)
1775 hash
->movm_stack_size
+= 2 * 4;
1779 /* Now look for the two stack adjustment variants. */
1780 if (byte1
== 0xf8 && byte2
== 0xfe)
1782 int temp
= bfd_get_8 (abfd
, contents
+ addr
+ 2);
1783 temp
= ((temp
& 0xff) ^ (~0x7f)) + 0x80;
1785 hash
->stack_size
= -temp
;
1787 else if (byte1
== 0xfa && byte2
== 0xfe)
1789 int temp
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1790 temp
= ((temp
& 0xffff) ^ (~0x7fff)) + 0x8000;
1794 hash
->stack_size
= temp
;
1797 /* If the total stack to be allocated by the call instruction is more
1798 than 255 bytes, then we can't remove the stack adjustment by using
1799 "call" (we might still be able to remove the "movm" instruction. */
1800 if (hash
->stack_size
+ hash
->movm_stack_size
> 255)
1801 hash
->stack_size
= 0;
1804 /* Delete some bytes from a section while relaxing. */
1807 mn10300_elf_relax_delete_bytes (bfd
*abfd
,
1812 Elf_Internal_Shdr
*symtab_hdr
;
1813 unsigned int sec_shndx
;
1815 Elf_Internal_Rela
*irel
, *irelend
;
1816 Elf_Internal_Rela
*irelalign
;
1818 Elf_Internal_Sym
*isym
, *isymend
;
1819 struct elf_link_hash_entry
**sym_hashes
;
1820 struct elf_link_hash_entry
**end_hashes
;
1821 unsigned int symcount
;
1823 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1825 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1830 irel
= elf_section_data (sec
)->relocs
;
1831 irelend
= irel
+ sec
->reloc_count
;
1833 if (sec
->reloc_count
> 0)
1835 /* If there is an align reloc at the end of the section ignore it.
1836 GAS creates these relocs for reasons of its own, and they just
1837 serve to keep the section artifically inflated. */
1838 if (ELF32_R_TYPE ((irelend
- 1)->r_info
) == (int) R_MN10300_ALIGN
)
1841 /* The deletion must stop at the next ALIGN reloc for an aligment
1842 power larger than, or not a multiple of, the number of bytes we
1844 for (; irel
< irelend
; irel
++)
1846 int alignment
= 1 << irel
->r_addend
;
1848 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_ALIGN
1849 && irel
->r_offset
> addr
1850 && irel
->r_offset
< toaddr
1851 && (count
< alignment
1852 || alignment
% count
!= 0))
1855 toaddr
= irel
->r_offset
;
1861 /* Actually delete the bytes. */
1862 memmove (contents
+ addr
, contents
+ addr
+ count
,
1863 (size_t) (toaddr
- addr
- count
));
1865 /* Adjust the section's size if we are shrinking it, or else
1866 pad the bytes between the end of the shrunken region and
1867 the start of the next region with NOP codes. */
1868 if (irelalign
== NULL
)
1871 /* Include symbols at the end of the section, but
1872 not at the end of a sub-region of the section. */
1879 #define NOP_OPCODE 0xcb
1881 for (i
= 0; i
< count
; i
++)
1882 bfd_put_8 (abfd
, (bfd_vma
) NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
1885 /* Adjust all the relocs. */
1886 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1888 /* Get the new reloc address. */
1889 if ((irel
->r_offset
> addr
1890 && irel
->r_offset
< toaddr
)
1891 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_ALIGN
1892 && irel
->r_offset
== toaddr
))
1893 irel
->r_offset
-= count
;
1896 /* Adjust the local symbols in the section, reducing their value
1897 by the number of bytes deleted. Note - symbols within the deleted
1898 region are moved to the address of the start of the region, which
1899 actually means that they will address the byte beyond the end of
1900 the region once the deletion has been completed. */
1901 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1902 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1903 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1905 if (isym
->st_shndx
== sec_shndx
1906 && isym
->st_value
> addr
1907 && isym
->st_value
< toaddr
)
1909 if (isym
->st_value
< addr
+ count
)
1910 isym
->st_value
= addr
;
1912 isym
->st_value
-= count
;
1914 /* Adjust the function symbol's size as well. */
1915 else if (isym
->st_shndx
== sec_shndx
1916 && ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
1917 && isym
->st_value
+ isym
->st_size
> addr
1918 && isym
->st_value
+ isym
->st_size
< toaddr
)
1919 isym
->st_size
-= count
;
1922 /* Now adjust the global symbols defined in this section. */
1923 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1924 - symtab_hdr
->sh_info
);
1925 sym_hashes
= elf_sym_hashes (abfd
);
1926 end_hashes
= sym_hashes
+ symcount
;
1927 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1929 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1931 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1932 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1933 && sym_hash
->root
.u
.def
.section
== sec
1934 && sym_hash
->root
.u
.def
.value
> addr
1935 && sym_hash
->root
.u
.def
.value
< toaddr
)
1937 if (sym_hash
->root
.u
.def
.value
< addr
+ count
)
1938 sym_hash
->root
.u
.def
.value
= addr
;
1940 sym_hash
->root
.u
.def
.value
-= count
;
1942 /* Adjust the function symbol's size as well. */
1943 else if (sym_hash
->root
.type
== bfd_link_hash_defined
1944 && sym_hash
->root
.u
.def
.section
== sec
1945 && sym_hash
->type
== STT_FUNC
1946 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
1947 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
< toaddr
)
1948 sym_hash
->size
-= count
;
1951 /* See if we can move the ALIGN reloc forward.
1952 We have adjusted r_offset for it already. */
1953 if (irelalign
!= NULL
)
1955 bfd_vma alignto
, alignaddr
;
1957 if ((int) irelalign
->r_addend
> 0)
1959 /* This is the old address. */
1960 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1961 /* This is where the align points to now. */
1962 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1963 1 << irelalign
->r_addend
);
1964 if (alignaddr
< alignto
)
1965 /* Tail recursion. */
1966 return mn10300_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1967 (int) (alignto
- alignaddr
));
1974 /* Return TRUE if a symbol exists at the given address, else return
1978 mn10300_elf_symbol_address_p (bfd
*abfd
,
1980 Elf_Internal_Sym
*isym
,
1983 Elf_Internal_Shdr
*symtab_hdr
;
1984 unsigned int sec_shndx
;
1985 Elf_Internal_Sym
*isymend
;
1986 struct elf_link_hash_entry
**sym_hashes
;
1987 struct elf_link_hash_entry
**end_hashes
;
1988 unsigned int symcount
;
1990 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1992 /* Examine all the symbols. */
1993 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1994 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1995 if (isym
->st_shndx
== sec_shndx
1996 && isym
->st_value
== addr
)
1999 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2000 - symtab_hdr
->sh_info
);
2001 sym_hashes
= elf_sym_hashes (abfd
);
2002 end_hashes
= sym_hashes
+ symcount
;
2003 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2005 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
2007 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2008 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
2009 && sym_hash
->root
.u
.def
.section
== sec
2010 && sym_hash
->root
.u
.def
.value
== addr
)
2017 /* This function handles relaxing for the mn10300.
2019 There are quite a few relaxing opportunities available on the mn10300:
2021 * calls:32 -> calls:16 2 bytes
2022 * call:32 -> call:16 2 bytes
2024 * call:32 -> calls:32 1 byte
2025 * call:16 -> calls:16 1 byte
2026 * These are done anytime using "calls" would result
2027 in smaller code, or when necessary to preserve the
2028 meaning of the program.
2032 * In some circumstances we can move instructions
2033 from a function prologue into a "call" instruction.
2034 This is only done if the resulting code is no larger
2035 than the original code.
2037 * jmp:32 -> jmp:16 2 bytes
2038 * jmp:16 -> bra:8 1 byte
2040 * If the previous instruction is a conditional branch
2041 around the jump/bra, we may be able to reverse its condition
2042 and change its target to the jump's target. The jump/bra
2043 can then be deleted. 2 bytes
2045 * mov abs32 -> mov abs16 1 or 2 bytes
2047 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
2048 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
2050 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
2051 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
2053 We don't handle imm16->imm8 or d16->d8 as they're very rare
2054 and somewhat more difficult to support. */
2057 mn10300_elf_relax_section (bfd
*abfd
,
2059 struct bfd_link_info
*link_info
,
2062 Elf_Internal_Shdr
*symtab_hdr
;
2063 Elf_Internal_Rela
*internal_relocs
= NULL
;
2064 Elf_Internal_Rela
*irel
, *irelend
;
2065 bfd_byte
*contents
= NULL
;
2066 Elf_Internal_Sym
*isymbuf
= NULL
;
2067 struct elf32_mn10300_link_hash_table
*hash_table
;
2068 asection
*section
= sec
;
2069 bfd_vma align_gap_adjustment
;
2071 if (link_info
->relocatable
)
2072 (*link_info
->callbacks
->einfo
)
2073 (_("%P%F: --relax and -r may not be used together\n"));
2075 /* Assume nothing changes. */
2078 /* We need a pointer to the mn10300 specific hash table. */
2079 hash_table
= elf32_mn10300_hash_table (link_info
);
2080 if (hash_table
== NULL
)
2083 /* Initialize fields in each hash table entry the first time through. */
2084 if ((hash_table
->flags
& MN10300_HASH_ENTRIES_INITIALIZED
) == 0)
2088 /* Iterate over all the input bfds. */
2089 for (input_bfd
= link_info
->input_bfds
;
2091 input_bfd
= input_bfd
->link_next
)
2093 /* We're going to need all the symbols for each bfd. */
2094 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2095 if (symtab_hdr
->sh_info
!= 0)
2097 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2098 if (isymbuf
== NULL
)
2099 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2100 symtab_hdr
->sh_info
, 0,
2102 if (isymbuf
== NULL
)
2106 /* Iterate over each section in this bfd. */
2107 for (section
= input_bfd
->sections
;
2109 section
= section
->next
)
2111 struct elf32_mn10300_link_hash_entry
*hash
;
2112 Elf_Internal_Sym
*sym
;
2113 asection
*sym_sec
= NULL
;
2114 const char *sym_name
;
2117 /* If there's nothing to do in this section, skip it. */
2118 if (! ((section
->flags
& SEC_RELOC
) != 0
2119 && section
->reloc_count
!= 0))
2121 if ((section
->flags
& SEC_ALLOC
) == 0)
2124 /* Get cached copy of section contents if it exists. */
2125 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
2126 contents
= elf_section_data (section
)->this_hdr
.contents
;
2127 else if (section
->size
!= 0)
2129 /* Go get them off disk. */
2130 if (!bfd_malloc_and_get_section (input_bfd
, section
,
2137 /* If there aren't any relocs, then there's nothing to do. */
2138 if ((section
->flags
& SEC_RELOC
) != 0
2139 && section
->reloc_count
!= 0)
2141 /* Get a copy of the native relocations. */
2142 internal_relocs
= _bfd_elf_link_read_relocs (input_bfd
, section
,
2144 link_info
->keep_memory
);
2145 if (internal_relocs
== NULL
)
2148 /* Now examine each relocation. */
2149 irel
= internal_relocs
;
2150 irelend
= irel
+ section
->reloc_count
;
2151 for (; irel
< irelend
; irel
++)
2154 unsigned long r_index
;
2157 r_type
= ELF32_R_TYPE (irel
->r_info
);
2158 r_index
= ELF32_R_SYM (irel
->r_info
);
2160 if (r_type
< 0 || r_type
>= (int) R_MN10300_MAX
)
2163 /* We need the name and hash table entry of the target
2169 if (r_index
< symtab_hdr
->sh_info
)
2171 /* A local symbol. */
2172 Elf_Internal_Sym
*isym
;
2173 struct elf_link_hash_table
*elftab
;
2176 isym
= isymbuf
+ r_index
;
2177 if (isym
->st_shndx
== SHN_UNDEF
)
2178 sym_sec
= bfd_und_section_ptr
;
2179 else if (isym
->st_shndx
== SHN_ABS
)
2180 sym_sec
= bfd_abs_section_ptr
;
2181 else if (isym
->st_shndx
== SHN_COMMON
)
2182 sym_sec
= bfd_com_section_ptr
;
2185 = bfd_section_from_elf_index (input_bfd
,
2189 = bfd_elf_string_from_elf_section (input_bfd
,
2194 /* If it isn't a function, then we don't care
2196 if (ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
)
2199 /* Tack on an ID so we can uniquely identify this
2200 local symbol in the global hash table. */
2201 amt
= strlen (sym_name
) + 10;
2202 new_name
= bfd_malloc (amt
);
2203 if (new_name
== NULL
)
2206 sprintf (new_name
, "%s_%08x", sym_name
, sym_sec
->id
);
2207 sym_name
= new_name
;
2209 elftab
= &hash_table
->static_hash_table
->root
;
2210 hash
= ((struct elf32_mn10300_link_hash_entry
*)
2211 elf_link_hash_lookup (elftab
, sym_name
,
2212 TRUE
, TRUE
, FALSE
));
2217 r_index
-= symtab_hdr
->sh_info
;
2218 hash
= (struct elf32_mn10300_link_hash_entry
*)
2219 elf_sym_hashes (input_bfd
)[r_index
];
2222 sym_name
= hash
->root
.root
.root
.string
;
2223 if ((section
->flags
& SEC_CODE
) != 0)
2225 /* If this is not a "call" instruction, then we
2226 should convert "call" instructions to "calls"
2228 code
= bfd_get_8 (input_bfd
,
2229 contents
+ irel
->r_offset
- 1);
2230 if (code
!= 0xdd && code
!= 0xcd)
2231 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
2234 /* If this is a jump/call, then bump the
2235 direct_calls counter. Else force "call" to
2236 "calls" conversions. */
2237 if (r_type
== R_MN10300_PCREL32
2238 || r_type
== R_MN10300_PLT32
2239 || r_type
== R_MN10300_PLT16
2240 || r_type
== R_MN10300_PCREL16
)
2241 hash
->direct_calls
++;
2243 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
2247 /* Now look at the actual contents to get the stack size,
2248 and a list of what registers were saved in the prologue
2250 if ((section
->flags
& SEC_CODE
) != 0)
2252 Elf_Internal_Sym
*isym
, *isymend
;
2253 unsigned int sec_shndx
;
2254 struct elf_link_hash_entry
**hashes
;
2255 struct elf_link_hash_entry
**end_hashes
;
2256 unsigned int symcount
;
2258 sec_shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
2261 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2262 - symtab_hdr
->sh_info
);
2263 hashes
= elf_sym_hashes (input_bfd
);
2264 end_hashes
= hashes
+ symcount
;
2266 /* Look at each function defined in this section and
2267 update info for that function. */
2268 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
2269 for (isym
= isymbuf
; isym
< isymend
; isym
++)
2271 if (isym
->st_shndx
== sec_shndx
2272 && ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)
2274 struct elf_link_hash_table
*elftab
;
2276 struct elf_link_hash_entry
**lhashes
= hashes
;
2278 /* Skip a local symbol if it aliases a
2280 for (; lhashes
< end_hashes
; lhashes
++)
2282 hash
= (struct elf32_mn10300_link_hash_entry
*) *lhashes
;
2283 if ((hash
->root
.root
.type
== bfd_link_hash_defined
2284 || hash
->root
.root
.type
== bfd_link_hash_defweak
)
2285 && hash
->root
.root
.u
.def
.section
== section
2286 && hash
->root
.type
== STT_FUNC
2287 && hash
->root
.root
.u
.def
.value
== isym
->st_value
)
2290 if (lhashes
!= end_hashes
)
2293 if (isym
->st_shndx
== SHN_UNDEF
)
2294 sym_sec
= bfd_und_section_ptr
;
2295 else if (isym
->st_shndx
== SHN_ABS
)
2296 sym_sec
= bfd_abs_section_ptr
;
2297 else if (isym
->st_shndx
== SHN_COMMON
)
2298 sym_sec
= bfd_com_section_ptr
;
2301 = bfd_section_from_elf_index (input_bfd
,
2304 sym_name
= (bfd_elf_string_from_elf_section
2305 (input_bfd
, symtab_hdr
->sh_link
,
2308 /* Tack on an ID so we can uniquely identify this
2309 local symbol in the global hash table. */
2310 amt
= strlen (sym_name
) + 10;
2311 new_name
= bfd_malloc (amt
);
2312 if (new_name
== NULL
)
2315 sprintf (new_name
, "%s_%08x", sym_name
, sym_sec
->id
);
2316 sym_name
= new_name
;
2318 elftab
= &hash_table
->static_hash_table
->root
;
2319 hash
= ((struct elf32_mn10300_link_hash_entry
*)
2320 elf_link_hash_lookup (elftab
, sym_name
,
2321 TRUE
, TRUE
, FALSE
));
2323 compute_function_info (input_bfd
, hash
,
2324 isym
->st_value
, contents
);
2325 hash
->value
= isym
->st_value
;
2329 for (; hashes
< end_hashes
; hashes
++)
2331 hash
= (struct elf32_mn10300_link_hash_entry
*) *hashes
;
2332 if ((hash
->root
.root
.type
== bfd_link_hash_defined
2333 || hash
->root
.root
.type
== bfd_link_hash_defweak
)
2334 && hash
->root
.root
.u
.def
.section
== section
2335 && hash
->root
.type
== STT_FUNC
)
2336 compute_function_info (input_bfd
, hash
,
2337 (hash
)->root
.root
.u
.def
.value
,
2342 /* Cache or free any memory we allocated for the relocs. */
2343 if (internal_relocs
!= NULL
2344 && elf_section_data (section
)->relocs
!= internal_relocs
)
2345 free (internal_relocs
);
2346 internal_relocs
= NULL
;
2348 /* Cache or free any memory we allocated for the contents. */
2349 if (contents
!= NULL
2350 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
2352 if (! link_info
->keep_memory
)
2356 /* Cache the section contents for elf_link_input_bfd. */
2357 elf_section_data (section
)->this_hdr
.contents
= contents
;
2363 /* Cache or free any memory we allocated for the symbols. */
2365 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2367 if (! link_info
->keep_memory
)
2371 /* Cache the symbols for elf_link_input_bfd. */
2372 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2378 /* Now iterate on each symbol in the hash table and perform
2379 the final initialization steps on each. */
2380 elf32_mn10300_link_hash_traverse (hash_table
,
2381 elf32_mn10300_finish_hash_table_entry
,
2383 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
2384 elf32_mn10300_finish_hash_table_entry
,
2388 /* This section of code collects all our local symbols, sorts
2389 them by value, and looks for multiple symbols referring to
2390 the same address. For those symbols, the flags are merged.
2391 At this point, the only flag that can be set is
2392 MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2394 int static_count
= 0, i
;
2395 struct elf32_mn10300_link_hash_entry
**entries
;
2396 struct elf32_mn10300_link_hash_entry
**ptr
;
2398 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
2399 elf32_mn10300_count_hash_table_entries
,
2402 entries
= bfd_malloc (static_count
* sizeof (* ptr
));
2405 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
2406 elf32_mn10300_list_hash_table_entries
,
2409 qsort (entries
, static_count
, sizeof (entries
[0]), sort_by_value
);
2411 for (i
= 0; i
< static_count
- 1; i
++)
2412 if (entries
[i
]->value
&& entries
[i
]->value
== entries
[i
+1]->value
)
2414 int v
= entries
[i
]->flags
;
2417 for (j
= i
+ 1; j
< static_count
&& entries
[j
]->value
== entries
[i
]->value
; j
++)
2418 v
|= entries
[j
]->flags
;
2420 for (j
= i
; j
< static_count
&& entries
[j
]->value
== entries
[i
]->value
; j
++)
2421 entries
[j
]->flags
= v
;
2427 /* All entries in the hash table are fully initialized. */
2428 hash_table
->flags
|= MN10300_HASH_ENTRIES_INITIALIZED
;
2430 /* Now that everything has been initialized, go through each
2431 code section and delete any prologue insns which will be
2432 redundant because their operations will be performed by
2433 a "call" instruction. */
2434 for (input_bfd
= link_info
->input_bfds
;
2436 input_bfd
= input_bfd
->link_next
)
2438 /* We're going to need all the local symbols for each bfd. */
2439 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2440 if (symtab_hdr
->sh_info
!= 0)
2442 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2443 if (isymbuf
== NULL
)
2444 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2445 symtab_hdr
->sh_info
, 0,
2447 if (isymbuf
== NULL
)
2451 /* Walk over each section in this bfd. */
2452 for (section
= input_bfd
->sections
;
2454 section
= section
->next
)
2456 unsigned int sec_shndx
;
2457 Elf_Internal_Sym
*isym
, *isymend
;
2458 struct elf_link_hash_entry
**hashes
;
2459 struct elf_link_hash_entry
**end_hashes
;
2460 unsigned int symcount
;
2462 /* Skip non-code sections and empty sections. */
2463 if ((section
->flags
& SEC_CODE
) == 0 || section
->size
== 0)
2466 if (section
->reloc_count
!= 0)
2468 /* Get a copy of the native relocations. */
2469 internal_relocs
= _bfd_elf_link_read_relocs (input_bfd
, section
,
2471 link_info
->keep_memory
);
2472 if (internal_relocs
== NULL
)
2476 /* Get cached copy of section contents if it exists. */
2477 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
2478 contents
= elf_section_data (section
)->this_hdr
.contents
;
2481 /* Go get them off disk. */
2482 if (!bfd_malloc_and_get_section (input_bfd
, section
,
2487 sec_shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
2490 /* Now look for any function in this section which needs
2491 insns deleted from its prologue. */
2492 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
2493 for (isym
= isymbuf
; isym
< isymend
; isym
++)
2495 struct elf32_mn10300_link_hash_entry
*sym_hash
;
2496 asection
*sym_sec
= NULL
;
2497 const char *sym_name
;
2499 struct elf_link_hash_table
*elftab
;
2502 if (isym
->st_shndx
!= sec_shndx
)
2505 if (isym
->st_shndx
== SHN_UNDEF
)
2506 sym_sec
= bfd_und_section_ptr
;
2507 else if (isym
->st_shndx
== SHN_ABS
)
2508 sym_sec
= bfd_abs_section_ptr
;
2509 else if (isym
->st_shndx
== SHN_COMMON
)
2510 sym_sec
= bfd_com_section_ptr
;
2513 = bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
2516 = bfd_elf_string_from_elf_section (input_bfd
,
2517 symtab_hdr
->sh_link
,
2520 /* Tack on an ID so we can uniquely identify this
2521 local symbol in the global hash table. */
2522 amt
= strlen (sym_name
) + 10;
2523 new_name
= bfd_malloc (amt
);
2524 if (new_name
== NULL
)
2526 sprintf (new_name
, "%s_%08x", sym_name
, sym_sec
->id
);
2527 sym_name
= new_name
;
2529 elftab
= & hash_table
->static_hash_table
->root
;
2530 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
2531 elf_link_hash_lookup (elftab
, sym_name
,
2532 FALSE
, FALSE
, FALSE
);
2535 if (sym_hash
== NULL
)
2538 if (! (sym_hash
->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
2539 && ! (sym_hash
->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
2543 /* Note that we've changed things. */
2544 elf_section_data (section
)->relocs
= internal_relocs
;
2545 elf_section_data (section
)->this_hdr
.contents
= contents
;
2546 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2548 /* Count how many bytes we're going to delete. */
2549 if (sym_hash
->movm_args
)
2552 if (sym_hash
->stack_size
> 0)
2554 if (sym_hash
->stack_size
<= 128)
2560 /* Note that we've deleted prologue bytes for this
2562 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
2564 /* Actually delete the bytes. */
2565 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
2571 /* Something changed. Not strictly necessary, but
2572 may lead to more relaxing opportunities. */
2577 /* Look for any global functions in this section which
2578 need insns deleted from their prologues. */
2579 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2580 - symtab_hdr
->sh_info
);
2581 hashes
= elf_sym_hashes (input_bfd
);
2582 end_hashes
= hashes
+ symcount
;
2583 for (; hashes
< end_hashes
; hashes
++)
2585 struct elf32_mn10300_link_hash_entry
*sym_hash
;
2587 sym_hash
= (struct elf32_mn10300_link_hash_entry
*) *hashes
;
2588 if ((sym_hash
->root
.root
.type
== bfd_link_hash_defined
2589 || sym_hash
->root
.root
.type
== bfd_link_hash_defweak
)
2590 && sym_hash
->root
.root
.u
.def
.section
== section
2591 && ! (sym_hash
->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
2592 && ! (sym_hash
->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
2597 /* Note that we've changed things. */
2598 elf_section_data (section
)->relocs
= internal_relocs
;
2599 elf_section_data (section
)->this_hdr
.contents
= contents
;
2600 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2602 /* Count how many bytes we're going to delete. */
2603 if (sym_hash
->movm_args
)
2606 if (sym_hash
->stack_size
> 0)
2608 if (sym_hash
->stack_size
<= 128)
2614 /* Note that we've deleted prologue bytes for this
2616 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
2618 /* Actually delete the bytes. */
2619 symval
= sym_hash
->root
.root
.u
.def
.value
;
2620 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
2626 /* Something changed. Not strictly necessary, but
2627 may lead to more relaxing opportunities. */
2632 /* Cache or free any memory we allocated for the relocs. */
2633 if (internal_relocs
!= NULL
2634 && elf_section_data (section
)->relocs
!= internal_relocs
)
2635 free (internal_relocs
);
2636 internal_relocs
= NULL
;
2638 /* Cache or free any memory we allocated for the contents. */
2639 if (contents
!= NULL
2640 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
2642 if (! link_info
->keep_memory
)
2645 /* Cache the section contents for elf_link_input_bfd. */
2646 elf_section_data (section
)->this_hdr
.contents
= contents
;
2651 /* Cache or free any memory we allocated for the symbols. */
2653 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2655 if (! link_info
->keep_memory
)
2658 /* Cache the symbols for elf_link_input_bfd. */
2659 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2665 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2667 internal_relocs
= NULL
;
2669 /* For error_return. */
2672 /* We don't have to do anything for a relocatable link, if
2673 this section does not have relocs, or if this is not a
2675 if (link_info
->relocatable
2676 || (sec
->flags
& SEC_RELOC
) == 0
2677 || sec
->reloc_count
== 0
2678 || (sec
->flags
& SEC_CODE
) == 0)
2681 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2683 /* Get a copy of the native relocations. */
2684 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
2685 link_info
->keep_memory
);
2686 if (internal_relocs
== NULL
)
2689 /* Scan for worst case alignment gap changes. Note that this logic
2690 is not ideal; what we should do is run this scan for every
2691 opcode/address range and adjust accordingly, but that's
2692 expensive. Worst case is that for an alignment of N bytes, we
2693 move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
2694 all before it. Plus, this still doesn't cover cross-section
2695 jumps with section alignment. */
2696 irelend
= internal_relocs
+ sec
->reloc_count
;
2697 align_gap_adjustment
= 0;
2698 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2700 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_ALIGN
)
2702 bfd_vma adj
= 1 << irel
->r_addend
;
2703 bfd_vma aend
= irel
->r_offset
;
2705 aend
= BFD_ALIGN (aend
, 1 << irel
->r_addend
);
2706 adj
= 2 * adj
- adj
- 1;
2708 /* Record the biggest adjustmnet. Skip any alignment at the
2709 end of our section. */
2710 if (align_gap_adjustment
< adj
2711 && aend
< sec
->output_section
->vma
+ sec
->output_offset
+ sec
->size
)
2712 align_gap_adjustment
= adj
;
2716 /* Walk through them looking for relaxing opportunities. */
2717 irelend
= internal_relocs
+ sec
->reloc_count
;
2718 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2721 bfd_signed_vma jump_offset
;
2722 asection
*sym_sec
= NULL
;
2723 struct elf32_mn10300_link_hash_entry
*h
= NULL
;
2725 /* If this isn't something that can be relaxed, then ignore
2727 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_NONE
2728 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_8
2729 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_MAX
)
2732 /* Get the section contents if we haven't done so already. */
2733 if (contents
== NULL
)
2735 /* Get cached copy if it exists. */
2736 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2737 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2740 /* Go get them off disk. */
2741 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
2746 /* Read this BFD's symbols if we haven't done so already. */
2747 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2749 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2750 if (isymbuf
== NULL
)
2751 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
2752 symtab_hdr
->sh_info
, 0,
2754 if (isymbuf
== NULL
)
2758 /* Get the value of the symbol referred to by the reloc. */
2759 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2761 Elf_Internal_Sym
*isym
;
2762 const char *sym_name
;
2765 /* A local symbol. */
2766 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
2767 if (isym
->st_shndx
== SHN_UNDEF
)
2768 sym_sec
= bfd_und_section_ptr
;
2769 else if (isym
->st_shndx
== SHN_ABS
)
2770 sym_sec
= bfd_abs_section_ptr
;
2771 else if (isym
->st_shndx
== SHN_COMMON
)
2772 sym_sec
= bfd_com_section_ptr
;
2774 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2776 sym_name
= bfd_elf_string_from_elf_section (abfd
,
2777 symtab_hdr
->sh_link
,
2780 if ((sym_sec
->flags
& SEC_MERGE
)
2781 && sym_sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2783 symval
= isym
->st_value
;
2785 /* GAS may reduce relocations against symbols in SEC_MERGE
2786 sections to a relocation against the section symbol when
2787 the original addend was zero. When the reloc is against
2788 a section symbol we should include the addend in the
2789 offset passed to _bfd_merged_section_offset, since the
2790 location of interest is the original symbol. On the
2791 other hand, an access to "sym+addend" where "sym" is not
2792 a section symbol should not include the addend; Such an
2793 access is presumed to be an offset from "sym"; The
2794 location of interest is just "sym". */
2795 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
2796 symval
+= irel
->r_addend
;
2798 symval
= _bfd_merged_section_offset (abfd
, & sym_sec
,
2799 elf_section_data (sym_sec
)->sec_info
,
2802 if (ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
2803 symval
+= irel
->r_addend
;
2805 symval
+= sym_sec
->output_section
->vma
2806 + sym_sec
->output_offset
- irel
->r_addend
;
2809 symval
= (isym
->st_value
2810 + sym_sec
->output_section
->vma
2811 + sym_sec
->output_offset
);
2813 /* Tack on an ID so we can uniquely identify this
2814 local symbol in the global hash table. */
2815 new_name
= bfd_malloc ((bfd_size_type
) strlen (sym_name
) + 10);
2816 if (new_name
== NULL
)
2818 sprintf (new_name
, "%s_%08x", sym_name
, sym_sec
->id
);
2819 sym_name
= new_name
;
2821 h
= (struct elf32_mn10300_link_hash_entry
*)
2822 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
2823 sym_name
, FALSE
, FALSE
, FALSE
);
2830 /* An external symbol. */
2831 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
2832 h
= (struct elf32_mn10300_link_hash_entry
*)
2833 (elf_sym_hashes (abfd
)[indx
]);
2834 BFD_ASSERT (h
!= NULL
);
2835 if (h
->root
.root
.type
!= bfd_link_hash_defined
2836 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
2837 /* This appears to be a reference to an undefined
2838 symbol. Just ignore it--it will be caught by the
2839 regular reloc processing. */
2842 /* Check for a reference to a discarded symbol and ignore it. */
2843 if (h
->root
.root
.u
.def
.section
->output_section
== NULL
)
2846 sym_sec
= h
->root
.root
.u
.def
.section
->output_section
;
2848 symval
= (h
->root
.root
.u
.def
.value
2849 + h
->root
.root
.u
.def
.section
->output_section
->vma
2850 + h
->root
.root
.u
.def
.section
->output_offset
);
2853 /* For simplicity of coding, we are going to modify the section
2854 contents, the section relocs, and the BFD symbol table. We
2855 must tell the rest of the code not to free up this
2856 information. It would be possible to instead create a table
2857 of changes which have to be made, as is done in coff-mips.c;
2858 that would be more work, but would require less memory when
2859 the linker is run. */
2861 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2862 branch/call, also deal with "call" -> "calls" conversions and
2863 insertion of prologue data into "call" instructions. */
2864 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL32
2865 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PLT32
)
2867 bfd_vma value
= symval
;
2869 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PLT32
2871 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_INTERNAL
2872 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_HIDDEN
2873 && h
->root
.plt
.offset
!= (bfd_vma
) -1)
2877 splt
= bfd_get_section_by_name (elf_hash_table (link_info
)
2880 value
= ((splt
->output_section
->vma
2881 + splt
->output_offset
2882 + h
->root
.plt
.offset
)
2883 - (sec
->output_section
->vma
2884 + sec
->output_offset
2888 /* If we've got a "call" instruction that needs to be turned
2889 into a "calls" instruction, do so now. It saves a byte. */
2890 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
2894 /* Get the opcode. */
2895 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2897 /* Make sure we're working with a "call" instruction! */
2900 /* Note that we've changed the relocs, section contents,
2902 elf_section_data (sec
)->relocs
= internal_relocs
;
2903 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2904 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2906 /* Fix the opcode. */
2907 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 1);
2908 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
2910 /* Fix irel->r_offset and irel->r_addend. */
2911 irel
->r_offset
+= 1;
2912 irel
->r_addend
+= 1;
2914 /* Delete one byte of data. */
2915 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2916 irel
->r_offset
+ 3, 1))
2919 /* That will change things, so, we should relax again.
2920 Note that this is not required, and it may be slow. */
2926 /* We've got a "call" instruction which needs some data
2927 from target function filled in. */
2930 /* Get the opcode. */
2931 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2933 /* Insert data from the target function into the "call"
2934 instruction if needed. */
2937 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 4);
2938 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
2939 contents
+ irel
->r_offset
+ 5);
2943 /* Deal with pc-relative gunk. */
2944 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
2945 value
-= irel
->r_offset
;
2946 value
+= irel
->r_addend
;
2948 /* See if the value will fit in 16 bits, note the high value is
2949 0x7fff + 2 as the target will be two bytes closer if we are
2950 able to relax, if it's in the same section. */
2951 if (sec
->output_section
== sym_sec
->output_section
)
2952 jump_offset
= 0x8001;
2954 jump_offset
= 0x7fff;
2956 /* Account for jumps across alignment boundaries using
2957 align_gap_adjustment. */
2958 if ((bfd_signed_vma
) value
< jump_offset
- (bfd_signed_vma
) align_gap_adjustment
2959 && ((bfd_signed_vma
) value
> -0x8000 + (bfd_signed_vma
) align_gap_adjustment
))
2963 /* Get the opcode. */
2964 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2966 if (code
!= 0xdc && code
!= 0xdd && code
!= 0xff)
2969 /* Note that we've changed the relocs, section contents, etc. */
2970 elf_section_data (sec
)->relocs
= internal_relocs
;
2971 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2972 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2974 /* Fix the opcode. */
2976 bfd_put_8 (abfd
, 0xcc, contents
+ irel
->r_offset
- 1);
2977 else if (code
== 0xdd)
2978 bfd_put_8 (abfd
, 0xcd, contents
+ irel
->r_offset
- 1);
2979 else if (code
== 0xff)
2980 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2982 /* Fix the relocation's type. */
2983 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2984 (ELF32_R_TYPE (irel
->r_info
)
2985 == (int) R_MN10300_PLT32
)
2989 /* Delete two bytes of data. */
2990 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2991 irel
->r_offset
+ 1, 2))
2994 /* That will change things, so, we should relax again.
2995 Note that this is not required, and it may be slow. */
3000 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
3002 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL16
)
3004 bfd_vma value
= symval
;
3006 /* If we've got a "call" instruction that needs to be turned
3007 into a "calls" instruction, do so now. It saves a byte. */
3008 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
3012 /* Get the opcode. */
3013 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3015 /* Make sure we're working with a "call" instruction! */
3018 /* Note that we've changed the relocs, section contents,
3020 elf_section_data (sec
)->relocs
= internal_relocs
;
3021 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3022 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3024 /* Fix the opcode. */
3025 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 1);
3026 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
3028 /* Fix irel->r_offset and irel->r_addend. */
3029 irel
->r_offset
+= 1;
3030 irel
->r_addend
+= 1;
3032 /* Delete one byte of data. */
3033 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3034 irel
->r_offset
+ 1, 1))
3037 /* That will change things, so, we should relax again.
3038 Note that this is not required, and it may be slow. */
3046 /* Get the opcode. */
3047 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3049 /* Insert data from the target function into the "call"
3050 instruction if needed. */
3053 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 2);
3054 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
3055 contents
+ irel
->r_offset
+ 3);
3059 /* Deal with pc-relative gunk. */
3060 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
3061 value
-= irel
->r_offset
;
3062 value
+= irel
->r_addend
;
3064 /* See if the value will fit in 8 bits, note the high value is
3065 0x7f + 1 as the target will be one bytes closer if we are
3067 if ((long) value
< 0x80 && (long) value
> -0x80)
3071 /* Get the opcode. */
3072 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3077 /* Note that we've changed the relocs, section contents, etc. */
3078 elf_section_data (sec
)->relocs
= internal_relocs
;
3079 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3080 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3082 /* Fix the opcode. */
3083 bfd_put_8 (abfd
, 0xca, contents
+ irel
->r_offset
- 1);
3085 /* Fix the relocation's type. */
3086 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3089 /* Delete one byte of data. */
3090 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3091 irel
->r_offset
+ 1, 1))
3094 /* That will change things, so, we should relax again.
3095 Note that this is not required, and it may be slow. */
3100 /* Try to eliminate an unconditional 8 bit pc-relative branch
3101 which immediately follows a conditional 8 bit pc-relative
3102 branch around the unconditional branch.
3109 This happens when the bCC can't reach lab2 at assembly time,
3110 but due to other relaxations it can reach at link time. */
3111 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL8
)
3113 Elf_Internal_Rela
*nrel
;
3114 bfd_vma value
= symval
;
3117 /* Deal with pc-relative gunk. */
3118 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
3119 value
-= irel
->r_offset
;
3120 value
+= irel
->r_addend
;
3122 /* Do nothing if this reloc is the last byte in the section. */
3123 if (irel
->r_offset
== sec
->size
)
3126 /* See if the next instruction is an unconditional pc-relative
3127 branch, more often than not this test will fail, so we
3128 test it first to speed things up. */
3129 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
3133 /* Also make sure the next relocation applies to the next
3134 instruction and that it's a pc-relative 8 bit branch. */
3137 || irel
->r_offset
+ 2 != nrel
->r_offset
3138 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10300_PCREL8
)
3141 /* Make sure our destination immediately follows the
3142 unconditional branch. */
3143 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
3144 + irel
->r_offset
+ 3))
3147 /* Now make sure we are a conditional branch. This may not
3148 be necessary, but why take the chance.
3150 Note these checks assume that R_MN10300_PCREL8 relocs
3151 only occur on bCC and bCCx insns. If they occured
3152 elsewhere, we'd need to know the start of this insn
3153 for this check to be accurate. */
3154 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3155 if (code
!= 0xc0 && code
!= 0xc1 && code
!= 0xc2
3156 && code
!= 0xc3 && code
!= 0xc4 && code
!= 0xc5
3157 && code
!= 0xc6 && code
!= 0xc7 && code
!= 0xc8
3158 && code
!= 0xc9 && code
!= 0xe8 && code
!= 0xe9
3159 && code
!= 0xea && code
!= 0xeb)
3162 /* We also have to be sure there is no symbol/label
3163 at the unconditional branch. */
3164 if (mn10300_elf_symbol_address_p (abfd
, sec
, isymbuf
,
3165 irel
->r_offset
+ 1))
3168 /* Note that we've changed the relocs, section contents, etc. */
3169 elf_section_data (sec
)->relocs
= internal_relocs
;
3170 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3171 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3173 /* Reverse the condition of the first branch. */
3219 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3221 /* Set the reloc type and symbol for the first branch
3222 from the second branch. */
3223 irel
->r_info
= nrel
->r_info
;
3225 /* Make the reloc for the second branch a null reloc. */
3226 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
3229 /* Delete two bytes of data. */
3230 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3231 irel
->r_offset
+ 1, 2))
3234 /* That will change things, so, we should relax again.
3235 Note that this is not required, and it may be slow. */
3239 /* Try to turn a 24 immediate, displacement or absolute address
3240 into a 8 immediate, displacement or absolute address. */
3241 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_24
)
3243 bfd_vma value
= symval
;
3244 value
+= irel
->r_addend
;
3246 /* See if the value will fit in 8 bits. */
3247 if ((long) value
< 0x7f && (long) value
> -0x80)
3251 /* AM33 insns which have 24 operands are 6 bytes long and
3252 will have 0xfd as the first byte. */
3254 /* Get the first opcode. */
3255 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
3259 /* Get the second opcode. */
3260 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
3262 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3263 equivalent instructions exists. */
3264 if (code
!= 0x6b && code
!= 0x7b
3265 && code
!= 0x8b && code
!= 0x9b
3266 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
3267 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
3268 || (code
& 0x0f) == 0x0e))
3270 /* Not safe if the high bit is on as relaxing may
3271 move the value out of high mem and thus not fit
3272 in a signed 8bit value. This is currently over
3274 if ((value
& 0x80) == 0)
3276 /* Note that we've changed the relocation contents,
3278 elf_section_data (sec
)->relocs
= internal_relocs
;
3279 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3280 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3282 /* Fix the opcode. */
3283 bfd_put_8 (abfd
, 0xfb, contents
+ irel
->r_offset
- 3);
3284 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3286 /* Fix the relocation's type. */
3288 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3291 /* Delete two bytes of data. */
3292 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3293 irel
->r_offset
+ 1, 2))
3296 /* That will change things, so, we should relax
3297 again. Note that this is not required, and it
3307 /* Try to turn a 32bit immediate, displacement or absolute address
3308 into a 16bit immediate, displacement or absolute address. */
3309 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_32
3310 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOT32
3311 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTOFF32
)
3313 bfd_vma value
= symval
;
3315 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_MN10300_32
)
3319 sgot
= bfd_get_section_by_name (elf_hash_table (link_info
)
3322 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOT32
)
3324 value
= sgot
->output_offset
;
3327 value
+= h
->root
.got
.offset
;
3329 value
+= (elf_local_got_offsets
3330 (abfd
)[ELF32_R_SYM (irel
->r_info
)]);
3332 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTOFF32
)
3333 value
-= sgot
->output_section
->vma
;
3334 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTPC32
)
3335 value
= (sgot
->output_section
->vma
3336 - (sec
->output_section
->vma
3337 + sec
->output_offset
3343 value
+= irel
->r_addend
;
3345 /* See if the value will fit in 24 bits.
3346 We allow any 16bit match here. We prune those we can't
3348 if ((long) value
< 0x7fffff && (long) value
> -0x800000)
3352 /* AM33 insns which have 32bit operands are 7 bytes long and
3353 will have 0xfe as the first byte. */
3355 /* Get the first opcode. */
3356 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
3360 /* Get the second opcode. */
3361 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
3363 /* All the am33 32 -> 24 relaxing possibilities. */
3364 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3365 equivalent instructions exists. */
3366 if (code
!= 0x6b && code
!= 0x7b
3367 && code
!= 0x8b && code
!= 0x9b
3368 && (ELF32_R_TYPE (irel
->r_info
)
3369 != (int) R_MN10300_GOTPC32
)
3370 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
3371 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
3372 || (code
& 0x0f) == 0x0e))
3374 /* Not safe if the high bit is on as relaxing may
3375 move the value out of high mem and thus not fit
3376 in a signed 16bit value. This is currently over
3378 if ((value
& 0x8000) == 0)
3380 /* Note that we've changed the relocation contents,
3382 elf_section_data (sec
)->relocs
= internal_relocs
;
3383 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3384 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3386 /* Fix the opcode. */
3387 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 3);
3388 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3390 /* Fix the relocation's type. */
3392 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3393 (ELF32_R_TYPE (irel
->r_info
)
3394 == (int) R_MN10300_GOTOFF32
)
3395 ? R_MN10300_GOTOFF24
3396 : (ELF32_R_TYPE (irel
->r_info
)
3397 == (int) R_MN10300_GOT32
)
3401 /* Delete one byte of data. */
3402 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3403 irel
->r_offset
+ 3, 1))
3406 /* That will change things, so, we should relax
3407 again. Note that this is not required, and it
3416 /* See if the value will fit in 16 bits.
3417 We allow any 16bit match here. We prune those we can't
3419 if ((long) value
< 0x7fff && (long) value
> -0x8000)
3423 /* Most insns which have 32bit operands are 6 bytes long;
3424 exceptions are pcrel insns and bit insns.
3426 We handle pcrel insns above. We don't bother trying
3427 to handle the bit insns here.
3429 The first byte of the remaining insns will be 0xfc. */
3431 /* Get the first opcode. */
3432 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
3437 /* Get the second opcode. */
3438 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3440 if ((code
& 0xf0) < 0x80)
3441 switch (code
& 0xf0)
3443 /* mov (d32,am),dn -> mov (d32,am),dn
3444 mov dm,(d32,am) -> mov dn,(d32,am)
3445 mov (d32,am),an -> mov (d32,am),an
3446 mov dm,(d32,am) -> mov dn,(d32,am)
3447 movbu (d32,am),dn -> movbu (d32,am),dn
3448 movbu dm,(d32,am) -> movbu dn,(d32,am)
3449 movhu (d32,am),dn -> movhu (d32,am),dn
3450 movhu dm,(d32,am) -> movhu dn,(d32,am) */
3459 /* Not safe if the high bit is on as relaxing may
3460 move the value out of high mem and thus not fit
3461 in a signed 16bit value. */
3463 && (value
& 0x8000))
3466 /* Note that we've changed the relocation contents, etc. */
3467 elf_section_data (sec
)->relocs
= internal_relocs
;
3468 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3469 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3471 /* Fix the opcode. */
3472 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3473 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3475 /* Fix the relocation's type. */
3476 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3477 (ELF32_R_TYPE (irel
->r_info
)
3478 == (int) R_MN10300_GOTOFF32
)
3479 ? R_MN10300_GOTOFF16
3480 : (ELF32_R_TYPE (irel
->r_info
)
3481 == (int) R_MN10300_GOT32
)
3483 : (ELF32_R_TYPE (irel
->r_info
)
3484 == (int) R_MN10300_GOTPC32
)
3485 ? R_MN10300_GOTPC16
:
3488 /* Delete two bytes of data. */
3489 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3490 irel
->r_offset
+ 2, 2))
3493 /* That will change things, so, we should relax again.
3494 Note that this is not required, and it may be slow. */
3498 else if ((code
& 0xf0) == 0x80
3499 || (code
& 0xf0) == 0x90)
3500 switch (code
& 0xf3)
3502 /* mov dn,(abs32) -> mov dn,(abs16)
3503 movbu dn,(abs32) -> movbu dn,(abs16)
3504 movhu dn,(abs32) -> movhu dn,(abs16) */
3508 /* Note that we've changed the relocation contents, etc. */
3509 elf_section_data (sec
)->relocs
= internal_relocs
;
3510 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3511 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3513 if ((code
& 0xf3) == 0x81)
3514 code
= 0x01 + (code
& 0x0c);
3515 else if ((code
& 0xf3) == 0x82)
3516 code
= 0x02 + (code
& 0x0c);
3517 else if ((code
& 0xf3) == 0x83)
3518 code
= 0x03 + (code
& 0x0c);
3522 /* Fix the opcode. */
3523 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3525 /* Fix the relocation's type. */
3526 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3527 (ELF32_R_TYPE (irel
->r_info
)
3528 == (int) R_MN10300_GOTOFF32
)
3529 ? R_MN10300_GOTOFF16
3530 : (ELF32_R_TYPE (irel
->r_info
)
3531 == (int) R_MN10300_GOT32
)
3533 : (ELF32_R_TYPE (irel
->r_info
)
3534 == (int) R_MN10300_GOTPC32
)
3535 ? R_MN10300_GOTPC16
:
3538 /* The opcode got shorter too, so we have to fix the
3539 addend and offset too! */
3540 irel
->r_offset
-= 1;
3542 /* Delete three bytes of data. */
3543 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3544 irel
->r_offset
+ 1, 3))
3547 /* That will change things, so, we should relax again.
3548 Note that this is not required, and it may be slow. */
3552 /* mov am,(abs32) -> mov am,(abs16)
3553 mov am,(d32,sp) -> mov am,(d16,sp)
3554 mov dm,(d32,sp) -> mov dm,(d32,sp)
3555 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3556 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3562 /* sp-based offsets are zero-extended. */
3563 if (code
>= 0x90 && code
<= 0x93
3564 && (long) value
< 0)
3567 /* Note that we've changed the relocation contents, etc. */
3568 elf_section_data (sec
)->relocs
= internal_relocs
;
3569 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3570 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3572 /* Fix the opcode. */
3573 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3574 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3576 /* Fix the relocation's type. */
3577 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3578 (ELF32_R_TYPE (irel
->r_info
)
3579 == (int) R_MN10300_GOTOFF32
)
3580 ? R_MN10300_GOTOFF16
3581 : (ELF32_R_TYPE (irel
->r_info
)
3582 == (int) R_MN10300_GOT32
)
3584 : (ELF32_R_TYPE (irel
->r_info
)
3585 == (int) R_MN10300_GOTPC32
)
3586 ? R_MN10300_GOTPC16
:
3589 /* Delete two bytes of data. */
3590 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3591 irel
->r_offset
+ 2, 2))
3594 /* That will change things, so, we should relax again.
3595 Note that this is not required, and it may be slow. */
3599 else if ((code
& 0xf0) < 0xf0)
3600 switch (code
& 0xfc)
3602 /* mov imm32,dn -> mov imm16,dn
3603 mov imm32,an -> mov imm16,an
3604 mov (abs32),dn -> mov (abs16),dn
3605 movbu (abs32),dn -> movbu (abs16),dn
3606 movhu (abs32),dn -> movhu (abs16),dn */
3612 /* Not safe if the high bit is on as relaxing may
3613 move the value out of high mem and thus not fit
3614 in a signed 16bit value. */
3616 && (value
& 0x8000))
3619 /* mov imm16, an zero-extends the immediate. */
3621 && (long) value
< 0)
3624 /* Note that we've changed the relocation contents, etc. */
3625 elf_section_data (sec
)->relocs
= internal_relocs
;
3626 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3627 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3629 if ((code
& 0xfc) == 0xcc)
3630 code
= 0x2c + (code
& 0x03);
3631 else if ((code
& 0xfc) == 0xdc)
3632 code
= 0x24 + (code
& 0x03);
3633 else if ((code
& 0xfc) == 0xa4)
3634 code
= 0x30 + (code
& 0x03);
3635 else if ((code
& 0xfc) == 0xa8)
3636 code
= 0x34 + (code
& 0x03);
3637 else if ((code
& 0xfc) == 0xac)
3638 code
= 0x38 + (code
& 0x03);
3642 /* Fix the opcode. */
3643 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3645 /* Fix the relocation's type. */
3646 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3647 (ELF32_R_TYPE (irel
->r_info
)
3648 == (int) R_MN10300_GOTOFF32
)
3649 ? R_MN10300_GOTOFF16
3650 : (ELF32_R_TYPE (irel
->r_info
)
3651 == (int) R_MN10300_GOT32
)
3653 : (ELF32_R_TYPE (irel
->r_info
)
3654 == (int) R_MN10300_GOTPC32
)
3655 ? R_MN10300_GOTPC16
:
3658 /* The opcode got shorter too, so we have to fix the
3659 addend and offset too! */
3660 irel
->r_offset
-= 1;
3662 /* Delete three bytes of data. */
3663 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3664 irel
->r_offset
+ 1, 3))
3667 /* That will change things, so, we should relax again.
3668 Note that this is not required, and it may be slow. */
3672 /* mov (abs32),an -> mov (abs16),an
3673 mov (d32,sp),an -> mov (d16,sp),an
3674 mov (d32,sp),dn -> mov (d16,sp),dn
3675 movbu (d32,sp),dn -> movbu (d16,sp),dn
3676 movhu (d32,sp),dn -> movhu (d16,sp),dn
3677 add imm32,dn -> add imm16,dn
3678 cmp imm32,dn -> cmp imm16,dn
3679 add imm32,an -> add imm16,an
3680 cmp imm32,an -> cmp imm16,an
3681 and imm32,dn -> and imm16,dn
3682 or imm32,dn -> or imm16,dn
3683 xor imm32,dn -> xor imm16,dn
3684 btst imm32,dn -> btst imm16,dn */
3700 /* cmp imm16, an zero-extends the immediate. */
3702 && (long) value
< 0)
3705 /* So do sp-based offsets. */
3706 if (code
>= 0xb0 && code
<= 0xb3
3707 && (long) value
< 0)
3710 /* Note that we've changed the relocation contents, etc. */
3711 elf_section_data (sec
)->relocs
= internal_relocs
;
3712 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3713 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3715 /* Fix the opcode. */
3716 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3717 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3719 /* Fix the relocation's type. */
3720 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3721 (ELF32_R_TYPE (irel
->r_info
)
3722 == (int) R_MN10300_GOTOFF32
)
3723 ? R_MN10300_GOTOFF16
3724 : (ELF32_R_TYPE (irel
->r_info
)
3725 == (int) R_MN10300_GOT32
)
3727 : (ELF32_R_TYPE (irel
->r_info
)
3728 == (int) R_MN10300_GOTPC32
)
3729 ? R_MN10300_GOTPC16
:
3732 /* Delete two bytes of data. */
3733 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3734 irel
->r_offset
+ 2, 2))
3737 /* That will change things, so, we should relax again.
3738 Note that this is not required, and it may be slow. */
3742 else if (code
== 0xfe)
3744 /* add imm32,sp -> add imm16,sp */
3746 /* Note that we've changed the relocation contents, etc. */
3747 elf_section_data (sec
)->relocs
= internal_relocs
;
3748 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3749 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3751 /* Fix the opcode. */
3752 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3753 bfd_put_8 (abfd
, 0xfe, contents
+ irel
->r_offset
- 1);
3755 /* Fix the relocation's type. */
3756 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3757 (ELF32_R_TYPE (irel
->r_info
)
3758 == (int) R_MN10300_GOT32
)
3760 : (ELF32_R_TYPE (irel
->r_info
)
3761 == (int) R_MN10300_GOTOFF32
)
3762 ? R_MN10300_GOTOFF16
3763 : (ELF32_R_TYPE (irel
->r_info
)
3764 == (int) R_MN10300_GOTPC32
)
3765 ? R_MN10300_GOTPC16
:
3768 /* Delete two bytes of data. */
3769 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3770 irel
->r_offset
+ 2, 2))
3773 /* That will change things, so, we should relax again.
3774 Note that this is not required, and it may be slow. */
3783 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3785 if (! link_info
->keep_memory
)
3789 /* Cache the symbols for elf_link_input_bfd. */
3790 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3794 if (contents
!= NULL
3795 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3797 if (! link_info
->keep_memory
)
3801 /* Cache the section contents for elf_link_input_bfd. */
3802 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3806 if (internal_relocs
!= NULL
3807 && elf_section_data (sec
)->relocs
!= internal_relocs
)
3808 free (internal_relocs
);
3814 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3816 if (contents
!= NULL
3817 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
3819 if (internal_relocs
!= NULL
3820 && elf_section_data (section
)->relocs
!= internal_relocs
)
3821 free (internal_relocs
);
3826 /* This is a version of bfd_generic_get_relocated_section_contents
3827 which uses mn10300_elf_relocate_section. */
3830 mn10300_elf_get_relocated_section_contents (bfd
*output_bfd
,
3831 struct bfd_link_info
*link_info
,
3832 struct bfd_link_order
*link_order
,
3834 bfd_boolean relocatable
,
3837 Elf_Internal_Shdr
*symtab_hdr
;
3838 asection
*input_section
= link_order
->u
.indirect
.section
;
3839 bfd
*input_bfd
= input_section
->owner
;
3840 asection
**sections
= NULL
;
3841 Elf_Internal_Rela
*internal_relocs
= NULL
;
3842 Elf_Internal_Sym
*isymbuf
= NULL
;
3844 /* We only need to handle the case of relaxing, or of having a
3845 particular set of section contents, specially. */
3847 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
3848 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
3853 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3855 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
3856 (size_t) input_section
->size
);
3858 if ((input_section
->flags
& SEC_RELOC
) != 0
3859 && input_section
->reloc_count
> 0)
3862 Elf_Internal_Sym
*isym
, *isymend
;
3865 internal_relocs
= _bfd_elf_link_read_relocs (input_bfd
, input_section
,
3867 if (internal_relocs
== NULL
)
3870 if (symtab_hdr
->sh_info
!= 0)
3872 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3873 if (isymbuf
== NULL
)
3874 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3875 symtab_hdr
->sh_info
, 0,
3877 if (isymbuf
== NULL
)
3881 amt
= symtab_hdr
->sh_info
;
3882 amt
*= sizeof (asection
*);
3883 sections
= bfd_malloc (amt
);
3884 if (sections
== NULL
&& amt
!= 0)
3887 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
3888 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
3892 if (isym
->st_shndx
== SHN_UNDEF
)
3893 isec
= bfd_und_section_ptr
;
3894 else if (isym
->st_shndx
== SHN_ABS
)
3895 isec
= bfd_abs_section_ptr
;
3896 else if (isym
->st_shndx
== SHN_COMMON
)
3897 isec
= bfd_com_section_ptr
;
3899 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
3904 if (! mn10300_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
3905 input_section
, data
, internal_relocs
,
3909 if (sections
!= NULL
)
3911 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3913 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
3914 free (internal_relocs
);
3920 if (sections
!= NULL
)
3922 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3924 if (internal_relocs
!= NULL
3925 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
3926 free (internal_relocs
);
3930 /* Assorted hash table functions. */
3932 /* Initialize an entry in the link hash table. */
3934 /* Create an entry in an MN10300 ELF linker hash table. */
3936 static struct bfd_hash_entry
*
3937 elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry
*entry
,
3938 struct bfd_hash_table
*table
,
3941 struct elf32_mn10300_link_hash_entry
*ret
=
3942 (struct elf32_mn10300_link_hash_entry
*) entry
;
3944 /* Allocate the structure if it has not already been allocated by a
3947 ret
= (struct elf32_mn10300_link_hash_entry
*)
3948 bfd_hash_allocate (table
, sizeof (* ret
));
3950 return (struct bfd_hash_entry
*) ret
;
3952 /* Call the allocation method of the superclass. */
3953 ret
= (struct elf32_mn10300_link_hash_entry
*)
3954 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
3958 ret
->direct_calls
= 0;
3959 ret
->stack_size
= 0;
3961 ret
->movm_stack_size
= 0;
3966 return (struct bfd_hash_entry
*) ret
;
3969 /* Create an mn10300 ELF linker hash table. */
3971 static struct bfd_link_hash_table
*
3972 elf32_mn10300_link_hash_table_create (bfd
*abfd
)
3974 struct elf32_mn10300_link_hash_table
*ret
;
3975 bfd_size_type amt
= sizeof (* ret
);
3977 ret
= bfd_malloc (amt
);
3981 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
3982 elf32_mn10300_link_hash_newfunc
,
3983 sizeof (struct elf32_mn10300_link_hash_entry
),
3991 amt
= sizeof (struct elf_link_hash_table
);
3992 ret
->static_hash_table
= bfd_malloc (amt
);
3993 if (ret
->static_hash_table
== NULL
)
3999 if (!_bfd_elf_link_hash_table_init (&ret
->static_hash_table
->root
, abfd
,
4000 elf32_mn10300_link_hash_newfunc
,
4001 sizeof (struct elf32_mn10300_link_hash_entry
),
4004 free (ret
->static_hash_table
);
4008 return & ret
->root
.root
;
4011 /* Free an mn10300 ELF linker hash table. */
4014 elf32_mn10300_link_hash_table_free (struct bfd_link_hash_table
*hash
)
4016 struct elf32_mn10300_link_hash_table
*ret
4017 = (struct elf32_mn10300_link_hash_table
*) hash
;
4019 _bfd_generic_link_hash_table_free
4020 ((struct bfd_link_hash_table
*) ret
->static_hash_table
);
4021 _bfd_generic_link_hash_table_free
4022 ((struct bfd_link_hash_table
*) ret
);
4025 static unsigned long
4026 elf_mn10300_mach (flagword flags
)
4028 switch (flags
& EF_MN10300_MACH
)
4030 case E_MN10300_MACH_MN10300
:
4032 return bfd_mach_mn10300
;
4034 case E_MN10300_MACH_AM33
:
4035 return bfd_mach_am33
;
4037 case E_MN10300_MACH_AM33_2
:
4038 return bfd_mach_am33_2
;
4042 /* The final processing done just before writing out a MN10300 ELF object
4043 file. This gets the MN10300 architecture right based on the machine
4047 _bfd_mn10300_elf_final_write_processing (bfd
*abfd
,
4048 bfd_boolean linker ATTRIBUTE_UNUSED
)
4052 switch (bfd_get_mach (abfd
))
4055 case bfd_mach_mn10300
:
4056 val
= E_MN10300_MACH_MN10300
;
4060 val
= E_MN10300_MACH_AM33
;
4063 case bfd_mach_am33_2
:
4064 val
= E_MN10300_MACH_AM33_2
;
4068 elf_elfheader (abfd
)->e_flags
&= ~ (EF_MN10300_MACH
);
4069 elf_elfheader (abfd
)->e_flags
|= val
;
4073 _bfd_mn10300_elf_object_p (bfd
*abfd
)
4075 bfd_default_set_arch_mach (abfd
, bfd_arch_mn10300
,
4076 elf_mn10300_mach (elf_elfheader (abfd
)->e_flags
));
4080 /* Merge backend specific data from an object file to the output
4081 object file when linking. */
4084 _bfd_mn10300_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
4086 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4087 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4090 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
4091 && bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
4093 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
4094 bfd_get_mach (ibfd
)))
4101 #define PLT0_ENTRY_SIZE 15
4102 #define PLT_ENTRY_SIZE 20
4103 #define PIC_PLT_ENTRY_SIZE 24
4105 static const bfd_byte elf_mn10300_plt0_entry
[PLT0_ENTRY_SIZE
] =
4107 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
4108 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
4109 0xf0, 0xf4, /* jmp (a0) */
4112 static const bfd_byte elf_mn10300_plt_entry
[PLT_ENTRY_SIZE
] =
4114 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
4115 0xf0, 0xf4, /* jmp (a0) */
4116 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4117 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
4120 static const bfd_byte elf_mn10300_pic_plt_entry
[PIC_PLT_ENTRY_SIZE
] =
4122 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
4123 0xf0, 0xf4, /* jmp (a0) */
4124 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4125 0xf8, 0x22, 8, /* mov (8,a2),a0 */
4126 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
4127 0xf0, 0xf4, /* jmp (a0) */
4130 /* Return size of the first PLT entry. */
4131 #define elf_mn10300_sizeof_plt0(info) \
4132 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4134 /* Return size of a PLT entry. */
4135 #define elf_mn10300_sizeof_plt(info) \
4136 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4138 /* Return offset of the PLT0 address in an absolute PLT entry. */
4139 #define elf_mn10300_plt_plt0_offset(info) 16
4141 /* Return offset of the linker in PLT0 entry. */
4142 #define elf_mn10300_plt0_linker_offset(info) 2
4144 /* Return offset of the GOT id in PLT0 entry. */
4145 #define elf_mn10300_plt0_gotid_offset(info) 9
4147 /* Return offset of the temporary in PLT entry. */
4148 #define elf_mn10300_plt_temp_offset(info) 8
4150 /* Return offset of the symbol in PLT entry. */
4151 #define elf_mn10300_plt_symbol_offset(info) 2
4153 /* Return offset of the relocation in PLT entry. */
4154 #define elf_mn10300_plt_reloc_offset(info) 11
4156 /* The name of the dynamic interpreter. This is put in the .interp
4159 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4161 /* Create dynamic sections when linking against a dynamic object. */
4164 _bfd_mn10300_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
4168 const struct elf_backend_data
* bed
= get_elf_backend_data (abfd
);
4171 switch (bed
->s
->arch_size
)
4182 bfd_set_error (bfd_error_bad_value
);
4186 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4187 .rel[a].bss sections. */
4188 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
4189 | SEC_LINKER_CREATED
);
4191 s
= bfd_make_section_with_flags (abfd
,
4192 (bed
->default_use_rela_p
4193 ? ".rela.plt" : ".rel.plt"),
4194 flags
| SEC_READONLY
);
4196 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
4199 if (! _bfd_mn10300_elf_create_got_section (abfd
, info
))
4203 const char * secname
;
4208 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4210 secflags
= bfd_get_section_flags (abfd
, sec
);
4211 if ((secflags
& (SEC_DATA
| SEC_LINKER_CREATED
))
4212 || ((secflags
& SEC_HAS_CONTENTS
) != SEC_HAS_CONTENTS
))
4215 secname
= bfd_get_section_name (abfd
, sec
);
4216 relname
= bfd_malloc (strlen (secname
) + 6);
4217 strcpy (relname
, ".rela");
4218 strcat (relname
, secname
);
4220 s
= bfd_make_section_with_flags (abfd
, relname
,
4221 flags
| SEC_READONLY
);
4223 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
4228 if (bed
->want_dynbss
)
4230 /* The .dynbss section is a place to put symbols which are defined
4231 by dynamic objects, are referenced by regular objects, and are
4232 not functions. We must allocate space for them in the process
4233 image and use a R_*_COPY reloc to tell the dynamic linker to
4234 initialize them at run time. The linker script puts the .dynbss
4235 section into the .bss section of the final image. */
4236 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
4237 SEC_ALLOC
| SEC_LINKER_CREATED
);
4241 /* The .rel[a].bss section holds copy relocs. This section is not
4242 normally needed. We need to create it here, though, so that the
4243 linker will map it to an output section. We can't just create it
4244 only if we need it, because we will not know whether we need it
4245 until we have seen all the input files, and the first time the
4246 main linker code calls BFD after examining all the input files
4247 (size_dynamic_sections) the input sections have already been
4248 mapped to the output sections. If the section turns out not to
4249 be needed, we can discard it later. We will never need this
4250 section when generating a shared object, since they do not use
4254 s
= bfd_make_section_with_flags (abfd
,
4255 (bed
->default_use_rela_p
4256 ? ".rela.bss" : ".rel.bss"),
4257 flags
| SEC_READONLY
);
4259 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
4267 /* Adjust a symbol defined by a dynamic object and referenced by a
4268 regular object. The current definition is in some section of the
4269 dynamic object, but we're not including those sections. We have to
4270 change the definition to something the rest of the link can
4274 _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info
* info
,
4275 struct elf_link_hash_entry
* h
)
4280 dynobj
= elf_hash_table (info
)->dynobj
;
4282 /* Make sure we know what is going on here. */
4283 BFD_ASSERT (dynobj
!= NULL
4285 || h
->u
.weakdef
!= NULL
4288 && !h
->def_regular
)));
4290 /* If this is a function, put it in the procedure linkage table. We
4291 will fill in the contents of the procedure linkage table later,
4292 when we know the address of the .got section. */
4293 if (h
->type
== STT_FUNC
4300 /* This case can occur if we saw a PLT reloc in an input
4301 file, but the symbol was never referred to by a dynamic
4302 object. In such a case, we don't actually need to build
4303 a procedure linkage table, and we can just do a REL32
4305 BFD_ASSERT (h
->needs_plt
);
4309 /* Make sure this symbol is output as a dynamic symbol. */
4310 if (h
->dynindx
== -1)
4312 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4316 s
= bfd_get_section_by_name (dynobj
, ".plt");
4317 BFD_ASSERT (s
!= NULL
);
4319 /* If this is the first .plt entry, make room for the special
4322 s
->size
+= elf_mn10300_sizeof_plt0 (info
);
4324 /* If this symbol is not defined in a regular file, and we are
4325 not generating a shared library, then set the symbol to this
4326 location in the .plt. This is required to make function
4327 pointers compare as equal between the normal executable and
4328 the shared library. */
4332 h
->root
.u
.def
.section
= s
;
4333 h
->root
.u
.def
.value
= s
->size
;
4336 h
->plt
.offset
= s
->size
;
4338 /* Make room for this entry. */
4339 s
->size
+= elf_mn10300_sizeof_plt (info
);
4341 /* We also need to make an entry in the .got.plt section, which
4342 will be placed in the .got section by the linker script. */
4343 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
4344 BFD_ASSERT (s
!= NULL
);
4347 /* We also need to make an entry in the .rela.plt section. */
4348 s
= bfd_get_section_by_name (dynobj
, ".rela.plt");
4349 BFD_ASSERT (s
!= NULL
);
4350 s
->size
+= sizeof (Elf32_External_Rela
);
4355 /* If this is a weak symbol, and there is a real definition, the
4356 processor independent code will have arranged for us to see the
4357 real definition first, and we can just use the same value. */
4358 if (h
->u
.weakdef
!= NULL
)
4360 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
4361 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
4362 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
4363 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
4367 /* This is a reference to a symbol defined by a dynamic object which
4368 is not a function. */
4370 /* If we are creating a shared library, we must presume that the
4371 only references to the symbol are via the global offset table.
4372 For such cases we need not do anything here; the relocations will
4373 be handled correctly by relocate_section. */
4377 /* If there are no references to this symbol that do not use the
4378 GOT, we don't need to generate a copy reloc. */
4379 if (!h
->non_got_ref
)
4384 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
4385 h
->root
.root
.string
);
4389 /* We must allocate the symbol in our .dynbss section, which will
4390 become part of the .bss section of the executable. There will be
4391 an entry for this symbol in the .dynsym section. The dynamic
4392 object will contain position independent code, so all references
4393 from the dynamic object to this symbol will go through the global
4394 offset table. The dynamic linker will use the .dynsym entry to
4395 determine the address it must put in the global offset table, so
4396 both the dynamic object and the regular object will refer to the
4397 same memory location for the variable. */
4399 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
4400 BFD_ASSERT (s
!= NULL
);
4402 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4403 copy the initial value out of the dynamic object and into the
4404 runtime process image. We need to remember the offset into the
4405 .rela.bss section we are going to use. */
4406 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
4410 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
4411 BFD_ASSERT (srel
!= NULL
);
4412 srel
->size
+= sizeof (Elf32_External_Rela
);
4416 return _bfd_elf_adjust_dynamic_copy (h
, s
);
4419 /* Set the sizes of the dynamic sections. */
4422 _bfd_mn10300_elf_size_dynamic_sections (bfd
* output_bfd
,
4423 struct bfd_link_info
* info
)
4429 bfd_boolean reltext
;
4431 dynobj
= elf_hash_table (info
)->dynobj
;
4432 BFD_ASSERT (dynobj
!= NULL
);
4434 if (elf_hash_table (info
)->dynamic_sections_created
)
4436 /* Set the contents of the .interp section to the interpreter. */
4437 if (info
->executable
)
4439 s
= bfd_get_section_by_name (dynobj
, ".interp");
4440 BFD_ASSERT (s
!= NULL
);
4441 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4442 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4447 /* We may have created entries in the .rela.got section.
4448 However, if we are not creating the dynamic sections, we will
4449 not actually use these entries. Reset the size of .rela.got,
4450 which will cause it to get stripped from the output file
4452 s
= bfd_get_section_by_name (dynobj
, ".rela.got");
4457 /* The check_relocs and adjust_dynamic_symbol entry points have
4458 determined the sizes of the various dynamic sections. Allocate
4463 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4467 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
4470 /* It's OK to base decisions on the section name, because none
4471 of the dynobj section names depend upon the input files. */
4472 name
= bfd_get_section_name (dynobj
, s
);
4474 if (streq (name
, ".plt"))
4476 /* Remember whether there is a PLT. */
4479 else if (CONST_STRNEQ (name
, ".rela"))
4485 /* Remember whether there are any reloc sections other
4487 if (! streq (name
, ".rela.plt"))
4489 const char * outname
;
4493 /* If this relocation section applies to a read only
4494 section, then we probably need a DT_TEXTREL
4495 entry. The entries in the .rela.plt section
4496 really apply to the .got section, which we
4497 created ourselves and so know is not readonly. */
4498 outname
= bfd_get_section_name (output_bfd
,
4500 target
= bfd_get_section_by_name (output_bfd
, outname
+ 5);
4502 && (target
->flags
& SEC_READONLY
) != 0
4503 && (target
->flags
& SEC_ALLOC
) != 0)
4507 /* We use the reloc_count field as a counter if we need
4508 to copy relocs into the output file. */
4512 else if (! CONST_STRNEQ (name
, ".got")
4513 && ! streq (name
, ".dynbss"))
4514 /* It's not one of our sections, so don't allocate space. */
4519 /* If we don't need this section, strip it from the
4520 output file. This is mostly to handle .rela.bss and
4521 .rela.plt. We must create both sections in
4522 create_dynamic_sections, because they must be created
4523 before the linker maps input sections to output
4524 sections. The linker does that before
4525 adjust_dynamic_symbol is called, and it is that
4526 function which decides whether anything needs to go
4527 into these sections. */
4528 s
->flags
|= SEC_EXCLUDE
;
4532 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
4535 /* Allocate memory for the section contents. We use bfd_zalloc
4536 here in case unused entries are not reclaimed before the
4537 section's contents are written out. This should not happen,
4538 but this way if it does, we get a R_MN10300_NONE reloc
4539 instead of garbage. */
4540 s
->contents
= bfd_zalloc (dynobj
, s
->size
);
4541 if (s
->contents
== NULL
)
4545 if (elf_hash_table (info
)->dynamic_sections_created
)
4547 /* Add some entries to the .dynamic section. We fill in the
4548 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4549 but we must add the entries now so that we get the correct
4550 size for the .dynamic section. The DT_DEBUG entry is filled
4551 in by the dynamic linker and used by the debugger. */
4554 if (!_bfd_elf_add_dynamic_entry (info
, DT_DEBUG
, 0))
4560 if (!_bfd_elf_add_dynamic_entry (info
, DT_PLTGOT
, 0)
4561 || !_bfd_elf_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
4562 || !_bfd_elf_add_dynamic_entry (info
, DT_PLTREL
, DT_RELA
)
4563 || !_bfd_elf_add_dynamic_entry (info
, DT_JMPREL
, 0))
4569 if (!_bfd_elf_add_dynamic_entry (info
, DT_RELA
, 0)
4570 || !_bfd_elf_add_dynamic_entry (info
, DT_RELASZ
, 0)
4571 || !_bfd_elf_add_dynamic_entry (info
, DT_RELAENT
,
4572 sizeof (Elf32_External_Rela
)))
4578 if (!_bfd_elf_add_dynamic_entry (info
, DT_TEXTREL
, 0))
4586 /* Finish up dynamic symbol handling. We set the contents of various
4587 dynamic sections here. */
4590 _bfd_mn10300_elf_finish_dynamic_symbol (bfd
* output_bfd
,
4591 struct bfd_link_info
* info
,
4592 struct elf_link_hash_entry
* h
,
4593 Elf_Internal_Sym
* sym
)
4597 dynobj
= elf_hash_table (info
)->dynobj
;
4599 if (h
->plt
.offset
!= (bfd_vma
) -1)
4606 Elf_Internal_Rela rel
;
4608 /* This symbol has an entry in the procedure linkage table. Set
4611 BFD_ASSERT (h
->dynindx
!= -1);
4613 splt
= bfd_get_section_by_name (dynobj
, ".plt");
4614 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
4615 srel
= bfd_get_section_by_name (dynobj
, ".rela.plt");
4616 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
4618 /* Get the index in the procedure linkage table which
4619 corresponds to this symbol. This is the index of this symbol
4620 in all the symbols for which we are making plt entries. The
4621 first entry in the procedure linkage table is reserved. */
4622 plt_index
= ((h
->plt
.offset
- elf_mn10300_sizeof_plt0 (info
))
4623 / elf_mn10300_sizeof_plt (info
));
4625 /* Get the offset into the .got table of the entry that
4626 corresponds to this function. Each .got entry is 4 bytes.
4627 The first three are reserved. */
4628 got_offset
= (plt_index
+ 3) * 4;
4630 /* Fill in the entry in the procedure linkage table. */
4633 memcpy (splt
->contents
+ h
->plt
.offset
, elf_mn10300_plt_entry
,
4634 elf_mn10300_sizeof_plt (info
));
4635 bfd_put_32 (output_bfd
,
4636 (sgot
->output_section
->vma
4637 + sgot
->output_offset
4639 (splt
->contents
+ h
->plt
.offset
4640 + elf_mn10300_plt_symbol_offset (info
)));
4642 bfd_put_32 (output_bfd
,
4643 (1 - h
->plt
.offset
- elf_mn10300_plt_plt0_offset (info
)),
4644 (splt
->contents
+ h
->plt
.offset
4645 + elf_mn10300_plt_plt0_offset (info
)));
4649 memcpy (splt
->contents
+ h
->plt
.offset
, elf_mn10300_pic_plt_entry
,
4650 elf_mn10300_sizeof_plt (info
));
4652 bfd_put_32 (output_bfd
, got_offset
,
4653 (splt
->contents
+ h
->plt
.offset
4654 + elf_mn10300_plt_symbol_offset (info
)));
4657 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rela
),
4658 (splt
->contents
+ h
->plt
.offset
4659 + elf_mn10300_plt_reloc_offset (info
)));
4661 /* Fill in the entry in the global offset table. */
4662 bfd_put_32 (output_bfd
,
4663 (splt
->output_section
->vma
4664 + splt
->output_offset
4666 + elf_mn10300_plt_temp_offset (info
)),
4667 sgot
->contents
+ got_offset
);
4669 /* Fill in the entry in the .rela.plt section. */
4670 rel
.r_offset
= (sgot
->output_section
->vma
4671 + sgot
->output_offset
4673 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_JMP_SLOT
);
4675 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4676 (bfd_byte
*) ((Elf32_External_Rela
*) srel
->contents
4679 if (!h
->def_regular
)
4680 /* Mark the symbol as undefined, rather than as defined in
4681 the .plt section. Leave the value alone. */
4682 sym
->st_shndx
= SHN_UNDEF
;
4685 if (h
->got
.offset
!= (bfd_vma
) -1)
4689 Elf_Internal_Rela rel
;
4691 /* This symbol has an entry in the global offset table. Set it up. */
4692 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4693 srel
= bfd_get_section_by_name (dynobj
, ".rela.got");
4694 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
4696 rel
.r_offset
= (sgot
->output_section
->vma
4697 + sgot
->output_offset
4698 + (h
->got
.offset
& ~1));
4700 /* If this is a -Bsymbolic link, and the symbol is defined
4701 locally, we just want to emit a RELATIVE reloc. Likewise if
4702 the symbol was forced to be local because of a version file.
4703 The entry in the global offset table will already have been
4704 initialized in the relocate_section function. */
4706 && (info
->symbolic
|| h
->dynindx
== -1)
4709 rel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
4710 rel
.r_addend
= (h
->root
.u
.def
.value
4711 + h
->root
.u
.def
.section
->output_section
->vma
4712 + h
->root
.u
.def
.section
->output_offset
);
4716 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
4717 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_GLOB_DAT
);
4721 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4722 (bfd_byte
*) ((Elf32_External_Rela
*) srel
->contents
4723 + srel
->reloc_count
));
4724 ++ srel
->reloc_count
;
4730 Elf_Internal_Rela rel
;
4732 /* This symbol needs a copy reloc. Set it up. */
4733 BFD_ASSERT (h
->dynindx
!= -1
4734 && (h
->root
.type
== bfd_link_hash_defined
4735 || h
->root
.type
== bfd_link_hash_defweak
));
4737 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
4739 BFD_ASSERT (s
!= NULL
);
4741 rel
.r_offset
= (h
->root
.u
.def
.value
4742 + h
->root
.u
.def
.section
->output_section
->vma
4743 + h
->root
.u
.def
.section
->output_offset
);
4744 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_COPY
);
4746 bfd_elf32_swap_reloca_out (output_bfd
, & rel
,
4747 (bfd_byte
*) ((Elf32_External_Rela
*) s
->contents
4752 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4753 if (streq (h
->root
.root
.string
, "_DYNAMIC")
4754 || h
== elf_hash_table (info
)->hgot
)
4755 sym
->st_shndx
= SHN_ABS
;
4760 /* Finish up the dynamic sections. */
4763 _bfd_mn10300_elf_finish_dynamic_sections (bfd
* output_bfd
,
4764 struct bfd_link_info
* info
)
4770 dynobj
= elf_hash_table (info
)->dynobj
;
4772 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
4773 BFD_ASSERT (sgot
!= NULL
);
4774 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
4776 if (elf_hash_table (info
)->dynamic_sections_created
)
4779 Elf32_External_Dyn
* dyncon
;
4780 Elf32_External_Dyn
* dynconend
;
4782 BFD_ASSERT (sdyn
!= NULL
);
4784 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
4785 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
4787 for (; dyncon
< dynconend
; dyncon
++)
4789 Elf_Internal_Dyn dyn
;
4793 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4807 s
= bfd_get_section_by_name (output_bfd
, name
);
4808 BFD_ASSERT (s
!= NULL
);
4809 dyn
.d_un
.d_ptr
= s
->vma
;
4810 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4814 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4815 BFD_ASSERT (s
!= NULL
);
4816 dyn
.d_un
.d_val
= s
->size
;
4817 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4821 /* My reading of the SVR4 ABI indicates that the
4822 procedure linkage table relocs (DT_JMPREL) should be
4823 included in the overall relocs (DT_RELA). This is
4824 what Solaris does. However, UnixWare can not handle
4825 that case. Therefore, we override the DT_RELASZ entry
4826 here to make it not include the JMPREL relocs. Since
4827 the linker script arranges for .rela.plt to follow all
4828 other relocation sections, we don't have to worry
4829 about changing the DT_RELA entry. */
4830 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4832 dyn
.d_un
.d_val
-= s
->size
;
4833 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4838 /* Fill in the first entry in the procedure linkage table. */
4839 splt
= bfd_get_section_by_name (dynobj
, ".plt");
4840 if (splt
&& splt
->size
> 0)
4844 memcpy (splt
->contents
, elf_mn10300_pic_plt_entry
,
4845 elf_mn10300_sizeof_plt (info
));
4849 memcpy (splt
->contents
, elf_mn10300_plt0_entry
, PLT0_ENTRY_SIZE
);
4850 bfd_put_32 (output_bfd
,
4851 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
4852 splt
->contents
+ elf_mn10300_plt0_gotid_offset (info
));
4853 bfd_put_32 (output_bfd
,
4854 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
4855 splt
->contents
+ elf_mn10300_plt0_linker_offset (info
));
4858 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4859 really seem like the right value. */
4860 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
4864 /* Fill in the first three entries in the global offset table. */
4868 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
4870 bfd_put_32 (output_bfd
,
4871 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4873 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
4874 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
4877 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
4882 /* Classify relocation types, such that combreloc can sort them
4885 static enum elf_reloc_type_class
4886 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela
*rela
)
4888 switch ((int) ELF32_R_TYPE (rela
->r_info
))
4890 case R_MN10300_RELATIVE
: return reloc_class_relative
;
4891 case R_MN10300_JMP_SLOT
: return reloc_class_plt
;
4892 case R_MN10300_COPY
: return reloc_class_copy
;
4893 default: return reloc_class_normal
;
4898 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4899 #define TARGET_LITTLE_NAME "elf32-mn10300"
4900 #define ELF_ARCH bfd_arch_mn10300
4901 #define ELF_MACHINE_CODE EM_MN10300
4902 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4903 #define ELF_MAXPAGESIZE 0x1000
4906 #define elf_info_to_howto mn10300_info_to_howto
4907 #define elf_info_to_howto_rel 0
4908 #define elf_backend_can_gc_sections 1
4909 #define elf_backend_rela_normal 1
4910 #define elf_backend_check_relocs mn10300_elf_check_relocs
4911 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4912 #define elf_backend_relocate_section mn10300_elf_relocate_section
4913 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4914 #define bfd_elf32_bfd_get_relocated_section_contents \
4915 mn10300_elf_get_relocated_section_contents
4916 #define bfd_elf32_bfd_link_hash_table_create \
4917 elf32_mn10300_link_hash_table_create
4918 #define bfd_elf32_bfd_link_hash_table_free \
4919 elf32_mn10300_link_hash_table_free
4921 #ifndef elf_symbol_leading_char
4922 #define elf_symbol_leading_char '_'
4925 /* So we can set bits in e_flags. */
4926 #define elf_backend_final_write_processing \
4927 _bfd_mn10300_elf_final_write_processing
4928 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4930 #define bfd_elf32_bfd_merge_private_bfd_data \
4931 _bfd_mn10300_elf_merge_private_bfd_data
4933 #define elf_backend_can_gc_sections 1
4934 #define elf_backend_create_dynamic_sections \
4935 _bfd_mn10300_elf_create_dynamic_sections
4936 #define elf_backend_adjust_dynamic_symbol \
4937 _bfd_mn10300_elf_adjust_dynamic_symbol
4938 #define elf_backend_size_dynamic_sections \
4939 _bfd_mn10300_elf_size_dynamic_sections
4940 #define elf_backend_omit_section_dynsym \
4941 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4942 #define elf_backend_finish_dynamic_symbol \
4943 _bfd_mn10300_elf_finish_dynamic_symbol
4944 #define elf_backend_finish_dynamic_sections \
4945 _bfd_mn10300_elf_finish_dynamic_sections
4947 #define elf_backend_reloc_type_class \
4948 _bfd_mn10300_elf_reloc_type_class
4950 #define elf_backend_want_got_plt 1
4951 #define elf_backend_plt_readonly 1
4952 #define elf_backend_want_plt_sym 0
4953 #define elf_backend_got_header_size 12
4955 #include "elf32-target.h"